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

Commit 5d2397d1 authored by rambowang's avatar rambowang
Browse files

Update CarrierServiceStateTracker with new CarrierConfigManager APIs

Improve the performance of CarrierServiceStateTracker by:
- Replacing the carrier config change broadcast receiver with listener
  which has much lower lagency.
- Retrieving subset (instead of all) of carrier config to consume less
  memory

Bug: 263267340
Test: atest CarrierServiceStateTrackerTest
Change-Id: If81ff78fcc07a3ac53181ab70c2e9e40274dd112
parent dd886588
Loading
Loading
Loading
Loading
+36 −29
Original line number Diff line number Diff line
@@ -19,10 +19,8 @@ package com.android.internal.telephony;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Handler;
import android.os.HandlerExecutor;
@@ -102,8 +100,34 @@ public class CarrierServiceStateTracker extends Handler {
        this.mSST = sst;
        mTelephonyManager = mPhone.getContext().getSystemService(
                TelephonyManager.class).createForSubscriptionId(mPhone.getSubId());
        phone.getContext().registerReceiver(mBroadcastReceiver, new IntentFilter(
                CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));
        CarrierConfigManager ccm = mPhone.getContext().getSystemService(CarrierConfigManager.class);
        ccm.registerCarrierConfigChangeListener(
                mPhone.getContext().getMainExecutor(),
                (slotIndex, subId, carrierId, specificCarrierId) -> {
                    if (slotIndex != mPhone.getPhoneId()) return;

                    Rlog.d(LOG_TAG, "onCarrierConfigChanged: slotIndex=" + slotIndex
                            + ", subId=" + subId + ", carrierId=" + carrierId);

                    // Only get carrier configs used for EmergencyNetworkNotification
                    // and PrefNetworkNotification
                    PersistableBundle b =
                            CarrierConfigManager.getCarrierConfigSubset(
                                    mPhone.getContext(),
                                    mPhone.getSubId(),
                                    CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT,
                                    CarrierConfigManager
                                            .KEY_PREF_NETWORK_NOTIFICATION_DELAY_INT);
                    if (b.isEmpty()) return;

                    for (Map.Entry<Integer, NotificationType> entry :
                            mNotificationTypeMap.entrySet()) {
                        NotificationType notificationType = entry.getValue();
                        notificationType.setDelay(b);
                    }
                    handleConfigChanges();
                });

        // Listen for subscriber changes
        SubscriptionManager.from(mPhone.getContext()).addOnSubscriptionsChangedListener(
                new OnSubscriptionsChangedListener(this.getLooper()) {
@@ -246,7 +270,7 @@ public class CarrierServiceStateTracker extends Handler {
        TelephonyManager tm = ((TelephonyManager) context.getSystemService(
                Context.TELEPHONY_SERVICE)).createForSubscriptionId(mPhone.getSubId());

        boolean isCarrierConfigEnabled = isCarrierConfigEnableNr(context);
        boolean isCarrierConfigEnabled = isCarrierConfigEnableNr();
        boolean isRadioAccessFamilySupported = checkSupportedBitmask(
                tm.getSupportedRadioAccessFamily(), TelephonyManager.NETWORK_TYPE_BITMASK_NR);
        boolean isNrNetworkTypeAllowed = checkSupportedBitmask(
@@ -261,15 +285,13 @@ public class CarrierServiceStateTracker extends Handler {
        return (isCarrierConfigEnabled && isRadioAccessFamilySupported && isNrNetworkTypeAllowed);
    }

    private boolean isCarrierConfigEnableNr(Context context) {
        CarrierConfigManager carrierConfigManager = (CarrierConfigManager)
                context.getSystemService(Context.CARRIER_CONFIG_SERVICE);
        if (carrierConfigManager == null) {
            Rlog.e(LOG_TAG, "isCarrierConfigEnableNr: CarrierConfigManager is null");
            return false;
        }
        PersistableBundle config = carrierConfigManager.getConfigForSubId(mPhone.getSubId());
        if (config == null) {
    private boolean isCarrierConfigEnableNr() {
        PersistableBundle config =
                CarrierConfigManager.getCarrierConfigSubset(
                        mPhone.getContext(),
                        mPhone.getSubId(),
                        CarrierConfigManager.KEY_CARRIER_NR_AVAILABILITIES_INT_ARRAY);
        if (config.isEmpty()) {
            Rlog.e(LOG_TAG, "isCarrierConfigEnableNr: Cannot get config " + mPhone.getSubId());
            return false;
        }
@@ -348,21 +370,6 @@ public class CarrierServiceStateTracker extends Handler {
        return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            CarrierConfigManager carrierConfigManager = (CarrierConfigManager)
                    context.getSystemService(Context.CARRIER_CONFIG_SERVICE);
            PersistableBundle b = carrierConfigManager.getConfigForSubId(mPhone.getSubId());

            for (Map.Entry<Integer, NotificationType> entry : mNotificationTypeMap.entrySet()) {
                NotificationType notificationType = entry.getValue();
                notificationType.setDelay(b);
            }
            handleConfigChanges();
        }
    };

    /**
     * Post a notification to the NotificationManager for changing network type.
     */
+16 −5
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.internal.telephony;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
@@ -28,7 +29,6 @@ import static org.mockito.Mockito.when;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
@@ -43,9 +43,11 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.MockitoAnnotations;

import java.util.Map;
import java.util.concurrent.Executor;

/**
 * Unit tests for {@link com.android.internal.telephony.CarrierServiceStateTracker}.
@@ -57,6 +59,7 @@ public class CarrierServiceStateTrackerTest extends TelephonyTest {

    private CarrierServiceStateTracker mSpyCarrierSST;
    private CarrierServiceStateTracker mCarrierSST;
    private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;

    private static final int SUB_ID = 1;

@@ -68,10 +71,18 @@ public class CarrierServiceStateTrackerTest extends TelephonyTest {
        MockitoAnnotations.initMocks(this);
        logd(LOG_TAG + "Setup!");
        super.setUp(getClass().getSimpleName());
        doReturn((Executor) Runnable::run).when(mContext).getMainExecutor();
        mBundle = mContextFixture.getCarrierConfigBundle();
        when(mPhone.getSubId()).thenReturn(SUB_ID);
        when(mCarrierConfigManager.getConfigForSubId(anyInt(), any())).thenReturn(mBundle);

        // Capture listener to emulate the carrier config change notification used later
        ArgumentCaptor<CarrierConfigManager.CarrierConfigChangeListener> listenerArgumentCaptor =
                ArgumentCaptor.forClass(CarrierConfigManager.CarrierConfigChangeListener.class);
        mCarrierSST = new CarrierServiceStateTracker(mPhone, mSST);
        verify(mCarrierConfigManager).registerCarrierConfigChangeListener(any(),
                listenerArgumentCaptor.capture());
        mCarrierConfigChangeListener = listenerArgumentCaptor.getAllValues().get(0);
        mSpyCarrierSST = spy(mCarrierSST);

        mNotificationManager = (NotificationManager) mContext.getSystemService(
@@ -140,8 +151,8 @@ public class CarrierServiceStateTrackerTest extends TelephonyTest {
    @SmallTest
    public void testSendPrefNetworkNotification() {
        logd(LOG_TAG + ":testSendPrefNetworkNotification()");
        Intent intent = new Intent().setAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        mContext.sendBroadcast(intent);
        mCarrierConfigChangeListener.onCarrierConfigChanged(0 /* slotIndex */, SUB_ID,
                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
        processAllMessages();

        Map<Integer, CarrierServiceStateTracker.NotificationType> notificationTypeMap =
@@ -190,8 +201,8 @@ public class CarrierServiceStateTrackerTest extends TelephonyTest {
    @SmallTest
    public void testSendEmergencyNetworkNotification() {
        logd(LOG_TAG + ":testSendEmergencyNetworkNotification()");
        Intent intent = new Intent().setAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        mContext.sendBroadcast(intent);
        mCarrierConfigChangeListener.onCarrierConfigChanged(0 /* slotIndex */, SUB_ID,
                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
        processAllMessages();

        Map<Integer, CarrierServiceStateTracker.NotificationType> notificationTypeMap =
+2 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mockito;

import java.util.Collections;
import java.util.concurrent.Executor;

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
@@ -90,6 +91,7 @@ public class DisplayInfoControllerTest extends TelephonyTest {
        logd("DisplayInfoControllerTest setup!");
        super.setUp(getClass().getSimpleName());

        doReturn((Executor) Runnable::run).when(mContext).getMainExecutor();
        mSstHandler = new ServiceStateTrackerTestHandler(getClass().getSimpleName());
        mSstHandler.start();
        waitUntilReady();
+3 −0
Original line number Diff line number Diff line
@@ -112,6 +112,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.Executor;

public class ServiceStateTrackerTest extends TelephonyTest {
    // Mocked classes
@@ -229,6 +230,7 @@ public class ServiceStateTrackerTest extends TelephonyTest {
        mSubInfo = Mockito.mock(SubscriptionInfo.class);
        mServiceStateStats = Mockito.mock(ServiceStateStats.class);

        doReturn((Executor) Runnable::run).when(mContext).getMainExecutor();
        mContextFixture.putResource(R.string.config_wwan_network_service_package,
                "com.android.phone");
        mContextFixture.putResource(R.string.config_wlan_network_service_package,
@@ -242,6 +244,7 @@ public class ServiceStateTrackerTest extends TelephonyTest {

        replaceInstance(ProxyController.class, "sProxyController", null, mProxyController);
        mBundle = mContextFixture.getCarrierConfigBundle();
        when(mCarrierConfigManager.getConfigForSubId(anyInt(), any())).thenReturn(mBundle);
        mBundle.putStringArray(
                CarrierConfigManager.KEY_ROAMING_OPERATOR_STRING_ARRAY, new String[]{"123456"});