@@ -4,23 +4,50 @@ import android.annotation.SuppressLint
44import android.content.ComponentName
55import android.content.Context
66import android.content.pm.PackageManager
7+ import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED
8+ import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED
79import android.content.res.Configuration
810import android.graphics.Color
911import android.view.ViewGroup
1012import androidx.loader.content.CursorLoader
1113import com.google.android.material.color.MaterialColors
1214import 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
1419import 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
1523import org.fossify.commons.models.GlobalConfig
1624import 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
1938fun 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
2552fun 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+
200242fun Context.checkAppIconColor () {
201243 val appId = baseConfig.appId
202244 if (appId.isNotEmpty() && baseConfig.lastIconColor != baseConfig.appIconColor) {
@@ -214,7 +256,7 @@ fun Context.checkAppIconColor() {
214256
215257fun 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) {
0 commit comments