From f18e3bafe61f8f0dde8b8d6e2cbb3f32f69fe108 Mon Sep 17 00:00:00 2001 From: Jacky Wang Date: Wed, 6 Nov 2024 11:47:29 +0800 Subject: [PATCH] Add SettingsPreferenceBindingFactory and support restriction Bug: 377600992 Flag: com.android.settings.flags.catalyst Test: testdpc Change-Id: I14c37a3cfb1d69108ad4f5dabd4f35e8ec8899bd --- .../settings/PreferenceRestrictionMixin.kt | 44 +++++++++++++++++ .../android/settings/SettingsApplication.java | 2 + .../SettingsPreferenceBindingFactory.kt | 49 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 src/com/android/settings/PreferenceRestrictionMixin.kt create mode 100644 src/com/android/settings/SettingsPreferenceBindingFactory.kt diff --git a/src/com/android/settings/PreferenceRestrictionMixin.kt b/src/com/android/settings/PreferenceRestrictionMixin.kt new file mode 100644 index 00000000000..c70b94bf500 --- /dev/null +++ b/src/com/android/settings/PreferenceRestrictionMixin.kt @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings + +import android.content.Context +import android.os.UserHandle +import androidx.annotation.CallSuper +import com.android.settingslib.RestrictedLockUtilsInternal +import com.android.settingslib.metadata.PreferenceRestrictionProvider + +/** Mixin to support restriction. */ +interface PreferenceRestrictionMixin : PreferenceRestrictionProvider { + + val restrictionKey: String + + val useAdminDisabledSummary: Boolean + get() = false + + @CallSuper fun isEnabled(context: Context) = !context.hasBaseUserRestriction(restrictionKey) + + override fun isRestricted(context: Context) = + RestrictedLockUtilsInternal.checkIfRestrictionEnforced( + context, + restrictionKey, + UserHandle.myUserId(), + ) != null +} + +fun Context.hasBaseUserRestriction(restrictionKey: String) = + RestrictedLockUtilsInternal.hasBaseUserRestriction(this, restrictionKey, UserHandle.myUserId()) diff --git a/src/com/android/settings/SettingsApplication.java b/src/com/android/settings/SettingsApplication.java index c908855ee49..99d3d922a0c 100644 --- a/src/com/android/settings/SettingsApplication.java +++ b/src/com/android/settings/SettingsApplication.java @@ -46,6 +46,7 @@ import com.android.settingslib.datastore.BackupRestoreStorageManager; import com.android.settingslib.metadata.PreferenceScreenMetadata; import com.android.settingslib.metadata.PreferenceScreenRegistry; import com.android.settingslib.metadata.ProvidePreferenceScreenOptions; +import com.android.settingslib.preference.PreferenceBindingFactory; import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory; import com.google.android.setupcompat.util.WizardManagerHelper; @@ -76,6 +77,7 @@ public class SettingsApplication extends Application { if (Flags.catalyst()) { PreferenceScreenRegistry.INSTANCE.setPreferenceScreensSupplier( this::getPreferenceScreens); + PreferenceBindingFactory.setDefaultFactory(new SettingsPreferenceBindingFactory()); } BackupRestoreStorageManager.getInstance(this) diff --git a/src/com/android/settings/SettingsPreferenceBindingFactory.kt b/src/com/android/settings/SettingsPreferenceBindingFactory.kt new file mode 100644 index 00000000000..53e5d0f45b5 --- /dev/null +++ b/src/com/android/settings/SettingsPreferenceBindingFactory.kt @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings + +import android.os.UserHandle +import androidx.preference.Preference +import com.android.settingslib.RestrictedPreferenceHelperProvider +import com.android.settingslib.metadata.PreferenceHierarchyNode +import com.android.settingslib.preference.DefaultPreferenceBindingFactory +import com.android.settingslib.preference.PreferenceBinding + +/** Preference binding factory for settings app. */ +class SettingsPreferenceBindingFactory : DefaultPreferenceBindingFactory() { + override fun bind( + preference: Preference, + node: PreferenceHierarchyNode, + preferenceBinding: PreferenceBinding?, + ) { + super.bind(preference, node, preferenceBinding) + + // handle restriction consistently + val metadata = node.metadata + if (metadata is PreferenceRestrictionMixin) { + if (preference is RestrictedPreferenceHelperProvider) { + preference.getRestrictedPreferenceHelper().apply { + val restrictionKey = metadata.restrictionKey + if (!preference.context.hasBaseUserRestriction(restrictionKey)) { + useAdminDisabledSummary(metadata.useAdminDisabledSummary) + checkRestrictionAndSetDisabled(restrictionKey, UserHandle.myUserId()) + } + } + } + } + } +}