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

Commit a3108d5b authored by Eghosa Ewansiha-Vlachavas's avatar Eghosa Ewansiha-Vlachavas Committed by Android (Google) Code Review
Browse files

Merge "[1/n] Aspect ratio settings app list refinement" into udc-qpr-dev

parents c9c1c444 6bd9bff2
Loading
Loading
Loading
Loading
+12 −13
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.applications.appcompat;

import static android.os.UserHandle.getUserHandleForUid;
import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_FULLSCREEN_OVERRIDE;
import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_OVERRIDE;

@@ -26,8 +27,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.RemoteException;
import android.provider.DeviceConfig;
import android.util.ArrayMap;
@@ -40,7 +41,6 @@ import com.android.settings.Utils;

import com.google.common.annotations.VisibleForTesting;

import java.util.List;
import java.util.Map;

/**
@@ -63,15 +63,12 @@ public class UserAspectRatioManager {
    private final Context mContext;
    private final IPackageManager mIPm;
    /** Apps that have launcher entry defined in manifest */
    private final List<ResolveInfo> mInfoHasLauncherEntryList;
    private final Map<Integer, String> mUserAspectRatioMap;
    private final Map<Integer, CharSequence> mUserAspectRatioA11yMap;

    public UserAspectRatioManager(@NonNull Context context) {
        mContext = context;
        mIPm = AppGlobals.getPackageManager();
        mInfoHasLauncherEntryList = mContext.getPackageManager().queryIntentActivities(
                UserAspectRatioManager.LAUNCHER_ENTRY_INTENT, PackageManager.GET_META_DATA);
        mUserAspectRatioA11yMap = new ArrayMap<>();
        mUserAspectRatioMap = getUserMinAspectRatioMapping();
    }
@@ -159,9 +156,7 @@ public class UserAspectRatioManager {
        Boolean appAllowsUserAspectRatioOverride = readComponentProperty(
                mContext.getPackageManager(), app.packageName,
                PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_OVERRIDE);
        boolean hasLauncherEntry = mInfoHasLauncherEntryList.stream()
                .anyMatch(info -> info.activityInfo.packageName.equals(app.packageName));
        return !FALSE.equals(appAllowsUserAspectRatioOverride) && hasLauncherEntry;
        return !FALSE.equals(appAllowsUserAspectRatioOverride) && hasLauncherEntry(app);
    }

    /**
@@ -178,6 +173,15 @@ public class UserAspectRatioManager {
                    DEFAULT_VALUE_ENABLE_USER_ASPECT_RATIO_FULLSCREEN);
    }

    LauncherApps getLauncherApps() {
        return mContext.getSystemService(LauncherApps.class);
    }

    private boolean hasLauncherEntry(@NonNull ApplicationInfo app) {
        return !getLauncherApps().getActivityList(app.packageName, getUserHandleForUid(app.uid))
                .isEmpty();
    }

    private static boolean getValueFromDeviceConfig(String name, boolean defaultValue) {
        return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_WINDOW_MANAGER, name, defaultValue);
    }
@@ -267,9 +271,4 @@ public class UserAspectRatioManager {
        }
        return null;
    }

    @VisibleForTesting
    void addInfoHasLauncherEntry(@NonNull ResolveInfo infoHasLauncherEntry) {
        mInfoHasLauncherEntryList.add(infoHasLauncherEntry);
    }
}
+18 −15
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@
package com.android.settings.spa.app.appcompat

import android.content.Context
import android.content.pm.ActivityInfo
import android.content.pm.ApplicationInfo
import android.content.pm.LauncherActivityInfo
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.provider.DeviceConfig.NAMESPACE_WINDOW_MANAGER
import android.view.WindowManager.PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_OVERRIDE
import androidx.compose.runtime.CompositionLocalProvider
@@ -46,9 +46,9 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import org.mockito.Mockito.any
import org.mockito.Mockito.anyInt
import org.mockito.MockitoSession
import org.mockito.Spy
import org.mockito.quality.Strictness
@@ -76,6 +76,12 @@ class UserAspectRatioAppPreferenceTest {
    @Mock
    private lateinit var packageManager: PackageManager

    @Mock
    private lateinit var launcherApps: LauncherApps

    @Mock
    private lateinit var launcherActivities: List<LauncherActivityInfo>

    @Before
    fun setUp() {
        mockSession = ExtendedMockito.mockitoSession()
@@ -86,6 +92,8 @@ class UserAspectRatioAppPreferenceTest {
            .startMocking()
        whenever(context.resources).thenReturn(resources)
        whenever(context.packageManager).thenReturn(packageManager)
        whenever(context.getSystemService(Context.LAUNCHER_APPS_SERVICE)).thenReturn(launcherApps)
        whenever(launcherApps.getActivityList(anyString(), any())).thenReturn(launcherActivities)
        // True is ignored but need this here or getBoolean will complain null object
        mockProperty(PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_OVERRIDE, true)
    }
@@ -107,6 +115,8 @@ class UserAspectRatioAppPreferenceTest {

    @Test
    fun whenCannotDisplayAspectRatioUi_notDisplayed() {
        whenever(launcherActivities.isEmpty()).thenReturn(true)

        setContent()

        composeTestRule.onRoot().assertIsNotDisplayed()
@@ -115,8 +125,7 @@ class UserAspectRatioAppPreferenceTest {
    @Test
    fun whenCanDisplayAspectRatioUiAndConfigFalse_notDisplayed() {
        setConfig(false)
        whenever(packageManager.queryIntentActivities(any(), anyInt()))
            .thenReturn(listOf(RESOLVE_INFO))
        whenever(launcherActivities.isEmpty()).thenReturn(false)

        setContent()

@@ -127,6 +136,8 @@ class UserAspectRatioAppPreferenceTest {
    fun whenCannotDisplayAspectRatioUiAndConfigTrue_notDisplayed() {
        setConfig(true)

        whenever(launcherActivities.isEmpty()).thenReturn(true)

        setContent()

        composeTestRule.onRoot().assertIsNotDisplayed()
@@ -135,9 +146,7 @@ class UserAspectRatioAppPreferenceTest {
    @Test
    fun whenCanDisplayAspectRatioUiAndConfigTrue_Displayed() {
        setConfig(true)
        whenever(packageManager.queryIntentActivities(any(), anyInt()))
            .thenReturn(listOf(RESOLVE_INFO))

        whenever(launcherActivities.isEmpty()).thenReturn(false)
        setContent()

        composeTestRule.onNode(
@@ -151,8 +160,7 @@ class UserAspectRatioAppPreferenceTest {
    @Test
    fun onClick_startActivity() {
        setConfig(true)
        whenever(packageManager.queryIntentActivities(any(), anyInt()))
            .thenReturn(listOf(RESOLVE_INFO))
        whenever(launcherActivities.isEmpty()).thenReturn(false)

        setContent()
        composeTestRule.onRoot().performClick()
@@ -196,10 +204,5 @@ class UserAspectRatioAppPreferenceTest {
            packageName = PACKAGE_NAME
            uid = UID
        }
        private val RESOLVE_INFO = ResolveInfo().apply {
            activityInfo = ActivityInfo().apply {
                packageName = PACKAGE_NAME
            }
        }
    }
}
 No newline at end of file
+22 −14
Original line number Diff line number Diff line
@@ -32,15 +32,18 @@ import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.provider.DeviceConfig;

@@ -55,6 +58,8 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

/**
 * To run this test: atest SettingsUnitTests:UserAspectRatioManagerTest
 */
@@ -67,14 +72,23 @@ public class UserAspectRatioManagerTest {
    private String mOriginalSettingsFlag;
    private String mOriginalFullscreenFlag;
    private String mPackageName = "com.test.mypackage";

    private LauncherApps mLauncherApps;
    private List<LauncherActivityInfo> mLauncherActivities;
    @Before
    public void setUp() {
        mContext = spy(ApplicationProvider.getApplicationContext());
        mResources = spy(mContext.getResources());
        mUtils = new UserAspectRatioManager(mContext);
        mLauncherApps = mock(LauncherApps.class);
        mLauncherActivities = mock(List.class);
        mUtils = new UserAspectRatioManager(mContext) {
            @Override
            LauncherApps getLauncherApps() {
                return mLauncherApps;
            }
        };

        when(mContext.getResources()).thenReturn(mResources);
        doReturn(mLauncherActivities).when(mLauncherApps).getActivityList(anyString(), any());

        mOriginalSettingsFlag = DeviceConfig.getProperty(
                DeviceConfig.NAMESPACE_WINDOW_MANAGER, KEY_ENABLE_USER_ASPECT_RATIO_SETTINGS);
@@ -98,13 +112,14 @@ public class UserAspectRatioManagerTest {
    public void testCanDisplayAspectRatioUi() {
        final ApplicationInfo canDisplay = new ApplicationInfo();
        canDisplay.packageName = "com.app.candisplay";
        addResolveInfoLauncherEntry(canDisplay.packageName);

        doReturn(false).when(mLauncherActivities).isEmpty();
        assertTrue(mUtils.canDisplayAspectRatioUi(canDisplay));

        final ApplicationInfo noLauncherEntry = new ApplicationInfo();
        noLauncherEntry.packageName = "com.app.nolauncherentry";

        doReturn(true).when(mLauncherActivities).isEmpty();
        assertFalse(mUtils.canDisplayAspectRatioUi(noLauncherEntry));
    }

@@ -112,10 +127,10 @@ public class UserAspectRatioManagerTest {
    public void testCanDisplayAspectRatioUi_hasLauncher_propertyFalse_returnFalse()
            throws PackageManager.NameNotFoundException {
        mockProperty(PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_OVERRIDE, false);
        doReturn(true).when(mLauncherActivities).isEmpty();

        final ApplicationInfo canDisplay = new ApplicationInfo();
        canDisplay.packageName = mPackageName;
        addResolveInfoLauncherEntry(canDisplay.packageName);

        assertFalse(mUtils.canDisplayAspectRatioUi(canDisplay));
    }
@@ -124,6 +139,7 @@ public class UserAspectRatioManagerTest {
    public void testCanDisplayAspectRatioUi_noLauncher_propertyTrue_returnFalse()
            throws PackageManager.NameNotFoundException {
        mockProperty(PROPERTY_COMPAT_ALLOW_USER_ASPECT_RATIO_OVERRIDE, true);
        doReturn(true).when(mLauncherActivities).isEmpty();

        final ApplicationInfo noLauncherEntry = new ApplicationInfo();
        noLauncherEntry.packageName = mPackageName;
@@ -267,12 +283,4 @@ public class UserAspectRatioManagerTest {
        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_WINDOW_MANAGER,
                KEY_ENABLE_USER_ASPECT_RATIO_FULLSCREEN, enabled, makeDefault);
    }

    private void addResolveInfoLauncherEntry(String packageName) {
        final ResolveInfo resolveInfo = mock(ResolveInfo.class);
        final ActivityInfo activityInfo = mock(ActivityInfo.class);
        activityInfo.packageName = packageName;
        resolveInfo.activityInfo = activityInfo;
        mUtils.addInfoHasLauncherEntry(resolveInfo);
    }
}