blob: b17a9411f07b5c164b6f5f21c48df1d08deb9db4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
17package com.android.server;
18
19import com.android.internal.app.ResolverActivity;
20import com.android.internal.util.FastXmlSerializer;
21import com.android.internal.util.XmlUtils;
22
23import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlPullParserException;
25import org.xmlpull.v1.XmlSerializer;
26
27import android.app.ActivityManagerNative;
28import android.app.IActivityManager;
29import android.app.PendingIntent;
30import android.app.PendingIntent.CanceledException;
31import android.content.ComponentName;
32import android.content.ContentResolver;
33import android.content.Context;
34import android.content.Intent;
35import android.content.IntentFilter;
36import android.content.pm.ActivityInfo;
37import android.content.pm.ApplicationInfo;
38import android.content.pm.ComponentInfo;
39import android.content.pm.IPackageDataObserver;
40import android.content.pm.IPackageDeleteObserver;
41import android.content.pm.IPackageInstallObserver;
42import android.content.pm.IPackageManager;
43import android.content.pm.IPackageStatsObserver;
44import android.content.pm.InstrumentationInfo;
45import android.content.pm.PackageInfo;
46import android.content.pm.PackageManager;
47import android.content.pm.PackageStats;
48import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
49import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
50import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
51import static android.content.pm.PackageManager.PKG_INSTALL_COMPLETE;
52import static android.content.pm.PackageManager.PKG_INSTALL_INCOMPLETE;
53import android.content.pm.PackageParser;
54import android.content.pm.PermissionInfo;
55import android.content.pm.PermissionGroupInfo;
56import android.content.pm.ProviderInfo;
57import android.content.pm.ResolveInfo;
58import android.content.pm.ServiceInfo;
59import android.content.pm.Signature;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.net.Uri;
61import android.os.Binder;
Dianne Hackborn851a5412009-05-08 12:06:44 -070062import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Bundle;
64import android.os.HandlerThread;
65import android.os.Parcel;
66import android.os.RemoteException;
67import android.os.Environment;
68import android.os.FileObserver;
69import android.os.FileUtils;
70import android.os.Handler;
71import android.os.ParcelFileDescriptor;
72import android.os.Process;
73import android.os.ServiceManager;
74import android.os.SystemClock;
75import android.os.SystemProperties;
76import android.util.*;
77import android.view.Display;
78import android.view.WindowManager;
79
80import java.io.File;
81import java.io.FileDescriptor;
82import java.io.FileInputStream;
83import java.io.FileNotFoundException;
84import java.io.FileOutputStream;
85import java.io.FileReader;
86import java.io.FilenameFilter;
87import java.io.IOException;
88import java.io.InputStream;
89import java.io.PrintWriter;
90import java.util.ArrayList;
91import java.util.Arrays;
92import java.util.Collections;
93import java.util.Comparator;
94import java.util.Enumeration;
95import java.util.HashMap;
96import java.util.HashSet;
97import java.util.Iterator;
98import java.util.List;
99import java.util.Map;
100import java.util.Set;
101import java.util.zip.ZipEntry;
102import java.util.zip.ZipFile;
103import java.util.zip.ZipOutputStream;
104
105class PackageManagerService extends IPackageManager.Stub {
106 private static final String TAG = "PackageManager";
107 private static final boolean DEBUG_SETTINGS = false;
108 private static final boolean DEBUG_PREFERRED = false;
109
110 private static final boolean MULTIPLE_APPLICATION_UIDS = true;
111 private static final int RADIO_UID = Process.PHONE_UID;
112 private static final int FIRST_APPLICATION_UID =
113 Process.FIRST_APPLICATION_UID;
114 private static final int MAX_APPLICATION_UIDS = 1000;
115
116 private static final boolean SHOW_INFO = false;
117
118 private static final boolean GET_CERTIFICATES = true;
119
120 private static final int REMOVE_EVENTS =
121 FileObserver.CLOSE_WRITE | FileObserver.DELETE | FileObserver.MOVED_FROM;
122 private static final int ADD_EVENTS =
123 FileObserver.CLOSE_WRITE /*| FileObserver.CREATE*/ | FileObserver.MOVED_TO;
124
125 private static final int OBSERVER_EVENTS = REMOVE_EVENTS | ADD_EVENTS;
126
127 static final int SCAN_MONITOR = 1<<0;
128 static final int SCAN_NO_DEX = 1<<1;
129 static final int SCAN_FORCE_DEX = 1<<2;
130 static final int SCAN_UPDATE_SIGNATURE = 1<<3;
131 static final int SCAN_FORWARD_LOCKED = 1<<4;
The Android Open Source Project10592532009-03-18 17:39:46 -0700132 static final int SCAN_NEW_INSTALL = 1<<5;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
134 static final int LOG_BOOT_PROGRESS_PMS_START = 3060;
135 static final int LOG_BOOT_PROGRESS_PMS_SYSTEM_SCAN_START = 3070;
136 static final int LOG_BOOT_PROGRESS_PMS_DATA_SCAN_START = 3080;
137 static final int LOG_BOOT_PROGRESS_PMS_SCAN_END = 3090;
138 static final int LOG_BOOT_PROGRESS_PMS_READY = 3100;
139
140 final HandlerThread mHandlerThread = new HandlerThread("PackageManager",
141 Process.THREAD_PRIORITY_BACKGROUND);
142 final Handler mHandler;
143
Dianne Hackborn851a5412009-05-08 12:06:44 -0700144 final int mSdkVersion = Build.VERSION.SDK_INT;
145 final String mSdkCodename = "REL".equals(Build.VERSION.CODENAME)
146 ? null : Build.VERSION.CODENAME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148 final Context mContext;
149 final boolean mFactoryTest;
150 final DisplayMetrics mMetrics;
151 final int mDefParseFlags;
152 final String[] mSeparateProcesses;
153
154 // This is where all application persistent data goes.
155 final File mAppDataDir;
156
157 // This is the object monitoring the framework dir.
158 final FileObserver mFrameworkInstallObserver;
159
160 // This is the object monitoring the system app dir.
161 final FileObserver mSystemInstallObserver;
162
163 // This is the object monitoring mAppInstallDir.
164 final FileObserver mAppInstallObserver;
165
166 // This is the object monitoring mDrmAppPrivateInstallDir.
167 final FileObserver mDrmAppInstallObserver;
168
169 // Used for priviledge escalation. MUST NOT BE CALLED WITH mPackages
170 // LOCK HELD. Can be called with mInstallLock held.
171 final Installer mInstaller;
172
173 final File mFrameworkDir;
174 final File mSystemAppDir;
175 final File mAppInstallDir;
176
177 // Directory containing the private parts (e.g. code and non-resource assets) of forward-locked
178 // apps.
179 final File mDrmAppPrivateInstallDir;
180
181 // ----------------------------------------------------------------
182
183 // Lock for state used when installing and doing other long running
184 // operations. Methods that must be called with this lock held have
185 // the prefix "LI".
186 final Object mInstallLock = new Object();
187
188 // These are the directories in the 3rd party applications installed dir
189 // that we have currently loaded packages from. Keys are the application's
190 // installed zip file (absolute codePath), and values are Package.
191 final HashMap<String, PackageParser.Package> mAppDirs =
192 new HashMap<String, PackageParser.Package>();
193
194 // Information for the parser to write more useful error messages.
195 File mScanningPath;
196 int mLastScanError;
197
198 final int[] mOutPermissions = new int[3];
199
200 // ----------------------------------------------------------------
201
202 // Keys are String (package name), values are Package. This also serves
203 // as the lock for the global state. Methods that must be called with
204 // this lock held have the prefix "LP".
205 final HashMap<String, PackageParser.Package> mPackages =
206 new HashMap<String, PackageParser.Package>();
207
208 final Settings mSettings;
209 boolean mRestoredSettings;
210 boolean mReportedUidError;
211
212 // Group-ids that are given to all packages as read from etc/permissions/*.xml.
213 int[] mGlobalGids;
214
215 // These are the built-in uid -> permission mappings that were read from the
216 // etc/permissions.xml file.
217 final SparseArray<HashSet<String>> mSystemPermissions =
218 new SparseArray<HashSet<String>>();
219
220 // These are the built-in shared libraries that were read from the
221 // etc/permissions.xml file.
222 final HashMap<String, String> mSharedLibraries = new HashMap<String, String>();
223
224 // All available activities, for your resolving pleasure.
225 final ActivityIntentResolver mActivities =
226 new ActivityIntentResolver();
227
228 // All available receivers, for your resolving pleasure.
229 final ActivityIntentResolver mReceivers =
230 new ActivityIntentResolver();
231
232 // All available services, for your resolving pleasure.
233 final ServiceIntentResolver mServices = new ServiceIntentResolver();
234
235 // Keys are String (provider class name), values are Provider.
236 final HashMap<ComponentName, PackageParser.Provider> mProvidersByComponent =
237 new HashMap<ComponentName, PackageParser.Provider>();
238
239 // Mapping from provider base names (first directory in content URI codePath)
240 // to the provider information.
241 final HashMap<String, PackageParser.Provider> mProviders =
242 new HashMap<String, PackageParser.Provider>();
243
244 // Mapping from instrumentation class names to info about them.
245 final HashMap<ComponentName, PackageParser.Instrumentation> mInstrumentation =
246 new HashMap<ComponentName, PackageParser.Instrumentation>();
247
248 // Mapping from permission names to info about them.
249 final HashMap<String, PackageParser.PermissionGroup> mPermissionGroups =
250 new HashMap<String, PackageParser.PermissionGroup>();
251
252 boolean mSystemReady;
253 boolean mSafeMode;
254 boolean mHasSystemUidErrors;
255
256 ApplicationInfo mAndroidApplication;
257 final ActivityInfo mResolveActivity = new ActivityInfo();
258 final ResolveInfo mResolveInfo = new ResolveInfo();
259 ComponentName mResolveComponentName;
260 PackageParser.Package mPlatformPackage;
261
262 public static final IPackageManager main(Context context, boolean factoryTest) {
263 PackageManagerService m = new PackageManagerService(context, factoryTest);
264 ServiceManager.addService("package", m);
265 return m;
266 }
267
268 static String[] splitString(String str, char sep) {
269 int count = 1;
270 int i = 0;
271 while ((i=str.indexOf(sep, i)) >= 0) {
272 count++;
273 i++;
274 }
275
276 String[] res = new String[count];
277 i=0;
278 count = 0;
279 int lastI=0;
280 while ((i=str.indexOf(sep, i)) >= 0) {
281 res[count] = str.substring(lastI, i);
282 count++;
283 i++;
284 lastI = i;
285 }
286 res[count] = str.substring(lastI, str.length());
287 return res;
288 }
289
290 public PackageManagerService(Context context, boolean factoryTest) {
291 EventLog.writeEvent(LOG_BOOT_PROGRESS_PMS_START,
292 SystemClock.uptimeMillis());
293
294 if (mSdkVersion <= 0) {
295 Log.w(TAG, "**** ro.build.version.sdk not set!");
296 }
297
298 mContext = context;
299 mFactoryTest = factoryTest;
300 mMetrics = new DisplayMetrics();
301 mSettings = new Settings();
302 mSettings.addSharedUserLP("android.uid.system",
303 Process.SYSTEM_UID, ApplicationInfo.FLAG_SYSTEM);
304 mSettings.addSharedUserLP("android.uid.phone",
305 MULTIPLE_APPLICATION_UIDS
306 ? RADIO_UID : FIRST_APPLICATION_UID,
307 ApplicationInfo.FLAG_SYSTEM);
308
309 String separateProcesses = SystemProperties.get("debug.separate_processes");
310 if (separateProcesses != null && separateProcesses.length() > 0) {
311 if ("*".equals(separateProcesses)) {
312 mDefParseFlags = PackageParser.PARSE_IGNORE_PROCESSES;
313 mSeparateProcesses = null;
314 Log.w(TAG, "Running with debug.separate_processes: * (ALL)");
315 } else {
316 mDefParseFlags = 0;
317 mSeparateProcesses = separateProcesses.split(",");
318 Log.w(TAG, "Running with debug.separate_processes: "
319 + separateProcesses);
320 }
321 } else {
322 mDefParseFlags = 0;
323 mSeparateProcesses = null;
324 }
325
326 Installer installer = new Installer();
327 // Little hacky thing to check if installd is here, to determine
328 // whether we are running on the simulator and thus need to take
329 // care of building the /data file structure ourself.
330 // (apparently the sim now has a working installer)
331 if (installer.ping() && Process.supportsProcesses()) {
332 mInstaller = installer;
333 } else {
334 mInstaller = null;
335 }
336
337 WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
338 Display d = wm.getDefaultDisplay();
339 d.getMetrics(mMetrics);
340
341 synchronized (mInstallLock) {
342 synchronized (mPackages) {
343 mHandlerThread.start();
344 mHandler = new Handler(mHandlerThread.getLooper());
345
346 File dataDir = Environment.getDataDirectory();
347 mAppDataDir = new File(dataDir, "data");
348 mDrmAppPrivateInstallDir = new File(dataDir, "app-private");
349
350 if (mInstaller == null) {
351 // Make sure these dirs exist, when we are running in
352 // the simulator.
353 // Make a wide-open directory for random misc stuff.
354 File miscDir = new File(dataDir, "misc");
355 miscDir.mkdirs();
356 mAppDataDir.mkdirs();
357 mDrmAppPrivateInstallDir.mkdirs();
358 }
359
360 readPermissions();
361
362 mRestoredSettings = mSettings.readLP();
363 long startTime = SystemClock.uptimeMillis();
364
365 EventLog.writeEvent(LOG_BOOT_PROGRESS_PMS_SYSTEM_SCAN_START,
366 startTime);
367
368 int scanMode = SCAN_MONITOR;
369
370 final HashSet<String> libFiles = new HashSet<String>();
371
372 mFrameworkDir = new File(Environment.getRootDirectory(), "framework");
373
374 if (mInstaller != null) {
375 /**
376 * Out of paranoia, ensure that everything in the boot class
377 * path has been dexed.
378 */
379 String bootClassPath = System.getProperty("java.boot.class.path");
380 if (bootClassPath != null) {
381 String[] paths = splitString(bootClassPath, ':');
382 for (int i=0; i<paths.length; i++) {
383 try {
384 if (dalvik.system.DexFile.isDexOptNeeded(paths[i])) {
385 libFiles.add(paths[i]);
386 mInstaller.dexopt(paths[i], Process.SYSTEM_UID, true);
387 }
388 } catch (FileNotFoundException e) {
389 Log.w(TAG, "Boot class path not found: " + paths[i]);
390 } catch (IOException e) {
391 Log.w(TAG, "Exception reading boot class path: " + paths[i], e);
392 }
393 }
394 } else {
395 Log.w(TAG, "No BOOTCLASSPATH found!");
396 }
397
398 /**
399 * Also ensure all external libraries have had dexopt run on them.
400 */
401 if (mSharedLibraries.size() > 0) {
402 Iterator<String> libs = mSharedLibraries.values().iterator();
403 while (libs.hasNext()) {
404 String lib = libs.next();
405 try {
406 if (dalvik.system.DexFile.isDexOptNeeded(lib)) {
407 libFiles.add(lib);
408 mInstaller.dexopt(lib, Process.SYSTEM_UID, true);
409 }
410 } catch (FileNotFoundException e) {
411 Log.w(TAG, "Library not found: " + lib);
412 } catch (IOException e) {
413 Log.w(TAG, "Exception reading library: " + lib, e);
414 }
415 }
416 }
417
418 // Gross hack for now: we know this file doesn't contain any
419 // code, so don't dexopt it to avoid the resulting log spew.
420 libFiles.add(mFrameworkDir.getPath() + "/framework-res.apk");
421
422 /**
423 * And there are a number of commands implemented in Java, which
424 * we currently need to do the dexopt on so that they can be
425 * run from a non-root shell.
426 */
427 String[] frameworkFiles = mFrameworkDir.list();
428 if (frameworkFiles != null && mInstaller != null) {
429 for (int i=0; i<frameworkFiles.length; i++) {
430 File libPath = new File(mFrameworkDir, frameworkFiles[i]);
431 String path = libPath.getPath();
432 // Skip the file if we alrady did it.
433 if (libFiles.contains(path)) {
434 continue;
435 }
436 // Skip the file if it is not a type we want to dexopt.
437 if (!path.endsWith(".apk") && !path.endsWith(".jar")) {
438 continue;
439 }
440 try {
441 if (dalvik.system.DexFile.isDexOptNeeded(path)) {
442 mInstaller.dexopt(path, Process.SYSTEM_UID, true);
443 }
444 } catch (FileNotFoundException e) {
445 Log.w(TAG, "Jar not found: " + path);
446 } catch (IOException e) {
447 Log.w(TAG, "Exception reading jar: " + path, e);
448 }
449 }
450 }
451 }
452
453 mFrameworkInstallObserver = new AppDirObserver(
454 mFrameworkDir.getPath(), OBSERVER_EVENTS, true);
455 mFrameworkInstallObserver.startWatching();
456 scanDirLI(mFrameworkDir, PackageParser.PARSE_IS_SYSTEM,
457 scanMode | SCAN_NO_DEX);
458 mSystemAppDir = new File(Environment.getRootDirectory(), "app");
459 mSystemInstallObserver = new AppDirObserver(
460 mSystemAppDir.getPath(), OBSERVER_EVENTS, true);
461 mSystemInstallObserver.startWatching();
462 scanDirLI(mSystemAppDir, PackageParser.PARSE_IS_SYSTEM, scanMode);
463 mAppInstallDir = new File(dataDir, "app");
464 if (mInstaller == null) {
465 // Make sure these dirs exist, when we are running in
466 // the simulator.
467 mAppInstallDir.mkdirs(); // scanDirLI() assumes this dir exists
468 }
469 //look for any incomplete package installations
470 ArrayList<String> deletePkgsList = mSettings.getListOfIncompleteInstallPackages();
471 //clean up list
472 for(int i = 0; i < deletePkgsList.size(); i++) {
473 //clean up here
474 cleanupInstallFailedPackage(deletePkgsList.get(i));
475 }
476 //delete tmp files
477 deleteTempPackageFiles();
478
479 EventLog.writeEvent(LOG_BOOT_PROGRESS_PMS_DATA_SCAN_START,
480 SystemClock.uptimeMillis());
481 mAppInstallObserver = new AppDirObserver(
482 mAppInstallDir.getPath(), OBSERVER_EVENTS, false);
483 mAppInstallObserver.startWatching();
484 scanDirLI(mAppInstallDir, 0, scanMode);
485
486 mDrmAppInstallObserver = new AppDirObserver(
487 mDrmAppPrivateInstallDir.getPath(), OBSERVER_EVENTS, false);
488 mDrmAppInstallObserver.startWatching();
489 scanDirLI(mDrmAppPrivateInstallDir, 0, scanMode);
490
491 EventLog.writeEvent(LOG_BOOT_PROGRESS_PMS_SCAN_END,
492 SystemClock.uptimeMillis());
493 Log.i(TAG, "Time to scan packages: "
494 + ((SystemClock.uptimeMillis()-startTime)/1000f)
495 + " seconds");
496
497 updatePermissionsLP();
498
499 mSettings.writeLP();
500
501 EventLog.writeEvent(LOG_BOOT_PROGRESS_PMS_READY,
502 SystemClock.uptimeMillis());
503
504 // Now after opening every single application zip, make sure they
505 // are all flushed. Not really needed, but keeps things nice and
506 // tidy.
507 Runtime.getRuntime().gc();
508 } // synchronized (mPackages)
509 } // synchronized (mInstallLock)
510 }
511
512 @Override
513 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
514 throws RemoteException {
515 try {
516 return super.onTransact(code, data, reply, flags);
517 } catch (RuntimeException e) {
518 if (!(e instanceof SecurityException) && !(e instanceof IllegalArgumentException)) {
519 Log.e(TAG, "Package Manager Crash", e);
520 }
521 throw e;
522 }
523 }
524
525 void cleanupInstallFailedPackage(String packageName) {
526 if (mInstaller != null) {
527 int retCode = mInstaller.remove(packageName);
528 if (retCode < 0) {
529 Log.w(TAG, "Couldn't remove app data directory for package: "
530 + packageName + ", retcode=" + retCode);
531 }
532 } else {
533 //for emulator
534 PackageParser.Package pkg = mPackages.get(packageName);
535 File dataDir = new File(pkg.applicationInfo.dataDir);
536 dataDir.delete();
537 }
538 mSettings.removePackageLP(packageName);
539 }
540
541 void readPermissions() {
542 // Read permissions from .../etc/permission directory.
543 File libraryDir = new File(Environment.getRootDirectory(), "etc/permissions");
544 if (!libraryDir.exists() || !libraryDir.isDirectory()) {
545 Log.w(TAG, "No directory " + libraryDir + ", skipping");
546 return;
547 }
548 if (!libraryDir.canRead()) {
549 Log.w(TAG, "Directory " + libraryDir + " cannot be read");
550 return;
551 }
552
553 // Iterate over the files in the directory and scan .xml files
554 for (File f : libraryDir.listFiles()) {
555 // We'll read platform.xml last
556 if (f.getPath().endsWith("etc/permissions/platform.xml")) {
557 continue;
558 }
559
560 if (!f.getPath().endsWith(".xml")) {
561 Log.i(TAG, "Non-xml file " + f + " in " + libraryDir + " directory, ignoring");
562 continue;
563 }
564 if (!f.canRead()) {
565 Log.w(TAG, "Permissions library file " + f + " cannot be read");
566 continue;
567 }
568
569 readPermissionsFromXml(f);
570 }
571
572 // Read permissions from .../etc/permissions/platform.xml last so it will take precedence
573 final File permFile = new File(Environment.getRootDirectory(),
574 "etc/permissions/platform.xml");
575 readPermissionsFromXml(permFile);
576 }
577
578 private void readPermissionsFromXml(File permFile) {
579 FileReader permReader = null;
580 try {
581 permReader = new FileReader(permFile);
582 } catch (FileNotFoundException e) {
583 Log.w(TAG, "Couldn't find or open permissions file " + permFile);
584 return;
585 }
586
587 try {
588 XmlPullParser parser = Xml.newPullParser();
589 parser.setInput(permReader);
590
591 XmlUtils.beginDocument(parser, "permissions");
592
593 while (true) {
594 XmlUtils.nextElement(parser);
595 if (parser.getEventType() == XmlPullParser.END_DOCUMENT) {
596 break;
597 }
598
599 String name = parser.getName();
600 if ("group".equals(name)) {
601 String gidStr = parser.getAttributeValue(null, "gid");
602 if (gidStr != null) {
603 int gid = Integer.parseInt(gidStr);
604 mGlobalGids = appendInt(mGlobalGids, gid);
605 } else {
606 Log.w(TAG, "<group> without gid at "
607 + parser.getPositionDescription());
608 }
609
610 XmlUtils.skipCurrentTag(parser);
611 continue;
612 } else if ("permission".equals(name)) {
613 String perm = parser.getAttributeValue(null, "name");
614 if (perm == null) {
615 Log.w(TAG, "<permission> without name at "
616 + parser.getPositionDescription());
617 XmlUtils.skipCurrentTag(parser);
618 continue;
619 }
620 perm = perm.intern();
621 readPermission(parser, perm);
622
623 } else if ("assign-permission".equals(name)) {
624 String perm = parser.getAttributeValue(null, "name");
625 if (perm == null) {
626 Log.w(TAG, "<assign-permission> without name at "
627 + parser.getPositionDescription());
628 XmlUtils.skipCurrentTag(parser);
629 continue;
630 }
631 String uidStr = parser.getAttributeValue(null, "uid");
632 if (uidStr == null) {
633 Log.w(TAG, "<assign-permission> without uid at "
634 + parser.getPositionDescription());
635 XmlUtils.skipCurrentTag(parser);
636 continue;
637 }
638 int uid = Process.getUidForName(uidStr);
639 if (uid < 0) {
640 Log.w(TAG, "<assign-permission> with unknown uid \""
641 + uidStr + "\" at "
642 + parser.getPositionDescription());
643 XmlUtils.skipCurrentTag(parser);
644 continue;
645 }
646 perm = perm.intern();
647 HashSet<String> perms = mSystemPermissions.get(uid);
648 if (perms == null) {
649 perms = new HashSet<String>();
650 mSystemPermissions.put(uid, perms);
651 }
652 perms.add(perm);
653 XmlUtils.skipCurrentTag(parser);
654
655 } else if ("library".equals(name)) {
656 String lname = parser.getAttributeValue(null, "name");
657 String lfile = parser.getAttributeValue(null, "file");
658 if (lname == null) {
659 Log.w(TAG, "<library> without name at "
660 + parser.getPositionDescription());
661 } else if (lfile == null) {
662 Log.w(TAG, "<library> without file at "
663 + parser.getPositionDescription());
664 } else {
665 Log.i(TAG, "Got library " + lname + " in " + lfile);
666 this.mSharedLibraries.put(lname, lfile);
667 }
668 XmlUtils.skipCurrentTag(parser);
669 continue;
670
671 } else {
672 XmlUtils.skipCurrentTag(parser);
673 continue;
674 }
675
676 }
677 } catch (XmlPullParserException e) {
678 Log.w(TAG, "Got execption parsing permissions.", e);
679 } catch (IOException e) {
680 Log.w(TAG, "Got execption parsing permissions.", e);
681 }
682 }
683
684 void readPermission(XmlPullParser parser, String name)
685 throws IOException, XmlPullParserException {
686
687 name = name.intern();
688
689 BasePermission bp = mSettings.mPermissions.get(name);
690 if (bp == null) {
691 bp = new BasePermission(name, null, BasePermission.TYPE_BUILTIN);
692 mSettings.mPermissions.put(name, bp);
693 }
694 int outerDepth = parser.getDepth();
695 int type;
696 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
697 && (type != XmlPullParser.END_TAG
698 || parser.getDepth() > outerDepth)) {
699 if (type == XmlPullParser.END_TAG
700 || type == XmlPullParser.TEXT) {
701 continue;
702 }
703
704 String tagName = parser.getName();
705 if ("group".equals(tagName)) {
706 String gidStr = parser.getAttributeValue(null, "gid");
707 if (gidStr != null) {
708 int gid = Process.getGidForName(gidStr);
709 bp.gids = appendInt(bp.gids, gid);
710 } else {
711 Log.w(TAG, "<group> without gid at "
712 + parser.getPositionDescription());
713 }
714 }
715 XmlUtils.skipCurrentTag(parser);
716 }
717 }
718
719 static int[] appendInt(int[] cur, int val) {
720 if (cur == null) {
721 return new int[] { val };
722 }
723 final int N = cur.length;
724 for (int i=0; i<N; i++) {
725 if (cur[i] == val) {
726 return cur;
727 }
728 }
729 int[] ret = new int[N+1];
730 System.arraycopy(cur, 0, ret, 0, N);
731 ret[N] = val;
732 return ret;
733 }
734
735 static int[] appendInts(int[] cur, int[] add) {
736 if (add == null) return cur;
737 if (cur == null) return add;
738 final int N = add.length;
739 for (int i=0; i<N; i++) {
740 cur = appendInt(cur, add[i]);
741 }
742 return cur;
743 }
744
745 PackageInfo generatePackageInfo(PackageParser.Package p, int flags) {
746 final PackageSetting ps = (PackageSetting)p.mExtras;
747 if (ps == null) {
748 return null;
749 }
750 final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps;
751 return PackageParser.generatePackageInfo(p, gp.gids, flags);
752 }
753
754 public PackageInfo getPackageInfo(String packageName, int flags) {
755 synchronized (mPackages) {
756 PackageParser.Package p = mPackages.get(packageName);
757 if (Config.LOGV) Log.v(
758 TAG, "getApplicationInfo " + packageName
759 + ": " + p);
760 if (p != null) {
761 return generatePackageInfo(p, flags);
762 }
763 if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) {
764 return generatePackageInfoFromSettingsLP(packageName, flags);
765 }
766 }
767 return null;
768 }
769
770 public int getPackageUid(String packageName) {
771 synchronized (mPackages) {
772 PackageParser.Package p = mPackages.get(packageName);
773 if(p != null) {
774 return p.applicationInfo.uid;
775 }
776 PackageSetting ps = mSettings.mPackages.get(packageName);
777 if((ps == null) || (ps.pkg == null) || (ps.pkg.applicationInfo == null)) {
778 return -1;
779 }
780 p = ps.pkg;
781 return p != null ? p.applicationInfo.uid : -1;
782 }
783 }
784
785 public int[] getPackageGids(String packageName) {
786 synchronized (mPackages) {
787 PackageParser.Package p = mPackages.get(packageName);
788 if (Config.LOGV) Log.v(
789 TAG, "getApplicationInfo " + packageName
790 + ": " + p);
791 if (p != null) {
792 final PackageSetting ps = (PackageSetting)p.mExtras;
793 final SharedUserSetting suid = ps.sharedUser;
794 return suid != null ? suid.gids : ps.gids;
795 }
796 }
797 // stupid thing to indicate an error.
798 return new int[0];
799 }
800
801 public PermissionInfo getPermissionInfo(String name, int flags) {
802 synchronized (mPackages) {
803 final BasePermission p = mSettings.mPermissions.get(name);
804 if (p != null && p.perm != null) {
805 return PackageParser.generatePermissionInfo(p.perm, flags);
806 }
807 return null;
808 }
809 }
810
811 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags) {
812 synchronized (mPackages) {
813 ArrayList<PermissionInfo> out = new ArrayList<PermissionInfo>(10);
814 for (BasePermission p : mSettings.mPermissions.values()) {
815 if (group == null) {
816 if (p.perm.info.group == null) {
817 out.add(PackageParser.generatePermissionInfo(p.perm, flags));
818 }
819 } else {
820 if (group.equals(p.perm.info.group)) {
821 out.add(PackageParser.generatePermissionInfo(p.perm, flags));
822 }
823 }
824 }
825
826 if (out.size() > 0) {
827 return out;
828 }
829 return mPermissionGroups.containsKey(group) ? out : null;
830 }
831 }
832
833 public PermissionGroupInfo getPermissionGroupInfo(String name, int flags) {
834 synchronized (mPackages) {
835 return PackageParser.generatePermissionGroupInfo(
836 mPermissionGroups.get(name), flags);
837 }
838 }
839
840 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
841 synchronized (mPackages) {
842 final int N = mPermissionGroups.size();
843 ArrayList<PermissionGroupInfo> out
844 = new ArrayList<PermissionGroupInfo>(N);
845 for (PackageParser.PermissionGroup pg : mPermissionGroups.values()) {
846 out.add(PackageParser.generatePermissionGroupInfo(pg, flags));
847 }
848 return out;
849 }
850 }
851
852 private ApplicationInfo generateApplicationInfoFromSettingsLP(String packageName, int flags) {
853 PackageSetting ps = mSettings.mPackages.get(packageName);
854 if(ps != null) {
855 if(ps.pkg == null) {
856 PackageInfo pInfo = generatePackageInfoFromSettingsLP(packageName, flags);
857 if(pInfo != null) {
858 return pInfo.applicationInfo;
859 }
860 return null;
861 }
862 return PackageParser.generateApplicationInfo(ps.pkg, flags);
863 }
864 return null;
865 }
866
867 private PackageInfo generatePackageInfoFromSettingsLP(String packageName, int flags) {
868 PackageSetting ps = mSettings.mPackages.get(packageName);
869 if(ps != null) {
870 if(ps.pkg == null) {
871 ps.pkg = new PackageParser.Package(packageName);
872 ps.pkg.applicationInfo.packageName = packageName;
873 }
874 return generatePackageInfo(ps.pkg, flags);
875 }
876 return null;
877 }
878
879 public ApplicationInfo getApplicationInfo(String packageName, int flags) {
880 synchronized (mPackages) {
881 PackageParser.Package p = mPackages.get(packageName);
882 if (Config.LOGV) Log.v(
883 TAG, "getApplicationInfo " + packageName
884 + ": " + p);
885 if (p != null) {
886 // Note: isEnabledLP() does not apply here - always return info
887 return PackageParser.generateApplicationInfo(p, flags);
888 }
889 if ("android".equals(packageName)||"system".equals(packageName)) {
890 return mAndroidApplication;
891 }
892 if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) {
893 return generateApplicationInfoFromSettingsLP(packageName, flags);
894 }
895 }
896 return null;
897 }
898
899
900 public void freeStorageAndNotify(final long freeStorageSize, final IPackageDataObserver observer) {
901 mContext.enforceCallingOrSelfPermission(
902 android.Manifest.permission.CLEAR_APP_CACHE, null);
903 // Queue up an async operation since clearing cache may take a little while.
904 mHandler.post(new Runnable() {
905 public void run() {
906 mHandler.removeCallbacks(this);
907 int retCode = -1;
908 if (mInstaller != null) {
909 retCode = mInstaller.freeCache(freeStorageSize);
910 if (retCode < 0) {
911 Log.w(TAG, "Couldn't clear application caches");
912 }
913 } //end if mInstaller
914 if (observer != null) {
915 try {
916 observer.onRemoveCompleted(null, (retCode >= 0));
917 } catch (RemoteException e) {
918 Log.w(TAG, "RemoveException when invoking call back");
919 }
920 }
921 }
922 });
923 }
924
925 public void freeStorage(final long freeStorageSize, final PendingIntent opFinishedIntent) {
926 mContext.enforceCallingOrSelfPermission(
927 android.Manifest.permission.CLEAR_APP_CACHE, null);
928 // Queue up an async operation since clearing cache may take a little while.
929 mHandler.post(new Runnable() {
930 public void run() {
931 mHandler.removeCallbacks(this);
932 int retCode = -1;
933 if (mInstaller != null) {
934 retCode = mInstaller.freeCache(freeStorageSize);
935 if (retCode < 0) {
936 Log.w(TAG, "Couldn't clear application caches");
937 }
938 }
939 if(opFinishedIntent != null) {
940 try {
941 // Callback via pending intent
942 opFinishedIntent.send((retCode >= 0) ? 1 : 0);
943 } catch (CanceledException e1) {
944 Log.i(TAG, "Failed to send pending intent");
945 }
946 }
947 }
948 });
949 }
950
951 public ActivityInfo getActivityInfo(ComponentName component, int flags) {
952 synchronized (mPackages) {
953 PackageParser.Activity a = mActivities.mActivities.get(component);
954 if (Config.LOGV) Log.v(
955 TAG, "getActivityInfo " + component + ": " + a);
956 if (a != null && mSettings.isEnabledLP(a.info, flags)) {
957 return PackageParser.generateActivityInfo(a, flags);
958 }
959 if (mResolveComponentName.equals(component)) {
960 return mResolveActivity;
961 }
962 }
963 return null;
964 }
965
966 public ActivityInfo getReceiverInfo(ComponentName component, int flags) {
967 synchronized (mPackages) {
968 PackageParser.Activity a = mReceivers.mActivities.get(component);
969 if (Config.LOGV) Log.v(
970 TAG, "getReceiverInfo " + component + ": " + a);
971 if (a != null && mSettings.isEnabledLP(a.info, flags)) {
972 return PackageParser.generateActivityInfo(a, flags);
973 }
974 }
975 return null;
976 }
977
978 public ServiceInfo getServiceInfo(ComponentName component, int flags) {
979 synchronized (mPackages) {
980 PackageParser.Service s = mServices.mServices.get(component);
981 if (Config.LOGV) Log.v(
982 TAG, "getServiceInfo " + component + ": " + s);
983 if (s != null && mSettings.isEnabledLP(s.info, flags)) {
984 return PackageParser.generateServiceInfo(s, flags);
985 }
986 }
987 return null;
988 }
989
990 public String[] getSystemSharedLibraryNames() {
991 Set<String> libSet;
992 synchronized (mPackages) {
993 libSet = mSharedLibraries.keySet();
994 }
995 int size = libSet.size();
996 if (size > 0) {
997 String[] libs = new String[size];
998 libSet.toArray(libs);
999 return libs;
1000 }
1001 return null;
1002 }
1003
1004 public int checkPermission(String permName, String pkgName) {
1005 synchronized (mPackages) {
1006 PackageParser.Package p = mPackages.get(pkgName);
1007 if (p != null && p.mExtras != null) {
1008 PackageSetting ps = (PackageSetting)p.mExtras;
1009 if (ps.sharedUser != null) {
1010 if (ps.sharedUser.grantedPermissions.contains(permName)) {
1011 return PackageManager.PERMISSION_GRANTED;
1012 }
1013 } else if (ps.grantedPermissions.contains(permName)) {
1014 return PackageManager.PERMISSION_GRANTED;
1015 }
1016 }
1017 }
1018 return PackageManager.PERMISSION_DENIED;
1019 }
1020
1021 public int checkUidPermission(String permName, int uid) {
1022 synchronized (mPackages) {
1023 Object obj = mSettings.getUserIdLP(uid);
1024 if (obj != null) {
1025 if (obj instanceof SharedUserSetting) {
1026 SharedUserSetting sus = (SharedUserSetting)obj;
1027 if (sus.grantedPermissions.contains(permName)) {
1028 return PackageManager.PERMISSION_GRANTED;
1029 }
1030 } else if (obj instanceof PackageSetting) {
1031 PackageSetting ps = (PackageSetting)obj;
1032 if (ps.grantedPermissions.contains(permName)) {
1033 return PackageManager.PERMISSION_GRANTED;
1034 }
1035 }
1036 } else {
1037 HashSet<String> perms = mSystemPermissions.get(uid);
1038 if (perms != null && perms.contains(permName)) {
1039 return PackageManager.PERMISSION_GRANTED;
1040 }
1041 }
1042 }
1043 return PackageManager.PERMISSION_DENIED;
1044 }
1045
1046 private BasePermission findPermissionTreeLP(String permName) {
1047 for(BasePermission bp : mSettings.mPermissionTrees.values()) {
1048 if (permName.startsWith(bp.name) &&
1049 permName.length() > bp.name.length() &&
1050 permName.charAt(bp.name.length()) == '.') {
1051 return bp;
1052 }
1053 }
1054 return null;
1055 }
1056
1057 private BasePermission checkPermissionTreeLP(String permName) {
1058 if (permName != null) {
1059 BasePermission bp = findPermissionTreeLP(permName);
1060 if (bp != null) {
1061 if (bp.uid == Binder.getCallingUid()) {
1062 return bp;
1063 }
1064 throw new SecurityException("Calling uid "
1065 + Binder.getCallingUid()
1066 + " is not allowed to add to permission tree "
1067 + bp.name + " owned by uid " + bp.uid);
1068 }
1069 }
1070 throw new SecurityException("No permission tree found for " + permName);
1071 }
1072
1073 public boolean addPermission(PermissionInfo info) {
1074 synchronized (mPackages) {
1075 if (info.labelRes == 0 && info.nonLocalizedLabel == null) {
1076 throw new SecurityException("Label must be specified in permission");
1077 }
1078 BasePermission tree = checkPermissionTreeLP(info.name);
1079 BasePermission bp = mSettings.mPermissions.get(info.name);
1080 boolean added = bp == null;
1081 if (added) {
1082 bp = new BasePermission(info.name, tree.sourcePackage,
1083 BasePermission.TYPE_DYNAMIC);
1084 } else if (bp.type != BasePermission.TYPE_DYNAMIC) {
1085 throw new SecurityException(
1086 "Not allowed to modify non-dynamic permission "
1087 + info.name);
1088 }
1089 bp.perm = new PackageParser.Permission(tree.perm.owner,
1090 new PermissionInfo(info));
1091 bp.perm.info.packageName = tree.perm.info.packageName;
1092 bp.uid = tree.uid;
1093 if (added) {
1094 mSettings.mPermissions.put(info.name, bp);
1095 }
1096 mSettings.writeLP();
1097 return added;
1098 }
1099 }
1100
1101 public void removePermission(String name) {
1102 synchronized (mPackages) {
1103 checkPermissionTreeLP(name);
1104 BasePermission bp = mSettings.mPermissions.get(name);
1105 if (bp != null) {
1106 if (bp.type != BasePermission.TYPE_DYNAMIC) {
1107 throw new SecurityException(
1108 "Not allowed to modify non-dynamic permission "
1109 + name);
1110 }
1111 mSettings.mPermissions.remove(name);
1112 mSettings.writeLP();
1113 }
1114 }
1115 }
1116
1117 public int checkSignatures(String pkg1, String pkg2) {
1118 synchronized (mPackages) {
1119 PackageParser.Package p1 = mPackages.get(pkg1);
1120 PackageParser.Package p2 = mPackages.get(pkg2);
1121 if (p1 == null || p1.mExtras == null
1122 || p2 == null || p2.mExtras == null) {
1123 return PackageManager.SIGNATURE_UNKNOWN_PACKAGE;
1124 }
1125 return checkSignaturesLP(p1, p2);
1126 }
1127 }
1128
1129 int checkSignaturesLP(PackageParser.Package p1, PackageParser.Package p2) {
1130 if (p1.mSignatures == null) {
1131 return p2.mSignatures == null
1132 ? PackageManager.SIGNATURE_NEITHER_SIGNED
1133 : PackageManager.SIGNATURE_FIRST_NOT_SIGNED;
1134 }
1135 if (p2.mSignatures == null) {
1136 return PackageManager.SIGNATURE_SECOND_NOT_SIGNED;
1137 }
1138 final int N1 = p1.mSignatures.length;
1139 final int N2 = p2.mSignatures.length;
1140 for (int i=0; i<N1; i++) {
1141 boolean match = false;
1142 for (int j=0; j<N2; j++) {
1143 if (p1.mSignatures[i].equals(p2.mSignatures[j])) {
1144 match = true;
1145 break;
1146 }
1147 }
1148 if (!match) {
1149 return PackageManager.SIGNATURE_NO_MATCH;
1150 }
1151 }
1152 return PackageManager.SIGNATURE_MATCH;
1153 }
1154
1155 public String[] getPackagesForUid(int uid) {
1156 synchronized (mPackages) {
1157 Object obj = mSettings.getUserIdLP(uid);
1158 if (obj instanceof SharedUserSetting) {
1159 SharedUserSetting sus = (SharedUserSetting)obj;
1160 final int N = sus.packages.size();
1161 String[] res = new String[N];
1162 Iterator<PackageSetting> it = sus.packages.iterator();
1163 int i=0;
1164 while (it.hasNext()) {
1165 res[i++] = it.next().name;
1166 }
1167 return res;
1168 } else if (obj instanceof PackageSetting) {
1169 PackageSetting ps = (PackageSetting)obj;
1170 return new String[] { ps.name };
1171 }
1172 }
1173 return null;
1174 }
1175
1176 public String getNameForUid(int uid) {
1177 synchronized (mPackages) {
1178 Object obj = mSettings.getUserIdLP(uid);
1179 if (obj instanceof SharedUserSetting) {
1180 SharedUserSetting sus = (SharedUserSetting)obj;
1181 return sus.name + ":" + sus.userId;
1182 } else if (obj instanceof PackageSetting) {
1183 PackageSetting ps = (PackageSetting)obj;
1184 return ps.name;
1185 }
1186 }
1187 return null;
1188 }
1189
1190 public int getUidForSharedUser(String sharedUserName) {
1191 if(sharedUserName == null) {
1192 return -1;
1193 }
1194 synchronized (mPackages) {
1195 SharedUserSetting suid = mSettings.getSharedUserLP(sharedUserName, 0, false);
1196 if(suid == null) {
1197 return -1;
1198 }
1199 return suid.userId;
1200 }
1201 }
1202
1203 public ResolveInfo resolveIntent(Intent intent, String resolvedType,
1204 int flags) {
1205 List<ResolveInfo> query = queryIntentActivities(intent, resolvedType, flags);
1206 if (query != null) {
1207 final int N = query.size();
1208 if (N == 1) {
1209 return query.get(0);
1210 } else if (N > 1) {
1211 // If there is more than one activity with the same priority,
1212 // then let the user decide between them.
1213 ResolveInfo r0 = query.get(0);
1214 ResolveInfo r1 = query.get(1);
1215 if (false) {
1216 System.out.println(r0.activityInfo.name +
1217 "=" + r0.priority + " vs " +
1218 r1.activityInfo.name +
1219 "=" + r1.priority);
1220 }
1221 // If the first activity has a higher priority, or a different
1222 // default, then it is always desireable to pick it.
1223 if (r0.priority != r1.priority
1224 || r0.preferredOrder != r1.preferredOrder
1225 || r0.isDefault != r1.isDefault) {
1226 return query.get(0);
1227 }
1228 // If we have saved a preference for a preferred activity for
1229 // this Intent, use that.
1230 ResolveInfo ri = findPreferredActivity(intent, resolvedType,
1231 flags, query, r0.priority);
1232 if (ri != null) {
1233 return ri;
1234 }
1235 return mResolveInfo;
1236 }
1237 }
1238 return null;
1239 }
1240
1241 ResolveInfo findPreferredActivity(Intent intent, String resolvedType,
1242 int flags, List<ResolveInfo> query, int priority) {
1243 synchronized (mPackages) {
1244 if (DEBUG_PREFERRED) intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION);
1245 List<PreferredActivity> prefs =
1246 mSettings.mPreferredActivities.queryIntent(null,
1247 intent, resolvedType,
1248 (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0);
1249 if (prefs != null && prefs.size() > 0) {
1250 // First figure out how good the original match set is.
1251 // We will only allow preferred activities that came
1252 // from the same match quality.
1253 int match = 0;
1254 final int N = query.size();
1255 if (DEBUG_PREFERRED) Log.v(TAG, "Figuring out best match...");
1256 for (int j=0; j<N; j++) {
1257 ResolveInfo ri = query.get(j);
1258 if (DEBUG_PREFERRED) Log.v(TAG, "Match for " + ri.activityInfo
1259 + ": 0x" + Integer.toHexString(match));
1260 if (ri.match > match) match = ri.match;
1261 }
1262 if (DEBUG_PREFERRED) Log.v(TAG, "Best match: 0x"
1263 + Integer.toHexString(match));
1264 match &= IntentFilter.MATCH_CATEGORY_MASK;
1265 final int M = prefs.size();
1266 for (int i=0; i<M; i++) {
1267 PreferredActivity pa = prefs.get(i);
1268 if (pa.mMatch != match) {
1269 continue;
1270 }
1271 ActivityInfo ai = getActivityInfo(pa.mActivity, flags);
1272 if (DEBUG_PREFERRED) {
1273 Log.v(TAG, "Got preferred activity:");
1274 ai.dump(new LogPrinter(Log.INFO, TAG), " ");
1275 }
1276 if (ai != null) {
1277 for (int j=0; j<N; j++) {
1278 ResolveInfo ri = query.get(j);
1279 if (!ri.activityInfo.applicationInfo.packageName
1280 .equals(ai.applicationInfo.packageName)) {
1281 continue;
1282 }
1283 if (!ri.activityInfo.name.equals(ai.name)) {
1284 continue;
1285 }
1286
1287 // Okay we found a previously set preferred app.
1288 // If the result set is different from when this
1289 // was created, we need to clear it and re-ask the
1290 // user their preference.
1291 if (!pa.sameSet(query, priority)) {
1292 Log.i(TAG, "Result set changed, dropping preferred activity for "
1293 + intent + " type " + resolvedType);
1294 mSettings.mPreferredActivities.removeFilter(pa);
1295 return null;
1296 }
1297
1298 // Yay!
1299 return ri;
1300 }
1301 }
1302 }
1303 }
1304 }
1305 return null;
1306 }
1307
1308 public List<ResolveInfo> queryIntentActivities(Intent intent,
1309 String resolvedType, int flags) {
1310 ComponentName comp = intent.getComponent();
1311 if (comp != null) {
1312 List<ResolveInfo> list = new ArrayList<ResolveInfo>(1);
1313 ActivityInfo ai = getActivityInfo(comp, flags);
1314 if (ai != null) {
1315 ResolveInfo ri = new ResolveInfo();
1316 ri.activityInfo = ai;
1317 list.add(ri);
1318 }
1319 return list;
1320 }
1321
1322 synchronized (mPackages) {
1323 return (List<ResolveInfo>)mActivities.
1324 queryIntent(null, intent, resolvedType, flags);
1325 }
1326 }
1327
1328 public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller,
1329 Intent[] specifics, String[] specificTypes, Intent intent,
1330 String resolvedType, int flags) {
1331 final String resultsAction = intent.getAction();
1332
1333 List<ResolveInfo> results = queryIntentActivities(
1334 intent, resolvedType, flags|PackageManager.GET_RESOLVED_FILTER);
1335 if (Config.LOGV) Log.v(TAG, "Query " + intent + ": " + results);
1336
1337 int specificsPos = 0;
1338 int N;
1339
1340 // todo: note that the algorithm used here is O(N^2). This
1341 // isn't a problem in our current environment, but if we start running
1342 // into situations where we have more than 5 or 10 matches then this
1343 // should probably be changed to something smarter...
1344
1345 // First we go through and resolve each of the specific items
1346 // that were supplied, taking care of removing any corresponding
1347 // duplicate items in the generic resolve list.
1348 if (specifics != null) {
1349 for (int i=0; i<specifics.length; i++) {
1350 final Intent sintent = specifics[i];
1351 if (sintent == null) {
1352 continue;
1353 }
1354
1355 if (Config.LOGV) Log.v(TAG, "Specific #" + i + ": " + sintent);
1356 String action = sintent.getAction();
1357 if (resultsAction != null && resultsAction.equals(action)) {
1358 // If this action was explicitly requested, then don't
1359 // remove things that have it.
1360 action = null;
1361 }
1362 ComponentName comp = sintent.getComponent();
1363 ResolveInfo ri = null;
1364 ActivityInfo ai = null;
1365 if (comp == null) {
1366 ri = resolveIntent(
1367 sintent,
1368 specificTypes != null ? specificTypes[i] : null,
1369 flags);
1370 if (ri == null) {
1371 continue;
1372 }
1373 if (ri == mResolveInfo) {
1374 // ACK! Must do something better with this.
1375 }
1376 ai = ri.activityInfo;
1377 comp = new ComponentName(ai.applicationInfo.packageName,
1378 ai.name);
1379 } else {
1380 ai = getActivityInfo(comp, flags);
1381 if (ai == null) {
1382 continue;
1383 }
1384 }
1385
1386 // Look for any generic query activities that are duplicates
1387 // of this specific one, and remove them from the results.
1388 if (Config.LOGV) Log.v(TAG, "Specific #" + i + ": " + ai);
1389 N = results.size();
1390 int j;
1391 for (j=specificsPos; j<N; j++) {
1392 ResolveInfo sri = results.get(j);
1393 if ((sri.activityInfo.name.equals(comp.getClassName())
1394 && sri.activityInfo.applicationInfo.packageName.equals(
1395 comp.getPackageName()))
1396 || (action != null && sri.filter.matchAction(action))) {
1397 results.remove(j);
1398 if (Config.LOGV) Log.v(
1399 TAG, "Removing duplicate item from " + j
1400 + " due to specific " + specificsPos);
1401 if (ri == null) {
1402 ri = sri;
1403 }
1404 j--;
1405 N--;
1406 }
1407 }
1408
1409 // Add this specific item to its proper place.
1410 if (ri == null) {
1411 ri = new ResolveInfo();
1412 ri.activityInfo = ai;
1413 }
1414 results.add(specificsPos, ri);
1415 ri.specificIndex = i;
1416 specificsPos++;
1417 }
1418 }
1419
1420 // Now we go through the remaining generic results and remove any
1421 // duplicate actions that are found here.
1422 N = results.size();
1423 for (int i=specificsPos; i<N-1; i++) {
1424 final ResolveInfo rii = results.get(i);
1425 if (rii.filter == null) {
1426 continue;
1427 }
1428
1429 // Iterate over all of the actions of this result's intent
1430 // filter... typically this should be just one.
1431 final Iterator<String> it = rii.filter.actionsIterator();
1432 if (it == null) {
1433 continue;
1434 }
1435 while (it.hasNext()) {
1436 final String action = it.next();
1437 if (resultsAction != null && resultsAction.equals(action)) {
1438 // If this action was explicitly requested, then don't
1439 // remove things that have it.
1440 continue;
1441 }
1442 for (int j=i+1; j<N; j++) {
1443 final ResolveInfo rij = results.get(j);
1444 if (rij.filter != null && rij.filter.hasAction(action)) {
1445 results.remove(j);
1446 if (Config.LOGV) Log.v(
1447 TAG, "Removing duplicate item from " + j
1448 + " due to action " + action + " at " + i);
1449 j--;
1450 N--;
1451 }
1452 }
1453 }
1454
1455 // If the caller didn't request filter information, drop it now
1456 // so we don't have to marshall/unmarshall it.
1457 if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) {
1458 rii.filter = null;
1459 }
1460 }
1461
1462 // Filter out the caller activity if so requested.
1463 if (caller != null) {
1464 N = results.size();
1465 for (int i=0; i<N; i++) {
1466 ActivityInfo ainfo = results.get(i).activityInfo;
1467 if (caller.getPackageName().equals(ainfo.applicationInfo.packageName)
1468 && caller.getClassName().equals(ainfo.name)) {
1469 results.remove(i);
1470 break;
1471 }
1472 }
1473 }
1474
1475 // If the caller didn't request filter information,
1476 // drop them now so we don't have to
1477 // marshall/unmarshall it.
1478 if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) {
1479 N = results.size();
1480 for (int i=0; i<N; i++) {
1481 results.get(i).filter = null;
1482 }
1483 }
1484
1485 if (Config.LOGV) Log.v(TAG, "Result: " + results);
1486 return results;
1487 }
1488
1489 public List<ResolveInfo> queryIntentReceivers(Intent intent,
1490 String resolvedType, int flags) {
1491 synchronized (mPackages) {
1492 return (List<ResolveInfo>)mReceivers.
1493 queryIntent(null, intent, resolvedType, flags);
1494 }
1495 }
1496
1497 public ResolveInfo resolveService(Intent intent, String resolvedType,
1498 int flags) {
1499 List<ResolveInfo> query = queryIntentServices(intent, resolvedType,
1500 flags);
1501 if (query != null) {
1502 if (query.size() >= 1) {
1503 // If there is more than one service with the same priority,
1504 // just arbitrarily pick the first one.
1505 return query.get(0);
1506 }
1507 }
1508 return null;
1509 }
1510
1511 public List<ResolveInfo> queryIntentServices(Intent intent,
1512 String resolvedType, int flags) {
1513 ComponentName comp = intent.getComponent();
1514 if (comp != null) {
1515 List<ResolveInfo> list = new ArrayList<ResolveInfo>(1);
1516 ServiceInfo si = getServiceInfo(comp, flags);
1517 if (si != null) {
1518 ResolveInfo ri = new ResolveInfo();
1519 ri.serviceInfo = si;
1520 list.add(ri);
1521 }
1522 return list;
1523 }
1524
1525 synchronized (mPackages) {
1526 return (List<ResolveInfo>)mServices.
1527 queryIntent(null, intent, resolvedType, flags);
1528 }
1529 }
1530
1531 public List<PackageInfo> getInstalledPackages(int flags) {
1532 ArrayList<PackageInfo> finalList = new ArrayList<PackageInfo>();
1533
1534 synchronized (mPackages) {
1535 if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) {
1536 Iterator<PackageSetting> i = mSettings.mPackages.values().iterator();
1537 while (i.hasNext()) {
1538 final PackageSetting ps = i.next();
1539 PackageInfo psPkg = generatePackageInfoFromSettingsLP(ps.name, flags);
1540 if(psPkg != null) {
1541 finalList.add(psPkg);
1542 }
1543 }
1544 }
1545 else {
1546 Iterator<PackageParser.Package> i = mPackages.values().iterator();
1547 while (i.hasNext()) {
1548 final PackageParser.Package p = i.next();
1549 if (p.applicationInfo != null) {
1550 PackageInfo pi = generatePackageInfo(p, flags);
1551 if(pi != null) {
1552 finalList.add(pi);
1553 }
1554 }
1555 }
1556 }
1557 }
1558 return finalList;
1559 }
1560
1561 public List<ApplicationInfo> getInstalledApplications(int flags) {
1562 ArrayList<ApplicationInfo> finalList = new ArrayList<ApplicationInfo>();
1563 synchronized(mPackages) {
1564 if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) {
1565 Iterator<PackageSetting> i = mSettings.mPackages.values().iterator();
1566 while (i.hasNext()) {
1567 final PackageSetting ps = i.next();
1568 ApplicationInfo ai = generateApplicationInfoFromSettingsLP(ps.name, flags);
1569 if(ai != null) {
1570 finalList.add(ai);
1571 }
1572 }
1573 }
1574 else {
1575 Iterator<PackageParser.Package> i = mPackages.values().iterator();
1576 while (i.hasNext()) {
1577 final PackageParser.Package p = i.next();
1578 if (p.applicationInfo != null) {
1579 ApplicationInfo ai = PackageParser.generateApplicationInfo(p, flags);
1580 if(ai != null) {
1581 finalList.add(ai);
1582 }
1583 }
1584 }
1585 }
1586 }
1587 return finalList;
1588 }
1589
1590 public List<ApplicationInfo> getPersistentApplications(int flags) {
1591 ArrayList<ApplicationInfo> finalList = new ArrayList<ApplicationInfo>();
1592
1593 synchronized (mPackages) {
1594 Iterator<PackageParser.Package> i = mPackages.values().iterator();
1595 while (i.hasNext()) {
1596 PackageParser.Package p = i.next();
1597 if (p.applicationInfo != null
1598 && (p.applicationInfo.flags&ApplicationInfo.FLAG_PERSISTENT) != 0
1599 && (!mSafeMode || (p.applicationInfo.flags
1600 &ApplicationInfo.FLAG_SYSTEM) != 0)) {
1601 finalList.add(p.applicationInfo);
1602 }
1603 }
1604 }
1605
1606 return finalList;
1607 }
1608
1609 public ProviderInfo resolveContentProvider(String name, int flags) {
1610 synchronized (mPackages) {
1611 final PackageParser.Provider provider = mProviders.get(name);
1612 return provider != null
1613 && mSettings.isEnabledLP(provider.info, flags)
1614 && (!mSafeMode || (provider.info.applicationInfo.flags
1615 &ApplicationInfo.FLAG_SYSTEM) != 0)
1616 ? PackageParser.generateProviderInfo(provider, flags)
1617 : null;
1618 }
1619 }
1620
1621 public void querySyncProviders(List outNames, List outInfo) {
1622 synchronized (mPackages) {
1623 Iterator<Map.Entry<String, PackageParser.Provider>> i
1624 = mProviders.entrySet().iterator();
1625
1626 while (i.hasNext()) {
1627 Map.Entry<String, PackageParser.Provider> entry = i.next();
1628 PackageParser.Provider p = entry.getValue();
1629
1630 if (p.syncable
1631 && (!mSafeMode || (p.info.applicationInfo.flags
1632 &ApplicationInfo.FLAG_SYSTEM) != 0)) {
1633 outNames.add(entry.getKey());
1634 outInfo.add(PackageParser.generateProviderInfo(p, 0));
1635 }
1636 }
1637 }
1638 }
1639
1640 public List<ProviderInfo> queryContentProviders(String processName,
1641 int uid, int flags) {
1642 ArrayList<ProviderInfo> finalList = null;
1643
1644 synchronized (mPackages) {
1645 Iterator<PackageParser.Provider> i = mProvidersByComponent.values().iterator();
1646 while (i.hasNext()) {
1647 PackageParser.Provider p = i.next();
1648 if (p.info.authority != null
1649 && (processName == null ||
1650 (p.info.processName.equals(processName)
1651 && p.info.applicationInfo.uid == uid))
1652 && mSettings.isEnabledLP(p.info, flags)
1653 && (!mSafeMode || (p.info.applicationInfo.flags
1654 &ApplicationInfo.FLAG_SYSTEM) != 0)) {
1655 if (finalList == null) {
1656 finalList = new ArrayList<ProviderInfo>(3);
1657 }
1658 finalList.add(PackageParser.generateProviderInfo(p,
1659 flags));
1660 }
1661 }
1662 }
1663
1664 if (finalList != null) {
1665 Collections.sort(finalList, mProviderInitOrderSorter);
1666 }
1667
1668 return finalList;
1669 }
1670
1671 public InstrumentationInfo getInstrumentationInfo(ComponentName name,
1672 int flags) {
1673 synchronized (mPackages) {
1674 final PackageParser.Instrumentation i = mInstrumentation.get(name);
1675 return PackageParser.generateInstrumentationInfo(i, flags);
1676 }
1677 }
1678
1679 public List<InstrumentationInfo> queryInstrumentation(String targetPackage,
1680 int flags) {
1681 ArrayList<InstrumentationInfo> finalList =
1682 new ArrayList<InstrumentationInfo>();
1683
1684 synchronized (mPackages) {
1685 Iterator<PackageParser.Instrumentation> i = mInstrumentation.values().iterator();
1686 while (i.hasNext()) {
1687 PackageParser.Instrumentation p = i.next();
1688 if (targetPackage == null
1689 || targetPackage.equals(p.info.targetPackage)) {
1690 finalList.add(PackageParser.generateInstrumentationInfo(p,
1691 flags));
1692 }
1693 }
1694 }
1695
1696 return finalList;
1697 }
1698
1699 private void scanDirLI(File dir, int flags, int scanMode) {
1700 Log.d(TAG, "Scanning app dir " + dir);
1701
1702 String[] files = dir.list();
1703
1704 int i;
1705 for (i=0; i<files.length; i++) {
1706 File file = new File(dir, files[i]);
1707 PackageParser.Package pkg = scanPackageLI(file, file, file,
1708 flags|PackageParser.PARSE_MUST_BE_APK, scanMode);
1709 }
1710 }
1711
1712 private static void reportSettingsProblem(int priority, String msg) {
1713 try {
1714 File dataDir = Environment.getDataDirectory();
1715 File systemDir = new File(dataDir, "system");
1716 File fname = new File(systemDir, "uiderrors.txt");
1717 FileOutputStream out = new FileOutputStream(fname, true);
1718 PrintWriter pw = new PrintWriter(out);
1719 pw.println(msg);
1720 pw.close();
1721 FileUtils.setPermissions(
1722 fname.toString(),
1723 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IROTH,
1724 -1, -1);
1725 } catch (java.io.IOException e) {
1726 }
1727 Log.println(priority, TAG, msg);
1728 }
1729
1730 private boolean collectCertificatesLI(PackageParser pp, PackageSetting ps,
1731 PackageParser.Package pkg, File srcFile, int parseFlags) {
1732 if (GET_CERTIFICATES) {
1733 if (ps == null || !ps.codePath.equals(srcFile)
1734 || ps.getTimeStamp() != srcFile.lastModified()) {
1735 Log.i(TAG, srcFile.toString() + " changed; collecting certs");
1736 if (!pp.collectCertificates(pkg, parseFlags)) {
1737 mLastScanError = pp.getParseError();
1738 return false;
1739 }
1740 }
1741 }
1742 return true;
1743 }
1744
1745 /*
1746 * Scan a package and return the newly parsed package.
1747 * Returns null in case of errors and the error code is stored in mLastScanError
1748 */
1749 private PackageParser.Package scanPackageLI(File scanFile,
1750 File destCodeFile, File destResourceFile, int parseFlags,
1751 int scanMode) {
1752 mLastScanError = PackageManager.INSTALL_SUCCEEDED;
1753 parseFlags |= mDefParseFlags;
1754 PackageParser pp = new PackageParser(scanFile.getPath());
1755 pp.setSeparateProcesses(mSeparateProcesses);
Dianne Hackborn851a5412009-05-08 12:06:44 -07001756 pp.setSdkVersion(mSdkVersion, mSdkCodename);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 final PackageParser.Package pkg = pp.parsePackage(scanFile,
1758 destCodeFile.getAbsolutePath(), mMetrics, parseFlags);
1759 if (pkg == null) {
1760 mLastScanError = pp.getParseError();
1761 return null;
1762 }
1763 PackageSetting ps;
1764 PackageSetting updatedPkg;
1765 synchronized (mPackages) {
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07001766 ps = mSettings.peekPackageLP(pkg.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 updatedPkg = mSettings.mDisabledSysPackages.get(pkg.packageName);
1768 }
1769 if (updatedPkg != null) {
1770 // An updated system app will not have the PARSE_IS_SYSTEM flag set initially
1771 parseFlags |= PackageParser.PARSE_IS_SYSTEM;
1772 }
1773 if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) {
1774 // Check for updated system applications here
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07001775 if (updatedPkg != null) {
1776 if ((ps != null) && (!ps.codePath.getPath().equals(scanFile.getPath()))) {
1777 if (pkg.mVersionCode <= ps.versionCode) {
1778 // The system package has been updated and the code path does not match
1779 // Ignore entry. Just return
1780 Log.w(TAG, "Package:" + pkg.packageName +
1781 " has been updated. Ignoring the one from path:"+scanFile);
1782 mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE;
1783 return null;
1784 } else {
1785 // Delete the older apk pointed to by ps
1786 deletePackageResourcesLI(ps.name, ps.codePathString, ps.resourcePathString);
1787 mSettings.enableSystemPackageLP(ps.name);
1788 }
1789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
1791 }
1792 if (!collectCertificatesLI(pp, ps, pkg, scanFile, parseFlags)) {
1793 Log.i(TAG, "Failed verifying certificates for package:" + pkg.packageName);
1794 return null;
1795 }
1796 // The apk is forward locked (not public) if its code and resources
1797 // are kept in different files.
1798 if (ps != null && !ps.codePath.equals(ps.resourcePath)) {
1799 scanMode |= SCAN_FORWARD_LOCKED;
1800 }
1801 // Note that we invoke the following method only if we are about to unpack an application
1802 return scanPackageLI(scanFile, destCodeFile, destResourceFile,
1803 pkg, parseFlags, scanMode | SCAN_UPDATE_SIGNATURE);
1804 }
1805
1806 private static String fixProcessName(String defProcessName,
1807 String processName, int uid) {
1808 if (processName == null) {
1809 return defProcessName;
1810 }
1811 return processName;
1812 }
1813
1814 private boolean verifySignaturesLP(PackageSetting pkgSetting,
1815 PackageParser.Package pkg, int parseFlags, boolean updateSignature) {
1816 if (pkg.mSignatures != null) {
1817 if (!pkgSetting.signatures.updateSignatures(pkg.mSignatures,
1818 updateSignature)) {
1819 Log.e(TAG, "Package " + pkg.packageName
1820 + " signatures do not match the previously installed version; ignoring!");
1821 mLastScanError = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE;
1822 return false;
1823 }
1824
1825 if (pkgSetting.sharedUser != null) {
1826 if (!pkgSetting.sharedUser.signatures.mergeSignatures(
1827 pkg.mSignatures, updateSignature)) {
1828 Log.e(TAG, "Package " + pkg.packageName
1829 + " has no signatures that match those in shared user "
1830 + pkgSetting.sharedUser.name + "; ignoring!");
1831 mLastScanError = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;
1832 return false;
1833 }
1834 }
1835 } else {
1836 pkg.mSignatures = pkgSetting.signatures.mSignatures;
1837 }
1838 return true;
1839 }
1840
1841 private PackageParser.Package scanPackageLI(
1842 File scanFile, File destCodeFile, File destResourceFile,
1843 PackageParser.Package pkg, int parseFlags, int scanMode) {
1844
1845 mScanningPath = scanFile;
1846 if (pkg == null) {
1847 mLastScanError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
1848 return null;
1849 }
1850
1851 final String pkgName = pkg.applicationInfo.packageName;
1852 if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) {
1853 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
1854 }
1855
1856 if (pkgName.equals("android")) {
1857 synchronized (mPackages) {
1858 if (mAndroidApplication != null) {
1859 Log.w(TAG, "*************************************************");
1860 Log.w(TAG, "Core android package being redefined. Skipping.");
1861 Log.w(TAG, " file=" + mScanningPath);
1862 Log.w(TAG, "*************************************************");
1863 mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE;
1864 return null;
1865 }
1866
1867 // Set up information for our fall-back user intent resolution
1868 // activity.
1869 mPlatformPackage = pkg;
1870 pkg.mVersionCode = mSdkVersion;
1871 mAndroidApplication = pkg.applicationInfo;
1872 mResolveActivity.applicationInfo = mAndroidApplication;
1873 mResolveActivity.name = ResolverActivity.class.getName();
1874 mResolveActivity.packageName = mAndroidApplication.packageName;
1875 mResolveActivity.processName = mAndroidApplication.processName;
1876 mResolveActivity.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
1877 mResolveActivity.flags = ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
1878 mResolveActivity.theme = com.android.internal.R.style.Theme_Dialog_Alert;
1879 mResolveActivity.exported = true;
1880 mResolveActivity.enabled = true;
1881 mResolveInfo.activityInfo = mResolveActivity;
1882 mResolveInfo.priority = 0;
1883 mResolveInfo.preferredOrder = 0;
1884 mResolveInfo.match = 0;
1885 mResolveComponentName = new ComponentName(
1886 mAndroidApplication.packageName, mResolveActivity.name);
1887 }
1888 }
1889
1890 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0 && Config.LOGD) Log.d(
1891 TAG, "Scanning package " + pkgName);
1892 if (mPackages.containsKey(pkgName) || mSharedLibraries.containsKey(pkgName)) {
1893 Log.w(TAG, "*************************************************");
1894 Log.w(TAG, "Application package " + pkgName
1895 + " already installed. Skipping duplicate.");
1896 Log.w(TAG, "*************************************************");
1897 mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE;
1898 return null;
1899 }
1900
1901 SharedUserSetting suid = null;
1902 PackageSetting pkgSetting = null;
1903
1904 boolean removeExisting = false;
1905
1906 synchronized (mPackages) {
1907 // Check all shared libraries and map to their actual file path.
1908 if (pkg.usesLibraryFiles != null) {
1909 for (int i=0; i<pkg.usesLibraryFiles.length; i++) {
1910 String file = mSharedLibraries.get(pkg.usesLibraryFiles[i]);
1911 if (file == null) {
1912 Log.e(TAG, "Package " + pkg.packageName
1913 + " requires unavailable shared library "
1914 + pkg.usesLibraryFiles[i] + "; ignoring!");
1915 mLastScanError = PackageManager.INSTALL_FAILED_MISSING_SHARED_LIBRARY;
1916 return null;
1917 }
1918 pkg.usesLibraryFiles[i] = file;
1919 }
1920 }
1921
1922 if (pkg.mSharedUserId != null) {
1923 suid = mSettings.getSharedUserLP(pkg.mSharedUserId,
1924 pkg.applicationInfo.flags, true);
1925 if (suid == null) {
1926 Log.w(TAG, "Creating application package " + pkgName
1927 + " for shared user failed");
1928 mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
1929 return null;
1930 }
1931 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0 && Config.LOGD) {
1932 Log.d(TAG, "Shared UserID " + pkg.mSharedUserId + " (uid="
1933 + suid.userId + "): packages=" + suid.packages);
1934 }
1935 }
1936
1937 // Just create the setting, don't add it yet
1938 pkgSetting = mSettings.getPackageLP(pkg, suid, destCodeFile,
1939 destResourceFile, pkg.applicationInfo.flags, true, false);
1940 if (pkgSetting == null) {
1941 Log.w(TAG, "Creating application package " + pkgName + " failed");
1942 mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
1943 return null;
1944 }
1945 if(mSettings.mDisabledSysPackages.get(pkg.packageName) != null) {
1946 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
1947 }
1948
1949 pkg.applicationInfo.uid = pkgSetting.userId;
1950 pkg.mExtras = pkgSetting;
1951
1952 if (!verifySignaturesLP(pkgSetting, pkg, parseFlags,
1953 (scanMode&SCAN_UPDATE_SIGNATURE) != 0)) {
1954 if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) == 0) {
1955 mLastScanError = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE;
1956 return null;
1957 }
1958 // The signature has changed, but this package is in the system
1959 // image... let's recover!
Suchi Amalapurapuc4dd60f2009-03-24 21:10:53 -07001960 pkgSetting.signatures.mSignatures = pkg.mSignatures;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 // However... if this package is part of a shared user, but it
1962 // doesn't match the signature of the shared user, let's fail.
1963 // What this means is that you can't change the signatures
1964 // associated with an overall shared user, which doesn't seem all
1965 // that unreasonable.
1966 if (pkgSetting.sharedUser != null) {
1967 if (!pkgSetting.sharedUser.signatures.mergeSignatures(
1968 pkg.mSignatures, false)) {
1969 mLastScanError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
1970 return null;
1971 }
1972 }
1973 removeExisting = true;
1974 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001975
1976 // Verify that this new package doesn't have any content providers
1977 // that conflict with existing packages. Only do this if the
1978 // package isn't already installed, since we don't want to break
1979 // things that are installed.
1980 if ((scanMode&SCAN_NEW_INSTALL) != 0) {
1981 int N = pkg.providers.size();
1982 int i;
1983 for (i=0; i<N; i++) {
1984 PackageParser.Provider p = pkg.providers.get(i);
1985 String names[] = p.info.authority.split(";");
1986 for (int j = 0; j < names.length; j++) {
1987 if (mProviders.containsKey(names[j])) {
1988 PackageParser.Provider other = mProviders.get(names[j]);
1989 Log.w(TAG, "Can't install because provider name " + names[j] +
1990 " (in package " + pkg.applicationInfo.packageName +
1991 ") is already used by "
1992 + ((other != null && other.component != null)
1993 ? other.component.getPackageName() : "?"));
1994 mLastScanError = PackageManager.INSTALL_FAILED_CONFLICTING_PROVIDER;
1995 return null;
1996 }
1997 }
1998 }
1999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001
2002 if (removeExisting) {
2003 if (mInstaller != null) {
2004 int ret = mInstaller.remove(pkgName);
2005 if (ret != 0) {
2006 String msg = "System package " + pkg.packageName
2007 + " could not have data directory erased after signature change.";
2008 reportSettingsProblem(Log.WARN, msg);
2009 mLastScanError = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE;
2010 return null;
2011 }
2012 }
2013 Log.w(TAG, "System package " + pkg.packageName
2014 + " signature changed: existing data removed.");
2015 mLastScanError = PackageManager.INSTALL_SUCCEEDED;
2016 }
2017
2018 long scanFileTime = scanFile.lastModified();
2019 final boolean forceDex = (scanMode&SCAN_FORCE_DEX) != 0;
2020 final boolean scanFileNewer = forceDex || scanFileTime != pkgSetting.getTimeStamp();
2021 pkg.applicationInfo.processName = fixProcessName(
2022 pkg.applicationInfo.packageName,
2023 pkg.applicationInfo.processName,
2024 pkg.applicationInfo.uid);
2025 pkg.applicationInfo.publicSourceDir = pkgSetting.resourcePathString;
2026
2027 File dataPath;
2028 if (mPlatformPackage == pkg) {
2029 // The system package is special.
2030 dataPath = new File (Environment.getDataDirectory(), "system");
2031 pkg.applicationInfo.dataDir = dataPath.getPath();
2032 } else {
2033 // This is a normal package, need to make its data directory.
2034 dataPath = new File(mAppDataDir, pkgName);
2035 if (dataPath.exists()) {
2036 mOutPermissions[1] = 0;
2037 FileUtils.getPermissions(dataPath.getPath(), mOutPermissions);
2038 if (mOutPermissions[1] == pkg.applicationInfo.uid
2039 || !Process.supportsProcesses()) {
2040 pkg.applicationInfo.dataDir = dataPath.getPath();
2041 } else {
2042 boolean recovered = false;
2043 if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) {
2044 // If this is a system app, we can at least delete its
2045 // current data so the application will still work.
2046 if (mInstaller != null) {
2047 int ret = mInstaller.remove(pkgName);
2048 if(ret >= 0) {
2049 // Old data gone!
2050 String msg = "System package " + pkg.packageName
2051 + " has changed from uid: "
2052 + mOutPermissions[1] + " to "
2053 + pkg.applicationInfo.uid + "; old data erased";
2054 reportSettingsProblem(Log.WARN, msg);
2055 recovered = true;
2056
2057 // And now re-install the app.
2058 ret = mInstaller.install(pkgName, pkg.applicationInfo.uid,
2059 pkg.applicationInfo.uid);
2060 if (ret == -1) {
2061 // Ack should not happen!
2062 msg = "System package " + pkg.packageName
2063 + " could not have data directory re-created after delete.";
2064 reportSettingsProblem(Log.WARN, msg);
2065 mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
2066 return null;
2067 }
2068 }
2069 }
2070 if (!recovered) {
2071 mHasSystemUidErrors = true;
2072 }
2073 }
2074 if (!recovered) {
2075 pkg.applicationInfo.dataDir = "/mismatched_uid/settings_"
2076 + pkg.applicationInfo.uid + "/fs_"
2077 + mOutPermissions[1];
2078 String msg = "Package " + pkg.packageName
2079 + " has mismatched uid: "
2080 + mOutPermissions[1] + " on disk, "
2081 + pkg.applicationInfo.uid + " in settings";
2082 synchronized (mPackages) {
2083 if (!mReportedUidError) {
2084 mReportedUidError = true;
2085 msg = msg + "; read messages:\n"
2086 + mSettings.getReadMessagesLP();
2087 }
2088 reportSettingsProblem(Log.ERROR, msg);
2089 }
2090 }
2091 }
2092 pkg.applicationInfo.dataDir = dataPath.getPath();
2093 } else {
2094 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0 && Config.LOGV)
2095 Log.v(TAG, "Want this data dir: " + dataPath);
2096 //invoke installer to do the actual installation
2097 if (mInstaller != null) {
2098 int ret = mInstaller.install(pkgName, pkg.applicationInfo.uid,
2099 pkg.applicationInfo.uid);
2100 if(ret < 0) {
2101 // Error from installer
2102 mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
2103 return null;
2104 }
2105 } else {
2106 dataPath.mkdirs();
2107 if (dataPath.exists()) {
2108 FileUtils.setPermissions(
2109 dataPath.toString(),
2110 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
2111 pkg.applicationInfo.uid, pkg.applicationInfo.uid);
2112 }
2113 }
2114 if (dataPath.exists()) {
2115 pkg.applicationInfo.dataDir = dataPath.getPath();
2116 } else {
2117 Log.w(TAG, "Unable to create data directory: " + dataPath);
2118 pkg.applicationInfo.dataDir = null;
2119 }
2120 }
2121 }
2122
2123 // Perform shared library installation and dex validation and
2124 // optimization, if this is not a system app.
2125 if (mInstaller != null) {
2126 String path = scanFile.getPath();
2127 if (scanFileNewer) {
2128 Log.i(TAG, path + " changed; unpacking");
2129 try {
2130 cachePackageSharedLibsLI(pkg, dataPath, scanFile);
2131 } catch (IOException e) {
2132 Log.e(TAG, "Failure extracting shared libs", e);
2133 if(mInstaller != null) {
2134 mInstaller.remove(pkgName);
2135 } else {
2136 dataPath.delete();
2137 }
2138 mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
2139 return null;
2140 }
2141 }
2142
2143 if ((scanMode&SCAN_NO_DEX) == 0
2144 && (pkg.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) {
2145 int ret = 0;
2146 try {
2147 if (forceDex || dalvik.system.DexFile.isDexOptNeeded(path)) {
2148 ret = mInstaller.dexopt(path, pkg.applicationInfo.uid,
2149 (scanMode&SCAN_FORWARD_LOCKED) == 0);
2150 }
2151 } catch (FileNotFoundException e) {
2152 Log.w(TAG, "Apk not found for dexopt: " + path);
2153 ret = -1;
2154 } catch (IOException e) {
2155 Log.w(TAG, "Exception reading apk: " + path, e);
2156 ret = -1;
2157 }
2158 if (ret < 0) {
2159 //error from installer
2160 mLastScanError = PackageManager.INSTALL_FAILED_DEXOPT;
2161 return null;
2162 }
2163 }
2164 }
2165
2166 if (mFactoryTest && pkg.requestedPermissions.contains(
2167 android.Manifest.permission.FACTORY_TEST)) {
2168 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_FACTORY_TEST;
2169 }
2170
2171 if ((scanMode&SCAN_MONITOR) != 0) {
2172 pkg.mPath = destCodeFile.getAbsolutePath();
2173 mAppDirs.put(pkg.mPath, pkg);
2174 }
2175
2176 synchronized (mPackages) {
2177 // We don't expect installation to fail beyond this point
2178 // Add the new setting to mSettings
2179 mSettings.insertPackageSettingLP(pkgSetting, pkg.packageName, suid);
2180 // Add the new setting to mPackages
2181 mPackages.put(pkg.applicationInfo.packageName, pkg);
2182 int N = pkg.providers.size();
2183 StringBuilder r = null;
2184 int i;
2185 for (i=0; i<N; i++) {
2186 PackageParser.Provider p = pkg.providers.get(i);
2187 p.info.processName = fixProcessName(pkg.applicationInfo.processName,
2188 p.info.processName, pkg.applicationInfo.uid);
2189 mProvidersByComponent.put(new ComponentName(p.info.packageName,
2190 p.info.name), p);
2191 p.syncable = p.info.isSyncable;
2192 String names[] = p.info.authority.split(";");
2193 p.info.authority = null;
2194 for (int j = 0; j < names.length; j++) {
2195 if (j == 1 && p.syncable) {
2196 // We only want the first authority for a provider to possibly be
2197 // syncable, so if we already added this provider using a different
2198 // authority clear the syncable flag. We copy the provider before
2199 // changing it because the mProviders object contains a reference
2200 // to a provider that we don't want to change.
2201 // Only do this for the second authority since the resulting provider
2202 // object can be the same for all future authorities for this provider.
2203 p = new PackageParser.Provider(p);
2204 p.syncable = false;
2205 }
2206 if (!mProviders.containsKey(names[j])) {
2207 mProviders.put(names[j], p);
2208 if (p.info.authority == null) {
2209 p.info.authority = names[j];
2210 } else {
2211 p.info.authority = p.info.authority + ";" + names[j];
2212 }
2213 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0 && Config.LOGD)
2214 Log.d(TAG, "Registered content provider: " + names[j] +
2215 ", className = " + p.info.name +
2216 ", isSyncable = " + p.info.isSyncable);
2217 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07002218 PackageParser.Provider other = mProviders.get(names[j]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 Log.w(TAG, "Skipping provider name " + names[j] +
2220 " (in package " + pkg.applicationInfo.packageName +
The Android Open Source Project10592532009-03-18 17:39:46 -07002221 "): name already used by "
2222 + ((other != null && other.component != null)
2223 ? other.component.getPackageName() : "?"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225 }
2226 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) {
2227 if (r == null) {
2228 r = new StringBuilder(256);
2229 } else {
2230 r.append(' ');
2231 }
2232 r.append(p.info.name);
2233 }
2234 }
2235 if (r != null) {
2236 if (Config.LOGD) Log.d(TAG, " Providers: " + r);
2237 }
2238
2239 N = pkg.services.size();
2240 r = null;
2241 for (i=0; i<N; i++) {
2242 PackageParser.Service s = pkg.services.get(i);
2243 s.info.processName = fixProcessName(pkg.applicationInfo.processName,
2244 s.info.processName, pkg.applicationInfo.uid);
2245 mServices.addService(s);
2246 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) {
2247 if (r == null) {
2248 r = new StringBuilder(256);
2249 } else {
2250 r.append(' ');
2251 }
2252 r.append(s.info.name);
2253 }
2254 }
2255 if (r != null) {
2256 if (Config.LOGD) Log.d(TAG, " Services: " + r);
2257 }
2258
2259 N = pkg.receivers.size();
2260 r = null;
2261 for (i=0; i<N; i++) {
2262 PackageParser.Activity a = pkg.receivers.get(i);
2263 a.info.processName = fixProcessName(pkg.applicationInfo.processName,
2264 a.info.processName, pkg.applicationInfo.uid);
2265 mReceivers.addActivity(a, "receiver");
2266 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) {
2267 if (r == null) {
2268 r = new StringBuilder(256);
2269 } else {
2270 r.append(' ');
2271 }
2272 r.append(a.info.name);
2273 }
2274 }
2275 if (r != null) {
2276 if (Config.LOGD) Log.d(TAG, " Receivers: " + r);
2277 }
2278
2279 N = pkg.activities.size();
2280 r = null;
2281 for (i=0; i<N; i++) {
2282 PackageParser.Activity a = pkg.activities.get(i);
2283 a.info.processName = fixProcessName(pkg.applicationInfo.processName,
2284 a.info.processName, pkg.applicationInfo.uid);
2285 mActivities.addActivity(a, "activity");
2286 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) {
2287 if (r == null) {
2288 r = new StringBuilder(256);
2289 } else {
2290 r.append(' ');
2291 }
2292 r.append(a.info.name);
2293 }
2294 }
2295 if (r != null) {
2296 if (Config.LOGD) Log.d(TAG, " Activities: " + r);
2297 }
2298
2299 N = pkg.permissionGroups.size();
2300 r = null;
2301 for (i=0; i<N; i++) {
2302 PackageParser.PermissionGroup pg = pkg.permissionGroups.get(i);
2303 PackageParser.PermissionGroup cur = mPermissionGroups.get(pg.info.name);
2304 if (cur == null) {
2305 mPermissionGroups.put(pg.info.name, pg);
2306 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) {
2307 if (r == null) {
2308 r = new StringBuilder(256);
2309 } else {
2310 r.append(' ');
2311 }
2312 r.append(pg.info.name);
2313 }
2314 } else {
2315 Log.w(TAG, "Permission group " + pg.info.name + " from package "
2316 + pg.info.packageName + " ignored: original from "
2317 + cur.info.packageName);
2318 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) {
2319 if (r == null) {
2320 r = new StringBuilder(256);
2321 } else {
2322 r.append(' ');
2323 }
2324 r.append("DUP:");
2325 r.append(pg.info.name);
2326 }
2327 }
2328 }
2329 if (r != null) {
2330 if (Config.LOGD) Log.d(TAG, " Permission Groups: " + r);
2331 }
2332
2333 N = pkg.permissions.size();
2334 r = null;
2335 for (i=0; i<N; i++) {
2336 PackageParser.Permission p = pkg.permissions.get(i);
2337 HashMap<String, BasePermission> permissionMap =
2338 p.tree ? mSettings.mPermissionTrees
2339 : mSettings.mPermissions;
2340 p.group = mPermissionGroups.get(p.info.group);
2341 if (p.info.group == null || p.group != null) {
2342 BasePermission bp = permissionMap.get(p.info.name);
2343 if (bp == null) {
2344 bp = new BasePermission(p.info.name, p.info.packageName,
2345 BasePermission.TYPE_NORMAL);
2346 permissionMap.put(p.info.name, bp);
2347 }
2348 if (bp.perm == null) {
2349 if (bp.sourcePackage == null
2350 || bp.sourcePackage.equals(p.info.packageName)) {
2351 BasePermission tree = findPermissionTreeLP(p.info.name);
2352 if (tree == null
2353 || tree.sourcePackage.equals(p.info.packageName)) {
2354 bp.perm = p;
2355 bp.uid = pkg.applicationInfo.uid;
2356 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) {
2357 if (r == null) {
2358 r = new StringBuilder(256);
2359 } else {
2360 r.append(' ');
2361 }
2362 r.append(p.info.name);
2363 }
2364 } else {
2365 Log.w(TAG, "Permission " + p.info.name + " from package "
2366 + p.info.packageName + " ignored: base tree "
2367 + tree.name + " is from package "
2368 + tree.sourcePackage);
2369 }
2370 } else {
2371 Log.w(TAG, "Permission " + p.info.name + " from package "
2372 + p.info.packageName + " ignored: original from "
2373 + bp.sourcePackage);
2374 }
2375 } else if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) {
2376 if (r == null) {
2377 r = new StringBuilder(256);
2378 } else {
2379 r.append(' ');
2380 }
2381 r.append("DUP:");
2382 r.append(p.info.name);
2383 }
2384 } else {
2385 Log.w(TAG, "Permission " + p.info.name + " from package "
2386 + p.info.packageName + " ignored: no group "
2387 + p.group);
2388 }
2389 }
2390 if (r != null) {
2391 if (Config.LOGD) Log.d(TAG, " Permissions: " + r);
2392 }
2393
2394 N = pkg.instrumentation.size();
2395 r = null;
2396 for (i=0; i<N; i++) {
2397 PackageParser.Instrumentation a = pkg.instrumentation.get(i);
2398 a.info.packageName = pkg.applicationInfo.packageName;
2399 a.info.sourceDir = pkg.applicationInfo.sourceDir;
2400 a.info.publicSourceDir = pkg.applicationInfo.publicSourceDir;
2401 a.info.dataDir = pkg.applicationInfo.dataDir;
2402 mInstrumentation.put(a.component, a);
2403 if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) {
2404 if (r == null) {
2405 r = new StringBuilder(256);
2406 } else {
2407 r.append(' ');
2408 }
2409 r.append(a.info.name);
2410 }
2411 }
2412 if (r != null) {
2413 if (Config.LOGD) Log.d(TAG, " Instrumentation: " + r);
2414 }
2415
2416 pkgSetting.setTimeStamp(scanFileTime);
2417 }
2418
2419 return pkg;
2420 }
2421
2422 private void cachePackageSharedLibsLI(PackageParser.Package pkg,
2423 File dataPath, File scanFile) throws IOException {
2424 File sharedLibraryDir = new File(dataPath.getPath() + "/lib");
2425 final String sharedLibraryABI = "armeabi";
2426 final String apkLibraryDirectory = "lib/" + sharedLibraryABI + "/";
2427 final String apkSharedLibraryPrefix = apkLibraryDirectory + "lib";
2428 final String sharedLibrarySuffix = ".so";
2429 boolean createdSharedLib = false;
2430 try {
2431 ZipFile zipFile = new ZipFile(scanFile);
2432 Enumeration<ZipEntry> entries =
2433 (Enumeration<ZipEntry>) zipFile.entries();
2434
2435 while (entries.hasMoreElements()) {
2436 ZipEntry entry = entries.nextElement();
2437 if (entry.isDirectory()) {
2438 continue;
2439 }
2440 String entryName = entry.getName();
2441 if (! (entryName.startsWith(apkSharedLibraryPrefix)
2442 && entryName.endsWith(sharedLibrarySuffix))) {
2443 continue;
2444 }
2445 String libFileName = entryName.substring(
2446 apkLibraryDirectory.length());
2447 if (libFileName.contains("/")
2448 || (!FileUtils.isFilenameSafe(new File(libFileName)))) {
2449 continue;
2450 }
2451 String sharedLibraryFilePath = sharedLibraryDir.getPath() +
2452 File.separator + libFileName;
2453 File sharedLibraryFile = new File(sharedLibraryFilePath);
2454 if (! sharedLibraryFile.exists() ||
2455 sharedLibraryFile.length() != entry.getSize() ||
2456 sharedLibraryFile.lastModified() != entry.getTime()) {
2457 if (Config.LOGD) {
2458 Log.d(TAG, "Caching shared lib " + entry.getName());
2459 }
2460 if (mInstaller == null) {
2461 sharedLibraryDir.mkdir();
2462 createdSharedLib = true;
2463 }
2464 cacheSharedLibLI(pkg, zipFile, entry, sharedLibraryDir,
2465 sharedLibraryFile);
2466 }
2467 }
2468 } catch (IOException e) {
2469 Log.e(TAG, "Failed to cache package shared libs", e);
2470 if(createdSharedLib) {
2471 sharedLibraryDir.delete();
2472 }
2473 throw e;
2474 }
2475 }
2476
2477 private void cacheSharedLibLI(PackageParser.Package pkg,
2478 ZipFile zipFile, ZipEntry entry,
2479 File sharedLibraryDir,
2480 File sharedLibraryFile) throws IOException {
2481 InputStream inputStream = zipFile.getInputStream(entry);
2482 try {
2483 File tempFile = File.createTempFile("tmp", "tmp", sharedLibraryDir);
2484 String tempFilePath = tempFile.getPath();
2485 // XXX package manager can't change owner, so the lib files for
2486 // now need to be left as world readable and owned by the system.
2487 if (! FileUtils.copyToFile(inputStream, tempFile) ||
2488 ! tempFile.setLastModified(entry.getTime()) ||
2489 FileUtils.setPermissions(tempFilePath,
2490 FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP
2491 |FileUtils.S_IROTH, -1, -1) != 0 ||
2492 ! tempFile.renameTo(sharedLibraryFile)) {
2493 // Failed to properly write file.
2494 tempFile.delete();
2495 throw new IOException("Couldn't create cached shared lib "
2496 + sharedLibraryFile + " in " + sharedLibraryDir);
2497 }
2498 } finally {
2499 inputStream.close();
2500 }
2501 }
2502
2503 void removePackageLI(PackageParser.Package pkg, boolean chatty) {
2504 if (chatty && Config.LOGD) Log.d(
2505 TAG, "Removing package " + pkg.applicationInfo.packageName );
2506
2507 synchronized (mPackages) {
2508 if (pkg.mPreferredOrder > 0) {
2509 mSettings.mPreferredPackages.remove(pkg);
2510 pkg.mPreferredOrder = 0;
2511 updatePreferredIndicesLP();
2512 }
2513
2514 clearPackagePreferredActivitiesLP(pkg.packageName);
2515
2516 mPackages.remove(pkg.applicationInfo.packageName);
2517 if (pkg.mPath != null) {
2518 mAppDirs.remove(pkg.mPath);
2519 }
2520
2521 PackageSetting ps = (PackageSetting)pkg.mExtras;
2522 if (ps != null && ps.sharedUser != null) {
2523 // XXX don't do this until the data is removed.
2524 if (false) {
2525 ps.sharedUser.packages.remove(ps);
2526 if (ps.sharedUser.packages.size() == 0) {
2527 // Remove.
2528 }
2529 }
2530 }
2531
2532 int N = pkg.providers.size();
2533 StringBuilder r = null;
2534 int i;
2535 for (i=0; i<N; i++) {
2536 PackageParser.Provider p = pkg.providers.get(i);
2537 mProvidersByComponent.remove(new ComponentName(p.info.packageName,
2538 p.info.name));
2539 if (p.info.authority == null) {
2540
2541 /* The is another ContentProvider with this authority when
2542 * this app was installed so this authority is null,
2543 * Ignore it as we don't have to unregister the provider.
2544 */
2545 continue;
2546 }
2547 String names[] = p.info.authority.split(";");
2548 for (int j = 0; j < names.length; j++) {
2549 if (mProviders.get(names[j]) == p) {
2550 mProviders.remove(names[j]);
2551 if (chatty && Config.LOGD) Log.d(
2552 TAG, "Unregistered content provider: " + names[j] +
2553 ", className = " + p.info.name +
2554 ", isSyncable = " + p.info.isSyncable);
2555 }
2556 }
2557 if (chatty) {
2558 if (r == null) {
2559 r = new StringBuilder(256);
2560 } else {
2561 r.append(' ');
2562 }
2563 r.append(p.info.name);
2564 }
2565 }
2566 if (r != null) {
2567 if (Config.LOGD) Log.d(TAG, " Providers: " + r);
2568 }
2569
2570 N = pkg.services.size();
2571 r = null;
2572 for (i=0; i<N; i++) {
2573 PackageParser.Service s = pkg.services.get(i);
2574 mServices.removeService(s);
2575 if (chatty) {
2576 if (r == null) {
2577 r = new StringBuilder(256);
2578 } else {
2579 r.append(' ');
2580 }
2581 r.append(s.info.name);
2582 }
2583 }
2584 if (r != null) {
2585 if (Config.LOGD) Log.d(TAG, " Services: " + r);
2586 }
2587
2588 N = pkg.receivers.size();
2589 r = null;
2590 for (i=0; i<N; i++) {
2591 PackageParser.Activity a = pkg.receivers.get(i);
2592 mReceivers.removeActivity(a, "receiver");
2593 if (chatty) {
2594 if (r == null) {
2595 r = new StringBuilder(256);
2596 } else {
2597 r.append(' ');
2598 }
2599 r.append(a.info.name);
2600 }
2601 }
2602 if (r != null) {
2603 if (Config.LOGD) Log.d(TAG, " Receivers: " + r);
2604 }
2605
2606 N = pkg.activities.size();
2607 r = null;
2608 for (i=0; i<N; i++) {
2609 PackageParser.Activity a = pkg.activities.get(i);
2610 mActivities.removeActivity(a, "activity");
2611 if (chatty) {
2612 if (r == null) {
2613 r = new StringBuilder(256);
2614 } else {
2615 r.append(' ');
2616 }
2617 r.append(a.info.name);
2618 }
2619 }
2620 if (r != null) {
2621 if (Config.LOGD) Log.d(TAG, " Activities: " + r);
2622 }
2623
2624 N = pkg.permissions.size();
2625 r = null;
2626 for (i=0; i<N; i++) {
2627 PackageParser.Permission p = pkg.permissions.get(i);
2628 boolean tree = false;
2629 BasePermission bp = mSettings.mPermissions.get(p.info.name);
2630 if (bp == null) {
2631 tree = true;
2632 bp = mSettings.mPermissionTrees.get(p.info.name);
2633 }
2634 if (bp != null && bp.perm == p) {
2635 if (bp.type != BasePermission.TYPE_BUILTIN) {
2636 if (tree) {
2637 mSettings.mPermissionTrees.remove(p.info.name);
2638 } else {
2639 mSettings.mPermissions.remove(p.info.name);
2640 }
2641 } else {
2642 bp.perm = null;
2643 }
2644 if (chatty) {
2645 if (r == null) {
2646 r = new StringBuilder(256);
2647 } else {
2648 r.append(' ');
2649 }
2650 r.append(p.info.name);
2651 }
2652 }
2653 }
2654 if (r != null) {
2655 if (Config.LOGD) Log.d(TAG, " Permissions: " + r);
2656 }
2657
2658 N = pkg.instrumentation.size();
2659 r = null;
2660 for (i=0; i<N; i++) {
2661 PackageParser.Instrumentation a = pkg.instrumentation.get(i);
2662 mInstrumentation.remove(a.component);
2663 if (chatty) {
2664 if (r == null) {
2665 r = new StringBuilder(256);
2666 } else {
2667 r.append(' ');
2668 }
2669 r.append(a.info.name);
2670 }
2671 }
2672 if (r != null) {
2673 if (Config.LOGD) Log.d(TAG, " Instrumentation: " + r);
2674 }
2675 }
2676 }
2677
2678 private static final boolean isPackageFilename(String name) {
2679 return name != null && name.endsWith(".apk");
2680 }
2681
2682 private void updatePermissionsLP() {
2683 // Make sure there are no dangling permission trees.
2684 Iterator<BasePermission> it = mSettings.mPermissionTrees
2685 .values().iterator();
2686 while (it.hasNext()) {
2687 BasePermission bp = it.next();
2688 if (bp.perm == null) {
2689 Log.w(TAG, "Removing dangling permission tree: " + bp.name
2690 + " from package " + bp.sourcePackage);
2691 it.remove();
2692 }
2693 }
2694
2695 // Make sure all dynamic permissions have been assigned to a package,
2696 // and make sure there are no dangling permissions.
2697 it = mSettings.mPermissions.values().iterator();
2698 while (it.hasNext()) {
2699 BasePermission bp = it.next();
2700 if (bp.type == BasePermission.TYPE_DYNAMIC) {
2701 if (DEBUG_SETTINGS) Log.v(TAG, "Dynamic permission: name="
2702 + bp.name + " pkg=" + bp.sourcePackage
2703 + " info=" + bp.pendingInfo);
2704 if (bp.perm == null && bp.pendingInfo != null) {
2705 BasePermission tree = findPermissionTreeLP(bp.name);
2706 if (tree != null) {
2707 bp.perm = new PackageParser.Permission(tree.perm.owner,
2708 new PermissionInfo(bp.pendingInfo));
2709 bp.perm.info.packageName = tree.perm.info.packageName;
2710 bp.perm.info.name = bp.name;
2711 bp.uid = tree.uid;
2712 }
2713 }
2714 }
2715 if (bp.perm == null) {
2716 Log.w(TAG, "Removing dangling permission: " + bp.name
2717 + " from package " + bp.sourcePackage);
2718 it.remove();
2719 }
2720 }
2721
2722 // Now update the permissions for all packages, in particular
2723 // replace the granted permissions of the system packages.
2724 for (PackageParser.Package pkg : mPackages.values()) {
2725 grantPermissionsLP(pkg, false);
2726 }
2727 }
2728
2729 private void grantPermissionsLP(PackageParser.Package pkg, boolean replace) {
2730 final PackageSetting ps = (PackageSetting)pkg.mExtras;
2731 if (ps == null) {
2732 return;
2733 }
2734 final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps;
2735 boolean addedPermission = false;
2736
2737 if (replace) {
2738 ps.permissionsFixed = false;
2739 if (gp == ps) {
2740 gp.grantedPermissions.clear();
2741 gp.gids = mGlobalGids;
2742 }
2743 }
2744
2745 if (gp.gids == null) {
2746 gp.gids = mGlobalGids;
2747 }
2748
2749 final int N = pkg.requestedPermissions.size();
2750 for (int i=0; i<N; i++) {
2751 String name = pkg.requestedPermissions.get(i);
2752 BasePermission bp = mSettings.mPermissions.get(name);
2753 PackageParser.Permission p = bp != null ? bp.perm : null;
2754 if (false) {
2755 if (gp != ps) {
2756 Log.i(TAG, "Package " + pkg.packageName + " checking " + name
2757 + ": " + p);
2758 }
2759 }
2760 if (p != null) {
2761 final String perm = p.info.name;
2762 boolean allowed;
2763 if (p.info.protectionLevel == PermissionInfo.PROTECTION_NORMAL
2764 || p.info.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS) {
2765 allowed = true;
2766 } else if (p.info.protectionLevel == PermissionInfo.PROTECTION_SIGNATURE
2767 || p.info.protectionLevel == PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM) {
2768 allowed = (checkSignaturesLP(p.owner, pkg)
2769 == PackageManager.SIGNATURE_MATCH)
2770 || (checkSignaturesLP(mPlatformPackage, pkg)
2771 == PackageManager.SIGNATURE_MATCH);
2772 if (p.info.protectionLevel == PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM) {
2773 if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
2774 // For updated system applications, the signatureOrSystem permission
2775 // is granted only if it had been defined by the original application.
2776 if ((pkg.applicationInfo.flags
2777 & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
2778 PackageSetting sysPs = mSettings.getDisabledSystemPkg(pkg.packageName);
2779 if(sysPs.grantedPermissions.contains(perm)) {
2780 allowed = true;
2781 } else {
2782 allowed = false;
2783 }
2784 } else {
2785 allowed = true;
2786 }
2787 }
2788 }
2789 } else {
2790 allowed = false;
2791 }
2792 if (false) {
2793 if (gp != ps) {
2794 Log.i(TAG, "Package " + pkg.packageName + " granting " + perm);
2795 }
2796 }
2797 if (allowed) {
2798 if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0
2799 && ps.permissionsFixed) {
2800 // If this is an existing, non-system package, then
2801 // we can't add any new permissions to it.
2802 if (!gp.loadedPermissions.contains(perm)) {
2803 allowed = false;
2804 }
2805 }
2806 if (allowed) {
2807 if (!gp.grantedPermissions.contains(perm)) {
2808 addedPermission = true;
2809 gp.grantedPermissions.add(perm);
2810 gp.gids = appendInts(gp.gids, bp.gids);
2811 }
2812 } else {
2813 Log.w(TAG, "Not granting permission " + perm
2814 + " to package " + pkg.packageName
2815 + " because it was previously installed without");
2816 }
2817 } else {
2818 Log.w(TAG, "Not granting permission " + perm
2819 + " to package " + pkg.packageName
2820 + " (protectionLevel=" + p.info.protectionLevel
2821 + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags)
2822 + ")");
2823 }
2824 } else {
2825 Log.w(TAG, "Unknown permission " + name
2826 + " in package " + pkg.packageName);
2827 }
2828 }
2829
2830 if ((addedPermission || replace) && !ps.permissionsFixed &&
2831 (ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) {
2832 // This is the first that we have heard about this package, so the
2833 // permissions we have now selected are fixed until explicitly
2834 // changed.
2835 ps.permissionsFixed = true;
2836 gp.loadedPermissions = new HashSet<String>(gp.grantedPermissions);
2837 }
2838 }
2839
2840 private final class ActivityIntentResolver
2841 extends IntentResolver<PackageParser.ActivityIntentInfo, ResolveInfo> {
2842 public List queryIntent(ContentResolver resolver, Intent intent,
2843 String resolvedType, boolean defaultOnly) {
2844 mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0;
2845 return super.queryIntent(resolver, intent, resolvedType, defaultOnly);
2846 }
2847
2848 public List queryIntent(ContentResolver resolver, Intent intent,
2849 String resolvedType, int flags) {
2850 mFlags = flags;
2851 return super.queryIntent(
2852 resolver, intent, resolvedType,
2853 (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0);
2854 }
2855
2856 public final void addActivity(PackageParser.Activity a, String type) {
2857 mActivities.put(a.component, a);
2858 if (SHOW_INFO || Config.LOGV) Log.v(
2859 TAG, " " + type + " " +
2860 (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel : a.info.name) + ":");
2861 if (SHOW_INFO || Config.LOGV) Log.v(TAG, " Class=" + a.info.name);
2862 int NI = a.intents.size();
2863 int j;
2864 for (j=0; j<NI; j++) {
2865 PackageParser.ActivityIntentInfo intent = a.intents.get(j);
2866 if (SHOW_INFO || Config.LOGV) {
2867 Log.v(TAG, " IntentFilter:");
2868 intent.dump(new LogPrinter(Log.VERBOSE, TAG), " ");
2869 }
2870 if (!intent.debugCheck()) {
2871 Log.w(TAG, "==> For Activity " + a.info.name);
2872 }
2873 addFilter(intent);
2874 }
2875 }
2876
2877 public final void removeActivity(PackageParser.Activity a, String type) {
2878 mActivities.remove(a.component);
2879 if (SHOW_INFO || Config.LOGV) Log.v(
2880 TAG, " " + type + " " +
2881 (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel : a.info.name) + ":");
2882 if (SHOW_INFO || Config.LOGV) Log.v(TAG, " Class=" + a.info.name);
2883 int NI = a.intents.size();
2884 int j;
2885 for (j=0; j<NI; j++) {
2886 PackageParser.ActivityIntentInfo intent = a.intents.get(j);
2887 if (SHOW_INFO || Config.LOGV) {
2888 Log.v(TAG, " IntentFilter:");
2889 intent.dump(new LogPrinter(Log.VERBOSE, TAG), " ");
2890 }
2891 removeFilter(intent);
2892 }
2893 }
2894
2895 @Override
2896 protected boolean allowFilterResult(
2897 PackageParser.ActivityIntentInfo filter, List<ResolveInfo> dest) {
2898 ActivityInfo filterAi = filter.activity.info;
2899 for (int i=dest.size()-1; i>=0; i--) {
2900 ActivityInfo destAi = dest.get(i).activityInfo;
2901 if (destAi.name == filterAi.name
2902 && destAi.packageName == filterAi.packageName) {
2903 return false;
2904 }
2905 }
2906 return true;
2907 }
2908
2909 @Override
2910 protected ResolveInfo newResult(PackageParser.ActivityIntentInfo info,
2911 int match) {
2912 if (!mSettings.isEnabledLP(info.activity.info, mFlags)) {
2913 return null;
2914 }
2915 final PackageParser.Activity activity = info.activity;
2916 if (mSafeMode && (activity.info.applicationInfo.flags
2917 &ApplicationInfo.FLAG_SYSTEM) == 0) {
2918 return null;
2919 }
2920 final ResolveInfo res = new ResolveInfo();
2921 res.activityInfo = PackageParser.generateActivityInfo(activity,
2922 mFlags);
2923 if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) {
2924 res.filter = info;
2925 }
2926 res.priority = info.getPriority();
2927 res.preferredOrder = activity.owner.mPreferredOrder;
2928 //System.out.println("Result: " + res.activityInfo.className +
2929 // " = " + res.priority);
2930 res.match = match;
2931 res.isDefault = info.hasDefault;
2932 res.labelRes = info.labelRes;
2933 res.nonLocalizedLabel = info.nonLocalizedLabel;
2934 res.icon = info.icon;
2935 return res;
2936 }
2937
2938 @Override
2939 protected void sortResults(List<ResolveInfo> results) {
2940 Collections.sort(results, mResolvePrioritySorter);
2941 }
2942
2943 @Override
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002944 protected void dumpFilter(PrintWriter out, String prefix,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 PackageParser.ActivityIntentInfo filter) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002946 out.print(prefix); out.print(
2947 Integer.toHexString(System.identityHashCode(filter.activity)));
2948 out.print(' ');
2949 out.println(filter.activity.componentShortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 }
2951
2952// List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) {
2953// final Iterator<ResolveInfo> i = resolveInfoList.iterator();
2954// final List<ResolveInfo> retList = Lists.newArrayList();
2955// while (i.hasNext()) {
2956// final ResolveInfo resolveInfo = i.next();
2957// if (isEnabledLP(resolveInfo.activityInfo)) {
2958// retList.add(resolveInfo);
2959// }
2960// }
2961// return retList;
2962// }
2963
2964 // Keys are String (activity class name), values are Activity.
2965 private final HashMap<ComponentName, PackageParser.Activity> mActivities
2966 = new HashMap<ComponentName, PackageParser.Activity>();
2967 private int mFlags;
2968 }
2969
2970 private final class ServiceIntentResolver
2971 extends IntentResolver<PackageParser.ServiceIntentInfo, ResolveInfo> {
2972 public List queryIntent(ContentResolver resolver, Intent intent,
2973 String resolvedType, boolean defaultOnly) {
2974 mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0;
2975 return super.queryIntent(resolver, intent, resolvedType, defaultOnly);
2976 }
2977
2978 public List queryIntent(ContentResolver resolver, Intent intent,
2979 String resolvedType, int flags) {
2980 mFlags = flags;
2981 return super.queryIntent(
2982 resolver, intent, resolvedType,
2983 (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0);
2984 }
2985
2986 public final void addService(PackageParser.Service s) {
2987 mServices.put(s.component, s);
2988 if (SHOW_INFO || Config.LOGV) Log.v(
2989 TAG, " " + (s.info.nonLocalizedLabel != null
2990 ? s.info.nonLocalizedLabel : s.info.name) + ":");
2991 if (SHOW_INFO || Config.LOGV) Log.v(
2992 TAG, " Class=" + s.info.name);
2993 int NI = s.intents.size();
2994 int j;
2995 for (j=0; j<NI; j++) {
2996 PackageParser.ServiceIntentInfo intent = s.intents.get(j);
2997 if (SHOW_INFO || Config.LOGV) {
2998 Log.v(TAG, " IntentFilter:");
2999 intent.dump(new LogPrinter(Log.VERBOSE, TAG), " ");
3000 }
3001 if (!intent.debugCheck()) {
3002 Log.w(TAG, "==> For Service " + s.info.name);
3003 }
3004 addFilter(intent);
3005 }
3006 }
3007
3008 public final void removeService(PackageParser.Service s) {
3009 mServices.remove(s.component);
3010 if (SHOW_INFO || Config.LOGV) Log.v(
3011 TAG, " " + (s.info.nonLocalizedLabel != null
3012 ? s.info.nonLocalizedLabel : s.info.name) + ":");
3013 if (SHOW_INFO || Config.LOGV) Log.v(
3014 TAG, " Class=" + s.info.name);
3015 int NI = s.intents.size();
3016 int j;
3017 for (j=0; j<NI; j++) {
3018 PackageParser.ServiceIntentInfo intent = s.intents.get(j);
3019 if (SHOW_INFO || Config.LOGV) {
3020 Log.v(TAG, " IntentFilter:");
3021 intent.dump(new LogPrinter(Log.VERBOSE, TAG), " ");
3022 }
3023 removeFilter(intent);
3024 }
3025 }
3026
3027 @Override
3028 protected boolean allowFilterResult(
3029 PackageParser.ServiceIntentInfo filter, List<ResolveInfo> dest) {
3030 ServiceInfo filterSi = filter.service.info;
3031 for (int i=dest.size()-1; i>=0; i--) {
3032 ServiceInfo destAi = dest.get(i).serviceInfo;
3033 if (destAi.name == filterSi.name
3034 && destAi.packageName == filterSi.packageName) {
3035 return false;
3036 }
3037 }
3038 return true;
3039 }
3040
3041 @Override
3042 protected ResolveInfo newResult(PackageParser.ServiceIntentInfo filter,
3043 int match) {
3044 final PackageParser.ServiceIntentInfo info = (PackageParser.ServiceIntentInfo)filter;
3045 if (!mSettings.isEnabledLP(info.service.info, mFlags)) {
3046 return null;
3047 }
3048 final PackageParser.Service service = info.service;
3049 if (mSafeMode && (service.info.applicationInfo.flags
3050 &ApplicationInfo.FLAG_SYSTEM) == 0) {
3051 return null;
3052 }
3053 final ResolveInfo res = new ResolveInfo();
3054 res.serviceInfo = PackageParser.generateServiceInfo(service,
3055 mFlags);
3056 if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) {
3057 res.filter = filter;
3058 }
3059 res.priority = info.getPriority();
3060 res.preferredOrder = service.owner.mPreferredOrder;
3061 //System.out.println("Result: " + res.activityInfo.className +
3062 // " = " + res.priority);
3063 res.match = match;
3064 res.isDefault = info.hasDefault;
3065 res.labelRes = info.labelRes;
3066 res.nonLocalizedLabel = info.nonLocalizedLabel;
3067 res.icon = info.icon;
3068 return res;
3069 }
3070
3071 @Override
3072 protected void sortResults(List<ResolveInfo> results) {
3073 Collections.sort(results, mResolvePrioritySorter);
3074 }
3075
3076 @Override
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003077 protected void dumpFilter(PrintWriter out, String prefix,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 PackageParser.ServiceIntentInfo filter) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003079 out.print(prefix); out.print(
3080 Integer.toHexString(System.identityHashCode(filter.service)));
3081 out.print(' ');
3082 out.println(filter.service.componentShortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 }
3084
3085// List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) {
3086// final Iterator<ResolveInfo> i = resolveInfoList.iterator();
3087// final List<ResolveInfo> retList = Lists.newArrayList();
3088// while (i.hasNext()) {
3089// final ResolveInfo resolveInfo = (ResolveInfo) i;
3090// if (isEnabledLP(resolveInfo.serviceInfo)) {
3091// retList.add(resolveInfo);
3092// }
3093// }
3094// return retList;
3095// }
3096
3097 // Keys are String (activity class name), values are Activity.
3098 private final HashMap<ComponentName, PackageParser.Service> mServices
3099 = new HashMap<ComponentName, PackageParser.Service>();
3100 private int mFlags;
3101 };
3102
3103 private static final Comparator<ResolveInfo> mResolvePrioritySorter =
3104 new Comparator<ResolveInfo>() {
3105 public int compare(ResolveInfo r1, ResolveInfo r2) {
3106 int v1 = r1.priority;
3107 int v2 = r2.priority;
3108 //System.out.println("Comparing: q1=" + q1 + " q2=" + q2);
3109 if (v1 != v2) {
3110 return (v1 > v2) ? -1 : 1;
3111 }
3112 v1 = r1.preferredOrder;
3113 v2 = r2.preferredOrder;
3114 if (v1 != v2) {
3115 return (v1 > v2) ? -1 : 1;
3116 }
3117 if (r1.isDefault != r2.isDefault) {
3118 return r1.isDefault ? -1 : 1;
3119 }
3120 v1 = r1.match;
3121 v2 = r2.match;
3122 //System.out.println("Comparing: m1=" + m1 + " m2=" + m2);
3123 return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0);
3124 }
3125 };
3126
3127 private static final Comparator<ProviderInfo> mProviderInitOrderSorter =
3128 new Comparator<ProviderInfo>() {
3129 public int compare(ProviderInfo p1, ProviderInfo p2) {
3130 final int v1 = p1.initOrder;
3131 final int v2 = p2.initOrder;
3132 return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0);
3133 }
3134 };
3135
3136 private static final void sendPackageBroadcast(String action, String pkg, Bundle extras) {
3137 IActivityManager am = ActivityManagerNative.getDefault();
3138 if (am != null) {
3139 try {
3140 final Intent intent = new Intent(action,
3141 pkg != null ? Uri.fromParts("package", pkg, null) : null);
3142 if (extras != null) {
3143 intent.putExtras(extras);
3144 }
3145 am.broadcastIntent(
3146 null, intent,
3147 null, null, 0, null, null, null, false, false);
3148 } catch (RemoteException ex) {
3149 }
3150 }
3151 }
3152
3153 private final class AppDirObserver extends FileObserver {
3154 public AppDirObserver(String path, int mask, boolean isrom) {
3155 super(path, mask);
3156 mRootDir = path;
3157 mIsRom = isrom;
3158 }
3159
3160 public void onEvent(int event, String path) {
3161 String removedPackage = null;
3162 int removedUid = -1;
3163 String addedPackage = null;
3164 int addedUid = -1;
3165
3166 synchronized (mInstallLock) {
3167 String fullPathStr = null;
3168 File fullPath = null;
3169 if (path != null) {
3170 fullPath = new File(mRootDir, path);
3171 fullPathStr = fullPath.getPath();
3172 }
3173
3174 if (Config.LOGV) Log.v(
3175 TAG, "File " + fullPathStr + " changed: "
3176 + Integer.toHexString(event));
3177
3178 if (!isPackageFilename(path)) {
3179 if (Config.LOGV) Log.v(
3180 TAG, "Ignoring change of non-package file: " + fullPathStr);
3181 return;
3182 }
3183
3184 if ((event&REMOVE_EVENTS) != 0) {
3185 synchronized (mInstallLock) {
3186 PackageParser.Package p = mAppDirs.get(fullPathStr);
3187 if (p != null) {
3188 removePackageLI(p, true);
3189 removedPackage = p.applicationInfo.packageName;
3190 removedUid = p.applicationInfo.uid;
3191 }
3192 }
3193 }
3194
3195 if ((event&ADD_EVENTS) != 0) {
3196 PackageParser.Package p = mAppDirs.get(fullPathStr);
3197 if (p == null) {
3198 p = scanPackageLI(fullPath, fullPath, fullPath,
3199 (mIsRom ? PackageParser.PARSE_IS_SYSTEM : 0) |
3200 PackageParser.PARSE_CHATTY |
3201 PackageParser.PARSE_MUST_BE_APK,
3202 SCAN_MONITOR);
3203 if (p != null) {
3204 synchronized (mPackages) {
3205 grantPermissionsLP(p, false);
3206 }
3207 addedPackage = p.applicationInfo.packageName;
3208 addedUid = p.applicationInfo.uid;
3209 }
3210 }
3211 }
3212
3213 synchronized (mPackages) {
3214 mSettings.writeLP();
3215 }
3216 }
3217
3218 if (removedPackage != null) {
3219 Bundle extras = new Bundle(1);
3220 extras.putInt(Intent.EXTRA_UID, removedUid);
3221 extras.putBoolean(Intent.EXTRA_DATA_REMOVED, false);
3222 sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, extras);
3223 }
3224 if (addedPackage != null) {
3225 Bundle extras = new Bundle(1);
3226 extras.putInt(Intent.EXTRA_UID, addedUid);
3227 sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, addedPackage, extras);
3228 }
3229 }
3230
3231 private final String mRootDir;
3232 private final boolean mIsRom;
3233 }
Jacek Surazskic64322c2009-04-28 15:26:38 +02003234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 /* Called when a downloaded package installation has been confirmed by the user */
3236 public void installPackage(
3237 final Uri packageURI, final IPackageInstallObserver observer, final int flags) {
Jacek Surazskic64322c2009-04-28 15:26:38 +02003238 installPackage(packageURI, observer, flags, null);
3239 }
3240
3241 /* Called when a downloaded package installation has been confirmed by the user */
3242 public void installPackage(
3243 final Uri packageURI, final IPackageInstallObserver observer, final int flags,
3244 final String installerPackageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 mContext.enforceCallingOrSelfPermission(
3246 android.Manifest.permission.INSTALL_PACKAGES, null);
Jacek Surazskic64322c2009-04-28 15:26:38 +02003247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 // Queue up an async operation since the package installation may take a little while.
3249 mHandler.post(new Runnable() {
3250 public void run() {
3251 mHandler.removeCallbacks(this);
3252 PackageInstalledInfo res;
3253 synchronized (mInstallLock) {
Jacek Surazskic64322c2009-04-28 15:26:38 +02003254 res = installPackageLI(packageURI, flags, true, installerPackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 }
3256 if (observer != null) {
3257 try {
3258 observer.packageInstalled(res.name, res.returnCode);
3259 } catch (RemoteException e) {
3260 Log.i(TAG, "Observer no longer exists.");
3261 }
3262 }
3263 // There appears to be a subtle deadlock condition if the sendPackageBroadcast
3264 // call appears in the synchronized block above.
3265 if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) {
3266 res.removedInfo.sendBroadcast(false, true);
3267 Bundle extras = new Bundle(1);
3268 extras.putInt(Intent.EXTRA_UID, res.uid);
Dianne Hackbornf63220f2009-03-24 18:38:43 -07003269 final boolean update = res.removedInfo.removedPackage != null;
3270 if (update) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 extras.putBoolean(Intent.EXTRA_REPLACING, true);
3272 }
3273 sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED,
3274 res.pkg.applicationInfo.packageName,
3275 extras);
Dianne Hackbornf63220f2009-03-24 18:38:43 -07003276 if (update) {
3277 sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED,
3278 res.pkg.applicationInfo.packageName,
3279 extras);
3280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 }
3282 Runtime.getRuntime().gc();
3283 }
3284 });
3285 }
3286
3287 class PackageInstalledInfo {
3288 String name;
3289 int uid;
3290 PackageParser.Package pkg;
3291 int returnCode;
3292 PackageRemovedInfo removedInfo;
3293 }
3294
3295 /*
3296 * Install a non-existing package.
3297 */
3298 private void installNewPackageLI(String pkgName,
3299 File tmpPackageFile,
3300 String destFilePath, File destPackageFile, File destResourceFile,
The Android Open Source Project10592532009-03-18 17:39:46 -07003301 PackageParser.Package pkg, boolean forwardLocked, boolean newInstall,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003302 String installerPackageName, PackageInstalledInfo res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 // Remember this for later, in case we need to rollback this install
3304 boolean dataDirExists = (new File(mAppDataDir, pkgName)).exists();
3305 res.name = pkgName;
3306 synchronized(mPackages) {
3307 if (mPackages.containsKey(pkgName) || mAppDirs.containsKey(destFilePath)) {
3308 // Don't allow installation over an existing package with the same name.
3309 Log.w(TAG, "Attempt to re-install " + pkgName
3310 + " without first uninstalling.");
3311 res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS;
3312 return;
3313 }
3314 }
3315 if (destPackageFile.exists()) {
3316 // It's safe to do this because we know (from the above check) that the file
3317 // isn't currently used for an installed package.
3318 destPackageFile.delete();
3319 }
3320 mLastScanError = PackageManager.INSTALL_SUCCEEDED;
3321 PackageParser.Package newPackage = scanPackageLI(tmpPackageFile, destPackageFile,
3322 destResourceFile, pkg, 0,
3323 SCAN_MONITOR | SCAN_FORCE_DEX
3324 | SCAN_UPDATE_SIGNATURE
The Android Open Source Project10592532009-03-18 17:39:46 -07003325 | (forwardLocked ? SCAN_FORWARD_LOCKED : 0)
3326 | (newInstall ? SCAN_NEW_INSTALL : 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 if (newPackage == null) {
3328 Log.w(TAG, "Package couldn't be installed in " + destPackageFile);
3329 if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) {
3330 res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK;
3331 }
3332 } else {
3333 updateSettingsLI(pkgName, tmpPackageFile,
3334 destFilePath, destPackageFile,
3335 destResourceFile, pkg,
3336 newPackage,
3337 true,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003338 forwardLocked,
3339 installerPackageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 res);
3341 // delete the partially installed application. the data directory will have to be
3342 // restored if it was already existing
3343 if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) {
3344 // remove package from internal structures. Note that we want deletePackageX to
3345 // delete the package data and cache directories that it created in
3346 // scanPackageLocked, unless those directories existed before we even tried to
3347 // install.
3348 deletePackageLI(
3349 pkgName, true,
3350 dataDirExists ? PackageManager.DONT_DELETE_DATA : 0,
3351 res.removedInfo);
3352 }
3353 }
3354 }
3355
3356 private void replacePackageLI(String pkgName,
3357 File tmpPackageFile,
3358 String destFilePath, File destPackageFile, File destResourceFile,
The Android Open Source Project10592532009-03-18 17:39:46 -07003359 PackageParser.Package pkg, boolean forwardLocked, boolean newInstall,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003360 String installerPackageName, PackageInstalledInfo res) {
3361
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07003362 PackageParser.Package oldPackage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 // First find the old package info and check signatures
3364 synchronized(mPackages) {
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07003365 oldPackage = mPackages.get(pkgName);
3366 if(checkSignaturesLP(pkg, oldPackage) != PackageManager.SIGNATURE_MATCH) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 res.returnCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
3368 return;
3369 }
3370 }
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07003371 boolean sysPkg = ((oldPackage.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 if(sysPkg) {
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07003373 replaceSystemPackageLI(oldPackage,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 tmpPackageFile, destFilePath,
The Android Open Source Project10592532009-03-18 17:39:46 -07003375 destPackageFile, destResourceFile, pkg, forwardLocked,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003376 newInstall, installerPackageName, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 } else {
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07003378 replaceNonSystemPackageLI(oldPackage, tmpPackageFile, destFilePath,
The Android Open Source Project10592532009-03-18 17:39:46 -07003379 destPackageFile, destResourceFile, pkg, forwardLocked,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003380 newInstall, installerPackageName, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 }
3382 }
3383
3384 private void replaceNonSystemPackageLI(PackageParser.Package deletedPackage,
3385 File tmpPackageFile,
3386 String destFilePath, File destPackageFile, File destResourceFile,
The Android Open Source Project10592532009-03-18 17:39:46 -07003387 PackageParser.Package pkg, boolean forwardLocked, boolean newInstall,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003388 String installerPackageName, PackageInstalledInfo res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 PackageParser.Package newPackage = null;
3390 String pkgName = deletedPackage.packageName;
3391 boolean deletedPkg = true;
3392 boolean updatedSettings = false;
Jacek Surazskic64322c2009-04-28 15:26:38 +02003393
3394 String oldInstallerPackageName = null;
3395 synchronized (mPackages) {
3396 oldInstallerPackageName = mSettings.getInstallerPackageName(pkgName);
3397 }
3398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 int parseFlags = PackageManager.REPLACE_EXISTING_PACKAGE;
3400 // First delete the existing package while retaining the data directory
3401 if (!deletePackageLI(pkgName, false, PackageManager.DONT_DELETE_DATA,
3402 res.removedInfo)) {
3403 // If the existing package was'nt successfully deleted
3404 res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE;
3405 deletedPkg = false;
3406 } else {
3407 // Successfully deleted the old package. Now proceed with re-installation
3408 mLastScanError = PackageManager.INSTALL_SUCCEEDED;
3409 newPackage = scanPackageLI(tmpPackageFile, destPackageFile,
3410 destResourceFile, pkg, parseFlags,
3411 SCAN_MONITOR | SCAN_FORCE_DEX
3412 | SCAN_UPDATE_SIGNATURE
The Android Open Source Project10592532009-03-18 17:39:46 -07003413 | (forwardLocked ? SCAN_FORWARD_LOCKED : 0)
3414 | (newInstall ? SCAN_NEW_INSTALL : 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 if (newPackage == null) {
3416 Log.w(TAG, "Package couldn't be installed in " + destPackageFile);
3417 if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) {
3418 res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK;
3419 }
3420 } else {
3421 updateSettingsLI(pkgName, tmpPackageFile,
3422 destFilePath, destPackageFile,
3423 destResourceFile, pkg,
3424 newPackage,
3425 true,
3426 forwardLocked,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003427 installerPackageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 res);
3429 updatedSettings = true;
3430 }
3431 }
3432
3433 if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) {
3434 // If we deleted an exisiting package, the old source and resource files that we
3435 // were keeping around in case we needed them (see below) can now be deleted
3436 final ApplicationInfo deletedPackageAppInfo = deletedPackage.applicationInfo;
3437 final ApplicationInfo installedPackageAppInfo =
3438 newPackage.applicationInfo;
3439 if (!deletedPackageAppInfo.sourceDir
3440 .equals(installedPackageAppInfo.sourceDir)) {
3441 new File(deletedPackageAppInfo.sourceDir).delete();
3442 }
3443 if (!deletedPackageAppInfo.publicSourceDir
3444 .equals(installedPackageAppInfo.publicSourceDir)) {
3445 new File(deletedPackageAppInfo.publicSourceDir).delete();
3446 }
3447 //update signature on the new package setting
3448 //this should always succeed, since we checked the
3449 //signature earlier.
3450 synchronized(mPackages) {
3451 verifySignaturesLP(mSettings.mPackages.get(pkgName), pkg,
3452 parseFlags, true);
3453 }
3454 } else {
3455 // remove package from internal structures. Note that we want deletePackageX to
3456 // delete the package data and cache directories that it created in
3457 // scanPackageLocked, unless those directories existed before we even tried to
3458 // install.
3459 if(updatedSettings) {
3460 deletePackageLI(
3461 pkgName, true,
3462 PackageManager.DONT_DELETE_DATA,
3463 res.removedInfo);
3464 }
3465 // Since we failed to install the new package we need to restore the old
3466 // package that we deleted.
3467 if(deletedPkg) {
3468 installPackageLI(
3469 Uri.fromFile(new File(deletedPackage.mPath)),
3470 isForwardLocked(deletedPackage)
3471 ? PackageManager.FORWARD_LOCK_PACKAGE
Jacek Surazskic64322c2009-04-28 15:26:38 +02003472 : 0, false, oldInstallerPackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 }
3474 }
3475 }
3476
3477 private void replaceSystemPackageLI(PackageParser.Package deletedPackage,
3478 File tmpPackageFile,
3479 String destFilePath, File destPackageFile, File destResourceFile,
The Android Open Source Project10592532009-03-18 17:39:46 -07003480 PackageParser.Package pkg, boolean forwardLocked, boolean newInstall,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003481 String installerPackageName, PackageInstalledInfo res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 PackageParser.Package newPackage = null;
3483 boolean updatedSettings = false;
3484 int parseFlags = PackageManager.REPLACE_EXISTING_PACKAGE |
3485 PackageParser.PARSE_IS_SYSTEM;
3486 String packageName = deletedPackage.packageName;
3487 res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE;
3488 if (packageName == null) {
3489 Log.w(TAG, "Attempt to delete null packageName.");
3490 return;
3491 }
3492 PackageParser.Package oldPkg;
3493 PackageSetting oldPkgSetting;
3494 synchronized (mPackages) {
3495 oldPkg = mPackages.get(packageName);
3496 oldPkgSetting = mSettings.mPackages.get(packageName);
3497 if((oldPkg == null) || (oldPkg.applicationInfo == null) ||
3498 (oldPkgSetting == null)) {
3499 Log.w(TAG, "Could'nt find package:"+packageName+" information");
3500 return;
3501 }
3502 }
3503 res.removedInfo.uid = oldPkg.applicationInfo.uid;
3504 res.removedInfo.removedPackage = packageName;
3505 // Remove existing system package
3506 removePackageLI(oldPkg, true);
3507 synchronized (mPackages) {
3508 res.removedInfo.removedUid = mSettings.disableSystemPackageLP(packageName);
3509 }
3510
3511 // Successfully disabled the old package. Now proceed with re-installation
3512 mLastScanError = PackageManager.INSTALL_SUCCEEDED;
3513 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
3514 newPackage = scanPackageLI(tmpPackageFile, destPackageFile,
3515 destResourceFile, pkg, parseFlags,
3516 SCAN_MONITOR | SCAN_FORCE_DEX
3517 | SCAN_UPDATE_SIGNATURE
The Android Open Source Project10592532009-03-18 17:39:46 -07003518 | (forwardLocked ? SCAN_FORWARD_LOCKED : 0)
3519 | (newInstall ? SCAN_NEW_INSTALL : 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 if (newPackage == null) {
3521 Log.w(TAG, "Package couldn't be installed in " + destPackageFile);
3522 if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) {
3523 res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK;
3524 }
3525 } else {
3526 updateSettingsLI(packageName, tmpPackageFile,
3527 destFilePath, destPackageFile,
3528 destResourceFile, pkg,
3529 newPackage,
3530 true,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003531 forwardLocked,
3532 installerPackageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 res);
3534 updatedSettings = true;
3535 }
3536
3537 if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) {
3538 //update signature on the new package setting
3539 //this should always succeed, since we checked the
3540 //signature earlier.
3541 synchronized(mPackages) {
3542 verifySignaturesLP(mSettings.mPackages.get(packageName), pkg,
3543 parseFlags, true);
3544 }
3545 } else {
3546 // Re installation failed. Restore old information
3547 // Remove new pkg information
3548 removePackageLI(newPackage, true);
3549 // Add back the old system package
3550 scanPackageLI(oldPkgSetting.codePath, oldPkgSetting.codePath,
3551 oldPkgSetting.resourcePath,
3552 oldPkg, parseFlags,
3553 SCAN_MONITOR
The Android Open Source Project10592532009-03-18 17:39:46 -07003554 | SCAN_UPDATE_SIGNATURE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 // Restore the old system information in Settings
3556 synchronized(mPackages) {
3557 if(updatedSettings) {
3558 mSettings.enableSystemPackageLP(packageName);
Jacek Surazskic64322c2009-04-28 15:26:38 +02003559 mSettings.setInstallerPackageName(packageName,
3560 oldPkgSetting.installerPackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 }
3562 mSettings.writeLP();
3563 }
3564 }
3565 }
3566
3567 private void updateSettingsLI(String pkgName, File tmpPackageFile,
3568 String destFilePath, File destPackageFile,
3569 File destResourceFile,
3570 PackageParser.Package pkg,
3571 PackageParser.Package newPackage,
3572 boolean replacingExistingPackage,
3573 boolean forwardLocked,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003574 String installerPackageName, PackageInstalledInfo res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 synchronized (mPackages) {
3576 //write settings. the installStatus will be incomplete at this stage.
3577 //note that the new package setting would have already been
3578 //added to mPackages. It hasn't been persisted yet.
3579 mSettings.setInstallStatus(pkgName, PKG_INSTALL_INCOMPLETE);
3580 mSettings.writeLP();
3581 }
3582
3583 int retCode = 0;
3584 if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) {
3585 retCode = mInstaller.movedex(tmpPackageFile.toString(),
3586 destPackageFile.toString());
3587 if (retCode != 0) {
3588 Log.e(TAG, "Couldn't rename dex file: " + destPackageFile);
3589 res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
3590 return;
3591 }
3592 }
3593 // XXX There are probably some big issues here: upon doing
3594 // the rename, we have reached the point of no return (the
3595 // original .apk is gone!), so we can't fail. Yet... we can.
3596 if (!tmpPackageFile.renameTo(destPackageFile)) {
3597 Log.e(TAG, "Couldn't move package file to: " + destPackageFile);
3598 res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
3599 } else {
3600 res.returnCode = setPermissionsLI(pkgName, newPackage, destFilePath,
3601 destResourceFile,
3602 forwardLocked);
3603 if(res.returnCode != PackageManager.INSTALL_SUCCEEDED) {
3604 return;
3605 } else {
3606 Log.d(TAG, "New package installed in " + destPackageFile);
3607 }
3608 }
3609 if(res.returnCode != PackageManager.INSTALL_SUCCEEDED) {
3610 if (mInstaller != null) {
3611 mInstaller.rmdex(tmpPackageFile.getPath());
3612 }
3613 }
3614
3615 synchronized (mPackages) {
3616 grantPermissionsLP(newPackage, true);
3617 res.name = pkgName;
3618 res.uid = newPackage.applicationInfo.uid;
3619 res.pkg = newPackage;
3620 mSettings.setInstallStatus(pkgName, PKG_INSTALL_COMPLETE);
Jacek Surazskic64322c2009-04-28 15:26:38 +02003621 mSettings.setInstallerPackageName(pkgName, installerPackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 res.returnCode = PackageManager.INSTALL_SUCCEEDED;
3623 //to update install status
3624 mSettings.writeLP();
3625 }
3626 }
3627
The Android Open Source Project10592532009-03-18 17:39:46 -07003628 private PackageInstalledInfo installPackageLI(Uri pPackageURI,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003629 int pFlags, boolean newInstall, String installerPackageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 File tmpPackageFile = null;
3631 String pkgName = null;
3632 boolean forwardLocked = false;
3633 boolean replacingExistingPackage = false;
3634 // Result object to be returned
3635 PackageInstalledInfo res = new PackageInstalledInfo();
3636 res.returnCode = PackageManager.INSTALL_SUCCEEDED;
3637 res.uid = -1;
3638 res.pkg = null;
3639 res.removedInfo = new PackageRemovedInfo();
3640
3641 main_flow: try {
3642 tmpPackageFile = createTempPackageFile();
3643 if (tmpPackageFile == null) {
3644 res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
3645 break main_flow;
3646 }
3647 tmpPackageFile.deleteOnExit(); // paranoia
3648 if (pPackageURI.getScheme().equals("file")) {
3649 final File srcPackageFile = new File(pPackageURI.getPath());
3650 // We copy the source package file to a temp file and then rename it to the
3651 // destination file in order to eliminate a window where the package directory
3652 // scanner notices the new package file but it's not completely copied yet.
3653 if (!FileUtils.copyFile(srcPackageFile, tmpPackageFile)) {
3654 Log.e(TAG, "Couldn't copy package file to temp file.");
3655 res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
3656 break main_flow;
3657 }
3658 } else if (pPackageURI.getScheme().equals("content")) {
3659 ParcelFileDescriptor fd;
3660 try {
3661 fd = mContext.getContentResolver().openFileDescriptor(pPackageURI, "r");
3662 } catch (FileNotFoundException e) {
3663 Log.e(TAG, "Couldn't open file descriptor from download service.");
3664 res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
3665 break main_flow;
3666 }
3667 if (fd == null) {
3668 Log.e(TAG, "Couldn't open file descriptor from download service (null).");
3669 res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
3670 break main_flow;
3671 }
3672 if (Config.LOGV) {
3673 Log.v(TAG, "Opened file descriptor from download service.");
3674 }
3675 ParcelFileDescriptor.AutoCloseInputStream
3676 dlStream = new ParcelFileDescriptor.AutoCloseInputStream(fd);
3677 // We copy the source package file to a temp file and then rename it to the
3678 // destination file in order to eliminate a window where the package directory
3679 // scanner notices the new package file but it's not completely copied yet.
3680 if (!FileUtils.copyToFile(dlStream, tmpPackageFile)) {
3681 Log.e(TAG, "Couldn't copy package stream to temp file.");
3682 res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
3683 break main_flow;
3684 }
3685 } else {
3686 Log.e(TAG, "Package URI is not 'file:' or 'content:' - " + pPackageURI);
3687 res.returnCode = PackageManager.INSTALL_FAILED_INVALID_URI;
3688 break main_flow;
3689 }
3690 pkgName = PackageParser.parsePackageName(
3691 tmpPackageFile.getAbsolutePath(), 0);
3692 if (pkgName == null) {
3693 Log.e(TAG, "Couldn't find a package name in : " + tmpPackageFile);
3694 res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK;
3695 break main_flow;
3696 }
3697 res.name = pkgName;
3698 //initialize some variables before installing pkg
3699 final String pkgFileName = pkgName + ".apk";
3700 final File destDir = ((pFlags&PackageManager.FORWARD_LOCK_PACKAGE) != 0)
3701 ? mDrmAppPrivateInstallDir
3702 : mAppInstallDir;
3703 final File destPackageFile = new File(destDir, pkgFileName);
3704 final String destFilePath = destPackageFile.getAbsolutePath();
3705 File destResourceFile;
3706 if ((pFlags&PackageManager.FORWARD_LOCK_PACKAGE) != 0) {
3707 final String publicZipFileName = pkgName + ".zip";
3708 destResourceFile = new File(mAppInstallDir, publicZipFileName);
3709 forwardLocked = true;
3710 } else {
3711 destResourceFile = destPackageFile;
3712 }
3713 // Retrieve PackageSettings and parse package
3714 int parseFlags = PackageParser.PARSE_CHATTY;
3715 parseFlags |= mDefParseFlags;
3716 PackageParser pp = new PackageParser(tmpPackageFile.getPath());
3717 pp.setSeparateProcesses(mSeparateProcesses);
Dianne Hackborn851a5412009-05-08 12:06:44 -07003718 pp.setSdkVersion(mSdkVersion, mSdkCodename);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 final PackageParser.Package pkg = pp.parsePackage(tmpPackageFile,
3720 destPackageFile.getAbsolutePath(), mMetrics, parseFlags);
3721 if (pkg == null) {
3722 res.returnCode = pp.getParseError();
3723 break main_flow;
3724 }
3725 if (GET_CERTIFICATES && !pp.collectCertificates(pkg, parseFlags)) {
3726 res.returnCode = pp.getParseError();
3727 break main_flow;
3728 }
3729
3730 synchronized (mPackages) {
3731 //check if installing already existing package
3732 if ((pFlags&PackageManager.REPLACE_EXISTING_PACKAGE) != 0
3733 && mPackages.containsKey(pkgName)) {
3734 replacingExistingPackage = true;
3735 }
3736 }
3737
3738 if(replacingExistingPackage) {
3739 replacePackageLI(pkgName,
3740 tmpPackageFile,
3741 destFilePath, destPackageFile, destResourceFile,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003742 pkg, forwardLocked, newInstall, installerPackageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 res);
3744 } else {
3745 installNewPackageLI(pkgName,
3746 tmpPackageFile,
3747 destFilePath, destPackageFile, destResourceFile,
Jacek Surazskic64322c2009-04-28 15:26:38 +02003748 pkg, forwardLocked, newInstall, installerPackageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 res);
3750 }
3751 } finally {
3752 if (tmpPackageFile != null && tmpPackageFile.exists()) {
3753 tmpPackageFile.delete();
3754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003756 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 }
3758
3759 private int setPermissionsLI(String pkgName,
3760 PackageParser.Package newPackage,
3761 String destFilePath,
3762 File destResourceFile,
3763 boolean forwardLocked) {
3764 int retCode;
3765 if (forwardLocked) {
3766 try {
3767 extractPublicFiles(newPackage, destResourceFile);
3768 } catch (IOException e) {
3769 Log.e(TAG, "Couldn't create a new zip file for the public parts of a" +
3770 " forward-locked app.");
3771 return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
3772 } finally {
3773 //TODO clean up the extracted public files
3774 }
3775 if (mInstaller != null) {
3776 retCode = mInstaller.setForwardLockPerm(pkgName,
3777 newPackage.applicationInfo.uid);
3778 } else {
3779 final int filePermissions =
3780 FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP;
3781 retCode = FileUtils.setPermissions(destFilePath, filePermissions, -1,
3782 newPackage.applicationInfo.uid);
3783 }
3784 } else {
3785 final int filePermissions =
3786 FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP
3787 |FileUtils.S_IROTH;
3788 retCode = FileUtils.setPermissions(destFilePath, filePermissions, -1, -1);
3789 }
3790 if (retCode != 0) {
3791 Log.e(TAG, "Couldn't set new package file permissions for " + destFilePath
3792 + ". The return code was: " + retCode);
3793 }
3794 return PackageManager.INSTALL_SUCCEEDED;
3795 }
3796
3797 private boolean isForwardLocked(PackageParser.Package deletedPackage) {
3798 final ApplicationInfo applicationInfo = deletedPackage.applicationInfo;
3799 return applicationInfo.sourceDir.startsWith(mDrmAppPrivateInstallDir.getAbsolutePath());
3800 }
3801
3802 private void extractPublicFiles(PackageParser.Package newPackage,
3803 File publicZipFile) throws IOException {
3804 final ZipOutputStream publicZipOutStream =
3805 new ZipOutputStream(new FileOutputStream(publicZipFile));
3806 final ZipFile privateZip = new ZipFile(newPackage.mPath);
3807
3808 // Copy manifest, resources.arsc and res directory to public zip
3809
3810 final Enumeration<? extends ZipEntry> privateZipEntries = privateZip.entries();
3811 while (privateZipEntries.hasMoreElements()) {
3812 final ZipEntry zipEntry = privateZipEntries.nextElement();
3813 final String zipEntryName = zipEntry.getName();
3814 if ("AndroidManifest.xml".equals(zipEntryName)
3815 || "resources.arsc".equals(zipEntryName)
3816 || zipEntryName.startsWith("res/")) {
3817 try {
3818 copyZipEntry(zipEntry, privateZip, publicZipOutStream);
3819 } catch (IOException e) {
3820 try {
3821 publicZipOutStream.close();
3822 throw e;
3823 } finally {
3824 publicZipFile.delete();
3825 }
3826 }
3827 }
3828 }
3829
3830 publicZipOutStream.close();
3831 FileUtils.setPermissions(
3832 publicZipFile.getAbsolutePath(),
3833 FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP|FileUtils.S_IROTH,
3834 -1, -1);
3835 }
3836
3837 private static void copyZipEntry(ZipEntry zipEntry,
3838 ZipFile inZipFile,
3839 ZipOutputStream outZipStream) throws IOException {
3840 byte[] buffer = new byte[4096];
3841 int num;
3842
3843 ZipEntry newEntry;
3844 if (zipEntry.getMethod() == ZipEntry.STORED) {
3845 // Preserve the STORED method of the input entry.
3846 newEntry = new ZipEntry(zipEntry);
3847 } else {
3848 // Create a new entry so that the compressed len is recomputed.
3849 newEntry = new ZipEntry(zipEntry.getName());
3850 }
3851 outZipStream.putNextEntry(newEntry);
3852
3853 InputStream data = inZipFile.getInputStream(zipEntry);
3854 while ((num = data.read(buffer)) > 0) {
3855 outZipStream.write(buffer, 0, num);
3856 }
3857 outZipStream.flush();
3858 }
3859
3860 private void deleteTempPackageFiles() {
3861 FilenameFilter filter = new FilenameFilter() {
3862 public boolean accept(File dir, String name) {
3863 return name.startsWith("vmdl") && name.endsWith(".tmp");
3864 }
3865 };
3866 String tmpFilesList[] = mAppInstallDir.list(filter);
3867 if(tmpFilesList == null) {
3868 return;
3869 }
3870 for(int i = 0; i < tmpFilesList.length; i++) {
3871 File tmpFile = new File(mAppInstallDir, tmpFilesList[i]);
3872 tmpFile.delete();
3873 }
3874 }
3875
3876 private File createTempPackageFile() {
3877 File tmpPackageFile;
3878 try {
3879 tmpPackageFile = File.createTempFile("vmdl", ".tmp", mAppInstallDir);
3880 } catch (IOException e) {
3881 Log.e(TAG, "Couldn't create temp file for downloaded package file.");
3882 return null;
3883 }
3884 try {
3885 FileUtils.setPermissions(
3886 tmpPackageFile.getCanonicalPath(), FileUtils.S_IRUSR|FileUtils.S_IWUSR,
3887 -1, -1);
3888 } catch (IOException e) {
3889 Log.e(TAG, "Trouble getting the canoncical path for a temp file.");
3890 return null;
3891 }
3892 return tmpPackageFile;
3893 }
3894
3895 public void deletePackage(final String packageName,
3896 final IPackageDeleteObserver observer,
3897 final int flags) {
3898 mContext.enforceCallingOrSelfPermission(
3899 android.Manifest.permission.DELETE_PACKAGES, null);
3900 // Queue up an async operation since the package deletion may take a little while.
3901 mHandler.post(new Runnable() {
3902 public void run() {
3903 mHandler.removeCallbacks(this);
3904 final boolean succeded = deletePackageX(packageName, true, true, flags);
3905 if (observer != null) {
3906 try {
3907 observer.packageDeleted(succeded);
3908 } catch (RemoteException e) {
3909 Log.i(TAG, "Observer no longer exists.");
3910 } //end catch
3911 } //end if
3912 } //end run
3913 });
3914 }
3915
3916 /**
3917 * This method is an internal method that could be get invoked either
3918 * to delete an installed package or to clean up a failed installation.
3919 * After deleting an installed package, a broadcast is sent to notify any
3920 * listeners that the package has been installed. For cleaning up a failed
3921 * installation, the broadcast is not necessary since the package's
3922 * installation wouldn't have sent the initial broadcast either
3923 * The key steps in deleting a package are
3924 * deleting the package information in internal structures like mPackages,
3925 * deleting the packages base directories through installd
3926 * updating mSettings to reflect current status
3927 * persisting settings for later use
3928 * sending a broadcast if necessary
3929 */
3930
3931 private boolean deletePackageX(String packageName, boolean sendBroadCast,
3932 boolean deleteCodeAndResources, int flags) {
3933 PackageRemovedInfo info = new PackageRemovedInfo();
Romain Guy96f43572009-03-24 20:27:49 -07003934 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935
3936 synchronized (mInstallLock) {
3937 res = deletePackageLI(packageName, deleteCodeAndResources, flags, info);
3938 }
3939
3940 if(res && sendBroadCast) {
Romain Guy96f43572009-03-24 20:27:49 -07003941 boolean systemUpdate = info.isRemovedPackageSystemUpdate;
3942 info.sendBroadcast(deleteCodeAndResources, systemUpdate);
3943
3944 // If the removed package was a system update, the old system packaged
3945 // was re-enabled; we need to broadcast this information
3946 if (systemUpdate) {
3947 Bundle extras = new Bundle(1);
3948 extras.putInt(Intent.EXTRA_UID, info.removedUid >= 0 ? info.removedUid : info.uid);
3949 extras.putBoolean(Intent.EXTRA_REPLACING, true);
3950
3951 sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, packageName, extras);
3952 sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName, extras);
3953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 }
3955 return res;
3956 }
3957
3958 static class PackageRemovedInfo {
3959 String removedPackage;
3960 int uid = -1;
3961 int removedUid = -1;
Romain Guy96f43572009-03-24 20:27:49 -07003962 boolean isRemovedPackageSystemUpdate = false;
3963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 void sendBroadcast(boolean fullRemove, boolean replacing) {
3965 Bundle extras = new Bundle(1);
3966 extras.putInt(Intent.EXTRA_UID, removedUid >= 0 ? removedUid : uid);
3967 extras.putBoolean(Intent.EXTRA_DATA_REMOVED, fullRemove);
3968 if (replacing) {
3969 extras.putBoolean(Intent.EXTRA_REPLACING, true);
3970 }
3971 if (removedPackage != null) {
3972 sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, extras);
3973 }
3974 if (removedUid >= 0) {
3975 sendPackageBroadcast(Intent.ACTION_UID_REMOVED, null, extras);
3976 }
3977 }
3978 }
3979
3980 /*
3981 * This method deletes the package from internal data structures. If the DONT_DELETE_DATA
3982 * flag is not set, the data directory is removed as well.
3983 * make sure this flag is set for partially installed apps. If not its meaningless to
3984 * delete a partially installed application.
3985 */
3986 private void removePackageDataLI(PackageParser.Package p, PackageRemovedInfo outInfo,
3987 int flags) {
3988 String packageName = p.packageName;
3989 outInfo.removedPackage = packageName;
3990 removePackageLI(p, true);
3991 // Retrieve object to delete permissions for shared user later on
3992 PackageSetting deletedPs;
3993 synchronized (mPackages) {
3994 deletedPs = mSettings.mPackages.get(packageName);
3995 }
3996 if ((flags&PackageManager.DONT_DELETE_DATA) == 0) {
3997 if (mInstaller != null) {
3998 int retCode = mInstaller.remove(packageName);
3999 if (retCode < 0) {
4000 Log.w(TAG, "Couldn't remove app data or cache directory for package: "
4001 + packageName + ", retcode=" + retCode);
4002 // we don't consider this to be a failure of the core package deletion
4003 }
4004 } else {
4005 //for emulator
4006 PackageParser.Package pkg = mPackages.get(packageName);
4007 File dataDir = new File(pkg.applicationInfo.dataDir);
4008 dataDir.delete();
4009 }
4010 synchronized (mPackages) {
4011 outInfo.removedUid = mSettings.removePackageLP(packageName);
4012 }
4013 }
4014 synchronized (mPackages) {
4015 if ( (deletedPs != null) && (deletedPs.sharedUser != null)) {
4016 // remove permissions associated with package
4017 mSettings.updateSharedUserPerms (deletedPs);
4018 }
4019 // Save settings now
4020 mSettings.writeLP ();
4021 }
4022 }
4023
4024 /*
4025 * Tries to delete system package.
4026 */
4027 private boolean deleteSystemPackageLI(PackageParser.Package p,
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07004028 int flags, PackageRemovedInfo outInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 ApplicationInfo applicationInfo = p.applicationInfo;
4030 //applicable for non-partially installed applications only
4031 if (applicationInfo == null) {
4032 Log.w(TAG, "Package " + p.packageName + " has no applicationInfo.");
4033 return false;
4034 }
4035 PackageSetting ps = null;
4036 // Confirm if the system package has been updated
4037 // An updated system app can be deleted. This will also have to restore
4038 // the system pkg from system partition
4039 synchronized (mPackages) {
4040 ps = mSettings.getDisabledSystemPkg(p.packageName);
4041 }
4042 if (ps == null) {
4043 Log.w(TAG, "Attempt to delete system package "+ p.packageName);
4044 return false;
4045 } else {
4046 Log.i(TAG, "Deleting system pkg from data partition");
4047 }
4048 // Delete the updated package
Romain Guy96f43572009-03-24 20:27:49 -07004049 outInfo.isRemovedPackageSystemUpdate = true;
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07004050 boolean deleteCodeAndResources = false;
4051 if (ps.versionCode < p.mVersionCode) {
4052 // Delete code and resources for downgrades
4053 deleteCodeAndResources = true;
4054 if ((flags & PackageManager.DONT_DELETE_DATA) == 0) {
4055 flags &= ~PackageManager.DONT_DELETE_DATA;
4056 }
4057 } else {
4058 // Preserve data by setting flag
4059 if ((flags & PackageManager.DONT_DELETE_DATA) == 0) {
4060 flags |= PackageManager.DONT_DELETE_DATA;
4061 }
4062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 boolean ret = deleteInstalledPackageLI(p, deleteCodeAndResources, flags, outInfo);
4064 if (!ret) {
4065 return false;
4066 }
4067 synchronized (mPackages) {
4068 // Reinstate the old system package
4069 mSettings.enableSystemPackageLP(p.packageName);
4070 }
4071 // Install the system package
4072 PackageParser.Package newPkg = scanPackageLI(ps.codePath, ps.codePath, ps.resourcePath,
4073 PackageParser.PARSE_MUST_BE_APK | PackageParser.PARSE_IS_SYSTEM,
4074 SCAN_MONITOR);
4075
4076 if (newPkg == null) {
4077 Log.w(TAG, "Failed to restore system package:"+p.packageName+" with error:" + mLastScanError);
4078 return false;
4079 }
4080 synchronized (mPackages) {
4081 mSettings.writeLP();
4082 }
4083 return true;
4084 }
4085
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07004086 private void deletePackageResourcesLI(String packageName,
4087 String sourceDir, String publicSourceDir) {
4088 File sourceFile = new File(sourceDir);
4089 if (!sourceFile.exists()) {
4090 Log.w(TAG, "Package source " + sourceDir + " does not exist.");
4091 }
4092 // Delete application's code and resources
4093 sourceFile.delete();
4094 final File publicSourceFile = new File(publicSourceDir);
4095 if (publicSourceFile.exists()) {
4096 publicSourceFile.delete();
4097 }
4098 if (mInstaller != null) {
4099 int retCode = mInstaller.rmdex(sourceFile.toString());
4100 if (retCode < 0) {
4101 Log.w(TAG, "Couldn't remove dex file for package: "
4102 + packageName + " at location " + sourceFile.toString() + ", retcode=" + retCode);
4103 // we don't consider this to be a failure of the core package deletion
4104 }
4105 }
4106 }
4107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 private boolean deleteInstalledPackageLI(PackageParser.Package p,
4109 boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo) {
4110 ApplicationInfo applicationInfo = p.applicationInfo;
4111 if (applicationInfo == null) {
4112 Log.w(TAG, "Package " + p.packageName + " has no applicationInfo.");
4113 return false;
4114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 outInfo.uid = applicationInfo.uid;
4116
4117 // Delete package data from internal structures and also remove data if flag is set
4118 removePackageDataLI(p, outInfo, flags);
4119
4120 // Delete application code and resources
4121 if (deleteCodeAndResources) {
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07004122 deletePackageResourcesLI(applicationInfo.packageName,
4123 applicationInfo.sourceDir, applicationInfo.publicSourceDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 }
4125 return true;
4126 }
4127
4128 /*
4129 * This method handles package deletion in general
4130 */
4131 private boolean deletePackageLI(String packageName,
4132 boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo) {
4133 if (packageName == null) {
4134 Log.w(TAG, "Attempt to delete null packageName.");
4135 return false;
4136 }
4137 PackageParser.Package p;
4138 boolean dataOnly = false;
4139 synchronized (mPackages) {
4140 p = mPackages.get(packageName);
4141 if (p == null) {
4142 //this retrieves partially installed apps
4143 dataOnly = true;
4144 PackageSetting ps = mSettings.mPackages.get(packageName);
4145 if (ps == null) {
4146 Log.w(TAG, "Package named '" + packageName +"' doesn't exist.");
4147 return false;
4148 }
4149 p = ps.pkg;
4150 }
4151 }
4152 if (p == null) {
4153 Log.w(TAG, "Package named '" + packageName +"' doesn't exist.");
4154 return false;
4155 }
4156
4157 if (dataOnly) {
4158 // Delete application data first
4159 removePackageDataLI(p, outInfo, flags);
4160 return true;
4161 }
4162 // At this point the package should have ApplicationInfo associated with it
4163 if (p.applicationInfo == null) {
4164 Log.w(TAG, "Package " + p.packageName + " has no applicationInfo.");
4165 return false;
4166 }
4167 if ( (p.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4168 Log.i(TAG, "Removing system package:"+p.packageName);
4169 // When an updated system application is deleted we delete the existing resources as well and
4170 // fall back to existing code in system partition
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07004171 return deleteSystemPackageLI(p, flags, outInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 }
4173 Log.i(TAG, "Removing non-system package:"+p.packageName);
4174 return deleteInstalledPackageLI (p, deleteCodeAndResources, flags, outInfo);
4175 }
4176
4177 public void clearApplicationUserData(final String packageName,
4178 final IPackageDataObserver observer) {
4179 mContext.enforceCallingOrSelfPermission(
4180 android.Manifest.permission.CLEAR_APP_USER_DATA, null);
4181 // Queue up an async operation since the package deletion may take a little while.
4182 mHandler.post(new Runnable() {
4183 public void run() {
4184 mHandler.removeCallbacks(this);
4185 final boolean succeeded;
4186 synchronized (mInstallLock) {
4187 succeeded = clearApplicationUserDataLI(packageName);
4188 }
4189 if (succeeded) {
4190 // invoke DeviceStorageMonitor's update method to clear any notifications
4191 DeviceStorageMonitorService dsm = (DeviceStorageMonitorService)
4192 ServiceManager.getService(DeviceStorageMonitorService.SERVICE);
4193 if (dsm != null) {
4194 dsm.updateMemory();
4195 }
4196 }
4197 if(observer != null) {
4198 try {
4199 observer.onRemoveCompleted(packageName, succeeded);
4200 } catch (RemoteException e) {
4201 Log.i(TAG, "Observer no longer exists.");
4202 }
4203 } //end if observer
4204 } //end run
4205 });
4206 }
4207
4208 private boolean clearApplicationUserDataLI(String packageName) {
4209 if (packageName == null) {
4210 Log.w(TAG, "Attempt to delete null packageName.");
4211 return false;
4212 }
4213 PackageParser.Package p;
4214 boolean dataOnly = false;
4215 synchronized (mPackages) {
4216 p = mPackages.get(packageName);
4217 if(p == null) {
4218 dataOnly = true;
4219 PackageSetting ps = mSettings.mPackages.get(packageName);
4220 if((ps == null) || (ps.pkg == null)) {
4221 Log.w(TAG, "Package named '" + packageName +"' doesn't exist.");
4222 return false;
4223 }
4224 p = ps.pkg;
4225 }
4226 }
4227 if(!dataOnly) {
4228 //need to check this only for fully installed applications
4229 if (p == null) {
4230 Log.w(TAG, "Package named '" + packageName +"' doesn't exist.");
4231 return false;
4232 }
4233 final ApplicationInfo applicationInfo = p.applicationInfo;
4234 if (applicationInfo == null) {
4235 Log.w(TAG, "Package " + packageName + " has no applicationInfo.");
4236 return false;
4237 }
4238 }
4239 if (mInstaller != null) {
4240 int retCode = mInstaller.clearUserData(packageName);
4241 if (retCode < 0) {
4242 Log.w(TAG, "Couldn't remove cache files for package: "
4243 + packageName);
4244 return false;
4245 }
4246 }
4247 return true;
4248 }
4249
4250 public void deleteApplicationCacheFiles(final String packageName,
4251 final IPackageDataObserver observer) {
4252 mContext.enforceCallingOrSelfPermission(
4253 android.Manifest.permission.DELETE_CACHE_FILES, null);
4254 // Queue up an async operation since the package deletion may take a little while.
4255 mHandler.post(new Runnable() {
4256 public void run() {
4257 mHandler.removeCallbacks(this);
4258 final boolean succeded;
4259 synchronized (mInstallLock) {
4260 succeded = deleteApplicationCacheFilesLI(packageName);
4261 }
4262 if(observer != null) {
4263 try {
4264 observer.onRemoveCompleted(packageName, succeded);
4265 } catch (RemoteException e) {
4266 Log.i(TAG, "Observer no longer exists.");
4267 }
4268 } //end if observer
4269 } //end run
4270 });
4271 }
4272
4273 private boolean deleteApplicationCacheFilesLI(String packageName) {
4274 if (packageName == null) {
4275 Log.w(TAG, "Attempt to delete null packageName.");
4276 return false;
4277 }
4278 PackageParser.Package p;
4279 synchronized (mPackages) {
4280 p = mPackages.get(packageName);
4281 }
4282 if (p == null) {
4283 Log.w(TAG, "Package named '" + packageName +"' doesn't exist.");
4284 return false;
4285 }
4286 final ApplicationInfo applicationInfo = p.applicationInfo;
4287 if (applicationInfo == null) {
4288 Log.w(TAG, "Package " + packageName + " has no applicationInfo.");
4289 return false;
4290 }
4291 if (mInstaller != null) {
4292 int retCode = mInstaller.deleteCacheFiles(packageName);
4293 if (retCode < 0) {
4294 Log.w(TAG, "Couldn't remove cache files for package: "
4295 + packageName);
4296 return false;
4297 }
4298 }
4299 return true;
4300 }
4301
4302 public void getPackageSizeInfo(final String packageName,
4303 final IPackageStatsObserver observer) {
4304 mContext.enforceCallingOrSelfPermission(
4305 android.Manifest.permission.GET_PACKAGE_SIZE, null);
4306 // Queue up an async operation since the package deletion may take a little while.
4307 mHandler.post(new Runnable() {
4308 public void run() {
4309 mHandler.removeCallbacks(this);
4310 PackageStats lStats = new PackageStats(packageName);
4311 final boolean succeded;
4312 synchronized (mInstallLock) {
4313 succeded = getPackageSizeInfoLI(packageName, lStats);
4314 }
4315 if(observer != null) {
4316 try {
4317 observer.onGetStatsCompleted(lStats, succeded);
4318 } catch (RemoteException e) {
4319 Log.i(TAG, "Observer no longer exists.");
4320 }
4321 } //end if observer
4322 } //end run
4323 });
4324 }
4325
4326 private boolean getPackageSizeInfoLI(String packageName, PackageStats pStats) {
4327 if (packageName == null) {
4328 Log.w(TAG, "Attempt to get size of null packageName.");
4329 return false;
4330 }
4331 PackageParser.Package p;
4332 boolean dataOnly = false;
4333 synchronized (mPackages) {
4334 p = mPackages.get(packageName);
4335 if(p == null) {
4336 dataOnly = true;
4337 PackageSetting ps = mSettings.mPackages.get(packageName);
4338 if((ps == null) || (ps.pkg == null)) {
4339 Log.w(TAG, "Package named '" + packageName +"' doesn't exist.");
4340 return false;
4341 }
4342 p = ps.pkg;
4343 }
4344 }
4345 String publicSrcDir = null;
4346 if(!dataOnly) {
4347 final ApplicationInfo applicationInfo = p.applicationInfo;
4348 if (applicationInfo == null) {
4349 Log.w(TAG, "Package " + packageName + " has no applicationInfo.");
4350 return false;
4351 }
4352 publicSrcDir = isForwardLocked(p) ? applicationInfo.publicSourceDir : null;
4353 }
4354 if (mInstaller != null) {
4355 int res = mInstaller.getSizeInfo(packageName, p.mPath,
4356 publicSrcDir, pStats);
4357 if (res < 0) {
4358 return false;
4359 } else {
4360 return true;
4361 }
4362 }
4363 return true;
4364 }
4365
4366
4367 public void addPackageToPreferred(String packageName) {
4368 mContext.enforceCallingOrSelfPermission(
4369 android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
4370
4371 synchronized (mPackages) {
4372 PackageParser.Package p = mPackages.get(packageName);
4373 if (p == null) {
4374 return;
4375 }
4376 PackageSetting ps = (PackageSetting)p.mExtras;
4377 if (ps != null) {
4378 mSettings.mPreferredPackages.remove(ps);
4379 mSettings.mPreferredPackages.add(0, ps);
4380 updatePreferredIndicesLP();
4381 mSettings.writeLP();
4382 }
4383 }
4384 }
4385
4386 public void removePackageFromPreferred(String packageName) {
4387 mContext.enforceCallingOrSelfPermission(
4388 android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
4389
4390 synchronized (mPackages) {
4391 PackageParser.Package p = mPackages.get(packageName);
4392 if (p == null) {
4393 return;
4394 }
4395 if (p.mPreferredOrder > 0) {
4396 PackageSetting ps = (PackageSetting)p.mExtras;
4397 if (ps != null) {
4398 mSettings.mPreferredPackages.remove(ps);
4399 p.mPreferredOrder = 0;
4400 updatePreferredIndicesLP();
4401 mSettings.writeLP();
4402 }
4403 }
4404 }
4405 }
4406
4407 private void updatePreferredIndicesLP() {
4408 final ArrayList<PackageSetting> pkgs
4409 = mSettings.mPreferredPackages;
4410 final int N = pkgs.size();
4411 for (int i=0; i<N; i++) {
4412 pkgs.get(i).pkg.mPreferredOrder = N - i;
4413 }
4414 }
4415
4416 public List<PackageInfo> getPreferredPackages(int flags) {
4417 synchronized (mPackages) {
4418 final ArrayList<PackageInfo> res = new ArrayList<PackageInfo>();
4419 final ArrayList<PackageSetting> pref = mSettings.mPreferredPackages;
4420 final int N = pref.size();
4421 for (int i=0; i<N; i++) {
4422 res.add(generatePackageInfo(pref.get(i).pkg, flags));
4423 }
4424 return res;
4425 }
4426 }
4427
4428 public void addPreferredActivity(IntentFilter filter, int match,
4429 ComponentName[] set, ComponentName activity) {
4430 mContext.enforceCallingOrSelfPermission(
4431 android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
4432
4433 synchronized (mPackages) {
4434 Log.i(TAG, "Adding preferred activity " + activity + ":");
4435 filter.dump(new LogPrinter(Log.INFO, TAG), " ");
4436 mSettings.mPreferredActivities.addFilter(
4437 new PreferredActivity(filter, match, set, activity));
4438 mSettings.writeLP();
4439 }
4440 }
4441
4442 public void clearPackagePreferredActivities(String packageName) {
4443 mContext.enforceCallingOrSelfPermission(
4444 android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
4445
4446 synchronized (mPackages) {
4447 if (clearPackagePreferredActivitiesLP(packageName)) {
4448 mSettings.writeLP();
4449 }
4450 }
4451 }
4452
4453 boolean clearPackagePreferredActivitiesLP(String packageName) {
4454 boolean changed = false;
4455 Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator();
4456 while (it.hasNext()) {
4457 PreferredActivity pa = it.next();
4458 if (pa.mActivity.getPackageName().equals(packageName)) {
4459 it.remove();
4460 changed = true;
4461 }
4462 }
4463 return changed;
4464 }
4465
4466 public int getPreferredActivities(List<IntentFilter> outFilters,
4467 List<ComponentName> outActivities, String packageName) {
4468
4469 int num = 0;
4470 synchronized (mPackages) {
4471 Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator();
4472 while (it.hasNext()) {
4473 PreferredActivity pa = it.next();
4474 if (packageName == null
4475 || pa.mActivity.getPackageName().equals(packageName)) {
4476 if (outFilters != null) {
4477 outFilters.add(new IntentFilter(pa));
4478 }
4479 if (outActivities != null) {
4480 outActivities.add(pa.mActivity);
4481 }
4482 }
4483 }
4484 }
4485
4486 return num;
4487 }
4488
4489 public void setApplicationEnabledSetting(String appPackageName,
4490 int newState, int flags) {
4491 setEnabledSetting(appPackageName, null, newState, flags);
4492 }
4493
4494 public void setComponentEnabledSetting(ComponentName componentName,
4495 int newState, int flags) {
4496 setEnabledSetting(componentName.getPackageName(),
4497 componentName.getClassName(), newState, flags);
4498 }
4499
4500 private void setEnabledSetting(
4501 final String packageNameStr, String classNameStr, int newState, final int flags) {
4502 if (!(newState == COMPONENT_ENABLED_STATE_DEFAULT
4503 || newState == COMPONENT_ENABLED_STATE_ENABLED
4504 || newState == COMPONENT_ENABLED_STATE_DISABLED)) {
4505 throw new IllegalArgumentException("Invalid new component state: "
4506 + newState);
4507 }
4508 PackageSetting pkgSetting;
4509 final int uid = Binder.getCallingUid();
4510 final int permission = mContext.checkCallingPermission(
4511 android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE);
4512 final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED);
4513 int packageUid = -1;
4514 synchronized (mPackages) {
4515 pkgSetting = mSettings.mPackages.get(packageNameStr);
4516 if (pkgSetting == null) {
4517 if (classNameStr == null) {
4518 throw new IllegalArgumentException(
4519 "Unknown package: " + packageNameStr);
4520 }
4521 throw new IllegalArgumentException(
4522 "Unknown component: " + packageNameStr
4523 + "/" + classNameStr);
4524 }
4525 if (!allowedByPermission && (uid != pkgSetting.userId)) {
4526 throw new SecurityException(
4527 "Permission Denial: attempt to change component state from pid="
4528 + Binder.getCallingPid()
4529 + ", uid=" + uid + ", package uid=" + pkgSetting.userId);
4530 }
4531 packageUid = pkgSetting.userId;
4532 if (classNameStr == null) {
4533 // We're dealing with an application/package level state change
4534 pkgSetting.enabled = newState;
4535 } else {
4536 // We're dealing with a component level state change
4537 switch (newState) {
4538 case COMPONENT_ENABLED_STATE_ENABLED:
4539 pkgSetting.enableComponentLP(classNameStr);
4540 break;
4541 case COMPONENT_ENABLED_STATE_DISABLED:
4542 pkgSetting.disableComponentLP(classNameStr);
4543 break;
4544 case COMPONENT_ENABLED_STATE_DEFAULT:
4545 pkgSetting.restoreComponentLP(classNameStr);
4546 break;
4547 default:
4548 Log.e(TAG, "Invalid new component state: " + newState);
4549 }
4550 }
4551 mSettings.writeLP();
4552 }
4553
4554 long callingId = Binder.clearCallingIdentity();
4555 try {
4556 Bundle extras = new Bundle(2);
4557 extras.putBoolean(Intent.EXTRA_DONT_KILL_APP,
4558 (flags&PackageManager.DONT_KILL_APP) != 0);
4559 extras.putInt(Intent.EXTRA_UID, packageUid);
4560 sendPackageBroadcast(Intent.ACTION_PACKAGE_CHANGED, packageNameStr, extras);
4561 } finally {
4562 Binder.restoreCallingIdentity(callingId);
4563 }
4564 }
4565
Jacek Surazskic64322c2009-04-28 15:26:38 +02004566 public String getInstallerPackageName(String packageName) {
4567 synchronized (mPackages) {
4568 PackageSetting pkg = mSettings.mPackages.get(packageName);
4569 if (pkg == null) {
4570 throw new IllegalArgumentException("Unknown package: " + packageName);
4571 }
4572 return pkg.installerPackageName;
4573 }
4574 }
4575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 public int getApplicationEnabledSetting(String appPackageName) {
4577 synchronized (mPackages) {
4578 PackageSetting pkg = mSettings.mPackages.get(appPackageName);
4579 if (pkg == null) {
4580 throw new IllegalArgumentException("Unknown package: " + appPackageName);
4581 }
4582 return pkg.enabled;
4583 }
4584 }
4585
4586 public int getComponentEnabledSetting(ComponentName componentName) {
4587 synchronized (mPackages) {
4588 final String packageNameStr = componentName.getPackageName();
4589 PackageSetting pkg = mSettings.mPackages.get(packageNameStr);
4590 if (pkg == null) {
4591 throw new IllegalArgumentException("Unknown component: " + componentName);
4592 }
4593 final String classNameStr = componentName.getClassName();
4594 return pkg.currentEnabledStateLP(classNameStr);
4595 }
4596 }
4597
4598 public void enterSafeMode() {
4599 if (!mSystemReady) {
4600 mSafeMode = true;
4601 }
4602 }
4603
4604 public void systemReady() {
4605 mSystemReady = true;
4606 }
4607
4608 public boolean isSafeMode() {
4609 return mSafeMode;
4610 }
4611
4612 public boolean hasSystemUidErrors() {
4613 return mHasSystemUidErrors;
4614 }
4615
4616 static String arrayToString(int[] array) {
4617 StringBuffer buf = new StringBuffer(128);
4618 buf.append('[');
4619 if (array != null) {
4620 for (int i=0; i<array.length; i++) {
4621 if (i > 0) buf.append(", ");
4622 buf.append(array[i]);
4623 }
4624 }
4625 buf.append(']');
4626 return buf.toString();
4627 }
4628
4629 @Override
4630 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4631 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4632 != PackageManager.PERMISSION_GRANTED) {
4633 pw.println("Permission Denial: can't dump ActivityManager from from pid="
4634 + Binder.getCallingPid()
4635 + ", uid=" + Binder.getCallingUid()
4636 + " without permission "
4637 + android.Manifest.permission.DUMP);
4638 return;
4639 }
4640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 synchronized (mPackages) {
4642 pw.println("Activity Resolver Table:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004643 mActivities.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 pw.println(" ");
4645 pw.println("Receiver Resolver Table:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004646 mReceivers.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004647 pw.println(" ");
4648 pw.println("Service Resolver Table:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004649 mServices.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004650 pw.println(" ");
4651 pw.println("Preferred Activities:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004652 mSettings.mPreferredActivities.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 pw.println(" ");
4654 pw.println("Preferred Packages:");
4655 {
4656 for (PackageSetting ps : mSettings.mPreferredPackages) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004657 pw.print(" "); pw.println(ps.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 }
4659 }
4660 pw.println(" ");
4661 pw.println("Permissions:");
4662 {
4663 for (BasePermission p : mSettings.mPermissions.values()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004664 pw.print(" Permission ["); pw.print(p.name); pw.print("] (");
4665 pw.print(Integer.toHexString(System.identityHashCode(p)));
4666 pw.println("):");
4667 pw.print(" sourcePackage="); pw.println(p.sourcePackage);
4668 pw.print(" uid="); pw.print(p.uid);
4669 pw.print(" gids="); pw.print(arrayToString(p.gids));
4670 pw.print(" type="); pw.println(p.type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 }
4672 }
4673 pw.println(" ");
4674 pw.println("Packages:");
4675 {
4676 for (PackageSetting ps : mSettings.mPackages.values()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004677 pw.print(" Package ["); pw.print(ps.name); pw.print("] (");
4678 pw.print(Integer.toHexString(System.identityHashCode(ps)));
4679 pw.println("):");
4680 pw.print(" userId="); pw.print(ps.userId);
4681 pw.print(" gids="); pw.println(arrayToString(ps.gids));
4682 pw.print(" sharedUser="); pw.println(ps.sharedUser);
4683 pw.print(" pkg="); pw.println(ps.pkg);
4684 pw.print(" codePath="); pw.println(ps.codePathString);
4685 pw.print(" resourcePath="); pw.println(ps.resourcePathString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 if (ps.pkg != null) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004687 pw.print(" dataDir="); pw.println(ps.pkg.applicationInfo.dataDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004689 pw.print(" timeStamp="); pw.println(ps.getTimeStampStr());
4690 pw.print(" signatures="); pw.println(ps.signatures);
4691 pw.print(" permissionsFixed="); pw.print(ps.permissionsFixed);
4692 pw.print(" pkgFlags=0x"); pw.print(Integer.toHexString(ps.pkgFlags));
4693 pw.print(" installStatus="); pw.print(ps.installStatus);
4694 pw.print(" enabled="); pw.println(ps.enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 if (ps.disabledComponents.size() > 0) {
4696 pw.println(" disabledComponents:");
4697 for (String s : ps.disabledComponents) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004698 pw.print(" "); pw.println(s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 }
4700 }
4701 if (ps.enabledComponents.size() > 0) {
4702 pw.println(" enabledComponents:");
4703 for (String s : ps.enabledComponents) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004704 pw.print(" "); pw.println(s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 }
4706 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004707 if (ps.grantedPermissions.size() > 0) {
4708 pw.println(" grantedPermissions:");
4709 for (String s : ps.grantedPermissions) {
4710 pw.print(" "); pw.println(s);
4711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004712 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004713 if (ps.loadedPermissions.size() > 0) {
4714 pw.println(" loadedPermissions:");
4715 for (String s : ps.loadedPermissions) {
4716 pw.print(" "); pw.println(s);
4717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 }
4719 }
4720 }
4721 pw.println(" ");
4722 pw.println("Shared Users:");
4723 {
4724 for (SharedUserSetting su : mSettings.mSharedUsers.values()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004725 pw.print(" SharedUser ["); pw.print(su.name); pw.print("] (");
4726 pw.print(Integer.toHexString(System.identityHashCode(su)));
4727 pw.println("):");
4728 pw.print(" userId="); pw.print(su.userId);
4729 pw.print(" gids="); pw.println(arrayToString(su.gids));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730 pw.println(" grantedPermissions:");
4731 for (String s : su.grantedPermissions) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004732 pw.print(" "); pw.println(s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004733 }
4734 pw.println(" loadedPermissions:");
4735 for (String s : su.loadedPermissions) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004736 pw.print(" "); pw.println(s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 }
4738 }
4739 }
4740 pw.println(" ");
4741 pw.println("Settings parse messages:");
4742 pw.println(mSettings.mReadMessages.toString());
4743 }
4744 }
4745
4746 static final class BasePermission {
4747 final static int TYPE_NORMAL = 0;
4748 final static int TYPE_BUILTIN = 1;
4749 final static int TYPE_DYNAMIC = 2;
4750
4751 final String name;
4752 final String sourcePackage;
4753 final int type;
4754 PackageParser.Permission perm;
4755 PermissionInfo pendingInfo;
4756 int uid;
4757 int[] gids;
4758
4759 BasePermission(String _name, String _sourcePackage, int _type) {
4760 name = _name;
4761 sourcePackage = _sourcePackage;
4762 type = _type;
4763 }
4764 }
4765
4766 static class PackageSignatures {
4767 private Signature[] mSignatures;
4768
4769 PackageSignatures(Signature[] sigs) {
4770 assignSignatures(sigs);
4771 }
4772
4773 PackageSignatures() {
4774 }
4775
4776 void writeXml(XmlSerializer serializer, String tagName,
4777 ArrayList<Signature> pastSignatures) throws IOException {
4778 if (mSignatures == null) {
4779 return;
4780 }
4781 serializer.startTag(null, tagName);
4782 serializer.attribute(null, "count",
4783 Integer.toString(mSignatures.length));
4784 for (int i=0; i<mSignatures.length; i++) {
4785 serializer.startTag(null, "cert");
4786 final Signature sig = mSignatures[i];
4787 final int sigHash = sig.hashCode();
4788 final int numPast = pastSignatures.size();
4789 int j;
4790 for (j=0; j<numPast; j++) {
4791 Signature pastSig = pastSignatures.get(j);
4792 if (pastSig.hashCode() == sigHash && pastSig.equals(sig)) {
4793 serializer.attribute(null, "index", Integer.toString(j));
4794 break;
4795 }
4796 }
4797 if (j >= numPast) {
4798 pastSignatures.add(sig);
4799 serializer.attribute(null, "index", Integer.toString(numPast));
4800 serializer.attribute(null, "key", sig.toCharsString());
4801 }
4802 serializer.endTag(null, "cert");
4803 }
4804 serializer.endTag(null, tagName);
4805 }
4806
4807 void readXml(XmlPullParser parser, ArrayList<Signature> pastSignatures)
4808 throws IOException, XmlPullParserException {
4809 String countStr = parser.getAttributeValue(null, "count");
4810 if (countStr == null) {
4811 reportSettingsProblem(Log.WARN,
4812 "Error in package manager settings: <signatures> has"
4813 + " no count at " + parser.getPositionDescription());
4814 XmlUtils.skipCurrentTag(parser);
4815 }
4816 final int count = Integer.parseInt(countStr);
4817 mSignatures = new Signature[count];
4818 int pos = 0;
4819
4820 int outerDepth = parser.getDepth();
4821 int type;
4822 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
4823 && (type != XmlPullParser.END_TAG
4824 || parser.getDepth() > outerDepth)) {
4825 if (type == XmlPullParser.END_TAG
4826 || type == XmlPullParser.TEXT) {
4827 continue;
4828 }
4829
4830 String tagName = parser.getName();
4831 if (tagName.equals("cert")) {
4832 if (pos < count) {
4833 String index = parser.getAttributeValue(null, "index");
4834 if (index != null) {
4835 try {
4836 int idx = Integer.parseInt(index);
4837 String key = parser.getAttributeValue(null, "key");
4838 if (key == null) {
4839 if (idx >= 0 && idx < pastSignatures.size()) {
4840 Signature sig = pastSignatures.get(idx);
4841 if (sig != null) {
4842 mSignatures[pos] = pastSignatures.get(idx);
4843 pos++;
4844 } else {
4845 reportSettingsProblem(Log.WARN,
4846 "Error in package manager settings: <cert> "
4847 + "index " + index + " is not defined at "
4848 + parser.getPositionDescription());
4849 }
4850 } else {
4851 reportSettingsProblem(Log.WARN,
4852 "Error in package manager settings: <cert> "
4853 + "index " + index + " is out of bounds at "
4854 + parser.getPositionDescription());
4855 }
4856 } else {
4857 while (pastSignatures.size() <= idx) {
4858 pastSignatures.add(null);
4859 }
4860 Signature sig = new Signature(key);
4861 pastSignatures.set(idx, sig);
4862 mSignatures[pos] = sig;
4863 pos++;
4864 }
4865 } catch (NumberFormatException e) {
4866 reportSettingsProblem(Log.WARN,
4867 "Error in package manager settings: <cert> "
4868 + "index " + index + " is not a number at "
4869 + parser.getPositionDescription());
4870 }
4871 } else {
4872 reportSettingsProblem(Log.WARN,
4873 "Error in package manager settings: <cert> has"
4874 + " no index at " + parser.getPositionDescription());
4875 }
4876 } else {
4877 reportSettingsProblem(Log.WARN,
4878 "Error in package manager settings: too "
4879 + "many <cert> tags, expected " + count
4880 + " at " + parser.getPositionDescription());
4881 }
4882 } else {
4883 reportSettingsProblem(Log.WARN,
4884 "Unknown element under <cert>: "
4885 + parser.getName());
4886 }
4887 XmlUtils.skipCurrentTag(parser);
4888 }
4889
4890 if (pos < count) {
4891 // Should never happen -- there is an error in the written
4892 // settings -- but if it does we don't want to generate
4893 // a bad array.
4894 Signature[] newSigs = new Signature[pos];
4895 System.arraycopy(mSignatures, 0, newSigs, 0, pos);
4896 mSignatures = newSigs;
4897 }
4898 }
4899
4900 /**
4901 * If any of the given 'sigs' is contained in the existing signatures,
4902 * then completely replace the current signatures with the ones in
4903 * 'sigs'. This is used for updating an existing package to a newly
4904 * installed version.
4905 */
4906 boolean updateSignatures(Signature[] sigs, boolean update) {
4907 if (mSignatures == null) {
4908 if (update) {
4909 assignSignatures(sigs);
4910 }
4911 return true;
4912 }
4913 if (sigs == null) {
4914 return false;
4915 }
4916
4917 for (int i=0; i<sigs.length; i++) {
4918 Signature sig = sigs[i];
4919 for (int j=0; j<mSignatures.length; j++) {
4920 if (mSignatures[j].equals(sig)) {
4921 if (update) {
4922 assignSignatures(sigs);
4923 }
4924 return true;
4925 }
4926 }
4927 }
4928 return false;
4929 }
4930
4931 /**
4932 * If any of the given 'sigs' is contained in the existing signatures,
4933 * then add in any new signatures found in 'sigs'. This is used for
4934 * including a new package into an existing shared user id.
4935 */
4936 boolean mergeSignatures(Signature[] sigs, boolean update) {
4937 if (mSignatures == null) {
4938 if (update) {
4939 assignSignatures(sigs);
4940 }
4941 return true;
4942 }
4943 if (sigs == null) {
4944 return false;
4945 }
4946
4947 Signature[] added = null;
4948 int addedCount = 0;
4949 boolean haveMatch = false;
4950 for (int i=0; i<sigs.length; i++) {
4951 Signature sig = sigs[i];
4952 boolean found = false;
4953 for (int j=0; j<mSignatures.length; j++) {
4954 if (mSignatures[j].equals(sig)) {
4955 found = true;
4956 haveMatch = true;
4957 break;
4958 }
4959 }
4960
4961 if (!found) {
4962 if (added == null) {
4963 added = new Signature[sigs.length];
4964 }
4965 added[i] = sig;
4966 addedCount++;
4967 }
4968 }
4969
4970 if (!haveMatch) {
4971 // Nothing matched -- reject the new signatures.
4972 return false;
4973 }
4974 if (added == null) {
4975 // Completely matched -- nothing else to do.
4976 return true;
4977 }
4978
4979 // Add additional signatures in.
4980 if (update) {
4981 Signature[] total = new Signature[addedCount+mSignatures.length];
4982 System.arraycopy(mSignatures, 0, total, 0, mSignatures.length);
4983 int j = mSignatures.length;
4984 for (int i=0; i<added.length; i++) {
4985 if (added[i] != null) {
4986 total[j] = added[i];
4987 j++;
4988 }
4989 }
4990 mSignatures = total;
4991 }
4992 return true;
4993 }
4994
4995 private void assignSignatures(Signature[] sigs) {
4996 if (sigs == null) {
4997 mSignatures = null;
4998 return;
4999 }
5000 mSignatures = new Signature[sigs.length];
5001 for (int i=0; i<sigs.length; i++) {
5002 mSignatures[i] = sigs[i];
5003 }
5004 }
5005
5006 @Override
5007 public String toString() {
5008 StringBuffer buf = new StringBuffer(128);
5009 buf.append("PackageSignatures{");
5010 buf.append(Integer.toHexString(System.identityHashCode(this)));
5011 buf.append(" [");
5012 if (mSignatures != null) {
5013 for (int i=0; i<mSignatures.length; i++) {
5014 if (i > 0) buf.append(", ");
5015 buf.append(Integer.toHexString(
5016 System.identityHashCode(mSignatures[i])));
5017 }
5018 }
5019 buf.append("]}");
5020 return buf.toString();
5021 }
5022 }
5023
5024 static class PreferredActivity extends IntentFilter {
5025 final int mMatch;
5026 final String[] mSetPackages;
5027 final String[] mSetClasses;
5028 final String[] mSetComponents;
5029 final ComponentName mActivity;
5030 final String mShortActivity;
5031 String mParseError;
5032
5033 PreferredActivity(IntentFilter filter, int match, ComponentName[] set,
5034 ComponentName activity) {
5035 super(filter);
5036 mMatch = match&IntentFilter.MATCH_CATEGORY_MASK;
5037 mActivity = activity;
5038 mShortActivity = activity.flattenToShortString();
5039 mParseError = null;
5040 if (set != null) {
5041 final int N = set.length;
5042 String[] myPackages = new String[N];
5043 String[] myClasses = new String[N];
5044 String[] myComponents = new String[N];
5045 for (int i=0; i<N; i++) {
5046 ComponentName cn = set[i];
5047 if (cn == null) {
5048 mSetPackages = null;
5049 mSetClasses = null;
5050 mSetComponents = null;
5051 return;
5052 }
5053 myPackages[i] = cn.getPackageName().intern();
5054 myClasses[i] = cn.getClassName().intern();
5055 myComponents[i] = cn.flattenToShortString().intern();
5056 }
5057 mSetPackages = myPackages;
5058 mSetClasses = myClasses;
5059 mSetComponents = myComponents;
5060 } else {
5061 mSetPackages = null;
5062 mSetClasses = null;
5063 mSetComponents = null;
5064 }
5065 }
5066
5067 PreferredActivity(XmlPullParser parser) throws XmlPullParserException,
5068 IOException {
5069 mShortActivity = parser.getAttributeValue(null, "name");
5070 mActivity = ComponentName.unflattenFromString(mShortActivity);
5071 if (mActivity == null) {
5072 mParseError = "Bad activity name " + mShortActivity;
5073 }
5074 String matchStr = parser.getAttributeValue(null, "match");
5075 mMatch = matchStr != null ? Integer.parseInt(matchStr, 16) : 0;
5076 String setCountStr = parser.getAttributeValue(null, "set");
5077 int setCount = setCountStr != null ? Integer.parseInt(setCountStr) : 0;
5078
5079 String[] myPackages = setCount > 0 ? new String[setCount] : null;
5080 String[] myClasses = setCount > 0 ? new String[setCount] : null;
5081 String[] myComponents = setCount > 0 ? new String[setCount] : null;
5082
5083 int setPos = 0;
5084
5085 int outerDepth = parser.getDepth();
5086 int type;
5087 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
5088 && (type != XmlPullParser.END_TAG
5089 || parser.getDepth() > outerDepth)) {
5090 if (type == XmlPullParser.END_TAG
5091 || type == XmlPullParser.TEXT) {
5092 continue;
5093 }
5094
5095 String tagName = parser.getName();
5096 //Log.i(TAG, "Parse outerDepth=" + outerDepth + " depth="
5097 // + parser.getDepth() + " tag=" + tagName);
5098 if (tagName.equals("set")) {
5099 String name = parser.getAttributeValue(null, "name");
5100 if (name == null) {
5101 if (mParseError == null) {
5102 mParseError = "No name in set tag in preferred activity "
5103 + mShortActivity;
5104 }
5105 } else if (setPos >= setCount) {
5106 if (mParseError == null) {
5107 mParseError = "Too many set tags in preferred activity "
5108 + mShortActivity;
5109 }
5110 } else {
5111 ComponentName cn = ComponentName.unflattenFromString(name);
5112 if (cn == null) {
5113 if (mParseError == null) {
5114 mParseError = "Bad set name " + name + " in preferred activity "
5115 + mShortActivity;
5116 }
5117 } else {
5118 myPackages[setPos] = cn.getPackageName();
5119 myClasses[setPos] = cn.getClassName();
5120 myComponents[setPos] = name;
5121 setPos++;
5122 }
5123 }
5124 XmlUtils.skipCurrentTag(parser);
5125 } else if (tagName.equals("filter")) {
5126 //Log.i(TAG, "Starting to parse filter...");
5127 readFromXml(parser);
5128 //Log.i(TAG, "Finished filter: outerDepth=" + outerDepth + " depth="
5129 // + parser.getDepth() + " tag=" + parser.getName());
5130 } else {
5131 reportSettingsProblem(Log.WARN,
5132 "Unknown element under <preferred-activities>: "
5133 + parser.getName());
5134 XmlUtils.skipCurrentTag(parser);
5135 }
5136 }
5137
5138 if (setPos != setCount) {
5139 if (mParseError == null) {
5140 mParseError = "Not enough set tags (expected " + setCount
5141 + " but found " + setPos + ") in " + mShortActivity;
5142 }
5143 }
5144
5145 mSetPackages = myPackages;
5146 mSetClasses = myClasses;
5147 mSetComponents = myComponents;
5148 }
5149
5150 public void writeToXml(XmlSerializer serializer) throws IOException {
5151 final int NS = mSetClasses != null ? mSetClasses.length : 0;
5152 serializer.attribute(null, "name", mShortActivity);
5153 serializer.attribute(null, "match", Integer.toHexString(mMatch));
5154 serializer.attribute(null, "set", Integer.toString(NS));
5155 for (int s=0; s<NS; s++) {
5156 serializer.startTag(null, "set");
5157 serializer.attribute(null, "name", mSetComponents[s]);
5158 serializer.endTag(null, "set");
5159 }
5160 serializer.startTag(null, "filter");
5161 super.writeToXml(serializer);
5162 serializer.endTag(null, "filter");
5163 }
5164
5165 boolean sameSet(List<ResolveInfo> query, int priority) {
5166 if (mSetPackages == null) return false;
5167 final int NQ = query.size();
5168 final int NS = mSetPackages.length;
5169 int numMatch = 0;
5170 for (int i=0; i<NQ; i++) {
5171 ResolveInfo ri = query.get(i);
5172 if (ri.priority != priority) continue;
5173 ActivityInfo ai = ri.activityInfo;
5174 boolean good = false;
5175 for (int j=0; j<NS; j++) {
5176 if (mSetPackages[j].equals(ai.packageName)
5177 && mSetClasses[j].equals(ai.name)) {
5178 numMatch++;
5179 good = true;
5180 break;
5181 }
5182 }
5183 if (!good) return false;
5184 }
5185 return numMatch == NS;
5186 }
5187 }
5188
5189 static class GrantedPermissions {
5190 final int pkgFlags;
5191
5192 HashSet<String> grantedPermissions = new HashSet<String>();
5193 int[] gids;
5194
5195 HashSet<String> loadedPermissions = new HashSet<String>();
5196
5197 GrantedPermissions(int pkgFlags) {
5198 this.pkgFlags = pkgFlags & ApplicationInfo.FLAG_SYSTEM;
5199 }
5200 }
5201
5202 /**
5203 * Settings base class for pending and resolved classes.
5204 */
5205 static class PackageSettingBase extends GrantedPermissions {
5206 final String name;
5207 final File codePath;
5208 final String codePathString;
5209 final File resourcePath;
5210 final String resourcePathString;
5211 private long timeStamp;
5212 private String timeStampString = "0";
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005213 final int versionCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214
5215 PackageSignatures signatures = new PackageSignatures();
5216
5217 boolean permissionsFixed;
5218
5219 /* Explicitly disabled components */
5220 HashSet<String> disabledComponents = new HashSet<String>(0);
5221 /* Explicitly enabled components */
5222 HashSet<String> enabledComponents = new HashSet<String>(0);
5223 int enabled = COMPONENT_ENABLED_STATE_DEFAULT;
5224 int installStatus = PKG_INSTALL_COMPLETE;
Jacek Surazskic64322c2009-04-28 15:26:38 +02005225
5226 /* package name of the app that installed this package */
5227 String installerPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228
5229 PackageSettingBase(String name, File codePath, File resourcePath,
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005230 int pVersionCode, int pkgFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 super(pkgFlags);
5232 this.name = name;
5233 this.codePath = codePath;
5234 this.codePathString = codePath.toString();
5235 this.resourcePath = resourcePath;
5236 this.resourcePathString = resourcePath.toString();
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005237 this.versionCode = pVersionCode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 }
5239
Jacek Surazskic64322c2009-04-28 15:26:38 +02005240 public void setInstallerPackageName(String packageName) {
5241 installerPackageName = packageName;
5242 }
5243
5244 String getInstallerPackageName() {
5245 return installerPackageName;
5246 }
5247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005248 public void setInstallStatus(int newStatus) {
5249 installStatus = newStatus;
5250 }
5251
5252 public int getInstallStatus() {
5253 return installStatus;
5254 }
5255
5256 public void setTimeStamp(long newStamp) {
5257 if (newStamp != timeStamp) {
5258 timeStamp = newStamp;
5259 timeStampString = Long.toString(newStamp);
5260 }
5261 }
5262
5263 public void setTimeStamp(long newStamp, String newStampStr) {
5264 timeStamp = newStamp;
5265 timeStampString = newStampStr;
5266 }
5267
5268 public long getTimeStamp() {
5269 return timeStamp;
5270 }
5271
5272 public String getTimeStampStr() {
5273 return timeStampString;
5274 }
5275
5276 public void copyFrom(PackageSettingBase base) {
5277 grantedPermissions = base.grantedPermissions;
5278 gids = base.gids;
5279 loadedPermissions = base.loadedPermissions;
5280
5281 timeStamp = base.timeStamp;
5282 timeStampString = base.timeStampString;
5283 signatures = base.signatures;
5284 permissionsFixed = base.permissionsFixed;
5285 disabledComponents = base.disabledComponents;
5286 enabledComponents = base.enabledComponents;
5287 enabled = base.enabled;
5288 installStatus = base.installStatus;
5289 }
5290
5291 void enableComponentLP(String componentClassName) {
5292 disabledComponents.remove(componentClassName);
5293 enabledComponents.add(componentClassName);
5294 }
5295
5296 void disableComponentLP(String componentClassName) {
5297 enabledComponents.remove(componentClassName);
5298 disabledComponents.add(componentClassName);
5299 }
5300
5301 void restoreComponentLP(String componentClassName) {
5302 enabledComponents.remove(componentClassName);
5303 disabledComponents.remove(componentClassName);
5304 }
5305
5306 int currentEnabledStateLP(String componentName) {
5307 if (enabledComponents.contains(componentName)) {
5308 return COMPONENT_ENABLED_STATE_ENABLED;
5309 } else if (disabledComponents.contains(componentName)) {
5310 return COMPONENT_ENABLED_STATE_DISABLED;
5311 } else {
5312 return COMPONENT_ENABLED_STATE_DEFAULT;
5313 }
5314 }
5315 }
5316
5317 /**
5318 * Settings data for a particular package we know about.
5319 */
5320 static final class PackageSetting extends PackageSettingBase {
5321 int userId;
5322 PackageParser.Package pkg;
5323 SharedUserSetting sharedUser;
5324
5325 PackageSetting(String name, File codePath, File resourcePath,
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005326 int pVersionCode, int pkgFlags) {
5327 super(name, codePath, resourcePath, pVersionCode, pkgFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328 }
5329
5330 @Override
5331 public String toString() {
5332 return "PackageSetting{"
5333 + Integer.toHexString(System.identityHashCode(this))
5334 + " " + name + "/" + userId + "}";
5335 }
5336 }
5337
5338 /**
5339 * Settings data for a particular shared user ID we know about.
5340 */
5341 static final class SharedUserSetting extends GrantedPermissions {
5342 final String name;
5343 int userId;
5344 final HashSet<PackageSetting> packages = new HashSet<PackageSetting>();
5345 final PackageSignatures signatures = new PackageSignatures();
5346
5347 SharedUserSetting(String _name, int _pkgFlags) {
5348 super(_pkgFlags);
5349 name = _name;
5350 }
5351
5352 @Override
5353 public String toString() {
5354 return "SharedUserSetting{"
5355 + Integer.toHexString(System.identityHashCode(this))
5356 + " " + name + "/" + userId + "}";
5357 }
5358 }
5359
5360 /**
5361 * Holds information about dynamic settings.
5362 */
5363 private static final class Settings {
5364 private final File mSettingsFilename;
5365 private final File mBackupSettingsFilename;
5366 private final HashMap<String, PackageSetting> mPackages =
5367 new HashMap<String, PackageSetting>();
5368 // The user's preferred packages/applications, in order of preference.
5369 // First is the most preferred.
5370 private final ArrayList<PackageSetting> mPreferredPackages =
5371 new ArrayList<PackageSetting>();
5372 // List of replaced system applications
5373 final HashMap<String, PackageSetting> mDisabledSysPackages =
5374 new HashMap<String, PackageSetting>();
5375
5376 // The user's preferred activities associated with particular intent
5377 // filters.
5378 private final IntentResolver<PreferredActivity, PreferredActivity> mPreferredActivities =
5379 new IntentResolver<PreferredActivity, PreferredActivity>() {
5380 @Override
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005381 protected void dumpFilter(PrintWriter out, String prefix,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 PreferredActivity filter) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005383 out.print(prefix); out.print(
5384 Integer.toHexString(System.identityHashCode(filter)));
5385 out.print(' ');
5386 out.print(filter.mActivity.flattenToShortString());
5387 out.print(" match=0x");
5388 out.println( Integer.toHexString(filter.mMatch));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 if (filter.mSetComponents != null) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005390 out.print(prefix); out.println(" Selected from:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 for (int i=0; i<filter.mSetComponents.length; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005392 out.print(prefix); out.print(" ");
5393 out.println(filter.mSetComponents[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 }
5395 }
5396 }
5397 };
5398 private final HashMap<String, SharedUserSetting> mSharedUsers =
5399 new HashMap<String, SharedUserSetting>();
5400 private final ArrayList<Object> mUserIds = new ArrayList<Object>();
5401 private final SparseArray<Object> mOtherUserIds =
5402 new SparseArray<Object>();
5403
5404 // For reading/writing settings file.
5405 private final ArrayList<Signature> mPastSignatures =
5406 new ArrayList<Signature>();
5407
5408 // Mapping from permission names to info about them.
5409 final HashMap<String, BasePermission> mPermissions =
5410 new HashMap<String, BasePermission>();
5411
5412 // Mapping from permission tree names to info about them.
5413 final HashMap<String, BasePermission> mPermissionTrees =
5414 new HashMap<String, BasePermission>();
5415
5416 private final ArrayList<String> mPendingPreferredPackages
5417 = new ArrayList<String>();
5418
5419 private final StringBuilder mReadMessages = new StringBuilder();
5420
5421 private static final class PendingPackage extends PackageSettingBase {
5422 final int sharedId;
5423
5424 PendingPackage(String name, File codePath, File resourcePath,
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005425 int sharedId, int pVersionCode, int pkgFlags) {
5426 super(name, codePath, resourcePath, pVersionCode, pkgFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 this.sharedId = sharedId;
5428 }
5429 }
5430 private final ArrayList<PendingPackage> mPendingPackages
5431 = new ArrayList<PendingPackage>();
5432
5433 Settings() {
5434 File dataDir = Environment.getDataDirectory();
5435 File systemDir = new File(dataDir, "system");
5436 systemDir.mkdirs();
5437 FileUtils.setPermissions(systemDir.toString(),
5438 FileUtils.S_IRWXU|FileUtils.S_IRWXG
5439 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
5440 -1, -1);
5441 mSettingsFilename = new File(systemDir, "packages.xml");
5442 mBackupSettingsFilename = new File(systemDir, "packages-backup.xml");
5443 }
5444
5445 PackageSetting getPackageLP(PackageParser.Package pkg,
5446 SharedUserSetting sharedUser, File codePath, File resourcePath,
5447 int pkgFlags, boolean create, boolean add) {
5448 final String name = pkg.packageName;
5449 PackageSetting p = getPackageLP(name, sharedUser, codePath,
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005450 resourcePath, pkg.mVersionCode, pkgFlags, create, add);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451
5452 if (p != null) {
5453 p.pkg = pkg;
5454 }
5455 return p;
5456 }
5457
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005458 PackageSetting peekPackageLP(String name) {
5459 return mPackages.get(name);
5460 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005461 PackageSetting p = mPackages.get(name);
5462 if (p != null && p.codePath.getPath().equals(codePath)) {
5463 return p;
5464 }
5465 return null;
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005466 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 }
5468
5469 void setInstallStatus(String pkgName, int status) {
5470 PackageSetting p = mPackages.get(pkgName);
5471 if(p != null) {
5472 if(p.getInstallStatus() != status) {
5473 p.setInstallStatus(status);
5474 }
5475 }
5476 }
5477
Jacek Surazskic64322c2009-04-28 15:26:38 +02005478 void setInstallerPackageName(String pkgName,
5479 String installerPkgName) {
5480 PackageSetting p = mPackages.get(pkgName);
5481 if(p != null) {
5482 p.setInstallerPackageName(installerPkgName);
5483 }
5484 }
5485
5486 String getInstallerPackageName(String pkgName) {
5487 PackageSetting p = mPackages.get(pkgName);
5488 return (p == null) ? null : p.getInstallerPackageName();
5489 }
5490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 int getInstallStatus(String pkgName) {
5492 PackageSetting p = mPackages.get(pkgName);
5493 if(p != null) {
5494 return p.getInstallStatus();
5495 }
5496 return -1;
5497 }
5498
5499 SharedUserSetting getSharedUserLP(String name,
5500 int pkgFlags, boolean create) {
5501 SharedUserSetting s = mSharedUsers.get(name);
5502 if (s == null) {
5503 if (!create) {
5504 return null;
5505 }
5506 s = new SharedUserSetting(name, pkgFlags);
5507 if (MULTIPLE_APPLICATION_UIDS) {
5508 s.userId = newUserIdLP(s);
5509 } else {
5510 s.userId = FIRST_APPLICATION_UID;
5511 }
5512 Log.i(TAG, "New shared user " + name + ": id=" + s.userId);
5513 // < 0 means we couldn't assign a userid; fall out and return
5514 // s, which is currently null
5515 if (s.userId >= 0) {
5516 mSharedUsers.put(name, s);
5517 }
5518 }
5519
5520 return s;
5521 }
5522
5523 int disableSystemPackageLP(String name) {
5524 PackageSetting p = mPackages.get(name);
5525 if(p == null) {
5526 Log.w(TAG, "Package:"+name+" is not an installed package");
5527 return -1;
5528 }
5529 PackageSetting dp = mDisabledSysPackages.get(name);
5530 // always make sure the system package code and resource paths dont change
5531 if(dp == null) {
5532 if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
5533 p.pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
5534 }
5535 mDisabledSysPackages.put(name, p);
5536 }
5537 return removePackageLP(name);
5538 }
5539
5540 PackageSetting enableSystemPackageLP(String name) {
5541 PackageSetting p = mDisabledSysPackages.get(name);
5542 if(p == null) {
5543 Log.w(TAG, "Package:"+name+" is not disabled");
5544 return null;
5545 }
5546 // Reset flag in ApplicationInfo object
5547 if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
5548 p.pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
5549 }
5550 PackageSetting ret = addPackageLP(name, p.codePath,
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005551 p.resourcePath, p.userId, p.versionCode, p.pkgFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 mDisabledSysPackages.remove(name);
5553 return ret;
5554 }
5555
5556 PackageSetting addPackageLP(String name, File codePath,
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005557 File resourcePath, int uid, int vc, int pkgFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 PackageSetting p = mPackages.get(name);
5559 if (p != null) {
5560 if (p.userId == uid) {
5561 return p;
5562 }
5563 reportSettingsProblem(Log.ERROR,
5564 "Adding duplicate package, keeping first: " + name);
5565 return null;
5566 }
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005567 p = new PackageSetting(name, codePath, resourcePath, vc, pkgFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 p.userId = uid;
5569 if (addUserIdLP(uid, p, name)) {
5570 mPackages.put(name, p);
5571 return p;
5572 }
5573 return null;
5574 }
5575
5576 SharedUserSetting addSharedUserLP(String name, int uid, int pkgFlags) {
5577 SharedUserSetting s = mSharedUsers.get(name);
5578 if (s != null) {
5579 if (s.userId == uid) {
5580 return s;
5581 }
5582 reportSettingsProblem(Log.ERROR,
5583 "Adding duplicate shared user, keeping first: " + name);
5584 return null;
5585 }
5586 s = new SharedUserSetting(name, pkgFlags);
5587 s.userId = uid;
5588 if (addUserIdLP(uid, s, name)) {
5589 mSharedUsers.put(name, s);
5590 return s;
5591 }
5592 return null;
5593 }
5594
5595 private PackageSetting getPackageLP(String name,
5596 SharedUserSetting sharedUser, File codePath, File resourcePath,
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005597 int vc, int pkgFlags, boolean create, boolean add) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 PackageSetting p = mPackages.get(name);
5599 if (p != null) {
5600 if (!p.codePath.equals(codePath)) {
5601 // Check to see if its a disabled system app
5602 PackageSetting ps = mDisabledSysPackages.get(name);
5603 if((ps != null) && ((ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0)) {
5604 // Could be a replaced system package
5605 // Note that if the user replaced a system app, the user has to physically
5606 // delete the new one in order to revert to the system app. So even
5607 // if the user updated the system app via an update, the user still
5608 // has to delete the one installed in the data partition in order to pick up the
5609 // new system package.
5610 return p;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07005611 } else if ((p.pkg != null) && (p.pkg.applicationInfo != null) &&
5612 ((p.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)) {
5613 // Check for non-system apps
5614 reportSettingsProblem(Log.WARN,
5615 "Package " + name + " codePath changed from " + p.codePath
5616 + " to " + codePath + "; Retaining data and using new code");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 } else {
5618 reportSettingsProblem(Log.WARN,
5619 "Package " + name + " codePath changed from " + p.codePath
5620 + " to " + codePath + "; replacing with new");
5621 p = null;
5622 }
5623 } else if (p.sharedUser != sharedUser) {
5624 reportSettingsProblem(Log.WARN,
5625 "Package " + name + " shared user changed from "
5626 + (p.sharedUser != null ? p.sharedUser.name : "<nothing>")
5627 + " to "
5628 + (sharedUser != null ? sharedUser.name : "<nothing>")
5629 + "; replacing with new");
5630 p = null;
5631 }
5632 }
5633 if (p == null) {
5634 // Create a new PackageSettings entry. this can end up here because
5635 // of code path mismatch or user id mismatch of an updated system partition
5636 if (!create) {
5637 return null;
5638 }
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005639 p = new PackageSetting(name, codePath, resourcePath, vc, pkgFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 p.setTimeStamp(codePath.lastModified());
5641 if (sharedUser != null) {
5642 p.userId = sharedUser.userId;
5643 } else if (MULTIPLE_APPLICATION_UIDS) {
5644 p.userId = newUserIdLP(p);
5645 } else {
5646 p.userId = FIRST_APPLICATION_UID;
5647 }
5648 if (p.userId < 0) {
5649 reportSettingsProblem(Log.WARN,
5650 "Package " + name + " could not be assigned a valid uid");
5651 return null;
5652 }
5653 if (add) {
5654 // Finish adding new package by adding it and updating shared
5655 // user preferences
5656 insertPackageSettingLP(p, name, sharedUser);
5657 }
5658 }
5659 return p;
5660 }
5661
5662 // Utility method that adds a PackageSetting to mPackages and
5663 // completes updating the shared user attributes
5664 private void insertPackageSettingLP(PackageSetting p, String name,
5665 SharedUserSetting sharedUser) {
5666 mPackages.put(name, p);
5667 if (sharedUser != null) {
5668 if (p.sharedUser != null && p.sharedUser != sharedUser) {
5669 reportSettingsProblem(Log.ERROR,
5670 "Package " + p.name + " was user "
5671 + p.sharedUser + " but is now " + sharedUser
5672 + "; I am not changing its files so it will probably fail!");
5673 p.sharedUser.packages.remove(p);
5674 } else if (p.userId != sharedUser.userId) {
5675 reportSettingsProblem(Log.ERROR,
5676 "Package " + p.name + " was user id " + p.userId
5677 + " but is now user " + sharedUser
5678 + " with id " + sharedUser.userId
5679 + "; I am not changing its files so it will probably fail!");
5680 }
5681
5682 sharedUser.packages.add(p);
5683 p.sharedUser = sharedUser;
5684 p.userId = sharedUser.userId;
5685 }
5686 }
5687
5688 private void updateSharedUserPerms (PackageSetting deletedPs) {
5689 if ( (deletedPs == null) || (deletedPs.pkg == null)) {
5690 Log.i(TAG, "Trying to update info for null package. Just ignoring");
5691 return;
5692 }
5693 // No sharedUserId
5694 if (deletedPs.sharedUser == null) {
5695 return;
5696 }
5697 SharedUserSetting sus = deletedPs.sharedUser;
5698 // Update permissions
5699 for (String eachPerm: deletedPs.pkg.requestedPermissions) {
5700 boolean used = false;
5701 if (!sus.grantedPermissions.contains (eachPerm)) {
5702 continue;
5703 }
5704 for (PackageSetting pkg:sus.packages) {
5705 if (pkg.grantedPermissions.contains (eachPerm)) {
5706 used = true;
5707 break;
5708 }
5709 }
5710 if (!used) {
5711 // can safely delete this permission from list
5712 sus.grantedPermissions.remove(eachPerm);
5713 sus.loadedPermissions.remove(eachPerm);
5714 }
5715 }
5716 // Update gids
5717 int newGids[] = null;
5718 for (PackageSetting pkg:sus.packages) {
5719 newGids = appendInts(newGids, pkg.gids);
5720 }
5721 sus.gids = newGids;
5722 }
5723
5724 private int removePackageLP(String name) {
5725 PackageSetting p = mPackages.get(name);
5726 if (p != null) {
5727 mPackages.remove(name);
5728 if (p.sharedUser != null) {
5729 p.sharedUser.packages.remove(p);
5730 if (p.sharedUser.packages.size() == 0) {
5731 mSharedUsers.remove(p.sharedUser.name);
5732 removeUserIdLP(p.sharedUser.userId);
5733 return p.sharedUser.userId;
5734 }
5735 } else {
5736 removeUserIdLP(p.userId);
5737 return p.userId;
5738 }
5739 }
5740 return -1;
5741 }
5742
5743 private boolean addUserIdLP(int uid, Object obj, Object name) {
5744 if (uid >= FIRST_APPLICATION_UID + MAX_APPLICATION_UIDS) {
5745 return false;
5746 }
5747
5748 if (uid >= FIRST_APPLICATION_UID) {
5749 int N = mUserIds.size();
5750 final int index = uid - FIRST_APPLICATION_UID;
5751 while (index >= N) {
5752 mUserIds.add(null);
5753 N++;
5754 }
5755 if (mUserIds.get(index) != null) {
5756 reportSettingsProblem(Log.ERROR,
5757 "Adding duplicate shared id: " + uid
5758 + " name=" + name);
5759 return false;
5760 }
5761 mUserIds.set(index, obj);
5762 } else {
5763 if (mOtherUserIds.get(uid) != null) {
5764 reportSettingsProblem(Log.ERROR,
5765 "Adding duplicate shared id: " + uid
5766 + " name=" + name);
5767 return false;
5768 }
5769 mOtherUserIds.put(uid, obj);
5770 }
5771 return true;
5772 }
5773
5774 public Object getUserIdLP(int uid) {
5775 if (uid >= FIRST_APPLICATION_UID) {
5776 int N = mUserIds.size();
5777 final int index = uid - FIRST_APPLICATION_UID;
5778 return index < N ? mUserIds.get(index) : null;
5779 } else {
5780 return mOtherUserIds.get(uid);
5781 }
5782 }
5783
5784 private void removeUserIdLP(int uid) {
5785 if (uid >= FIRST_APPLICATION_UID) {
5786 int N = mUserIds.size();
5787 final int index = uid - FIRST_APPLICATION_UID;
5788 if (index < N) mUserIds.set(index, null);
5789 } else {
5790 mOtherUserIds.remove(uid);
5791 }
5792 }
5793
5794 void writeLP() {
5795 //Debug.startMethodTracing("/data/system/packageprof", 8 * 1024 * 1024);
5796
5797 // Keep the old settings around until we know the new ones have
5798 // been successfully written.
5799 if (mSettingsFilename.exists()) {
5800 if (mBackupSettingsFilename.exists()) {
5801 mBackupSettingsFilename.delete();
5802 }
5803 mSettingsFilename.renameTo(mBackupSettingsFilename);
5804 }
5805
5806 mPastSignatures.clear();
5807
5808 try {
5809 FileOutputStream str = new FileOutputStream(mSettingsFilename);
5810
5811 //XmlSerializer serializer = XmlUtils.serializerInstance();
5812 XmlSerializer serializer = new FastXmlSerializer();
5813 serializer.setOutput(str, "utf-8");
5814 serializer.startDocument(null, true);
5815 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
5816
5817 serializer.startTag(null, "packages");
5818
5819 serializer.startTag(null, "permission-trees");
5820 for (BasePermission bp : mPermissionTrees.values()) {
5821 writePermission(serializer, bp);
5822 }
5823 serializer.endTag(null, "permission-trees");
5824
5825 serializer.startTag(null, "permissions");
5826 for (BasePermission bp : mPermissions.values()) {
5827 writePermission(serializer, bp);
5828 }
5829 serializer.endTag(null, "permissions");
5830
5831 for (PackageSetting pkg : mPackages.values()) {
5832 writePackage(serializer, pkg);
5833 }
5834
5835 for (PackageSetting pkg : mDisabledSysPackages.values()) {
5836 writeDisabledSysPackage(serializer, pkg);
5837 }
5838
5839 serializer.startTag(null, "preferred-packages");
5840 int N = mPreferredPackages.size();
5841 for (int i=0; i<N; i++) {
5842 PackageSetting pkg = mPreferredPackages.get(i);
5843 serializer.startTag(null, "item");
5844 serializer.attribute(null, "name", pkg.name);
5845 serializer.endTag(null, "item");
5846 }
5847 serializer.endTag(null, "preferred-packages");
5848
5849 serializer.startTag(null, "preferred-activities");
5850 for (PreferredActivity pa : mPreferredActivities.filterSet()) {
5851 serializer.startTag(null, "item");
5852 pa.writeToXml(serializer);
5853 serializer.endTag(null, "item");
5854 }
5855 serializer.endTag(null, "preferred-activities");
5856
5857 for (SharedUserSetting usr : mSharedUsers.values()) {
5858 serializer.startTag(null, "shared-user");
5859 serializer.attribute(null, "name", usr.name);
5860 serializer.attribute(null, "userId",
5861 Integer.toString(usr.userId));
5862 usr.signatures.writeXml(serializer, "sigs", mPastSignatures);
5863 serializer.startTag(null, "perms");
5864 for (String name : usr.grantedPermissions) {
5865 serializer.startTag(null, "item");
5866 serializer.attribute(null, "name", name);
5867 serializer.endTag(null, "item");
5868 }
5869 serializer.endTag(null, "perms");
5870 serializer.endTag(null, "shared-user");
5871 }
5872
5873 serializer.endTag(null, "packages");
5874
5875 serializer.endDocument();
5876
5877 str.flush();
5878 str.close();
5879
5880 // New settings successfully written, old ones are no longer
5881 // needed.
5882 mBackupSettingsFilename.delete();
5883 FileUtils.setPermissions(mSettingsFilename.toString(),
5884 FileUtils.S_IRUSR|FileUtils.S_IWUSR
5885 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
5886 |FileUtils.S_IROTH,
5887 -1, -1);
5888
5889 } catch(XmlPullParserException e) {
5890 Log.w(TAG, "Unable to write package manager settings, current changes will be lost at reboot", e);
5891
5892 } catch(java.io.IOException e) {
5893 Log.w(TAG, "Unable to write package manager settings, current changes will be lost at reboot", e);
5894
5895 }
5896
5897 //Debug.stopMethodTracing();
5898 }
5899
5900 void writeDisabledSysPackage(XmlSerializer serializer, final PackageSetting pkg)
5901 throws java.io.IOException {
5902 serializer.startTag(null, "updated-package");
5903 serializer.attribute(null, "name", pkg.name);
5904 serializer.attribute(null, "codePath", pkg.codePathString);
5905 serializer.attribute(null, "ts", pkg.getTimeStampStr());
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005906 serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 if (!pkg.resourcePathString.equals(pkg.codePathString)) {
5908 serializer.attribute(null, "resourcePath", pkg.resourcePathString);
5909 }
5910 if (pkg.sharedUser == null) {
5911 serializer.attribute(null, "userId",
5912 Integer.toString(pkg.userId));
5913 } else {
5914 serializer.attribute(null, "sharedUserId",
5915 Integer.toString(pkg.userId));
5916 }
5917 serializer.startTag(null, "perms");
5918 if (pkg.sharedUser == null) {
5919 // If this is a shared user, the permissions will
5920 // be written there. We still need to write an
5921 // empty permissions list so permissionsFixed will
5922 // be set.
5923 for (final String name : pkg.grantedPermissions) {
5924 BasePermission bp = mPermissions.get(name);
5925 if ((bp != null) && (bp.perm != null) && (bp.perm.info != null)) {
5926 // We only need to write signature or system permissions but this wont
5927 // match the semantics of grantedPermissions. So write all permissions.
5928 serializer.startTag(null, "item");
5929 serializer.attribute(null, "name", name);
5930 serializer.endTag(null, "item");
5931 }
5932 }
5933 }
5934 serializer.endTag(null, "perms");
5935 serializer.endTag(null, "updated-package");
5936 }
5937
5938 void writePackage(XmlSerializer serializer, final PackageSetting pkg)
5939 throws java.io.IOException {
5940 serializer.startTag(null, "package");
5941 serializer.attribute(null, "name", pkg.name);
5942 serializer.attribute(null, "codePath", pkg.codePathString);
5943 if (!pkg.resourcePathString.equals(pkg.codePathString)) {
5944 serializer.attribute(null, "resourcePath", pkg.resourcePathString);
5945 }
5946 serializer.attribute(null, "system",
5947 (pkg.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0
5948 ? "true" : "false");
5949 serializer.attribute(null, "ts", pkg.getTimeStampStr());
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07005950 serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 if (pkg.sharedUser == null) {
5952 serializer.attribute(null, "userId",
5953 Integer.toString(pkg.userId));
5954 } else {
5955 serializer.attribute(null, "sharedUserId",
5956 Integer.toString(pkg.userId));
5957 }
5958 if (pkg.enabled != COMPONENT_ENABLED_STATE_DEFAULT) {
5959 serializer.attribute(null, "enabled",
5960 pkg.enabled == COMPONENT_ENABLED_STATE_ENABLED
5961 ? "true" : "false");
5962 }
5963 if(pkg.installStatus == PKG_INSTALL_INCOMPLETE) {
5964 serializer.attribute(null, "installStatus", "false");
5965 }
Jacek Surazskic64322c2009-04-28 15:26:38 +02005966 if (pkg.installerPackageName != null) {
5967 serializer.attribute(null, "installer", pkg.installerPackageName);
5968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 pkg.signatures.writeXml(serializer, "sigs", mPastSignatures);
5970 if ((pkg.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) {
5971 serializer.startTag(null, "perms");
5972 if (pkg.sharedUser == null) {
5973 // If this is a shared user, the permissions will
5974 // be written there. We still need to write an
5975 // empty permissions list so permissionsFixed will
5976 // be set.
5977 for (final String name : pkg.grantedPermissions) {
5978 serializer.startTag(null, "item");
5979 serializer.attribute(null, "name", name);
5980 serializer.endTag(null, "item");
5981 }
5982 }
5983 serializer.endTag(null, "perms");
5984 }
5985 if (pkg.disabledComponents.size() > 0) {
5986 serializer.startTag(null, "disabled-components");
5987 for (final String name : pkg.disabledComponents) {
5988 serializer.startTag(null, "item");
5989 serializer.attribute(null, "name", name);
5990 serializer.endTag(null, "item");
5991 }
5992 serializer.endTag(null, "disabled-components");
5993 }
5994 if (pkg.enabledComponents.size() > 0) {
5995 serializer.startTag(null, "enabled-components");
5996 for (final String name : pkg.enabledComponents) {
5997 serializer.startTag(null, "item");
5998 serializer.attribute(null, "name", name);
5999 serializer.endTag(null, "item");
6000 }
6001 serializer.endTag(null, "enabled-components");
6002 }
Jacek Surazskic64322c2009-04-28 15:26:38 +02006003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006004 serializer.endTag(null, "package");
6005 }
6006
6007 void writePermission(XmlSerializer serializer, BasePermission bp)
6008 throws XmlPullParserException, java.io.IOException {
6009 if (bp.type != BasePermission.TYPE_BUILTIN
6010 && bp.sourcePackage != null) {
6011 serializer.startTag(null, "item");
6012 serializer.attribute(null, "name", bp.name);
6013 serializer.attribute(null, "package", bp.sourcePackage);
6014 if (DEBUG_SETTINGS) Log.v(TAG,
6015 "Writing perm: name=" + bp.name + " type=" + bp.type);
6016 if (bp.type == BasePermission.TYPE_DYNAMIC) {
6017 PermissionInfo pi = bp.perm != null ? bp.perm.info
6018 : bp.pendingInfo;
6019 if (pi != null) {
6020 serializer.attribute(null, "type", "dynamic");
6021 if (pi.icon != 0) {
6022 serializer.attribute(null, "icon",
6023 Integer.toString(pi.icon));
6024 }
6025 if (pi.nonLocalizedLabel != null) {
6026 serializer.attribute(null, "label",
6027 pi.nonLocalizedLabel.toString());
6028 }
6029 if (pi.protectionLevel !=
6030 PermissionInfo.PROTECTION_NORMAL) {
6031 serializer.attribute(null, "protection",
6032 Integer.toString(pi.protectionLevel));
6033 }
6034 }
6035 }
6036 serializer.endTag(null, "item");
6037 }
6038 }
6039
6040 String getReadMessagesLP() {
6041 return mReadMessages.toString();
6042 }
6043
6044 ArrayList<String> getListOfIncompleteInstallPackages() {
6045 HashSet<String> kList = new HashSet<String>(mPackages.keySet());
6046 Iterator<String> its = kList.iterator();
6047 ArrayList<String> ret = new ArrayList<String>();
6048 while(its.hasNext()) {
6049 String key = its.next();
6050 PackageSetting ps = mPackages.get(key);
6051 if(ps.getInstallStatus() == PKG_INSTALL_INCOMPLETE) {
6052 ret.add(key);
6053 }
6054 }
6055 return ret;
6056 }
6057
6058 boolean readLP() {
6059 FileInputStream str = null;
6060 if (mBackupSettingsFilename.exists()) {
6061 try {
6062 str = new FileInputStream(mBackupSettingsFilename);
6063 mReadMessages.append("Reading from backup settings file\n");
6064 Log.i(TAG, "Reading from backup settings file!");
6065 } catch (java.io.IOException e) {
6066 // We'll try for the normal settings file.
6067 }
6068 }
6069
6070 mPastSignatures.clear();
6071
6072 try {
6073 if (str == null) {
6074 if (!mSettingsFilename.exists()) {
6075 mReadMessages.append("No settings file found\n");
6076 Log.i(TAG, "No current settings file!");
6077 return false;
6078 }
6079 str = new FileInputStream(mSettingsFilename);
6080 }
6081 XmlPullParser parser = Xml.newPullParser();
6082 parser.setInput(str, null);
6083
6084 int type;
6085 while ((type=parser.next()) != XmlPullParser.START_TAG
6086 && type != XmlPullParser.END_DOCUMENT) {
6087 ;
6088 }
6089
6090 if (type != XmlPullParser.START_TAG) {
6091 mReadMessages.append("No start tag found in settings file\n");
6092 Log.e(TAG, "No start tag found in package manager settings");
6093 return false;
6094 }
6095
6096 int outerDepth = parser.getDepth();
6097 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6098 && (type != XmlPullParser.END_TAG
6099 || parser.getDepth() > outerDepth)) {
6100 if (type == XmlPullParser.END_TAG
6101 || type == XmlPullParser.TEXT) {
6102 continue;
6103 }
6104
6105 String tagName = parser.getName();
6106 if (tagName.equals("package")) {
6107 readPackageLP(parser);
6108 } else if (tagName.equals("permissions")) {
6109 readPermissionsLP(mPermissions, parser);
6110 } else if (tagName.equals("permission-trees")) {
6111 readPermissionsLP(mPermissionTrees, parser);
6112 } else if (tagName.equals("shared-user")) {
6113 readSharedUserLP(parser);
6114 } else if (tagName.equals("preferred-packages")) {
6115 readPreferredPackagesLP(parser);
6116 } else if (tagName.equals("preferred-activities")) {
6117 readPreferredActivitiesLP(parser);
6118 } else if(tagName.equals("updated-package")) {
6119 readDisabledSysPackageLP(parser);
6120 } else {
6121 Log.w(TAG, "Unknown element under <packages>: "
6122 + parser.getName());
6123 XmlUtils.skipCurrentTag(parser);
6124 }
6125 }
6126
6127 str.close();
6128
6129 } catch(XmlPullParserException e) {
6130 mReadMessages.append("Error reading: " + e.toString());
6131 Log.e(TAG, "Error reading package manager settings", e);
6132
6133 } catch(java.io.IOException e) {
6134 mReadMessages.append("Error reading: " + e.toString());
6135 Log.e(TAG, "Error reading package manager settings", e);
6136
6137 }
6138
6139 int N = mPendingPackages.size();
6140 for (int i=0; i<N; i++) {
6141 final PendingPackage pp = mPendingPackages.get(i);
6142 Object idObj = getUserIdLP(pp.sharedId);
6143 if (idObj != null && idObj instanceof SharedUserSetting) {
6144 PackageSetting p = getPackageLP(pp.name,
6145 (SharedUserSetting)idObj, pp.codePath, pp.resourcePath,
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07006146 pp.versionCode, pp.pkgFlags, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006147 if (p == null) {
6148 Log.w(TAG, "Unable to create application package for "
6149 + pp.name);
6150 continue;
6151 }
6152 p.copyFrom(pp);
6153 } else if (idObj != null) {
6154 String msg = "Bad package setting: package " + pp.name
6155 + " has shared uid " + pp.sharedId
6156 + " that is not a shared uid\n";
6157 mReadMessages.append(msg);
6158 Log.e(TAG, msg);
6159 } else {
6160 String msg = "Bad package setting: package " + pp.name
6161 + " has shared uid " + pp.sharedId
6162 + " that is not defined\n";
6163 mReadMessages.append(msg);
6164 Log.e(TAG, msg);
6165 }
6166 }
6167 mPendingPackages.clear();
6168
6169 N = mPendingPreferredPackages.size();
6170 mPreferredPackages.clear();
6171 for (int i=0; i<N; i++) {
6172 final String name = mPendingPreferredPackages.get(i);
6173 final PackageSetting p = mPackages.get(name);
6174 if (p != null) {
6175 mPreferredPackages.add(p);
6176 } else {
6177 Log.w(TAG, "Unknown preferred package: " + name);
6178 }
6179 }
6180 mPendingPreferredPackages.clear();
6181
6182 mReadMessages.append("Read completed successfully: "
6183 + mPackages.size() + " packages, "
6184 + mSharedUsers.size() + " shared uids\n");
6185
6186 return true;
6187 }
6188
6189 private int readInt(XmlPullParser parser, String ns, String name,
6190 int defValue) {
6191 String v = parser.getAttributeValue(ns, name);
6192 try {
6193 if (v == null) {
6194 return defValue;
6195 }
6196 return Integer.parseInt(v);
6197 } catch (NumberFormatException e) {
6198 reportSettingsProblem(Log.WARN,
6199 "Error in package manager settings: attribute " +
6200 name + " has bad integer value " + v + " at "
6201 + parser.getPositionDescription());
6202 }
6203 return defValue;
6204 }
6205
6206 private void readPermissionsLP(HashMap<String, BasePermission> out,
6207 XmlPullParser parser)
6208 throws IOException, XmlPullParserException {
6209 int outerDepth = parser.getDepth();
6210 int type;
6211 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6212 && (type != XmlPullParser.END_TAG
6213 || parser.getDepth() > outerDepth)) {
6214 if (type == XmlPullParser.END_TAG
6215 || type == XmlPullParser.TEXT) {
6216 continue;
6217 }
6218
6219 String tagName = parser.getName();
6220 if (tagName.equals("item")) {
6221 String name = parser.getAttributeValue(null, "name");
6222 String sourcePackage = parser.getAttributeValue(null, "package");
6223 String ptype = parser.getAttributeValue(null, "type");
6224 if (name != null && sourcePackage != null) {
6225 boolean dynamic = "dynamic".equals(ptype);
6226 BasePermission bp = new BasePermission(name, sourcePackage,
6227 dynamic
6228 ? BasePermission.TYPE_DYNAMIC
6229 : BasePermission.TYPE_NORMAL);
6230 if (dynamic) {
6231 PermissionInfo pi = new PermissionInfo();
6232 pi.packageName = sourcePackage.intern();
6233 pi.name = name.intern();
6234 pi.icon = readInt(parser, null, "icon", 0);
6235 pi.nonLocalizedLabel = parser.getAttributeValue(
6236 null, "label");
6237 pi.protectionLevel = readInt(parser, null, "protection",
6238 PermissionInfo.PROTECTION_NORMAL);
6239 bp.pendingInfo = pi;
6240 }
6241 out.put(bp.name, bp);
6242 } else {
6243 reportSettingsProblem(Log.WARN,
6244 "Error in package manager settings: permissions has"
6245 + " no name at " + parser.getPositionDescription());
6246 }
6247 } else {
6248 reportSettingsProblem(Log.WARN,
6249 "Unknown element reading permissions: "
6250 + parser.getName() + " at "
6251 + parser.getPositionDescription());
6252 }
6253 XmlUtils.skipCurrentTag(parser);
6254 }
6255 }
6256
6257 private void readDisabledSysPackageLP(XmlPullParser parser)
6258 throws XmlPullParserException, IOException {
6259 String name = parser.getAttributeValue(null, "name");
6260 String codePathStr = parser.getAttributeValue(null, "codePath");
6261 String resourcePathStr = parser.getAttributeValue(null, "resourcePath");
6262 if(resourcePathStr == null) {
6263 resourcePathStr = codePathStr;
6264 }
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07006265 String version = parser.getAttributeValue(null, "version");
6266 int versionCode = 0;
6267 if (version != null) {
6268 try {
6269 versionCode = Integer.parseInt(version);
6270 } catch (NumberFormatException e) {
6271 }
6272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273
6274 int pkgFlags = 0;
6275 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
6276 PackageSetting ps = new PackageSetting(name,
6277 new File(codePathStr),
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07006278 new File(resourcePathStr), versionCode, pkgFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 String timeStampStr = parser.getAttributeValue(null, "ts");
6280 if (timeStampStr != null) {
6281 try {
6282 long timeStamp = Long.parseLong(timeStampStr);
6283 ps.setTimeStamp(timeStamp, timeStampStr);
6284 } catch (NumberFormatException e) {
6285 }
6286 }
6287 String idStr = parser.getAttributeValue(null, "userId");
6288 ps.userId = idStr != null ? Integer.parseInt(idStr) : 0;
6289 if(ps.userId <= 0) {
6290 String sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
6291 ps.userId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0;
6292 }
6293 int outerDepth = parser.getDepth();
6294 int type;
6295 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6296 && (type != XmlPullParser.END_TAG
6297 || parser.getDepth() > outerDepth)) {
6298 if (type == XmlPullParser.END_TAG
6299 || type == XmlPullParser.TEXT) {
6300 continue;
6301 }
6302
6303 String tagName = parser.getName();
6304 if (tagName.equals("perms")) {
6305 readGrantedPermissionsLP(parser,
6306 ps.grantedPermissions);
6307 } else {
6308 reportSettingsProblem(Log.WARN,
6309 "Unknown element under <updated-package>: "
6310 + parser.getName());
6311 XmlUtils.skipCurrentTag(parser);
6312 }
6313 }
6314 mDisabledSysPackages.put(name, ps);
6315 }
6316
6317 private void readPackageLP(XmlPullParser parser)
6318 throws XmlPullParserException, IOException {
6319 String name = null;
6320 String idStr = null;
6321 String sharedIdStr = null;
6322 String codePathStr = null;
6323 String resourcePathStr = null;
6324 String systemStr = null;
Jacek Surazskic64322c2009-04-28 15:26:38 +02006325 String installerPackageName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326 int pkgFlags = 0;
6327 String timeStampStr;
6328 long timeStamp = 0;
6329 PackageSettingBase packageSetting = null;
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07006330 String version = null;
6331 int versionCode = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006332 try {
6333 name = parser.getAttributeValue(null, "name");
6334 idStr = parser.getAttributeValue(null, "userId");
6335 sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
6336 codePathStr = parser.getAttributeValue(null, "codePath");
6337 resourcePathStr = parser.getAttributeValue(null, "resourcePath");
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07006338 version = parser.getAttributeValue(null, "version");
6339 if (version != null) {
6340 try {
6341 versionCode = Integer.parseInt(version);
6342 } catch (NumberFormatException e) {
6343 }
6344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 systemStr = parser.getAttributeValue(null, "system");
Jacek Surazskic64322c2009-04-28 15:26:38 +02006346 installerPackageName = parser.getAttributeValue(null, "installer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 if (systemStr != null) {
6348 if ("true".equals(systemStr)) {
6349 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
6350 }
6351 } else {
6352 // Old settings that don't specify system... just treat
6353 // them as system, good enough.
6354 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
6355 }
6356 timeStampStr = parser.getAttributeValue(null, "ts");
6357 if (timeStampStr != null) {
6358 try {
6359 timeStamp = Long.parseLong(timeStampStr);
6360 } catch (NumberFormatException e) {
6361 }
6362 }
6363 if (DEBUG_SETTINGS) Log.v(TAG, "Reading package: " + name
6364 + " userId=" + idStr + " sharedUserId=" + sharedIdStr);
6365 int userId = idStr != null ? Integer.parseInt(idStr) : 0;
6366 if (resourcePathStr == null) {
6367 resourcePathStr = codePathStr;
6368 }
6369 if (name == null) {
6370 reportSettingsProblem(Log.WARN,
6371 "Error in package manager settings: <package> has no name at "
6372 + parser.getPositionDescription());
6373 } else if (codePathStr == null) {
6374 reportSettingsProblem(Log.WARN,
6375 "Error in package manager settings: <package> has no codePath at "
6376 + parser.getPositionDescription());
6377 } else if (userId > 0) {
6378 packageSetting = addPackageLP(name.intern(), new File(codePathStr),
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07006379 new File(resourcePathStr), userId, versionCode, pkgFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 if (DEBUG_SETTINGS) Log.i(TAG, "Reading package " + name
6381 + ": userId=" + userId + " pkg=" + packageSetting);
6382 if (packageSetting == null) {
6383 reportSettingsProblem(Log.ERROR,
6384 "Failure adding uid " + userId
6385 + " while parsing settings at "
6386 + parser.getPositionDescription());
6387 } else {
6388 packageSetting.setTimeStamp(timeStamp, timeStampStr);
6389 }
6390 } else if (sharedIdStr != null) {
6391 userId = sharedIdStr != null
6392 ? Integer.parseInt(sharedIdStr) : 0;
6393 if (userId > 0) {
6394 packageSetting = new PendingPackage(name.intern(), new File(codePathStr),
Suchi Amalapurapuc2af31f2009-05-08 14:44:41 -07006395 new File(resourcePathStr), userId, versionCode, pkgFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 packageSetting.setTimeStamp(timeStamp, timeStampStr);
6397 mPendingPackages.add((PendingPackage) packageSetting);
6398 if (DEBUG_SETTINGS) Log.i(TAG, "Reading package " + name
6399 + ": sharedUserId=" + userId + " pkg="
6400 + packageSetting);
6401 } else {
6402 reportSettingsProblem(Log.WARN,
6403 "Error in package manager settings: package "
6404 + name + " has bad sharedId " + sharedIdStr
6405 + " at " + parser.getPositionDescription());
6406 }
6407 } else {
6408 reportSettingsProblem(Log.WARN,
6409 "Error in package manager settings: package "
6410 + name + " has bad userId " + idStr + " at "
6411 + parser.getPositionDescription());
6412 }
6413 } catch (NumberFormatException e) {
6414 reportSettingsProblem(Log.WARN,
6415 "Error in package manager settings: package "
6416 + name + " has bad userId " + idStr + " at "
6417 + parser.getPositionDescription());
6418 }
6419 if (packageSetting != null) {
Jacek Surazskic64322c2009-04-28 15:26:38 +02006420 packageSetting.installerPackageName = installerPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006421 final String enabledStr = parser.getAttributeValue(null, "enabled");
6422 if (enabledStr != null) {
6423 if (enabledStr.equalsIgnoreCase("true")) {
6424 packageSetting.enabled = COMPONENT_ENABLED_STATE_ENABLED;
6425 } else if (enabledStr.equalsIgnoreCase("false")) {
6426 packageSetting.enabled = COMPONENT_ENABLED_STATE_DISABLED;
6427 } else if (enabledStr.equalsIgnoreCase("default")) {
6428 packageSetting.enabled = COMPONENT_ENABLED_STATE_DEFAULT;
6429 } else {
6430 reportSettingsProblem(Log.WARN,
6431 "Error in package manager settings: package "
6432 + name + " has bad enabled value: " + idStr
6433 + " at " + parser.getPositionDescription());
6434 }
6435 } else {
6436 packageSetting.enabled = COMPONENT_ENABLED_STATE_DEFAULT;
6437 }
6438 final String installStatusStr = parser.getAttributeValue(null, "installStatus");
6439 if (installStatusStr != null) {
6440 if (installStatusStr.equalsIgnoreCase("false")) {
6441 packageSetting.installStatus = PKG_INSTALL_INCOMPLETE;
6442 } else {
6443 packageSetting.installStatus = PKG_INSTALL_COMPLETE;
6444 }
6445 }
6446
6447 int outerDepth = parser.getDepth();
6448 int type;
6449 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6450 && (type != XmlPullParser.END_TAG
6451 || parser.getDepth() > outerDepth)) {
6452 if (type == XmlPullParser.END_TAG
6453 || type == XmlPullParser.TEXT) {
6454 continue;
6455 }
6456
6457 String tagName = parser.getName();
6458 if (tagName.equals("disabled-components")) {
6459 readDisabledComponentsLP(packageSetting, parser);
6460 } else if (tagName.equals("enabled-components")) {
6461 readEnabledComponentsLP(packageSetting, parser);
6462 } else if (tagName.equals("sigs")) {
6463 packageSetting.signatures.readXml(parser, mPastSignatures);
6464 } else if (tagName.equals("perms")) {
6465 readGrantedPermissionsLP(parser,
6466 packageSetting.loadedPermissions);
6467 packageSetting.permissionsFixed = true;
6468 } else {
6469 reportSettingsProblem(Log.WARN,
6470 "Unknown element under <package>: "
6471 + parser.getName());
6472 XmlUtils.skipCurrentTag(parser);
6473 }
6474 }
6475 } else {
6476 XmlUtils.skipCurrentTag(parser);
6477 }
6478 }
6479
6480 private void readDisabledComponentsLP(PackageSettingBase packageSetting,
6481 XmlPullParser parser)
6482 throws IOException, XmlPullParserException {
6483 int outerDepth = parser.getDepth();
6484 int type;
6485 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6486 && (type != XmlPullParser.END_TAG
6487 || parser.getDepth() > outerDepth)) {
6488 if (type == XmlPullParser.END_TAG
6489 || type == XmlPullParser.TEXT) {
6490 continue;
6491 }
6492
6493 String tagName = parser.getName();
6494 if (tagName.equals("item")) {
6495 String name = parser.getAttributeValue(null, "name");
6496 if (name != null) {
6497 packageSetting.disabledComponents.add(name.intern());
6498 } else {
6499 reportSettingsProblem(Log.WARN,
6500 "Error in package manager settings: <disabled-components> has"
6501 + " no name at " + parser.getPositionDescription());
6502 }
6503 } else {
6504 reportSettingsProblem(Log.WARN,
6505 "Unknown element under <disabled-components>: "
6506 + parser.getName());
6507 }
6508 XmlUtils.skipCurrentTag(parser);
6509 }
6510 }
6511
6512 private void readEnabledComponentsLP(PackageSettingBase packageSetting,
6513 XmlPullParser parser)
6514 throws IOException, XmlPullParserException {
6515 int outerDepth = parser.getDepth();
6516 int type;
6517 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6518 && (type != XmlPullParser.END_TAG
6519 || parser.getDepth() > outerDepth)) {
6520 if (type == XmlPullParser.END_TAG
6521 || type == XmlPullParser.TEXT) {
6522 continue;
6523 }
6524
6525 String tagName = parser.getName();
6526 if (tagName.equals("item")) {
6527 String name = parser.getAttributeValue(null, "name");
6528 if (name != null) {
6529 packageSetting.enabledComponents.add(name.intern());
6530 } else {
6531 reportSettingsProblem(Log.WARN,
6532 "Error in package manager settings: <enabled-components> has"
6533 + " no name at " + parser.getPositionDescription());
6534 }
6535 } else {
6536 reportSettingsProblem(Log.WARN,
6537 "Unknown element under <enabled-components>: "
6538 + parser.getName());
6539 }
6540 XmlUtils.skipCurrentTag(parser);
6541 }
6542 }
6543
6544 private void readSharedUserLP(XmlPullParser parser)
6545 throws XmlPullParserException, IOException {
6546 String name = null;
6547 String idStr = null;
6548 int pkgFlags = 0;
6549 SharedUserSetting su = null;
6550 try {
6551 name = parser.getAttributeValue(null, "name");
6552 idStr = parser.getAttributeValue(null, "userId");
6553 int userId = idStr != null ? Integer.parseInt(idStr) : 0;
6554 if ("true".equals(parser.getAttributeValue(null, "system"))) {
6555 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
6556 }
6557 if (name == null) {
6558 reportSettingsProblem(Log.WARN,
6559 "Error in package manager settings: <shared-user> has no name at "
6560 + parser.getPositionDescription());
6561 } else if (userId == 0) {
6562 reportSettingsProblem(Log.WARN,
6563 "Error in package manager settings: shared-user "
6564 + name + " has bad userId " + idStr + " at "
6565 + parser.getPositionDescription());
6566 } else {
6567 if ((su=addSharedUserLP(name.intern(), userId, pkgFlags)) == null) {
6568 reportSettingsProblem(Log.ERROR,
6569 "Occurred while parsing settings at "
6570 + parser.getPositionDescription());
6571 }
6572 }
6573 } catch (NumberFormatException e) {
6574 reportSettingsProblem(Log.WARN,
6575 "Error in package manager settings: package "
6576 + name + " has bad userId " + idStr + " at "
6577 + parser.getPositionDescription());
6578 };
6579
6580 if (su != null) {
6581 int outerDepth = parser.getDepth();
6582 int type;
6583 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6584 && (type != XmlPullParser.END_TAG
6585 || parser.getDepth() > outerDepth)) {
6586 if (type == XmlPullParser.END_TAG
6587 || type == XmlPullParser.TEXT) {
6588 continue;
6589 }
6590
6591 String tagName = parser.getName();
6592 if (tagName.equals("sigs")) {
6593 su.signatures.readXml(parser, mPastSignatures);
6594 } else if (tagName.equals("perms")) {
6595 readGrantedPermissionsLP(parser, su.loadedPermissions);
6596 } else {
6597 reportSettingsProblem(Log.WARN,
6598 "Unknown element under <shared-user>: "
6599 + parser.getName());
6600 XmlUtils.skipCurrentTag(parser);
6601 }
6602 }
6603
6604 } else {
6605 XmlUtils.skipCurrentTag(parser);
6606 }
6607 }
6608
6609 private void readGrantedPermissionsLP(XmlPullParser parser,
6610 HashSet<String> outPerms) throws IOException, XmlPullParserException {
6611 int outerDepth = parser.getDepth();
6612 int type;
6613 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6614 && (type != XmlPullParser.END_TAG
6615 || parser.getDepth() > outerDepth)) {
6616 if (type == XmlPullParser.END_TAG
6617 || type == XmlPullParser.TEXT) {
6618 continue;
6619 }
6620
6621 String tagName = parser.getName();
6622 if (tagName.equals("item")) {
6623 String name = parser.getAttributeValue(null, "name");
6624 if (name != null) {
6625 outPerms.add(name.intern());
6626 } else {
6627 reportSettingsProblem(Log.WARN,
6628 "Error in package manager settings: <perms> has"
6629 + " no name at " + parser.getPositionDescription());
6630 }
6631 } else {
6632 reportSettingsProblem(Log.WARN,
6633 "Unknown element under <perms>: "
6634 + parser.getName());
6635 }
6636 XmlUtils.skipCurrentTag(parser);
6637 }
6638 }
6639
6640 private void readPreferredPackagesLP(XmlPullParser parser)
6641 throws XmlPullParserException, IOException {
6642 int outerDepth = parser.getDepth();
6643 int type;
6644 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6645 && (type != XmlPullParser.END_TAG
6646 || parser.getDepth() > outerDepth)) {
6647 if (type == XmlPullParser.END_TAG
6648 || type == XmlPullParser.TEXT) {
6649 continue;
6650 }
6651
6652 String tagName = parser.getName();
6653 if (tagName.equals("item")) {
6654 String name = parser.getAttributeValue(null, "name");
6655 if (name != null) {
6656 mPendingPreferredPackages.add(name);
6657 } else {
6658 reportSettingsProblem(Log.WARN,
6659 "Error in package manager settings: <preferred-package> has no name at "
6660 + parser.getPositionDescription());
6661 }
6662 } else {
6663 reportSettingsProblem(Log.WARN,
6664 "Unknown element under <preferred-packages>: "
6665 + parser.getName());
6666 }
6667 XmlUtils.skipCurrentTag(parser);
6668 }
6669 }
6670
6671 private void readPreferredActivitiesLP(XmlPullParser parser)
6672 throws XmlPullParserException, IOException {
6673 int outerDepth = parser.getDepth();
6674 int type;
6675 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
6676 && (type != XmlPullParser.END_TAG
6677 || parser.getDepth() > outerDepth)) {
6678 if (type == XmlPullParser.END_TAG
6679 || type == XmlPullParser.TEXT) {
6680 continue;
6681 }
6682
6683 String tagName = parser.getName();
6684 if (tagName.equals("item")) {
6685 PreferredActivity pa = new PreferredActivity(parser);
6686 if (pa.mParseError == null) {
6687 mPreferredActivities.addFilter(pa);
6688 } else {
6689 reportSettingsProblem(Log.WARN,
6690 "Error in package manager settings: <preferred-activity> "
6691 + pa.mParseError + " at "
6692 + parser.getPositionDescription());
6693 }
6694 } else {
6695 reportSettingsProblem(Log.WARN,
6696 "Unknown element under <preferred-activities>: "
6697 + parser.getName());
6698 XmlUtils.skipCurrentTag(parser);
6699 }
6700 }
6701 }
6702
6703 // Returns -1 if we could not find an available UserId to assign
6704 private int newUserIdLP(Object obj) {
6705 // Let's be stupidly inefficient for now...
6706 final int N = mUserIds.size();
6707 for (int i=0; i<N; i++) {
6708 if (mUserIds.get(i) == null) {
6709 mUserIds.set(i, obj);
6710 return FIRST_APPLICATION_UID + i;
6711 }
6712 }
6713
6714 // None left?
6715 if (N >= MAX_APPLICATION_UIDS) {
6716 return -1;
6717 }
6718
6719 mUserIds.add(obj);
6720 return FIRST_APPLICATION_UID + N;
6721 }
6722
6723 public PackageSetting getDisabledSystemPkg(String name) {
6724 synchronized(mPackages) {
6725 PackageSetting ps = mDisabledSysPackages.get(name);
6726 return ps;
6727 }
6728 }
6729
6730 boolean isEnabledLP(ComponentInfo componentInfo, int flags) {
6731 final PackageSetting packageSettings = mPackages.get(componentInfo.packageName);
6732 if (Config.LOGV) {
6733 Log.v(TAG, "isEnabledLock - packageName = " + componentInfo.packageName
6734 + " componentName = " + componentInfo.name);
6735 Log.v(TAG, "enabledComponents: "
6736 + Arrays.toString(packageSettings.enabledComponents.toArray()));
6737 Log.v(TAG, "disabledComponents: "
6738 + Arrays.toString(packageSettings.disabledComponents.toArray()));
6739 }
6740 return ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0)
6741 || ((componentInfo.enabled
6742 && ((packageSettings.enabled == COMPONENT_ENABLED_STATE_ENABLED)
6743 || (componentInfo.applicationInfo.enabled
6744 && packageSettings.enabled != COMPONENT_ENABLED_STATE_DISABLED))
6745 && !packageSettings.disabledComponents.contains(componentInfo.name))
6746 || packageSettings.enabledComponents.contains(componentInfo.name));
6747 }
6748 }
6749}