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

Commit 06af608b authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[Catalyst] Add PreferenceChangeReason IntDef" into main

parents ed8d8ba3 dd9da695
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -39,5 +39,7 @@ annotation class DataChangeReason {
        const val RESTORE = 3
        /** Data is synced from another profile (e.g. personal profile to work profile). */
        const val SYNC_ACROSS_PROFILES = 4

        fun isDataChange(reason: Int): Boolean = reason in UNKNOWN..SYNC_ACROSS_PROFILES
    }
}
+18 −0
Original line number Diff line number Diff line
@@ -44,6 +44,24 @@ annotation class ReadWritePermit {
    }
}

/** The reason of preference change. */
@IntDef(
    PreferenceChangeReason.VALUE,
    PreferenceChangeReason.STATE,
    PreferenceChangeReason.DEPENDENT,
)
@Retention(AnnotationRetention.SOURCE)
annotation class PreferenceChangeReason {
    companion object {
        /** Preference value is changed. */
        const val VALUE = 1000
        /** Preference state (title/summary, enable state, etc.) is changed. */
        const val STATE = 1001
        /** Dependent preference state is changed. */
        const val DEPENDENT = 1002
    }
}

/** Indicates how sensitive of the data. */
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.TYPE)
+12 −11
Original line number Diff line number Diff line
@@ -25,12 +25,14 @@ import androidx.preference.Preference
import androidx.preference.PreferenceDataStore
import androidx.preference.PreferenceGroup
import androidx.preference.PreferenceScreen
import com.android.settingslib.datastore.DataChangeReason
import com.android.settingslib.datastore.HandlerExecutor
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.KeyedDataObservable
import com.android.settingslib.datastore.KeyedObservable
import com.android.settingslib.datastore.KeyedObserver
import com.android.settingslib.metadata.PersistentPreference
import com.android.settingslib.metadata.PreferenceChangeReason
import com.android.settingslib.metadata.PreferenceHierarchy
import com.android.settingslib.metadata.PreferenceHierarchyNode
import com.android.settingslib.metadata.PreferenceLifecycleContext
@@ -73,7 +75,7 @@ class PreferenceScreenBindingHelper(
                    ?.keyValueStore

            override fun notifyPreferenceChange(key: String) =
                notifyChange(key, CHANGE_REASON_STATE)
                notifyChange(key, PreferenceChangeReason.STATE)

            @Suppress("DEPRECATION")
            override fun startActivityForResult(
@@ -91,7 +93,13 @@ class PreferenceScreenBindingHelper(
    private val preferenceObserver: KeyedObserver<String?>

    private val storageObserver =
        KeyedObserver<String> { key, _ -> notifyChange(key, CHANGE_REASON_VALUE) }
        KeyedObserver<String> { key, reason ->
            if (DataChangeReason.isDataChange(reason)) {
                notifyChange(key, PreferenceChangeReason.VALUE)
            } else {
                notifyChange(key, PreferenceChangeReason.STATE)
            }
        }

    init {
        val preferencesBuilder = ImmutableMap.builder<String, PreferenceHierarchyNode>()
@@ -148,7 +156,7 @@ class PreferenceScreenBindingHelper(
        }

        // check reason to avoid potential infinite loop
        if (reason != CHANGE_REASON_DEPENDENT) {
        if (reason != PreferenceChangeReason.DEPENDENT) {
            notifyDependents(key, mutableSetOf())
        }
    }
@@ -157,7 +165,7 @@ class PreferenceScreenBindingHelper(
    private fun notifyDependents(key: String, notifiedKeys: MutableSet<String>) {
        if (!notifiedKeys.add(key)) return
        for (dependency in dependencies[key]) {
            notifyChange(dependency, CHANGE_REASON_DEPENDENT)
            notifyChange(dependency, PreferenceChangeReason.DEPENDENT)
            notifyDependents(dependency, notifiedKeys)
        }
    }
@@ -210,13 +218,6 @@ class PreferenceScreenBindingHelper(
    }

    companion object {
        /** Preference value is changed. */
        const val CHANGE_REASON_VALUE = 0
        /** Preference state (title/summary, enable state, etc.) is changed. */
        const val CHANGE_REASON_STATE = 1
        /** Dependent preference state is changed. */
        const val CHANGE_REASON_DEPENDENT = 2

        /** Updates preference screen that has incomplete hierarchy. */
        @JvmStatic
        fun bind(preferenceScreen: PreferenceScreen) {