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

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

Merge "SystemServer: Broadcast callback no longer need synchro" into main

parents 6964abb8 5d0a6677
Loading
Loading
Loading
Loading
+27 −62
Original line number Diff line number Diff line
@@ -742,19 +742,15 @@ class BluetoothManagerService {

    // Called from unsafe binder thread
    IBluetooth registerAdapter(IBluetoothManagerCallback callback) {
        synchronized (mCallbacks) {
        mCallbacks.register(callback);
        }
        // Copy to local variable to avoid race condition when checking for null
        AdapterBinder adapter = mAdapter;
        return adapter != null ? adapter.getAdapterBinder() : null;
    }

    void unregisterAdapter(IBluetoothManagerCallback callback) {
        synchronized (mCallbacks) {
        mCallbacks.unregister(callback);
    }
    }

    boolean isEnabled() {
        return getState() == STATE_ON;
@@ -1248,74 +1244,43 @@ class BluetoothManagerService {
        mHandler.obtainMessage(MESSAGE_USER_UNLOCKED, userHandle).sendToTarget();
    }

    private void sendBluetoothOnCallback() {
        synchronized (mCallbacks) {
            try {
                int n = mCallbacks.beginBroadcast();
                Log.d(TAG, "Broadcasting onBluetoothOn() to " + n + " receivers.");
                for (int i = 0; i < n; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onBluetoothOn();
                    } catch (RemoteException e) {
                        Log.e(TAG, "Unable to call onBluetoothOn() on callback #" + i, e);
                    }
                }
            } finally {
                mCallbacks.finishBroadcast();
            }
        }
    @FunctionalInterface
    public interface RemoteExceptionConsumer<T> {
        void accept(T t) throws RemoteException;
    }

    private void sendBluetoothOffCallback() {
        synchronized (mCallbacks) {
            try {
                int n = mCallbacks.beginBroadcast();
                Log.d(TAG, "Broadcasting onBluetoothOff() to " + n + " receivers.");
                for (int i = 0; i < n; i++) {
    private void broadcastToAdapters(
            String logAction, RemoteExceptionConsumer<IBluetoothManagerCallback> action) {
        final int itemCount = mCallbacks.beginBroadcast();
        Log.d(TAG, "Broadcasting " + logAction + "() to " + itemCount + " receivers.");
        for (int i = 0; i < itemCount; i++) {
            try {
                        mCallbacks.getBroadcastItem(i).onBluetoothOff();
                action.accept(mCallbacks.getBroadcastItem(i));
            } catch (RemoteException e) {
                        Log.e(TAG, "Unable to call onBluetoothOff() on callback #" + i, e);
                Log.e(TAG, "RemoteException while calling " + logAction + "()#" + i, e);
            }
        }
            } finally {
        mCallbacks.finishBroadcast();
    }

    private void sendBluetoothOnCallback() {
        broadcastToAdapters("sendBluetoothOnCallback", IBluetoothManagerCallback::onBluetoothOn);
    }

    private void sendBluetoothOffCallback() {
        broadcastToAdapters("sendBluetoothOffCallback", IBluetoothManagerCallback::onBluetoothOff);
    }

    /** Inform BluetoothAdapter instances that Adapter service is up */
    private void sendBluetoothServiceUpCallback() {
        synchronized (mCallbacks) {
            int n = mCallbacks.beginBroadcast();
            Log.d(TAG, "sendBluetoothServiceUpCallback(): to " + n + " receivers");
            for (int i = 0; i < n; i++) {
                try {
                    mCallbacks
                            .getBroadcastItem(i)
                            .onBluetoothServiceUp(mAdapter.getAdapterBinder().asBinder());
                } catch (RemoteException e) {
                    Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
                }
            }
            mCallbacks.finishBroadcast();
        }
        broadcastToAdapters(
                "sendBluetoothServiceUpCallback",
                (item) -> item.onBluetoothServiceUp(mAdapter.getAdapterBinder().asBinder()));
    }

    /** Inform BluetoothAdapter instances that Adapter service is down */
    private void sendBluetoothServiceDownCallback() {
        synchronized (mCallbacks) {
            int n = mCallbacks.beginBroadcast();
            Log.d(TAG, "sendBluetoothServiceDownCallback(): to " + n + " receivers");
            for (int i = 0; i < n; i++) {
                try {
                    mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
                } catch (RemoteException e) {
                    Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
                }
            }
            mCallbacks.finishBroadcast();
        }
        broadcastToAdapters(
                "sendBluetoothServiceDownCallback",
                IBluetoothManagerCallback::onBluetoothServiceDown);
    }

    // Called from unsafe binder thread