Fix FontRequest thread leaks and UI bugs related to custom fonts.
This commit is contained in:
parent
b081b9adbb
commit
b3e2d8d843
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -63,21 +63,23 @@ object WidgetHelper {
|
||||
R.array.com_google_android_gms_fonts_certs
|
||||
)
|
||||
|
||||
val handlerThread = HandlerThread("generateView")
|
||||
val callback = object : FontsContractCompat.FontRequestCallback() {
|
||||
override fun onTypefaceRetrieved(typeface: Typeface) {
|
||||
handlerThread.quit()
|
||||
function.invoke(typeface)
|
||||
}
|
||||
|
||||
override fun onTypefaceRequestFailed(reason: Int) {
|
||||
handlerThread.quit();
|
||||
function.invoke(null)
|
||||
}
|
||||
}
|
||||
|
||||
val handlerThread = HandlerThread("generateView")
|
||||
handlerThread.start()
|
||||
if (Looper.myLooper() == null) {
|
||||
Looper.prepare()
|
||||
}
|
||||
//if (Looper.myLooper() == null) {
|
||||
// Looper.prepare()
|
||||
//}
|
||||
|
||||
Handler(handlerThread.looper).run {
|
||||
FontsContractCompat.requestFont(context, request, callback, this)
|
||||
|
@ -38,12 +38,15 @@ class CustomFontActivity : AppCompatActivity() {
|
||||
private lateinit var adapter: SlimAdapter
|
||||
private lateinit var viewModel: CustomFontViewModel
|
||||
private lateinit var binding: ActivityCustomFontBinding
|
||||
private lateinit var handlerThread: HandlerThread
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
viewModel = ViewModelProvider(this).get(CustomFontViewModel::class.java)
|
||||
binding = ActivityCustomFontBinding.inflate(layoutInflater)
|
||||
handlerThread = HandlerThread("listCustomFonts")
|
||||
handlerThread.start()
|
||||
|
||||
binding.listView.setHasFixedSize(true)
|
||||
val mLayoutManager = LinearLayoutManager(this)
|
||||
@ -64,14 +67,17 @@ class CustomFontActivity : AppCompatActivity() {
|
||||
injector
|
||||
.text(R.id.text, item)
|
||||
.with<TextView>(R.id.text) {
|
||||
val googleSans: Typeface = when (Preferences.customFontVariant) {
|
||||
"100" -> Typeface.createFromAsset(this.assets, "fonts/google_sans_thin.ttf")
|
||||
"200" -> Typeface.createFromAsset(this.assets, "fonts/google_sans_light.ttf")
|
||||
"500" -> Typeface.createFromAsset(this.assets, "fonts/google_sans_medium.ttf")
|
||||
"700" -> Typeface.createFromAsset(this.assets, "fonts/google_sans_bold.ttf")
|
||||
"800" -> Typeface.createFromAsset(this.assets, "fonts/google_sans_black.ttf")
|
||||
else -> Typeface.createFromAsset(this.assets, "fonts/google_sans_regular.ttf")
|
||||
}
|
||||
val googleSans: Typeface? = androidx.core.content.res.ResourcesCompat.getFont(
|
||||
this,
|
||||
when (Preferences.customFontVariant) {
|
||||
"100" -> R.font.google_sans_thin
|
||||
"200" -> R.font.google_sans_light
|
||||
"500" -> R.font.google_sans_medium
|
||||
"700" -> R.font.google_sans_bold
|
||||
"800" -> R.font.google_sans_black
|
||||
else -> R.font.google_sans_regular
|
||||
}
|
||||
)
|
||||
it.typeface = googleSans
|
||||
}
|
||||
|
||||
@ -97,32 +103,49 @@ class CustomFontActivity : AppCompatActivity() {
|
||||
)
|
||||
|
||||
|
||||
val callback = object : FontsContractCompat.FontRequestCallback() {
|
||||
override fun onTypefaceRetrieved(typeface: Typeface) {
|
||||
it.typeface = typeface
|
||||
it.isVisible = true
|
||||
class Callback : FontsContractCompat.FontRequestCallback() {
|
||||
var handler: Handler? = Handler(handlerThread.looper)
|
||||
|
||||
it.measure(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
fun cancel() {
|
||||
if (handler != null) {
|
||||
handler!!.removeCallbacksAndMessages(null)
|
||||
handler = null
|
||||
}
|
||||
}
|
||||
|
||||
protected fun finalize() {
|
||||
cancel()
|
||||
}
|
||||
|
||||
override fun onTypefaceRetrieved(typeface: Typeface) {
|
||||
if (it.tag == this) {
|
||||
it.tag = null
|
||||
it.typeface = typeface
|
||||
it.setTextColor(getColor(R.color.colorPrimaryText))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onTypefaceRequestFailed(reason: Int) {
|
||||
it.isVisible = false
|
||||
it.layoutParams = it.layoutParams.apply {
|
||||
height = 0
|
||||
if (it.tag == this) {
|
||||
it.tag = null
|
||||
//it.text = item.fontFamily + " ($reason)"
|
||||
it.setTextColor(getColor(R.color.errorColorText))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val handlerThread = HandlerThread(item.fontFamily)
|
||||
handlerThread.start()
|
||||
val mHandler = Handler(handlerThread.looper)
|
||||
(it.tag as Callback?)?.cancel()
|
||||
val callback = Callback()
|
||||
it.tag = callback
|
||||
it.typeface = null
|
||||
it.setTextColor(getColor(R.color.colorSecondaryText))
|
||||
|
||||
val mHandler = callback.handler!!
|
||||
FontsContractCompat.requestFont(this, request, callback, mHandler)
|
||||
}
|
||||
|
||||
injector.clicked(R.id.text) {
|
||||
if ((it as TextView).typeface == null) return@clicked
|
||||
val dialog = BottomSheetMenu<Int>(this, header = item.fontFamily)
|
||||
if (item.fontVariants.isEmpty()) {
|
||||
dialog.addItem(SettingsStringHelper.getVariantLabel(this, "regular"), -1)
|
||||
@ -147,6 +170,12 @@ class CustomFontActivity : AppCompatActivity() {
|
||||
setContentView(binding.root)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
handlerThread.quit()
|
||||
filterJob?.cancel()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private var filterJob: Job? = null
|
||||
|
||||
private fun subscribeUi(binding: ActivityCustomFontBinding, viewModel: CustomFontViewModel) {
|
||||
@ -204,6 +233,13 @@ class CustomFontActivity : AppCompatActivity() {
|
||||
adapter.updateData(filteredList)
|
||||
binding.loader.visibility = View.INVISIBLE
|
||||
}
|
||||
} else {
|
||||
delay(200)
|
||||
withContext(Dispatchers.Main) {
|
||||
adapter.updateData(listOf(getString(R.string.custom_font_subtitle_1)).filter {
|
||||
it.contains(search ?: "", ignoreCase = true)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -276,7 +276,7 @@ class TypographyFragment : Fragment() {
|
||||
Intent(requireContext(), CustomFontActivity::class.java),
|
||||
RequestCode.CUSTOM_FONT_CHOOSER_REQUEST_CODE.code
|
||||
)
|
||||
} else if (value != Constants.CUSTOM_FONT_DOWNLOADED) {
|
||||
} else if (value != Preferences.customFont) {
|
||||
Preferences.bulk {
|
||||
customFont = value
|
||||
customFontFile = ""
|
||||
|
@ -876,14 +876,17 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) {
|
||||
|
||||
// Custom Font
|
||||
if (Preferences.customFont == Constants.CUSTOM_FONT_GOOGLE_SANS) {
|
||||
val googleSans: Typeface = when (Preferences.customFontVariant) {
|
||||
"100" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_thin.ttf")
|
||||
"200" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_light.ttf")
|
||||
"500" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_medium.ttf")
|
||||
"700" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_bold.ttf")
|
||||
"800" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_black.ttf")
|
||||
else -> Typeface.createFromAsset(context.assets, "fonts/google_sans_regular.ttf")
|
||||
}
|
||||
val googleSans: Typeface? = androidx.core.content.res.ResourcesCompat.getFont(
|
||||
context,
|
||||
when (Preferences.customFontVariant) {
|
||||
"100" -> R.font.google_sans_thin
|
||||
"200" -> R.font.google_sans_light
|
||||
"500" -> R.font.google_sans_medium
|
||||
"700" -> R.font.google_sans_bold
|
||||
"800" -> R.font.google_sans_black
|
||||
else -> R.font.google_sans_regular
|
||||
}
|
||||
)
|
||||
|
||||
listOf<TextView>(
|
||||
bindingView.date,
|
||||
|
@ -919,14 +919,17 @@ class StandardWidget(val context: Context) {
|
||||
|
||||
// Custom Font
|
||||
if (Preferences.customFont == Constants.CUSTOM_FONT_GOOGLE_SANS) {
|
||||
val googleSans: Typeface = when (Preferences.customFontVariant) {
|
||||
"100" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_thin.ttf")
|
||||
"200" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_light.ttf")
|
||||
"500" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_medium.ttf")
|
||||
"700" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_bold.ttf")
|
||||
"800" -> Typeface.createFromAsset(context.assets, "fonts/google_sans_black.ttf")
|
||||
else -> Typeface.createFromAsset(context.assets, "fonts/google_sans_regular.ttf")
|
||||
}
|
||||
val googleSans: Typeface? = androidx.core.content.res.ResourcesCompat.getFont(
|
||||
context,
|
||||
when (Preferences.customFontVariant) {
|
||||
"100" -> R.font.google_sans_thin
|
||||
"200" -> R.font.google_sans_light
|
||||
"500" -> R.font.google_sans_medium
|
||||
"700" -> R.font.google_sans_bold
|
||||
"800" -> R.font.google_sans_black
|
||||
else -> R.font.google_sans_regular
|
||||
}
|
||||
)
|
||||
|
||||
listOf<TextView>(
|
||||
bindingView.date,
|
||||
|
@ -9,6 +9,14 @@
|
||||
app:fontStyle="italic"
|
||||
app:fontWeight="400"
|
||||
app:font="@font/google_sans_italic" />
|
||||
<font
|
||||
app:fontStyle="normal"
|
||||
app:fontWeight="100"
|
||||
app:font="@font/google_sans_thin" />
|
||||
<font
|
||||
app:fontStyle="italic"
|
||||
app:fontWeight="100"
|
||||
app:font="@font/google_sans_thin_italic" />
|
||||
<font
|
||||
app:fontStyle="normal"
|
||||
app:fontWeight="200"
|
||||
@ -19,34 +27,26 @@
|
||||
app:font="@font/google_sans_light_italic" />
|
||||
<font
|
||||
app:fontStyle="normal"
|
||||
app:fontWeight="300"
|
||||
app:font="@font/google_sans_thin" />
|
||||
<font
|
||||
app:fontStyle="italic"
|
||||
app:fontWeight="300"
|
||||
app:font="@font/google_sans_thin_italic" />
|
||||
<font
|
||||
app:fontStyle="normal"
|
||||
app:fontWeight="700"
|
||||
app:fontWeight="500"
|
||||
app:font="@font/google_sans_medium" />
|
||||
<font
|
||||
app:fontStyle="italic"
|
||||
app:fontWeight="700"
|
||||
app:fontWeight="500"
|
||||
app:font="@font/google_sans_medium_italic" />
|
||||
<font
|
||||
app:fontStyle="normal"
|
||||
app:fontWeight="800"
|
||||
app:fontWeight="700"
|
||||
app:font="@font/google_sans_bold" />
|
||||
<font
|
||||
app:fontStyle="italic"
|
||||
app:fontWeight="800"
|
||||
app:fontWeight="700"
|
||||
app:font="@font/google_sans_bold_italic" />
|
||||
<font
|
||||
app:fontStyle="normal"
|
||||
app:fontWeight="900"
|
||||
app:fontWeight="800"
|
||||
app:font="@font/google_sans_black" />
|
||||
<font
|
||||
app:fontStyle="italic"
|
||||
app:fontWeight="900"
|
||||
app:fontWeight="800"
|
||||
app:font="@font/google_sans_black_italic" />
|
||||
</font-family>
|
@ -139,10 +139,10 @@
|
||||
<item name="android:gravity">center_horizontal</item>
|
||||
</style>
|
||||
|
||||
<style name="AnotherWidget.Widget.Date" parent="AnotherWidget.Subtitle">
|
||||
<style name="AnotherWidget.Widget.Date" parent="AnotherWidget.Widget.Subtitle">
|
||||
</style>
|
||||
|
||||
<style name="AnotherWidget.Widget.Date.Big" parent="AnotherWidget.Title">
|
||||
<style name="AnotherWidget.Widget.Date.Big" parent="AnotherWidget.Widget.Title">
|
||||
<item name="android:textSize">22sp</item>
|
||||
</style>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user