blob: ec500c2ee1542719d5257e35e4030f7502827e4a [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
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.FileOutputStream;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070022import java.util.HashMap;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070023import java.util.Iterator;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070024import java.util.Map;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070025
26import org.xmlpull.v1.XmlPullParser;
27import org.xmlpull.v1.XmlPullParserException;
28import org.xmlpull.v1.XmlSerializer;
29
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070030import com.android.internal.util.FastXmlSerializer;
31
32import android.app.ActivityManager;
33import android.app.AppGlobals;
34import android.content.pm.ApplicationInfo;
35import android.content.pm.IPackageManager;
36import android.content.res.CompatibilityInfo;
37import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080038import android.os.Looper;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070039import android.os.Message;
40import android.os.RemoteException;
Dianne Hackborn39606a02012-07-31 17:54:35 -070041import android.util.AtomicFile;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070042import android.util.Slog;
43import android.util.Xml;
44
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070045public final class CompatModePackages {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070046 private final String TAG = ActivityManagerService.TAG;
47 private final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
48
49 private final ActivityManagerService mService;
50 private final AtomicFile mFile;
51
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070052 // Compatibility state: no longer ask user to select the mode.
53 public static final int COMPAT_FLAG_DONT_ASK = 1<<0;
54 // Compatibility state: compatibility mode is enabled.
55 public static final int COMPAT_FLAG_ENABLED = 1<<1;
56
57 private final HashMap<String, Integer> mPackages = new HashMap<String, Integer>();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070058
Dianne Hackborn40c8db52012-02-10 18:59:48 -080059 private static final int MSG_WRITE = ActivityManagerService.FIRST_COMPAT_MODE_MSG;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070060
Jeff Brown6f357d32014-01-15 20:40:55 -080061 private final CompatHandler mHandler;
62
63 private final class CompatHandler extends Handler {
64 public CompatHandler(Looper looper) {
65 super(looper, null, true);
66 }
67
68 @Override
69 public void handleMessage(Message msg) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070070 switch (msg.what) {
71 case MSG_WRITE:
72 saveCompatModes();
73 break;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070074 }
75 }
76 };
77
Jeff Brown6f357d32014-01-15 20:40:55 -080078 public CompatModePackages(ActivityManagerService service, File systemDir, Handler handler) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070079 mService = service;
80 mFile = new AtomicFile(new File(systemDir, "packages-compat.xml"));
Jeff Brown6f357d32014-01-15 20:40:55 -080081 mHandler = new CompatHandler(handler.getLooper());
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070082
83 FileInputStream fis = null;
84 try {
85 fis = mFile.openRead();
86 XmlPullParser parser = Xml.newPullParser();
87 parser.setInput(fis, null);
88 int eventType = parser.getEventType();
Narayan Kamath2ac3cb72014-01-06 11:31:35 +000089 while (eventType != XmlPullParser.START_TAG &&
90 eventType != XmlPullParser.END_DOCUMENT) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070091 eventType = parser.next();
92 }
Narayan Kamath2ac3cb72014-01-06 11:31:35 +000093 if (eventType == XmlPullParser.END_DOCUMENT) {
94 return;
95 }
96
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070097 String tagName = parser.getName();
98 if ("compat-packages".equals(tagName)) {
99 eventType = parser.next();
100 do {
101 if (eventType == XmlPullParser.START_TAG) {
102 tagName = parser.getName();
103 if (parser.getDepth() == 2) {
104 if ("pkg".equals(tagName)) {
105 String pkg = parser.getAttributeValue(null, "name");
106 if (pkg != null) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700107 String mode = parser.getAttributeValue(null, "mode");
108 int modeInt = 0;
109 if (mode != null) {
110 try {
111 modeInt = Integer.parseInt(mode);
112 } catch (NumberFormatException e) {
113 }
114 }
115 mPackages.put(pkg, modeInt);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700116 }
117 }
118 }
119 }
120 eventType = parser.next();
121 } while (eventType != XmlPullParser.END_DOCUMENT);
122 }
123 } catch (XmlPullParserException e) {
124 Slog.w(TAG, "Error reading compat-packages", e);
125 } catch (java.io.IOException e) {
126 if (fis != null) Slog.w(TAG, "Error reading compat-packages", e);
127 } finally {
128 if (fis != null) {
129 try {
130 fis.close();
131 } catch (java.io.IOException e1) {
132 }
133 }
134 }
135 }
136
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700137 public HashMap<String, Integer> getPackages() {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700138 return mPackages;
139 }
140
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700141 private int getPackageFlags(String packageName) {
142 Integer flags = mPackages.get(packageName);
143 return flags != null ? flags : 0;
144 }
145
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700146 public void handlePackageAddedLocked(String packageName, boolean updated) {
147 ApplicationInfo ai = null;
148 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700149 ai = AppGlobals.getPackageManager().getApplicationInfo(packageName, 0, 0);
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700150 } catch (RemoteException e) {
151 }
152 if (ai == null) {
153 return;
154 }
155 CompatibilityInfo ci = compatibilityInfoForPackageLocked(ai);
156 final boolean mayCompat = !ci.alwaysSupportsScreen()
157 && !ci.neverSupportsScreen();
158
Dianne Hackborna9551702011-06-14 21:05:03 -0700159 if (updated) {
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700160 // Update -- if the app no longer can run in compat mode, clear
161 // any current settings for it.
162 if (!mayCompat && mPackages.containsKey(packageName)) {
163 mPackages.remove(packageName);
164 mHandler.removeMessages(MSG_WRITE);
165 Message msg = mHandler.obtainMessage(MSG_WRITE);
166 mHandler.sendMessageDelayed(msg, 10000);
167 }
168 }
169 }
170
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700171 public CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700172 CompatibilityInfo ci = new CompatibilityInfo(ai, mService.mConfiguration.screenLayout,
Dianne Hackborndf6e9802011-05-26 14:20:23 -0700173 mService.mConfiguration.smallestScreenWidthDp,
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700174 (getPackageFlags(ai.packageName)&COMPAT_FLAG_ENABLED) != 0);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700175 //Slog.i(TAG, "*********** COMPAT FOR PKG " + ai.packageName + ": " + ci);
176 return ci;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700177 }
178
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700179 public int computeCompatModeLocked(ApplicationInfo ai) {
180 boolean enabled = (getPackageFlags(ai.packageName)&COMPAT_FLAG_ENABLED) != 0;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700181 CompatibilityInfo info = new CompatibilityInfo(ai,
Dianne Hackborndf6e9802011-05-26 14:20:23 -0700182 mService.mConfiguration.screenLayout,
183 mService.mConfiguration.smallestScreenWidthDp, enabled);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700184 if (info.alwaysSupportsScreen()) {
185 return ActivityManager.COMPAT_MODE_NEVER;
186 }
187 if (info.neverSupportsScreen()) {
188 return ActivityManager.COMPAT_MODE_ALWAYS;
189 }
190 return enabled ? ActivityManager.COMPAT_MODE_ENABLED
191 : ActivityManager.COMPAT_MODE_DISABLED;
192 }
193
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700194 public boolean getFrontActivityAskCompatModeLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700195 ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked(null);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700196 if (r == null) {
197 return false;
198 }
199 return getPackageAskCompatModeLocked(r.packageName);
200 }
201
202 public boolean getPackageAskCompatModeLocked(String packageName) {
203 return (getPackageFlags(packageName)&COMPAT_FLAG_DONT_ASK) == 0;
204 }
205
206 public void setFrontActivityAskCompatModeLocked(boolean ask) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700207 ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked(null);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700208 if (r != null) {
209 setPackageAskCompatModeLocked(r.packageName, ask);
210 }
211 }
212
213 public void setPackageAskCompatModeLocked(String packageName, boolean ask) {
214 int curFlags = getPackageFlags(packageName);
215 int newFlags = ask ? (curFlags&~COMPAT_FLAG_DONT_ASK) : (curFlags|COMPAT_FLAG_DONT_ASK);
216 if (curFlags != newFlags) {
217 if (newFlags != 0) {
218 mPackages.put(packageName, newFlags);
219 } else {
220 mPackages.remove(packageName);
221 }
222 mHandler.removeMessages(MSG_WRITE);
223 Message msg = mHandler.obtainMessage(MSG_WRITE);
224 mHandler.sendMessageDelayed(msg, 10000);
225 }
226 }
227
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700228 public int getFrontActivityScreenCompatModeLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700229 ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked(null);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700230 if (r == null) {
231 return ActivityManager.COMPAT_MODE_UNKNOWN;
232 }
233 return computeCompatModeLocked(r.info.applicationInfo);
234 }
235
236 public void setFrontActivityScreenCompatModeLocked(int mode) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700237 ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked(null);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700238 if (r == null) {
239 Slog.w(TAG, "setFrontActivityScreenCompatMode failed: no top activity");
240 return;
241 }
242 setPackageScreenCompatModeLocked(r.info.applicationInfo, mode);
243 }
244
245 public int getPackageScreenCompatModeLocked(String packageName) {
246 ApplicationInfo ai = null;
247 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700248 ai = AppGlobals.getPackageManager().getApplicationInfo(packageName, 0, 0);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700249 } catch (RemoteException e) {
250 }
251 if (ai == null) {
252 return ActivityManager.COMPAT_MODE_UNKNOWN;
253 }
254 return computeCompatModeLocked(ai);
255 }
256
257 public void setPackageScreenCompatModeLocked(String packageName, int mode) {
258 ApplicationInfo ai = null;
259 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700260 ai = AppGlobals.getPackageManager().getApplicationInfo(packageName, 0, 0);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700261 } catch (RemoteException e) {
262 }
263 if (ai == null) {
264 Slog.w(TAG, "setPackageScreenCompatMode failed: unknown package " + packageName);
265 return;
266 }
267 setPackageScreenCompatModeLocked(ai, mode);
268 }
269
270 private void setPackageScreenCompatModeLocked(ApplicationInfo ai, int mode) {
271 final String packageName = ai.packageName;
272
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700273 int curFlags = getPackageFlags(packageName);
274
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700275 boolean enable;
276 switch (mode) {
277 case ActivityManager.COMPAT_MODE_DISABLED:
278 enable = false;
279 break;
280 case ActivityManager.COMPAT_MODE_ENABLED:
281 enable = true;
282 break;
283 case ActivityManager.COMPAT_MODE_TOGGLE:
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700284 enable = (curFlags&COMPAT_FLAG_ENABLED) == 0;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700285 break;
286 default:
287 Slog.w(TAG, "Unknown screen compat mode req #" + mode + "; ignoring");
288 return;
289 }
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700290
291 int newFlags = curFlags;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700292 if (enable) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700293 newFlags |= COMPAT_FLAG_ENABLED;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700294 } else {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700295 newFlags &= ~COMPAT_FLAG_ENABLED;
296 }
297
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700298 CompatibilityInfo ci = compatibilityInfoForPackageLocked(ai);
299 if (ci.alwaysSupportsScreen()) {
300 Slog.w(TAG, "Ignoring compat mode change of " + packageName
301 + "; compatibility never needed");
302 newFlags = 0;
303 }
304 if (ci.neverSupportsScreen()) {
305 Slog.w(TAG, "Ignoring compat mode change of " + packageName
306 + "; compatibility always needed");
307 newFlags = 0;
308 }
309
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700310 if (newFlags != curFlags) {
311 if (newFlags != 0) {
312 mPackages.put(packageName, newFlags);
313 } else {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700314 mPackages.remove(packageName);
315 }
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700316
317 // Need to get compatibility info in new state.
318 ci = compatibilityInfoForPackageLocked(ai);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700319
320 mHandler.removeMessages(MSG_WRITE);
321 Message msg = mHandler.obtainMessage(MSG_WRITE);
322 mHandler.sendMessageDelayed(msg, 10000);
323
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324 final ActivityStack stack = mService.getFocusedStack();
Craig Mautner20e72272013-04-01 13:45:53 -0700325 ActivityRecord starting = stack.restartPackage(packageName);
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700326
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700327 // Tell all processes that loaded this package about the change.
328 for (int i=mService.mLruProcesses.size()-1; i>=0; i--) {
329 ProcessRecord app = mService.mLruProcesses.get(i);
Dianne Hackborn78a369c2013-06-11 17:10:32 -0700330 if (!app.pkgList.containsKey(packageName)) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700331 continue;
332 }
333 try {
334 if (app.thread != null) {
335 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc "
336 + app.processName + " new compat " + ci);
337 app.thread.updatePackageCompatibilityInfo(packageName, ci);
338 }
339 } catch (Exception e) {
340 }
341 }
342
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700343 if (starting != null) {
Craig Mautner20e72272013-04-01 13:45:53 -0700344 stack.ensureActivityConfigurationLocked(starting, 0);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700345 // And we need to make sure at this point that all other activities
346 // are made visible with the correct configuration.
Craig Mautner20e72272013-04-01 13:45:53 -0700347 stack.ensureActivitiesVisibleLocked(starting, 0);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700348 }
349 }
350 }
351
352 void saveCompatModes() {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700353 HashMap<String, Integer> pkgs;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700354 synchronized (mService) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700355 pkgs = new HashMap<String, Integer>(mPackages);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700356 }
357
358 FileOutputStream fos = null;
359
360 try {
361 fos = mFile.startWrite();
362 XmlSerializer out = new FastXmlSerializer();
363 out.setOutput(fos, "utf-8");
364 out.startDocument(null, true);
365 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
366 out.startTag(null, "compat-packages");
367
368 final IPackageManager pm = AppGlobals.getPackageManager();
369 final int screenLayout = mService.mConfiguration.screenLayout;
Dianne Hackborndf6e9802011-05-26 14:20:23 -0700370 final int smallestScreenWidthDp = mService.mConfiguration.smallestScreenWidthDp;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700371 final Iterator<Map.Entry<String, Integer>> it = pkgs.entrySet().iterator();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700372 while (it.hasNext()) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700373 Map.Entry<String, Integer> entry = it.next();
374 String pkg = entry.getKey();
375 int mode = entry.getValue();
376 if (mode == 0) {
377 continue;
378 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700379 ApplicationInfo ai = null;
380 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700381 ai = pm.getApplicationInfo(pkg, 0, 0);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700382 } catch (RemoteException e) {
383 }
384 if (ai == null) {
385 continue;
386 }
Dianne Hackborndf6e9802011-05-26 14:20:23 -0700387 CompatibilityInfo info = new CompatibilityInfo(ai, screenLayout,
388 smallestScreenWidthDp, false);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700389 if (info.alwaysSupportsScreen()) {
390 continue;
391 }
392 if (info.neverSupportsScreen()) {
393 continue;
394 }
395 out.startTag(null, "pkg");
396 out.attribute(null, "name", pkg);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700397 out.attribute(null, "mode", Integer.toString(mode));
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700398 out.endTag(null, "pkg");
399 }
400
401 out.endTag(null, "compat-packages");
402 out.endDocument();
403
404 mFile.finishWrite(fos);
405 } catch (java.io.IOException e1) {
406 Slog.w(TAG, "Error writing compat packages", e1);
407 if (fos != null) {
408 mFile.failWrite(fos);
409 }
410 }
411 }
412}