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

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

SystemServer: ENABLE_MESSAGE handle in function

Having a separate method allow to be called without using the handler.
This will be used by the messenger

Bug: 288450479
Bug: 321804999
Test: m service-bluetooth | No-op Change
Change-Id: I8c6de8a41ff5d4e6310f1b754ee95096ad773824
parent b9ef05ca
Loading
Loading
Loading
Loading
+85 −76
Original line number Diff line number Diff line
@@ -1435,11 +1435,11 @@ class BluetoothManagerService {
    }

    private BluetoothServiceConnection mConnection = new BluetoothServiceConnection();
    private int mWaitForEnableRetry;

    @VisibleForTesting
    class BluetoothHandler extends Handler {
        boolean mGetNameAddressOnly = false;
        private int mWaitForEnableRetry;
        private int mWaitForDisableRetry;

        BluetoothHandler(Looper looper) {
@@ -1486,84 +1486,14 @@ class BluetoothManagerService {
                case MESSAGE_ENABLE:
                    int quietEnable = msg.arg1;
                    int isBle = msg.arg2;
                    if (mShutdownInProgress) {
                        Log.d(TAG, "Skip Bluetooth Enable in device shutdown process");
                        break;
                    }

                    Log.d(TAG, "MESSAGE_ENABLE(" + quietEnable + "): mAdapter=" + mAdapter);

                    if (mHandler.hasMessages(MESSAGE_HANDLE_DISABLE_DELAYED)
                            || mHandler.hasMessages(MESSAGE_HANDLE_ENABLE_DELAYED)) {
                        // We are handling enable or disable right now, wait for it.
                        mHandler.sendMessageDelayed(
                                mHandler.obtainMessage(MESSAGE_ENABLE, quietEnable, isBle),
                                ENABLE_DISABLE_DELAY_MS);
                        break;
                    }
                    Log.d(
                            TAG,
                            ("MESSAGE_ENABLE(quietEnable=" + quietEnable + ", isBle=" + isBle + ")")
                                    + (": mAdapter=" + mAdapter));

                    mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
                    mEnable = true;
                    handleEnableMessage(quietEnable, isBle);

                    if (isBle == 0) {
                        setBluetoothPersistedState(BLUETOOTH_ON_BLUETOOTH);
                    }

                    // Use service interface to get the exact state
                    mAdapterLock.readLock().lock();
                    try {
                        if (mAdapter != null) {
                            boolean isHandled = true;
                            switch (mState.get()) {
                                case STATE_BLE_ON:
                                    if (isBle == 1) {
                                        Log.i(TAG, "Already at BLE_ON State");
                                    } else {
                                        Log.w(TAG, "BT Enable in BLE_ON State, going to ON");
                                        mAdapter.startBrEdr(mContext.getAttributionSource());
                                    }
                                    break;
                                case STATE_BLE_TURNING_ON:
                                case STATE_TURNING_ON:
                                case STATE_ON:
                                    Log.i(TAG, "MESSAGE_ENABLE: already enabled");
                                    break;
                                default:
                                    isHandled = false;
                                    break;
                            }
                            if (isHandled) break;
                        }
                    } catch (RemoteException e) {
                        Log.e(TAG, "", e);
                    } finally {
                        mAdapterLock.readLock().unlock();
                    }

                    mQuietEnable = (quietEnable == 1);
                    if (mAdapter == null) {
                        handleEnable(mQuietEnable);
                    } else {
                        //
                        // We need to wait until transitioned to STATE_OFF and
                        // the previous Bluetooth process has exited. The
                        // waiting period has three components:
                        // (a) Wait until the local state is STATE_OFF. This
                        //     is accomplished by sending delay a message
                        //     MESSAGE_HANDLE_ENABLE_DELAYED
                        // (b) Wait until the STATE_OFF state is updated to
                        //     all components.
                        // (c) Wait until the Bluetooth process exits, and
                        //     ActivityManager detects it.
                        // The waiting for (b) and (c) is accomplished by
                        // delaying the MESSAGE_RESTART_BLUETOOTH_SERVICE
                        // message. The delay time is backed off if Bluetooth
                        // continuously failed to turn on itself.
                        //
                        mWaitForEnableRetry = 0;
                        mHandler.sendEmptyMessageDelayed(
                                MESSAGE_HANDLE_ENABLE_DELAYED, ENABLE_DISABLE_DELAY_MS);
                    }
                    break;

                case MESSAGE_DISABLE:
@@ -1950,6 +1880,85 @@ class BluetoothManagerService {
        return mHandler.hasMessages(MESSAGE_TIMEOUT_BIND);
    }

    private void handleEnableMessage(int quietEnable, int isBle) {
        if (mShutdownInProgress) {
            Log.d(TAG, "Skip Bluetooth Enable in device shutdown process");
            return;
        }

        if (mHandler.hasMessages(MESSAGE_HANDLE_DISABLE_DELAYED)
                || mHandler.hasMessages(MESSAGE_HANDLE_ENABLE_DELAYED)) {
            // We are handling enable or disable right now, wait for it.
            mHandler.sendMessageDelayed(
                    mHandler.obtainMessage(MESSAGE_ENABLE, quietEnable, isBle),
                    ENABLE_DISABLE_DELAY_MS);
            return;
        }

        mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
        mEnable = true;

        if (isBle == 0) {
            setBluetoothPersistedState(BLUETOOTH_ON_BLUETOOTH);
        }

        // Use service interface to get the exact state
        mAdapterLock.readLock().lock();
        try {
            if (mAdapter != null) {
                boolean isHandled = true;
                switch (mState.get()) {
                    case STATE_BLE_ON:
                        if (isBle == 1) {
                            Log.i(TAG, "Already at BLE_ON State");
                        } else {
                            Log.w(TAG, "BT Enable in BLE_ON State, going to ON");
                            mAdapter.startBrEdr(mContext.getAttributionSource());
                        }
                        break;
                    case STATE_BLE_TURNING_ON:
                    case STATE_TURNING_ON:
                    case STATE_ON:
                        Log.i(TAG, "MESSAGE_ENABLE: already enabled");
                        break;
                    default:
                        isHandled = false;
                        break;
                }
                if (isHandled) return;
            }
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        } finally {
            mAdapterLock.readLock().unlock();
        }

        mQuietEnable = (quietEnable == 1);
        if (mAdapter == null) {
            handleEnable(mQuietEnable);
        } else {
            //
            // We need to wait until transitioned to STATE_OFF and
            // the previous Bluetooth process has exited. The
            // waiting period has three components:
            // (a) Wait until the local state is STATE_OFF. This
            //     is accomplished by sending delay a message
            //     MESSAGE_HANDLE_ENABLE_DELAYED
            // (b) Wait until the STATE_OFF state is updated to
            //     all components.
            // (c) Wait until the Bluetooth process exits, and
            //     ActivityManager detects it.
            // The waiting for (b) and (c) is accomplished by
            // delaying the MESSAGE_RESTART_BLUETOOTH_SERVICE
            // message. The delay time is backed off if Bluetooth
            // continuously failed to turn on itself.
            //
            mWaitForEnableRetry = 0;
            mHandler.sendEmptyMessageDelayed(
                    MESSAGE_HANDLE_ENABLE_DELAYED, ENABLE_DISABLE_DELAY_MS);
        }
    }

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    private void handleEnable(boolean quietMode) {
        mQuietEnable = quietMode;