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

Commit 56f3c678 authored by William Escande's avatar William Escande
Browse files

Refactor code to prepare for user switch delay

Factorise code to have only one place to delay critical callback

Bug: 269973219
Test: manual testing
Change-Id: Ic4f89b6444a1f6c245e2b3f8f4d5de5e33327ad2
parent 73cb99ed
Loading
Loading
Loading
Loading
+51 −77
Original line number Diff line number Diff line
@@ -391,29 +391,14 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
        return false;
    }

    final Runnable mOnAirplaneModeChangedRunnable = () -> {
        onAirplaneModeChanged();
    };

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    public void onAirplaneModeChanged() {
        if (mHandler != null) {
            int delayAirplaneMs = 0;
            int state = getState();
            Log.d(TAG, "onAirplaneModeChanged state : " + BluetoothAdapter.nameForState(state)
                + ", isAirplaneModeOn() : " + isAirplaneModeOn());
            if (mHandler.hasCallbacks(mOnAirplaneModeChangedRunnable)) {
                mHandler.removeCallbacks(mOnAirplaneModeChangedRunnable);
            }
            /** If only LE mode with airplane on, should disable le, and turn off airplane
             *  should not turn on le.
             */
    private int estimateBusyTime(int state) {
        if (state == BluetoothAdapter.STATE_BLE_ON && isBluetoothPersistedStateOn()) {
                delayAirplaneMs = SERVICE_RESTART_TIME_MS;
            } if (state != BluetoothAdapter.STATE_ON && state != BluetoothAdapter.STATE_OFF
            // Bluetooth is in BLE and is starting classic
            return SERVICE_RESTART_TIME_MS;
        } else if (state != BluetoothAdapter.STATE_ON && state != BluetoothAdapter.STATE_OFF
                && state != BluetoothAdapter.STATE_BLE_ON) {
                // If Bluetooth is turning state, should handle airplane event after delay
                delayAirplaneMs = ADD_PROXY_DELAY_MS;
            // Bluetooth is turning state
            return ADD_PROXY_DELAY_MS;
        } else if (mHandler.hasMessages(MESSAGE_ENABLE)
                || mHandler.hasMessages(MESSAGE_DISABLE)
                || mHandler.hasMessages(MESSAGE_HANDLE_ENABLE_DELAYED)
@@ -421,16 +406,45 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                || mHandler.hasMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE)
                || mHandler.hasMessages(MESSAGE_TIMEOUT_BIND)
                || mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE)) {
                // If Bluetooth restarting, should handle airplane event after delay
                delayAirplaneMs = SERVICE_RESTART_TIME_MS;
            // Bluetooth is restarting
            return SERVICE_RESTART_TIME_MS;
        }
            if (delayAirplaneMs > 0) {
                Log.d(TAG, "onAirplaneModeChanged delay MS : " + delayAirplaneMs);
                mHandler.postDelayed(mOnAirplaneModeChangedRunnable, delayAirplaneMs);
                return;
        return 0;
    }

    private void delayModeChangedIfNeeded(Object token, Runnable r, String modechanged) {
        mHandler.removeCallbacksAndMessages(token);

        final int state = getState();
        final int delayMs = estimateBusyTime(state);
        Log.d(TAG, "delayModeChangedIfNeeded(" + modechanged + "): "
                + "state=" + BluetoothAdapter.nameForState(state)
                + ", isAirplaneModeOn()=" + isAirplaneModeOn()
                + ", isSatelliteModeSensitive()=" + isSatelliteModeSensitive()
                + ", isSatelliteModeOn()=" + isSatelliteModeOn()
                + ", delayed=" + delayMs + "ms");

        if (delayMs > 0) {
            mHandler.postDelayed(() -> delayModeChangedIfNeeded(token, r, modechanged),
                    token, delayMs);
        } else {
            r.run();
        }
        handleAirplaneModeChanged();
    }

    private static final Object ON_AIRPLANE_MODE_CHANGED_TOKEN = new Object();
    private static final Object ON_SATELLITE_MODE_CHANGED_TOKEN = new Object();

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    void onAirplaneModeChanged() {
        delayModeChangedIfNeeded(ON_AIRPLANE_MODE_CHANGED_TOKEN,
                () -> handleAirplaneModeChanged(), "onAirplaneModeChanged");
    }

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    void onSatelliteModeChanged() {
        delayModeChangedIfNeeded(ON_SATELLITE_MODE_CHANGED_TOKEN,
                () -> handleSatelliteModeChanged(), "onSatelliteModeChanged");
    }

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
@@ -492,48 +506,6 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
        }
    }

    final Runnable mOnSatelliteModeChangedRunnable = () -> {
        onSatelliteModeChanged();
    };

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    void onSatelliteModeChanged() {
        int delaySatelliteMs = 0;
        int state = getState();
        Log.d(TAG, "onSatelliteModeChanged state : " + BluetoothAdapter.nameForState(state)
                + ", isSatelliteModeSensitive() : " + isSatelliteModeSensitive()
                + ", isSatelliteModeOn() : " + isSatelliteModeOn());

        if (mHandler.hasCallbacks(mOnSatelliteModeChangedRunnable)) {
            mHandler.removeCallbacks(mOnSatelliteModeChangedRunnable);
        }

        if (state == BluetoothAdapter.STATE_BLE_ON && isBluetoothPersistedStateOn()) {
            delaySatelliteMs = SERVICE_RESTART_TIME_MS;
        }
        if (state != BluetoothAdapter.STATE_ON && state != BluetoothAdapter.STATE_OFF
                && state != BluetoothAdapter.STATE_BLE_ON) {
            // If Bluetooth is turning state, should handle event after delay
            delaySatelliteMs = ADD_PROXY_DELAY_MS;
        } else if (mHandler.hasMessages(MESSAGE_ENABLE)
                || mHandler.hasMessages(MESSAGE_DISABLE)
                || mHandler.hasMessages(MESSAGE_HANDLE_ENABLE_DELAYED)
                || mHandler.hasMessages(MESSAGE_HANDLE_DISABLE_DELAYED)
                || mHandler.hasMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE)
                || mHandler.hasMessages(MESSAGE_TIMEOUT_BIND)
                || mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE)) {
            // If Bluetooth restarting, should handle event after delay
            delaySatelliteMs = SERVICE_RESTART_TIME_MS;
        }

        if (delaySatelliteMs > 0) {
            Log.d(TAG, "onSatelliteModeChanged delay MS : " + delaySatelliteMs);
            mHandler.postDelayed(mOnSatelliteModeChangedRunnable, delaySatelliteMs);
        } else {
            handleSatelliteModeChanged();
        }
    }

    private void handleSatelliteModeChanged() {
        if (shouldBluetoothBeOn() && getState() != BluetoothAdapter.STATE_ON) {
            sendEnableMsg(mQuietEnableExternal,
@@ -3154,8 +3126,10 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
    private void recoverBluetoothServiceFromError(boolean clearBle) {
        Log.e(TAG, "recoverBluetoothServiceFromError");
        boolean repeatAirplaneRunnable = false;
        if (mHandler.hasCallbacks(mOnAirplaneModeChangedRunnable)) {
            mHandler.removeCallbacks(mOnAirplaneModeChangedRunnable);

        // 0 means we are matching unset `what` since we are using a token instead
        if (mHandler.hasMessages(0, ON_AIRPLANE_MODE_CHANGED_TOKEN)) {
            mHandler.removeCallbacksAndMessages(ON_AIRPLANE_MODE_CHANGED_TOKEN);
            repeatAirplaneRunnable = true;
        }
        mBluetoothLock.readLock().lock();
@@ -3207,7 +3181,7 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
        mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);

        if (repeatAirplaneRunnable) {
            mHandler.postDelayed(mOnAirplaneModeChangedRunnable, ERROR_RESTART_TIME_MS);
            onAirplaneModeChanged();
        }
    }