Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.os.UserManager
import android.provider.BaseColumns
import android.provider.BlockedNumberContract.BlockedNumbers
import android.provider.ContactsContract.CommonDataKinds.BaseTypes
Expand Down Expand Up @@ -146,6 +147,9 @@ val Context.areSystemAnimationsEnabled: Boolean get() = Settings.Global.getFloat
val Context.appLockManager
get() = AppLockManager.getInstance(applicationContext as Application)

val Context.isCredentialStorageAvailable: Boolean
get() = getSystemService(UserManager::class.java)?.isUserUnlocked ?: true

fun Context.toast(id: Int, length: Int = Toast.LENGTH_SHORT) {
toast(getString(id), length)
}
Expand Down Expand Up @@ -1373,24 +1377,28 @@ fun Context.formatWithDeprecatedBadge(

fun Context.applyFontToTextView(
textView: TextView,
typeface: Typeface = FontHelper.getTypeface(this),
typeface: Typeface? = null,
force: Boolean = false
) {
if (typeface == Typeface.DEFAULT && !force) return // avoid unnecessary calls and overwrites
if (typeface == null && !isCredentialStorageAvailable) return
val actualTypeface = typeface ?: FontHelper.getTypeface(this)
if (actualTypeface == Typeface.DEFAULT && !force) return // avoid unnecessary calls and overwrites
val existingStyle = textView.typeface?.style ?: Typeface.NORMAL
textView.setTypeface(typeface, existingStyle)
textView.setTypeface(actualTypeface, existingStyle)
}

fun Context.applyFontToViewRecursively(
view: View?,
typeface: Typeface = FontHelper.getTypeface(this),
typeface: Typeface? = null,
force: Boolean = false
) {
if (view == null) return
if (view is TextView) applyFontToTextView(view, typeface, force)
if (typeface == null && !isCredentialStorageAvailable) return
val actualTypeface = typeface ?: FontHelper.getTypeface(this)
if (view is TextView) applyFontToTextView(view, actualTypeface, force)
if (view is ViewGroup) {
for (i in 0 until view.childCount) {
applyFontToViewRecursively(view.getChildAt(i), typeface, force)
applyFontToViewRecursively(view.getChildAt(i), actualTypeface, force)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.Typeface
import org.fossify.commons.extensions.baseConfig
import org.fossify.commons.extensions.ensureFontPresentLocally
import org.fossify.commons.extensions.isCredentialStorageAvailable
import java.io.File

/**
Expand All @@ -16,18 +17,21 @@ object FontHelper {

fun getTypeface(
context: Context,
fontType: Int = context.baseConfig.fontType,
fontFileName: String = context.baseConfig.fontName
fontType: Int? = null,
fontFileName: String? = null
): Typeface {
if (fontType == cachedFontType && fontFileName == cachedFontFileName && cachedTypeface != null) {
if (!context.isCredentialStorageAvailable) return Typeface.DEFAULT
val actualFontType = fontType ?: context.baseConfig.fontType
val actualFontFileName = fontFileName ?: context.baseConfig.fontName
if (actualFontType == cachedFontType && actualFontFileName == cachedFontFileName && cachedTypeface != null) {
return cachedTypeface!!
}

cachedFontType = fontType
cachedFontFileName = fontFileName
cachedTypeface = when (fontType) {
cachedFontType = actualFontType
cachedFontFileName = actualFontFileName
cachedTypeface = when (actualFontType) {
FONT_TYPE_MONOSPACE -> Typeface.MONOSPACE
FONT_TYPE_CUSTOM -> loadCustomFont(context, fontFileName)
FONT_TYPE_CUSTOM -> loadCustomFont(context, actualFontFileName)
else -> Typeface.DEFAULT
}
return cachedTypeface!!
Expand Down
Loading