Fix the bug that user setting is removed when switching
The user is only started once and will not trigger onUserStarting in
subsequent switching, we should not remove it from the mapping otherwise
all the package configuration will get lost and game mode can't be set.
Bug: 258724297
Test: atest GameManagerServiceTests
Change-Id: I79fecd6f0ef5d751ad4fbadb8cb3cc43df8f3bce
diff --git a/services/core/java/com/android/server/app/GameManagerService.java b/services/core/java/com/android/server/app/GameManagerService.java
index 31d707d..64f2aa3 100644
--- a/services/core/java/com/android/server/app/GameManagerService.java
+++ b/services/core/java/com/android/server/app/GameManagerService.java
@@ -1314,16 +1314,9 @@
void onUserSwitching(TargetUser from, TargetUser to) {
final int toUserId = to.getUserIdentifier();
- if (from != null) {
- synchronized (mLock) {
- final int fromUserId = from.getUserIdentifier();
- if (mSettings.containsKey(fromUserId)) {
- sendUserMessage(fromUserId, REMOVE_SETTINGS, "ON_USER_SWITCHING",
- 0 /*delayMillis*/);
- }
- }
- }
-
+ // we want to re-populate the setting when switching user as the device config may have
+ // changed, which will only update for the previous user, see
+ // DeviceConfigListener#onPropertiesChanged.
sendUserMessage(toUserId, POPULATE_GAME_MODE_SETTINGS, "ON_USER_SWITCHING",
0 /*delayMillis*/);
@@ -1392,8 +1385,9 @@
Slog.v(TAG, "Package configuration not found for " + packageName);
return;
}
+ } else {
+ updateFps(packageConfig, packageName, gameMode, userId);
}
- updateFps(packageConfig, packageName, gameMode, userId);
updateUseAngle(packageName, gameMode);
}
diff --git a/services/tests/mockingservicestests/src/com/android/server/app/GameManagerServiceTests.java b/services/tests/mockingservicestests/src/com/android/server/app/GameManagerServiceTests.java
index d78f6d83..24e5175 100644
--- a/services/tests/mockingservicestests/src/com/android/server/app/GameManagerServiceTests.java
+++ b/services/tests/mockingservicestests/src/com/android/server/app/GameManagerServiceTests.java
@@ -1507,6 +1507,39 @@
}
@Test
+ public void testSwitchUser() {
+ mockManageUsersGranted();
+ mockModifyGameModeGranted();
+
+ mockDeviceConfigBattery();
+ final Context context = InstrumentationRegistry.getContext();
+ GameManagerService gameManagerService = new GameManagerService(mMockContext,
+ mTestLooper.getLooper(), context.getFilesDir());
+ startUser(gameManagerService, USER_ID_1);
+ startUser(gameManagerService, USER_ID_2);
+ gameManagerService.setGameMode(mPackageName, GameManager.GAME_MODE_BATTERY, USER_ID_1);
+ checkReportedModes(gameManagerService, GameManager.GAME_MODE_STANDARD,
+ GameManager.GAME_MODE_BATTERY);
+ assertEquals(gameManagerService.getGameMode(mPackageName, USER_ID_1),
+ GameManager.GAME_MODE_BATTERY);
+
+ mockDeviceConfigAll();
+ switchUser(gameManagerService, USER_ID_1, USER_ID_2);
+ assertEquals(gameManagerService.getGameMode(mPackageName, USER_ID_2),
+ GameManager.GAME_MODE_STANDARD);
+ checkReportedModes(gameManagerService, GameManager.GAME_MODE_STANDARD,
+ GameManager.GAME_MODE_BATTERY, GameManager.GAME_MODE_PERFORMANCE);
+ gameManagerService.setGameMode(mPackageName, GameManager.GAME_MODE_PERFORMANCE, USER_ID_2);
+ gameManagerService.setGameMode(mPackageName, GameManager.GAME_MODE_BATTERY, USER_ID_1);
+
+ switchUser(gameManagerService, USER_ID_2, USER_ID_1);
+ checkReportedModes(gameManagerService, GameManager.GAME_MODE_STANDARD,
+ GameManager.GAME_MODE_BATTERY, GameManager.GAME_MODE_PERFORMANCE);
+ gameManagerService.setGameMode(mPackageName, GameManager.GAME_MODE_PERFORMANCE, USER_ID_2);
+ gameManagerService.setGameMode(mPackageName, GameManager.GAME_MODE_BATTERY, USER_ID_1);
+ }
+
+ @Test
public void testUpdateResolutionScalingFactor() {
mockModifyGameModeGranted();
mockDeviceConfigBattery();