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

Commit 960f5d3a authored by Rambo Wang's avatar Rambo Wang Committed by Android Build Cherrypicker Worker
Browse files

Update PinStorage with new CarrierConfigManager APIs

- Replace carrier config change receiver with listener
- Retrieve subset carrier config to save memory

Bug: 244087782
Test: atest PinStorageTest
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:24f60d181041068215df205ad1fc9cc06897a043)
Merged-In: I519c66cbae205735ed8a4d39c34557c9a5789cb4

Change-Id: I954106e4a1365982fe77755ef1b410f6c2881ef5
parent 0b84cee5
Loading
Loading
Loading
Loading
+17 −16
Original line number Diff line number Diff line
@@ -133,7 +133,6 @@ public class PinStorage extends Handler {

    // Events
    private static final int ICC_CHANGED_EVENT = 1;
    private static final int CARRIER_CONFIG_CHANGED_EVENT = 2;
    private static final int TIMER_EXPIRATION_EVENT = 3;
    private static final int USER_UNLOCKED_EVENT = 4;
    private static final int SUPPLY_PIN_COMPLETE = 5;
@@ -157,14 +156,11 @@ public class PinStorage extends Handler {
    private final SparseArray<byte[]> mRamStorage;

    /** Receiver for the required intents. */
    private final BroadcastReceiver mCarrierConfigChangedReceiver = new BroadcastReceiver() {
    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(action)) {
                int slotId = intent.getIntExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, -1);
                sendMessage(obtainMessage(CARRIER_CONFIG_CHANGED_EVENT, slotId, 0));
            } else if (TelephonyManager.ACTION_SIM_CARD_STATE_CHANGED.equals(action)
            if (TelephonyManager.ACTION_SIM_CARD_STATE_CHANGED.equals(action)
                    || TelephonyManager.ACTION_SIM_APPLICATION_STATE_CHANGED.equals(action)) {
                int slotId = intent.getIntExtra(PhoneConstants.PHONE_KEY, -1);
                int state = intent.getIntExtra(
@@ -189,11 +185,16 @@ public class PinStorage extends Handler {

        // Register for necessary intents.
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        intentFilter.addAction(TelephonyManager.ACTION_SIM_CARD_STATE_CHANGED);
        intentFilter.addAction(TelephonyManager.ACTION_SIM_APPLICATION_STATE_CHANGED);
        intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
        mContext.registerReceiver(mCarrierConfigChangedReceiver, intentFilter);
        mContext.registerReceiver(mBroadcastReceiver, intentFilter);

        CarrierConfigManager ccm = mContext.getSystemService(CarrierConfigManager.class);
        // Callback directly handle config change and should be executed in handler thread
        ccm.registerCarrierConfigChangeListener(this::post,
                (slotIndex, subId, carrierId, specificCarrierId) ->
                        onCarrierConfigurationChanged(slotIndex));

        // Initialize the long term secret key. This needs to be present in all cases:
        //  - if the device is not secure or is locked: key does not require user authentication
@@ -561,7 +562,7 @@ public class PinStorage extends Handler {
        }
    }

    private void onCarrierConfigChanged(int slotId) {
    private void onCarrierConfigurationChanged(int slotId) {
        logv("onCarrierConfigChanged[%d]", slotId);
        if (!isCacheAllowed(slotId)) {
            logd("onCarrierConfigChanged[%d] - PIN caching not allowed", slotId);
@@ -591,9 +592,6 @@ public class PinStorage extends Handler {
            case ICC_CHANGED_EVENT:
                onSimStatusChange(/* slotId= */ msg.arg1, /* state= */ msg.arg2);
                break;
            case CARRIER_CONFIG_CHANGED_EVENT:
                onCarrierConfigChanged(/* slotId= */ msg.arg1);
                break;
            case TIMER_EXPIRATION_EVENT:
                onTimerExpiration();
                break;
@@ -996,12 +994,15 @@ public class PinStorage extends Handler {
                mContext.getSystemService(CarrierConfigManager.class);
        if (configManager != null) {
            Phone phone = PhoneFactory.getPhone(slotId);
            if (phone != null) {
            try {
                // If an invalid subId is used, this bundle will contain default values.
                config = configManager.getConfigForSubId(phone.getSubId());
                config = configManager.getConfigForSubId(phone.getSubId(),
                        CarrierConfigManager.KEY_STORE_SIM_PIN_FOR_UNATTENDED_REBOOT_BOOL);
            } catch (RuntimeException e) {
                loge("Can't get carrier config subset.");
            }
        }
        if (config == null) {
        if (config == null || config.isEmpty()) {
            config = CarrierConfigManager.getDefaultConfig();
        }

+31 −7
Original line number Diff line number Diff line
@@ -19,8 +19,11 @@ import static com.android.internal.telephony.uicc.IccCardStatus.PinState.PINSTAT

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Intent;
@@ -29,6 +32,7 @@ import android.os.WorkSource;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
@@ -44,6 +48,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
@@ -58,19 +64,38 @@ public class PinStorageTest extends TelephonyTest {
    private int mBootCount;
    private int mSimulatedRebootsCount;
    private PinStorage mPinStorage;
    private PersistableBundle mBundle;

    // mocks
    private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;

    private void simulateReboot() {
        mSimulatedRebootsCount++;
        Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.BOOT_COUNT, mBootCount + mSimulatedRebootsCount);

        createPinStorageAndCaptureListener();
    }

    private void createPinStorageAndCaptureListener() {
        // Capture listener to emulate the carrier config change notification used later
        ArgumentCaptor<CarrierConfigManager.CarrierConfigChangeListener> listenerArgumentCaptor =
                ArgumentCaptor.forClass(CarrierConfigManager.CarrierConfigChangeListener.class);
        mPinStorage = new PinStorage(mContext);
        mPinStorage.mShortTermSecretKeyDurationMinutes = 0;
        verify(mCarrierConfigManager, atLeastOnce()).registerCarrierConfigChangeListener(any(),
                listenerArgumentCaptor.capture());
        mCarrierConfigChangeListener = listenerArgumentCaptor.getAllValues().get(0);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp(getClass().getSimpleName());
        mCarrierConfigChangeListener = Mockito.mock(
                CarrierConfigManager.CarrierConfigChangeListener.class);

        mBundle = mContextFixture.getCarrierConfigBundle();
        when(mCarrierConfigManager.getConfigForSubId(anyInt(), any())).thenReturn(mBundle);

        // Store boot count, so that correct value can be restored at the end.
        mBootCount = Settings.Global.getInt(
@@ -89,8 +114,7 @@ public class PinStorageTest extends TelephonyTest {
        when(mKeyguardManager.isDeviceSecure()).thenReturn(false);
        when(mKeyguardManager.isDeviceLocked()).thenReturn(false);

        mPinStorage = new PinStorage(mContext);
        mPinStorage.mShortTermSecretKeyDurationMinutes = 0;
        createPinStorageAndCaptureListener();
    }

    @After
@@ -341,7 +365,7 @@ public class PinStorageTest extends TelephonyTest {
        PersistableBundle carrierConfigs = new PersistableBundle();
        carrierConfigs.putBoolean(
                CarrierConfigManager.KEY_STORE_SIM_PIN_FOR_UNATTENDED_REBOOT_BOOL, false);
        when(mCarrierConfigManager.getConfigForSubId(anyInt())).thenReturn(carrierConfigs);
        when(mCarrierConfigManager.getConfigForSubId(anyInt(), any())).thenReturn(carrierConfigs);

        mPinStorage.storePin("1234", 0);

@@ -362,10 +386,10 @@ public class PinStorageTest extends TelephonyTest {
        PersistableBundle carrierConfigs = new PersistableBundle();
        carrierConfigs.putBoolean(
                CarrierConfigManager.KEY_STORE_SIM_PIN_FOR_UNATTENDED_REBOOT_BOOL, false);
        when(mCarrierConfigManager.getConfigForSubId(anyInt())).thenReturn(carrierConfigs);
        final Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, 0);
        mContext.sendBroadcast(intent);
        when(mCarrierConfigManager.getConfigForSubId(anyInt(), any())).thenReturn(carrierConfigs);
        mCarrierConfigChangeListener.onCarrierConfigChanged(0 /* slotIndex */,
                SubscriptionManager.INVALID_SUBSCRIPTION_ID,
                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
        processAllMessages();

        int result = mPinStorage.prepareUnattendedReboot(sWorkSource);