[Catalyst] Remove UserManager.addUserRestrictionsListener usage
Bug: 377600992 Bug: 379130874 Flag: com.android.settings.flags.catalyst Test: testdpc Change-Id: If0111a19af7a077a0a06c4a4fa7f1e555fbed3b9
This commit is contained in:
@@ -17,7 +17,6 @@
|
|||||||
package com.android.settings;
|
package com.android.settings;
|
||||||
|
|
||||||
import static com.android.settings.SettingsActivity.EXTRA_FRAGMENT_ARG_KEY;
|
import static com.android.settings.SettingsActivity.EXTRA_FRAGMENT_ARG_KEY;
|
||||||
import static com.android.settingslib.media.PhoneMediaDevice.isDesktop;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
@@ -187,13 +186,6 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
|
|||||||
|
|
||||||
/** Returns if catalyst is enabled on current screen. */
|
/** Returns if catalyst is enabled on current screen. */
|
||||||
public final boolean isCatalystEnabled() {
|
public final boolean isCatalystEnabled() {
|
||||||
// TODO(b/379130874): make Catalyst compatible with desktop device, such as user restriction
|
|
||||||
// check.
|
|
||||||
Context context = getContext();
|
|
||||||
if (context != null && isDesktop(context)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return getPreferenceScreenCreator() != null;
|
return getPreferenceScreenCreator() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ import com.android.settingslib.preference.PreferenceScreenBindingHelper.Companio
|
|||||||
|
|
||||||
/** Helper to rebind preference immediately when user restriction is changed. */
|
/** Helper to rebind preference immediately when user restriction is changed. */
|
||||||
class UserRestrictionBindingHelper(
|
class UserRestrictionBindingHelper(
|
||||||
context: Context,
|
private val context: Context,
|
||||||
private val screenBindingHelper: PreferenceScreenBindingHelper,
|
private val screenBindingHelper: PreferenceScreenBindingHelper,
|
||||||
) : AutoCloseable {
|
) : KeyedObserver<String>, AutoCloseable {
|
||||||
private val restrictionKeysToPreferenceKeys: Map<String, MutableSet<String>> =
|
private val restrictionKeysToPreferenceKeys: Map<String, MutableSet<String>> =
|
||||||
mutableMapOf<String, MutableSet<String>>()
|
mutableMapOf<String, MutableSet<String>>()
|
||||||
.apply {
|
.apply {
|
||||||
@@ -42,27 +42,29 @@ class UserRestrictionBindingHelper(
|
|||||||
}
|
}
|
||||||
.toMap()
|
.toMap()
|
||||||
|
|
||||||
private val userRestrictionObserver: KeyedObserver<String?>?
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (restrictionKeysToPreferenceKeys.isEmpty()) {
|
val restrictionKeys = restrictionKeysToPreferenceKeys.keys
|
||||||
userRestrictionObserver = null
|
if (restrictionKeys.isNotEmpty()) {
|
||||||
} else {
|
val userRestrictions = UserRestrictions.get(context)
|
||||||
val observer =
|
val executor = HandlerExecutor.main
|
||||||
KeyedObserver<String?> { restrictionKey, _ ->
|
for (restrictionKey in restrictionKeys) {
|
||||||
restrictionKey?.let { notifyRestrictionChanged(it) }
|
userRestrictions.addObserver(restrictionKey, this, executor)
|
||||||
}
|
}
|
||||||
UserRestrictions.addObserver(context, observer, HandlerExecutor.main)
|
|
||||||
userRestrictionObserver = observer
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun notifyRestrictionChanged(restrictionKey: String) {
|
override fun onKeyChanged(restrictionKey: String, reason: Int) {
|
||||||
val keys = restrictionKeysToPreferenceKeys[restrictionKey] ?: return
|
val keys = restrictionKeysToPreferenceKeys[restrictionKey] ?: return
|
||||||
for (key in keys) screenBindingHelper.notifyChange(key, CHANGE_REASON_STATE)
|
for (key in keys) screenBindingHelper.notifyChange(key, CHANGE_REASON_STATE)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
userRestrictionObserver?.let { UserRestrictions.removeObserver(it) }
|
val restrictionKeys = restrictionKeysToPreferenceKeys.keys
|
||||||
|
if (restrictionKeys.isNotEmpty()) {
|
||||||
|
val userRestrictions = UserRestrictions.get(context)
|
||||||
|
for (restrictionKey in restrictionKeys) {
|
||||||
|
userRestrictions.removeObserver(restrictionKey, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,68 +16,58 @@
|
|||||||
|
|
||||||
package com.android.settings.restriction
|
package com.android.settings.restriction
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Bundle
|
import android.content.Intent
|
||||||
import android.os.IUserRestrictionsListener
|
import android.content.IntentFilter
|
||||||
import android.os.UserManager
|
import android.os.UserManager
|
||||||
import com.android.settingslib.datastore.KeyedDataObservable
|
import com.android.settingslib.datastore.AbstractKeyedDataObservable
|
||||||
|
import com.android.settingslib.datastore.DataChangeReason
|
||||||
import com.android.settingslib.datastore.KeyedObserver
|
import com.android.settingslib.datastore.KeyedObserver
|
||||||
import java.util.concurrent.Executor
|
import java.util.concurrent.Executor
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
|
||||||
|
|
||||||
/** Helper class to monitor user restriction changes. */
|
/** Helper class to monitor user restriction changes. */
|
||||||
object UserRestrictions {
|
class UserRestrictions private constructor(private val applicationContext: Context) {
|
||||||
private val observable = KeyedDataObservable<String>()
|
|
||||||
|
|
||||||
private val userRestrictionsListener =
|
private val observable =
|
||||||
object : IUserRestrictionsListener.Stub() {
|
object : AbstractKeyedDataObservable<String>() {
|
||||||
override fun onUserRestrictionsChanged(
|
override fun onFirstObserverAdded() {
|
||||||
userId: Int,
|
val intentFilter = IntentFilter()
|
||||||
newRestrictions: Bundle,
|
intentFilter.addAction(UserManager.ACTION_USER_RESTRICTIONS_CHANGED)
|
||||||
prevRestrictions: Bundle,
|
applicationContext.registerReceiver(broadcastReceiver, intentFilter)
|
||||||
) {
|
}
|
||||||
// there is no API to remove listener, do a quick check to avoid unnecessary work
|
|
||||||
if (!observable.hasAnyObserver()) return
|
|
||||||
|
|
||||||
val changedKeys = mutableSetOf<String>()
|
override fun onLastObserverRemoved() {
|
||||||
val keys = newRestrictions.keySet() + prevRestrictions.keySet()
|
applicationContext.unregisterReceiver(broadcastReceiver)
|
||||||
for (key in keys) {
|
|
||||||
if (newRestrictions.getBoolean(key) != prevRestrictions.getBoolean(key)) {
|
|
||||||
changedKeys.add(key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (key in changedKeys) observable.notifyChange(key, 0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val listenerAdded = AtomicBoolean()
|
private val broadcastReceiver: BroadcastReceiver =
|
||||||
|
object : BroadcastReceiver() {
|
||||||
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
// there is no way to get the changed keys, just notify all observers
|
||||||
|
observable.notifyChange(DataChangeReason.UPDATE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun addObserver(context: Context, observer: KeyedObserver<String?>, executor: Executor) {
|
fun addObserver(observer: KeyedObserver<String?>, executor: Executor) =
|
||||||
context.addUserRestrictionsListener()
|
|
||||||
observable.addObserver(observer, executor)
|
observable.addObserver(observer, executor)
|
||||||
}
|
|
||||||
|
|
||||||
fun addObserver(
|
fun addObserver(key: String, observer: KeyedObserver<String>, executor: Executor) =
|
||||||
context: Context,
|
|
||||||
key: String,
|
|
||||||
observer: KeyedObserver<String>,
|
|
||||||
executor: Executor,
|
|
||||||
) {
|
|
||||||
context.addUserRestrictionsListener()
|
|
||||||
observable.addObserver(key, observer, executor)
|
observable.addObserver(key, observer, executor)
|
||||||
}
|
|
||||||
|
|
||||||
private fun Context.addUserRestrictionsListener() {
|
|
||||||
if (listenerAdded.getAndSet(true)) return
|
|
||||||
// surprisingly, there is no way to remove the listener
|
|
||||||
applicationContext
|
|
||||||
.getSystemService(UserManager::class.java)
|
|
||||||
.addUserRestrictionsListener(userRestrictionsListener)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeObserver(observer: KeyedObserver<String?>) = observable.removeObserver(observer)
|
fun removeObserver(observer: KeyedObserver<String?>) = observable.removeObserver(observer)
|
||||||
|
|
||||||
fun removeObserver(key: String, observer: KeyedObserver<String>) =
|
fun removeObserver(key: String, observer: KeyedObserver<String>) =
|
||||||
observable.removeObserver(key, observer)
|
observable.removeObserver(key, observer)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@Volatile private var instance: UserRestrictions? = null
|
||||||
|
|
||||||
|
fun get(context: Context) =
|
||||||
|
instance
|
||||||
|
?: synchronized(this) {
|
||||||
|
instance ?: UserRestrictions(context.applicationContext).also { instance = it }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user