From c4a3a50d58f7f307cfdee8ae6a7e041087eaa4c5 Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Wed, 25 Oct 2023 14:57:16 +0800 Subject: [PATCH] Migrate to CompoundButton Switch and SwitchCompat are both CompoundButton. Using CompoundButton in Java will helps migration in the future. Bug: 306658427 Test: manual - on affected pages Change-Id: I7cdc2601879a85d33f77239e38263320d5a6984e --- .../android/settings/TrustedCredentialsFragment.java | 6 +++--- .../settings/accessibility/ShortcutPreference.java | 4 ++-- .../settings/applications/appops/AppOpsCategory.java | 12 +++++------- .../CredentialManagerPreferenceController.java | 6 +++--- .../manageapplications/ApplicationViewHolder.java | 11 ++++------- .../face/FaceEnrollAccessibilityToggle.java | 5 ++--- .../bluetooth/BluetoothPairingDialogFragment.java | 6 +++--- .../settings/development/tare/TareHomePage.java | 3 +-- .../settings/users/AppRestrictionsFragment.java | 7 +------ 9 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/com/android/settings/TrustedCredentialsFragment.java b/src/com/android/settings/TrustedCredentialsFragment.java index ca95c1ad34d..a150850f922 100644 --- a/src/com/android/settings/TrustedCredentialsFragment.java +++ b/src/com/android/settings/TrustedCredentialsFragment.java @@ -52,13 +52,13 @@ import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.BaseExpandableListAdapter; +import android.widget.CompoundButton; import android.widget.ExpandableListView; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; -import android.widget.Switch; import android.widget.TextView; import androidx.annotation.NonNull; @@ -456,10 +456,10 @@ public class TrustedCredentialsFragment extends ObservableFragment return "Group" + getUserIdByGroup(groupPosition); } - private class ViewHolder { + private static class ViewHolder { private TextView mSubjectPrimaryView; private TextView mSubjectSecondaryView; - private Switch mSwitch; + private CompoundButton mSwitch; } } diff --git a/src/com/android/settings/accessibility/ShortcutPreference.java b/src/com/android/settings/accessibility/ShortcutPreference.java index 81bc5863872..4b6868e0871 100644 --- a/src/com/android/settings/accessibility/ShortcutPreference.java +++ b/src/com/android/settings/accessibility/ShortcutPreference.java @@ -21,8 +21,8 @@ import android.util.AttributeSet; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; +import android.widget.CompoundButton; import android.widget.LinearLayout; -import android.widget.Switch; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; @@ -88,7 +88,7 @@ public class ShortcutPreference extends Preference { mSettingsEditable ? outValue.resourceId : /* Remove background */ 0); } - Switch switchWidget = + CompoundButton switchWidget = holder.itemView.findViewById(com.android.settingslib.R.id.switchWidget); if (switchWidget != null) { // Consumes move events to ignore drag actions. diff --git a/src/com/android/settings/applications/appops/AppOpsCategory.java b/src/com/android/settings/applications/appops/AppOpsCategory.java index 57d7dc4c490..95ee256a1fd 100644 --- a/src/com/android/settings/applications/appops/AppOpsCategory.java +++ b/src/com/android/settings/applications/appops/AppOpsCategory.java @@ -29,9 +29,9 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; +import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.ListView; -import android.widget.Switch; import android.widget.TextView; import androidx.fragment.app.ListFragment; @@ -244,14 +244,12 @@ public class AppOpsCategory extends ListFragment implements public static class AppListAdapter extends BaseAdapter { private final Resources mResources; private final LayoutInflater mInflater; - private final AppOpsState mState; List mList; - public AppListAdapter(Context context, AppOpsState state) { + public AppListAdapter(Context context) { mResources = context.getResources(); mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - mState = state; } public void setData(List data) { @@ -294,7 +292,7 @@ public class AppOpsCategory extends ListFragment implements ((TextView) view.findViewById(R.id.op_name)).setText( item.getTimeText(mResources, false)); view.findViewById(R.id.op_time).setVisibility(View.GONE); - ((Switch) view.findViewById(R.id.op_switch)).setChecked( + ((CompoundButton) view.findViewById(R.id.op_switch)).setChecked( item.getPrimaryOpMode() == AppOpsManager.MODE_ALLOWED); return view; @@ -318,7 +316,7 @@ public class AppOpsCategory extends ListFragment implements setHasOptionsMenu(true); // Create an empty adapter we will use to display the loaded data. - mAdapter = new AppListAdapter(getActivity(), mState); + mAdapter = new AppListAdapter(getActivity()); setListAdapter(mAdapter); // Start out with a progress indicator. @@ -332,7 +330,7 @@ public class AppOpsCategory extends ListFragment implements AppOpEntry entry = mAdapter.getItem(position); if (entry != null) { // We treat this as tapping on the check box, toggling the app op state. - Switch sw = v.findViewById(R.id.op_switch); + CompoundButton sw = v.findViewById(R.id.op_switch); boolean checked = !sw.isChecked(); sw.setChecked(checked); AppOpsManager.OpEntry op = entry.getOpEntry(0); diff --git a/src/com/android/settings/applications/credentials/CredentialManagerPreferenceController.java b/src/com/android/settings/applications/credentials/CredentialManagerPreferenceController.java index 4021110cfa2..b0905bab077 100644 --- a/src/com/android/settings/applications/credentials/CredentialManagerPreferenceController.java +++ b/src/com/android/settings/applications/credentials/CredentialManagerPreferenceController.java @@ -47,7 +47,7 @@ import android.service.autofill.AutofillServiceInfo; import android.text.TextUtils; import android.util.Log; import android.view.View; -import android.widget.Switch; +import android.widget.CompoundButton; import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; @@ -969,7 +969,7 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl } // Stores a reference to the switch view. - private @Nullable Switch mSwitch; + private @Nullable CompoundButton mSwitch; // Switch text for on and off states private @NonNull boolean mChecked = false; @@ -1022,7 +1022,7 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl // Setup the switch. View checkableView = view.itemView.findViewById(com.android.settingslib.R.id.switchWidget); - if (checkableView instanceof Switch switchView) { + if (checkableView instanceof CompoundButton switchView) { switchView.setChecked(mChecked); switchView.setOnClickListener(mListener); diff --git a/src/com/android/settings/applications/manageapplications/ApplicationViewHolder.java b/src/com/android/settings/applications/manageapplications/ApplicationViewHolder.java index dbb97e92f6c..c94edc6bb26 100644 --- a/src/com/android/settings/applications/manageapplications/ApplicationViewHolder.java +++ b/src/com/android/settings/applications/manageapplications/ApplicationViewHolder.java @@ -31,9 +31,9 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.ProgressBar; -import android.widget.Switch; import android.widget.TextView; import androidx.annotation.StringRes; @@ -67,7 +67,7 @@ public class ApplicationViewHolder extends RecyclerView.ViewHolder { @VisibleForTesting final ViewGroup mWidgetContainer; @VisibleForTesting - final Switch mSwitch; + final CompoundButton mSwitch; final ImageView mAddIcon; final ProgressBar mProgressBar; @@ -173,10 +173,6 @@ public class ApplicationViewHolder extends RecyclerView.ViewHolder { mAppName.setContentDescription(contentDescription); } - void setIcon(int drawableRes) { - mAppIcon.setImageResource(drawableRes); - } - void setIcon(Drawable icon) { if (icon == null) { return; @@ -219,7 +215,8 @@ public class ApplicationViewHolder extends RecyclerView.ViewHolder { } } - void updateSwitch(Switch.OnCheckedChangeListener listener, boolean enabled, boolean checked) { + void updateSwitch(CompoundButton.OnCheckedChangeListener listener, boolean enabled, + boolean checked) { if (mSwitch != null && mWidgetContainer != null) { mWidgetContainer.setFocusable(false); mWidgetContainer.setClickable(false); diff --git a/src/com/android/settings/biometrics/face/FaceEnrollAccessibilityToggle.java b/src/com/android/settings/biometrics/face/FaceEnrollAccessibilityToggle.java index f6ba0f989e5..86c1a501c82 100644 --- a/src/com/android/settings/biometrics/face/FaceEnrollAccessibilityToggle.java +++ b/src/com/android/settings/biometrics/face/FaceEnrollAccessibilityToggle.java @@ -22,7 +22,6 @@ import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.CompoundButton; import android.widget.LinearLayout; -import android.widget.Switch; import android.widget.TextView; import com.android.settings.R; @@ -32,7 +31,7 @@ import com.android.settings.R; */ public class FaceEnrollAccessibilityToggle extends LinearLayout { - private Switch mSwitch; + private final CompoundButton mSwitch; public FaceEnrollAccessibilityToggle(Context context) { this(context, null /* attrs */); @@ -76,7 +75,7 @@ public class FaceEnrollAccessibilityToggle extends LinearLayout { mSwitch.setOnCheckedChangeListener(listener); } - public Switch getSwitch() { + public CompoundButton getSwitch() { return mSwitch; } } diff --git a/src/com/android/settings/bluetooth/BluetoothPairingDialogFragment.java b/src/com/android/settings/bluetooth/BluetoothPairingDialogFragment.java index a3f9bd4e1ca..33e6fc3ccbd 100644 --- a/src/com/android/settings/bluetooth/BluetoothPairingDialogFragment.java +++ b/src/com/android/settings/bluetooth/BluetoothPairingDialogFragment.java @@ -32,8 +32,8 @@ import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.CheckBox; +import android.widget.CompoundButton; import android.widget.EditText; -import android.widget.Switch; import android.widget.TextView; import androidx.annotation.VisibleForTesting; @@ -342,8 +342,8 @@ public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment i TextView pairingViewCaption = (TextView) view.findViewById(R.id.pairing_caption); TextView pairingViewContent = (TextView) view.findViewById(R.id.pairing_subhead); TextView messagePairing = (TextView) view.findViewById(R.id.pairing_code_message); - Switch contactSharing = (Switch) view.findViewById( - R.id.phonebook_sharing_message_confirm_pin); + CompoundButton contactSharing = + view.findViewById(R.id.phonebook_sharing_message_confirm_pin); view.findViewById(R.id.phonebook_sharing).setVisibility( mPairingController.isContactSharingVisible() ? View.VISIBLE : View.GONE); mPairingController.setContactSharingState(); diff --git a/src/com/android/settings/development/tare/TareHomePage.java b/src/com/android/settings/development/tare/TareHomePage.java index cfe8d5e8295..0eb93fc93a1 100644 --- a/src/com/android/settings/development/tare/TareHomePage.java +++ b/src/com/android/settings/development/tare/TareHomePage.java @@ -35,7 +35,6 @@ import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; -import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; @@ -45,7 +44,7 @@ import com.android.settings.R; public class TareHomePage extends Activity { private static final String TAG = "TareHomePage"; - private Switch mOnSwitch; + private CompoundButton mOnSwitch; private Button mRevButton; private TextView mAlarmManagerView; private TextView mJobSchedulerView; diff --git a/src/com/android/settings/users/AppRestrictionsFragment.java b/src/com/android/settings/users/AppRestrictionsFragment.java index 465da39be9f..1532448718c 100644 --- a/src/com/android/settings/users/AppRestrictionsFragment.java +++ b/src/com/android/settings/users/AppRestrictionsFragment.java @@ -45,7 +45,6 @@ import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; -import android.widget.Switch; import androidx.preference.ListPreference; import androidx.preference.MultiSelectListPreference; @@ -175,10 +174,6 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen panelOpen = open; } - List getChildren() { - return mChildren; - } - @Override public void onBindViewHolder(PreferenceViewHolder view) { super.onBindViewHolder(view); @@ -197,7 +192,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen ViewGroup widget = (ViewGroup) view.findViewById(android.R.id.widget_frame); widget.setEnabled(!isImmutable()); if (widget.getChildCount() > 0) { - final Switch toggle = (Switch) widget.getChildAt(0); + final CompoundButton toggle = (CompoundButton) widget.getChildAt(0); toggle.setEnabled(!isImmutable()); toggle.setTag(this); toggle.setClickable(true);