Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5fb93489 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Adding a link to Setting activity for the Home app"

parents 8ab54108 43fccf98
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@
        <extra android:name="for_work" android:value="false" />
    </com.android.settings.widget.AppPreference>

    <com.android.settings.widget.AppPreference
    <com.android.settings.widget.GearPreference
        android:key="default_home"
        android:title="@string/home_app"
        android:fragment="com.android.settings.applications.defaultapps.DefaultHomePicker"
+19 −3
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
        if (currentDefaultHome != null) {
            return new DefaultAppInfo(mContext, mPackageManager, mUserId, currentDefaultHome);
        }
        final ActivityInfo onlyAppInfo = getOnlyAppInfo();
        final ActivityInfo onlyAppInfo = getOnlyAppInfo(homeActivities);
        if (onlyAppInfo != null) {
            return new DefaultAppInfo(mContext, mPackageManager, mUserId,
                    onlyAppInfo.getComponentName());
@@ -71,8 +71,7 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
        return null;
    }

    private ActivityInfo getOnlyAppInfo() {
        final List<ResolveInfo> homeActivities = new ArrayList<>();
    private ActivityInfo getOnlyAppInfo(List<ResolveInfo> homeActivities) {
        final List<ActivityInfo> appLabels = new ArrayList<>();

        mPackageManager.getHomeActivities(homeActivities);
@@ -88,6 +87,23 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
                : 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) {
        ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
        PackageManager pm = context.getPackageManager();
+36 −0
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@
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.anyList;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
@@ -26,6 +29,8 @@ import static org.mockito.Mockito.when;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.UserManager;
import android.support.v7.preference.Preference;

@@ -42,6 +47,9 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;

import java.util.Arrays;


@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultHomePreferenceControllerTest {
@@ -112,4 +120,32 @@ public class DefaultHomePreferenceControllerTest {
        assertThat(DefaultHomePreferenceController.isHomeDefault(pkgName, mPackageManager))
                .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();
    }
}