Directly show the layout selection screen.

When a user clicks on the physical keyboard in order to select a
layout, rather than showing them a dialog with an empty list of
layouts, send them straight to the screen where they can pick which
layouts to enable.

Also, only request keyboard layouts that are appropriate for the
input device we're in the process of configuring.

Bug: 25062009
Change-Id: I4483dfc89afc8d148b2cfa7c6a5f66d2a02f712a
diff --git a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
index e8ef5ef..4c12a45 100644
--- a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
+++ b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
@@ -571,10 +571,13 @@
     }
 
     private void showKeyboardLayoutDialog(InputDeviceIdentifier inputDeviceIdentifier) {
-        KeyboardLayoutDialogFragment fragment = new KeyboardLayoutDialogFragment(
-                inputDeviceIdentifier);
-        fragment.setTargetFragment(this, 0);
-        fragment.show(getActivity().getFragmentManager(), "keyboardLayout");
+        KeyboardLayoutDialogFragment fragment = (KeyboardLayoutDialogFragment)
+                getFragmentManager().findFragmentByTag("keyboardLayout");
+        if (fragment == null) {
+            fragment = new KeyboardLayoutDialogFragment(inputDeviceIdentifier);
+            fragment.setTargetFragment(this, 0);
+            fragment.show(getActivity().getFragmentManager(), "keyboardLayout");
+        }
     }
 
     @Override
diff --git a/src/com/android/settings/inputmethod/KeyboardLayoutDialogFragment.java b/src/com/android/settings/inputmethod/KeyboardLayoutDialogFragment.java
index c77b2c9..a770a74 100644
--- a/src/com/android/settings/inputmethod/KeyboardLayoutDialogFragment.java
+++ b/src/com/android/settings/inputmethod/KeyboardLayoutDialogFragment.java
@@ -53,6 +53,7 @@
     private int mInputDeviceId = -1;
     private InputManager mIm;
     private KeyboardLayoutAdapter mAdapter;
+    private boolean mHasShownLayoutSelectionScreen;
 
     public KeyboardLayoutDialogFragment() {
     }
@@ -178,6 +179,7 @@
             dialog.getListView().setItemChecked(data.current, true);
         }
         updateSwitchHintVisibility();
+        showSetupKeyboardLayoutsIfNecessary();
     }
 
     @Override
@@ -212,6 +214,17 @@
         }
     }
 
+    private void showSetupKeyboardLayoutsIfNecessary() {
+        AlertDialog dialog = (AlertDialog)getDialog();
+        if (dialog != null
+                && mAdapter.getCount() == 1 && mAdapter.getItem(0) == null
+                && !mHasShownLayoutSelectionScreen) {
+            mHasShownLayoutSelectionScreen = true;
+            ((OnSetupKeyboardLayoutsListener)getTargetFragment()).onSetupKeyboardLayouts(
+                    mInputDeviceIdentifier);
+        }
+    }
+
     private static final class KeyboardLayoutAdapter extends ArrayAdapter<KeyboardLayout> {
         private final LayoutInflater mInflater;
         private int mCheckedItem = -1;
@@ -300,7 +313,7 @@
         public Keyboards loadInBackground() {
             Keyboards keyboards = new Keyboards();
             InputManager im = (InputManager)getContext().getSystemService(Context.INPUT_SERVICE);
-            String[] keyboardLayoutDescriptors = im.getKeyboardLayoutsForInputDevice(
+            String[] keyboardLayoutDescriptors = im.getEnabledKeyboardLayoutsForInputDevice(
                     mInputDeviceIdentifier);
             for (String keyboardLayoutDescriptor : keyboardLayoutDescriptors) {
                 KeyboardLayout keyboardLayout = im.getKeyboardLayout(keyboardLayoutDescriptor);
@@ -351,4 +364,4 @@
     public interface OnSetupKeyboardLayoutsListener {
         public void onSetupKeyboardLayouts(InputDeviceIdentifier mInputDeviceIdentifier);
     }
-}
\ No newline at end of file
+}
diff --git a/src/com/android/settings/inputmethod/KeyboardLayoutPickerFragment.java b/src/com/android/settings/inputmethod/KeyboardLayoutPickerFragment.java
index 69586b9..59ce747 100644
--- a/src/com/android/settings/inputmethod/KeyboardLayoutPickerFragment.java
+++ b/src/com/android/settings/inputmethod/KeyboardLayoutPickerFragment.java
@@ -65,7 +65,7 @@
         }
 
         mIm = (InputManager)getSystemService(Context.INPUT_SERVICE);
-        mKeyboardLayouts = mIm.getKeyboardLayouts();
+        mKeyboardLayouts = mIm.getKeyboardLayoutsForInputDevice(mInputDeviceIdentifier);
         Arrays.sort(mKeyboardLayouts);
         setPreferenceScreen(createPreferenceHierarchy());
     }
@@ -149,7 +149,7 @@
     }
 
     private void updateCheckedState() {
-        String[] enabledKeyboardLayouts = mIm.getKeyboardLayoutsForInputDevice(
+        String[] enabledKeyboardLayouts = mIm.getEnabledKeyboardLayoutsForInputDevice(
                 mInputDeviceIdentifier);
         Arrays.sort(enabledKeyboardLayouts);