Merge "Add the dialog when clicking on the diabled switch." into main

This commit is contained in:
Wa Gao
2023-11-10 19:13:13 +00:00
committed by Android (Google) Code Review
5 changed files with 252 additions and 91 deletions

View File

@@ -34,17 +34,11 @@ import com.android.settingslib.search.SearchIndexable;
public class ContentProtectionPreferenceFragment extends DashboardFragment {
private static final String TAG = "ContentProtectionPreferenceFragment";
@VisibleForTesting
static final String KEY_WORK_PROFILE_SWITCH =
"content_protection_preference_user_consent_work_profile_switch";
// Required by @SearchIndexable to make the fragment and preferences to be indexed.
// Do not rename.
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.layout.content_protection_preference_fragment);
private SwitchPreference mWorkProfileSwitch;
@Override
public void onAttach(Context context) {
super.onAttach(context);
@@ -53,14 +47,6 @@ public class ContentProtectionPreferenceFragment extends DashboardFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWorkProfileSwitch = getPreferenceScreen().findPreference(KEY_WORK_PROFILE_SWITCH);
// If any work profile on the device, display the disable toggle unchecked
if (Utils.getManagedProfile(getContext().getSystemService(UserManager.class)) != null) {
mWorkProfileSwitch.setVisible(true);
mWorkProfileSwitch.setEnabled(false);
mWorkProfileSwitch.setChecked(false);
}
}
@Override

View File

@@ -0,0 +1,86 @@
/*
* Copyright (C) 2023 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.security;
import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedSwitchPreference;
/** Preference controller for content protection work profile switch bar. */
public class ContentProtectionWorkSwitchController extends TogglePreferenceController {
public ContentProtectionWorkSwitchController(
@NonNull Context context, @NonNull String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return getManagedProfile() != null ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}
// The switch is always set to unchecked until Android V by design
@Override
public boolean isChecked() {
return false;
}
// The switch is disabled until Android V by design
@Override
public boolean setChecked(boolean isChecked) {
return false;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
RestrictedSwitchPreference switchPreference = screen.findPreference(getPreferenceKey());
UserHandle managedProfile = getManagedProfile();
if (managedProfile != null) {
switchPreference.setDisabledByAdmin(getEnforcedAdmin(managedProfile));
}
}
@Override
public int getSliceHighlightMenuRes() {
return R.string.menu_key_security;
}
@VisibleForTesting
@Nullable
protected UserHandle getManagedProfile() {
return Utils.getManagedProfile(mContext.getSystemService(UserManager.class));
}
@VisibleForTesting
@Nullable
protected RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin(
@NonNull UserHandle managedProfile) {
return RestrictedLockUtils.getProfileOrDeviceOwner(mContext, managedProfile);
}
}