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

Commit 563ed065 authored by joonhunshin's avatar joonhunshin
Browse files

Changed to use AlarmManager to trigger screen off timer even when device is in deep sleep.

Bug: 373971182
Test: atest SatelliteSessionControllerTest
      SatelliteManagerTestOnMockService
      Regration test b/374011391
Flag: EXEMPT bug fix
Change-Id: If3f190ae1e7520d9f06829559a5da35b9a165502
parent a2ebc644
Loading
Loading
Loading
Loading
+40 −2
Original line number Diff line number Diff line
@@ -40,18 +40,22 @@ import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_SUCC

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.AlarmManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncResult;
import android.os.Build;
import android.os.HandlerExecutor;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.PersistableBundle;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.WorkSource;
import android.telephony.DropBoxManagerLoggerBackend;
import android.telephony.PersistentLogger;
import android.telephony.ServiceState;
@@ -183,8 +187,15 @@ public class SatelliteSessionController extends StateMachine {
    @Nullable private PersistentLogger mPersistentLogger = null;
    @Nullable private DeviceStateMonitor mDeviceStateMonitor;
    @NonNull private SessionMetricsStats mSessionMetricsStats;

    @NonNull private FeatureFlags mFeatureFlags;
    @NonNull private AlarmManager mAlarmManager;
    private final AlarmManager.OnAlarmListener mAlarmListener = new AlarmManager.OnAlarmListener() {
        @Override
        public void onAlarm() {
            plogd("onAlarm: screen off timer expired");
            sendMessage(EVENT_SCREEN_OFF_INACTIVITY_TIMER_TIMED_OUT);
        }
    };

    /**
     * @return The singleton instance of SatelliteSessionController.
@@ -296,6 +307,7 @@ public class SatelliteSessionController extends StateMachine {
        }
        mDeviceStateMonitor = satellitePhone.getDeviceStateMonitor();
        mSessionMetricsStats = SessionMetricsStats.getInstance();
        mAlarmManager = mContext.getSystemService(AlarmManager.class);

        if (mFeatureFlags.carrierRoamingNbIotNtn()) {
            // Register to received Cellular service state
@@ -594,6 +606,9 @@ public class SatelliteSessionController extends StateMachine {
        plogd("cleanUpResource");
        mIsDeviceAlignedWithSatellite = false;
        unregisterForScreenStateChanged();
        if (mAlarmManager != null) {
            mAlarmManager.cancel(mAlarmListener);
        }

        if (mFeatureFlags.carrierRoamingNbIotNtn()) {
            // Register to received Cellular service state
@@ -621,6 +636,16 @@ public class SatelliteSessionController extends StateMachine {
        sendMessage(EVENT_SERVICE_STATE_CHANGED, new AsyncResult(null, serviceState, null));
    }

    /**
     * Uses this function to set AlarmManager object for testing.
     *
     * @param alarmManager The instance of AlarmManager.
     */
    @VisibleForTesting
    public void setAlarmManager(AlarmManager alarmManager) {
        mAlarmManager = alarmManager;
    }

    private boolean isDemoMode() {
        return mIsDemoMode;
    }
@@ -1677,13 +1702,26 @@ public class SatelliteSessionController extends StateMachine {
        if (!screenOn) {
            // Screen off, start timer
            int timeoutMillis = getScreenOffInactivityTimeoutDurationSec() * 1000;
            sendMessageDelayed(EVENT_SCREEN_OFF_INACTIVITY_TIMER_TIMED_OUT, timeoutMillis);

            if (mAlarmManager == null) {
                plogd("handleEventScreenStateChanged: can not access AlarmManager to start timer");
                return;
            }

            mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    SystemClock.elapsedRealtime() + timeoutMillis,
                    TAG, new HandlerExecutor(getHandler()), new WorkSource(), mAlarmListener);
            plogd("handleEventScreenStateChanged: start timer " + timeoutMillis);
        } else {
            // Screen on, stop timer
            removeMessages(EVENT_SCREEN_OFF_INACTIVITY_TIMER_TIMED_OUT);

            if (mAlarmManager == null) {
                plogd("handleEventScreenStateChanged: can not access AlarmManager to stop timer");
                return;
            }

            mAlarmManager.cancel(mAlarmListener);
            plogd("handleEventScreenStateChanged: stop timer");
        }
    }
+32 −13
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
@@ -50,6 +52,7 @@ import static org.mockito.Mockito.when;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.AlarmManager;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncResult;
@@ -79,6 +82,7 @@ import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -116,9 +120,12 @@ public class SatelliteSessionControllerTest extends TelephonyTest {
    @Mock private DatagramController mMockDatagramController;
    @Mock private ServiceState mMockServiceState;
    @Mock private SessionMetricsStats mMockSessionMetricsStats;
    @Mock private AlarmManager mAlarmManager;

    @Captor ArgumentCaptor<Handler> mHandlerCaptor;
    @Captor ArgumentCaptor<Integer> mMsgCaptor;
    @Captor ArgumentCaptor<Executor> mExecutorArgumentCaptor;
    @Captor ArgumentCaptor<AlarmManager.OnAlarmListener> mOnAlarmListenerArgumentCaptor;

    @Before
    public void setUp() throws Exception {
@@ -160,6 +167,7 @@ public class SatelliteSessionControllerTest extends TelephonyTest {
                mTestSatelliteModemStateCallback);
        assertSuccessfulModemStateChangedCallback(
                mTestSatelliteModemStateCallback, SatelliteManager.SATELLITE_MODEM_STATE_OFF);
        mTestSatelliteSessionController.setAlarmManager(mAlarmManager);
    }

    @After
@@ -237,11 +245,18 @@ public class SatelliteSessionControllerTest extends TelephonyTest {
        processAllMessages();
        clearInvocations(mMockSatelliteController);

        // Verify that the screen off inactivity timer is started.
        assertTrue(mTestSatelliteSessionController.isScreenOffInActivityTimerStarted());

        // Time shift to cause timeout
        moveTimeForward(SCREEN_OFF_INACTIVITY_TIMEOUT_SEC * 1000);
        // Verify that the screen off inactivity timer is set.
        verify(mAlarmManager).setExact(
                eq(AlarmManager.ELAPSED_REALTIME_WAKEUP),
                anyLong(),
                anyString(),
                mExecutorArgumentCaptor.capture(),
                any(),
                mOnAlarmListenerArgumentCaptor.capture()
        );
        // Notify alarm expired
        mExecutorArgumentCaptor.getValue().execute(
                () -> mOnAlarmListenerArgumentCaptor.getValue().onAlarm());
        processAllMessages();

        // Verify that SatelliteController#requestSatelliteEnabled() was called.
@@ -287,15 +302,23 @@ public class SatelliteSessionControllerTest extends TelephonyTest {
        sendScreenStateChanged(mHandlerCaptor.getValue(), mMsgCaptor.getValue(), false);
        processAllMessages();

        // Verify that the screen off inactivity timer is started.
        assertTrue(mTestSatelliteSessionController.isScreenOffInActivityTimerStarted());
        // Verify that the screen off inactivity timer is set.
        verify(mAlarmManager).setExact(
                eq(AlarmManager.ELAPSED_REALTIME_WAKEUP),
                anyLong(),
                anyString(),
                mExecutorArgumentCaptor.capture(),
                any(),
                mOnAlarmListenerArgumentCaptor.capture()
        );

        // Notify Screen on
        sendScreenStateChanged(mHandlerCaptor.getValue(), mMsgCaptor.getValue(), true);

        processAllMessages();

        // Verify that the screen off inactivity timer is stopped
        assertFalse(mTestSatelliteSessionController.isScreenOffInActivityTimerStarted());
        // Verify that the screen off inactivity timer is clear.
        verify(mAlarmManager).cancel(eq(mOnAlarmListenerArgumentCaptor.getValue()));
    }

    @Test
@@ -2035,10 +2058,6 @@ public class SatelliteSessionControllerTest extends TelephonyTest {
            return hasDeferredMessages(event);
        }

        boolean isScreenOffInActivityTimerStarted() {
            return hasMessages(EVENT_SCREEN_OFF_INACTIVITY_TIMER_TIMED_OUT);
        }

        protected boolean isSatelliteEnabledForNtnOnlySubscription() {
            return mSatelliteEnabledForNtnOnlySubscription;
        }