Merge "Add the controller for the top switch." into main

This commit is contained in:
Wa Gao
2023-11-07 01:22:43 +00:00
committed by Android (Google) Code Review
4 changed files with 364 additions and 22 deletions

View File

@@ -16,26 +16,35 @@
package com.android.settings.security;
import android.content.Context;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.os.UserManager;
import androidx.annotation.VisibleForTesting;
import androidx.preference.SwitchPreference;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
import android.os.Bundle;
@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);
@@ -44,7 +53,14 @@ public class ContentProtectionPreferenceFragment extends DashboardFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// TODO(b/304681048): Update the toggles' behavior according to user's profile
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,94 @@
/*
* 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.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.widget.Switch;
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.settings.widget.SettingsMainSwitchPreference;
import com.android.settingslib.widget.OnMainSwitchChangeListener;
/** Preference controller for content protection toggle switch bar. */
public class ContentProtectionTogglePreferenceController extends TogglePreferenceController
implements OnMainSwitchChangeListener {
@VisibleForTesting
static final String KEY_CONTENT_PROTECTION_PREFERENCE = "content_protection_user_consent";
private SettingsMainSwitchPreference mSwitchBar;
private final ContentResolver mContentResolver;
private final boolean isFullyManagedDevice = Utils.getDeviceOwnerComponent(mContext) != null;
public ContentProtectionTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mContentResolver = context.getContentResolver();
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean isChecked() {
if (isFullyManagedDevice) {
// If fully managed device, it should always unchecked
return false;
}
return Settings.Global.getInt(mContentResolver, KEY_CONTENT_PROTECTION_PREFERENCE, 0) >= 0;
}
@Override
public boolean setChecked(boolean isChecked) {
mSwitchBar.setChecked(isChecked);
Settings.Global.putInt(
mContentResolver, KEY_CONTENT_PROTECTION_PREFERENCE, isChecked ? 1 : -1);
return true;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mSwitchBar = screen.findPreference(getPreferenceKey());
mSwitchBar.addOnSwitchChangeListener(this);
if (isFullyManagedDevice) {
// If fully managed device, the switch bar is greyed out
mSwitchBar.setEnabled(false);
}
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
if (isChecked != isChecked()) {
setChecked(isChecked);
}
}
@Override
public int getSliceHighlightMenuRes() {
return R.string.menu_key_security;
}
}