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

Commit 9295e681 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge "Remove unused {get,set}BluetoothClass hidden api"

parents 06829ebd 18533155
Loading
Loading
Loading
Loading
+0 −37
Original line number Diff line number Diff line
@@ -316,42 +316,6 @@ class AdapterProperties {
        }
    }

    /**
     * Set the Bluetooth Class of Device (CoD) of the adapter.
     *
     * <p>Bluetooth stack stores some adapter properties in native BT stack storage and some in the
     * Java Android stack. Bluetooth CoD is stored in the Android layer through
     * {@link android.provider.Settings.Global#BLUETOOTH_CLASS_OF_DEVICE}.
     *
     * <p>Due to this, the getAdapterPropertyNative and adapterPropertyChangedCallback methods don't
     * actually update mBluetoothClass. Hence, we update the field mBluetoothClass every time we
     * successfully update BluetoothClass.
     *
     * @param bluetoothClass BluetoothClass of the device
     */
    boolean setBluetoothClass(BluetoothClass bluetoothClass) {
        synchronized (mObject) {
            boolean result =
                    mService.setAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE,
                            bluetoothClass.getClassOfDeviceBytes());

            if (result) {
                mBluetoothClass = bluetoothClass;
            }

            return result;
        }
    }

    /**
     * @return the BluetoothClass of the Bluetooth adapter.
     */
    BluetoothClass getBluetoothClass() {
        synchronized (mObject) {
            return mBluetoothClass;
        }
    }

    boolean setIoCapability(int capability) {
        synchronized (mObject) {
            boolean result = mService.setAdapterPropertyNative(
@@ -1202,7 +1166,6 @@ class AdapterProperties {
        writer.println(TAG);
        writer.println("  " + "Name: " + getName());
        writer.println("  " + "Address: " + Utils.getAddressStringFromByte(mAddress));
        writer.println("  " + "BluetoothClass: " + getBluetoothClass());
        writer.println("  " + "ScanMode: " + dumpScanMode(getScanMode()));
        writer.println("  " + "ConnectionState: " + dumpConnectionState(getConnectionState()));
        writer.println("  " + "State: " + BluetoothAdapter.nameForState(getState()));
+0 −73
Original line number Diff line number Diff line
@@ -47,7 +47,6 @@ import android.bluetooth.BluetoothActivityEnergyInfo;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter.ActiveDeviceProfile;
import android.bluetooth.BluetoothAdapter.ActiveDeviceUse;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothFrameworkInitializer;
import android.bluetooth.BluetoothMap;
@@ -485,7 +484,6 @@ public class AdapterService extends Service {
                            && mRegisteredProfiles.size() == mRunningProfiles.size()) {
                        mAdapterProperties.onBluetoothReady();
                        updateUuids();
                        setBluetoothClassFromConfig();
                        initProfileServices();
                        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_LOCAL_IO_CAPS);
                        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_DYNAMIC_AUDIO_BUFFER);
@@ -859,21 +857,6 @@ public class AdapterService extends Service {
        }
    }

    /**
     * Sets the Bluetooth CoD value of the local adapter if there exists a config value for it.
     */
    void setBluetoothClassFromConfig() {
        int bluetoothClassConfig = retrieveBluetoothClassConfig();
        if (bluetoothClassConfig != 0) {
            mAdapterProperties.setBluetoothClass(new BluetoothClass(bluetoothClassConfig));
        }
    }

    private int retrieveBluetoothClassConfig() {
        return Settings.Global.getInt(
                getContentResolver(), Settings.Global.BLUETOOTH_CLASS_OF_DEVICE, 0);
    }

    void startProfileServices() {
        debugLog("startCoreServices()");
        Class[] supportedProfileServices = Config.getSupportedProfiles();
@@ -884,7 +867,6 @@ public class AdapterService extends Service {
                .equals(supportedProfileServices[0].getSimpleName())) {
            mAdapterProperties.onBluetoothReady();
            updateUuids();
            setBluetoothClassFromConfig();
            mAdapterStateMachine.sendMessage(AdapterState.BREDR_STARTED);
        } else {
            setAllProfileServiceStates(supportedProfileServices, BluetoothAdapter.STATE_ON);
@@ -2151,61 +2133,6 @@ public class AdapterService extends Service {
            return service.mAdapterProperties.setName(name);
        }

        @Override
        public void getBluetoothClass(AttributionSource source,
                SynchronousResultReceiver receiver) {
            try {
                receiver.send(getBluetoothClass(source));
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
        }
        @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
        private BluetoothClass getBluetoothClass(AttributionSource attributionSource) {
            AdapterService service = getService();
            if (service == null
                    || !callerIsSystemOrActiveOrManagedUser(service, TAG, "getBluetoothClass")
                    || !Utils.checkConnectPermissionForDataDelivery(
                            service, attributionSource, "AdapterSource getBluetoothClass")) {
                return null;
            }

            return service.mAdapterProperties.getBluetoothClass();
        }

        @Override
        public void setBluetoothClass(BluetoothClass bluetoothClass, AttributionSource source,
                SynchronousResultReceiver receiver) {
            try {
                receiver.send(setBluetoothClass(bluetoothClass, source));
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
        }
        @RequiresPermission(allOf = {
                android.Manifest.permission.BLUETOOTH_CONNECT,
                android.Manifest.permission.BLUETOOTH_PRIVILEGED,
        })
        private boolean setBluetoothClass(BluetoothClass bluetoothClass, AttributionSource source) {
            AdapterService service = getService();
            if (service == null
                    || !callerIsSystemOrActiveOrManagedUser(service, TAG, "setBluetoothClass")
                    || !Utils.checkConnectPermissionForDataDelivery(service, source, TAG)) {
                return false;
            }

            enforceBluetoothPrivilegedPermission(service);

            if (!service.mAdapterProperties.setBluetoothClass(bluetoothClass)) {
              return false;
            }

            return Settings.Global.putInt(
                    service.getContentResolver(),
                    Settings.Global.BLUETOOTH_CLASS_OF_DEVICE,
                    bluetoothClass.getClassOfDevice());
        }

        @Override
        public void getIoCapability(AttributionSource source, SynchronousResultReceiver receiver) {
            try {
+0 −14
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.IBluetoothOobDataCallback;
import android.content.AttributionSource;
import android.os.ParcelUuid;
@@ -96,12 +95,6 @@ public class AdapterServiceBinderTest {
        verify(mService, never()).generateLocalOobData(transport, cb);
    }

    @Test
    public void getBluetoothClass() {
        mBinder.getBluetoothClass(mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).getBluetoothClass();
    }

    @Test
    public void getIoCapability() {
        mBinder.getIoCapability(mAttributionSource, SynchronousResultReceiver.get());
@@ -171,13 +164,6 @@ public class AdapterServiceBinderTest {
        verify(mService).retrievePendingSocketForServiceRecord(uuid, mAttributionSource);
    }

    @Test
    public void setBluetoothClass() {
        BluetoothClass btClass = new BluetoothClass(0);
        mBinder.setBluetoothClass(btClass, mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).setBluetoothClass(btClass);
    }

    @Test
    public void setIoCapability() {
        int capability = BluetoothAdapter.IO_CAPABILITY_MAX - 1;
+0 −66
Original line number Diff line number Diff line
@@ -1802,72 +1802,6 @@ public final class BluetoothAdapter {
        return false;
    }

    /**
     * Returns the {@link BluetoothClass} Bluetooth Class of Device (CoD) of the local Bluetooth
     * adapter.
     *
     * @return {@link BluetoothClass} Bluetooth CoD of local Bluetooth device.
     *
     * @hide
     */
    @RequiresLegacyBluetoothAdminPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public BluetoothClass getBluetoothClass() {
        if (getState() != STATE_ON) {
            return null;
        }
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                final SynchronousResultReceiver<BluetoothClass> recv =
                        SynchronousResultReceiver.get();
                mService.getBluetoothClass(mAttributionSource, recv);
                return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
            }
        } catch (RemoteException | TimeoutException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
            mServiceLock.readLock().unlock();
        }
        return null;
    }

    /**
     * Sets the {@link BluetoothClass} Bluetooth Class of Device (CoD) of the local Bluetooth
     * adapter.
     *
     * <p>Note: This value persists across system reboot.
     *
     * @param bluetoothClass {@link BluetoothClass} to set the local Bluetooth adapter to.
     * @return true if successful, false if unsuccessful.
     *
     * @hide
     */
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {
            android.Manifest.permission.BLUETOOTH_CONNECT,
            android.Manifest.permission.BLUETOOTH_PRIVILEGED,
    })
    public boolean setBluetoothClass(BluetoothClass bluetoothClass) {
        if (getState() != STATE_ON) {
            return false;
        }
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
                mService.setBluetoothClass(bluetoothClass, mAttributionSource, recv);
                return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(false);
            }
        } catch (RemoteException | TimeoutException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
            mServiceLock.readLock().unlock();
        }
        return false;
    }

    /**
     * Returns the Input/Output capability of the device for classic Bluetooth.
     *
+0 −30
Original line number Diff line number Diff line
@@ -23,10 +23,6 @@ import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

/**
 * Represents a Bluetooth class, which describes general characteristics
 * and capabilities of a device. For example, a Bluetooth class will
@@ -290,32 +286,6 @@ public final class BluetoothClass implements Parcelable {
        return mClass;
    }

    /**
     * Return the Bluetooth Class of Device (CoD) value including the
     * {@link BluetoothClass.Service}, {@link BluetoothClass.Device.Major} and
     * minor device fields.
     *
     * <p>This value is a byte array representation of Bluetooth CoD as in
     * Bluetooth specification.
     *
     * <p>Bluetooth COD information is 3 bytes, but stored as an int. Hence the
     * MSB is useless and needs to be thrown away. The lower 3 bytes are
     * converted into a byte array MSB to LSB. Hence, using BIG_ENDIAN.
     *
     * @see <a href="Bluetooth CoD">https://www.bluetooth.com/specifications/assigned-numbers/baseband</a>
     *
     * @hide
     */
    public byte[] getClassOfDeviceBytes() {
        byte[] bytes = ByteBuffer.allocate(4)
                .order(ByteOrder.BIG_ENDIAN)
                .putInt(mClass)
                .array();

        // Discard the top byte
        return Arrays.copyOfRange(bytes, 1, bytes.length);
    }

    public static final int PROFILE_HEADSET = 0;

    public static final int PROFILE_A2DP = 1;
Loading