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

Commit a49c40ce authored by Hall Liu's avatar Hall Liu
Browse files

Defer sendDeviceStateChanged until SIM config loaded

When a sim is inserted, the service state will change before all the
data is fully available. To prevent the headset from seeing bad data,
wait until the sim config is fully loaded before calling
sendDeviceStateChanged.

Change-Id: I49831b8b1e494b2263cbc543c187b170faaaaca0
Fixes: 37301718
Test: manual
parent aba31bca
Loading
Loading
Loading
Loading
+38 −5
Original line number Original line Diff line number Diff line
@@ -17,7 +17,10 @@
package com.android.bluetooth.hfp;
package com.android.bluetooth.hfp;


import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.PhoneStateListener;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.telephony.SignalStrength;
@@ -26,6 +29,9 @@ import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
import android.util.Log;
import android.util.Log;


import com.android.internal.telephony.IccCardConstants;
import com.android.internal.telephony.TelephonyIntents;



// Note:
// Note:
// All methods in this class are not thread safe, donot call them from
// All methods in this class are not thread safe, donot call them from
@@ -247,15 +253,42 @@ class HeadsetPhoneState {
        PhoneStateListener mPhoneStateListener = new PhoneStateListener(subId) {
        PhoneStateListener mPhoneStateListener = new PhoneStateListener(subId) {
            @Override
            @Override
            public void onServiceStateChanged(ServiceState serviceState) {
            public void onServiceStateChanged(ServiceState serviceState) {

                mServiceState = serviceState;
                mServiceState = serviceState;
                mService = (serviceState.getState() == ServiceState.STATE_IN_SERVICE) ?
                int newService = (serviceState.getState() == ServiceState.STATE_IN_SERVICE) ?
                    HeadsetHalConstants.NETWORK_STATE_AVAILABLE :
                    HeadsetHalConstants.NETWORK_STATE_AVAILABLE :
                    HeadsetHalConstants.NETWORK_STATE_NOT_AVAILABLE;
                    HeadsetHalConstants.NETWORK_STATE_NOT_AVAILABLE;
                setRoam(serviceState.getRoaming() ? HeadsetHalConstants.SERVICE_TYPE_ROAMING
                int newRoam = serviceState.getRoaming() ? HeadsetHalConstants.SERVICE_TYPE_ROAMING
                                                  : HeadsetHalConstants.SERVICE_TYPE_HOME);
                                                  : HeadsetHalConstants.SERVICE_TYPE_HOME;


                if (newService == mService && newRoam == mRoam) {
                    // Debounce the state change
                    return;
                }
                mService = newService;
                mRoam = newRoam;

                // If this is due to a SIM insertion, we want to defer sending device state changed
                // until all the SIM config is loaded.
                if (newService == HeadsetHalConstants.NETWORK_STATE_NOT_AVAILABLE) {
                    sendDeviceStateChanged();
                    return;
                }
                IntentFilter simStateChangedFilter =
                        new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
                mContext.registerReceiver(new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
                            // This is a sticky broadcast, so if it's already been loaded,
                            // this'll execute immediately.
                            if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(
                                    intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE))) {
                                sendDeviceStateChanged();
                                sendDeviceStateChanged();
                                mContext.unregisterReceiver(this);
                            }
                        }
                    }
                }, simStateChangedFilter);
            }
            }


            @Override
            @Override