Handle 'no settings activity defined' case

This is potentially an issue for OEM defined rotary IMEs that don't
define a settings activity

Bug: 171516803
Test: manual
Change-Id: I140e76ad67c1d59081bda57c172dbf1c2d6c4cf3
diff --git a/src/com/android/car/settings/inputmethod/EnabledKeyboardPreferenceController.java b/src/com/android/car/settings/inputmethod/EnabledKeyboardPreferenceController.java
index fe1bb2a..dd82ad2 100644
--- a/src/com/android/car/settings/inputmethod/EnabledKeyboardPreferenceController.java
+++ b/src/com/android/car/settings/inputmethod/EnabledKeyboardPreferenceController.java
@@ -128,7 +128,13 @@
             try {
                 Intent intent = new Intent(Intent.ACTION_MAIN);
                 String settingsActivity = inputMethodInfo.getSettingsActivity();
+                if (settingsActivity == null) {
+                    LOG.d("IME's Settings Activity not defined.");
+                    return true;
+                }
+
                 intent.setClassName(inputMethodInfo.getPackageName(), settingsActivity);
+
                 // Invoke a settings activity of an input method.
                 getContext().startActivity(intent);
             } catch (final ActivityNotFoundException e) {
diff --git a/tests/robotests/src/com/android/car/settings/inputmethod/EnabledKeyboardPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/inputmethod/EnabledKeyboardPreferenceControllerTest.java
index 69517ae..ebfec1d 100644
--- a/tests/robotests/src/com/android/car/settings/inputmethod/EnabledKeyboardPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/inputmethod/EnabledKeyboardPreferenceControllerTest.java
@@ -194,7 +194,7 @@
     }
 
     @Test
-    public void performClick_noSettingsActivity_noCrash() {
+    public void performClick_missingSettingsActivity_noCrash() {
         // Set to true if you'd like Robolectric to strictly simulate the real Android behavior when
         // calling {@link Context#startActivity(android.content.Intent)}. Real Android throws a
         // {@link android.content.ActivityNotFoundException} if given an {@link Intent} that is not
@@ -214,18 +214,41 @@
         assertThat(intent).isNull();
     }
 
+    @Test
+    public void performClick_noSettingsActivity_noCrash() {
+        ShadowApplication.getInstance().checkActivities(true);
+        getShadowDevicePolicyManager(mContext).setPermittedInputMethodsForCurrentUser(null);
+        List<InputMethodInfo> infos = createInputMethodInfoList(
+                DISALLOWED_PACKAGE_NAME, /* settingsActivity= */ null);
+        getShadowInputMethodManager(mContext).setEnabledInputMethodList(infos);
+
+        mControllerHelper.getController().refreshUi();
+
+        Preference preference = mPreferenceGroup.getPreference(0);
+        preference.performClick();
+
+        Intent intent = ShadowApplication.getInstance().getNextStartedActivity();
+        assertThat(intent).isNull();
+    }
+
     private List<InputMethodInfo> createInputMethodInfoList(String packageName) {
+        return createInputMethodInfoList(packageName, DUMMY_SETTINGS_ACTIVITY);
+    }
+
+    private List<InputMethodInfo> createInputMethodInfoList(String packageName,
+            String settingsName) {
         List<InputMethodInfo> infos = new ArrayList<>();
         PackageManager packageManager = mContext.getPackageManager();
         infos.add(createMockInputMethodInfoWithSubtypes(
-                packageManager, getShadowInputMethodManager(mContext), packageName));
+                packageManager, getShadowInputMethodManager(mContext), packageName, settingsName));
         return infos;
     }
 
     private static InputMethodInfo createMockInputMethodInfoWithSubtypes(
             PackageManager packageManager, ShadowInputMethodManager inputMethodManager,
-            String packageName) {
-        InputMethodInfo mockInfo = createMockInputMethodInfo(packageManager, packageName);
+            String packageName, String settingsName) {
+        InputMethodInfo mockInfo = createMockInputMethodInfo(packageManager, packageName,
+                settingsName);
         List<InputMethodSubtype> subtypes = createSubtypes();
         inputMethodManager.setEnabledInputMethodSubtypeList(subtypes);
 
@@ -233,13 +256,13 @@
     }
 
     private static InputMethodInfo createMockInputMethodInfo(
-            PackageManager packageManager, String packageName) {
+            PackageManager packageManager, String packageName, String settingsName) {
         InputMethodInfo mockInfo = mock(InputMethodInfo.class);
         when(mockInfo.getPackageName()).thenReturn(packageName);
         when(mockInfo.getId()).thenReturn(DUMMY_ID);
         when(mockInfo.loadLabel(packageManager)).thenReturn(DUMMY_LABEL);
         when(mockInfo.getServiceInfo()).thenReturn(new ServiceInfo());
-        when(mockInfo.getSettingsActivity()).thenReturn(DUMMY_SETTINGS_ACTIVITY);
+        when(mockInfo.getSettingsActivity()).thenReturn(settingsName);
         return mockInfo;
     }