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

Commit 2eaa6aad authored by Hakjun Choi's avatar Hakjun Choi Committed by Android (Google) Code Review
Browse files

Merge "Disable signal strength update if screen off" into main

parents c5669111 4bf13b9f
Loading
Loading
Loading
Loading
+44 −1
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static android.hardware.radio.V1_0.DeviceStateType.LOW_DATA_EXPECTED;
import static android.hardware.radio.V1_0.DeviceStateType.POWER_SAVE_MODE;
import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;

import android.annotation.NonNull;
import android.app.UiModeManager;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -47,6 +48,7 @@ import android.util.LocalLog;
import android.view.Display;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.flags.FeatureFlags;
import com.android.internal.util.IndentingPrintWriter;
import com.android.telephony.Rlog;

@@ -92,10 +94,14 @@ public class DeviceStateMonitor extends Handler {
    private static final int NR_NSA_TRACKING_INDICATIONS_ALWAYS_ON = 2;

    private final Phone mPhone;
    @NonNull
    private final FeatureFlags mFeatureFlags;

    private final LocalLog mLocalLog = new LocalLog(64);

    private final RegistrantList mPhysicalChannelConfigRegistrants = new RegistrantList();
    private final RegistrantList mSignalStrengthReportDecisionCallbackRegistrants =
            new RegistrantList();

    private final NetworkRequest mWifiNetworkRequest =
            new NetworkRequest.Builder()
@@ -269,8 +275,9 @@ public class DeviceStateMonitor extends Handler {
     *
     * @param phone Phone object
     */
    public DeviceStateMonitor(Phone phone) {
    public DeviceStateMonitor(Phone phone, @NonNull FeatureFlags featureFlags) {
        mPhone = phone;
        mFeatureFlags = featureFlags;
        DisplayManager dm = (DisplayManager) phone.getContext().getSystemService(
                Context.DISPLAY_SERVICE);
        dm.registerDisplayListener(mDisplayListener, null);
@@ -602,6 +609,15 @@ public class DeviceStateMonitor extends Handler {
            // use a null message since we don't care of receiving response
            mPhone.mCi.getBarringInfo(null);
        }

        // Determine whether to notify registrants about the non-terrestrial signal strength change.
        if (mFeatureFlags.oemEnabledSatelliteFlag()) {
            if (shouldEnableSignalStrengthReports()) {
                mSignalStrengthReportDecisionCallbackRegistrants.notifyResult(true);
            } else {
                mSignalStrengthReportDecisionCallbackRegistrants.notifyResult(false);
            }
        }
    }

    /**
@@ -777,6 +793,33 @@ public class DeviceStateMonitor extends Handler {
        mPhysicalChannelConfigRegistrants.remove(h);
    }

    /**
     * Register a callback to decide whether signal strength should be notified or not.
     * @param h Handler to notify
     * @param what msg.what when the message is delivered
     * @param obj AsyncResult.userObj when the message is delivered
     */
    public void registerForSignalStrengthReportDecision(Handler h, int what, Object obj) {
        if (!mFeatureFlags.oemEnabledSatelliteFlag()) {
            Rlog.d(TAG, "oemEnabledSatelliteFlag is disabled");
            return;
        }
        Registrant r = new Registrant(h, what, obj);
        mSignalStrengthReportDecisionCallbackRegistrants.add(r);
    }

    /**
     * Register a callback to decide whether signal strength should be notified or not.
     * @param h Handler to notify
     */
    public void unregisterForSignalStrengthReportDecision(Handler h) {
        if (!mFeatureFlags.oemEnabledSatelliteFlag()) {
            Rlog.d(TAG, "oemEnabledSatelliteFlag is disabled");
            return;
        }
        mSignalStrengthReportDecisionCallbackRegistrants.remove(h);
    }

    /**
     * @param msg Debug message
     * @param logIntoLocalLog True if log into the local log
+1 −1
Original line number Diff line number Diff line
@@ -359,7 +359,7 @@ public class GsmCdmaPhone extends Phone {
                .inject(EmergencyNumberTracker.class.getName()).makeEmergencyNumberTracker(
                        this, this.mCi);
        mDeviceStateMonitor = mTelephonyComponentFactory.inject(DeviceStateMonitor.class.getName())
                .makeDeviceStateMonitor(this);
                .makeDeviceStateMonitor(this, mFeatureFlags);

        // DisplayInfoController creates an OverrideNetworkTypeController, which uses
        // DeviceStateMonitor so needs to be crated after it is instantiated.
+6 −2
Original line number Diff line number Diff line
@@ -406,8 +406,12 @@ public class TelephonyComponentFactory {
        return new AppSmsManager(context);
    }

    public DeviceStateMonitor makeDeviceStateMonitor(Phone phone) {
        return new DeviceStateMonitor(phone);
    /**
     * Create a DeviceStateMonitor.
     */
    public DeviceStateMonitor makeDeviceStateMonitor(Phone phone,
            @NonNull FeatureFlags featureFlags) {
        return new DeviceStateMonitor(phone, featureFlags);
    }

    /**
+22 −14
Original line number Diff line number Diff line
@@ -74,6 +74,7 @@ import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.CommandsInterface;
import com.android.internal.telephony.DeviceStateMonitor;
import com.android.internal.telephony.IIntegerConsumer;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.flags.FeatureFlags;
@@ -146,7 +147,7 @@ public class SatelliteController extends Handler {
    private static final int EVENT_REQUEST_NTN_SIGNAL_STRENGTH_DONE = 33;
    private static final int EVENT_NTN_SIGNAL_STRENGTH_CHANGED = 34;
    private static final int CMD_START_SENDING_NTN_SIGNAL_STRENGTH = 35;
    private static final int EVENT_START_SENDING_NTN_SIGNAL_STRENGTH_DONE = 36;
    private static final int EVENT_UPDATE_SIGNAL_STRENGTH_REPORTING = 36;

    @NonNull private static SatelliteController sInstance;
    @NonNull private final Context mContext;
@@ -159,6 +160,7 @@ public class SatelliteController extends Handler {
    @NonNull private final SubscriptionManagerService mSubscriptionManagerService;
    private final CommandsInterface mCi;
    private ContentResolver mContentResolver = null;
    private final DeviceStateMonitor mDSM;

    private final Object mRadioStateLock = new Object();

@@ -299,6 +301,7 @@ public class SatelliteController extends Handler {
        mFeatureFlags = featureFlags;
        Phone phone = SatelliteServiceUtils.getPhone();
        mCi = phone.mCi;
        mDSM = phone.getDeviceStateMonitor();
        // Create the SatelliteModemInterface singleton, which is used to manage connections
        // to the satellite service and HAL interface.
        mSatelliteModemInterface = SatelliteModemInterface.make(mContext, this);
@@ -353,6 +356,8 @@ public class SatelliteController extends Handler {
                        handleCarrierConfigChanged(slotIndex, subId, carrierId, specificCarrierId);
        mCarrierConfigManager.registerCarrierConfigChangeListener(
                        new HandlerExecutor(new Handler(looper)), mCarrierConfigChangeListener);
        mDSM.registerForSignalStrengthReportDecision(this, CMD_START_SENDING_NTN_SIGNAL_STRENGTH,
                null);
    }

    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
@@ -1129,26 +1134,29 @@ public class SatelliteController extends Handler {
            }

            case CMD_START_SENDING_NTN_SIGNAL_STRENGTH: {
                logd("CMD_START_SENDING_NTN_SIGNAL_STRENGTH");
                request = (SatelliteControllerHandlerRequest) msg.obj;
                boolean startSendingNtnSignalStrength =  (boolean) request.argument;
                if (mSatelliteModemInterface.isSatelliteServiceSupported()) {
                    onCompleted = obtainMessage(EVENT_START_SENDING_NTN_SIGNAL_STRENGTH_DONE,
                ar = (AsyncResult) msg.obj;
                boolean shouldReport = (boolean) ar.result;
                logd("CMD_START_SENDING_NTN_SIGNAL_STRENGTH: shouldReport=" + shouldReport);
                request = new SatelliteControllerHandlerRequest(shouldReport,
                        SatelliteServiceUtils.getPhone());
                if (SATELLITE_RESULT_SUCCESS != evaluateOemSatelliteRequestAllowed(true)) {
                    return;
                }
                onCompleted = obtainMessage(EVENT_UPDATE_SIGNAL_STRENGTH_REPORTING,
                        request);
                    if (startSendingNtnSignalStrength) {
                if (shouldReport) {
                    mSatelliteModemInterface.startSendingNtnSignalStrength(onCompleted);
                } else {
                    mSatelliteModemInterface.stopSendingNtnSignalStrength(onCompleted);
                }
                }
                break;
            }

            case EVENT_START_SENDING_NTN_SIGNAL_STRENGTH_DONE: {
            case EVENT_UPDATE_SIGNAL_STRENGTH_REPORTING: {
                ar = (AsyncResult) msg.obj;
                request = (SatelliteControllerHandlerRequest) ar.userObj;
                int errorCode =  SatelliteServiceUtils.getSatelliteError(ar,
                        "EVENT_START_SENDING_NTN_SIGNAL_STRENGTH_DONE");
                        "EVENT_UPDATE_SIGNAL_STRENGTH_REPORTING");
                if (errorCode != SATELLITE_RESULT_SUCCESS) {
                    loge(((boolean) request.argument ? "startSendingNtnSignalStrength"
                            : "stopSendingNtnSignalStrength") + "returns " + errorCode);
+175 −1
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ import static android.hardware.radio.V1_0.DeviceStateType.LOW_DATA_EXPECTED;
import static android.hardware.radio.V1_0.DeviceStateType.POWER_SAVE_MODE;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
@@ -30,6 +32,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import static java.util.Arrays.asList;

@@ -40,22 +43,31 @@ import android.content.Intent;
import android.hardware.radio.V1_5.IndicationFilter;
import android.net.ConnectivityManager;
import android.net.TetheringManager;
import android.os.AsyncResult;
import android.os.BatteryManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.test.suitebuilder.annotation.MediumTest;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;

import com.android.internal.telephony.flags.FeatureFlags;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
@@ -114,6 +126,7 @@ public class DeviceStateMonitorTest extends TelephonyTest {

    private static final int STATE_OFF = 0;
    private static final int STATE_ON = 1;
    private static final long TIMEOUT = 500;

    // The keys are the single IndicationFilter flags,
    // The values are the array of states, when one state turn on, the corresponding
@@ -135,6 +148,9 @@ public class DeviceStateMonitorTest extends TelephonyTest {
    UiModeManager mUiModeManager;

    private DeviceStateMonitor mDSM;
    private TestSatelliteController mSatelliteControllerUT;

    @Mock private FeatureFlags mFeatureFlags;

    // Given a stateType, return the event type that can change the state
    private int state2Event(@StateType int stateType) {
@@ -162,11 +178,12 @@ public class DeviceStateMonitorTest extends TelephonyTest {
    @Before
    public void setUp() throws Exception {
        super.setUp(getClass().getSimpleName());
        MockitoAnnotations.initMocks(this);
        mUiModeManager = mock(UiModeManager.class);
        mContextFixture.setSystemService(Context.UI_MODE_SERVICE, mUiModeManager);
        // We don't even need a mock executor, we just need to not throw.
        doReturn(null).when(mContextFixture.getTestDouble()).getMainExecutor();
        mDSM = new DeviceStateMonitor(mPhone);
        mDSM = new DeviceStateMonitor(mPhone, mFeatureFlags);

        // Initialize with ALL states off
        updateAllStatesToOff();
@@ -177,6 +194,7 @@ public class DeviceStateMonitorTest extends TelephonyTest {

    @After
    public void tearDown() throws Exception {
        mSatelliteControllerUT = null;
        mDSM = null;
        super.tearDown();
    }
@@ -453,4 +471,160 @@ public class DeviceStateMonitorTest extends TelephonyTest {
        verify(mSimulatedCommandsVerifier).setUnsolResponseFilter(
                eq(INDICATION_FILTERS_MINIMUM), nullable(Message.class));
    }

    @Test
    public void testRegisterForSignalStrengthReportDecisionWithFeatureEnabled() {
        logd("testRegisterForSignalStrengthReportDecisionWithFeatureEnabled()");
        when(mFeatureFlags.oemEnabledSatelliteFlag()).thenReturn(true);
        mSatelliteControllerUT = new TestSatelliteController(Looper.myLooper(), mDSM);

        updateState(STATE_TYPE_RADIO_OFF_OR_NOT_AVAILABLE, 0);
        updateState(STATE_TYPE_SCREEN, STATE_OFF);
        mSatelliteControllerUT.resetCount();
        sEventDeviceStatusChanged.drainPermits();

        updateState(STATE_TYPE_SCREEN, STATE_ON);
        assertTrue(waitForEventDeviceStatusChanged());
        assertEquals(0, mSatelliteControllerUT.getStartEventCount());
        assertEquals(1, mSatelliteControllerUT.getStopEventCount());
        mSatelliteControllerUT.resetCount();

        mSatelliteControllerUT.resetCount();
        updateState(STATE_TYPE_SCREEN, STATE_OFF);
        assertTrue(waitForEventDeviceStatusChanged());
        assertEquals(0, mSatelliteControllerUT.getStartEventCount());
        assertEquals(1, mSatelliteControllerUT.getStopEventCount());
        mSatelliteControllerUT.resetCount();

        updateState(STATE_TYPE_RADIO_ON, 0);
        assertTrue(waitForEventDeviceStatusChanged());
        assertEquals(0, mSatelliteControllerUT.getStartEventCount());
        assertEquals(1, mSatelliteControllerUT.getStopEventCount());
        mSatelliteControllerUT.resetCount();

        updateState(STATE_TYPE_SCREEN, STATE_ON);
        assertTrue(waitForEventDeviceStatusChanged());
        assertEquals(1, mSatelliteControllerUT.getStartEventCount());
        assertEquals(0, mSatelliteControllerUT.getStopEventCount());
        mSatelliteControllerUT.resetCount();

        updateState(STATE_TYPE_RADIO_OFF_OR_NOT_AVAILABLE, 0);
        assertTrue(waitForEventDeviceStatusChanged());
        assertEquals(0, mSatelliteControllerUT.getStartEventCount());
        assertEquals(1, mSatelliteControllerUT.getStopEventCount());
    }

    @Test
    public void testRegisterForSignalStrengthReportDecisionWithFeatureDisabled() {
        logd("testRegisterForSignalStrengthReportDecisionWithFeatureDisabled()");
        when(mFeatureFlags.oemEnabledSatelliteFlag()).thenReturn(false);
        mSatelliteControllerUT = new TestSatelliteController(Looper.myLooper(), mDSM);

        updateState(STATE_TYPE_RADIO_OFF_OR_NOT_AVAILABLE, 0);
        updateState(STATE_TYPE_SCREEN, STATE_OFF);
        mSatelliteControllerUT.resetCount();
        sEventDeviceStatusChanged.drainPermits();


        /* Sending stop ntn signal strength as radio is off */
        updateState(STATE_TYPE_SCREEN, STATE_ON);
        assertFalse(waitForEventDeviceStatusChanged());
        assertEquals(0, mSatelliteControllerUT.getStartEventCount());
        assertEquals(0, mSatelliteControllerUT.getStopEventCount());

        updateState(STATE_TYPE_SCREEN, STATE_OFF);
        assertFalse(waitForEventDeviceStatusChanged());
        assertEquals(0, mSatelliteControllerUT.getStartEventCount());
        assertEquals(0, mSatelliteControllerUT.getStopEventCount());

        updateState(STATE_TYPE_RADIO_ON, 0);
        assertFalse(waitForEventDeviceStatusChanged());
        assertEquals(0, mSatelliteControllerUT.getStartEventCount());
        assertEquals(0, mSatelliteControllerUT.getStopEventCount());

        updateState(STATE_TYPE_SCREEN, STATE_ON);
        assertFalse(waitForEventDeviceStatusChanged());
        assertEquals(0, mSatelliteControllerUT.getStartEventCount());
        assertEquals(0, mSatelliteControllerUT.getStopEventCount());

        updateState(STATE_TYPE_RADIO_OFF_OR_NOT_AVAILABLE, 0);
        assertFalse(waitForEventDeviceStatusChanged());
        assertEquals(0, mSatelliteControllerUT.getStartEventCount());
        assertEquals(0, mSatelliteControllerUT.getStopEventCount());
    }

    private static Semaphore sEventDeviceStatusChanged = new Semaphore(0);
    private boolean waitForEventDeviceStatusChanged() {
        try {
            if (!sEventDeviceStatusChanged.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS)) {
                logd("Time out to receive EVENT_DEVICE_STATUS_CHANGED");
                return false;
            }
        } catch (Exception ex) {
            logd("waitForEventDeviceStatusChanged: ex=" + ex);
            return false;
        }
        return true;
    }

    private static class TestSatelliteController extends Handler {
        public static final int EVENT_DEVICE_STATUS_CHANGED = 35;
        private final DeviceStateMonitor mDsm;
        private int mStartEventCount;
        private int mStopEventCount;

        TestSatelliteController(Looper looper, DeviceStateMonitor dsm) {
            super(looper);
            mDsm = dsm;
            mDsm.registerForSignalStrengthReportDecision(this, EVENT_DEVICE_STATUS_CHANGED, null);
        }

        /**
         * Resets the count of occurred events.
         */
        public void resetCount() {
            mStartEventCount = 0;
            mStopEventCount = 0;
        }

        public int getStartEventCount() {
            return mStartEventCount;
        }

        public int getStopEventCount() {
            return mStopEventCount;
        }

        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case EVENT_DEVICE_STATUS_CHANGED: {
                    logd("EVENT_DEVICE_STATUS_CHANGED");
                    AsyncResult ar = (AsyncResult) msg.obj;
                    boolean shouldReport = (boolean) ar.result;
                    if (shouldReport) {
                        startSendingNtnSignalStrength();
                    } else {
                        stopSendingNtnSignalStrength();
                    }
                    try {
                        sEventDeviceStatusChanged.release();
                    } catch (Exception ex) {
                        logd("waitForEventDeviceStatusChanged: ex=" + ex);
                    }
                    break;
                }
                default:
                    break;
            }
        }

        private void startSendingNtnSignalStrength() {
            mStartEventCount++;
        }

        private void stopSendingNtnSignalStrength() {
            mStopEventCount++;
        }
    }
}
Loading