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

Commit a571133c authored by Jacky Wang's avatar Jacky Wang Committed by Android (Google) Code Review
Browse files

Merge changes from topic "catalyst" into main

* changes:
  [Catalyst] Refine launch intent for graph
  [Catalyst] Introduce PreferenceHierarchyGenerator
parents 3e426c32 cb3bbb08
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -414,7 +414,8 @@ fun PreferenceMetadata.toProto(
            restricted = metadata.isRestricted(context)
        }
        metadata.intent(context)?.let { actionTarget = it.toActionTarget(context) }
        screenMetadata.getLaunchIntent(context, metadata)?.let { launchIntent = it.toProto() }
        val launchTarget = if (screenMetadata != metadata) metadata else null
        screenMetadata.getLaunchIntent(context, launchTarget)?.let { launchIntent = it.toProto() }
        for (tag in metadata.tags(context)) addTags(tag)
    }
    persistent = metadata.isPersistent(context)
+2 −4
Original line number Diff line number Diff line
@@ -44,11 +44,9 @@ fun Intent.toProto(): IntentProto = intentProto {
    this@toProto.type?.let { mimeType = it }
}

fun IntentProto.toIntent(): Intent? {
    if (!hasComponent()) return null
    val componentName = ComponentName.unflattenFromString(component) ?: return null
fun IntentProto.toIntent(): Intent {
    val intent = Intent()
    intent.component = componentName
    if (hasComponent()) intent.component = ComponentName.unflattenFromString(component)
    if (hasAction()) intent.action = action
    if (hasData()) intent.data = Uri.parse(data)
    if (hasPkg()) intent.`package` = pkg
+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>
}