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

Commit 93d2b542 authored by Rambo Wang's avatar Rambo Wang Committed by Android (Google) Code Review
Browse files

Merge "Update PinStorage with new CarrierConfigManager APIs"

parents 142a8b25 24f60d18
Loading
Loading
Loading
Loading
+17 −16
Original line number Diff line number Diff line
@@ -132,7 +132,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;
@@ -156,14 +155,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(
@@ -188,11 +184,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
@@ -560,7 +561,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);
@@ -590,9 +591,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;
@@ -995,12 +993,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);