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.AtomicFile; 23 import android.util.Slog; 24 import android.util.TypedXmlPullParser; 25 import android.util.TypedXmlSerializer; 26 import android.util.Xml; 27 28 import com.android.internal.annotations.VisibleForTesting; 29 import com.android.internal.util.XmlUtils; 30 31 import org.xmlpull.v1.XmlPullParser; 32 import org.xmlpull.v1.XmlPullParserException; 33 34 import java.io.File; 35 import java.io.FileInputStream; 36 import java.io.FileOutputStream; 37 import java.io.IOException; 38 import java.util.Map; 39 40 /** 41 * Persists all GameService related settings. 42 * @hide 43 */ 44 public class GameManagerSettings { 45 46 // The XML file follows the below format: 47 // <?xml> 48 // <packages> 49 // <package></package> 50 // ... 51 // </packages> 52 private static final String GAME_SERVICE_FILE_NAME = "game-manager-service.xml"; 53 54 private static final String TAG_PACKAGE = "package"; 55 private static final String TAG_PACKAGES = "packages"; 56 private static final String ATTR_NAME = "name"; 57 private static final String ATTR_GAME_MODE = "gameMode"; 58 59 private final File mSystemDir; 60 @VisibleForTesting 61 final AtomicFile mSettingsFile; 62 63 // PackageName -> GameMode 64 private final ArrayMap<String, Integer> mGameModes = new ArrayMap<>(); 65 GameManagerSettings(File dataDir)66 GameManagerSettings(File dataDir) { 67 mSystemDir = new File(dataDir, "system"); 68 mSystemDir.mkdirs(); 69 FileUtils.setPermissions(mSystemDir.toString(), 70 FileUtils.S_IRWXU | FileUtils.S_IRWXG 71 | FileUtils.S_IROTH | FileUtils.S_IXOTH, 72 -1, -1); 73 mSettingsFile = new AtomicFile(new File(mSystemDir, GAME_SERVICE_FILE_NAME)); 74 } 75 76 /** 77 * Return the game mode of a given package. 78 * This operation must be synced with an external lock. 79 */ getGameModeLocked(String packageName)80 int getGameModeLocked(String packageName) { 81 if (mGameModes.containsKey(packageName)) { 82 return mGameModes.get(packageName); 83 } 84 return GameManager.GAME_MODE_UNSUPPORTED; 85 } 86 87 /** 88 * Set the game mode of a given package. 89 * This operation must be synced with an external lock. 90 */ setGameModeLocked(String packageName, int gameMode)91 void setGameModeLocked(String packageName, int gameMode) { 92 mGameModes.put(packageName, gameMode); 93 } 94 95 /** 96 * Remove the game mode of a given package. 97 * This operation must be synced with an external lock. 98 */ removeGame(String packageName)99 void removeGame(String packageName) { 100 mGameModes.remove(packageName); 101 } 102 103 /** 104 * Write all current game service settings into disk. 105 * This operation must be synced with an external lock. 106 */ writePersistentDataLocked()107 void writePersistentDataLocked() { 108 FileOutputStream fstr = null; 109 try { 110 fstr = mSettingsFile.startWrite(); 111 112 final TypedXmlSerializer serializer = Xml.resolveSerializer(fstr); 113 serializer.startDocument(null, true); 114 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 115 116 serializer.startTag(null, TAG_PACKAGES); 117 for (Map.Entry<String, Integer> entry : mGameModes.entrySet()) { 118 serializer.startTag(null, TAG_PACKAGE); 119 serializer.attribute(null, ATTR_NAME, entry.getKey()); 120 serializer.attributeInt(null, ATTR_GAME_MODE, entry.getValue()); 121 serializer.endTag(null, TAG_PACKAGE); 122 } 123 serializer.endTag(null, TAG_PACKAGES); 124 125 serializer.endDocument(); 126 127 mSettingsFile.finishWrite(fstr); 128 129 FileUtils.setPermissions(mSettingsFile.toString(), 130 FileUtils.S_IRUSR | FileUtils.S_IWUSR 131 | FileUtils.S_IRGRP | FileUtils.S_IWGRP, 132 -1, -1); 133 return; 134 } catch (java.io.IOException e) { 135 mSettingsFile.failWrite(fstr); 136 Slog.wtf(GameManagerService.TAG, "Unable to write game manager service settings, " 137 + "current changes will be lost at reboot", e); 138 } 139 } 140 141 /** 142 * Read game service settings from the disk. 143 * This operation must be synced with an external lock. 144 */ readPersistentDataLocked()145 boolean readPersistentDataLocked() { 146 mGameModes.clear(); 147 148 if (!mSettingsFile.exists()) { 149 Slog.v(GameManagerService.TAG, "Settings file doesn't exists, skip reading"); 150 return false; 151 } 152 153 try { 154 final FileInputStream str = mSettingsFile.openRead(); 155 156 final TypedXmlPullParser parser = Xml.resolvePullParser(str); 157 int type; 158 while ((type = parser.next()) != XmlPullParser.START_TAG 159 && type != XmlPullParser.END_DOCUMENT) { 160 // Do nothing 161 } 162 if (type != XmlPullParser.START_TAG) { 163 Slog.wtf(GameManagerService.TAG, 164 "No start tag found in package manager settings"); 165 return false; 166 } 167 168 int outerDepth = parser.getDepth(); 169 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 170 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 171 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 172 continue; 173 } 174 175 String tagName = parser.getName(); 176 if (tagName.equals(TAG_PACKAGE)) { 177 readPackage(parser); 178 } else { 179 Slog.w(GameManagerService.TAG, "Unknown element: " + parser.getName()); 180 XmlUtils.skipCurrentTag(parser); 181 } 182 } 183 } catch (XmlPullParserException | java.io.IOException e) { 184 Slog.wtf(GameManagerService.TAG, "Error reading package manager settings", e); 185 return false; 186 } 187 188 return true; 189 } 190 readPackage(TypedXmlPullParser parser)191 private void readPackage(TypedXmlPullParser parser) throws XmlPullParserException, 192 IOException { 193 String name = null; 194 int gameMode = GameManager.GAME_MODE_UNSUPPORTED; 195 try { 196 name = parser.getAttributeValue(null, ATTR_NAME); 197 gameMode = parser.getAttributeInt(null, ATTR_GAME_MODE); 198 } catch (XmlPullParserException e) { 199 Slog.wtf(GameManagerService.TAG, "Error reading game mode", e); 200 } 201 if (name != null) { 202 mGameModes.put(name, gameMode); 203 } else { 204 XmlUtils.skipCurrentTag(parser); 205 } 206 } 207 } 208