Fix multiple layouts marked as selected

There is a bug in the layout picker that causes all layouts with
the same name to be selected when the layout picker is open.

Bug: 389057831
Flag: EXEMPT minor bugfix
Test: Flashed on device
Change-Id: I22e6b1b5497df8a2a8ebe2e353d913affd250c06
This commit is contained in:
Josep del Rio
2025-01-10 14:16:15 +00:00
parent e28dcd02bc
commit 342f2af05f

View File

@@ -57,8 +57,8 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
private KeyboardLayout[] mKeyboardLayouts; private KeyboardLayout[] mKeyboardLayouts;
private PreferenceScreen mScreen; private PreferenceScreen mScreen;
private String mPreviousSelection; private String mPreviousSelection;
private String mFinalSelectedLayout; private String mFinalSelectedLayoutDescriptor;
private String mLayout; private String mSelectedLayoutDescriptor;
private MetricsFeatureProvider mMetricsFeatureProvider; private MetricsFeatureProvider mMetricsFeatureProvider;
private KeyboardLayoutSelectedCallback mKeyboardLayoutSelectedCallback; private KeyboardLayoutSelectedCallback mKeyboardLayoutSelectedCallback;
@@ -83,8 +83,8 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
mInputMethodSubtype = mInputMethodSubtype =
arguments.getParcelable( arguments.getParcelable(
InputPeripheralsSettingsUtils.EXTRA_INPUT_METHOD_SUBTYPE); InputPeripheralsSettingsUtils.EXTRA_INPUT_METHOD_SUBTYPE);
mLayout = getSelectedLayoutLabel(); mSelectedLayoutDescriptor = getSelectedLayoutDescriptor();
mFinalSelectedLayout = mLayout; mFinalSelectedLayoutDescriptor = mSelectedLayoutDescriptor;
mKeyboardLayouts = mIm.getKeyboardLayoutListForInputDevice( mKeyboardLayouts = mIm.getKeyboardLayoutListForInputDevice(
mInputDeviceIdentifier, mUserId, mInputMethodInfo, mInputMethodSubtype); mInputDeviceIdentifier, mUserId, mInputMethodInfo, mInputMethodSubtype);
InputPeripheralsSettingsUtils.sortKeyboardLayoutsByLabel(mKeyboardLayouts); InputPeripheralsSettingsUtils.sortKeyboardLayoutsByLabel(mKeyboardLayouts);
@@ -106,8 +106,12 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
@Override @Override
public void onStop() { public void onStop() {
if (mLayout != null && !mLayout.equals(mFinalSelectedLayout)) { if (mSelectedLayoutDescriptor != null
String change = "From:" + mLayout + ", to:" + mFinalSelectedLayout; && !mSelectedLayoutDescriptor.equals(mFinalSelectedLayoutDescriptor)) {
String change = "From:"
+ getLayoutLabel(mSelectedLayoutDescriptor)
+ ", to:"
+ getLayoutLabel(mFinalSelectedLayoutDescriptor);
mMetricsFeatureProvider.action( mMetricsFeatureProvider.action(
mContext, SettingsEnums.ACTION_PK_LAYOUT_CHANGED, change); mContext, SettingsEnums.ACTION_PK_LAYOUT_CHANGED, change);
} }
@@ -152,7 +156,7 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
} }
setLayout(pref); setLayout(pref);
mPreviousSelection = preference.getKey(); mPreviousSelection = preference.getKey();
mFinalSelectedLayout = pref.getTitle().toString(); mFinalSelectedLayoutDescriptor = mPreviousSelection;
return true; return true;
} }
@@ -182,12 +186,12 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
pref = new TickButtonPreference(mScreen.getContext()); pref = new TickButtonPreference(mScreen.getContext());
pref.setTitle(layout.getLabel()); pref.setTitle(layout.getLabel());
if (mLayout.equals(layout.getLabel())) { if (mSelectedLayoutDescriptor.equals(layout.getDescriptor())) {
if (mKeyboardLayoutSelectedCallback != null) { if (mKeyboardLayoutSelectedCallback != null) {
mKeyboardLayoutSelectedCallback.onSelected(layout); mKeyboardLayoutSelectedCallback.onSelected(layout);
} }
pref.setSelected(true); pref.setSelected(true);
mPreviousSelection = layout.getDescriptor(); mPreviousSelection = mSelectedLayoutDescriptor;
} }
pref.setKey(layout.getDescriptor()); pref.setKey(layout.getDescriptor());
mScreen.addPreference(pref); mScreen.addPreference(pref);
@@ -204,15 +208,19 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
mPreferenceMap.get(preference).getDescriptor()); mPreferenceMap.get(preference).getDescriptor());
} }
private String getSelectedLayoutLabel() { private String getSelectedLayoutDescriptor() {
String label = mContext.getString(R.string.keyboard_default_layout);
KeyboardLayoutSelectionResult result = InputPeripheralsSettingsUtils.getKeyboardLayout( KeyboardLayoutSelectionResult result = InputPeripheralsSettingsUtils.getKeyboardLayout(
mIm, mUserId, mInputDeviceIdentifier, mInputMethodInfo, mInputMethodSubtype); mIm, mUserId, mInputDeviceIdentifier, mInputMethodInfo, mInputMethodSubtype);
return result.getLayoutDescriptor();
}
private String getLayoutLabel(String descriptor) {
String label = mContext.getString(R.string.keyboard_default_layout);
KeyboardLayout[] keyboardLayouts = InputPeripheralsSettingsUtils.getKeyboardLayouts( KeyboardLayout[] keyboardLayouts = InputPeripheralsSettingsUtils.getKeyboardLayouts(
mIm, mUserId, mInputDeviceIdentifier, mInputMethodInfo, mInputMethodSubtype); mIm, mUserId, mInputDeviceIdentifier, mInputMethodInfo, mInputMethodSubtype);
if (result.getLayoutDescriptor() != null) { if (descriptor != null) {
for (KeyboardLayout keyboardLayout : keyboardLayouts) { for (KeyboardLayout keyboardLayout : keyboardLayouts) {
if (keyboardLayout.getDescriptor().equals(result.getLayoutDescriptor())) { if (keyboardLayout.getDescriptor().equals(descriptor)) {
label = keyboardLayout.getLabel(); label = keyboardLayout.getLabel();
break; break;
} }