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

Commit 34d18591 authored by Hui Peng's avatar Hui Peng Committed by Android (Google) Code Review
Browse files

Merge "Fix a deadlock bug in Bluetooth Framework" into tm-qpr-dev

parents 44f49cb8 340bc09a
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -4233,14 +4233,18 @@ public final class BluetoothAdapter {

    /*package*/ IBluetooth getBluetoothService() {
        synchronized (sServiceLock) {
            if (sProxyServiceStateCallbacks.isEmpty()) {
                throw new IllegalStateException(
                        "Anonymous service access requires at least one lifecycle in process");
            }
            return sService;
        }
    }

    /**
     * Registers a IBluetoothManagerCallback and returns the cached
     * Bluetooth service proxy object.
     *
     * TODO: rename this API to registerBlueoothManagerCallback or something?
     * the current name does not match what it does very well.
     *
     * /
    @UnsupportedAppUsage
    /*package*/ IBluetooth getBluetoothService(IBluetoothManagerCallback cb) {
        Objects.requireNonNull(cb);
+34 −76
Original line number Diff line number Diff line
@@ -1278,55 +1278,14 @@ public final class BluetoothDevice implements Parcelable, Attributable {

    private static final String NULL_MAC_ADDRESS = "00:00:00:00:00:00";

    /**
     * Lazy initialization. Guaranteed final after first object constructed, or
     * getService() called.
     * TODO: Unify implementation of sService amongst BluetoothFoo API's
     */
    private static volatile IBluetooth sService;

    private final String mAddress;
    @AddressType private final int mAddressType;

    private AttributionSource mAttributionSource;

    /*package*/
    static IBluetooth getService() {
        synchronized (BluetoothDevice.class) {
            if (sService == null) {
                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                sService = adapter.getBluetoothService(sStateChangeCallback);
            }
        }
        return sService;
    }

    static IBluetoothManagerCallback sStateChangeCallback = new IBluetoothManagerCallback.Stub() {

        public void onBluetoothServiceUp(IBluetooth bluetoothService)
                throws RemoteException {
            synchronized (BluetoothDevice.class) {
                if (sService == null) {
                    sService = bluetoothService;
                }
        return BluetoothAdapter.getDefaultAdapter().getBluetoothService();
    }
        }

        public void onBluetoothServiceDown()
                throws RemoteException {
            synchronized (BluetoothDevice.class) {
                sService = null;
            }
        }

        public void onBrEdrDown() {
            if (DBG) Log.d(TAG, "onBrEdrDown: reached BLE ON state");
        }

        public void onOobData(@Transport int transport, OobData oobData) {
            if (DBG) Log.d(TAG, "onOobData: got data");
        }
    };

    /**
     * Create a new BluetoothDevice.
@@ -1340,7 +1299,6 @@ public final class BluetoothDevice implements Parcelable, Attributable {
     * @hide
     */
    /*package*/ BluetoothDevice(String address, int addressType) {
        getService();  // ensures sService is initialized
        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
            throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
        }
@@ -1488,7 +1446,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public @Nullable String getIdentityAddress() {
        if (DBG) log("getIdentityAddress()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final String defaultValue = null;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot get identity address");
@@ -1518,7 +1476,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public String getName() {
        if (DBG) log("getName()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final String defaultValue = null;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot get Remote Device name");
@@ -1553,7 +1511,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public int getType() {
        if (DBG) log("getType()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = DEVICE_TYPE_UNKNOWN;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot get Remote Device type");
@@ -1582,7 +1540,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public String getAlias() {
        if (DBG) log("getAlias()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final String defaultValue = null;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias");
@@ -1643,7 +1601,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
            throw new IllegalArgumentException("alias cannot be the empty string");
        }
        if (DBG) log("setAlias(" + alias + ")");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot set Remote Device name");
@@ -1677,7 +1635,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public @IntRange(from = -100, to = 100) int getBatteryLevel() {
        if (DBG) log("getBatteryLevel()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = BATTERY_LEVEL_BLUETOOTH_OFF;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "Bluetooth disabled. Cannot get remote device battery level");
@@ -1772,7 +1730,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    private boolean createBondInternal(int transport, @Nullable OobData remoteP192Data,
            @Nullable OobData remoteP256Data) {
        if (DBG) log("createBondOutOfBand()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "BT not enabled, createBondOutOfBand failed");
@@ -1805,7 +1763,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public boolean isBondingInitiatedLocally() {
        if (DBG) log("isBondingInitiatedLocally()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "BT not enabled, isBondingInitiatedLocally failed");
@@ -1832,7 +1790,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public boolean cancelBondProcess() {
        if (DBG) log("cancelBondProcess()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond");
@@ -1865,7 +1823,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public boolean removeBond() {
        if (DBG) log("removeBond()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond");
@@ -1955,7 +1913,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @SuppressLint("AndroidFrameworkRequiresPermission")
    public int getBondState() {
        if (DBG) log("getBondState(" + getAnonymizedAddress() + ")");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        if (service == null) {
            Log.e(TAG, "BT not enabled. Cannot get bond state");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
@@ -1988,7 +1946,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public boolean canBondWithoutDialog() {
        if (DBG) log("canBondWithoutDialog, device: " + this);
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot check if we can skip pairing dialog");
@@ -2042,7 +2000,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
        if (!BluetoothAdapter.checkBluetoothAddress(getAddress())) {
            throw new IllegalArgumentException("device cannot have an invalid address");
        }
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot connect to remote device.");
@@ -2089,7 +2047,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
        if (!BluetoothAdapter.checkBluetoothAddress(getAddress())) {
            throw new IllegalArgumentException("device cannot have an invalid address");
        }
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot disconnect to remote device.");
@@ -2121,7 +2079,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public boolean isConnected() {
        if (DBG) log("isConnected()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = CONNECTION_STATE_DISCONNECTED;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "Proxy not attached to service");
@@ -2153,7 +2111,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public boolean isEncrypted() {
        if (DBG) log("isEncrypted()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = CONNECTION_STATE_DISCONNECTED;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "Proxy not attached to service");
@@ -2182,7 +2140,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public BluetoothClass getBluetoothClass() {
        if (DBG) log("getBluetoothClass()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = 0;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class");
@@ -2216,7 +2174,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public ParcelUuid[] getUuids() {
        if (DBG) log("getUuids()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final ParcelUuid[] defaultValue = null;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot get remote device Uuids");
@@ -2283,7 +2241,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public boolean fetchUuidsWithSdp(@Transport int transport) {
        if (DBG) log("fetchUuidsWithSdp()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot fetchUuidsWithSdp");
@@ -2325,7 +2283,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public boolean sdpSearch(ParcelUuid uuid) {
        if (DBG) log("sdpSearch()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot query remote device sdp records");
@@ -2352,7 +2310,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public boolean setPin(byte[] pin) {
        if (DBG) log("setPin()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot set Remote Device pin");
@@ -2398,7 +2356,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public boolean setPairingConfirmation(boolean confirm) {
        if (DBG) log("setPairingConfirmation()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot set pairing confirmation");
@@ -2437,7 +2395,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public @AccessPermission int getPhonebookAccessPermission() {
        if (DBG) log("getPhonebookAccessPermission()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = ACCESS_UNKNOWN;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "Proxy not attached to service");
@@ -2484,7 +2442,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public boolean setSilenceMode(boolean silence) {
        if (DBG) log("setSilenceMode()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            throw new IllegalStateException("Bluetooth is not turned ON");
@@ -2514,7 +2472,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public boolean isInSilenceMode() {
        if (DBG) log("isInSilenceMode()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            throw new IllegalStateException("Bluetooth is not turned ON");
@@ -2545,7 +2503,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public boolean setPhonebookAccessPermission(@AccessPermission int value) {
        if (DBG) log("setPhonebookAccessPermission()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "Proxy not attached to service");
@@ -2574,7 +2532,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public @AccessPermission int getMessageAccessPermission() {
        if (DBG) log("getMessageAccessPermission()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = ACCESS_UNKNOWN;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "Proxy not attached to service");
@@ -2611,7 +2569,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
            throw new IllegalArgumentException(value + "is not a valid AccessPermission value");
        }
        if (DBG) log("setMessageAccessPermission()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "Proxy not attached to service");
@@ -2640,7 +2598,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public @AccessPermission int getSimAccessPermission() {
        if (DBG) log("getSimAccessPermission()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final int defaultValue = ACCESS_UNKNOWN;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "Proxy not attached to service");
@@ -2673,7 +2631,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public boolean setSimAccessPermission(int value) {
        if (DBG) log("setSimAccessPermission()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.w(TAG, "Proxy not attached to service");
@@ -3184,7 +3142,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public boolean setMetadata(@MetadataKey int key, @NonNull byte[] value) {
        if (DBG) log("setMetadata()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "Bluetooth is not enabled. Cannot set metadata");
@@ -3219,7 +3177,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public byte[] getMetadata(@MetadataKey int key) {
        if (DBG) log("getMetadata()");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final byte[] defaultValue = null;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "Bluetooth is not enabled. Cannot get metadata");
@@ -3262,7 +3220,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    })
    public boolean setLowLatencyAudioAllowed(boolean allowed) {
        if (DBG) log("setLowLatencyAudioAllowed(" + allowed + ")");
        final IBluetooth service = sService;
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "Bluetooth is not enabled. Cannot allow low latency");