[Physical Keyboard] Null check before using layout descriptor

If descriptor is null, pass null to LayoutSelectedCallback.onSelected to
get default layout preview image.

Bug: 396006944
Flag: com.android.settings.keyboard.keyboard_and_touchpad_a11y_new_page_enabled
Test: atest
packages/apps/Settings/tests/robotests/src/com/android/settings/inputmethod/

Change-Id: I6b454a5f0b95c571cac15258977aab5ee12d4327
This commit is contained in:
shaoweishen
2025-03-12 05:04:02 +00:00
parent a0e0065766
commit c1438d80f4
2 changed files with 18 additions and 6 deletions

View File

@@ -26,6 +26,7 @@ import android.os.Bundle;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodSubtype;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@@ -58,7 +59,7 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
private PreferenceScreen mScreen;
private String mPreviousSelection;
private String mFinalSelectedLayoutDescriptor;
private String mSelectedLayoutDescriptor;
@Nullable private String mSelectedLayoutDescriptor;
private MetricsFeatureProvider mMetricsFeatureProvider;
private KeyboardLayoutSelectedCallback mKeyboardLayoutSelectedCallback;
@@ -186,7 +187,8 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
pref = new TickButtonPreference(mScreen.getContext());
pref.setTitle(layout.getLabel());
if (mSelectedLayoutDescriptor.equals(layout.getDescriptor())) {
if (mSelectedLayoutDescriptor != null && mSelectedLayoutDescriptor.equals(
layout.getDescriptor())) {
if (mKeyboardLayoutSelectedCallback != null) {
mKeyboardLayoutSelectedCallback.onSelected(layout);
}
@@ -197,6 +199,12 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
mScreen.addPreference(pref);
mPreferenceMap.put(pref, layout);
}
if (mSelectedLayoutDescriptor == null && mKeyboardLayoutSelectedCallback != null) {
// Pass null here since getKeyboardLayoutPreview() accept null layout, which will
// return default preview image
mKeyboardLayoutSelectedCallback.onSelected(null);
}
}
private void setLayout(TickButtonPreference preference) {
@@ -233,6 +241,6 @@ public class NewKeyboardLayoutPickerController extends BasePreferenceController
/**
* Called when KeyboardLayout been selected.
*/
void onSelected(KeyboardLayout keyboardLayout);
void onSelected(@Nullable KeyboardLayout keyboardLayout);
}
}