Add SettingsPreferenceBindingFactory and support restriction

Bug: 377600992
Flag: com.android.settings.flags.catalyst
Test: testdpc
Change-Id: I14c37a3cfb1d69108ad4f5dabd4f35e8ec8899bd
This commit is contained in:
Jacky Wang
2024-11-06 11:47:29 +08:00
parent 3551614baf
commit f18e3bafe6
3 changed files with 95 additions and 0 deletions

View File

@@ -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())

View File

@@ -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)

View File

@@ -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())
}
}
}
}
}
}