Migrate icon settings
This commit is contained in:
@@ -16,14 +16,11 @@ class CalendarDynamicLauncherIcon(
|
||||
backgroundScale: Float,
|
||||
val packageName: String,
|
||||
val drawableIds: IntArray,
|
||||
autoGenerateBackgroundMode: Int
|
||||
) : DynamicLauncherIcon(
|
||||
foreground,
|
||||
background,
|
||||
foregroundScale,
|
||||
backgroundScale,
|
||||
/** Not needed, we already have a background **/
|
||||
autoGenerateBackgroundMode
|
||||
) {
|
||||
|
||||
var currentDay = 0
|
||||
@@ -39,7 +36,7 @@ class CalendarDynamicLauncherIcon(
|
||||
Executors.newSingleThreadExecutor().execute {
|
||||
val currentDayDrawable = resources.getDrawableOrNull(drawableIds[day - 1])
|
||||
?: return@execute
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && currentDayDrawable is AdaptiveIconDrawable) {
|
||||
if (currentDayDrawable is AdaptiveIconDrawable) {
|
||||
foreground = currentDayDrawable.foreground
|
||||
background = currentDayDrawable.background
|
||||
foregroundScale = 1.5f
|
||||
|
||||
@@ -23,9 +23,7 @@ class ClockDynamicLauncherIcon(
|
||||
foreground,
|
||||
background,
|
||||
foregroundScale,
|
||||
backgroundScale,
|
||||
/** Not needed, we already have a background **/
|
||||
LauncherIcon.BACKGROUND_WHITE
|
||||
backgroundScale
|
||||
) {
|
||||
|
||||
|
||||
|
||||
@@ -7,15 +7,13 @@ abstract class DynamicLauncherIcon(
|
||||
foreground: Drawable,
|
||||
background: Drawable?,
|
||||
foregroundScale: Float,
|
||||
backgroundScale: Float,
|
||||
autoGenerateBackgroundMode: Int
|
||||
backgroundScale: Float
|
||||
)
|
||||
: LauncherIcon(
|
||||
foreground,
|
||||
background,
|
||||
foregroundScale,
|
||||
backgroundScale,
|
||||
autoGenerateBackgroundMode
|
||||
) {
|
||||
|
||||
abstract fun update(context: Context)
|
||||
|
||||
@@ -28,24 +28,6 @@ private val SUPPORTED_GRAYSCALE_MAP_PROVIDERS = arrayOf(
|
||||
class IconPackManager(
|
||||
val context: Context
|
||||
) {
|
||||
var selectedIconPack: String
|
||||
get() {
|
||||
return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
|
||||
.getString(KEY_ICON_PACK, "")!!
|
||||
}
|
||||
set(value) {
|
||||
Log.d("MM20", "Selected icon pack: $value")
|
||||
context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
|
||||
.edit()
|
||||
.putString(KEY_ICON_PACK, value)
|
||||
.apply()
|
||||
}
|
||||
|
||||
|
||||
fun selectIconPack(iconPack: String) {
|
||||
selectedIconPack = iconPack
|
||||
}
|
||||
|
||||
suspend fun getInstalledIconPacks(): List<IconPack> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).iconDao().getInstalledIconPacks().map {
|
||||
|
||||
@@ -6,16 +6,17 @@ import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.util.LruCache
|
||||
import de.mm20.launcher2.icons.providers.*
|
||||
import de.mm20.launcher2.preferences.LauncherDataStore
|
||||
import de.mm20.launcher2.preferences.LauncherPreferences
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
class IconRepository(
|
||||
val context: Context,
|
||||
private val iconPackManager: IconPackManager,
|
||||
private val dynamicIconController: DynamicIconController,
|
||||
private val dataStore: LauncherDataStore
|
||||
) {
|
||||
|
||||
private val appReceiver = object : BroadcastReceiver() {
|
||||
@@ -28,7 +29,7 @@ class IconRepository(
|
||||
|
||||
private val cache = LruCache<String, LauncherIcon>(200)
|
||||
|
||||
private var iconProviders: List<IconProvider> = listOf()
|
||||
private var iconProviders: MutableStateFlow<List<IconProvider>> = MutableStateFlow(listOf())
|
||||
private lateinit var placeholderProvider: IconProvider
|
||||
|
||||
init {
|
||||
@@ -41,33 +42,64 @@ class IconRepository(
|
||||
addAction(Intent.ACTION_PACKAGE_CHANGED)
|
||||
addDataScheme("package")
|
||||
})
|
||||
recreate()
|
||||
|
||||
scope.launch {
|
||||
dataStore.data.map { it.icons }.distinctUntilChanged().collectLatest {
|
||||
val placeholderProvider = if (it.themedIcons) {
|
||||
ThemedPlaceholderIconProvider(context)
|
||||
} else {
|
||||
PlaceholderIconProvider(context)
|
||||
}
|
||||
val providers = mutableListOf<IconProvider>()
|
||||
|
||||
if (it.themedIcons) {
|
||||
providers.add(ThemedIconProvider(context))
|
||||
}
|
||||
|
||||
if (it.iconPack.isNotBlank()) {
|
||||
providers.add(IconPackIconProvider(context, it.iconPack, it.legacyIconBg))
|
||||
}
|
||||
providers.add(GoogleClockIconProvider(context))
|
||||
providers.add(CalendarIconProvider(context))
|
||||
providers.add(SystemIconProvider(context, it.legacyIconBg))
|
||||
providers.add(placeholderProvider)
|
||||
cache.evictAll()
|
||||
|
||||
this@IconRepository.placeholderProvider = placeholderProvider
|
||||
iconProviders.value = providers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun getIcon(searchable: Searchable, size: Int): Flow<LauncherIcon> = flow {
|
||||
var icon = cache.get(searchable.key)
|
||||
if (icon != null) {
|
||||
emit(icon)
|
||||
return@flow
|
||||
}
|
||||
fun getIcon(searchable: Searchable, size: Int): Flow<LauncherIcon> = channelFlow {
|
||||
iconProviders.collectLatest { providers ->
|
||||
var icon = cache.get(searchable.key)
|
||||
if (icon != null) {
|
||||
send(icon)
|
||||
return@collectLatest
|
||||
}
|
||||
|
||||
icon = placeholderProvider.getIcon(searchable, size)
|
||||
emit(icon)
|
||||
val placeholder = placeholderProvider.getIcon(searchable, size)
|
||||
placeholder?.let { send(it) }
|
||||
|
||||
for (provider in iconProviders) {
|
||||
val ic = provider.getIcon(searchable, size)
|
||||
if (ic != null) {
|
||||
icon = ic
|
||||
if (icon is DynamicLauncherIcon) {
|
||||
dynamicIconController.registerIcon(icon)
|
||||
for (provider in providers) {
|
||||
val ic = provider.getIcon(searchable, size)
|
||||
if (ic != null) {
|
||||
if (ic is DynamicLauncherIcon) {
|
||||
dynamicIconController.registerIcon(ic)
|
||||
}
|
||||
icon = ic
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
if (icon != null) {
|
||||
cache.put(searchable.key, icon)
|
||||
send(icon)
|
||||
} else {
|
||||
icon = placeholderProvider.getIcon(searchable, size)
|
||||
send(icon)
|
||||
}
|
||||
}
|
||||
if (icon != null) {
|
||||
cache.put(searchable.key, icon)
|
||||
emit(icon)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,27 +117,7 @@ class IconRepository(
|
||||
}
|
||||
}
|
||||
|
||||
fun recreate() {
|
||||
placeholderProvider = if (LauncherPreferences.instance.themedIcons) {
|
||||
ThemedPlaceholderIconProvider(context)
|
||||
} else {
|
||||
PlaceholderIconProvider(context)
|
||||
}
|
||||
val providers = mutableListOf<IconProvider>()
|
||||
|
||||
if (LauncherPreferences.instance.themedIcons) {
|
||||
providers.add(ThemedIconProvider(context))
|
||||
}
|
||||
|
||||
if (iconPackManager.selectedIconPack.isNotBlank()) {
|
||||
providers.add(IconPackIconProvider(context, iconPackManager.selectedIconPack))
|
||||
}
|
||||
providers.add(GoogleClockIconProvider(context))
|
||||
providers.add(CalendarIconProvider(context))
|
||||
providers.add(SystemIconProvider(context))
|
||||
providers.add(placeholderProvider)
|
||||
cache.evictAll()
|
||||
|
||||
iconProviders = providers
|
||||
suspend fun getInstalledIconPacks(): List<IconPack> {
|
||||
return iconPackManager.getInstalledIconPacks()
|
||||
}
|
||||
}
|
||||
@@ -6,5 +6,5 @@ import org.koin.dsl.module
|
||||
val iconsModule = module {
|
||||
single { DynamicIconController(androidContext()) }
|
||||
single { IconPackManager(androidContext()) }
|
||||
single { IconRepository(androidContext(), get(), get()) }
|
||||
single { IconRepository(androidContext(), get(), get(), get()) }
|
||||
}
|
||||
@@ -21,8 +21,6 @@ class ThemedCalendarDynamicLauncherIcon(
|
||||
background = background,
|
||||
foregroundScale = foregroundScale,
|
||||
backgroundScale = 1f,
|
||||
/** Not needed, we already have a background **/
|
||||
BACKGROUND_WHITE
|
||||
) {
|
||||
|
||||
var currentDay = 0
|
||||
|
||||
@@ -42,8 +42,7 @@ class CalendarIconProvider(val context: Context): IconProvider {
|
||||
foregroundScale = 1.5f,
|
||||
backgroundScale = 1.5f,
|
||||
packageName = component.packageName,
|
||||
drawableIds = drawableIds,
|
||||
autoGenerateBackgroundMode = LauncherPreferences.instance.legacyIconBg.toInt()
|
||||
drawableIds = drawableIds
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -17,13 +17,16 @@ import de.mm20.launcher2.database.AppDatabase
|
||||
import de.mm20.launcher2.icons.CalendarDynamicLauncherIcon
|
||||
import de.mm20.launcher2.icons.LauncherIcon
|
||||
import de.mm20.launcher2.ktx.randomElementOrNull
|
||||
import de.mm20.launcher2.preferences.IconShape
|
||||
import de.mm20.launcher2.preferences.LauncherPreferences
|
||||
import de.mm20.launcher2.preferences.Settings
|
||||
import de.mm20.launcher2.search.data.LauncherApp
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class IconPackIconProvider(val context: Context, val iconPack: String): IconProvider {
|
||||
class IconPackIconProvider(
|
||||
private val context: Context,
|
||||
private val iconPack: String,
|
||||
private val legacyIconBackground: Settings.IconSettings.LegacyIconBackground
|
||||
): IconProvider {
|
||||
override suspend fun getIcon(searchable: Searchable, size: Int): LauncherIcon? {
|
||||
if (searchable !is LauncherApp) return null
|
||||
val res = try {
|
||||
@@ -57,18 +60,14 @@ class IconPackIconProvider(val context: Context, val iconPack: String): IconProv
|
||||
LauncherIcon(
|
||||
foreground = drawable,
|
||||
foregroundScale = getScale(),
|
||||
autoGenerateBackgroundMode = LauncherPreferences.instance.legacyIconBg.toInt()
|
||||
autoGenerateBackgroundMode = legacyIconBackground.number
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getScale(): Float {
|
||||
return when (LauncherPreferences.instance.iconShape) {
|
||||
IconShape.CIRCLE, IconShape.PLATFORM_DEFAULT -> 0.7f
|
||||
else -> 0.8f
|
||||
|
||||
}
|
||||
return 0.7f
|
||||
}
|
||||
|
||||
private suspend fun generateIcon(
|
||||
@@ -151,7 +150,7 @@ class IconPackIconProvider(val context: Context, val iconPack: String): IconProv
|
||||
return LauncherIcon(
|
||||
foreground = BitmapDrawable(context.resources, bitmap),
|
||||
foregroundScale = getScale(),
|
||||
autoGenerateBackgroundMode = LauncherPreferences.instance.legacyIconBg.toInt()
|
||||
autoGenerateBackgroundMode = legacyIconBackground.number
|
||||
)
|
||||
}
|
||||
|
||||
@@ -200,7 +199,6 @@ class IconPackIconProvider(val context: Context, val iconPack: String): IconProv
|
||||
backgroundScale = 1.5f,
|
||||
packageName = iconPack,
|
||||
drawableIds = drawableIds,
|
||||
autoGenerateBackgroundMode = LauncherPreferences.instance.legacyIconBg.toInt()
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,14 @@ package de.mm20.launcher2.icons.providers
|
||||
|
||||
import android.content.Context
|
||||
import de.mm20.launcher2.icons.LauncherIcon
|
||||
import de.mm20.launcher2.preferences.Settings
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
|
||||
class SystemIconProvider(val context: Context) : IconProvider {
|
||||
class SystemIconProvider(
|
||||
private val context: Context,
|
||||
private val legacyIconBackground: Settings.IconSettings.LegacyIconBackground
|
||||
) : IconProvider {
|
||||
override suspend fun getIcon(searchable: Searchable, size: Int): LauncherIcon? {
|
||||
return searchable.loadIcon(context, size)
|
||||
return searchable.loadIcon(context, size, legacyIconBackground)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user