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

Commit 20decc93 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Framework VCP: use wrapper for readability" into main

parents cb90118c d55576b2
Loading
Loading
Loading
Loading
+80 −121
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.bluetooth;

import static android.Manifest.permission.BLUETOOTH_CONNECT;
import static android.Manifest.permission.BLUETOOTH_PRIVILEGED;
import static android.bluetooth.BluetoothUtils.callServiceIfEnabled;
import static android.bluetooth.BluetoothUtils.executeFromBinder;

import static java.util.Objects.requireNonNull;
@@ -43,6 +44,7 @@ import android.util.Log;

import com.android.internal.annotations.GuardedBy;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -327,18 +329,15 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public @NonNull List<BluetoothDevice> getConnectedDevices() {
        Log.d(TAG, "getConnectedDevices()");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
        } else if (mAdapter.isEnabled()) {
            try {
                return Attributable.setAttributionSource(
                        service.getConnectedDevices(mAttributionSource), mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        return Collections.emptyList();
        List<BluetoothDevice> defaultValue = Collections.emptyList();

        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s ->
                        Attributable.setAttributionSource(
                                s.getConnectedDevices(mAttributionSource), mAttributionSource),
                defaultValue);
    }

    /**
@@ -351,20 +350,17 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
        Log.d(TAG, "getDevicesMatchingStates()");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
        } else if (mAdapter.isEnabled()) {
            try {
                return Attributable.setAttributionSource(
                        service.getDevicesMatchingConnectionStates(states, mAttributionSource),
                        mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        return Collections.emptyList();
        Log.d(TAG, "getDevicesMatchingStates(" + Arrays.toString(states) + ")");
        List<BluetoothDevice> defaultValue = Collections.emptyList();

        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s ->
                        Attributable.setAttributionSource(
                                s.getDevicesMatchingConnectionStates(states, mAttributionSource),
                                mAttributionSource),
                defaultValue);
    }

    /**
@@ -378,17 +374,16 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresPermission(BLUETOOTH_CONNECT)
    public int getConnectionState(BluetoothDevice device) {
        Log.d(TAG, "getConnectionState(" + device + ")");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            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) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        int defaultValue = BluetoothProfile.STATE_DISCONNECTED;

        if (!isValidDevice(device)) {
            return defaultValue;
        }
        return BluetoothProfile.STATE_DISCONNECTED;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.getConnectionState(device, mAttributionSource),
                defaultValue);
    }

    /**
@@ -553,26 +548,16 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
            @NonNull BluetoothDevice device,
            @IntRange(from = 1, to = 255) int instanceId,
            @IntRange(from = -255, to = 255) int volumeOffset) {
        Log.d(TAG, "setVolumeOffset(" + device + ", " + instanceId + ", " + volumeOffset + ")");

        requireNonNull(device);
        Log.d(
                TAG,
                "setVolumeOffset("
                        + device
                        + "/"
                        + instanceId
                        + " volumeOffset: "
                        + volumeOffset
                        + ")");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            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) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        if (!isValidDevice(device)) {
            return;
        }
        callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.setVolumeOffset(device, instanceId, volumeOffset, mAttributionSource));
    }

    /**
@@ -588,25 +573,18 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public boolean isVolumeOffsetAvailable(@NonNull BluetoothDevice device) {
        requireNonNull(device);
        Log.d(TAG, "isVolumeOffsetAvailable(" + device + ")");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            Log.d(TAG, "Proxy not attached to service" + Log.getStackTraceString(new Throwable()));
            return false;
        }
        final boolean defaultValue = false;

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

        try {
            return service.isVolumeOffsetAvailable(device, mAttributionSource);
        } catch (RemoteException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        requireNonNull(device);
        if (!isValidDevice(device)) {
            return defaultValue;
        }

        return false;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.isVolumeOffsetAvailable(device, mAttributionSource),
                defaultValue);
    }

    /**
@@ -620,26 +598,18 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public int getNumberOfVolumeOffsetInstances(@NonNull BluetoothDevice device) {
        requireNonNull(device);
        Log.d(TAG, "getNumberOfVolumeOffsetInstances(" + device + ")");
        final IBluetoothVolumeControl service = getService();
        final int defaultValue = 0;

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

        if (!mAdapter.isEnabled()) {
        requireNonNull(device);
        if (!isValidDevice(device)) {
            return defaultValue;
        }
        try {
            return service.getNumberOfVolumeOffsetInstances(device, mAttributionSource);
        } catch (RemoteException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        }

        return defaultValue;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.getNumberOfVolumeOffsetInstances(device, mAttributionSource),
                defaultValue);
    }

    /**
@@ -660,28 +630,22 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    public boolean setConnectionPolicy(
            @NonNull BluetoothDevice device, @ConnectionPolicy int connectionPolicy) {
        Log.d(TAG, "setConnectionPolicy(" + device + ", " + connectionPolicy + ")");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            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)) {
            try {
                return service.setConnectionPolicy(device, connectionPolicy, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        boolean defaultValue = false;
        if (!isValidDevice(device)
                || (connectionPolicy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
                        && connectionPolicy != BluetoothProfile.CONNECTION_POLICY_ALLOWED)) {
            return defaultValue;
        }
        return false;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.setConnectionPolicy(device, connectionPolicy, mAttributionSource),
                defaultValue);
    }

    /**
     * Get the connection policy of the profile.
     *
     * <p>The connection policy can be any of: {@link #CONNECTION_POLICY_ALLOWED}, {@link
     * #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN}
     *
     * @param device Bluetooth device
     * @return connection policy of the device
     * @hide
@@ -691,17 +655,15 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public @ConnectionPolicy int getConnectionPolicy(@NonNull BluetoothDevice device) {
        Log.v(TAG, "getConnectionPolicy(" + device + ")");
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            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) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        int defaultValue = BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
        if (!isValidDevice(device)) {
            return defaultValue;
        }
        return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.getConnectionPolicy(device, mAttributionSource),
                defaultValue);
    }

    /**
@@ -730,19 +692,16 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
            @IntRange(from = 0, to = 255) int volume,
            boolean isGroupOperation) {
        requireNonNull(device);
        if (!isValidDevice(device)) {
            return;
        }
        if (volume < 0 || volume > 255) {
            throw new IllegalArgumentException("illegal volume " + volume);
        }
        final IBluetoothVolumeControl service = getService();
        if (service == null) {
            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) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.setDeviceVolume(device, volume, isGroupOperation, mAttributionSource));
    }

    private static boolean isValidDevice(@Nullable BluetoothDevice device) {