Migrate new() to SwitchPreferenceCompat

Bug: 306658427
Test: manual - on Settings pages
Change-Id: I258a8c63d5c46403c6d333a9d2f34c33cb35e27f
This commit is contained in:
Chaohui Wang
2023-10-24 11:38:26 +08:00
parent 5036a9cb17
commit cfdb3b1f1b
19 changed files with 93 additions and 83 deletions

View File

@@ -44,7 +44,8 @@ import android.widget.CheckBox;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.DialogCreatable; import com.android.settings.DialogCreatable;
@@ -75,7 +76,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
private static final TextUtils.SimpleStringSplitter sStringColonSplitter = private static final TextUtils.SimpleStringSplitter sStringColonSplitter =
new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR); new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR);
protected SwitchPreference mFollowingTypingSwitchPreference; protected TwoStatePreference mFollowingTypingSwitchPreference;
// TODO(b/147021230): Move duplicated functions with android/internal/accessibility into util. // TODO(b/147021230): Move duplicated functions with android/internal/accessibility into util.
private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener; private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
@@ -201,8 +202,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
getSettingsLifecycle().addObserver(magnificationModePreferenceController); getSettingsLifecycle().addObserver(magnificationModePreferenceController);
magnificationModePreferenceController.displayPreference(getPreferenceScreen()); magnificationModePreferenceController.displayPreference(getPreferenceScreen());
mFollowingTypingSwitchPreference = mFollowingTypingSwitchPreference = new SwitchPreferenceCompat(getPrefContext());
new SwitchPreference(getPrefContext());
mFollowingTypingSwitchPreference.setTitle( mFollowingTypingSwitchPreference.setTitle(
R.string.accessibility_screen_magnification_follow_typing_title); R.string.accessibility_screen_magnification_follow_typing_title);
mFollowingTypingSwitchPreference.setSummary( mFollowingTypingSwitchPreference.setSummary(
@@ -260,7 +260,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
return; return;
} }
var alwaysOnPreference = new SwitchPreference(getPrefContext()); var alwaysOnPreference = new SwitchPreferenceCompat(getPrefContext());
alwaysOnPreference.setTitle( alwaysOnPreference.setTitle(
R.string.accessibility_screen_magnification_always_on_title); R.string.accessibility_screen_magnification_always_on_title);
alwaysOnPreference.setSummary( alwaysOnPreference.setSummary(
@@ -285,7 +285,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
return; return;
} }
SwitchPreference joystickPreference = new SwitchPreference(getPrefContext()); TwoStatePreference joystickPreference = new SwitchPreferenceCompat(getPrefContext());
joystickPreference.setTitle( joystickPreference.setTitle(
R.string.accessibility_screen_magnification_joystick_title); R.string.accessibility_screen_magnification_joystick_title);
joystickPreference.setSummary( joystickPreference.setSummary(

View File

@@ -25,7 +25,8 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R; import com.android.settings.R;
@@ -81,7 +82,7 @@ public class BluetoothDetailsDataSyncController extends BluetoothDetailsControll
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
SwitchPreference switchPreference = (SwitchPreference) preference; TwoStatePreference switchPreference = (TwoStatePreference) preference;
String key = switchPreference.getKey(); String key = switchPreference.getKey();
if (key.equals(KEY_PERM_SYNC)) { if (key.equals(KEY_PERM_SYNC)) {
if (switchPreference.isChecked()) { if (switchPreference.isChecked()) {
@@ -106,7 +107,7 @@ public class BluetoothDetailsDataSyncController extends BluetoothDetailsControll
@Override @Override
protected void refresh() { protected void refresh() {
SwitchPreference permSyncPref = mPreferenceCategory.findPreference(KEY_PERM_SYNC); TwoStatePreference permSyncPref = mPreferenceCategory.findPreference(KEY_PERM_SYNC);
if (permSyncPref == null) { if (permSyncPref == null) {
permSyncPref = createPermSyncPreference(mPreferenceCategory.getContext()); permSyncPref = createPermSyncPreference(mPreferenceCategory.getContext());
mPreferenceCategory.addPreference(permSyncPref); mPreferenceCategory.addPreference(permSyncPref);
@@ -132,8 +133,8 @@ public class BluetoothDetailsDataSyncController extends BluetoothDetailsControll
} }
@VisibleForTesting @VisibleForTesting
SwitchPreference createPermSyncPreference(Context context) { TwoStatePreference createPermSyncPreference(Context context) {
SwitchPreference pref = new SwitchPreference(context); TwoStatePreference pref = new SwitchPreferenceCompat(context);
pref.setKey(KEY_PERM_SYNC); pref.setKey(KEY_PERM_SYNC);
pref.setTitle(context.getString(R.string.bluetooth_details_permissions_sync_title)); pref.setTitle(context.getString(R.string.bluetooth_details_permissions_sync_title));
pref.setSummary(context.getString(R.string.bluetooth_details_permissions_sync_summary)); pref.setSummary(context.getString(R.string.bluetooth_details_permissions_sync_summary));

View File

@@ -32,7 +32,8 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.SettingsUIDeviceConfig; import com.android.settings.core.SettingsUIDeviceConfig;
@@ -112,14 +113,14 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
/** /**
* Creates a switch preference for the particular profile. * Creates a switch preference for the particular profile.
* *
* @param context The context to use when creating the SwitchPreference * @param context The context to use when creating the TwoStatePreference
* @param profile The profile for which the preference controls. * @param profile The profile for which the preference controls.
* @return A preference that allows the user to choose whether this profile * @return A preference that allows the user to choose whether this profile
* will be connected to. * will be connected to.
*/ */
private SwitchPreference createProfilePreference(Context context, private TwoStatePreference createProfilePreference(Context context,
LocalBluetoothProfile profile) { LocalBluetoothProfile profile) {
SwitchPreference pref = new SwitchPreference(context); TwoStatePreference pref = new SwitchPreferenceCompat(context);
pref.setKey(profile.toString()); pref.setKey(profile.toString());
pref.setTitle(profile.getNameResource(mCachedDevice.getDevice())); pref.setTitle(profile.getNameResource(mCachedDevice.getDevice()));
pref.setOnPreferenceClickListener(this); pref.setOnPreferenceClickListener(this);
@@ -148,9 +149,9 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
} }
/** /**
* Refreshes the state for an existing SwitchPreference for a profile. * Refreshes the state for an existing TwoStatePreference for a profile.
*/ */
private void refreshProfilePreference(SwitchPreference profilePref, private void refreshProfilePreference(TwoStatePreference profilePref,
LocalBluetoothProfile profile) { LocalBluetoothProfile profile) {
BluetoothDevice device = mCachedDevice.getDevice(); BluetoothDevice device = mCachedDevice.getDevice();
boolean isLeAudioEnabled = isLeAudioEnabled(); boolean isLeAudioEnabled = isLeAudioEnabled();
@@ -189,8 +190,8 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
if (profile instanceof A2dpProfile) { if (profile instanceof A2dpProfile) {
A2dpProfile a2dp = (A2dpProfile) profile; A2dpProfile a2dp = (A2dpProfile) profile;
SwitchPreference highQualityPref = (SwitchPreference) mProfilesContainer.findPreference( TwoStatePreference highQualityPref =
HIGH_QUALITY_AUDIO_PREF_TAG); mProfilesContainer.findPreference(HIGH_QUALITY_AUDIO_PREF_TAG);
if (highQualityPref != null) { if (highQualityPref != null) {
if (a2dp.isEnabled(device) && a2dp.supportsHighQualityAudio(device)) { if (a2dp.isEnabled(device) && a2dp.supportsHighQualityAudio(device)) {
highQualityPref.setVisible(true); highQualityPref.setVisible(true);
@@ -275,7 +276,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
return false; return false;
} }
} }
SwitchPreference profilePref = (SwitchPreference) preference; TwoStatePreference profilePref = (TwoStatePreference) preference;
if (profilePref.isChecked()) { if (profilePref.isChecked()) {
enableProfile(profile); enableProfile(profile);
} else { } else {
@@ -454,12 +455,12 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
BluetoothDevice device = mCachedDevice.getDevice(); BluetoothDevice device = mCachedDevice.getDevice();
A2dpProfile a2dp = (A2dpProfile) profile; A2dpProfile a2dp = (A2dpProfile) profile;
if (a2dp.isProfileReady() && a2dp.supportsHighQualityAudio(device)) { if (a2dp.isProfileReady() && a2dp.supportsHighQualityAudio(device)) {
SwitchPreference highQualityAudioPref = new SwitchPreference( TwoStatePreference highQualityAudioPref = new SwitchPreferenceCompat(
mProfilesContainer.getContext()); mProfilesContainer.getContext());
highQualityAudioPref.setKey(HIGH_QUALITY_AUDIO_PREF_TAG); highQualityAudioPref.setKey(HIGH_QUALITY_AUDIO_PREF_TAG);
highQualityAudioPref.setVisible(false); highQualityAudioPref.setVisible(false);
highQualityAudioPref.setOnPreferenceClickListener(clickedPref -> { highQualityAudioPref.setOnPreferenceClickListener(clickedPref -> {
boolean enable = ((SwitchPreference) clickedPref).isChecked(); boolean enable = ((TwoStatePreference) clickedPref).isChecked();
a2dp.setHighQualityAudioEnabled(mCachedDevice.getDevice(), enable); a2dp.setHighQualityAudioEnabled(mCachedDevice.getDevice(), enable);
return true; return true;
}); });
@@ -531,8 +532,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
if (profile == null || !profile.isProfileReady()) { if (profile == null || !profile.isProfileReady()) {
continue; continue;
} }
SwitchPreference pref = mProfilesContainer.findPreference( TwoStatePreference pref = mProfilesContainer.findPreference(profile.toString());
profile.toString());
if (pref == null) { if (pref == null) {
pref = createProfilePreference(mProfilesContainer.getContext(), profile); pref = createProfilePreference(mProfilesContainer.getContext(), profile);
mProfilesContainer.addPreference(pref); mProfilesContainer.addPreference(pref);
@@ -541,8 +541,8 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
refreshProfilePreference(pref, profile); refreshProfilePreference(pref, profile);
} }
for (LocalBluetoothProfile removedProfile : mCachedDevice.getRemovedProfiles()) { for (LocalBluetoothProfile removedProfile : mCachedDevice.getRemovedProfiles()) {
final SwitchPreference pref = mProfilesContainer.findPreference( final TwoStatePreference pref =
removedProfile.toString()); mProfilesContainer.findPreference(removedProfile.toString());
if (pref != null) { if (pref != null) {
mProfilesContainer.removePreference(pref); mProfilesContainer.removePreference(pref);
} }

View File

@@ -31,7 +31,8 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -72,7 +73,7 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
SwitchPreference switchPreference = (SwitchPreference) preference; TwoStatePreference switchPreference = (TwoStatePreference) preference;
String key = switchPreference.getKey(); String key = switchPreference.getKey();
if (TextUtils.equals(key, KEY_SPATIAL_AUDIO)) { if (TextUtils.equals(key, KEY_SPATIAL_AUDIO)) {
updateSpatializerEnabled(switchPreference.isChecked()); updateSpatializerEnabled(switchPreference.isChecked());
@@ -124,7 +125,7 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
getAvailableDevice(); getAvailableDevice();
} }
SwitchPreference spatialAudioPref = mProfilesContainer.findPreference(KEY_SPATIAL_AUDIO); TwoStatePreference spatialAudioPref = mProfilesContainer.findPreference(KEY_SPATIAL_AUDIO);
if (spatialAudioPref == null && mAudioDevice != null) { if (spatialAudioPref == null && mAudioDevice != null) {
spatialAudioPref = createSpatialAudioPreference(mProfilesContainer.getContext()); spatialAudioPref = createSpatialAudioPreference(mProfilesContainer.getContext());
mProfilesContainer.addPreference(spatialAudioPref); mProfilesContainer.addPreference(spatialAudioPref);
@@ -132,7 +133,7 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
if (spatialAudioPref != null) { if (spatialAudioPref != null) {
mProfilesContainer.removePreference(spatialAudioPref); mProfilesContainer.removePreference(spatialAudioPref);
} }
final SwitchPreference headTrackingPref = final TwoStatePreference headTrackingPref =
mProfilesContainer.findPreference(KEY_HEAD_TRACKING); mProfilesContainer.findPreference(KEY_HEAD_TRACKING);
if (headTrackingPref != null) { if (headTrackingPref != null) {
mProfilesContainer.removePreference(headTrackingPref); mProfilesContainer.removePreference(headTrackingPref);
@@ -144,12 +145,12 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
refreshSpatialAudioEnabled(spatialAudioPref); refreshSpatialAudioEnabled(spatialAudioPref);
} }
private void refreshSpatialAudioEnabled(SwitchPreference spatialAudioPref) { private void refreshSpatialAudioEnabled(TwoStatePreference spatialAudioPref) {
boolean isSpatialAudioOn = mSpatializer.getCompatibleAudioDevices().contains(mAudioDevice); boolean isSpatialAudioOn = mSpatializer.getCompatibleAudioDevices().contains(mAudioDevice);
Log.d(TAG, "refresh() isSpatialAudioOn : " + isSpatialAudioOn); Log.d(TAG, "refresh() isSpatialAudioOn : " + isSpatialAudioOn);
spatialAudioPref.setChecked(isSpatialAudioOn); spatialAudioPref.setChecked(isSpatialAudioOn);
SwitchPreference headTrackingPref = mProfilesContainer.findPreference(KEY_HEAD_TRACKING); TwoStatePreference headTrackingPref = mProfilesContainer.findPreference(KEY_HEAD_TRACKING);
if (headTrackingPref == null) { if (headTrackingPref == null) {
headTrackingPref = createHeadTrackingPreference(mProfilesContainer.getContext()); headTrackingPref = createHeadTrackingPreference(mProfilesContainer.getContext());
mProfilesContainer.addPreference(headTrackingPref); mProfilesContainer.addPreference(headTrackingPref);
@@ -157,8 +158,8 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
refreshHeadTracking(spatialAudioPref, headTrackingPref); refreshHeadTracking(spatialAudioPref, headTrackingPref);
} }
private void refreshHeadTracking(SwitchPreference spatialAudioPref, private void refreshHeadTracking(TwoStatePreference spatialAudioPref,
SwitchPreference headTrackingPref) { TwoStatePreference headTrackingPref) {
boolean isHeadTrackingAvailable = boolean isHeadTrackingAvailable =
spatialAudioPref.isChecked() && mSpatializer.hasHeadTracker(mAudioDevice); spatialAudioPref.isChecked() && mSpatializer.hasHeadTracker(mAudioDevice);
Log.d(TAG, "refresh() has head tracker : " + mSpatializer.hasHeadTracker(mAudioDevice)); Log.d(TAG, "refresh() has head tracker : " + mSpatializer.hasHeadTracker(mAudioDevice));
@@ -169,8 +170,8 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
} }
@VisibleForTesting @VisibleForTesting
SwitchPreference createSpatialAudioPreference(Context context) { TwoStatePreference createSpatialAudioPreference(Context context) {
SwitchPreference pref = new SwitchPreference(context); TwoStatePreference pref = new SwitchPreferenceCompat(context);
pref.setKey(KEY_SPATIAL_AUDIO); pref.setKey(KEY_SPATIAL_AUDIO);
pref.setTitle(context.getString(R.string.bluetooth_details_spatial_audio_title)); pref.setTitle(context.getString(R.string.bluetooth_details_spatial_audio_title));
pref.setSummary(context.getString(R.string.bluetooth_details_spatial_audio_summary)); pref.setSummary(context.getString(R.string.bluetooth_details_spatial_audio_summary));
@@ -179,8 +180,8 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
} }
@VisibleForTesting @VisibleForTesting
SwitchPreference createHeadTrackingPreference(Context context) { TwoStatePreference createHeadTrackingPreference(Context context) {
SwitchPreference pref = new SwitchPreference(context); TwoStatePreference pref = new SwitchPreferenceCompat(context);
pref.setKey(KEY_HEAD_TRACKING); pref.setKey(KEY_HEAD_TRACKING);
pref.setTitle(context.getString(R.string.bluetooth_details_head_tracking_title)); pref.setTitle(context.getString(R.string.bluetooth_details_head_tracking_title));
pref.setSummary(context.getString(R.string.bluetooth_details_head_tracking_summary)); pref.setSummary(context.getString(R.string.bluetooth_details_head_tracking_summary));

View File

@@ -40,7 +40,8 @@ import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.dashboard.profileselector.ProfileSelectDialog; import com.android.settings.dashboard.profileselector.ProfileSelectDialog;
@@ -157,8 +158,8 @@ public class StylusDevicesController extends AbstractPreferenceController implem
return pref; return pref;
} }
private SwitchPreference createButtonPressPreference() { private TwoStatePreference createButtonPressPreference() {
SwitchPreference pref = new SwitchPreference(mContext); TwoStatePreference pref = new SwitchPreferenceCompat(mContext);
pref.setKey(KEY_IGNORE_BUTTON); pref.setKey(KEY_IGNORE_BUTTON);
pref.setTitle(mContext.getString(R.string.stylus_ignore_button)); pref.setTitle(mContext.getString(R.string.stylus_ignore_button));
pref.setIcon(R.drawable.ic_block); pref.setIcon(R.drawable.ic_block);
@@ -198,7 +199,7 @@ public class StylusDevicesController extends AbstractPreferenceController implem
case KEY_IGNORE_BUTTON: case KEY_IGNORE_BUTTON:
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),
Secure.STYLUS_BUTTONS_ENABLED, Secure.STYLUS_BUTTONS_ENABLED,
((SwitchPreference) preference).isChecked() ? 0 : 1); ((TwoStatePreference) preference).isChecked() ? 0 : 1);
break; break;
} }
return true; return true;

View File

@@ -26,7 +26,8 @@ import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceClickListener; import androidx.preference.Preference.OnPreferenceClickListener;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
@@ -39,7 +40,7 @@ public class UsbDetailsPowerRoleController extends UsbDetailsController
private static final String KEY_USB_USE_POWER_ONLY = "usb_use_power_only"; private static final String KEY_USB_USE_POWER_ONLY = "usb_use_power_only";
private PreferenceCategory mPreferenceCategory; private PreferenceCategory mPreferenceCategory;
private SwitchPreference mSwitchPreference; private TwoStatePreference mSwitchPreference;
private int mNextPowerRole; private int mNextPowerRole;
@@ -60,7 +61,7 @@ public class UsbDetailsPowerRoleController extends UsbDetailsController
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mPreferenceCategory = screen.findPreference(getPreferenceKey()); mPreferenceCategory = screen.findPreference(getPreferenceKey());
mSwitchPreference = new SwitchPreference(mPreferenceCategory.getContext()); mSwitchPreference = new SwitchPreferenceCompat(mPreferenceCategory.getContext());
mSwitchPreference.setTitle(R.string.usb_use_power_only); mSwitchPreference.setTitle(R.string.usb_use_power_only);
mSwitchPreference.setKey(KEY_USB_USE_POWER_ONLY); mSwitchPreference.setKey(KEY_USB_USE_POWER_ONLY);
mSwitchPreference.setOnPreferenceClickListener(this); mSwitchPreference.setOnPreferenceClickListener(this);

View File

@@ -25,7 +25,8 @@ import android.os.SystemProperties;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
@@ -41,7 +42,7 @@ public class UsbDetailsTranscodeMtpController extends UsbDetailsController
private static final String KEY_USB_TRANSCODE_FILES = "usb_transcode_files"; private static final String KEY_USB_TRANSCODE_FILES = "usb_transcode_files";
private PreferenceCategory mPreferenceCategory; private PreferenceCategory mPreferenceCategory;
private SwitchPreference mSwitchPreference; private TwoStatePreference mSwitchPreference;
public UsbDetailsTranscodeMtpController(Context context, UsbDetailsFragment fragment, public UsbDetailsTranscodeMtpController(Context context, UsbDetailsFragment fragment,
UsbBackend backend) { UsbBackend backend) {
@@ -53,7 +54,7 @@ public class UsbDetailsTranscodeMtpController extends UsbDetailsController
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mPreferenceCategory = screen.findPreference(getPreferenceKey()); mPreferenceCategory = screen.findPreference(getPreferenceKey());
mSwitchPreference = new SwitchPreference(mPreferenceCategory.getContext()); mSwitchPreference = new SwitchPreferenceCompat(mPreferenceCategory.getContext());
mSwitchPreference.setTitle(R.string.usb_transcode_files); mSwitchPreference.setTitle(R.string.usb_transcode_files);
mSwitchPreference.setKey(KEY_USB_TRANSCODE_FILES); mSwitchPreference.setKey(KEY_USB_TRANSCODE_FILES);
mSwitchPreference.setOnPreferenceClickListener(this); mSwitchPreference.setOnPreferenceClickListener(this);

View File

@@ -27,7 +27,7 @@ import android.util.Log;
import androidx.annotation.XmlRes; import androidx.annotation.XmlRes;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.TwoStatePreference;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.android.internal.jank.InteractionJankMonitor; import com.android.internal.jank.InteractionJankMonitor;
@@ -71,9 +71,9 @@ public abstract class InstrumentedPreferenceFragment extends ObservablePreferenc
super.onStart(); super.onStart();
// Override the OnPreferenceTreeClickListener in super.onStart() to inject jank detection. // Override the OnPreferenceTreeClickListener in super.onStart() to inject jank detection.
getPreferenceManager().setOnPreferenceTreeClickListener((preference) -> { getPreferenceManager().setOnPreferenceTreeClickListener((preference) -> {
if (preference instanceof SwitchPreference) { if (preference instanceof TwoStatePreference twoStatePreference) {
SettingsJankMonitor.detectSwitchPreferenceClickJank( SettingsJankMonitor.detectSwitchPreferenceClickJank(
getListView(), (SwitchPreference) preference); getListView(), twoStatePreference);
} }
return onPreferenceTreeClick(preference); return onPreferenceTreeClick(preference);
}); });

View File

@@ -38,7 +38,7 @@ import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
@@ -600,7 +600,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
.setWidgetLayoutResource(R.layout.preference_external_action_icon); .setWidgetLayoutResource(R.layout.preference_external_action_icon);
return externalActionPreference; return externalActionPreference;
case SWITCH: case SWITCH:
return new SwitchPreference(getPrefContext()); return new SwitchPreferenceCompat(getPrefContext());
case SWITCH_WITH_ACTION: case SWITCH_WITH_ACTION:
return new PrimarySwitchPreference(getPrefContext()); return new PrimarySwitchPreference(getPrefContext());
case GROUP: case GROUP:

View File

@@ -33,7 +33,8 @@ import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener; import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.internal.compat.CompatibilityChangeConfig; import com.android.internal.compat.CompatibilityChangeConfig;
import com.android.internal.compat.CompatibilityChangeInfo; import com.android.internal.compat.CompatibilityChangeInfo;
@@ -171,7 +172,7 @@ public class PlatformCompatDashboard extends DashboardFragment {
Preference createPreferenceForChange(Context context, CompatibilityChangeInfo change, Preference createPreferenceForChange(Context context, CompatibilityChangeInfo change,
CompatibilityChangeConfig configMappings) { CompatibilityChangeConfig configMappings) {
final boolean currentValue = configMappings.isChangeEnabled(change.getId()); final boolean currentValue = configMappings.isChangeEnabled(change.getId());
final SwitchPreference item = new SwitchPreference(context); final TwoStatePreference item = new SwitchPreferenceCompat(context);
final String changeName = final String changeName =
change.getName() != null ? change.getName() : "Change_" + change.getId(); change.getName() != null ? change.getName() : "Change_" + change.getId();
item.setSummary(changeName); item.setSummary(changeName);

View File

@@ -31,7 +31,8 @@ import android.util.Log;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.internal.statusbar.IStatusBarService; import com.android.internal.statusbar.IStatusBarService;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
@@ -82,7 +83,7 @@ public class DevelopmentTilePreferenceController extends BasePreferenceControlle
|| ((enabledSetting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) || ((enabledSetting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
&& sInfo.enabled); && sInfo.enabled);
SwitchPreference preference = new SwitchPreference(context); TwoStatePreference preference = new SwitchPreferenceCompat(context);
preference.setTitle(sInfo.loadLabel(mPackageManager)); preference.setTitle(sInfo.loadLabel(mPackageManager));
preference.setIcon(sInfo.icon); preference.setIcon(sInfo.icon);
preference.setKey(sInfo.name); preference.setKey(sInfo.name);

View File

@@ -27,7 +27,8 @@ import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent; import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController; import com.android.settings.core.TogglePreferenceController;
@@ -42,7 +43,7 @@ import java.util.List;
public class DeviceStateAutoRotateSettingController extends TogglePreferenceController implements public class DeviceStateAutoRotateSettingController extends TogglePreferenceController implements
LifecycleObserver { LifecycleObserver {
private SwitchPreference mPreference; private TwoStatePreference mPreference;
private final DeviceStateRotationLockSettingsManager mAutoRotateSettingsManager; private final DeviceStateRotationLockSettingsManager mAutoRotateSettingsManager;
private final int mOrder; private final int mOrder;
@@ -86,7 +87,7 @@ public class DeviceStateAutoRotateSettingController extends TogglePreferenceCont
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
mPreference = new SwitchPreference(mContext); mPreference = new SwitchPreferenceCompat(mContext);
mPreference.setTitle(mDeviceStateDescription); mPreference.setTitle(mDeviceStateDescription);
mPreference.setKey(getPreferenceKey()); mPreference.setKey(getPreferenceKey());
mPreference.setOrder(mOrder); mPreference.setOrder(mOrder);

View File

@@ -25,7 +25,8 @@ import android.hardware.input.KeyboardLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -40,7 +41,7 @@ public class KeyboardLayoutPickerController extends BasePreferenceController imp
InputManager.InputDeviceListener, LifecycleObserver, OnStart, OnStop { InputManager.InputDeviceListener, LifecycleObserver, OnStart, OnStop {
private final InputManager mIm; private final InputManager mIm;
private final Map<SwitchPreference, KeyboardLayout> mPreferenceMap; private final Map<TwoStatePreference, KeyboardLayout> mPreferenceMap;
private Fragment mParent; private Fragment mParent;
private int mInputDeviceId; private int mInputDeviceId;
@@ -95,11 +96,10 @@ public class KeyboardLayoutPickerController extends BasePreferenceController imp
@Override @Override
public boolean handlePreferenceTreeClick(Preference preference) { public boolean handlePreferenceTreeClick(Preference preference) {
if (!(preference instanceof SwitchPreference)) { if (!(preference instanceof TwoStatePreference switchPref)) {
return false; return false;
} }
final SwitchPreference switchPref = (SwitchPreference) preference;
final KeyboardLayout layout = mPreferenceMap.get(switchPref); final KeyboardLayout layout = mPreferenceMap.get(switchPref);
if (layout != null) { if (layout != null) {
final boolean checked = switchPref.isChecked(); final boolean checked = switchPref.isChecked();
@@ -138,7 +138,7 @@ public class KeyboardLayoutPickerController extends BasePreferenceController imp
mInputDeviceIdentifier); mInputDeviceIdentifier);
Arrays.sort(enabledKeyboardLayouts); Arrays.sort(enabledKeyboardLayouts);
for (Map.Entry<SwitchPreference, KeyboardLayout> entry : mPreferenceMap.entrySet()) { for (Map.Entry<TwoStatePreference, KeyboardLayout> entry : mPreferenceMap.entrySet()) {
entry.getKey().setChecked(Arrays.binarySearch(enabledKeyboardLayouts, entry.getKey().setChecked(Arrays.binarySearch(enabledKeyboardLayouts,
entry.getValue().getDescriptor()) >= 0); entry.getValue().getDescriptor()) >= 0);
} }
@@ -149,7 +149,7 @@ public class KeyboardLayoutPickerController extends BasePreferenceController imp
return; return;
} }
for (KeyboardLayout layout : mKeyboardLayouts) { for (KeyboardLayout layout : mKeyboardLayouts) {
final SwitchPreference pref = new SwitchPreference(mScreen.getContext()); final TwoStatePreference pref = new SwitchPreferenceCompat(mScreen.getContext());
pref.setTitle(layout.getLabel()); pref.setTitle(layout.getLabel());
pref.setSummary(layout.getCollection()); pref.setSummary(layout.getCollection());
// TODO: Waiting for new API to use a prefix with special number to setKey // TODO: Waiting for new API to use a prefix with special number to setKey

View File

@@ -54,7 +54,8 @@ import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.Preference.OnPreferenceClickListener; import androidx.preference.Preference.OnPreferenceClickListener;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
@@ -132,7 +133,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
} }
}; };
static class AppRestrictionsPreference extends SwitchPreference { static class AppRestrictionsPreference extends SwitchPreferenceCompat {
private boolean hasSettings; private boolean hasSettings;
private OnClickListener listener; private OnClickListener listener;
private ArrayList<RestrictionEntry> restrictions; private ArrayList<RestrictionEntry> restrictions;
@@ -687,10 +688,10 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
Preference p = null; Preference p = null;
switch (entry.getType()) { switch (entry.getType()) {
case RestrictionEntry.TYPE_BOOLEAN: case RestrictionEntry.TYPE_BOOLEAN:
p = new SwitchPreference(getPrefContext()); p = new SwitchPreferenceCompat(getPrefContext());
p.setTitle(entry.getTitle()); p.setTitle(entry.getTitle());
p.setSummary(entry.getDescription()); p.setSummary(entry.getDescription());
((SwitchPreference)p).setChecked(entry.getSelectedState()); ((TwoStatePreference) p).setChecked(entry.getSelectedState());
break; break;
case RestrictionEntry.TYPE_CHOICE: case RestrictionEntry.TYPE_CHOICE:
case RestrictionEntry.TYPE_CHOICE_LEVEL: case RestrictionEntry.TYPE_CHOICE_LEVEL:

7
src/com/android/settings/wfd/WifiDisplaySettings.java Executable file → Normal file
View File

@@ -59,7 +59,8 @@ import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
import com.android.internal.app.MediaRouteDialogPresenter; import com.android.internal.app.MediaRouteDialogPresenter;
import com.android.settings.R; import com.android.settings.R;
@@ -370,7 +371,7 @@ public final class WifiDisplaySettings extends SettingsPreferenceFragment implem
} }
// switch for Listen Mode // switch for Listen Mode
SwitchPreference pref = new SwitchPreference(getPrefContext()) { TwoStatePreference pref = new SwitchPreferenceCompat(getPrefContext()) {
@Override @Override
protected void onClick() { protected void onClick() {
mListen = !mListen; mListen = !mListen;
@@ -383,7 +384,7 @@ public final class WifiDisplaySettings extends SettingsPreferenceFragment implem
mCertCategory.addPreference(pref); mCertCategory.addPreference(pref);
// switch for Autonomous GO // switch for Autonomous GO
pref = new SwitchPreference(getPrefContext()) { pref = new SwitchPreferenceCompat(getPrefContext()) {
@Override @Override
protected void onClick() { protected void onClick() {
mAutoGO = !mAutoGO; mAutoGO = !mAutoGO;

View File

@@ -32,7 +32,7 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreferenceCompat;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R; import com.android.settings.R;
@@ -124,7 +124,7 @@ public class ToggleScreenMagnificationPreferenceFragmentForSetupWizardTest {
mPreferenceManager.setPreferences(mPreferenceManager.createPreferenceScreen(context)); mPreferenceManager.setPreferences(mPreferenceManager.createPreferenceScreen(context));
mTopIntroPreference = new TopIntroPreference(context); mTopIntroPreference = new TopIntroPreference(context);
mSettingsPreference = new Preference(context); mSettingsPreference = new Preference(context);
mFollowingTypingSwitchPreference = new SwitchPreference(context); mFollowingTypingSwitchPreference = new SwitchPreferenceCompat(context);
} }
@Override @Override

View File

@@ -43,7 +43,7 @@ import android.os.Bundle;
import android.provider.Settings; import android.provider.Settings;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.preference.SwitchPreference; import androidx.preference.TwoStatePreference;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import com.android.settings.DialogCreatable; import com.android.settings.DialogCreatable;
@@ -133,7 +133,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
mFragController.create(R.id.main_content, /* bundle= */ null).start().resume(); mFragController.create(R.id.main_content, /* bundle= */ null).start().resume();
final SwitchPreference switchPreference = final TwoStatePreference switchPreference =
mFragController.get().findPreference( mFragController.get().findPreference(
MagnificationFollowTypingPreferenceController.PREF_KEY); MagnificationFollowTypingPreferenceController.PREF_KEY);
assertThat(switchPreference).isNotNull(); assertThat(switchPreference).isNotNull();
@@ -146,7 +146,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
mFragController.create(R.id.main_content, /* bundle= */ null).start().resume(); mFragController.create(R.id.main_content, /* bundle= */ null).start().resume();
final SwitchPreference switchPreference = final TwoStatePreference switchPreference =
mFragController.get().findPreference( mFragController.get().findPreference(
MagnificationFollowTypingPreferenceController.PREF_KEY); MagnificationFollowTypingPreferenceController.PREF_KEY);
assertThat(switchPreference).isNotNull(); assertThat(switchPreference).isNotNull();

View File

@@ -25,7 +25,7 @@ import android.companion.CompanionDeviceManager;
import android.companion.datatransfer.PermissionSyncRequest; import android.companion.datatransfer.PermissionSyncRequest;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.SwitchPreference; import androidx.preference.TwoStatePreference;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -56,7 +56,7 @@ public class BluetoothDetailsDataSyncControllerTest extends BluetoothDetailsCont
private CompanionDeviceManager mCompanionDeviceManager; private CompanionDeviceManager mCompanionDeviceManager;
private PermissionSyncRequest mPermissionSyncRequest; private PermissionSyncRequest mPermissionSyncRequest;
private SwitchPreference mPermSyncPreference; private TwoStatePreference mPermSyncPreference;
@Before @Before
public void setUp() { public void setUp() {

View File

@@ -32,7 +32,7 @@ import android.media.AudioManager;
import android.media.Spatializer; import android.media.Spatializer;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.SwitchPreference; import androidx.preference.TwoStatePreference;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -68,8 +68,8 @@ public class BluetoothDetailsSpatialAudioControllerTest extends BluetoothDetails
private AudioDeviceAttributes mAvailableDevice; private AudioDeviceAttributes mAvailableDevice;
private BluetoothDetailsSpatialAudioController mController; private BluetoothDetailsSpatialAudioController mController;
private SwitchPreference mSpatialAudioPref; private TwoStatePreference mSpatialAudioPref;
private SwitchPreference mHeadTrackingPref; private TwoStatePreference mHeadTrackingPref;
@Before @Before
public void setUp() { public void setUp() {