Merge "Always use Spp to generate SettingsPage in Settings"
diff --git a/res-product/values/strings.xml b/res-product/values/strings.xml
index 2d411e4..cd208c1 100644
--- a/res-product/values/strings.xml
+++ b/res-product/values/strings.xml
@@ -667,4 +667,25 @@
     <string name="reset_internet_text" product="default">This will end your phone call</string>
     <!-- Description for interrupting the voice call alert. [CHAR_LIMIT=NONE] -->
     <string name="reset_internet_text" product="tablet">This will end your phone call</string>
+
+    <!-- An explanation text that the pattern needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
+    <string name="lockpassword_confirm_your_pattern_details_frp" product="default">Your phone was reset to factory settings. To use this phone, enter your previous pattern.</string>
+    <!-- An explanation text that the pattern needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
+    <string name="lockpassword_confirm_your_pattern_details_frp" product="tablet">Your tablet was reset to factory settings. To use this tablet, enter your previous pattern.</string>
+    <!-- An explanation text that the pattern needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
+    <string name="lockpassword_confirm_your_pattern_details_frp" product="device">Your device was reset to factory settings. To use this device, enter your previous pattern.</string>
+
+    <!-- An explanation text that the pin needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
+    <string name="lockpassword_confirm_your_pin_details_frp" product="default">Your phone was reset to factory settings. To use this phone, enter your previous PIN.</string>
+    <!-- An explanation text that the pin needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
+    <string name="lockpassword_confirm_your_pin_details_frp" product="tablet">Your tablet was reset to factory settings. To use this tablet, enter your previous PIN.</string>
+    <!-- An explanation text that the pin needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
+    <string name="lockpassword_confirm_your_pin_details_frp" product="device">Your device was reset to factory settings. To use this device, enter your previous PIN.</string>
+
+    <!-- An explanation text that the password needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
+    <string name="lockpassword_confirm_your_password_details_frp" product="default">Your phone was reset to factory settings. To use this phone, enter your previous password.</string>
+    <!-- An explanation text that the password needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
+    <string name="lockpassword_confirm_your_password_details_frp" product="tablet">Your tablet was reset to factory settings. To use this tablet, enter your previous password.</string>
+    <!-- An explanation text that the password needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
+    <string name="lockpassword_confirm_your_password_details_frp" product="device">Your device was reset to factory settings. To use this device, enter your previous password.</string>
 </resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index bf04611..273e3ae 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -3334,27 +3334,6 @@
          continue. [CHAR LIMIT=100] -->
     <string name="lockpassword_strong_auth_required_work_password">For added security, enter your work password</string>
 
-    <!-- An explanation text that the pattern needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
-    <string name="lockpassword_confirm_your_pattern_details_frp" product="default">Your phone was reset to factory settings. To use this phone, enter your previous pattern.</string>
-    <!-- An explanation text that the pattern needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
-    <string name="lockpassword_confirm_your_pattern_details_frp" product="tablet">Your tablet was reset to factory settings. To use this tablet, enter your previous pattern.</string>
-    <!-- An explanation text that the pattern needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
-    <string name="lockpassword_confirm_your_pattern_details_frp" product="device">Your device was reset to factory settings. To use this device, enter your previous pattern.</string>
-
-    <!-- An explanation text that the pin needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
-    <string name="lockpassword_confirm_your_pin_details_frp" product="default">Your phone was reset to factory settings. To use this phone, enter your previous PIN.</string>
-    <!-- An explanation text that the pin needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
-    <string name="lockpassword_confirm_your_pin_details_frp" product="tablet">Your tablet was reset to factory settings. To use this tablet, enter your previous PIN.</string>
-    <!-- An explanation text that the pin needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
-    <string name="lockpassword_confirm_your_pin_details_frp" product="device">Your device was reset to factory settings. To use this device, enter your previous PIN.</string>
-
-    <!-- An explanation text that the password needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
-    <string name="lockpassword_confirm_your_password_details_frp" product="default">Your phone was reset to factory settings. To use this phone, enter your previous password.</string>
-    <!-- An explanation text that the password needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
-    <string name="lockpassword_confirm_your_password_details_frp" product="tablet">Your tablet was reset to factory settings. To use this tablet, enter your previous password.</string>
-    <!-- An explanation text that the password needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
-    <string name="lockpassword_confirm_your_password_details_frp" product="device">Your device was reset to factory settings. To use this device, enter your previous password.</string>
-
     <!-- Header shown when pattern needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
     <string name="lockpassword_confirm_your_pattern_header_frp">Verify pattern</string>
     <!-- Header shown when the pin needs to be solved because the device was factory reset. [CHAR LIMIT=100] -->
@@ -3828,6 +3807,8 @@
     <string name="language_and_input_for_work_category_title">Work profile keyboards &amp; tools</string>
     <!-- Title for the 'Virtual keyboards for work' preference. [CHAR LIMIT=45] -->
     <string name="virtual_keyboards_for_work_title">On-screen keyboard for work</string>
+    <!-- Summary text for none selected keyboard default layout -->
+    <string name="keyboard_default_layout">Default</string>
 
     <!-- Title for the button to trigger the 'trackpad settings' page if only connect with a touchpad. [CHAR LIMIT=35] -->
     <string name="trackpad_settings">Touchpad</string>
@@ -10259,14 +10240,14 @@
     <!-- Apps > App Details > Wifi access > Description. [CHAR LIMIT=NONE] -->
     <string name="change_wifi_state_app_detail_summary">Allow this app to turn Wi-Fi on or off, scan and connect to Wi-Fi networks, add or remove networks, or start a local-only hotspot</string>
 
-    <!-- Title for Nfc Tag apps control [CHAR LIMIT=35] -->
-    <string name="change_nfc_tag_apps_title">NFC Tag apps control</string>
+    <!-- Title for NFC launch [CHAR LIMIT=35] -->
+    <string name="change_nfc_tag_apps_title">NFC launch</string>
 
-    <!-- Apps > App Details > Nfc Tag apps control > Switch title. [CHAR LIMIT=NONE] -->
-    <string name="change_nfc_tag_apps_detail_switch">Allow app to popup upon NFC tags detected</string>
+    <!-- Apps > App Details > Nfc launch > Switch title. [CHAR LIMIT=NONE] -->
+    <string name="change_nfc_tag_apps_detail_switch">Allow launch on NFC scan</string>
 
-    <!-- Apps > App Details > Nfc Tag apps control > Description. [CHAR LIMIT=NONE] -->
-    <string name="change_nfc_tag_apps_detail_summary">Allow this app to launch and get tag contents when the device detests NFC tags</string>
+    <!-- Apps > App Details > Nfc launch > Description. [CHAR LIMIT=NONE] -->
+    <string name="change_nfc_tag_apps_detail_summary">Allow this app to launch when a NFC tag is scanned.\nIf this permission is on, the app will be available as an option whenever a tag is detected.</string>
 
     <!-- Title for media output settings -->
     <string name="media_output_title">Play media to</string>
diff --git a/res/xml/keyboard_settings_enabled_locales_list.xml b/res/xml/keyboard_settings_enabled_locales_list.xml
index 06b8d5f..79d45ca 100644
--- a/res/xml/keyboard_settings_enabled_locales_list.xml
+++ b/res/xml/keyboard_settings_enabled_locales_list.xml
@@ -16,8 +16,5 @@
 <PreferenceScreen
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:title="@string/physical_keyboard_title">
-    <PreferenceCategory
-        android:key="enabled_locales_keyboard_layout"
-        android:title="@string/enabled_locales_keyboard_layout">
-    </PreferenceCategory>
+
 </PreferenceScreen>
\ No newline at end of file
diff --git a/src/com/android/settings/accessibility/OWNERS b/src/com/android/settings/accessibility/OWNERS
index ca4b880..1091a04 100644
--- a/src/com/android/settings/accessibility/OWNERS
+++ b/src/com/android/settings/accessibility/OWNERS
@@ -1,6 +1,7 @@
 # Default reviewers for this and subdirectories.
 danielnorman@google.com
 menghanli@google.com
+thomasli@google.com
 
 per-file HapticFeedbackIntensityPreferenceController.java = michaelwr@google.com
 per-file *Vibration* = michaelwr@google.com
diff --git a/src/com/android/settings/deviceinfo/simstatus/SimStatusPreferenceController.java b/src/com/android/settings/deviceinfo/simstatus/SimStatusPreferenceController.java
index ae002f3..828e016 100644
--- a/src/com/android/settings/deviceinfo/simstatus/SimStatusPreferenceController.java
+++ b/src/com/android/settings/deviceinfo/simstatus/SimStatusPreferenceController.java
@@ -24,6 +24,8 @@
 
 import androidx.annotation.VisibleForTesting;
 import androidx.fragment.app.Fragment;
+import androidx.lifecycle.LifecycleOwner;
+import androidx.lifecycle.LiveData;
 import androidx.lifecycle.Observer;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceCategory;
@@ -44,6 +46,7 @@
 
     private Fragment mFragment;
     private SlotSimStatus mSlotSimStatus;
+    private Observer<LifecycleOwner> mLifecycleOwnerObserver;
     private Observer mSimChangeObserver;
 
     public SimStatusPreferenceController(Context context, String prefKey) {
@@ -112,13 +115,25 @@
         if (mFragment == null) {
             return;
         }
-        final int simSlot = getSimSlotIndex();
-        if (mSimChangeObserver == null) {
-            mSimChangeObserver = x -> updateStateBySlot(preference, simSlot);
-            mFragment.getViewLifecycleOwnerLiveData().observeForever(lifecycleOwner -> {
-                mSlotSimStatus.observe(lifecycleOwner, mSimChangeObserver);
-            });
-        } else {
+        if (mLifecycleOwnerObserver == null) {
+            final LiveData<LifecycleOwner> dataLifecycleOwner
+                    = mFragment.getViewLifecycleOwnerLiveData();
+            mLifecycleOwnerObserver = owner -> {
+                if (owner != null) {
+                    final int simSlot = getSimSlotIndex();
+                    mSimChangeObserver = x -> updateStateBySlot(preference, simSlot);
+                    mSlotSimStatus.observe(owner, mSimChangeObserver);
+                } else {
+                    if (mSimChangeObserver != null) {
+                        mSlotSimStatus.removeObserver(mSimChangeObserver);
+                        mSimChangeObserver = null;
+                    }
+                    dataLifecycleOwner.removeObserver(mLifecycleOwnerObserver);
+                }
+            };
+            dataLifecycleOwner.observeForever(mLifecycleOwnerObserver);
+        } else if (mSimChangeObserver != null) {
+            final int simSlot = getSimSlotIndex();
             updateStateBySlot(preference, simSlot);
         }
     }
diff --git a/src/com/android/settings/inputmethod/NewKeyboardLayoutEnabledLocalesFragment.java b/src/com/android/settings/inputmethod/NewKeyboardLayoutEnabledLocalesFragment.java
index 5c0f88f..9311c97 100644
--- a/src/com/android/settings/inputmethod/NewKeyboardLayoutEnabledLocalesFragment.java
+++ b/src/com/android/settings/inputmethod/NewKeyboardLayoutEnabledLocalesFragment.java
@@ -20,64 +20,124 @@
 import android.content.Context;
 import android.hardware.input.InputDeviceIdentifier;
 import android.hardware.input.InputManager;
+import android.hardware.input.KeyboardLayout;
 import android.os.Bundle;
+import android.os.UserHandle;
 import android.view.InputDevice;
+import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodManager;
+import android.view.inputmethod.InputMethodSubtype;
 
 import androidx.preference.Preference;
 import androidx.preference.PreferenceCategory;
+import androidx.preference.PreferenceScreen;
 
 import com.android.settings.R;
 import com.android.settings.core.SubSettingLauncher;
 import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.inputmethod.NewKeyboardSettingsUtils.KeyboardInfo;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
 
 public class NewKeyboardLayoutEnabledLocalesFragment extends DashboardFragment
         implements InputManager.InputDeviceListener {
 
     private static final String TAG = "NewKeyboardLayoutEnabledLocalesFragment";
-    private static final String PREF_KEY_ENABLED_LOCALES = "enabled_locales_keyboard_layout";
-
-    static final String EXTRA_KEYBOARD_DEVICE_NAME = "extra_keyboard_device_name";
 
     private InputManager mIm;
+    private InputMethodManager mImm;
     private InputDeviceIdentifier mInputDeviceIdentifier;
+    private int mUserId;
     private int mInputDeviceId;
     private Context mContext;
+    private Map<String, KeyboardInfo> mKeyboardLanguageLayouts = new HashMap<>();
 
     @Override
     public void onActivityCreated(final Bundle icicle) {
         super.onActivityCreated(icicle);
-
         Bundle arguments = getArguments();
-        final String title = arguments.getString(EXTRA_KEYBOARD_DEVICE_NAME);
-        mInputDeviceIdentifier = arguments.getParcelable(
-                KeyboardLayoutPickerFragment.EXTRA_INPUT_DEVICE_IDENTIFIER);
+        final String title =
+                arguments.getString(NewKeyboardSettingsUtils.EXTRA_KEYBOARD_DEVICE_NAME);
+        mInputDeviceIdentifier =
+                arguments.getParcelable(NewKeyboardSettingsUtils.EXTRA_INPUT_DEVICE_IDENTIFIER);
         getActivity().setTitle(title);
-        final PreferenceCategory category = findPreference(PREF_KEY_ENABLED_LOCALES);
+        updateCheckedState();
+    }
 
-        // TODO(b/252816846): Need APIs to get the available keyboards from Inputmanager.
-        // For example: InputMethodManager.getEnabledInputMethodLocales()
-        //              InputManager.getKeyboardLayoutForLocale()
-        // Hardcode the default value for demo purpose
-        String[] keyboardLanguages = {"English (US)", "German (Germany)", "Spanish (Spain)"};
-        String[] keyboardLayouts = {"English (US)", "German", "Spanish"};
-        for (int i = 0; i < keyboardLanguages.length; i++) {
+    private void updateCheckedState() {
+        PreferenceScreen preferenceScreen = getPreferenceScreen();
+        preferenceScreen.removeAll();
+        List<InputMethodInfo> infoList = mImm.getEnabledInputMethodListAsUser(mUserId);
+        for (InputMethodInfo info : infoList) {
+            mKeyboardLanguageLayouts.clear();
+            List<InputMethodSubtype> subtypes =
+                    mImm.getEnabledInputMethodSubtypeList(info, true);
+            for (InputMethodSubtype subtype : subtypes) {
+                if (subtype.isSuitableForPhysicalKeyboardLayoutMapping()) {
+                    mapLanguageWithLayout(info, subtype);
+                }
+            }
+            updatePreferenceLayout(preferenceScreen, info);
+        }
+    }
+
+    private void mapLanguageWithLayout(InputMethodInfo info, InputMethodSubtype subtype) {
+        KeyboardLayout[] keyboardLayouts = getKeyboardLayouts(info, subtype);
+        String layout = getKeyboardLayout(info, subtype);
+        String language = getLanguage(info, subtype);
+        if (layout != null) {
+            for (int i = 0; i < keyboardLayouts.length; i++) {
+                if (keyboardLayouts[i].getDescriptor().equals(layout)) {
+                    KeyboardInfo keyboardInfo = new KeyboardInfo(
+                            language,
+                            keyboardLayouts[i].getLabel(),
+                            info,
+                            subtype);
+                    mKeyboardLanguageLayouts.put(subtype.getLanguageTag(), keyboardInfo);
+                    break;
+                }
+            }
+        } else {
+            // if there is no auto-selected layout, we should show "Default"
+            KeyboardInfo keyboardInfo = new KeyboardInfo(
+                    language,
+                    mContext.getString(R.string.keyboard_default_layout),
+                    info,
+                    subtype);
+            mKeyboardLanguageLayouts.put(subtype.getLanguageTag(), keyboardInfo);
+        }
+    }
+
+    private void updatePreferenceLayout(PreferenceScreen preferenceScreen, InputMethodInfo info) {
+        if (mKeyboardLanguageLayouts.isEmpty()) {
+            return;
+        }
+        PreferenceCategory preferenceCategory = new PreferenceCategory(mContext);
+        preferenceCategory.setTitle(info.loadLabel(mContext.getPackageManager()).toString());
+        preferenceCategory.setKey(info.getPackageName());
+        preferenceScreen.addPreference(preferenceCategory);
+        for (Map.Entry<String, KeyboardInfo> entry : mKeyboardLanguageLayouts.entrySet()) {
             final Preference pref = new Preference(mContext);
-            String key = "keyboard_language_label_" + String.valueOf(i);
-            String keyboardLanguageTitle = keyboardLanguages[i];
-            String keyboardLanguageSummary = keyboardLayouts[i];
-            // TODO: Waiting for new API to use a prefix with special number to setKey
+            String key = "keyboard_language_" + entry.getKey();
+            NewKeyboardSettingsUtils.KeyboardInfo keyboardInfo = entry.getValue();
             pref.setKey(key);
-            pref.setTitle(keyboardLanguageTitle);
-            pref.setSummary(keyboardLanguageSummary);
+            pref.setTitle(keyboardInfo.getLanguage());
+            pref.setSummary(keyboardInfo.getLayout());
             pref.setOnPreferenceClickListener(
                     preference -> {
                         showKeyboardLayoutPicker(
-                                keyboardLanguageTitle,
-                                keyboardLanguageSummary,
-                                mInputDeviceIdentifier);
+                                keyboardInfo.getLanguage(),
+                                keyboardInfo.getLayout(),
+                                mInputDeviceIdentifier,
+                                mUserId,
+                                keyboardInfo.getInputMethodInfo(),
+                                keyboardInfo.getInputMethodSubtype());
                         return true;
                     });
-            category.addPreference(pref);
+            preferenceCategory.addPreference(pref);
         }
     }
 
@@ -96,7 +156,7 @@
     @Override
     public void onInputDeviceChanged(int deviceId) {
         if (mInputDeviceId >= 0 && deviceId == mInputDeviceId) {
-            // TODO(b/252816846): Need APIs to update the available keyboards.
+            updateCheckedState();
         }
     }
 
@@ -105,7 +165,9 @@
         super.onCreate(savedInstanceState);
         mContext = getContext();
         mIm = mContext.getSystemService(InputManager.class);
+        mImm = mContext.getSystemService(InputMethodManager.class);
         mInputDeviceId = -1;
+        mUserId = UserHandle.myUserId();
     }
 
     @Override
@@ -131,7 +193,7 @@
     @Override
     public void onResume() {
         super.onResume();
-        // TODO(b/252816846): Need APIs to get the available keyboards from Inputmanager.
+        updateCheckedState();
     }
 
     @Override
@@ -149,17 +211,50 @@
         return R.xml.keyboard_settings_enabled_locales_list;
     }
 
-    private void showKeyboardLayoutPicker(String language, String layout,
-            InputDeviceIdentifier inputDeviceIdentifier) {
+    private void showKeyboardLayoutPicker(
+            String language,
+            String layout,
+            InputDeviceIdentifier inputDeviceIdentifier,
+            int userId,
+            InputMethodInfo inputMethodInfo,
+            InputMethodSubtype inputMethodSubtype) {
         Bundle arguments = new Bundle();
-        arguments.putParcelable(KeyboardLayoutPickerFragment.EXTRA_INPUT_DEVICE_IDENTIFIER,
-                inputDeviceIdentifier);
-        arguments.putString(NewKeyboardLayoutPickerFragment.EXTRA_TITLE, language);
-        arguments.putString(NewKeyboardLayoutPickerFragment.EXTRA_KEYBOARD_LAYOUT, layout);
+        arguments.putParcelable(
+                NewKeyboardSettingsUtils.EXTRA_INPUT_DEVICE_IDENTIFIER, inputDeviceIdentifier);
+        arguments.putParcelable(
+                NewKeyboardSettingsUtils.EXTRA_INPUT_METHOD_INFO, inputMethodInfo);
+        arguments.putParcelable(
+                NewKeyboardSettingsUtils.EXTRA_INPUT_METHOD_SUBTYPE, inputMethodSubtype);
+        arguments.putInt(NewKeyboardSettingsUtils.EXTRA_USER_ID, userId);
+        arguments.putString(NewKeyboardSettingsUtils.EXTRA_TITLE, language);
+        arguments.putString(NewKeyboardSettingsUtils.EXTRA_KEYBOARD_LAYOUT, layout);
         new SubSettingLauncher(mContext)
                 .setSourceMetricsCategory(getMetricsCategory())
                 .setDestination(NewKeyboardLayoutPickerFragment.class.getName())
                 .setArguments(arguments)
                 .launch();
     }
+
+    private KeyboardLayout[] getKeyboardLayouts(InputMethodInfo info, InputMethodSubtype subtype) {
+        return mIm.getKeyboardLayoutListForInputDevice(
+                mInputDeviceIdentifier, mUserId, info, subtype);
+    }
+
+    private String getKeyboardLayout(InputMethodInfo info, InputMethodSubtype subtype) {
+        return mIm.getKeyboardLayoutForInputDevice(
+                mInputDeviceIdentifier, mUserId, info, subtype);
+    }
+
+    private String getLanguage(InputMethodInfo info, InputMethodSubtype subtype) {
+        String language;
+        if (subtype.getLanguageTag().isEmpty()) {
+            language = subtype.getDisplayName(
+                    mContext,
+                    info.getPackageName(),
+                    info.getServiceInfo().applicationInfo).toString();
+        } else {
+            language = Locale.forLanguageTag(subtype.getLanguageTag()).getDisplayName();
+        }
+        return language;
+    }
 }
diff --git a/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerContent.java b/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerContent.java
index dc94306..bb452f7 100644
--- a/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerContent.java
+++ b/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerContent.java
@@ -20,6 +20,8 @@
 import android.content.Context;
 import android.hardware.input.InputDeviceIdentifier;
 import android.os.Bundle;
+import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.settings.R;
 import com.android.settings.dashboard.DashboardFragment;
@@ -28,31 +30,28 @@
 
     private static final String TAG = "KeyboardLayoutPicker";
 
-    static final String EXTRA_TITLE = "keyboard_layout_picker_title";
-    static final String EXTRA_KEYBOARD_LAYOUT = "keyboard_layout";
-
-    /**
-     * Intent extra: The input device descriptor of the keyboard whose keyboard
-     * layout is to be changed.
-     */
-    public static final String EXTRA_INPUT_DEVICE_IDENTIFIER = "input_device_identifier";
-
     @Override
     public void onAttach(Context context) {
         super.onAttach(context);
 
         Bundle arguments = getArguments();
-        final String title = arguments.getString(EXTRA_TITLE);
-        final String layout = arguments.getString(EXTRA_KEYBOARD_LAYOUT);
+        final String title = arguments.getString(NewKeyboardSettingsUtils.EXTRA_TITLE);
+        final String layout = arguments.getString(NewKeyboardSettingsUtils.EXTRA_KEYBOARD_LAYOUT);
+        final int userId = arguments.getInt(NewKeyboardSettingsUtils.EXTRA_USER_ID);
         final InputDeviceIdentifier inputDeviceIdentifier =
-                arguments.getParcelable(EXTRA_INPUT_DEVICE_IDENTIFIER);
+                arguments.getParcelable(NewKeyboardSettingsUtils.EXTRA_INPUT_DEVICE_IDENTIFIER);
+        final InputMethodInfo inputMethodInfo =
+                arguments.getParcelable(NewKeyboardSettingsUtils.EXTRA_INPUT_METHOD_INFO);
+        final InputMethodSubtype inputMethodSubtype =
+                arguments.getParcelable(NewKeyboardSettingsUtils.EXTRA_INPUT_METHOD_SUBTYPE);
+
 
         if (inputDeviceIdentifier == null) {
             getActivity().finish();
         }
         getActivity().setTitle(title);
-        use(NewKeyboardLayoutPickerController.class).initialize(this /*parent*/,
-                inputDeviceIdentifier, layout);
+        use(NewKeyboardLayoutPickerController.class).initialize(this /*parent*/, userId,
+                inputDeviceIdentifier, inputMethodInfo, inputMethodSubtype, layout);
     }
 
     @Override
diff --git a/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerController.java b/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerController.java
index 4a598d5..eb0a7aa 100644
--- a/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerController.java
+++ b/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerController.java
@@ -21,6 +21,8 @@
 import android.hardware.input.InputManager;
 import android.hardware.input.KeyboardLayout;
 import android.view.InputDevice;
+import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodSubtype;
 
 import androidx.fragment.app.Fragment;
 import androidx.preference.Preference;
@@ -31,7 +33,6 @@
 import com.android.settingslib.core.lifecycle.events.OnStart;
 import com.android.settingslib.core.lifecycle.events.OnStop;
 
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -42,7 +43,11 @@
 
     private Fragment mParent;
     private int mInputDeviceId;
+    private int mUserId;
     private InputDeviceIdentifier mInputDeviceIdentifier;
+    private InputMethodInfo mInputMethodInfo;
+    private InputMethodSubtype mInputMethodSubtype;
+
     private KeyboardLayout[] mKeyboardLayouts;
     private PreferenceScreen mScreen;
     private String mPreviousSelection;
@@ -55,13 +60,16 @@
         mPreferenceMap = new HashMap<>();
     }
 
-    public void initialize(Fragment parent, InputDeviceIdentifier inputDeviceIdentifier,
-            String layout) {
-        mLayout = layout;
+    public void initialize(Fragment parent, int userId, InputDeviceIdentifier inputDeviceIdentifier,
+            InputMethodInfo imeInfo, InputMethodSubtype imeSubtype, String layout) {
         mParent = parent;
+        mUserId = userId;
         mInputDeviceIdentifier = inputDeviceIdentifier;
-        mKeyboardLayouts = mIm.getKeyboardLayoutsForInputDevice(mInputDeviceIdentifier);
-        Arrays.sort(mKeyboardLayouts);
+        mInputMethodInfo = imeInfo;
+        mInputMethodSubtype = imeSubtype;
+        mLayout = layout;
+        mKeyboardLayouts = mIm.getKeyboardLayoutListForInputDevice(
+                inputDeviceIdentifier, userId, imeInfo, imeSubtype);
     }
 
     @Override
@@ -102,15 +110,12 @@
         }
 
         final KeyboardLayoutPreference pref = (KeyboardLayoutPreference) preference;
-        // TODO(b/259530132): Need APIs to update the available keyboards for input device.
-        // For example:
-        // inputManager.setCurrentKeyboardLayoutForInputDevice(
-        //            InputDevice..., Userid..., ImeSubType ..., String keyboardLayoutDescriptor)
+        pref.setCheckMark(true);
         if (mPreviousSelection != null && !mPreviousSelection.equals(preference.getKey())) {
             KeyboardLayoutPreference preSelectedPref = mScreen.findPreference(mPreviousSelection);
-            pref.setCheckMark(true);
             preSelectedPref.setCheckMark(false);
         }
+        setLayout(pref);
         mPreviousSelection = preference.getKey();
         return true;
     }
@@ -129,13 +134,7 @@
 
     @Override
     public void onInputDeviceChanged(int deviceId) {
-        if (mInputDeviceId >= 0 && deviceId == mInputDeviceId) {
-            updateCheckedState();
-        }
-    }
-
-    private void updateCheckedState() {
-        // TODO(b/259530132): Need API to update the keyboard language layout list.
+        // Do nothing.
     }
 
     private void createPreferenceHierarchy() {
@@ -143,14 +142,22 @@
             final KeyboardLayoutPreference pref;
             if (mLayout.equals(layout.getLabel())) {
                 pref = new KeyboardLayoutPreference(mScreen.getContext(), layout.getLabel(), true);
-                mPreviousSelection = layout.getLabel();
+                mPreviousSelection = layout.getDescriptor();
             } else {
                 pref = new KeyboardLayoutPreference(mScreen.getContext(), layout.getLabel(), false);
             }
-            // TODO: Waiting for new API to use a prefix with special number to setKey
-            pref.setKey(layout.getLabel());
+            pref.setKey(layout.getDescriptor());
             mScreen.addPreference(pref);
             mPreferenceMap.put(pref, layout);
         }
     }
+
+    private void setLayout(KeyboardLayoutPreference preference) {
+        mIm.setKeyboardLayoutForInputDevice(
+                mInputDeviceIdentifier,
+                mUserId,
+                mInputMethodInfo,
+                mInputMethodSubtype,
+                mPreferenceMap.get(preference).getDescriptor());
+    }
 }
diff --git a/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerFragment.java b/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerFragment.java
index c2f41a2..169b84b 100644
--- a/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerFragment.java
+++ b/src/com/android/settings/inputmethod/NewKeyboardLayoutPickerFragment.java
@@ -27,15 +27,6 @@
 
 public class NewKeyboardLayoutPickerFragment extends Fragment {
 
-    static final String EXTRA_TITLE = "keyboard_layout_picker_title";
-    static final String EXTRA_KEYBOARD_LAYOUT = "keyboard_layout";
-
-    /**
-     * Intent extra: The input device descriptor of the keyboard whose keyboard
-     * layout is to be changed.
-     */
-    public static final String EXTRA_INPUT_DEVICE_IDENTIFIER = "input_device_identifier";
-
     private ViewGroup mFragmentView;
 
     @Override
diff --git a/src/com/android/settings/inputmethod/NewKeyboardSettingsUtils.java b/src/com/android/settings/inputmethod/NewKeyboardSettingsUtils.java
new file mode 100644
index 0000000..9a1b2e4
--- /dev/null
+++ b/src/com/android/settings/inputmethod/NewKeyboardSettingsUtils.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.inputmethod;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodManager;
+import android.view.inputmethod.InputMethodSubtype;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Utilities of keyboard settings
+ */
+public class NewKeyboardSettingsUtils {
+
+    static final String EXTRA_KEYBOARD_DEVICE_NAME = "extra_keyboard_device_name";
+    static final String EXTRA_TITLE = "keyboard_layout_picker_title";
+    static final String EXTRA_KEYBOARD_LAYOUT = "keyboard_layout";
+    static final String EXTRA_USER_ID = "user_id";
+    static final String EXTRA_INPUT_DEVICE_IDENTIFIER = "input_device_identifier";
+    static final String EXTRA_INPUT_METHOD_INFO = "input_method_info";
+    static final String EXTRA_INPUT_METHOD_SUBTYPE = "input_method_subtype";
+
+    static InputMethodInfo getActiveIme(Context context, InputMethodManager imm) {
+        InputMethodInfo activeIme = null;
+        List<InputMethodInfo> infoList = imm.getEnabledInputMethodList();
+        String imeId = Settings.Secure.getStringForUser(context.getContentResolver(),
+                Settings.Secure.DEFAULT_INPUT_METHOD, context.getUserId());
+        for (InputMethodInfo method : infoList) {
+            if (method.getId().equals(imeId)) {
+                activeIme = method;
+            }
+        }
+        return activeIme;
+    }
+
+    static List<String> getSuitableImeLabels(Context context, InputMethodManager imm, int userId) {
+        List<String> suitableInputMethodInfoLabels = new ArrayList<>();
+        List<InputMethodInfo> infoList = imm.getEnabledInputMethodListAsUser(userId);
+        for (InputMethodInfo info : infoList) {
+            List<InputMethodSubtype> subtypes =
+                    imm.getEnabledInputMethodSubtypeList(info, true);
+            for (InputMethodSubtype subtype : subtypes) {
+                if (subtype.isSuitableForPhysicalKeyboardLayoutMapping()) {
+                    suitableInputMethodInfoLabels.add(
+                            info.loadLabel(context.getPackageManager()).toString());
+                    break;
+                }
+            }
+        }
+        return suitableInputMethodInfoLabels;
+    }
+
+    static class KeyboardInfo {
+        String mLanguage;
+        String mLayout;
+        InputMethodInfo mInputMethodInfo;
+        InputMethodSubtype mInputMethodSubtype;
+
+        KeyboardInfo(
+                String language,
+                String layout,
+                InputMethodInfo inputMethodInfo,
+                InputMethodSubtype inputMethodSubtype) {
+            mLanguage = language;
+            mLayout = layout;
+            mInputMethodInfo = inputMethodInfo;
+            mInputMethodSubtype = inputMethodSubtype;
+        }
+
+        String getLanguage() {
+            return mLanguage;
+        }
+
+        String getLayout() {
+            return mLayout;
+        }
+
+        InputMethodInfo getInputMethodInfo() {
+            return mInputMethodInfo;
+        }
+
+        InputMethodSubtype getInputMethodSubtype() {
+            return mInputMethodSubtype;
+        }
+    }
+}
diff --git a/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java b/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java
index 7d43d7c..20a634e 100644
--- a/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java
+++ b/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java
@@ -35,6 +35,7 @@
 import android.text.TextUtils;
 import android.util.FeatureFlagUtils;
 import android.view.InputDevice;
+import android.view.inputmethod.InputMethodManager;
 
 import androidx.preference.Preference;
 import androidx.preference.Preference.OnPreferenceChangeListener;
@@ -54,9 +55,7 @@
 import java.text.Collator;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.Objects;
 
 @SearchIndexable
@@ -73,6 +72,7 @@
     private final ArrayList<HardKeyboardDeviceInfo> mLastHardKeyboards = new ArrayList<>();
 
     private InputManager mIm;
+    private InputMethodManager mImm;
     @NonNull
     private PreferenceCategory mKeyboardAssistanceCategory;
     @NonNull
@@ -90,6 +90,7 @@
         mBluetoothAddress = activity.getIntent().getStringExtra(EXTRA_BT_ADDRESS);
         addPreferencesFromResource(R.xml.physical_keyboard_settings);
         mIm = Preconditions.checkNotNull(activity.getSystemService(InputManager.class));
+        mImm = Preconditions.checkNotNull(activity.getSystemService(InputMethodManager.class));
         mKeyboardAssistanceCategory = Preconditions.checkNotNull(
                 (PreferenceCategory) findPreference(KEYBOARD_OPTIONS_CATEGORY));
         mShowVirtualKeyboardSwitch = Preconditions.checkNotNull(
@@ -194,21 +195,15 @@
             final Preference pref = new Preference(getPrefContext());
             pref.setTitle(hardKeyboardDeviceInfo.mDeviceName);
             if (mIsNewKeyboardSettings) {
-                // TODO(b/252816846): Need InputMethodManager to provide the enabled locales.
-                // Hardcode Languages for demo until inputMethodManager provides the latest API.
-                // For example: InputMethodManager.getEnabledInputMethodLocales();
-                String[] keyboardLanguages =
-                        {"English (US)", "German (Germany)", "Spanish (Spain)"};
-                String[] keyboardLayouts = {"English (US)", "German", "Spanish"};
-                Map<String, String> keyboardMap = new HashMap<>();
-                for (int i = 0; i < keyboardLanguages.length; i++) {
-                    keyboardMap.put(keyboardLanguages[i], keyboardLayouts[i]);
-                }
-                if (!keyboardMap.isEmpty()) {
-                    String summary = keyboardMap.get(keyboardLanguages[0]);
+                List<String> suitableImes = new ArrayList<>();
+                suitableImes.addAll(
+                        NewKeyboardSettingsUtils.getSuitableImeLabels(
+                                getContext(), mImm, UserHandle.myUserId()));
+                if (!suitableImes.isEmpty()) {
+                    String summary = suitableImes.get(0);
                     StringBuilder result = new StringBuilder(summary);
-                    for (int i = 1; i < keyboardLanguages.length; i++) {
-                        result.append(", ").append(keyboardMap.get(keyboardLanguages[i]));
+                    for (int i = 1; i < suitableImes.size(); i++) {
+                        result.append(", ").append(suitableImes.get(i));
                     }
                     pref.setSummary(result.toString());
                 } else {
@@ -245,12 +240,10 @@
 
     private void showEnabledLocalesKeyboardLayoutList(String keyboardName,
             InputDeviceIdentifier inputDeviceIdentifier) {
-        // TODO(b/252816846: Need to get enabled locales.
         Bundle arguments = new Bundle();
-        arguments.putParcelable(KeyboardLayoutPickerFragment.EXTRA_INPUT_DEVICE_IDENTIFIER,
+        arguments.putParcelable(NewKeyboardSettingsUtils.EXTRA_INPUT_DEVICE_IDENTIFIER,
                 inputDeviceIdentifier);
-        arguments.putString(NewKeyboardLayoutEnabledLocalesFragment.EXTRA_KEYBOARD_DEVICE_NAME,
-                keyboardName);
+        arguments.putString(NewKeyboardSettingsUtils.EXTRA_KEYBOARD_DEVICE_NAME, keyboardName);
         new SubSettingLauncher(getContext())
                 .setSourceMetricsCategory(getMetricsCategory())
                 .setDestination(NewKeyboardLayoutEnabledLocalesFragment.class.getName())
diff --git a/src/com/android/settings/spa/app/appinfo/AppPermissionPreference.kt b/src/com/android/settings/spa/app/appinfo/AppPermissionPreference.kt
index c7d775e..ad666dc 100644
--- a/src/com/android/settings/spa/app/appinfo/AppPermissionPreference.kt
+++ b/src/com/android/settings/spa/app/appinfo/AppPermissionPreference.kt
@@ -27,6 +27,7 @@
 import androidx.compose.runtime.remember
 import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.res.stringResource
+import androidx.lifecycle.LiveData
 import com.android.settings.R
 import com.android.settingslib.spa.widget.preference.Preference
 import com.android.settingslib.spa.widget.preference.PreferenceModel
@@ -36,13 +37,17 @@
 private const val EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"
 
 @Composable
-fun AppPermissionPreference(app: ApplicationInfo) {
+fun AppPermissionPreference(
+    app: ApplicationInfo,
+    summaryLiveData: LiveData<AppPermissionSummaryState> = rememberAppPermissionSummary(app),
+) {
     val context = LocalContext.current
-    val summaryLiveData = remember { AppPermissionSummaryLiveData(context, app) }
-    val summaryState = summaryLiveData.observeAsState(initial = AppPermissionSummaryState(
-        summary = stringResource(R.string.summary_placeholder),
-        enabled = false,
-    ))
+    val summaryState = summaryLiveData.observeAsState(
+        initial = AppPermissionSummaryState(
+            summary = stringResource(R.string.summary_placeholder),
+            enabled = false,
+        )
+    )
     Preference(
         model = remember {
             object : PreferenceModel {
diff --git a/src/com/android/settings/spa/app/appinfo/AppPermissionSummary.kt b/src/com/android/settings/spa/app/appinfo/AppPermissionSummary.kt
index 9b8b0fd..de6bd10 100644
--- a/src/com/android/settings/spa/app/appinfo/AppPermissionSummary.kt
+++ b/src/com/android/settings/spa/app/appinfo/AppPermissionSummary.kt
@@ -20,6 +20,9 @@
 import android.content.pm.ApplicationInfo
 import android.content.pm.PackageManager.OnPermissionsChangedListener
 import android.icu.text.ListFormatter
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.remember
+import androidx.compose.ui.platform.LocalContext
 import androidx.lifecycle.LiveData
 import com.android.settings.R
 import com.android.settingslib.applications.PermissionsSummaryHelper
@@ -33,6 +36,12 @@
     val enabled: Boolean,
 )
 
+@Composable
+fun rememberAppPermissionSummary(app: ApplicationInfo): AppPermissionSummaryLiveData {
+    val context = LocalContext.current
+    return remember { AppPermissionSummaryLiveData(context, app) }
+}
+
 class AppPermissionSummaryLiveData(
     private val context: Context,
     private val app: ApplicationInfo,
diff --git a/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppPermissionPreferenceTest.kt b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppPermissionPreferenceTest.kt
new file mode 100644
index 0000000..1646851
--- /dev/null
+++ b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppPermissionPreferenceTest.kt
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.spa.app.appinfo
+
+import android.content.Context
+import android.content.Intent
+import android.content.pm.ApplicationInfo
+import androidx.compose.runtime.CompositionLocalProvider
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.test.assertIsDisplayed
+import androidx.compose.ui.test.junit4.createComposeRule
+import androidx.compose.ui.test.onNodeWithText
+import androidx.compose.ui.test.onRoot
+import androidx.compose.ui.test.performClick
+import androidx.lifecycle.MutableLiveData
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.android.settings.R
+import com.android.settingslib.spa.testutils.delay
+import com.android.settingslib.spaprivileged.model.app.userHandle
+import com.google.common.truth.Truth.assertThat
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentCaptor
+import org.mockito.Mockito.any
+import org.mockito.Mockito.doNothing
+import org.mockito.Mockito.eq
+import org.mockito.Mockito.verify
+import org.mockito.Spy
+import org.mockito.junit.MockitoJUnit
+import org.mockito.junit.MockitoRule
+
+@RunWith(AndroidJUnit4::class)
+class AppPermissionPreferenceTest {
+    @get:Rule
+    val composeTestRule = createComposeRule()
+
+    @get:Rule
+    val mockito: MockitoRule = MockitoJUnit.rule()
+
+    @Spy
+    private val context: Context = ApplicationProvider.getApplicationContext()
+
+    @Test
+    fun title_display() {
+        setContent()
+
+        composeTestRule.onNodeWithText(context.getString(R.string.permissions_label))
+            .assertIsDisplayed()
+    }
+
+    @Test
+    fun whenClick_startActivity() {
+        doNothing().`when`(context).startActivityAsUser(any(), any())
+
+        setContent()
+        composeTestRule.onRoot().performClick()
+        composeTestRule.delay()
+
+        val intentCaptor = ArgumentCaptor.forClass(Intent::class.java)
+        verify(context).startActivityAsUser(intentCaptor.capture(), eq(APP.userHandle))
+        val intent = intentCaptor.value
+        assertThat(intent.action).isEqualTo(Intent.ACTION_MANAGE_APP_PERMISSIONS)
+        assertThat(intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME)).isEqualTo(PACKAGE_NAME)
+        assertThat(intent.getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)).isEqualTo(true)
+    }
+
+    private fun setContent() {
+        composeTestRule.setContent {
+            CompositionLocalProvider(LocalContext provides context) {
+                AppPermissionPreference(
+                    app = APP,
+                    summaryLiveData = MutableLiveData(
+                        AppPermissionSummaryState(summary = SUMMARY, enabled = true)
+                    ),
+                )
+            }
+        }
+        composeTestRule.delay()
+    }
+
+    private companion object {
+        const val PACKAGE_NAME = "package.name"
+        const val SUMMARY = "Summary"
+        private const val EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"
+
+        val APP = ApplicationInfo().apply {
+            packageName = PACKAGE_NAME
+        }
+    }
+}
\ No newline at end of file