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

Commit d3bac830 authored by Sal Savage's avatar Sal Savage
Browse files

Refactor A2DP Sink logging to be unguarded

With the recent addition of a process minimum default log level, the
Android Log framework will now enforce the set log level against the
various log invocations we make in code. We no longer need to guard log
invocations on our own.

Tag: #refactor
Flag: EXEMPT, logging only change
Bug: 315046089
Test: atest BluetoothInstrumentationTests
Change-Id: I7a06b207b8b855a36ebcc89314f8af1378f8a468
parent 0780e65a
Loading
Loading
Loading
Loading
+4 −11
Original line number Original line Diff line number Diff line
@@ -31,8 +31,7 @@ import java.util.Objects;
 * A2DP Sink Native Interface to/from JNI.
 * A2DP Sink Native Interface to/from JNI.
 */
 */
public class A2dpSinkNativeInterface {
public class A2dpSinkNativeInterface {
    private static final String TAG = "A2dpSinkNativeInterface";
    private static final String TAG = A2dpSinkNativeInterface.class.getSimpleName();
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
    private AdapterService mAdapterService;
    private AdapterService mAdapterService;


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


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


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


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


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

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


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


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


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


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


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


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


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


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


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


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


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


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


    // Configuration Variables
    // Configuration Variables
    private static final int DEFAULT_DUCK_PERCENT = 25;
    private static final int DEFAULT_DUCK_PERCENT = 25;
@@ -93,9 +92,7 @@ public class A2dpSinkStreamHandler extends Handler {
    private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
    private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
        @Override
        @Override
        public void onAudioFocusChange(int focusChange) {
        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)
            A2dpSinkStreamHandler.this.obtainMessage(AUDIO_FOCUS_CHANGE, focusChange)
                    .sendToTarget();
                    .sendToTarget();
        }
        }
@@ -132,10 +129,7 @@ public class A2dpSinkStreamHandler extends Handler {


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


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


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


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

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


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

}
}