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

Commit 809fb58f authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge "SystemServer: factorize bindToAdapter" into main

parents 3bd7ea9c d2f67cb5
Loading
Loading
Loading
Loading
+22 −30
Original line number Diff line number Diff line
@@ -1479,15 +1479,7 @@ class BluetoothManagerService {
                        if (mAdapter == null && !isBinding()) {
                            Log.d(TAG, "Binding to service to get name and address");
                            mGetNameAddressOnly = true;
                            mHandler.sendEmptyMessageDelayed(MESSAGE_TIMEOUT_BIND, TIMEOUT_BIND_MS);
                            Intent i = new Intent(IBluetooth.class.getName());
                            if (!doBind(
                                    i,
                                    mConnection,
                                    Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
                                    UserHandle.CURRENT)) {
                                mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
                            }
                            bindToAdapter();
                        } else if (mAdapter != null) {
                            try {
                                storeNameAndAddress(
@@ -1974,23 +1966,33 @@ class BluetoothManagerService {
        }
    }

    private void bindToAdapter() {
        UserHandle user = UserHandle.CURRENT;
        int flags = Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT;
        Intent intent = new Intent(IBluetooth.class.getName());
        ComponentName comp = resolveSystemService(intent);
        if (comp == null) {
            Log.e(TAG, "No ComponentName found for intent=" + intent);
            return;
        }
        intent.setComponent(comp);

        mHandler.sendEmptyMessageDelayed(MESSAGE_TIMEOUT_BIND, TIMEOUT_BIND_MS);
        Log.d(TAG, "Start binding to the Bluetooth service with intent=" + intent);
        if (!mContext.bindServiceAsUser(intent, mConnection, flags, user)) {
            Log.e(TAG, "Fail to bind to intent=" + intent);
            mContext.unbindService(mConnection);
            mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
        }
    }

    private void handleEnable(boolean quietMode) {
        mQuietEnable = quietMode;

        mAdapterLock.writeLock().lock();
        try {
            if (mAdapter == null && !isBinding()) {
                Log.d(TAG, "binding Bluetooth service");
                // Start bind timeout and bind
                mHandler.sendEmptyMessageDelayed(MESSAGE_TIMEOUT_BIND, TIMEOUT_BIND_MS);
                Intent i = new Intent(IBluetooth.class.getName());
                if (!doBind(
                        i,
                        mConnection,
                        Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
                        UserHandle.CURRENT)) {
                    mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
                }
                bindToAdapter();
            } else if (!Flags.fastBindToApp() && mAdapter != null) {
                // Enable bluetooth
                try {
@@ -2004,16 +2006,6 @@ class BluetoothManagerService {
        }
    }

    boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) {
        ComponentName comp = resolveSystemService(intent);
        intent.setComponent(comp);
        if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) {
            Log.e(TAG, "Fail to bind to: " + intent);
            return false;
        }
        return true;
    }

    private void handleDisable() {
        mAdapterLock.readLock().lock();
        try {
+2 −0
Original line number Diff line number Diff line
@@ -219,8 +219,10 @@ public class BluetoothManagerServiceTest {
                        any(ServiceConnection.class),
                        anyInt(),
                        any(UserHandle.class));
        doNothing().when(mContext).unbindService(any());
        mManagerService.enableBle("enable_bindFailure_removesTimeout", mBinder);
        syncHandler(MESSAGE_ENABLE);
        verify(mContext).unbindService(any());

        // TODO(b/280518177): Failed to start should be noted / reported in metrics
        // Maybe show a popup or a crash notification