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

Commit d212caf5 authored by Lynn Yeh's avatar Lynn Yeh Committed by Android (Google) Code Review
Browse files

Merge "Unlock screen rotation temprary for the UNFOLDED only" into 24D1-dev

parents 0f7ebeba 7f96fce0
Loading
Loading
Loading
Loading
+109 −0
Original line number Diff line number Diff line
@@ -133,6 +133,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -440,6 +441,9 @@ public class SatelliteController extends Handler {
    private long mSessionStartTimeStamp;
    private long mSessionProcessingTimeStamp;

    // Variable for backup and restore device's screen rotation settings.
    private String mDeviceRotationLockToBackupAndRestore = null;

    /**
     * @return The singleton instance of SatelliteController.
     */
@@ -995,6 +999,7 @@ public class SatelliteController extends Handler {
                            mWaitingForRadioDisabled = true;
                        }
                        setSettingsKeyForSatelliteMode(SATELLITE_MODE_ENABLED_TRUE);
                        setSettingsKeyToAllowDeviceRotation(SATELLITE_MODE_ENABLED_TRUE);
                        evaluateToSendSatelliteEnabledSuccess();
                    } else {
                        /**
@@ -3582,6 +3587,109 @@ public class SatelliteController extends Handler {
                    Settings.Global.SATELLITE_MODE_ENABLED, val);
    }

    /**
     * Allow screen rotation temporary in rotation locked foldable device.
     * <p>
     * Temporarily allow screen rotation user to catch satellite signals properly by UI guide in
     * emergency situations. Unlock the setting value so that the screen rotation is not locked, and
     * return it to the original value when the satellite service is finished.
     * <p>
     * Note that, only the unfolded screen will be temporarily allowed screen rotation.
     *
     * @param val {@link SATELLITE_MODE_ENABLED_TRUE} if satellite mode is enabled,
     *     {@link SATELLITE_MODE_ENABLED_FALSE} satellite mode is not enabled.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    protected void setSettingsKeyToAllowDeviceRotation(int val) {
        // Only allows on a foldable device type.
        if (!isFoldable(mContext)) {
            return;
        }

        switch (val) {
            case SATELLITE_MODE_ENABLED_TRUE:
                mDeviceRotationLockToBackupAndRestore =
                        Settings.Secure.getString(mContentResolver,
                                Settings.Secure.DEVICE_STATE_ROTATION_LOCK);
                String unlockedRotationSettings = replaceDeviceRotationValue(
                        mDeviceRotationLockToBackupAndRestore == null
                                ? "" : mDeviceRotationLockToBackupAndRestore,
                        Settings.Secure.DEVICE_STATE_ROTATION_KEY_UNFOLDED,
                        Settings.Secure.DEVICE_STATE_ROTATION_LOCK_UNLOCKED);
                Settings.Secure.putString(mContentResolver,
                        Settings.Secure.DEVICE_STATE_ROTATION_LOCK, unlockedRotationSettings);
                logd("setSettingsKeyToAllowDeviceRotation(TRUE), RotationSettings is changed"
                        + " from " + mDeviceRotationLockToBackupAndRestore
                        + " to " + unlockedRotationSettings);
                break;
            case SATELLITE_MODE_ENABLED_FALSE:
                if (mDeviceRotationLockToBackupAndRestore == null) {
                    break;
                }
                Settings.Secure.putString(mContentResolver,
                        Settings.Secure.DEVICE_STATE_ROTATION_LOCK,
                        mDeviceRotationLockToBackupAndRestore);
                logd("setSettingsKeyToAllowDeviceRotation(FALSE), RotationSettings is restored to"
                        + mDeviceRotationLockToBackupAndRestore);
                mDeviceRotationLockToBackupAndRestore = "";
                break;
            default:
                loge("setSettingsKeyToAllowDeviceRotation(" + val + "), never reach here.");
                break;
        }
    }

    /**
     * If the device type is foldable.
     *
     * @param context context
     * @return {@code true} if device type is foldable. {@code false} for otherwise.
     */
    private boolean isFoldable(Context context) {
        return context.getResources().getIntArray(R.array.config_foldedDeviceStates).length > 0;
    }

    /**
     * Replaces a value of given a target key with a new value in a string of key-value pairs.
     * <p>
     * Replaces the value corresponding to the target key with a new value. If the key value is not
     * found in the device rotation information, it is not replaced.
     *
     * @param deviceRotationValue Device rotation key values separated by colon(':').
     * @param targetKey The key of the new item caller wants to add.
     * @param newValue  The value of the new item caller want to add.
     * @return A new string where all the key-value pairs.
     */
    private static String replaceDeviceRotationValue(
            @NonNull String deviceRotationValue, int targetKey, int newValue) {
        // Use list of Key-Value pair
        List<Pair<Integer, Integer>> keyValuePairs = new ArrayList<>();

        String[] pairs = deviceRotationValue.split(":");
        if (pairs.length % 2 != 0) {
            // Return without modifying. The key-value may be incorrect if length is an odd number.
            loge("The length of key-value pair do not match. Return without modification.");
            return deviceRotationValue;
        }

        // collect into keyValuePairs
        for (int i = 0; i < pairs.length; i += 2) {
            try {
                int key = Integer.parseInt(pairs[i]);
                int value = Integer.parseInt(pairs[i + 1]);
                keyValuePairs.add(new Pair<>(key, key == targetKey ? newValue : value));
            } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                // Return without modifying if got exception.
                loge("got error while parsing key-value. Return without modification. e:" + e);
                return deviceRotationValue;
            }
        }

        return keyValuePairs.stream()
                .map(pair -> pair.first + ":" + pair.second) // Convert to "key:value" format
                .collect(Collectors.joining(":")); // Join pairs with colons
    }

    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    protected boolean areAllRadiosDisabled() {
        synchronized (mRadioStateLock) {
@@ -3636,6 +3744,7 @@ public class SatelliteController extends Handler {
            mIsEmergency = false;
            mIsSatelliteEnabled = false;
            setSettingsKeyForSatelliteMode(SATELLITE_MODE_ENABLED_FALSE);
            setSettingsKeyToAllowDeviceRotation(SATELLITE_MODE_ENABLED_FALSE);
            if (callback != null) callback.accept(error);
            updateSatelliteEnabledState(
                    false, "moveSatelliteToOffStateAndCleanUpResources");
+19 −0
Original line number Diff line number Diff line
@@ -786,6 +786,7 @@ public class SatelliteControllerTest extends TelephonyTest {
        // Successfully enable satellite
        mIIntegerConsumerResults.clear();
        mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled = false;
        mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled = false;
        setUpResponseForRequestSatelliteEnabled(true, false, false, SATELLITE_RESULT_SUCCESS);
        mSatelliteControllerUT.requestSatelliteEnabled(SUB_ID, true, false, false,
                mIIntegerConsumer);
@@ -795,6 +796,7 @@ public class SatelliteControllerTest extends TelephonyTest {
        assertEquals(SATELLITE_RESULT_SUCCESS, (long) mIIntegerConsumerResults.get(0));
        verifySatelliteEnabled(true, SATELLITE_RESULT_SUCCESS);
        assertTrue(mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled);
        assertTrue(mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled);
        assertEquals(
                SATELLITE_MODE_ENABLED_TRUE, mSatelliteControllerUT.satelliteModeSettingValue);
        verify(mMockSatelliteSessionController, times(1)).onSatelliteEnabledStateChanged(eq(true));
@@ -805,6 +807,7 @@ public class SatelliteControllerTest extends TelephonyTest {

        // Successfully disable satellite when radio is turned off.
        mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled = false;
        mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled = false;
        setUpResponseForRequestSatelliteEnabled(false, false, false, SATELLITE_RESULT_SUCCESS);
        setRadioPower(false);
        mSatelliteControllerUT.onCellularRadioPowerOffRequested();
@@ -813,6 +816,7 @@ public class SatelliteControllerTest extends TelephonyTest {
        processAllMessages();
        verifySatelliteEnabled(false, SATELLITE_RESULT_SUCCESS);
        assertTrue(mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled);
        assertTrue(mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled);
        assertEquals(
                SATELLITE_MODE_ENABLED_FALSE, mSatelliteControllerUT.satelliteModeSettingValue);
        verify(mMockSatelliteSessionController, times(2)).onSatelliteEnabledStateChanged(eq(false));
@@ -838,6 +842,7 @@ public class SatelliteControllerTest extends TelephonyTest {
        mIIntegerConsumerResults.clear();
        clearInvocations(mMockPointingAppController);
        mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled = false;
        mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled = false;
        setUpResponseForRequestSatelliteEnabled(true, false, false,
                SATELLITE_RESULT_INVALID_MODEM_STATE);
        mSatelliteControllerUT.requestSatelliteEnabled(SUB_ID, true, false, false,
@@ -849,11 +854,13 @@ public class SatelliteControllerTest extends TelephonyTest {
        verify(mMockPointingAppController, never()).startPointingUI(anyBoolean(), anyBoolean(),
                anyBoolean());
        assertFalse(mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled);
        assertFalse(mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled);
        verify(mMockControllerMetricsStats, times(1)).reportServiceEnablementFailCount();

        // Successfully enable satellite when radio is on.
        mIIntegerConsumerResults.clear();
        mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled = false;
        mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled = false;
        setUpResponseForRequestSatelliteEnabled(true, false, false, SATELLITE_RESULT_SUCCESS);
        mSatelliteControllerUT.requestSatelliteEnabled(SUB_ID, true, false, false,
                mIIntegerConsumer);
@@ -862,6 +869,7 @@ public class SatelliteControllerTest extends TelephonyTest {
        assertEquals(SATELLITE_RESULT_SUCCESS, (long) mIIntegerConsumerResults.get(0));
        verifySatelliteEnabled(true, SATELLITE_RESULT_SUCCESS);
        assertTrue(mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled);
        assertTrue(mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled);
        assertEquals(SATELLITE_MODE_ENABLED_TRUE, mSatelliteControllerUT.satelliteModeSettingValue);
        verify(mMockSatelliteSessionController, times(2)).onSatelliteEnabledStateChanged(eq(true));
        verify(mMockSatelliteSessionController, times(4)).setDemoMode(eq(false));
@@ -3458,6 +3466,7 @@ public class SatelliteControllerTest extends TelephonyTest {

        // startSendingNtnSignalStrength should be invoked when satellite is enabled
        mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled = false;
        mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled = false;
        setUpResponseForRequestSatelliteEnabled(true, false, false, SATELLITE_RESULT_SUCCESS);
        mSatelliteControllerUT.requestSatelliteEnabled(SUB_ID, true, false, false,
                mIIntegerConsumer);
@@ -3466,6 +3475,7 @@ public class SatelliteControllerTest extends TelephonyTest {
        assertEquals(SATELLITE_RESULT_SUCCESS, (long) mIIntegerConsumerResults.get(0));
        verifySatelliteEnabled(true, SATELLITE_RESULT_SUCCESS);
        assertTrue(mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled);
        assertTrue(mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled);
        assertEquals(
                SATELLITE_MODE_ENABLED_TRUE, mSatelliteControllerUT.satelliteModeSettingValue);
        verify(mMockSatelliteModemInterface, times(1)).startSendingNtnSignalStrength(
@@ -3517,6 +3527,7 @@ public class SatelliteControllerTest extends TelephonyTest {
        reset(mMockSatelliteModemInterface);
        doReturn(true).when(mMockSatelliteModemInterface).isSatelliteServiceSupported();
        mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled = false;
        mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled = false;
        setUpResponseForRequestSatelliteEnabled(true, false, false, SATELLITE_RESULT_SUCCESS);
        mSatelliteControllerUT.requestSatelliteEnabled(SUB_ID, true, false, false,
                mIIntegerConsumer);
@@ -3525,6 +3536,7 @@ public class SatelliteControllerTest extends TelephonyTest {
        assertEquals(SATELLITE_RESULT_SUCCESS, (long) mIIntegerConsumerResults.get(0));
        verifySatelliteEnabled(true, SATELLITE_RESULT_SUCCESS);
        assertTrue(mSatelliteControllerUT.setSettingsKeyForSatelliteModeCalled);
        assertTrue(mSatelliteControllerUT.setSettingsKeyToAllowDeviceRotationCalled);
        assertEquals(
                SATELLITE_MODE_ENABLED_TRUE, mSatelliteControllerUT.satelliteModeSettingValue);
        verify(mMockSatelliteModemInterface, times(1)).startSendingNtnSignalStrength(
@@ -4479,6 +4491,7 @@ public class SatelliteControllerTest extends TelephonyTest {
        public boolean allRadiosDisabled = true;
        public long elapsedRealtime = 0;
        public int satelliteModeSettingValue = SATELLITE_MODE_ENABLED_FALSE;
        public boolean setSettingsKeyToAllowDeviceRotationCalled = false;

        TestSatelliteController(
                Context context, Looper looper, @NonNull FeatureFlags featureFlags) {
@@ -4498,6 +4511,12 @@ public class SatelliteControllerTest extends TelephonyTest {
            setSettingsKeyForSatelliteModeCalled = true;
        }

        @Override
        protected void setSettingsKeyToAllowDeviceRotation(int val) {
            logd("setSettingsKeyToAllowDeviceRotation: val=" + val);
            setSettingsKeyToAllowDeviceRotationCalled = true;
        }

        @Override
        protected boolean areAllRadiosDisabled() {
            return allRadiosDisabled;