Loading service/src/com/android/server/bluetooth/BluetoothManagerService.java +8 −17 Original line number Diff line number Diff line Loading @@ -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); } } Loading Loading @@ -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); } /** Loading Loading @@ -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"); } Loading Loading @@ -1281,7 +1272,7 @@ class BluetoothManagerService { } if (persist) { persistBluetoothSetting(BLUETOOTH_OFF); setBluetoothPersistedState(BLUETOOTH_OFF); } mEnableExternal = false; sendDisableMsg( Loading Loading @@ -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 Loading Loading @@ -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, Loading service/src/com/android/server/bluetooth/BluetoothServerProxy.java +12 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); } } } service/tests/src/com/android/server/bluetooth/BluetoothManagerServiceTest.java +15 −10 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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()); Loading Loading @@ -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); Loading @@ -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); Loading Loading
service/src/com/android/server/bluetooth/BluetoothManagerService.java +8 −17 Original line number Diff line number Diff line Loading @@ -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); } } Loading Loading @@ -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); } /** Loading Loading @@ -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"); } Loading Loading @@ -1281,7 +1272,7 @@ class BluetoothManagerService { } if (persist) { persistBluetoothSetting(BLUETOOTH_OFF); setBluetoothPersistedState(BLUETOOTH_OFF); } mEnableExternal = false; sendDisableMsg( Loading Loading @@ -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 Loading Loading @@ -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, Loading
service/src/com/android/server/bluetooth/BluetoothServerProxy.java +12 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); } } }
service/tests/src/com/android/server/bluetooth/BluetoothManagerServiceTest.java +15 −10 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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()); Loading Loading @@ -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); Loading @@ -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); Loading