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

Commit b5d407ab 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] Support restriction for Sound settings
  Add SettingsPreferenceBindingFactory and support restriction
  Implement RestrictedPreferenceHelperProvider for restricted preference
parents 288cb409 d33b0a51
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
/*
 * 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())
+10 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.widget.CheckedTextView;
import android.widget.ListAdapter;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AlertDialog.Builder;
import androidx.preference.ListPreferenceDialogFragmentCompat;
@@ -40,11 +41,14 @@ import androidx.preference.PreferenceViewHolder;

import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedPreferenceHelper;
import com.android.settingslib.RestrictedPreferenceHelperProvider;

import java.util.ArrayList;
import java.util.List;

public class RestrictedListPreference extends CustomListPreference {
public class RestrictedListPreference extends CustomListPreference implements
        RestrictedPreferenceHelperProvider {

    private final RestrictedPreferenceHelper mHelper;
    private final List<RestrictedItem> mRestrictedItems = new ArrayList<>();
    private boolean mRequiresActiveUnlockedProfile = false;
@@ -61,6 +65,11 @@ public class RestrictedListPreference extends CustomListPreference {
        mHelper = new RestrictedPreferenceHelper(context, this, attrs);
    }

    @Override
    public @NonNull RestrictedPreferenceHelper getRestrictedPreferenceHelper() {
        return mHelper;
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
+2 −0
Original line number Diff line number Diff line
@@ -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)
+49 −0
Original line number Diff line number Diff line
/*
 * 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())
                    }
                }
            }
        }
    }
}
+7 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import com.android.settings.applications.appinfo.AppInfoDashboardFragment;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedPreferenceHelper;
import com.android.settingslib.RestrictedPreferenceHelperProvider;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
@@ -37,7 +38,7 @@ import com.android.settingslib.utils.ThreadUtils;
import com.android.settingslib.widget.AppSwitchPreference;

public class UnrestrictedDataAccessPreference extends AppSwitchPreference implements
        DataSaverBackend.Listener {
        DataSaverBackend.Listener, RestrictedPreferenceHelperProvider {
    private static final String ECM_SETTING_IDENTIFIER = "android:unrestricted_data_access";

    private final ApplicationsState mApplicationsState;
@@ -78,6 +79,11 @@ public class UnrestrictedDataAccessPreference extends AppSwitchPreference implem
        return entry.info.packageName + "|" + entry.info.uid;
    }

    @Override
    public @NonNull RestrictedPreferenceHelper getRestrictedPreferenceHelper() {
        return mHelper;
    }

    @Override
    public void onAttached() {
        super.onAttached();
Loading