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

Commit b48565d4 authored by Mina Granic's avatar Mina Granic
Browse files

Set restored aspect ratio for newly installed apps in a service.

Setting the restored user aspect ratio for a particular app
needs to wait until that app is installed. Settings app, which
 handles the Backup and Restore of user aspect ratio settings,
can be killed at any point post restore operation, meaning that
it cannot be the one listening to the package-added signals.
Therefore, if an app is not yet installed, the resolved aspect
ratio by the Settings app is given to the
UserAspectRatioService. This service stores the aspect ratios,
listens to package-added signals, and sets the values when the
apps are installed.

Restore needs to be done in the Settings app, since only it has
access to available user aspect ratio settings allowed on that
device (set/overridden by the OEM), to be able to correctly
resolve which aspect ratio should be set.

Flag: com.android.window.flags.restore_user_aspect_ratio_settings_using_service
Fixes: 408430597
Fixes: 413014252
Test: atest SettingsUnitTests:UserAspectRatioBackupManagerTest
Change-Id: Id82acd269db86ebc775e1b20b96396d00ad3bff2
parent 2488e2b6
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import android.view.DisplayInfo;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.content.PackageMonitor;
import com.android.settings.R;
import com.android.window.flags.Flags;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -135,8 +136,9 @@ class UserAspectRatioBackupManager {
        mStorage = new UserAspectRatioRestoreStorage(context, mUserId, instantSource);
        mLogger = logger;

        if (!Flags.restoreUserAspectRatioSettingsUsingService()) {
            mPackageMonitor.register(context, UserHandle.of(UserHandle.USER_ALL), handler);

        }

        populateAvailableUserAspectRatioSettingOptions(mContext.getResources().getIntArray(
                R.array.config_userAspectRatioOverrideValues));
@@ -293,7 +295,9 @@ class UserAspectRatioBackupManager {
                        + pkgName);
                continue;
            }
            if (isPackageInstalled(pkgName)) {
            // If restoring via service is enabled, try to set the user aspect ratio even if the
            // package is not installed - this would be restored later by a system service.
            if (isPackageInstalled(pkgName) || Flags.restoreUserAspectRatioSettingsUsingService()) {
                Slog.d(TAG, "StageAndApplyRestoredPayload Found package: " + pkgName);
                checkExistingAspectRatioAndApplyRestore(pkgName, aspectRatio);
            } else {
@@ -453,5 +457,4 @@ class UserAspectRatioBackupManager {
        }
        return maxDimensions;
    }

}
+27 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_4_3;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_FULLSCREEN;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_SPLIT_SCREEN;
import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_UNSET;
import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
@@ -47,14 +48,21 @@ import android.content.pm.PackageManager;
import android.os.HandlerThread;
import android.os.Process;
import android.os.RemoteException;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.platform.test.flag.junit.SetFlagsRule;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.android.settings.testutils.FakeInstantSource;
import com.android.settings.testutils.FakeSharedPreferences;
import com.android.window.flags.Flags;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -76,6 +84,10 @@ import java.util.Map;
 */
@RunWith(AndroidJUnit4.class)
public class UserAspectRatioBackupManagerTest {
    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(DEVICE_DEFAULT);
    private static final String DEFAULT_PACKAGE_NAME = "com.android.testapp";
    private static final String OTHER_PACKAGE_NAME = "com.android.anotherapp";

@@ -185,6 +197,7 @@ public class UserAspectRatioBackupManagerTest {
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_RESTORE_USER_ASPECT_RATIO_SETTINGS_USING_SERVICE)
    public void testRestore_appNotInstalled_aspectRatioStored() throws Exception {
        final byte[] out = writeTestPayload(DEFAULT_PACKAGE_ASPECT_RATIO_MAP);
        // Backed up app is not installed on the restore device.
@@ -197,6 +210,19 @@ public class UserAspectRatioBackupManagerTest {
                DEFAULT_PACKAGE_NAME, USER_MIN_ASPECT_RATIO_UNSET));
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_RESTORE_USER_ASPECT_RATIO_SETTINGS_USING_SERVICE)
    public void testRestore_appNotInstalled_delegateRestoreToService() throws Exception {
        final byte[] out = writeTestPayload(DEFAULT_PACKAGE_ASPECT_RATIO_MAP);
        // Backed up app is not installed on the restore device.
        setUpInstalledPackages(List.of());

        mBackupManager.stageAndApplyRestoredPayload(out);

        verify(mMockIPackageManager).setUserMinAspectRatio(DEFAULT_PACKAGE_NAME,
                DEFAULT_USER_ID, USER_MIN_ASPECT_RATIO_FULLSCREEN);
    }

    @Test
    public void testRestore_aspectRatioRestoreForInstalledApp() throws Exception {
        final byte[] out = writeTestPayload(DEFAULT_PACKAGE_ASPECT_RATIO_MAP);
@@ -222,6 +248,7 @@ public class UserAspectRatioBackupManagerTest {
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_RESTORE_USER_ASPECT_RATIO_SETTINGS_USING_SERVICE)
    public void testPackageAdded_aspectRatioRestored() throws Exception {
        final byte[] out = writeTestPayload(DEFAULT_PACKAGE_ASPECT_RATIO_MAP);
        // Backed up app is not installed on the restore device.