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

Commit 43bfbd1a authored by William Escande's avatar William Escande
Browse files

Stop calling app to know its state. Use Callback

Test: atest AdapterServiceTests
Flag: com.android.bluetooth.flags.broadcast_adapter_state_with_callback
Bug: 357645528
Bug: 356769126
Bug: 341476279
Change-Id: I96c1606d6b46432323d80c5c4238113d84f913ce
parent 91ec84a4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import android.os.ResultReceiver;
 */
interface IBluetooth
{
    // TODO: b/357645528 - delete aidl method
    @JavaPassthrough(annotation="@android.annotation.RequiresNoPermission")
    int getState();

+13 −4
Original line number Diff line number Diff line
@@ -1152,8 +1152,10 @@ public class AdapterService extends Service {
    }

    private void invalidateBluetoothGetStateCache() {
        if (!Flags.broadcastAdapterStateWithCallback()) {
            BluetoothAdapter.invalidateBluetoothGetStateCache();
        }
    }

    void updateLeAudioProfileServiceState() {
        Set<Integer> nonSupportedProfiles = new HashSet<>();
@@ -1183,7 +1185,9 @@ public class AdapterService extends Service {

    void updateAdapterState(int prevState, int newState) {
        mAdapterProperties.setState(newState);
        if (!Flags.broadcastAdapterStateWithCallback()) {
            invalidateBluetoothGetStateCache();
        }

        // Only BluetoothManagerService should be registered
        int n = mRemoteCallbacks.beginBroadcast();
@@ -1481,7 +1485,9 @@ public class AdapterService extends Service {
        BluetoothAdapter.invalidateGetProfileConnectionStateCache();
        BluetoothAdapter.invalidateIsOffloadedFilteringSupportedCache();
        BluetoothDevice.invalidateBluetoothGetBondStateCache();
        if (!Flags.broadcastAdapterStateWithCallback()) {
            BluetoothAdapter.invalidateBluetoothGetStateCache();
        }
        BluetoothAdapter.invalidateGetAdapterConnectionStateCache();
        BluetoothMap.invalidateBluetoothGetConnectionStateCache();
        BluetoothSap.invalidateBluetoothGetConnectionStateCache();
@@ -2239,7 +2245,9 @@ public class AdapterService extends Service {

        AdapterServiceBinder(AdapterService svc) {
            mService = svc;
            if (!Flags.broadcastAdapterStateWithCallback()) {
                mService.invalidateBluetoothGetStateCache();
            }
            BluetoothAdapter.getDefaultAdapter().disableBluetoothGetStateCache();
        }

@@ -2250,6 +2258,7 @@ public class AdapterService extends Service {
            return mService;
        }

        // TODO: b/357645528 - delete getState method
        @Override
        public int getState() {
            AdapterService service = getService();
+13 −0
Original line number Diff line number Diff line
@@ -873,6 +873,8 @@ public final class BluetoothAdapter {
    @GuardedBy("mServiceLock")
    private IBluetooth mService;

    private static int sAdapterState = BluetoothAdapter.STATE_OFF;

    private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock();

    @GuardedBy("sServiceLock")
@@ -1490,6 +1492,9 @@ public final class BluetoothAdapter {

    /** Fetch the current bluetooth state. If the service is down, return OFF. */
    private @InternalAdapterState int getStateInternal() {
        if (Flags.broadcastAdapterStateWithCallback()) {
            return sAdapterState;
        }
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
@@ -3748,6 +3753,10 @@ public final class BluetoothAdapter {
                        }
                    }
                }

                public void onBluetoothAdapterStateChange(int newState) {
                    sAdapterState = newState;
                }
            };

    private final IBluetoothManagerCallback mManagerCallback =
@@ -3840,6 +3849,10 @@ public final class BluetoothAdapter {
                                        });
                            });
                }

                public void onBluetoothAdapterStateChange(int newState) {
                    // Nothing to do, this is entirely handled by sManagerCallback.
                }
            };

    /**
+1 −0
Original line number Diff line number Diff line
@@ -26,4 +26,5 @@ oneway interface IBluetoothManagerCallback {
    void onBluetoothServiceDown();
    void onBluetoothOn();
    void onBluetoothOff();
    void onBluetoothAdapterStateChange(int newState);
}
+32 −0
Original line number Diff line number Diff line
@@ -782,6 +782,16 @@ class BluetoothManagerService {
    IBluetooth registerAdapter(IBluetoothManagerCallback callback) {
        synchronized (mCallbacks) {
            mCallbacks.register(callback);
            if (Flags.broadcastAdapterStateWithCallback()) {
                try {
                    callback.onBluetoothAdapterStateChange(getState());
                } catch (RemoteException e) {
                    Log.e(
                            TAG,
                            "registerAdapter: Unable to call onBluetoothAdapterStateChange()",
                            e);
                }
            }
        }
        return mAdapter != null ? mAdapter.getAdapterBinder() : null;
    }
@@ -1387,6 +1397,27 @@ class BluetoothManagerService {
        }
    }

    private void sendBluetoothAdapterStateChangeCallback(int newState) {
        if (!Flags.broadcastAdapterStateWithCallback()) {
            return;
        }
        synchronized (mCallbacks) {
            try {
                int n = mCallbacks.beginBroadcast();
                Log.d(TAG, "sendBluetoothAdapterStateChangeCallback(): to " + n + " receivers");
                for (int i = 0; i < n; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onBluetoothAdapterStateChange(newState);
                    } catch (RemoteException e) {
                        Log.e(TAG, "onBluetoothAdapterStateChange: failed for callback #" + i, e);
                    }
                }
            } finally {
                mCallbacks.finishBroadcast();
            }
        }
    }

    String getAddress() {
        mAdapterLock.readLock().lock();
        try {
@@ -2041,6 +2072,7 @@ class BluetoothManagerService {
            return;
        }
        mState.set(newState);
        sendBluetoothAdapterStateChangeCallback(newState);

        if (prevState == STATE_ON) {
            autoOnSetupTimer();