Skip to content

Commit 223872a

Browse files
committed
fix: don't save font data as blob
1 parent 0133673 commit 223872a

File tree

5 files changed

+78
-60
lines changed

5 files changed

+78
-60
lines changed

commons/src/main/kotlin/org/fossify/commons/activities/CustomizationActivity.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ import org.fossify.commons.helpers.FontHelper
4545
import org.fossify.commons.helpers.MyContentProvider.COL_ACCENT_COLOR
4646
import org.fossify.commons.helpers.MyContentProvider.COL_APP_ICON_COLOR
4747
import org.fossify.commons.helpers.MyContentProvider.COL_BACKGROUND_COLOR
48-
import org.fossify.commons.helpers.MyContentProvider.COL_FONT_DATA
4948
import org.fossify.commons.helpers.MyContentProvider.COL_FONT_NAME
5049
import org.fossify.commons.helpers.MyContentProvider.COL_FONT_TYPE
5150
import org.fossify.commons.helpers.MyContentProvider.COL_PRIMARY_COLOR
5251
import org.fossify.commons.helpers.MyContentProvider.COL_TEXT_COLOR
5352
import org.fossify.commons.helpers.MyContentProvider.COL_THEME_TYPE
53+
import org.fossify.commons.helpers.MyContentProvider.FONTS_URI
5454
import org.fossify.commons.helpers.MyContentProvider.GLOBAL_THEME_CUSTOM
5555
import org.fossify.commons.helpers.MyContentProvider.GLOBAL_THEME_DISABLED
5656
import org.fossify.commons.helpers.MyContentProvider.GLOBAL_THEME_SYSTEM
@@ -488,13 +488,21 @@ class CustomizationActivity : BaseSimpleActivity() {
488488
put(COL_APP_ICON_COLOR, curAppIconColor)
489489
put(COL_FONT_TYPE, curFontType)
490490
put(COL_FONT_NAME, curFontFileName)
491-
if (curFontType == FONT_TYPE_CUSTOM && curFontFileName.isNotEmpty()) {
492-
FontHelper.getFontData(this@CustomizationActivity, curFontFileName)?.let {
493-
put(COL_FONT_DATA, it)
494-
}
495-
}
496491
}
497492
)
493+
494+
if (curFontType == FONT_TYPE_CUSTOM && curFontFileName.isNotEmpty()) {
495+
FontHelper.getFontData(this, curFontFileName)?.let { fontData ->
496+
val fontUri = FONTS_URI.buildUpon()
497+
.appendPath(curFontFileName)
498+
.build()
499+
try {
500+
contentResolver.openOutputStream(fontUri, "w")
501+
?.use { it.write(fontData) }
502+
} catch (_: Exception) {
503+
}
504+
}
505+
}
498506
}
499507

500508
hasUnsavedChanges = false
@@ -560,7 +568,7 @@ class CustomizationActivity : BaseSimpleActivity() {
560568
}
561569
}
562570
}
563-
571+
564572
setupFontPicker()
565573
}
566574

commons/src/main/kotlin/org/fossify/commons/extensions/Context-styling.kt

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,50 @@ import android.annotation.SuppressLint
44
import android.content.ComponentName
55
import android.content.Context
66
import android.content.pm.PackageManager
7+
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED
8+
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED
79
import android.content.res.Configuration
810
import android.graphics.Color
911
import android.view.ViewGroup
1012
import androidx.loader.content.CursorLoader
1113
import com.google.android.material.color.MaterialColors
1214
import org.fossify.commons.R
13-
import org.fossify.commons.helpers.*
15+
import org.fossify.commons.helpers.DARK_GREY
16+
import org.fossify.commons.helpers.FONT_TYPE_CUSTOM
17+
import org.fossify.commons.helpers.FontHelper
18+
import org.fossify.commons.helpers.MyContentProvider
1419
import org.fossify.commons.helpers.MyContentProvider.GLOBAL_THEME_SYSTEM
20+
import org.fossify.commons.helpers.appIconColorStrings
21+
import org.fossify.commons.helpers.ensureBackgroundThread
22+
import org.fossify.commons.helpers.isSPlus
1523
import org.fossify.commons.models.GlobalConfig
1624
import org.fossify.commons.models.isGlobalThemingEnabled
17-
import org.fossify.commons.views.*
25+
import org.fossify.commons.views.MyAppCompatCheckbox
26+
import org.fossify.commons.views.MyAppCompatSpinner
27+
import org.fossify.commons.views.MyAutoCompleteTextView
28+
import org.fossify.commons.views.MyButton
29+
import org.fossify.commons.views.MyCompatRadioButton
30+
import org.fossify.commons.views.MyEditText
31+
import org.fossify.commons.views.MyFloatingActionButton
32+
import org.fossify.commons.views.MyMaterialSwitch
33+
import org.fossify.commons.views.MySeekBar
34+
import org.fossify.commons.views.MyTextInputLayout
35+
import org.fossify.commons.views.MyTextView
36+
import java.io.File
1837

1938
fun Context.isDynamicTheme() = isSPlus() && baseConfig.isSystemThemeEnabled
2039

21-
fun Context.isBlackAndWhiteTheme() = baseConfig.textColor == Color.WHITE && baseConfig.primaryColor == Color.BLACK && baseConfig.backgroundColor == Color.BLACK
40+
fun Context.isBlackAndWhiteTheme(): Boolean {
41+
return baseConfig.textColor == Color.WHITE
42+
&& baseConfig.primaryColor == Color.BLACK
43+
&& baseConfig.backgroundColor == Color.BLACK
44+
}
2245

23-
fun Context.isWhiteTheme() = baseConfig.textColor == DARK_GREY && baseConfig.primaryColor == Color.WHITE && baseConfig.backgroundColor == Color.WHITE
46+
fun Context.isWhiteTheme(): Boolean {
47+
return baseConfig.textColor == DARK_GREY
48+
&& baseConfig.primaryColor == Color.WHITE
49+
&& baseConfig.backgroundColor == Color.WHITE
50+
}
2451

2552
fun Context.isSystemInDarkMode() = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_YES != 0
2653

@@ -134,12 +161,8 @@ fun Context.syncGlobalConfig(callback: (() -> Unit)? = null) {
134161
if (it.fontType >= 0 && (fontType != it.fontType || fontName != it.fontName)) {
135162
fontType = it.fontType
136163
fontName = it.fontName
137-
if (it.fontType == FONT_TYPE_CUSTOM && it.fontData != null) {
138-
FontHelper.saveFontData(
139-
context = this@syncGlobalConfig,
140-
fontData = it.fontData,
141-
fileName = it.fontName
142-
)
164+
if (it.fontType == FONT_TYPE_CUSTOM && it.fontName.isNotEmpty()) {
165+
ensureFontPresentLocally(it.fontName)
143166
}
144167
FontHelper.clearCache()
145168
}
@@ -187,8 +210,7 @@ fun Context.getGlobalConfig(cursorLoader: CursorLoader): GlobalConfig? {
187210
showCheckmarksOnSwitches = cursor.getIntValue(MyContentProvider.COL_SHOW_CHECKMARKS_ON_SWITCHES) != 0,
188211
lastUpdatedTS = cursor.getIntValue(MyContentProvider.COL_LAST_UPDATED_TS),
189212
fontType = cursor.getIntValueOr(MyContentProvider.COL_FONT_TYPE, -1),
190-
fontName = cursor.getStringValueOr(MyContentProvider.COL_FONT_NAME, ""),
191-
fontData = cursor.getBlobValueOrNull(MyContentProvider.COL_FONT_DATA)
213+
fontName = cursor.getStringValueOr(MyContentProvider.COL_FONT_NAME, "")
192214
)
193215
} catch (_: Exception) {
194216
}
@@ -197,6 +219,26 @@ fun Context.getGlobalConfig(cursorLoader: CursorLoader): GlobalConfig? {
197219
return null
198220
}
199221

222+
fun Context.ensureFontPresentLocally(fontName: String): Boolean {
223+
if (fontName.isEmpty()) return false
224+
val localFile = File(FontHelper.getFontsDir(this), fontName)
225+
if (localFile.exists()) return true
226+
227+
val fontUri = MyContentProvider.FONTS_URI.buildUpon()
228+
.appendPath(fontName)
229+
.build()
230+
231+
return try {
232+
contentResolver.openInputStream(fontUri)?.use { input ->
233+
localFile.outputStream().use { output ->
234+
input.copyTo(output)
235+
}
236+
} != null
237+
} catch (_: Exception) {
238+
false
239+
}
240+
}
241+
200242
fun Context.checkAppIconColor() {
201243
val appId = baseConfig.appId
202244
if (appId.isNotEmpty() && baseConfig.lastIconColor != baseConfig.appIconColor) {
@@ -214,7 +256,7 @@ fun Context.checkAppIconColor() {
214256

215257
fun Context.toggleAppIconColor(appId: String, colorIndex: Int, color: Int, enable: Boolean) {
216258
val className = "${appId.removeSuffix(".debug")}.activities.SplashActivity${appIconColorStrings[colorIndex]}"
217-
val state = if (enable) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED
259+
val state = if (enable) COMPONENT_ENABLED_STATE_ENABLED else COMPONENT_ENABLED_STATE_DISABLED
218260
try {
219261
packageManager.setComponentEnabledSetting(ComponentName(appId, className), state, PackageManager.DONT_KILL_APP)
220262
if (enable) {

commons/src/main/kotlin/org/fossify/commons/helpers/FontHelper.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.fossify.commons.helpers
33
import android.content.Context
44
import android.graphics.Typeface
55
import org.fossify.commons.extensions.baseConfig
6+
import org.fossify.commons.extensions.ensureFontPresentLocally
67
import java.io.File
78

89
/**
@@ -34,8 +35,12 @@ object FontHelper {
3435

3536
private fun loadCustomFont(context: Context, fileName: String): Typeface {
3637
if (fileName.isEmpty()) return Typeface.DEFAULT
38+
val fontFile = File(getFontsDir(context), fileName)
39+
if (!fontFile.exists()) {
40+
context.ensureFontPresentLocally(fileName)
41+
}
42+
3743
return try {
38-
val fontFile = File(getFontsDir(context), fileName)
3944
if (fontFile.exists()) Typeface.createFromFile(fontFile) else Typeface.DEFAULT
4045
} catch (_: Exception) {
4146
Typeface.DEFAULT

commons/src/main/kotlin/org/fossify/commons/helpers/MyContentProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.core.net.toUri
66
object MyContentProvider {
77
private const val AUTHORITY = "org.fossify.android.provider"
88
val MY_CONTENT_URI: Uri = "content://$AUTHORITY/settings".toUri()
9+
val FONTS_URI: Uri = "content://$AUTHORITY/fonts".toUri()
910

1011
const val ACTION_GLOBAL_CONFIG_UPDATED = "org.fossify.android.GLOBAL_CONFIG_UPDATED"
1112
const val PERMISSION_WRITE_GLOBAL_SETTINGS = "org.fossify.android.permission.WRITE_GLOBAL_SETTINGS"
@@ -25,7 +26,6 @@ object MyContentProvider {
2526
// Font customization
2627
const val COL_FONT_TYPE = "font_type"
2728
const val COL_FONT_NAME = "font_name"
28-
const val COL_FONT_DATA = "font_data"
2929

3030
const val GLOBAL_THEME_DISABLED = 0
3131
const val GLOBAL_THEME_SYSTEM = 1

commons/src/main/kotlin/org/fossify/commons/models/GlobalConfig.kt

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,7 @@ data class GlobalConfig(
1414
val lastUpdatedTS: Int = 0,
1515
val fontType: Int = FONT_TYPE_SYSTEM_DEFAULT,
1616
val fontName: String = "",
17-
val fontData: ByteArray? = null,
18-
) {
19-
override fun equals(other: Any?): Boolean {
20-
if (this === other) return true
21-
if (javaClass != other?.javaClass) return false
22-
23-
other as GlobalConfig
24-
25-
if (themeType != other.themeType) return false
26-
if (textColor != other.textColor) return false
27-
if (backgroundColor != other.backgroundColor) return false
28-
if (primaryColor != other.primaryColor) return false
29-
if (accentColor != other.accentColor) return false
30-
if (appIconColor != other.appIconColor) return false
31-
if (showCheckmarksOnSwitches != other.showCheckmarksOnSwitches) return false
32-
if (lastUpdatedTS != other.lastUpdatedTS) return false
33-
if (fontType != other.fontType) return false
34-
if (fontName != other.fontName) return false
35-
if (!fontData.contentEquals(other.fontData)) return false
36-
37-
return true
38-
}
39-
40-
override fun hashCode(): Int {
41-
var result = themeType
42-
result = 31 * result + textColor
43-
result = 31 * result + backgroundColor
44-
result = 31 * result + primaryColor
45-
result = 31 * result + accentColor
46-
result = 31 * result + appIconColor
47-
result = 31 * result + showCheckmarksOnSwitches.hashCode()
48-
result = 31 * result + lastUpdatedTS
49-
result = 31 * result + fontType
50-
result = 31 * result + fontName.hashCode()
51-
result = 31 * result + (fontData?.contentHashCode() ?: 0)
52-
return result
53-
}
54-
}
17+
)
5518

5619
fun GlobalConfig?.isGlobalThemingEnabled(): Boolean {
5720
return this != null && themeType != MyContentProvider.GLOBAL_THEME_DISABLED

0 commit comments

Comments
 (0)