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

Commit 8955f098 authored by rambowang's avatar rambowang
Browse files

Update EmergencyNumberTracker with new CarrierConfigManager APIs

- Replace carrier config change broadcast receiver with listener
- Retrieve subset of carrier config as needed to save memory

Bug: 263267340
Test: atest EmergencyNumberTrackerTest
Change-Id: I7241243c892e5247ed336469bcdae158e337d1a2
parent 8e30609a
Loading
Loading
Loading
Loading
+39 −27
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.annotation.NonNull;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.AsyncResult;
import android.os.Environment;
@@ -161,10 +160,6 @@ public class EmergencyNumberTracker extends Handler {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(
                    CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)) {
                onCarrierConfigChanged();
                return;
            } else if (intent.getAction().equals(
                    TelephonyManager.ACTION_NETWORK_COUNTRY_CHANGED)) {
                int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, -1);
                if (phoneId == mPhone.getPhoneId()) {
@@ -191,23 +186,21 @@ public class EmergencyNumberTracker extends Handler {
            CarrierConfigManager configMgr = (CarrierConfigManager)
                    mPhone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
            if (configMgr != null) {
                PersistableBundle b = configMgr.getConfigForSubId(mPhone.getSubId());
                if (b != null) {
                PersistableBundle b = getCarrierConfigSubset(
                        CarrierConfigManager.KEY_EMERGENCY_NUMBER_PREFIX_STRING_ARRAY);
                if (!b.isEmpty()) {
                    mEmergencyNumberPrefix = b.getStringArray(
                            CarrierConfigManager.KEY_EMERGENCY_NUMBER_PREFIX_STRING_ARRAY);
                }

                // Callback which directly handle config change should be executed on handler thread
                configMgr.registerCarrierConfigChangeListener(this::post,
                        (slotIndex, subId, carrierId, specificCarrierId) ->
                                onCarrierConfigUpdated(slotIndex));
            } else {
                loge("CarrierConfigManager is null.");
            }

            // Receive Carrier Config Changes
            IntentFilter filter = new IntentFilter(
                    CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
            // Receive Telephony Network Country Changes
            filter.addAction(TelephonyManager.ACTION_NETWORK_COUNTRY_CHANGED);

            mPhone.getContext().registerReceiver(mIntentReceiver, filter);

            mIsHalVersionLessThan1Dot4 = mPhone.getHalVersion(HAL_SERVICE_VOICE)
                                                 .lessOrEqual(new HalVersion(1, 3));
        } else {
@@ -357,24 +350,43 @@ public class EmergencyNumberTracker extends Handler {
        }
    }

    private void onCarrierConfigChanged() {
    private void onCarrierConfigUpdated(int slotIndex) {
        if (mPhone != null) {
            CarrierConfigManager configMgr = (CarrierConfigManager)
                    mPhone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
            if (configMgr != null) {
                PersistableBundle b = configMgr.getConfigForSubId(mPhone.getSubId());
                if (b != null) {
                    String[] emergencyNumberPrefix = b.getStringArray(
            if (slotIndex != mPhone.getPhoneId()) return;

            PersistableBundle b =
                    getCarrierConfigSubset(
                            CarrierConfigManager.KEY_EMERGENCY_NUMBER_PREFIX_STRING_ARRAY);
            if (!b.isEmpty()) {
                String[] emergencyNumberPrefix =
                        b.getStringArray(
                                CarrierConfigManager.KEY_EMERGENCY_NUMBER_PREFIX_STRING_ARRAY);
                if (!Arrays.equals(mEmergencyNumberPrefix, emergencyNumberPrefix)) {
                        this.obtainMessage(EVENT_UPDATE_EMERGENCY_NUMBER_PREFIX,
                                emergencyNumberPrefix).sendToTarget();
                    }
                    this.obtainMessage(EVENT_UPDATE_EMERGENCY_NUMBER_PREFIX, emergencyNumberPrefix)
                            .sendToTarget();
                }
            }
        } else {
            loge("onCarrierConfigChanged mPhone is null.");
            loge("onCarrierConfigurationChanged mPhone is null.");
        }
    }

    @NonNull
    private PersistableBundle getCarrierConfigSubset(String key) {
        PersistableBundle bundle = new PersistableBundle();

        if (mPhone != null) {
            CarrierConfigManager ccm =
                    mPhone.getContext().getSystemService(CarrierConfigManager.class);
            try {
                if (ccm != null) {
                    bundle = ccm.getConfigForSubId(mPhone.getPhoneId(), key);
                }
            } catch (RuntimeException e) {
                loge("CarrierConfigLoader is not available.");
            }
        }
        return bundle;
    }

    private String getInitialCountryIso() {