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

Commit ce342337 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] Leverage useParcel extension
  [Catalyst] Add more docs for some API
parents 2296d78a b85622a2
Loading
Loading
Loading
Loading
+39 −41
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@
package com.android.settingslib.graph

import android.os.Bundle
import android.os.Parcel
import com.android.settingslib.graph.proto.PreferenceProto
import com.android.settingslib.ipc.MessageCodec
import com.android.settingslib.metadata.PreferenceCoordinate
import com.android.settingslib.metadata.useParcel
import java.util.Arrays

/** Message codec for [PreferenceGetterRequest]. */
@@ -57,20 +57,19 @@ class PreferenceGetterResponseCodec : MessageCodec<PreferenceGetterResponse> {

    private fun Map<PreferenceCoordinate, Int>.toErrorsByteArray(): ByteArray? {
        if (isEmpty()) return null
        val parcel = Parcel.obtain()
        return useParcel { parcel ->
            parcel.writeInt(size)
            for ((coordinate, code) in this) {
                coordinate.writeToParcel(parcel, 0)
                parcel.writeInt(code)
            }
        val bytes = parcel.marshall()
        parcel.recycle()
        return bytes
            parcel.marshall()
        }
    }

    private fun Map<PreferenceCoordinate, PreferenceProto>.toPreferencesByteArray(): ByteArray? {
        if (isEmpty()) return null
        val parcel = Parcel.obtain()
        return useParcel { parcel ->
            parcel.writeInt(size)
            for ((coordinate, preferenceProto) in this) {
                coordinate.writeToParcel(parcel, 0)
@@ -78,9 +77,8 @@ class PreferenceGetterResponseCodec : MessageCodec<PreferenceGetterResponse> {
                parcel.writeInt(data.size)
                parcel.writeByteArray(data)
            }
        val bytes = parcel.marshall()
        parcel.recycle()
        return bytes
            parcel.marshall()
        }
    }

    override fun decode(data: Bundle) =
@@ -91,7 +89,7 @@ class PreferenceGetterResponseCodec : MessageCodec<PreferenceGetterResponse> {

    private fun ByteArray?.toErrors(): Map<PreferenceCoordinate, Int> {
        if (this == null) return emptyMap()
        val parcel = Parcel.obtain()
        return useParcel { parcel ->
            parcel.unmarshall(this, 0, size)
            parcel.setDataPosition(0)
            val count = parcel.readInt()
@@ -100,13 +98,13 @@ class PreferenceGetterResponseCodec : MessageCodec<PreferenceGetterResponse> {
                val coordinate = PreferenceCoordinate(parcel)
                errors[coordinate] = parcel.readInt()
            }
        parcel.recycle()
        return errors
            errors
        }
    }

    private fun ByteArray?.toPreferences(): Map<PreferenceCoordinate, PreferenceProto> {
        if (this == null) return emptyMap()
        val parcel = Parcel.obtain()
        return useParcel { parcel ->
            parcel.unmarshall(this, 0, size)
            parcel.setDataPosition(0)
            val count = parcel.readInt()
@@ -117,8 +115,8 @@ class PreferenceGetterResponseCodec : MessageCodec<PreferenceGetterResponse> {
                val array = ByteArray(bytes).also { parcel.readByteArray(it) }
                preferences[coordinate] = PreferenceProto.parseFrom(array)
            }
        parcel.recycle()
        return preferences
            preferences
        }
    }

    companion object {
+5 −6
Original line number Diff line number Diff line
@@ -61,8 +61,7 @@ fun Parcelable.marshallParcel(): ByteArray = useParcel { parcel ->
fun ByteArray.unmarshallBundle(): Bundle = unmarshallParcel(Bundle.CREATOR)

/** Unmarshall a byte array to [Parcelable]. */
inline fun <reified T> ByteArray.unmarshallParcel(creator: Parcelable.Creator<T>): T =
    useParcel { parcel ->
fun <T> ByteArray.unmarshallParcel(creator: Parcelable.Creator<T>): T = useParcel { parcel ->
    parcel.unmarshall(this, 0, size)
    parcel.setDataPosition(0)
    return@useParcel creator.createFromParcel(parcel)
+8 −1
Original line number Diff line number Diff line
@@ -141,7 +141,14 @@ interface PreferenceMetadata {
     */
    fun isEnabled(context: Context): Boolean = true

    /** Returns the keys of depended preferences. */
    /**
     * Returns the keys of depended preferences.
     *
     * Keep in mind that the dependency is effective only on the same screen. For cross screen
     * dependency, especially for preference screen entry point, add observer (e.g. on the depended
     * preference's data store) explicitly to update the preference with
     * [PreferenceLifecycleProvider].
     */
    fun dependencies(context: Context): Array<String> = arrayOf()

    /** Returns if the preference is persistent in datastore. */
+4 −2
Original line number Diff line number Diff line
@@ -84,9 +84,11 @@ interface PreferenceRestrictionProvider {
}

/**
 * Preference lifecycle to deal with preference state.
 * Preference lifecycle to deal with preference UI state.
 *
 * Implement this interface when preference depends on runtime conditions.
 * Implement this interface when preference depends on runtime conditions for UI update. Note that
 * [PreferenceMetadata] could be created for UI (shown in UI widget) or background (e.g. external
 * Get/Set), callbacks in this interface will ONLY be invoked when it is for UI.
 */
interface PreferenceLifecycleProvider {