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

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

Adapter: registerBluetoothConnection

Registering the callback is done in two step, the API should return true
as long as it managed to add the callback to its list.
Registering the callback to the Bluetooth service is done anytime the
bluetooth is up

Bug: 339585576
Bug: 341846395
Flag: Exempt fixing test flakiness
Test: atest CtsBluetoothTestCases
Change-Id: Ibced64d8901456f925b154aff3620810ddab2e3c
parent 695ba152
Loading
Loading
Loading
Loading
+28 −31
Original line number Diff line number Diff line
@@ -3762,23 +3762,10 @@ public final class BluetoothAdapter {
                                }
                            }
                        }
                        synchronized (mBluetoothConnectionCallbackExecutorMap) {
                            if (!mBluetoothConnectionCallbackExecutorMap.isEmpty()) {
                                try {
                                    mService.registerBluetoothConnectionCallback(
                                            mConnectionCallback, mAttributionSource);
                                } catch (RemoteException e) {
                                    Log.e(
                                            TAG,
                                            "onBluetoothServiceUp: Failed to register "
                                                    + "bluetooth connection callback",
                                            e);
                                }
                            }
                        }
                    } finally {
                        mServiceLock.readLock().unlock();
                    }
                    registerBluetoothConnectionCallbackIfNeeded();
                }

                public void onBluetoothServiceDown() {
@@ -4630,7 +4617,7 @@ public final class BluetoothAdapter {
    }

    @SuppressLint("AndroidFrameworkBluetoothPermission")
    private final IBluetoothConnectionCallback mConnectionCallback =
    private final IBluetoothConnectionCallback mBluetoothConnectionCallback =
            new IBluetoothConnectionCallback.Stub() {
                @Override
                public void onDeviceConnected(BluetoothDevice device) {
@@ -4686,27 +4673,37 @@ public final class BluetoothAdapter {
            }

            if (mBluetoothConnectionCallbackExecutorMap.isEmpty()) {
                // If the callback map is empty, we register the service-to-app callback
                registerBluetoothConnectionCallback();
            }

            mBluetoothConnectionCallbackExecutorMap.put(callback, executor);
        }

        return true;
    }

    private void registerBluetoothConnectionCallback() {
        mServiceLock.readLock().lock();
        try {
            if (mService == null) {
                        return false;
                return;
            }
            mService.registerBluetoothConnectionCallback(
                            mConnectionCallback, mAttributionSource);
                    mBluetoothConnectionCallback, mAttributionSource);
        } catch (RemoteException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
                    return false;
        } finally {
            mServiceLock.readLock().unlock();
        }
    }

            // Adds the passed in callback to our map of callbacks to executors
            mBluetoothConnectionCallbackExecutorMap.put(callback, executor);
    private void registerBluetoothConnectionCallbackIfNeeded() {
        synchronized (mBluetoothConnectionCallbackExecutorMap) {
            if (mBluetoothConnectionCallbackExecutorMap.isEmpty()) {
                return;
            }
            registerBluetoothConnectionCallback();
        }

        return true;
    }

    /**
@@ -4745,7 +4742,7 @@ public final class BluetoothAdapter {
                        return true;
                    }
                    mService.unregisterBluetoothConnectionCallback(
                            mConnectionCallback, mAttributionSource);
                            mBluetoothConnectionCallback, mAttributionSource);
                } catch (RemoteException e) {
                    Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
                } finally {