1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.app; 18 19 import android.app.GameManager; 20 import android.os.FileUtils; 21 import android.util.ArrayMap; 22 import android.util.ArraySet; 23 import android.util.AtomicFile; 24 import android.util.Slog; 25 import android.util.Xml; 26 27 import com.android.internal.annotations.VisibleForTesting; 28 import com.android.internal.util.XmlUtils; 29 import com.android.modules.utils.TypedXmlPullParser; 30 import com.android.modules.utils.TypedXmlSerializer; 31 import com.android.server.app.GameManagerService.GamePackageConfiguration; 32 import com.android.server.app.GameManagerService.GamePackageConfiguration.GameModeConfiguration; 33 34 import org.xmlpull.v1.XmlPullParser; 35 import org.xmlpull.v1.XmlPullParserException; 36 37 import java.io.File; 38 import java.io.FileInputStream; 39 import java.io.FileOutputStream; 40 import java.io.IOException; 41 42 /** 43 * Persists all GameService related settings. 44 * 45 * @hide 46 */ 47 public class GameManagerSettings { 48 public static final String TAG = "GameManagerService_GameManagerSettings"; 49 // The XML file follows the below format: 50 // <?xml> 51 // <packages> 52 // <package name="" gameMode=""> 53 // <gameModeConfig gameMode="" fps="" scaling="" useAngle="" loadingBoost=""> 54 // </gameModeConfig> 55 // ... 56 // </package> 57 // ... 58 // </packages> 59 private static final String GAME_SERVICE_FILE_NAME = "game-manager-service.xml"; 60 61 private static final String TAG_PACKAGE = "package"; 62 private static final String TAG_PACKAGES = "packages"; 63 private static final String TAG_GAME_MODE_CONFIG = "gameModeConfig"; 64 65 private static final String ATTR_NAME = "name"; 66 private static final String ATTR_GAME_MODE = "gameMode"; 67 private static final String ATTR_SCALING = "scaling"; 68 private static final String ATTR_FPS = "fps"; 69 private static final String ATTR_USE_ANGLE = "useAngle"; 70 private static final String ATTR_LOADING_BOOST_DURATION = "loadingBoost"; 71 72 private final File mSystemDir; 73 @VisibleForTesting 74 final AtomicFile mSettingsFile; 75 76 // PackageName -> GameMode 77 private final ArrayMap<String, Integer> mGameModes = new ArrayMap<>(); 78 // PackageName -> GamePackageConfiguration 79 private final ArrayMap<String, GamePackageConfiguration> mConfigOverrides = new ArrayMap<>(); 80 GameManagerSettings(File dataDir)81 GameManagerSettings(File dataDir) { 82 mSystemDir = new File(dataDir, "system"); 83 mSystemDir.mkdirs(); 84 FileUtils.setPermissions(mSystemDir.toString(), 85 FileUtils.S_IRWXU | FileUtils.S_IRWXG 86 | FileUtils.S_IROTH | FileUtils.S_IXOTH, 87 -1, -1); 88 mSettingsFile = new AtomicFile(new File(mSystemDir, GAME_SERVICE_FILE_NAME)); 89 } 90 91 /** 92 * Returns the game mode of a given package. 93 * This operation must be synced with an external lock. 94 */ getGameModeLocked(String packageName)95 int getGameModeLocked(String packageName) { 96 if (mGameModes.containsKey(packageName)) { 97 final int gameMode = mGameModes.get(packageName); 98 if (gameMode == GameManager.GAME_MODE_UNSUPPORTED) { 99 // force replace cached UNSUPPORTED mode with STANDARD starting in U 100 return GameManager.GAME_MODE_STANDARD; 101 } 102 return gameMode; 103 } 104 return GameManager.GAME_MODE_STANDARD; 105 } 106 107 /** 108 * Sets the game mode of a given package. 109 * This operation must be synced with an external lock. 110 */ setGameModeLocked(String packageName, int gameMode)111 void setGameModeLocked(String packageName, int gameMode) { 112 mGameModes.put(packageName, gameMode); 113 } 114 115 /** 116 * Removes all game settings of a given package. 117 * This operation must be synced with an external lock. 118 */ removeGameLocked(String packageName)119 void removeGameLocked(String packageName) { 120 mGameModes.remove(packageName); 121 mConfigOverrides.remove(packageName); 122 } 123 124 /** 125 * Returns the game config override of a given package or null if absent. 126 * This operation must be synced with an external lock. 127 */ getConfigOverrideLocked(String packageName)128 GamePackageConfiguration getConfigOverrideLocked(String packageName) { 129 return mConfigOverrides.get(packageName); 130 } 131 132 /** 133 * Sets the game config override of a given package. 134 * This operation must be synced with an external lock. 135 */ setConfigOverrideLocked(String packageName, GamePackageConfiguration configOverride)136 void setConfigOverrideLocked(String packageName, GamePackageConfiguration configOverride) { 137 mConfigOverrides.put(packageName, configOverride); 138 } 139 140 /** 141 * Removes the game mode config override of a given package. 142 * This operation must be synced with an external lock. 143 */ removeConfigOverrideLocked(String packageName)144 void removeConfigOverrideLocked(String packageName) { 145 mConfigOverrides.remove(packageName); 146 } 147 148 /** 149 * Writes all current game service settings into disk. 150 * This operation must be synced with an external lock. 151 */ writePersistentDataLocked()152 void writePersistentDataLocked() { 153 FileOutputStream fstr = null; 154 try { 155 fstr = mSettingsFile.startWrite(); 156 157 final TypedXmlSerializer serializer = Xml.resolveSerializer(fstr); 158 serializer.startDocument(null, true); 159 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 160 161 serializer.startTag(null, TAG_PACKAGES); 162 final ArraySet<String> packageNames = new ArraySet<>(mGameModes.keySet()); 163 packageNames.addAll(mConfigOverrides.keySet()); 164 for (String packageName : packageNames) { 165 serializer.startTag(null, TAG_PACKAGE); 166 serializer.attribute(null, ATTR_NAME, packageName); 167 if (mGameModes.containsKey(packageName)) { 168 serializer.attributeInt(null, ATTR_GAME_MODE, mGameModes.get(packageName)); 169 } 170 writeGameModeConfigTags(serializer, mConfigOverrides.get(packageName)); 171 serializer.endTag(null, TAG_PACKAGE); 172 } 173 serializer.endTag(null, TAG_PACKAGES); 174 175 serializer.endDocument(); 176 177 mSettingsFile.finishWrite(fstr); 178 179 FileUtils.setPermissions(mSettingsFile.toString(), 180 FileUtils.S_IRUSR | FileUtils.S_IWUSR 181 | FileUtils.S_IRGRP | FileUtils.S_IWGRP, 182 -1, -1); 183 return; 184 } catch (java.io.IOException e) { 185 mSettingsFile.failWrite(fstr); 186 Slog.wtf(TAG, "Unable to write game manager service settings, " 187 + "current changes will be lost at reboot", e); 188 } 189 } 190 writeGameModeConfigTags(TypedXmlSerializer serializer, GamePackageConfiguration config)191 private void writeGameModeConfigTags(TypedXmlSerializer serializer, 192 GamePackageConfiguration config) throws IOException { 193 if (config == null) { 194 return; 195 } 196 final int[] gameModes = config.getAvailableGameModes(); 197 for (final int mode : gameModes) { 198 final GameModeConfiguration modeConfig = config.getGameModeConfiguration(mode); 199 if (modeConfig != null) { 200 serializer.startTag(null, TAG_GAME_MODE_CONFIG); 201 serializer.attributeInt(null, ATTR_GAME_MODE, mode); 202 serializer.attributeBoolean(null, ATTR_USE_ANGLE, modeConfig.getUseAngle()); 203 serializer.attribute(null, ATTR_FPS, modeConfig.getFpsStr()); 204 serializer.attributeFloat(null, ATTR_SCALING, modeConfig.getScaling()); 205 serializer.attributeInt(null, ATTR_LOADING_BOOST_DURATION, 206 modeConfig.getLoadingBoostDuration()); 207 serializer.endTag(null, TAG_GAME_MODE_CONFIG); 208 } 209 } 210 } 211 212 /** 213 * Reads game service settings from the disk. 214 * This operation must be synced with an external lock. 215 */ readPersistentDataLocked()216 boolean readPersistentDataLocked() { 217 mGameModes.clear(); 218 219 if (!mSettingsFile.exists()) { 220 Slog.v(TAG, "Settings file doesn't exist, skip reading"); 221 return false; 222 } 223 224 try (FileInputStream str = mSettingsFile.openRead()) { 225 final TypedXmlPullParser parser = Xml.resolvePullParser(str); 226 int type; 227 while ((type = parser.next()) != XmlPullParser.START_TAG 228 && type != XmlPullParser.END_DOCUMENT) { 229 // Do nothing 230 } 231 if (type != XmlPullParser.START_TAG) { 232 Slog.wtf(TAG, "No start tag found in game manager settings"); 233 return false; 234 } 235 236 int outerDepth = parser.getDepth(); 237 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 238 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 239 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 240 continue; 241 } 242 243 String tagName = parser.getName(); 244 if (type == XmlPullParser.START_TAG && TAG_PACKAGE.equals(tagName)) { 245 readPackage(parser); 246 } else { 247 XmlUtils.skipCurrentTag(parser); 248 Slog.w(TAG, "Unknown element under packages tag: " + tagName + " with type: " 249 + type); 250 } 251 } 252 } catch (XmlPullParserException | java.io.IOException e) { 253 Slog.wtf(TAG, "Error reading game manager settings", e); 254 return false; 255 } 256 return true; 257 } 258 259 // this must be called on tag of type START_TAG. readPackage(TypedXmlPullParser parser)260 private void readPackage(TypedXmlPullParser parser) throws XmlPullParserException, 261 IOException { 262 final String name = parser.getAttributeValue(null, ATTR_NAME); 263 if (name == null) { 264 Slog.wtf(TAG, "No package name found in package tag"); 265 XmlUtils.skipCurrentTag(parser); 266 return; 267 } 268 try { 269 final int gameMode = parser.getAttributeInt(null, ATTR_GAME_MODE); 270 mGameModes.put(name, gameMode); 271 } catch (XmlPullParserException e) { 272 Slog.v(TAG, "No game mode selected by user for package" + name); 273 } 274 final int packageTagDepth = parser.getDepth(); 275 int type; 276 final GamePackageConfiguration config = new GamePackageConfiguration(name); 277 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 278 && (type != XmlPullParser.END_TAG 279 || parser.getDepth() > packageTagDepth)) { 280 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 281 continue; 282 } 283 final String tagName = parser.getName(); 284 if (type == XmlPullParser.START_TAG && TAG_GAME_MODE_CONFIG.equals(tagName)) { 285 readGameModeConfig(parser, config); 286 } else { 287 XmlUtils.skipCurrentTag(parser); 288 Slog.w(TAG, "Unknown element under package tag: " + tagName + " with type: " 289 + type); 290 } 291 } 292 if (config.hasActiveGameModeConfig()) { 293 mConfigOverrides.put(name, config); 294 } 295 } 296 297 // this must be called on tag of type START_TAG. readGameModeConfig(TypedXmlPullParser parser, GamePackageConfiguration config)298 private void readGameModeConfig(TypedXmlPullParser parser, GamePackageConfiguration config) { 299 final int gameMode; 300 try { 301 gameMode = parser.getAttributeInt(null, ATTR_GAME_MODE); 302 } catch (XmlPullParserException e) { 303 Slog.wtf(TAG, "Invalid game mode value in config tag: " + parser.getAttributeValue(null, 304 ATTR_GAME_MODE), e); 305 return; 306 } 307 308 final GameModeConfiguration modeConfig = config.getOrAddDefaultGameModeConfiguration( 309 gameMode); 310 try { 311 final float scaling = parser.getAttributeFloat(null, ATTR_SCALING); 312 modeConfig.setScaling(scaling); 313 } catch (XmlPullParserException e) { 314 final String rawScaling = parser.getAttributeValue(null, ATTR_SCALING); 315 if (rawScaling != null) { 316 Slog.wtf(TAG, "Invalid scaling value in config tag: " + rawScaling, e); 317 } 318 } 319 320 final String fps = parser.getAttributeValue(null, ATTR_FPS); 321 modeConfig.setFpsStr(fps != null ? fps : GameModeConfiguration.DEFAULT_FPS); 322 323 try { 324 final boolean useAngle = parser.getAttributeBoolean(null, ATTR_USE_ANGLE); 325 modeConfig.setUseAngle(useAngle); 326 } catch (XmlPullParserException e) { 327 final String rawUseAngle = parser.getAttributeValue(null, ATTR_USE_ANGLE); 328 if (rawUseAngle != null) { 329 Slog.wtf(TAG, "Invalid useAngle value in config tag: " + rawUseAngle, e); 330 } 331 } 332 try { 333 final int loadingBoostDuration = parser.getAttributeInt(null, 334 ATTR_LOADING_BOOST_DURATION); 335 modeConfig.setLoadingBoostDuration(loadingBoostDuration); 336 } catch (XmlPullParserException e) { 337 final String rawLoadingBoost = parser.getAttributeValue(null, 338 ATTR_LOADING_BOOST_DURATION); 339 if (rawLoadingBoost != null) { 340 Slog.wtf(TAG, "Invalid loading boost in config tag: " + rawLoadingBoost, e); 341 } 342 } 343 } 344 } 345