Adding a link to Setting activity for the Home app

The link is only visible if there is a default home add and it
implements a settings activity

Test: make -j20 RunSettingsRoboTests
Change-Id: Iac5d0ebd561bed2e5b2f84236cdb728362ed8663
This commit is contained in:
Sunny Goyal
2017-11-13 10:09:09 -08:00
parent 136c6888ae
commit 43fccf9842
3 changed files with 56 additions and 4 deletions

View File

@@ -33,7 +33,7 @@
<extra android:name="for_work" android:value="false" /> <extra android:name="for_work" android:value="false" />
</com.android.settings.widget.AppPreference> </com.android.settings.widget.AppPreference>
<com.android.settings.widget.AppPreference <com.android.settings.widget.GearPreference
android:key="default_home" android:key="default_home"
android:title="@string/home_app" android:title="@string/home_app"
android:fragment="com.android.settings.applications.defaultapps.DefaultHomePicker" android:fragment="com.android.settings.applications.defaultapps.DefaultHomePicker"

View File

@@ -63,7 +63,7 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
if (currentDefaultHome != null) { if (currentDefaultHome != null) {
return new DefaultAppInfo(mContext, mPackageManager, mUserId, currentDefaultHome); return new DefaultAppInfo(mContext, mPackageManager, mUserId, currentDefaultHome);
} }
final ActivityInfo onlyAppInfo = getOnlyAppInfo(); final ActivityInfo onlyAppInfo = getOnlyAppInfo(homeActivities);
if (onlyAppInfo != null) { if (onlyAppInfo != null) {
return new DefaultAppInfo(mContext, mPackageManager, mUserId, return new DefaultAppInfo(mContext, mPackageManager, mUserId,
onlyAppInfo.getComponentName()); onlyAppInfo.getComponentName());
@@ -71,8 +71,7 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
return null; return null;
} }
private ActivityInfo getOnlyAppInfo() { private ActivityInfo getOnlyAppInfo(List<ResolveInfo> homeActivities) {
final List<ResolveInfo> homeActivities = new ArrayList<>();
final List<ActivityInfo> appLabels = new ArrayList<>(); final List<ActivityInfo> appLabels = new ArrayList<>();
mPackageManager.getHomeActivities(homeActivities); mPackageManager.getHomeActivities(homeActivities);
@@ -88,6 +87,23 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
: null; : null;
} }
@Override
protected Intent getSettingIntent(DefaultAppInfo info) {
final String packageName;
if (info.componentName != null) {
packageName = info.componentName.getPackageName();
} else if (info.packageItemInfo != null) {
packageName = info.packageItemInfo.packageName;
} else {
return null;
}
Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
.setPackage(packageName)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return mPackageManager.queryIntentActivities(intent, 0).size() == 1 ? intent : null;
}
public static boolean hasHomePreference(String pkg, Context context) { public static boolean hasHomePreference(String pkg, Context context) {
ArrayList<ResolveInfo> homeActivities = new ArrayList<>(); ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
PackageManager pm = context.getPackageManager(); PackageManager pm = context.getPackageManager();

View File

@@ -17,6 +17,9 @@
package com.android.settings.applications.defaultapps; package com.android.settings.applications.defaultapps;
import static com.google.common.truth.Truth.assertThat; 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.anyList; import static org.mockito.Matchers.anyList;
import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@@ -26,6 +29,8 @@ import static org.mockito.Mockito.when;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.UserManager; import android.os.UserManager;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
@@ -42,6 +47,9 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers; import org.robolectric.util.ReflectionHelpers;
import java.util.Arrays;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultHomePreferenceControllerTest { public class DefaultHomePreferenceControllerTest {
@@ -112,4 +120,32 @@ public class DefaultHomePreferenceControllerTest {
assertThat(DefaultHomePreferenceController.isHomeDefault(pkgName, mPackageManager)) assertThat(DefaultHomePreferenceController.isHomeDefault(pkgName, mPackageManager))
.isFalse(); .isFalse();
} }
@Test
public void testGetSettingIntent_homeHasNoSetting_shouldNotReturnSettingIntent() {
when(mPackageManager.getHomeActivities(anyList())).thenReturn(
new ComponentName("test.pkg", "class"));
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(
Arrays.asList(mock(ResolveInfo.class)));
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();
}
} }