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

Commit d770a4bc authored by Aritra Sen's avatar Aritra Sen
Browse files

Move all the adapter properties updates from Broadcasts to direct calls via Handlers.

Retain broadcasts for metrics related logic only.

Tag: #refactor
Bug: 296932947
Test: atest BluetoothInstrumentationTests
Change-Id: If9b8ded92bfe2dfa7127793df3a1c6b749ee003b
parent 95fcc49a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -1290,6 +1290,8 @@ public class A2dpService extends ProfileService {
        mAdapterService
                .getSilenceDeviceManager()
                .a2dpConnectionStateChanged(device, fromState, toState);
        mAdapterService.updateProfileConnectionAdapterProperties(
                device, BluetoothProfile.A2DP, toState, fromState);
    }

    /**
+2 −0
Original line number Diff line number Diff line
@@ -644,5 +644,7 @@ public class A2dpSinkService extends ProfileService {
    void connectionStateChanged(BluetoothDevice device, int fromState, int toState) {
        mAdapterService.notifyProfileConnectionStateChangeToGatt(
                BluetoothProfile.A2DP_SINK, fromState, toState);
        mAdapterService.updateProfileConnectionAdapterProperties(
                device, BluetoothProfile.A2DP_SINK, toState, fromState);
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import com.android.bluetooth.BluetoothMetricsProto;
import com.android.bluetooth.R;
import com.android.bluetooth.Utils;
import com.android.bluetooth.a2dpsink.A2dpSinkService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.ProfileService;
import com.android.internal.annotations.VisibleForTesting;
@@ -1341,6 +1342,12 @@ class AvrcpControllerStateMachine extends StateMachine {
            MetricsLogger.logProfileConnectionEvent(
                    BluetoothMetricsProto.ProfileId.AVRCP_CONTROLLER);
        }
        AdapterService adapterService = AdapterService.getAdapterService();
        if (adapterService != null) {
            adapterService.updateProfileConnectionAdapterProperties(
                    mDevice, BluetoothProfile.AVRCP_CONTROLLER, currentState, mMostRecentState);
        }

        logD("Connection state " + mDevice + ": " + mMostRecentState + "->" + currentState);
        Intent intent = new Intent(BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, mMostRecentState);
+34 −24
Original line number Diff line number Diff line
@@ -49,6 +49,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.ParcelUuid;
import android.os.SystemProperties;
import android.os.UserHandle;
@@ -149,6 +151,8 @@ class AdapterProperties {
    private List<BufferConstraint> mBufferConstraintList;

    private boolean mReceiverRegistered;
    private Handler mHandler;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -159,49 +163,49 @@ class AdapterProperties {
            }
            switch (action) {
                case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.HEADSET, intent);
                    logConnectionStateChanges(BluetoothProfile.HEADSET, intent);
                    break;
                case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.A2DP, intent);
                    logConnectionStateChanges(BluetoothProfile.A2DP, intent);
                    break;
                case BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.HEADSET_CLIENT, intent);
                    logConnectionStateChanges(BluetoothProfile.HEADSET_CLIENT, intent);
                    break;
                case BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.HEARING_AID, intent);
                    logConnectionStateChanges(BluetoothProfile.HEARING_AID, intent);
                    break;
                case BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.A2DP_SINK, intent);
                    logConnectionStateChanges(BluetoothProfile.A2DP_SINK, intent);
                    break;
                case BluetoothHidDevice.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.HID_DEVICE, intent);
                    logConnectionStateChanges(BluetoothProfile.HID_DEVICE, intent);
                    break;
                case BluetoothHidHost.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.HID_HOST, intent);
                    logConnectionStateChanges(BluetoothProfile.HID_HOST, intent);
                    break;
                case BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.AVRCP_CONTROLLER, intent);
                    logConnectionStateChanges(BluetoothProfile.AVRCP_CONTROLLER, intent);
                    break;
                case BluetoothPan.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.PAN, intent);
                    logConnectionStateChanges(BluetoothProfile.PAN, intent);
                    break;
                case BluetoothMap.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.MAP, intent);
                    logConnectionStateChanges(BluetoothProfile.MAP, intent);
                    break;
                case BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.MAP_CLIENT, intent);
                    logConnectionStateChanges(BluetoothProfile.MAP_CLIENT, intent);
                    break;
                case BluetoothSap.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.SAP, intent);
                    logConnectionStateChanges(BluetoothProfile.SAP, intent);
                    break;
                case BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.PBAP_CLIENT, intent);
                    logConnectionStateChanges(BluetoothProfile.PBAP_CLIENT, intent);
                    break;
                case BluetoothPbap.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.PBAP, intent);
                    logConnectionStateChanges(BluetoothProfile.PBAP, intent);
                    break;
                case BluetoothLeAudio.ACTION_LE_AUDIO_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.LE_AUDIO, intent);
                    logConnectionStateChanges(BluetoothProfile.LE_AUDIO, intent);
                    break;
                default:
                    Log.w(TAG, "Received unknown intent " + intent);
@@ -245,6 +249,8 @@ class AdapterProperties {
                SystemProperties.getBoolean(A2DP_OFFLOAD_SUPPORTED_PROPERTY, false)
                && !SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);

        mHandler = new Handler(Looper.getMainLooper());

        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
@@ -735,7 +741,7 @@ class AdapterProperties {
    }

    @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
    private void sendConnectionStateChange(int profile, Intent connIntent) {
    private void logConnectionStateChanges(int profile, Intent connIntent) {
        BluetoothDevice device = connIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        int prevState = connIntent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
        int state = connIntent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
@@ -745,23 +751,27 @@ class AdapterProperties {
                    metricId, device.getName());
            MetricsLogger.getInstance().logSanitizedBluetoothDeviceName(metricId, device.getName());
        }
        Log.d(TAG, "PROFILE_CONNECTION_STATE_CHANGE: profile="
                + BluetoothProfile.getProfileName(profile) + ", device=" + device + ", "
                + prevState + " -> " + state);
        BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_CONNECTION_STATE_CHANGED, state,
                0 /* deprecated */, profile, mService.obfuscateAddress(device),
                metricId, 0, -1);
    }

    @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
    void updateOnProfileConnectionChanged(
            BluetoothDevice device, int profile, int state, int prevState) {
        mHandler.post(() -> sendConnectionStateChange(device, profile, state, prevState));
    }

    @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
    void sendConnectionStateChange(BluetoothDevice device, int profile, int state, int prevState) {
        Log.d(TAG, "PROFILE_CONNECTION_STATE_CHANGE: profile="
                + BluetoothProfile.getProfileName(profile) + ", device=" + device + ", "
                + prevState + " -> " + state);
        if (!isNormalStateTransition(prevState, state)) {
            Log.w(TAG, "PROFILE_CONNECTION_STATE_CHANGE: unexpected transition for profile="
                    + BluetoothProfile.getProfileName(profile)
                    + ", device=" + device + ", " + prevState + " -> " + state);
        }
        sendConnectionStateChange(device, profile, state, prevState);
    }

    @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
    void sendConnectionStateChange(BluetoothDevice device, int profile, int state, int prevState) {
        if (!validateProfileConnectionState(state) || !validateProfileConnectionState(prevState)) {
            // Previously, an invalid state was broadcast anyway,
            // with the invalid state converted to -1 in the intent.
+6 −0
Original line number Diff line number Diff line
@@ -2043,6 +2043,12 @@ public class AdapterService extends Service {
        return mDatabaseManager.getCustomMeta(device, key);
    }

    /** Update Adapter Properties when BT profiles connection state changes. */
    public void updateProfileConnectionAdapterProperties(
            BluetoothDevice device, int profile, int state, int prevState) {
        mAdapterProperties.updateOnProfileConnectionChanged(device, profile, state, prevState);
    }

    /** Handlers for incoming service calls */
    private AdapterServiceBinder mBinder;

Loading