Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fb12077b authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Introduce PreferenceHierarchyGenerator

Bug: 407910711
Flag: EXEMPT new code in framework
Test: devtool
Change-Id: I905b41d42efea2ce0586e7aa18facb0a73cac8ed
parent 3995a1f7
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -204,3 +204,13 @@ fun preferenceHierarchy(
    metadata: PreferenceMetadata,
    init: PreferenceHierarchy.() -> Unit,
) = PreferenceHierarchy(context, metadata).also(init)

/**
 * Builder function to create [PreferenceHierarchy] with coroutine in
 * [DSL](https://kotlinlang.org/docs/type-safe-builders.html) manner.
 */
suspend fun asyncPreferenceHierarchy(
    context: Context,
    metadata: PreferenceMetadata,
    init: suspend PreferenceHierarchy.() -> Unit,
) = PreferenceHierarchy(context, metadata).also { init(it) }
+15 −1
Original line number Diff line number Diff line
@@ -63,10 +63,14 @@ interface PreferenceScreenMetadata : PreferenceMetadata {
    fun hasCompleteHierarchy(): Boolean = true

    /**
     * Returns the hierarchy of preference screen.
     * Returns the static hierarchy of preference screen.
     *
     * The implementation MUST include all preferences into the hierarchy regardless of the runtime
     * conditions. DO NOT check any condition (except compile time flag) before adding a preference.
     *
     * If the screen has different [PreferenceHierarchy] based on additional information (e.g. app
     * filter, profile), implements [PreferenceHierarchyGenerator]. The UI framework will support
     * switching [PreferenceHierarchy] on current screen with given type.
     */
    fun getPreferenceHierarchy(context: Context): PreferenceHierarchy

@@ -78,6 +82,16 @@ interface PreferenceScreenMetadata : PreferenceMetadata {
    fun getLaunchIntent(context: Context, metadata: PreferenceMetadata?): Intent? = null
}

/** Generator of [PreferenceHierarchy] based on given type. */
interface PreferenceHierarchyGenerator<T> {

    /** Default type to generate [PreferenceHierarchy]. */
    val defaultType: T

    /** Generates [PreferenceHierarchy] with given type. */
    suspend fun generatePreferenceHierarchy(context: Context, type: T): PreferenceHierarchy
}

/**
 * Factory of [PreferenceScreenMetadata].
 *
+7 −1
Original line number Diff line number Diff line
@@ -167,6 +167,12 @@ abstract class PreferenceLifecycleContext(context: Context) : ContextWrapper(con
    /** Notifies that preference state of given key is changed and updates preference widget UI. */
    abstract fun notifyPreferenceChange(key: String)

    /**
     * Switches preference hierarchy to given type, the screen metadata must implement
     * `PreferenceHierarchyGenerator`.
     */
    open fun switchPreferenceHierarchy(type: Any?): Unit = TODO()

    /**
     * Starts activity for result, see [android.app.Activity.startActivityForResult].
     *
@@ -184,6 +190,6 @@ abstract class PreferenceLifecycleContext(context: Context) : ContextWrapper(con
     */
    abstract fun <I, O> registerForActivityResult(
        contract: ActivityResultContract<I, O>,
        callback: ActivityResultCallback<O>
        callback: ActivityResultCallback<O>,
    ): ActivityResultLauncher<I>
}