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

Commit 2ce04d33 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes from topic "bt-cleanup-cherry-pick"

* changes:
  Defer sending NETWORK_STATE_AVAILABLE to headset
  Defer sendDeviceStateChanged until SIM config loaded
  A2DP: Only unregister receiver if we registered
parents 690ca30b 309a4ce6
Loading
Loading
Loading
Loading
+15 −6
Original line number Diff line number Diff line
@@ -48,7 +48,9 @@ public class A2dpService extends ProfileService {
    private A2dpStateMachine mStateMachine;
    private Avrcp mAvrcp;

    private BroadcastReceiver mConnectionStateChangedReceiver = new BroadcastReceiver() {
    private BroadcastReceiver mConnectionStateChangedReceiver = null;

    private class CodecSupportReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (!BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
@@ -108,9 +110,12 @@ public class A2dpService extends ProfileService {
        mAvrcp = Avrcp.make(this);
        mStateMachine = A2dpStateMachine.make(this, this);
        setA2dpService(this);
        if (mConnectionStateChangedReceiver == null) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
            mConnectionStateChangedReceiver = new CodecSupportReceiver();
            registerReceiver(mConnectionStateChangedReceiver, filter);
        }
        return true;
    }

@@ -125,9 +130,13 @@ public class A2dpService extends ProfileService {
    }

    protected boolean cleanup() {
        if (mConnectionStateChangedReceiver != null) {
            unregisterReceiver(mConnectionStateChangedReceiver);
            mConnectionStateChangedReceiver = null;
        }
        if (mStateMachine != null) {
            mStateMachine.cleanup();
            mStateMachine = null;
        }
        if (mAvrcp != null) {
            mAvrcp.cleanup();
+49 −7
Original line number Diff line number Diff line
@@ -17,7 +17,10 @@
package com.android.bluetooth.hfp;

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

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


// Note:
// All methods in this class are not thread safe, donot call them from
@@ -41,6 +47,10 @@ class HeadsetPhoneState {
    // HFP 1.6 CIND service
    private int mService = HeadsetHalConstants.NETWORK_STATE_NOT_AVAILABLE;

    // Check this before sending out service state to the device -- if the SIM isn't fully
    // loaded, don't expose that the network is available.
    private boolean mIsSimStateLoaded = false;

    // Number of active (foreground) calls
    private int mNumActive = 0;

@@ -237,17 +247,20 @@ class HeadsetPhoneState {

    void sendDeviceStateChanged()
    {
        int service =
                mIsSimStateLoaded ? mService : HeadsetHalConstants.NETWORK_STATE_NOT_AVAILABLE;
        // When out of service, send signal strength as 0. Some devices don't
        // use the service indicator, but only the signal indicator
        int signal = mService == HeadsetHalConstants.NETWORK_STATE_AVAILABLE ? mSignal : 0;
        int signal = service == HeadsetHalConstants.NETWORK_STATE_AVAILABLE ? mSignal : 0;

        Log.d(TAG, "sendDeviceStateChanged. mService="+ mService +
                   " mIsSimStateLoaded=" + mIsSimStateLoaded +
                   " mSignal=" + signal +" mRoam="+ mRoam +
                   " mBatteryCharge=" + mBatteryCharge);
        HeadsetStateMachine sm = mStateMachine;
        if (sm != null) {
            sm.sendMessage(HeadsetStateMachine.DEVICE_STATE_CHANGED,
                new HeadsetDeviceState(mService, mRoam, signal, mBatteryCharge));
                new HeadsetDeviceState(service, mRoam, signal, mBatteryCharge));
        }
    }

@@ -255,15 +268,44 @@ class HeadsetPhoneState {
        PhoneStateListener mPhoneStateListener = new PhoneStateListener(subId) {
            @Override
            public void onServiceStateChanged(ServiceState 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_NOT_AVAILABLE;
                setRoam(serviceState.getRoaming() ? HeadsetHalConstants.SERVICE_TYPE_ROAMING
                                                  : HeadsetHalConstants.SERVICE_TYPE_HOME);
                int newRoam = serviceState.getRoaming() ? HeadsetHalConstants.SERVICE_TYPE_ROAMING
                                                  : 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) {
                    mIsSimStateLoaded = false;
                    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))) {
                                mIsSimStateLoaded = true;
                                sendDeviceStateChanged();
                                mContext.unregisterReceiver(this);
                            }
                        }
                    }
                }, simStateChangedFilter);
            }

            @Override