Merge "Fix crash in clicking Default Home gear icon." am: 0c724f5181
am: 51a72d4c38

Change-Id: I7ff6b0e405c3d87faa869bd53eee9983648ccf69
diff --git a/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java
index 02409b2..20cf152 100644
--- a/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java
+++ b/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java
@@ -106,7 +106,9 @@
         Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
                 .setPackage(packageName)
                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
-        return mPackageManager.queryIntentActivities(intent, 0).size() == 1 ? intent : null;
+        return intent.resolveActivity(mPackageManager.getPackageManager()) != null
+               ? intent
+               : null;
     }
 
     public static boolean hasHomePreference(String pkg, Context context) {
diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceControllerTest.java
index a9cfa01..e97e285 100644
--- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceControllerTest.java
@@ -17,8 +17,8 @@
 package com.android.settings.applications.defaultapps;
 
 import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
 import static org.mockito.Matchers.anyList;
 import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.mock;
@@ -29,6 +29,9 @@
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.os.UserManager;
 import android.support.v7.preference.Preference;
@@ -52,6 +55,9 @@
 @RunWith(SettingsRobolectricTestRunner.class)
 public class DefaultHomePreferenceControllerTest {
 
+    private static final String TEST_PACKAGE = "test.pkg";
+    private static final String TEST_CLASS = "class";
+
     @Mock
     private UserManager mUserManager;
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -106,14 +112,14 @@
     @Test
     public void testIsHomeDefault_noDefaultSet_shouldReturnTrue() {
         when(mPackageManager.getHomeActivities(anyList())).thenReturn(null);
-        assertThat(DefaultHomePreferenceController.isHomeDefault("test.pkg", mPackageManager))
+        assertThat(DefaultHomePreferenceController.isHomeDefault(TEST_PACKAGE, mPackageManager))
                 .isTrue();
     }
 
     @Test
     public void testIsHomeDefault_defaultSetToPkg_shouldReturnTrue() {
-        final String pkgName = "test.pkg";
-        final ComponentName defaultHome = new ComponentName(pkgName, "class");
+        final String pkgName = TEST_PACKAGE;
+        final ComponentName defaultHome = new ComponentName(pkgName, TEST_CLASS);
 
         when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
 
@@ -123,8 +129,8 @@
 
     @Test
     public void testIsHomeDefault_defaultSetToOtherPkg_shouldReturnFalse() {
-        final String pkgName = "test.pkg";
-        final ComponentName defaultHome = new ComponentName("not" + pkgName, "class");
+        final String pkgName = TEST_PACKAGE;
+        final ComponentName defaultHome = new ComponentName("not" + pkgName, TEST_CLASS);
 
         when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
 
@@ -135,29 +141,28 @@
     @Test
     public void testGetSettingIntent_homeHasNoSetting_shouldNotReturnSettingIntent() {
         when(mPackageManager.getHomeActivities(anyList()))
-            .thenReturn(new ComponentName("test.pkg", "class"));
+            .thenReturn(new ComponentName(TEST_PACKAGE, TEST_CLASS));
+        when(mPackageManager.getPackageManager().resolveActivity(any(Intent.class), anyInt()))
+            .thenReturn(null);
+
         assertThat(mController.getSettingIntent(mController.getDefaultAppInfo())).isNull();
     }
 
     @Test
     public void testGetSettingIntent_homeHasOneSetting_shouldReturnSettingIntent() {
         when(mPackageManager.getHomeActivities(anyList()))
-            .thenReturn(new ComponentName("test.pkg", "class"));
-        when(mPackageManager.queryIntentActivities(any(), eq(0)))
-            .thenReturn(Collections.singletonList(mock(ResolveInfo.class)));
+            .thenReturn(new ComponentName(TEST_PACKAGE, TEST_CLASS));
+        final ResolveInfo info = mock(ResolveInfo.class);
+        info.activityInfo = mock(ActivityInfo.class);
+        info.activityInfo.name = TEST_CLASS;
+        info.activityInfo.applicationInfo = mock(ApplicationInfo.class);
+        info.activityInfo.applicationInfo.packageName = TEST_PACKAGE;
+        when(mPackageManager.getPackageManager().resolveActivity(any(Intent.class), anyInt()))
+            .thenReturn(info);
 
         Intent intent = mController.getSettingIntent(mController.getDefaultAppInfo());
         assertThat(intent).isNotNull();
-        assertThat(intent.getPackage()).isEqualTo("test.pkg");
-    }
-
-    @Test
-    public void testGetSettingIntent_homeHasMultipleSettings_shouldNotReturnSettingIntent() {
-        when(mPackageManager.getHomeActivities(anyList()))
-            .thenReturn(new ComponentName("test.pkg", "class"));
-        when(mPackageManager.queryIntentActivities(any(), eq(0)))
-            .thenReturn(Arrays.asList(mock(ResolveInfo.class), mock(ResolveInfo.class)));
-        assertThat(mController.getSettingIntent(mController.getDefaultAppInfo())).isNull();
+        assertThat(intent.getPackage()).isEqualTo(TEST_PACKAGE);
     }
 
     @Test