You can now import subtitle fonts :)

This commit is contained in:
Blatzar 2022-04-17 01:03:18 +02:00
parent d2beb5e253
commit f451b71b0b
3 changed files with 190 additions and 135 deletions

View file

@ -37,6 +37,7 @@ import com.lagradost.cloudstream3.utils.UIHelper.hideSystemUI
import com.lagradost.cloudstream3.utils.UIHelper.navigate import com.lagradost.cloudstream3.utils.UIHelper.navigate
import com.lagradost.cloudstream3.utils.UIHelper.popCurrentPage import com.lagradost.cloudstream3.utils.UIHelper.popCurrentPage
import kotlinx.android.synthetic.main.subtitle_settings.* import kotlinx.android.synthetic.main.subtitle_settings.*
import java.io.File
const val SUBTITLE_KEY = "subtitle_settings" const val SUBTITLE_KEY = "subtitle_settings"
const val SUBTITLE_AUTO_SELECT_KEY = "subs_auto_select" const val SUBTITLE_AUTO_SELECT_KEY = "subs_auto_select"
@ -51,6 +52,7 @@ data class SaveCaptionStyle(
@JsonProperty("edgeColor") var edgeColor: Int, @JsonProperty("edgeColor") var edgeColor: Int,
@FontRes @FontRes
@JsonProperty("typeface") var typeface: Int?, @JsonProperty("typeface") var typeface: Int?,
@JsonProperty("typefaceFilePath") var typefaceFilePath: String?,
/**in dp**/ /**in dp**/
@JsonProperty("elevation") var elevation: Int, @JsonProperty("elevation") var elevation: Int,
/**in sp**/ /**in sp**/
@ -64,17 +66,26 @@ class SubtitlesFragment : Fragment() {
val applyStyleEvent = Event<SaveCaptionStyle>() val applyStyleEvent = Event<SaveCaptionStyle>()
fun Context.fromSaveToStyle(data: SaveCaptionStyle): CaptionStyleCompat { fun Context.fromSaveToStyle(data: SaveCaptionStyle): CaptionStyleCompat {
val typeface = data.typeface
return CaptionStyleCompat( return CaptionStyleCompat(
data.foregroundColor, data.foregroundColor,
data.backgroundColor, data.backgroundColor,
data.windowColor, data.windowColor,
data.edgeType, data.edgeType,
data.edgeColor, data.edgeColor,
if (typeface == null) Typeface.SANS_SERIF else ResourcesCompat.getFont( data.typefaceFilePath?.let {
try {
// RuntimeException: Font asset not found
Typeface.createFromFile(File(it))
} catch (e: Exception) {
null
}
} ?: data.typeface?.let {
ResourcesCompat.getFont(
this, this,
typeface it
) )
}
?: Typeface.SANS_SERIF
) )
} }
@ -106,11 +117,24 @@ class SubtitlesFragment : Fragment() {
CaptionStyleCompat.EDGE_TYPE_OUTLINE, CaptionStyleCompat.EDGE_TYPE_OUTLINE,
getDefColor(1), getDefColor(1),
null, null,
null,
DEF_SUBS_ELEVATION, DEF_SUBS_ELEVATION,
null, null,
) )
} }
private fun Context.getSavedFonts(): List<File> {
val externalFiles = getExternalFilesDir(null) ?: return emptyList()
val fontDir = File(externalFiles.absolutePath + "/Fonts").also {
it.mkdir()
}
return fontDir.list()?.mapNotNull {
if (it.endsWith(".ttf")) {
File(fontDir.absolutePath + "/" + it)
} else null
} ?: listOf()
}
private fun Context.getCurrentStyle(): CaptionStyleCompat { private fun Context.getCurrentStyle(): CaptionStyleCompat {
return fromSaveToStyle(getCurrentSavedStyle()) return fromSaveToStyle(getCurrentSavedStyle())
} }
@ -191,6 +215,10 @@ class SubtitlesFragment : Fragment() {
hide = arguments?.getBoolean("hide") ?: true hide = arguments?.getBoolean("hide") ?: true
onColorSelectedEvent += ::onColorSelected onColorSelectedEvent += ::onColorSelected
onDialogDismissedEvent += ::onDialogDismissed onDialogDismissedEvent += ::onDialogDismissed
subs_import_text?.text = getString(R.string.subs_import_text).format(
context?.getExternalFilesDir(null)?.absolutePath.toString() + "/Fonts"
)
context?.fixPaddingStatusbar(subs_root) context?.fixPaddingStatusbar(subs_root)
@ -395,22 +423,38 @@ class SubtitlesFragment : Fragment() {
Pair(R.font.comic_sans, "Comic Sans"), Pair(R.font.comic_sans, "Comic Sans"),
Pair(R.font.poppins_regular, "Poppins"), Pair(R.font.poppins_regular, "Poppins"),
) )
val savedFontTypes = textView.context.getSavedFonts()
val currentIndex =
savedFontTypes.indexOfFirst { it.absolutePath == state.typefaceFilePath }
.let { index ->
if (index == -1)
fontTypes.indexOfFirst { it.first == state.typeface }
else index + fontTypes.size
}
//showBottomDialog //showBottomDialog
activity?.showDialog( activity?.showDialog(
fontTypes.map { it.second }, fontTypes.map { it.second } + savedFontTypes.map { it.name },
fontTypes.map { it.first }.indexOf(state.typeface), currentIndex,
(textView as TextView).text.toString(), (textView as TextView).text.toString(),
false, false,
dismissCallback dismissCallback
) { index -> ) { index ->
state.typeface = fontTypes.map { it.first }[index] if (index < fontTypes.size) {
state.typeface = fontTypes[index].first
state.typefaceFilePath = null
} else {
state.typefaceFilePath = savedFontTypes[index - fontTypes.size].absolutePath
state.typeface = null
}
textView.context.updateState() textView.context.updateState()
} }
} }
subs_font.setOnLongClickListener { textView -> subs_font.setOnLongClickListener { textView ->
state.typeface = null state.typeface = null
state.typefaceFilePath = null
textView.context.updateState() textView.context.updateState()
showToast(activity, R.string.subs_default_reset_toast, Toast.LENGTH_SHORT) showToast(activity, R.string.subs_default_reset_toast, Toast.LENGTH_SHORT)
return@setOnLongClickListener true return@setOnLongClickListener true

View file

@ -1,185 +1,195 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/subs_root"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/subs_root"
android:background="?attr/primaryBlackBackground"> android:background="?attr/primaryBlackBackground">
<LinearLayout <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:orientation="vertical">
<TextView <TextView
android:paddingStart="20dp" android:layout_width="match_parent"
android:paddingEnd="20dp" android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" android:layout_marginBottom="10dp"
android:textStyle="bold" android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/subtitles_settings" android:text="@string/subtitles_settings"
android:textSize="20sp"
android:textColor="?attr/textColor" android:textColor="?attr/textColor"
android:layout_width="match_parent" android:textSize="20sp"
android:layout_rowWeight="1" android:textStyle="bold" />
android:layout_height="wrap_content" />
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="75sp"> android:layout_height="75sp">
<ImageView <ImageView
android:scaleType="centerCrop"
android:src="@drawable/subtitles_preview_background"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:contentDescription="@string/preview_background_img_des" /> android:contentDescription="@string/preview_background_img_des"
android:scaleType="centerCrop"
android:src="@drawable/subtitles_preview_background" />
<com.google.android.exoplayer2.ui.SubtitleView <com.google.android.exoplayer2.ui.SubtitleView
android:id="@+id/subtitle_text" android:id="@+id/subtitle_text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" android:layout_gravity="center"
android:foregroundGravity="center" android:foregroundGravity="center" />
android:layout_height="match_parent" />
</FrameLayout> </FrameLayout>
<TextView <TextView
android:id="@+id/subs_font"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:nextFocusDown="@id/subs_font_size" android:nextFocusDown="@id/subs_font_size"
android:text="@string/subs_font" />
<TextView
android:id="@+id/subs_font_size"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_font"
android:text="@string/subs_font"
style="@style/SettingsItem" />
<TextView
android:nextFocusUp="@id/subs_font" android:nextFocusUp="@id/subs_font"
android:nextFocusDown="@id/subs_text_color" android:nextFocusDown="@id/subs_text_color"
android:text="@string/subs_font_size" />
<TextView
android:id="@+id/subs_text_color"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_font_size"
android:text="@string/subs_font_size"
style="@style/SettingsItem" />
<TextView
android:nextFocusUp="@id/subs_font_size" android:nextFocusUp="@id/subs_font_size"
android:nextFocusDown="@id/subs_outline_color" android:nextFocusDown="@id/subs_outline_color"
android:text="@string/subs_text_color" />
<TextView
android:id="@+id/subs_outline_color"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_text_color"
android:text="@string/subs_text_color"
style="@style/SettingsItem" />
<TextView
android:nextFocusUp="@id/subs_text_color" android:nextFocusUp="@id/subs_text_color"
android:nextFocusDown="@id/subs_background_color" android:nextFocusDown="@id/subs_background_color"
android:text="@string/subs_outline_color" />
<TextView
android:id="@+id/subs_background_color"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_outline_color"
android:text="@string/subs_outline_color"
style="@style/SettingsItem" />
<TextView
android:nextFocusUp="@id/subs_outline_color" android:nextFocusUp="@id/subs_outline_color"
android:nextFocusDown="@id/subs_window_color" android:nextFocusDown="@id/subs_window_color"
android:text="@string/subs_background_color" />
<TextView
android:id="@+id/subs_window_color"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_background_color"
android:text="@string/subs_background_color"
style="@style/SettingsItem" />
<TextView
android:nextFocusUp="@id/subs_background_color" android:nextFocusUp="@id/subs_background_color"
android:nextFocusDown="@id/subs_edge_type" android:nextFocusDown="@id/subs_edge_type"
android:text="@string/subs_window_color" />
<TextView
android:id="@+id/subs_edge_type"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_window_color"
android:text="@string/subs_window_color"
style="@style/SettingsItem" />
<TextView
android:nextFocusUp="@id/subs_window_color" android:nextFocusUp="@id/subs_window_color"
android:nextFocusDown="@id/subs_subtitle_elevation" android:nextFocusDown="@id/subs_subtitle_elevation"
android:text="@string/subs_edge_type" />
<TextView
android:id="@+id/subs_subtitle_elevation"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_edge_type"
android:text="@string/subs_edge_type"
style="@style/SettingsItem" />
<TextView
android:nextFocusUp="@id/subs_edge_type" android:nextFocusUp="@id/subs_edge_type"
android:nextFocusDown="@id/subs_auto_select_language" android:nextFocusDown="@id/subs_auto_select_language"
android:text="@string/subs_subtitle_elevation" />
<TextView
android:id="@+id/subs_auto_select_language"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_subtitle_elevation"
android:text="@string/subs_subtitle_elevation"
style="@style/SettingsItem" />
<TextView
android:nextFocusUp="@id/subs_subtitle_elevation" android:nextFocusUp="@id/subs_subtitle_elevation"
android:nextFocusDown="@id/subs_download_languages" android:nextFocusDown="@id/subs_download_languages"
android:text="@string/subs_auto_select_language" />
<TextView
android:id="@+id/subs_download_languages"
style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_auto_select_language"
android:text="@string/subs_auto_select_language"
style="@style/SettingsItem" />
<TextView
android:nextFocusUp="@id/subs_auto_select_language" android:nextFocusUp="@id/subs_auto_select_language"
android:nextFocusDown="@id/apply_btt" android:nextFocusDown="@id/apply_btt"
android:nextFocusLeft="@id/apply_btt" android:text="@string/subs_download_languages" />
android:nextFocusRight="@id/cancel_btt"
android:id="@+id/subs_download_languages"
android:text="@string/subs_download_languages"
style="@style/SettingsItem" />
<TextView <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:gravity="center" android:gravity="center"
android:text="@string/subs_hold_to_reset_to_default" android:text="@string/subs_hold_to_reset_to_default"
android:textSize="14sp"
android:textColor="?attr/textColor" android:textColor="?attr/textColor"
android:textSize="14sp" />
<TextView
android:id="@+id/subs_import_text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1" android:layout_rowWeight="1"
android:layout_height="wrap_content" /> android:gravity="center"
android:text="@string/subs_import_text"
android:textColor="?attr/textColor"
android:textSize="14sp" />
<LinearLayout <LinearLayout
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:gravity="bottom|end" android:gravity="bottom|end"
android:layout_width="match_parent" android:orientation="horizontal">
android:layout_height="60dp">
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:nextFocusUp="@id/subs_download_languages"
android:nextFocusRight="@id/cancel_btt"
style="@style/WhiteButton"
android:layout_gravity="center_vertical|end"
android:visibility="visible"
android:text="@string/sort_apply"
android:id="@+id/apply_btt" android:id="@+id/apply_btt"
android:layout_width="wrap_content"> style="@style/WhiteButton"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical|end"
android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_download_languages"
android:text="@string/sort_apply"
android:visibility="visible">
<requestFocus /> <requestFocus />
</com.google.android.material.button.MaterialButton> </com.google.android.material.button.MaterialButton>
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:nextFocusUp="@id/subs_download_languages"
android:nextFocusLeft="@id/apply_btt"
style="@style/BlackButton"
android:layout_gravity="center_vertical|end"
android:text="@string/sort_cancel"
android:id="@+id/cancel_btt" android:id="@+id/cancel_btt"
android:layout_width="wrap_content" /> style="@style/BlackButton"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical|end"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusUp="@id/subs_download_languages"
android:text="@string/sort_cancel" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View file

@ -175,6 +175,7 @@
<string name="subs_auto_select_language">Auto-Select Language</string> <string name="subs_auto_select_language">Auto-Select Language</string>
<string name="subs_download_languages">Download Languages</string> <string name="subs_download_languages">Download Languages</string>
<string name="subs_hold_to_reset_to_default">Hold to reset to default</string> <string name="subs_hold_to_reset_to_default">Hold to reset to default</string>
<string name="subs_import_text" formatted="true">Import fonts by placing them in %s</string>
<string name="continue_watching">Continue Watching</string> <string name="continue_watching">Continue Watching</string>
<string name="action_remove_watching">Remove</string> <string name="action_remove_watching">Remove</string>