Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LiveInformation features for preference defaults #5184

Draft
wants to merge 1 commit into
base: 15-dev
Choose a base branch
from
Draft
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
130 changes: 105 additions & 25 deletions lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ import com.android.launcher3.InvariantDeviceProfile.INDEX_DEFAULT
import com.android.launcher3.LauncherAppState
import com.android.launcher3.R
import com.android.launcher3.graphics.IconShape as L3IconShape
import android.util.Log
import app.lawnchair.ui.preferences.data.liveinfo.LiveInformationManager
import app.lawnchair.ui.preferences.data.liveinfo.model.LiveInformation
import com.android.launcher3.util.DynamicResource
import com.android.launcher3.util.MainThreadInitializedObject
import com.android.launcher3.util.SafeCloseable
import com.patrykmichalik.opto.core.PreferenceImpl
import com.patrykmichalik.opto.core.PreferenceManager
import com.patrykmichalik.opto.core.firstBlocking
import com.patrykmichalik.opto.core.setBlocking
Expand All @@ -61,14 +65,15 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.serialization.encodeToString

class PreferenceManager2 private constructor(private val context: Context) :
PreferenceManager,
SafeCloseable {

private val scope = MainScope()
private val resourceProvider = DynamicResource.provider(context)
private val liveInformationManager = LiveInformationManager.getInstance(context)
private var liveInformation: LiveInformation? = null

private fun idpPreference(
key: Preferences.Key<Int>,
Expand All @@ -91,23 +96,41 @@ class PreferenceManager2 private constructor(private val context: Context) :

val hotseatMode = preference(
key = stringPreferencesKey("hotseat_mode"),
defaultValue = HotseatMode.fromString(context.getString(R.string.config_default_hotseat_mode)),
defaultValue = HotseatMode.fromString(
getRemoteDefault("hotseat_mode")
?: context.getString(R.string.config_default_hotseat_mode),
),
parse = { HotseatMode.fromString(it) },
save = { it.toString() },
onSet = { reloadHelper.restart() },
)

val iconShape = preference(
key = stringPreferencesKey(name = "icon_shape"),
defaultValue = IconShape.fromString(value = context.getString(R.string.config_default_icon_shape), context = context) ?: IconShape.Circle,
parse = { IconShape.fromString(value = it, context = context) ?: IconShapeManager.getSystemIconShape(context) },
defaultValue = IconShape.fromString(
value = getRemoteDefault("icon_shape")
?: context.getString(R.string.config_default_icon_shape),
context = context,
) ?: IconShape.Circle,
parse = {
IconShape.fromString(value = it, context = context)
?: IconShapeManager.getSystemIconShape(context)
},
save = { it.toString() },
)

val customIconShape = preference(
val customIconShape: PreferenceImpl<IconShape?, String> = preference(
key = stringPreferencesKey(name = "custom_icon_shape"),
defaultValue = null,
parse = { IconShape.fromString(value = it, context = context) ?: IconShapeManager.getSystemIconShape(context) },
defaultValue = getRemoteDefault("custom_icon_shape")?.let {
IconShape.fromString(
value = it,
context = context,
)
},
parse = {
IconShape.fromString(value = it, context = context)
?: IconShapeManager.getSystemIconShape(context)
},
save = { it.toString() },
onSet = { it?.let(iconShape::setBlocking) },
)
Expand All @@ -119,7 +142,9 @@ class PreferenceManager2 private constructor(private val context: Context) :

val colorStyle = preference(
key = stringPreferencesKey("color_style"),
defaultValue = ColorStyle.fromString("tonal_spot"),
defaultValue = ColorStyle.fromString(
getRemoteDefault("color_style") ?: "tonal_spot",
),
parse = ColorStyle::fromString,
save = ColorStyle::toString,
onSet = { reloadHelper.restart() },
Expand All @@ -130,47 +155,65 @@ class PreferenceManager2 private constructor(private val context: Context) :
parse = ColorOption::fromString,
save = ColorOption::toString,
onSet = { reloadHelper.restart() },
defaultValue = ColorOption.fromString(context.getString(R.string.config_default_accent_color)),
defaultValue = ColorOption.fromString(
getRemoteDefault("stroke_color")
?: context.getString(R.string.config_default_accent_color),
),
)

val hotseatBackgroundColor = preference(
key = stringPreferencesKey(name = "hotseat_bg_color"),
parse = ColorOption::fromString,
save = ColorOption::toString,
onSet = { reloadHelper.reloadGrid() },
defaultValue = ColorOption.fromString(context.getString(R.string.config_default_hotseat_bg_color)),
defaultValue = ColorOption.fromString(
getRemoteDefault("hotseat_bg_color")
?: context.getString(R.string.config_default_hotseat_bg_color),
),
)

val appDrawerBackgroundColor = preference(
key = stringPreferencesKey(name = "app_drawer_bg_color"),
parse = ColorOption::fromString,
save = ColorOption::toString,
onSet = { reloadHelper.recreate() },
defaultValue = ColorOption.fromString(context.getString(R.string.config_default_app_drawer_bg_color)),
defaultValue = ColorOption.fromString(
getRemoteDefault("app_drawer_bg_color")
?: context.getString(R.string.config_default_app_drawer_bg_color),
),
)

val notificationDotColor = preference(
key = stringPreferencesKey(name = "notification_dot_color"),
parse = ColorOption::fromString,
save = ColorOption::toString,
onSet = { reloadHelper.reloadGrid() },
defaultValue = ColorOption.fromString(context.getString(R.string.config_default_notification_dot_color)),
defaultValue = ColorOption.fromString(
getRemoteDefault("notification_dot_color")
?: context.getString(R.string.config_default_notification_dot_color),
),
)

val notificationDotTextColor = preference(
key = stringPreferencesKey(name = "notification_dot_text_color"),
parse = ColorOption::fromString,
save = ColorOption::toString,
onSet = { reloadHelper.reloadGrid() },
defaultValue = ColorOption.fromString(context.getString(R.string.config_default_notification_dot_text_color)),
defaultValue = ColorOption.fromString(
getRemoteDefault("notification_dot_text_color")
?: context.getString(R.string.config_default_notification_dot_text_color),
),
)

val folderColor = preference(
key = stringPreferencesKey(name = "folder_color"),
parse = ColorOption::fromString,
save = ColorOption::toString,
onSet = { reloadHelper.reloadGrid() },
defaultValue = ColorOption.fromString(context.getString(R.string.config_default_folder_color)),
defaultValue = ColorOption.fromString(
getRemoteDefault("folder_color")
?: context.getString(R.string.config_default_folder_color),
),
)

val showNotificationCount = preference(
Expand All @@ -195,7 +238,9 @@ class PreferenceManager2 private constructor(private val context: Context) :

val hotseatQsbProvider = preference(
key = stringPreferencesKey(name = "dock_search_bar_provider"),
defaultValue = QsbSearchProvider.resolveDefault(context),
defaultValue = getRemoteDefault("dock_search_bar_provider")?.let {
QsbSearchProvider.fromId(it)
} ?: QsbSearchProvider.resolveDefault(context),
parse = { QsbSearchProvider.fromId(it) },
save = { it.id },
onSet = { reloadHelper.recreate() },
Expand All @@ -211,7 +256,10 @@ class PreferenceManager2 private constructor(private val context: Context) :
parse = ColorOption::fromString,
save = ColorOption::toString,
onSet = { reloadHelper.recreate() },
defaultValue = ColorOption.fromString(context.getString(R.string.config_default_accent_color)),
defaultValue = ColorOption.fromString(
getRemoteDefault("accent_color")
?: context.getString(R.string.config_default_accent_color),
),
)

val hiddenApps = preference(
Expand Down Expand Up @@ -287,7 +335,8 @@ class PreferenceManager2 private constructor(private val context: Context) :

val launcherPopupOrder = preference(
key = stringPreferencesKey(name = "launcher_popup_order"),
defaultValue = LauncherOptionsPopup.DEFAULT_ORDER,
defaultValue = getRemoteDefault("launcher_popup_order")
?: LauncherOptionsPopup.DEFAULT_ORDER,
onSet = { reloadHelper.reloadGrid() },
)

Expand Down Expand Up @@ -327,13 +376,14 @@ class PreferenceManager2 private constructor(private val context: Context) :

val hiddenAppsInSearch = preference(
key = stringPreferencesKey(name = "hidden_apps_in_search"),
defaultValue = HiddenAppsInSearch.NEVER,
defaultValue = getRemoteDefault("hidden_apps_in_search") ?: HiddenAppsInSearch.NEVER,
onSet = { reloadHelper.recreate() },
)

val searchAlgorithm = preference(
key = stringPreferencesKey(name = "search_algorithm"),
defaultValue = LawnchairSearchAlgorithm.LOCAL_SEARCH,
defaultValue = getRemoteDefault("search_algorithm")
?: LawnchairSearchAlgorithm.LOCAL_SEARCH,
onSet = { reloadHelper.recreate() },
)

Expand Down Expand Up @@ -366,7 +416,9 @@ class PreferenceManager2 private constructor(private val context: Context) :

val workspaceTextColor = preference(
key = stringPreferencesKey(name = "workspace_text_color"),
defaultValue = ColorMode.AUTO,
defaultValue = getRemoteDefault("workspace_text_color")?.let {
ColorMode.fromString(it)
} ?: ColorMode.AUTO,
parse = { ColorMode.fromString(it) ?: ColorMode.AUTO },
save = { it.toString() },
onSet = { reloadHelper.recreate() },
Expand Down Expand Up @@ -469,7 +521,10 @@ class PreferenceManager2 private constructor(private val context: Context) :

val webSuggestionProvider = preference(
key = stringPreferencesKey(name = "web_suggestion_provider"),
defaultValue = WebSearchProvider.fromString(context.resources.getString(R.string.config_default_web_suggestion_provider)),
defaultValue = WebSearchProvider.fromString(
getRemoteDefault("web_suggestion_provider")
?: context.resources.getString(R.string.config_default_web_suggestion_provider),
),
parse = { WebSearchProvider.fromString(it) },
save = { it.toString() },
onSet = { reloadHelper.recreate() },
Expand Down Expand Up @@ -558,7 +613,7 @@ class PreferenceManager2 private constructor(private val context: Context) :

val additionalFonts = preference(
key = stringPreferencesKey(name = "additional_fonts"),
defaultValue = "",
defaultValue = getRemoteDefault("additional_fonts") ?: "",
)

val enableTaskbarOnPhone = preference(
Expand All @@ -573,7 +628,10 @@ class PreferenceManager2 private constructor(private val context: Context) :

val smartspaceMode = preference(
key = stringPreferencesKey("smartspace_mode"),
defaultValue = SmartspaceMode.fromString(context.getString(R.string.config_default_smartspace_mode)),
defaultValue = SmartspaceMode.fromString(
getRemoteDefault("smartspace_mode")
?: context.getString(R.string.config_default_smartspace_mode),
),
parse = { SmartspaceMode.fromString(it) },
save = { it.toString() },
onSet = { reloadHelper.recreate() },
Expand Down Expand Up @@ -606,14 +664,20 @@ class PreferenceManager2 private constructor(private val context: Context) :

val smartspaceTimeFormat = preference(
key = stringPreferencesKey("smartspace_time_format"),
defaultValue = SmartspaceTimeFormat.fromString(context.getString(R.string.config_default_smartspace_time_format)),
defaultValue = SmartspaceTimeFormat.fromString(
getRemoteDefault("smartspace_time_format")
?: context.getString(R.string.config_default_smartspace_time_format)
),
parse = { SmartspaceTimeFormat.fromString(it) },
save = { it.toString() },
)

val smartspaceCalendar = preference(
key = stringPreferencesKey(name = "smartspace_calendar"),
defaultValue = SmartspaceCalendar.fromString(context.getString(R.string.config_default_smart_space_calendar)),
defaultValue = SmartspaceCalendar.fromString(
getRemoteDefault("smartspace_calendar")
?: context.getString(R.string.config_default_smart_space_calendar)
),
parse = { SmartspaceCalendar.fromString(it) },
save = { it.toString() },
)
Expand Down Expand Up @@ -678,6 +742,10 @@ class PreferenceManager2 private constructor(private val context: Context) :
LauncherAppState.getInstance(context).reloadIcons()
}
.launchIn(scope)
liveInformationManager.liveInformation.get()
.distinctUntilChanged()
.onEach { liveInformation = it }
.launchIn(scope)
}

private fun initializeIconShape(shape: IconShape) {
Expand All @@ -689,6 +757,16 @@ class PreferenceManager2 private constructor(private val context: Context) :
override fun close() {
}

private fun getRemoteDefault(key: String): String? =
liveInformation?.features?.get(key)
.also { value ->
if (value == null) {
Log.v(TAG, "getRemoteDefault: $key -> no remote default")
} else {
Log.i(TAG, "getRemoteDefault: $key -> $value")
}
}

companion object {
private val Context.preferencesDataStore by preferencesDataStore(
name = "preferences",
Expand All @@ -700,6 +778,8 @@ class PreferenceManager2 private constructor(private val context: Context) :

@JvmStatic
fun getInstance(context: Context) = INSTANCE.get(context)!!

private const val TAG = "PreferenceManager2"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import kotlinx.serialization.Serializable
data class LiveInformation(
private val version: Int = 2,
val announcements: List<Announcement>,
val features: Map<String, String?>,
) {

companion object {
val default = LiveInformation(
version = 2,
announcements = emptyList(),
features = emptyMap(),
)
}
}
Loading