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

Commit fd48e3f3 authored by Anurag Awasthi's avatar Anurag Awasthi Committed by Android Build Coastguard Worker
Browse files

[Metrics] Add logging to profile connection attempts

Current logging adds support to measure for A2DP and HFP profile
attempts along with their result and reasons for profile connection
attempts.

Test: m followed by connecting to BT device supporting A2DP and HFP
and then statsd_testdrive 696

Bug: 285027829
(cherry picked from https://android-review.googlesource.com/q/commit:1d02bb3a5f095c162155425acd61812f76b66d56)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:0964e2c746232e21e66843fe7587e92921a6cc0c)
Merged-In: Ie70c5a40a00b57d8c65857151774e6346acced85
Change-Id: Ie70c5a40a00b57d8c65857151774e6346acced85
parent 2a634f7f
Loading
Loading
Loading
Loading
+39 −2
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.android.bluetooth.BluetoothStatsLog;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.ProfileService;
@@ -169,6 +170,8 @@ final class A2dpStateMachine extends StateMachine {
                                        BluetoothA2dp.STATE_PLAYING);
                }
            }

            logFailureIfNeeded();
        }

        @Override
@@ -260,6 +263,26 @@ final class A2dpStateMachine extends StateMachine {
                    break;
            }
        }

        private void logFailureIfNeeded() {
            if (mLastConnectionState == BluetoothProfile.STATE_CONNECTING
                    || mLastConnectionState == BluetoothProfile.STATE_DISCONNECTED) {
                // Result for disconnected -> disconnected is unknown as it should
                // not have occurred.
                int result =
                        (mLastConnectionState == BluetoothProfile.STATE_CONNECTING)
                                ? BluetoothProtoEnums.RESULT_FAILURE
                                : BluetoothProtoEnums.RESULT_UNKNOWN;

                BluetoothStatsLog.write(
                        BluetoothStatsLog.BLUETOOTH_PROFILE_CONNECTION_ATTEMPTED,
                        BluetoothProfile.A2DP,
                        result,
                        mLastConnectionState,
                        BluetoothProfile.STATE_DISCONNECTED,
                        BluetoothProtoEnums.REASON_UNEXPECTED_STATE);
            }
        }
    }

    @VisibleForTesting
@@ -481,10 +504,11 @@ final class A2dpStateMachine extends StateMachine {
            // it differs from what we had saved before.
            mA2dpService.updateOptionalCodecsSupport(mDevice);
            mA2dpService.updateLowLatencyAudioSupport(mDevice);

            broadcastConnectionState(mConnectionState, mLastConnectionState);
            // Upon connected, the audio starts out as stopped
            broadcastAudioState(BluetoothA2dp.STATE_NOT_PLAYING,
                                BluetoothA2dp.STATE_PLAYING);
            broadcastAudioState(BluetoothA2dp.STATE_NOT_PLAYING, BluetoothA2dp.STATE_PLAYING);
            logSuccessIfNeeded();
        }

        @Override
@@ -593,6 +617,19 @@ final class A2dpStateMachine extends StateMachine {
                    break;
            }
        }

        private void logSuccessIfNeeded() {
            if (mLastConnectionState == BluetoothProfile.STATE_CONNECTING
                    || mLastConnectionState == BluetoothProfile.STATE_DISCONNECTED) {
                BluetoothStatsLog.write(
                        BluetoothStatsLog.BLUETOOTH_PROFILE_CONNECTION_ATTEMPTED,
                        BluetoothProfile.A2DP,
                        BluetoothProtoEnums.RESULT_SUCCESS,
                        mLastConnectionState,
                        BluetoothProfile.STATE_CONNECTED,
                        BluetoothProtoEnums.REASON_SUCCESS);
            }
        }
    }

    int getConnectionState() {
+39 −1
Original line number Diff line number Diff line
@@ -483,7 +483,10 @@ public class HeadsetStateMachine extends StateMachine {
            mHasWbsEnabled = false;
            mHasSwbEnabled = false;
            mHasNrecEnabled = false;

            broadcastStateTransitions();
            logFailureIfNeeded();

            // Remove the state machine for unbonded devices
            if (mPrevState != null
                    && mAdapterService.getBondState(mDevice) == BluetoothDevice.BOND_NONE) {
@@ -505,7 +508,9 @@ public class HeadsetStateMachine extends StateMachine {
                    if (!mNativeInterface.connectHfp(device)) {
                        stateLogE("CONNECT failed for connectHfp(" + device + ")");
                        // No state transition is involved, fire broadcast immediately
                        broadcastConnectionState(device, BluetoothProfile.STATE_DISCONNECTED,
                        broadcastConnectionState(
                                device,
                                BluetoothProfile.STATE_DISCONNECTED,
                                BluetoothProfile.STATE_DISCONNECTED);
                        break;
                    }
@@ -578,6 +583,25 @@ public class HeadsetStateMachine extends StateMachine {
                    break;
            }
        }

        private void logFailureIfNeeded() {
            if (mPrevState == mConnecting || mPrevState == mDisconnected) {
                // Result for disconnected -> disconnected is unknown as it should
                // not have occurred.
                int result =
                        (mPrevState == mConnecting)
                                ? BluetoothProtoEnums.RESULT_FAILURE
                                : BluetoothProtoEnums.RESULT_UNKNOWN;

                BluetoothStatsLog.write(
                        BluetoothStatsLog.BLUETOOTH_PROFILE_CONNECTION_ATTEMPTED,
                        BluetoothProfile.A2DP,
                        result,
                        mPrevState.getConnectionStateInt(),
                        BluetoothProfile.STATE_DISCONNECTED,
                        BluetoothProtoEnums.REASON_UNEXPECTED_STATE);
            }
        }
    }

    // Per HFP 1.7.1 spec page 23/144, Pending state needs to handle
@@ -1090,7 +1114,9 @@ public class HeadsetStateMachine extends StateMachine {
                // or the retry count reached MAX_RETRY_DISCONNECT_AUDIO.
                mAudioDisconnectRetry = 0;
            }

            broadcastStateTransitions();
            logSuccessIfNeeded();
        }

        @Override
@@ -1184,6 +1210,18 @@ public class HeadsetStateMachine extends StateMachine {
                    break;
            }
        }

        private void logSuccessIfNeeded() {
            if (mPrevState == mConnecting || mPrevState == mDisconnected) {
                BluetoothStatsLog.write(
                        BluetoothStatsLog.BLUETOOTH_PROFILE_CONNECTION_ATTEMPTED,
                        BluetoothProfile.HEADSET,
                        BluetoothProtoEnums.RESULT_SUCCESS,
                        mPrevState.getConnectionStateInt(),
                        BluetoothProfile.STATE_CONNECTED,
                        BluetoothProtoEnums.REASON_SUCCESS);
            }
        }
    }

    class AudioConnecting extends ConnectedBase {