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

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

Merge changes I969e156c,I4d2e8d7b,I2ac8526c into main

* changes:
  SystemServer: Airplane change when FactoryReset
  Remove un-necessary post
  Untangled RESTORE_USER_SETTING message
parents de9adba3 36c8630f
Loading
Loading
Loading
Loading
+64 −77
Original line number Original line Diff line number Diff line
@@ -142,7 +142,7 @@ class BluetoothManagerService {
    // Delay for the addProxy function in msec
    // Delay for the addProxy function in msec
    private static final int ADD_PROXY_DELAY_MS = 100 * HW_MULTIPLIER;
    private static final int ADD_PROXY_DELAY_MS = 100 * HW_MULTIPLIER;
    // Delay for retrying enable and disable in msec
    // Delay for retrying enable and disable in msec
    private static final int ENABLE_DISABLE_DELAY_MS = 300 * HW_MULTIPLIER;
    @VisibleForTesting static final int ENABLE_DISABLE_DELAY_MS = 300 * HW_MULTIPLIER;


    @VisibleForTesting static final int MESSAGE_ENABLE = 1;
    @VisibleForTesting static final int MESSAGE_ENABLE = 1;
    @VisibleForTesting static final int MESSAGE_DISABLE = 2;
    @VisibleForTesting static final int MESSAGE_DISABLE = 2;
@@ -157,10 +157,8 @@ class BluetoothManagerService {
    @VisibleForTesting static final int MESSAGE_GET_NAME_AND_ADDRESS = 200;
    @VisibleForTesting static final int MESSAGE_GET_NAME_AND_ADDRESS = 200;
    @VisibleForTesting static final int MESSAGE_USER_SWITCHED = 300;
    @VisibleForTesting static final int MESSAGE_USER_SWITCHED = 300;
    @VisibleForTesting static final int MESSAGE_USER_UNLOCKED = 301;
    @VisibleForTesting static final int MESSAGE_USER_UNLOCKED = 301;
    @VisibleForTesting static final int MESSAGE_RESTORE_USER_SETTING = 500;
    @VisibleForTesting static final int MESSAGE_RESTORE_USER_SETTING_OFF = 501;

    @VisibleForTesting static final int MESSAGE_RESTORE_USER_SETTING_ON = 502;
    private static final int RESTORE_SETTING_TO_ON = 1;
    private static final int RESTORE_SETTING_TO_OFF = 0;


    private static final int MAX_ERROR_RESTART_RETRIES = 6;
    private static final int MAX_ERROR_RESTART_RETRIES = 6;
    private static final int MAX_WAIT_FOR_ENABLE_DISABLE_RETRIES = 10;
    private static final int MAX_WAIT_FOR_ENABLE_DISABLE_RETRIES = 10;
@@ -184,7 +182,7 @@ class BluetoothManagerService {
    private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks =
    private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks =
            new RemoteCallbackList<>();
            new RemoteCallbackList<>();
    private final BluetoothServiceBinder mBinder;
    private final BluetoothServiceBinder mBinder;
    private final BluetoothHandler mHandler;
    @VisibleForTesting final BluetoothHandler mHandler;
    private final ContentResolver mContentResolver;
    private final ContentResolver mContentResolver;
    private final Context mContext;
    private final Context mContext;
    private final Looper mLooper;
    private final Looper mLooper;
@@ -401,39 +399,30 @@ class BluetoothManagerService {
    private static final Object ON_SWITCH_USER_TOKEN = new Object();
    private static final Object ON_SWITCH_USER_TOKEN = new Object();


    Unit onAirplaneModeChanged(boolean isAirplaneModeOn) {
    Unit onAirplaneModeChanged(boolean isAirplaneModeOn) {
        mHandler.postDelayed(
                () ->
        delayModeChangedIfNeeded(
        delayModeChangedIfNeeded(
                ON_AIRPLANE_MODE_CHANGED_TOKEN,
                ON_AIRPLANE_MODE_CHANGED_TOKEN,
                () -> handleAirplaneModeChanged(isAirplaneModeOn),
                () -> handleAirplaneModeChanged(isAirplaneModeOn),
                                "onAirplaneModeChanged"),
                "onAirplaneModeChanged");
                ON_AIRPLANE_MODE_CHANGED_TOKEN,
                0);
        return Unit.INSTANCE;
        return Unit.INSTANCE;
    }
    }


    // TODO(b/289584302): Update to private once use_new_satellite_mode is enabled
    // TODO(b/289584302): Update to private once use_new_satellite_mode is enabled
    Unit onSatelliteModeChanged(boolean isSatelliteModeOn) {
    Unit onSatelliteModeChanged(boolean isSatelliteModeOn) {
        mHandler.postDelayed(
                () ->
        delayModeChangedIfNeeded(
        delayModeChangedIfNeeded(
                ON_SATELLITE_MODE_CHANGED_TOKEN,
                ON_SATELLITE_MODE_CHANGED_TOKEN,
                () -> handleSatelliteModeChanged(isSatelliteModeOn),
                () -> handleSatelliteModeChanged(isSatelliteModeOn),
                                "onSatelliteModeChanged"),
                "onSatelliteModeChanged");
                ON_SATELLITE_MODE_CHANGED_TOKEN,
                0);
        return Unit.INSTANCE;
        return Unit.INSTANCE;
    }
    }


    // Call is coming from the systemServer main thread and need to be post to avoid race
    void onSwitchUser(UserHandle userHandle) {
    void onSwitchUser(UserHandle userHandle) {
        mHandler.postDelayed(
        mHandler.post(
                () ->
                () ->
                        delayModeChangedIfNeeded(
                        delayModeChangedIfNeeded(
                                ON_SWITCH_USER_TOKEN,
                                ON_SWITCH_USER_TOKEN,
                                () -> handleSwitchUser(userHandle),
                                () -> handleSwitchUser(userHandle),
                                "onSwitchUser"),
                                "onSwitchUser"));
                ON_SWITCH_USER_TOKEN,
                0);
    }
    }


    private void forceToOffFromModeChange(int currentState, int reason) {
    private void forceToOffFromModeChange(int currentState, int reason) {
@@ -460,8 +449,8 @@ class BluetoothManagerService {
    }
    }


    private void handleAirplaneModeChanged(boolean isAirplaneModeOn) {
    private void handleAirplaneModeChanged(boolean isAirplaneModeOn) {
        synchronized (this) {
        boolean isPersistStateOn = isBluetoothPersistedStateOn();
            if (isBluetoothPersistedStateOn()) {
        if (isPersistStateOn) {
            if (isAirplaneModeOn) {
            if (isAirplaneModeOn) {
                setBluetoothPersistedState(BLUETOOTH_ON_AIRPLANE);
                setBluetoothPersistedState(BLUETOOTH_ON_AIRPLANE);
            } else {
            } else {
@@ -474,17 +463,19 @@ class BluetoothManagerService {
        Log.d(
        Log.d(
                TAG,
                TAG,
                ("handleAirplaneModeChanged(" + isAirplaneModeOn + "):")
                ("handleAirplaneModeChanged(" + isAirplaneModeOn + "):")
                        + (" mEnableExternal=" + mEnableExternal)
                        + (" isPersistStateOn=" + isPersistStateOn)
                        + (" currentState=" + nameForState(currentState)));
                        + (" currentState=" + nameForState(currentState)));


        if (isAirplaneModeOn) {
        if (isAirplaneModeOn) {
            forceToOffFromModeChange(currentState, ENABLE_DISABLE_REASON_AIRPLANE_MODE);
            forceToOffFromModeChange(currentState, ENABLE_DISABLE_REASON_AIRPLANE_MODE);
            } else if (mEnableExternal) {
        } else if (mEnableExternal && currentState != STATE_ON && isPersistStateOn) {
            // isPersistStateOn is checked to prevent race with RESTORE_USER_SETTING
            sendEnableMsg(mQuietEnableExternal, ENABLE_DISABLE_REASON_AIRPLANE_MODE);
            sendEnableMsg(mQuietEnableExternal, ENABLE_DISABLE_REASON_AIRPLANE_MODE);
        } else if (currentState != STATE_ON) {
        } else if (currentState != STATE_ON) {
            autoOnSetupTimer();
            autoOnSetupTimer();
        }
        }
    }
    }
    }


    private void handleSatelliteModeChanged(boolean isSatelliteModeOn) {
    private void handleSatelliteModeChanged(boolean isSatelliteModeOn) {
        final int currentState = mState.get();
        final int currentState = mState.get();
@@ -557,17 +548,15 @@ class BluetoothManagerService {
                                            + (" prevValue=" + prevValue)
                                            + (" prevValue=" + prevValue)
                                            + (" newValue=" + newValue));
                                            + (" newValue=" + newValue));


                            if ((newValue != null)
                            if ((newValue == null)
                                    && (prevValue != null)
                                    || (prevValue == null)
                                    && !prevValue.equals(newValue)) {
                                    || prevValue.equals(newValue)) {
                                mHandler.obtainMessage(
                                return;
                                                MESSAGE_RESTORE_USER_SETTING,
                                                newValue.equals("0")
                                                        ? RESTORE_SETTING_TO_OFF
                                                        : RESTORE_SETTING_TO_ON,
                                                0)
                                        .sendToTarget();
                            }
                            }
                            mHandler.sendEmptyMessage(
                                    newValue.equals("0")
                                            ? MESSAGE_RESTORE_USER_SETTING_OFF
                                            : MESSAGE_RESTORE_USER_SETTING_ON);
                        }
                        }
                    } else if (action.equals(Intent.ACTION_SHUTDOWN)) {
                    } else if (action.equals(Intent.ACTION_SHUTDOWN)) {
                        Log.i(TAG, "Device is shutting down.");
                        Log.i(TAG, "Device is shutting down.");
@@ -1539,24 +1528,26 @@ class BluetoothManagerService {
                    }
                    }
                    break;
                    break;


                case MESSAGE_RESTORE_USER_SETTING:
                case MESSAGE_RESTORE_USER_SETTING_OFF:
                    if ((msg.arg1 == RESTORE_SETTING_TO_OFF) && mEnable) {
                    if (!mEnable) {
                        Log.d(TAG, "MESSAGE_RESTORE_USER_SETTING: set Bluetooth state to disabled");
                        Log.w(TAG, "RESTORE_USER_SETTING_OFF: Unhandled: already disabled");
                        break;
                    }
                    Log.d(TAG, "RESTORE_USER_SETTING_OFF: set Bluetooth state to disabled");
                    setBluetoothPersistedState(BLUETOOTH_OFF);
                    setBluetoothPersistedState(BLUETOOTH_OFF);
                    mEnableExternal = false;
                    mEnableExternal = false;
                    sendDisableMsg(ENABLE_DISABLE_REASON_RESTORE_USER_SETTING);
                    sendDisableMsg(ENABLE_DISABLE_REASON_RESTORE_USER_SETTING);
                    } else if ((msg.arg1 == RESTORE_SETTING_TO_ON) && !mEnable) {
                    break;
                        Log.d(TAG, "MESSAGE_RESTORE_USER_SETTING: set Bluetooth state to enabled");

                case MESSAGE_RESTORE_USER_SETTING_ON:
                    if (mEnable) {
                        Log.w(TAG, "RESTORE_USER_SETTING_ON: Unhandled: already enabled");
                        break;
                    }
                    Log.d(TAG, "RESTORE_USER_SETTING_ON: set Bluetooth state to enabled");
                    mQuietEnableExternal = false;
                    mQuietEnableExternal = false;
                    mEnableExternal = true;
                    mEnableExternal = true;
                    sendEnableMsg(false, ENABLE_DISABLE_REASON_RESTORE_USER_SETTING);
                    sendEnableMsg(false, ENABLE_DISABLE_REASON_RESTORE_USER_SETTING);
                    } else {
                        Log.w(
                                TAG,
                                "MESSAGE_RESTORE_USER_SETTING: Unhandled."
                                        + (" mEnable=" + mEnable)
                                        + (" msg.arg1=" + msg.arg1));
                    }
                    break;
                    break;


                case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
                case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
@@ -1618,14 +1609,10 @@ class BluetoothManagerService {
                    bluetoothStateChangeHandler(prevState, newState);
                    bluetoothStateChangeHandler(prevState, newState);
                    // handle error state transition case from TURNING_ON to OFF
                    // handle error state transition case from TURNING_ON to OFF
                    // unbind and rebind bluetooth service and enable bluetooth
                    // unbind and rebind bluetooth service and enable bluetooth
                    if ((prevState == STATE_BLE_TURNING_ON)
                    if ((prevState == STATE_BLE_TURNING_ON) && (newState == STATE_OFF) && mEnable) {
                            && (newState == STATE_OFF)
                            && mEnable) {
                        recoverBluetoothServiceFromError(false);
                        recoverBluetoothServiceFromError(false);
                    }
                    }
                    if ((prevState == STATE_TURNING_ON)
                    if ((prevState == STATE_TURNING_ON) && (newState == STATE_BLE_ON) && mEnable) {
                            && (newState == STATE_BLE_ON)
                            && mEnable) {
                        recoverBluetoothServiceFromError(true);
                        recoverBluetoothServiceFromError(true);
                    }
                    }
                    // If we tried to enable BT while BT was in the process of shutting down,
                    // If we tried to enable BT while BT was in the process of shutting down,
+74 −4
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.bluetooth;
import static android.bluetooth.BluetoothAdapter.STATE_BLE_ON;
import static android.bluetooth.BluetoothAdapter.STATE_BLE_ON;
import static android.bluetooth.BluetoothAdapter.STATE_OFF;
import static android.bluetooth.BluetoothAdapter.STATE_OFF;
import static android.bluetooth.BluetoothAdapter.STATE_ON;
import static android.bluetooth.BluetoothAdapter.STATE_ON;
import static android.bluetooth.BluetoothAdapter.STATE_TURNING_OFF;
import static android.bluetooth.BluetoothAdapter.STATE_TURNING_ON;
import static android.bluetooth.BluetoothAdapter.STATE_TURNING_ON;


import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_BLUETOOTH_SERVICE_CONNECTED;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_BLUETOOTH_SERVICE_CONNECTED;
@@ -26,7 +27,9 @@ import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_BLUET
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_BLUETOOTH_STATE_CHANGE;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_BLUETOOTH_STATE_CHANGE;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_DISABLE;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_DISABLE;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_ENABLE;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_ENABLE;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_HANDLE_DISABLE_DELAYED;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_RESTART_BLUETOOTH_SERVICE;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_RESTART_BLUETOOTH_SERVICE;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_RESTORE_USER_SETTING_OFF;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_TIMEOUT_BIND;
import static com.android.server.bluetooth.BluetoothManagerService.MESSAGE_TIMEOUT_BIND;


import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertThat;
@@ -101,6 +104,7 @@ public class BluetoothManagerServiceTest {
    }
    }


    private static final int STATE_BLE_TURNING_ON = 14; // can't find the symbol because hidden api
    private static final int STATE_BLE_TURNING_ON = 14; // can't find the symbol because hidden api
    private static final int STATE_BLE_TURNING_OFF = 16; // can't find the symbol because hidden api


    BluetoothManagerService mManagerService;
    BluetoothManagerService mManagerService;


@@ -185,6 +189,7 @@ public class BluetoothManagerServiceTest {


        doReturn(mAdapterBinder).when(mBluetoothServerProxy).createAdapterBinder(any());
        doReturn(mAdapterBinder).when(mBluetoothServerProxy).createAdapterBinder(any());
        doReturn(mAdapterService).when(mAdapterBinder).getAdapterBinder();
        doReturn(mAdapterService).when(mAdapterBinder).getAdapterBinder();
        doReturn(mBinder).when(mAdapterService).asBinder();


        doReturn(mock(Intent.class))
        doReturn(mock(Intent.class))
                .when(mContext)
                .when(mContext)
@@ -238,6 +243,17 @@ public class BluetoothManagerServiceTest {
                        });
                        });
    }
    }


    private void discardMessage(int... what) {
        IntStream.of(what)
                .forEach(
                        w -> {
                            Message msg = mLooper.nextMessage();
                            assertThat(msg).isNotNull();
                            assertThat(msg.what).isEqualTo(w);
                            // Drop the message
                        });
    }

    @Test
    @Test
    public void onUserRestrictionsChanged_disallowBluetooth_onlySendDisableMessageOnSystemUser()
    public void onUserRestrictionsChanged_disallowBluetooth_onlySendDisableMessageOnSystemUser()
            throws InterruptedException {
            throws InterruptedException {
@@ -358,6 +374,22 @@ public class BluetoothManagerServiceTest {
        return btCallback;
        return btCallback;
    }
    }


    private void transition_onToBleOn(IBluetoothCallback btCallback) throws Exception {
        verify(mAdapterBinder).onToBleOn(any());

        btCallback.onBluetoothStateChange(STATE_TURNING_OFF, STATE_BLE_ON);
        syncHandler(MESSAGE_BLUETOOTH_STATE_CHANGE);
    }

    private void transition_onToOff(IBluetoothCallback btCallback) throws Exception {
        transition_onToBleOn(btCallback);
        verify(mAdapterBinder).bleOnToOff(any());

        // When all the profile are started, adapterService consider it is ON
        btCallback.onBluetoothStateChange(STATE_BLE_TURNING_OFF, STATE_OFF);
        syncHandler(MESSAGE_BLUETOOTH_STATE_CHANGE);
    }

    @Test
    @Test
    public void enable_whileTurningToBleOn_shouldEnable() throws Exception {
    public void enable_whileTurningToBleOn_shouldEnable() throws Exception {
        mManagerService.enableBle("enable_whileTurningToBleOn_shouldEnable", mBinder);
        mManagerService.enableBle("enable_whileTurningToBleOn_shouldEnable", mBinder);
@@ -450,9 +482,47 @@ public class BluetoothManagerServiceTest {
        assertThat(mManagerService.getState()).isEqualTo(STATE_OFF);
        assertThat(mManagerService.getState()).isEqualTo(STATE_OFF);


        mLooper.moveTimeForward(120_000);
        mLooper.moveTimeForward(120_000);
        Message msg = mLooper.nextMessage();
        discardMessage(MESSAGE_RESTART_BLUETOOTH_SERVICE);
        assertThat(msg).isNotNull();
    }
        assertThat(msg.what).isEqualTo(MESSAGE_RESTART_BLUETOOTH_SERVICE);

        // Discard the msg without executing it
    @Test
    public void disableAirplane_whenNothing_startBluetooth() throws Exception {
        doReturn(BluetoothManagerService.BLUETOOTH_ON_BLUETOOTH)
                .when(mBluetoothServerProxy)
                .getBluetoothPersistedState(any(), anyInt());
        mManagerService.enable("disableAirplane_whenNothing_startBluetooth");
        discardMessage(MESSAGE_ENABLE);

        mManagerService.onAirplaneModeChanged(false);
        discardMessage(MESSAGE_ENABLE);
    }

    @Test
    public void disableAirplane_whenFactoryReset_doesNotStartBluetooth() throws Exception {
        doAnswer(
                        invocation -> {
                            IBinder.DeathRecipient recipient = invocation.getArgument(0);
                            recipient.binderDied();
                            return null;
                        })
                .when(mBinder)
                .linkToDeath(any(), anyInt());

        mManagerService.enable("test_offToOn");
        syncHandler(MESSAGE_ENABLE);
        IBluetoothCallback btCallback = transition_offToOn();
        assertThat(mManagerService.getState()).isEqualTo(STATE_ON);

        mManagerService.mHandler.sendEmptyMessage(MESSAGE_RESTORE_USER_SETTING_OFF);
        syncHandler(MESSAGE_RESTORE_USER_SETTING_OFF);
        syncHandler(MESSAGE_DISABLE);
        mLooper.moveTimeForward(BluetoothManagerService.ENABLE_DISABLE_DELAY_MS);
        syncHandler(MESSAGE_HANDLE_DISABLE_DELAYED);
        mLooper.moveTimeForward(BluetoothManagerService.ENABLE_DISABLE_DELAY_MS);
        syncHandler(MESSAGE_HANDLE_DISABLE_DELAYED);
        transition_onToOff(btCallback);

        mManagerService.onAirplaneModeChanged(false);
        assertThat(mLooper.nextMessage()).isNull(); // Must not create a MESSAGE_ENABLE
    }
    }
}
}