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

Commit 9f117687 authored by Jack He's avatar Jack He
Browse files

AdapterProperties: Logging and logic refactor

* Add more logging in AdapterProperties to track profile and adapter
  connection state changes
* Removed unnecessary actions in AdapterProperties intent filter
* Use switch instead of if-else-if statements in AdapterProperties
  broadcast receiver

Bug: 65591907
Test: make, pair with Bluetooth devices
Change-Id: I105e718d9c5c698bddd6a5d1ce60b2826e6e5f85
parent 0993afe1
Loading
Loading
Loading
Loading
+102 −66
Original line number Diff line number Diff line
@@ -97,32 +97,51 @@ class AdapterProperties {
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "Received intent " + intent);
            String action = intent.getAction();
            if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
            if (action == null) {
                Log.w(TAG, "Received intent with null action");
                return;
            }
            switch (action) {
                case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.HEADSET, intent);
            } else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.A2DP, intent);
            } else if (BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.HEADSET_CLIENT, intent);
            } else if (BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.A2DP_SINK, intent);
            } else if (BluetoothInputHost.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothInputHost.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.INPUT_HOST, intent);
            } else if (BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.INPUT_DEVICE, intent);
            } else if (BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.AVRCP_CONTROLLER, intent);
            } else if (BluetoothPan.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothPan.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.PAN, intent);
            } else if (BluetoothMap.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothMap.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.MAP, intent);
            } else if (BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.MAP_CLIENT, intent);
            } else if (BluetoothSap.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothSap.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.SAP, intent);
            } else if (BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    break;
                case BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED:
                    sendConnectionStateChange(BluetoothProfile.PBAP_CLIENT, intent);
                    break;
                default:
                    Log.w(TAG, "Received unknown intent " + intent);
                    break;
            }
        }
    };
@@ -130,7 +149,7 @@ class AdapterProperties {
    // Lock for all getters and setters.
    // If finer grained locking is needer, more locks
    // can be added here.
    private Object mObject = new Object();
    private final Object mObject = new Object();

    public AdapterProperties(AdapterService service) {
        mService = service;
@@ -157,7 +176,6 @@ class AdapterProperties {
        filter.addAction(BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothSap.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_UUID);
        mService.registerReceiver(mReceiver, filter);
        mReceiverRegistered = true;
    }
@@ -232,18 +250,6 @@ class AdapterProperties {
        return mUuids;
    }

    /**
     * Set local adapter UUIDs.
     *
     * @param uuids the uuids to be set.
     */
    boolean setUuids(ParcelUuid[] uuids) {
        synchronized (mObject) {
            return mService.setAdapterPropertyNative(
                    AbstractionLayer.BT_PROPERTY_UUIDS, Utils.uuidsToByteArray(uuids));
        }
    }

    /**
     * @return the mAddress
     */
@@ -382,8 +388,10 @@ class AdapterProperties {
    // state changes.
    void onBondStateChanged(BluetoothDevice device, int state)
    {
        if(device == null)
        if (device == null) {
            Log.w(TAG, "onBondStateChanged, device is null");
            return;
        }
        try {
            byte[] addrByte = Utils.getByteAddress(device);
            DeviceProperties prop = mRemoteDevices.getDeviceProperties(device);
@@ -406,7 +414,7 @@ class AdapterProperties {
            }
        }
        catch(Exception ee) {
            Log.e(TAG, "Exception in onBondStateChanged : ", ee);
            Log.w(TAG, "onBondStateChanged: Exception ", ee);
        }
    }

@@ -442,6 +450,14 @@ class AdapterProperties {
        BluetoothDevice device = connIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        int prevState = connIntent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
        int state = connIntent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
        Log.d(TAG,
                "PROFILE_CONNECTION_STATE_CHANGE: profile=" + profile + ", device=" + device + ", "
                        + prevState + " -> " + state);
        if (!isNormalStateTransition(prevState, state)) {
            Log.e(TAG,
                    "PROFILE_CONNECTION_STATE_CHANGE: invalid transition for profile=" + profile
                            + ", device=" + device + ", " + prevState + " -> " + state);
        }
        sendConnectionStateChange(device, profile, state, prevState);
    }
    void sendConnectionStateChange(BluetoothDevice device, int profile, int state, int prevState) {
@@ -451,8 +467,8 @@ class AdapterProperties {
            // with the invalid state converted to -1 in the intent.
            // Better to log an error and not send an intent with
            // invalid contents or set mAdapterConnectionState to -1.
            errorLog("Error in sendConnectionStateChange: "
                    + "prevState " + prevState + " state " + state);
            errorLog("sendConnectionStateChange: invalid state transition " + prevState + " -> "
                    + state);
            return;
        }

@@ -460,19 +476,25 @@ class AdapterProperties {
            updateProfileConnectionState(profile, state, prevState);

            if (updateCountersAndCheckForConnectionStateChange(state, prevState)) {
                setConnectionState(state);
                int newAdapterState = convertToAdapterState(state);
                int prevAdapterState = convertToAdapterState(prevState);
                setConnectionState(newAdapterState);

                Intent intent = new Intent(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
                intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
                intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
                        convertToAdapterState(state));
                intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE,
                        convertToAdapterState(prevState));
                intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, newAdapterState);
                intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, prevAdapterState);
                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
                mService.sendBroadcastAsUser(intent, UserHandle.ALL,
                        mService.BLUETOOTH_PERM);
                Log.d(TAG, "CONNECTION_STATE_CHANGE: " + device + ": "
                        + prevState + " -> " + state);
                Log.d(TAG,
                        "ADAPTER_CONNECTION_STATE_CHANGE: " + device + ": " + prevAdapterState
                                + " -> " + newAdapterState);
                if (!isNormalStateTransition(prevState, state)) {
                    Log.e(TAG,
                            "ADAPTER_CONNECTION_STATE_CHANGE: invalid transition for profile="
                                    + profile + ", device=" + device + ", " + prevState + " -> "
                                    + state);
                }
                mService.sendBroadcastAsUser(intent, UserHandle.ALL, AdapterService.BLUETOOTH_PERM);
            }
        }
    }
@@ -484,8 +506,7 @@ class AdapterProperties {
                state == BluetoothProfile.STATE_DISCONNECTING);
    }


    private int convertToAdapterState(int state) {
    private static int convertToAdapterState(int state) {
        switch (state) {
            case BluetoothProfile.STATE_DISCONNECTED:
                return BluetoothAdapter.STATE_DISCONNECTED;
@@ -496,10 +517,25 @@ class AdapterProperties {
            case BluetoothProfile.STATE_CONNECTING:
                return BluetoothAdapter.STATE_CONNECTING;
        }
        Log.e(TAG, "Error in convertToAdapterState");
        Log.e(TAG, "convertToAdapterState, unknow state " + state);
        return -1;
    }

    private static boolean isNormalStateTransition(int prevState, int nextState) {
        switch (prevState) {
            case BluetoothProfile.STATE_DISCONNECTED:
                return nextState == BluetoothProfile.STATE_CONNECTING;
            case BluetoothProfile.STATE_CONNECTED:
                return nextState == BluetoothProfile.STATE_DISCONNECTING;
            case BluetoothProfile.STATE_DISCONNECTING:
            case BluetoothProfile.STATE_CONNECTING:
                return (nextState == BluetoothProfile.STATE_DISCONNECTED)
                        || (nextState == BluetoothProfile.STATE_CONNECTED);
            default:
                return false;
        }
    }

    private boolean updateCountersAndCheckForConnectionStateChange(int state, int prevState) {
        switch (prevState) {
            case BluetoothProfile.STATE_CONNECTING:
@@ -611,8 +647,8 @@ class AdapterProperties {
                        intent = new Intent(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
                        intent.putExtra(BluetoothAdapter.EXTRA_LOCAL_NAME, mName);
                        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
                        mService.sendBroadcastAsUser(intent, UserHandle.ALL,
                                 mService.BLUETOOTH_PERM);
                        mService.sendBroadcastAsUser(
                                intent, UserHandle.ALL, AdapterService.BLUETOOTH_PERM);
                        debugLog("Name is: " + mName);
                        break;
                    case AbstractionLayer.BT_PROPERTY_BDADDR:
@@ -623,7 +659,7 @@ class AdapterProperties {
                        intent.putExtra(BluetoothAdapter.EXTRA_BLUETOOTH_ADDRESS, address);
                        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
                        mService.sendBroadcastAsUser(
                                intent, UserHandle.ALL, mService.BLUETOOTH_PERM);
                                intent, UserHandle.ALL, AdapterService.BLUETOOTH_PERM);
                        break;
                    case AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE:
                        mBluetoothClass = Utils.byteArrayToInt(val, 0);
@@ -631,11 +667,11 @@ class AdapterProperties {
                        break;
                    case AbstractionLayer.BT_PROPERTY_ADAPTER_SCAN_MODE:
                        int mode = Utils.byteArrayToInt(val, 0);
                        mScanMode = mService.convertScanModeFromHal(mode);
                        mScanMode = AdapterService.convertScanModeFromHal(mode);
                        intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
                        intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, mScanMode);
                        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
                        mService.sendBroadcast(intent, mService.BLUETOOTH_PERM);
                        mService.sendBroadcast(intent, AdapterService.BLUETOOTH_PERM);
                        debugLog("Scan Mode:" + mScanMode);
                        if (mBluetoothDisabling) {
                            mBluetoothDisabling=false;
@@ -671,7 +707,7 @@ class AdapterProperties {
        }
    }

    void updateFeatureSupport(byte[] val) {
    private void updateFeatureSupport(byte[] val) {
        mVersSupported = ((0xFF & ((int)val[1])) << 8)
                            + (0xFF & ((int)val[0]));
        mNumOfAdvertisementInstancesSupported = (0xFF & ((int)val[3]));
@@ -783,25 +819,25 @@ class AdapterProperties {
                mDiscovering = false;
                mDiscoveryEndMs = System.currentTimeMillis();
                intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
                mService.sendBroadcast(intent, mService.BLUETOOTH_PERM);
                mService.sendBroadcast(intent, AdapterService.BLUETOOTH_PERM);
            } else if (state == AbstractionLayer.BT_DISCOVERY_STARTED) {
                mDiscovering = true;
                mDiscoveryEndMs = System.currentTimeMillis() + DEFAULT_DISCOVERY_TIMEOUT_MS;
                intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
                mService.sendBroadcast(intent, mService.BLUETOOTH_PERM);
                mService.sendBroadcast(intent, AdapterService.BLUETOOTH_PERM);
            }
        }
    }

    private void infoLog(String msg) {
    private static void infoLog(String msg) {
        if (VDBG) Log.i(TAG, msg);
    }

    private void debugLog(String msg) {
    private static void debugLog(String msg) {
        if (DBG) Log.d(TAG, msg);
    }

    private void errorLog(String msg) {
    private static void errorLog(String msg) {
        Log.e(TAG, msg);
    }
}