blob: 26264e5da147edc7c960dfd3f3a3156d2bc0ce5c [file] [log] [blame]
Craig Mautnerc2c0a612014-02-20 20:25:41 -08001/*
2 * Copyright (C) 2014 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
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070017package com.android.server.am;
18
Wale Ogunwalee23149f2015-03-06 15:39:44 -080019import static com.android.server.am.ActivityManagerDebugConfig.*;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070020import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080021
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070022import java.io.File;
23import java.io.FileInputStream;
24import java.io.FileOutputStream;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010025import java.nio.charset.StandardCharsets;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070026import java.util.HashMap;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070027import java.util.Iterator;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070028import java.util.Map;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070029
30import org.xmlpull.v1.XmlPullParser;
31import org.xmlpull.v1.XmlPullParserException;
32import org.xmlpull.v1.XmlSerializer;
33
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070034import com.android.internal.util.FastXmlSerializer;
35
36import android.app.ActivityManager;
37import android.app.AppGlobals;
38import android.content.pm.ApplicationInfo;
39import android.content.pm.IPackageManager;
40import android.content.res.CompatibilityInfo;
41import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080042import android.os.Looper;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070043import android.os.Message;
44import android.os.RemoteException;
Dianne Hackborn39606a02012-07-31 17:54:35 -070045import android.util.AtomicFile;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070046import android.util.Slog;
47import android.util.Xml;
48
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070049public final class CompatModePackages {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080050 private static final String TAG = TAG_WITH_CLASS_NAME ? "CompatModePackages" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070051 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070052
53 private final ActivityManagerService mService;
54 private final AtomicFile mFile;
55
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070056 // Compatibility state: no longer ask user to select the mode.
57 public static final int COMPAT_FLAG_DONT_ASK = 1<<0;
58 // Compatibility state: compatibility mode is enabled.
59 public static final int COMPAT_FLAG_ENABLED = 1<<1;
60
61 private final HashMap<String, Integer> mPackages = new HashMap<String, Integer>();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070062
Dianne Hackborn40c8db52012-02-10 18:59:48 -080063 private static final int MSG_WRITE = ActivityManagerService.FIRST_COMPAT_MODE_MSG;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070064
Jeff Brown6f357d32014-01-15 20:40:55 -080065 private final CompatHandler mHandler;
66
67 private final class CompatHandler extends Handler {
68 public CompatHandler(Looper looper) {
69 super(looper, null, true);
70 }
71
72 @Override
73 public void handleMessage(Message msg) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070074 switch (msg.what) {
75 case MSG_WRITE:
76 saveCompatModes();
77 break;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070078 }
79 }
80 };
81
Jeff Brown6f357d32014-01-15 20:40:55 -080082 public CompatModePackages(ActivityManagerService service, File systemDir, Handler handler) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070083 mService = service;
84 mFile = new AtomicFile(new File(systemDir, "packages-compat.xml"));
Jeff Brown6f357d32014-01-15 20:40:55 -080085 mHandler = new CompatHandler(handler.getLooper());
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070086
87 FileInputStream fis = null;
88 try {
89 fis = mFile.openRead();
90 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010091 parser.setInput(fis, StandardCharsets.UTF_8.name());
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070092 int eventType = parser.getEventType();
Narayan Kamath2ac3cb72014-01-06 11:31:35 +000093 while (eventType != XmlPullParser.START_TAG &&
94 eventType != XmlPullParser.END_DOCUMENT) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070095 eventType = parser.next();
96 }
Narayan Kamath2ac3cb72014-01-06 11:31:35 +000097 if (eventType == XmlPullParser.END_DOCUMENT) {
98 return;
99 }
100
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700101 String tagName = parser.getName();
102 if ("compat-packages".equals(tagName)) {
103 eventType = parser.next();
104 do {
105 if (eventType == XmlPullParser.START_TAG) {
106 tagName = parser.getName();
107 if (parser.getDepth() == 2) {
108 if ("pkg".equals(tagName)) {
109 String pkg = parser.getAttributeValue(null, "name");
110 if (pkg != null) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700111 String mode = parser.getAttributeValue(null, "mode");
112 int modeInt = 0;
113 if (mode != null) {
114 try {
115 modeInt = Integer.parseInt(mode);
116 } catch (NumberFormatException e) {
117 }
118 }
119 mPackages.put(pkg, modeInt);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700120 }
121 }
122 }
123 }
124 eventType = parser.next();
125 } while (eventType != XmlPullParser.END_DOCUMENT);
126 }
127 } catch (XmlPullParserException e) {
128 Slog.w(TAG, "Error reading compat-packages", e);
129 } catch (java.io.IOException e) {
130 if (fis != null) Slog.w(TAG, "Error reading compat-packages", e);
131 } finally {
132 if (fis != null) {
133 try {
134 fis.close();
135 } catch (java.io.IOException e1) {
136 }
137 }
138 }
139 }
140
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700141 public HashMap<String, Integer> getPackages() {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700142 return mPackages;
143 }
144
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700145 private int getPackageFlags(String packageName) {
146 Integer flags = mPackages.get(packageName);
147 return flags != null ? flags : 0;
148 }
149
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700150 public void handlePackageAddedLocked(String packageName, boolean updated) {
151 ApplicationInfo ai = null;
152 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700153 ai = AppGlobals.getPackageManager().getApplicationInfo(packageName, 0, 0);
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700154 } catch (RemoteException e) {
155 }
156 if (ai == null) {
157 return;
158 }
159 CompatibilityInfo ci = compatibilityInfoForPackageLocked(ai);
160 final boolean mayCompat = !ci.alwaysSupportsScreen()
161 && !ci.neverSupportsScreen();
162
Dianne Hackborna9551702011-06-14 21:05:03 -0700163 if (updated) {
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700164 // Update -- if the app no longer can run in compat mode, clear
165 // any current settings for it.
166 if (!mayCompat && mPackages.containsKey(packageName)) {
167 mPackages.remove(packageName);
168 mHandler.removeMessages(MSG_WRITE);
169 Message msg = mHandler.obtainMessage(MSG_WRITE);
170 mHandler.sendMessageDelayed(msg, 10000);
171 }
172 }
173 }
174
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700175 public CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700176 CompatibilityInfo ci = new CompatibilityInfo(ai, mService.mConfiguration.screenLayout,
Dianne Hackborndf6e9802011-05-26 14:20:23 -0700177 mService.mConfiguration.smallestScreenWidthDp,
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700178 (getPackageFlags(ai.packageName)&COMPAT_FLAG_ENABLED) != 0);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700179 //Slog.i(TAG, "*********** COMPAT FOR PKG " + ai.packageName + ": " + ci);
180 return ci;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700181 }
182
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700183 public int computeCompatModeLocked(ApplicationInfo ai) {
184 boolean enabled = (getPackageFlags(ai.packageName)&COMPAT_FLAG_ENABLED) != 0;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700185 CompatibilityInfo info = new CompatibilityInfo(ai,
Dianne Hackborndf6e9802011-05-26 14:20:23 -0700186 mService.mConfiguration.screenLayout,
187 mService.mConfiguration.smallestScreenWidthDp, enabled);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700188 if (info.alwaysSupportsScreen()) {
189 return ActivityManager.COMPAT_MODE_NEVER;
190 }
191 if (info.neverSupportsScreen()) {
192 return ActivityManager.COMPAT_MODE_ALWAYS;
193 }
194 return enabled ? ActivityManager.COMPAT_MODE_ENABLED
195 : ActivityManager.COMPAT_MODE_DISABLED;
196 }
197
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700198 public boolean getFrontActivityAskCompatModeLocked() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700199 ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked();
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700200 if (r == null) {
201 return false;
202 }
203 return getPackageAskCompatModeLocked(r.packageName);
204 }
205
206 public boolean getPackageAskCompatModeLocked(String packageName) {
207 return (getPackageFlags(packageName)&COMPAT_FLAG_DONT_ASK) == 0;
208 }
209
210 public void setFrontActivityAskCompatModeLocked(boolean ask) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700211 ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked();
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700212 if (r != null) {
213 setPackageAskCompatModeLocked(r.packageName, ask);
214 }
215 }
216
217 public void setPackageAskCompatModeLocked(String packageName, boolean ask) {
218 int curFlags = getPackageFlags(packageName);
219 int newFlags = ask ? (curFlags&~COMPAT_FLAG_DONT_ASK) : (curFlags|COMPAT_FLAG_DONT_ASK);
220 if (curFlags != newFlags) {
221 if (newFlags != 0) {
222 mPackages.put(packageName, newFlags);
223 } else {
224 mPackages.remove(packageName);
225 }
226 mHandler.removeMessages(MSG_WRITE);
227 Message msg = mHandler.obtainMessage(MSG_WRITE);
228 mHandler.sendMessageDelayed(msg, 10000);
229 }
230 }
231
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700232 public int getFrontActivityScreenCompatModeLocked() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700233 ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700234 if (r == null) {
235 return ActivityManager.COMPAT_MODE_UNKNOWN;
236 }
237 return computeCompatModeLocked(r.info.applicationInfo);
238 }
239
240 public void setFrontActivityScreenCompatModeLocked(int mode) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700241 ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700242 if (r == null) {
243 Slog.w(TAG, "setFrontActivityScreenCompatMode failed: no top activity");
244 return;
245 }
246 setPackageScreenCompatModeLocked(r.info.applicationInfo, mode);
247 }
248
249 public int getPackageScreenCompatModeLocked(String packageName) {
250 ApplicationInfo ai = null;
251 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700252 ai = AppGlobals.getPackageManager().getApplicationInfo(packageName, 0, 0);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700253 } catch (RemoteException e) {
254 }
255 if (ai == null) {
256 return ActivityManager.COMPAT_MODE_UNKNOWN;
257 }
258 return computeCompatModeLocked(ai);
259 }
260
261 public void setPackageScreenCompatModeLocked(String packageName, int mode) {
262 ApplicationInfo ai = null;
263 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700264 ai = AppGlobals.getPackageManager().getApplicationInfo(packageName, 0, 0);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700265 } catch (RemoteException e) {
266 }
267 if (ai == null) {
268 Slog.w(TAG, "setPackageScreenCompatMode failed: unknown package " + packageName);
269 return;
270 }
271 setPackageScreenCompatModeLocked(ai, mode);
272 }
273
274 private void setPackageScreenCompatModeLocked(ApplicationInfo ai, int mode) {
275 final String packageName = ai.packageName;
276
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700277 int curFlags = getPackageFlags(packageName);
278
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700279 boolean enable;
280 switch (mode) {
281 case ActivityManager.COMPAT_MODE_DISABLED:
282 enable = false;
283 break;
284 case ActivityManager.COMPAT_MODE_ENABLED:
285 enable = true;
286 break;
287 case ActivityManager.COMPAT_MODE_TOGGLE:
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700288 enable = (curFlags&COMPAT_FLAG_ENABLED) == 0;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700289 break;
290 default:
291 Slog.w(TAG, "Unknown screen compat mode req #" + mode + "; ignoring");
292 return;
293 }
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700294
295 int newFlags = curFlags;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700296 if (enable) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700297 newFlags |= COMPAT_FLAG_ENABLED;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700298 } else {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700299 newFlags &= ~COMPAT_FLAG_ENABLED;
300 }
301
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700302 CompatibilityInfo ci = compatibilityInfoForPackageLocked(ai);
303 if (ci.alwaysSupportsScreen()) {
304 Slog.w(TAG, "Ignoring compat mode change of " + packageName
305 + "; compatibility never needed");
306 newFlags = 0;
307 }
308 if (ci.neverSupportsScreen()) {
309 Slog.w(TAG, "Ignoring compat mode change of " + packageName
310 + "; compatibility always needed");
311 newFlags = 0;
312 }
313
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700314 if (newFlags != curFlags) {
315 if (newFlags != 0) {
316 mPackages.put(packageName, newFlags);
317 } else {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700318 mPackages.remove(packageName);
319 }
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700320
321 // Need to get compatibility info in new state.
322 ci = compatibilityInfoForPackageLocked(ai);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700323
324 mHandler.removeMessages(MSG_WRITE);
325 Message msg = mHandler.obtainMessage(MSG_WRITE);
326 mHandler.sendMessageDelayed(msg, 10000);
327
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700328 final ActivityStack stack = mService.getFocusedStack();
Craig Mautner20e72272013-04-01 13:45:53 -0700329 ActivityRecord starting = stack.restartPackage(packageName);
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700330
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700331 // Tell all processes that loaded this package about the change.
332 for (int i=mService.mLruProcesses.size()-1; i>=0; i--) {
333 ProcessRecord app = mService.mLruProcesses.get(i);
Dianne Hackborn78a369c2013-06-11 17:10:32 -0700334 if (!app.pkgList.containsKey(packageName)) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700335 continue;
336 }
337 try {
338 if (app.thread != null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700339 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending to proc "
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700340 + app.processName + " new compat " + ci);
341 app.thread.updatePackageCompatibilityInfo(packageName, ci);
342 }
343 } catch (Exception e) {
344 }
345 }
346
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700347 if (starting != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700348 stack.ensureActivityConfigurationLocked(starting, 0, false);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700349 // And we need to make sure at this point that all other activities
350 // are made visible with the correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700351 stack.ensureActivitiesVisibleLocked(starting, 0, !PRESERVE_WINDOWS);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700352 }
353 }
354 }
355
356 void saveCompatModes() {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700357 HashMap<String, Integer> pkgs;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700358 synchronized (mService) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700359 pkgs = new HashMap<String, Integer>(mPackages);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700360 }
361
362 FileOutputStream fos = null;
363
364 try {
365 fos = mFile.startWrite();
366 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100367 out.setOutput(fos, StandardCharsets.UTF_8.name());
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700368 out.startDocument(null, true);
369 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
370 out.startTag(null, "compat-packages");
371
372 final IPackageManager pm = AppGlobals.getPackageManager();
373 final int screenLayout = mService.mConfiguration.screenLayout;
Dianne Hackborndf6e9802011-05-26 14:20:23 -0700374 final int smallestScreenWidthDp = mService.mConfiguration.smallestScreenWidthDp;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700375 final Iterator<Map.Entry<String, Integer>> it = pkgs.entrySet().iterator();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700376 while (it.hasNext()) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700377 Map.Entry<String, Integer> entry = it.next();
378 String pkg = entry.getKey();
379 int mode = entry.getValue();
380 if (mode == 0) {
381 continue;
382 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700383 ApplicationInfo ai = null;
384 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700385 ai = pm.getApplicationInfo(pkg, 0, 0);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700386 } catch (RemoteException e) {
387 }
388 if (ai == null) {
389 continue;
390 }
Dianne Hackborndf6e9802011-05-26 14:20:23 -0700391 CompatibilityInfo info = new CompatibilityInfo(ai, screenLayout,
392 smallestScreenWidthDp, false);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700393 if (info.alwaysSupportsScreen()) {
394 continue;
395 }
396 if (info.neverSupportsScreen()) {
397 continue;
398 }
399 out.startTag(null, "pkg");
400 out.attribute(null, "name", pkg);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700401 out.attribute(null, "mode", Integer.toString(mode));
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700402 out.endTag(null, "pkg");
403 }
404
405 out.endTag(null, "compat-packages");
406 out.endDocument();
407
408 mFile.finishWrite(fos);
409 } catch (java.io.IOException e1) {
410 Slog.w(TAG, "Error writing compat packages", e1);
411 if (fos != null) {
412 mFile.failWrite(fos);
413 }
414 }
415 }
416}