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

Commit de3bb2f4 authored by rambowang's avatar rambowang Committed by Rambo Wang
Browse files

Update SignalStrengthController with new CarrierConfigManager APIs

- Replace carrier config change receiver with listener which has much
  lower lagency
- Due to a large list of carrier configs are used and the list may change in near future, getConfigForSubId is NOT replaced with subset version.

Bug: 263267340
Test: atest SignalStrengthControllerTest
Change-Id: Iab2ba44e502bb1134f2db9f3cd95c4a228e69c97
parent f03d253e
Loading
Loading
Loading
Loading
+8 −27
Original line number Diff line number Diff line
@@ -20,10 +20,7 @@ import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.IBinder;
@@ -100,7 +97,6 @@ public class SignalStrengthController extends Handler {
    private static final int EVENT_POLL_SIGNAL_STRENGTH                     = 7;
    private static final int EVENT_SIGNAL_STRENGTH_UPDATE                   = 8;
    private static final int EVENT_POLL_SIGNAL_STRENGTH_DONE                = 9;
    private static final int EVENT_CARRIER_CONFIG_CHANGED                   = 10;

    @NonNull
    private final Phone mPhone;
@@ -145,20 +141,6 @@ public class SignalStrengthController extends Handler {
    @NonNull
    private final LocalLog mLocalLog = new LocalLog(64);

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(action)) {
                int phoneId = intent.getExtras().getInt(CarrierConfigManager.EXTRA_SLOT_INDEX);
                // Ignore the carrier config changed if the phoneId is not matched.
                if (phoneId == mPhone.getPhoneId()) {
                    sendEmptyMessage(EVENT_CARRIER_CONFIG_CHANGED);
                }
            }
        }
    };

    public SignalStrengthController(@NonNull Phone phone) {
        mPhone = phone;
        mCi = mPhone.mCi;
@@ -168,10 +150,12 @@ public class SignalStrengthController extends Handler {
        mCi.setOnSignalStrengthUpdate(this, EVENT_SIGNAL_STRENGTH_UPDATE, null);
        setSignalStrengthDefaultValues();

        CarrierConfigManager ccm = mPhone.getContext().getSystemService(CarrierConfigManager.class);
        mCarrierConfig = getCarrierConfig();
        IntentFilter filter = new IntentFilter();
        filter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        mPhone.getContext().registerReceiver(mBroadcastReceiver, filter);
        // Callback which directly handle config change should be executed on handler thread
        ccm.registerCarrierConfigChangeListener(this::post,
                (slotIndex, subId, carrierId, specificCarrierId) ->
                        onCarrierConfigurationChanged(slotIndex));
    }

    @Override
@@ -284,11 +268,6 @@ public class SignalStrengthController extends Handler {
                break;
            }

            case EVENT_CARRIER_CONFIG_CHANGED: {
                onCarrierConfigChanged();
                break;
            }

            default:
                log("Unhandled message with number: " + msg.what);
                break;
@@ -1129,7 +1108,9 @@ public class SignalStrengthController extends Handler {
        return earfcnPairList;
    }

    private void onCarrierConfigChanged() {
    private void onCarrierConfigurationChanged(int slotIndex) {
        if (slotIndex != mPhone.getPhoneId()) return;

        mCarrierConfig = getCarrierConfig();
        log("Carrier Config changed.");

+17 −8
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
@@ -34,7 +35,6 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
@@ -50,6 +50,7 @@ import android.telephony.CellSignalStrengthWcdma;
import android.telephony.SignalStrength;
import android.telephony.SignalStrengthUpdateRequest;
import android.telephony.SignalThresholdInfo;
import android.telephony.TelephonyManager;
import android.test.suitebuilder.annotation.MediumTest;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
@@ -94,16 +95,13 @@ public class SignalStrengthControllerTest extends TelephonyTest {

    private SignalStrengthController mSsc;
    private PersistableBundle mBundle;
    private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;

    @Before
    public void setUp() throws Exception {
        super.setUp(this.getClass().getSimpleName());
        mHandler = Mockito.mock(Handler.class);

        when(mPhone.getSubId()).thenReturn(ACTIVE_SUB_ID);
        mSsc = new SignalStrengthController(mPhone);
        replaceInstance(Handler.class, "mLooper", mHandler, mSsc.getLooper());
        replaceInstance(Phone.class, "mLooper", mPhone, mSsc.getLooper());

        // Config a fixed supported RAN/MeasurementTypes to make the test more stable
        mBundle = mContextFixture.getCarrierConfigBundle();
@@ -153,6 +151,18 @@ public class SignalStrengthControllerTest extends TelephonyTest {
                        15, /* SIGNAL_STRENGTH_GOOD */
                        30  /* SIGNAL_STRENGTH_GREAT */
                });

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

        replaceInstance(Handler.class, "mLooper", mHandler, mSsc.getLooper());
        replaceInstance(Phone.class, "mLooper", mPhone, mSsc.getLooper());

        processAllMessages();
        reset(mSimulatedCommandsVerifier);
    }
@@ -964,9 +974,8 @@ public class SignalStrengthControllerTest extends TelephonyTest {
                .thenReturn(mockConfigManager);
        when(mockConfigManager.getConfigForSubId(anyInt())).thenReturn(mBundle);

        Intent intent = new Intent().setAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, PHONE_ID);
        mContext.sendBroadcast(intent);
        mCarrierConfigChangeListener.onCarrierConfigChanged(PHONE_ID, ACTIVE_SUB_ID,
                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
        processAllMessages();
    }