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

Commit d6fb7f95 authored by Sal Savage's avatar Sal Savage Committed by Gerrit Code Review
Browse files

Merge "Refactor A2DP Sink logging to be unguarded" into main

parents c0683223 d3bac830
Loading
Loading
Loading
Loading
+4 −11
Original line number Diff line number Diff line
@@ -31,8 +31,7 @@ import java.util.Objects;
 * A2DP Sink Native Interface to/from JNI.
 */
public class A2dpSinkNativeInterface {
    private static final String TAG = "A2dpSinkNativeInterface";
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
    private static final String TAG = A2dpSinkNativeInterface.class.getSimpleName();
    private AdapterService mAdapterService;

    @GuardedBy("INSTANCE_LOCK")
@@ -171,9 +170,7 @@ public class A2dpSinkNativeInterface {
    public void onConnectionStateChanged(byte[] address, int state) {
        StackEvent event =
                StackEvent.connectionStateChanged(getDevice(address), state);
        if (DBG) {
        Log.d(TAG, "onConnectionStateChanged: " + event);
        }
        sendMessageToService(event);
    }

@@ -182,9 +179,7 @@ public class A2dpSinkNativeInterface {
     */
    public void onAudioStateChanged(byte[] address, int state) {
        StackEvent event = StackEvent.audioStateChanged(getDevice(address), state);
        if (DBG) {
        Log.d(TAG, "onAudioStateChanged: " + event);
        }
        sendMessageToService(event);
    }

@@ -194,9 +189,7 @@ public class A2dpSinkNativeInterface {
    public void onAudioConfigChanged(byte[] address, int sampleRate, int channelCount) {
        StackEvent event = StackEvent.audioConfigChanged(
                getDevice(address), sampleRate, channelCount);
        if (DBG) {
        Log.d(TAG, "onAudioConfigChanged: " + event);
        }
        sendMessageToService(event);
    }

+7 −21
Original line number Diff line number Diff line
@@ -49,7 +49,6 @@ import java.util.concurrent.ConcurrentHashMap;
 */
public class A2dpSinkService extends ProfileService {
    private static final String TAG = A2dpSinkService.class.getSimpleName();
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);

    private final Map<BluetoothDevice, A2dpSinkStateMachine> mDeviceStateMap =
            new ConcurrentHashMap<>(1);
@@ -373,17 +372,12 @@ public class A2dpSinkService extends ProfileService {
     */
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    public boolean connect(BluetoothDevice device) {
        Log.d(TAG, "connect device=" + device);
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED,
                "Need BLUETOOTH_PRIVILEGED permission");
        if (device == null) {
            throw new IllegalArgumentException("Null device");
        }
        if (DBG) {
            StringBuilder sb = new StringBuilder();
            dump(sb);
            Log.d(TAG, " connect device: " + device
                    + ", InstanceMap start state: " + sb.toString());
        }
        if (getConnectionPolicy(device) == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
            Log.w(TAG, "Connection not allowed: <" + device
                    + "> is CONNECTION_POLICY_FORBIDDEN");
@@ -408,13 +402,7 @@ public class A2dpSinkService extends ProfileService {
     * @return true if disconnect is successful, false otherwise.
     */
    public boolean disconnect(BluetoothDevice device) {
        if (DBG) {
            StringBuilder sb = new StringBuilder();
            dump(sb);
            Log.d(TAG, "A2DP disconnect device: " + device
                    + ", InstanceMap start state: " + sb.toString());
        }

        Log.d(TAG, "disconnect device=" + device);
        if (device == null) {
            throw new IllegalArgumentException("Null device");
        }
@@ -475,21 +463,21 @@ public class A2dpSinkService extends ProfileService {
    }

    List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
        if (DBG) Log.d(TAG, "getDevicesMatchingConnectionStates" + Arrays.toString(states));
        Log.d(TAG, "getDevicesMatchingConnectionStates(states=" + Arrays.toString(states) + ")");
        List<BluetoothDevice> deviceList = new ArrayList<>();
        BluetoothDevice[] bondedDevices = mAdapterService.getBondedDevices();
        int connectionState;
        for (BluetoothDevice device : bondedDevices) {
            connectionState = getConnectionState(device);
            if (DBG) Log.d(TAG, "Device: " + device + "State: " + connectionState);
            Log.d(TAG, "Device: " + device + "State: " + connectionState);
            for (int i = 0; i < states.length; i++) {
                if (connectionState == states[i]) {
                    deviceList.add(device);
                }
            }
        }
        if (DBG) Log.d(TAG, deviceList.toString());
        Log.d(TAG, "GetDevicesDone");
        Log.d(TAG, "getDevicesMatchingConnectionStates(" + Arrays.toString(states) + "): Found "
                + deviceList.toString());
        return deviceList;
    }

@@ -528,9 +516,7 @@ public class A2dpSinkService extends ProfileService {
    public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) {
        enforceCallingOrSelfPermission(
                BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        if (DBG) {
        Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }

        if (!mDatabaseManager.setProfileConnectionPolicy(device, BluetoothProfile.A2DP_SINK,
                  connectionPolicy)) {
+13 −15
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import com.android.internal.util.StateMachine;

class A2dpSinkStateMachine extends StateMachine {
    private static final String TAG = A2dpSinkStateMachine.class.getSimpleName();
    static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);

    // 0->99 Events from Outside
    @VisibleForTesting static final int CONNECT = 1;
@@ -75,7 +74,6 @@ class A2dpSinkStateMachine extends StateMachine {
        mDeviceAddress = Utils.getByteAddress(mDevice);
        mService = service;
        mNativeInterface = nativeInterface;
        if (DBG) Log.d(TAG, device.toString());

        mDisconnected = new Disconnected();
        mConnecting = new Connecting();
@@ -88,6 +86,7 @@ class A2dpSinkStateMachine extends StateMachine {
        addState(mDisconnecting);

        setInitialState(mDisconnected);
        Log.d(TAG, "[" + mDevice + "] State machine created");
    }

    /**
@@ -141,13 +140,14 @@ class A2dpSinkStateMachine extends StateMachine {

    @Override
    protected void unhandledMessage(Message msg) {
        Log.w(TAG, "unhandledMessage in state " + getCurrentState() + "msg.what=" + msg.what);
        Log.w(TAG, "[" + mDevice + "] unhandledMessage state=" + getCurrentState() + ", msg.what="
                + msg.what);
    }

    class Disconnected extends State {
        @Override
        public void enter() {
            if (DBG) Log.d(TAG, "Enter Disconnected");
            Log.d(TAG, "[" + mDevice + "] Enter Disconnected");
            if (mMostRecentState != BluetoothProfile.STATE_DISCONNECTED) {
                sendMessage(CLEANUP);
            }
@@ -161,7 +161,7 @@ class A2dpSinkStateMachine extends StateMachine {
                    processStackEvent((StackEvent) message.obj);
                    return true;
                case CONNECT:
                    if (DBG) Log.d(TAG, "Connect");
                    Log.d(TAG, "[" + mDevice + "] Connect");
                    transitionTo(mConnecting);
                    return true;
                case CLEANUP:
@@ -179,8 +179,8 @@ class A2dpSinkStateMachine extends StateMachine {
                        case StackEvent.CONNECTION_STATE_CONNECTING:
                            if (mService.getConnectionPolicy(mDevice)
                                    == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
                                Log.w(TAG, "Ignore incoming connection, profile is"
                                        + " turned off for " + mDevice);
                                Log.w(TAG, "[" + mDevice + "] Ignore incoming connection, profile"
                                        + " is turned off");
                                mNativeInterface.disconnectA2dpSink(mDevice);
                            } else {
                                mConnecting.mIncomingConnection = true;
@@ -203,7 +203,7 @@ class A2dpSinkStateMachine extends StateMachine {

        @Override
        public void enter() {
            if (DBG) Log.d(TAG, "Enter Connecting");
            Log.d(TAG, "[" + mDevice + "] Enter Connecting");
            onConnectionStateChanged(BluetoothProfile.STATE_CONNECTING);
            sendMessageDelayed(CONNECT_TIMEOUT, CONNECT_TIMEOUT_MS);

@@ -224,7 +224,8 @@ class A2dpSinkStateMachine extends StateMachine {
                    transitionTo(mDisconnected);
                    return true;
                case DISCONNECT:
                    Log.d(TAG, "Received disconnect message while connecting. deferred");
                    Log.d(TAG, "[" + mDevice + "] Received disconnect message while connecting."
                            + "deferred");
                    deferMessage(message);
                    return true;
            }
@@ -255,7 +256,7 @@ class A2dpSinkStateMachine extends StateMachine {
    class Connected extends State {
        @Override
        public void enter() {
            if (DBG) Log.d(TAG, "Enter Connected");
            Log.d(TAG, "[" + mDevice + "] Enter Connected");
            onConnectionStateChanged(BluetoothProfile.STATE_CONNECTED);
        }

@@ -296,7 +297,7 @@ class A2dpSinkStateMachine extends StateMachine {
    protected class Disconnecting extends State {
        @Override
        public void enter() {
            if (DBG) Log.d(TAG, "Enter Disconnecting");
            Log.d(TAG, "[" + mDevice + "] Enter Disconnecting");
            onConnectionStateChanged(BluetoothProfile.STATE_DISCONNECTING);
            transitionTo(mDisconnected);
        }
@@ -309,10 +310,7 @@ class A2dpSinkStateMachine extends StateMachine {
        if (currentState == BluetoothProfile.STATE_CONNECTED) {
            MetricsLogger.logProfileConnectionEvent(BluetoothMetricsProto.ProfileId.A2DP_SINK);
        }
        if (DBG) {
            Log.d(TAG, "Connection state " + mDevice + ": " + mMostRecentState + "->"
                    + currentState);
        }
        Log.d(TAG, "[" + mDevice + "] Connection state: " + mMostRecentState + "->" + currentState);
        Intent intent = new Intent(BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, mMostRecentState);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, currentState);
+11 −23
Original line number Diff line number Diff line
@@ -48,8 +48,7 @@ import com.android.bluetooth.avrcpcontroller.AvrcpControllerService;
 * restored.
 */
public class A2dpSinkStreamHandler extends Handler {
    private static final String TAG = "A2dpSinkStreamHandler";
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
    private static final String TAG = A2dpSinkStreamHandler.class.getSimpleName();

    // Configuration Variables
    private static final int DEFAULT_DUCK_PERCENT = 25;
@@ -93,9 +92,7 @@ public class A2dpSinkStreamHandler extends Handler {
    private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            if (DBG) {
                Log.d(TAG, "onAudioFocusChangeListener focuschange " + focusChange);
            }
            Log.d(TAG, "onAudioFocusChangeListener(focusChange= " + focusChange + ")");
            A2dpSinkStreamHandler.this.obtainMessage(AUDIO_FOCUS_CHANGE, focusChange)
                    .sendToTarget();
        }
@@ -132,10 +129,7 @@ public class A2dpSinkStreamHandler extends Handler {

    @Override
    public void handleMessage(Message message) {
        if (DBG) {
            Log.d(TAG, " process message: " + message.what);
            Log.d(TAG, " current audioFocus state =  " + mAudioFocus);
        }
        Log.d(TAG, "process message: " + message.what + ", audioFocus=" + mAudioFocus);
        switch (message.what) {
            case SRC_STR_START:
                mStreamAvailable = true;
@@ -183,10 +177,8 @@ public class A2dpSinkStreamHandler extends Handler {

            case AUDIO_FOCUS_CHANGE:
                final int focusChangeCode = (int) message.obj;
                if (DBG) {
                Log.d(TAG, "New audioFocus =  " + focusChangeCode
                        + " Previous audio focus = " + mAudioFocus);
                }
                mAudioFocus = focusChangeCode;
                // message.obj is the newly granted audio focus.
                switch (mAudioFocus) {
@@ -204,9 +196,7 @@ public class A2dpSinkStreamHandler extends Handler {
                            duckPercent = DEFAULT_DUCK_PERCENT;
                        }
                        float duckRatio = (duckPercent / 100.0f);
                        if (DBG) {
                        Log.d(TAG, "Setting reduce gain on transient loss gain=" + duckRatio);
                        }
                        setFluorideAudioTrackGain(duckRatio);
                        break;

@@ -240,14 +230,14 @@ public class A2dpSinkStreamHandler extends Handler {
     * Utility functions.
     */
    private void requestAudioFocusIfNone() {
        if (DBG) Log.d(TAG, "requestAudioFocusIfNone()");
        Log.d(TAG, "requestAudioFocusIfNone()");
        if (mAudioFocus != AudioManager.AUDIOFOCUS_GAIN) {
            requestAudioFocus();
        }
    }

    private synchronized int requestAudioFocus() {
        if (DBG) Log.d(TAG, "requestAudioFocus()");
        Log.d(TAG, "requestAudioFocus()");
        // Bluetooth A2DP may carry Music, Audio Books, Navigation, or other sounds so mark content
        // type unknown.
        AudioAttributes streamAttributes =
@@ -284,8 +274,7 @@ public class A2dpSinkStreamHandler extends Handler {
     * chosen to use it.
     */
    private synchronized void requestMediaKeyFocus() {
        if (DBG) Log.d(TAG, "requestMediaKeyFocus()");

        Log.d(TAG, "requestMediaKeyFocus()");
        if (mMediaPlayer == null) {
            AudioAttributes attrs = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_MEDIA)
@@ -310,7 +299,7 @@ public class A2dpSinkStreamHandler extends Handler {
    }

    private synchronized void abandonAudioFocus() {
        if (DBG) Log.d(TAG, "abandonAudioFocus()");
        Log.d(TAG, "abandonAudioFocus()");
        stopFluorideStreaming();
        mAudioManager.abandonAudioFocus(mAudioFocusListener);
        mAudioFocus = AudioManager.AUDIOFOCUS_NONE;
@@ -321,7 +310,7 @@ public class A2dpSinkStreamHandler extends Handler {
     * we're no longer playing audio.
     */
    private synchronized void releaseMediaKeyFocus() {
        if (DBG) Log.d(TAG, "releaseMediaKeyFocus()");
        Log.d(TAG, "releaseMediaKeyFocus()");
        if (mMediaPlayer == null) {
            return;
        }
@@ -359,5 +348,4 @@ public class A2dpSinkStreamHandler extends Handler {
        return mA2dpSinkService.getResources()
                .getBoolean(R.bool.a2dp_sink_automatically_request_audio_focus);
    }

}