| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 19 | import com.android.internal.app.IMediaContainerService; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.app.ResolverActivity; |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 21 | import com.android.internal.content.PackageHelper; |
| Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 22 | import com.android.internal.util.FastXmlSerializer; |
| 23 | import com.android.internal.util.XmlUtils; |
| David 'Digit' Turner | add1376 | 2010-02-03 17:34:58 -0800 | [diff] [blame] | 24 | import com.android.server.JournaledFile; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | import org.xmlpull.v1.XmlPullParser; |
| 27 | import org.xmlpull.v1.XmlPullParserException; |
| 28 | import org.xmlpull.v1.XmlSerializer; |
| 29 | |
| 30 | import android.app.ActivityManagerNative; |
| 31 | import android.app.IActivityManager; |
| Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 32 | import android.app.admin.DevicePolicyManager; |
| 33 | import android.app.admin.IDevicePolicyManager; |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 34 | import android.app.backup.IBackupManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.content.ComponentName; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.content.IntentFilter; |
| Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 39 | import android.content.IntentSender; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 40 | import android.content.ServiceConnection; |
| Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 41 | import android.content.IntentSender.SendIntentException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.content.pm.ActivityInfo; |
| 43 | import android.content.pm.ApplicationInfo; |
| 44 | import android.content.pm.ComponentInfo; |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 45 | import android.content.pm.FeatureInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.content.pm.IPackageDataObserver; |
| 47 | import android.content.pm.IPackageDeleteObserver; |
| 48 | import android.content.pm.IPackageInstallObserver; |
| 49 | import android.content.pm.IPackageManager; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 50 | import android.content.pm.IPackageMoveObserver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.content.pm.IPackageStatsObserver; |
| 52 | import android.content.pm.InstrumentationInfo; |
| 53 | import android.content.pm.PackageInfo; |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 54 | import android.content.pm.PackageInfoLite; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.content.pm.PackageManager; |
| 56 | import android.content.pm.PackageStats; |
| 57 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; |
| 58 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED; |
| 59 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.content.pm.PackageParser; |
| 61 | import android.content.pm.PermissionInfo; |
| 62 | import android.content.pm.PermissionGroupInfo; |
| 63 | import android.content.pm.ProviderInfo; |
| 64 | import android.content.pm.ResolveInfo; |
| 65 | import android.content.pm.ServiceInfo; |
| 66 | import android.content.pm.Signature; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.net.Uri; |
| 68 | import android.os.Binder; |
| Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 69 | import android.os.Build; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.os.Bundle; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 71 | import android.os.Debug; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.os.HandlerThread; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 73 | import android.os.IBinder; |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 74 | import android.os.Looper; |
| 75 | import android.os.Message; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.os.Parcel; |
| 77 | import android.os.RemoteException; |
| 78 | import android.os.Environment; |
| 79 | import android.os.FileObserver; |
| 80 | import android.os.FileUtils; |
| 81 | import android.os.Handler; |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 82 | import android.os.StatFs; |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 83 | import android.os.storage.StorageResultCode; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.os.ParcelFileDescriptor; |
| 85 | import android.os.Process; |
| 86 | import android.os.ServiceManager; |
| 87 | import android.os.SystemClock; |
| 88 | import android.os.SystemProperties; |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 89 | import android.provider.Settings; |
| Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 90 | import android.security.SystemKeyStore; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.util.*; |
| 92 | import android.view.Display; |
| 93 | import android.view.WindowManager; |
| 94 | |
| 95 | import java.io.File; |
| 96 | import java.io.FileDescriptor; |
| 97 | import java.io.FileInputStream; |
| 98 | import java.io.FileNotFoundException; |
| 99 | import java.io.FileOutputStream; |
| 100 | import java.io.FileReader; |
| 101 | import java.io.FilenameFilter; |
| 102 | import java.io.IOException; |
| 103 | import java.io.InputStream; |
| 104 | import java.io.PrintWriter; |
| Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 105 | import java.security.NoSuchAlgorithmException; |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 106 | import java.text.SimpleDateFormat; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import java.util.ArrayList; |
| 108 | import java.util.Arrays; |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 109 | import java.util.Collection; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import java.util.Collections; |
| 111 | import java.util.Comparator; |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 112 | import java.util.Date; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import java.util.Enumeration; |
| 114 | import java.util.HashMap; |
| 115 | import java.util.HashSet; |
| 116 | import java.util.Iterator; |
| 117 | import java.util.List; |
| 118 | import java.util.Map; |
| 119 | import java.util.Set; |
| 120 | import java.util.zip.ZipEntry; |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 121 | import java.util.zip.ZipException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | import java.util.zip.ZipFile; |
| 123 | import java.util.zip.ZipOutputStream; |
| 124 | |
| 125 | class PackageManagerService extends IPackageManager.Stub { |
| 126 | private static final String TAG = "PackageManager"; |
| 127 | private static final boolean DEBUG_SETTINGS = false; |
| 128 | private static final boolean DEBUG_PREFERRED = false; |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 129 | private static final boolean DEBUG_UPGRADE = false; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 130 | private static final boolean DEBUG_INSTALL = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
| 132 | private static final boolean MULTIPLE_APPLICATION_UIDS = true; |
| 133 | private static final int RADIO_UID = Process.PHONE_UID; |
| Mike Lockwood | d42685d | 2009-09-03 09:25:22 -0400 | [diff] [blame] | 134 | private static final int LOG_UID = Process.LOG_UID; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | private static final int FIRST_APPLICATION_UID = |
| 136 | Process.FIRST_APPLICATION_UID; |
| 137 | private static final int MAX_APPLICATION_UIDS = 1000; |
| 138 | |
| 139 | private static final boolean SHOW_INFO = false; |
| 140 | |
| 141 | private static final boolean GET_CERTIFICATES = true; |
| 142 | |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 143 | private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled"; |
| 144 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | private static final int REMOVE_EVENTS = |
| 146 | FileObserver.CLOSE_WRITE | FileObserver.DELETE | FileObserver.MOVED_FROM; |
| 147 | private static final int ADD_EVENTS = |
| 148 | FileObserver.CLOSE_WRITE /*| FileObserver.CREATE*/ | FileObserver.MOVED_TO; |
| 149 | |
| 150 | private static final int OBSERVER_EVENTS = REMOVE_EVENTS | ADD_EVENTS; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 151 | // Suffix used during package installation when copying/moving |
| 152 | // package apks to install directory. |
| 153 | private static final String INSTALL_PACKAGE_SUFFIX = "-"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | |
| Suchi Amalapurapu | 1f9e1b4 | 2010-02-26 13:14:31 -0800 | [diff] [blame] | 155 | /** |
| 156 | * Indicates the state of installation. Used by PackageManager to |
| 157 | * figure out incomplete installations. Say a package is being installed |
| 158 | * (the state is set to PKG_INSTALL_INCOMPLETE) and remains so till |
| 159 | * the package installation is successful or unsuccesful lin which case |
| 160 | * the PackageManager will no longer maintain state information associated |
| 161 | * with the package. If some exception(like device freeze or battery being |
| 162 | * pulled out) occurs during installation of a package, the PackageManager |
| 163 | * needs this information to clean up the previously failed installation. |
| 164 | */ |
| 165 | private static final int PKG_INSTALL_INCOMPLETE = 0; |
| 166 | private static final int PKG_INSTALL_COMPLETE = 1; |
| 167 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | static final int SCAN_MONITOR = 1<<0; |
| 169 | static final int SCAN_NO_DEX = 1<<1; |
| 170 | static final int SCAN_FORCE_DEX = 1<<2; |
| 171 | static final int SCAN_UPDATE_SIGNATURE = 1<<3; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 172 | static final int SCAN_NEW_INSTALL = 1<<4; |
| 173 | static final int SCAN_NO_PATHS = 1<<5; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 175 | static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName( |
| 176 | "com.android.defcontainer", |
| 177 | "com.android.defcontainer.DefaultContainerService"); |
| 178 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | final HandlerThread mHandlerThread = new HandlerThread("PackageManager", |
| 180 | Process.THREAD_PRIORITY_BACKGROUND); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 181 | final PackageHandler mHandler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | |
| Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 183 | final int mSdkVersion = Build.VERSION.SDK_INT; |
| 184 | final String mSdkCodename = "REL".equals(Build.VERSION.CODENAME) |
| 185 | ? null : Build.VERSION.CODENAME; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 186 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | final Context mContext; |
| 188 | final boolean mFactoryTest; |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 189 | final boolean mNoDexOpt; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | final DisplayMetrics mMetrics; |
| 191 | final int mDefParseFlags; |
| 192 | final String[] mSeparateProcesses; |
| 193 | |
| 194 | // This is where all application persistent data goes. |
| 195 | final File mAppDataDir; |
| 196 | |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 197 | // If Encrypted File System feature is enabled, all application persistent data |
| 198 | // should go here instead. |
| 199 | final File mSecureAppDataDir; |
| 200 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | // This is the object monitoring the framework dir. |
| 202 | final FileObserver mFrameworkInstallObserver; |
| 203 | |
| 204 | // This is the object monitoring the system app dir. |
| 205 | final FileObserver mSystemInstallObserver; |
| 206 | |
| 207 | // This is the object monitoring mAppInstallDir. |
| 208 | final FileObserver mAppInstallObserver; |
| 209 | |
| 210 | // This is the object monitoring mDrmAppPrivateInstallDir. |
| 211 | final FileObserver mDrmAppInstallObserver; |
| 212 | |
| 213 | // Used for priviledge escalation. MUST NOT BE CALLED WITH mPackages |
| 214 | // LOCK HELD. Can be called with mInstallLock held. |
| 215 | final Installer mInstaller; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 216 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | final File mFrameworkDir; |
| 218 | final File mSystemAppDir; |
| 219 | final File mAppInstallDir; |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 220 | final File mDalvikCacheDir; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | |
| 222 | // Directory containing the private parts (e.g. code and non-resource assets) of forward-locked |
| 223 | // apps. |
| 224 | final File mDrmAppPrivateInstallDir; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 225 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | // ---------------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 227 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | // Lock for state used when installing and doing other long running |
| 229 | // operations. Methods that must be called with this lock held have |
| 230 | // the prefix "LI". |
| 231 | final Object mInstallLock = new Object(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 232 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | // These are the directories in the 3rd party applications installed dir |
| 234 | // that we have currently loaded packages from. Keys are the application's |
| 235 | // installed zip file (absolute codePath), and values are Package. |
| 236 | final HashMap<String, PackageParser.Package> mAppDirs = |
| 237 | new HashMap<String, PackageParser.Package>(); |
| 238 | |
| 239 | // Information for the parser to write more useful error messages. |
| 240 | File mScanningPath; |
| 241 | int mLastScanError; |
| 242 | |
| 243 | final int[] mOutPermissions = new int[3]; |
| 244 | |
| 245 | // ---------------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 246 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | // Keys are String (package name), values are Package. This also serves |
| 248 | // as the lock for the global state. Methods that must be called with |
| 249 | // this lock held have the prefix "LP". |
| 250 | final HashMap<String, PackageParser.Package> mPackages = |
| 251 | new HashMap<String, PackageParser.Package>(); |
| 252 | |
| 253 | final Settings mSettings; |
| 254 | boolean mRestoredSettings; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | |
| 256 | // Group-ids that are given to all packages as read from etc/permissions/*.xml. |
| 257 | int[] mGlobalGids; |
| 258 | |
| 259 | // These are the built-in uid -> permission mappings that were read from the |
| 260 | // etc/permissions.xml file. |
| 261 | final SparseArray<HashSet<String>> mSystemPermissions = |
| 262 | new SparseArray<HashSet<String>>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 263 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | // These are the built-in shared libraries that were read from the |
| 265 | // etc/permissions.xml file. |
| 266 | final HashMap<String, String> mSharedLibraries = new HashMap<String, String>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 267 | |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 268 | // Temporary for building the final shared libraries for an .apk. |
| 269 | String[] mTmpSharedLibraries = null; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 270 | |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 271 | // These are the features this devices supports that were read from the |
| 272 | // etc/permissions.xml file. |
| 273 | final HashMap<String, FeatureInfo> mAvailableFeatures = |
| 274 | new HashMap<String, FeatureInfo>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 275 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | // All available activities, for your resolving pleasure. |
| 277 | final ActivityIntentResolver mActivities = |
| 278 | new ActivityIntentResolver(); |
| 279 | |
| 280 | // All available receivers, for your resolving pleasure. |
| 281 | final ActivityIntentResolver mReceivers = |
| 282 | new ActivityIntentResolver(); |
| 283 | |
| 284 | // All available services, for your resolving pleasure. |
| 285 | final ServiceIntentResolver mServices = new ServiceIntentResolver(); |
| 286 | |
| 287 | // Keys are String (provider class name), values are Provider. |
| 288 | final HashMap<ComponentName, PackageParser.Provider> mProvidersByComponent = |
| 289 | new HashMap<ComponentName, PackageParser.Provider>(); |
| 290 | |
| 291 | // Mapping from provider base names (first directory in content URI codePath) |
| 292 | // to the provider information. |
| 293 | final HashMap<String, PackageParser.Provider> mProviders = |
| 294 | new HashMap<String, PackageParser.Provider>(); |
| 295 | |
| 296 | // Mapping from instrumentation class names to info about them. |
| 297 | final HashMap<ComponentName, PackageParser.Instrumentation> mInstrumentation = |
| 298 | new HashMap<ComponentName, PackageParser.Instrumentation>(); |
| 299 | |
| 300 | // Mapping from permission names to info about them. |
| 301 | final HashMap<String, PackageParser.PermissionGroup> mPermissionGroups = |
| 302 | new HashMap<String, PackageParser.PermissionGroup>(); |
| 303 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 304 | // Packages whose data we have transfered into another package, thus |
| 305 | // should no longer exist. |
| 306 | final HashSet<String> mTransferedPackages = new HashSet<String>(); |
| 307 | |
| Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 308 | // Broadcast actions that are only available to the system. |
| 309 | final HashSet<String> mProtectedBroadcasts = new HashSet<String>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 310 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | boolean mSystemReady; |
| 312 | boolean mSafeMode; |
| 313 | boolean mHasSystemUidErrors; |
| 314 | |
| 315 | ApplicationInfo mAndroidApplication; |
| 316 | final ActivityInfo mResolveActivity = new ActivityInfo(); |
| 317 | final ResolveInfo mResolveInfo = new ResolveInfo(); |
| 318 | ComponentName mResolveComponentName; |
| 319 | PackageParser.Package mPlatformPackage; |
| 320 | |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 321 | // Set of pending broadcasts for aggregating enable/disable of components. |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 322 | final HashMap<String, ArrayList<String>> mPendingBroadcasts |
| 323 | = new HashMap<String, ArrayList<String>>(); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 324 | // Service Connection to remote media container service to copy |
| 325 | // package uri's from external media onto secure containers |
| 326 | // or internal storage. |
| 327 | private IMediaContainerService mContainerService = null; |
| 328 | |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 329 | static final int SEND_PENDING_BROADCAST = 1; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 330 | static final int MCS_BOUND = 3; |
| 331 | static final int END_COPY = 4; |
| 332 | static final int INIT_COPY = 5; |
| 333 | static final int MCS_UNBIND = 6; |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 334 | static final int START_CLEANING_PACKAGE = 7; |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 335 | static final int FIND_INSTALL_LOC = 8; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 336 | static final int POST_INSTALL = 9; |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 337 | static final int MCS_RECONNECT = 10; |
| 338 | static final int MCS_GIVE_UP = 11; |
| 339 | |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 340 | // Delay time in millisecs |
| 341 | static final int BROADCAST_DELAY = 10 * 1000; |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 342 | final private DefaultContainerConnection mDefContainerConn = |
| 343 | new DefaultContainerConnection(); |
| 344 | class DefaultContainerConnection implements ServiceConnection { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 345 | public void onServiceConnected(ComponentName name, IBinder service) { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 346 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceConnected"); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 347 | IMediaContainerService imcs = |
| 348 | IMediaContainerService.Stub.asInterface(service); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 349 | mHandler.sendMessage(mHandler.obtainMessage(MCS_BOUND, imcs)); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | public void onServiceDisconnected(ComponentName name) { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 353 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceDisconnected"); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 354 | } |
| 355 | }; |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 356 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 357 | // Recordkeeping of restore-after-install operations that are currently in flight |
| 358 | // between the Package Manager and the Backup Manager |
| 359 | class PostInstallData { |
| 360 | public InstallArgs args; |
| 361 | public PackageInstalledInfo res; |
| 362 | |
| 363 | PostInstallData(InstallArgs _a, PackageInstalledInfo _r) { |
| 364 | args = _a; |
| 365 | res = _r; |
| 366 | } |
| 367 | }; |
| 368 | final SparseArray<PostInstallData> mRunningInstalls = new SparseArray<PostInstallData>(); |
| 369 | int mNextInstallToken = 1; // nonzero; will be wrapped back to 1 when ++ overflows |
| 370 | |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 371 | class PackageHandler extends Handler { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 372 | private boolean mBound = false; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 373 | final ArrayList<HandlerParams> mPendingInstalls = |
| 374 | new ArrayList<HandlerParams>(); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 375 | |
| 376 | private boolean connectToService() { |
| 377 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Trying to bind to" + |
| 378 | " DefaultContainerService"); |
| 379 | Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 380 | if (mContext.bindService(service, mDefContainerConn, |
| 381 | Context.BIND_AUTO_CREATE)) { |
| 382 | mBound = true; |
| 383 | return true; |
| 384 | } |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | private void disconnectService() { |
| 389 | mContainerService = null; |
| 390 | mBound = false; |
| 391 | mContext.unbindService(mDefContainerConn); |
| 392 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 393 | |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 394 | PackageHandler(Looper looper) { |
| 395 | super(looper); |
| 396 | } |
| 397 | public void handleMessage(Message msg) { |
| 398 | switch (msg.what) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 399 | case INIT_COPY: { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 400 | if (DEBUG_SD_INSTALL) Log.i(TAG, "init_copy"); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 401 | HandlerParams params = (HandlerParams) msg.obj; |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 402 | int idx = mPendingInstalls.size(); |
| 403 | if (DEBUG_SD_INSTALL) Log.i(TAG, "idx=" + idx); |
| 404 | // If a bind was already initiated we dont really |
| 405 | // need to do anything. The pending install |
| 406 | // will be processed later on. |
| 407 | if (!mBound) { |
| 408 | // If this is the only one pending we might |
| 409 | // have to bind to the service again. |
| 410 | if (!connectToService()) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 411 | Log.e(TAG, "Failed to bind to media container service"); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 412 | params.serviceError(); |
| 413 | return; |
| 414 | } else { |
| 415 | // Once we bind to the service, the first |
| 416 | // pending request will be processed. |
| 417 | mPendingInstalls.add(idx, params); |
| 418 | } |
| 419 | } else { |
| 420 | mPendingInstalls.add(idx, params); |
| 421 | // Already bound to the service. Just make |
| 422 | // sure we trigger off processing the first request. |
| 423 | if (idx == 0) { |
| 424 | mHandler.sendEmptyMessage(MCS_BOUND); |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | break; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 428 | } |
| 429 | case MCS_BOUND: { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 430 | if (DEBUG_SD_INSTALL) Log.i(TAG, "mcs_bound"); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 431 | if (msg.obj != null) { |
| 432 | mContainerService = (IMediaContainerService) msg.obj; |
| 433 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 434 | if (mContainerService == null) { |
| 435 | // Something seriously wrong. Bail out |
| 436 | Log.e(TAG, "Cannot bind to media container service"); |
| 437 | for (HandlerParams params : mPendingInstalls) { |
| 438 | mPendingInstalls.remove(0); |
| 439 | // Indicate service bind error |
| 440 | params.serviceError(); |
| 441 | } |
| 442 | mPendingInstalls.clear(); |
| 443 | } else if (mPendingInstalls.size() > 0) { |
| 444 | HandlerParams params = mPendingInstalls.get(0); |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 445 | if (params != null) { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 446 | params.startCopy(); |
| 447 | } |
| 448 | } else { |
| 449 | // Should never happen ideally. |
| 450 | Log.w(TAG, "Empty queue"); |
| 451 | } |
| 452 | break; |
| 453 | } |
| 454 | case MCS_RECONNECT : { |
| 455 | if (DEBUG_SD_INSTALL) Log.i(TAG, "mcs_reconnect"); |
| 456 | if (mPendingInstalls.size() > 0) { |
| 457 | if (mBound) { |
| 458 | disconnectService(); |
| 459 | } |
| 460 | if (!connectToService()) { |
| 461 | Log.e(TAG, "Failed to bind to media container service"); |
| 462 | for (HandlerParams params : mPendingInstalls) { |
| 463 | mPendingInstalls.remove(0); |
| 464 | // Indicate service bind error |
| 465 | params.serviceError(); |
| 466 | } |
| 467 | mPendingInstalls.clear(); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | break; |
| 471 | } |
| 472 | case MCS_UNBIND : { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 473 | if (DEBUG_SD_INSTALL) Log.i(TAG, "mcs_unbind"); |
| 474 | // Delete pending install |
| 475 | if (mPendingInstalls.size() > 0) { |
| 476 | mPendingInstalls.remove(0); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 477 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 478 | if (mPendingInstalls.size() == 0) { |
| 479 | if (mBound) { |
| 480 | disconnectService(); |
| 481 | } |
| 482 | } else { |
| 483 | // There are more pending requests in queue. |
| 484 | // Just post MCS_BOUND message to trigger processing |
| 485 | // of next pending install. |
| 486 | mHandler.sendEmptyMessage(MCS_BOUND); |
| 487 | } |
| 488 | break; |
| 489 | } |
| 490 | case MCS_GIVE_UP: { |
| 491 | if (DEBUG_SD_INSTALL) Log.i(TAG, "mcs_giveup too many retries"); |
| 492 | HandlerParams params = mPendingInstalls.remove(0); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 493 | break; |
| 494 | } |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 495 | case SEND_PENDING_BROADCAST : { |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 496 | String packages[]; |
| 497 | ArrayList components[]; |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 498 | int size = 0; |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 499 | int uids[]; |
| 500 | synchronized (mPackages) { |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 501 | if (mPendingBroadcasts == null) { |
| 502 | return; |
| 503 | } |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 504 | size = mPendingBroadcasts.size(); |
| 505 | if (size <= 0) { |
| 506 | // Nothing to be done. Just return |
| 507 | return; |
| 508 | } |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 509 | packages = new String[size]; |
| 510 | components = new ArrayList[size]; |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 511 | uids = new int[size]; |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 512 | Iterator<HashMap.Entry<String, ArrayList<String>>> |
| 513 | it = mPendingBroadcasts.entrySet().iterator(); |
| 514 | int i = 0; |
| 515 | while (it.hasNext() && i < size) { |
| 516 | HashMap.Entry<String, ArrayList<String>> ent = it.next(); |
| 517 | packages[i] = ent.getKey(); |
| 518 | components[i] = ent.getValue(); |
| 519 | PackageSetting ps = mSettings.mPackages.get(ent.getKey()); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 520 | uids[i] = (ps != null) ? ps.userId : -1; |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 521 | i++; |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 522 | } |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 523 | size = i; |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 524 | mPendingBroadcasts.clear(); |
| 525 | } |
| 526 | // Send broadcasts |
| 527 | for (int i = 0; i < size; i++) { |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 528 | sendPackageChangedBroadcast(packages[i], true, |
| 529 | (ArrayList<String>)components[i], uids[i]); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 530 | } |
| 531 | break; |
| 532 | } |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 533 | case START_CLEANING_PACKAGE: { |
| 534 | String packageName = (String)msg.obj; |
| 535 | synchronized (mPackages) { |
| 536 | if (!mSettings.mPackagesToBeCleaned.contains(packageName)) { |
| 537 | mSettings.mPackagesToBeCleaned.add(packageName); |
| 538 | } |
| 539 | } |
| 540 | startCleaningPackages(); |
| 541 | } break; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 542 | case POST_INSTALL: { |
| 543 | if (DEBUG_INSTALL) Log.v(TAG, "Handling post-install for " + msg.arg1); |
| 544 | PostInstallData data = mRunningInstalls.get(msg.arg1); |
| 545 | mRunningInstalls.delete(msg.arg1); |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 546 | boolean deleteOld = false; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 547 | |
| 548 | if (data != null) { |
| 549 | InstallArgs args = data.args; |
| 550 | PackageInstalledInfo res = data.res; |
| 551 | |
| 552 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
| 553 | res.removedInfo.sendBroadcast(false, true); |
| 554 | Bundle extras = new Bundle(1); |
| 555 | extras.putInt(Intent.EXTRA_UID, res.uid); |
| 556 | final boolean update = res.removedInfo.removedPackage != null; |
| 557 | if (update) { |
| 558 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 559 | } |
| 560 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, |
| 561 | res.pkg.applicationInfo.packageName, |
| 562 | extras); |
| 563 | if (update) { |
| 564 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, |
| 565 | res.pkg.applicationInfo.packageName, |
| 566 | extras); |
| 567 | } |
| 568 | if (res.removedInfo.args != null) { |
| 569 | // Remove the replaced package's older resources safely now |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 570 | deleteOld = true; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 571 | } |
| 572 | } |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 573 | // Force a gc to clear up things |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 574 | Runtime.getRuntime().gc(); |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 575 | // We delete after a gc for applications on sdcard. |
| 576 | if (deleteOld) { |
| 577 | synchronized (mInstallLock) { |
| 578 | res.removedInfo.args.doPostDeleteLI(true); |
| 579 | } |
| 580 | } |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 581 | if (args.observer != null) { |
| 582 | try { |
| 583 | args.observer.packageInstalled(res.name, res.returnCode); |
| 584 | } catch (RemoteException e) { |
| 585 | Log.i(TAG, "Observer no longer exists."); |
| 586 | } |
| 587 | } |
| 588 | } else { |
| 589 | Log.e(TAG, "Bogus post-install token " + msg.arg1); |
| 590 | } |
| 591 | } break; |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 595 | |
| 596 | static boolean installOnSd(int flags) { |
| 597 | if (((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) || |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 598 | ((flags & PackageManager.INSTALL_EXTERNAL) == 0)) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 599 | return false; |
| 600 | } |
| 601 | return true; |
| 602 | } |
| 603 | |
| 604 | static boolean isFwdLocked(int flags) { |
| 605 | if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) { |
| 606 | return true; |
| 607 | } |
| 608 | return false; |
| 609 | } |
| 610 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | public static final IPackageManager main(Context context, boolean factoryTest) { |
| 612 | PackageManagerService m = new PackageManagerService(context, factoryTest); |
| 613 | ServiceManager.addService("package", m); |
| 614 | return m; |
| 615 | } |
| 616 | |
| 617 | static String[] splitString(String str, char sep) { |
| 618 | int count = 1; |
| 619 | int i = 0; |
| 620 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 621 | count++; |
| 622 | i++; |
| 623 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 624 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | String[] res = new String[count]; |
| 626 | i=0; |
| 627 | count = 0; |
| 628 | int lastI=0; |
| 629 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 630 | res[count] = str.substring(lastI, i); |
| 631 | count++; |
| 632 | i++; |
| 633 | lastI = i; |
| 634 | } |
| 635 | res[count] = str.substring(lastI, str.length()); |
| 636 | return res; |
| 637 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 638 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | public PackageManagerService(Context context, boolean factoryTest) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 640 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_START, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | SystemClock.uptimeMillis()); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 642 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | if (mSdkVersion <= 0) { |
| 644 | Log.w(TAG, "**** ro.build.version.sdk not set!"); |
| 645 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 646 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | mContext = context; |
| 648 | mFactoryTest = factoryTest; |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 649 | mNoDexOpt = "eng".equals(SystemProperties.get("ro.build.type")); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | mMetrics = new DisplayMetrics(); |
| 651 | mSettings = new Settings(); |
| 652 | mSettings.addSharedUserLP("android.uid.system", |
| 653 | Process.SYSTEM_UID, ApplicationInfo.FLAG_SYSTEM); |
| 654 | mSettings.addSharedUserLP("android.uid.phone", |
| 655 | MULTIPLE_APPLICATION_UIDS |
| 656 | ? RADIO_UID : FIRST_APPLICATION_UID, |
| 657 | ApplicationInfo.FLAG_SYSTEM); |
| Mike Lockwood | d42685d | 2009-09-03 09:25:22 -0400 | [diff] [blame] | 658 | mSettings.addSharedUserLP("android.uid.log", |
| 659 | MULTIPLE_APPLICATION_UIDS |
| 660 | ? LOG_UID : FIRST_APPLICATION_UID, |
| 661 | ApplicationInfo.FLAG_SYSTEM); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | |
| 663 | String separateProcesses = SystemProperties.get("debug.separate_processes"); |
| 664 | if (separateProcesses != null && separateProcesses.length() > 0) { |
| 665 | if ("*".equals(separateProcesses)) { |
| 666 | mDefParseFlags = PackageParser.PARSE_IGNORE_PROCESSES; |
| 667 | mSeparateProcesses = null; |
| 668 | Log.w(TAG, "Running with debug.separate_processes: * (ALL)"); |
| 669 | } else { |
| 670 | mDefParseFlags = 0; |
| 671 | mSeparateProcesses = separateProcesses.split(","); |
| 672 | Log.w(TAG, "Running with debug.separate_processes: " |
| 673 | + separateProcesses); |
| 674 | } |
| 675 | } else { |
| 676 | mDefParseFlags = 0; |
| 677 | mSeparateProcesses = null; |
| 678 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 679 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | Installer installer = new Installer(); |
| 681 | // Little hacky thing to check if installd is here, to determine |
| 682 | // whether we are running on the simulator and thus need to take |
| 683 | // care of building the /data file structure ourself. |
| 684 | // (apparently the sim now has a working installer) |
| 685 | if (installer.ping() && Process.supportsProcesses()) { |
| 686 | mInstaller = installer; |
| 687 | } else { |
| 688 | mInstaller = null; |
| 689 | } |
| 690 | |
| 691 | WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); |
| 692 | Display d = wm.getDefaultDisplay(); |
| 693 | d.getMetrics(mMetrics); |
| 694 | |
| 695 | synchronized (mInstallLock) { |
| 696 | synchronized (mPackages) { |
| 697 | mHandlerThread.start(); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 698 | mHandler = new PackageHandler(mHandlerThread.getLooper()); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 699 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | File dataDir = Environment.getDataDirectory(); |
| 701 | mAppDataDir = new File(dataDir, "data"); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 702 | mSecureAppDataDir = new File(dataDir, "secure/data"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | mDrmAppPrivateInstallDir = new File(dataDir, "app-private"); |
| 704 | |
| 705 | if (mInstaller == null) { |
| 706 | // Make sure these dirs exist, when we are running in |
| 707 | // the simulator. |
| 708 | // Make a wide-open directory for random misc stuff. |
| 709 | File miscDir = new File(dataDir, "misc"); |
| 710 | miscDir.mkdirs(); |
| 711 | mAppDataDir.mkdirs(); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 712 | mSecureAppDataDir.mkdirs(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | mDrmAppPrivateInstallDir.mkdirs(); |
| 714 | } |
| 715 | |
| 716 | readPermissions(); |
| 717 | |
| 718 | mRestoredSettings = mSettings.readLP(); |
| 719 | long startTime = SystemClock.uptimeMillis(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 720 | |
| 721 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SYSTEM_SCAN_START, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | startTime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 723 | |
| Suchi Amalapurapu | daec1722 | 2010-01-14 21:25:16 -0800 | [diff] [blame] | 724 | // Set flag to monitor and not change apk file paths when |
| 725 | // scanning install directories. |
| 726 | int scanMode = SCAN_MONITOR | SCAN_NO_PATHS; |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 727 | if (mNoDexOpt) { |
| 728 | Log.w(TAG, "Running ENG build: no pre-dexopt!"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 729 | scanMode |= SCAN_NO_DEX; |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 730 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 731 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | final HashSet<String> libFiles = new HashSet<String>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 733 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | mFrameworkDir = new File(Environment.getRootDirectory(), "framework"); |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 735 | mDalvikCacheDir = new File(dataDir, "dalvik-cache"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 736 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | if (mInstaller != null) { |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 738 | boolean didDexOpt = false; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 739 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | /** |
| 741 | * Out of paranoia, ensure that everything in the boot class |
| 742 | * path has been dexed. |
| 743 | */ |
| 744 | String bootClassPath = System.getProperty("java.boot.class.path"); |
| 745 | if (bootClassPath != null) { |
| 746 | String[] paths = splitString(bootClassPath, ':'); |
| 747 | for (int i=0; i<paths.length; i++) { |
| 748 | try { |
| 749 | if (dalvik.system.DexFile.isDexOptNeeded(paths[i])) { |
| 750 | libFiles.add(paths[i]); |
| 751 | mInstaller.dexopt(paths[i], Process.SYSTEM_UID, true); |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 752 | didDexOpt = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | } |
| 754 | } catch (FileNotFoundException e) { |
| 755 | Log.w(TAG, "Boot class path not found: " + paths[i]); |
| 756 | } catch (IOException e) { |
| 757 | Log.w(TAG, "Exception reading boot class path: " + paths[i], e); |
| 758 | } |
| 759 | } |
| 760 | } else { |
| 761 | Log.w(TAG, "No BOOTCLASSPATH found!"); |
| 762 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 763 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | /** |
| 765 | * Also ensure all external libraries have had dexopt run on them. |
| 766 | */ |
| 767 | if (mSharedLibraries.size() > 0) { |
| 768 | Iterator<String> libs = mSharedLibraries.values().iterator(); |
| 769 | while (libs.hasNext()) { |
| 770 | String lib = libs.next(); |
| 771 | try { |
| 772 | if (dalvik.system.DexFile.isDexOptNeeded(lib)) { |
| 773 | libFiles.add(lib); |
| 774 | mInstaller.dexopt(lib, Process.SYSTEM_UID, true); |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 775 | didDexOpt = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | } |
| 777 | } catch (FileNotFoundException e) { |
| 778 | Log.w(TAG, "Library not found: " + lib); |
| 779 | } catch (IOException e) { |
| 780 | Log.w(TAG, "Exception reading library: " + lib, e); |
| 781 | } |
| 782 | } |
| 783 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 784 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | // Gross hack for now: we know this file doesn't contain any |
| 786 | // code, so don't dexopt it to avoid the resulting log spew. |
| 787 | libFiles.add(mFrameworkDir.getPath() + "/framework-res.apk"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 788 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | /** |
| 790 | * And there are a number of commands implemented in Java, which |
| 791 | * we currently need to do the dexopt on so that they can be |
| 792 | * run from a non-root shell. |
| 793 | */ |
| 794 | String[] frameworkFiles = mFrameworkDir.list(); |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 795 | if (frameworkFiles != null) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | for (int i=0; i<frameworkFiles.length; i++) { |
| 797 | File libPath = new File(mFrameworkDir, frameworkFiles[i]); |
| 798 | String path = libPath.getPath(); |
| 799 | // Skip the file if we alrady did it. |
| 800 | if (libFiles.contains(path)) { |
| 801 | continue; |
| 802 | } |
| 803 | // Skip the file if it is not a type we want to dexopt. |
| 804 | if (!path.endsWith(".apk") && !path.endsWith(".jar")) { |
| 805 | continue; |
| 806 | } |
| 807 | try { |
| 808 | if (dalvik.system.DexFile.isDexOptNeeded(path)) { |
| 809 | mInstaller.dexopt(path, Process.SYSTEM_UID, true); |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 810 | didDexOpt = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | } |
| 812 | } catch (FileNotFoundException e) { |
| 813 | Log.w(TAG, "Jar not found: " + path); |
| 814 | } catch (IOException e) { |
| 815 | Log.w(TAG, "Exception reading jar: " + path, e); |
| 816 | } |
| 817 | } |
| 818 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 819 | |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 820 | if (didDexOpt) { |
| 821 | // If we had to do a dexopt of one of the previous |
| 822 | // things, then something on the system has changed. |
| 823 | // Consider this significant, and wipe away all other |
| 824 | // existing dexopt files to ensure we don't leave any |
| 825 | // dangling around. |
| 826 | String[] files = mDalvikCacheDir.list(); |
| 827 | if (files != null) { |
| 828 | for (int i=0; i<files.length; i++) { |
| 829 | String fn = files[i]; |
| 830 | if (fn.startsWith("data@app@") |
| 831 | || fn.startsWith("data@app-private@")) { |
| 832 | Log.i(TAG, "Pruning dalvik file: " + fn); |
| 833 | (new File(mDalvikCacheDir, fn)).delete(); |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 839 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 840 | // Find base frameworks (resource packages without code). |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | mFrameworkInstallObserver = new AppDirObserver( |
| 842 | mFrameworkDir.getPath(), OBSERVER_EVENTS, true); |
| 843 | mFrameworkInstallObserver.startWatching(); |
| 844 | scanDirLI(mFrameworkDir, PackageParser.PARSE_IS_SYSTEM, |
| Suchi Amalapurapu | daec1722 | 2010-01-14 21:25:16 -0800 | [diff] [blame] | 845 | scanMode | SCAN_NO_DEX); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 846 | |
| 847 | // Collect all system packages. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | mSystemAppDir = new File(Environment.getRootDirectory(), "app"); |
| 849 | mSystemInstallObserver = new AppDirObserver( |
| 850 | mSystemAppDir.getPath(), OBSERVER_EVENTS, true); |
| 851 | mSystemInstallObserver.startWatching(); |
| Suchi Amalapurapu | daec1722 | 2010-01-14 21:25:16 -0800 | [diff] [blame] | 852 | scanDirLI(mSystemAppDir, PackageParser.PARSE_IS_SYSTEM, scanMode); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 853 | |
| 854 | if (mInstaller != null) { |
| 855 | if (DEBUG_UPGRADE) Log.v(TAG, "Running installd update commands"); |
| 856 | mInstaller.moveFiles(); |
| 857 | } |
| 858 | |
| 859 | // Prune any system packages that no longer exist. |
| 860 | Iterator<PackageSetting> psit = mSettings.mPackages.values().iterator(); |
| 861 | while (psit.hasNext()) { |
| 862 | PackageSetting ps = psit.next(); |
| 863 | if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0 |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 864 | && !mPackages.containsKey(ps.name) |
| 865 | && !mSettings.mDisabledSysPackages.containsKey(ps.name)) { |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 866 | psit.remove(); |
| 867 | String msg = "System package " + ps.name |
| 868 | + " no longer exists; wiping its data"; |
| 869 | reportSettingsProblem(Log.WARN, msg); |
| 870 | if (mInstaller != null) { |
| 871 | // XXX how to set useEncryptedFSDir for packages that |
| 872 | // are not encrypted? |
| 873 | mInstaller.remove(ps.name, true); |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | mAppInstallDir = new File(dataDir, "app"); |
| 879 | if (mInstaller == null) { |
| 880 | // Make sure these dirs exist, when we are running in |
| 881 | // the simulator. |
| 882 | mAppInstallDir.mkdirs(); // scanDirLI() assumes this dir exists |
| 883 | } |
| 884 | //look for any incomplete package installations |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 885 | ArrayList<PackageSetting> deletePkgsList = mSettings.getListOfIncompleteInstallPackages(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | //clean up list |
| 887 | for(int i = 0; i < deletePkgsList.size(); i++) { |
| 888 | //clean up here |
| 889 | cleanupInstallFailedPackage(deletePkgsList.get(i)); |
| 890 | } |
| 891 | //delete tmp files |
| 892 | deleteTempPackageFiles(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 893 | |
| 894 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_DATA_SCAN_START, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | SystemClock.uptimeMillis()); |
| 896 | mAppInstallObserver = new AppDirObserver( |
| 897 | mAppInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 898 | mAppInstallObserver.startWatching(); |
| 899 | scanDirLI(mAppInstallDir, 0, scanMode); |
| 900 | |
| 901 | mDrmAppInstallObserver = new AppDirObserver( |
| 902 | mDrmAppPrivateInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 903 | mDrmAppInstallObserver.startWatching(); |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 904 | scanDirLI(mDrmAppPrivateInstallDir, PackageParser.PARSE_FORWARD_LOCK, scanMode); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 906 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SCAN_END, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | SystemClock.uptimeMillis()); |
| 908 | Log.i(TAG, "Time to scan packages: " |
| 909 | + ((SystemClock.uptimeMillis()-startTime)/1000f) |
| 910 | + " seconds"); |
| 911 | |
| 912 | updatePermissionsLP(); |
| 913 | |
| 914 | mSettings.writeLP(); |
| 915 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 916 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_READY, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | SystemClock.uptimeMillis()); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 918 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | // Now after opening every single application zip, make sure they |
| 920 | // are all flushed. Not really needed, but keeps things nice and |
| 921 | // tidy. |
| 922 | Runtime.getRuntime().gc(); |
| 923 | } // synchronized (mPackages) |
| 924 | } // synchronized (mInstallLock) |
| 925 | } |
| Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 926 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | @Override |
| 928 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 929 | throws RemoteException { |
| 930 | try { |
| 931 | return super.onTransact(code, data, reply, flags); |
| 932 | } catch (RuntimeException e) { |
| 933 | if (!(e instanceof SecurityException) && !(e instanceof IllegalArgumentException)) { |
| 934 | Log.e(TAG, "Package Manager Crash", e); |
| 935 | } |
| 936 | throw e; |
| 937 | } |
| 938 | } |
| 939 | |
| Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 940 | void cleanupInstallFailedPackage(PackageSetting ps) { |
| 941 | Log.i(TAG, "Cleaning up incompletely installed app: " + ps.name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | if (mInstaller != null) { |
| Kenny Root | bdbc925 | 2010-01-28 12:03:49 -0800 | [diff] [blame] | 943 | boolean useSecureFS = useEncryptedFilesystemForPackage(ps.pkg); |
| 944 | int retCode = mInstaller.remove(ps.name, useSecureFS); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | if (retCode < 0) { |
| 946 | Log.w(TAG, "Couldn't remove app data directory for package: " |
| Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 947 | + ps.name + ", retcode=" + retCode); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | } |
| 949 | } else { |
| 950 | //for emulator |
| Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 951 | PackageParser.Package pkg = mPackages.get(ps.name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | File dataDir = new File(pkg.applicationInfo.dataDir); |
| 953 | dataDir.delete(); |
| 954 | } |
| Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 955 | if (ps.codePath != null) { |
| 956 | if (!ps.codePath.delete()) { |
| 957 | Log.w(TAG, "Unable to remove old code file: " + ps.codePath); |
| 958 | } |
| 959 | } |
| 960 | if (ps.resourcePath != null) { |
| 961 | if (!ps.resourcePath.delete() && !ps.resourcePath.equals(ps.codePath)) { |
| 962 | Log.w(TAG, "Unable to remove old code file: " + ps.resourcePath); |
| 963 | } |
| 964 | } |
| 965 | mSettings.removePackageLP(ps.name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | void readPermissions() { |
| 969 | // Read permissions from .../etc/permission directory. |
| 970 | File libraryDir = new File(Environment.getRootDirectory(), "etc/permissions"); |
| 971 | if (!libraryDir.exists() || !libraryDir.isDirectory()) { |
| 972 | Log.w(TAG, "No directory " + libraryDir + ", skipping"); |
| 973 | return; |
| 974 | } |
| 975 | if (!libraryDir.canRead()) { |
| 976 | Log.w(TAG, "Directory " + libraryDir + " cannot be read"); |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | // Iterate over the files in the directory and scan .xml files |
| 981 | for (File f : libraryDir.listFiles()) { |
| 982 | // We'll read platform.xml last |
| 983 | if (f.getPath().endsWith("etc/permissions/platform.xml")) { |
| 984 | continue; |
| 985 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 986 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | if (!f.getPath().endsWith(".xml")) { |
| 988 | Log.i(TAG, "Non-xml file " + f + " in " + libraryDir + " directory, ignoring"); |
| 989 | continue; |
| 990 | } |
| 991 | if (!f.canRead()) { |
| 992 | Log.w(TAG, "Permissions library file " + f + " cannot be read"); |
| 993 | continue; |
| 994 | } |
| 995 | |
| 996 | readPermissionsFromXml(f); |
| 997 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 998 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | // Read permissions from .../etc/permissions/platform.xml last so it will take precedence |
| 1000 | final File permFile = new File(Environment.getRootDirectory(), |
| 1001 | "etc/permissions/platform.xml"); |
| 1002 | readPermissionsFromXml(permFile); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1003 | |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1004 | StringBuilder sb = new StringBuilder(128); |
| 1005 | sb.append("Libs:"); |
| 1006 | Iterator<String> it = mSharedLibraries.keySet().iterator(); |
| 1007 | while (it.hasNext()) { |
| 1008 | sb.append(' '); |
| 1009 | String name = it.next(); |
| 1010 | sb.append(name); |
| 1011 | sb.append(':'); |
| 1012 | sb.append(mSharedLibraries.get(name)); |
| 1013 | } |
| 1014 | Log.i(TAG, sb.toString()); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1015 | |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1016 | sb.setLength(0); |
| 1017 | sb.append("Features:"); |
| 1018 | it = mAvailableFeatures.keySet().iterator(); |
| 1019 | while (it.hasNext()) { |
| 1020 | sb.append(' '); |
| 1021 | sb.append(it.next()); |
| 1022 | } |
| 1023 | Log.i(TAG, sb.toString()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1025 | |
| 1026 | private void readPermissionsFromXml(File permFile) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | FileReader permReader = null; |
| 1028 | try { |
| 1029 | permReader = new FileReader(permFile); |
| 1030 | } catch (FileNotFoundException e) { |
| 1031 | Log.w(TAG, "Couldn't find or open permissions file " + permFile); |
| 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | try { |
| 1036 | XmlPullParser parser = Xml.newPullParser(); |
| 1037 | parser.setInput(permReader); |
| 1038 | |
| 1039 | XmlUtils.beginDocument(parser, "permissions"); |
| 1040 | |
| 1041 | while (true) { |
| 1042 | XmlUtils.nextElement(parser); |
| 1043 | if (parser.getEventType() == XmlPullParser.END_DOCUMENT) { |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | String name = parser.getName(); |
| 1048 | if ("group".equals(name)) { |
| 1049 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1050 | if (gidStr != null) { |
| 1051 | int gid = Integer.parseInt(gidStr); |
| 1052 | mGlobalGids = appendInt(mGlobalGids, gid); |
| 1053 | } else { |
| 1054 | Log.w(TAG, "<group> without gid at " |
| 1055 | + parser.getPositionDescription()); |
| 1056 | } |
| 1057 | |
| 1058 | XmlUtils.skipCurrentTag(parser); |
| 1059 | continue; |
| 1060 | } else if ("permission".equals(name)) { |
| 1061 | String perm = parser.getAttributeValue(null, "name"); |
| 1062 | if (perm == null) { |
| 1063 | Log.w(TAG, "<permission> without name at " |
| 1064 | + parser.getPositionDescription()); |
| 1065 | XmlUtils.skipCurrentTag(parser); |
| 1066 | continue; |
| 1067 | } |
| 1068 | perm = perm.intern(); |
| 1069 | readPermission(parser, perm); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1070 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | } else if ("assign-permission".equals(name)) { |
| 1072 | String perm = parser.getAttributeValue(null, "name"); |
| 1073 | if (perm == null) { |
| 1074 | Log.w(TAG, "<assign-permission> without name at " |
| 1075 | + parser.getPositionDescription()); |
| 1076 | XmlUtils.skipCurrentTag(parser); |
| 1077 | continue; |
| 1078 | } |
| 1079 | String uidStr = parser.getAttributeValue(null, "uid"); |
| 1080 | if (uidStr == null) { |
| 1081 | Log.w(TAG, "<assign-permission> without uid at " |
| 1082 | + parser.getPositionDescription()); |
| 1083 | XmlUtils.skipCurrentTag(parser); |
| 1084 | continue; |
| 1085 | } |
| 1086 | int uid = Process.getUidForName(uidStr); |
| 1087 | if (uid < 0) { |
| 1088 | Log.w(TAG, "<assign-permission> with unknown uid \"" |
| 1089 | + uidStr + "\" at " |
| 1090 | + parser.getPositionDescription()); |
| 1091 | XmlUtils.skipCurrentTag(parser); |
| 1092 | continue; |
| 1093 | } |
| 1094 | perm = perm.intern(); |
| 1095 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 1096 | if (perms == null) { |
| 1097 | perms = new HashSet<String>(); |
| 1098 | mSystemPermissions.put(uid, perms); |
| 1099 | } |
| 1100 | perms.add(perm); |
| 1101 | XmlUtils.skipCurrentTag(parser); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1102 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | } else if ("library".equals(name)) { |
| 1104 | String lname = parser.getAttributeValue(null, "name"); |
| 1105 | String lfile = parser.getAttributeValue(null, "file"); |
| 1106 | if (lname == null) { |
| 1107 | Log.w(TAG, "<library> without name at " |
| 1108 | + parser.getPositionDescription()); |
| 1109 | } else if (lfile == null) { |
| 1110 | Log.w(TAG, "<library> without file at " |
| 1111 | + parser.getPositionDescription()); |
| 1112 | } else { |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1113 | //Log.i(TAG, "Got library " + lname + " in " + lfile); |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1114 | mSharedLibraries.put(lname, lfile); |
| 1115 | } |
| 1116 | XmlUtils.skipCurrentTag(parser); |
| 1117 | continue; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1118 | |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1119 | } else if ("feature".equals(name)) { |
| 1120 | String fname = parser.getAttributeValue(null, "name"); |
| 1121 | if (fname == null) { |
| 1122 | Log.w(TAG, "<feature> without name at " |
| 1123 | + parser.getPositionDescription()); |
| 1124 | } else { |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1125 | //Log.i(TAG, "Got feature " + fname); |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1126 | FeatureInfo fi = new FeatureInfo(); |
| 1127 | fi.name = fname; |
| 1128 | mAvailableFeatures.put(fname, fi); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | } |
| 1130 | XmlUtils.skipCurrentTag(parser); |
| 1131 | continue; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1132 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | } else { |
| 1134 | XmlUtils.skipCurrentTag(parser); |
| 1135 | continue; |
| 1136 | } |
| 1137 | |
| 1138 | } |
| 1139 | } catch (XmlPullParserException e) { |
| 1140 | Log.w(TAG, "Got execption parsing permissions.", e); |
| 1141 | } catch (IOException e) { |
| 1142 | Log.w(TAG, "Got execption parsing permissions.", e); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | void readPermission(XmlPullParser parser, String name) |
| 1147 | throws IOException, XmlPullParserException { |
| 1148 | |
| 1149 | name = name.intern(); |
| 1150 | |
| 1151 | BasePermission bp = mSettings.mPermissions.get(name); |
| 1152 | if (bp == null) { |
| 1153 | bp = new BasePermission(name, null, BasePermission.TYPE_BUILTIN); |
| 1154 | mSettings.mPermissions.put(name, bp); |
| 1155 | } |
| 1156 | int outerDepth = parser.getDepth(); |
| 1157 | int type; |
| 1158 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 1159 | && (type != XmlPullParser.END_TAG |
| 1160 | || parser.getDepth() > outerDepth)) { |
| 1161 | if (type == XmlPullParser.END_TAG |
| 1162 | || type == XmlPullParser.TEXT) { |
| 1163 | continue; |
| 1164 | } |
| 1165 | |
| 1166 | String tagName = parser.getName(); |
| 1167 | if ("group".equals(tagName)) { |
| 1168 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1169 | if (gidStr != null) { |
| 1170 | int gid = Process.getGidForName(gidStr); |
| 1171 | bp.gids = appendInt(bp.gids, gid); |
| 1172 | } else { |
| 1173 | Log.w(TAG, "<group> without gid at " |
| 1174 | + parser.getPositionDescription()); |
| 1175 | } |
| 1176 | } |
| 1177 | XmlUtils.skipCurrentTag(parser); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | static int[] appendInt(int[] cur, int val) { |
| 1182 | if (cur == null) { |
| 1183 | return new int[] { val }; |
| 1184 | } |
| 1185 | final int N = cur.length; |
| 1186 | for (int i=0; i<N; i++) { |
| 1187 | if (cur[i] == val) { |
| 1188 | return cur; |
| 1189 | } |
| 1190 | } |
| 1191 | int[] ret = new int[N+1]; |
| 1192 | System.arraycopy(cur, 0, ret, 0, N); |
| 1193 | ret[N] = val; |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
| 1197 | static int[] appendInts(int[] cur, int[] add) { |
| 1198 | if (add == null) return cur; |
| 1199 | if (cur == null) return add; |
| 1200 | final int N = add.length; |
| 1201 | for (int i=0; i<N; i++) { |
| 1202 | cur = appendInt(cur, add[i]); |
| 1203 | } |
| 1204 | return cur; |
| 1205 | } |
| 1206 | |
| 1207 | PackageInfo generatePackageInfo(PackageParser.Package p, int flags) { |
| Suchi Amalapurapu | b897cff | 2009-10-14 12:11:48 -0700 | [diff] [blame] | 1208 | if ((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
| 1209 | // The package has been uninstalled but has retained data and resources. |
| 1210 | return PackageParser.generatePackageInfo(p, null, flags); |
| 1211 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | final PackageSetting ps = (PackageSetting)p.mExtras; |
| 1213 | if (ps == null) { |
| 1214 | return null; |
| 1215 | } |
| 1216 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 1217 | return PackageParser.generatePackageInfo(p, gp.gids, flags); |
| 1218 | } |
| 1219 | |
| 1220 | public PackageInfo getPackageInfo(String packageName, int flags) { |
| 1221 | synchronized (mPackages) { |
| 1222 | PackageParser.Package p = mPackages.get(packageName); |
| 1223 | if (Config.LOGV) Log.v( |
| Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 1224 | TAG, "getPackageInfo " + packageName |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1225 | + ": " + p); |
| 1226 | if (p != null) { |
| 1227 | return generatePackageInfo(p, flags); |
| 1228 | } |
| 1229 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
| 1230 | return generatePackageInfoFromSettingsLP(packageName, flags); |
| 1231 | } |
| 1232 | } |
| 1233 | return null; |
| 1234 | } |
| 1235 | |
| Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1236 | public String[] currentToCanonicalPackageNames(String[] names) { |
| 1237 | String[] out = new String[names.length]; |
| 1238 | synchronized (mPackages) { |
| 1239 | for (int i=names.length-1; i>=0; i--) { |
| 1240 | PackageSetting ps = mSettings.mPackages.get(names[i]); |
| 1241 | out[i] = ps != null && ps.realName != null ? ps.realName : names[i]; |
| 1242 | } |
| 1243 | } |
| 1244 | return out; |
| 1245 | } |
| 1246 | |
| 1247 | public String[] canonicalToCurrentPackageNames(String[] names) { |
| 1248 | String[] out = new String[names.length]; |
| 1249 | synchronized (mPackages) { |
| 1250 | for (int i=names.length-1; i>=0; i--) { |
| 1251 | String cur = mSettings.mRenamedPackages.get(names[i]); |
| 1252 | out[i] = cur != null ? cur : names[i]; |
| 1253 | } |
| 1254 | } |
| 1255 | return out; |
| 1256 | } |
| 1257 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | public int getPackageUid(String packageName) { |
| 1259 | synchronized (mPackages) { |
| 1260 | PackageParser.Package p = mPackages.get(packageName); |
| 1261 | if(p != null) { |
| 1262 | return p.applicationInfo.uid; |
| 1263 | } |
| 1264 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1265 | if((ps == null) || (ps.pkg == null) || (ps.pkg.applicationInfo == null)) { |
| 1266 | return -1; |
| 1267 | } |
| 1268 | p = ps.pkg; |
| 1269 | return p != null ? p.applicationInfo.uid : -1; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | public int[] getPackageGids(String packageName) { |
| 1274 | synchronized (mPackages) { |
| 1275 | PackageParser.Package p = mPackages.get(packageName); |
| 1276 | if (Config.LOGV) Log.v( |
| Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 1277 | TAG, "getPackageGids" + packageName |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | + ": " + p); |
| 1279 | if (p != null) { |
| 1280 | final PackageSetting ps = (PackageSetting)p.mExtras; |
| 1281 | final SharedUserSetting suid = ps.sharedUser; |
| 1282 | return suid != null ? suid.gids : ps.gids; |
| 1283 | } |
| 1284 | } |
| 1285 | // stupid thing to indicate an error. |
| 1286 | return new int[0]; |
| 1287 | } |
| 1288 | |
| 1289 | public PermissionInfo getPermissionInfo(String name, int flags) { |
| 1290 | synchronized (mPackages) { |
| 1291 | final BasePermission p = mSettings.mPermissions.get(name); |
| 1292 | if (p != null && p.perm != null) { |
| 1293 | return PackageParser.generatePermissionInfo(p.perm, flags); |
| 1294 | } |
| 1295 | return null; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | public List<PermissionInfo> queryPermissionsByGroup(String group, int flags) { |
| 1300 | synchronized (mPackages) { |
| 1301 | ArrayList<PermissionInfo> out = new ArrayList<PermissionInfo>(10); |
| 1302 | for (BasePermission p : mSettings.mPermissions.values()) { |
| 1303 | if (group == null) { |
| 1304 | if (p.perm.info.group == null) { |
| 1305 | out.add(PackageParser.generatePermissionInfo(p.perm, flags)); |
| 1306 | } |
| 1307 | } else { |
| 1308 | if (group.equals(p.perm.info.group)) { |
| 1309 | out.add(PackageParser.generatePermissionInfo(p.perm, flags)); |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1313 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | if (out.size() > 0) { |
| 1315 | return out; |
| 1316 | } |
| 1317 | return mPermissionGroups.containsKey(group) ? out : null; |
| 1318 | } |
| 1319 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1320 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | public PermissionGroupInfo getPermissionGroupInfo(String name, int flags) { |
| 1322 | synchronized (mPackages) { |
| 1323 | return PackageParser.generatePermissionGroupInfo( |
| 1324 | mPermissionGroups.get(name), flags); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | public List<PermissionGroupInfo> getAllPermissionGroups(int flags) { |
| 1329 | synchronized (mPackages) { |
| 1330 | final int N = mPermissionGroups.size(); |
| 1331 | ArrayList<PermissionGroupInfo> out |
| 1332 | = new ArrayList<PermissionGroupInfo>(N); |
| 1333 | for (PackageParser.PermissionGroup pg : mPermissionGroups.values()) { |
| 1334 | out.add(PackageParser.generatePermissionGroupInfo(pg, flags)); |
| 1335 | } |
| 1336 | return out; |
| 1337 | } |
| 1338 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1339 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1340 | private ApplicationInfo generateApplicationInfoFromSettingsLP(String packageName, int flags) { |
| 1341 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1342 | if(ps != null) { |
| 1343 | if(ps.pkg == null) { |
| 1344 | PackageInfo pInfo = generatePackageInfoFromSettingsLP(packageName, flags); |
| 1345 | if(pInfo != null) { |
| 1346 | return pInfo.applicationInfo; |
| 1347 | } |
| 1348 | return null; |
| 1349 | } |
| 1350 | return PackageParser.generateApplicationInfo(ps.pkg, flags); |
| 1351 | } |
| 1352 | return null; |
| 1353 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1354 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | private PackageInfo generatePackageInfoFromSettingsLP(String packageName, int flags) { |
| 1356 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1357 | if(ps != null) { |
| 1358 | if(ps.pkg == null) { |
| 1359 | ps.pkg = new PackageParser.Package(packageName); |
| 1360 | ps.pkg.applicationInfo.packageName = packageName; |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 1361 | ps.pkg.applicationInfo.flags = ps.pkgFlags; |
| 1362 | ps.pkg.applicationInfo.publicSourceDir = ps.resourcePathString; |
| 1363 | ps.pkg.applicationInfo.sourceDir = ps.codePathString; |
| 1364 | ps.pkg.applicationInfo.dataDir = getDataPathForPackage(ps.pkg).getPath(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | } |
| 1366 | return generatePackageInfo(ps.pkg, flags); |
| 1367 | } |
| 1368 | return null; |
| 1369 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1370 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1371 | public ApplicationInfo getApplicationInfo(String packageName, int flags) { |
| 1372 | synchronized (mPackages) { |
| 1373 | PackageParser.Package p = mPackages.get(packageName); |
| 1374 | if (Config.LOGV) Log.v( |
| 1375 | TAG, "getApplicationInfo " + packageName |
| 1376 | + ": " + p); |
| 1377 | if (p != null) { |
| 1378 | // Note: isEnabledLP() does not apply here - always return info |
| Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 1379 | return PackageParser.generateApplicationInfo(p, flags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | } |
| 1381 | if ("android".equals(packageName)||"system".equals(packageName)) { |
| 1382 | return mAndroidApplication; |
| 1383 | } |
| 1384 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
| 1385 | return generateApplicationInfoFromSettingsLP(packageName, flags); |
| 1386 | } |
| 1387 | } |
| 1388 | return null; |
| 1389 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1390 | |
| 1391 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | public void freeStorageAndNotify(final long freeStorageSize, final IPackageDataObserver observer) { |
| 1393 | mContext.enforceCallingOrSelfPermission( |
| 1394 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1395 | // Queue up an async operation since clearing cache may take a little while. |
| 1396 | mHandler.post(new Runnable() { |
| 1397 | public void run() { |
| 1398 | mHandler.removeCallbacks(this); |
| 1399 | int retCode = -1; |
| 1400 | if (mInstaller != null) { |
| 1401 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1402 | if (retCode < 0) { |
| 1403 | Log.w(TAG, "Couldn't clear application caches"); |
| 1404 | } |
| 1405 | } //end if mInstaller |
| 1406 | if (observer != null) { |
| 1407 | try { |
| 1408 | observer.onRemoveCompleted(null, (retCode >= 0)); |
| 1409 | } catch (RemoteException e) { |
| 1410 | Log.w(TAG, "RemoveException when invoking call back"); |
| 1411 | } |
| 1412 | } |
| 1413 | } |
| 1414 | }); |
| 1415 | } |
| 1416 | |
| Suchi Amalapurapu | bc806f6 | 2009-06-17 15:18:19 -0700 | [diff] [blame] | 1417 | public void freeStorage(final long freeStorageSize, final IntentSender pi) { |
| Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1418 | mContext.enforceCallingOrSelfPermission( |
| 1419 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1420 | // Queue up an async operation since clearing cache may take a little while. |
| 1421 | mHandler.post(new Runnable() { |
| 1422 | public void run() { |
| 1423 | mHandler.removeCallbacks(this); |
| 1424 | int retCode = -1; |
| 1425 | if (mInstaller != null) { |
| 1426 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1427 | if (retCode < 0) { |
| 1428 | Log.w(TAG, "Couldn't clear application caches"); |
| 1429 | } |
| 1430 | } |
| 1431 | if(pi != null) { |
| 1432 | try { |
| 1433 | // Callback via pending intent |
| 1434 | int code = (retCode >= 0) ? 1 : 0; |
| 1435 | pi.sendIntent(null, code, null, |
| 1436 | null, null); |
| 1437 | } catch (SendIntentException e1) { |
| 1438 | Log.i(TAG, "Failed to send pending intent"); |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | }); |
| 1443 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1444 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 | public ActivityInfo getActivityInfo(ComponentName component, int flags) { |
| 1446 | synchronized (mPackages) { |
| 1447 | PackageParser.Activity a = mActivities.mActivities.get(component); |
| Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 1448 | |
| 1449 | if (Config.LOGV) Log.v(TAG, "getActivityInfo " + component + ": " + a); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | if (a != null && mSettings.isEnabledLP(a.info, flags)) { |
| Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 1451 | return PackageParser.generateActivityInfo(a, flags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | } |
| 1453 | if (mResolveComponentName.equals(component)) { |
| 1454 | return mResolveActivity; |
| 1455 | } |
| 1456 | } |
| 1457 | return null; |
| 1458 | } |
| 1459 | |
| 1460 | public ActivityInfo getReceiverInfo(ComponentName component, int flags) { |
| 1461 | synchronized (mPackages) { |
| 1462 | PackageParser.Activity a = mReceivers.mActivities.get(component); |
| 1463 | if (Config.LOGV) Log.v( |
| 1464 | TAG, "getReceiverInfo " + component + ": " + a); |
| 1465 | if (a != null && mSettings.isEnabledLP(a.info, flags)) { |
| 1466 | return PackageParser.generateActivityInfo(a, flags); |
| 1467 | } |
| 1468 | } |
| 1469 | return null; |
| 1470 | } |
| 1471 | |
| 1472 | public ServiceInfo getServiceInfo(ComponentName component, int flags) { |
| 1473 | synchronized (mPackages) { |
| 1474 | PackageParser.Service s = mServices.mServices.get(component); |
| 1475 | if (Config.LOGV) Log.v( |
| 1476 | TAG, "getServiceInfo " + component + ": " + s); |
| 1477 | if (s != null && mSettings.isEnabledLP(s.info, flags)) { |
| 1478 | return PackageParser.generateServiceInfo(s, flags); |
| 1479 | } |
| 1480 | } |
| 1481 | return null; |
| 1482 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1483 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | public String[] getSystemSharedLibraryNames() { |
| 1485 | Set<String> libSet; |
| 1486 | synchronized (mPackages) { |
| 1487 | libSet = mSharedLibraries.keySet(); |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1488 | int size = libSet.size(); |
| 1489 | if (size > 0) { |
| 1490 | String[] libs = new String[size]; |
| 1491 | libSet.toArray(libs); |
| 1492 | return libs; |
| 1493 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1494 | } |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1495 | return null; |
| 1496 | } |
| 1497 | |
| 1498 | public FeatureInfo[] getSystemAvailableFeatures() { |
| 1499 | Collection<FeatureInfo> featSet; |
| 1500 | synchronized (mPackages) { |
| 1501 | featSet = mAvailableFeatures.values(); |
| 1502 | int size = featSet.size(); |
| 1503 | if (size > 0) { |
| 1504 | FeatureInfo[] features = new FeatureInfo[size+1]; |
| 1505 | featSet.toArray(features); |
| 1506 | FeatureInfo fi = new FeatureInfo(); |
| 1507 | fi.reqGlEsVersion = SystemProperties.getInt("ro.opengles.version", |
| 1508 | FeatureInfo.GL_ES_VERSION_UNDEFINED); |
| 1509 | features[size] = fi; |
| 1510 | return features; |
| 1511 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1512 | } |
| 1513 | return null; |
| 1514 | } |
| 1515 | |
| Dianne Hackborn | 039c68e | 2009-09-26 16:39:23 -0700 | [diff] [blame] | 1516 | public boolean hasSystemFeature(String name) { |
| 1517 | synchronized (mPackages) { |
| 1518 | return mAvailableFeatures.containsKey(name); |
| 1519 | } |
| 1520 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1521 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1522 | public int checkPermission(String permName, String pkgName) { |
| 1523 | synchronized (mPackages) { |
| 1524 | PackageParser.Package p = mPackages.get(pkgName); |
| 1525 | if (p != null && p.mExtras != null) { |
| 1526 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 1527 | if (ps.sharedUser != null) { |
| 1528 | if (ps.sharedUser.grantedPermissions.contains(permName)) { |
| 1529 | return PackageManager.PERMISSION_GRANTED; |
| 1530 | } |
| 1531 | } else if (ps.grantedPermissions.contains(permName)) { |
| 1532 | return PackageManager.PERMISSION_GRANTED; |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | return PackageManager.PERMISSION_DENIED; |
| 1537 | } |
| 1538 | |
| 1539 | public int checkUidPermission(String permName, int uid) { |
| 1540 | synchronized (mPackages) { |
| 1541 | Object obj = mSettings.getUserIdLP(uid); |
| 1542 | if (obj != null) { |
| 1543 | if (obj instanceof SharedUserSetting) { |
| 1544 | SharedUserSetting sus = (SharedUserSetting)obj; |
| 1545 | if (sus.grantedPermissions.contains(permName)) { |
| 1546 | return PackageManager.PERMISSION_GRANTED; |
| 1547 | } |
| 1548 | } else if (obj instanceof PackageSetting) { |
| 1549 | PackageSetting ps = (PackageSetting)obj; |
| 1550 | if (ps.grantedPermissions.contains(permName)) { |
| 1551 | return PackageManager.PERMISSION_GRANTED; |
| 1552 | } |
| 1553 | } |
| 1554 | } else { |
| 1555 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 1556 | if (perms != null && perms.contains(permName)) { |
| 1557 | return PackageManager.PERMISSION_GRANTED; |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | return PackageManager.PERMISSION_DENIED; |
| 1562 | } |
| 1563 | |
| 1564 | private BasePermission findPermissionTreeLP(String permName) { |
| 1565 | for(BasePermission bp : mSettings.mPermissionTrees.values()) { |
| 1566 | if (permName.startsWith(bp.name) && |
| 1567 | permName.length() > bp.name.length() && |
| 1568 | permName.charAt(bp.name.length()) == '.') { |
| 1569 | return bp; |
| 1570 | } |
| 1571 | } |
| 1572 | return null; |
| 1573 | } |
| 1574 | |
| 1575 | private BasePermission checkPermissionTreeLP(String permName) { |
| 1576 | if (permName != null) { |
| 1577 | BasePermission bp = findPermissionTreeLP(permName); |
| 1578 | if (bp != null) { |
| 1579 | if (bp.uid == Binder.getCallingUid()) { |
| 1580 | return bp; |
| 1581 | } |
| 1582 | throw new SecurityException("Calling uid " |
| 1583 | + Binder.getCallingUid() |
| 1584 | + " is not allowed to add to permission tree " |
| 1585 | + bp.name + " owned by uid " + bp.uid); |
| 1586 | } |
| 1587 | } |
| 1588 | throw new SecurityException("No permission tree found for " + permName); |
| 1589 | } |
| 1590 | |
| 1591 | public boolean addPermission(PermissionInfo info) { |
| 1592 | synchronized (mPackages) { |
| 1593 | if (info.labelRes == 0 && info.nonLocalizedLabel == null) { |
| 1594 | throw new SecurityException("Label must be specified in permission"); |
| 1595 | } |
| 1596 | BasePermission tree = checkPermissionTreeLP(info.name); |
| 1597 | BasePermission bp = mSettings.mPermissions.get(info.name); |
| 1598 | boolean added = bp == null; |
| 1599 | if (added) { |
| 1600 | bp = new BasePermission(info.name, tree.sourcePackage, |
| 1601 | BasePermission.TYPE_DYNAMIC); |
| 1602 | } else if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 1603 | throw new SecurityException( |
| 1604 | "Not allowed to modify non-dynamic permission " |
| 1605 | + info.name); |
| 1606 | } |
| 1607 | bp.perm = new PackageParser.Permission(tree.perm.owner, |
| 1608 | new PermissionInfo(info)); |
| 1609 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 1610 | bp.uid = tree.uid; |
| 1611 | if (added) { |
| 1612 | mSettings.mPermissions.put(info.name, bp); |
| 1613 | } |
| 1614 | mSettings.writeLP(); |
| 1615 | return added; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | public void removePermission(String name) { |
| 1620 | synchronized (mPackages) { |
| 1621 | checkPermissionTreeLP(name); |
| 1622 | BasePermission bp = mSettings.mPermissions.get(name); |
| 1623 | if (bp != null) { |
| 1624 | if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 1625 | throw new SecurityException( |
| 1626 | "Not allowed to modify non-dynamic permission " |
| 1627 | + name); |
| 1628 | } |
| 1629 | mSettings.mPermissions.remove(name); |
| 1630 | mSettings.writeLP(); |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | |
| Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 1635 | public boolean isProtectedBroadcast(String actionName) { |
| 1636 | synchronized (mPackages) { |
| 1637 | return mProtectedBroadcasts.contains(actionName); |
| 1638 | } |
| 1639 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1640 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | public int checkSignatures(String pkg1, String pkg2) { |
| 1642 | synchronized (mPackages) { |
| 1643 | PackageParser.Package p1 = mPackages.get(pkg1); |
| 1644 | PackageParser.Package p2 = mPackages.get(pkg2); |
| 1645 | if (p1 == null || p1.mExtras == null |
| 1646 | || p2 == null || p2.mExtras == null) { |
| 1647 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 1648 | } |
| Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 1649 | return checkSignaturesLP(p1.mSignatures, p2.mSignatures); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | } |
| 1651 | } |
| 1652 | |
| Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 1653 | public int checkUidSignatures(int uid1, int uid2) { |
| 1654 | synchronized (mPackages) { |
| 1655 | Signature[] s1; |
| 1656 | Signature[] s2; |
| 1657 | Object obj = mSettings.getUserIdLP(uid1); |
| 1658 | if (obj != null) { |
| 1659 | if (obj instanceof SharedUserSetting) { |
| 1660 | s1 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 1661 | } else if (obj instanceof PackageSetting) { |
| 1662 | s1 = ((PackageSetting)obj).signatures.mSignatures; |
| 1663 | } else { |
| 1664 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 1665 | } |
| 1666 | } else { |
| 1667 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 1668 | } |
| 1669 | obj = mSettings.getUserIdLP(uid2); |
| 1670 | if (obj != null) { |
| 1671 | if (obj instanceof SharedUserSetting) { |
| 1672 | s2 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 1673 | } else if (obj instanceof PackageSetting) { |
| 1674 | s2 = ((PackageSetting)obj).signatures.mSignatures; |
| 1675 | } else { |
| 1676 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 1677 | } |
| 1678 | } else { |
| 1679 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 1680 | } |
| 1681 | return checkSignaturesLP(s1, s2); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | int checkSignaturesLP(Signature[] s1, Signature[] s2) { |
| 1686 | if (s1 == null) { |
| 1687 | return s2 == null |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | ? PackageManager.SIGNATURE_NEITHER_SIGNED |
| 1689 | : PackageManager.SIGNATURE_FIRST_NOT_SIGNED; |
| 1690 | } |
| Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 1691 | if (s2 == null) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1692 | return PackageManager.SIGNATURE_SECOND_NOT_SIGNED; |
| 1693 | } |
| Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 1694 | final int N1 = s1.length; |
| 1695 | final int N2 = s2.length; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | for (int i=0; i<N1; i++) { |
| 1697 | boolean match = false; |
| 1698 | for (int j=0; j<N2; j++) { |
| Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 1699 | if (s1[i].equals(s2[j])) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1700 | match = true; |
| 1701 | break; |
| 1702 | } |
| 1703 | } |
| 1704 | if (!match) { |
| 1705 | return PackageManager.SIGNATURE_NO_MATCH; |
| 1706 | } |
| 1707 | } |
| 1708 | return PackageManager.SIGNATURE_MATCH; |
| 1709 | } |
| 1710 | |
| 1711 | public String[] getPackagesForUid(int uid) { |
| 1712 | synchronized (mPackages) { |
| 1713 | Object obj = mSettings.getUserIdLP(uid); |
| 1714 | if (obj instanceof SharedUserSetting) { |
| 1715 | SharedUserSetting sus = (SharedUserSetting)obj; |
| 1716 | final int N = sus.packages.size(); |
| 1717 | String[] res = new String[N]; |
| 1718 | Iterator<PackageSetting> it = sus.packages.iterator(); |
| 1719 | int i=0; |
| 1720 | while (it.hasNext()) { |
| 1721 | res[i++] = it.next().name; |
| 1722 | } |
| 1723 | return res; |
| 1724 | } else if (obj instanceof PackageSetting) { |
| 1725 | PackageSetting ps = (PackageSetting)obj; |
| 1726 | return new String[] { ps.name }; |
| 1727 | } |
| 1728 | } |
| 1729 | return null; |
| 1730 | } |
| 1731 | |
| 1732 | public String getNameForUid(int uid) { |
| 1733 | synchronized (mPackages) { |
| 1734 | Object obj = mSettings.getUserIdLP(uid); |
| 1735 | if (obj instanceof SharedUserSetting) { |
| 1736 | SharedUserSetting sus = (SharedUserSetting)obj; |
| 1737 | return sus.name + ":" + sus.userId; |
| 1738 | } else if (obj instanceof PackageSetting) { |
| 1739 | PackageSetting ps = (PackageSetting)obj; |
| 1740 | return ps.name; |
| 1741 | } |
| 1742 | } |
| 1743 | return null; |
| 1744 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1745 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | public int getUidForSharedUser(String sharedUserName) { |
| 1747 | if(sharedUserName == null) { |
| 1748 | return -1; |
| 1749 | } |
| 1750 | synchronized (mPackages) { |
| 1751 | SharedUserSetting suid = mSettings.getSharedUserLP(sharedUserName, 0, false); |
| 1752 | if(suid == null) { |
| 1753 | return -1; |
| 1754 | } |
| 1755 | return suid.userId; |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | public ResolveInfo resolveIntent(Intent intent, String resolvedType, |
| 1760 | int flags) { |
| 1761 | List<ResolveInfo> query = queryIntentActivities(intent, resolvedType, flags); |
| Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 1762 | return chooseBestActivity(intent, resolvedType, flags, query); |
| 1763 | } |
| 1764 | |
| Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 1765 | private ResolveInfo chooseBestActivity(Intent intent, String resolvedType, |
| 1766 | int flags, List<ResolveInfo> query) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1767 | if (query != null) { |
| 1768 | final int N = query.size(); |
| 1769 | if (N == 1) { |
| 1770 | return query.get(0); |
| 1771 | } else if (N > 1) { |
| 1772 | // If there is more than one activity with the same priority, |
| 1773 | // then let the user decide between them. |
| 1774 | ResolveInfo r0 = query.get(0); |
| 1775 | ResolveInfo r1 = query.get(1); |
| 1776 | if (false) { |
| 1777 | System.out.println(r0.activityInfo.name + |
| 1778 | "=" + r0.priority + " vs " + |
| 1779 | r1.activityInfo.name + |
| 1780 | "=" + r1.priority); |
| 1781 | } |
| 1782 | // If the first activity has a higher priority, or a different |
| 1783 | // default, then it is always desireable to pick it. |
| 1784 | if (r0.priority != r1.priority |
| 1785 | || r0.preferredOrder != r1.preferredOrder |
| 1786 | || r0.isDefault != r1.isDefault) { |
| 1787 | return query.get(0); |
| 1788 | } |
| 1789 | // If we have saved a preference for a preferred activity for |
| 1790 | // this Intent, use that. |
| 1791 | ResolveInfo ri = findPreferredActivity(intent, resolvedType, |
| 1792 | flags, query, r0.priority); |
| 1793 | if (ri != null) { |
| 1794 | return ri; |
| 1795 | } |
| 1796 | return mResolveInfo; |
| 1797 | } |
| 1798 | } |
| 1799 | return null; |
| 1800 | } |
| 1801 | |
| 1802 | ResolveInfo findPreferredActivity(Intent intent, String resolvedType, |
| 1803 | int flags, List<ResolveInfo> query, int priority) { |
| 1804 | synchronized (mPackages) { |
| 1805 | if (DEBUG_PREFERRED) intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION); |
| 1806 | List<PreferredActivity> prefs = |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 1807 | mSettings.mPreferredActivities.queryIntent(intent, resolvedType, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1808 | (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0); |
| 1809 | if (prefs != null && prefs.size() > 0) { |
| 1810 | // First figure out how good the original match set is. |
| 1811 | // We will only allow preferred activities that came |
| 1812 | // from the same match quality. |
| 1813 | int match = 0; |
| 1814 | final int N = query.size(); |
| 1815 | if (DEBUG_PREFERRED) Log.v(TAG, "Figuring out best match..."); |
| 1816 | for (int j=0; j<N; j++) { |
| 1817 | ResolveInfo ri = query.get(j); |
| 1818 | if (DEBUG_PREFERRED) Log.v(TAG, "Match for " + ri.activityInfo |
| 1819 | + ": 0x" + Integer.toHexString(match)); |
| 1820 | if (ri.match > match) match = ri.match; |
| 1821 | } |
| 1822 | if (DEBUG_PREFERRED) Log.v(TAG, "Best match: 0x" |
| 1823 | + Integer.toHexString(match)); |
| 1824 | match &= IntentFilter.MATCH_CATEGORY_MASK; |
| 1825 | final int M = prefs.size(); |
| 1826 | for (int i=0; i<M; i++) { |
| 1827 | PreferredActivity pa = prefs.get(i); |
| 1828 | if (pa.mMatch != match) { |
| 1829 | continue; |
| 1830 | } |
| 1831 | ActivityInfo ai = getActivityInfo(pa.mActivity, flags); |
| 1832 | if (DEBUG_PREFERRED) { |
| 1833 | Log.v(TAG, "Got preferred activity:"); |
| 1834 | ai.dump(new LogPrinter(Log.INFO, TAG), " "); |
| 1835 | } |
| 1836 | if (ai != null) { |
| 1837 | for (int j=0; j<N; j++) { |
| 1838 | ResolveInfo ri = query.get(j); |
| 1839 | if (!ri.activityInfo.applicationInfo.packageName |
| 1840 | .equals(ai.applicationInfo.packageName)) { |
| 1841 | continue; |
| 1842 | } |
| 1843 | if (!ri.activityInfo.name.equals(ai.name)) { |
| 1844 | continue; |
| 1845 | } |
| 1846 | |
| 1847 | // Okay we found a previously set preferred app. |
| 1848 | // If the result set is different from when this |
| 1849 | // was created, we need to clear it and re-ask the |
| 1850 | // user their preference. |
| 1851 | if (!pa.sameSet(query, priority)) { |
| 1852 | Log.i(TAG, "Result set changed, dropping preferred activity for " |
| 1853 | + intent + " type " + resolvedType); |
| 1854 | mSettings.mPreferredActivities.removeFilter(pa); |
| 1855 | return null; |
| 1856 | } |
| 1857 | |
| 1858 | // Yay! |
| 1859 | return ri; |
| 1860 | } |
| 1861 | } |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | return null; |
| 1866 | } |
| 1867 | |
| 1868 | public List<ResolveInfo> queryIntentActivities(Intent intent, |
| 1869 | String resolvedType, int flags) { |
| 1870 | ComponentName comp = intent.getComponent(); |
| 1871 | if (comp != null) { |
| 1872 | List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
| 1873 | ActivityInfo ai = getActivityInfo(comp, flags); |
| 1874 | if (ai != null) { |
| 1875 | ResolveInfo ri = new ResolveInfo(); |
| 1876 | ri.activityInfo = ai; |
| 1877 | list.add(ri); |
| 1878 | } |
| 1879 | return list; |
| 1880 | } |
| 1881 | |
| 1882 | synchronized (mPackages) { |
| Dianne Hackborn | c14b9ccd | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 1883 | String pkgName = intent.getPackage(); |
| 1884 | if (pkgName == null) { |
| 1885 | return (List<ResolveInfo>)mActivities.queryIntent(intent, |
| 1886 | resolvedType, flags); |
| 1887 | } |
| 1888 | PackageParser.Package pkg = mPackages.get(pkgName); |
| 1889 | if (pkg != null) { |
| 1890 | return (List<ResolveInfo>) mActivities.queryIntentForPackage(intent, |
| 1891 | resolvedType, flags, pkg.activities); |
| 1892 | } |
| 1893 | return null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller, |
| 1898 | Intent[] specifics, String[] specificTypes, Intent intent, |
| 1899 | String resolvedType, int flags) { |
| 1900 | final String resultsAction = intent.getAction(); |
| 1901 | |
| 1902 | List<ResolveInfo> results = queryIntentActivities( |
| 1903 | intent, resolvedType, flags|PackageManager.GET_RESOLVED_FILTER); |
| 1904 | if (Config.LOGV) Log.v(TAG, "Query " + intent + ": " + results); |
| 1905 | |
| 1906 | int specificsPos = 0; |
| 1907 | int N; |
| 1908 | |
| 1909 | // todo: note that the algorithm used here is O(N^2). This |
| 1910 | // isn't a problem in our current environment, but if we start running |
| 1911 | // into situations where we have more than 5 or 10 matches then this |
| 1912 | // should probably be changed to something smarter... |
| 1913 | |
| 1914 | // First we go through and resolve each of the specific items |
| 1915 | // that were supplied, taking care of removing any corresponding |
| 1916 | // duplicate items in the generic resolve list. |
| 1917 | if (specifics != null) { |
| 1918 | for (int i=0; i<specifics.length; i++) { |
| 1919 | final Intent sintent = specifics[i]; |
| 1920 | if (sintent == null) { |
| 1921 | continue; |
| 1922 | } |
| 1923 | |
| 1924 | if (Config.LOGV) Log.v(TAG, "Specific #" + i + ": " + sintent); |
| 1925 | String action = sintent.getAction(); |
| 1926 | if (resultsAction != null && resultsAction.equals(action)) { |
| 1927 | // If this action was explicitly requested, then don't |
| 1928 | // remove things that have it. |
| 1929 | action = null; |
| 1930 | } |
| 1931 | ComponentName comp = sintent.getComponent(); |
| 1932 | ResolveInfo ri = null; |
| 1933 | ActivityInfo ai = null; |
| 1934 | if (comp == null) { |
| 1935 | ri = resolveIntent( |
| 1936 | sintent, |
| 1937 | specificTypes != null ? specificTypes[i] : null, |
| 1938 | flags); |
| 1939 | if (ri == null) { |
| 1940 | continue; |
| 1941 | } |
| 1942 | if (ri == mResolveInfo) { |
| 1943 | // ACK! Must do something better with this. |
| 1944 | } |
| 1945 | ai = ri.activityInfo; |
| 1946 | comp = new ComponentName(ai.applicationInfo.packageName, |
| 1947 | ai.name); |
| 1948 | } else { |
| 1949 | ai = getActivityInfo(comp, flags); |
| 1950 | if (ai == null) { |
| 1951 | continue; |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | // Look for any generic query activities that are duplicates |
| 1956 | // of this specific one, and remove them from the results. |
| 1957 | if (Config.LOGV) Log.v(TAG, "Specific #" + i + ": " + ai); |
| 1958 | N = results.size(); |
| 1959 | int j; |
| 1960 | for (j=specificsPos; j<N; j++) { |
| 1961 | ResolveInfo sri = results.get(j); |
| 1962 | if ((sri.activityInfo.name.equals(comp.getClassName()) |
| 1963 | && sri.activityInfo.applicationInfo.packageName.equals( |
| 1964 | comp.getPackageName())) |
| 1965 | || (action != null && sri.filter.matchAction(action))) { |
| 1966 | results.remove(j); |
| 1967 | if (Config.LOGV) Log.v( |
| 1968 | TAG, "Removing duplicate item from " + j |
| 1969 | + " due to specific " + specificsPos); |
| 1970 | if (ri == null) { |
| 1971 | ri = sri; |
| 1972 | } |
| 1973 | j--; |
| 1974 | N--; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | // Add this specific item to its proper place. |
| 1979 | if (ri == null) { |
| 1980 | ri = new ResolveInfo(); |
| 1981 | ri.activityInfo = ai; |
| 1982 | } |
| 1983 | results.add(specificsPos, ri); |
| 1984 | ri.specificIndex = i; |
| 1985 | specificsPos++; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | // Now we go through the remaining generic results and remove any |
| 1990 | // duplicate actions that are found here. |
| 1991 | N = results.size(); |
| 1992 | for (int i=specificsPos; i<N-1; i++) { |
| 1993 | final ResolveInfo rii = results.get(i); |
| 1994 | if (rii.filter == null) { |
| 1995 | continue; |
| 1996 | } |
| 1997 | |
| 1998 | // Iterate over all of the actions of this result's intent |
| 1999 | // filter... typically this should be just one. |
| 2000 | final Iterator<String> it = rii.filter.actionsIterator(); |
| 2001 | if (it == null) { |
| 2002 | continue; |
| 2003 | } |
| 2004 | while (it.hasNext()) { |
| 2005 | final String action = it.next(); |
| 2006 | if (resultsAction != null && resultsAction.equals(action)) { |
| 2007 | // If this action was explicitly requested, then don't |
| 2008 | // remove things that have it. |
| 2009 | continue; |
| 2010 | } |
| 2011 | for (int j=i+1; j<N; j++) { |
| 2012 | final ResolveInfo rij = results.get(j); |
| 2013 | if (rij.filter != null && rij.filter.hasAction(action)) { |
| 2014 | results.remove(j); |
| 2015 | if (Config.LOGV) Log.v( |
| 2016 | TAG, "Removing duplicate item from " + j |
| 2017 | + " due to action " + action + " at " + i); |
| 2018 | j--; |
| 2019 | N--; |
| 2020 | } |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | // If the caller didn't request filter information, drop it now |
| 2025 | // so we don't have to marshall/unmarshall it. |
| 2026 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2027 | rii.filter = null; |
| 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | // Filter out the caller activity if so requested. |
| 2032 | if (caller != null) { |
| 2033 | N = results.size(); |
| 2034 | for (int i=0; i<N; i++) { |
| 2035 | ActivityInfo ainfo = results.get(i).activityInfo; |
| 2036 | if (caller.getPackageName().equals(ainfo.applicationInfo.packageName) |
| 2037 | && caller.getClassName().equals(ainfo.name)) { |
| 2038 | results.remove(i); |
| 2039 | break; |
| 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | // If the caller didn't request filter information, |
| 2045 | // drop them now so we don't have to |
| 2046 | // marshall/unmarshall it. |
| 2047 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2048 | N = results.size(); |
| 2049 | for (int i=0; i<N; i++) { |
| 2050 | results.get(i).filter = null; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | if (Config.LOGV) Log.v(TAG, "Result: " + results); |
| 2055 | return results; |
| 2056 | } |
| 2057 | |
| 2058 | public List<ResolveInfo> queryIntentReceivers(Intent intent, |
| 2059 | String resolvedType, int flags) { |
| Dianne Hackborn | c14b9ccd | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2060 | ComponentName comp = intent.getComponent(); |
| 2061 | if (comp != null) { |
| 2062 | List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
| 2063 | ActivityInfo ai = getReceiverInfo(comp, flags); |
| 2064 | if (ai != null) { |
| 2065 | ResolveInfo ri = new ResolveInfo(); |
| 2066 | ri.activityInfo = ai; |
| 2067 | list.add(ri); |
| 2068 | } |
| 2069 | return list; |
| 2070 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2071 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2072 | synchronized (mPackages) { |
| Dianne Hackborn | c14b9ccd | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2073 | String pkgName = intent.getPackage(); |
| 2074 | if (pkgName == null) { |
| 2075 | return (List<ResolveInfo>)mReceivers.queryIntent(intent, |
| 2076 | resolvedType, flags); |
| 2077 | } |
| 2078 | PackageParser.Package pkg = mPackages.get(pkgName); |
| 2079 | if (pkg != null) { |
| 2080 | return (List<ResolveInfo>) mReceivers.queryIntentForPackage(intent, |
| 2081 | resolvedType, flags, pkg.receivers); |
| 2082 | } |
| 2083 | return null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | public ResolveInfo resolveService(Intent intent, String resolvedType, |
| 2088 | int flags) { |
| 2089 | List<ResolveInfo> query = queryIntentServices(intent, resolvedType, |
| 2090 | flags); |
| 2091 | if (query != null) { |
| 2092 | if (query.size() >= 1) { |
| 2093 | // If there is more than one service with the same priority, |
| 2094 | // just arbitrarily pick the first one. |
| 2095 | return query.get(0); |
| 2096 | } |
| 2097 | } |
| 2098 | return null; |
| 2099 | } |
| 2100 | |
| 2101 | public List<ResolveInfo> queryIntentServices(Intent intent, |
| 2102 | String resolvedType, int flags) { |
| 2103 | ComponentName comp = intent.getComponent(); |
| 2104 | if (comp != null) { |
| 2105 | List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
| 2106 | ServiceInfo si = getServiceInfo(comp, flags); |
| 2107 | if (si != null) { |
| 2108 | ResolveInfo ri = new ResolveInfo(); |
| 2109 | ri.serviceInfo = si; |
| 2110 | list.add(ri); |
| 2111 | } |
| 2112 | return list; |
| 2113 | } |
| 2114 | |
| 2115 | synchronized (mPackages) { |
| Dianne Hackborn | c14b9ccd | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2116 | String pkgName = intent.getPackage(); |
| 2117 | if (pkgName == null) { |
| 2118 | return (List<ResolveInfo>)mServices.queryIntent(intent, |
| 2119 | resolvedType, flags); |
| 2120 | } |
| 2121 | PackageParser.Package pkg = mPackages.get(pkgName); |
| 2122 | if (pkg != null) { |
| 2123 | return (List<ResolveInfo>)mServices.queryIntentForPackage(intent, |
| 2124 | resolvedType, flags, pkg.services); |
| 2125 | } |
| 2126 | return null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2127 | } |
| 2128 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2129 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2130 | public List<PackageInfo> getInstalledPackages(int flags) { |
| 2131 | ArrayList<PackageInfo> finalList = new ArrayList<PackageInfo>(); |
| 2132 | |
| 2133 | synchronized (mPackages) { |
| 2134 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
| 2135 | Iterator<PackageSetting> i = mSettings.mPackages.values().iterator(); |
| 2136 | while (i.hasNext()) { |
| 2137 | final PackageSetting ps = i.next(); |
| 2138 | PackageInfo psPkg = generatePackageInfoFromSettingsLP(ps.name, flags); |
| 2139 | if(psPkg != null) { |
| 2140 | finalList.add(psPkg); |
| 2141 | } |
| 2142 | } |
| 2143 | } |
| 2144 | else { |
| 2145 | Iterator<PackageParser.Package> i = mPackages.values().iterator(); |
| 2146 | while (i.hasNext()) { |
| 2147 | final PackageParser.Package p = i.next(); |
| 2148 | if (p.applicationInfo != null) { |
| 2149 | PackageInfo pi = generatePackageInfo(p, flags); |
| 2150 | if(pi != null) { |
| 2151 | finalList.add(pi); |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | } |
| 2157 | return finalList; |
| 2158 | } |
| 2159 | |
| 2160 | public List<ApplicationInfo> getInstalledApplications(int flags) { |
| 2161 | ArrayList<ApplicationInfo> finalList = new ArrayList<ApplicationInfo>(); |
| 2162 | synchronized(mPackages) { |
| 2163 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
| 2164 | Iterator<PackageSetting> i = mSettings.mPackages.values().iterator(); |
| 2165 | while (i.hasNext()) { |
| 2166 | final PackageSetting ps = i.next(); |
| 2167 | ApplicationInfo ai = generateApplicationInfoFromSettingsLP(ps.name, flags); |
| 2168 | if(ai != null) { |
| 2169 | finalList.add(ai); |
| 2170 | } |
| 2171 | } |
| 2172 | } |
| 2173 | else { |
| 2174 | Iterator<PackageParser.Package> i = mPackages.values().iterator(); |
| 2175 | while (i.hasNext()) { |
| 2176 | final PackageParser.Package p = i.next(); |
| 2177 | if (p.applicationInfo != null) { |
| 2178 | ApplicationInfo ai = PackageParser.generateApplicationInfo(p, flags); |
| 2179 | if(ai != null) { |
| 2180 | finalList.add(ai); |
| 2181 | } |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | } |
| 2186 | return finalList; |
| 2187 | } |
| 2188 | |
| 2189 | public List<ApplicationInfo> getPersistentApplications(int flags) { |
| 2190 | ArrayList<ApplicationInfo> finalList = new ArrayList<ApplicationInfo>(); |
| 2191 | |
| 2192 | synchronized (mPackages) { |
| 2193 | Iterator<PackageParser.Package> i = mPackages.values().iterator(); |
| 2194 | while (i.hasNext()) { |
| 2195 | PackageParser.Package p = i.next(); |
| 2196 | if (p.applicationInfo != null |
| 2197 | && (p.applicationInfo.flags&ApplicationInfo.FLAG_PERSISTENT) != 0 |
| 2198 | && (!mSafeMode || (p.applicationInfo.flags |
| 2199 | &ApplicationInfo.FLAG_SYSTEM) != 0)) { |
| 2200 | finalList.add(p.applicationInfo); |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | return finalList; |
| 2206 | } |
| 2207 | |
| 2208 | public ProviderInfo resolveContentProvider(String name, int flags) { |
| 2209 | synchronized (mPackages) { |
| 2210 | final PackageParser.Provider provider = mProviders.get(name); |
| 2211 | return provider != null |
| 2212 | && mSettings.isEnabledLP(provider.info, flags) |
| 2213 | && (!mSafeMode || (provider.info.applicationInfo.flags |
| 2214 | &ApplicationInfo.FLAG_SYSTEM) != 0) |
| 2215 | ? PackageParser.generateProviderInfo(provider, flags) |
| 2216 | : null; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2220 | /** |
| 2221 | * @deprecated |
| 2222 | */ |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2223 | public void querySyncProviders(List outNames, List outInfo) { |
| 2224 | synchronized (mPackages) { |
| 2225 | Iterator<Map.Entry<String, PackageParser.Provider>> i |
| 2226 | = mProviders.entrySet().iterator(); |
| 2227 | |
| 2228 | while (i.hasNext()) { |
| 2229 | Map.Entry<String, PackageParser.Provider> entry = i.next(); |
| 2230 | PackageParser.Provider p = entry.getValue(); |
| 2231 | |
| 2232 | if (p.syncable |
| 2233 | && (!mSafeMode || (p.info.applicationInfo.flags |
| 2234 | &ApplicationInfo.FLAG_SYSTEM) != 0)) { |
| 2235 | outNames.add(entry.getKey()); |
| 2236 | outInfo.add(PackageParser.generateProviderInfo(p, 0)); |
| 2237 | } |
| 2238 | } |
| 2239 | } |
| 2240 | } |
| 2241 | |
| 2242 | public List<ProviderInfo> queryContentProviders(String processName, |
| 2243 | int uid, int flags) { |
| 2244 | ArrayList<ProviderInfo> finalList = null; |
| 2245 | |
| 2246 | synchronized (mPackages) { |
| 2247 | Iterator<PackageParser.Provider> i = mProvidersByComponent.values().iterator(); |
| 2248 | while (i.hasNext()) { |
| 2249 | PackageParser.Provider p = i.next(); |
| 2250 | if (p.info.authority != null |
| 2251 | && (processName == null || |
| 2252 | (p.info.processName.equals(processName) |
| 2253 | && p.info.applicationInfo.uid == uid)) |
| 2254 | && mSettings.isEnabledLP(p.info, flags) |
| 2255 | && (!mSafeMode || (p.info.applicationInfo.flags |
| 2256 | &ApplicationInfo.FLAG_SYSTEM) != 0)) { |
| 2257 | if (finalList == null) { |
| 2258 | finalList = new ArrayList<ProviderInfo>(3); |
| 2259 | } |
| 2260 | finalList.add(PackageParser.generateProviderInfo(p, |
| 2261 | flags)); |
| 2262 | } |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | if (finalList != null) { |
| 2267 | Collections.sort(finalList, mProviderInitOrderSorter); |
| 2268 | } |
| 2269 | |
| 2270 | return finalList; |
| 2271 | } |
| 2272 | |
| 2273 | public InstrumentationInfo getInstrumentationInfo(ComponentName name, |
| 2274 | int flags) { |
| 2275 | synchronized (mPackages) { |
| 2276 | final PackageParser.Instrumentation i = mInstrumentation.get(name); |
| 2277 | return PackageParser.generateInstrumentationInfo(i, flags); |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | public List<InstrumentationInfo> queryInstrumentation(String targetPackage, |
| 2282 | int flags) { |
| 2283 | ArrayList<InstrumentationInfo> finalList = |
| 2284 | new ArrayList<InstrumentationInfo>(); |
| 2285 | |
| 2286 | synchronized (mPackages) { |
| 2287 | Iterator<PackageParser.Instrumentation> i = mInstrumentation.values().iterator(); |
| 2288 | while (i.hasNext()) { |
| 2289 | PackageParser.Instrumentation p = i.next(); |
| 2290 | if (targetPackage == null |
| 2291 | || targetPackage.equals(p.info.targetPackage)) { |
| 2292 | finalList.add(PackageParser.generateInstrumentationInfo(p, |
| 2293 | flags)); |
| 2294 | } |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | return finalList; |
| 2299 | } |
| 2300 | |
| 2301 | private void scanDirLI(File dir, int flags, int scanMode) { |
| 2302 | Log.d(TAG, "Scanning app dir " + dir); |
| 2303 | |
| 2304 | String[] files = dir.list(); |
| 2305 | |
| 2306 | int i; |
| 2307 | for (i=0; i<files.length; i++) { |
| 2308 | File file = new File(dir, files[i]); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 2309 | if (!isPackageFilename(files[i])) { |
| 2310 | // Ignore entries which are not apk's |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 2311 | continue; |
| 2312 | } |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 2313 | PackageParser.Package pkg = scanPackageLI(file, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2314 | flags|PackageParser.PARSE_MUST_BE_APK, scanMode); |
| Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 2315 | // Don't mess around with apps in system partition. |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 2316 | if (pkg == null && (flags & PackageParser.PARSE_IS_SYSTEM) == 0 && |
| 2317 | mLastScanError == PackageManager.INSTALL_FAILED_INVALID_APK) { |
| Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 2318 | // Delete the apk |
| 2319 | Log.w(TAG, "Cleaning up failed install of " + file); |
| 2320 | file.delete(); |
| 2321 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2322 | } |
| 2323 | } |
| 2324 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2325 | private static File getSettingsProblemFile() { |
| 2326 | File dataDir = Environment.getDataDirectory(); |
| 2327 | File systemDir = new File(dataDir, "system"); |
| 2328 | File fname = new File(systemDir, "uiderrors.txt"); |
| 2329 | return fname; |
| 2330 | } |
| 2331 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2332 | private static void reportSettingsProblem(int priority, String msg) { |
| 2333 | try { |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2334 | File fname = getSettingsProblemFile(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2335 | FileOutputStream out = new FileOutputStream(fname, true); |
| 2336 | PrintWriter pw = new PrintWriter(out); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2337 | SimpleDateFormat formatter = new SimpleDateFormat(); |
| 2338 | String dateString = formatter.format(new Date(System.currentTimeMillis())); |
| 2339 | pw.println(dateString + ": " + msg); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2340 | pw.close(); |
| 2341 | FileUtils.setPermissions( |
| 2342 | fname.toString(), |
| 2343 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IROTH, |
| 2344 | -1, -1); |
| 2345 | } catch (java.io.IOException e) { |
| 2346 | } |
| 2347 | Log.println(priority, TAG, msg); |
| 2348 | } |
| 2349 | |
| 2350 | private boolean collectCertificatesLI(PackageParser pp, PackageSetting ps, |
| 2351 | PackageParser.Package pkg, File srcFile, int parseFlags) { |
| 2352 | if (GET_CERTIFICATES) { |
| 2353 | if (ps == null || !ps.codePath.equals(srcFile) |
| 2354 | || ps.getTimeStamp() != srcFile.lastModified()) { |
| 2355 | Log.i(TAG, srcFile.toString() + " changed; collecting certs"); |
| 2356 | if (!pp.collectCertificates(pkg, parseFlags)) { |
| 2357 | mLastScanError = pp.getParseError(); |
| 2358 | return false; |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | return true; |
| 2363 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2364 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2365 | /* |
| 2366 | * Scan a package and return the newly parsed package. |
| 2367 | * Returns null in case of errors and the error code is stored in mLastScanError |
| 2368 | */ |
| 2369 | private PackageParser.Package scanPackageLI(File scanFile, |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2370 | int parseFlags, int scanMode) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2371 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 2372 | String scanPath = scanFile.getPath(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2373 | parseFlags |= mDefParseFlags; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 2374 | PackageParser pp = new PackageParser(scanPath); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2375 | pp.setSeparateProcesses(mSeparateProcesses); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2376 | final PackageParser.Package pkg = pp.parsePackage(scanFile, |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2377 | scanPath, mMetrics, parseFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2378 | if (pkg == null) { |
| 2379 | mLastScanError = pp.getParseError(); |
| 2380 | return null; |
| 2381 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2382 | PackageSetting ps = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2383 | PackageSetting updatedPkg; |
| 2384 | synchronized (mPackages) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2385 | // Look to see if we already know about this package. |
| 2386 | String oldName = mSettings.mRenamedPackages.get(pkg.packageName); |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 2387 | if (pkg.mOriginalPackages != null && pkg.mOriginalPackages.contains(oldName)) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2388 | // This package has been renamed to its original name. Let's |
| 2389 | // use that. |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 2390 | ps = mSettings.peekPackageLP(oldName); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2391 | } |
| 2392 | // If there was no original package, see one for the real package name. |
| 2393 | if (ps == null) { |
| 2394 | ps = mSettings.peekPackageLP(pkg.packageName); |
| 2395 | } |
| 2396 | // Check to see if this package could be hiding/updating a system |
| 2397 | // package. Must look for it either under the original or real |
| 2398 | // package name depending on our state. |
| 2399 | updatedPkg = mSettings.mDisabledSysPackages.get( |
| 2400 | ps != null ? ps.name : pkg.packageName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2401 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2402 | // First check if this is a system package that may involve an update |
| 2403 | if (updatedPkg != null && (parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
| 2404 | if (!ps.codePath.equals(scanFile)) { |
| 2405 | // The path has changed from what was last scanned... check the |
| 2406 | // version of the new path against what we have stored to determine |
| 2407 | // what to do. |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 2408 | if (pkg.mVersionCode < ps.versionCode) { |
| 2409 | // The system package has been updated and the code path does not match |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2410 | // Ignore entry. Skip it. |
| 2411 | Log.i(TAG, "Package " + ps.name + " at " + scanFile |
| 2412 | + "ignored: updated version " + ps.versionCode |
| 2413 | + " better than this " + pkg.mVersionCode); |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 2414 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 2415 | return null; |
| 2416 | } else { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2417 | // The current app on the system partion is better than |
| 2418 | // what we have updated to on the data partition; switch |
| 2419 | // back to the system partition version. |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 2420 | // At this point, its safely assumed that package installation for |
| 2421 | // apps in system partition will go through. If not there won't be a working |
| 2422 | // version of the app |
| 2423 | synchronized (mPackages) { |
| 2424 | // Just remove the loaded entries from package lists. |
| 2425 | mPackages.remove(ps.name); |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 2426 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2427 | Log.w(TAG, "Package " + ps.name + " at " + scanFile |
| 2428 | + "reverting from " + ps.codePathString |
| 2429 | + ": new version " + pkg.mVersionCode |
| 2430 | + " better than installed " + ps.versionCode); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 2431 | InstallArgs args = new FileInstallArgs(ps.codePathString, ps.resourcePathString); |
| 2432 | args.cleanUpResourcesLI(); |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 2433 | mSettings.enableSystemPackageLP(ps.name); |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 2434 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2435 | } |
| 2436 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2437 | if (updatedPkg != null) { |
| 2438 | // An updated system app will not have the PARSE_IS_SYSTEM flag set initially |
| 2439 | parseFlags |= PackageParser.PARSE_IS_SYSTEM; |
| 2440 | } |
| 2441 | // Verify certificates against what was last scanned |
| 2442 | if (!collectCertificatesLI(pp, ps, pkg, scanFile, parseFlags)) { |
| 2443 | Log.i(TAG, "Failed verifying certificates for package:" + pkg.packageName); |
| 2444 | return null; |
| 2445 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2446 | // The apk is forward locked (not public) if its code and resources |
| 2447 | // are kept in different files. |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 2448 | // TODO grab this value from PackageSettings |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2449 | if (ps != null && !ps.codePath.equals(ps.resourcePath)) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 2450 | parseFlags |= PackageParser.PARSE_FORWARD_LOCK; |
| Suchi Amalapurapu | f2c1072 | 2009-07-29 17:19:39 -0700 | [diff] [blame] | 2451 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 2452 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2453 | String codePath = null; |
| 2454 | String resPath = null; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 2455 | if ((parseFlags & PackageParser.PARSE_FORWARD_LOCK) != 0) { |
| 2456 | if (ps != null && ps.resourcePathString != null) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2457 | resPath = ps.resourcePathString; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 2458 | } else { |
| 2459 | // Should not happen at all. Just log an error. |
| 2460 | Log.e(TAG, "Resource path not set for pkg : " + pkg.packageName); |
| 2461 | } |
| 2462 | } else { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2463 | resPath = pkg.mScanPath; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 2464 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2465 | codePath = pkg.mScanPath; |
| 2466 | // Set application objects path explicitly. |
| 2467 | setApplicationInfoPaths(pkg, codePath, resPath); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2468 | // Note that we invoke the following method only if we are about to unpack an application |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 2469 | return scanPackageLI(pkg, parseFlags, scanMode | SCAN_UPDATE_SIGNATURE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2470 | } |
| 2471 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2472 | private static void setApplicationInfoPaths(PackageParser.Package pkg, |
| 2473 | String destCodePath, String destResPath) { |
| 2474 | pkg.mPath = pkg.mScanPath = destCodePath; |
| 2475 | pkg.applicationInfo.sourceDir = destCodePath; |
| 2476 | pkg.applicationInfo.publicSourceDir = destResPath; |
| 2477 | } |
| 2478 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2479 | private static String fixProcessName(String defProcessName, |
| 2480 | String processName, int uid) { |
| 2481 | if (processName == null) { |
| 2482 | return defProcessName; |
| 2483 | } |
| 2484 | return processName; |
| 2485 | } |
| 2486 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2487 | private boolean verifySignaturesLP(PackageSetting pkgSetting, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2488 | PackageParser.Package pkg, int parseFlags, boolean updateSignature) { |
| 2489 | if (pkg.mSignatures != null) { |
| 2490 | if (!pkgSetting.signatures.updateSignatures(pkg.mSignatures, |
| 2491 | updateSignature)) { |
| 2492 | Log.e(TAG, "Package " + pkg.packageName |
| 2493 | + " signatures do not match the previously installed version; ignoring!"); |
| 2494 | mLastScanError = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
| 2495 | return false; |
| 2496 | } |
| 2497 | |
| 2498 | if (pkgSetting.sharedUser != null) { |
| 2499 | if (!pkgSetting.sharedUser.signatures.mergeSignatures( |
| 2500 | pkg.mSignatures, updateSignature)) { |
| 2501 | Log.e(TAG, "Package " + pkg.packageName |
| 2502 | + " has no signatures that match those in shared user " |
| 2503 | + pkgSetting.sharedUser.name + "; ignoring!"); |
| 2504 | mLastScanError = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; |
| 2505 | return false; |
| 2506 | } |
| 2507 | } |
| 2508 | } else { |
| 2509 | pkg.mSignatures = pkgSetting.signatures.mSignatures; |
| 2510 | } |
| 2511 | return true; |
| 2512 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2513 | |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 2514 | public boolean performDexOpt(String packageName) { |
| 2515 | if (!mNoDexOpt) { |
| 2516 | return false; |
| 2517 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2518 | |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 2519 | PackageParser.Package p; |
| 2520 | synchronized (mPackages) { |
| 2521 | p = mPackages.get(packageName); |
| 2522 | if (p == null || p.mDidDexOpt) { |
| 2523 | return false; |
| 2524 | } |
| 2525 | } |
| 2526 | synchronized (mInstallLock) { |
| 2527 | return performDexOptLI(p, false) == DEX_OPT_PERFORMED; |
| 2528 | } |
| 2529 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2530 | |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 2531 | static final int DEX_OPT_SKIPPED = 0; |
| 2532 | static final int DEX_OPT_PERFORMED = 1; |
| 2533 | static final int DEX_OPT_FAILED = -1; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2534 | |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 2535 | private int performDexOptLI(PackageParser.Package pkg, boolean forceDex) { |
| 2536 | boolean performed = false; |
| Marco Nelissen | d595c79 | 2009-07-02 15:23:26 -0700 | [diff] [blame] | 2537 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0 && mInstaller != null) { |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 2538 | String path = pkg.mScanPath; |
| 2539 | int ret = 0; |
| 2540 | try { |
| 2541 | if (forceDex || dalvik.system.DexFile.isDexOptNeeded(path)) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2542 | ret = mInstaller.dexopt(path, pkg.applicationInfo.uid, |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 2543 | !isForwardLocked(pkg)); |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 2544 | pkg.mDidDexOpt = true; |
| 2545 | performed = true; |
| 2546 | } |
| 2547 | } catch (FileNotFoundException e) { |
| 2548 | Log.w(TAG, "Apk not found for dexopt: " + path); |
| 2549 | ret = -1; |
| 2550 | } catch (IOException e) { |
| 2551 | Log.w(TAG, "Exception reading apk: " + path, e); |
| 2552 | ret = -1; |
| 2553 | } |
| 2554 | if (ret < 0) { |
| 2555 | //error from installer |
| 2556 | return DEX_OPT_FAILED; |
| 2557 | } |
| 2558 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2559 | |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 2560 | return performed ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 2561 | } |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2562 | |
| 2563 | private static boolean useEncryptedFilesystemForPackage(PackageParser.Package pkg) { |
| 2564 | return Environment.isEncryptedFilesystemEnabled() && |
| 2565 | ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_NEVER_ENCRYPT) == 0); |
| 2566 | } |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 2567 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2568 | private boolean verifyPackageUpdate(PackageSetting oldPkg, PackageParser.Package newPkg) { |
| 2569 | if ((oldPkg.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 2570 | Log.w(TAG, "Unable to update from " + oldPkg.name |
| 2571 | + " to " + newPkg.packageName |
| 2572 | + ": old package not in system partition"); |
| 2573 | return false; |
| 2574 | } else if (mPackages.get(oldPkg.name) != null) { |
| 2575 | Log.w(TAG, "Unable to update from " + oldPkg.name |
| 2576 | + " to " + newPkg.packageName |
| 2577 | + ": old package still exists"); |
| 2578 | return false; |
| 2579 | } |
| 2580 | return true; |
| 2581 | } |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 2582 | |
| 2583 | private File getDataPathForPackage(PackageParser.Package pkg) { |
| 2584 | boolean useEncryptedFSDir = useEncryptedFilesystemForPackage(pkg); |
| 2585 | File dataPath; |
| 2586 | if (useEncryptedFSDir) { |
| 2587 | dataPath = new File(mSecureAppDataDir, pkg.packageName); |
| 2588 | } else { |
| 2589 | dataPath = new File(mAppDataDir, pkg.packageName); |
| 2590 | } |
| 2591 | return dataPath; |
| 2592 | } |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2593 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2594 | private PackageParser.Package scanPackageLI(PackageParser.Package pkg, |
| 2595 | int parseFlags, int scanMode) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 2596 | File scanFile = new File(pkg.mScanPath); |
| Suchi Amalapurapu | 7040ce7 | 2010-02-08 23:55:56 -0800 | [diff] [blame] | 2597 | if (scanFile == null || pkg.applicationInfo.sourceDir == null || |
| 2598 | pkg.applicationInfo.publicSourceDir == null) { |
| Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 2599 | // Bail out. The resource and code paths haven't been set. |
| 2600 | Log.w(TAG, " Code and resource paths haven't been set correctly"); |
| 2601 | mLastScanError = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 2602 | return null; |
| 2603 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2604 | mScanningPath = scanFile; |
| 2605 | if (pkg == null) { |
| 2606 | mLastScanError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME; |
| 2607 | return null; |
| 2608 | } |
| 2609 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2610 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
| 2611 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 2612 | } |
| 2613 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2614 | if (pkg.packageName.equals("android")) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | synchronized (mPackages) { |
| 2616 | if (mAndroidApplication != null) { |
| 2617 | Log.w(TAG, "*************************************************"); |
| 2618 | Log.w(TAG, "Core android package being redefined. Skipping."); |
| 2619 | Log.w(TAG, " file=" + mScanningPath); |
| 2620 | Log.w(TAG, "*************************************************"); |
| 2621 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 2622 | return null; |
| 2623 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2624 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2625 | // Set up information for our fall-back user intent resolution |
| 2626 | // activity. |
| 2627 | mPlatformPackage = pkg; |
| 2628 | pkg.mVersionCode = mSdkVersion; |
| 2629 | mAndroidApplication = pkg.applicationInfo; |
| 2630 | mResolveActivity.applicationInfo = mAndroidApplication; |
| 2631 | mResolveActivity.name = ResolverActivity.class.getName(); |
| 2632 | mResolveActivity.packageName = mAndroidApplication.packageName; |
| 2633 | mResolveActivity.processName = mAndroidApplication.processName; |
| 2634 | mResolveActivity.launchMode = ActivityInfo.LAUNCH_MULTIPLE; |
| 2635 | mResolveActivity.flags = ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS; |
| 2636 | mResolveActivity.theme = com.android.internal.R.style.Theme_Dialog_Alert; |
| 2637 | mResolveActivity.exported = true; |
| 2638 | mResolveActivity.enabled = true; |
| 2639 | mResolveInfo.activityInfo = mResolveActivity; |
| 2640 | mResolveInfo.priority = 0; |
| 2641 | mResolveInfo.preferredOrder = 0; |
| 2642 | mResolveInfo.match = 0; |
| 2643 | mResolveComponentName = new ComponentName( |
| 2644 | mAndroidApplication.packageName, mResolveActivity.name); |
| 2645 | } |
| 2646 | } |
| 2647 | |
| 2648 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0 && Config.LOGD) Log.d( |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2649 | TAG, "Scanning package " + pkg.packageName); |
| 2650 | if (mPackages.containsKey(pkg.packageName) |
| 2651 | || mSharedLibraries.containsKey(pkg.packageName)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2652 | Log.w(TAG, "*************************************************"); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2653 | Log.w(TAG, "Application package " + pkg.packageName |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2654 | + " already installed. Skipping duplicate."); |
| 2655 | Log.w(TAG, "*************************************************"); |
| 2656 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 2657 | return null; |
| 2658 | } |
| 2659 | |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 2660 | // Initialize package source and resource directories |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 2661 | File destCodeFile = new File(pkg.applicationInfo.sourceDir); |
| 2662 | File destResourceFile = new File(pkg.applicationInfo.publicSourceDir); |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 2663 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2664 | SharedUserSetting suid = null; |
| 2665 | PackageSetting pkgSetting = null; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2666 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2667 | boolean removeExisting = false; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2668 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2669 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 2670 | // Only system apps can use these features. |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 2671 | pkg.mOriginalPackages = null; |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2672 | pkg.mRealPackage = null; |
| 2673 | pkg.mAdoptPermissions = null; |
| 2674 | } |
| 2675 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2676 | synchronized (mPackages) { |
| 2677 | // Check all shared libraries and map to their actual file path. |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 2678 | if (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null) { |
| 2679 | if (mTmpSharedLibraries == null || |
| 2680 | mTmpSharedLibraries.length < mSharedLibraries.size()) { |
| 2681 | mTmpSharedLibraries = new String[mSharedLibraries.size()]; |
| 2682 | } |
| 2683 | int num = 0; |
| 2684 | int N = pkg.usesLibraries != null ? pkg.usesLibraries.size() : 0; |
| 2685 | for (int i=0; i<N; i++) { |
| 2686 | String file = mSharedLibraries.get(pkg.usesLibraries.get(i)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2687 | if (file == null) { |
| 2688 | Log.e(TAG, "Package " + pkg.packageName |
| 2689 | + " requires unavailable shared library " |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 2690 | + pkg.usesLibraries.get(i) + "; failing!"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2691 | mLastScanError = PackageManager.INSTALL_FAILED_MISSING_SHARED_LIBRARY; |
| 2692 | return null; |
| 2693 | } |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 2694 | mTmpSharedLibraries[num] = file; |
| 2695 | num++; |
| 2696 | } |
| 2697 | N = pkg.usesOptionalLibraries != null ? pkg.usesOptionalLibraries.size() : 0; |
| 2698 | for (int i=0; i<N; i++) { |
| 2699 | String file = mSharedLibraries.get(pkg.usesOptionalLibraries.get(i)); |
| 2700 | if (file == null) { |
| 2701 | Log.w(TAG, "Package " + pkg.packageName |
| 2702 | + " desires unavailable shared library " |
| 2703 | + pkg.usesOptionalLibraries.get(i) + "; ignoring!"); |
| 2704 | } else { |
| 2705 | mTmpSharedLibraries[num] = file; |
| 2706 | num++; |
| 2707 | } |
| 2708 | } |
| 2709 | if (num > 0) { |
| 2710 | pkg.usesLibraryFiles = new String[num]; |
| 2711 | System.arraycopy(mTmpSharedLibraries, 0, |
| 2712 | pkg.usesLibraryFiles, 0, num); |
| 2713 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2714 | |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 2715 | if (pkg.reqFeatures != null) { |
| 2716 | N = pkg.reqFeatures.size(); |
| 2717 | for (int i=0; i<N; i++) { |
| 2718 | FeatureInfo fi = pkg.reqFeatures.get(i); |
| 2719 | if ((fi.flags&FeatureInfo.FLAG_REQUIRED) == 0) { |
| 2720 | // Don't care. |
| 2721 | continue; |
| 2722 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2723 | |
| Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 2724 | if (fi.name != null) { |
| 2725 | if (mAvailableFeatures.get(fi.name) == null) { |
| 2726 | Log.e(TAG, "Package " + pkg.packageName |
| 2727 | + " requires unavailable feature " |
| 2728 | + fi.name + "; failing!"); |
| 2729 | mLastScanError = PackageManager.INSTALL_FAILED_MISSING_FEATURE; |
| 2730 | return null; |
| 2731 | } |
| 2732 | } |
| 2733 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2734 | } |
| 2735 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2736 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | if (pkg.mSharedUserId != null) { |
| 2738 | suid = mSettings.getSharedUserLP(pkg.mSharedUserId, |
| 2739 | pkg.applicationInfo.flags, true); |
| 2740 | if (suid == null) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2741 | Log.w(TAG, "Creating application package " + pkg.packageName |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2742 | + " for shared user failed"); |
| 2743 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 2744 | return null; |
| 2745 | } |
| 2746 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0 && Config.LOGD) { |
| 2747 | Log.d(TAG, "Shared UserID " + pkg.mSharedUserId + " (uid=" |
| 2748 | + suid.userId + "): packages=" + suid.packages); |
| 2749 | } |
| 2750 | } |
| Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 2751 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2752 | if (false) { |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 2753 | if (pkg.mOriginalPackages != null) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2754 | Log.w(TAG, "WAITING FOR DEBUGGER"); |
| 2755 | Debug.waitForDebugger(); |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 2756 | Log.i(TAG, "Package " + pkg.packageName + " from original packages" |
| 2757 | + pkg.mOriginalPackages); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2758 | } |
| 2759 | } |
| 2760 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2761 | // Check if we are renaming from an original package name. |
| 2762 | PackageSetting origPackage = null; |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2763 | String realName = null; |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 2764 | if (pkg.mOriginalPackages != null) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2765 | // This package may need to be renamed to a previously |
| 2766 | // installed name. Let's check on that... |
| 2767 | String renamed = mSettings.mRenamedPackages.get(pkg.mRealPackage); |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 2768 | if (pkg.mOriginalPackages.contains(renamed)) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2769 | // This package had originally been installed as the |
| 2770 | // original name, and we have already taken care of |
| 2771 | // transitioning to the new one. Just update the new |
| 2772 | // one to continue using the old name. |
| 2773 | realName = pkg.mRealPackage; |
| 2774 | if (!pkg.packageName.equals(renamed)) { |
| 2775 | // Callers into this function may have already taken |
| 2776 | // care of renaming the package; only do it here if |
| 2777 | // it is not already done. |
| 2778 | pkg.setPackageName(renamed); |
| 2779 | } |
| 2780 | |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 2781 | } else { |
| 2782 | for (int i=pkg.mOriginalPackages.size()-1; i>=0; i--) { |
| 2783 | if ((origPackage=mSettings.peekPackageLP( |
| 2784 | pkg.mOriginalPackages.get(i))) != null) { |
| 2785 | // We do have the package already installed under its |
| 2786 | // original name... should we use it? |
| 2787 | if (!verifyPackageUpdate(origPackage, pkg)) { |
| 2788 | // New package is not compatible with original. |
| 2789 | origPackage = null; |
| 2790 | continue; |
| 2791 | } else if (origPackage.sharedUser != null) { |
| 2792 | // Make sure uid is compatible between packages. |
| 2793 | if (!origPackage.sharedUser.name.equals(pkg.mSharedUserId)) { |
| 2794 | Log.w(TAG, "Unable to migrate data from " + origPackage.name |
| 2795 | + " to " + pkg.packageName + ": old uid " |
| 2796 | + origPackage.sharedUser.name |
| 2797 | + " differs from " + pkg.mSharedUserId); |
| 2798 | origPackage = null; |
| 2799 | continue; |
| 2800 | } |
| 2801 | } else { |
| 2802 | if (DEBUG_UPGRADE) Log.v(TAG, "Renaming new package " |
| 2803 | + pkg.packageName + " to old name " + origPackage.name); |
| 2804 | } |
| 2805 | break; |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2806 | } |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2807 | } |
| 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | if (mTransferedPackages.contains(pkg.packageName)) { |
| 2812 | Log.w(TAG, "Package " + pkg.packageName |
| 2813 | + " was transferred to another, but its .apk remains"); |
| 2814 | } |
| 2815 | |
| Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 2816 | // Just create the setting, don't add it yet. For already existing packages |
| 2817 | // the PkgSetting exists already and doesn't have to be created. |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2818 | pkgSetting = mSettings.getPackageLP(pkg, origPackage, realName, suid, destCodeFile, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2819 | destResourceFile, pkg.applicationInfo.flags, true, false); |
| 2820 | if (pkgSetting == null) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2821 | Log.w(TAG, "Creating application package " + pkg.packageName + " failed"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2822 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 2823 | return null; |
| 2824 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2825 | |
| 2826 | if (pkgSetting.origPackage != null) { |
| 2827 | // If we are first transitioning from an original package, |
| 2828 | // fix up the new package's name now. We need to do this after |
| 2829 | // looking up the package under its new name, so getPackageLP |
| 2830 | // can take care of fiddling things correctly. |
| 2831 | pkg.setPackageName(origPackage.name); |
| 2832 | |
| 2833 | // File a report about this. |
| 2834 | String msg = "New package " + pkgSetting.realName |
| 2835 | + " renamed to replace old package " + pkgSetting.name; |
| 2836 | reportSettingsProblem(Log.WARN, msg); |
| 2837 | |
| 2838 | // Make a note of it. |
| 2839 | mTransferedPackages.add(origPackage.name); |
| 2840 | |
| 2841 | // No longer need to retain this. |
| 2842 | pkgSetting.origPackage = null; |
| 2843 | } |
| 2844 | |
| 2845 | if (realName != null) { |
| 2846 | // Make a note of it. |
| 2847 | mTransferedPackages.add(pkg.packageName); |
| 2848 | } |
| 2849 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2850 | if (mSettings.mDisabledSysPackages.get(pkg.packageName) != null) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2851 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
| 2852 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2853 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2854 | pkg.applicationInfo.uid = pkgSetting.userId; |
| 2855 | pkg.mExtras = pkgSetting; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2856 | |
| 2857 | if (!verifySignaturesLP(pkgSetting, pkg, parseFlags, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2858 | (scanMode&SCAN_UPDATE_SIGNATURE) != 0)) { |
| 2859 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) == 0) { |
| 2860 | mLastScanError = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
| 2861 | return null; |
| 2862 | } |
| 2863 | // The signature has changed, but this package is in the system |
| 2864 | // image... let's recover! |
| Suchi Amalapurapu | c4dd60f | 2009-03-24 21:10:53 -0700 | [diff] [blame] | 2865 | pkgSetting.signatures.mSignatures = pkg.mSignatures; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2866 | // However... if this package is part of a shared user, but it |
| 2867 | // doesn't match the signature of the shared user, let's fail. |
| 2868 | // What this means is that you can't change the signatures |
| 2869 | // associated with an overall shared user, which doesn't seem all |
| 2870 | // that unreasonable. |
| 2871 | if (pkgSetting.sharedUser != null) { |
| 2872 | if (!pkgSetting.sharedUser.signatures.mergeSignatures( |
| 2873 | pkg.mSignatures, false)) { |
| 2874 | mLastScanError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 2875 | return null; |
| 2876 | } |
| 2877 | } |
| 2878 | removeExisting = true; |
| 2879 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2880 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2881 | // Verify that this new package doesn't have any content providers |
| 2882 | // that conflict with existing packages. Only do this if the |
| 2883 | // package isn't already installed, since we don't want to break |
| 2884 | // things that are installed. |
| 2885 | if ((scanMode&SCAN_NEW_INSTALL) != 0) { |
| 2886 | int N = pkg.providers.size(); |
| 2887 | int i; |
| 2888 | for (i=0; i<N; i++) { |
| 2889 | PackageParser.Provider p = pkg.providers.get(i); |
| Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 2890 | if (p.info.authority != null) { |
| 2891 | String names[] = p.info.authority.split(";"); |
| 2892 | for (int j = 0; j < names.length; j++) { |
| 2893 | if (mProviders.containsKey(names[j])) { |
| 2894 | PackageParser.Provider other = mProviders.get(names[j]); |
| 2895 | Log.w(TAG, "Can't install because provider name " + names[j] + |
| 2896 | " (in package " + pkg.applicationInfo.packageName + |
| 2897 | ") is already used by " |
| 2898 | + ((other != null && other.getComponentName() != null) |
| 2899 | ? other.getComponentName().getPackageName() : "?")); |
| 2900 | mLastScanError = PackageManager.INSTALL_FAILED_CONFLICTING_PROVIDER; |
| 2901 | return null; |
| 2902 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2903 | } |
| 2904 | } |
| 2905 | } |
| 2906 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2907 | } |
| 2908 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2909 | final String pkgName = pkg.packageName; |
| 2910 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2911 | if (removeExisting) { |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2912 | boolean useEncryptedFSDir = useEncryptedFilesystemForPackage(pkg); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2913 | if (mInstaller != null) { |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2914 | int ret = mInstaller.remove(pkgName, useEncryptedFSDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2915 | if (ret != 0) { |
| 2916 | String msg = "System package " + pkg.packageName |
| 2917 | + " could not have data directory erased after signature change."; |
| 2918 | reportSettingsProblem(Log.WARN, msg); |
| 2919 | mLastScanError = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 2920 | return null; |
| 2921 | } |
| 2922 | } |
| 2923 | Log.w(TAG, "System package " + pkg.packageName |
| 2924 | + " signature changed: existing data removed."); |
| 2925 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
| 2926 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2927 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 2928 | if (pkg.mAdoptPermissions != null) { |
| 2929 | // This package wants to adopt ownership of permissions from |
| 2930 | // another package. |
| 2931 | for (int i=pkg.mAdoptPermissions.size()-1; i>=0; i--) { |
| 2932 | String origName = pkg.mAdoptPermissions.get(i); |
| 2933 | PackageSetting orig = mSettings.peekPackageLP(origName); |
| 2934 | if (orig != null) { |
| 2935 | if (verifyPackageUpdate(orig, pkg)) { |
| 2936 | Log.i(TAG, "Adopting permissions from " |
| 2937 | + origName + " to " + pkg.packageName); |
| 2938 | mSettings.transferPermissions(origName, pkg.packageName); |
| 2939 | } |
| 2940 | } |
| 2941 | } |
| 2942 | } |
| 2943 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2944 | long scanFileTime = scanFile.lastModified(); |
| 2945 | final boolean forceDex = (scanMode&SCAN_FORCE_DEX) != 0; |
| 2946 | final boolean scanFileNewer = forceDex || scanFileTime != pkgSetting.getTimeStamp(); |
| 2947 | pkg.applicationInfo.processName = fixProcessName( |
| 2948 | pkg.applicationInfo.packageName, |
| 2949 | pkg.applicationInfo.processName, |
| 2950 | pkg.applicationInfo.uid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2951 | |
| 2952 | File dataPath; |
| 2953 | if (mPlatformPackage == pkg) { |
| 2954 | // The system package is special. |
| 2955 | dataPath = new File (Environment.getDataDirectory(), "system"); |
| 2956 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 2957 | } else { |
| 2958 | // This is a normal package, need to make its data directory. |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2959 | boolean useEncryptedFSDir = useEncryptedFilesystemForPackage(pkg); |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 2960 | dataPath = getDataPathForPackage(pkg); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2961 | |
| 2962 | boolean uidError = false; |
| 2963 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2964 | if (dataPath.exists()) { |
| 2965 | mOutPermissions[1] = 0; |
| 2966 | FileUtils.getPermissions(dataPath.getPath(), mOutPermissions); |
| 2967 | if (mOutPermissions[1] == pkg.applicationInfo.uid |
| 2968 | || !Process.supportsProcesses()) { |
| 2969 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 2970 | } else { |
| 2971 | boolean recovered = false; |
| 2972 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
| 2973 | // If this is a system app, we can at least delete its |
| 2974 | // current data so the application will still work. |
| 2975 | if (mInstaller != null) { |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2976 | int ret = mInstaller.remove(pkgName, useEncryptedFSDir); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2977 | if (ret >= 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2978 | // Old data gone! |
| 2979 | String msg = "System package " + pkg.packageName |
| 2980 | + " has changed from uid: " |
| 2981 | + mOutPermissions[1] + " to " |
| 2982 | + pkg.applicationInfo.uid + "; old data erased"; |
| 2983 | reportSettingsProblem(Log.WARN, msg); |
| 2984 | recovered = true; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2985 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2986 | // And now re-install the app. |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2987 | ret = mInstaller.install(pkgName, useEncryptedFSDir, pkg.applicationInfo.uid, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2988 | pkg.applicationInfo.uid); |
| 2989 | if (ret == -1) { |
| 2990 | // Ack should not happen! |
| 2991 | msg = "System package " + pkg.packageName |
| 2992 | + " could not have data directory re-created after delete."; |
| 2993 | reportSettingsProblem(Log.WARN, msg); |
| 2994 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 2995 | return null; |
| 2996 | } |
| 2997 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2998 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2999 | if (!recovered) { |
| 3000 | mHasSystemUidErrors = true; |
| 3001 | } |
| 3002 | } |
| 3003 | if (!recovered) { |
| 3004 | pkg.applicationInfo.dataDir = "/mismatched_uid/settings_" |
| 3005 | + pkg.applicationInfo.uid + "/fs_" |
| 3006 | + mOutPermissions[1]; |
| 3007 | String msg = "Package " + pkg.packageName |
| 3008 | + " has mismatched uid: " |
| 3009 | + mOutPermissions[1] + " on disk, " |
| 3010 | + pkg.applicationInfo.uid + " in settings"; |
| 3011 | synchronized (mPackages) { |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3012 | mSettings.mReadMessages.append(msg); |
| 3013 | mSettings.mReadMessages.append('\n'); |
| 3014 | uidError = true; |
| 3015 | if (!pkgSetting.uidError) { |
| 3016 | reportSettingsProblem(Log.ERROR, msg); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3017 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3018 | } |
| 3019 | } |
| 3020 | } |
| 3021 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3022 | } else { |
| 3023 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0 && Config.LOGV) |
| 3024 | Log.v(TAG, "Want this data dir: " + dataPath); |
| 3025 | //invoke installer to do the actual installation |
| 3026 | if (mInstaller != null) { |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 3027 | int ret = mInstaller.install(pkgName, useEncryptedFSDir, pkg.applicationInfo.uid, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3028 | pkg.applicationInfo.uid); |
| 3029 | if(ret < 0) { |
| 3030 | // Error from installer |
| 3031 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3032 | return null; |
| 3033 | } |
| 3034 | } else { |
| 3035 | dataPath.mkdirs(); |
| 3036 | if (dataPath.exists()) { |
| 3037 | FileUtils.setPermissions( |
| 3038 | dataPath.toString(), |
| 3039 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 3040 | pkg.applicationInfo.uid, pkg.applicationInfo.uid); |
| 3041 | } |
| 3042 | } |
| 3043 | if (dataPath.exists()) { |
| 3044 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3045 | } else { |
| 3046 | Log.w(TAG, "Unable to create data directory: " + dataPath); |
| 3047 | pkg.applicationInfo.dataDir = null; |
| 3048 | } |
| 3049 | } |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3050 | |
| 3051 | pkgSetting.uidError = uidError; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3052 | } |
| 3053 | |
| 3054 | // Perform shared library installation and dex validation and |
| 3055 | // optimization, if this is not a system app. |
| 3056 | if (mInstaller != null) { |
| 3057 | String path = scanFile.getPath(); |
| 3058 | if (scanFileNewer) { |
| 3059 | Log.i(TAG, path + " changed; unpacking"); |
| Dianne Hackborn | b181118 | 2009-05-21 15:45:42 -0700 | [diff] [blame] | 3060 | int err = cachePackageSharedLibsLI(pkg, dataPath, scanFile); |
| 3061 | if (err != PackageManager.INSTALL_SUCCEEDED) { |
| 3062 | mLastScanError = err; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3063 | return null; |
| 3064 | } |
| 3065 | } |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3066 | pkg.mScanPath = path; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3067 | |
| Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3068 | if ((scanMode&SCAN_NO_DEX) == 0) { |
| 3069 | if (performDexOptLI(pkg, forceDex) == DEX_OPT_FAILED) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3070 | mLastScanError = PackageManager.INSTALL_FAILED_DEXOPT; |
| 3071 | return null; |
| 3072 | } |
| 3073 | } |
| 3074 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3075 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3076 | if (mFactoryTest && pkg.requestedPermissions.contains( |
| 3077 | android.Manifest.permission.FACTORY_TEST)) { |
| 3078 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_FACTORY_TEST; |
| 3079 | } |
| 3080 | |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3081 | // Request the ActivityManager to kill the process(only for existing packages) |
| 3082 | // so that we do not end up in a confused state while the user is still using the older |
| 3083 | // version of the application while the new one gets installed. |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3084 | if ((parseFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3085 | killApplication(pkg.applicationInfo.packageName, |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3086 | pkg.applicationInfo.uid); |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3087 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3088 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3089 | synchronized (mPackages) { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 3090 | // We don't expect installation to fail beyond this point, |
| 3091 | if ((scanMode&SCAN_MONITOR) != 0) { |
| 3092 | mAppDirs.put(pkg.mPath, pkg); |
| 3093 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3094 | // Add the new setting to mSettings |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3095 | mSettings.insertPackageSettingLP(pkgSetting, pkg); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3096 | // Add the new setting to mPackages |
| Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 3097 | mPackages.put(pkg.applicationInfo.packageName, pkg); |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 3098 | // Make sure we don't accidentally delete its data. |
| 3099 | mSettings.mPackagesToBeCleaned.remove(pkgName); |
| 3100 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3101 | int N = pkg.providers.size(); |
| 3102 | StringBuilder r = null; |
| 3103 | int i; |
| 3104 | for (i=0; i<N; i++) { |
| 3105 | PackageParser.Provider p = pkg.providers.get(i); |
| 3106 | p.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 3107 | p.info.processName, pkg.applicationInfo.uid); |
| 3108 | mProvidersByComponent.put(new ComponentName(p.info.packageName, |
| 3109 | p.info.name), p); |
| 3110 | p.syncable = p.info.isSyncable; |
| Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3111 | if (p.info.authority != null) { |
| 3112 | String names[] = p.info.authority.split(";"); |
| 3113 | p.info.authority = null; |
| 3114 | for (int j = 0; j < names.length; j++) { |
| 3115 | if (j == 1 && p.syncable) { |
| 3116 | // We only want the first authority for a provider to possibly be |
| 3117 | // syncable, so if we already added this provider using a different |
| 3118 | // authority clear the syncable flag. We copy the provider before |
| 3119 | // changing it because the mProviders object contains a reference |
| 3120 | // to a provider that we don't want to change. |
| 3121 | // Only do this for the second authority since the resulting provider |
| 3122 | // object can be the same for all future authorities for this provider. |
| 3123 | p = new PackageParser.Provider(p); |
| 3124 | p.syncable = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3125 | } |
| Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3126 | if (!mProviders.containsKey(names[j])) { |
| 3127 | mProviders.put(names[j], p); |
| 3128 | if (p.info.authority == null) { |
| 3129 | p.info.authority = names[j]; |
| 3130 | } else { |
| 3131 | p.info.authority = p.info.authority + ";" + names[j]; |
| 3132 | } |
| 3133 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0 && Config.LOGD) |
| 3134 | Log.d(TAG, "Registered content provider: " + names[j] + |
| 3135 | ", className = " + p.info.name + |
| 3136 | ", isSyncable = " + p.info.isSyncable); |
| 3137 | } else { |
| 3138 | PackageParser.Provider other = mProviders.get(names[j]); |
| 3139 | Log.w(TAG, "Skipping provider name " + names[j] + |
| 3140 | " (in package " + pkg.applicationInfo.packageName + |
| 3141 | "): name already used by " |
| 3142 | + ((other != null && other.getComponentName() != null) |
| 3143 | ? other.getComponentName().getPackageName() : "?")); |
| 3144 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3148 | if (r == null) { |
| 3149 | r = new StringBuilder(256); |
| 3150 | } else { |
| 3151 | r.append(' '); |
| 3152 | } |
| 3153 | r.append(p.info.name); |
| 3154 | } |
| 3155 | } |
| 3156 | if (r != null) { |
| 3157 | if (Config.LOGD) Log.d(TAG, " Providers: " + r); |
| 3158 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3159 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3160 | N = pkg.services.size(); |
| 3161 | r = null; |
| 3162 | for (i=0; i<N; i++) { |
| 3163 | PackageParser.Service s = pkg.services.get(i); |
| 3164 | s.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 3165 | s.info.processName, pkg.applicationInfo.uid); |
| 3166 | mServices.addService(s); |
| 3167 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3168 | if (r == null) { |
| 3169 | r = new StringBuilder(256); |
| 3170 | } else { |
| 3171 | r.append(' '); |
| 3172 | } |
| 3173 | r.append(s.info.name); |
| 3174 | } |
| 3175 | } |
| 3176 | if (r != null) { |
| 3177 | if (Config.LOGD) Log.d(TAG, " Services: " + r); |
| 3178 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3179 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3180 | N = pkg.receivers.size(); |
| 3181 | r = null; |
| 3182 | for (i=0; i<N; i++) { |
| 3183 | PackageParser.Activity a = pkg.receivers.get(i); |
| 3184 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 3185 | a.info.processName, pkg.applicationInfo.uid); |
| 3186 | mReceivers.addActivity(a, "receiver"); |
| 3187 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3188 | if (r == null) { |
| 3189 | r = new StringBuilder(256); |
| 3190 | } else { |
| 3191 | r.append(' '); |
| 3192 | } |
| 3193 | r.append(a.info.name); |
| 3194 | } |
| 3195 | } |
| 3196 | if (r != null) { |
| 3197 | if (Config.LOGD) Log.d(TAG, " Receivers: " + r); |
| 3198 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3199 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3200 | N = pkg.activities.size(); |
| 3201 | r = null; |
| 3202 | for (i=0; i<N; i++) { |
| 3203 | PackageParser.Activity a = pkg.activities.get(i); |
| 3204 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 3205 | a.info.processName, pkg.applicationInfo.uid); |
| 3206 | mActivities.addActivity(a, "activity"); |
| 3207 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3208 | if (r == null) { |
| 3209 | r = new StringBuilder(256); |
| 3210 | } else { |
| 3211 | r.append(' '); |
| 3212 | } |
| 3213 | r.append(a.info.name); |
| 3214 | } |
| 3215 | } |
| 3216 | if (r != null) { |
| 3217 | if (Config.LOGD) Log.d(TAG, " Activities: " + r); |
| 3218 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3219 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3220 | N = pkg.permissionGroups.size(); |
| 3221 | r = null; |
| 3222 | for (i=0; i<N; i++) { |
| 3223 | PackageParser.PermissionGroup pg = pkg.permissionGroups.get(i); |
| 3224 | PackageParser.PermissionGroup cur = mPermissionGroups.get(pg.info.name); |
| 3225 | if (cur == null) { |
| 3226 | mPermissionGroups.put(pg.info.name, pg); |
| 3227 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3228 | if (r == null) { |
| 3229 | r = new StringBuilder(256); |
| 3230 | } else { |
| 3231 | r.append(' '); |
| 3232 | } |
| 3233 | r.append(pg.info.name); |
| 3234 | } |
| 3235 | } else { |
| 3236 | Log.w(TAG, "Permission group " + pg.info.name + " from package " |
| 3237 | + pg.info.packageName + " ignored: original from " |
| 3238 | + cur.info.packageName); |
| 3239 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3240 | if (r == null) { |
| 3241 | r = new StringBuilder(256); |
| 3242 | } else { |
| 3243 | r.append(' '); |
| 3244 | } |
| 3245 | r.append("DUP:"); |
| 3246 | r.append(pg.info.name); |
| 3247 | } |
| 3248 | } |
| 3249 | } |
| 3250 | if (r != null) { |
| 3251 | if (Config.LOGD) Log.d(TAG, " Permission Groups: " + r); |
| 3252 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3253 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3254 | N = pkg.permissions.size(); |
| 3255 | r = null; |
| 3256 | for (i=0; i<N; i++) { |
| 3257 | PackageParser.Permission p = pkg.permissions.get(i); |
| 3258 | HashMap<String, BasePermission> permissionMap = |
| 3259 | p.tree ? mSettings.mPermissionTrees |
| 3260 | : mSettings.mPermissions; |
| 3261 | p.group = mPermissionGroups.get(p.info.group); |
| 3262 | if (p.info.group == null || p.group != null) { |
| 3263 | BasePermission bp = permissionMap.get(p.info.name); |
| 3264 | if (bp == null) { |
| 3265 | bp = new BasePermission(p.info.name, p.info.packageName, |
| 3266 | BasePermission.TYPE_NORMAL); |
| 3267 | permissionMap.put(p.info.name, bp); |
| 3268 | } |
| 3269 | if (bp.perm == null) { |
| 3270 | if (bp.sourcePackage == null |
| 3271 | || bp.sourcePackage.equals(p.info.packageName)) { |
| 3272 | BasePermission tree = findPermissionTreeLP(p.info.name); |
| 3273 | if (tree == null |
| 3274 | || tree.sourcePackage.equals(p.info.packageName)) { |
| 3275 | bp.perm = p; |
| 3276 | bp.uid = pkg.applicationInfo.uid; |
| 3277 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3278 | if (r == null) { |
| 3279 | r = new StringBuilder(256); |
| 3280 | } else { |
| 3281 | r.append(' '); |
| 3282 | } |
| 3283 | r.append(p.info.name); |
| 3284 | } |
| 3285 | } else { |
| 3286 | Log.w(TAG, "Permission " + p.info.name + " from package " |
| 3287 | + p.info.packageName + " ignored: base tree " |
| 3288 | + tree.name + " is from package " |
| 3289 | + tree.sourcePackage); |
| 3290 | } |
| 3291 | } else { |
| 3292 | Log.w(TAG, "Permission " + p.info.name + " from package " |
| 3293 | + p.info.packageName + " ignored: original from " |
| 3294 | + bp.sourcePackage); |
| 3295 | } |
| 3296 | } else if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3297 | if (r == null) { |
| 3298 | r = new StringBuilder(256); |
| 3299 | } else { |
| 3300 | r.append(' '); |
| 3301 | } |
| 3302 | r.append("DUP:"); |
| 3303 | r.append(p.info.name); |
| 3304 | } |
| 3305 | } else { |
| 3306 | Log.w(TAG, "Permission " + p.info.name + " from package " |
| 3307 | + p.info.packageName + " ignored: no group " |
| 3308 | + p.group); |
| 3309 | } |
| 3310 | } |
| 3311 | if (r != null) { |
| 3312 | if (Config.LOGD) Log.d(TAG, " Permissions: " + r); |
| 3313 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3314 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3315 | N = pkg.instrumentation.size(); |
| 3316 | r = null; |
| 3317 | for (i=0; i<N; i++) { |
| 3318 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
| 3319 | a.info.packageName = pkg.applicationInfo.packageName; |
| 3320 | a.info.sourceDir = pkg.applicationInfo.sourceDir; |
| 3321 | a.info.publicSourceDir = pkg.applicationInfo.publicSourceDir; |
| 3322 | a.info.dataDir = pkg.applicationInfo.dataDir; |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3323 | mInstrumentation.put(a.getComponentName(), a); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3324 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3325 | if (r == null) { |
| 3326 | r = new StringBuilder(256); |
| 3327 | } else { |
| 3328 | r.append(' '); |
| 3329 | } |
| 3330 | r.append(a.info.name); |
| 3331 | } |
| 3332 | } |
| 3333 | if (r != null) { |
| 3334 | if (Config.LOGD) Log.d(TAG, " Instrumentation: " + r); |
| 3335 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3336 | |
| Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 3337 | if (pkg.protectedBroadcasts != null) { |
| 3338 | N = pkg.protectedBroadcasts.size(); |
| 3339 | for (i=0; i<N; i++) { |
| 3340 | mProtectedBroadcasts.add(pkg.protectedBroadcasts.get(i)); |
| 3341 | } |
| 3342 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3343 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3344 | pkgSetting.setTimeStamp(scanFileTime); |
| 3345 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3346 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3347 | return pkg; |
| 3348 | } |
| 3349 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3350 | private void killApplication(String pkgName, int uid) { |
| 3351 | // Request the ActivityManager to kill the process(only for existing packages) |
| 3352 | // so that we do not end up in a confused state while the user is still using the older |
| 3353 | // version of the application while the new one gets installed. |
| 3354 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 3355 | if (am != null) { |
| 3356 | try { |
| 3357 | am.killApplicationWithUid(pkgName, uid); |
| 3358 | } catch (RemoteException e) { |
| 3359 | } |
| 3360 | } |
| 3361 | } |
| 3362 | |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3363 | // The following constants are returned by cachePackageSharedLibsForAbiLI |
| 3364 | // to indicate if native shared libraries were found in the package. |
| 3365 | // Values are: |
| 3366 | // PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES => native libraries found and installed |
| 3367 | // PACKAGE_INSTALL_NATIVE_NO_LIBRARIES => no native libraries in package |
| 3368 | // PACKAGE_INSTALL_NATIVE_ABI_MISMATCH => native libraries for another ABI found |
| 3369 | // in package (and not installed) |
| 3370 | // |
| 3371 | private static final int PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES = 0; |
| 3372 | private static final int PACKAGE_INSTALL_NATIVE_NO_LIBRARIES = 1; |
| 3373 | private static final int PACKAGE_INSTALL_NATIVE_ABI_MISMATCH = 2; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3374 | |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3375 | // Find all files of the form lib/<cpuAbi>/lib<name>.so in the .apk |
| 3376 | // and automatically copy them to /data/data/<appname>/lib if present. |
| 3377 | // |
| 3378 | // NOTE: this method may throw an IOException if the library cannot |
| 3379 | // be copied to its final destination, e.g. if there isn't enough |
| 3380 | // room left on the data partition, or a ZipException if the package |
| 3381 | // file is malformed. |
| 3382 | // |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3383 | private int cachePackageSharedLibsForAbiLI(PackageParser.Package pkg, |
| 3384 | File dataPath, File scanFile, String cpuAbi) throws IOException, ZipException { |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3385 | File sharedLibraryDir = new File(dataPath.getPath() + "/lib"); |
| 3386 | final String apkLib = "lib/"; |
| 3387 | final int apkLibLen = apkLib.length(); |
| 3388 | final int cpuAbiLen = cpuAbi.length(); |
| 3389 | final String libPrefix = "lib"; |
| 3390 | final int libPrefixLen = libPrefix.length(); |
| 3391 | final String libSuffix = ".so"; |
| 3392 | final int libSuffixLen = libSuffix.length(); |
| 3393 | boolean hasNativeLibraries = false; |
| 3394 | boolean installedNativeLibraries = false; |
| 3395 | |
| 3396 | // the minimum length of a valid native shared library of the form |
| 3397 | // lib/<something>/lib<name>.so. |
| 3398 | final int minEntryLen = apkLibLen + 2 + libPrefixLen + 1 + libSuffixLen; |
| 3399 | |
| 3400 | ZipFile zipFile = new ZipFile(scanFile); |
| 3401 | Enumeration<ZipEntry> entries = |
| 3402 | (Enumeration<ZipEntry>) zipFile.entries(); |
| 3403 | |
| 3404 | while (entries.hasMoreElements()) { |
| 3405 | ZipEntry entry = entries.nextElement(); |
| 3406 | // skip directories |
| 3407 | if (entry.isDirectory()) { |
| 3408 | continue; |
| 3409 | } |
| 3410 | String entryName = entry.getName(); |
| 3411 | |
| 3412 | // check that the entry looks like lib/<something>/lib<name>.so |
| 3413 | // here, but don't check the ABI just yet. |
| 3414 | // |
| 3415 | // - must be sufficiently long |
| 3416 | // - must end with libSuffix, i.e. ".so" |
| 3417 | // - must start with apkLib, i.e. "lib/" |
| 3418 | if (entryName.length() < minEntryLen || |
| 3419 | !entryName.endsWith(libSuffix) || |
| 3420 | !entryName.startsWith(apkLib) ) { |
| 3421 | continue; |
| 3422 | } |
| 3423 | |
| 3424 | // file name must start with libPrefix, i.e. "lib" |
| 3425 | int lastSlash = entryName.lastIndexOf('/'); |
| 3426 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3427 | if (lastSlash < 0 || |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3428 | !entryName.regionMatches(lastSlash+1, libPrefix, 0, libPrefixLen) ) { |
| 3429 | continue; |
| 3430 | } |
| 3431 | |
| 3432 | hasNativeLibraries = true; |
| 3433 | |
| 3434 | // check the cpuAbi now, between lib/ and /lib<name>.so |
| 3435 | // |
| 3436 | if (lastSlash != apkLibLen + cpuAbiLen || |
| 3437 | !entryName.regionMatches(apkLibLen, cpuAbi, 0, cpuAbiLen) ) |
| 3438 | continue; |
| 3439 | |
| 3440 | // extract the library file name, ensure it doesn't contain |
| 3441 | // weird characters. we're guaranteed here that it doesn't contain |
| 3442 | // a directory separator though. |
| 3443 | String libFileName = entryName.substring(lastSlash+1); |
| 3444 | if (!FileUtils.isFilenameSafe(new File(libFileName))) { |
| 3445 | continue; |
| 3446 | } |
| 3447 | |
| 3448 | installedNativeLibraries = true; |
| 3449 | |
| 3450 | String sharedLibraryFilePath = sharedLibraryDir.getPath() + |
| 3451 | File.separator + libFileName; |
| 3452 | File sharedLibraryFile = new File(sharedLibraryFilePath); |
| 3453 | if (! sharedLibraryFile.exists() || |
| 3454 | sharedLibraryFile.length() != entry.getSize() || |
| 3455 | sharedLibraryFile.lastModified() != entry.getTime()) { |
| 3456 | if (Config.LOGD) { |
| 3457 | Log.d(TAG, "Caching shared lib " + entry.getName()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3458 | } |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3459 | if (mInstaller == null) { |
| 3460 | sharedLibraryDir.mkdir(); |
| Dianne Hackborn | b181118 | 2009-05-21 15:45:42 -0700 | [diff] [blame] | 3461 | } |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3462 | cacheNativeBinaryLI(pkg, zipFile, entry, sharedLibraryDir, |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3463 | sharedLibraryFile); |
| 3464 | } |
| 3465 | } |
| 3466 | if (!hasNativeLibraries) |
| 3467 | return PACKAGE_INSTALL_NATIVE_NO_LIBRARIES; |
| 3468 | |
| 3469 | if (!installedNativeLibraries) |
| 3470 | return PACKAGE_INSTALL_NATIVE_ABI_MISMATCH; |
| 3471 | |
| 3472 | return PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES; |
| 3473 | } |
| 3474 | |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3475 | // Find the gdbserver executable program in a package at |
| 3476 | // lib/<cpuAbi>/gdbserver and copy it to /data/data/<name>/lib/gdbserver |
| 3477 | // |
| 3478 | // Returns PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES on success, |
| 3479 | // or PACKAGE_INSTALL_NATIVE_NO_LIBRARIES otherwise. |
| 3480 | // |
| 3481 | private int cachePackageGdbServerLI(PackageParser.Package pkg, |
| 3482 | File dataPath, File scanFile, String cpuAbi) throws IOException, ZipException { |
| 3483 | File installGdbServerDir = new File(dataPath.getPath() + "/lib"); |
| 3484 | final String GDBSERVER = "gdbserver"; |
| 3485 | final String apkGdbServerPath = "lib/" + cpuAbi + "/" + GDBSERVER; |
| 3486 | |
| 3487 | ZipFile zipFile = new ZipFile(scanFile); |
| 3488 | Enumeration<ZipEntry> entries = |
| 3489 | (Enumeration<ZipEntry>) zipFile.entries(); |
| 3490 | |
| 3491 | while (entries.hasMoreElements()) { |
| 3492 | ZipEntry entry = entries.nextElement(); |
| 3493 | // skip directories |
| 3494 | if (entry.isDirectory()) { |
| 3495 | continue; |
| 3496 | } |
| 3497 | String entryName = entry.getName(); |
| 3498 | |
| 3499 | if (!entryName.equals(apkGdbServerPath)) { |
| 3500 | continue; |
| 3501 | } |
| 3502 | |
| 3503 | String installGdbServerPath = installGdbServerDir.getPath() + |
| 3504 | "/" + GDBSERVER; |
| 3505 | File installGdbServerFile = new File(installGdbServerPath); |
| 3506 | if (! installGdbServerFile.exists() || |
| 3507 | installGdbServerFile.length() != entry.getSize() || |
| 3508 | installGdbServerFile.lastModified() != entry.getTime()) { |
| 3509 | if (Config.LOGD) { |
| 3510 | Log.d(TAG, "Caching gdbserver " + entry.getName()); |
| 3511 | } |
| 3512 | if (mInstaller == null) { |
| 3513 | installGdbServerDir.mkdir(); |
| 3514 | } |
| 3515 | cacheNativeBinaryLI(pkg, zipFile, entry, installGdbServerDir, |
| 3516 | installGdbServerFile); |
| 3517 | } |
| 3518 | return PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES; |
| 3519 | } |
| 3520 | return PACKAGE_INSTALL_NATIVE_NO_LIBRARIES; |
| 3521 | } |
| 3522 | |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3523 | // extract shared libraries stored in the APK as lib/<cpuAbi>/lib<name>.so |
| 3524 | // and copy them to /data/data/<appname>/lib. |
| 3525 | // |
| 3526 | // This function will first try the main CPU ABI defined by Build.CPU_ABI |
| 3527 | // (which corresponds to ro.product.cpu.abi), and also try an alternate |
| 3528 | // one if ro.product.cpu.abi2 is defined. |
| 3529 | // |
| 3530 | private int cachePackageSharedLibsLI(PackageParser.Package pkg, |
| 3531 | File dataPath, File scanFile) { |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3532 | String cpuAbi = Build.CPU_ABI; |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3533 | try { |
| 3534 | int result = cachePackageSharedLibsForAbiLI(pkg, dataPath, scanFile, cpuAbi); |
| 3535 | |
| 3536 | // some architectures are capable of supporting several CPU ABIs |
| 3537 | // for example, 'armeabi-v7a' also supports 'armeabi' native code |
| 3538 | // this is indicated by the definition of the ro.product.cpu.abi2 |
| 3539 | // system property. |
| 3540 | // |
| 3541 | // only scan the package twice in case of ABI mismatch |
| 3542 | if (result == PACKAGE_INSTALL_NATIVE_ABI_MISMATCH) { |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3543 | final String cpuAbi2 = SystemProperties.get("ro.product.cpu.abi2",null); |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3544 | if (cpuAbi2 != null) { |
| 3545 | result = cachePackageSharedLibsForAbiLI(pkg, dataPath, scanFile, cpuAbi2); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3546 | } |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3547 | |
| 3548 | if (result == PACKAGE_INSTALL_NATIVE_ABI_MISMATCH) { |
| 3549 | Log.w(TAG,"Native ABI mismatch from package file"); |
| 3550 | return PackageManager.INSTALL_FAILED_INVALID_APK; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3551 | } |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3552 | |
| 3553 | if (result == PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES) { |
| 3554 | cpuAbi = cpuAbi2; |
| 3555 | } |
| 3556 | } |
| 3557 | |
| 3558 | // for debuggable packages, also extract gdbserver from lib/<abi> |
| 3559 | // into /data/data/<appname>/lib too. |
| 3560 | if (result == PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES && |
| 3561 | (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 3562 | int result2 = cachePackageGdbServerLI(pkg, dataPath, scanFile, cpuAbi); |
| 3563 | if (result2 == PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES) { |
| 3564 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_NATIVE_DEBUGGABLE; |
| 3565 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3566 | } |
| David 'Digit' Turner | feba743 | 2009-11-06 17:54:12 -0800 | [diff] [blame] | 3567 | } catch (ZipException e) { |
| 3568 | Log.w(TAG, "Failed to extract data from package file", e); |
| 3569 | return PackageManager.INSTALL_FAILED_INVALID_APK; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3570 | } catch (IOException e) { |
| Dianne Hackborn | b181118 | 2009-05-21 15:45:42 -0700 | [diff] [blame] | 3571 | Log.w(TAG, "Failed to cache package shared libs", e); |
| 3572 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3573 | } |
| Dianne Hackborn | b181118 | 2009-05-21 15:45:42 -0700 | [diff] [blame] | 3574 | return PackageManager.INSTALL_SUCCEEDED; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3575 | } |
| 3576 | |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3577 | private void cacheNativeBinaryLI(PackageParser.Package pkg, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3578 | ZipFile zipFile, ZipEntry entry, |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3579 | File binaryDir, |
| 3580 | File binaryFile) throws IOException { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3581 | InputStream inputStream = zipFile.getInputStream(entry); |
| 3582 | try { |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3583 | File tempFile = File.createTempFile("tmp", "tmp", binaryDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3584 | String tempFilePath = tempFile.getPath(); |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3585 | // XXX package manager can't change owner, so the executable files for |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3586 | // now need to be left as world readable and owned by the system. |
| 3587 | if (! FileUtils.copyToFile(inputStream, tempFile) || |
| 3588 | ! tempFile.setLastModified(entry.getTime()) || |
| 3589 | FileUtils.setPermissions(tempFilePath, |
| 3590 | FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3591 | |FileUtils.S_IXUSR|FileUtils.S_IXGRP|FileUtils.S_IXOTH |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3592 | |FileUtils.S_IROTH, -1, -1) != 0 || |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3593 | ! tempFile.renameTo(binaryFile)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3594 | // Failed to properly write file. |
| 3595 | tempFile.delete(); |
| David 'Digit' Turner | 1edab2b | 2010-01-21 15:15:23 -0800 | [diff] [blame] | 3596 | throw new IOException("Couldn't create cached binary " |
| 3597 | + binaryFile + " in " + binaryDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3598 | } |
| 3599 | } finally { |
| 3600 | inputStream.close(); |
| 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | void removePackageLI(PackageParser.Package pkg, boolean chatty) { |
| 3605 | if (chatty && Config.LOGD) Log.d( |
| 3606 | TAG, "Removing package " + pkg.applicationInfo.packageName ); |
| 3607 | |
| 3608 | synchronized (mPackages) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3609 | clearPackagePreferredActivitiesLP(pkg.packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3610 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3611 | mPackages.remove(pkg.applicationInfo.packageName); |
| 3612 | if (pkg.mPath != null) { |
| 3613 | mAppDirs.remove(pkg.mPath); |
| 3614 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3615 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3616 | PackageSetting ps = (PackageSetting)pkg.mExtras; |
| 3617 | if (ps != null && ps.sharedUser != null) { |
| 3618 | // XXX don't do this until the data is removed. |
| 3619 | if (false) { |
| 3620 | ps.sharedUser.packages.remove(ps); |
| 3621 | if (ps.sharedUser.packages.size() == 0) { |
| 3622 | // Remove. |
| 3623 | } |
| 3624 | } |
| 3625 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3626 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3627 | int N = pkg.providers.size(); |
| 3628 | StringBuilder r = null; |
| 3629 | int i; |
| 3630 | for (i=0; i<N; i++) { |
| 3631 | PackageParser.Provider p = pkg.providers.get(i); |
| 3632 | mProvidersByComponent.remove(new ComponentName(p.info.packageName, |
| 3633 | p.info.name)); |
| 3634 | if (p.info.authority == null) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3635 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3636 | /* The is another ContentProvider with this authority when |
| 3637 | * this app was installed so this authority is null, |
| 3638 | * Ignore it as we don't have to unregister the provider. |
| 3639 | */ |
| 3640 | continue; |
| 3641 | } |
| 3642 | String names[] = p.info.authority.split(";"); |
| 3643 | for (int j = 0; j < names.length; j++) { |
| 3644 | if (mProviders.get(names[j]) == p) { |
| 3645 | mProviders.remove(names[j]); |
| 3646 | if (chatty && Config.LOGD) Log.d( |
| 3647 | TAG, "Unregistered content provider: " + names[j] + |
| 3648 | ", className = " + p.info.name + |
| 3649 | ", isSyncable = " + p.info.isSyncable); |
| 3650 | } |
| 3651 | } |
| 3652 | if (chatty) { |
| 3653 | if (r == null) { |
| 3654 | r = new StringBuilder(256); |
| 3655 | } else { |
| 3656 | r.append(' '); |
| 3657 | } |
| 3658 | r.append(p.info.name); |
| 3659 | } |
| 3660 | } |
| 3661 | if (r != null) { |
| 3662 | if (Config.LOGD) Log.d(TAG, " Providers: " + r); |
| 3663 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3664 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3665 | N = pkg.services.size(); |
| 3666 | r = null; |
| 3667 | for (i=0; i<N; i++) { |
| 3668 | PackageParser.Service s = pkg.services.get(i); |
| 3669 | mServices.removeService(s); |
| 3670 | if (chatty) { |
| 3671 | if (r == null) { |
| 3672 | r = new StringBuilder(256); |
| 3673 | } else { |
| 3674 | r.append(' '); |
| 3675 | } |
| 3676 | r.append(s.info.name); |
| 3677 | } |
| 3678 | } |
| 3679 | if (r != null) { |
| 3680 | if (Config.LOGD) Log.d(TAG, " Services: " + r); |
| 3681 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3682 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3683 | N = pkg.receivers.size(); |
| 3684 | r = null; |
| 3685 | for (i=0; i<N; i++) { |
| 3686 | PackageParser.Activity a = pkg.receivers.get(i); |
| 3687 | mReceivers.removeActivity(a, "receiver"); |
| 3688 | if (chatty) { |
| 3689 | if (r == null) { |
| 3690 | r = new StringBuilder(256); |
| 3691 | } else { |
| 3692 | r.append(' '); |
| 3693 | } |
| 3694 | r.append(a.info.name); |
| 3695 | } |
| 3696 | } |
| 3697 | if (r != null) { |
| 3698 | if (Config.LOGD) Log.d(TAG, " Receivers: " + r); |
| 3699 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3700 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3701 | N = pkg.activities.size(); |
| 3702 | r = null; |
| 3703 | for (i=0; i<N; i++) { |
| 3704 | PackageParser.Activity a = pkg.activities.get(i); |
| 3705 | mActivities.removeActivity(a, "activity"); |
| 3706 | if (chatty) { |
| 3707 | if (r == null) { |
| 3708 | r = new StringBuilder(256); |
| 3709 | } else { |
| 3710 | r.append(' '); |
| 3711 | } |
| 3712 | r.append(a.info.name); |
| 3713 | } |
| 3714 | } |
| 3715 | if (r != null) { |
| 3716 | if (Config.LOGD) Log.d(TAG, " Activities: " + r); |
| 3717 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3718 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3719 | N = pkg.permissions.size(); |
| 3720 | r = null; |
| 3721 | for (i=0; i<N; i++) { |
| 3722 | PackageParser.Permission p = pkg.permissions.get(i); |
| 3723 | boolean tree = false; |
| 3724 | BasePermission bp = mSettings.mPermissions.get(p.info.name); |
| 3725 | if (bp == null) { |
| 3726 | tree = true; |
| 3727 | bp = mSettings.mPermissionTrees.get(p.info.name); |
| 3728 | } |
| 3729 | if (bp != null && bp.perm == p) { |
| 3730 | if (bp.type != BasePermission.TYPE_BUILTIN) { |
| 3731 | if (tree) { |
| 3732 | mSettings.mPermissionTrees.remove(p.info.name); |
| 3733 | } else { |
| 3734 | mSettings.mPermissions.remove(p.info.name); |
| 3735 | } |
| 3736 | } else { |
| 3737 | bp.perm = null; |
| 3738 | } |
| 3739 | if (chatty) { |
| 3740 | if (r == null) { |
| 3741 | r = new StringBuilder(256); |
| 3742 | } else { |
| 3743 | r.append(' '); |
| 3744 | } |
| 3745 | r.append(p.info.name); |
| 3746 | } |
| 3747 | } |
| 3748 | } |
| 3749 | if (r != null) { |
| 3750 | if (Config.LOGD) Log.d(TAG, " Permissions: " + r); |
| 3751 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3752 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3753 | N = pkg.instrumentation.size(); |
| 3754 | r = null; |
| 3755 | for (i=0; i<N; i++) { |
| 3756 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3757 | mInstrumentation.remove(a.getComponentName()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3758 | if (chatty) { |
| 3759 | if (r == null) { |
| 3760 | r = new StringBuilder(256); |
| 3761 | } else { |
| 3762 | r.append(' '); |
| 3763 | } |
| 3764 | r.append(a.info.name); |
| 3765 | } |
| 3766 | } |
| 3767 | if (r != null) { |
| 3768 | if (Config.LOGD) Log.d(TAG, " Instrumentation: " + r); |
| 3769 | } |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | private static final boolean isPackageFilename(String name) { |
| 3774 | return name != null && name.endsWith(".apk"); |
| 3775 | } |
| 3776 | |
| 3777 | private void updatePermissionsLP() { |
| 3778 | // Make sure there are no dangling permission trees. |
| 3779 | Iterator<BasePermission> it = mSettings.mPermissionTrees |
| 3780 | .values().iterator(); |
| 3781 | while (it.hasNext()) { |
| 3782 | BasePermission bp = it.next(); |
| 3783 | if (bp.perm == null) { |
| 3784 | Log.w(TAG, "Removing dangling permission tree: " + bp.name |
| 3785 | + " from package " + bp.sourcePackage); |
| 3786 | it.remove(); |
| 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | // Make sure all dynamic permissions have been assigned to a package, |
| 3791 | // and make sure there are no dangling permissions. |
| 3792 | it = mSettings.mPermissions.values().iterator(); |
| 3793 | while (it.hasNext()) { |
| 3794 | BasePermission bp = it.next(); |
| 3795 | if (bp.type == BasePermission.TYPE_DYNAMIC) { |
| 3796 | if (DEBUG_SETTINGS) Log.v(TAG, "Dynamic permission: name=" |
| 3797 | + bp.name + " pkg=" + bp.sourcePackage |
| 3798 | + " info=" + bp.pendingInfo); |
| 3799 | if (bp.perm == null && bp.pendingInfo != null) { |
| 3800 | BasePermission tree = findPermissionTreeLP(bp.name); |
| 3801 | if (tree != null) { |
| 3802 | bp.perm = new PackageParser.Permission(tree.perm.owner, |
| 3803 | new PermissionInfo(bp.pendingInfo)); |
| 3804 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 3805 | bp.perm.info.name = bp.name; |
| 3806 | bp.uid = tree.uid; |
| 3807 | } |
| 3808 | } |
| 3809 | } |
| 3810 | if (bp.perm == null) { |
| 3811 | Log.w(TAG, "Removing dangling permission: " + bp.name |
| 3812 | + " from package " + bp.sourcePackage); |
| 3813 | it.remove(); |
| 3814 | } |
| 3815 | } |
| 3816 | |
| 3817 | // Now update the permissions for all packages, in particular |
| 3818 | // replace the granted permissions of the system packages. |
| 3819 | for (PackageParser.Package pkg : mPackages.values()) { |
| 3820 | grantPermissionsLP(pkg, false); |
| 3821 | } |
| 3822 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3823 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3824 | private void grantPermissionsLP(PackageParser.Package pkg, boolean replace) { |
| 3825 | final PackageSetting ps = (PackageSetting)pkg.mExtras; |
| 3826 | if (ps == null) { |
| 3827 | return; |
| 3828 | } |
| 3829 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 3830 | boolean addedPermission = false; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3831 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3832 | if (replace) { |
| 3833 | ps.permissionsFixed = false; |
| 3834 | if (gp == ps) { |
| 3835 | gp.grantedPermissions.clear(); |
| 3836 | gp.gids = mGlobalGids; |
| 3837 | } |
| 3838 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3839 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3840 | if (gp.gids == null) { |
| 3841 | gp.gids = mGlobalGids; |
| 3842 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3843 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3844 | final int N = pkg.requestedPermissions.size(); |
| 3845 | for (int i=0; i<N; i++) { |
| 3846 | String name = pkg.requestedPermissions.get(i); |
| 3847 | BasePermission bp = mSettings.mPermissions.get(name); |
| 3848 | PackageParser.Permission p = bp != null ? bp.perm : null; |
| 3849 | if (false) { |
| 3850 | if (gp != ps) { |
| 3851 | Log.i(TAG, "Package " + pkg.packageName + " checking " + name |
| 3852 | + ": " + p); |
| 3853 | } |
| 3854 | } |
| 3855 | if (p != null) { |
| 3856 | final String perm = p.info.name; |
| 3857 | boolean allowed; |
| 3858 | if (p.info.protectionLevel == PermissionInfo.PROTECTION_NORMAL |
| 3859 | || p.info.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS) { |
| 3860 | allowed = true; |
| 3861 | } else if (p.info.protectionLevel == PermissionInfo.PROTECTION_SIGNATURE |
| 3862 | || p.info.protectionLevel == PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM) { |
| Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 3863 | allowed = (checkSignaturesLP(p.owner.mSignatures, pkg.mSignatures) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3864 | == PackageManager.SIGNATURE_MATCH) |
| Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 3865 | || (checkSignaturesLP(mPlatformPackage.mSignatures, pkg.mSignatures) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3866 | == PackageManager.SIGNATURE_MATCH); |
| 3867 | if (p.info.protectionLevel == PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM) { |
| 3868 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 3869 | // For updated system applications, the signatureOrSystem permission |
| 3870 | // is granted only if it had been defined by the original application. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3871 | if ((pkg.applicationInfo.flags |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3872 | & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) { |
| 3873 | PackageSetting sysPs = mSettings.getDisabledSystemPkg(pkg.packageName); |
| 3874 | if(sysPs.grantedPermissions.contains(perm)) { |
| 3875 | allowed = true; |
| 3876 | } else { |
| 3877 | allowed = false; |
| 3878 | } |
| 3879 | } else { |
| 3880 | allowed = true; |
| 3881 | } |
| 3882 | } |
| 3883 | } |
| 3884 | } else { |
| 3885 | allowed = false; |
| 3886 | } |
| 3887 | if (false) { |
| 3888 | if (gp != ps) { |
| 3889 | Log.i(TAG, "Package " + pkg.packageName + " granting " + perm); |
| 3890 | } |
| 3891 | } |
| 3892 | if (allowed) { |
| 3893 | if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0 |
| 3894 | && ps.permissionsFixed) { |
| 3895 | // If this is an existing, non-system package, then |
| 3896 | // we can't add any new permissions to it. |
| 3897 | if (!gp.loadedPermissions.contains(perm)) { |
| 3898 | allowed = false; |
| Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 3899 | // Except... if this is a permission that was added |
| 3900 | // to the platform (note: need to only do this when |
| 3901 | // updating the platform). |
| 3902 | final int NP = PackageParser.NEW_PERMISSIONS.length; |
| 3903 | for (int ip=0; ip<NP; ip++) { |
| 3904 | final PackageParser.NewPermissionInfo npi |
| 3905 | = PackageParser.NEW_PERMISSIONS[ip]; |
| 3906 | if (npi.name.equals(perm) |
| 3907 | && pkg.applicationInfo.targetSdkVersion < npi.sdkVersion) { |
| 3908 | allowed = true; |
| San Mehat | 5a3a77d | 2009-06-01 09:25:28 -0700 | [diff] [blame] | 3909 | Log.i(TAG, "Auto-granting WRITE_EXTERNAL_STORAGE to old pkg " |
| Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 3910 | + pkg.packageName); |
| 3911 | break; |
| 3912 | } |
| 3913 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | if (allowed) { |
| 3917 | if (!gp.grantedPermissions.contains(perm)) { |
| 3918 | addedPermission = true; |
| 3919 | gp.grantedPermissions.add(perm); |
| 3920 | gp.gids = appendInts(gp.gids, bp.gids); |
| 3921 | } |
| 3922 | } else { |
| 3923 | Log.w(TAG, "Not granting permission " + perm |
| 3924 | + " to package " + pkg.packageName |
| 3925 | + " because it was previously installed without"); |
| 3926 | } |
| 3927 | } else { |
| 3928 | Log.w(TAG, "Not granting permission " + perm |
| 3929 | + " to package " + pkg.packageName |
| 3930 | + " (protectionLevel=" + p.info.protectionLevel |
| 3931 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 3932 | + ")"); |
| 3933 | } |
| 3934 | } else { |
| 3935 | Log.w(TAG, "Unknown permission " + name |
| 3936 | + " in package " + pkg.packageName); |
| 3937 | } |
| 3938 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3939 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3940 | if ((addedPermission || replace) && !ps.permissionsFixed && |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3941 | ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) || |
| 3942 | ((ps.pkgFlags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)){ |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3943 | // This is the first that we have heard about this package, so the |
| 3944 | // permissions we have now selected are fixed until explicitly |
| 3945 | // changed. |
| 3946 | ps.permissionsFixed = true; |
| 3947 | gp.loadedPermissions = new HashSet<String>(gp.grantedPermissions); |
| 3948 | } |
| 3949 | } |
| 3950 | |
| 3951 | private final class ActivityIntentResolver |
| 3952 | extends IntentResolver<PackageParser.ActivityIntentInfo, ResolveInfo> { |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 3953 | public List queryIntent(Intent intent, String resolvedType, boolean defaultOnly) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3954 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 3955 | return super.queryIntent(intent, resolvedType, defaultOnly); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3956 | } |
| 3957 | |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 3958 | public List queryIntent(Intent intent, String resolvedType, int flags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3959 | mFlags = flags; |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 3960 | return super.queryIntent(intent, resolvedType, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3961 | (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0); |
| 3962 | } |
| 3963 | |
| Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 3964 | public List queryIntentForPackage(Intent intent, String resolvedType, int flags, |
| 3965 | ArrayList<PackageParser.Activity> packageActivities) { |
| 3966 | if (packageActivities == null) { |
| 3967 | return null; |
| 3968 | } |
| 3969 | mFlags = flags; |
| 3970 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
| 3971 | int N = packageActivities.size(); |
| 3972 | ArrayList<ArrayList<PackageParser.ActivityIntentInfo>> listCut = |
| 3973 | new ArrayList<ArrayList<PackageParser.ActivityIntentInfo>>(N); |
| Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 3974 | |
| 3975 | ArrayList<PackageParser.ActivityIntentInfo> intentFilters; |
| Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 3976 | for (int i = 0; i < N; ++i) { |
| Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 3977 | intentFilters = packageActivities.get(i).intents; |
| 3978 | if (intentFilters != null && intentFilters.size() > 0) { |
| 3979 | listCut.add(intentFilters); |
| 3980 | } |
| Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 3981 | } |
| 3982 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut); |
| 3983 | } |
| 3984 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3985 | public final void addActivity(PackageParser.Activity a, String type) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3986 | mActivities.put(a.getComponentName(), a); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3987 | if (SHOW_INFO || Config.LOGV) Log.v( |
| 3988 | TAG, " " + type + " " + |
| 3989 | (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel : a.info.name) + ":"); |
| 3990 | if (SHOW_INFO || Config.LOGV) Log.v(TAG, " Class=" + a.info.name); |
| 3991 | int NI = a.intents.size(); |
| Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 3992 | for (int j=0; j<NI; j++) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3993 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
| 3994 | if (SHOW_INFO || Config.LOGV) { |
| 3995 | Log.v(TAG, " IntentFilter:"); |
| 3996 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 3997 | } |
| 3998 | if (!intent.debugCheck()) { |
| 3999 | Log.w(TAG, "==> For Activity " + a.info.name); |
| 4000 | } |
| 4001 | addFilter(intent); |
| 4002 | } |
| 4003 | } |
| 4004 | |
| 4005 | public final void removeActivity(PackageParser.Activity a, String type) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4006 | mActivities.remove(a.getComponentName()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4007 | if (SHOW_INFO || Config.LOGV) Log.v( |
| 4008 | TAG, " " + type + " " + |
| 4009 | (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel : a.info.name) + ":"); |
| 4010 | if (SHOW_INFO || Config.LOGV) Log.v(TAG, " Class=" + a.info.name); |
| 4011 | int NI = a.intents.size(); |
| Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4012 | for (int j=0; j<NI; j++) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4013 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
| 4014 | if (SHOW_INFO || Config.LOGV) { |
| 4015 | Log.v(TAG, " IntentFilter:"); |
| 4016 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4017 | } |
| 4018 | removeFilter(intent); |
| 4019 | } |
| 4020 | } |
| 4021 | |
| 4022 | @Override |
| 4023 | protected boolean allowFilterResult( |
| 4024 | PackageParser.ActivityIntentInfo filter, List<ResolveInfo> dest) { |
| 4025 | ActivityInfo filterAi = filter.activity.info; |
| 4026 | for (int i=dest.size()-1; i>=0; i--) { |
| 4027 | ActivityInfo destAi = dest.get(i).activityInfo; |
| 4028 | if (destAi.name == filterAi.name |
| 4029 | && destAi.packageName == filterAi.packageName) { |
| 4030 | return false; |
| 4031 | } |
| 4032 | } |
| 4033 | return true; |
| 4034 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4035 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4036 | @Override |
| 4037 | protected ResolveInfo newResult(PackageParser.ActivityIntentInfo info, |
| 4038 | int match) { |
| 4039 | if (!mSettings.isEnabledLP(info.activity.info, mFlags)) { |
| 4040 | return null; |
| 4041 | } |
| 4042 | final PackageParser.Activity activity = info.activity; |
| 4043 | if (mSafeMode && (activity.info.applicationInfo.flags |
| 4044 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 4045 | return null; |
| 4046 | } |
| 4047 | final ResolveInfo res = new ResolveInfo(); |
| 4048 | res.activityInfo = PackageParser.generateActivityInfo(activity, |
| 4049 | mFlags); |
| 4050 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 4051 | res.filter = info; |
| 4052 | } |
| 4053 | res.priority = info.getPriority(); |
| 4054 | res.preferredOrder = activity.owner.mPreferredOrder; |
| 4055 | //System.out.println("Result: " + res.activityInfo.className + |
| 4056 | // " = " + res.priority); |
| 4057 | res.match = match; |
| 4058 | res.isDefault = info.hasDefault; |
| 4059 | res.labelRes = info.labelRes; |
| 4060 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 4061 | res.icon = info.icon; |
| 4062 | return res; |
| 4063 | } |
| 4064 | |
| 4065 | @Override |
| 4066 | protected void sortResults(List<ResolveInfo> results) { |
| 4067 | Collections.sort(results, mResolvePrioritySorter); |
| 4068 | } |
| 4069 | |
| 4070 | @Override |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4071 | protected void dumpFilter(PrintWriter out, String prefix, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4072 | PackageParser.ActivityIntentInfo filter) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4073 | out.print(prefix); out.print( |
| 4074 | Integer.toHexString(System.identityHashCode(filter.activity))); |
| 4075 | out.print(' '); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4076 | out.println(filter.activity.getComponentShortName()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 4080 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 4081 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 4082 | // while (i.hasNext()) { |
| 4083 | // final ResolveInfo resolveInfo = i.next(); |
| 4084 | // if (isEnabledLP(resolveInfo.activityInfo)) { |
| 4085 | // retList.add(resolveInfo); |
| 4086 | // } |
| 4087 | // } |
| 4088 | // return retList; |
| 4089 | // } |
| 4090 | |
| 4091 | // Keys are String (activity class name), values are Activity. |
| 4092 | private final HashMap<ComponentName, PackageParser.Activity> mActivities |
| 4093 | = new HashMap<ComponentName, PackageParser.Activity>(); |
| 4094 | private int mFlags; |
| 4095 | } |
| 4096 | |
| 4097 | private final class ServiceIntentResolver |
| 4098 | extends IntentResolver<PackageParser.ServiceIntentInfo, ResolveInfo> { |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4099 | public List queryIntent(Intent intent, String resolvedType, boolean defaultOnly) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4100 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4101 | return super.queryIntent(intent, resolvedType, defaultOnly); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4102 | } |
| 4103 | |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4104 | public List queryIntent(Intent intent, String resolvedType, int flags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4105 | mFlags = flags; |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4106 | return super.queryIntent(intent, resolvedType, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4107 | (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0); |
| 4108 | } |
| 4109 | |
| Dianne Hackborn | c14b9ccd | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4110 | public List queryIntentForPackage(Intent intent, String resolvedType, int flags, |
| 4111 | ArrayList<PackageParser.Service> packageServices) { |
| 4112 | if (packageServices == null) { |
| 4113 | return null; |
| 4114 | } |
| 4115 | mFlags = flags; |
| 4116 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
| 4117 | int N = packageServices.size(); |
| 4118 | ArrayList<ArrayList<PackageParser.ServiceIntentInfo>> listCut = |
| 4119 | new ArrayList<ArrayList<PackageParser.ServiceIntentInfo>>(N); |
| 4120 | |
| 4121 | ArrayList<PackageParser.ServiceIntentInfo> intentFilters; |
| 4122 | for (int i = 0; i < N; ++i) { |
| 4123 | intentFilters = packageServices.get(i).intents; |
| 4124 | if (intentFilters != null && intentFilters.size() > 0) { |
| 4125 | listCut.add(intentFilters); |
| 4126 | } |
| 4127 | } |
| 4128 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut); |
| 4129 | } |
| 4130 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4131 | public final void addService(PackageParser.Service s) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4132 | mServices.put(s.getComponentName(), s); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4133 | if (SHOW_INFO || Config.LOGV) Log.v( |
| 4134 | TAG, " " + (s.info.nonLocalizedLabel != null |
| 4135 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
| 4136 | if (SHOW_INFO || Config.LOGV) Log.v( |
| 4137 | TAG, " Class=" + s.info.name); |
| 4138 | int NI = s.intents.size(); |
| 4139 | int j; |
| 4140 | for (j=0; j<NI; j++) { |
| 4141 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
| 4142 | if (SHOW_INFO || Config.LOGV) { |
| 4143 | Log.v(TAG, " IntentFilter:"); |
| 4144 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4145 | } |
| 4146 | if (!intent.debugCheck()) { |
| 4147 | Log.w(TAG, "==> For Service " + s.info.name); |
| 4148 | } |
| 4149 | addFilter(intent); |
| 4150 | } |
| 4151 | } |
| 4152 | |
| 4153 | public final void removeService(PackageParser.Service s) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4154 | mServices.remove(s.getComponentName()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4155 | if (SHOW_INFO || Config.LOGV) Log.v( |
| 4156 | TAG, " " + (s.info.nonLocalizedLabel != null |
| 4157 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
| 4158 | if (SHOW_INFO || Config.LOGV) Log.v( |
| 4159 | TAG, " Class=" + s.info.name); |
| 4160 | int NI = s.intents.size(); |
| 4161 | int j; |
| 4162 | for (j=0; j<NI; j++) { |
| 4163 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
| 4164 | if (SHOW_INFO || Config.LOGV) { |
| 4165 | Log.v(TAG, " IntentFilter:"); |
| 4166 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4167 | } |
| 4168 | removeFilter(intent); |
| 4169 | } |
| 4170 | } |
| 4171 | |
| 4172 | @Override |
| 4173 | protected boolean allowFilterResult( |
| 4174 | PackageParser.ServiceIntentInfo filter, List<ResolveInfo> dest) { |
| 4175 | ServiceInfo filterSi = filter.service.info; |
| 4176 | for (int i=dest.size()-1; i>=0; i--) { |
| 4177 | ServiceInfo destAi = dest.get(i).serviceInfo; |
| 4178 | if (destAi.name == filterSi.name |
| 4179 | && destAi.packageName == filterSi.packageName) { |
| 4180 | return false; |
| 4181 | } |
| 4182 | } |
| 4183 | return true; |
| 4184 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4185 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4186 | @Override |
| 4187 | protected ResolveInfo newResult(PackageParser.ServiceIntentInfo filter, |
| 4188 | int match) { |
| 4189 | final PackageParser.ServiceIntentInfo info = (PackageParser.ServiceIntentInfo)filter; |
| 4190 | if (!mSettings.isEnabledLP(info.service.info, mFlags)) { |
| 4191 | return null; |
| 4192 | } |
| 4193 | final PackageParser.Service service = info.service; |
| 4194 | if (mSafeMode && (service.info.applicationInfo.flags |
| 4195 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 4196 | return null; |
| 4197 | } |
| 4198 | final ResolveInfo res = new ResolveInfo(); |
| 4199 | res.serviceInfo = PackageParser.generateServiceInfo(service, |
| 4200 | mFlags); |
| 4201 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 4202 | res.filter = filter; |
| 4203 | } |
| 4204 | res.priority = info.getPriority(); |
| 4205 | res.preferredOrder = service.owner.mPreferredOrder; |
| 4206 | //System.out.println("Result: " + res.activityInfo.className + |
| 4207 | // " = " + res.priority); |
| 4208 | res.match = match; |
| 4209 | res.isDefault = info.hasDefault; |
| 4210 | res.labelRes = info.labelRes; |
| 4211 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 4212 | res.icon = info.icon; |
| 4213 | return res; |
| 4214 | } |
| 4215 | |
| 4216 | @Override |
| 4217 | protected void sortResults(List<ResolveInfo> results) { |
| 4218 | Collections.sort(results, mResolvePrioritySorter); |
| 4219 | } |
| 4220 | |
| 4221 | @Override |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4222 | protected void dumpFilter(PrintWriter out, String prefix, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4223 | PackageParser.ServiceIntentInfo filter) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4224 | out.print(prefix); out.print( |
| 4225 | Integer.toHexString(System.identityHashCode(filter.service))); |
| 4226 | out.print(' '); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4227 | out.println(filter.service.getComponentShortName()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 4231 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 4232 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 4233 | // while (i.hasNext()) { |
| 4234 | // final ResolveInfo resolveInfo = (ResolveInfo) i; |
| 4235 | // if (isEnabledLP(resolveInfo.serviceInfo)) { |
| 4236 | // retList.add(resolveInfo); |
| 4237 | // } |
| 4238 | // } |
| 4239 | // return retList; |
| 4240 | // } |
| 4241 | |
| 4242 | // Keys are String (activity class name), values are Activity. |
| 4243 | private final HashMap<ComponentName, PackageParser.Service> mServices |
| 4244 | = new HashMap<ComponentName, PackageParser.Service>(); |
| 4245 | private int mFlags; |
| 4246 | }; |
| 4247 | |
| 4248 | private static final Comparator<ResolveInfo> mResolvePrioritySorter = |
| 4249 | new Comparator<ResolveInfo>() { |
| 4250 | public int compare(ResolveInfo r1, ResolveInfo r2) { |
| 4251 | int v1 = r1.priority; |
| 4252 | int v2 = r2.priority; |
| 4253 | //System.out.println("Comparing: q1=" + q1 + " q2=" + q2); |
| 4254 | if (v1 != v2) { |
| 4255 | return (v1 > v2) ? -1 : 1; |
| 4256 | } |
| 4257 | v1 = r1.preferredOrder; |
| 4258 | v2 = r2.preferredOrder; |
| 4259 | if (v1 != v2) { |
| 4260 | return (v1 > v2) ? -1 : 1; |
| 4261 | } |
| 4262 | if (r1.isDefault != r2.isDefault) { |
| 4263 | return r1.isDefault ? -1 : 1; |
| 4264 | } |
| 4265 | v1 = r1.match; |
| 4266 | v2 = r2.match; |
| 4267 | //System.out.println("Comparing: m1=" + m1 + " m2=" + m2); |
| 4268 | return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0); |
| 4269 | } |
| 4270 | }; |
| 4271 | |
| 4272 | private static final Comparator<ProviderInfo> mProviderInitOrderSorter = |
| 4273 | new Comparator<ProviderInfo>() { |
| 4274 | public int compare(ProviderInfo p1, ProviderInfo p2) { |
| 4275 | final int v1 = p1.initOrder; |
| 4276 | final int v2 = p2.initOrder; |
| 4277 | return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0); |
| 4278 | } |
| 4279 | }; |
| 4280 | |
| 4281 | private static final void sendPackageBroadcast(String action, String pkg, Bundle extras) { |
| 4282 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 4283 | if (am != null) { |
| 4284 | try { |
| 4285 | final Intent intent = new Intent(action, |
| 4286 | pkg != null ? Uri.fromParts("package", pkg, null) : null); |
| 4287 | if (extras != null) { |
| 4288 | intent.putExtras(extras); |
| 4289 | } |
| Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 4290 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4291 | am.broadcastIntent( |
| 4292 | null, intent, |
| 4293 | null, null, 0, null, null, null, false, false); |
| 4294 | } catch (RemoteException ex) { |
| 4295 | } |
| 4296 | } |
| 4297 | } |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 4298 | |
| 4299 | public String nextPackageToClean(String lastPackage) { |
| 4300 | synchronized (mPackages) { |
| 4301 | if (!mMediaMounted) { |
| 4302 | // If the external storage is no longer mounted at this point, |
| 4303 | // the caller may not have been able to delete all of this |
| 4304 | // packages files and can not delete any more. Bail. |
| 4305 | return null; |
| 4306 | } |
| 4307 | if (lastPackage != null) { |
| 4308 | mSettings.mPackagesToBeCleaned.remove(lastPackage); |
| 4309 | } |
| 4310 | return mSettings.mPackagesToBeCleaned.size() > 0 |
| 4311 | ? mSettings.mPackagesToBeCleaned.get(0) : null; |
| 4312 | } |
| 4313 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4314 | |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 4315 | void schedulePackageCleaning(String packageName) { |
| 4316 | mHandler.sendMessage(mHandler.obtainMessage(START_CLEANING_PACKAGE, packageName)); |
| 4317 | } |
| 4318 | |
| 4319 | void startCleaningPackages() { |
| 4320 | synchronized (mPackages) { |
| 4321 | if (!mMediaMounted) { |
| 4322 | return; |
| 4323 | } |
| 4324 | if (mSettings.mPackagesToBeCleaned.size() <= 0) { |
| 4325 | return; |
| 4326 | } |
| 4327 | } |
| 4328 | Intent intent = new Intent(PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE); |
| 4329 | intent.setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 4330 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 4331 | if (am != null) { |
| 4332 | try { |
| 4333 | am.startService(null, intent, null); |
| 4334 | } catch (RemoteException e) { |
| 4335 | } |
| 4336 | } |
| 4337 | } |
| 4338 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4339 | private final class AppDirObserver extends FileObserver { |
| 4340 | public AppDirObserver(String path, int mask, boolean isrom) { |
| 4341 | super(path, mask); |
| 4342 | mRootDir = path; |
| 4343 | mIsRom = isrom; |
| 4344 | } |
| 4345 | |
| 4346 | public void onEvent(int event, String path) { |
| 4347 | String removedPackage = null; |
| 4348 | int removedUid = -1; |
| 4349 | String addedPackage = null; |
| 4350 | int addedUid = -1; |
| 4351 | |
| 4352 | synchronized (mInstallLock) { |
| 4353 | String fullPathStr = null; |
| 4354 | File fullPath = null; |
| 4355 | if (path != null) { |
| 4356 | fullPath = new File(mRootDir, path); |
| 4357 | fullPathStr = fullPath.getPath(); |
| 4358 | } |
| 4359 | |
| 4360 | if (Config.LOGV) Log.v( |
| 4361 | TAG, "File " + fullPathStr + " changed: " |
| 4362 | + Integer.toHexString(event)); |
| 4363 | |
| 4364 | if (!isPackageFilename(path)) { |
| 4365 | if (Config.LOGV) Log.v( |
| 4366 | TAG, "Ignoring change of non-package file: " + fullPathStr); |
| 4367 | return; |
| 4368 | } |
| 4369 | |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4370 | // Ignore packages that are being installed or |
| 4371 | // have just been installed. |
| 4372 | if (ignoreCodePath(fullPathStr)) { |
| 4373 | return; |
| 4374 | } |
| 4375 | PackageParser.Package p = null; |
| 4376 | synchronized (mPackages) { |
| 4377 | p = mAppDirs.get(fullPathStr); |
| 4378 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4379 | if ((event&REMOVE_EVENTS) != 0) { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4380 | if (p != null) { |
| 4381 | removePackageLI(p, true); |
| 4382 | removedPackage = p.applicationInfo.packageName; |
| 4383 | removedUid = p.applicationInfo.uid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | if ((event&ADD_EVENTS) != 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4388 | if (p == null) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 4389 | p = scanPackageLI(fullPath, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4390 | (mIsRom ? PackageParser.PARSE_IS_SYSTEM : 0) | |
| 4391 | PackageParser.PARSE_CHATTY | |
| 4392 | PackageParser.PARSE_MUST_BE_APK, |
| Andrew Stadler | 48c0273 | 2010-01-15 00:03:41 -0800 | [diff] [blame] | 4393 | SCAN_MONITOR | SCAN_NO_PATHS); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4394 | if (p != null) { |
| 4395 | synchronized (mPackages) { |
| 4396 | grantPermissionsLP(p, false); |
| 4397 | } |
| 4398 | addedPackage = p.applicationInfo.packageName; |
| 4399 | addedUid = p.applicationInfo.uid; |
| 4400 | } |
| 4401 | } |
| 4402 | } |
| 4403 | |
| 4404 | synchronized (mPackages) { |
| 4405 | mSettings.writeLP(); |
| 4406 | } |
| 4407 | } |
| 4408 | |
| 4409 | if (removedPackage != null) { |
| 4410 | Bundle extras = new Bundle(1); |
| 4411 | extras.putInt(Intent.EXTRA_UID, removedUid); |
| 4412 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, false); |
| 4413 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, extras); |
| 4414 | } |
| 4415 | if (addedPackage != null) { |
| 4416 | Bundle extras = new Bundle(1); |
| 4417 | extras.putInt(Intent.EXTRA_UID, addedUid); |
| 4418 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, addedPackage, extras); |
| 4419 | } |
| 4420 | } |
| 4421 | |
| 4422 | private final String mRootDir; |
| 4423 | private final boolean mIsRom; |
| 4424 | } |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 4425 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4426 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 4427 | public void installPackage( |
| 4428 | final Uri packageURI, final IPackageInstallObserver observer, final int flags) { |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 4429 | installPackage(packageURI, observer, flags, null); |
| 4430 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4431 | |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 4432 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 4433 | public void installPackage( |
| 4434 | final Uri packageURI, final IPackageInstallObserver observer, final int flags, |
| 4435 | final String installerPackageName) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4436 | mContext.enforceCallingOrSelfPermission( |
| 4437 | android.Manifest.permission.INSTALL_PACKAGES, null); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4438 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4439 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 4440 | msg.obj = new InstallParams(packageURI, observer, flags, |
| 4441 | installerPackageName); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4442 | mHandler.sendMessage(msg); |
| 4443 | } |
| 4444 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4445 | public void finishPackageInstall(int token) { |
| 4446 | if (DEBUG_INSTALL) Log.v(TAG, "BM finishing package install for " + token); |
| 4447 | Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
| 4448 | mHandler.sendMessage(msg); |
| 4449 | } |
| 4450 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4451 | private void processPendingInstall(final InstallArgs args, final int currentStatus) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4452 | // Queue up an async operation since the package installation may take a little while. |
| 4453 | mHandler.post(new Runnable() { |
| 4454 | public void run() { |
| 4455 | mHandler.removeCallbacks(this); |
| Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 4456 | // Result object to be returned |
| 4457 | PackageInstalledInfo res = new PackageInstalledInfo(); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4458 | res.returnCode = currentStatus; |
| Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 4459 | res.uid = -1; |
| 4460 | res.pkg = null; |
| 4461 | res.removedInfo = new PackageRemovedInfo(); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4462 | args.doPreInstall(res.returnCode); |
| 4463 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
| Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 4464 | synchronized (mInstallLock) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4465 | installPackageLI(args, true, res); |
| Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 4466 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4467 | args.doPostInstall(res.returnCode); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4468 | } |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4469 | |
| 4470 | // A restore should be performed at this point if (a) the install |
| 4471 | // succeeded, (b) the operation is not an update, and (c) the new |
| 4472 | // package has a backupAgent defined. |
| 4473 | final boolean update = res.removedInfo.removedPackage != null; |
| Christopher Tate | 59eac4b | 2010-02-19 19:25:45 -0800 | [diff] [blame] | 4474 | boolean doRestore = (!update |
| 4475 | && res.pkg != null |
| 4476 | && res.pkg.applicationInfo.backupAgentName != null); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4477 | |
| 4478 | // Set up the post-install work request bookkeeping. This will be used |
| 4479 | // and cleaned up by the post-install event handling regardless of whether |
| 4480 | // there's a restore pass performed. Token values are >= 1. |
| 4481 | int token; |
| 4482 | if (mNextInstallToken < 0) mNextInstallToken = 1; |
| 4483 | token = mNextInstallToken++; |
| 4484 | |
| 4485 | PostInstallData data = new PostInstallData(args, res); |
| 4486 | mRunningInstalls.put(token, data); |
| 4487 | if (DEBUG_INSTALL) Log.v(TAG, "+ starting restore round-trip " + token); |
| 4488 | |
| 4489 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED && doRestore) { |
| 4490 | // Pass responsibility to the Backup Manager. It will perform a |
| 4491 | // restore if appropriate, then pass responsibility back to the |
| 4492 | // Package Manager to run the post-install observer callbacks |
| 4493 | // and broadcasts. |
| 4494 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 4495 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 4496 | if (bm != null) { |
| 4497 | if (DEBUG_INSTALL) Log.v(TAG, "token " + token |
| 4498 | + " to BM for possible restore"); |
| 4499 | try { |
| 4500 | bm.restoreAtInstall(res.pkg.applicationInfo.packageName, token); |
| 4501 | } catch (RemoteException e) { |
| 4502 | // can't happen; the backup manager is local |
| 4503 | } catch (Exception e) { |
| 4504 | Log.e(TAG, "Exception trying to enqueue restore", e); |
| 4505 | doRestore = false; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4506 | } |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4507 | } else { |
| 4508 | Log.e(TAG, "Backup Manager not found!"); |
| 4509 | doRestore = false; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4510 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4511 | } |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4512 | |
| 4513 | if (!doRestore) { |
| 4514 | // No restore possible, or the Backup Manager was mysteriously not |
| 4515 | // available -- just fire the post-install work request directly. |
| 4516 | if (DEBUG_INSTALL) Log.v(TAG, "No restore - queue post-install for " + token); |
| 4517 | Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
| 4518 | mHandler.sendMessage(msg); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4519 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4520 | } |
| 4521 | }); |
| 4522 | } |
| 4523 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4524 | abstract class HandlerParams { |
| 4525 | final static int MAX_RETRIES = 4; |
| 4526 | int retry = 0; |
| 4527 | final void startCopy() { |
| 4528 | try { |
| 4529 | if (DEBUG_SD_INSTALL) Log.i(TAG, "startCopy"); |
| 4530 | retry++; |
| 4531 | if (retry > MAX_RETRIES) { |
| 4532 | Log.w(TAG, "Failed to invoke remote methods on default container service. Giving up"); |
| 4533 | mHandler.sendEmptyMessage(MCS_GIVE_UP); |
| 4534 | handleServiceError(); |
| 4535 | return; |
| 4536 | } else { |
| 4537 | handleStartCopy(); |
| 4538 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Posting install MCS_UNBIND"); |
| 4539 | mHandler.sendEmptyMessage(MCS_UNBIND); |
| 4540 | } |
| 4541 | } catch (RemoteException e) { |
| 4542 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Posting install MCS_RECONNECT"); |
| 4543 | mHandler.sendEmptyMessage(MCS_RECONNECT); |
| 4544 | } |
| 4545 | handleReturnCode(); |
| 4546 | } |
| 4547 | |
| 4548 | final void serviceError() { |
| 4549 | if (DEBUG_SD_INSTALL) Log.i(TAG, "serviceError"); |
| 4550 | handleServiceError(); |
| 4551 | handleReturnCode(); |
| 4552 | } |
| 4553 | abstract void handleStartCopy() throws RemoteException; |
| 4554 | abstract void handleServiceError(); |
| 4555 | abstract void handleReturnCode(); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4556 | } |
| 4557 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4558 | class InstallParams extends HandlerParams { |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 4559 | final IPackageInstallObserver observer; |
| 4560 | int flags; |
| 4561 | final Uri packageURI; |
| 4562 | final String installerPackageName; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4563 | private InstallArgs mArgs; |
| 4564 | private int mRet; |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 4565 | InstallParams(Uri packageURI, |
| 4566 | IPackageInstallObserver observer, int flags, |
| 4567 | String installerPackageName) { |
| 4568 | this.packageURI = packageURI; |
| 4569 | this.flags = flags; |
| 4570 | this.observer = observer; |
| 4571 | this.installerPackageName = installerPackageName; |
| 4572 | } |
| 4573 | |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 4574 | private int installLocationPolicy(PackageInfoLite pkgLite, int flags) { |
| 4575 | String packageName = pkgLite.packageName; |
| 4576 | int installLocation = pkgLite.installLocation; |
| 4577 | boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| 4578 | synchronized (mPackages) { |
| 4579 | PackageParser.Package pkg = mPackages.get(packageName); |
| 4580 | if (pkg != null) { |
| 4581 | if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| 4582 | // Check for updated system application. |
| 4583 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 4584 | if (onSd) { |
| 4585 | Log.w(TAG, "Cannot install update to system app on sdcard"); |
| 4586 | return PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION; |
| 4587 | } |
| 4588 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 4589 | } else { |
| 4590 | // When replacing apps make sure we honour |
| 4591 | // the existing app location if not overwritten by other options |
| Suchi Amalapurapu | 6069beb | 2010-03-10 09:46:49 -0800 | [diff] [blame] | 4592 | boolean prevOnSd = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 4593 | if (onSd) { |
| 4594 | // Install flag overrides everything. |
| 4595 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 4596 | } |
| 4597 | // If current upgrade does not specify install location. |
| 4598 | if (installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
| 4599 | // Application explicitly specified internal. |
| 4600 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 4601 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { |
| 4602 | // App explictly prefers external. Let policy decide |
| 4603 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_AUTO) { |
| 4604 | // Prefer previous location |
| 4605 | return prevOnSd ? PackageHelper.RECOMMEND_INSTALL_EXTERNAL: |
| 4606 | PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 4607 | } |
| 4608 | } |
| 4609 | } else { |
| 4610 | // Invalid install. Return error code |
| 4611 | return PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS; |
| 4612 | } |
| 4613 | } |
| 4614 | } |
| 4615 | // All the special cases have been taken care of. |
| 4616 | // Return result based on recommended install location. |
| 4617 | if (onSd) { |
| 4618 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 4619 | } |
| 4620 | return pkgLite.recommendedInstallLocation; |
| 4621 | } |
| 4622 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4623 | public void handleStartCopy() throws RemoteException { |
| Suchi Amalapurapu | 1f9e1b4 | 2010-02-26 13:14:31 -0800 | [diff] [blame] | 4624 | int ret = PackageManager.INSTALL_SUCCEEDED; |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 4625 | boolean fwdLocked = (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 4626 | boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| Suchi Amalapurapu | 1f9e1b4 | 2010-02-26 13:14:31 -0800 | [diff] [blame] | 4627 | // Dont need to invoke getInstallLocation for forward locked apps. |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 4628 | if (fwdLocked && onSd) { |
| 4629 | Log.w(TAG, "Cannot install fwd locked apps on sdcard"); |
| 4630 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4631 | } else { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4632 | // Remote call to find out default install location |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 4633 | PackageInfoLite pkgLite = mContainerService.getMinimalPackageInfo(packageURI); |
| 4634 | int loc = installLocationPolicy(pkgLite, flags); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4635 | // Use install location to create InstallArgs and temporary |
| 4636 | // install location |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 4637 | if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION){ |
| 4638 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
| 4639 | } else if (loc == PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS){ |
| 4640 | ret = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 4641 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE){ |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4642 | ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 4643 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_APK) { |
| 4644 | ret = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 4645 | } else { |
| Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 4646 | // Override install location with flags |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4647 | if ((flags & PackageManager.INSTALL_EXTERNAL) == 0){ |
| 4648 | if (loc == PackageHelper.RECOMMEND_INSTALL_EXTERNAL) { |
| 4649 | // Set the flag to install on external media. |
| 4650 | flags |= PackageManager.INSTALL_EXTERNAL; |
| 4651 | } else { |
| 4652 | // Make sure the flag for installing on external |
| 4653 | // media is unset |
| 4654 | flags &= ~PackageManager.INSTALL_EXTERNAL; |
| 4655 | } |
| 4656 | } |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4657 | } |
| 4658 | } |
| 4659 | // Create the file args now. |
| 4660 | mArgs = createInstallArgs(this); |
| 4661 | if (ret == PackageManager.INSTALL_SUCCEEDED) { |
| 4662 | // Create copy only if we are not in an erroneous state. |
| 4663 | // Remote call to initiate copy using temporary file |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4664 | ret = mArgs.copyApk(mContainerService, true); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4665 | } |
| 4666 | mRet = ret; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4667 | } |
| 4668 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4669 | @Override |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4670 | void handleReturnCode() { |
| 4671 | processPendingInstall(mArgs, mRet); |
| 4672 | } |
| 4673 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4674 | @Override |
| 4675 | void handleServiceError() { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4676 | mArgs = createInstallArgs(this); |
| 4677 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4678 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4679 | } |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4680 | |
| 4681 | /* |
| 4682 | * Utility class used in movePackage api. |
| 4683 | * srcArgs and targetArgs are not set for invalid flags and make |
| 4684 | * sure to do null checks when invoking methods on them. |
| 4685 | * We probably want to return ErrorPrams for both failed installs |
| 4686 | * and moves. |
| 4687 | */ |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4688 | class MoveParams extends HandlerParams { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4689 | final IPackageMoveObserver observer; |
| 4690 | final int flags; |
| 4691 | final String packageName; |
| 4692 | final InstallArgs srcArgs; |
| 4693 | final InstallArgs targetArgs; |
| 4694 | int mRet; |
| 4695 | MoveParams(InstallArgs srcArgs, |
| 4696 | IPackageMoveObserver observer, |
| 4697 | int flags, String packageName) { |
| 4698 | this.srcArgs = srcArgs; |
| 4699 | this.observer = observer; |
| 4700 | this.flags = flags; |
| 4701 | this.packageName = packageName; |
| 4702 | if (srcArgs != null) { |
| 4703 | Uri packageUri = Uri.fromFile(new File(srcArgs.getCodePath())); |
| 4704 | targetArgs = createInstallArgs(packageUri, flags, packageName); |
| 4705 | } else { |
| 4706 | targetArgs = null; |
| 4707 | } |
| 4708 | } |
| 4709 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4710 | public void handleStartCopy() throws RemoteException { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4711 | // Create the file args now. |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4712 | mRet = targetArgs.copyApk(mContainerService, false); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4713 | targetArgs.doPreInstall(mRet); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4714 | if (DEBUG_SD_INSTALL) { |
| 4715 | StringBuilder builder = new StringBuilder(); |
| 4716 | if (srcArgs != null) { |
| 4717 | builder.append("src: "); |
| 4718 | builder.append(srcArgs.getCodePath()); |
| 4719 | } |
| 4720 | if (targetArgs != null) { |
| 4721 | builder.append(" target : "); |
| 4722 | builder.append(targetArgs.getCodePath()); |
| 4723 | } |
| 4724 | Log.i(TAG, "Posting move MCS_UNBIND for " + builder.toString()); |
| 4725 | } |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4726 | } |
| 4727 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4728 | @Override |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4729 | void handleReturnCode() { |
| 4730 | targetArgs.doPostInstall(mRet); |
| 4731 | // TODO invoke pending move |
| 4732 | processPendingMove(this, mRet); |
| 4733 | } |
| 4734 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4735 | @Override |
| 4736 | void handleServiceError() { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4737 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4738 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4739 | } |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 4740 | |
| 4741 | private InstallArgs createInstallArgs(InstallParams params) { |
| 4742 | if (installOnSd(params.flags)) { |
| 4743 | return new SdInstallArgs(params); |
| 4744 | } else { |
| 4745 | return new FileInstallArgs(params); |
| 4746 | } |
| 4747 | } |
| 4748 | |
| 4749 | private InstallArgs createInstallArgs(int flags, String fullCodePath, String fullResourcePath) { |
| 4750 | if (installOnSd(flags)) { |
| 4751 | return new SdInstallArgs(fullCodePath, fullResourcePath); |
| 4752 | } else { |
| 4753 | return new FileInstallArgs(fullCodePath, fullResourcePath); |
| 4754 | } |
| 4755 | } |
| 4756 | |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4757 | private InstallArgs createInstallArgs(Uri packageURI, int flags, String pkgName) { |
| 4758 | if (installOnSd(flags)) { |
| 4759 | String cid = getNextCodePath(null, pkgName, "/" + SdInstallArgs.RES_FILE_NAME); |
| 4760 | return new SdInstallArgs(packageURI, cid); |
| 4761 | } else { |
| 4762 | return new FileInstallArgs(packageURI, pkgName); |
| 4763 | } |
| 4764 | } |
| 4765 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4766 | static abstract class InstallArgs { |
| 4767 | final IPackageInstallObserver observer; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4768 | // Always refers to PackageManager flags only |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4769 | final int flags; |
| 4770 | final Uri packageURI; |
| 4771 | final String installerPackageName; |
| 4772 | |
| 4773 | InstallArgs(Uri packageURI, |
| 4774 | IPackageInstallObserver observer, int flags, |
| 4775 | String installerPackageName) { |
| 4776 | this.packageURI = packageURI; |
| 4777 | this.flags = flags; |
| 4778 | this.observer = observer; |
| 4779 | this.installerPackageName = installerPackageName; |
| 4780 | } |
| 4781 | |
| 4782 | abstract void createCopyFile(); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4783 | abstract int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4784 | abstract int doPreInstall(int status); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4785 | abstract boolean doRename(int status, String pkgName, String oldCodePath); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4786 | abstract int doPostInstall(int status); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4787 | abstract String getCodePath(); |
| 4788 | abstract String getResourcePath(); |
| 4789 | // Need installer lock especially for dex file removal. |
| 4790 | abstract void cleanUpResourcesLI(); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4791 | abstract boolean doPostDeleteLI(boolean delete); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4792 | } |
| 4793 | |
| 4794 | class FileInstallArgs extends InstallArgs { |
| 4795 | File installDir; |
| 4796 | String codeFileName; |
| 4797 | String resourceFileName; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4798 | boolean created = false; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4799 | |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 4800 | FileInstallArgs(InstallParams params) { |
| 4801 | super(params.packageURI, params.observer, |
| 4802 | params.flags, params.installerPackageName); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | FileInstallArgs(String fullCodePath, String fullResourcePath) { |
| 4806 | super(null, null, 0, null); |
| 4807 | File codeFile = new File(fullCodePath); |
| 4808 | installDir = codeFile.getParentFile(); |
| 4809 | codeFileName = fullCodePath; |
| 4810 | resourceFileName = fullResourcePath; |
| 4811 | } |
| 4812 | |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4813 | FileInstallArgs(Uri packageURI, String pkgName) { |
| 4814 | super(packageURI, null, 0, null); |
| 4815 | boolean fwdLocked = isFwdLocked(flags); |
| 4816 | installDir = fwdLocked ? mDrmAppPrivateInstallDir : mAppInstallDir; |
| 4817 | String apkName = getNextCodePath(null, pkgName, ".apk"); |
| 4818 | codeFileName = new File(installDir, apkName + ".apk").getPath(); |
| 4819 | resourceFileName = getResourcePathFromCodePath(); |
| 4820 | } |
| 4821 | |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 4822 | String getCodePath() { |
| 4823 | return codeFileName; |
| 4824 | } |
| 4825 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4826 | void createCopyFile() { |
| 4827 | boolean fwdLocked = isFwdLocked(flags); |
| 4828 | installDir = fwdLocked ? mDrmAppPrivateInstallDir : mAppInstallDir; |
| 4829 | codeFileName = createTempPackageFile(installDir).getPath(); |
| 4830 | resourceFileName = getResourcePathFromCodePath(); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4831 | created = true; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4832 | } |
| 4833 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 4834 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4835 | if (temp) { |
| 4836 | // Generate temp file name |
| 4837 | createCopyFile(); |
| 4838 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4839 | // Get a ParcelFileDescriptor to write to the output file |
| 4840 | File codeFile = new File(codeFileName); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4841 | if (!created) { |
| 4842 | try { |
| 4843 | codeFile.createNewFile(); |
| 4844 | // Set permissions |
| 4845 | if (!setPermissions()) { |
| 4846 | // Failed setting permissions. |
| 4847 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 4848 | } |
| 4849 | } catch (IOException e) { |
| 4850 | Log.w(TAG, "Failed to create file " + codeFile); |
| 4851 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 4852 | } |
| 4853 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4854 | ParcelFileDescriptor out = null; |
| 4855 | try { |
| 4856 | out = ParcelFileDescriptor.open(codeFile, |
| 4857 | ParcelFileDescriptor.MODE_READ_WRITE); |
| 4858 | } catch (FileNotFoundException e) { |
| 4859 | Log.e(TAG, "Failed to create file descritpor for : " + codeFileName); |
| 4860 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 4861 | } |
| 4862 | // Copy the resource now |
| 4863 | int ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 4864 | try { |
| 4865 | if (imcs.copyResource(packageURI, out)) { |
| 4866 | ret = PackageManager.INSTALL_SUCCEEDED; |
| 4867 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4868 | } finally { |
| 4869 | try { if (out != null) out.close(); } catch (IOException e) {} |
| 4870 | } |
| 4871 | return ret; |
| 4872 | } |
| 4873 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4874 | int doPreInstall(int status) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4875 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 4876 | cleanUp(); |
| 4877 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4878 | return status; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | boolean doRename(int status, final String pkgName, String oldCodePath) { |
| 4882 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 4883 | cleanUp(); |
| 4884 | return false; |
| 4885 | } else { |
| 4886 | // Rename based on packageName |
| 4887 | File codeFile = new File(getCodePath()); |
| 4888 | String apkName = getNextCodePath(oldCodePath, pkgName, ".apk"); |
| 4889 | File desFile = new File(installDir, apkName + ".apk"); |
| 4890 | if (!codeFile.renameTo(desFile)) { |
| 4891 | return false; |
| 4892 | } |
| 4893 | // Reset paths since the file has been renamed. |
| 4894 | codeFileName = desFile.getPath(); |
| 4895 | resourceFileName = getResourcePathFromCodePath(); |
| 4896 | // Set permissions |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4897 | if (!setPermissions()) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4898 | // Failed setting permissions. |
| 4899 | return false; |
| 4900 | } |
| 4901 | return true; |
| 4902 | } |
| 4903 | } |
| 4904 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4905 | int doPostInstall(int status) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4906 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 4907 | cleanUp(); |
| 4908 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4909 | return status; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | String getResourcePath() { |
| 4913 | return resourceFileName; |
| 4914 | } |
| 4915 | |
| 4916 | String getResourcePathFromCodePath() { |
| 4917 | String codePath = getCodePath(); |
| 4918 | if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) { |
| 4919 | String apkNameOnly = getApkName(codePath); |
| 4920 | return mAppInstallDir.getPath() + "/" + apkNameOnly + ".zip"; |
| 4921 | } else { |
| 4922 | return codePath; |
| 4923 | } |
| 4924 | } |
| 4925 | |
| 4926 | private boolean cleanUp() { |
| 4927 | boolean ret = true; |
| 4928 | String sourceDir = getCodePath(); |
| 4929 | String publicSourceDir = getResourcePath(); |
| 4930 | if (sourceDir != null) { |
| 4931 | File sourceFile = new File(sourceDir); |
| 4932 | if (!sourceFile.exists()) { |
| 4933 | Log.w(TAG, "Package source " + sourceDir + " does not exist."); |
| 4934 | ret = false; |
| 4935 | } |
| 4936 | // Delete application's code and resources |
| 4937 | sourceFile.delete(); |
| 4938 | } |
| 4939 | if (publicSourceDir != null && !publicSourceDir.equals(sourceDir)) { |
| 4940 | final File publicSourceFile = new File(publicSourceDir); |
| 4941 | if (!publicSourceFile.exists()) { |
| 4942 | Log.w(TAG, "Package public source " + publicSourceFile + " does not exist."); |
| 4943 | } |
| 4944 | if (publicSourceFile.exists()) { |
| 4945 | publicSourceFile.delete(); |
| 4946 | } |
| 4947 | } |
| 4948 | return ret; |
| 4949 | } |
| 4950 | |
| 4951 | void cleanUpResourcesLI() { |
| 4952 | String sourceDir = getCodePath(); |
| 4953 | if (cleanUp() && mInstaller != null) { |
| 4954 | int retCode = mInstaller.rmdex(sourceDir); |
| 4955 | if (retCode < 0) { |
| 4956 | Log.w(TAG, "Couldn't remove dex file for package: " |
| 4957 | + " at location " |
| 4958 | + sourceDir + ", retcode=" + retCode); |
| 4959 | // we don't consider this to be a failure of the core package deletion |
| 4960 | } |
| 4961 | } |
| 4962 | } |
| 4963 | |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4964 | private boolean setPermissions() { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4965 | // TODO Do this in a more elegant way later on. for now just a hack |
| 4966 | if (!isFwdLocked(flags)) { |
| 4967 | final int filePermissions = |
| 4968 | FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP |
| 4969 | |FileUtils.S_IROTH; |
| 4970 | int retCode = FileUtils.setPermissions(getCodePath(), filePermissions, -1, -1); |
| 4971 | if (retCode != 0) { |
| 4972 | Log.e(TAG, "Couldn't set new package file permissions for " + |
| 4973 | getCodePath() |
| 4974 | + ". The return code was: " + retCode); |
| 4975 | // TODO Define new internal error |
| 4976 | return false; |
| 4977 | } |
| 4978 | return true; |
| 4979 | } |
| 4980 | return true; |
| 4981 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4982 | |
| 4983 | boolean doPostDeleteLI(boolean delete) { |
| 4984 | cleanUpResourcesLI(); |
| 4985 | return true; |
| 4986 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4987 | } |
| 4988 | |
| 4989 | class SdInstallArgs extends InstallArgs { |
| 4990 | String cid; |
| 4991 | String cachePath; |
| 4992 | static final String RES_FILE_NAME = "pkg.apk"; |
| 4993 | |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 4994 | SdInstallArgs(InstallParams params) { |
| 4995 | super(params.packageURI, params.observer, |
| 4996 | params.flags, params.installerPackageName); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4997 | } |
| 4998 | |
| 4999 | SdInstallArgs(String fullCodePath, String fullResourcePath) { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5000 | super(null, null, PackageManager.INSTALL_EXTERNAL, null); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5001 | // Extract cid from fullCodePath |
| 5002 | int eidx = fullCodePath.lastIndexOf("/"); |
| 5003 | String subStr1 = fullCodePath.substring(0, eidx); |
| 5004 | int sidx = subStr1.lastIndexOf("/"); |
| 5005 | cid = subStr1.substring(sidx+1, eidx); |
| 5006 | cachePath = subStr1; |
| 5007 | } |
| 5008 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5009 | SdInstallArgs(String cid) { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5010 | super(null, null, PackageManager.INSTALL_EXTERNAL, null); |
| 5011 | this.cid = cid; |
| 5012 | } |
| 5013 | |
| 5014 | SdInstallArgs(Uri packageURI, String cid) { |
| 5015 | super(packageURI, null, PackageManager.INSTALL_EXTERNAL, null); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5016 | this.cid = cid; |
| 5017 | } |
| 5018 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5019 | void createCopyFile() { |
| 5020 | cid = getTempContainerId(); |
| 5021 | } |
| 5022 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5023 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5024 | if (temp) { |
| 5025 | createCopyFile(); |
| 5026 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5027 | cachePath = imcs.copyResourceToContainer( |
| 5028 | packageURI, cid, |
| 5029 | getEncryptKey(), RES_FILE_NAME); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5030 | return (cachePath == null) ? PackageManager.INSTALL_FAILED_CONTAINER_ERROR : |
| 5031 | PackageManager.INSTALL_SUCCEEDED; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | @Override |
| 5035 | String getCodePath() { |
| 5036 | return cachePath + "/" + RES_FILE_NAME; |
| 5037 | } |
| 5038 | |
| 5039 | @Override |
| 5040 | String getResourcePath() { |
| 5041 | return cachePath + "/" + RES_FILE_NAME; |
| 5042 | } |
| 5043 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5044 | int doPreInstall(int status) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5045 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 5046 | // Destroy container |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 5047 | PackageHelper.destroySdDir(cid); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5048 | } else { |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 5049 | boolean mounted = PackageHelper.isContainerMounted(cid); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5050 | if (!mounted) { |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 5051 | cachePath = PackageHelper.mountSdDir(cid, getEncryptKey(), Process.SYSTEM_UID); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5052 | if (cachePath == null) { |
| 5053 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 5054 | } |
| 5055 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5056 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5057 | return status; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | boolean doRename(int status, final String pkgName, |
| 5061 | String oldCodePath) { |
| 5062 | String newCacheId = getNextCodePath(oldCodePath, pkgName, "/" + RES_FILE_NAME); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5063 | String newCachePath = null; |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5064 | if (PackageHelper.isContainerMounted(cid)) { |
| 5065 | // Unmount the container |
| 5066 | if (!PackageHelper.unMountSdDir(cid)) { |
| 5067 | Log.i(TAG, "Failed to unmount " + cid + " before renaming"); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5068 | return false; |
| 5069 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5070 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5071 | if (!PackageHelper.renameSdDir(cid, newCacheId)) { |
| 5072 | Log.e(TAG, "Failed to rename " + cid + " to " + newCacheId); |
| 5073 | return false; |
| 5074 | } |
| 5075 | if (!PackageHelper.isContainerMounted(newCacheId)) { |
| 5076 | Log.w(TAG, "Mounting container " + newCacheId); |
| 5077 | newCachePath = PackageHelper.mountSdDir(newCacheId, |
| 5078 | getEncryptKey(), Process.SYSTEM_UID); |
| 5079 | } else { |
| 5080 | newCachePath = PackageHelper.getSdDir(newCacheId); |
| 5081 | } |
| 5082 | if (newCachePath == null) { |
| 5083 | Log.w(TAG, "Failed to get cache path for " + newCacheId); |
| 5084 | return false; |
| 5085 | } |
| 5086 | Log.i(TAG, "Succesfully renamed " + cid + |
| 5087 | " at path: " + cachePath + " to " + newCacheId + |
| 5088 | " at new path: " + newCachePath); |
| 5089 | cid = newCacheId; |
| 5090 | cachePath = newCachePath; |
| 5091 | return true; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5092 | } |
| 5093 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5094 | int doPostInstall(int status) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5095 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 5096 | cleanUp(); |
| 5097 | } else { |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 5098 | boolean mounted = PackageHelper.isContainerMounted(cid); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5099 | if (!mounted) { |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 5100 | PackageHelper.mountSdDir(cid, |
| 5101 | getEncryptKey(), Process.myUid()); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5102 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5103 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5104 | return status; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5105 | } |
| 5106 | |
| 5107 | private void cleanUp() { |
| 5108 | // Destroy secure container |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 5109 | PackageHelper.destroySdDir(cid); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | void cleanUpResourcesLI() { |
| 5113 | String sourceFile = getCodePath(); |
| 5114 | // Remove dex file |
| 5115 | if (mInstaller != null) { |
| 5116 | int retCode = mInstaller.rmdex(sourceFile.toString()); |
| 5117 | if (retCode < 0) { |
| 5118 | Log.w(TAG, "Couldn't remove dex file for package: " |
| 5119 | + " at location " |
| 5120 | + sourceFile.toString() + ", retcode=" + retCode); |
| 5121 | // we don't consider this to be a failure of the core package deletion |
| 5122 | } |
| 5123 | } |
| 5124 | cleanUp(); |
| 5125 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5126 | |
| 5127 | boolean matchContainer(String app) { |
| 5128 | if (cid.startsWith(app)) { |
| 5129 | return true; |
| 5130 | } |
| 5131 | return false; |
| 5132 | } |
| 5133 | |
| 5134 | String getPackageName() { |
| 5135 | int idx = cid.lastIndexOf("-"); |
| 5136 | if (idx == -1) { |
| 5137 | return cid; |
| 5138 | } |
| 5139 | return cid.substring(0, idx); |
| 5140 | } |
| 5141 | |
| 5142 | boolean doPostDeleteLI(boolean delete) { |
| 5143 | boolean ret = false; |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 5144 | boolean mounted = PackageHelper.isContainerMounted(cid); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5145 | if (mounted) { |
| 5146 | // Unmount first |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 5147 | ret = PackageHelper.unMountSdDir(cid); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5148 | } |
| 5149 | if (ret && delete) { |
| 5150 | cleanUpResourcesLI(); |
| 5151 | } |
| 5152 | return ret; |
| 5153 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5154 | }; |
| 5155 | |
| 5156 | // Utility method used to create code paths based on package name and available index. |
| 5157 | private static String getNextCodePath(String oldCodePath, String prefix, String suffix) { |
| 5158 | String idxStr = ""; |
| 5159 | int idx = 1; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5160 | // Fall back to default value of idx=1 if prefix is not |
| 5161 | // part of oldCodePath |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5162 | if (oldCodePath != null) { |
| Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 5163 | String subStr = oldCodePath; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5164 | // Drop the suffix right away |
| Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 5165 | if (subStr.endsWith(suffix)) { |
| 5166 | subStr = subStr.substring(0, subStr.length() - suffix.length()); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5167 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5168 | // If oldCodePath already contains prefix find out the |
| 5169 | // ending index to either increment or decrement. |
| 5170 | int sidx = subStr.lastIndexOf(prefix); |
| 5171 | if (sidx != -1) { |
| 5172 | subStr = subStr.substring(sidx + prefix.length()); |
| 5173 | if (subStr != null) { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5174 | if (subStr.startsWith(INSTALL_PACKAGE_SUFFIX)) { |
| 5175 | subStr = subStr.substring(INSTALL_PACKAGE_SUFFIX.length()); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5176 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5177 | try { |
| 5178 | idx = Integer.parseInt(subStr); |
| 5179 | if (idx <= 1) { |
| 5180 | idx++; |
| 5181 | } else { |
| 5182 | idx--; |
| 5183 | } |
| 5184 | } catch(NumberFormatException e) { |
| 5185 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5186 | } |
| 5187 | } |
| 5188 | } |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5189 | idxStr = INSTALL_PACKAGE_SUFFIX + Integer.toString(idx); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5190 | return prefix + idxStr; |
| 5191 | } |
| 5192 | |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5193 | // Utility method used to ignore ADD/REMOVE events |
| 5194 | // by directory observer. |
| 5195 | private static boolean ignoreCodePath(String fullPathStr) { |
| 5196 | String apkName = getApkName(fullPathStr); |
| 5197 | int idx = apkName.lastIndexOf(INSTALL_PACKAGE_SUFFIX); |
| 5198 | if (idx != -1 && ((idx+1) < apkName.length())) { |
| 5199 | // Make sure the package ends with a numeral |
| 5200 | String version = apkName.substring(idx+1); |
| 5201 | try { |
| 5202 | Integer.parseInt(version); |
| 5203 | return true; |
| 5204 | } catch (NumberFormatException e) {} |
| 5205 | } |
| 5206 | return false; |
| 5207 | } |
| 5208 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5209 | // Utility method that returns the relative package path with respect |
| 5210 | // to the installation directory. Like say for /data/data/com.test-1.apk |
| 5211 | // string com.test-1 is returned. |
| 5212 | static String getApkName(String codePath) { |
| 5213 | if (codePath == null) { |
| 5214 | return null; |
| 5215 | } |
| 5216 | int sidx = codePath.lastIndexOf("/"); |
| 5217 | int eidx = codePath.lastIndexOf("."); |
| 5218 | if (eidx == -1) { |
| 5219 | eidx = codePath.length(); |
| 5220 | } else if (eidx == 0) { |
| 5221 | Log.w(TAG, " Invalid code path, "+ codePath + " Not a valid apk name"); |
| 5222 | return null; |
| 5223 | } |
| 5224 | return codePath.substring(sidx+1, eidx); |
| 5225 | } |
| 5226 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5227 | class PackageInstalledInfo { |
| 5228 | String name; |
| 5229 | int uid; |
| 5230 | PackageParser.Package pkg; |
| 5231 | int returnCode; |
| 5232 | PackageRemovedInfo removedInfo; |
| 5233 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5234 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5235 | /* |
| 5236 | * Install a non-existing package. |
| 5237 | */ |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5238 | private void installNewPackageLI(PackageParser.Package pkg, |
| 5239 | int parseFlags, |
| 5240 | int scanMode, |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5241 | String installerPackageName, PackageInstalledInfo res) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5242 | // Remember this for later, in case we need to rollback this install |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5243 | String pkgName = pkg.packageName; |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 5244 | |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 5245 | boolean dataDirExists = getDataPathForPackage(pkg).exists(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5246 | res.name = pkgName; |
| 5247 | synchronized(mPackages) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5248 | if (mPackages.containsKey(pkgName) || mAppDirs.containsKey(pkg.mPath)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5249 | // Don't allow installation over an existing package with the same name. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5250 | Log.w(TAG, "Attempt to re-install " + pkgName |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5251 | + " without first uninstalling."); |
| 5252 | res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 5253 | return; |
| 5254 | } |
| 5255 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5256 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5257 | PackageParser.Package newPackage = scanPackageLI(pkg, parseFlags, scanMode); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5258 | if (newPackage == null) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5259 | Log.w(TAG, "Package couldn't be installed in " + pkg.mPath); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5260 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 5261 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 5262 | } |
| 5263 | } else { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5264 | updateSettingsLI(newPackage, |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5265 | installerPackageName, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5266 | res); |
| 5267 | // delete the partially installed application. the data directory will have to be |
| 5268 | // restored if it was already existing |
| 5269 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
| 5270 | // remove package from internal structures. Note that we want deletePackageX to |
| 5271 | // delete the package data and cache directories that it created in |
| 5272 | // scanPackageLocked, unless those directories existed before we even tried to |
| 5273 | // install. |
| 5274 | deletePackageLI( |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5275 | pkgName, false, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5276 | dataDirExists ? PackageManager.DONT_DELETE_DATA : 0, |
| 5277 | res.removedInfo); |
| 5278 | } |
| 5279 | } |
| 5280 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5281 | |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5282 | private void replacePackageLI(PackageParser.Package pkg, |
| 5283 | int parseFlags, |
| 5284 | int scanMode, |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5285 | String installerPackageName, PackageInstalledInfo res) { |
| 5286 | |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 5287 | PackageParser.Package oldPackage; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5288 | String pkgName = pkg.packageName; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5289 | // First find the old package info and check signatures |
| 5290 | synchronized(mPackages) { |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 5291 | oldPackage = mPackages.get(pkgName); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5292 | if (checkSignaturesLP(pkg.mSignatures, oldPackage.mSignatures) |
| Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 5293 | != PackageManager.SIGNATURE_MATCH) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5294 | res.returnCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 5295 | return; |
| 5296 | } |
| 5297 | } |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 5298 | boolean sysPkg = ((oldPackage.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5299 | if (sysPkg) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5300 | replaceSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, installerPackageName, res); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5301 | } else { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5302 | replaceNonSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, installerPackageName, res); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5303 | } |
| 5304 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5305 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5306 | private void replaceNonSystemPackageLI(PackageParser.Package deletedPackage, |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5307 | PackageParser.Package pkg, |
| 5308 | int parseFlags, int scanMode, |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5309 | String installerPackageName, PackageInstalledInfo res) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5310 | PackageParser.Package newPackage = null; |
| 5311 | String pkgName = deletedPackage.packageName; |
| 5312 | boolean deletedPkg = true; |
| 5313 | boolean updatedSettings = false; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5314 | |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5315 | String oldInstallerPackageName = null; |
| 5316 | synchronized (mPackages) { |
| 5317 | oldInstallerPackageName = mSettings.getInstallerPackageName(pkgName); |
| 5318 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5319 | |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5320 | parseFlags |= PackageManager.INSTALL_REPLACE_EXISTING; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5321 | // First delete the existing package while retaining the data directory |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5322 | if (!deletePackageLI(pkgName, true, PackageManager.DONT_DELETE_DATA, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5323 | res.removedInfo)) { |
| 5324 | // If the existing package was'nt successfully deleted |
| 5325 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 5326 | deletedPkg = false; |
| 5327 | } else { |
| 5328 | // Successfully deleted the old package. Now proceed with re-installation |
| 5329 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5330 | newPackage = scanPackageLI(pkg, parseFlags, scanMode); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5331 | if (newPackage == null) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5332 | Log.w(TAG, "Package couldn't be installed in " + pkg.mPath); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5333 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 5334 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| Suchi Amalapurapu | 110fea7 | 2010-01-14 17:50:23 -0800 | [diff] [blame] | 5335 | } |
| 5336 | } else { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5337 | updateSettingsLI(newPackage, |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5338 | installerPackageName, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5339 | res); |
| 5340 | updatedSettings = true; |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
| 5345 | // If we deleted an exisiting package, the old source and resource files that we |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5346 | // were keeping around in case we needed them (see below) can now be deleted. |
| 5347 | // This info will be set on the res.removedInfo to clean up later on as post |
| 5348 | // install action. |
| 5349 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5350 | //update signature on the new package setting |
| 5351 | //this should always succeed, since we checked the |
| 5352 | //signature earlier. |
| 5353 | synchronized(mPackages) { |
| 5354 | verifySignaturesLP(mSettings.mPackages.get(pkgName), pkg, |
| 5355 | parseFlags, true); |
| 5356 | } |
| 5357 | } else { |
| 5358 | // remove package from internal structures. Note that we want deletePackageX to |
| 5359 | // delete the package data and cache directories that it created in |
| 5360 | // scanPackageLocked, unless those directories existed before we even tried to |
| 5361 | // install. |
| 5362 | if(updatedSettings) { |
| 5363 | deletePackageLI( |
| 5364 | pkgName, true, |
| 5365 | PackageManager.DONT_DELETE_DATA, |
| 5366 | res.removedInfo); |
| 5367 | } |
| 5368 | // Since we failed to install the new package we need to restore the old |
| 5369 | // package that we deleted. |
| 5370 | if(deletedPkg) { |
| Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5371 | File restoreFile = new File(deletedPackage.mPath); |
| 5372 | if (restoreFile == null) { |
| 5373 | Log.e(TAG, "Failed allocating storage when restoring pkg : " + pkgName); |
| 5374 | return; |
| 5375 | } |
| Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5376 | PackageInstalledInfo restoreRes = new PackageInstalledInfo(); |
| 5377 | restoreRes.removedInfo = new PackageRemovedInfo(); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5378 | // Parse old package |
| 5379 | parseFlags |= ~PackageManager.INSTALL_REPLACE_EXISTING; |
| 5380 | scanPackageLI(restoreFile, parseFlags, scanMode); |
| 5381 | synchronized (mPackages) { |
| 5382 | grantPermissionsLP(deletedPackage, false); |
| 5383 | mSettings.writeLP(); |
| 5384 | } |
| Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5385 | if (restoreRes.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
| 5386 | Log.e(TAG, "Failed restoring pkg : " + pkgName + " after failed upgrade"); |
| 5387 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5388 | } |
| 5389 | } |
| 5390 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5391 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5392 | private void replaceSystemPackageLI(PackageParser.Package deletedPackage, |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5393 | PackageParser.Package pkg, |
| 5394 | int parseFlags, int scanMode, |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5395 | String installerPackageName, PackageInstalledInfo res) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5396 | PackageParser.Package newPackage = null; |
| 5397 | boolean updatedSettings = false; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5398 | parseFlags |= PackageManager.INSTALL_REPLACE_EXISTING | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5399 | PackageParser.PARSE_IS_SYSTEM; |
| 5400 | String packageName = deletedPackage.packageName; |
| 5401 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 5402 | if (packageName == null) { |
| 5403 | Log.w(TAG, "Attempt to delete null packageName."); |
| 5404 | return; |
| 5405 | } |
| 5406 | PackageParser.Package oldPkg; |
| 5407 | PackageSetting oldPkgSetting; |
| 5408 | synchronized (mPackages) { |
| 5409 | oldPkg = mPackages.get(packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5410 | oldPkgSetting = mSettings.mPackages.get(packageName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5411 | if((oldPkg == null) || (oldPkg.applicationInfo == null) || |
| 5412 | (oldPkgSetting == null)) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5413 | Log.w(TAG, "Couldn't find package:"+packageName+" information"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5414 | return; |
| 5415 | } |
| 5416 | } |
| 5417 | res.removedInfo.uid = oldPkg.applicationInfo.uid; |
| 5418 | res.removedInfo.removedPackage = packageName; |
| 5419 | // Remove existing system package |
| 5420 | removePackageLI(oldPkg, true); |
| 5421 | synchronized (mPackages) { |
| 5422 | res.removedInfo.removedUid = mSettings.disableSystemPackageLP(packageName); |
| 5423 | } |
| 5424 | |
| 5425 | // Successfully disabled the old package. Now proceed with re-installation |
| 5426 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
| 5427 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5428 | newPackage = scanPackageLI(pkg, parseFlags, scanMode); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5429 | if (newPackage == null) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5430 | Log.w(TAG, "Package couldn't be installed in " + pkg.mPath); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5431 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 5432 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 5433 | } |
| 5434 | } else { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5435 | updateSettingsLI(newPackage, installerPackageName, res); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5436 | updatedSettings = true; |
| 5437 | } |
| 5438 | |
| 5439 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
| 5440 | //update signature on the new package setting |
| 5441 | //this should always succeed, since we checked the |
| 5442 | //signature earlier. |
| 5443 | synchronized(mPackages) { |
| 5444 | verifySignaturesLP(mSettings.mPackages.get(packageName), pkg, |
| 5445 | parseFlags, true); |
| 5446 | } |
| 5447 | } else { |
| 5448 | // Re installation failed. Restore old information |
| 5449 | // Remove new pkg information |
| Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 5450 | if (newPackage != null) { |
| 5451 | removePackageLI(newPackage, true); |
| 5452 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5453 | // Add back the old system package |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5454 | scanPackageLI(oldPkg, parseFlags, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5455 | SCAN_MONITOR |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5456 | | SCAN_UPDATE_SIGNATURE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5457 | // Restore the old system information in Settings |
| 5458 | synchronized(mPackages) { |
| 5459 | if(updatedSettings) { |
| 5460 | mSettings.enableSystemPackageLP(packageName); |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5461 | mSettings.setInstallerPackageName(packageName, |
| 5462 | oldPkgSetting.installerPackageName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5463 | } |
| 5464 | mSettings.writeLP(); |
| 5465 | } |
| 5466 | } |
| 5467 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5468 | |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5469 | // Utility method used to move dex files during install. |
| 5470 | private int moveDexFiles(PackageParser.Package newPackage) { |
| 5471 | int retCode; |
| 5472 | if ((newPackage.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) { |
| 5473 | retCode = mInstaller.movedex(newPackage.mScanPath, newPackage.mPath); |
| 5474 | if (retCode != 0) { |
| 5475 | Log.e(TAG, "Couldn't rename dex file: " + newPackage.mPath); |
| 5476 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 5477 | } |
| 5478 | } |
| 5479 | return PackageManager.INSTALL_SUCCEEDED; |
| 5480 | } |
| 5481 | |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5482 | private void updateSettingsLI(PackageParser.Package newPackage, |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5483 | String installerPackageName, PackageInstalledInfo res) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5484 | String pkgName = newPackage.packageName; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5485 | synchronized (mPackages) { |
| 5486 | //write settings. the installStatus will be incomplete at this stage. |
| 5487 | //note that the new package setting would have already been |
| 5488 | //added to mPackages. It hasn't been persisted yet. |
| 5489 | mSettings.setInstallStatus(pkgName, PKG_INSTALL_INCOMPLETE); |
| 5490 | mSettings.writeLP(); |
| 5491 | } |
| 5492 | |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5493 | if ((res.returnCode = moveDexFiles(newPackage)) |
| 5494 | != PackageManager.INSTALL_SUCCEEDED) { |
| 5495 | // Discontinue if moving dex files failed. |
| 5496 | return; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5497 | } |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5498 | if((res.returnCode = setPermissionsLI(newPackage)) |
| 5499 | != PackageManager.INSTALL_SUCCEEDED) { |
| 5500 | if (mInstaller != null) { |
| 5501 | mInstaller.rmdex(newPackage.mScanPath); |
| 5502 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5503 | return; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5504 | } else { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5505 | Log.d(TAG, "New package installed in " + newPackage.mPath); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5506 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5507 | synchronized (mPackages) { |
| 5508 | grantPermissionsLP(newPackage, true); |
| 5509 | res.name = pkgName; |
| 5510 | res.uid = newPackage.applicationInfo.uid; |
| 5511 | res.pkg = newPackage; |
| 5512 | mSettings.setInstallStatus(pkgName, PKG_INSTALL_COMPLETE); |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5513 | mSettings.setInstallerPackageName(pkgName, installerPackageName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5514 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 5515 | //to update install status |
| 5516 | mSettings.writeLP(); |
| 5517 | } |
| 5518 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5519 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5520 | private void installPackageLI(InstallArgs args, |
| 5521 | boolean newInstall, PackageInstalledInfo res) { |
| 5522 | int pFlags = args.flags; |
| 5523 | String installerPackageName = args.installerPackageName; |
| 5524 | File tmpPackageFile = new File(args.getCodePath()); |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5525 | boolean forwardLocked = ((pFlags & PackageManager.INSTALL_FORWARD_LOCK) != 0); |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5526 | boolean onSd = ((pFlags & PackageManager.INSTALL_EXTERNAL) != 0); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 5527 | boolean replace = false; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5528 | int scanMode = SCAN_MONITOR | SCAN_FORCE_DEX | SCAN_UPDATE_SIGNATURE |
| 5529 | | (newInstall ? SCAN_NEW_INSTALL : 0); |
| Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5530 | // Result object to be returned |
| 5531 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 5532 | |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 5533 | // Retrieve PackageSettings and parse package |
| 5534 | int parseFlags = PackageParser.PARSE_CHATTY | |
| 5535 | (forwardLocked ? PackageParser.PARSE_FORWARD_LOCK : 0) | |
| 5536 | (onSd ? PackageParser.PARSE_ON_SDCARD : 0); |
| 5537 | parseFlags |= mDefParseFlags; |
| 5538 | PackageParser pp = new PackageParser(tmpPackageFile.getPath()); |
| 5539 | pp.setSeparateProcesses(mSeparateProcesses); |
| 5540 | final PackageParser.Package pkg = pp.parsePackage(tmpPackageFile, |
| 5541 | null, mMetrics, parseFlags); |
| 5542 | if (pkg == null) { |
| 5543 | res.returnCode = pp.getParseError(); |
| 5544 | return; |
| 5545 | } |
| 5546 | String pkgName = res.name = pkg.packageName; |
| 5547 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_TEST_ONLY) != 0) { |
| 5548 | if ((pFlags&PackageManager.INSTALL_ALLOW_TEST) == 0) { |
| 5549 | res.returnCode = PackageManager.INSTALL_FAILED_TEST_ONLY; |
| 5550 | return; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5551 | } |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 5552 | } |
| 5553 | if (GET_CERTIFICATES && !pp.collectCertificates(pkg, parseFlags)) { |
| 5554 | res.returnCode = pp.getParseError(); |
| 5555 | return; |
| 5556 | } |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 5557 | // Get rid of all references to package scan path via parser. |
| 5558 | pp = null; |
| 5559 | String oldCodePath = null; |
| 5560 | boolean systemApp = false; |
| 5561 | synchronized (mPackages) { |
| 5562 | // Check if installing already existing package |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5563 | if ((pFlags&PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| 5564 | String oldName = mSettings.mRenamedPackages.get(pkgName); |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 5565 | if (pkg.mOriginalPackages != null |
| 5566 | && pkg.mOriginalPackages.contains(oldName) |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5567 | && mPackages.containsKey(oldName)) { |
| 5568 | // This package is derived from an original package, |
| 5569 | // and this device has been updating from that original |
| 5570 | // name. We must continue using the original name, so |
| 5571 | // rename the new package here. |
| Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 5572 | pkg.setPackageName(oldName); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5573 | pkgName = pkg.packageName; |
| 5574 | replace = true; |
| 5575 | } else if (mPackages.containsKey(pkgName)) { |
| 5576 | // This package, under its official name, already exists |
| 5577 | // on the device; we should replace it. |
| 5578 | replace = true; |
| 5579 | } |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 5580 | } |
| 5581 | PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 5582 | if (ps != null) { |
| 5583 | oldCodePath = mSettings.mPackages.get(pkgName).codePathString; |
| 5584 | if (ps.pkg != null && ps.pkg.applicationInfo != null) { |
| 5585 | systemApp = (ps.pkg.applicationInfo.flags & |
| 5586 | ApplicationInfo.FLAG_SYSTEM) != 0; |
| Dianne Hackborn | ade3eca | 2009-05-11 18:54:45 -0700 | [diff] [blame] | 5587 | } |
| 5588 | } |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 5589 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5590 | |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 5591 | if (systemApp && onSd) { |
| 5592 | // Disable updates to system apps on sdcard |
| 5593 | Log.w(TAG, "Cannot install updates to system apps on sdcard"); |
| 5594 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
| 5595 | return; |
| 5596 | } |
| Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 5597 | |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 5598 | if (!args.doRename(res.returnCode, pkgName, oldCodePath)) { |
| 5599 | res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 5600 | return; |
| 5601 | } |
| 5602 | // Set application objects path explicitly after the rename |
| 5603 | setApplicationInfoPaths(pkg, args.getCodePath(), args.getResourcePath()); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5604 | if (replace) { |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 5605 | replacePackageLI(pkg, parseFlags, scanMode, |
| 5606 | installerPackageName, res); |
| 5607 | } else { |
| 5608 | installNewPackageLI(pkg, parseFlags, scanMode, |
| 5609 | installerPackageName,res); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5610 | } |
| 5611 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5612 | |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5613 | private int setPermissionsLI(PackageParser.Package newPackage) { |
| 5614 | String pkgName = newPackage.packageName; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5615 | int retCode = 0; |
| 5616 | // TODO Gross hack but fix later. Ideally move this to be a post installation |
| 5617 | // check after alloting uid. |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5618 | if ((newPackage.applicationInfo.flags |
| 5619 | & ApplicationInfo.FLAG_FORWARD_LOCK) != 0) { |
| 5620 | File destResourceFile = new File(newPackage.applicationInfo.publicSourceDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5621 | try { |
| 5622 | extractPublicFiles(newPackage, destResourceFile); |
| 5623 | } catch (IOException e) { |
| 5624 | Log.e(TAG, "Couldn't create a new zip file for the public parts of a" + |
| 5625 | " forward-locked app."); |
| 5626 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 5627 | } finally { |
| 5628 | //TODO clean up the extracted public files |
| 5629 | } |
| 5630 | if (mInstaller != null) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5631 | retCode = mInstaller.setForwardLockPerm(getApkName(newPackage.mPath), |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5632 | newPackage.applicationInfo.uid); |
| 5633 | } else { |
| 5634 | final int filePermissions = |
| 5635 | FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5636 | retCode = FileUtils.setPermissions(newPackage.mPath, filePermissions, -1, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5637 | newPackage.applicationInfo.uid); |
| 5638 | } |
| 5639 | } else { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5640 | // The permissions on the resource file was set when it was copied for |
| 5641 | // non forward locked apps and apps on sdcard |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5642 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5643 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5644 | if (retCode != 0) { |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5645 | Log.e(TAG, "Couldn't set new package file permissions for " + |
| 5646 | newPackage.mPath |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5647 | + ". The return code was: " + retCode); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5648 | // TODO Define new internal error |
| 5649 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5650 | } |
| 5651 | return PackageManager.INSTALL_SUCCEEDED; |
| 5652 | } |
| 5653 | |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5654 | private boolean isForwardLocked(PackageParser.Package pkg) { |
| 5655 | return ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5656 | } |
| 5657 | |
| 5658 | private void extractPublicFiles(PackageParser.Package newPackage, |
| 5659 | File publicZipFile) throws IOException { |
| 5660 | final ZipOutputStream publicZipOutStream = |
| 5661 | new ZipOutputStream(new FileOutputStream(publicZipFile)); |
| 5662 | final ZipFile privateZip = new ZipFile(newPackage.mPath); |
| 5663 | |
| 5664 | // Copy manifest, resources.arsc and res directory to public zip |
| 5665 | |
| 5666 | final Enumeration<? extends ZipEntry> privateZipEntries = privateZip.entries(); |
| 5667 | while (privateZipEntries.hasMoreElements()) { |
| 5668 | final ZipEntry zipEntry = privateZipEntries.nextElement(); |
| 5669 | final String zipEntryName = zipEntry.getName(); |
| 5670 | if ("AndroidManifest.xml".equals(zipEntryName) |
| 5671 | || "resources.arsc".equals(zipEntryName) |
| 5672 | || zipEntryName.startsWith("res/")) { |
| 5673 | try { |
| 5674 | copyZipEntry(zipEntry, privateZip, publicZipOutStream); |
| 5675 | } catch (IOException e) { |
| 5676 | try { |
| 5677 | publicZipOutStream.close(); |
| 5678 | throw e; |
| 5679 | } finally { |
| 5680 | publicZipFile.delete(); |
| 5681 | } |
| 5682 | } |
| 5683 | } |
| 5684 | } |
| 5685 | |
| 5686 | publicZipOutStream.close(); |
| 5687 | FileUtils.setPermissions( |
| 5688 | publicZipFile.getAbsolutePath(), |
| 5689 | FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP|FileUtils.S_IROTH, |
| 5690 | -1, -1); |
| 5691 | } |
| 5692 | |
| 5693 | private static void copyZipEntry(ZipEntry zipEntry, |
| 5694 | ZipFile inZipFile, |
| 5695 | ZipOutputStream outZipStream) throws IOException { |
| 5696 | byte[] buffer = new byte[4096]; |
| 5697 | int num; |
| 5698 | |
| 5699 | ZipEntry newEntry; |
| 5700 | if (zipEntry.getMethod() == ZipEntry.STORED) { |
| 5701 | // Preserve the STORED method of the input entry. |
| 5702 | newEntry = new ZipEntry(zipEntry); |
| 5703 | } else { |
| 5704 | // Create a new entry so that the compressed len is recomputed. |
| 5705 | newEntry = new ZipEntry(zipEntry.getName()); |
| 5706 | } |
| 5707 | outZipStream.putNextEntry(newEntry); |
| 5708 | |
| 5709 | InputStream data = inZipFile.getInputStream(zipEntry); |
| 5710 | while ((num = data.read(buffer)) > 0) { |
| 5711 | outZipStream.write(buffer, 0, num); |
| 5712 | } |
| 5713 | outZipStream.flush(); |
| 5714 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5715 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5716 | private void deleteTempPackageFiles() { |
| 5717 | FilenameFilter filter = new FilenameFilter() { |
| 5718 | public boolean accept(File dir, String name) { |
| 5719 | return name.startsWith("vmdl") && name.endsWith(".tmp"); |
| 5720 | } |
| 5721 | }; |
| 5722 | String tmpFilesList[] = mAppInstallDir.list(filter); |
| 5723 | if(tmpFilesList == null) { |
| 5724 | return; |
| 5725 | } |
| 5726 | for(int i = 0; i < tmpFilesList.length; i++) { |
| 5727 | File tmpFile = new File(mAppInstallDir, tmpFilesList[i]); |
| 5728 | tmpFile.delete(); |
| 5729 | } |
| 5730 | } |
| 5731 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5732 | private File createTempPackageFile(File installDir) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5733 | File tmpPackageFile; |
| 5734 | try { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5735 | tmpPackageFile = File.createTempFile("vmdl", ".tmp", installDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5736 | } catch (IOException e) { |
| 5737 | Log.e(TAG, "Couldn't create temp file for downloaded package file."); |
| 5738 | return null; |
| 5739 | } |
| 5740 | try { |
| 5741 | FileUtils.setPermissions( |
| 5742 | tmpPackageFile.getCanonicalPath(), FileUtils.S_IRUSR|FileUtils.S_IWUSR, |
| 5743 | -1, -1); |
| 5744 | } catch (IOException e) { |
| 5745 | Log.e(TAG, "Trouble getting the canoncical path for a temp file."); |
| 5746 | return null; |
| 5747 | } |
| 5748 | return tmpPackageFile; |
| 5749 | } |
| 5750 | |
| 5751 | public void deletePackage(final String packageName, |
| 5752 | final IPackageDeleteObserver observer, |
| 5753 | final int flags) { |
| 5754 | mContext.enforceCallingOrSelfPermission( |
| 5755 | android.Manifest.permission.DELETE_PACKAGES, null); |
| 5756 | // Queue up an async operation since the package deletion may take a little while. |
| 5757 | mHandler.post(new Runnable() { |
| 5758 | public void run() { |
| 5759 | mHandler.removeCallbacks(this); |
| 5760 | final boolean succeded = deletePackageX(packageName, true, true, flags); |
| 5761 | if (observer != null) { |
| 5762 | try { |
| 5763 | observer.packageDeleted(succeded); |
| 5764 | } catch (RemoteException e) { |
| 5765 | Log.i(TAG, "Observer no longer exists."); |
| 5766 | } //end catch |
| 5767 | } //end if |
| 5768 | } //end run |
| 5769 | }); |
| 5770 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5771 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5772 | /** |
| 5773 | * This method is an internal method that could be get invoked either |
| 5774 | * to delete an installed package or to clean up a failed installation. |
| 5775 | * After deleting an installed package, a broadcast is sent to notify any |
| 5776 | * listeners that the package has been installed. For cleaning up a failed |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5777 | * installation, the broadcast is not necessary since the package's |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5778 | * installation wouldn't have sent the initial broadcast either |
| 5779 | * The key steps in deleting a package are |
| 5780 | * deleting the package information in internal structures like mPackages, |
| 5781 | * deleting the packages base directories through installd |
| 5782 | * updating mSettings to reflect current status |
| 5783 | * persisting settings for later use |
| 5784 | * sending a broadcast if necessary |
| 5785 | */ |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5786 | private boolean deletePackageX(String packageName, boolean sendBroadCast, |
| 5787 | boolean deleteCodeAndResources, int flags) { |
| 5788 | PackageRemovedInfo info = new PackageRemovedInfo(); |
| Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 5789 | boolean res; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5790 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 5791 | IDevicePolicyManager dpm = IDevicePolicyManager.Stub.asInterface( |
| 5792 | ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)); |
| 5793 | try { |
| 5794 | if (dpm != null && dpm.packageHasActiveAdmins(packageName)) { |
| 5795 | Log.w(TAG, "Not removing package " + packageName + ": has active device admin"); |
| 5796 | return false; |
| 5797 | } |
| 5798 | } catch (RemoteException e) { |
| 5799 | } |
| 5800 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5801 | synchronized (mInstallLock) { |
| 5802 | res = deletePackageLI(packageName, deleteCodeAndResources, flags, info); |
| 5803 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5804 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5805 | if(res && sendBroadCast) { |
| Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 5806 | boolean systemUpdate = info.isRemovedPackageSystemUpdate; |
| 5807 | info.sendBroadcast(deleteCodeAndResources, systemUpdate); |
| 5808 | |
| 5809 | // If the removed package was a system update, the old system packaged |
| 5810 | // was re-enabled; we need to broadcast this information |
| 5811 | if (systemUpdate) { |
| 5812 | Bundle extras = new Bundle(1); |
| 5813 | extras.putInt(Intent.EXTRA_UID, info.removedUid >= 0 ? info.removedUid : info.uid); |
| 5814 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 5815 | |
| 5816 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, packageName, extras); |
| 5817 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName, extras); |
| 5818 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5819 | } |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 5820 | // Force a gc here. |
| 5821 | Runtime.getRuntime().gc(); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5822 | // Delete the resources here after sending the broadcast to let |
| 5823 | // other processes clean up before deleting resources. |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5824 | if (info.args != null) { |
| 5825 | synchronized (mInstallLock) { |
| 5826 | info.args.doPostDeleteLI(deleteCodeAndResources); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5827 | } |
| 5828 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5829 | return res; |
| 5830 | } |
| 5831 | |
| 5832 | static class PackageRemovedInfo { |
| 5833 | String removedPackage; |
| 5834 | int uid = -1; |
| 5835 | int removedUid = -1; |
| Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 5836 | boolean isRemovedPackageSystemUpdate = false; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5837 | // Clean up resources deleted packages. |
| 5838 | InstallArgs args = null; |
| Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 5839 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5840 | void sendBroadcast(boolean fullRemove, boolean replacing) { |
| 5841 | Bundle extras = new Bundle(1); |
| 5842 | extras.putInt(Intent.EXTRA_UID, removedUid >= 0 ? removedUid : uid); |
| 5843 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, fullRemove); |
| 5844 | if (replacing) { |
| 5845 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 5846 | } |
| 5847 | if (removedPackage != null) { |
| 5848 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, extras); |
| 5849 | } |
| 5850 | if (removedUid >= 0) { |
| 5851 | sendPackageBroadcast(Intent.ACTION_UID_REMOVED, null, extras); |
| 5852 | } |
| 5853 | } |
| 5854 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5855 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5856 | /* |
| 5857 | * This method deletes the package from internal data structures. If the DONT_DELETE_DATA |
| 5858 | * flag is not set, the data directory is removed as well. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5859 | * make sure this flag is set for partially installed apps. If not its meaningless to |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5860 | * delete a partially installed application. |
| 5861 | */ |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5862 | private void removePackageDataLI(PackageParser.Package p, PackageRemovedInfo outInfo, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5863 | int flags) { |
| 5864 | String packageName = p.packageName; |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 5865 | if (outInfo != null) { |
| 5866 | outInfo.removedPackage = packageName; |
| 5867 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5868 | removePackageLI(p, true); |
| 5869 | // Retrieve object to delete permissions for shared user later on |
| 5870 | PackageSetting deletedPs; |
| 5871 | synchronized (mPackages) { |
| 5872 | deletedPs = mSettings.mPackages.get(packageName); |
| 5873 | } |
| 5874 | if ((flags&PackageManager.DONT_DELETE_DATA) == 0) { |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 5875 | boolean useEncryptedFSDir = useEncryptedFilesystemForPackage(p); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5876 | if (mInstaller != null) { |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 5877 | int retCode = mInstaller.remove(packageName, useEncryptedFSDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5878 | if (retCode < 0) { |
| 5879 | Log.w(TAG, "Couldn't remove app data or cache directory for package: " |
| 5880 | + packageName + ", retcode=" + retCode); |
| 5881 | // we don't consider this to be a failure of the core package deletion |
| 5882 | } |
| 5883 | } else { |
| 5884 | //for emulator |
| 5885 | PackageParser.Package pkg = mPackages.get(packageName); |
| 5886 | File dataDir = new File(pkg.applicationInfo.dataDir); |
| 5887 | dataDir.delete(); |
| 5888 | } |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5889 | schedulePackageCleaning(packageName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5890 | synchronized (mPackages) { |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 5891 | if (outInfo != null) { |
| 5892 | outInfo.removedUid = mSettings.removePackageLP(packageName); |
| 5893 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5894 | } |
| 5895 | } |
| 5896 | synchronized (mPackages) { |
| 5897 | if ( (deletedPs != null) && (deletedPs.sharedUser != null)) { |
| 5898 | // remove permissions associated with package |
| Suchi Amalapurapu | 2ed287b | 2009-08-05 12:43:00 -0700 | [diff] [blame] | 5899 | mSettings.updateSharedUserPermsLP(deletedPs, mGlobalGids); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5900 | } |
| Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5901 | if (deletedPs != null) { |
| 5902 | // remove from preferred activities. |
| 5903 | ArrayList<PreferredActivity> removed = new ArrayList<PreferredActivity>(); |
| 5904 | for (PreferredActivity pa : mSettings.mPreferredActivities.filterSet()) { |
| 5905 | if (pa.mActivity.getPackageName().equals(deletedPs.name)) { |
| 5906 | removed.add(pa); |
| 5907 | } |
| 5908 | } |
| 5909 | for (PreferredActivity pa : removed) { |
| 5910 | mSettings.mPreferredActivities.removeFilter(pa); |
| 5911 | } |
| 5912 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5913 | // Save settings now |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5914 | mSettings.writeLP(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5915 | } |
| 5916 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5917 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5918 | /* |
| 5919 | * Tries to delete system package. |
| 5920 | */ |
| 5921 | private boolean deleteSystemPackageLI(PackageParser.Package p, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 5922 | int flags, PackageRemovedInfo outInfo) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5923 | ApplicationInfo applicationInfo = p.applicationInfo; |
| 5924 | //applicable for non-partially installed applications only |
| 5925 | if (applicationInfo == null) { |
| 5926 | Log.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
| 5927 | return false; |
| 5928 | } |
| 5929 | PackageSetting ps = null; |
| 5930 | // Confirm if the system package has been updated |
| 5931 | // An updated system app can be deleted. This will also have to restore |
| 5932 | // the system pkg from system partition |
| 5933 | synchronized (mPackages) { |
| 5934 | ps = mSettings.getDisabledSystemPkg(p.packageName); |
| 5935 | } |
| 5936 | if (ps == null) { |
| 5937 | Log.w(TAG, "Attempt to delete system package "+ p.packageName); |
| 5938 | return false; |
| 5939 | } else { |
| 5940 | Log.i(TAG, "Deleting system pkg from data partition"); |
| 5941 | } |
| 5942 | // Delete the updated package |
| Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 5943 | outInfo.isRemovedPackageSystemUpdate = true; |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 5944 | boolean deleteCodeAndResources = false; |
| 5945 | if (ps.versionCode < p.mVersionCode) { |
| 5946 | // Delete code and resources for downgrades |
| 5947 | deleteCodeAndResources = true; |
| 5948 | if ((flags & PackageManager.DONT_DELETE_DATA) == 0) { |
| 5949 | flags &= ~PackageManager.DONT_DELETE_DATA; |
| 5950 | } |
| 5951 | } else { |
| 5952 | // Preserve data by setting flag |
| 5953 | if ((flags & PackageManager.DONT_DELETE_DATA) == 0) { |
| 5954 | flags |= PackageManager.DONT_DELETE_DATA; |
| 5955 | } |
| 5956 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5957 | boolean ret = deleteInstalledPackageLI(p, deleteCodeAndResources, flags, outInfo); |
| 5958 | if (!ret) { |
| 5959 | return false; |
| 5960 | } |
| 5961 | synchronized (mPackages) { |
| 5962 | // Reinstate the old system package |
| 5963 | mSettings.enableSystemPackageLP(p.packageName); |
| 5964 | } |
| 5965 | // Install the system package |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5966 | PackageParser.Package newPkg = scanPackageLI(ps.codePath, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5967 | PackageParser.PARSE_MUST_BE_APK | PackageParser.PARSE_IS_SYSTEM, |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5968 | SCAN_MONITOR | SCAN_NO_PATHS); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5969 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5970 | if (newPkg == null) { |
| 5971 | Log.w(TAG, "Failed to restore system package:"+p.packageName+" with error:" + mLastScanError); |
| 5972 | return false; |
| 5973 | } |
| 5974 | synchronized (mPackages) { |
| Suchi Amalapurapu | 701f516 | 2009-06-03 15:47:55 -0700 | [diff] [blame] | 5975 | grantPermissionsLP(newPkg, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5976 | mSettings.writeLP(); |
| 5977 | } |
| 5978 | return true; |
| 5979 | } |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 5980 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5981 | private boolean deleteInstalledPackageLI(PackageParser.Package p, |
| 5982 | boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo) { |
| 5983 | ApplicationInfo applicationInfo = p.applicationInfo; |
| 5984 | if (applicationInfo == null) { |
| 5985 | Log.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
| 5986 | return false; |
| 5987 | } |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 5988 | if (outInfo != null) { |
| 5989 | outInfo.uid = applicationInfo.uid; |
| 5990 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5991 | |
| 5992 | // Delete package data from internal structures and also remove data if flag is set |
| 5993 | removePackageDataLI(p, outInfo, flags); |
| 5994 | |
| 5995 | // Delete application code and resources |
| 5996 | if (deleteCodeAndResources) { |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5997 | // TODO can pick up from PackageSettings as well |
| Suchi Amalapurapu | 6069beb | 2010-03-10 09:46:49 -0800 | [diff] [blame] | 5998 | int installFlags = ((p.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE)!=0) ? |
| Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5999 | PackageManager.INSTALL_EXTERNAL : 0; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6000 | installFlags |= ((p.applicationInfo.flags & ApplicationInfo.FLAG_FORWARD_LOCK)!=0) ? |
| 6001 | PackageManager.INSTALL_FORWARD_LOCK : 0; |
| 6002 | outInfo.args = createInstallArgs(installFlags, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 6003 | applicationInfo.sourceDir, applicationInfo.publicSourceDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6004 | } |
| 6005 | return true; |
| 6006 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6007 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6008 | /* |
| 6009 | * This method handles package deletion in general |
| 6010 | */ |
| 6011 | private boolean deletePackageLI(String packageName, |
| 6012 | boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo) { |
| 6013 | if (packageName == null) { |
| 6014 | Log.w(TAG, "Attempt to delete null packageName."); |
| 6015 | return false; |
| 6016 | } |
| 6017 | PackageParser.Package p; |
| 6018 | boolean dataOnly = false; |
| 6019 | synchronized (mPackages) { |
| 6020 | p = mPackages.get(packageName); |
| 6021 | if (p == null) { |
| 6022 | //this retrieves partially installed apps |
| 6023 | dataOnly = true; |
| 6024 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 6025 | if (ps == null) { |
| 6026 | Log.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
| 6027 | return false; |
| 6028 | } |
| 6029 | p = ps.pkg; |
| 6030 | } |
| 6031 | } |
| 6032 | if (p == null) { |
| 6033 | Log.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
| 6034 | return false; |
| 6035 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6036 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6037 | if (dataOnly) { |
| 6038 | // Delete application data first |
| 6039 | removePackageDataLI(p, outInfo, flags); |
| 6040 | return true; |
| 6041 | } |
| 6042 | // At this point the package should have ApplicationInfo associated with it |
| 6043 | if (p.applicationInfo == null) { |
| 6044 | Log.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
| 6045 | return false; |
| 6046 | } |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6047 | boolean ret = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6048 | if ( (p.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 6049 | Log.i(TAG, "Removing system package:"+p.packageName); |
| 6050 | // When an updated system application is deleted we delete the existing resources as well and |
| 6051 | // fall back to existing code in system partition |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6052 | ret = deleteSystemPackageLI(p, flags, outInfo); |
| 6053 | } else { |
| 6054 | Log.i(TAG, "Removing non-system package:"+p.packageName); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6055 | // Kill application pre-emptively especially for apps on sd. |
| 6056 | killApplication(packageName, p.applicationInfo.uid); |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6057 | ret = deleteInstalledPackageLI (p, deleteCodeAndResources, flags, outInfo); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6058 | } |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6059 | return ret; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6060 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6061 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6062 | public void clearApplicationUserData(final String packageName, |
| 6063 | final IPackageDataObserver observer) { |
| 6064 | mContext.enforceCallingOrSelfPermission( |
| 6065 | android.Manifest.permission.CLEAR_APP_USER_DATA, null); |
| 6066 | // Queue up an async operation since the package deletion may take a little while. |
| 6067 | mHandler.post(new Runnable() { |
| 6068 | public void run() { |
| 6069 | mHandler.removeCallbacks(this); |
| 6070 | final boolean succeeded; |
| 6071 | synchronized (mInstallLock) { |
| 6072 | succeeded = clearApplicationUserDataLI(packageName); |
| 6073 | } |
| 6074 | if (succeeded) { |
| 6075 | // invoke DeviceStorageMonitor's update method to clear any notifications |
| 6076 | DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) |
| 6077 | ServiceManager.getService(DeviceStorageMonitorService.SERVICE); |
| 6078 | if (dsm != null) { |
| 6079 | dsm.updateMemory(); |
| 6080 | } |
| 6081 | } |
| 6082 | if(observer != null) { |
| 6083 | try { |
| 6084 | observer.onRemoveCompleted(packageName, succeeded); |
| 6085 | } catch (RemoteException e) { |
| 6086 | Log.i(TAG, "Observer no longer exists."); |
| 6087 | } |
| 6088 | } //end if observer |
| 6089 | } //end run |
| 6090 | }); |
| 6091 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6092 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6093 | private boolean clearApplicationUserDataLI(String packageName) { |
| 6094 | if (packageName == null) { |
| 6095 | Log.w(TAG, "Attempt to delete null packageName."); |
| 6096 | return false; |
| 6097 | } |
| 6098 | PackageParser.Package p; |
| 6099 | boolean dataOnly = false; |
| 6100 | synchronized (mPackages) { |
| 6101 | p = mPackages.get(packageName); |
| 6102 | if(p == null) { |
| 6103 | dataOnly = true; |
| 6104 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 6105 | if((ps == null) || (ps.pkg == null)) { |
| 6106 | Log.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
| 6107 | return false; |
| 6108 | } |
| 6109 | p = ps.pkg; |
| 6110 | } |
| 6111 | } |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6112 | boolean useEncryptedFSDir = false; |
| 6113 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6114 | if(!dataOnly) { |
| 6115 | //need to check this only for fully installed applications |
| 6116 | if (p == null) { |
| 6117 | Log.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
| 6118 | return false; |
| 6119 | } |
| 6120 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 6121 | if (applicationInfo == null) { |
| 6122 | Log.w(TAG, "Package " + packageName + " has no applicationInfo."); |
| 6123 | return false; |
| 6124 | } |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6125 | useEncryptedFSDir = useEncryptedFilesystemForPackage(p); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6126 | } |
| 6127 | if (mInstaller != null) { |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6128 | int retCode = mInstaller.clearUserData(packageName, useEncryptedFSDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6129 | if (retCode < 0) { |
| 6130 | Log.w(TAG, "Couldn't remove cache files for package: " |
| 6131 | + packageName); |
| 6132 | return false; |
| 6133 | } |
| 6134 | } |
| 6135 | return true; |
| 6136 | } |
| 6137 | |
| 6138 | public void deleteApplicationCacheFiles(final String packageName, |
| 6139 | final IPackageDataObserver observer) { |
| 6140 | mContext.enforceCallingOrSelfPermission( |
| 6141 | android.Manifest.permission.DELETE_CACHE_FILES, null); |
| 6142 | // Queue up an async operation since the package deletion may take a little while. |
| 6143 | mHandler.post(new Runnable() { |
| 6144 | public void run() { |
| 6145 | mHandler.removeCallbacks(this); |
| 6146 | final boolean succeded; |
| 6147 | synchronized (mInstallLock) { |
| 6148 | succeded = deleteApplicationCacheFilesLI(packageName); |
| 6149 | } |
| 6150 | if(observer != null) { |
| 6151 | try { |
| 6152 | observer.onRemoveCompleted(packageName, succeded); |
| 6153 | } catch (RemoteException e) { |
| 6154 | Log.i(TAG, "Observer no longer exists."); |
| 6155 | } |
| 6156 | } //end if observer |
| 6157 | } //end run |
| 6158 | }); |
| 6159 | } |
| 6160 | |
| 6161 | private boolean deleteApplicationCacheFilesLI(String packageName) { |
| 6162 | if (packageName == null) { |
| 6163 | Log.w(TAG, "Attempt to delete null packageName."); |
| 6164 | return false; |
| 6165 | } |
| 6166 | PackageParser.Package p; |
| 6167 | synchronized (mPackages) { |
| 6168 | p = mPackages.get(packageName); |
| 6169 | } |
| 6170 | if (p == null) { |
| 6171 | Log.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
| 6172 | return false; |
| 6173 | } |
| 6174 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 6175 | if (applicationInfo == null) { |
| 6176 | Log.w(TAG, "Package " + packageName + " has no applicationInfo."); |
| 6177 | return false; |
| 6178 | } |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6179 | boolean useEncryptedFSDir = useEncryptedFilesystemForPackage(p); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6180 | if (mInstaller != null) { |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6181 | int retCode = mInstaller.deleteCacheFiles(packageName, useEncryptedFSDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6182 | if (retCode < 0) { |
| 6183 | Log.w(TAG, "Couldn't remove cache files for package: " |
| 6184 | + packageName); |
| 6185 | return false; |
| 6186 | } |
| 6187 | } |
| 6188 | return true; |
| 6189 | } |
| 6190 | |
| 6191 | public void getPackageSizeInfo(final String packageName, |
| 6192 | final IPackageStatsObserver observer) { |
| 6193 | mContext.enforceCallingOrSelfPermission( |
| 6194 | android.Manifest.permission.GET_PACKAGE_SIZE, null); |
| 6195 | // Queue up an async operation since the package deletion may take a little while. |
| 6196 | mHandler.post(new Runnable() { |
| 6197 | public void run() { |
| 6198 | mHandler.removeCallbacks(this); |
| 6199 | PackageStats lStats = new PackageStats(packageName); |
| 6200 | final boolean succeded; |
| 6201 | synchronized (mInstallLock) { |
| 6202 | succeded = getPackageSizeInfoLI(packageName, lStats); |
| 6203 | } |
| 6204 | if(observer != null) { |
| 6205 | try { |
| 6206 | observer.onGetStatsCompleted(lStats, succeded); |
| 6207 | } catch (RemoteException e) { |
| 6208 | Log.i(TAG, "Observer no longer exists."); |
| 6209 | } |
| 6210 | } //end if observer |
| 6211 | } //end run |
| 6212 | }); |
| 6213 | } |
| 6214 | |
| 6215 | private boolean getPackageSizeInfoLI(String packageName, PackageStats pStats) { |
| 6216 | if (packageName == null) { |
| 6217 | Log.w(TAG, "Attempt to get size of null packageName."); |
| 6218 | return false; |
| 6219 | } |
| 6220 | PackageParser.Package p; |
| 6221 | boolean dataOnly = false; |
| 6222 | synchronized (mPackages) { |
| 6223 | p = mPackages.get(packageName); |
| 6224 | if(p == null) { |
| 6225 | dataOnly = true; |
| 6226 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 6227 | if((ps == null) || (ps.pkg == null)) { |
| 6228 | Log.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
| 6229 | return false; |
| 6230 | } |
| 6231 | p = ps.pkg; |
| 6232 | } |
| 6233 | } |
| 6234 | String publicSrcDir = null; |
| 6235 | if(!dataOnly) { |
| 6236 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 6237 | if (applicationInfo == null) { |
| 6238 | Log.w(TAG, "Package " + packageName + " has no applicationInfo."); |
| 6239 | return false; |
| 6240 | } |
| 6241 | publicSrcDir = isForwardLocked(p) ? applicationInfo.publicSourceDir : null; |
| 6242 | } |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6243 | boolean useEncryptedFSDir = useEncryptedFilesystemForPackage(p); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6244 | if (mInstaller != null) { |
| 6245 | int res = mInstaller.getSizeInfo(packageName, p.mPath, |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6246 | publicSrcDir, pStats, useEncryptedFSDir); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6247 | if (res < 0) { |
| 6248 | return false; |
| 6249 | } else { |
| 6250 | return true; |
| 6251 | } |
| 6252 | } |
| 6253 | return true; |
| 6254 | } |
| 6255 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6256 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6257 | public void addPackageToPreferred(String packageName) { |
| 6258 | mContext.enforceCallingOrSelfPermission( |
| 6259 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| Dianne Hackborn | a7ca0e5 | 2009-12-01 14:31:55 -0800 | [diff] [blame] | 6260 | Log.w(TAG, "addPackageToPreferred: no longer implemented"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6261 | } |
| 6262 | |
| 6263 | public void removePackageFromPreferred(String packageName) { |
| 6264 | mContext.enforceCallingOrSelfPermission( |
| 6265 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| Dianne Hackborn | a7ca0e5 | 2009-12-01 14:31:55 -0800 | [diff] [blame] | 6266 | Log.w(TAG, "removePackageFromPreferred: no longer implemented"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6267 | } |
| 6268 | |
| 6269 | public List<PackageInfo> getPreferredPackages(int flags) { |
| Dianne Hackborn | a7ca0e5 | 2009-12-01 14:31:55 -0800 | [diff] [blame] | 6270 | return new ArrayList<PackageInfo>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6271 | } |
| 6272 | |
| 6273 | public void addPreferredActivity(IntentFilter filter, int match, |
| 6274 | ComponentName[] set, ComponentName activity) { |
| 6275 | mContext.enforceCallingOrSelfPermission( |
| 6276 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 6277 | |
| 6278 | synchronized (mPackages) { |
| 6279 | Log.i(TAG, "Adding preferred activity " + activity + ":"); |
| 6280 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
| 6281 | mSettings.mPreferredActivities.addFilter( |
| 6282 | new PreferredActivity(filter, match, set, activity)); |
| 6283 | mSettings.writeLP(); |
| 6284 | } |
| 6285 | } |
| 6286 | |
| Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 6287 | public void replacePreferredActivity(IntentFilter filter, int match, |
| 6288 | ComponentName[] set, ComponentName activity) { |
| 6289 | mContext.enforceCallingOrSelfPermission( |
| 6290 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 6291 | if (filter.countActions() != 1) { |
| 6292 | throw new IllegalArgumentException( |
| 6293 | "replacePreferredActivity expects filter to have only 1 action."); |
| 6294 | } |
| 6295 | if (filter.countCategories() != 1) { |
| 6296 | throw new IllegalArgumentException( |
| 6297 | "replacePreferredActivity expects filter to have only 1 category."); |
| 6298 | } |
| 6299 | if (filter.countDataAuthorities() != 0 |
| 6300 | || filter.countDataPaths() != 0 |
| 6301 | || filter.countDataSchemes() != 0 |
| 6302 | || filter.countDataTypes() != 0) { |
| 6303 | throw new IllegalArgumentException( |
| 6304 | "replacePreferredActivity expects filter to have no data authorities, " + |
| 6305 | "paths, schemes or types."); |
| 6306 | } |
| 6307 | synchronized (mPackages) { |
| 6308 | Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
| 6309 | String action = filter.getAction(0); |
| 6310 | String category = filter.getCategory(0); |
| 6311 | while (it.hasNext()) { |
| 6312 | PreferredActivity pa = it.next(); |
| 6313 | if (pa.getAction(0).equals(action) && pa.getCategory(0).equals(category)) { |
| 6314 | it.remove(); |
| 6315 | Log.i(TAG, "Removed preferred activity " + pa.mActivity + ":"); |
| 6316 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
| 6317 | } |
| 6318 | } |
| 6319 | addPreferredActivity(filter, match, set, activity); |
| 6320 | } |
| 6321 | } |
| 6322 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6323 | public void clearPackagePreferredActivities(String packageName) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6324 | synchronized (mPackages) { |
| Dianne Hackborn | 2ee89ea | 2010-03-10 18:27:09 -0800 | [diff] [blame^] | 6325 | int uid = Binder.getCallingUid(); |
| 6326 | PackageParser.Package pkg = mPackages.get(packageName); |
| 6327 | if (pkg.applicationInfo.uid != uid) { |
| 6328 | mContext.enforceCallingOrSelfPermission( |
| 6329 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 6330 | } |
| 6331 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6332 | if (clearPackagePreferredActivitiesLP(packageName)) { |
| 6333 | mSettings.writeLP(); |
| 6334 | } |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | boolean clearPackagePreferredActivitiesLP(String packageName) { |
| 6339 | boolean changed = false; |
| 6340 | Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
| 6341 | while (it.hasNext()) { |
| 6342 | PreferredActivity pa = it.next(); |
| 6343 | if (pa.mActivity.getPackageName().equals(packageName)) { |
| 6344 | it.remove(); |
| 6345 | changed = true; |
| 6346 | } |
| 6347 | } |
| 6348 | return changed; |
| 6349 | } |
| 6350 | |
| 6351 | public int getPreferredActivities(List<IntentFilter> outFilters, |
| 6352 | List<ComponentName> outActivities, String packageName) { |
| 6353 | |
| 6354 | int num = 0; |
| 6355 | synchronized (mPackages) { |
| 6356 | Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
| 6357 | while (it.hasNext()) { |
| 6358 | PreferredActivity pa = it.next(); |
| 6359 | if (packageName == null |
| 6360 | || pa.mActivity.getPackageName().equals(packageName)) { |
| 6361 | if (outFilters != null) { |
| 6362 | outFilters.add(new IntentFilter(pa)); |
| 6363 | } |
| 6364 | if (outActivities != null) { |
| 6365 | outActivities.add(pa.mActivity); |
| 6366 | } |
| 6367 | } |
| 6368 | } |
| 6369 | } |
| 6370 | |
| 6371 | return num; |
| 6372 | } |
| 6373 | |
| 6374 | public void setApplicationEnabledSetting(String appPackageName, |
| 6375 | int newState, int flags) { |
| 6376 | setEnabledSetting(appPackageName, null, newState, flags); |
| 6377 | } |
| 6378 | |
| 6379 | public void setComponentEnabledSetting(ComponentName componentName, |
| 6380 | int newState, int flags) { |
| 6381 | setEnabledSetting(componentName.getPackageName(), |
| 6382 | componentName.getClassName(), newState, flags); |
| 6383 | } |
| 6384 | |
| 6385 | private void setEnabledSetting( |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6386 | final String packageName, String className, int newState, final int flags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6387 | if (!(newState == COMPONENT_ENABLED_STATE_DEFAULT |
| 6388 | || newState == COMPONENT_ENABLED_STATE_ENABLED |
| 6389 | || newState == COMPONENT_ENABLED_STATE_DISABLED)) { |
| 6390 | throw new IllegalArgumentException("Invalid new component state: " |
| 6391 | + newState); |
| 6392 | } |
| 6393 | PackageSetting pkgSetting; |
| 6394 | final int uid = Binder.getCallingUid(); |
| 6395 | final int permission = mContext.checkCallingPermission( |
| 6396 | android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); |
| 6397 | final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6398 | boolean sendNow = false; |
| 6399 | boolean isApp = (className == null); |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 6400 | String componentName = isApp ? packageName : className; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6401 | int packageUid = -1; |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 6402 | ArrayList<String> components; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6403 | synchronized (mPackages) { |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6404 | pkgSetting = mSettings.mPackages.get(packageName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6405 | if (pkgSetting == null) { |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6406 | if (className == null) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6407 | throw new IllegalArgumentException( |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6408 | "Unknown package: " + packageName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6409 | } |
| 6410 | throw new IllegalArgumentException( |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6411 | "Unknown component: " + packageName |
| 6412 | + "/" + className); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6413 | } |
| 6414 | if (!allowedByPermission && (uid != pkgSetting.userId)) { |
| 6415 | throw new SecurityException( |
| 6416 | "Permission Denial: attempt to change component state from pid=" |
| 6417 | + Binder.getCallingPid() |
| 6418 | + ", uid=" + uid + ", package uid=" + pkgSetting.userId); |
| 6419 | } |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6420 | if (className == null) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6421 | // We're dealing with an application/package level state change |
| 6422 | pkgSetting.enabled = newState; |
| 6423 | } else { |
| 6424 | // We're dealing with a component level state change |
| 6425 | switch (newState) { |
| 6426 | case COMPONENT_ENABLED_STATE_ENABLED: |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6427 | pkgSetting.enableComponentLP(className); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6428 | break; |
| 6429 | case COMPONENT_ENABLED_STATE_DISABLED: |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6430 | pkgSetting.disableComponentLP(className); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6431 | break; |
| 6432 | case COMPONENT_ENABLED_STATE_DEFAULT: |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6433 | pkgSetting.restoreComponentLP(className); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6434 | break; |
| 6435 | default: |
| 6436 | Log.e(TAG, "Invalid new component state: " + newState); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6437 | return; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6438 | } |
| 6439 | } |
| 6440 | mSettings.writeLP(); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6441 | packageUid = pkgSetting.userId; |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 6442 | components = mPendingBroadcasts.get(packageName); |
| 6443 | boolean newPackage = components == null; |
| 6444 | if (newPackage) { |
| 6445 | components = new ArrayList<String>(); |
| 6446 | } |
| 6447 | if (!components.contains(componentName)) { |
| 6448 | components.add(componentName); |
| 6449 | } |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6450 | if ((flags&PackageManager.DONT_KILL_APP) == 0) { |
| 6451 | sendNow = true; |
| 6452 | // Purge entry from pending broadcast list if another one exists already |
| 6453 | // since we are sending one right away. |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 6454 | mPendingBroadcasts.remove(packageName); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6455 | } else { |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 6456 | if (newPackage) { |
| 6457 | mPendingBroadcasts.put(packageName, components); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6458 | } |
| 6459 | if (!mHandler.hasMessages(SEND_PENDING_BROADCAST)) { |
| 6460 | // Schedule a message |
| 6461 | mHandler.sendEmptyMessageDelayed(SEND_PENDING_BROADCAST, BROADCAST_DELAY); |
| 6462 | } |
| 6463 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6464 | } |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6465 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6466 | long callingId = Binder.clearCallingIdentity(); |
| 6467 | try { |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6468 | if (sendNow) { |
| 6469 | sendPackageChangedBroadcast(packageName, |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 6470 | (flags&PackageManager.DONT_KILL_APP) != 0, components, packageUid); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6471 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6472 | } finally { |
| 6473 | Binder.restoreCallingIdentity(callingId); |
| 6474 | } |
| 6475 | } |
| 6476 | |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6477 | private void sendPackageChangedBroadcast(String packageName, |
| Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 6478 | boolean killFlag, ArrayList<String> componentNames, int packageUid) { |
| 6479 | if (false) Log.v(TAG, "Sending package changed: package=" + packageName |
| 6480 | + " components=" + componentNames); |
| 6481 | Bundle extras = new Bundle(4); |
| 6482 | extras.putString(Intent.EXTRA_CHANGED_COMPONENT_NAME, componentNames.get(0)); |
| 6483 | String nameList[] = new String[componentNames.size()]; |
| 6484 | componentNames.toArray(nameList); |
| 6485 | extras.putStringArray(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST, nameList); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6486 | extras.putBoolean(Intent.EXTRA_DONT_KILL_APP, killFlag); |
| 6487 | extras.putInt(Intent.EXTRA_UID, packageUid); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6488 | sendPackageBroadcast(Intent.ACTION_PACKAGE_CHANGED, packageName, extras); |
| Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 6489 | } |
| 6490 | |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6491 | public String getInstallerPackageName(String packageName) { |
| 6492 | synchronized (mPackages) { |
| 6493 | PackageSetting pkg = mSettings.mPackages.get(packageName); |
| 6494 | if (pkg == null) { |
| 6495 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 6496 | } |
| 6497 | return pkg.installerPackageName; |
| 6498 | } |
| 6499 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6500 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6501 | public int getApplicationEnabledSetting(String appPackageName) { |
| 6502 | synchronized (mPackages) { |
| 6503 | PackageSetting pkg = mSettings.mPackages.get(appPackageName); |
| 6504 | if (pkg == null) { |
| 6505 | throw new IllegalArgumentException("Unknown package: " + appPackageName); |
| 6506 | } |
| 6507 | return pkg.enabled; |
| 6508 | } |
| 6509 | } |
| 6510 | |
| 6511 | public int getComponentEnabledSetting(ComponentName componentName) { |
| 6512 | synchronized (mPackages) { |
| 6513 | final String packageNameStr = componentName.getPackageName(); |
| 6514 | PackageSetting pkg = mSettings.mPackages.get(packageNameStr); |
| 6515 | if (pkg == null) { |
| 6516 | throw new IllegalArgumentException("Unknown component: " + componentName); |
| 6517 | } |
| 6518 | final String classNameStr = componentName.getClassName(); |
| 6519 | return pkg.currentEnabledStateLP(classNameStr); |
| 6520 | } |
| 6521 | } |
| 6522 | |
| 6523 | public void enterSafeMode() { |
| 6524 | if (!mSystemReady) { |
| 6525 | mSafeMode = true; |
| 6526 | } |
| 6527 | } |
| 6528 | |
| 6529 | public void systemReady() { |
| 6530 | mSystemReady = true; |
| Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 6531 | |
| 6532 | // Read the compatibilty setting when the system is ready. |
| Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 6533 | boolean compatibilityModeEnabled = android.provider.Settings.System.getInt( |
| Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 6534 | mContext.getContentResolver(), |
| 6535 | android.provider.Settings.System.COMPATIBILITY_MODE, 1) == 1; |
| Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 6536 | PackageParser.setCompatibilityModeEnabled(compatibilityModeEnabled); |
| Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 6537 | if (DEBUG_SETTINGS) { |
| Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 6538 | Log.d(TAG, "compatibility mode:" + compatibilityModeEnabled); |
| Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 6539 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6540 | } |
| 6541 | |
| 6542 | public boolean isSafeMode() { |
| 6543 | return mSafeMode; |
| 6544 | } |
| 6545 | |
| 6546 | public boolean hasSystemUidErrors() { |
| 6547 | return mHasSystemUidErrors; |
| 6548 | } |
| 6549 | |
| 6550 | static String arrayToString(int[] array) { |
| 6551 | StringBuffer buf = new StringBuffer(128); |
| 6552 | buf.append('['); |
| 6553 | if (array != null) { |
| 6554 | for (int i=0; i<array.length; i++) { |
| 6555 | if (i > 0) buf.append(", "); |
| 6556 | buf.append(array[i]); |
| 6557 | } |
| 6558 | } |
| 6559 | buf.append(']'); |
| 6560 | return buf.toString(); |
| 6561 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6562 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6563 | @Override |
| 6564 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 6565 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 6566 | != PackageManager.PERMISSION_GRANTED) { |
| 6567 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 6568 | + Binder.getCallingPid() |
| 6569 | + ", uid=" + Binder.getCallingUid() |
| 6570 | + " without permission " |
| 6571 | + android.Manifest.permission.DUMP); |
| 6572 | return; |
| 6573 | } |
| 6574 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6575 | synchronized (mPackages) { |
| 6576 | pw.println("Activity Resolver Table:"); |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6577 | mActivities.dump(pw, " "); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6578 | pw.println(" "); |
| 6579 | pw.println("Receiver Resolver Table:"); |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6580 | mReceivers.dump(pw, " "); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6581 | pw.println(" "); |
| 6582 | pw.println("Service Resolver Table:"); |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6583 | mServices.dump(pw, " "); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6584 | pw.println(" "); |
| 6585 | pw.println("Preferred Activities:"); |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6586 | mSettings.mPreferredActivities.dump(pw, " "); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6587 | pw.println(" "); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6588 | pw.println("Permissions:"); |
| 6589 | { |
| 6590 | for (BasePermission p : mSettings.mPermissions.values()) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6591 | pw.print(" Permission ["); pw.print(p.name); pw.print("] ("); |
| 6592 | pw.print(Integer.toHexString(System.identityHashCode(p))); |
| 6593 | pw.println("):"); |
| 6594 | pw.print(" sourcePackage="); pw.println(p.sourcePackage); |
| 6595 | pw.print(" uid="); pw.print(p.uid); |
| 6596 | pw.print(" gids="); pw.print(arrayToString(p.gids)); |
| 6597 | pw.print(" type="); pw.println(p.type); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6598 | } |
| 6599 | } |
| 6600 | pw.println(" "); |
| 6601 | pw.println("Packages:"); |
| 6602 | { |
| 6603 | for (PackageSetting ps : mSettings.mPackages.values()) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 6604 | pw.print(" Package ["); |
| 6605 | pw.print(ps.realName != null ? ps.realName : ps.name); |
| 6606 | pw.print("] ("); |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6607 | pw.print(Integer.toHexString(System.identityHashCode(ps))); |
| 6608 | pw.println("):"); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 6609 | if (ps.realName != null) { |
| 6610 | pw.print(" compat name="); pw.println(ps.name); |
| 6611 | } |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6612 | pw.print(" userId="); pw.print(ps.userId); |
| 6613 | pw.print(" gids="); pw.println(arrayToString(ps.gids)); |
| 6614 | pw.print(" sharedUser="); pw.println(ps.sharedUser); |
| 6615 | pw.print(" pkg="); pw.println(ps.pkg); |
| 6616 | pw.print(" codePath="); pw.println(ps.codePathString); |
| 6617 | pw.print(" resourcePath="); pw.println(ps.resourcePathString); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6618 | if (ps.pkg != null) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6619 | pw.print(" dataDir="); pw.println(ps.pkg.applicationInfo.dataDir); |
| Mitsuru Oshima | 841f13c | 2009-07-17 17:23:31 -0700 | [diff] [blame] | 6620 | pw.print(" targetSdk="); pw.println(ps.pkg.applicationInfo.targetSdkVersion); |
| Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 6621 | pw.print(" supportsScreens=["); |
| 6622 | boolean first = true; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6623 | if ((ps.pkg.applicationInfo.flags & |
| Mitsuru Oshima | 841f13c | 2009-07-17 17:23:31 -0700 | [diff] [blame] | 6624 | ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS) != 0) { |
| Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 6625 | if (!first) pw.print(", "); |
| 6626 | first = false; |
| 6627 | pw.print("medium"); |
| Mitsuru Oshima | 841f13c | 2009-07-17 17:23:31 -0700 | [diff] [blame] | 6628 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6629 | if ((ps.pkg.applicationInfo.flags & |
| Mitsuru Oshima | 841f13c | 2009-07-17 17:23:31 -0700 | [diff] [blame] | 6630 | ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS) != 0) { |
| Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 6631 | if (!first) pw.print(", "); |
| 6632 | first = false; |
| 6633 | pw.print("large"); |
| Mitsuru Oshima | 841f13c | 2009-07-17 17:23:31 -0700 | [diff] [blame] | 6634 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6635 | if ((ps.pkg.applicationInfo.flags & |
| Mitsuru Oshima | 841f13c | 2009-07-17 17:23:31 -0700 | [diff] [blame] | 6636 | ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS) != 0) { |
| Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 6637 | if (!first) pw.print(", "); |
| 6638 | first = false; |
| 6639 | pw.print("small"); |
| Mitsuru Oshima | 841f13c | 2009-07-17 17:23:31 -0700 | [diff] [blame] | 6640 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6641 | if ((ps.pkg.applicationInfo.flags & |
| Dianne Hackborn | c4db95c | 2009-07-21 17:46:02 -0700 | [diff] [blame] | 6642 | ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS) != 0) { |
| Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 6643 | if (!first) pw.print(", "); |
| 6644 | first = false; |
| 6645 | pw.print("resizeable"); |
| Dianne Hackborn | c4db95c | 2009-07-21 17:46:02 -0700 | [diff] [blame] | 6646 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6647 | if ((ps.pkg.applicationInfo.flags & |
| Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 6648 | ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES) != 0) { |
| 6649 | if (!first) pw.print(", "); |
| 6650 | first = false; |
| 6651 | pw.print("anyDensity"); |
| 6652 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6653 | } |
| Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 6654 | pw.println("]"); |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6655 | pw.print(" timeStamp="); pw.println(ps.getTimeStampStr()); |
| 6656 | pw.print(" signatures="); pw.println(ps.signatures); |
| 6657 | pw.print(" permissionsFixed="); pw.print(ps.permissionsFixed); |
| 6658 | pw.print(" pkgFlags=0x"); pw.print(Integer.toHexString(ps.pkgFlags)); |
| 6659 | pw.print(" installStatus="); pw.print(ps.installStatus); |
| 6660 | pw.print(" enabled="); pw.println(ps.enabled); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6661 | if (ps.disabledComponents.size() > 0) { |
| 6662 | pw.println(" disabledComponents:"); |
| 6663 | for (String s : ps.disabledComponents) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6664 | pw.print(" "); pw.println(s); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6665 | } |
| 6666 | } |
| 6667 | if (ps.enabledComponents.size() > 0) { |
| 6668 | pw.println(" enabledComponents:"); |
| 6669 | for (String s : ps.enabledComponents) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6670 | pw.print(" "); pw.println(s); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6671 | } |
| 6672 | } |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6673 | if (ps.grantedPermissions.size() > 0) { |
| 6674 | pw.println(" grantedPermissions:"); |
| 6675 | for (String s : ps.grantedPermissions) { |
| 6676 | pw.print(" "); pw.println(s); |
| 6677 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6678 | } |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6679 | if (ps.loadedPermissions.size() > 0) { |
| 6680 | pw.println(" loadedPermissions:"); |
| 6681 | for (String s : ps.loadedPermissions) { |
| 6682 | pw.print(" "); pw.println(s); |
| 6683 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6684 | } |
| 6685 | } |
| 6686 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 6687 | if (mSettings.mRenamedPackages.size() > 0) { |
| 6688 | pw.println(" "); |
| 6689 | pw.println("Renamed packages:"); |
| 6690 | for (HashMap.Entry<String, String> e |
| 6691 | : mSettings.mRenamedPackages.entrySet()) { |
| 6692 | pw.print(" "); pw.print(e.getKey()); pw.print(" -> "); |
| 6693 | pw.println(e.getValue()); |
| 6694 | } |
| 6695 | } |
| 6696 | if (mSettings.mDisabledSysPackages.size() > 0) { |
| 6697 | pw.println(" "); |
| 6698 | pw.println("Hidden system packages:"); |
| 6699 | for (PackageSetting ps : mSettings.mDisabledSysPackages.values()) { |
| 6700 | pw.print(" Package ["); |
| 6701 | pw.print(ps.realName != null ? ps.realName : ps.name); |
| 6702 | pw.print("] ("); |
| 6703 | pw.print(Integer.toHexString(System.identityHashCode(ps))); |
| 6704 | pw.println("):"); |
| 6705 | if (ps.realName != null) { |
| 6706 | pw.print(" compat name="); pw.println(ps.name); |
| 6707 | } |
| 6708 | pw.print(" userId="); pw.println(ps.userId); |
| 6709 | pw.print(" sharedUser="); pw.println(ps.sharedUser); |
| 6710 | pw.print(" codePath="); pw.println(ps.codePathString); |
| 6711 | pw.print(" resourcePath="); pw.println(ps.resourcePathString); |
| 6712 | } |
| 6713 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6714 | pw.println(" "); |
| 6715 | pw.println("Shared Users:"); |
| 6716 | { |
| 6717 | for (SharedUserSetting su : mSettings.mSharedUsers.values()) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6718 | pw.print(" SharedUser ["); pw.print(su.name); pw.print("] ("); |
| 6719 | pw.print(Integer.toHexString(System.identityHashCode(su))); |
| 6720 | pw.println("):"); |
| 6721 | pw.print(" userId="); pw.print(su.userId); |
| 6722 | pw.print(" gids="); pw.println(arrayToString(su.gids)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6723 | pw.println(" grantedPermissions:"); |
| 6724 | for (String s : su.grantedPermissions) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6725 | pw.print(" "); pw.println(s); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6726 | } |
| 6727 | pw.println(" loadedPermissions:"); |
| 6728 | for (String s : su.loadedPermissions) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 6729 | pw.print(" "); pw.println(s); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6730 | } |
| 6731 | } |
| 6732 | } |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 6733 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6734 | pw.println(" "); |
| 6735 | pw.println("Settings parse messages:"); |
| 6736 | pw.println(mSettings.mReadMessages.toString()); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 6737 | |
| 6738 | pw.println(" "); |
| 6739 | pw.println("Package warning messages:"); |
| 6740 | File fname = getSettingsProblemFile(); |
| 6741 | FileInputStream in; |
| 6742 | try { |
| 6743 | in = new FileInputStream(fname); |
| 6744 | int avail = in.available(); |
| 6745 | byte[] data = new byte[avail]; |
| 6746 | in.read(data); |
| 6747 | pw.println(new String(data)); |
| 6748 | } catch (FileNotFoundException e) { |
| 6749 | } catch (IOException e) { |
| 6750 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6751 | } |
| Jeff Hamilton | 5bfc64f | 2009-08-18 12:25:30 -0500 | [diff] [blame] | 6752 | |
| 6753 | synchronized (mProviders) { |
| 6754 | pw.println(" "); |
| 6755 | pw.println("Registered ContentProviders:"); |
| 6756 | for (PackageParser.Provider p : mProviders.values()) { |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 6757 | pw.print(" ["); pw.print(p.info.authority); pw.print("]: "); |
| Jeff Hamilton | 5bfc64f | 2009-08-18 12:25:30 -0500 | [diff] [blame] | 6758 | pw.println(p.toString()); |
| 6759 | } |
| 6760 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6761 | } |
| 6762 | |
| 6763 | static final class BasePermission { |
| 6764 | final static int TYPE_NORMAL = 0; |
| 6765 | final static int TYPE_BUILTIN = 1; |
| 6766 | final static int TYPE_DYNAMIC = 2; |
| 6767 | |
| 6768 | final String name; |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 6769 | String sourcePackage; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6770 | final int type; |
| 6771 | PackageParser.Permission perm; |
| 6772 | PermissionInfo pendingInfo; |
| 6773 | int uid; |
| 6774 | int[] gids; |
| 6775 | |
| 6776 | BasePermission(String _name, String _sourcePackage, int _type) { |
| 6777 | name = _name; |
| 6778 | sourcePackage = _sourcePackage; |
| 6779 | type = _type; |
| 6780 | } |
| 6781 | } |
| 6782 | |
| 6783 | static class PackageSignatures { |
| 6784 | private Signature[] mSignatures; |
| 6785 | |
| 6786 | PackageSignatures(Signature[] sigs) { |
| 6787 | assignSignatures(sigs); |
| 6788 | } |
| 6789 | |
| 6790 | PackageSignatures() { |
| 6791 | } |
| 6792 | |
| 6793 | void writeXml(XmlSerializer serializer, String tagName, |
| 6794 | ArrayList<Signature> pastSignatures) throws IOException { |
| 6795 | if (mSignatures == null) { |
| 6796 | return; |
| 6797 | } |
| 6798 | serializer.startTag(null, tagName); |
| 6799 | serializer.attribute(null, "count", |
| 6800 | Integer.toString(mSignatures.length)); |
| 6801 | for (int i=0; i<mSignatures.length; i++) { |
| 6802 | serializer.startTag(null, "cert"); |
| 6803 | final Signature sig = mSignatures[i]; |
| 6804 | final int sigHash = sig.hashCode(); |
| 6805 | final int numPast = pastSignatures.size(); |
| 6806 | int j; |
| 6807 | for (j=0; j<numPast; j++) { |
| 6808 | Signature pastSig = pastSignatures.get(j); |
| 6809 | if (pastSig.hashCode() == sigHash && pastSig.equals(sig)) { |
| 6810 | serializer.attribute(null, "index", Integer.toString(j)); |
| 6811 | break; |
| 6812 | } |
| 6813 | } |
| 6814 | if (j >= numPast) { |
| 6815 | pastSignatures.add(sig); |
| 6816 | serializer.attribute(null, "index", Integer.toString(numPast)); |
| 6817 | serializer.attribute(null, "key", sig.toCharsString()); |
| 6818 | } |
| 6819 | serializer.endTag(null, "cert"); |
| 6820 | } |
| 6821 | serializer.endTag(null, tagName); |
| 6822 | } |
| 6823 | |
| 6824 | void readXml(XmlPullParser parser, ArrayList<Signature> pastSignatures) |
| 6825 | throws IOException, XmlPullParserException { |
| 6826 | String countStr = parser.getAttributeValue(null, "count"); |
| 6827 | if (countStr == null) { |
| 6828 | reportSettingsProblem(Log.WARN, |
| 6829 | "Error in package manager settings: <signatures> has" |
| 6830 | + " no count at " + parser.getPositionDescription()); |
| 6831 | XmlUtils.skipCurrentTag(parser); |
| 6832 | } |
| 6833 | final int count = Integer.parseInt(countStr); |
| 6834 | mSignatures = new Signature[count]; |
| 6835 | int pos = 0; |
| 6836 | |
| 6837 | int outerDepth = parser.getDepth(); |
| 6838 | int type; |
| 6839 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 6840 | && (type != XmlPullParser.END_TAG |
| 6841 | || parser.getDepth() > outerDepth)) { |
| 6842 | if (type == XmlPullParser.END_TAG |
| 6843 | || type == XmlPullParser.TEXT) { |
| 6844 | continue; |
| 6845 | } |
| 6846 | |
| 6847 | String tagName = parser.getName(); |
| 6848 | if (tagName.equals("cert")) { |
| 6849 | if (pos < count) { |
| 6850 | String index = parser.getAttributeValue(null, "index"); |
| 6851 | if (index != null) { |
| 6852 | try { |
| 6853 | int idx = Integer.parseInt(index); |
| 6854 | String key = parser.getAttributeValue(null, "key"); |
| 6855 | if (key == null) { |
| 6856 | if (idx >= 0 && idx < pastSignatures.size()) { |
| 6857 | Signature sig = pastSignatures.get(idx); |
| 6858 | if (sig != null) { |
| 6859 | mSignatures[pos] = pastSignatures.get(idx); |
| 6860 | pos++; |
| 6861 | } else { |
| 6862 | reportSettingsProblem(Log.WARN, |
| 6863 | "Error in package manager settings: <cert> " |
| 6864 | + "index " + index + " is not defined at " |
| 6865 | + parser.getPositionDescription()); |
| 6866 | } |
| 6867 | } else { |
| 6868 | reportSettingsProblem(Log.WARN, |
| 6869 | "Error in package manager settings: <cert> " |
| 6870 | + "index " + index + " is out of bounds at " |
| 6871 | + parser.getPositionDescription()); |
| 6872 | } |
| 6873 | } else { |
| 6874 | while (pastSignatures.size() <= idx) { |
| 6875 | pastSignatures.add(null); |
| 6876 | } |
| 6877 | Signature sig = new Signature(key); |
| 6878 | pastSignatures.set(idx, sig); |
| 6879 | mSignatures[pos] = sig; |
| 6880 | pos++; |
| 6881 | } |
| 6882 | } catch (NumberFormatException e) { |
| 6883 | reportSettingsProblem(Log.WARN, |
| 6884 | "Error in package manager settings: <cert> " |
| 6885 | + "index " + index + " is not a number at " |
| 6886 | + parser.getPositionDescription()); |
| 6887 | } |
| 6888 | } else { |
| 6889 | reportSettingsProblem(Log.WARN, |
| 6890 | "Error in package manager settings: <cert> has" |
| 6891 | + " no index at " + parser.getPositionDescription()); |
| 6892 | } |
| 6893 | } else { |
| 6894 | reportSettingsProblem(Log.WARN, |
| 6895 | "Error in package manager settings: too " |
| 6896 | + "many <cert> tags, expected " + count |
| 6897 | + " at " + parser.getPositionDescription()); |
| 6898 | } |
| 6899 | } else { |
| 6900 | reportSettingsProblem(Log.WARN, |
| 6901 | "Unknown element under <cert>: " |
| 6902 | + parser.getName()); |
| 6903 | } |
| 6904 | XmlUtils.skipCurrentTag(parser); |
| 6905 | } |
| 6906 | |
| 6907 | if (pos < count) { |
| 6908 | // Should never happen -- there is an error in the written |
| 6909 | // settings -- but if it does we don't want to generate |
| 6910 | // a bad array. |
| 6911 | Signature[] newSigs = new Signature[pos]; |
| 6912 | System.arraycopy(mSignatures, 0, newSigs, 0, pos); |
| 6913 | mSignatures = newSigs; |
| 6914 | } |
| 6915 | } |
| 6916 | |
| 6917 | /** |
| 6918 | * If any of the given 'sigs' is contained in the existing signatures, |
| 6919 | * then completely replace the current signatures with the ones in |
| 6920 | * 'sigs'. This is used for updating an existing package to a newly |
| 6921 | * installed version. |
| 6922 | */ |
| 6923 | boolean updateSignatures(Signature[] sigs, boolean update) { |
| 6924 | if (mSignatures == null) { |
| 6925 | if (update) { |
| 6926 | assignSignatures(sigs); |
| 6927 | } |
| 6928 | return true; |
| 6929 | } |
| 6930 | if (sigs == null) { |
| 6931 | return false; |
| 6932 | } |
| 6933 | |
| 6934 | for (int i=0; i<sigs.length; i++) { |
| 6935 | Signature sig = sigs[i]; |
| 6936 | for (int j=0; j<mSignatures.length; j++) { |
| 6937 | if (mSignatures[j].equals(sig)) { |
| 6938 | if (update) { |
| 6939 | assignSignatures(sigs); |
| 6940 | } |
| 6941 | return true; |
| 6942 | } |
| 6943 | } |
| 6944 | } |
| 6945 | return false; |
| 6946 | } |
| 6947 | |
| 6948 | /** |
| 6949 | * If any of the given 'sigs' is contained in the existing signatures, |
| 6950 | * then add in any new signatures found in 'sigs'. This is used for |
| 6951 | * including a new package into an existing shared user id. |
| 6952 | */ |
| 6953 | boolean mergeSignatures(Signature[] sigs, boolean update) { |
| 6954 | if (mSignatures == null) { |
| 6955 | if (update) { |
| 6956 | assignSignatures(sigs); |
| 6957 | } |
| 6958 | return true; |
| 6959 | } |
| 6960 | if (sigs == null) { |
| 6961 | return false; |
| 6962 | } |
| 6963 | |
| 6964 | Signature[] added = null; |
| 6965 | int addedCount = 0; |
| 6966 | boolean haveMatch = false; |
| 6967 | for (int i=0; i<sigs.length; i++) { |
| 6968 | Signature sig = sigs[i]; |
| 6969 | boolean found = false; |
| 6970 | for (int j=0; j<mSignatures.length; j++) { |
| 6971 | if (mSignatures[j].equals(sig)) { |
| 6972 | found = true; |
| 6973 | haveMatch = true; |
| 6974 | break; |
| 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | if (!found) { |
| 6979 | if (added == null) { |
| 6980 | added = new Signature[sigs.length]; |
| 6981 | } |
| 6982 | added[i] = sig; |
| 6983 | addedCount++; |
| 6984 | } |
| 6985 | } |
| 6986 | |
| 6987 | if (!haveMatch) { |
| 6988 | // Nothing matched -- reject the new signatures. |
| 6989 | return false; |
| 6990 | } |
| 6991 | if (added == null) { |
| 6992 | // Completely matched -- nothing else to do. |
| 6993 | return true; |
| 6994 | } |
| 6995 | |
| 6996 | // Add additional signatures in. |
| 6997 | if (update) { |
| 6998 | Signature[] total = new Signature[addedCount+mSignatures.length]; |
| 6999 | System.arraycopy(mSignatures, 0, total, 0, mSignatures.length); |
| 7000 | int j = mSignatures.length; |
| 7001 | for (int i=0; i<added.length; i++) { |
| 7002 | if (added[i] != null) { |
| 7003 | total[j] = added[i]; |
| 7004 | j++; |
| 7005 | } |
| 7006 | } |
| 7007 | mSignatures = total; |
| 7008 | } |
| 7009 | return true; |
| 7010 | } |
| 7011 | |
| 7012 | private void assignSignatures(Signature[] sigs) { |
| 7013 | if (sigs == null) { |
| 7014 | mSignatures = null; |
| 7015 | return; |
| 7016 | } |
| 7017 | mSignatures = new Signature[sigs.length]; |
| 7018 | for (int i=0; i<sigs.length; i++) { |
| 7019 | mSignatures[i] = sigs[i]; |
| 7020 | } |
| 7021 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7022 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7023 | @Override |
| 7024 | public String toString() { |
| 7025 | StringBuffer buf = new StringBuffer(128); |
| 7026 | buf.append("PackageSignatures{"); |
| 7027 | buf.append(Integer.toHexString(System.identityHashCode(this))); |
| 7028 | buf.append(" ["); |
| 7029 | if (mSignatures != null) { |
| 7030 | for (int i=0; i<mSignatures.length; i++) { |
| 7031 | if (i > 0) buf.append(", "); |
| 7032 | buf.append(Integer.toHexString( |
| 7033 | System.identityHashCode(mSignatures[i]))); |
| 7034 | } |
| 7035 | } |
| 7036 | buf.append("]}"); |
| 7037 | return buf.toString(); |
| 7038 | } |
| 7039 | } |
| 7040 | |
| 7041 | static class PreferredActivity extends IntentFilter { |
| 7042 | final int mMatch; |
| 7043 | final String[] mSetPackages; |
| 7044 | final String[] mSetClasses; |
| 7045 | final String[] mSetComponents; |
| 7046 | final ComponentName mActivity; |
| 7047 | final String mShortActivity; |
| 7048 | String mParseError; |
| 7049 | |
| 7050 | PreferredActivity(IntentFilter filter, int match, ComponentName[] set, |
| 7051 | ComponentName activity) { |
| 7052 | super(filter); |
| 7053 | mMatch = match&IntentFilter.MATCH_CATEGORY_MASK; |
| 7054 | mActivity = activity; |
| 7055 | mShortActivity = activity.flattenToShortString(); |
| 7056 | mParseError = null; |
| 7057 | if (set != null) { |
| 7058 | final int N = set.length; |
| 7059 | String[] myPackages = new String[N]; |
| 7060 | String[] myClasses = new String[N]; |
| 7061 | String[] myComponents = new String[N]; |
| 7062 | for (int i=0; i<N; i++) { |
| 7063 | ComponentName cn = set[i]; |
| 7064 | if (cn == null) { |
| 7065 | mSetPackages = null; |
| 7066 | mSetClasses = null; |
| 7067 | mSetComponents = null; |
| 7068 | return; |
| 7069 | } |
| 7070 | myPackages[i] = cn.getPackageName().intern(); |
| 7071 | myClasses[i] = cn.getClassName().intern(); |
| 7072 | myComponents[i] = cn.flattenToShortString().intern(); |
| 7073 | } |
| 7074 | mSetPackages = myPackages; |
| 7075 | mSetClasses = myClasses; |
| 7076 | mSetComponents = myComponents; |
| 7077 | } else { |
| 7078 | mSetPackages = null; |
| 7079 | mSetClasses = null; |
| 7080 | mSetComponents = null; |
| 7081 | } |
| 7082 | } |
| 7083 | |
| 7084 | PreferredActivity(XmlPullParser parser) throws XmlPullParserException, |
| 7085 | IOException { |
| 7086 | mShortActivity = parser.getAttributeValue(null, "name"); |
| 7087 | mActivity = ComponentName.unflattenFromString(mShortActivity); |
| 7088 | if (mActivity == null) { |
| 7089 | mParseError = "Bad activity name " + mShortActivity; |
| 7090 | } |
| 7091 | String matchStr = parser.getAttributeValue(null, "match"); |
| 7092 | mMatch = matchStr != null ? Integer.parseInt(matchStr, 16) : 0; |
| 7093 | String setCountStr = parser.getAttributeValue(null, "set"); |
| 7094 | int setCount = setCountStr != null ? Integer.parseInt(setCountStr) : 0; |
| 7095 | |
| 7096 | String[] myPackages = setCount > 0 ? new String[setCount] : null; |
| 7097 | String[] myClasses = setCount > 0 ? new String[setCount] : null; |
| 7098 | String[] myComponents = setCount > 0 ? new String[setCount] : null; |
| 7099 | |
| 7100 | int setPos = 0; |
| 7101 | |
| 7102 | int outerDepth = parser.getDepth(); |
| 7103 | int type; |
| 7104 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 7105 | && (type != XmlPullParser.END_TAG |
| 7106 | || parser.getDepth() > outerDepth)) { |
| 7107 | if (type == XmlPullParser.END_TAG |
| 7108 | || type == XmlPullParser.TEXT) { |
| 7109 | continue; |
| 7110 | } |
| 7111 | |
| 7112 | String tagName = parser.getName(); |
| 7113 | //Log.i(TAG, "Parse outerDepth=" + outerDepth + " depth=" |
| 7114 | // + parser.getDepth() + " tag=" + tagName); |
| 7115 | if (tagName.equals("set")) { |
| 7116 | String name = parser.getAttributeValue(null, "name"); |
| 7117 | if (name == null) { |
| 7118 | if (mParseError == null) { |
| 7119 | mParseError = "No name in set tag in preferred activity " |
| 7120 | + mShortActivity; |
| 7121 | } |
| 7122 | } else if (setPos >= setCount) { |
| 7123 | if (mParseError == null) { |
| 7124 | mParseError = "Too many set tags in preferred activity " |
| 7125 | + mShortActivity; |
| 7126 | } |
| 7127 | } else { |
| 7128 | ComponentName cn = ComponentName.unflattenFromString(name); |
| 7129 | if (cn == null) { |
| 7130 | if (mParseError == null) { |
| 7131 | mParseError = "Bad set name " + name + " in preferred activity " |
| 7132 | + mShortActivity; |
| 7133 | } |
| 7134 | } else { |
| 7135 | myPackages[setPos] = cn.getPackageName(); |
| 7136 | myClasses[setPos] = cn.getClassName(); |
| 7137 | myComponents[setPos] = name; |
| 7138 | setPos++; |
| 7139 | } |
| 7140 | } |
| 7141 | XmlUtils.skipCurrentTag(parser); |
| 7142 | } else if (tagName.equals("filter")) { |
| 7143 | //Log.i(TAG, "Starting to parse filter..."); |
| 7144 | readFromXml(parser); |
| 7145 | //Log.i(TAG, "Finished filter: outerDepth=" + outerDepth + " depth=" |
| 7146 | // + parser.getDepth() + " tag=" + parser.getName()); |
| 7147 | } else { |
| 7148 | reportSettingsProblem(Log.WARN, |
| 7149 | "Unknown element under <preferred-activities>: " |
| 7150 | + parser.getName()); |
| 7151 | XmlUtils.skipCurrentTag(parser); |
| 7152 | } |
| 7153 | } |
| 7154 | |
| 7155 | if (setPos != setCount) { |
| 7156 | if (mParseError == null) { |
| 7157 | mParseError = "Not enough set tags (expected " + setCount |
| 7158 | + " but found " + setPos + ") in " + mShortActivity; |
| 7159 | } |
| 7160 | } |
| 7161 | |
| 7162 | mSetPackages = myPackages; |
| 7163 | mSetClasses = myClasses; |
| 7164 | mSetComponents = myComponents; |
| 7165 | } |
| 7166 | |
| 7167 | public void writeToXml(XmlSerializer serializer) throws IOException { |
| 7168 | final int NS = mSetClasses != null ? mSetClasses.length : 0; |
| 7169 | serializer.attribute(null, "name", mShortActivity); |
| 7170 | serializer.attribute(null, "match", Integer.toHexString(mMatch)); |
| 7171 | serializer.attribute(null, "set", Integer.toString(NS)); |
| 7172 | for (int s=0; s<NS; s++) { |
| 7173 | serializer.startTag(null, "set"); |
| 7174 | serializer.attribute(null, "name", mSetComponents[s]); |
| 7175 | serializer.endTag(null, "set"); |
| 7176 | } |
| 7177 | serializer.startTag(null, "filter"); |
| 7178 | super.writeToXml(serializer); |
| 7179 | serializer.endTag(null, "filter"); |
| 7180 | } |
| 7181 | |
| 7182 | boolean sameSet(List<ResolveInfo> query, int priority) { |
| 7183 | if (mSetPackages == null) return false; |
| 7184 | final int NQ = query.size(); |
| 7185 | final int NS = mSetPackages.length; |
| 7186 | int numMatch = 0; |
| 7187 | for (int i=0; i<NQ; i++) { |
| 7188 | ResolveInfo ri = query.get(i); |
| 7189 | if (ri.priority != priority) continue; |
| 7190 | ActivityInfo ai = ri.activityInfo; |
| 7191 | boolean good = false; |
| 7192 | for (int j=0; j<NS; j++) { |
| 7193 | if (mSetPackages[j].equals(ai.packageName) |
| 7194 | && mSetClasses[j].equals(ai.name)) { |
| 7195 | numMatch++; |
| 7196 | good = true; |
| 7197 | break; |
| 7198 | } |
| 7199 | } |
| 7200 | if (!good) return false; |
| 7201 | } |
| 7202 | return numMatch == NS; |
| 7203 | } |
| 7204 | } |
| 7205 | |
| 7206 | static class GrantedPermissions { |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 7207 | int pkgFlags; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7208 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7209 | HashSet<String> grantedPermissions = new HashSet<String>(); |
| 7210 | int[] gids; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7211 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7212 | HashSet<String> loadedPermissions = new HashSet<String>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7213 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7214 | GrantedPermissions(int pkgFlags) { |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7215 | setFlags(pkgFlags); |
| 7216 | } |
| 7217 | |
| 7218 | void setFlags(int pkgFlags) { |
| Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 7219 | this.pkgFlags = (pkgFlags & ApplicationInfo.FLAG_SYSTEM) | |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7220 | (pkgFlags & ApplicationInfo.FLAG_FORWARD_LOCK) | |
| Suchi Amalapurapu | 6069beb | 2010-03-10 09:46:49 -0800 | [diff] [blame] | 7221 | (pkgFlags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) | |
| Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 7222 | (pkgFlags & ApplicationInfo.FLAG_NEVER_ENCRYPT); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7223 | } |
| 7224 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7225 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7226 | /** |
| 7227 | * Settings base class for pending and resolved classes. |
| 7228 | */ |
| 7229 | static class PackageSettingBase extends GrantedPermissions { |
| 7230 | final String name; |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7231 | final String realName; |
| Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 7232 | File codePath; |
| 7233 | String codePathString; |
| 7234 | File resourcePath; |
| 7235 | String resourcePathString; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7236 | private long timeStamp; |
| 7237 | private String timeStampString = "0"; |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7238 | int versionCode; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7239 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7240 | boolean uidError; |
| 7241 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7242 | PackageSignatures signatures = new PackageSignatures(); |
| 7243 | |
| 7244 | boolean permissionsFixed; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7245 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7246 | /* Explicitly disabled components */ |
| 7247 | HashSet<String> disabledComponents = new HashSet<String>(0); |
| 7248 | /* Explicitly enabled components */ |
| 7249 | HashSet<String> enabledComponents = new HashSet<String>(0); |
| 7250 | int enabled = COMPONENT_ENABLED_STATE_DEFAULT; |
| 7251 | int installStatus = PKG_INSTALL_COMPLETE; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7252 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7253 | PackageSettingBase origPackage; |
| 7254 | |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7255 | /* package name of the app that installed this package */ |
| 7256 | String installerPackageName; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7257 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7258 | PackageSettingBase(String name, String realName, File codePath, File resourcePath, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7259 | int pVersionCode, int pkgFlags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7260 | super(pkgFlags); |
| 7261 | this.name = name; |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7262 | this.realName = realName; |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7263 | init(codePath, resourcePath, pVersionCode); |
| 7264 | } |
| 7265 | |
| 7266 | void init(File codePath, File resourcePath, int pVersionCode) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7267 | this.codePath = codePath; |
| 7268 | this.codePathString = codePath.toString(); |
| 7269 | this.resourcePath = resourcePath; |
| 7270 | this.resourcePathString = resourcePath.toString(); |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7271 | this.versionCode = pVersionCode; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7272 | } |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7273 | |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7274 | public void setInstallerPackageName(String packageName) { |
| 7275 | installerPackageName = packageName; |
| 7276 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7277 | |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7278 | String getInstallerPackageName() { |
| 7279 | return installerPackageName; |
| 7280 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7281 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7282 | public void setInstallStatus(int newStatus) { |
| 7283 | installStatus = newStatus; |
| 7284 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7285 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7286 | public int getInstallStatus() { |
| 7287 | return installStatus; |
| 7288 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7289 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7290 | public void setTimeStamp(long newStamp) { |
| 7291 | if (newStamp != timeStamp) { |
| 7292 | timeStamp = newStamp; |
| 7293 | timeStampString = Long.toString(newStamp); |
| 7294 | } |
| 7295 | } |
| 7296 | |
| 7297 | public void setTimeStamp(long newStamp, String newStampStr) { |
| 7298 | timeStamp = newStamp; |
| 7299 | timeStampString = newStampStr; |
| 7300 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7301 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7302 | public long getTimeStamp() { |
| 7303 | return timeStamp; |
| 7304 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7305 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7306 | public String getTimeStampStr() { |
| 7307 | return timeStampString; |
| 7308 | } |
| 7309 | |
| 7310 | public void copyFrom(PackageSettingBase base) { |
| 7311 | grantedPermissions = base.grantedPermissions; |
| 7312 | gids = base.gids; |
| 7313 | loadedPermissions = base.loadedPermissions; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7314 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7315 | timeStamp = base.timeStamp; |
| 7316 | timeStampString = base.timeStampString; |
| 7317 | signatures = base.signatures; |
| 7318 | permissionsFixed = base.permissionsFixed; |
| 7319 | disabledComponents = base.disabledComponents; |
| 7320 | enabledComponents = base.enabledComponents; |
| 7321 | enabled = base.enabled; |
| 7322 | installStatus = base.installStatus; |
| 7323 | } |
| 7324 | |
| 7325 | void enableComponentLP(String componentClassName) { |
| 7326 | disabledComponents.remove(componentClassName); |
| 7327 | enabledComponents.add(componentClassName); |
| 7328 | } |
| 7329 | |
| 7330 | void disableComponentLP(String componentClassName) { |
| 7331 | enabledComponents.remove(componentClassName); |
| 7332 | disabledComponents.add(componentClassName); |
| 7333 | } |
| 7334 | |
| 7335 | void restoreComponentLP(String componentClassName) { |
| 7336 | enabledComponents.remove(componentClassName); |
| 7337 | disabledComponents.remove(componentClassName); |
| 7338 | } |
| 7339 | |
| 7340 | int currentEnabledStateLP(String componentName) { |
| 7341 | if (enabledComponents.contains(componentName)) { |
| 7342 | return COMPONENT_ENABLED_STATE_ENABLED; |
| 7343 | } else if (disabledComponents.contains(componentName)) { |
| 7344 | return COMPONENT_ENABLED_STATE_DISABLED; |
| 7345 | } else { |
| 7346 | return COMPONENT_ENABLED_STATE_DEFAULT; |
| 7347 | } |
| 7348 | } |
| 7349 | } |
| 7350 | |
| 7351 | /** |
| 7352 | * Settings data for a particular package we know about. |
| 7353 | */ |
| 7354 | static final class PackageSetting extends PackageSettingBase { |
| 7355 | int userId; |
| 7356 | PackageParser.Package pkg; |
| 7357 | SharedUserSetting sharedUser; |
| 7358 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7359 | PackageSetting(String name, String realName, File codePath, File resourcePath, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7360 | int pVersionCode, int pkgFlags) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7361 | super(name, realName, codePath, resourcePath, pVersionCode, pkgFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7362 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7363 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7364 | @Override |
| 7365 | public String toString() { |
| 7366 | return "PackageSetting{" |
| 7367 | + Integer.toHexString(System.identityHashCode(this)) |
| 7368 | + " " + name + "/" + userId + "}"; |
| 7369 | } |
| 7370 | } |
| 7371 | |
| 7372 | /** |
| 7373 | * Settings data for a particular shared user ID we know about. |
| 7374 | */ |
| 7375 | static final class SharedUserSetting extends GrantedPermissions { |
| 7376 | final String name; |
| 7377 | int userId; |
| 7378 | final HashSet<PackageSetting> packages = new HashSet<PackageSetting>(); |
| 7379 | final PackageSignatures signatures = new PackageSignatures(); |
| 7380 | |
| 7381 | SharedUserSetting(String _name, int _pkgFlags) { |
| 7382 | super(_pkgFlags); |
| 7383 | name = _name; |
| 7384 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7385 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7386 | @Override |
| 7387 | public String toString() { |
| 7388 | return "SharedUserSetting{" |
| 7389 | + Integer.toHexString(System.identityHashCode(this)) |
| 7390 | + " " + name + "/" + userId + "}"; |
| 7391 | } |
| 7392 | } |
| 7393 | |
| 7394 | /** |
| 7395 | * Holds information about dynamic settings. |
| 7396 | */ |
| 7397 | private static final class Settings { |
| 7398 | private final File mSettingsFilename; |
| 7399 | private final File mBackupSettingsFilename; |
| David 'Digit' Turner | add1376 | 2010-02-03 17:34:58 -0800 | [diff] [blame] | 7400 | private final File mPackageListFilename; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7401 | private final HashMap<String, PackageSetting> mPackages = |
| 7402 | new HashMap<String, PackageSetting>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7403 | // List of replaced system applications |
| 7404 | final HashMap<String, PackageSetting> mDisabledSysPackages = |
| 7405 | new HashMap<String, PackageSetting>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7406 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7407 | // The user's preferred activities associated with particular intent |
| 7408 | // filters. |
| 7409 | private final IntentResolver<PreferredActivity, PreferredActivity> mPreferredActivities = |
| 7410 | new IntentResolver<PreferredActivity, PreferredActivity>() { |
| 7411 | @Override |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 7412 | protected void dumpFilter(PrintWriter out, String prefix, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7413 | PreferredActivity filter) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 7414 | out.print(prefix); out.print( |
| 7415 | Integer.toHexString(System.identityHashCode(filter))); |
| 7416 | out.print(' '); |
| 7417 | out.print(filter.mActivity.flattenToShortString()); |
| 7418 | out.print(" match=0x"); |
| 7419 | out.println( Integer.toHexString(filter.mMatch)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7420 | if (filter.mSetComponents != null) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 7421 | out.print(prefix); out.println(" Selected from:"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7422 | for (int i=0; i<filter.mSetComponents.length; i++) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 7423 | out.print(prefix); out.print(" "); |
| 7424 | out.println(filter.mSetComponents[i]); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7425 | } |
| 7426 | } |
| 7427 | } |
| 7428 | }; |
| 7429 | private final HashMap<String, SharedUserSetting> mSharedUsers = |
| 7430 | new HashMap<String, SharedUserSetting>(); |
| 7431 | private final ArrayList<Object> mUserIds = new ArrayList<Object>(); |
| 7432 | private final SparseArray<Object> mOtherUserIds = |
| 7433 | new SparseArray<Object>(); |
| 7434 | |
| 7435 | // For reading/writing settings file. |
| 7436 | private final ArrayList<Signature> mPastSignatures = |
| 7437 | new ArrayList<Signature>(); |
| 7438 | |
| 7439 | // Mapping from permission names to info about them. |
| 7440 | final HashMap<String, BasePermission> mPermissions = |
| 7441 | new HashMap<String, BasePermission>(); |
| 7442 | |
| 7443 | // Mapping from permission tree names to info about them. |
| 7444 | final HashMap<String, BasePermission> mPermissionTrees = |
| 7445 | new HashMap<String, BasePermission>(); |
| 7446 | |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 7447 | // Packages that have been uninstalled and still need their external |
| 7448 | // storage data deleted. |
| 7449 | final ArrayList<String> mPackagesToBeCleaned = new ArrayList<String>(); |
| 7450 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7451 | // Packages that have been renamed since they were first installed. |
| 7452 | // Keys are the new names of the packages, values are the original |
| 7453 | // names. The packages appear everwhere else under their original |
| 7454 | // names. |
| 7455 | final HashMap<String, String> mRenamedPackages = new HashMap<String, String>(); |
| 7456 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7457 | private final StringBuilder mReadMessages = new StringBuilder(); |
| 7458 | |
| 7459 | private static final class PendingPackage extends PackageSettingBase { |
| 7460 | final int sharedId; |
| 7461 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7462 | PendingPackage(String name, String realName, File codePath, File resourcePath, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7463 | int sharedId, int pVersionCode, int pkgFlags) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7464 | super(name, realName, codePath, resourcePath, pVersionCode, pkgFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7465 | this.sharedId = sharedId; |
| 7466 | } |
| 7467 | } |
| 7468 | private final ArrayList<PendingPackage> mPendingPackages |
| 7469 | = new ArrayList<PendingPackage>(); |
| 7470 | |
| 7471 | Settings() { |
| 7472 | File dataDir = Environment.getDataDirectory(); |
| 7473 | File systemDir = new File(dataDir, "system"); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 7474 | // TODO(oam): This secure dir creation needs to be moved somewhere else (later) |
| 7475 | File systemSecureDir = new File(dataDir, "secure/system"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7476 | systemDir.mkdirs(); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 7477 | systemSecureDir.mkdirs(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7478 | FileUtils.setPermissions(systemDir.toString(), |
| 7479 | FileUtils.S_IRWXU|FileUtils.S_IRWXG |
| 7480 | |FileUtils.S_IROTH|FileUtils.S_IXOTH, |
| 7481 | -1, -1); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 7482 | FileUtils.setPermissions(systemSecureDir.toString(), |
| 7483 | FileUtils.S_IRWXU|FileUtils.S_IRWXG |
| 7484 | |FileUtils.S_IROTH|FileUtils.S_IXOTH, |
| 7485 | -1, -1); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7486 | mSettingsFilename = new File(systemDir, "packages.xml"); |
| 7487 | mBackupSettingsFilename = new File(systemDir, "packages-backup.xml"); |
| David 'Digit' Turner | add1376 | 2010-02-03 17:34:58 -0800 | [diff] [blame] | 7488 | mPackageListFilename = new File(systemDir, "packages.list"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7489 | } |
| 7490 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7491 | PackageSetting getPackageLP(PackageParser.Package pkg, PackageSetting origPackage, |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7492 | String realName, SharedUserSetting sharedUser, File codePath, File resourcePath, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7493 | int pkgFlags, boolean create, boolean add) { |
| 7494 | final String name = pkg.packageName; |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7495 | PackageSetting p = getPackageLP(name, origPackage, realName, sharedUser, codePath, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7496 | resourcePath, pkg.mVersionCode, pkgFlags, create, add); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7497 | return p; |
| 7498 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7499 | |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7500 | PackageSetting peekPackageLP(String name) { |
| 7501 | return mPackages.get(name); |
| 7502 | /* |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7503 | PackageSetting p = mPackages.get(name); |
| 7504 | if (p != null && p.codePath.getPath().equals(codePath)) { |
| 7505 | return p; |
| 7506 | } |
| 7507 | return null; |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7508 | */ |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7509 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7510 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7511 | void setInstallStatus(String pkgName, int status) { |
| 7512 | PackageSetting p = mPackages.get(pkgName); |
| 7513 | if(p != null) { |
| 7514 | if(p.getInstallStatus() != status) { |
| 7515 | p.setInstallStatus(status); |
| 7516 | } |
| 7517 | } |
| 7518 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7519 | |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7520 | void setInstallerPackageName(String pkgName, |
| 7521 | String installerPkgName) { |
| 7522 | PackageSetting p = mPackages.get(pkgName); |
| 7523 | if(p != null) { |
| 7524 | p.setInstallerPackageName(installerPkgName); |
| 7525 | } |
| 7526 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7527 | |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7528 | String getInstallerPackageName(String pkgName) { |
| 7529 | PackageSetting p = mPackages.get(pkgName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7530 | return (p == null) ? null : p.getInstallerPackageName(); |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7531 | } |
| 7532 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7533 | int getInstallStatus(String pkgName) { |
| 7534 | PackageSetting p = mPackages.get(pkgName); |
| 7535 | if(p != null) { |
| 7536 | return p.getInstallStatus(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7537 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7538 | return -1; |
| 7539 | } |
| 7540 | |
| 7541 | SharedUserSetting getSharedUserLP(String name, |
| 7542 | int pkgFlags, boolean create) { |
| 7543 | SharedUserSetting s = mSharedUsers.get(name); |
| 7544 | if (s == null) { |
| 7545 | if (!create) { |
| 7546 | return null; |
| 7547 | } |
| 7548 | s = new SharedUserSetting(name, pkgFlags); |
| 7549 | if (MULTIPLE_APPLICATION_UIDS) { |
| 7550 | s.userId = newUserIdLP(s); |
| 7551 | } else { |
| 7552 | s.userId = FIRST_APPLICATION_UID; |
| 7553 | } |
| 7554 | Log.i(TAG, "New shared user " + name + ": id=" + s.userId); |
| 7555 | // < 0 means we couldn't assign a userid; fall out and return |
| 7556 | // s, which is currently null |
| 7557 | if (s.userId >= 0) { |
| 7558 | mSharedUsers.put(name, s); |
| 7559 | } |
| 7560 | } |
| 7561 | |
| 7562 | return s; |
| 7563 | } |
| 7564 | |
| 7565 | int disableSystemPackageLP(String name) { |
| 7566 | PackageSetting p = mPackages.get(name); |
| 7567 | if(p == null) { |
| 7568 | Log.w(TAG, "Package:"+name+" is not an installed package"); |
| 7569 | return -1; |
| 7570 | } |
| 7571 | PackageSetting dp = mDisabledSysPackages.get(name); |
| 7572 | // always make sure the system package code and resource paths dont change |
| 7573 | if(dp == null) { |
| 7574 | if((p.pkg != null) && (p.pkg.applicationInfo != null)) { |
| 7575 | p.pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
| 7576 | } |
| 7577 | mDisabledSysPackages.put(name, p); |
| 7578 | } |
| 7579 | return removePackageLP(name); |
| 7580 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7581 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7582 | PackageSetting enableSystemPackageLP(String name) { |
| 7583 | PackageSetting p = mDisabledSysPackages.get(name); |
| 7584 | if(p == null) { |
| 7585 | Log.w(TAG, "Package:"+name+" is not disabled"); |
| 7586 | return null; |
| 7587 | } |
| 7588 | // Reset flag in ApplicationInfo object |
| 7589 | if((p.pkg != null) && (p.pkg.applicationInfo != null)) { |
| 7590 | p.pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
| 7591 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7592 | PackageSetting ret = addPackageLP(name, p.realName, p.codePath, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7593 | p.resourcePath, p.userId, p.versionCode, p.pkgFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7594 | mDisabledSysPackages.remove(name); |
| 7595 | return ret; |
| 7596 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7597 | |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7598 | PackageSetting addPackageLP(String name, String realName, File codePath, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7599 | File resourcePath, int uid, int vc, int pkgFlags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7600 | PackageSetting p = mPackages.get(name); |
| 7601 | if (p != null) { |
| 7602 | if (p.userId == uid) { |
| 7603 | return p; |
| 7604 | } |
| 7605 | reportSettingsProblem(Log.ERROR, |
| 7606 | "Adding duplicate package, keeping first: " + name); |
| 7607 | return null; |
| 7608 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7609 | p = new PackageSetting(name, realName, codePath, resourcePath, vc, pkgFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7610 | p.userId = uid; |
| 7611 | if (addUserIdLP(uid, p, name)) { |
| 7612 | mPackages.put(name, p); |
| 7613 | return p; |
| 7614 | } |
| 7615 | return null; |
| 7616 | } |
| 7617 | |
| 7618 | SharedUserSetting addSharedUserLP(String name, int uid, int pkgFlags) { |
| 7619 | SharedUserSetting s = mSharedUsers.get(name); |
| 7620 | if (s != null) { |
| 7621 | if (s.userId == uid) { |
| 7622 | return s; |
| 7623 | } |
| 7624 | reportSettingsProblem(Log.ERROR, |
| 7625 | "Adding duplicate shared user, keeping first: " + name); |
| 7626 | return null; |
| 7627 | } |
| 7628 | s = new SharedUserSetting(name, pkgFlags); |
| 7629 | s.userId = uid; |
| 7630 | if (addUserIdLP(uid, s, name)) { |
| 7631 | mSharedUsers.put(name, s); |
| 7632 | return s; |
| 7633 | } |
| 7634 | return null; |
| 7635 | } |
| 7636 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7637 | // Transfer ownership of permissions from one package to another. |
| 7638 | private void transferPermissions(String origPkg, String newPkg) { |
| 7639 | // Transfer ownership of permissions to the new package. |
| 7640 | for (int i=0; i<2; i++) { |
| 7641 | HashMap<String, BasePermission> permissions = |
| 7642 | i == 0 ? mPermissionTrees : mPermissions; |
| 7643 | for (BasePermission bp : permissions.values()) { |
| 7644 | if (origPkg.equals(bp.sourcePackage)) { |
| 7645 | if (DEBUG_UPGRADE) Log.v(TAG, |
| 7646 | "Moving permission " + bp.name |
| 7647 | + " from pkg " + bp.sourcePackage |
| 7648 | + " to " + newPkg); |
| 7649 | bp.sourcePackage = newPkg; |
| 7650 | bp.perm = null; |
| 7651 | if (bp.pendingInfo != null) { |
| 7652 | bp.sourcePackage = newPkg; |
| 7653 | } |
| 7654 | bp.uid = 0; |
| 7655 | bp.gids = null; |
| 7656 | } |
| 7657 | } |
| 7658 | } |
| 7659 | } |
| 7660 | |
| 7661 | private PackageSetting getPackageLP(String name, PackageSetting origPackage, |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7662 | String realName, SharedUserSetting sharedUser, File codePath, File resourcePath, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7663 | int vc, int pkgFlags, boolean create, boolean add) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7664 | PackageSetting p = mPackages.get(name); |
| 7665 | if (p != null) { |
| 7666 | if (!p.codePath.equals(codePath)) { |
| 7667 | // Check to see if its a disabled system app |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7668 | if((p != null) && ((p.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0)) { |
| Suchi Amalapurapu | b24a967 | 2009-07-01 14:04:43 -0700 | [diff] [blame] | 7669 | // This is an updated system app with versions in both system |
| 7670 | // and data partition. Just let the most recent version |
| 7671 | // take precedence. |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7672 | Log.w(TAG, "Trying to update system app code path from " + |
| 7673 | p.codePathString + " to " + codePath.toString()); |
| Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 7674 | } else { |
| Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 7675 | // Just a change in the code path is not an issue, but |
| 7676 | // let's log a message about it. |
| 7677 | Log.i(TAG, "Package " + name + " codePath changed from " + p.codePath |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 7678 | + " to " + codePath + "; Retaining data and using new"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7679 | } |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 7680 | } |
| 7681 | if (p.sharedUser != sharedUser) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7682 | reportSettingsProblem(Log.WARN, |
| 7683 | "Package " + name + " shared user changed from " |
| 7684 | + (p.sharedUser != null ? p.sharedUser.name : "<nothing>") |
| 7685 | + " to " |
| 7686 | + (sharedUser != null ? sharedUser.name : "<nothing>") |
| 7687 | + "; replacing with new"); |
| 7688 | p = null; |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 7689 | } else { |
| 7690 | if ((pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7691 | // If what we are scanning is a system package, then |
| 7692 | // make it so, regardless of whether it was previously |
| 7693 | // installed only in the data partition. |
| 7694 | p.pkgFlags |= ApplicationInfo.FLAG_SYSTEM; |
| 7695 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7696 | } |
| 7697 | } |
| 7698 | if (p == null) { |
| 7699 | // Create a new PackageSettings entry. this can end up here because |
| 7700 | // of code path mismatch or user id mismatch of an updated system partition |
| 7701 | if (!create) { |
| 7702 | return null; |
| 7703 | } |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7704 | if (origPackage != null) { |
| 7705 | // We are consuming the data from an existing package. |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7706 | p = new PackageSetting(origPackage.name, name, codePath, |
| 7707 | resourcePath, vc, pkgFlags); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7708 | if (DEBUG_UPGRADE) Log.v(TAG, "Package " + name |
| 7709 | + " is adopting original package " + origPackage.name); |
| 7710 | p.copyFrom(origPackage); |
| 7711 | p.sharedUser = origPackage.sharedUser; |
| 7712 | p.userId = origPackage.userId; |
| 7713 | p.origPackage = origPackage; |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7714 | mRenamedPackages.put(name, origPackage.name); |
| 7715 | name = origPackage.name; |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7716 | // Update new package state. |
| 7717 | p.setTimeStamp(codePath.lastModified()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7718 | } else { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7719 | p = new PackageSetting(name, realName, codePath, resourcePath, vc, pkgFlags); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 7720 | p.setTimeStamp(codePath.lastModified()); |
| 7721 | p.sharedUser = sharedUser; |
| 7722 | if (sharedUser != null) { |
| 7723 | p.userId = sharedUser.userId; |
| 7724 | } else if (MULTIPLE_APPLICATION_UIDS) { |
| 7725 | // Clone the setting here for disabled system packages |
| 7726 | PackageSetting dis = mDisabledSysPackages.get(name); |
| 7727 | if (dis != null) { |
| 7728 | // For disabled packages a new setting is created |
| 7729 | // from the existing user id. This still has to be |
| 7730 | // added to list of user id's |
| 7731 | // Copy signatures from previous setting |
| 7732 | if (dis.signatures.mSignatures != null) { |
| 7733 | p.signatures.mSignatures = dis.signatures.mSignatures.clone(); |
| 7734 | } |
| 7735 | p.userId = dis.userId; |
| 7736 | // Clone permissions |
| 7737 | p.grantedPermissions = new HashSet<String>(dis.grantedPermissions); |
| 7738 | p.loadedPermissions = new HashSet<String>(dis.loadedPermissions); |
| 7739 | // Clone component info |
| 7740 | p.disabledComponents = new HashSet<String>(dis.disabledComponents); |
| 7741 | p.enabledComponents = new HashSet<String>(dis.enabledComponents); |
| 7742 | // Add new setting to list of user ids |
| 7743 | addUserIdLP(p.userId, p, name); |
| 7744 | } else { |
| 7745 | // Assign new user id |
| 7746 | p.userId = newUserIdLP(p); |
| 7747 | } |
| 7748 | } else { |
| 7749 | p.userId = FIRST_APPLICATION_UID; |
| 7750 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7751 | } |
| 7752 | if (p.userId < 0) { |
| 7753 | reportSettingsProblem(Log.WARN, |
| 7754 | "Package " + name + " could not be assigned a valid uid"); |
| 7755 | return null; |
| 7756 | } |
| 7757 | if (add) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7758 | // Finish adding new package by adding it and updating shared |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7759 | // user preferences |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7760 | addPackageSettingLP(p, name, sharedUser); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7761 | } |
| 7762 | } |
| 7763 | return p; |
| 7764 | } |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7765 | |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7766 | private void insertPackageSettingLP(PackageSetting p, PackageParser.Package pkg) { |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7767 | p.pkg = pkg; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7768 | String codePath = pkg.applicationInfo.sourceDir; |
| 7769 | String resourcePath = pkg.applicationInfo.publicSourceDir; |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7770 | // Update code path if needed |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7771 | if (!codePath.equalsIgnoreCase(p.codePathString)) { |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7772 | Log.w(TAG, "Code path for pkg : " + p.pkg.packageName + |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 7773 | " changing from " + p.codePathString + " to " + codePath); |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7774 | p.codePath = new File(codePath); |
| 7775 | p.codePathString = codePath; |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7776 | } |
| 7777 | //Update resource path if needed |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7778 | if (!resourcePath.equalsIgnoreCase(p.resourcePathString)) { |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7779 | Log.w(TAG, "Resource path for pkg : " + p.pkg.packageName + |
| Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 7780 | " changing from " + p.resourcePathString + " to " + resourcePath); |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7781 | p.resourcePath = new File(resourcePath); |
| 7782 | p.resourcePathString = resourcePath; |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7783 | } |
| 7784 | // Update version code if needed |
| 7785 | if (pkg.mVersionCode != p.versionCode) { |
| 7786 | p.versionCode = pkg.mVersionCode; |
| 7787 | } |
| 7788 | addPackageSettingLP(p, pkg.packageName, p.sharedUser); |
| 7789 | } |
| 7790 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7791 | // Utility method that adds a PackageSetting to mPackages and |
| 7792 | // completes updating the shared user attributes |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7793 | private void addPackageSettingLP(PackageSetting p, String name, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7794 | SharedUserSetting sharedUser) { |
| 7795 | mPackages.put(name, p); |
| 7796 | if (sharedUser != null) { |
| 7797 | if (p.sharedUser != null && p.sharedUser != sharedUser) { |
| 7798 | reportSettingsProblem(Log.ERROR, |
| 7799 | "Package " + p.name + " was user " |
| 7800 | + p.sharedUser + " but is now " + sharedUser |
| 7801 | + "; I am not changing its files so it will probably fail!"); |
| 7802 | p.sharedUser.packages.remove(p); |
| 7803 | } else if (p.userId != sharedUser.userId) { |
| 7804 | reportSettingsProblem(Log.ERROR, |
| 7805 | "Package " + p.name + " was user id " + p.userId |
| 7806 | + " but is now user " + sharedUser |
| 7807 | + " with id " + sharedUser.userId |
| 7808 | + "; I am not changing its files so it will probably fail!"); |
| 7809 | } |
| 7810 | |
| 7811 | sharedUser.packages.add(p); |
| 7812 | p.sharedUser = sharedUser; |
| 7813 | p.userId = sharedUser.userId; |
| 7814 | } |
| 7815 | } |
| 7816 | |
| Suchi Amalapurapu | 2ed287b | 2009-08-05 12:43:00 -0700 | [diff] [blame] | 7817 | /* |
| 7818 | * Update the shared user setting when a package using |
| 7819 | * specifying the shared user id is removed. The gids |
| 7820 | * associated with each permission of the deleted package |
| 7821 | * are removed from the shared user's gid list only if its |
| 7822 | * not in use by other permissions of packages in the |
| 7823 | * shared user setting. |
| 7824 | */ |
| 7825 | private void updateSharedUserPermsLP(PackageSetting deletedPs, int[] globalGids) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7826 | if ( (deletedPs == null) || (deletedPs.pkg == null)) { |
| 7827 | Log.i(TAG, "Trying to update info for null package. Just ignoring"); |
| 7828 | return; |
| 7829 | } |
| 7830 | // No sharedUserId |
| 7831 | if (deletedPs.sharedUser == null) { |
| 7832 | return; |
| 7833 | } |
| 7834 | SharedUserSetting sus = deletedPs.sharedUser; |
| 7835 | // Update permissions |
| 7836 | for (String eachPerm: deletedPs.pkg.requestedPermissions) { |
| 7837 | boolean used = false; |
| 7838 | if (!sus.grantedPermissions.contains (eachPerm)) { |
| 7839 | continue; |
| 7840 | } |
| 7841 | for (PackageSetting pkg:sus.packages) { |
| Suchi Amalapurapu | d83006c | 2009-10-28 23:39:46 -0700 | [diff] [blame] | 7842 | if (pkg.pkg != null && |
| 7843 | !pkg.pkg.packageName.equalsIgnoreCase(deletedPs.pkg.packageName) && |
| 7844 | pkg.pkg.requestedPermissions.contains(eachPerm)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7845 | used = true; |
| 7846 | break; |
| 7847 | } |
| 7848 | } |
| 7849 | if (!used) { |
| 7850 | // can safely delete this permission from list |
| 7851 | sus.grantedPermissions.remove(eachPerm); |
| 7852 | sus.loadedPermissions.remove(eachPerm); |
| 7853 | } |
| 7854 | } |
| 7855 | // Update gids |
| Suchi Amalapurapu | 2ed287b | 2009-08-05 12:43:00 -0700 | [diff] [blame] | 7856 | int newGids[] = globalGids; |
| 7857 | for (String eachPerm : sus.grantedPermissions) { |
| 7858 | BasePermission bp = mPermissions.get(eachPerm); |
| Suchi Amalapurapu | d83006c | 2009-10-28 23:39:46 -0700 | [diff] [blame] | 7859 | if (bp != null) { |
| 7860 | newGids = appendInts(newGids, bp.gids); |
| 7861 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7862 | } |
| 7863 | sus.gids = newGids; |
| 7864 | } |
| Suchi Amalapurapu | 2ed287b | 2009-08-05 12:43:00 -0700 | [diff] [blame] | 7865 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7866 | private int removePackageLP(String name) { |
| 7867 | PackageSetting p = mPackages.get(name); |
| 7868 | if (p != null) { |
| 7869 | mPackages.remove(name); |
| 7870 | if (p.sharedUser != null) { |
| 7871 | p.sharedUser.packages.remove(p); |
| 7872 | if (p.sharedUser.packages.size() == 0) { |
| 7873 | mSharedUsers.remove(p.sharedUser.name); |
| 7874 | removeUserIdLP(p.sharedUser.userId); |
| 7875 | return p.sharedUser.userId; |
| 7876 | } |
| 7877 | } else { |
| 7878 | removeUserIdLP(p.userId); |
| 7879 | return p.userId; |
| 7880 | } |
| 7881 | } |
| 7882 | return -1; |
| 7883 | } |
| 7884 | |
| 7885 | private boolean addUserIdLP(int uid, Object obj, Object name) { |
| 7886 | if (uid >= FIRST_APPLICATION_UID + MAX_APPLICATION_UIDS) { |
| 7887 | return false; |
| 7888 | } |
| 7889 | |
| 7890 | if (uid >= FIRST_APPLICATION_UID) { |
| 7891 | int N = mUserIds.size(); |
| 7892 | final int index = uid - FIRST_APPLICATION_UID; |
| 7893 | while (index >= N) { |
| 7894 | mUserIds.add(null); |
| 7895 | N++; |
| 7896 | } |
| 7897 | if (mUserIds.get(index) != null) { |
| 7898 | reportSettingsProblem(Log.ERROR, |
| Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7899 | "Adding duplicate user id: " + uid |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7900 | + " name=" + name); |
| 7901 | return false; |
| 7902 | } |
| 7903 | mUserIds.set(index, obj); |
| 7904 | } else { |
| 7905 | if (mOtherUserIds.get(uid) != null) { |
| 7906 | reportSettingsProblem(Log.ERROR, |
| 7907 | "Adding duplicate shared id: " + uid |
| 7908 | + " name=" + name); |
| 7909 | return false; |
| 7910 | } |
| 7911 | mOtherUserIds.put(uid, obj); |
| 7912 | } |
| 7913 | return true; |
| 7914 | } |
| 7915 | |
| 7916 | public Object getUserIdLP(int uid) { |
| 7917 | if (uid >= FIRST_APPLICATION_UID) { |
| 7918 | int N = mUserIds.size(); |
| 7919 | final int index = uid - FIRST_APPLICATION_UID; |
| 7920 | return index < N ? mUserIds.get(index) : null; |
| 7921 | } else { |
| 7922 | return mOtherUserIds.get(uid); |
| 7923 | } |
| 7924 | } |
| 7925 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7926 | private Set<String> findPackagesWithFlag(int flag) { |
| 7927 | Set<String> ret = new HashSet<String>(); |
| 7928 | for (PackageSetting ps : mPackages.values()) { |
| 7929 | // Has to match atleast all the flag bits set on flag |
| 7930 | if ((ps.pkgFlags & flag) == flag) { |
| 7931 | ret.add(ps.name); |
| 7932 | } |
| 7933 | } |
| 7934 | return ret; |
| 7935 | } |
| 7936 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7937 | private void removeUserIdLP(int uid) { |
| 7938 | if (uid >= FIRST_APPLICATION_UID) { |
| 7939 | int N = mUserIds.size(); |
| 7940 | final int index = uid - FIRST_APPLICATION_UID; |
| 7941 | if (index < N) mUserIds.set(index, null); |
| 7942 | } else { |
| 7943 | mOtherUserIds.remove(uid); |
| 7944 | } |
| 7945 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7946 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7947 | void writeLP() { |
| 7948 | //Debug.startMethodTracing("/data/system/packageprof", 8 * 1024 * 1024); |
| 7949 | |
| 7950 | // Keep the old settings around until we know the new ones have |
| 7951 | // been successfully written. |
| 7952 | if (mSettingsFilename.exists()) { |
| Suchi Amalapurapu | 14e833f | 2009-10-20 11:27:32 -0700 | [diff] [blame] | 7953 | // Presence of backup settings file indicates that we failed |
| 7954 | // to persist settings earlier. So preserve the older |
| 7955 | // backup for future reference since the current settings |
| 7956 | // might have been corrupted. |
| 7957 | if (!mBackupSettingsFilename.exists()) { |
| 7958 | if (!mSettingsFilename.renameTo(mBackupSettingsFilename)) { |
| 7959 | Log.w(TAG, "Unable to backup package manager settings, current changes will be lost at reboot"); |
| 7960 | return; |
| 7961 | } |
| 7962 | } else { |
| 7963 | Log.w(TAG, "Preserving older settings backup"); |
| Suchi Amalapurapu | 3d7e855 | 2009-09-17 15:38:20 -0700 | [diff] [blame] | 7964 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7965 | } |
| 7966 | |
| 7967 | mPastSignatures.clear(); |
| 7968 | |
| 7969 | try { |
| 7970 | FileOutputStream str = new FileOutputStream(mSettingsFilename); |
| 7971 | |
| 7972 | //XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 7973 | XmlSerializer serializer = new FastXmlSerializer(); |
| 7974 | serializer.setOutput(str, "utf-8"); |
| 7975 | serializer.startDocument(null, true); |
| 7976 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 7977 | |
| 7978 | serializer.startTag(null, "packages"); |
| 7979 | |
| 7980 | serializer.startTag(null, "permission-trees"); |
| 7981 | for (BasePermission bp : mPermissionTrees.values()) { |
| 7982 | writePermission(serializer, bp); |
| 7983 | } |
| 7984 | serializer.endTag(null, "permission-trees"); |
| 7985 | |
| 7986 | serializer.startTag(null, "permissions"); |
| 7987 | for (BasePermission bp : mPermissions.values()) { |
| 7988 | writePermission(serializer, bp); |
| 7989 | } |
| 7990 | serializer.endTag(null, "permissions"); |
| 7991 | |
| 7992 | for (PackageSetting pkg : mPackages.values()) { |
| 7993 | writePackage(serializer, pkg); |
| 7994 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7995 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7996 | for (PackageSetting pkg : mDisabledSysPackages.values()) { |
| 7997 | writeDisabledSysPackage(serializer, pkg); |
| 7998 | } |
| 7999 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8000 | serializer.startTag(null, "preferred-activities"); |
| 8001 | for (PreferredActivity pa : mPreferredActivities.filterSet()) { |
| 8002 | serializer.startTag(null, "item"); |
| 8003 | pa.writeToXml(serializer); |
| 8004 | serializer.endTag(null, "item"); |
| 8005 | } |
| 8006 | serializer.endTag(null, "preferred-activities"); |
| 8007 | |
| 8008 | for (SharedUserSetting usr : mSharedUsers.values()) { |
| 8009 | serializer.startTag(null, "shared-user"); |
| 8010 | serializer.attribute(null, "name", usr.name); |
| 8011 | serializer.attribute(null, "userId", |
| 8012 | Integer.toString(usr.userId)); |
| 8013 | usr.signatures.writeXml(serializer, "sigs", mPastSignatures); |
| 8014 | serializer.startTag(null, "perms"); |
| 8015 | for (String name : usr.grantedPermissions) { |
| 8016 | serializer.startTag(null, "item"); |
| 8017 | serializer.attribute(null, "name", name); |
| 8018 | serializer.endTag(null, "item"); |
| 8019 | } |
| 8020 | serializer.endTag(null, "perms"); |
| 8021 | serializer.endTag(null, "shared-user"); |
| 8022 | } |
| 8023 | |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 8024 | if (mPackagesToBeCleaned.size() > 0) { |
| 8025 | for (int i=0; i<mPackagesToBeCleaned.size(); i++) { |
| 8026 | serializer.startTag(null, "cleaning-package"); |
| 8027 | serializer.attribute(null, "name", mPackagesToBeCleaned.get(i)); |
| 8028 | serializer.endTag(null, "cleaning-package"); |
| 8029 | } |
| 8030 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8031 | |
| 8032 | if (mRenamedPackages.size() > 0) { |
| 8033 | for (HashMap.Entry<String, String> e : mRenamedPackages.entrySet()) { |
| 8034 | serializer.startTag(null, "renamed-package"); |
| 8035 | serializer.attribute(null, "new", e.getKey()); |
| 8036 | serializer.attribute(null, "old", e.getValue()); |
| 8037 | serializer.endTag(null, "renamed-package"); |
| 8038 | } |
| 8039 | } |
| 8040 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8041 | serializer.endTag(null, "packages"); |
| 8042 | |
| 8043 | serializer.endDocument(); |
| 8044 | |
| 8045 | str.flush(); |
| 8046 | str.close(); |
| 8047 | |
| 8048 | // New settings successfully written, old ones are no longer |
| 8049 | // needed. |
| 8050 | mBackupSettingsFilename.delete(); |
| 8051 | FileUtils.setPermissions(mSettingsFilename.toString(), |
| 8052 | FileUtils.S_IRUSR|FileUtils.S_IWUSR |
| 8053 | |FileUtils.S_IRGRP|FileUtils.S_IWGRP |
| 8054 | |FileUtils.S_IROTH, |
| 8055 | -1, -1); |
| David 'Digit' Turner | add1376 | 2010-02-03 17:34:58 -0800 | [diff] [blame] | 8056 | |
| 8057 | // Write package list file now, use a JournaledFile. |
| 8058 | // |
| 8059 | File tempFile = new File(mPackageListFilename.toString() + ".tmp"); |
| 8060 | JournaledFile journal = new JournaledFile(mPackageListFilename, tempFile); |
| 8061 | |
| 8062 | str = new FileOutputStream(journal.chooseForWrite()); |
| 8063 | try { |
| 8064 | StringBuilder sb = new StringBuilder(); |
| 8065 | for (PackageSetting pkg : mPackages.values()) { |
| 8066 | ApplicationInfo ai = pkg.pkg.applicationInfo; |
| 8067 | String dataPath = ai.dataDir; |
| 8068 | boolean isDebug = (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 8069 | |
| 8070 | // Avoid any application that has a space in its path |
| 8071 | // or that is handled by the system. |
| 8072 | if (dataPath.indexOf(" ") >= 0 || ai.uid <= Process.FIRST_APPLICATION_UID) |
| 8073 | continue; |
| 8074 | |
| 8075 | // we store on each line the following information for now: |
| 8076 | // |
| 8077 | // pkgName - package name |
| 8078 | // userId - application-specific user id |
| 8079 | // debugFlag - 0 or 1 if the package is debuggable. |
| 8080 | // dataPath - path to package's data path |
| 8081 | // |
| 8082 | // NOTE: We prefer not to expose all ApplicationInfo flags for now. |
| 8083 | // |
| 8084 | // DO NOT MODIFY THIS FORMAT UNLESS YOU CAN ALSO MODIFY ITS USERS |
| 8085 | // FROM NATIVE CODE. AT THE MOMENT, LOOK AT THE FOLLOWING SOURCES: |
| 8086 | // system/core/run-as/run-as.c |
| 8087 | // |
| 8088 | sb.setLength(0); |
| 8089 | sb.append(ai.packageName); |
| 8090 | sb.append(" "); |
| 8091 | sb.append((int)ai.uid); |
| 8092 | sb.append(isDebug ? " 1 " : " 0 "); |
| 8093 | sb.append(dataPath); |
| 8094 | sb.append("\n"); |
| 8095 | str.write(sb.toString().getBytes()); |
| 8096 | } |
| 8097 | str.flush(); |
| 8098 | str.close(); |
| 8099 | journal.commit(); |
| 8100 | } |
| 8101 | catch (Exception e) { |
| 8102 | journal.rollback(); |
| 8103 | } |
| 8104 | |
| 8105 | FileUtils.setPermissions(mPackageListFilename.toString(), |
| 8106 | FileUtils.S_IRUSR|FileUtils.S_IWUSR |
| 8107 | |FileUtils.S_IRGRP|FileUtils.S_IWGRP |
| 8108 | |FileUtils.S_IROTH, |
| 8109 | -1, -1); |
| 8110 | |
| Suchi Amalapurapu | 8550f25 | 2009-09-29 15:20:32 -0700 | [diff] [blame] | 8111 | return; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8112 | |
| 8113 | } catch(XmlPullParserException e) { |
| 8114 | Log.w(TAG, "Unable to write package manager settings, current changes will be lost at reboot", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8115 | } catch(java.io.IOException e) { |
| 8116 | Log.w(TAG, "Unable to write package manager settings, current changes will be lost at reboot", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8117 | } |
| David 'Digit' Turner | add1376 | 2010-02-03 17:34:58 -0800 | [diff] [blame] | 8118 | // Clean up partially written files |
| Suchi Amalapurapu | 8550f25 | 2009-09-29 15:20:32 -0700 | [diff] [blame] | 8119 | if (mSettingsFilename.exists()) { |
| 8120 | if (!mSettingsFilename.delete()) { |
| 8121 | Log.i(TAG, "Failed to clean up mangled file: " + mSettingsFilename); |
| 8122 | } |
| 8123 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8124 | //Debug.stopMethodTracing(); |
| 8125 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8126 | |
| 8127 | void writeDisabledSysPackage(XmlSerializer serializer, final PackageSetting pkg) |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 8128 | throws java.io.IOException { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8129 | serializer.startTag(null, "updated-package"); |
| 8130 | serializer.attribute(null, "name", pkg.name); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8131 | if (pkg.realName != null) { |
| 8132 | serializer.attribute(null, "realName", pkg.realName); |
| 8133 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8134 | serializer.attribute(null, "codePath", pkg.codePathString); |
| 8135 | serializer.attribute(null, "ts", pkg.getTimeStampStr()); |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8136 | serializer.attribute(null, "version", String.valueOf(pkg.versionCode)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8137 | if (!pkg.resourcePathString.equals(pkg.codePathString)) { |
| 8138 | serializer.attribute(null, "resourcePath", pkg.resourcePathString); |
| 8139 | } |
| 8140 | if (pkg.sharedUser == null) { |
| 8141 | serializer.attribute(null, "userId", |
| 8142 | Integer.toString(pkg.userId)); |
| 8143 | } else { |
| 8144 | serializer.attribute(null, "sharedUserId", |
| 8145 | Integer.toString(pkg.userId)); |
| 8146 | } |
| 8147 | serializer.startTag(null, "perms"); |
| 8148 | if (pkg.sharedUser == null) { |
| 8149 | // If this is a shared user, the permissions will |
| 8150 | // be written there. We still need to write an |
| 8151 | // empty permissions list so permissionsFixed will |
| 8152 | // be set. |
| 8153 | for (final String name : pkg.grantedPermissions) { |
| 8154 | BasePermission bp = mPermissions.get(name); |
| 8155 | if ((bp != null) && (bp.perm != null) && (bp.perm.info != null)) { |
| 8156 | // We only need to write signature or system permissions but this wont |
| 8157 | // match the semantics of grantedPermissions. So write all permissions. |
| 8158 | serializer.startTag(null, "item"); |
| 8159 | serializer.attribute(null, "name", name); |
| 8160 | serializer.endTag(null, "item"); |
| 8161 | } |
| 8162 | } |
| 8163 | } |
| 8164 | serializer.endTag(null, "perms"); |
| 8165 | serializer.endTag(null, "updated-package"); |
| 8166 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8167 | |
| 8168 | void writePackage(XmlSerializer serializer, final PackageSetting pkg) |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 8169 | throws java.io.IOException { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8170 | serializer.startTag(null, "package"); |
| 8171 | serializer.attribute(null, "name", pkg.name); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8172 | if (pkg.realName != null) { |
| 8173 | serializer.attribute(null, "realName", pkg.realName); |
| 8174 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8175 | serializer.attribute(null, "codePath", pkg.codePathString); |
| 8176 | if (!pkg.resourcePathString.equals(pkg.codePathString)) { |
| 8177 | serializer.attribute(null, "resourcePath", pkg.resourcePathString); |
| 8178 | } |
| Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8179 | serializer.attribute(null, "flags", |
| 8180 | Integer.toString(pkg.pkgFlags)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8181 | serializer.attribute(null, "ts", pkg.getTimeStampStr()); |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8182 | serializer.attribute(null, "version", String.valueOf(pkg.versionCode)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8183 | if (pkg.sharedUser == null) { |
| 8184 | serializer.attribute(null, "userId", |
| 8185 | Integer.toString(pkg.userId)); |
| 8186 | } else { |
| 8187 | serializer.attribute(null, "sharedUserId", |
| 8188 | Integer.toString(pkg.userId)); |
| 8189 | } |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 8190 | if (pkg.uidError) { |
| 8191 | serializer.attribute(null, "uidError", "true"); |
| 8192 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8193 | if (pkg.enabled != COMPONENT_ENABLED_STATE_DEFAULT) { |
| 8194 | serializer.attribute(null, "enabled", |
| 8195 | pkg.enabled == COMPONENT_ENABLED_STATE_ENABLED |
| 8196 | ? "true" : "false"); |
| 8197 | } |
| 8198 | if(pkg.installStatus == PKG_INSTALL_INCOMPLETE) { |
| 8199 | serializer.attribute(null, "installStatus", "false"); |
| 8200 | } |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8201 | if (pkg.installerPackageName != null) { |
| 8202 | serializer.attribute(null, "installer", pkg.installerPackageName); |
| 8203 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8204 | pkg.signatures.writeXml(serializer, "sigs", mPastSignatures); |
| 8205 | if ((pkg.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 8206 | serializer.startTag(null, "perms"); |
| 8207 | if (pkg.sharedUser == null) { |
| 8208 | // If this is a shared user, the permissions will |
| 8209 | // be written there. We still need to write an |
| 8210 | // empty permissions list so permissionsFixed will |
| 8211 | // be set. |
| 8212 | for (final String name : pkg.grantedPermissions) { |
| 8213 | serializer.startTag(null, "item"); |
| 8214 | serializer.attribute(null, "name", name); |
| 8215 | serializer.endTag(null, "item"); |
| 8216 | } |
| 8217 | } |
| 8218 | serializer.endTag(null, "perms"); |
| 8219 | } |
| 8220 | if (pkg.disabledComponents.size() > 0) { |
| 8221 | serializer.startTag(null, "disabled-components"); |
| 8222 | for (final String name : pkg.disabledComponents) { |
| 8223 | serializer.startTag(null, "item"); |
| 8224 | serializer.attribute(null, "name", name); |
| 8225 | serializer.endTag(null, "item"); |
| 8226 | } |
| 8227 | serializer.endTag(null, "disabled-components"); |
| 8228 | } |
| 8229 | if (pkg.enabledComponents.size() > 0) { |
| 8230 | serializer.startTag(null, "enabled-components"); |
| 8231 | for (final String name : pkg.enabledComponents) { |
| 8232 | serializer.startTag(null, "item"); |
| 8233 | serializer.attribute(null, "name", name); |
| 8234 | serializer.endTag(null, "item"); |
| 8235 | } |
| 8236 | serializer.endTag(null, "enabled-components"); |
| 8237 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8238 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8239 | serializer.endTag(null, "package"); |
| 8240 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8241 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8242 | void writePermission(XmlSerializer serializer, BasePermission bp) |
| 8243 | throws XmlPullParserException, java.io.IOException { |
| 8244 | if (bp.type != BasePermission.TYPE_BUILTIN |
| 8245 | && bp.sourcePackage != null) { |
| 8246 | serializer.startTag(null, "item"); |
| 8247 | serializer.attribute(null, "name", bp.name); |
| 8248 | serializer.attribute(null, "package", bp.sourcePackage); |
| 8249 | if (DEBUG_SETTINGS) Log.v(TAG, |
| 8250 | "Writing perm: name=" + bp.name + " type=" + bp.type); |
| 8251 | if (bp.type == BasePermission.TYPE_DYNAMIC) { |
| 8252 | PermissionInfo pi = bp.perm != null ? bp.perm.info |
| 8253 | : bp.pendingInfo; |
| 8254 | if (pi != null) { |
| 8255 | serializer.attribute(null, "type", "dynamic"); |
| 8256 | if (pi.icon != 0) { |
| 8257 | serializer.attribute(null, "icon", |
| 8258 | Integer.toString(pi.icon)); |
| 8259 | } |
| 8260 | if (pi.nonLocalizedLabel != null) { |
| 8261 | serializer.attribute(null, "label", |
| 8262 | pi.nonLocalizedLabel.toString()); |
| 8263 | } |
| 8264 | if (pi.protectionLevel != |
| 8265 | PermissionInfo.PROTECTION_NORMAL) { |
| 8266 | serializer.attribute(null, "protection", |
| 8267 | Integer.toString(pi.protectionLevel)); |
| 8268 | } |
| 8269 | } |
| 8270 | } |
| 8271 | serializer.endTag(null, "item"); |
| 8272 | } |
| 8273 | } |
| 8274 | |
| 8275 | String getReadMessagesLP() { |
| 8276 | return mReadMessages.toString(); |
| 8277 | } |
| 8278 | |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 8279 | ArrayList<PackageSetting> getListOfIncompleteInstallPackages() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8280 | HashSet<String> kList = new HashSet<String>(mPackages.keySet()); |
| 8281 | Iterator<String> its = kList.iterator(); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 8282 | ArrayList<PackageSetting> ret = new ArrayList<PackageSetting>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8283 | while(its.hasNext()) { |
| 8284 | String key = its.next(); |
| 8285 | PackageSetting ps = mPackages.get(key); |
| 8286 | if(ps.getInstallStatus() == PKG_INSTALL_INCOMPLETE) { |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 8287 | ret.add(ps); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8288 | } |
| 8289 | } |
| 8290 | return ret; |
| 8291 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8292 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8293 | boolean readLP() { |
| 8294 | FileInputStream str = null; |
| 8295 | if (mBackupSettingsFilename.exists()) { |
| 8296 | try { |
| 8297 | str = new FileInputStream(mBackupSettingsFilename); |
| 8298 | mReadMessages.append("Reading from backup settings file\n"); |
| 8299 | Log.i(TAG, "Reading from backup settings file!"); |
| Suchi Amalapurapu | 14e833f | 2009-10-20 11:27:32 -0700 | [diff] [blame] | 8300 | if (mSettingsFilename.exists()) { |
| 8301 | // If both the backup and settings file exist, we |
| 8302 | // ignore the settings since it might have been |
| 8303 | // corrupted. |
| 8304 | Log.w(TAG, "Cleaning up settings file " + mSettingsFilename); |
| 8305 | mSettingsFilename.delete(); |
| 8306 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8307 | } catch (java.io.IOException e) { |
| 8308 | // We'll try for the normal settings file. |
| 8309 | } |
| 8310 | } |
| 8311 | |
| 8312 | mPastSignatures.clear(); |
| 8313 | |
| 8314 | try { |
| 8315 | if (str == null) { |
| 8316 | if (!mSettingsFilename.exists()) { |
| 8317 | mReadMessages.append("No settings file found\n"); |
| 8318 | Log.i(TAG, "No current settings file!"); |
| 8319 | return false; |
| 8320 | } |
| 8321 | str = new FileInputStream(mSettingsFilename); |
| 8322 | } |
| 8323 | XmlPullParser parser = Xml.newPullParser(); |
| 8324 | parser.setInput(str, null); |
| 8325 | |
| 8326 | int type; |
| 8327 | while ((type=parser.next()) != XmlPullParser.START_TAG |
| 8328 | && type != XmlPullParser.END_DOCUMENT) { |
| 8329 | ; |
| 8330 | } |
| 8331 | |
| 8332 | if (type != XmlPullParser.START_TAG) { |
| 8333 | mReadMessages.append("No start tag found in settings file\n"); |
| 8334 | Log.e(TAG, "No start tag found in package manager settings"); |
| 8335 | return false; |
| 8336 | } |
| 8337 | |
| 8338 | int outerDepth = parser.getDepth(); |
| 8339 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 8340 | && (type != XmlPullParser.END_TAG |
| 8341 | || parser.getDepth() > outerDepth)) { |
| 8342 | if (type == XmlPullParser.END_TAG |
| 8343 | || type == XmlPullParser.TEXT) { |
| 8344 | continue; |
| 8345 | } |
| 8346 | |
| 8347 | String tagName = parser.getName(); |
| 8348 | if (tagName.equals("package")) { |
| 8349 | readPackageLP(parser); |
| 8350 | } else if (tagName.equals("permissions")) { |
| 8351 | readPermissionsLP(mPermissions, parser); |
| 8352 | } else if (tagName.equals("permission-trees")) { |
| 8353 | readPermissionsLP(mPermissionTrees, parser); |
| 8354 | } else if (tagName.equals("shared-user")) { |
| 8355 | readSharedUserLP(parser); |
| 8356 | } else if (tagName.equals("preferred-packages")) { |
| Dianne Hackborn | a7ca0e5 | 2009-12-01 14:31:55 -0800 | [diff] [blame] | 8357 | // no longer used. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8358 | } else if (tagName.equals("preferred-activities")) { |
| 8359 | readPreferredActivitiesLP(parser); |
| 8360 | } else if(tagName.equals("updated-package")) { |
| 8361 | readDisabledSysPackageLP(parser); |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 8362 | } else if (tagName.equals("cleaning-package")) { |
| 8363 | String name = parser.getAttributeValue(null, "name"); |
| 8364 | if (name != null) { |
| 8365 | mPackagesToBeCleaned.add(name); |
| 8366 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8367 | } else if (tagName.equals("renamed-package")) { |
| 8368 | String nname = parser.getAttributeValue(null, "new"); |
| 8369 | String oname = parser.getAttributeValue(null, "old"); |
| 8370 | if (nname != null && oname != null) { |
| 8371 | mRenamedPackages.put(nname, oname); |
| 8372 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8373 | } else { |
| 8374 | Log.w(TAG, "Unknown element under <packages>: " |
| 8375 | + parser.getName()); |
| 8376 | XmlUtils.skipCurrentTag(parser); |
| 8377 | } |
| 8378 | } |
| 8379 | |
| 8380 | str.close(); |
| 8381 | |
| 8382 | } catch(XmlPullParserException e) { |
| 8383 | mReadMessages.append("Error reading: " + e.toString()); |
| 8384 | Log.e(TAG, "Error reading package manager settings", e); |
| 8385 | |
| 8386 | } catch(java.io.IOException e) { |
| 8387 | mReadMessages.append("Error reading: " + e.toString()); |
| 8388 | Log.e(TAG, "Error reading package manager settings", e); |
| 8389 | |
| 8390 | } |
| 8391 | |
| 8392 | int N = mPendingPackages.size(); |
| 8393 | for (int i=0; i<N; i++) { |
| 8394 | final PendingPackage pp = mPendingPackages.get(i); |
| 8395 | Object idObj = getUserIdLP(pp.sharedId); |
| 8396 | if (idObj != null && idObj instanceof SharedUserSetting) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8397 | PackageSetting p = getPackageLP(pp.name, null, pp.realName, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8398 | (SharedUserSetting)idObj, pp.codePath, pp.resourcePath, |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8399 | pp.versionCode, pp.pkgFlags, true, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8400 | if (p == null) { |
| 8401 | Log.w(TAG, "Unable to create application package for " |
| 8402 | + pp.name); |
| 8403 | continue; |
| 8404 | } |
| 8405 | p.copyFrom(pp); |
| 8406 | } else if (idObj != null) { |
| 8407 | String msg = "Bad package setting: package " + pp.name |
| 8408 | + " has shared uid " + pp.sharedId |
| 8409 | + " that is not a shared uid\n"; |
| 8410 | mReadMessages.append(msg); |
| 8411 | Log.e(TAG, msg); |
| 8412 | } else { |
| 8413 | String msg = "Bad package setting: package " + pp.name |
| 8414 | + " has shared uid " + pp.sharedId |
| 8415 | + " that is not defined\n"; |
| 8416 | mReadMessages.append(msg); |
| 8417 | Log.e(TAG, msg); |
| 8418 | } |
| 8419 | } |
| 8420 | mPendingPackages.clear(); |
| 8421 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8422 | mReadMessages.append("Read completed successfully: " |
| 8423 | + mPackages.size() + " packages, " |
| 8424 | + mSharedUsers.size() + " shared uids\n"); |
| 8425 | |
| 8426 | return true; |
| 8427 | } |
| 8428 | |
| 8429 | private int readInt(XmlPullParser parser, String ns, String name, |
| 8430 | int defValue) { |
| 8431 | String v = parser.getAttributeValue(ns, name); |
| 8432 | try { |
| 8433 | if (v == null) { |
| 8434 | return defValue; |
| 8435 | } |
| 8436 | return Integer.parseInt(v); |
| 8437 | } catch (NumberFormatException e) { |
| 8438 | reportSettingsProblem(Log.WARN, |
| 8439 | "Error in package manager settings: attribute " + |
| 8440 | name + " has bad integer value " + v + " at " |
| 8441 | + parser.getPositionDescription()); |
| 8442 | } |
| 8443 | return defValue; |
| 8444 | } |
| 8445 | |
| 8446 | private void readPermissionsLP(HashMap<String, BasePermission> out, |
| 8447 | XmlPullParser parser) |
| 8448 | throws IOException, XmlPullParserException { |
| 8449 | int outerDepth = parser.getDepth(); |
| 8450 | int type; |
| 8451 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 8452 | && (type != XmlPullParser.END_TAG |
| 8453 | || parser.getDepth() > outerDepth)) { |
| 8454 | if (type == XmlPullParser.END_TAG |
| 8455 | || type == XmlPullParser.TEXT) { |
| 8456 | continue; |
| 8457 | } |
| 8458 | |
| 8459 | String tagName = parser.getName(); |
| 8460 | if (tagName.equals("item")) { |
| 8461 | String name = parser.getAttributeValue(null, "name"); |
| 8462 | String sourcePackage = parser.getAttributeValue(null, "package"); |
| 8463 | String ptype = parser.getAttributeValue(null, "type"); |
| 8464 | if (name != null && sourcePackage != null) { |
| 8465 | boolean dynamic = "dynamic".equals(ptype); |
| 8466 | BasePermission bp = new BasePermission(name, sourcePackage, |
| 8467 | dynamic |
| 8468 | ? BasePermission.TYPE_DYNAMIC |
| 8469 | : BasePermission.TYPE_NORMAL); |
| 8470 | if (dynamic) { |
| 8471 | PermissionInfo pi = new PermissionInfo(); |
| 8472 | pi.packageName = sourcePackage.intern(); |
| 8473 | pi.name = name.intern(); |
| 8474 | pi.icon = readInt(parser, null, "icon", 0); |
| 8475 | pi.nonLocalizedLabel = parser.getAttributeValue( |
| 8476 | null, "label"); |
| 8477 | pi.protectionLevel = readInt(parser, null, "protection", |
| 8478 | PermissionInfo.PROTECTION_NORMAL); |
| 8479 | bp.pendingInfo = pi; |
| 8480 | } |
| 8481 | out.put(bp.name, bp); |
| 8482 | } else { |
| 8483 | reportSettingsProblem(Log.WARN, |
| 8484 | "Error in package manager settings: permissions has" |
| 8485 | + " no name at " + parser.getPositionDescription()); |
| 8486 | } |
| 8487 | } else { |
| 8488 | reportSettingsProblem(Log.WARN, |
| 8489 | "Unknown element reading permissions: " |
| 8490 | + parser.getName() + " at " |
| 8491 | + parser.getPositionDescription()); |
| 8492 | } |
| 8493 | XmlUtils.skipCurrentTag(parser); |
| 8494 | } |
| 8495 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8496 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8497 | private void readDisabledSysPackageLP(XmlPullParser parser) |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 8498 | throws XmlPullParserException, IOException { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8499 | String name = parser.getAttributeValue(null, "name"); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8500 | String realName = parser.getAttributeValue(null, "realName"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8501 | String codePathStr = parser.getAttributeValue(null, "codePath"); |
| 8502 | String resourcePathStr = parser.getAttributeValue(null, "resourcePath"); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8503 | if (resourcePathStr == null) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8504 | resourcePathStr = codePathStr; |
| 8505 | } |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8506 | String version = parser.getAttributeValue(null, "version"); |
| 8507 | int versionCode = 0; |
| 8508 | if (version != null) { |
| 8509 | try { |
| 8510 | versionCode = Integer.parseInt(version); |
| 8511 | } catch (NumberFormatException e) { |
| 8512 | } |
| 8513 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8514 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8515 | int pkgFlags = 0; |
| 8516 | pkgFlags |= ApplicationInfo.FLAG_SYSTEM; |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8517 | PackageSetting ps = new PackageSetting(name, realName, |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8518 | new File(codePathStr), |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8519 | new File(resourcePathStr), versionCode, pkgFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8520 | String timeStampStr = parser.getAttributeValue(null, "ts"); |
| 8521 | if (timeStampStr != null) { |
| 8522 | try { |
| 8523 | long timeStamp = Long.parseLong(timeStampStr); |
| 8524 | ps.setTimeStamp(timeStamp, timeStampStr); |
| 8525 | } catch (NumberFormatException e) { |
| 8526 | } |
| 8527 | } |
| 8528 | String idStr = parser.getAttributeValue(null, "userId"); |
| 8529 | ps.userId = idStr != null ? Integer.parseInt(idStr) : 0; |
| 8530 | if(ps.userId <= 0) { |
| 8531 | String sharedIdStr = parser.getAttributeValue(null, "sharedUserId"); |
| 8532 | ps.userId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0; |
| 8533 | } |
| 8534 | int outerDepth = parser.getDepth(); |
| 8535 | int type; |
| 8536 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 8537 | && (type != XmlPullParser.END_TAG |
| 8538 | || parser.getDepth() > outerDepth)) { |
| 8539 | if (type == XmlPullParser.END_TAG |
| 8540 | || type == XmlPullParser.TEXT) { |
| 8541 | continue; |
| 8542 | } |
| 8543 | |
| 8544 | String tagName = parser.getName(); |
| 8545 | if (tagName.equals("perms")) { |
| 8546 | readGrantedPermissionsLP(parser, |
| 8547 | ps.grantedPermissions); |
| 8548 | } else { |
| 8549 | reportSettingsProblem(Log.WARN, |
| 8550 | "Unknown element under <updated-package>: " |
| 8551 | + parser.getName()); |
| 8552 | XmlUtils.skipCurrentTag(parser); |
| 8553 | } |
| 8554 | } |
| 8555 | mDisabledSysPackages.put(name, ps); |
| 8556 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8557 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8558 | private void readPackageLP(XmlPullParser parser) |
| 8559 | throws XmlPullParserException, IOException { |
| 8560 | String name = null; |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8561 | String realName = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8562 | String idStr = null; |
| 8563 | String sharedIdStr = null; |
| 8564 | String codePathStr = null; |
| 8565 | String resourcePathStr = null; |
| 8566 | String systemStr = null; |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8567 | String installerPackageName = null; |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 8568 | String uidError = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8569 | int pkgFlags = 0; |
| 8570 | String timeStampStr; |
| 8571 | long timeStamp = 0; |
| 8572 | PackageSettingBase packageSetting = null; |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8573 | String version = null; |
| 8574 | int versionCode = 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8575 | try { |
| 8576 | name = parser.getAttributeValue(null, "name"); |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8577 | realName = parser.getAttributeValue(null, "realName"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8578 | idStr = parser.getAttributeValue(null, "userId"); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 8579 | uidError = parser.getAttributeValue(null, "uidError"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8580 | sharedIdStr = parser.getAttributeValue(null, "sharedUserId"); |
| 8581 | codePathStr = parser.getAttributeValue(null, "codePath"); |
| 8582 | resourcePathStr = parser.getAttributeValue(null, "resourcePath"); |
| Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8583 | version = parser.getAttributeValue(null, "version"); |
| 8584 | if (version != null) { |
| 8585 | try { |
| 8586 | versionCode = Integer.parseInt(version); |
| 8587 | } catch (NumberFormatException e) { |
| 8588 | } |
| 8589 | } |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8590 | installerPackageName = parser.getAttributeValue(null, "installer"); |
| Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8591 | |
| 8592 | systemStr = parser.getAttributeValue(null, "flags"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8593 | if (systemStr != null) { |
| Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8594 | try { |
| 8595 | pkgFlags = Integer.parseInt(systemStr); |
| 8596 | } catch (NumberFormatException e) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8597 | } |
| 8598 | } else { |
| Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8599 | // For backward compatibility |
| 8600 | systemStr = parser.getAttributeValue(null, "system"); |
| 8601 | if (systemStr != null) { |
| 8602 | pkgFlags |= ("true".equalsIgnoreCase(systemStr)) ? ApplicationInfo.FLAG_SYSTEM : 0; |
| 8603 | } else { |
| 8604 | // Old settings that don't specify system... just treat |
| 8605 | // them as system, good enough. |
| 8606 | pkgFlags |= ApplicationInfo.FLAG_SYSTEM; |
| 8607 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8608 | } |
| 8609 | timeStampStr = parser.getAttributeValue(null, "ts"); |
| 8610 | if (timeStampStr != null) { |
| 8611 | try { |
| 8612 | timeStamp = Long.parseLong(timeStampStr); |
| 8613 | } catch (NumberFormatException e) { |
| 8614 | } |
| 8615 | } |
| 8616 | if (DEBUG_SETTINGS) Log.v(TAG, "Reading package: " + name |
| 8617 | + " userId=" + idStr + " sharedUserId=" + sharedIdStr); |
| 8618 | int userId = idStr != null ? Integer.parseInt(idStr) : 0; |
| 8619 | if (resourcePathStr == null) { |
| 8620 | resourcePathStr = codePathStr; |
| 8621 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8622 | if (realName != null) { |
| 8623 | realName = realName.intern(); |
| 8624 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8625 | if (name == null) { |
| 8626 | reportSettingsProblem(Log.WARN, |
| 8627 | "Error in package manager settings: <package> has no name at " |
| 8628 | + parser.getPositionDescription()); |
| 8629 | } else if (codePathStr == null) { |
| 8630 | reportSettingsProblem(Log.WARN, |
| 8631 | "Error in package manager settings: <package> has no codePath at " |
| 8632 | + parser.getPositionDescription()); |
| 8633 | } else if (userId > 0) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8634 | packageSetting = addPackageLP(name.intern(), realName, |
| 8635 | new File(codePathStr), new File(resourcePathStr), |
| 8636 | userId, versionCode, pkgFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8637 | if (DEBUG_SETTINGS) Log.i(TAG, "Reading package " + name |
| 8638 | + ": userId=" + userId + " pkg=" + packageSetting); |
| 8639 | if (packageSetting == null) { |
| 8640 | reportSettingsProblem(Log.ERROR, |
| 8641 | "Failure adding uid " + userId |
| 8642 | + " while parsing settings at " |
| 8643 | + parser.getPositionDescription()); |
| 8644 | } else { |
| 8645 | packageSetting.setTimeStamp(timeStamp, timeStampStr); |
| 8646 | } |
| 8647 | } else if (sharedIdStr != null) { |
| 8648 | userId = sharedIdStr != null |
| 8649 | ? Integer.parseInt(sharedIdStr) : 0; |
| 8650 | if (userId > 0) { |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8651 | packageSetting = new PendingPackage(name.intern(), realName, |
| 8652 | new File(codePathStr), new File(resourcePathStr), |
| 8653 | userId, versionCode, pkgFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8654 | packageSetting.setTimeStamp(timeStamp, timeStampStr); |
| 8655 | mPendingPackages.add((PendingPackage) packageSetting); |
| 8656 | if (DEBUG_SETTINGS) Log.i(TAG, "Reading package " + name |
| 8657 | + ": sharedUserId=" + userId + " pkg=" |
| 8658 | + packageSetting); |
| 8659 | } else { |
| 8660 | reportSettingsProblem(Log.WARN, |
| 8661 | "Error in package manager settings: package " |
| 8662 | + name + " has bad sharedId " + sharedIdStr |
| 8663 | + " at " + parser.getPositionDescription()); |
| 8664 | } |
| 8665 | } else { |
| 8666 | reportSettingsProblem(Log.WARN, |
| 8667 | "Error in package manager settings: package " |
| 8668 | + name + " has bad userId " + idStr + " at " |
| 8669 | + parser.getPositionDescription()); |
| 8670 | } |
| 8671 | } catch (NumberFormatException e) { |
| 8672 | reportSettingsProblem(Log.WARN, |
| 8673 | "Error in package manager settings: package " |
| 8674 | + name + " has bad userId " + idStr + " at " |
| 8675 | + parser.getPositionDescription()); |
| 8676 | } |
| 8677 | if (packageSetting != null) { |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 8678 | packageSetting.uidError = "true".equals(uidError); |
| Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8679 | packageSetting.installerPackageName = installerPackageName; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8680 | final String enabledStr = parser.getAttributeValue(null, "enabled"); |
| 8681 | if (enabledStr != null) { |
| 8682 | if (enabledStr.equalsIgnoreCase("true")) { |
| 8683 | packageSetting.enabled = COMPONENT_ENABLED_STATE_ENABLED; |
| 8684 | } else if (enabledStr.equalsIgnoreCase("false")) { |
| 8685 | packageSetting.enabled = COMPONENT_ENABLED_STATE_DISABLED; |
| 8686 | } else if (enabledStr.equalsIgnoreCase("default")) { |
| 8687 | packageSetting.enabled = COMPONENT_ENABLED_STATE_DEFAULT; |
| 8688 | } else { |
| 8689 | reportSettingsProblem(Log.WARN, |
| 8690 | "Error in package manager settings: package " |
| 8691 | + name + " has bad enabled value: " + idStr |
| 8692 | + " at " + parser.getPositionDescription()); |
| 8693 | } |
| 8694 | } else { |
| 8695 | packageSetting.enabled = COMPONENT_ENABLED_STATE_DEFAULT; |
| 8696 | } |
| 8697 | final String installStatusStr = parser.getAttributeValue(null, "installStatus"); |
| 8698 | if (installStatusStr != null) { |
| 8699 | if (installStatusStr.equalsIgnoreCase("false")) { |
| 8700 | packageSetting.installStatus = PKG_INSTALL_INCOMPLETE; |
| 8701 | } else { |
| 8702 | packageSetting.installStatus = PKG_INSTALL_COMPLETE; |
| 8703 | } |
| 8704 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8705 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8706 | int outerDepth = parser.getDepth(); |
| 8707 | int type; |
| 8708 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 8709 | && (type != XmlPullParser.END_TAG |
| 8710 | || parser.getDepth() > outerDepth)) { |
| 8711 | if (type == XmlPullParser.END_TAG |
| 8712 | || type == XmlPullParser.TEXT) { |
| 8713 | continue; |
| 8714 | } |
| 8715 | |
| 8716 | String tagName = parser.getName(); |
| 8717 | if (tagName.equals("disabled-components")) { |
| 8718 | readDisabledComponentsLP(packageSetting, parser); |
| 8719 | } else if (tagName.equals("enabled-components")) { |
| 8720 | readEnabledComponentsLP(packageSetting, parser); |
| 8721 | } else if (tagName.equals("sigs")) { |
| 8722 | packageSetting.signatures.readXml(parser, mPastSignatures); |
| 8723 | } else if (tagName.equals("perms")) { |
| 8724 | readGrantedPermissionsLP(parser, |
| 8725 | packageSetting.loadedPermissions); |
| 8726 | packageSetting.permissionsFixed = true; |
| 8727 | } else { |
| 8728 | reportSettingsProblem(Log.WARN, |
| 8729 | "Unknown element under <package>: " |
| 8730 | + parser.getName()); |
| 8731 | XmlUtils.skipCurrentTag(parser); |
| 8732 | } |
| 8733 | } |
| 8734 | } else { |
| 8735 | XmlUtils.skipCurrentTag(parser); |
| 8736 | } |
| 8737 | } |
| 8738 | |
| 8739 | private void readDisabledComponentsLP(PackageSettingBase packageSetting, |
| 8740 | XmlPullParser parser) |
| 8741 | throws IOException, XmlPullParserException { |
| 8742 | int outerDepth = parser.getDepth(); |
| 8743 | int type; |
| 8744 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 8745 | && (type != XmlPullParser.END_TAG |
| 8746 | || parser.getDepth() > outerDepth)) { |
| 8747 | if (type == XmlPullParser.END_TAG |
| 8748 | || type == XmlPullParser.TEXT) { |
| 8749 | continue; |
| 8750 | } |
| 8751 | |
| 8752 | String tagName = parser.getName(); |
| 8753 | if (tagName.equals("item")) { |
| 8754 | String name = parser.getAttributeValue(null, "name"); |
| 8755 | if (name != null) { |
| 8756 | packageSetting.disabledComponents.add(name.intern()); |
| 8757 | } else { |
| 8758 | reportSettingsProblem(Log.WARN, |
| 8759 | "Error in package manager settings: <disabled-components> has" |
| 8760 | + " no name at " + parser.getPositionDescription()); |
| 8761 | } |
| 8762 | } else { |
| 8763 | reportSettingsProblem(Log.WARN, |
| 8764 | "Unknown element under <disabled-components>: " |
| 8765 | + parser.getName()); |
| 8766 | } |
| 8767 | XmlUtils.skipCurrentTag(parser); |
| 8768 | } |
| 8769 | } |
| 8770 | |
| 8771 | private void readEnabledComponentsLP(PackageSettingBase packageSetting, |
| 8772 | XmlPullParser parser) |
| 8773 | throws IOException, XmlPullParserException { |
| 8774 | int outerDepth = parser.getDepth(); |
| 8775 | int type; |
| 8776 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 8777 | && (type != XmlPullParser.END_TAG |
| 8778 | || parser.getDepth() > outerDepth)) { |
| 8779 | if (type == XmlPullParser.END_TAG |
| 8780 | || type == XmlPullParser.TEXT) { |
| 8781 | continue; |
| 8782 | } |
| 8783 | |
| 8784 | String tagName = parser.getName(); |
| 8785 | if (tagName.equals("item")) { |
| 8786 | String name = parser.getAttributeValue(null, "name"); |
| 8787 | if (name != null) { |
| 8788 | packageSetting.enabledComponents.add(name.intern()); |
| 8789 | } else { |
| 8790 | reportSettingsProblem(Log.WARN, |
| 8791 | "Error in package manager settings: <enabled-components> has" |
| 8792 | + " no name at " + parser.getPositionDescription()); |
| 8793 | } |
| 8794 | } else { |
| 8795 | reportSettingsProblem(Log.WARN, |
| 8796 | "Unknown element under <enabled-components>: " |
| 8797 | + parser.getName()); |
| 8798 | } |
| 8799 | XmlUtils.skipCurrentTag(parser); |
| 8800 | } |
| 8801 | } |
| 8802 | |
| 8803 | private void readSharedUserLP(XmlPullParser parser) |
| 8804 | throws XmlPullParserException, IOException { |
| 8805 | String name = null; |
| 8806 | String idStr = null; |
| 8807 | int pkgFlags = 0; |
| 8808 | SharedUserSetting su = null; |
| 8809 | try { |
| 8810 | name = parser.getAttributeValue(null, "name"); |
| 8811 | idStr = parser.getAttributeValue(null, "userId"); |
| 8812 | int userId = idStr != null ? Integer.parseInt(idStr) : 0; |
| 8813 | if ("true".equals(parser.getAttributeValue(null, "system"))) { |
| 8814 | pkgFlags |= ApplicationInfo.FLAG_SYSTEM; |
| 8815 | } |
| 8816 | if (name == null) { |
| 8817 | reportSettingsProblem(Log.WARN, |
| 8818 | "Error in package manager settings: <shared-user> has no name at " |
| 8819 | + parser.getPositionDescription()); |
| 8820 | } else if (userId == 0) { |
| 8821 | reportSettingsProblem(Log.WARN, |
| 8822 | "Error in package manager settings: shared-user " |
| 8823 | + name + " has bad userId " + idStr + " at " |
| 8824 | + parser.getPositionDescription()); |
| 8825 | } else { |
| 8826 | if ((su=addSharedUserLP(name.intern(), userId, pkgFlags)) == null) { |
| 8827 | reportSettingsProblem(Log.ERROR, |
| 8828 | "Occurred while parsing settings at " |
| 8829 | + parser.getPositionDescription()); |
| 8830 | } |
| 8831 | } |
| 8832 | } catch (NumberFormatException e) { |
| 8833 | reportSettingsProblem(Log.WARN, |
| 8834 | "Error in package manager settings: package " |
| 8835 | + name + " has bad userId " + idStr + " at " |
| 8836 | + parser.getPositionDescription()); |
| 8837 | }; |
| 8838 | |
| 8839 | if (su != null) { |
| 8840 | int outerDepth = parser.getDepth(); |
| 8841 | int type; |
| 8842 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 8843 | && (type != XmlPullParser.END_TAG |
| 8844 | || parser.getDepth() > outerDepth)) { |
| 8845 | if (type == XmlPullParser.END_TAG |
| 8846 | || type == XmlPullParser.TEXT) { |
| 8847 | continue; |
| 8848 | } |
| 8849 | |
| 8850 | String tagName = parser.getName(); |
| 8851 | if (tagName.equals("sigs")) { |
| 8852 | su.signatures.readXml(parser, mPastSignatures); |
| 8853 | } else if (tagName.equals("perms")) { |
| 8854 | readGrantedPermissionsLP(parser, su.loadedPermissions); |
| 8855 | } else { |
| 8856 | reportSettingsProblem(Log.WARN, |
| 8857 | "Unknown element under <shared-user>: " |
| 8858 | + parser.getName()); |
| 8859 | XmlUtils.skipCurrentTag(parser); |
| 8860 | } |
| 8861 | } |
| 8862 | |
| 8863 | } else { |
| 8864 | XmlUtils.skipCurrentTag(parser); |
| 8865 | } |
| 8866 | } |
| 8867 | |
| 8868 | private void readGrantedPermissionsLP(XmlPullParser parser, |
| 8869 | HashSet<String> outPerms) throws IOException, XmlPullParserException { |
| 8870 | int outerDepth = parser.getDepth(); |
| 8871 | int type; |
| 8872 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 8873 | && (type != XmlPullParser.END_TAG |
| 8874 | || parser.getDepth() > outerDepth)) { |
| 8875 | if (type == XmlPullParser.END_TAG |
| 8876 | || type == XmlPullParser.TEXT) { |
| 8877 | continue; |
| 8878 | } |
| 8879 | |
| 8880 | String tagName = parser.getName(); |
| 8881 | if (tagName.equals("item")) { |
| 8882 | String name = parser.getAttributeValue(null, "name"); |
| 8883 | if (name != null) { |
| 8884 | outPerms.add(name.intern()); |
| 8885 | } else { |
| 8886 | reportSettingsProblem(Log.WARN, |
| 8887 | "Error in package manager settings: <perms> has" |
| 8888 | + " no name at " + parser.getPositionDescription()); |
| 8889 | } |
| 8890 | } else { |
| 8891 | reportSettingsProblem(Log.WARN, |
| 8892 | "Unknown element under <perms>: " |
| 8893 | + parser.getName()); |
| 8894 | } |
| 8895 | XmlUtils.skipCurrentTag(parser); |
| 8896 | } |
| 8897 | } |
| 8898 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8899 | private void readPreferredActivitiesLP(XmlPullParser parser) |
| 8900 | throws XmlPullParserException, IOException { |
| 8901 | int outerDepth = parser.getDepth(); |
| 8902 | int type; |
| 8903 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 8904 | && (type != XmlPullParser.END_TAG |
| 8905 | || parser.getDepth() > outerDepth)) { |
| 8906 | if (type == XmlPullParser.END_TAG |
| 8907 | || type == XmlPullParser.TEXT) { |
| 8908 | continue; |
| 8909 | } |
| 8910 | |
| 8911 | String tagName = parser.getName(); |
| 8912 | if (tagName.equals("item")) { |
| 8913 | PreferredActivity pa = new PreferredActivity(parser); |
| 8914 | if (pa.mParseError == null) { |
| 8915 | mPreferredActivities.addFilter(pa); |
| 8916 | } else { |
| 8917 | reportSettingsProblem(Log.WARN, |
| 8918 | "Error in package manager settings: <preferred-activity> " |
| 8919 | + pa.mParseError + " at " |
| 8920 | + parser.getPositionDescription()); |
| 8921 | } |
| 8922 | } else { |
| 8923 | reportSettingsProblem(Log.WARN, |
| 8924 | "Unknown element under <preferred-activities>: " |
| 8925 | + parser.getName()); |
| 8926 | XmlUtils.skipCurrentTag(parser); |
| 8927 | } |
| 8928 | } |
| 8929 | } |
| 8930 | |
| 8931 | // Returns -1 if we could not find an available UserId to assign |
| 8932 | private int newUserIdLP(Object obj) { |
| 8933 | // Let's be stupidly inefficient for now... |
| 8934 | final int N = mUserIds.size(); |
| 8935 | for (int i=0; i<N; i++) { |
| 8936 | if (mUserIds.get(i) == null) { |
| 8937 | mUserIds.set(i, obj); |
| 8938 | return FIRST_APPLICATION_UID + i; |
| 8939 | } |
| 8940 | } |
| 8941 | |
| 8942 | // None left? |
| 8943 | if (N >= MAX_APPLICATION_UIDS) { |
| 8944 | return -1; |
| 8945 | } |
| 8946 | |
| 8947 | mUserIds.add(obj); |
| 8948 | return FIRST_APPLICATION_UID + N; |
| 8949 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8950 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8951 | public PackageSetting getDisabledSystemPkg(String name) { |
| 8952 | synchronized(mPackages) { |
| 8953 | PackageSetting ps = mDisabledSysPackages.get(name); |
| 8954 | return ps; |
| 8955 | } |
| 8956 | } |
| 8957 | |
| 8958 | boolean isEnabledLP(ComponentInfo componentInfo, int flags) { |
| 8959 | final PackageSetting packageSettings = mPackages.get(componentInfo.packageName); |
| 8960 | if (Config.LOGV) { |
| 8961 | Log.v(TAG, "isEnabledLock - packageName = " + componentInfo.packageName |
| 8962 | + " componentName = " + componentInfo.name); |
| 8963 | Log.v(TAG, "enabledComponents: " |
| 8964 | + Arrays.toString(packageSettings.enabledComponents.toArray())); |
| 8965 | Log.v(TAG, "disabledComponents: " |
| 8966 | + Arrays.toString(packageSettings.disabledComponents.toArray())); |
| 8967 | } |
| Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8968 | if (packageSettings == null) { |
| 8969 | if (false) { |
| 8970 | Log.w(TAG, "WAITING FOR DEBUGGER"); |
| 8971 | Debug.waitForDebugger(); |
| 8972 | Log.i(TAG, "We will crash!"); |
| 8973 | } |
| 8974 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8975 | return ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) |
| 8976 | || ((componentInfo.enabled |
| 8977 | && ((packageSettings.enabled == COMPONENT_ENABLED_STATE_ENABLED) |
| 8978 | || (componentInfo.applicationInfo.enabled |
| 8979 | && packageSettings.enabled != COMPONENT_ENABLED_STATE_DISABLED)) |
| 8980 | && !packageSettings.disabledComponents.contains(componentInfo.name)) |
| 8981 | || packageSettings.enabledComponents.contains(componentInfo.name)); |
| 8982 | } |
| 8983 | } |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8984 | |
| 8985 | // ------- apps on sdcard specific code ------- |
| 8986 | static final boolean DEBUG_SD_INSTALL = false; |
| Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 8987 | final private String mSdEncryptKey = "AppsOnSD"; |
| Oscar Montemayor | 462f037 | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 8988 | final private String mSdEncryptAlg = "AES"; |
| Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8989 | private boolean mMediaMounted = false; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8990 | private static final int MAX_CONTAINERS = 250; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8991 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8992 | private String getEncryptKey() { |
| 8993 | try { |
| 8994 | String sdEncKey = SystemKeyStore.getInstance().retrieveKeyHexString(mSdEncryptKey); |
| 8995 | if (sdEncKey == null) { |
| 8996 | sdEncKey = SystemKeyStore.getInstance(). |
| 8997 | generateNewKeyHexString(128, mSdEncryptAlg, mSdEncryptKey); |
| 8998 | if (sdEncKey == null) { |
| 8999 | Log.e(TAG, "Failed to create encryption keys"); |
| 9000 | return null; |
| 9001 | } |
| 9002 | } |
| 9003 | return sdEncKey; |
| 9004 | } catch (NoSuchAlgorithmException nsae) { |
| 9005 | Log.e(TAG, "Failed to create encryption keys with exception: " + nsae); |
| 9006 | return null; |
| 9007 | } |
| 9008 | } |
| 9009 | |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 9010 | static String getTempContainerId() { |
| Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 9011 | String prefix = "smdl2tmp"; |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 9012 | int tmpIdx = 1; |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 9013 | String list[] = PackageHelper.getSecureContainerList(); |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 9014 | if (list != null) { |
| 9015 | int idx = 0; |
| 9016 | int idList[] = new int[MAX_CONTAINERS]; |
| 9017 | boolean neverFound = true; |
| 9018 | for (String name : list) { |
| 9019 | // Ignore null entries |
| 9020 | if (name == null) { |
| 9021 | continue; |
| 9022 | } |
| 9023 | int sidx = name.indexOf(prefix); |
| 9024 | if (sidx == -1) { |
| 9025 | // Not a temp file. just ignore |
| 9026 | continue; |
| 9027 | } |
| 9028 | String subStr = name.substring(sidx + prefix.length()); |
| 9029 | idList[idx] = -1; |
| 9030 | if (subStr != null) { |
| 9031 | try { |
| 9032 | int cid = Integer.parseInt(subStr); |
| 9033 | idList[idx++] = cid; |
| 9034 | neverFound = false; |
| 9035 | } catch (NumberFormatException e) { |
| 9036 | } |
| 9037 | } |
| 9038 | } |
| 9039 | if (!neverFound) { |
| 9040 | // Sort idList |
| 9041 | Arrays.sort(idList); |
| 9042 | for (int j = 1; j <= idList.length; j++) { |
| 9043 | if (idList[j-1] != j) { |
| 9044 | tmpIdx = j; |
| 9045 | break; |
| 9046 | } |
| 9047 | } |
| 9048 | } |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 9049 | } |
| Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 9050 | return prefix + tmpIdx; |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 9051 | } |
| 9052 | |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 9053 | /* |
| 9054 | * Return true if PackageManager does have packages to be updated. |
| 9055 | */ |
| Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 9056 | public boolean updateExternalMediaStatus(final boolean mediaStatus) { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9057 | final boolean ret; |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 9058 | synchronized (mPackages) { |
| 9059 | if (DEBUG_SD_INSTALL) Log.i(TAG, "updateExternalMediaStatus:: mediaStatus=" + |
| 9060 | mediaStatus+", mMediaMounted=" + mMediaMounted); |
| 9061 | if (mediaStatus == mMediaMounted) { |
| Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 9062 | return false; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9063 | } |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 9064 | mMediaMounted = mediaStatus; |
| Suchi Amalapurapu | 6069beb | 2010-03-10 09:46:49 -0800 | [diff] [blame] | 9065 | Set<String> appList = mSettings.findPackagesWithFlag(ApplicationInfo.FLAG_EXTERNAL_STORAGE); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9066 | ret = appList != null && appList.size() > 0; |
| Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 9067 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9068 | // Queue up an async operation since the package installation may take a little while. |
| 9069 | mHandler.post(new Runnable() { |
| 9070 | public void run() { |
| 9071 | mHandler.removeCallbacks(this); |
| 9072 | updateExternalMediaStatusInner(mediaStatus, ret); |
| 9073 | } |
| 9074 | }); |
| 9075 | return ret; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9076 | } |
| 9077 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9078 | private void updateExternalMediaStatusInner(boolean mediaStatus, |
| 9079 | boolean sendUpdateBroadcast) { |
| 9080 | // If we are up here that means there are packages to be |
| 9081 | // enabled or disabled. |
| Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 9082 | final String list[] = PackageHelper.getSecureContainerList(); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9083 | if (list == null || list.length == 0) { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9084 | return; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9085 | } |
| Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 9086 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9087 | int uidList[] = new int[list.length]; |
| 9088 | int num = 0; |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9089 | HashSet<String> removeCids = new HashSet<String>(); |
| 9090 | HashMap<SdInstallArgs, String> processCids = new HashMap<SdInstallArgs, String>(); |
| 9091 | /*HashMap<String, String> cidPathMap = new HashMap<String, String>(); |
| 9092 | // Don't hold any locks when getting cache paths |
| 9093 | for (String cid : list) { |
| 9094 | String cpath = PackageHelper.getSdDir(cid); |
| 9095 | if (cpath == null) { |
| 9096 | removeCids.add(cid); |
| 9097 | } else { |
| 9098 | cidPathMap.put(cid, cpath); |
| 9099 | } |
| 9100 | }*/ |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9101 | synchronized (mPackages) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9102 | for (String cid : list) { |
| 9103 | SdInstallArgs args = new SdInstallArgs(cid); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9104 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Processing container " + cid); |
| 9105 | boolean failed = true; |
| 9106 | try { |
| 9107 | String pkgName = args.getPackageName(); |
| 9108 | if (pkgName == null) { |
| 9109 | continue; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9110 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9111 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Looking for pkg : " + pkgName); |
| 9112 | PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 9113 | if (ps != null && ps.codePathString != null && |
| Suchi Amalapurapu | 6069beb | 2010-03-10 09:46:49 -0800 | [diff] [blame] | 9114 | (ps.pkgFlags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9115 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Container : " + cid + |
| 9116 | " corresponds to pkg : " + pkgName + |
| 9117 | " at code path: " + ps.codePathString); |
| 9118 | // We do have a valid package installed on sdcard |
| 9119 | processCids.put(args, ps.codePathString); |
| 9120 | failed = false; |
| 9121 | int uid = ps.userId; |
| 9122 | if (uid != -1) { |
| 9123 | uidList[num++] = uid; |
| 9124 | } |
| 9125 | } |
| 9126 | } finally { |
| 9127 | if (failed) { |
| 9128 | // Stale container on sdcard. Just delete |
| 9129 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Container : " + cid + " stale"); |
| 9130 | removeCids.add(cid); |
| 9131 | } |
| Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 9132 | } |
| 9133 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9134 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9135 | // Organize uids |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9136 | int uidArr[] = null; |
| 9137 | if (num > 0) { |
| 9138 | // Sort uid list |
| 9139 | Arrays.sort(uidList, 0, num); |
| 9140 | // Throw away duplicates |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9141 | uidArr = new int[num]; |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9142 | uidArr[0] = uidList[0]; |
| 9143 | int di = 0; |
| 9144 | for (int i = 1; i < num; i++) { |
| 9145 | if (uidList[i-1] != uidList[i]) { |
| 9146 | uidArr[di++] = uidList[i]; |
| 9147 | } |
| 9148 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9149 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9150 | // Process packages with valid entries. |
| 9151 | if (mediaStatus) { |
| 9152 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Loading packages"); |
| 9153 | loadMediaPackages(processCids, uidArr, sendUpdateBroadcast, removeCids); |
| 9154 | startCleaningPackages(); |
| 9155 | } else { |
| 9156 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Unloading packages"); |
| 9157 | unloadMediaPackages(processCids, uidArr, sendUpdateBroadcast); |
| 9158 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9159 | } |
| 9160 | |
| 9161 | private void sendResourcesChangedBroadcast(boolean mediaStatus, |
| 9162 | ArrayList<String> pkgList, int uidArr[]) { |
| 9163 | int size = pkgList.size(); |
| 9164 | if (size > 0) { |
| 9165 | // Send broadcasts here |
| 9166 | Bundle extras = new Bundle(); |
| 9167 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, |
| 9168 | pkgList.toArray(new String[size])); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9169 | if (uidArr != null) { |
| 9170 | extras.putIntArray(Intent.EXTRA_CHANGED_UID_LIST, uidArr); |
| 9171 | } |
| 9172 | String action = mediaStatus ? Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE |
| 9173 | : Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9174 | sendPackageBroadcast(action, null, extras); |
| 9175 | } |
| 9176 | } |
| 9177 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9178 | /* |
| 9179 | * Look at potentially valid container ids from processCids |
| 9180 | * If package information doesn't match the one on record |
| 9181 | * or package scanning fails, the cid is added to list of |
| 9182 | * removeCids and cleaned up. Since cleaning up containers |
| 9183 | * involves destroying them, we do not want any parse |
| 9184 | * references to such stale containers. So force gc's |
| 9185 | * to avoid unnecessary crashes. |
| 9186 | */ |
| 9187 | private void loadMediaPackages(HashMap<SdInstallArgs, String> processCids, |
| 9188 | int uidArr[], boolean sendUpdateBroadcast, |
| 9189 | HashSet<String> removeCids) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9190 | ArrayList<String> pkgList = new ArrayList<String>(); |
| 9191 | Set<SdInstallArgs> keys = processCids.keySet(); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9192 | boolean doGc = false; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9193 | for (SdInstallArgs args : keys) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9194 | String codePath = processCids.get(args); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9195 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Loading container : " |
| 9196 | + args.cid); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9197 | int retCode = PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9198 | try { |
| 9199 | // Make sure there are no container errors first. |
| 9200 | if (args.doPreInstall(PackageManager.INSTALL_SUCCEEDED) |
| 9201 | != PackageManager.INSTALL_SUCCEEDED) { |
| 9202 | Log.e(TAG, "Failed to mount cid : " + args.cid + |
| 9203 | " when installing from sdcard"); |
| 9204 | continue; |
| 9205 | } |
| 9206 | // Check code path here. |
| 9207 | if (codePath == null || !codePath.equals(args.getCodePath())) { |
| 9208 | Log.e(TAG, "Container " + args.cid + " cachepath " + args.getCodePath()+ |
| 9209 | " does not match one in settings " + codePath); |
| 9210 | continue; |
| 9211 | } |
| 9212 | // Parse package |
| 9213 | int parseFlags = PackageParser.PARSE_CHATTY | |
| 9214 | PackageParser.PARSE_ON_SDCARD | mDefParseFlags; |
| 9215 | PackageParser pp = new PackageParser(codePath); |
| 9216 | pp.setSeparateProcesses(mSeparateProcesses); |
| 9217 | final PackageParser.Package pkg = pp.parsePackage(new File(codePath), |
| 9218 | codePath, mMetrics, parseFlags); |
| 9219 | pp = null; |
| 9220 | doGc = true; |
| 9221 | // Check for parse errors |
| 9222 | if (pkg == null) { |
| 9223 | Log.e(TAG, "Parse error when installing install pkg : " |
| 9224 | + args.cid + " from " + args.cachePath); |
| 9225 | continue; |
| 9226 | } |
| 9227 | setApplicationInfoPaths(pkg, codePath, codePath); |
| 9228 | synchronized (mInstallLock) { |
| 9229 | // Scan the package |
| 9230 | if (scanPackageLI(pkg, parseFlags, SCAN_MONITOR) != null) { |
| 9231 | synchronized (mPackages) { |
| 9232 | // Grant permissions |
| 9233 | grantPermissionsLP(pkg, false); |
| 9234 | // Persist settings |
| 9235 | mSettings.writeLP(); |
| 9236 | retCode = PackageManager.INSTALL_SUCCEEDED; |
| 9237 | pkgList.add(pkg.packageName); |
| 9238 | // Post process args |
| 9239 | args.doPostInstall(PackageManager.INSTALL_SUCCEEDED); |
| 9240 | } |
| 9241 | } else { |
| 9242 | Log.i(TAG, "Failed to install pkg: " + |
| 9243 | pkg.packageName + " from sdcard"); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9244 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9245 | } |
| 9246 | |
| 9247 | } finally { |
| 9248 | if (retCode != PackageManager.INSTALL_SUCCEEDED) { |
| 9249 | // Don't destroy container here. Wait till gc clears things up. |
| 9250 | removeCids.add(args.cid); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9251 | } |
| 9252 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9253 | } |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 9254 | // Send a broadcast to let everyone know we are done processing |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9255 | if (sendUpdateBroadcast) { |
| 9256 | sendResourcesChangedBroadcast(true, pkgList, uidArr); |
| 9257 | } |
| 9258 | if (doGc) { |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9259 | Runtime.getRuntime().gc(); |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9260 | } |
| 9261 | // Delete any stale containers if needed. |
| 9262 | if (removeCids != null) { |
| 9263 | for (String cid : removeCids) { |
| 9264 | Log.i(TAG, "Destroying stale container : " + cid); |
| 9265 | PackageHelper.destroySdDir(cid); |
| 9266 | } |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9267 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9268 | } |
| 9269 | |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9270 | private void unloadMediaPackages(HashMap<SdInstallArgs, String> processCids, |
| 9271 | int uidArr[], boolean sendUpdateBroadcast) { |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9272 | if (DEBUG_SD_INSTALL) Log.i(TAG, "unloading media packages"); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9273 | ArrayList<String> pkgList = new ArrayList<String>(); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9274 | ArrayList<SdInstallArgs> failedList = new ArrayList<SdInstallArgs>(); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9275 | Set<SdInstallArgs> keys = processCids.keySet(); |
| 9276 | for (SdInstallArgs args : keys) { |
| 9277 | String cid = args.cid; |
| 9278 | String pkgName = args.getPackageName(); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9279 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Trying to unload pkg : " + pkgName); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9280 | // Delete package internally |
| 9281 | PackageRemovedInfo outInfo = new PackageRemovedInfo(); |
| 9282 | synchronized (mInstallLock) { |
| 9283 | boolean res = deletePackageLI(pkgName, false, |
| 9284 | PackageManager.DONT_DELETE_DATA, outInfo); |
| 9285 | if (res) { |
| 9286 | pkgList.add(pkgName); |
| 9287 | } else { |
| 9288 | Log.e(TAG, "Failed to delete pkg from sdcard : " + pkgName); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9289 | failedList.add(args); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9290 | } |
| 9291 | } |
| 9292 | } |
| 9293 | // Send broadcasts |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9294 | if (sendUpdateBroadcast) { |
| 9295 | sendResourcesChangedBroadcast(false, pkgList, uidArr); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9296 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9297 | // Force gc |
| 9298 | Runtime.getRuntime().gc(); |
| 9299 | // Just unmount all valid containers. |
| 9300 | for (SdInstallArgs args : keys) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9301 | synchronized (mInstallLock) { |
| 9302 | args.doPostDeleteLI(false); |
| 9303 | } |
| 9304 | } |
| Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 9305 | } |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9306 | |
| 9307 | public void movePackage(final String packageName, |
| 9308 | final IPackageMoveObserver observer, final int flags) { |
| 9309 | if (packageName == null) { |
| 9310 | return; |
| 9311 | } |
| 9312 | mContext.enforceCallingOrSelfPermission( |
| 9313 | android.Manifest.permission.MOVE_PACKAGE, null); |
| 9314 | int returnCode = PackageManager.MOVE_SUCCEEDED; |
| 9315 | int currFlags = 0; |
| 9316 | int newFlags = 0; |
| 9317 | synchronized (mPackages) { |
| 9318 | PackageParser.Package pkg = mPackages.get(packageName); |
| 9319 | if (pkg == null) { |
| 9320 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
| 9321 | } |
| 9322 | // Disable moving fwd locked apps and system packages |
| 9323 | if (pkg.applicationInfo != null && |
| 9324 | (pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 9325 | Log.w(TAG, "Cannot move system application"); |
| 9326 | returnCode = PackageManager.MOVE_FAILED_SYSTEM_PACKAGE; |
| 9327 | } else if (pkg.applicationInfo != null && |
| 9328 | (pkg.applicationInfo.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0) { |
| 9329 | Log.w(TAG, "Cannot move forward locked app."); |
| 9330 | returnCode = PackageManager.MOVE_FAILED_FORWARD_LOCKED; |
| 9331 | } else { |
| 9332 | // Find install location first |
| 9333 | if ((flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 && |
| 9334 | (flags & PackageManager.MOVE_INTERNAL) != 0) { |
| 9335 | Log.w(TAG, "Ambigous flags specified for move location."); |
| 9336 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
| 9337 | } else { |
| 9338 | newFlags = (flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 ? |
| 9339 | PackageManager.INSTALL_EXTERNAL : 0; |
| Suchi Amalapurapu | 6069beb | 2010-03-10 09:46:49 -0800 | [diff] [blame] | 9340 | currFlags = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0 ? |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9341 | PackageManager.INSTALL_EXTERNAL : 0; |
| 9342 | if (newFlags == currFlags) { |
| 9343 | Log.w(TAG, "No move required. Trying to move to same location"); |
| 9344 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
| 9345 | } |
| 9346 | } |
| 9347 | } |
| 9348 | if (returnCode != PackageManager.MOVE_SUCCEEDED) { |
| 9349 | processPendingMove(new MoveParams(null, observer, 0, null), returnCode); |
| 9350 | } else { |
| 9351 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| 9352 | InstallArgs srcArgs = createInstallArgs(currFlags, pkg.applicationInfo.sourceDir, |
| 9353 | pkg.applicationInfo.publicSourceDir); |
| 9354 | MoveParams mp = new MoveParams(srcArgs, observer, newFlags, |
| 9355 | packageName); |
| 9356 | msg.obj = mp; |
| 9357 | mHandler.sendMessage(msg); |
| 9358 | } |
| 9359 | } |
| 9360 | } |
| 9361 | |
| 9362 | private void processPendingMove(final MoveParams mp, final int currentStatus) { |
| 9363 | // Queue up an async operation since the package deletion may take a little while. |
| 9364 | mHandler.post(new Runnable() { |
| 9365 | public void run() { |
| 9366 | mHandler.removeCallbacks(this); |
| 9367 | int returnCode = currentStatus; |
| 9368 | boolean moveSucceeded = (returnCode == PackageManager.MOVE_SUCCEEDED); |
| 9369 | if (moveSucceeded) { |
| 9370 | int uid = -1; |
| 9371 | synchronized (mPackages) { |
| 9372 | uid = mPackages.get(mp.packageName).applicationInfo.uid; |
| 9373 | } |
| 9374 | ArrayList<String> pkgList = new ArrayList<String>(); |
| 9375 | pkgList.add(mp.packageName); |
| 9376 | int uidArr[] = new int[] { uid }; |
| 9377 | // Send resources unavailable broadcast |
| 9378 | sendResourcesChangedBroadcast(false, pkgList, uidArr); |
| 9379 | |
| 9380 | // Update package code and resource paths |
| 9381 | synchronized (mPackages) { |
| 9382 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 9383 | if (pkg != null) { |
| 9384 | String oldCodePath = pkg.mPath; |
| 9385 | String newCodePath = mp.targetArgs.getCodePath(); |
| 9386 | String newResPath = mp.targetArgs.getResourcePath(); |
| 9387 | pkg.mPath = newCodePath; |
| 9388 | // Move dex files around |
| 9389 | if (moveDexFiles(pkg) |
| 9390 | != PackageManager.INSTALL_SUCCEEDED) { |
| 9391 | // Moving of dex files failed. Set |
| 9392 | // error code and abort move. |
| 9393 | pkg.mPath = pkg.mScanPath; |
| 9394 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 9395 | moveSucceeded = false; |
| 9396 | } else { |
| 9397 | pkg.mScanPath = newCodePath; |
| 9398 | pkg.applicationInfo.sourceDir = newCodePath; |
| 9399 | pkg.applicationInfo.publicSourceDir = newResPath; |
| 9400 | PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 9401 | ps.codePath = new File(pkg.applicationInfo.sourceDir); |
| 9402 | ps.codePathString = ps.codePath.getPath(); |
| 9403 | ps.resourcePath = new File(pkg.applicationInfo.publicSourceDir); |
| 9404 | ps.resourcePathString = ps.resourcePath.getPath(); |
| 9405 | // Set the application info flag correctly. |
| 9406 | if ((mp.flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
| Suchi Amalapurapu | 6069beb | 2010-03-10 09:46:49 -0800 | [diff] [blame] | 9407 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9408 | } else { |
| Suchi Amalapurapu | 6069beb | 2010-03-10 09:46:49 -0800 | [diff] [blame] | 9409 | pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9410 | } |
| 9411 | ps.setFlags(pkg.applicationInfo.flags); |
| 9412 | mAppDirs.remove(oldCodePath); |
| 9413 | mAppDirs.put(newCodePath, pkg); |
| 9414 | // Persist settings |
| 9415 | mSettings.writeLP(); |
| 9416 | } |
| 9417 | } |
| 9418 | } |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9419 | // Send resources available broadcast |
| 9420 | sendResourcesChangedBroadcast(true, pkgList, uidArr); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9421 | } |
| 9422 | if (!moveSucceeded){ |
| 9423 | // Clean up failed installation |
| 9424 | if (mp.targetArgs != null) { |
| 9425 | mp.targetArgs.doPostInstall( |
| Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9426 | returnCode); |
| 9427 | } |
| 9428 | } else { |
| 9429 | // Force a gc to clear things up. |
| 9430 | Runtime.getRuntime().gc(); |
| 9431 | // Delete older code |
| 9432 | synchronized (mInstallLock) { |
| 9433 | mp.srcArgs.doPostDeleteLI(true); |
| Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9434 | } |
| 9435 | } |
| 9436 | IPackageMoveObserver observer = mp.observer; |
| 9437 | if (observer != null) { |
| 9438 | try { |
| 9439 | observer.packageMoved(mp.packageName, returnCode); |
| 9440 | } catch (RemoteException e) { |
| 9441 | Log.i(TAG, "Observer no longer exists."); |
| 9442 | } |
| 9443 | } |
| 9444 | } |
| 9445 | }); |
| 9446 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9447 | } |