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

Commit 6c3a637f authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Migrate new() to SwitchPreferenceCompat" into main

parents a0abccad cfdb3b1f
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -44,7 +44,8 @@ import android.widget.CheckBox;

import androidx.preference.Preference;
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.settings.DialogCreatable;
@@ -75,7 +76,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
    private static final TextUtils.SimpleStringSplitter sStringColonSplitter =
            new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR);

    protected SwitchPreference mFollowingTypingSwitchPreference;
    protected TwoStatePreference mFollowingTypingSwitchPreference;

    // TODO(b/147021230): Move duplicated functions with android/internal/accessibility into util.
    private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
@@ -201,8 +202,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
        getSettingsLifecycle().addObserver(magnificationModePreferenceController);
        magnificationModePreferenceController.displayPreference(getPreferenceScreen());

        mFollowingTypingSwitchPreference =
                new SwitchPreference(getPrefContext());
        mFollowingTypingSwitchPreference = new SwitchPreferenceCompat(getPrefContext());
        mFollowingTypingSwitchPreference.setTitle(
                R.string.accessibility_screen_magnification_follow_typing_title);
        mFollowingTypingSwitchPreference.setSummary(
@@ -260,7 +260,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
            return;
        }

        var alwaysOnPreference = new SwitchPreference(getPrefContext());
        var alwaysOnPreference = new SwitchPreferenceCompat(getPrefContext());
        alwaysOnPreference.setTitle(
                R.string.accessibility_screen_magnification_always_on_title);
        alwaysOnPreference.setSummary(
@@ -285,7 +285,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
            return;
        }

        SwitchPreference joystickPreference = new SwitchPreference(getPrefContext());
        TwoStatePreference joystickPreference = new SwitchPreferenceCompat(getPrefContext());
        joystickPreference.setTitle(
                R.string.accessibility_screen_magnification_joystick_title);
        joystickPreference.setSummary(
+6 −5
Original line number Diff line number Diff line
@@ -25,7 +25,8 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat;
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.settings.R;
@@ -81,7 +82,7 @@ public class BluetoothDetailsDataSyncController extends BluetoothDetailsControll

    @Override
    public boolean onPreferenceClick(Preference preference) {
        SwitchPreference switchPreference = (SwitchPreference) preference;
        TwoStatePreference switchPreference = (TwoStatePreference) preference;
        String key = switchPreference.getKey();
        if (key.equals(KEY_PERM_SYNC)) {
            if (switchPreference.isChecked()) {
@@ -106,7 +107,7 @@ public class BluetoothDetailsDataSyncController extends BluetoothDetailsControll

    @Override
    protected void refresh() {
        SwitchPreference permSyncPref = mPreferenceCategory.findPreference(KEY_PERM_SYNC);
        TwoStatePreference permSyncPref = mPreferenceCategory.findPreference(KEY_PERM_SYNC);
        if (permSyncPref == null) {
            permSyncPref = createPermSyncPreference(mPreferenceCategory.getContext());
            mPreferenceCategory.addPreference(permSyncPref);
@@ -132,8 +133,8 @@ public class BluetoothDetailsDataSyncController extends BluetoothDetailsControll
    }

    @VisibleForTesting
    SwitchPreference createPermSyncPreference(Context context) {
        SwitchPreference pref = new SwitchPreference(context);
    TwoStatePreference createPermSyncPreference(Context context) {
        TwoStatePreference pref = new SwitchPreferenceCompat(context);
        pref.setKey(KEY_PERM_SYNC);
        pref.setTitle(context.getString(R.string.bluetooth_details_permissions_sync_title));
        pref.setSummary(context.getString(R.string.bluetooth_details_permissions_sync_summary));
+15 −15
Original line number Diff line number Diff line
@@ -32,7 +32,8 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat;
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.core.SettingsUIDeviceConfig;
@@ -112,14 +113,14 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
    /**
     * 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.
     * @return A preference that allows the user to choose whether this profile
     * will be connected to.
     */
    private SwitchPreference createProfilePreference(Context context,
    private TwoStatePreference createProfilePreference(Context context,
            LocalBluetoothProfile profile) {
        SwitchPreference pref = new SwitchPreference(context);
        TwoStatePreference pref = new SwitchPreferenceCompat(context);
        pref.setKey(profile.toString());
        pref.setTitle(profile.getNameResource(mCachedDevice.getDevice()));
        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) {
        BluetoothDevice device = mCachedDevice.getDevice();
        boolean isLeAudioEnabled = isLeAudioEnabled();
@@ -189,8 +190,8 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll

        if (profile instanceof A2dpProfile) {
            A2dpProfile a2dp = (A2dpProfile) profile;
            SwitchPreference highQualityPref = (SwitchPreference) mProfilesContainer.findPreference(
                    HIGH_QUALITY_AUDIO_PREF_TAG);
            TwoStatePreference highQualityPref =
                    mProfilesContainer.findPreference(HIGH_QUALITY_AUDIO_PREF_TAG);
            if (highQualityPref != null) {
                if (a2dp.isEnabled(device) && a2dp.supportsHighQualityAudio(device)) {
                    highQualityPref.setVisible(true);
@@ -275,7 +276,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
                return false;
            }
        }
        SwitchPreference profilePref = (SwitchPreference) preference;
        TwoStatePreference profilePref = (TwoStatePreference) preference;
        if (profilePref.isChecked()) {
            enableProfile(profile);
        } else {
@@ -454,12 +455,12 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
        BluetoothDevice device = mCachedDevice.getDevice();
        A2dpProfile a2dp = (A2dpProfile) profile;
        if (a2dp.isProfileReady() && a2dp.supportsHighQualityAudio(device)) {
            SwitchPreference highQualityAudioPref = new SwitchPreference(
            TwoStatePreference highQualityAudioPref = new SwitchPreferenceCompat(
                    mProfilesContainer.getContext());
            highQualityAudioPref.setKey(HIGH_QUALITY_AUDIO_PREF_TAG);
            highQualityAudioPref.setVisible(false);
            highQualityAudioPref.setOnPreferenceClickListener(clickedPref -> {
                boolean enable = ((SwitchPreference) clickedPref).isChecked();
                boolean enable = ((TwoStatePreference) clickedPref).isChecked();
                a2dp.setHighQualityAudioEnabled(mCachedDevice.getDevice(), enable);
                return true;
            });
@@ -531,8 +532,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
            if (profile == null || !profile.isProfileReady()) {
                continue;
            }
            SwitchPreference pref = mProfilesContainer.findPreference(
                    profile.toString());
            TwoStatePreference pref = mProfilesContainer.findPreference(profile.toString());
            if (pref == null) {
                pref = createProfilePreference(mProfilesContainer.getContext(), profile);
                mProfilesContainer.addPreference(pref);
@@ -541,8 +541,8 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
            refreshProfilePreference(pref, profile);
        }
        for (LocalBluetoothProfile removedProfile : mCachedDevice.getRemovedProfiles()) {
            final SwitchPreference pref = mProfilesContainer.findPreference(
                    removedProfile.toString());
            final TwoStatePreference pref =
                    mProfilesContainer.findPreference(removedProfile.toString());
            if (pref != null) {
                mProfilesContainer.removePreference(pref);
            }
+13 −12
Original line number Diff line number Diff line
@@ -31,7 +31,8 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;

import com.android.settings.R;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -72,7 +73,7 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont

    @Override
    public boolean onPreferenceClick(Preference preference) {
        SwitchPreference switchPreference = (SwitchPreference) preference;
        TwoStatePreference switchPreference = (TwoStatePreference) preference;
        String key = switchPreference.getKey();
        if (TextUtils.equals(key, KEY_SPATIAL_AUDIO)) {
            updateSpatializerEnabled(switchPreference.isChecked());
@@ -124,7 +125,7 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
            getAvailableDevice();
        }

        SwitchPreference spatialAudioPref = mProfilesContainer.findPreference(KEY_SPATIAL_AUDIO);
        TwoStatePreference spatialAudioPref = mProfilesContainer.findPreference(KEY_SPATIAL_AUDIO);
        if (spatialAudioPref == null && mAudioDevice != null) {
            spatialAudioPref = createSpatialAudioPreference(mProfilesContainer.getContext());
            mProfilesContainer.addPreference(spatialAudioPref);
@@ -132,7 +133,7 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
            if (spatialAudioPref != null) {
                mProfilesContainer.removePreference(spatialAudioPref);
            }
            final SwitchPreference headTrackingPref =
            final TwoStatePreference headTrackingPref =
                    mProfilesContainer.findPreference(KEY_HEAD_TRACKING);
            if (headTrackingPref != null) {
                mProfilesContainer.removePreference(headTrackingPref);
@@ -144,12 +145,12 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
        refreshSpatialAudioEnabled(spatialAudioPref);
    }

    private void refreshSpatialAudioEnabled(SwitchPreference spatialAudioPref) {
    private void refreshSpatialAudioEnabled(TwoStatePreference spatialAudioPref) {
        boolean isSpatialAudioOn = mSpatializer.getCompatibleAudioDevices().contains(mAudioDevice);
        Log.d(TAG, "refresh() isSpatialAudioOn : " + isSpatialAudioOn);
        spatialAudioPref.setChecked(isSpatialAudioOn);

        SwitchPreference headTrackingPref = mProfilesContainer.findPreference(KEY_HEAD_TRACKING);
        TwoStatePreference headTrackingPref = mProfilesContainer.findPreference(KEY_HEAD_TRACKING);
        if (headTrackingPref == null) {
            headTrackingPref = createHeadTrackingPreference(mProfilesContainer.getContext());
            mProfilesContainer.addPreference(headTrackingPref);
@@ -157,8 +158,8 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
        refreshHeadTracking(spatialAudioPref, headTrackingPref);
    }

    private void refreshHeadTracking(SwitchPreference spatialAudioPref,
                                     SwitchPreference headTrackingPref) {
    private void refreshHeadTracking(TwoStatePreference spatialAudioPref,
            TwoStatePreference headTrackingPref) {
        boolean isHeadTrackingAvailable =
                spatialAudioPref.isChecked() && mSpatializer.hasHeadTracker(mAudioDevice);
        Log.d(TAG, "refresh() has head tracker : " + mSpatializer.hasHeadTracker(mAudioDevice));
@@ -169,8 +170,8 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
    }

    @VisibleForTesting
    SwitchPreference createSpatialAudioPreference(Context context) {
        SwitchPreference pref = new SwitchPreference(context);
    TwoStatePreference createSpatialAudioPreference(Context context) {
        TwoStatePreference pref = new SwitchPreferenceCompat(context);
        pref.setKey(KEY_SPATIAL_AUDIO);
        pref.setTitle(context.getString(R.string.bluetooth_details_spatial_audio_title));
        pref.setSummary(context.getString(R.string.bluetooth_details_spatial_audio_summary));
@@ -179,8 +180,8 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
    }

    @VisibleForTesting
    SwitchPreference createHeadTrackingPreference(Context context) {
        SwitchPreference pref = new SwitchPreference(context);
    TwoStatePreference createHeadTrackingPreference(Context context) {
        TwoStatePreference pref = new SwitchPreferenceCompat(context);
        pref.setKey(KEY_HEAD_TRACKING);
        pref.setTitle(context.getString(R.string.bluetooth_details_head_tracking_title));
        pref.setSummary(context.getString(R.string.bluetooth_details_head_tracking_summary));
+5 −4
Original line number Diff line number Diff line
@@ -40,7 +40,8 @@ import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
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.dashboard.profileselector.ProfileSelectDialog;
@@ -157,8 +158,8 @@ public class StylusDevicesController extends AbstractPreferenceController implem
        return pref;
    }

    private SwitchPreference createButtonPressPreference() {
        SwitchPreference pref = new SwitchPreference(mContext);
    private TwoStatePreference createButtonPressPreference() {
        TwoStatePreference pref = new SwitchPreferenceCompat(mContext);
        pref.setKey(KEY_IGNORE_BUTTON);
        pref.setTitle(mContext.getString(R.string.stylus_ignore_button));
        pref.setIcon(R.drawable.ic_block);
@@ -198,7 +199,7 @@ public class StylusDevicesController extends AbstractPreferenceController implem
            case KEY_IGNORE_BUTTON:
                Settings.Secure.putInt(mContext.getContentResolver(),
                        Secure.STYLUS_BUTTONS_ENABLED,
                        ((SwitchPreference) preference).isChecked() ? 0 : 1);
                        ((TwoStatePreference) preference).isChecked() ? 0 : 1);
                break;
        }
        return true;
Loading