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

Commit 96b3032b authored by William Escande's avatar William Escande
Browse files

SystemServer Use wrapper for persistent override

Bug: 328846833
Test: atest ServiceBluetoothTests
Flag: Exempt, mechanical refactor
Change-Id: I1e0716179533eee927d3e2186055950d4021e0d9
parent a6a69c2d
Loading
Loading
Loading
Loading
+8 −17
Original line number Diff line number Diff line
@@ -482,9 +482,9 @@ class BluetoothManagerService {
        synchronized (this) {
            if (isBluetoothPersistedStateOn()) {
                if (isAirplaneModeOn) {
                    persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE);
                    setBluetoothPersistedState(BLUETOOTH_ON_AIRPLANE);
                } else {
                    persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
                    setBluetoothPersistedState(BLUETOOTH_ON_BLUETOOTH);
                }
            }

@@ -803,17 +803,8 @@ class BluetoothManagerService {
        return state == BLUETOOTH_ON_BLUETOOTH;
    }

    /** Save the Bluetooth on/off state */
    private void persistBluetoothSetting(int value) {
        Log.i(TAG, "Persisting Bluetooth Setting: " + value);
        // waive WRITE_SECURE_SETTINGS permission check
        final long callingIdentity = Binder.clearCallingIdentity();
        try {
            Settings.Global.putInt(
                    mContext.getContentResolver(), Settings.Global.BLUETOOTH_ON, value);
        } finally {
            Binder.restoreCallingIdentity(callingIdentity);
        }
    private void setBluetoothPersistedState(int state) {
        BluetoothServerProxy.getInstance().setBluetoothPersistedState(mContentResolver, state);
    }

    /**
@@ -1152,7 +1143,7 @@ class BluetoothManagerService {
                Log.i(TAG, "continueFromBleOnState: Starting br edr");
                // This triggers transition to STATE_ON
                mAdapter.startBrEdr(mContext.getAttributionSource());
                persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
                setBluetoothPersistedState(BLUETOOTH_ON_BLUETOOTH);
            } else {
                Log.i(TAG, "continueFromBleOnState: Staying in BLE_ON");
            }
@@ -1281,7 +1272,7 @@ class BluetoothManagerService {
            }

            if (persist) {
                persistBluetoothSetting(BLUETOOTH_OFF);
                setBluetoothPersistedState(BLUETOOTH_OFF);
            }
            mEnableExternal = false;
            sendDisableMsg(
@@ -1609,7 +1600,7 @@ class BluetoothManagerService {
                    mEnable = true;

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

                    // Use service interface to get the exact state
@@ -1759,7 +1750,7 @@ class BluetoothManagerService {
                case MESSAGE_RESTORE_USER_SETTING:
                    if ((msg.arg1 == RESTORE_SETTING_TO_OFF) && mEnable) {
                        Log.d(TAG, "MESSAGE_RESTORE_USER_SETTING: set Bluetooth state to disabled");
                        persistBluetoothSetting(BLUETOOTH_OFF);
                        setBluetoothPersistedState(BLUETOOTH_OFF);
                        mEnableExternal = false;
                        sendDisableMsg(
                                BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTORE_USER_SETTING,
+12 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.bluetooth;

import android.annotation.NonNull;
import android.content.ContentResolver;
import android.os.Binder;
import android.os.IBinder;
import android.provider.Settings;

@@ -70,4 +71,15 @@ class BluetoothServerProxy {
    int getBluetoothPersistedState(ContentResolver resolver, int defaultValue) {
        return Settings.Global.getInt(resolver, Settings.Global.BLUETOOTH_ON, defaultValue);
    }

    void setBluetoothPersistedState(ContentResolver resolver, int state) {
        Log.i(TAG, "setBluetoothPersistedState(" + state + ")");
        // waive WRITE_SECURE_SETTINGS permission check
        final long callingIdentity = Binder.clearCallingIdentity();
        try {
            Settings.Global.putInt(resolver, Settings.Global.BLUETOOTH_ON, state);
        } finally {
            Binder.restoreCallingIdentity(callingIdentity);
        }
    }
}
+15 −10
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
@@ -184,6 +185,20 @@ public class BluetoothManagerServiceTest {
        doReturn("00:11:22:33:44:55")
                .when(mBluetoothServerProxy)
                .settingsSecureGetString(any(), eq(Settings.Secure.BLUETOOTH_ADDRESS));
        // Set persisted state to BLUETOOTH_OFF to not generate unwanted behavior when starting test
        doReturn(BluetoothManagerService.BLUETOOTH_OFF)
                .when(mBluetoothServerProxy)
                .getBluetoothPersistedState(any(), anyInt());

        doAnswer(
                        inv -> {
                            doReturn(inv.getArguments()[1])
                                    .when(mBluetoothServerProxy)
                                    .getBluetoothPersistedState(any(), anyInt());
                            return null;
                        })
                .when(mBluetoothServerProxy)
                .setBluetoothPersistedState(any(), anyInt());

        // Test is not allowed to send broadcast as Bluetooth. doNothing Prevent SecurityException
        doNothing().when(mContext).sendBroadcastAsUser(any(), any(), any(), any());
@@ -395,11 +410,6 @@ public class BluetoothManagerServiceTest {

    @Test
    public void offToBleOn() throws Exception {
        // In order to go to BLE only, the persisted state should be BLUETOOTH_OFF
        doReturn(BluetoothManagerService.BLUETOOTH_OFF)
                .when(mBluetoothServerProxy)
                .getBluetoothPersistedState(any(), anyInt());

        mManagerService.enableBle("test_offToBleOn", mBinder);
        syncHandler(MESSAGE_ENABLE);

@@ -412,11 +422,6 @@ public class BluetoothManagerServiceTest {

    @Test
    public void offToOn() throws Exception {
        // In order to not go to BLE only, the persisted state should not be BLUETOOTH_OFF
        doReturn(BluetoothManagerService.BLUETOOTH_ON_BLUETOOTH)
                .when(mBluetoothServerProxy)
                .getBluetoothPersistedState(any(), anyInt());

        mManagerService.enable("test_offToOn");
        syncHandler(MESSAGE_ENABLE);