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

Commit c53f58b5 authored by William Escande's avatar William Escande
Browse files

VolumeControl: remove VDBG & DBG & log & isEnabled

Bug: 372328699
Test: m .
Flag: Exempt refactor
Change-Id: If4d64f069d83356b32bd93993e3396b7ec9c8e52
parent 97da86f8
Loading
Loading
Loading
Loading
+37 −57
Original line number Diff line number Diff line
@@ -61,8 +61,6 @@ import java.util.function.Consumer;
@SystemApi
public final class BluetoothVolumeControl implements BluetoothProfile, AutoCloseable {
    private static final String TAG = "BluetoothVolumeControl";
    private static final boolean DBG = true;
    private static final boolean VDBG = false;

    private CloseGuard mCloseGuard;

@@ -276,7 +274,7 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
     */
    @Override
    public void close() {
        if (VDBG) log("close()");
        Log.v(TAG, "close()");

        mAdapter.closeProfileProxy(this);
    }
@@ -328,12 +326,11 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public @NonNull List<BluetoothDevice> getConnectedDevices() {
        if (DBG) log("getConnectedDevices()");
        Log.d(TAG, "getConnectedDevices()");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled()) {
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
        } else if (mAdapter.isEnabled()) {
            try {
                return Attributable.setAttributionSource(
                        service.getConnectedDevices(mAttributionSource), mAttributionSource);
@@ -353,12 +350,11 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
        if (DBG) log("getDevicesMatchingStates()");
        Log.d(TAG, "getDevicesMatchingStates()");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled()) {
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
        } else if (mAdapter.isEnabled()) {
            try {
                return Attributable.setAttributionSource(
                        service.getDevicesMatchingConnectionStates(states, mAttributionSource),
@@ -379,12 +375,11 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresBluetoothConnectPermission
    @RequiresPermission(BLUETOOTH_CONNECT)
    public int getConnectionState(BluetoothDevice device) {
        if (DBG) log("getConnectionState(" + device + ")");
        Log.d(TAG, "getConnectionState(" + device + ")");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled() && isValidDevice(device)) {
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            try {
                return service.getConnectionState(device, mAttributionSource);
            } catch (RemoteException e) {
@@ -414,7 +409,7 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
            @NonNull @CallbackExecutor Executor executor, @NonNull Callback callback) {
        Objects.requireNonNull(executor, "executor cannot be null");
        Objects.requireNonNull(callback, "callback cannot be null");
        if (DBG) log("registerCallback");
        Log.d(TAG, "registerCallback");
        synchronized (mCallbackExecutorMap) {
            if (!mAdapter.isEnabled()) {
                /* If Bluetooth is off, just store callback and it will be registered
@@ -472,7 +467,7 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public void unregisterCallback(@NonNull Callback callback) {
        Objects.requireNonNull(callback, "callback cannot be null");
        if (DBG) log("unregisterCallback");
        Log.d(TAG, "unregisterCallback");
        synchronized (mCallbackExecutorMap) {
            if (mCallbackExecutorMap.remove(callback) == null) {
                throw new IllegalArgumentException("This callback has not been registered");
@@ -556,8 +551,8 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
            @NonNull BluetoothDevice device,
            @IntRange(from = 1, to = 255) int instanceId,
            @IntRange(from = -255, to = 255) int volumeOffset) {
        if (DBG) {
            log(
        Log.d(
                TAG,
                "setVolumeOffset("
                        + device
                        + "/"
@@ -565,12 +560,10 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
                        + " volumeOffset: "
                        + volumeOffset
                        + ")");
        }
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled()) {
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
        } else if (mAdapter.isEnabled()) {
            try {
                service.setVolumeOffset(device, instanceId, volumeOffset, mAttributionSource);
            } catch (RemoteException e) {
@@ -592,15 +585,14 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public boolean isVolumeOffsetAvailable(@NonNull BluetoothDevice device) {
        if (DBG) log("isVolumeOffsetAvailable(" + device + ")");
        Log.d(TAG, "isVolumeOffsetAvailable(" + device + ")");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
            return false;
        }

        if (!isEnabled()) {
        if (!mAdapter.isEnabled()) {
            return false;
        }

@@ -624,17 +616,16 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public int getNumberOfVolumeOffsetInstances(@NonNull BluetoothDevice device) {
        if (DBG) log("getNumberOfVolumeOffsetInstances(" + device + ")");
        Log.d(TAG, "getNumberOfVolumeOffsetInstances(" + device + ")");
        final IBluetoothVolumeControl service = getService();
        final int defaultValue = 0;

        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
            return defaultValue;
        }

        if (!isEnabled()) {
        if (!mAdapter.isEnabled()) {
            return defaultValue;
        }
        try {
@@ -663,12 +654,11 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public boolean setConnectionPolicy(
            @NonNull BluetoothDevice device, @ConnectionPolicy int connectionPolicy) {
        if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")");
        Log.d(TAG, "setConnectionPolicy(" + device + ", " + connectionPolicy + ")");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled()
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
        } else if (mAdapter.isEnabled()
                && isValidDevice(device)
                && (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
                        || connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED)) {
@@ -695,12 +685,11 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public @ConnectionPolicy int getConnectionPolicy(@NonNull BluetoothDevice device) {
        if (VDBG) log("getConnectionPolicy(" + device + ")");
        Log.v(TAG, "getConnectionPolicy(" + device + ")");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled() && isValidDevice(device)) {
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            try {
                return service.getConnectionPolicy(device, mAttributionSource);
            } catch (RemoteException e) {
@@ -741,9 +730,8 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
        }
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled()) {
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
        } else if (mAdapter.isEnabled()) {
            try {
                service.setDeviceVolume(device, volume, isGroupOperation, mAttributionSource);
            } catch (RemoteException e) {
@@ -752,15 +740,7 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
        }
    }

    private boolean isEnabled() {
        return mAdapter.getState() == BluetoothAdapter.STATE_ON;
    }

    private static boolean isValidDevice(@Nullable BluetoothDevice device) {
        return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
    }

    private static void log(String msg) {
        Log.d(TAG, msg);
    }
}