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 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 17 | package com.android.server.pm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.GRANT_REVOKE_PERMISSIONS; |
| 20 | import static android.Manifest.permission.READ_EXTERNAL_STORAGE; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 21 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; |
| 22 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED; |
Dianne Hackborn | 0ac3031 | 2011-06-17 14:49:23 -0700 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 24 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 25 | import static com.android.internal.util.ArrayUtils.appendInt; |
| 26 | import static com.android.internal.util.ArrayUtils.removeInt; |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 27 | import static libcore.io.OsConstants.S_ISLNK; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 28 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 29 | import com.android.internal.app.IMediaContainerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import com.android.internal.app.ResolverActivity; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 31 | import com.android.internal.content.NativeLibraryHelper; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 32 | import com.android.internal.content.PackageHelper; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 33 | import com.android.internal.util.XmlUtils; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 34 | import com.android.server.DeviceStorageMonitorService; |
| 35 | import com.android.server.EventLogTags; |
| 36 | import com.android.server.IntentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 38 | import org.xmlpull.v1.XmlPullParser; |
| 39 | import org.xmlpull.v1.XmlPullParserException; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.app.ActivityManagerNative; |
| 42 | import android.app.IActivityManager; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 43 | import android.app.admin.IDevicePolicyManager; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 44 | import android.app.backup.IBackupManager; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 45 | import android.content.BroadcastReceiver; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 46 | import android.content.ComponentName; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 47 | import android.content.Context; |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 48 | import android.content.IIntentReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.content.Intent; |
| 50 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 51 | import android.content.IntentSender; |
Jason parks | 1125d78 | 2011-01-12 09:47:26 -0600 | [diff] [blame] | 52 | import android.content.ServiceConnection; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 53 | import android.content.IntentSender.SendIntentException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.content.pm.ActivityInfo; |
| 55 | import android.content.pm.ApplicationInfo; |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 56 | import android.content.pm.FeatureInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.content.pm.IPackageDataObserver; |
| 58 | import android.content.pm.IPackageDeleteObserver; |
| 59 | import android.content.pm.IPackageInstallObserver; |
| 60 | import android.content.pm.IPackageManager; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 61 | import android.content.pm.IPackageMoveObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.content.pm.IPackageStatsObserver; |
| 63 | import android.content.pm.InstrumentationInfo; |
| 64 | import android.content.pm.PackageInfo; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 65 | import android.content.pm.PackageInfoLite; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.content.pm.PackageManager; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 67 | import android.content.pm.PackageParser; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 68 | import android.content.pm.PackageStats; |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 69 | import android.content.pm.ParceledListSlice; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 70 | import android.content.pm.PermissionGroupInfo; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 71 | import android.content.pm.PermissionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.content.pm.ProviderInfo; |
| 73 | import android.content.pm.ResolveInfo; |
| 74 | import android.content.pm.ServiceInfo; |
| 75 | import android.content.pm.Signature; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 76 | import android.content.pm.UserInfo; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 77 | import android.content.pm.ManifestDigest; |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 78 | import android.content.pm.VerifierDeviceIdentity; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 79 | import android.content.pm.VerifierInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.net.Uri; |
| 81 | import android.os.Binder; |
Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 82 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | import android.os.Bundle; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 84 | import android.os.Environment; |
| 85 | import android.os.FileObserver; |
| 86 | import android.os.FileUtils; |
| 87 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.os.HandlerThread; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 89 | import android.os.IBinder; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 90 | import android.os.Looper; |
| 91 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | import android.os.ParcelFileDescriptor; |
| 94 | import android.os.Process; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 95 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | import android.os.ServiceManager; |
| 97 | import android.os.SystemClock; |
| 98 | import android.os.SystemProperties; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 99 | import android.os.UserId; |
Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 100 | import android.security.SystemKeyStore; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 101 | import android.util.DisplayMetrics; |
| 102 | import android.util.EventLog; |
| 103 | import android.util.Log; |
| 104 | import android.util.LogPrinter; |
| 105 | import android.util.Slog; |
| 106 | import android.util.SparseArray; |
| 107 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.view.Display; |
| 109 | import android.view.WindowManager; |
| 110 | |
| 111 | import java.io.File; |
| 112 | import java.io.FileDescriptor; |
| 113 | import java.io.FileInputStream; |
| 114 | import java.io.FileNotFoundException; |
| 115 | import java.io.FileOutputStream; |
| 116 | import java.io.FileReader; |
| 117 | import java.io.FilenameFilter; |
| 118 | import java.io.IOException; |
| 119 | import java.io.InputStream; |
| 120 | import java.io.PrintWriter; |
Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 121 | import java.security.NoSuchAlgorithmException; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 122 | import java.security.PublicKey; |
| 123 | import java.security.cert.CertificateException; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 124 | import java.text.SimpleDateFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | import java.util.ArrayList; |
| 126 | import java.util.Arrays; |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 127 | import java.util.Collection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | import java.util.Collections; |
| 129 | import java.util.Comparator; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 130 | import java.util.Date; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | import java.util.Enumeration; |
| 132 | import java.util.HashMap; |
| 133 | import java.util.HashSet; |
| 134 | import java.util.Iterator; |
| 135 | import java.util.List; |
| 136 | import java.util.Map; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 137 | import java.util.Map.Entry; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | import java.util.Set; |
| 139 | import java.util.zip.ZipEntry; |
| 140 | import java.util.zip.ZipFile; |
| 141 | import java.util.zip.ZipOutputStream; |
| 142 | |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 143 | import libcore.io.ErrnoException; |
Kenny Root | 3f99afc | 2012-04-14 10:13:10 -0700 | [diff] [blame] | 144 | import libcore.io.IoUtils; |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 145 | import libcore.io.Libcore; |
| 146 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 147 | /** |
| 148 | * Keep track of all those .apks everywhere. |
| 149 | * |
| 150 | * This is very central to the platform's security; please run the unit |
| 151 | * tests whenever making modifications here: |
| 152 | * |
| 153 | mmm frameworks/base/tests/AndroidTests |
| 154 | adb install -r -f out/target/product/passion/data/app/AndroidTests.apk |
| 155 | adb shell am instrument -w -e class com.android.unit_tests.PackageManagerTests com.android.unit_tests/android.test.InstrumentationTestRunner |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 156 | * |
| 157 | * {@hide} |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 158 | */ |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 159 | public class PackageManagerService extends IPackageManager.Stub { |
| 160 | static final String TAG = "PackageManager"; |
| 161 | static final boolean DEBUG_SETTINGS = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | private static final boolean DEBUG_PREFERRED = false; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 163 | static final boolean DEBUG_UPGRADE = false; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 164 | private static final boolean DEBUG_INSTALL = false; |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 165 | private static final boolean DEBUG_REMOVE = false; |
| 166 | private static final boolean DEBUG_SHOW_INFO = false; |
| 167 | private static final boolean DEBUG_PACKAGE_INFO = false; |
| 168 | private static final boolean DEBUG_INTENT_MATCHING = false; |
| 169 | private static final boolean DEBUG_PACKAGE_SCANNING = false; |
| 170 | private static final boolean DEBUG_APP_DIR_OBSERVER = false; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 171 | private static final boolean DEBUG_VERIFY = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | private static final int RADIO_UID = Process.PHONE_UID; |
Mike Lockwood | d42685d | 2009-09-03 09:25:22 -0400 | [diff] [blame] | 174 | private static final int LOG_UID = Process.LOG_UID; |
Nick Pelly | cd0e839 | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 175 | private static final int NFC_UID = Process.NFC_UID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | private static final boolean GET_CERTIFICATES = true; |
| 178 | |
| 179 | private static final int REMOVE_EVENTS = |
| 180 | FileObserver.CLOSE_WRITE | FileObserver.DELETE | FileObserver.MOVED_FROM; |
| 181 | private static final int ADD_EVENTS = |
| 182 | FileObserver.CLOSE_WRITE /*| FileObserver.CREATE*/ | FileObserver.MOVED_TO; |
| 183 | |
| 184 | private static final int OBSERVER_EVENTS = REMOVE_EVENTS | ADD_EVENTS; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 185 | // Suffix used during package installation when copying/moving |
| 186 | // package apks to install directory. |
| 187 | private static final String INSTALL_PACKAGE_SUFFIX = "-"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | |
| 189 | static final int SCAN_MONITOR = 1<<0; |
| 190 | static final int SCAN_NO_DEX = 1<<1; |
| 191 | static final int SCAN_FORCE_DEX = 1<<2; |
| 192 | static final int SCAN_UPDATE_SIGNATURE = 1<<3; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 193 | static final int SCAN_NEW_INSTALL = 1<<4; |
| 194 | static final int SCAN_NO_PATHS = 1<<5; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 195 | static final int SCAN_UPDATE_TIME = 1<<6; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 196 | static final int SCAN_DEFER_DEX = 1<<7; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 198 | static final int REMOVE_CHATTY = 1<<16; |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 199 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Whether verification is enabled by default. |
| 202 | */ |
Brett Chabot | ae05e86 | 2011-08-24 18:38:26 -0700 | [diff] [blame] | 203 | // STOPSHIP: change this to true |
| 204 | private static final boolean DEFAULT_VERIFY_ENABLE = false; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * The default maximum time to wait for the verification agent to return in |
| 208 | * milliseconds. |
| 209 | */ |
| 210 | private static final long DEFAULT_VERIFICATION_TIMEOUT = 60 * 1000; |
| 211 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 212 | static final String DEFAULT_CONTAINER_PACKAGE = "com.android.defcontainer"; |
| 213 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 214 | static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName( |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 215 | DEFAULT_CONTAINER_PACKAGE, |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 216 | "com.android.defcontainer.DefaultContainerService"); |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 217 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 218 | private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive"; |
| 219 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 220 | private static final String LIB_DIR_NAME = "lib"; |
| 221 | |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 222 | static final String mTempContainerPrefix = "smdl2tmp"; |
| 223 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | final HandlerThread mHandlerThread = new HandlerThread("PackageManager", |
| 225 | Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 226 | final PackageHandler mHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 228 | final int mSdkVersion = Build.VERSION.SDK_INT; |
| 229 | final String mSdkCodename = "REL".equals(Build.VERSION.CODENAME) |
| 230 | ? null : Build.VERSION.CODENAME; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | final Context mContext; |
| 233 | final boolean mFactoryTest; |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 234 | final boolean mOnlyCore; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 235 | final boolean mNoDexOpt; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | final DisplayMetrics mMetrics; |
| 237 | final int mDefParseFlags; |
| 238 | final String[] mSeparateProcesses; |
| 239 | |
| 240 | // This is where all application persistent data goes. |
| 241 | final File mAppDataDir; |
| 242 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 243 | // This is where all application persistent data goes for secondary users. |
| 244 | final File mUserAppDataDir; |
| 245 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | // This is the object monitoring the framework dir. |
| 247 | final FileObserver mFrameworkInstallObserver; |
| 248 | |
| 249 | // This is the object monitoring the system app dir. |
| 250 | final FileObserver mSystemInstallObserver; |
| 251 | |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 252 | // This is the object monitoring the system app dir. |
| 253 | final FileObserver mVendorInstallObserver; |
| 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | // This is the object monitoring mAppInstallDir. |
| 256 | final FileObserver mAppInstallObserver; |
| 257 | |
| 258 | // This is the object monitoring mDrmAppPrivateInstallDir. |
| 259 | final FileObserver mDrmAppInstallObserver; |
| 260 | |
| 261 | // Used for priviledge escalation. MUST NOT BE CALLED WITH mPackages |
| 262 | // LOCK HELD. Can be called with mInstallLock held. |
| 263 | final Installer mInstaller; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 264 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | final File mFrameworkDir; |
| 266 | final File mSystemAppDir; |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 267 | final File mVendorAppDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | final File mAppInstallDir; |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 269 | final File mDalvikCacheDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | |
| 271 | // Directory containing the private parts (e.g. code and non-resource assets) of forward-locked |
| 272 | // apps. |
| 273 | final File mDrmAppPrivateInstallDir; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | // ---------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | // Lock for state used when installing and doing other long running |
| 278 | // operations. Methods that must be called with this lock held have |
| 279 | // the prefix "LI". |
| 280 | final Object mInstallLock = new Object(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 281 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | // These are the directories in the 3rd party applications installed dir |
| 283 | // that we have currently loaded packages from. Keys are the application's |
| 284 | // installed zip file (absolute codePath), and values are Package. |
| 285 | final HashMap<String, PackageParser.Package> mAppDirs = |
| 286 | new HashMap<String, PackageParser.Package>(); |
| 287 | |
| 288 | // Information for the parser to write more useful error messages. |
| 289 | File mScanningPath; |
| 290 | int mLastScanError; |
| 291 | |
| 292 | final int[] mOutPermissions = new int[3]; |
| 293 | |
| 294 | // ---------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 295 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | // Keys are String (package name), values are Package. This also serves |
| 297 | // as the lock for the global state. Methods that must be called with |
| 298 | // this lock held have the prefix "LP". |
| 299 | final HashMap<String, PackageParser.Package> mPackages = |
| 300 | new HashMap<String, PackageParser.Package>(); |
| 301 | |
| 302 | final Settings mSettings; |
| 303 | boolean mRestoredSettings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | |
| 305 | // Group-ids that are given to all packages as read from etc/permissions/*.xml. |
| 306 | int[] mGlobalGids; |
| 307 | |
| 308 | // These are the built-in uid -> permission mappings that were read from the |
| 309 | // etc/permissions.xml file. |
| 310 | final SparseArray<HashSet<String>> mSystemPermissions = |
| 311 | new SparseArray<HashSet<String>>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 312 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | // These are the built-in shared libraries that were read from the |
| 314 | // etc/permissions.xml file. |
| 315 | final HashMap<String, String> mSharedLibraries = new HashMap<String, String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 316 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 317 | // Temporary for building the final shared libraries for an .apk. |
| 318 | String[] mTmpSharedLibraries = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 319 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 320 | // These are the features this devices supports that were read from the |
| 321 | // etc/permissions.xml file. |
| 322 | final HashMap<String, FeatureInfo> mAvailableFeatures = |
| 323 | new HashMap<String, FeatureInfo>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | // All available activities, for your resolving pleasure. |
| 326 | final ActivityIntentResolver mActivities = |
| 327 | new ActivityIntentResolver(); |
| 328 | |
| 329 | // All available receivers, for your resolving pleasure. |
| 330 | final ActivityIntentResolver mReceivers = |
| 331 | new ActivityIntentResolver(); |
| 332 | |
| 333 | // All available services, for your resolving pleasure. |
| 334 | final ServiceIntentResolver mServices = new ServiceIntentResolver(); |
| 335 | |
| 336 | // Keys are String (provider class name), values are Provider. |
| 337 | final HashMap<ComponentName, PackageParser.Provider> mProvidersByComponent = |
| 338 | new HashMap<ComponentName, PackageParser.Provider>(); |
| 339 | |
| 340 | // Mapping from provider base names (first directory in content URI codePath) |
| 341 | // to the provider information. |
| 342 | final HashMap<String, PackageParser.Provider> mProviders = |
| 343 | new HashMap<String, PackageParser.Provider>(); |
| 344 | |
| 345 | // Mapping from instrumentation class names to info about them. |
| 346 | final HashMap<ComponentName, PackageParser.Instrumentation> mInstrumentation = |
| 347 | new HashMap<ComponentName, PackageParser.Instrumentation>(); |
| 348 | |
| 349 | // Mapping from permission names to info about them. |
| 350 | final HashMap<String, PackageParser.PermissionGroup> mPermissionGroups = |
| 351 | new HashMap<String, PackageParser.PermissionGroup>(); |
| 352 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 353 | // Packages whose data we have transfered into another package, thus |
| 354 | // should no longer exist. |
| 355 | final HashSet<String> mTransferedPackages = new HashSet<String>(); |
| 356 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 357 | // Broadcast actions that are only available to the system. |
| 358 | final HashSet<String> mProtectedBroadcasts = new HashSet<String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 359 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 360 | /** List of packages waiting for verification. */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 361 | final SparseArray<PackageVerificationState> mPendingVerification |
| 362 | = new SparseArray<PackageVerificationState>(); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 363 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 364 | final ArrayList<PackageParser.Package> mDeferredDexOpt = |
| 365 | new ArrayList<PackageParser.Package>(); |
| 366 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 367 | /** Token for keys in mPendingVerification. */ |
| 368 | private int mPendingVerificationToken = 0; |
| 369 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | boolean mSystemReady; |
| 371 | boolean mSafeMode; |
| 372 | boolean mHasSystemUidErrors; |
| 373 | |
| 374 | ApplicationInfo mAndroidApplication; |
| 375 | final ActivityInfo mResolveActivity = new ActivityInfo(); |
| 376 | final ResolveInfo mResolveInfo = new ResolveInfo(); |
| 377 | ComponentName mResolveComponentName; |
| 378 | PackageParser.Package mPlatformPackage; |
| 379 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 380 | // Set of pending broadcasts for aggregating enable/disable of components. |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 381 | final HashMap<String, ArrayList<String>> mPendingBroadcasts |
| 382 | = new HashMap<String, ArrayList<String>>(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 383 | // Service Connection to remote media container service to copy |
| 384 | // package uri's from external media onto secure containers |
| 385 | // or internal storage. |
| 386 | private IMediaContainerService mContainerService = null; |
| 387 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 388 | static final int SEND_PENDING_BROADCAST = 1; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 389 | static final int MCS_BOUND = 3; |
| 390 | static final int END_COPY = 4; |
| 391 | static final int INIT_COPY = 5; |
| 392 | static final int MCS_UNBIND = 6; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 393 | static final int START_CLEANING_PACKAGE = 7; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 394 | static final int FIND_INSTALL_LOC = 8; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 395 | static final int POST_INSTALL = 9; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 396 | static final int MCS_RECONNECT = 10; |
| 397 | static final int MCS_GIVE_UP = 11; |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 398 | static final int UPDATED_MEDIA_STATUS = 12; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 399 | static final int WRITE_SETTINGS = 13; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 400 | static final int WRITE_PACKAGE_RESTRICTIONS = 14; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 401 | static final int PACKAGE_VERIFIED = 15; |
| 402 | static final int CHECK_PENDING_VERIFICATION = 16; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 403 | |
| 404 | static final int WRITE_SETTINGS_DELAY = 10*1000; // 10 seconds |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 405 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 406 | // Delay time in millisecs |
| 407 | static final int BROADCAST_DELAY = 10 * 1000; |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 408 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 409 | static UserManager sUserManager; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 410 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 411 | // Stores a list of users whose package restrictions file needs to be updated |
| 412 | private HashSet<Integer> mDirtyUsers = new HashSet<Integer>(); |
| 413 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 414 | final private DefaultContainerConnection mDefContainerConn = |
| 415 | new DefaultContainerConnection(); |
| 416 | class DefaultContainerConnection implements ServiceConnection { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 417 | public void onServiceConnected(ComponentName name, IBinder service) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 418 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceConnected"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 419 | IMediaContainerService imcs = |
| 420 | IMediaContainerService.Stub.asInterface(service); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 421 | mHandler.sendMessage(mHandler.obtainMessage(MCS_BOUND, imcs)); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | public void onServiceDisconnected(ComponentName name) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 425 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceDisconnected"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 426 | } |
| 427 | }; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 428 | |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 429 | // Recordkeeping of restore-after-install operations that are currently in flight |
| 430 | // between the Package Manager and the Backup Manager |
| 431 | class PostInstallData { |
| 432 | public InstallArgs args; |
| 433 | public PackageInstalledInfo res; |
| 434 | |
| 435 | PostInstallData(InstallArgs _a, PackageInstalledInfo _r) { |
| 436 | args = _a; |
| 437 | res = _r; |
| 438 | } |
| 439 | }; |
| 440 | final SparseArray<PostInstallData> mRunningInstalls = new SparseArray<PostInstallData>(); |
| 441 | int mNextInstallToken = 1; // nonzero; will be wrapped back to 1 when ++ overflows |
| 442 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 443 | private final String mRequiredVerifierPackage; |
| 444 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 445 | class PackageHandler extends Handler { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 446 | private boolean mBound = false; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 447 | final ArrayList<HandlerParams> mPendingInstalls = |
| 448 | new ArrayList<HandlerParams>(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 449 | |
| 450 | private boolean connectToService() { |
| 451 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Trying to bind to" + |
| 452 | " DefaultContainerService"); |
| 453 | Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 454 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 455 | if (mContext.bindService(service, mDefContainerConn, |
| 456 | Context.BIND_AUTO_CREATE)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 457 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 458 | mBound = true; |
| 459 | return true; |
| 460 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 461 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 462 | return false; |
| 463 | } |
| 464 | |
| 465 | private void disconnectService() { |
| 466 | mContainerService = null; |
| 467 | mBound = false; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 468 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 469 | mContext.unbindService(mDefContainerConn); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 470 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 471 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 472 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 473 | PackageHandler(Looper looper) { |
| 474 | super(looper); |
| 475 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 476 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 477 | public void handleMessage(Message msg) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 478 | try { |
| 479 | doHandleMessage(msg); |
| 480 | } finally { |
| 481 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | void doHandleMessage(Message msg) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 486 | switch (msg.what) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 487 | case INIT_COPY: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 488 | if (DEBUG_INSTALL) Slog.i(TAG, "init_copy"); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 489 | HandlerParams params = (HandlerParams) msg.obj; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 490 | int idx = mPendingInstalls.size(); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 491 | if (DEBUG_INSTALL) Slog.i(TAG, "idx=" + idx); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 492 | // If a bind was already initiated we dont really |
| 493 | // need to do anything. The pending install |
| 494 | // will be processed later on. |
| 495 | if (!mBound) { |
| 496 | // If this is the only one pending we might |
| 497 | // have to bind to the service again. |
| 498 | if (!connectToService()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 499 | Slog.e(TAG, "Failed to bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 500 | params.serviceError(); |
| 501 | return; |
| 502 | } else { |
| 503 | // Once we bind to the service, the first |
| 504 | // pending request will be processed. |
| 505 | mPendingInstalls.add(idx, params); |
| 506 | } |
| 507 | } else { |
| 508 | mPendingInstalls.add(idx, params); |
| 509 | // Already bound to the service. Just make |
| 510 | // sure we trigger off processing the first request. |
| 511 | if (idx == 0) { |
| 512 | mHandler.sendEmptyMessage(MCS_BOUND); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | break; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 516 | } |
| 517 | case MCS_BOUND: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 518 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_bound"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 519 | if (msg.obj != null) { |
| 520 | mContainerService = (IMediaContainerService) msg.obj; |
| 521 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 522 | if (mContainerService == null) { |
| 523 | // Something seriously wrong. Bail out |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 524 | Slog.e(TAG, "Cannot bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 525 | for (HandlerParams params : mPendingInstalls) { |
| 526 | mPendingInstalls.remove(0); |
| 527 | // Indicate service bind error |
| 528 | params.serviceError(); |
| 529 | } |
| 530 | mPendingInstalls.clear(); |
| 531 | } else if (mPendingInstalls.size() > 0) { |
| 532 | HandlerParams params = mPendingInstalls.get(0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 533 | if (params != null) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 534 | if (params.startCopy()) { |
| 535 | // We are done... look for more work or to |
| 536 | // go idle. |
| 537 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 538 | "Checking for more work or unbind..."); |
| 539 | // Delete pending install |
| 540 | if (mPendingInstalls.size() > 0) { |
| 541 | mPendingInstalls.remove(0); |
| 542 | } |
| 543 | if (mPendingInstalls.size() == 0) { |
| 544 | if (mBound) { |
| 545 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 546 | "Posting delayed MCS_UNBIND"); |
| 547 | removeMessages(MCS_UNBIND); |
| 548 | Message ubmsg = obtainMessage(MCS_UNBIND); |
| 549 | // Unbind after a little delay, to avoid |
| 550 | // continual thrashing. |
| 551 | sendMessageDelayed(ubmsg, 10000); |
| 552 | } |
| 553 | } else { |
| 554 | // There are more pending requests in queue. |
| 555 | // Just post MCS_BOUND message to trigger processing |
| 556 | // of next pending install. |
| 557 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 558 | "Posting MCS_BOUND for next woek"); |
| 559 | mHandler.sendEmptyMessage(MCS_BOUND); |
| 560 | } |
| 561 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 562 | } |
| 563 | } else { |
| 564 | // Should never happen ideally. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 565 | Slog.w(TAG, "Empty queue"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 566 | } |
| 567 | break; |
| 568 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 569 | case MCS_RECONNECT: { |
| 570 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_reconnect"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 571 | if (mPendingInstalls.size() > 0) { |
| 572 | if (mBound) { |
| 573 | disconnectService(); |
| 574 | } |
| 575 | if (!connectToService()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 576 | Slog.e(TAG, "Failed to bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 577 | for (HandlerParams params : mPendingInstalls) { |
| 578 | mPendingInstalls.remove(0); |
| 579 | // Indicate service bind error |
| 580 | params.serviceError(); |
| 581 | } |
| 582 | mPendingInstalls.clear(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | break; |
| 586 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 587 | case MCS_UNBIND: { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 588 | // If there is no actual work left, then time to unbind. |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 589 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_unbind"); |
| 590 | |
| 591 | if (mPendingInstalls.size() == 0 && mPendingVerification.size() == 0) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 592 | if (mBound) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 593 | if (DEBUG_INSTALL) Slog.i(TAG, "calling disconnectService()"); |
| 594 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 595 | disconnectService(); |
| 596 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 597 | } else if (mPendingInstalls.size() > 0) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 598 | // There are more pending requests in queue. |
| 599 | // Just post MCS_BOUND message to trigger processing |
| 600 | // of next pending install. |
| 601 | mHandler.sendEmptyMessage(MCS_BOUND); |
| 602 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 603 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 604 | break; |
| 605 | } |
| 606 | case MCS_GIVE_UP: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 607 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_giveup too many retries"); |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 608 | mPendingInstalls.remove(0); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 609 | break; |
| 610 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 611 | case SEND_PENDING_BROADCAST: { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 612 | String packages[]; |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 613 | ArrayList<String> components[]; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 614 | int size = 0; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 615 | int uids[]; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 616 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 617 | synchronized (mPackages) { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 618 | if (mPendingBroadcasts == null) { |
| 619 | return; |
| 620 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 621 | size = mPendingBroadcasts.size(); |
| 622 | if (size <= 0) { |
| 623 | // Nothing to be done. Just return |
| 624 | return; |
| 625 | } |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 626 | packages = new String[size]; |
| 627 | components = new ArrayList[size]; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 628 | uids = new int[size]; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 629 | Iterator<Map.Entry<String, ArrayList<String>>> |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 630 | it = mPendingBroadcasts.entrySet().iterator(); |
| 631 | int i = 0; |
| 632 | while (it.hasNext() && i < size) { |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 633 | Map.Entry<String, ArrayList<String>> ent = it.next(); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 634 | packages[i] = ent.getKey(); |
| 635 | components[i] = ent.getValue(); |
| 636 | PackageSetting ps = mSettings.mPackages.get(ent.getKey()); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 637 | uids[i] = (ps != null) ? ps.appId : -1; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 638 | i++; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 639 | } |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 640 | size = i; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 641 | mPendingBroadcasts.clear(); |
| 642 | } |
| 643 | // Send broadcasts |
| 644 | for (int i = 0; i < size; i++) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 645 | sendPackageChangedBroadcast(packages[i], true, components[i], uids[i]); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 646 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 647 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 648 | break; |
| 649 | } |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 650 | case START_CLEANING_PACKAGE: { |
| 651 | String packageName = (String)msg.obj; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 652 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 653 | synchronized (mPackages) { |
| 654 | if (!mSettings.mPackagesToBeCleaned.contains(packageName)) { |
| 655 | mSettings.mPackagesToBeCleaned.add(packageName); |
| 656 | } |
| 657 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 658 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 659 | startCleaningPackages(); |
| 660 | } break; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 661 | case POST_INSTALL: { |
| 662 | if (DEBUG_INSTALL) Log.v(TAG, "Handling post-install for " + msg.arg1); |
| 663 | PostInstallData data = mRunningInstalls.get(msg.arg1); |
| 664 | mRunningInstalls.delete(msg.arg1); |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 665 | boolean deleteOld = false; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 666 | |
| 667 | if (data != null) { |
| 668 | InstallArgs args = data.args; |
| 669 | PackageInstalledInfo res = data.res; |
| 670 | |
| 671 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 672 | res.removedInfo.sendBroadcast(false, true); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 673 | Bundle extras = new Bundle(1); |
| 674 | extras.putInt(Intent.EXTRA_UID, res.uid); |
| 675 | final boolean update = res.removedInfo.removedPackage != null; |
| 676 | if (update) { |
| 677 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 678 | } |
| 679 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, |
| 680 | res.pkg.applicationInfo.packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 681 | extras, null, null, UserId.USER_ALL); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 682 | if (update) { |
| 683 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, |
| 684 | res.pkg.applicationInfo.packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 685 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 686 | sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, |
| 687 | null, null, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 688 | res.pkg.applicationInfo.packageName, null, |
| 689 | UserId.USER_ALL); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 690 | } |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 691 | if (res.removedInfo.args != null) { |
| 692 | // Remove the replaced package's older resources safely now |
| 693 | deleteOld = true; |
| 694 | } |
| 695 | } |
| 696 | // Force a gc to clear up things |
| 697 | Runtime.getRuntime().gc(); |
| 698 | // We delete after a gc for applications on sdcard. |
| 699 | if (deleteOld) { |
| 700 | synchronized (mInstallLock) { |
| 701 | res.removedInfo.args.doPostDeleteLI(true); |
| 702 | } |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 703 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 704 | if (args.observer != null) { |
| 705 | try { |
| 706 | args.observer.packageInstalled(res.name, res.returnCode); |
| 707 | } catch (RemoteException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 708 | Slog.i(TAG, "Observer no longer exists."); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 712 | Slog.e(TAG, "Bogus post-install token " + msg.arg1); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 713 | } |
| 714 | } break; |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 715 | case UPDATED_MEDIA_STATUS: { |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 716 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Got message UPDATED_MEDIA_STATUS"); |
| 717 | boolean reportStatus = msg.arg1 == 1; |
| 718 | boolean doGc = msg.arg2 == 1; |
| 719 | if (DEBUG_SD_INSTALL) Log.i(TAG, "reportStatus=" + reportStatus + ", doGc = " + doGc); |
| 720 | if (doGc) { |
| 721 | // Force a gc to clear up stale containers. |
| 722 | Runtime.getRuntime().gc(); |
| 723 | } |
| 724 | if (msg.obj != null) { |
Dianne Hackborn | ba24e4d | 2011-09-01 11:17:06 -0700 | [diff] [blame] | 725 | @SuppressWarnings("unchecked") |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 726 | Set<AsecInstallArgs> args = (Set<AsecInstallArgs>) msg.obj; |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 727 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Unloading all containers"); |
| 728 | // Unload containers |
| 729 | unloadAllContainers(args); |
| 730 | } |
| 731 | if (reportStatus) { |
| 732 | try { |
| 733 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Invoking MountService call back"); |
| 734 | PackageHelper.getMountService().finishMediaUpdate(); |
| 735 | } catch (RemoteException e) { |
| 736 | Log.e(TAG, "MountService not running?"); |
| 737 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 738 | } |
| 739 | } break; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 740 | case WRITE_SETTINGS: { |
| 741 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 742 | synchronized (mPackages) { |
| 743 | removeMessages(WRITE_SETTINGS); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 744 | removeMessages(WRITE_PACKAGE_RESTRICTIONS); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 745 | mSettings.writeLPr(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 746 | mDirtyUsers.clear(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 747 | } |
| 748 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 749 | } break; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 750 | case WRITE_PACKAGE_RESTRICTIONS: { |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 751 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 752 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 753 | removeMessages(WRITE_PACKAGE_RESTRICTIONS); |
| 754 | for (int userId : mDirtyUsers) { |
| 755 | mSettings.writePackageRestrictionsLPr(userId); |
| 756 | } |
| 757 | mDirtyUsers.clear(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 758 | } |
| 759 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 760 | } break; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 761 | case CHECK_PENDING_VERIFICATION: { |
| 762 | final int verificationId = msg.arg1; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 763 | final PackageVerificationState state = mPendingVerification.get(verificationId); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 764 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 765 | if (state != null) { |
| 766 | final InstallArgs args = state.getInstallArgs(); |
Kenny Root | a503a0f | 2011-10-03 14:45:28 -0700 | [diff] [blame] | 767 | Slog.i(TAG, "Verification timed out for " + args.packageURI.toString()); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 768 | mPendingVerification.remove(verificationId); |
| 769 | |
| 770 | int ret = PackageManager.INSTALL_FAILED_VERIFICATION_TIMEOUT; |
| 771 | processPendingInstall(args, ret); |
| 772 | |
| 773 | mHandler.sendEmptyMessage(MCS_UNBIND); |
| 774 | } |
| 775 | |
| 776 | break; |
| 777 | } |
| 778 | case PACKAGE_VERIFIED: { |
| 779 | final int verificationId = msg.arg1; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 780 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 781 | final PackageVerificationState state = mPendingVerification.get(verificationId); |
| 782 | if (state == null) { |
Kenny Root | a503a0f | 2011-10-03 14:45:28 -0700 | [diff] [blame] | 783 | Slog.w(TAG, "Invalid verification token " + verificationId + " received"); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 784 | break; |
| 785 | } |
| 786 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 787 | final PackageVerificationResponse response = (PackageVerificationResponse) msg.obj; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 788 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 789 | state.setVerifierResponse(response.callerUid, response.code); |
| 790 | |
| 791 | if (state.isVerificationComplete()) { |
| 792 | mPendingVerification.remove(verificationId); |
| 793 | |
| 794 | final InstallArgs args = state.getInstallArgs(); |
| 795 | |
| 796 | int ret; |
| 797 | if (state.isInstallAllowed()) { |
| 798 | ret = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 799 | try { |
| 800 | ret = args.copyApk(mContainerService, true); |
| 801 | } catch (RemoteException e) { |
| 802 | Slog.e(TAG, "Could not contact the ContainerService"); |
| 803 | } |
| 804 | } else { |
| 805 | ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 806 | } |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 807 | |
| 808 | processPendingInstall(args, ret); |
| 809 | |
| 810 | mHandler.sendEmptyMessage(MCS_UNBIND); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 813 | break; |
| 814 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 818 | |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 819 | void scheduleWriteSettingsLocked() { |
| 820 | if (!mHandler.hasMessages(WRITE_SETTINGS)) { |
| 821 | mHandler.sendEmptyMessageDelayed(WRITE_SETTINGS, WRITE_SETTINGS_DELAY); |
| 822 | } |
| 823 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 824 | |
| 825 | void scheduleWritePackageRestrictionsLocked(int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 826 | if (!sUserManager.exists(userId)) return; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 827 | mDirtyUsers.add(userId); |
| 828 | if (!mHandler.hasMessages(WRITE_PACKAGE_RESTRICTIONS)) { |
| 829 | mHandler.sendEmptyMessageDelayed(WRITE_PACKAGE_RESTRICTIONS, WRITE_SETTINGS_DELAY); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 833 | public static final IPackageManager main(Context context, boolean factoryTest, |
| 834 | boolean onlyCore) { |
| 835 | PackageManagerService m = new PackageManagerService(context, factoryTest, onlyCore); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | ServiceManager.addService("package", m); |
| 837 | return m; |
| 838 | } |
| 839 | |
| 840 | static String[] splitString(String str, char sep) { |
| 841 | int count = 1; |
| 842 | int i = 0; |
| 843 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 844 | count++; |
| 845 | i++; |
| 846 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 847 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | String[] res = new String[count]; |
| 849 | i=0; |
| 850 | count = 0; |
| 851 | int lastI=0; |
| 852 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 853 | res[count] = str.substring(lastI, i); |
| 854 | count++; |
| 855 | i++; |
| 856 | lastI = i; |
| 857 | } |
| 858 | res[count] = str.substring(lastI, str.length()); |
| 859 | return res; |
| 860 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 861 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 862 | public PackageManagerService(Context context, boolean factoryTest, boolean onlyCore) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 863 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_START, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | SystemClock.uptimeMillis()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 865 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | if (mSdkVersion <= 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 867 | Slog.w(TAG, "**** ro.build.version.sdk not set!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 869 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | mContext = context; |
| 871 | mFactoryTest = factoryTest; |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 872 | mOnlyCore = onlyCore; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 873 | mNoDexOpt = "eng".equals(SystemProperties.get("ro.build.type")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | mMetrics = new DisplayMetrics(); |
| 875 | mSettings = new Settings(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 876 | mSettings.addSharedUserLPw("android.uid.system", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | Process.SYSTEM_UID, ApplicationInfo.FLAG_SYSTEM); |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 878 | mSettings.addSharedUserLPw("android.uid.phone", RADIO_UID, ApplicationInfo.FLAG_SYSTEM); |
| 879 | mSettings.addSharedUserLPw("android.uid.log", LOG_UID, ApplicationInfo.FLAG_SYSTEM); |
| 880 | mSettings.addSharedUserLPw("android.uid.nfc", NFC_UID, ApplicationInfo.FLAG_SYSTEM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | |
| 882 | String separateProcesses = SystemProperties.get("debug.separate_processes"); |
| 883 | if (separateProcesses != null && separateProcesses.length() > 0) { |
| 884 | if ("*".equals(separateProcesses)) { |
| 885 | mDefParseFlags = PackageParser.PARSE_IGNORE_PROCESSES; |
| 886 | mSeparateProcesses = null; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 887 | Slog.w(TAG, "Running with debug.separate_processes: * (ALL)"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | } else { |
| 889 | mDefParseFlags = 0; |
| 890 | mSeparateProcesses = separateProcesses.split(","); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 891 | Slog.w(TAG, "Running with debug.separate_processes: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | + separateProcesses); |
| 893 | } |
| 894 | } else { |
| 895 | mDefParseFlags = 0; |
| 896 | mSeparateProcesses = null; |
| 897 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 898 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 899 | mInstaller = new Installer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | |
| 901 | WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); |
| 902 | Display d = wm.getDefaultDisplay(); |
| 903 | d.getMetrics(mMetrics); |
| 904 | |
| 905 | synchronized (mInstallLock) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 906 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | synchronized (mPackages) { |
| 908 | mHandlerThread.start(); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 909 | mHandler = new PackageHandler(mHandlerThread.getLooper()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 910 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | File dataDir = Environment.getDataDirectory(); |
| 912 | mAppDataDir = new File(dataDir, "data"); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 913 | mUserAppDataDir = new File(dataDir, "user"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | mDrmAppPrivateInstallDir = new File(dataDir, "app-private"); |
| 915 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 916 | sUserManager = new UserManager(mInstaller, mUserAppDataDir); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | readPermissions(); |
| 919 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 920 | mRestoredSettings = mSettings.readLPw(getUsers()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | long startTime = SystemClock.uptimeMillis(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 922 | |
| 923 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SYSTEM_SCAN_START, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | startTime); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 925 | |
Suchi Amalapurapu | daec172 | 2010-01-14 21:25:16 -0800 | [diff] [blame] | 926 | // Set flag to monitor and not change apk file paths when |
| 927 | // scanning install directories. |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 928 | int scanMode = SCAN_MONITOR | SCAN_NO_PATHS | SCAN_DEFER_DEX; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 929 | if (mNoDexOpt) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 930 | Slog.w(TAG, "Running ENG build: no pre-dexopt!"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 931 | scanMode |= SCAN_NO_DEX; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 932 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 933 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | final HashSet<String> libFiles = new HashSet<String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 935 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | mFrameworkDir = new File(Environment.getRootDirectory(), "framework"); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 937 | mDalvikCacheDir = new File(dataDir, "dalvik-cache"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 938 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 939 | boolean didDexOpt = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 940 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 941 | /** |
| 942 | * Out of paranoia, ensure that everything in the boot class |
| 943 | * path has been dexed. |
| 944 | */ |
| 945 | String bootClassPath = System.getProperty("java.boot.class.path"); |
| 946 | if (bootClassPath != null) { |
| 947 | String[] paths = splitString(bootClassPath, ':'); |
| 948 | for (int i=0; i<paths.length; i++) { |
| 949 | try { |
| 950 | if (dalvik.system.DexFile.isDexOptNeeded(paths[i])) { |
| 951 | libFiles.add(paths[i]); |
| 952 | mInstaller.dexopt(paths[i], Process.SYSTEM_UID, true); |
| 953 | didDexOpt = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 955 | } catch (FileNotFoundException e) { |
| 956 | Slog.w(TAG, "Boot class path not found: " + paths[i]); |
| 957 | } catch (IOException e) { |
Kenny Root | fec5e75 | 2011-09-23 10:09:58 -0700 | [diff] [blame] | 958 | Slog.w(TAG, "Cannot dexopt " + paths[i] + "; is it an APK or JAR? " |
| 959 | + e.getMessage()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | } |
| 961 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 962 | } else { |
| 963 | Slog.w(TAG, "No BOOTCLASSPATH found!"); |
| 964 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 965 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 966 | /** |
| 967 | * Also ensure all external libraries have had dexopt run on them. |
| 968 | */ |
| 969 | if (mSharedLibraries.size() > 0) { |
| 970 | Iterator<String> libs = mSharedLibraries.values().iterator(); |
| 971 | while (libs.hasNext()) { |
| 972 | String lib = libs.next(); |
| 973 | try { |
| 974 | if (dalvik.system.DexFile.isDexOptNeeded(lib)) { |
| 975 | libFiles.add(lib); |
| 976 | mInstaller.dexopt(lib, Process.SYSTEM_UID, true); |
| 977 | didDexOpt = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 979 | } catch (FileNotFoundException e) { |
| 980 | Slog.w(TAG, "Library not found: " + lib); |
| 981 | } catch (IOException e) { |
Kenny Root | fec5e75 | 2011-09-23 10:09:58 -0700 | [diff] [blame] | 982 | Slog.w(TAG, "Cannot dexopt " + lib + "; is it an APK or JAR? " |
| 983 | + e.getMessage()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | } |
| 985 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 986 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 987 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 988 | // Gross hack for now: we know this file doesn't contain any |
| 989 | // code, so don't dexopt it to avoid the resulting log spew. |
| 990 | libFiles.add(mFrameworkDir.getPath() + "/framework-res.apk"); |
| 991 | |
| 992 | /** |
| 993 | * And there are a number of commands implemented in Java, which |
| 994 | * we currently need to do the dexopt on so that they can be |
| 995 | * run from a non-root shell. |
| 996 | */ |
| 997 | String[] frameworkFiles = mFrameworkDir.list(); |
| 998 | if (frameworkFiles != null) { |
| 999 | for (int i=0; i<frameworkFiles.length; i++) { |
| 1000 | File libPath = new File(mFrameworkDir, frameworkFiles[i]); |
| 1001 | String path = libPath.getPath(); |
| 1002 | // Skip the file if we alrady did it. |
| 1003 | if (libFiles.contains(path)) { |
| 1004 | continue; |
| 1005 | } |
| 1006 | // Skip the file if it is not a type we want to dexopt. |
| 1007 | if (!path.endsWith(".apk") && !path.endsWith(".jar")) { |
| 1008 | continue; |
| 1009 | } |
| 1010 | try { |
| 1011 | if (dalvik.system.DexFile.isDexOptNeeded(path)) { |
| 1012 | mInstaller.dexopt(path, Process.SYSTEM_UID, true); |
| 1013 | didDexOpt = true; |
| 1014 | } |
| 1015 | } catch (FileNotFoundException e) { |
| 1016 | Slog.w(TAG, "Jar not found: " + path); |
| 1017 | } catch (IOException e) { |
| 1018 | Slog.w(TAG, "Exception reading jar: " + path, e); |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | if (didDexOpt) { |
| 1024 | // If we had to do a dexopt of one of the previous |
| 1025 | // things, then something on the system has changed. |
| 1026 | // Consider this significant, and wipe away all other |
| 1027 | // existing dexopt files to ensure we don't leave any |
| 1028 | // dangling around. |
| 1029 | String[] files = mDalvikCacheDir.list(); |
| 1030 | if (files != null) { |
| 1031 | for (int i=0; i<files.length; i++) { |
| 1032 | String fn = files[i]; |
| 1033 | if (fn.startsWith("data@app@") |
| 1034 | || fn.startsWith("data@app-private@")) { |
| 1035 | Slog.i(TAG, "Pruning dalvik file: " + fn); |
| 1036 | (new File(mDalvikCacheDir, fn)).delete(); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1041 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1042 | // Find base frameworks (resource packages without code). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | mFrameworkInstallObserver = new AppDirObserver( |
| 1044 | mFrameworkDir.getPath(), OBSERVER_EVENTS, true); |
| 1045 | mFrameworkInstallObserver.startWatching(); |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 1046 | scanDirLI(mFrameworkDir, PackageParser.PARSE_IS_SYSTEM |
| 1047 | | PackageParser.PARSE_IS_SYSTEM_DIR, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1048 | scanMode | SCAN_NO_DEX, 0); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1049 | |
| 1050 | // Collect all system packages. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | mSystemAppDir = new File(Environment.getRootDirectory(), "app"); |
| 1052 | mSystemInstallObserver = new AppDirObserver( |
| 1053 | mSystemAppDir.getPath(), OBSERVER_EVENTS, true); |
| 1054 | mSystemInstallObserver.startWatching(); |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 1055 | scanDirLI(mSystemAppDir, PackageParser.PARSE_IS_SYSTEM |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1056 | | PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1057 | |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 1058 | // Collect all vendor packages. |
| 1059 | mVendorAppDir = new File("/vendor/app"); |
| 1060 | mVendorInstallObserver = new AppDirObserver( |
| 1061 | mVendorAppDir.getPath(), OBSERVER_EVENTS, true); |
| 1062 | mVendorInstallObserver.startWatching(); |
| 1063 | scanDirLI(mVendorAppDir, PackageParser.PARSE_IS_SYSTEM |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1064 | | PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0); |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 1065 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1066 | if (DEBUG_UPGRADE) Log.v(TAG, "Running installd update commands"); |
| 1067 | mInstaller.moveFiles(); |
| 1068 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1069 | // Prune any system packages that no longer exist. |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1070 | if (!mOnlyCore) { |
| 1071 | Iterator<PackageSetting> psit = mSettings.mPackages.values().iterator(); |
| 1072 | while (psit.hasNext()) { |
| 1073 | PackageSetting ps = psit.next(); |
| 1074 | if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0 |
| 1075 | && !mPackages.containsKey(ps.name) |
| 1076 | && !mSettings.mDisabledSysPackages.containsKey(ps.name)) { |
| 1077 | psit.remove(); |
| 1078 | String msg = "System package " + ps.name |
| 1079 | + " no longer exists; wiping its data"; |
| 1080 | reportSettingsProblem(Log.WARN, msg); |
| 1081 | mInstaller.remove(ps.name, 0); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1082 | sUserManager.removePackageForAllUsers(ps.name); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1083 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | mAppInstallDir = new File(dataDir, "app"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1088 | //look for any incomplete package installations |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1089 | ArrayList<PackageSetting> deletePkgsList = mSettings.getListOfIncompleteInstallPackagesLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1090 | //clean up list |
| 1091 | for(int i = 0; i < deletePkgsList.size(); i++) { |
| 1092 | //clean up here |
| 1093 | cleanupInstallFailedPackage(deletePkgsList.get(i)); |
| 1094 | } |
| 1095 | //delete tmp files |
| 1096 | deleteTempPackageFiles(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1097 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1098 | if (!mOnlyCore) { |
| 1099 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_DATA_SCAN_START, |
| 1100 | SystemClock.uptimeMillis()); |
| 1101 | mAppInstallObserver = new AppDirObserver( |
| 1102 | mAppInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 1103 | mAppInstallObserver.startWatching(); |
| 1104 | scanDirLI(mAppInstallDir, 0, scanMode, 0); |
| 1105 | |
| 1106 | mDrmAppInstallObserver = new AppDirObserver( |
| 1107 | mDrmAppPrivateInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 1108 | mDrmAppInstallObserver.startWatching(); |
| 1109 | scanDirLI(mDrmAppPrivateInstallDir, PackageParser.PARSE_FORWARD_LOCK, |
| 1110 | scanMode, 0); |
| 1111 | } else { |
| 1112 | mAppInstallObserver = null; |
| 1113 | mDrmAppInstallObserver = null; |
| 1114 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1116 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SCAN_END, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | SystemClock.uptimeMillis()); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1118 | Slog.i(TAG, "Time to scan packages: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | + ((SystemClock.uptimeMillis()-startTime)/1000f) |
| 1120 | + " seconds"); |
| 1121 | |
Dianne Hackborn | f22221f | 2010-04-05 18:35:42 -0700 | [diff] [blame] | 1122 | // If the platform SDK has changed since the last time we booted, |
| 1123 | // we need to re-grant app permission to catch any new ones that |
| 1124 | // appear. This is really a hack, and means that apps can in some |
| 1125 | // cases get permissions that the user didn't initially explicitly |
| 1126 | // allow... it would be nice to have some better way to handle |
| 1127 | // this situation. |
| 1128 | final boolean regrantPermissions = mSettings.mInternalSdkPlatform |
| 1129 | != mSdkVersion; |
| 1130 | if (regrantPermissions) Slog.i(TAG, "Platform changed from " |
| 1131 | + mSettings.mInternalSdkPlatform + " to " + mSdkVersion |
| 1132 | + "; regranting permissions for internal storage"); |
| 1133 | mSettings.mInternalSdkPlatform = mSdkVersion; |
| 1134 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 1135 | updatePermissionsLPw(null, null, UPDATE_PERMISSIONS_ALL |
| 1136 | | (regrantPermissions |
| 1137 | ? (UPDATE_PERMISSIONS_REPLACE_PKG|UPDATE_PERMISSIONS_REPLACE_ALL) |
| 1138 | : 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1139 | |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 1140 | // Verify that all of the preferred activity components actually |
| 1141 | // exist. It is possible for applications to be updated and at |
| 1142 | // that point remove a previously declared activity component that |
| 1143 | // had been set as a preferred activity. We try to clean this up |
| 1144 | // the next time we encounter that preferred activity, but it is |
| 1145 | // possible for the user flow to never be able to return to that |
| 1146 | // situation so here we do a sanity check to make sure we haven't |
| 1147 | // left any junk around. |
| 1148 | ArrayList<PreferredActivity> removed = new ArrayList<PreferredActivity>(); |
| 1149 | for (PreferredActivity pa : mSettings.mPreferredActivities.filterSet()) { |
| 1150 | if (mActivities.mActivities.get(pa.mPref.mComponent) == null) { |
| 1151 | removed.add(pa); |
| 1152 | } |
| 1153 | } |
| 1154 | for (int i=0; i<removed.size(); i++) { |
| 1155 | PreferredActivity pa = removed.get(i); |
| 1156 | Slog.w(TAG, "Removing dangling preferred activity: " |
| 1157 | + pa.mPref.mComponent); |
| 1158 | mSettings.mPreferredActivities.removeFilter(pa); |
| 1159 | } |
| 1160 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1161 | // can downgrade to reader |
| 1162 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1164 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | SystemClock.uptimeMillis()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | // Now after opening every single application zip, make sure they |
| 1168 | // are all flushed. Not really needed, but keeps things nice and |
| 1169 | // tidy. |
| 1170 | Runtime.getRuntime().gc(); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1171 | |
| 1172 | mRequiredVerifierPackage = getRequiredVerifierLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | } // synchronized (mPackages) |
| 1174 | } // synchronized (mInstallLock) |
| 1175 | } |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 1176 | |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 1177 | public boolean isFirstBoot() { |
| 1178 | return !mRestoredSettings; |
| 1179 | } |
| 1180 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1181 | private String getRequiredVerifierLPr() { |
| 1182 | final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION); |
| 1183 | final List<ResolveInfo> receivers = queryIntentReceivers(verification, PACKAGE_MIME_TYPE, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1184 | PackageManager.GET_DISABLED_COMPONENTS, 0 /* TODO: Which userId? */); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1185 | |
| 1186 | String requiredVerifier = null; |
| 1187 | |
| 1188 | final int N = receivers.size(); |
| 1189 | for (int i = 0; i < N; i++) { |
| 1190 | final ResolveInfo info = receivers.get(i); |
| 1191 | |
| 1192 | if (info.activityInfo == null) { |
| 1193 | continue; |
| 1194 | } |
| 1195 | |
| 1196 | final String packageName = info.activityInfo.packageName; |
| 1197 | |
| 1198 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1199 | if (ps == null) { |
| 1200 | continue; |
| 1201 | } |
| 1202 | |
| 1203 | if (!ps.grantedPermissions |
| 1204 | .contains(android.Manifest.permission.PACKAGE_VERIFICATION_AGENT)) { |
| 1205 | continue; |
| 1206 | } |
| 1207 | |
| 1208 | if (requiredVerifier != null) { |
| 1209 | throw new RuntimeException("There can be only one required verifier"); |
| 1210 | } |
| 1211 | |
| 1212 | requiredVerifier = packageName; |
| 1213 | } |
| 1214 | |
| 1215 | return requiredVerifier; |
| 1216 | } |
| 1217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1218 | @Override |
| 1219 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1220 | throws RemoteException { |
| 1221 | try { |
| 1222 | return super.onTransact(code, data, reply, flags); |
| 1223 | } catch (RuntimeException e) { |
| 1224 | if (!(e instanceof SecurityException) && !(e instanceof IllegalArgumentException)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1225 | Slog.e(TAG, "Package Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | } |
| 1227 | throw e; |
| 1228 | } |
| 1229 | } |
| 1230 | |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1231 | void cleanupInstallFailedPackage(PackageSetting ps) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1232 | Slog.i(TAG, "Cleaning up incompletely installed app: " + ps.name); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1233 | int retCode = mInstaller.remove(ps.name, 0); |
| 1234 | if (retCode < 0) { |
| 1235 | Slog.w(TAG, "Couldn't remove app data directory for package: " |
| 1236 | + ps.name + ", retcode=" + retCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | } else { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1238 | sUserManager.removePackageForAllUsers(ps.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1239 | } |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1240 | if (ps.codePath != null) { |
| 1241 | if (!ps.codePath.delete()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1242 | Slog.w(TAG, "Unable to remove old code file: " + ps.codePath); |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | if (ps.resourcePath != null) { |
| 1246 | if (!ps.resourcePath.delete() && !ps.resourcePath.equals(ps.codePath)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1247 | Slog.w(TAG, "Unable to remove old code file: " + ps.resourcePath); |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1248 | } |
| 1249 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1250 | mSettings.removePackageLPw(ps.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | void readPermissions() { |
| 1254 | // Read permissions from .../etc/permission directory. |
| 1255 | File libraryDir = new File(Environment.getRootDirectory(), "etc/permissions"); |
| 1256 | if (!libraryDir.exists() || !libraryDir.isDirectory()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1257 | Slog.w(TAG, "No directory " + libraryDir + ", skipping"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | return; |
| 1259 | } |
| 1260 | if (!libraryDir.canRead()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1261 | Slog.w(TAG, "Directory " + libraryDir + " cannot be read"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | return; |
| 1263 | } |
| 1264 | |
| 1265 | // Iterate over the files in the directory and scan .xml files |
| 1266 | for (File f : libraryDir.listFiles()) { |
| 1267 | // We'll read platform.xml last |
| 1268 | if (f.getPath().endsWith("etc/permissions/platform.xml")) { |
| 1269 | continue; |
| 1270 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1271 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1272 | if (!f.getPath().endsWith(".xml")) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1273 | Slog.i(TAG, "Non-xml file " + f + " in " + libraryDir + " directory, ignoring"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | continue; |
| 1275 | } |
| 1276 | if (!f.canRead()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1277 | Slog.w(TAG, "Permissions library file " + f + " cannot be read"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | continue; |
| 1279 | } |
| 1280 | |
| 1281 | readPermissionsFromXml(f); |
| 1282 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1283 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | // Read permissions from .../etc/permissions/platform.xml last so it will take precedence |
| 1285 | final File permFile = new File(Environment.getRootDirectory(), |
| 1286 | "etc/permissions/platform.xml"); |
| 1287 | readPermissionsFromXml(permFile); |
| 1288 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1289 | |
| 1290 | private void readPermissionsFromXml(File permFile) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | FileReader permReader = null; |
| 1292 | try { |
| 1293 | permReader = new FileReader(permFile); |
| 1294 | } catch (FileNotFoundException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1295 | Slog.w(TAG, "Couldn't find or open permissions file " + permFile); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | return; |
| 1297 | } |
| 1298 | |
| 1299 | try { |
| 1300 | XmlPullParser parser = Xml.newPullParser(); |
| 1301 | parser.setInput(permReader); |
| 1302 | |
| 1303 | XmlUtils.beginDocument(parser, "permissions"); |
| 1304 | |
| 1305 | while (true) { |
| 1306 | XmlUtils.nextElement(parser); |
| 1307 | if (parser.getEventType() == XmlPullParser.END_DOCUMENT) { |
| 1308 | break; |
| 1309 | } |
| 1310 | |
| 1311 | String name = parser.getName(); |
| 1312 | if ("group".equals(name)) { |
| 1313 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1314 | if (gidStr != null) { |
| 1315 | int gid = Integer.parseInt(gidStr); |
| 1316 | mGlobalGids = appendInt(mGlobalGids, gid); |
| 1317 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1318 | Slog.w(TAG, "<group> without gid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1319 | + parser.getPositionDescription()); |
| 1320 | } |
| 1321 | |
| 1322 | XmlUtils.skipCurrentTag(parser); |
| 1323 | continue; |
| 1324 | } else if ("permission".equals(name)) { |
| 1325 | String perm = parser.getAttributeValue(null, "name"); |
| 1326 | if (perm == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1327 | Slog.w(TAG, "<permission> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | + parser.getPositionDescription()); |
| 1329 | XmlUtils.skipCurrentTag(parser); |
| 1330 | continue; |
| 1331 | } |
| 1332 | perm = perm.intern(); |
| 1333 | readPermission(parser, perm); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1334 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | } else if ("assign-permission".equals(name)) { |
| 1336 | String perm = parser.getAttributeValue(null, "name"); |
| 1337 | if (perm == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1338 | Slog.w(TAG, "<assign-permission> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | + parser.getPositionDescription()); |
| 1340 | XmlUtils.skipCurrentTag(parser); |
| 1341 | continue; |
| 1342 | } |
| 1343 | String uidStr = parser.getAttributeValue(null, "uid"); |
| 1344 | if (uidStr == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1345 | Slog.w(TAG, "<assign-permission> without uid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | + parser.getPositionDescription()); |
| 1347 | XmlUtils.skipCurrentTag(parser); |
| 1348 | continue; |
| 1349 | } |
| 1350 | int uid = Process.getUidForName(uidStr); |
| 1351 | if (uid < 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1352 | Slog.w(TAG, "<assign-permission> with unknown uid \"" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | + uidStr + "\" at " |
| 1354 | + parser.getPositionDescription()); |
| 1355 | XmlUtils.skipCurrentTag(parser); |
| 1356 | continue; |
| 1357 | } |
| 1358 | perm = perm.intern(); |
| 1359 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 1360 | if (perms == null) { |
| 1361 | perms = new HashSet<String>(); |
| 1362 | mSystemPermissions.put(uid, perms); |
| 1363 | } |
| 1364 | perms.add(perm); |
| 1365 | XmlUtils.skipCurrentTag(parser); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1366 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1367 | } else if ("library".equals(name)) { |
| 1368 | String lname = parser.getAttributeValue(null, "name"); |
| 1369 | String lfile = parser.getAttributeValue(null, "file"); |
| 1370 | if (lname == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1371 | Slog.w(TAG, "<library> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | + parser.getPositionDescription()); |
| 1373 | } else if (lfile == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1374 | Slog.w(TAG, "<library> without file at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | + parser.getPositionDescription()); |
| 1376 | } else { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1377 | //Log.i(TAG, "Got library " + lname + " in " + lfile); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1378 | mSharedLibraries.put(lname, lfile); |
| 1379 | } |
| 1380 | XmlUtils.skipCurrentTag(parser); |
| 1381 | continue; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1382 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1383 | } else if ("feature".equals(name)) { |
| 1384 | String fname = parser.getAttributeValue(null, "name"); |
| 1385 | if (fname == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1386 | Slog.w(TAG, "<feature> without name at " |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1387 | + parser.getPositionDescription()); |
| 1388 | } else { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1389 | //Log.i(TAG, "Got feature " + fname); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1390 | FeatureInfo fi = new FeatureInfo(); |
| 1391 | fi.name = fname; |
| 1392 | mAvailableFeatures.put(fname, fi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | } |
| 1394 | XmlUtils.skipCurrentTag(parser); |
| 1395 | continue; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1396 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | } else { |
| 1398 | XmlUtils.skipCurrentTag(parser); |
| 1399 | continue; |
| 1400 | } |
| 1401 | |
| 1402 | } |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 1403 | permReader.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | } catch (XmlPullParserException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1405 | Slog.w(TAG, "Got execption parsing permissions.", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1407 | Slog.w(TAG, "Got execption parsing permissions.", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | void readPermission(XmlPullParser parser, String name) |
| 1412 | throws IOException, XmlPullParserException { |
| 1413 | |
| 1414 | name = name.intern(); |
| 1415 | |
| 1416 | BasePermission bp = mSettings.mPermissions.get(name); |
| 1417 | if (bp == null) { |
| 1418 | bp = new BasePermission(name, null, BasePermission.TYPE_BUILTIN); |
| 1419 | mSettings.mPermissions.put(name, bp); |
| 1420 | } |
| 1421 | int outerDepth = parser.getDepth(); |
| 1422 | int type; |
| 1423 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 1424 | && (type != XmlPullParser.END_TAG |
| 1425 | || parser.getDepth() > outerDepth)) { |
| 1426 | if (type == XmlPullParser.END_TAG |
| 1427 | || type == XmlPullParser.TEXT) { |
| 1428 | continue; |
| 1429 | } |
| 1430 | |
| 1431 | String tagName = parser.getName(); |
| 1432 | if ("group".equals(tagName)) { |
| 1433 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1434 | if (gidStr != null) { |
| 1435 | int gid = Process.getGidForName(gidStr); |
| 1436 | bp.gids = appendInt(bp.gids, gid); |
| 1437 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1438 | Slog.w(TAG, "<group> without gid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | + parser.getPositionDescription()); |
| 1440 | } |
| 1441 | } |
| 1442 | XmlUtils.skipCurrentTag(parser); |
| 1443 | } |
| 1444 | } |
| 1445 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1446 | static int[] appendInts(int[] cur, int[] add) { |
| 1447 | if (add == null) return cur; |
| 1448 | if (cur == null) return add; |
| 1449 | final int N = add.length; |
| 1450 | for (int i=0; i<N; i++) { |
| 1451 | cur = appendInt(cur, add[i]); |
| 1452 | } |
| 1453 | return cur; |
| 1454 | } |
| 1455 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1456 | static int[] removeInts(int[] cur, int[] rem) { |
| 1457 | if (rem == null) return cur; |
| 1458 | if (cur == null) return cur; |
| 1459 | final int N = rem.length; |
| 1460 | for (int i=0; i<N; i++) { |
| 1461 | cur = removeInt(cur, rem[i]); |
| 1462 | } |
| 1463 | return cur; |
| 1464 | } |
| 1465 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1466 | PackageInfo generatePackageInfo(PackageParser.Package p, int flags, int userId) { |
| 1467 | if (!sUserManager.exists(userId)) return null; |
| 1468 | PackageInfo pi; |
Suchi Amalapurapu | b897cff | 2009-10-14 12:11:48 -0700 | [diff] [blame] | 1469 | if ((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
| 1470 | // The package has been uninstalled but has retained data and resources. |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1471 | pi = PackageParser.generatePackageInfo(p, null, flags, 0, 0, null, false, 0, userId); |
| 1472 | } else { |
| 1473 | final PackageSetting ps = (PackageSetting) p.mExtras; |
| 1474 | if (ps == null) { |
| 1475 | return null; |
| 1476 | } |
| 1477 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 1478 | pi = PackageParser.generatePackageInfo(p, gp.gids, flags, |
| 1479 | ps.firstInstallTime, ps.lastUpdateTime, gp.grantedPermissions, |
| 1480 | ps.getStopped(userId), ps.getEnabled(userId), userId); |
| 1481 | pi.applicationInfo.enabledSetting = ps.getEnabled(userId); |
| 1482 | pi.applicationInfo.enabled = |
| 1483 | pi.applicationInfo.enabledSetting == COMPONENT_ENABLED_STATE_DEFAULT |
| 1484 | || pi.applicationInfo.enabledSetting == COMPONENT_ENABLED_STATE_ENABLED; |
Suchi Amalapurapu | b897cff | 2009-10-14 12:11:48 -0700 | [diff] [blame] | 1485 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1486 | return pi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | } |
| 1488 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1489 | @Override |
| 1490 | public PackageInfo getPackageInfo(String packageName, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1491 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1492 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | synchronized (mPackages) { |
| 1494 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1495 | if (DEBUG_PACKAGE_INFO) |
| 1496 | Log.v(TAG, "getPackageInfo " + packageName + ": " + p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1498 | return generatePackageInfo(p, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | } |
| 1500 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1501 | return generatePackageInfoFromSettingsLPw(packageName, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | return null; |
| 1505 | } |
| 1506 | |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1507 | public String[] currentToCanonicalPackageNames(String[] names) { |
| 1508 | String[] out = new String[names.length]; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1509 | // reader |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1510 | synchronized (mPackages) { |
| 1511 | for (int i=names.length-1; i>=0; i--) { |
| 1512 | PackageSetting ps = mSettings.mPackages.get(names[i]); |
| 1513 | out[i] = ps != null && ps.realName != null ? ps.realName : names[i]; |
| 1514 | } |
| 1515 | } |
| 1516 | return out; |
| 1517 | } |
| 1518 | |
| 1519 | public String[] canonicalToCurrentPackageNames(String[] names) { |
| 1520 | String[] out = new String[names.length]; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1521 | // reader |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1522 | synchronized (mPackages) { |
| 1523 | for (int i=names.length-1; i>=0; i--) { |
| 1524 | String cur = mSettings.mRenamedPackages.get(names[i]); |
| 1525 | out[i] = cur != null ? cur : names[i]; |
| 1526 | } |
| 1527 | } |
| 1528 | return out; |
| 1529 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1530 | |
| 1531 | @Override |
| 1532 | public int getPackageUid(String packageName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1533 | if (!sUserManager.exists(userId)) return -1; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1534 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1535 | synchronized (mPackages) { |
| 1536 | PackageParser.Package p = mPackages.get(packageName); |
| 1537 | if(p != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1538 | return UserId.getUid(userId, p.applicationInfo.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | } |
| 1540 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1541 | if((ps == null) || (ps.pkg == null) || (ps.pkg.applicationInfo == null)) { |
| 1542 | return -1; |
| 1543 | } |
| 1544 | p = ps.pkg; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1545 | return p != null ? UserId.getUid(userId, p.applicationInfo.uid) : -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | public int[] getPackageGids(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1550 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | synchronized (mPackages) { |
| 1552 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1553 | if (DEBUG_PACKAGE_INFO) |
| 1554 | Log.v(TAG, "getPackageGids" + packageName + ": " + p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | if (p != null) { |
| 1556 | final PackageSetting ps = (PackageSetting)p.mExtras; |
| 1557 | final SharedUserSetting suid = ps.sharedUser; |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 1558 | int[] gids = suid != null ? suid.gids : ps.gids; |
| 1559 | |
| 1560 | // include GIDs for any unenforced permissions |
| 1561 | if (!isPermissionEnforcedLocked(READ_EXTERNAL_STORAGE)) { |
| 1562 | final BasePermission basePerm = mSettings.mPermissions.get( |
| 1563 | READ_EXTERNAL_STORAGE); |
| 1564 | gids = appendInts(gids, basePerm.gids); |
| 1565 | } |
| 1566 | |
| 1567 | return gids; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | // stupid thing to indicate an error. |
| 1571 | return new int[0]; |
| 1572 | } |
| 1573 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1574 | static final PermissionInfo generatePermissionInfo( |
| 1575 | BasePermission bp, int flags) { |
| 1576 | if (bp.perm != null) { |
| 1577 | return PackageParser.generatePermissionInfo(bp.perm, flags); |
| 1578 | } |
| 1579 | PermissionInfo pi = new PermissionInfo(); |
| 1580 | pi.name = bp.name; |
| 1581 | pi.packageName = bp.sourcePackage; |
| 1582 | pi.nonLocalizedLabel = bp.name; |
| 1583 | pi.protectionLevel = bp.protectionLevel; |
| 1584 | return pi; |
| 1585 | } |
| 1586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | public PermissionInfo getPermissionInfo(String name, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1588 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | synchronized (mPackages) { |
| 1590 | final BasePermission p = mSettings.mPermissions.get(name); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1591 | if (p != null) { |
| 1592 | return generatePermissionInfo(p, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | } |
| 1594 | return null; |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | public List<PermissionInfo> queryPermissionsByGroup(String group, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1599 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | synchronized (mPackages) { |
| 1601 | ArrayList<PermissionInfo> out = new ArrayList<PermissionInfo>(10); |
| 1602 | for (BasePermission p : mSettings.mPermissions.values()) { |
| 1603 | if (group == null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1604 | if (p.perm == null || p.perm.info.group == null) { |
| 1605 | out.add(generatePermissionInfo(p, flags)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | } |
| 1607 | } else { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1608 | if (p.perm != null && group.equals(p.perm.info.group)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | out.add(PackageParser.generatePermissionInfo(p.perm, flags)); |
| 1610 | } |
| 1611 | } |
| 1612 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1613 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | if (out.size() > 0) { |
| 1615 | return out; |
| 1616 | } |
| 1617 | return mPermissionGroups.containsKey(group) ? out : null; |
| 1618 | } |
| 1619 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | public PermissionGroupInfo getPermissionGroupInfo(String name, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1622 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1623 | synchronized (mPackages) { |
| 1624 | return PackageParser.generatePermissionGroupInfo( |
| 1625 | mPermissionGroups.get(name), flags); |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | public List<PermissionGroupInfo> getAllPermissionGroups(int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1630 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1631 | synchronized (mPackages) { |
| 1632 | final int N = mPermissionGroups.size(); |
| 1633 | ArrayList<PermissionGroupInfo> out |
| 1634 | = new ArrayList<PermissionGroupInfo>(N); |
| 1635 | for (PackageParser.PermissionGroup pg : mPermissionGroups.values()) { |
| 1636 | out.add(PackageParser.generatePermissionGroupInfo(pg, flags)); |
| 1637 | } |
| 1638 | return out; |
| 1639 | } |
| 1640 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1641 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1642 | private ApplicationInfo generateApplicationInfoFromSettingsLPw(String packageName, int flags, |
| 1643 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1644 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1646 | if (ps != null) { |
| 1647 | if (ps.pkg == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1648 | PackageInfo pInfo = generatePackageInfoFromSettingsLPw(packageName, flags, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1649 | if (pInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | return pInfo.applicationInfo; |
| 1651 | } |
| 1652 | return null; |
| 1653 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1654 | return PackageParser.generateApplicationInfo(ps.pkg, flags, ps.getStopped(userId), |
| 1655 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1656 | } |
| 1657 | return null; |
| 1658 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1659 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1660 | private PackageInfo generatePackageInfoFromSettingsLPw(String packageName, int flags, |
| 1661 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1662 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1664 | if (ps != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1665 | PackageParser.Package pkg = new PackageParser.Package(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1666 | if (ps.pkg == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | ps.pkg = new PackageParser.Package(packageName); |
| 1668 | ps.pkg.applicationInfo.packageName = packageName; |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 1669 | ps.pkg.applicationInfo.flags = ps.pkgFlags; |
| 1670 | ps.pkg.applicationInfo.publicSourceDir = ps.resourcePathString; |
| 1671 | ps.pkg.applicationInfo.sourceDir = ps.codePathString; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1672 | ps.pkg.applicationInfo.dataDir = |
| 1673 | getDataPathForPackage(ps.pkg.packageName, 0).getPath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 1674 | ps.pkg.applicationInfo.nativeLibraryDir = ps.nativeLibraryPathString; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1675 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1676 | // ps.pkg.mSetEnabled = ps.getEnabled(userId); |
| 1677 | // ps.pkg.mSetStopped = ps.getStopped(userId); |
| 1678 | return generatePackageInfo(ps.pkg, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1679 | } |
| 1680 | return null; |
| 1681 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1682 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1683 | @Override |
| 1684 | public ApplicationInfo getApplicationInfo(String packageName, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1685 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1686 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1687 | synchronized (mPackages) { |
| 1688 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1689 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | TAG, "getApplicationInfo " + packageName |
| 1691 | + ": " + p); |
| 1692 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1693 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1694 | if (ps == null) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1695 | // Note: isEnabledLP() does not apply here - always return info |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1696 | return PackageParser.generateApplicationInfo(p, flags, ps.getStopped(userId), |
| 1697 | ps.getEnabled(userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | } |
| 1699 | if ("android".equals(packageName)||"system".equals(packageName)) { |
| 1700 | return mAndroidApplication; |
| 1701 | } |
| 1702 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1703 | return generateApplicationInfoFromSettingsLPw(packageName, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1704 | } |
| 1705 | } |
| 1706 | return null; |
| 1707 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1708 | |
| 1709 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1710 | public void freeStorageAndNotify(final long freeStorageSize, final IPackageDataObserver observer) { |
| 1711 | mContext.enforceCallingOrSelfPermission( |
| 1712 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1713 | // Queue up an async operation since clearing cache may take a little while. |
| 1714 | mHandler.post(new Runnable() { |
| 1715 | public void run() { |
| 1716 | mHandler.removeCallbacks(this); |
| 1717 | int retCode = -1; |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1718 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1719 | if (retCode < 0) { |
| 1720 | Slog.w(TAG, "Couldn't clear application caches"); |
| 1721 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | if (observer != null) { |
| 1723 | try { |
| 1724 | observer.onRemoveCompleted(null, (retCode >= 0)); |
| 1725 | } catch (RemoteException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1726 | Slog.w(TAG, "RemoveException when invoking call back"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | }); |
| 1731 | } |
| 1732 | |
Suchi Amalapurapu | bc806f6 | 2009-06-17 15:18:19 -0700 | [diff] [blame] | 1733 | public void freeStorage(final long freeStorageSize, final IntentSender pi) { |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1734 | mContext.enforceCallingOrSelfPermission( |
| 1735 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1736 | // Queue up an async operation since clearing cache may take a little while. |
| 1737 | mHandler.post(new Runnable() { |
| 1738 | public void run() { |
| 1739 | mHandler.removeCallbacks(this); |
| 1740 | int retCode = -1; |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1741 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1742 | if (retCode < 0) { |
| 1743 | Slog.w(TAG, "Couldn't clear application caches"); |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1744 | } |
| 1745 | if(pi != null) { |
| 1746 | try { |
| 1747 | // Callback via pending intent |
| 1748 | int code = (retCode >= 0) ? 1 : 0; |
| 1749 | pi.sendIntent(null, code, null, |
| 1750 | null, null); |
| 1751 | } catch (SendIntentException e1) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1752 | Slog.i(TAG, "Failed to send pending intent"); |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | }); |
| 1757 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1758 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1759 | @Override |
| 1760 | public ActivityInfo getActivityInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1761 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1762 | synchronized (mPackages) { |
| 1763 | PackageParser.Activity a = mActivities.mActivities.get(component); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 1764 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1765 | if (DEBUG_PACKAGE_INFO) Log.v(TAG, "getActivityInfo " + component + ": " + a); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1766 | if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1767 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1768 | if (ps == null) return null; |
| 1769 | return PackageParser.generateActivityInfo(a, flags, ps.getStopped(userId), |
| 1770 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | } |
| 1772 | if (mResolveComponentName.equals(component)) { |
| 1773 | return mResolveActivity; |
| 1774 | } |
| 1775 | } |
| 1776 | return null; |
| 1777 | } |
| 1778 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1779 | @Override |
| 1780 | public ActivityInfo getReceiverInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1781 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1782 | synchronized (mPackages) { |
| 1783 | PackageParser.Activity a = mReceivers.mActivities.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1784 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1785 | TAG, "getReceiverInfo " + component + ": " + a); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1786 | if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1787 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1788 | if (ps == null) return null; |
| 1789 | return PackageParser.generateActivityInfo(a, flags, ps.getStopped(userId), |
| 1790 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | return null; |
| 1794 | } |
| 1795 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1796 | @Override |
| 1797 | public ServiceInfo getServiceInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1798 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1799 | synchronized (mPackages) { |
| 1800 | PackageParser.Service s = mServices.mServices.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1801 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | TAG, "getServiceInfo " + component + ": " + s); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1803 | if (s != null && mSettings.isEnabledLPr(s.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1804 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1805 | if (ps == null) return null; |
| 1806 | return PackageParser.generateServiceInfo(s, flags, ps.getStopped(userId), |
| 1807 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | return null; |
| 1811 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1812 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1813 | @Override |
| 1814 | public ProviderInfo getProviderInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1815 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 1816 | synchronized (mPackages) { |
| 1817 | PackageParser.Provider p = mProvidersByComponent.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1818 | if (DEBUG_PACKAGE_INFO) Log.v( |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 1819 | TAG, "getProviderInfo " + component + ": " + p); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1820 | if (p != null && mSettings.isEnabledLPr(p.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1821 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1822 | if (ps == null) return null; |
| 1823 | return PackageParser.generateProviderInfo(p, flags, ps.getStopped(userId), |
| 1824 | ps.getEnabled(userId), userId); |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | return null; |
| 1828 | } |
| 1829 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1830 | public String[] getSystemSharedLibraryNames() { |
| 1831 | Set<String> libSet; |
| 1832 | synchronized (mPackages) { |
| 1833 | libSet = mSharedLibraries.keySet(); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1834 | int size = libSet.size(); |
| 1835 | if (size > 0) { |
| 1836 | String[] libs = new String[size]; |
| 1837 | libSet.toArray(libs); |
| 1838 | return libs; |
| 1839 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1840 | } |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1841 | return null; |
| 1842 | } |
| 1843 | |
| 1844 | public FeatureInfo[] getSystemAvailableFeatures() { |
| 1845 | Collection<FeatureInfo> featSet; |
| 1846 | synchronized (mPackages) { |
| 1847 | featSet = mAvailableFeatures.values(); |
| 1848 | int size = featSet.size(); |
| 1849 | if (size > 0) { |
| 1850 | FeatureInfo[] features = new FeatureInfo[size+1]; |
| 1851 | featSet.toArray(features); |
| 1852 | FeatureInfo fi = new FeatureInfo(); |
| 1853 | fi.reqGlEsVersion = SystemProperties.getInt("ro.opengles.version", |
| 1854 | FeatureInfo.GL_ES_VERSION_UNDEFINED); |
| 1855 | features[size] = fi; |
| 1856 | return features; |
| 1857 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1858 | } |
| 1859 | return null; |
| 1860 | } |
| 1861 | |
Dianne Hackborn | 039c68e | 2009-09-26 16:39:23 -0700 | [diff] [blame] | 1862 | public boolean hasSystemFeature(String name) { |
| 1863 | synchronized (mPackages) { |
| 1864 | return mAvailableFeatures.containsKey(name); |
| 1865 | } |
| 1866 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1867 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1868 | private void checkValidCaller(int uid, int userId) { |
| 1869 | if (UserId.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) |
| 1870 | return; |
| 1871 | |
| 1872 | throw new SecurityException("Caller uid=" + uid |
| 1873 | + " is not privileged to communicate with user=" + userId); |
| 1874 | } |
| 1875 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1876 | public int checkPermission(String permName, String pkgName) { |
| 1877 | synchronized (mPackages) { |
| 1878 | PackageParser.Package p = mPackages.get(pkgName); |
| 1879 | if (p != null && p.mExtras != null) { |
| 1880 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 1881 | if (ps.sharedUser != null) { |
| 1882 | if (ps.sharedUser.grantedPermissions.contains(permName)) { |
| 1883 | return PackageManager.PERMISSION_GRANTED; |
| 1884 | } |
| 1885 | } else if (ps.grantedPermissions.contains(permName)) { |
| 1886 | return PackageManager.PERMISSION_GRANTED; |
| 1887 | } |
| 1888 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 1889 | if (!isPermissionEnforcedLocked(permName)) { |
| 1890 | return PackageManager.PERMISSION_GRANTED; |
| 1891 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1892 | } |
| 1893 | return PackageManager.PERMISSION_DENIED; |
| 1894 | } |
| 1895 | |
| 1896 | public int checkUidPermission(String permName, int uid) { |
| 1897 | synchronized (mPackages) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1898 | Object obj = mSettings.getUserIdLPr(UserId.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1899 | if (obj != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1900 | GrantedPermissions gp = (GrantedPermissions)obj; |
| 1901 | if (gp.grantedPermissions.contains(permName)) { |
| 1902 | return PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1903 | } |
| 1904 | } else { |
| 1905 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 1906 | if (perms != null && perms.contains(permName)) { |
| 1907 | return PackageManager.PERMISSION_GRANTED; |
| 1908 | } |
| 1909 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 1910 | if (!isPermissionEnforcedLocked(permName)) { |
| 1911 | return PackageManager.PERMISSION_GRANTED; |
| 1912 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1913 | } |
| 1914 | return PackageManager.PERMISSION_DENIED; |
| 1915 | } |
| 1916 | |
| 1917 | private BasePermission findPermissionTreeLP(String permName) { |
| 1918 | for(BasePermission bp : mSettings.mPermissionTrees.values()) { |
| 1919 | if (permName.startsWith(bp.name) && |
| 1920 | permName.length() > bp.name.length() && |
| 1921 | permName.charAt(bp.name.length()) == '.') { |
| 1922 | return bp; |
| 1923 | } |
| 1924 | } |
| 1925 | return null; |
| 1926 | } |
| 1927 | |
| 1928 | private BasePermission checkPermissionTreeLP(String permName) { |
| 1929 | if (permName != null) { |
| 1930 | BasePermission bp = findPermissionTreeLP(permName); |
| 1931 | if (bp != null) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1932 | if (bp.uid == UserId.getAppId(Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1933 | return bp; |
| 1934 | } |
| 1935 | throw new SecurityException("Calling uid " |
| 1936 | + Binder.getCallingUid() |
| 1937 | + " is not allowed to add to permission tree " |
| 1938 | + bp.name + " owned by uid " + bp.uid); |
| 1939 | } |
| 1940 | } |
| 1941 | throw new SecurityException("No permission tree found for " + permName); |
| 1942 | } |
| 1943 | |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 1944 | static boolean compareStrings(CharSequence s1, CharSequence s2) { |
| 1945 | if (s1 == null) { |
| 1946 | return s2 == null; |
| 1947 | } |
| 1948 | if (s2 == null) { |
| 1949 | return false; |
| 1950 | } |
| 1951 | if (s1.getClass() != s2.getClass()) { |
| 1952 | return false; |
| 1953 | } |
| 1954 | return s1.equals(s2); |
| 1955 | } |
| 1956 | |
| 1957 | static boolean comparePermissionInfos(PermissionInfo pi1, PermissionInfo pi2) { |
| 1958 | if (pi1.icon != pi2.icon) return false; |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 1959 | if (pi1.logo != pi2.logo) return false; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 1960 | if (pi1.protectionLevel != pi2.protectionLevel) return false; |
| 1961 | if (!compareStrings(pi1.name, pi2.name)) return false; |
| 1962 | if (!compareStrings(pi1.nonLocalizedLabel, pi2.nonLocalizedLabel)) return false; |
| 1963 | // We'll take care of setting this one. |
| 1964 | if (!compareStrings(pi1.packageName, pi2.packageName)) return false; |
| 1965 | // These are not currently stored in settings. |
| 1966 | //if (!compareStrings(pi1.group, pi2.group)) return false; |
| 1967 | //if (!compareStrings(pi1.nonLocalizedDescription, pi2.nonLocalizedDescription)) return false; |
| 1968 | //if (pi1.labelRes != pi2.labelRes) return false; |
| 1969 | //if (pi1.descriptionRes != pi2.descriptionRes) return false; |
| 1970 | return true; |
| 1971 | } |
| 1972 | |
| 1973 | boolean addPermissionLocked(PermissionInfo info, boolean async) { |
| 1974 | if (info.labelRes == 0 && info.nonLocalizedLabel == null) { |
| 1975 | throw new SecurityException("Label must be specified in permission"); |
| 1976 | } |
| 1977 | BasePermission tree = checkPermissionTreeLP(info.name); |
| 1978 | BasePermission bp = mSettings.mPermissions.get(info.name); |
| 1979 | boolean added = bp == null; |
| 1980 | boolean changed = true; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 1981 | int fixedLevel = PermissionInfo.fixProtectionLevel(info.protectionLevel); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 1982 | if (added) { |
| 1983 | bp = new BasePermission(info.name, tree.sourcePackage, |
| 1984 | BasePermission.TYPE_DYNAMIC); |
| 1985 | } else if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 1986 | throw new SecurityException( |
| 1987 | "Not allowed to modify non-dynamic permission " |
| 1988 | + info.name); |
| 1989 | } else { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 1990 | if (bp.protectionLevel == fixedLevel |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 1991 | && bp.perm.owner.equals(tree.perm.owner) |
| 1992 | && bp.uid == tree.uid |
| 1993 | && comparePermissionInfos(bp.perm.info, info)) { |
| 1994 | changed = false; |
| 1995 | } |
| 1996 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 1997 | bp.protectionLevel = fixedLevel; |
| 1998 | info = new PermissionInfo(info); |
| 1999 | info.protectionLevel = fixedLevel; |
| 2000 | bp.perm = new PackageParser.Permission(tree.perm.owner, info); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2001 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 2002 | bp.uid = tree.uid; |
| 2003 | if (added) { |
| 2004 | mSettings.mPermissions.put(info.name, bp); |
| 2005 | } |
| 2006 | if (changed) { |
| 2007 | if (!async) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2008 | mSettings.writeLPr(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2009 | } else { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2010 | scheduleWriteSettingsLocked(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2011 | } |
| 2012 | } |
| 2013 | return added; |
| 2014 | } |
| 2015 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2016 | public boolean addPermission(PermissionInfo info) { |
| 2017 | synchronized (mPackages) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2018 | return addPermissionLocked(info, false); |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | public boolean addPermissionAsync(PermissionInfo info) { |
| 2023 | synchronized (mPackages) { |
| 2024 | return addPermissionLocked(info, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | public void removePermission(String name) { |
| 2029 | synchronized (mPackages) { |
| 2030 | checkPermissionTreeLP(name); |
| 2031 | BasePermission bp = mSettings.mPermissions.get(name); |
| 2032 | if (bp != null) { |
| 2033 | if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 2034 | throw new SecurityException( |
| 2035 | "Not allowed to modify non-dynamic permission " |
| 2036 | + name); |
| 2037 | } |
| 2038 | mSettings.mPermissions.remove(name); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2039 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2044 | public void grantPermission(String packageName, String permissionName) { |
| 2045 | mContext.enforceCallingOrSelfPermission( |
| 2046 | android.Manifest.permission.GRANT_REVOKE_PERMISSIONS, null); |
| 2047 | synchronized (mPackages) { |
| 2048 | final PackageParser.Package pkg = mPackages.get(packageName); |
| 2049 | if (pkg == null) { |
| 2050 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2051 | } |
| 2052 | final BasePermission bp = mSettings.mPermissions.get(permissionName); |
| 2053 | if (bp == null) { |
| 2054 | throw new IllegalArgumentException("Unknown permission: " + packageName); |
| 2055 | } |
| 2056 | if (!pkg.requestedPermissions.contains(permissionName)) { |
| 2057 | throw new SecurityException("Package " + packageName |
| 2058 | + " has not requested permission " + permissionName); |
| 2059 | } |
| 2060 | if ((bp.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) == 0) { |
| 2061 | throw new SecurityException("Permission " + permissionName |
| 2062 | + " is not a development permission"); |
| 2063 | } |
| 2064 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2065 | if (ps == null) { |
| 2066 | return; |
| 2067 | } |
| 2068 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 2069 | if (gp.grantedPermissions.add(permissionName)) { |
| 2070 | if (ps.haveGids) { |
| 2071 | gp.gids = appendInts(gp.gids, bp.gids); |
| 2072 | } |
| 2073 | mSettings.writeLPr(); |
| 2074 | } |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | public void revokePermission(String packageName, String permissionName) { |
| 2079 | synchronized (mPackages) { |
| 2080 | final PackageParser.Package pkg = mPackages.get(packageName); |
| 2081 | if (pkg == null) { |
| 2082 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2083 | } |
| 2084 | if (pkg.applicationInfo.uid != Binder.getCallingUid()) { |
| 2085 | mContext.enforceCallingOrSelfPermission( |
| 2086 | android.Manifest.permission.GRANT_REVOKE_PERMISSIONS, null); |
| 2087 | } |
| 2088 | final BasePermission bp = mSettings.mPermissions.get(permissionName); |
| 2089 | if (bp == null) { |
| 2090 | throw new IllegalArgumentException("Unknown permission: " + packageName); |
| 2091 | } |
| 2092 | if (!pkg.requestedPermissions.contains(permissionName)) { |
| 2093 | throw new SecurityException("Package " + packageName |
| 2094 | + " has not requested permission " + permissionName); |
| 2095 | } |
| 2096 | if ((bp.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) == 0) { |
| 2097 | throw new SecurityException("Permission " + permissionName |
| 2098 | + " is not a development permission"); |
| 2099 | } |
| 2100 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2101 | if (ps == null) { |
| 2102 | return; |
| 2103 | } |
| 2104 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 2105 | if (gp.grantedPermissions.remove(permissionName)) { |
| 2106 | gp.grantedPermissions.remove(permissionName); |
| 2107 | if (ps.haveGids) { |
| 2108 | gp.gids = removeInts(gp.gids, bp.gids); |
| 2109 | } |
| 2110 | mSettings.writeLPr(); |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 2115 | public boolean isProtectedBroadcast(String actionName) { |
| 2116 | synchronized (mPackages) { |
| 2117 | return mProtectedBroadcasts.contains(actionName); |
| 2118 | } |
| 2119 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2121 | public int checkSignatures(String pkg1, String pkg2) { |
| 2122 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2123 | final PackageParser.Package p1 = mPackages.get(pkg1); |
| 2124 | final PackageParser.Package p2 = mPackages.get(pkg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2125 | if (p1 == null || p1.mExtras == null |
| 2126 | || p2 == null || p2.mExtras == null) { |
| 2127 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2128 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2129 | return compareSignatures(p1.mSignatures, p2.mSignatures); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2133 | public int checkUidSignatures(int uid1, int uid2) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2134 | // Map to base uids. |
| 2135 | uid1 = UserId.getAppId(uid1); |
| 2136 | uid2 = UserId.getAppId(uid2); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2137 | // reader |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2138 | synchronized (mPackages) { |
| 2139 | Signature[] s1; |
| 2140 | Signature[] s2; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2141 | Object obj = mSettings.getUserIdLPr(uid1); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2142 | if (obj != null) { |
| 2143 | if (obj instanceof SharedUserSetting) { |
| 2144 | s1 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 2145 | } else if (obj instanceof PackageSetting) { |
| 2146 | s1 = ((PackageSetting)obj).signatures.mSignatures; |
| 2147 | } else { |
| 2148 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2149 | } |
| 2150 | } else { |
| 2151 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2152 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2153 | obj = mSettings.getUserIdLPr(uid2); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2154 | if (obj != null) { |
| 2155 | if (obj instanceof SharedUserSetting) { |
| 2156 | s2 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 2157 | } else if (obj instanceof PackageSetting) { |
| 2158 | s2 = ((PackageSetting)obj).signatures.mSignatures; |
| 2159 | } else { |
| 2160 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2161 | } |
| 2162 | } else { |
| 2163 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2164 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2165 | return compareSignatures(s1, s2); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2169 | static int compareSignatures(Signature[] s1, Signature[] s2) { |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2170 | if (s1 == null) { |
| 2171 | return s2 == null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2172 | ? PackageManager.SIGNATURE_NEITHER_SIGNED |
| 2173 | : PackageManager.SIGNATURE_FIRST_NOT_SIGNED; |
| 2174 | } |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2175 | if (s2 == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2176 | return PackageManager.SIGNATURE_SECOND_NOT_SIGNED; |
| 2177 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2178 | HashSet<Signature> set1 = new HashSet<Signature>(); |
| 2179 | for (Signature sig : s1) { |
| 2180 | set1.add(sig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2181 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2182 | HashSet<Signature> set2 = new HashSet<Signature>(); |
| 2183 | for (Signature sig : s2) { |
| 2184 | set2.add(sig); |
| 2185 | } |
| 2186 | // Make sure s2 contains all signatures in s1. |
| 2187 | if (set1.equals(set2)) { |
| 2188 | return PackageManager.SIGNATURE_MATCH; |
| 2189 | } |
| 2190 | return PackageManager.SIGNATURE_NO_MATCH; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | public String[] getPackagesForUid(int uid) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2194 | uid = UserId.getAppId(uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2195 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2196 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2197 | Object obj = mSettings.getUserIdLPr(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2198 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2199 | final SharedUserSetting sus = (SharedUserSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2200 | final int N = sus.packages.size(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2201 | final String[] res = new String[N]; |
| 2202 | final Iterator<PackageSetting> it = sus.packages.iterator(); |
| 2203 | int i = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2204 | while (it.hasNext()) { |
| 2205 | res[i++] = it.next().name; |
| 2206 | } |
| 2207 | return res; |
| 2208 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2209 | final PackageSetting ps = (PackageSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2210 | return new String[] { ps.name }; |
| 2211 | } |
| 2212 | } |
| 2213 | return null; |
| 2214 | } |
| 2215 | |
| 2216 | public String getNameForUid(int uid) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2217 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2218 | synchronized (mPackages) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2219 | Object obj = mSettings.getUserIdLPr(UserId.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2220 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2221 | final SharedUserSetting sus = (SharedUserSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2222 | return sus.name + ":" + sus.userId; |
| 2223 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2224 | final PackageSetting ps = (PackageSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2225 | return ps.name; |
| 2226 | } |
| 2227 | } |
| 2228 | return null; |
| 2229 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2231 | public int getUidForSharedUser(String sharedUserName) { |
| 2232 | if(sharedUserName == null) { |
| 2233 | return -1; |
| 2234 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2235 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2236 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2237 | final SharedUserSetting suid = mSettings.getSharedUserLPw(sharedUserName, 0, false); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2238 | if (suid == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2239 | return -1; |
| 2240 | } |
| 2241 | return suid.userId; |
| 2242 | } |
| 2243 | } |
| 2244 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2245 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2246 | public ResolveInfo resolveIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2247 | int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2248 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2249 | List<ResolveInfo> query = queryIntentActivities(intent, resolvedType, flags, userId); |
| 2250 | return chooseBestActivity(intent, resolvedType, flags, query, userId); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 2253 | private ResolveInfo chooseBestActivity(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2254 | int flags, List<ResolveInfo> query, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2255 | if (query != null) { |
| 2256 | final int N = query.size(); |
| 2257 | if (N == 1) { |
| 2258 | return query.get(0); |
| 2259 | } else if (N > 1) { |
| 2260 | // If there is more than one activity with the same priority, |
| 2261 | // then let the user decide between them. |
| 2262 | ResolveInfo r0 = query.get(0); |
| 2263 | ResolveInfo r1 = query.get(1); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 2264 | if (DEBUG_INTENT_MATCHING) { |
| 2265 | Log.d(TAG, r0.activityInfo.name + "=" + r0.priority + " vs " |
| 2266 | + r1.activityInfo.name + "=" + r1.priority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2267 | } |
| 2268 | // If the first activity has a higher priority, or a different |
| 2269 | // default, then it is always desireable to pick it. |
| 2270 | if (r0.priority != r1.priority |
| 2271 | || r0.preferredOrder != r1.preferredOrder |
| 2272 | || r0.isDefault != r1.isDefault) { |
| 2273 | return query.get(0); |
| 2274 | } |
| 2275 | // If we have saved a preference for a preferred activity for |
| 2276 | // this Intent, use that. |
| 2277 | ResolveInfo ri = findPreferredActivity(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2278 | flags, query, r0.priority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2279 | if (ri != null) { |
| 2280 | return ri; |
| 2281 | } |
| 2282 | return mResolveInfo; |
| 2283 | } |
| 2284 | } |
| 2285 | return null; |
| 2286 | } |
| 2287 | |
| 2288 | ResolveInfo findPreferredActivity(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2289 | int flags, List<ResolveInfo> query, int priority, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2290 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2291 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2292 | synchronized (mPackages) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2293 | if (intent.getSelector() != null) { |
| 2294 | intent = intent.getSelector(); |
| 2295 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2296 | if (DEBUG_PREFERRED) intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION); |
| 2297 | List<PreferredActivity> prefs = |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 2298 | mSettings.mPreferredActivities.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2299 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2300 | if (prefs != null && prefs.size() > 0) { |
| 2301 | // First figure out how good the original match set is. |
| 2302 | // We will only allow preferred activities that came |
| 2303 | // from the same match quality. |
| 2304 | int match = 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2305 | |
| 2306 | if (DEBUG_PREFERRED) { |
| 2307 | Log.v(TAG, "Figuring out best match..."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2308 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2309 | |
| 2310 | final int N = query.size(); |
| 2311 | for (int j=0; j<N; j++) { |
| 2312 | final ResolveInfo ri = query.get(j); |
| 2313 | if (DEBUG_PREFERRED) { |
| 2314 | Log.v(TAG, "Match for " + ri.activityInfo + ": 0x" |
| 2315 | + Integer.toHexString(match)); |
| 2316 | } |
| 2317 | if (ri.match > match) { |
| 2318 | match = ri.match; |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | if (DEBUG_PREFERRED) { |
| 2323 | Log.v(TAG, "Best match: 0x" + Integer.toHexString(match)); |
| 2324 | } |
| 2325 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2326 | match &= IntentFilter.MATCH_CATEGORY_MASK; |
| 2327 | final int M = prefs.size(); |
| 2328 | for (int i=0; i<M; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2329 | final PreferredActivity pa = prefs.get(i); |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 2330 | if (pa.mPref.mMatch != match) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2331 | continue; |
| 2332 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2333 | final ActivityInfo ai = getActivityInfo(pa.mPref.mComponent, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2334 | if (DEBUG_PREFERRED) { |
| 2335 | Log.v(TAG, "Got preferred activity:"); |
Marco Nelissen | d85621c | 2010-09-03 09:25:33 -0700 | [diff] [blame] | 2336 | if (ai != null) { |
| 2337 | ai.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 2338 | } else { |
| 2339 | Log.v(TAG, " null"); |
| 2340 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2341 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2342 | if (ai == null) { |
| 2343 | // This previously registered preferred activity |
| 2344 | // component is no longer known. Most likely an update |
| 2345 | // to the app was installed and in the new version this |
| 2346 | // component no longer exists. Clean it up by removing |
| 2347 | // it from the preferred activities list, and skip it. |
| 2348 | Slog.w(TAG, "Removing dangling preferred activity: " |
| 2349 | + pa.mPref.mComponent); |
| 2350 | mSettings.mPreferredActivities.removeFilter(pa); |
| 2351 | continue; |
| 2352 | } |
| 2353 | for (int j=0; j<N; j++) { |
| 2354 | final ResolveInfo ri = query.get(j); |
| 2355 | if (!ri.activityInfo.applicationInfo.packageName |
| 2356 | .equals(ai.applicationInfo.packageName)) { |
| 2357 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2358 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2359 | if (!ri.activityInfo.name.equals(ai.name)) { |
| 2360 | continue; |
| 2361 | } |
| 2362 | |
| 2363 | // Okay we found a previously set preferred app. |
| 2364 | // If the result set is different from when this |
| 2365 | // was created, we need to clear it and re-ask the |
| 2366 | // user their preference. |
| 2367 | if (!pa.mPref.sameSet(query, priority)) { |
| 2368 | Slog.i(TAG, "Result set changed, dropping preferred activity for " |
| 2369 | + intent + " type " + resolvedType); |
| 2370 | mSettings.mPreferredActivities.removeFilter(pa); |
| 2371 | return null; |
| 2372 | } |
| 2373 | |
| 2374 | // Yay! |
| 2375 | return ri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2376 | } |
| 2377 | } |
| 2378 | } |
| 2379 | } |
| 2380 | return null; |
| 2381 | } |
| 2382 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2383 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2384 | public List<ResolveInfo> queryIntentActivities(Intent intent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2385 | String resolvedType, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2386 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2387 | ComponentName comp = intent.getComponent(); |
| 2388 | if (comp == null) { |
| 2389 | if (intent.getSelector() != null) { |
| 2390 | intent = intent.getSelector(); |
| 2391 | comp = intent.getComponent(); |
| 2392 | } |
| 2393 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2395 | if (comp != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2396 | final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2397 | final ActivityInfo ai = getActivityInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2398 | if (ai != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2399 | final ResolveInfo ri = new ResolveInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2400 | ri.activityInfo = ai; |
| 2401 | list.add(ri); |
| 2402 | } |
| 2403 | return list; |
| 2404 | } |
| 2405 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2406 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2407 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2408 | final String pkgName = intent.getPackage(); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2409 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2410 | return mActivities.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2411 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2412 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2413 | if (pkg != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2414 | return mActivities.queryIntentForPackage(intent, resolvedType, flags, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2415 | pkg.activities, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2416 | } |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 2417 | return new ArrayList<ResolveInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2418 | } |
| 2419 | } |
| 2420 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2421 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2422 | public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller, |
| 2423 | Intent[] specifics, String[] specificTypes, Intent intent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2424 | String resolvedType, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2425 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2426 | final String resultsAction = intent.getAction(); |
| 2427 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2428 | List<ResolveInfo> results = queryIntentActivities(intent, resolvedType, flags |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2429 | | PackageManager.GET_RESOLVED_FILTER, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2430 | |
| 2431 | if (DEBUG_INTENT_MATCHING) { |
| 2432 | Log.v(TAG, "Query " + intent + ": " + results); |
| 2433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2434 | |
| 2435 | int specificsPos = 0; |
| 2436 | int N; |
| 2437 | |
| 2438 | // todo: note that the algorithm used here is O(N^2). This |
| 2439 | // isn't a problem in our current environment, but if we start running |
| 2440 | // into situations where we have more than 5 or 10 matches then this |
| 2441 | // should probably be changed to something smarter... |
| 2442 | |
| 2443 | // First we go through and resolve each of the specific items |
| 2444 | // that were supplied, taking care of removing any corresponding |
| 2445 | // duplicate items in the generic resolve list. |
| 2446 | if (specifics != null) { |
| 2447 | for (int i=0; i<specifics.length; i++) { |
| 2448 | final Intent sintent = specifics[i]; |
| 2449 | if (sintent == null) { |
| 2450 | continue; |
| 2451 | } |
| 2452 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2453 | if (DEBUG_INTENT_MATCHING) { |
| 2454 | Log.v(TAG, "Specific #" + i + ": " + sintent); |
| 2455 | } |
| 2456 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2457 | String action = sintent.getAction(); |
| 2458 | if (resultsAction != null && resultsAction.equals(action)) { |
| 2459 | // If this action was explicitly requested, then don't |
| 2460 | // remove things that have it. |
| 2461 | action = null; |
| 2462 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2463 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2464 | ResolveInfo ri = null; |
| 2465 | ActivityInfo ai = null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2466 | |
| 2467 | ComponentName comp = sintent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2468 | if (comp == null) { |
| 2469 | ri = resolveIntent( |
| 2470 | sintent, |
| 2471 | specificTypes != null ? specificTypes[i] : null, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2472 | flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2473 | if (ri == null) { |
| 2474 | continue; |
| 2475 | } |
| 2476 | if (ri == mResolveInfo) { |
| 2477 | // ACK! Must do something better with this. |
| 2478 | } |
| 2479 | ai = ri.activityInfo; |
| 2480 | comp = new ComponentName(ai.applicationInfo.packageName, |
| 2481 | ai.name); |
| 2482 | } else { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2483 | ai = getActivityInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2484 | if (ai == null) { |
| 2485 | continue; |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | // Look for any generic query activities that are duplicates |
| 2490 | // of this specific one, and remove them from the results. |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2491 | if (DEBUG_INTENT_MATCHING) Log.v(TAG, "Specific #" + i + ": " + ai); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2492 | N = results.size(); |
| 2493 | int j; |
| 2494 | for (j=specificsPos; j<N; j++) { |
| 2495 | ResolveInfo sri = results.get(j); |
| 2496 | if ((sri.activityInfo.name.equals(comp.getClassName()) |
| 2497 | && sri.activityInfo.applicationInfo.packageName.equals( |
| 2498 | comp.getPackageName())) |
| 2499 | || (action != null && sri.filter.matchAction(action))) { |
| 2500 | results.remove(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2501 | if (DEBUG_INTENT_MATCHING) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2502 | TAG, "Removing duplicate item from " + j |
| 2503 | + " due to specific " + specificsPos); |
| 2504 | if (ri == null) { |
| 2505 | ri = sri; |
| 2506 | } |
| 2507 | j--; |
| 2508 | N--; |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | // Add this specific item to its proper place. |
| 2513 | if (ri == null) { |
| 2514 | ri = new ResolveInfo(); |
| 2515 | ri.activityInfo = ai; |
| 2516 | } |
| 2517 | results.add(specificsPos, ri); |
| 2518 | ri.specificIndex = i; |
| 2519 | specificsPos++; |
| 2520 | } |
| 2521 | } |
| 2522 | |
| 2523 | // Now we go through the remaining generic results and remove any |
| 2524 | // duplicate actions that are found here. |
| 2525 | N = results.size(); |
| 2526 | for (int i=specificsPos; i<N-1; i++) { |
| 2527 | final ResolveInfo rii = results.get(i); |
| 2528 | if (rii.filter == null) { |
| 2529 | continue; |
| 2530 | } |
| 2531 | |
| 2532 | // Iterate over all of the actions of this result's intent |
| 2533 | // filter... typically this should be just one. |
| 2534 | final Iterator<String> it = rii.filter.actionsIterator(); |
| 2535 | if (it == null) { |
| 2536 | continue; |
| 2537 | } |
| 2538 | while (it.hasNext()) { |
| 2539 | final String action = it.next(); |
| 2540 | if (resultsAction != null && resultsAction.equals(action)) { |
| 2541 | // If this action was explicitly requested, then don't |
| 2542 | // remove things that have it. |
| 2543 | continue; |
| 2544 | } |
| 2545 | for (int j=i+1; j<N; j++) { |
| 2546 | final ResolveInfo rij = results.get(j); |
| 2547 | if (rij.filter != null && rij.filter.hasAction(action)) { |
| 2548 | results.remove(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2549 | if (DEBUG_INTENT_MATCHING) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2550 | TAG, "Removing duplicate item from " + j |
| 2551 | + " due to action " + action + " at " + i); |
| 2552 | j--; |
| 2553 | N--; |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | // If the caller didn't request filter information, drop it now |
| 2559 | // so we don't have to marshall/unmarshall it. |
| 2560 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2561 | rii.filter = null; |
| 2562 | } |
| 2563 | } |
| 2564 | |
| 2565 | // Filter out the caller activity if so requested. |
| 2566 | if (caller != null) { |
| 2567 | N = results.size(); |
| 2568 | for (int i=0; i<N; i++) { |
| 2569 | ActivityInfo ainfo = results.get(i).activityInfo; |
| 2570 | if (caller.getPackageName().equals(ainfo.applicationInfo.packageName) |
| 2571 | && caller.getClassName().equals(ainfo.name)) { |
| 2572 | results.remove(i); |
| 2573 | break; |
| 2574 | } |
| 2575 | } |
| 2576 | } |
| 2577 | |
| 2578 | // If the caller didn't request filter information, |
| 2579 | // drop them now so we don't have to |
| 2580 | // marshall/unmarshall it. |
| 2581 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2582 | N = results.size(); |
| 2583 | for (int i=0; i<N; i++) { |
| 2584 | results.get(i).filter = null; |
| 2585 | } |
| 2586 | } |
| 2587 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2588 | if (DEBUG_INTENT_MATCHING) Log.v(TAG, "Result: " + results); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2589 | return results; |
| 2590 | } |
| 2591 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2592 | @Override |
| 2593 | public List<ResolveInfo> queryIntentReceivers(Intent intent, String resolvedType, int flags, |
| 2594 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2595 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2596 | ComponentName comp = intent.getComponent(); |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2597 | if (comp == null) { |
| 2598 | if (intent.getSelector() != null) { |
| 2599 | intent = intent.getSelector(); |
| 2600 | comp = intent.getComponent(); |
| 2601 | } |
| 2602 | } |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2603 | if (comp != null) { |
| 2604 | List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2605 | ActivityInfo ai = getReceiverInfo(comp, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2606 | if (ai != null) { |
| 2607 | ResolveInfo ri = new ResolveInfo(); |
| 2608 | ri.activityInfo = ai; |
| 2609 | list.add(ri); |
| 2610 | } |
| 2611 | return list; |
| 2612 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2613 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2614 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | synchronized (mPackages) { |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2616 | String pkgName = intent.getPackage(); |
| 2617 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2618 | return mReceivers.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2619 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2620 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2621 | if (pkg != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2622 | return mReceivers.queryIntentForPackage(intent, resolvedType, flags, pkg.receivers, |
| 2623 | userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2624 | } |
| 2625 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2629 | @Override |
| 2630 | public ResolveInfo resolveService(Intent intent, String resolvedType, int flags, int userId) { |
| 2631 | List<ResolveInfo> query = queryIntentServices(intent, resolvedType, flags, userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2632 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2633 | if (query != null) { |
| 2634 | if (query.size() >= 1) { |
| 2635 | // If there is more than one service with the same priority, |
| 2636 | // just arbitrarily pick the first one. |
| 2637 | return query.get(0); |
| 2638 | } |
| 2639 | } |
| 2640 | return null; |
| 2641 | } |
| 2642 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2643 | @Override |
| 2644 | public List<ResolveInfo> queryIntentServices(Intent intent, String resolvedType, int flags, |
| 2645 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2646 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2647 | ComponentName comp = intent.getComponent(); |
| 2648 | if (comp == null) { |
| 2649 | if (intent.getSelector() != null) { |
| 2650 | intent = intent.getSelector(); |
| 2651 | comp = intent.getComponent(); |
| 2652 | } |
| 2653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2654 | if (comp != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2655 | final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2656 | final ServiceInfo si = getServiceInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2657 | if (si != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2658 | final ResolveInfo ri = new ResolveInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2659 | ri.serviceInfo = si; |
| 2660 | list.add(ri); |
| 2661 | } |
| 2662 | return list; |
| 2663 | } |
| 2664 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2665 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2666 | synchronized (mPackages) { |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2667 | String pkgName = intent.getPackage(); |
| 2668 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2669 | return mServices.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2670 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2671 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2672 | if (pkg != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2673 | return mServices.queryIntentForPackage(intent, resolvedType, flags, pkg.services, |
| 2674 | userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2675 | } |
| 2676 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2677 | } |
| 2678 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2679 | |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2680 | private static final int getContinuationPoint(final String[] keys, final String key) { |
| 2681 | final int index; |
| 2682 | if (key == null) { |
| 2683 | index = 0; |
| 2684 | } else { |
| 2685 | final int insertPoint = Arrays.binarySearch(keys, key); |
| 2686 | if (insertPoint < 0) { |
| 2687 | index = -insertPoint; |
| 2688 | } else { |
| 2689 | index = insertPoint + 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2690 | } |
| 2691 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2692 | return index; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2693 | } |
| 2694 | |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2695 | public ParceledListSlice<PackageInfo> getInstalledPackages(int flags, String lastRead) { |
| 2696 | final ParceledListSlice<PackageInfo> list = new ParceledListSlice<PackageInfo>(); |
| 2697 | final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0; |
| 2698 | final String[] keys; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2699 | int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2700 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2701 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2702 | synchronized (mPackages) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2703 | if (listUninstalled) { |
| 2704 | keys = mSettings.mPackages.keySet().toArray(new String[mSettings.mPackages.size()]); |
| 2705 | } else { |
| 2706 | keys = mPackages.keySet().toArray(new String[mPackages.size()]); |
| 2707 | } |
| 2708 | |
| 2709 | Arrays.sort(keys); |
| 2710 | int i = getContinuationPoint(keys, lastRead); |
| 2711 | final int N = keys.length; |
| 2712 | |
| 2713 | while (i < N) { |
| 2714 | final String packageName = keys[i++]; |
| 2715 | |
| 2716 | PackageInfo pi = null; |
| 2717 | if (listUninstalled) { |
| 2718 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
| 2719 | if (ps != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2720 | pi = generatePackageInfoFromSettingsLPw(ps.name, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2721 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2722 | } else { |
| 2723 | final PackageParser.Package p = mPackages.get(packageName); |
| 2724 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2725 | pi = generatePackageInfo(p, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2726 | } |
| 2727 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2728 | |
Henrik Engström | 9771a31 | 2012-02-21 09:05:17 +0100 | [diff] [blame] | 2729 | if (pi != null && list.append(pi)) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2730 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | } |
| 2732 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2733 | |
| 2734 | if (i == N) { |
| 2735 | list.setLastSlice(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2736 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2738 | |
| 2739 | return list; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2740 | } |
| 2741 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2742 | @Override |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2743 | public ParceledListSlice<ApplicationInfo> getInstalledApplications(int flags, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2744 | String lastRead, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2745 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2746 | final ParceledListSlice<ApplicationInfo> list = new ParceledListSlice<ApplicationInfo>(); |
| 2747 | final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0; |
| 2748 | final String[] keys; |
| 2749 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2750 | // writer |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2751 | synchronized (mPackages) { |
| 2752 | if (listUninstalled) { |
| 2753 | keys = mSettings.mPackages.keySet().toArray(new String[mSettings.mPackages.size()]); |
| 2754 | } else { |
| 2755 | keys = mPackages.keySet().toArray(new String[mPackages.size()]); |
| 2756 | } |
| 2757 | |
| 2758 | Arrays.sort(keys); |
| 2759 | int i = getContinuationPoint(keys, lastRead); |
| 2760 | final int N = keys.length; |
| 2761 | |
| 2762 | while (i < N) { |
| 2763 | final String packageName = keys[i++]; |
| 2764 | |
| 2765 | ApplicationInfo ai = null; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2766 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2767 | if (listUninstalled) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2768 | if (ps != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2769 | ai = generateApplicationInfoFromSettingsLPw(ps.name, flags, userId); |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2770 | } |
| 2771 | } else { |
| 2772 | final PackageParser.Package p = mPackages.get(packageName); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2773 | if (p != null && ps != null) { |
| 2774 | ai = PackageParser.generateApplicationInfo(p, flags, ps.getStopped(userId), |
| 2775 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2776 | } |
| 2777 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2778 | |
Henrik Engström | 9771a31 | 2012-02-21 09:05:17 +0100 | [diff] [blame] | 2779 | if (ai != null && list.append(ai)) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2780 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2781 | } |
| 2782 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2783 | |
| 2784 | if (i == N) { |
| 2785 | list.setLastSlice(true); |
| 2786 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2787 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2788 | |
| 2789 | return list; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | public List<ApplicationInfo> getPersistentApplications(int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2793 | final ArrayList<ApplicationInfo> finalList = new ArrayList<ApplicationInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2794 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2795 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2796 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2797 | final Iterator<PackageParser.Package> i = mPackages.values().iterator(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2798 | final int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2799 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2800 | final PackageParser.Package p = i.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2801 | if (p.applicationInfo != null |
| 2802 | && (p.applicationInfo.flags&ApplicationInfo.FLAG_PERSISTENT) != 0 |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 2803 | && (!mSafeMode || isSystemApp(p))) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2804 | PackageSetting ps = mSettings.mPackages.get(p.packageName); |
| 2805 | finalList.add(PackageParser.generateApplicationInfo(p, flags, |
| 2806 | ps != null ? ps.getStopped(userId) : false, |
| 2807 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2808 | userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2809 | } |
| 2810 | } |
| 2811 | } |
| 2812 | |
| 2813 | return finalList; |
| 2814 | } |
| 2815 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2816 | @Override |
| 2817 | public ProviderInfo resolveContentProvider(String name, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2818 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2819 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2820 | synchronized (mPackages) { |
| 2821 | final PackageParser.Provider provider = mProviders.get(name); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2822 | PackageSetting ps = provider != null |
| 2823 | ? mSettings.mPackages.get(provider.owner.packageName) |
| 2824 | : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2825 | return provider != null |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2826 | && mSettings.isEnabledLPr(provider.info, flags, userId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2827 | && (!mSafeMode || (provider.info.applicationInfo.flags |
| 2828 | &ApplicationInfo.FLAG_SYSTEM) != 0) |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2829 | ? PackageParser.generateProviderInfo(provider, flags, |
| 2830 | ps != null ? ps.getStopped(userId) : false, |
| 2831 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2832 | userId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2833 | : null; |
| 2834 | } |
| 2835 | } |
| 2836 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2837 | /** |
| 2838 | * @deprecated |
| 2839 | */ |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 2840 | @Deprecated |
| 2841 | public void querySyncProviders(List<String> outNames, List<ProviderInfo> outInfo) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2842 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2843 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2844 | final Iterator<Map.Entry<String, PackageParser.Provider>> i = mProviders.entrySet() |
| 2845 | .iterator(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2846 | final int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2847 | while (i.hasNext()) { |
| 2848 | Map.Entry<String, PackageParser.Provider> entry = i.next(); |
| 2849 | PackageParser.Provider p = entry.getValue(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2850 | PackageSetting ps = mSettings.mPackages.get(p.owner.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2851 | |
| 2852 | if (p.syncable |
| 2853 | && (!mSafeMode || (p.info.applicationInfo.flags |
| 2854 | &ApplicationInfo.FLAG_SYSTEM) != 0)) { |
| 2855 | outNames.add(entry.getKey()); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2856 | outInfo.add(PackageParser.generateProviderInfo(p, 0, |
| 2857 | ps != null ? ps.getStopped(userId) : false, |
| 2858 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2859 | userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2860 | } |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | |
| 2865 | public List<ProviderInfo> queryContentProviders(String processName, |
| 2866 | int uid, int flags) { |
| 2867 | ArrayList<ProviderInfo> finalList = null; |
| 2868 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2869 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2870 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2871 | final Iterator<PackageParser.Provider> i = mProvidersByComponent.values().iterator(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2872 | final int userId = processName != null ? |
| 2873 | UserId.getUserId(uid) : UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2874 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2875 | final PackageParser.Provider p = i.next(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2876 | PackageSetting ps = mSettings.mPackages.get(p.owner.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2877 | if (p.info.authority != null |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2878 | && (processName == null |
| 2879 | || (p.info.processName.equals(processName) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2880 | && UserId.isSameApp(p.info.applicationInfo.uid, uid))) |
| 2881 | && mSettings.isEnabledLPr(p.info, flags, userId) |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2882 | && (!mSafeMode |
| 2883 | || (p.info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2884 | if (finalList == null) { |
| 2885 | finalList = new ArrayList<ProviderInfo>(3); |
| 2886 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2887 | finalList.add(PackageParser.generateProviderInfo(p, flags, |
| 2888 | ps != null ? ps.getStopped(userId) : false, |
| 2889 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2890 | userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2891 | } |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | if (finalList != null) { |
| 2896 | Collections.sort(finalList, mProviderInitOrderSorter); |
| 2897 | } |
| 2898 | |
| 2899 | return finalList; |
| 2900 | } |
| 2901 | |
| 2902 | public InstrumentationInfo getInstrumentationInfo(ComponentName name, |
| 2903 | int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2904 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2905 | synchronized (mPackages) { |
| 2906 | final PackageParser.Instrumentation i = mInstrumentation.get(name); |
| 2907 | return PackageParser.generateInstrumentationInfo(i, flags); |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | public List<InstrumentationInfo> queryInstrumentation(String targetPackage, |
| 2912 | int flags) { |
| 2913 | ArrayList<InstrumentationInfo> finalList = |
| 2914 | new ArrayList<InstrumentationInfo>(); |
| 2915 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2916 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2917 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2918 | final Iterator<PackageParser.Instrumentation> i = mInstrumentation.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2919 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2920 | final PackageParser.Instrumentation p = i.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2921 | if (targetPackage == null |
| 2922 | || targetPackage.equals(p.info.targetPackage)) { |
| 2923 | finalList.add(PackageParser.generateInstrumentationInfo(p, |
| 2924 | flags)); |
| 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | return finalList; |
| 2930 | } |
| 2931 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 2932 | private void scanDirLI(File dir, int flags, int scanMode, long currentTime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2933 | String[] files = dir.list(); |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 2934 | if (files == null) { |
| 2935 | Log.d(TAG, "No files in app dir " + dir); |
| 2936 | return; |
| 2937 | } |
| 2938 | |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 2939 | if (DEBUG_PACKAGE_SCANNING) { |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 2940 | Log.d(TAG, "Scanning app dir " + dir); |
| 2941 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2942 | |
| 2943 | int i; |
| 2944 | for (i=0; i<files.length; i++) { |
| 2945 | File file = new File(dir, files[i]); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 2946 | if (!isPackageFilename(files[i])) { |
| 2947 | // Ignore entries which are not apk's |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 2948 | continue; |
| 2949 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 2950 | PackageParser.Package pkg = scanPackageLI(file, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 2951 | flags|PackageParser.PARSE_MUST_BE_APK, scanMode, currentTime); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 2952 | // Don't mess around with apps in system partition. |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 2953 | if (pkg == null && (flags & PackageParser.PARSE_IS_SYSTEM) == 0 && |
| 2954 | mLastScanError == PackageManager.INSTALL_FAILED_INVALID_APK) { |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 2955 | // Delete the apk |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 2956 | Slog.w(TAG, "Cleaning up failed install of " + file); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 2957 | file.delete(); |
| 2958 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2959 | } |
| 2960 | } |
| 2961 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2962 | private static File getSettingsProblemFile() { |
| 2963 | File dataDir = Environment.getDataDirectory(); |
| 2964 | File systemDir = new File(dataDir, "system"); |
| 2965 | File fname = new File(systemDir, "uiderrors.txt"); |
| 2966 | return fname; |
| 2967 | } |
| 2968 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 2969 | static void reportSettingsProblem(int priority, String msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2970 | try { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2971 | File fname = getSettingsProblemFile(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2972 | FileOutputStream out = new FileOutputStream(fname, true); |
| 2973 | PrintWriter pw = new PrintWriter(out); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 2974 | SimpleDateFormat formatter = new SimpleDateFormat(); |
| 2975 | String dateString = formatter.format(new Date(System.currentTimeMillis())); |
| 2976 | pw.println(dateString + ": " + msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2977 | pw.close(); |
| 2978 | FileUtils.setPermissions( |
| 2979 | fname.toString(), |
| 2980 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IROTH, |
| 2981 | -1, -1); |
| 2982 | } catch (java.io.IOException e) { |
| 2983 | } |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 2984 | Slog.println(priority, TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | private boolean collectCertificatesLI(PackageParser pp, PackageSetting ps, |
| 2988 | PackageParser.Package pkg, File srcFile, int parseFlags) { |
| 2989 | if (GET_CERTIFICATES) { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 2990 | if (ps != null |
| 2991 | && ps.codePath.equals(srcFile) |
Kenny Root | 7d794fb | 2010-09-13 16:29:49 -0700 | [diff] [blame] | 2992 | && ps.timeStamp == srcFile.lastModified()) { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 2993 | if (ps.signatures.mSignatures != null |
| 2994 | && ps.signatures.mSignatures.length != 0) { |
| 2995 | // Optimization: reuse the existing cached certificates |
| 2996 | // if the package appears to be unchanged. |
| 2997 | pkg.mSignatures = ps.signatures.mSignatures; |
| 2998 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2999 | } |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3000 | |
| 3001 | Slog.w(TAG, "PackageSetting for " + ps.name + " is missing signatures. Collecting certs again to recover them."); |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3002 | } else { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3003 | Log.i(TAG, srcFile.toString() + " changed; collecting certs"); |
| 3004 | } |
| 3005 | |
| 3006 | if (!pp.collectCertificates(pkg, parseFlags)) { |
| 3007 | mLastScanError = pp.getParseError(); |
| 3008 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | return true; |
| 3012 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3013 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3014 | /* |
| 3015 | * Scan a package and return the newly parsed package. |
| 3016 | * Returns null in case of errors and the error code is stored in mLastScanError |
| 3017 | */ |
| 3018 | private PackageParser.Package scanPackageLI(File scanFile, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3019 | int parseFlags, int scanMode, long currentTime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3020 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3021 | String scanPath = scanFile.getPath(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3022 | parseFlags |= mDefParseFlags; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3023 | PackageParser pp = new PackageParser(scanPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3024 | pp.setSeparateProcesses(mSeparateProcesses); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 3025 | pp.setOnlyCoreApps(mOnlyCore); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3026 | final PackageParser.Package pkg = pp.parsePackage(scanFile, |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3027 | scanPath, mMetrics, parseFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3028 | if (pkg == null) { |
| 3029 | mLastScanError = pp.getParseError(); |
| 3030 | return null; |
| 3031 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3032 | PackageSetting ps = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3033 | PackageSetting updatedPkg; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3034 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3035 | synchronized (mPackages) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3036 | // Look to see if we already know about this package. |
| 3037 | String oldName = mSettings.mRenamedPackages.get(pkg.packageName); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3038 | if (pkg.mOriginalPackages != null && pkg.mOriginalPackages.contains(oldName)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3039 | // This package has been renamed to its original name. Let's |
| 3040 | // use that. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3041 | ps = mSettings.peekPackageLPr(oldName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3042 | } |
| 3043 | // If there was no original package, see one for the real package name. |
| 3044 | if (ps == null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3045 | ps = mSettings.peekPackageLPr(pkg.packageName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3046 | } |
| 3047 | // Check to see if this package could be hiding/updating a system |
| 3048 | // package. Must look for it either under the original or real |
| 3049 | // package name depending on our state. |
| 3050 | updatedPkg = mSettings.mDisabledSysPackages.get( |
| 3051 | ps != null ? ps.name : pkg.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3052 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3053 | // First check if this is a system package that may involve an update |
| 3054 | if (updatedPkg != null && (parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
Kenny Root | 9ee9274 | 2010-09-01 13:40:57 -0700 | [diff] [blame] | 3055 | if (ps != null && !ps.codePath.equals(scanFile)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3056 | // The path has changed from what was last scanned... check the |
| 3057 | // version of the new path against what we have stored to determine |
| 3058 | // what to do. |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3059 | if (pkg.mVersionCode < ps.versionCode) { |
| 3060 | // 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] | 3061 | // Ignore entry. Skip it. |
| 3062 | Log.i(TAG, "Package " + ps.name + " at " + scanFile |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 3063 | + " ignored: updated version " + ps.versionCode |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3064 | + " better than this " + pkg.mVersionCode); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3065 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3066 | return null; |
| 3067 | } else { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3068 | // The current app on the system partion is better than |
| 3069 | // what we have updated to on the data partition; switch |
| 3070 | // back to the system partition version. |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3071 | // At this point, its safely assumed that package installation for |
| 3072 | // apps in system partition will go through. If not there won't be a working |
| 3073 | // version of the app |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3074 | // writer |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3075 | synchronized (mPackages) { |
| 3076 | // Just remove the loaded entries from package lists. |
| 3077 | mPackages.remove(ps.name); |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 3078 | } |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3079 | Slog.w(TAG, "Package " + ps.name + " at " + scanFile |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3080 | + "reverting from " + ps.codePathString |
| 3081 | + ": new version " + pkg.mVersionCode |
| 3082 | + " better than installed " + ps.versionCode); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3083 | InstallArgs args = new FileInstallArgs(ps.codePathString, |
| 3084 | ps.resourcePathString, ps.nativeLibraryPathString); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3085 | args.cleanUpResourcesLI(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3086 | mSettings.enableSystemPackageLPw(ps.name); |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 3087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3088 | } |
| 3089 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3090 | if (updatedPkg != null) { |
| 3091 | // An updated system app will not have the PARSE_IS_SYSTEM flag set initially |
| 3092 | parseFlags |= PackageParser.PARSE_IS_SYSTEM; |
| 3093 | } |
| 3094 | // Verify certificates against what was last scanned |
| 3095 | if (!collectCertificatesLI(pp, ps, pkg, scanFile, parseFlags)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3096 | Slog.w(TAG, "Failed verifying certificates for package:" + pkg.packageName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3097 | return null; |
| 3098 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3099 | // The apk is forward locked (not public) if its code and resources |
| 3100 | // are kept in different files. |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3101 | // TODO grab this value from PackageSettings |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3102 | if (ps != null && !ps.codePath.equals(ps.resourcePath)) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3103 | parseFlags |= PackageParser.PARSE_FORWARD_LOCK; |
Suchi Amalapurapu | f2c1072 | 2009-07-29 17:19:39 -0700 | [diff] [blame] | 3104 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3105 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3106 | String codePath = null; |
| 3107 | String resPath = null; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3108 | if ((parseFlags & PackageParser.PARSE_FORWARD_LOCK) != 0) { |
| 3109 | if (ps != null && ps.resourcePathString != null) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3110 | resPath = ps.resourcePathString; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3111 | } else { |
| 3112 | // Should not happen at all. Just log an error. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3113 | Slog.e(TAG, "Resource path not set for pkg : " + pkg.packageName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3114 | } |
| 3115 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3116 | resPath = pkg.mScanPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3117 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3118 | codePath = pkg.mScanPath; |
| 3119 | // Set application objects path explicitly. |
| 3120 | setApplicationInfoPaths(pkg, codePath, resPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3121 | // Note that we invoke the following method only if we are about to unpack an application |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3122 | return scanPackageLI(pkg, parseFlags, scanMode | SCAN_UPDATE_SIGNATURE, currentTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3123 | } |
| 3124 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3125 | private static void setApplicationInfoPaths(PackageParser.Package pkg, String destCodePath, |
| 3126 | String destResPath) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3127 | pkg.mPath = pkg.mScanPath = destCodePath; |
| 3128 | pkg.applicationInfo.sourceDir = destCodePath; |
| 3129 | pkg.applicationInfo.publicSourceDir = destResPath; |
| 3130 | } |
| 3131 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3132 | private static String fixProcessName(String defProcessName, |
| 3133 | String processName, int uid) { |
| 3134 | if (processName == null) { |
| 3135 | return defProcessName; |
| 3136 | } |
| 3137 | return processName; |
| 3138 | } |
| 3139 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3140 | private boolean verifySignaturesLP(PackageSetting pkgSetting, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3141 | PackageParser.Package pkg) { |
| 3142 | if (pkgSetting.signatures.mSignatures != null) { |
| 3143 | // Already existing package. Make sure signatures match |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3144 | if (compareSignatures(pkgSetting.signatures.mSignatures, pkg.mSignatures) != |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3145 | PackageManager.SIGNATURE_MATCH) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3146 | Slog.e(TAG, "Package " + pkg.packageName |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3147 | + " signatures do not match the previously installed version; ignoring!"); |
| 3148 | mLastScanError = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3149 | return false; |
| 3150 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3151 | } |
| 3152 | // Check for shared user signatures |
| 3153 | if (pkgSetting.sharedUser != null && pkgSetting.sharedUser.signatures.mSignatures != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3154 | if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3155 | pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) { |
| 3156 | Slog.e(TAG, "Package " + pkg.packageName |
| 3157 | + " has no signatures that match those in shared user " |
| 3158 | + pkgSetting.sharedUser.name + "; ignoring!"); |
| 3159 | mLastScanError = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; |
| 3160 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3161 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3162 | } |
| 3163 | return true; |
| 3164 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3165 | |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 3166 | /** |
| 3167 | * Enforces that only the system UID or root's UID can call a method exposed |
| 3168 | * via Binder. |
| 3169 | * |
| 3170 | * @param message used as message if SecurityException is thrown |
| 3171 | * @throws SecurityException if the caller is not system or root |
| 3172 | */ |
| 3173 | private static final void enforceSystemOrRoot(String message) { |
| 3174 | final int uid = Binder.getCallingUid(); |
| 3175 | if (uid != Process.SYSTEM_UID && uid != 0) { |
| 3176 | throw new SecurityException(message); |
| 3177 | } |
| 3178 | } |
| 3179 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3180 | public void performBootDexOpt() { |
| 3181 | ArrayList<PackageParser.Package> pkgs = null; |
| 3182 | synchronized (mPackages) { |
| 3183 | if (mDeferredDexOpt.size() > 0) { |
| 3184 | pkgs = new ArrayList<PackageParser.Package>(mDeferredDexOpt); |
| 3185 | mDeferredDexOpt.clear(); |
| 3186 | } |
| 3187 | } |
| 3188 | if (pkgs != null) { |
| 3189 | for (int i=0; i<pkgs.size(); i++) { |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 3190 | if (!isFirstBoot()) { |
| 3191 | try { |
| 3192 | ActivityManagerNative.getDefault().showBootMessage( |
| 3193 | mContext.getResources().getString( |
| 3194 | com.android.internal.R.string.android_upgrading_apk, |
| 3195 | i+1, pkgs.size()), true); |
| 3196 | } catch (RemoteException e) { |
| 3197 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3198 | } |
| 3199 | PackageParser.Package p = pkgs.get(i); |
| 3200 | synchronized (mInstallLock) { |
| 3201 | if (!p.mDidDexOpt) { |
| 3202 | performDexOptLI(p, false, false); |
| 3203 | } |
| 3204 | } |
| 3205 | } |
| 3206 | } |
| 3207 | } |
| 3208 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3209 | public boolean performDexOpt(String packageName) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 3210 | enforceSystemOrRoot("Only the system can request dexopt be performed"); |
| 3211 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3212 | if (!mNoDexOpt) { |
| 3213 | return false; |
| 3214 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3215 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3216 | PackageParser.Package p; |
| 3217 | synchronized (mPackages) { |
| 3218 | p = mPackages.get(packageName); |
| 3219 | if (p == null || p.mDidDexOpt) { |
| 3220 | return false; |
| 3221 | } |
| 3222 | } |
| 3223 | synchronized (mInstallLock) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3224 | return performDexOptLI(p, false, false) == DEX_OPT_PERFORMED; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3225 | } |
| 3226 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3227 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3228 | static final int DEX_OPT_SKIPPED = 0; |
| 3229 | static final int DEX_OPT_PERFORMED = 1; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3230 | static final int DEX_OPT_DEFERRED = 2; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3231 | static final int DEX_OPT_FAILED = -1; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3232 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3233 | private int performDexOptLI(PackageParser.Package pkg, boolean forceDex, boolean defer) { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3234 | boolean performed = false; |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3235 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3236 | String path = pkg.mScanPath; |
| 3237 | int ret = 0; |
| 3238 | try { |
| 3239 | if (forceDex || dalvik.system.DexFile.isDexOptNeeded(path)) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3240 | if (!forceDex && defer) { |
| 3241 | mDeferredDexOpt.add(pkg); |
| 3242 | return DEX_OPT_DEFERRED; |
| 3243 | } else { |
| 3244 | Log.i(TAG, "Running dexopt on: " + pkg.applicationInfo.packageName); |
| 3245 | ret = mInstaller.dexopt(path, pkg.applicationInfo.uid, |
| 3246 | !isForwardLocked(pkg)); |
| 3247 | pkg.mDidDexOpt = true; |
| 3248 | performed = true; |
| 3249 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3250 | } |
| 3251 | } catch (FileNotFoundException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3252 | Slog.w(TAG, "Apk not found for dexopt: " + path); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3253 | ret = -1; |
| 3254 | } catch (IOException e) { |
Suchi Amalapurapu | 73dafa1 | 2010-04-01 16:31:31 -0700 | [diff] [blame] | 3255 | Slog.w(TAG, "IOException reading apk: " + path, e); |
| 3256 | ret = -1; |
| 3257 | } catch (dalvik.system.StaleDexCacheError e) { |
| 3258 | Slog.w(TAG, "StaleDexCacheError when reading apk: " + path, e); |
| 3259 | ret = -1; |
| 3260 | } catch (Exception e) { |
| 3261 | Slog.w(TAG, "Exception when doing dexopt : ", e); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3262 | ret = -1; |
| 3263 | } |
| 3264 | if (ret < 0) { |
| 3265 | //error from installer |
| 3266 | return DEX_OPT_FAILED; |
| 3267 | } |
| 3268 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3269 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3270 | return performed ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 3271 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 3272 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3273 | private boolean verifyPackageUpdateLPr(PackageSetting oldPkg, PackageParser.Package newPkg) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3274 | if ((oldPkg.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3275 | Slog.w(TAG, "Unable to update from " + oldPkg.name |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3276 | + " to " + newPkg.packageName |
| 3277 | + ": old package not in system partition"); |
| 3278 | return false; |
| 3279 | } else if (mPackages.get(oldPkg.name) != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3280 | Slog.w(TAG, "Unable to update from " + oldPkg.name |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3281 | + " to " + newPkg.packageName |
| 3282 | + ": old package still exists"); |
| 3283 | return false; |
| 3284 | } |
| 3285 | return true; |
| 3286 | } |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 3287 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3288 | File getDataPathForUser(int userId) { |
| 3289 | return new File(mUserAppDataDir.getAbsolutePath() + File.separator + userId); |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 3290 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3291 | |
| 3292 | private File getDataPathForPackage(String packageName, int userId) { |
Kenny Root | 9c8625e | 2011-07-25 09:42:22 -0700 | [diff] [blame] | 3293 | /* |
| 3294 | * Until we fully support multiple users, return the directory we |
| 3295 | * previously would have. The PackageManagerTests will need to be |
| 3296 | * revised when this is changed back.. |
| 3297 | */ |
| 3298 | if (userId == 0) { |
| 3299 | return new File(mAppDataDir, packageName); |
| 3300 | } else { |
| 3301 | return new File(mUserAppDataDir.getAbsolutePath() + File.separator + userId |
Amith Yamasani | 0f3f29b | 2011-05-09 14:17:14 -0700 | [diff] [blame] | 3302 | + File.separator + packageName); |
Kenny Root | 9c8625e | 2011-07-25 09:42:22 -0700 | [diff] [blame] | 3303 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3304 | } |
| 3305 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3306 | private PackageParser.Package scanPackageLI(PackageParser.Package pkg, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3307 | int parseFlags, int scanMode, long currentTime) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3308 | File scanFile = new File(pkg.mScanPath); |
Suchi Amalapurapu | 7040ce7 | 2010-02-08 23:55:56 -0800 | [diff] [blame] | 3309 | if (scanFile == null || pkg.applicationInfo.sourceDir == null || |
| 3310 | pkg.applicationInfo.publicSourceDir == null) { |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3311 | // Bail out. The resource and code paths haven't been set. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3312 | Slog.w(TAG, " Code and resource paths haven't been set correctly"); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3313 | mLastScanError = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 3314 | return null; |
| 3315 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3316 | mScanningPath = scanFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3318 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
| 3319 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 3320 | } |
| 3321 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3322 | if (pkg.packageName.equals("android")) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3323 | synchronized (mPackages) { |
| 3324 | if (mAndroidApplication != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3325 | Slog.w(TAG, "*************************************************"); |
| 3326 | Slog.w(TAG, "Core android package being redefined. Skipping."); |
| 3327 | Slog.w(TAG, " file=" + mScanningPath); |
| 3328 | Slog.w(TAG, "*************************************************"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3329 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3330 | return null; |
| 3331 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3333 | // Set up information for our fall-back user intent resolution |
| 3334 | // activity. |
| 3335 | mPlatformPackage = pkg; |
| 3336 | pkg.mVersionCode = mSdkVersion; |
| 3337 | mAndroidApplication = pkg.applicationInfo; |
| 3338 | mResolveActivity.applicationInfo = mAndroidApplication; |
| 3339 | mResolveActivity.name = ResolverActivity.class.getName(); |
| 3340 | mResolveActivity.packageName = mAndroidApplication.packageName; |
| 3341 | mResolveActivity.processName = mAndroidApplication.processName; |
| 3342 | mResolveActivity.launchMode = ActivityInfo.LAUNCH_MULTIPLE; |
| 3343 | mResolveActivity.flags = ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS; |
Joe Onorato | 646f46e | 2010-11-08 15:08:56 -0800 | [diff] [blame] | 3344 | mResolveActivity.theme = com.android.internal.R.style.Theme_Holo_Dialog_Alert; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3345 | mResolveActivity.exported = true; |
| 3346 | mResolveActivity.enabled = true; |
| 3347 | mResolveInfo.activityInfo = mResolveActivity; |
| 3348 | mResolveInfo.priority = 0; |
| 3349 | mResolveInfo.preferredOrder = 0; |
| 3350 | mResolveInfo.match = 0; |
| 3351 | mResolveComponentName = new ComponentName( |
| 3352 | mAndroidApplication.packageName, mResolveActivity.name); |
| 3353 | } |
| 3354 | } |
| 3355 | |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3356 | if (DEBUG_PACKAGE_SCANNING) { |
| 3357 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3358 | Log.d(TAG, "Scanning package " + pkg.packageName); |
| 3359 | } |
| 3360 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3361 | if (mPackages.containsKey(pkg.packageName) |
| 3362 | || mSharedLibraries.containsKey(pkg.packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3363 | Slog.w(TAG, "Application package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3364 | + " already installed. Skipping duplicate."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3365 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3366 | return null; |
| 3367 | } |
| 3368 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3369 | // Initialize package source and resource directories |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3370 | File destCodeFile = new File(pkg.applicationInfo.sourceDir); |
| 3371 | File destResourceFile = new File(pkg.applicationInfo.publicSourceDir); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3372 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3373 | SharedUserSetting suid = null; |
| 3374 | PackageSetting pkgSetting = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3375 | |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 3376 | if (!isSystemApp(pkg)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3377 | // Only system apps can use these features. |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3378 | pkg.mOriginalPackages = null; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3379 | pkg.mRealPackage = null; |
| 3380 | pkg.mAdoptPermissions = null; |
| 3381 | } |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 3382 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3383 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3384 | synchronized (mPackages) { |
| 3385 | // Check all shared libraries and map to their actual file path. |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3386 | if (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null) { |
| 3387 | if (mTmpSharedLibraries == null || |
| 3388 | mTmpSharedLibraries.length < mSharedLibraries.size()) { |
| 3389 | mTmpSharedLibraries = new String[mSharedLibraries.size()]; |
| 3390 | } |
| 3391 | int num = 0; |
| 3392 | int N = pkg.usesLibraries != null ? pkg.usesLibraries.size() : 0; |
| 3393 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3394 | final String file = mSharedLibraries.get(pkg.usesLibraries.get(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3395 | if (file == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3396 | Slog.e(TAG, "Package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3397 | + " requires unavailable shared library " |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3398 | + pkg.usesLibraries.get(i) + "; failing!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3399 | mLastScanError = PackageManager.INSTALL_FAILED_MISSING_SHARED_LIBRARY; |
| 3400 | return null; |
| 3401 | } |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3402 | mTmpSharedLibraries[num] = file; |
| 3403 | num++; |
| 3404 | } |
| 3405 | N = pkg.usesOptionalLibraries != null ? pkg.usesOptionalLibraries.size() : 0; |
| 3406 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3407 | final String file = mSharedLibraries.get(pkg.usesOptionalLibraries.get(i)); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3408 | if (file == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3409 | Slog.w(TAG, "Package " + pkg.packageName |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3410 | + " desires unavailable shared library " |
| 3411 | + pkg.usesOptionalLibraries.get(i) + "; ignoring!"); |
| 3412 | } else { |
| 3413 | mTmpSharedLibraries[num] = file; |
| 3414 | num++; |
| 3415 | } |
| 3416 | } |
| 3417 | if (num > 0) { |
| 3418 | pkg.usesLibraryFiles = new String[num]; |
| 3419 | System.arraycopy(mTmpSharedLibraries, 0, |
| 3420 | pkg.usesLibraryFiles, 0, num); |
| 3421 | } |
Kenny Root | 1683afa | 2011-01-07 14:27:50 -0800 | [diff] [blame] | 3422 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3423 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3424 | if (pkg.mSharedUserId != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3425 | suid = mSettings.getSharedUserLPw(pkg.mSharedUserId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3426 | pkg.applicationInfo.flags, true); |
| 3427 | if (suid == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3428 | Slog.w(TAG, "Creating application package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3429 | + " for shared user failed"); |
| 3430 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3431 | return null; |
| 3432 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3433 | if (DEBUG_PACKAGE_SCANNING) { |
| 3434 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3435 | Log.d(TAG, "Shared UserID " + pkg.mSharedUserId + " (uid=" + suid.userId |
| 3436 | + "): packages=" + suid.packages); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3437 | } |
| 3438 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3439 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3440 | // Check if we are renaming from an original package name. |
| 3441 | PackageSetting origPackage = null; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3442 | String realName = null; |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3443 | if (pkg.mOriginalPackages != null) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3444 | // This package may need to be renamed to a previously |
| 3445 | // installed name. Let's check on that... |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3446 | final String renamed = mSettings.mRenamedPackages.get(pkg.mRealPackage); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3447 | if (pkg.mOriginalPackages.contains(renamed)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3448 | // This package had originally been installed as the |
| 3449 | // original name, and we have already taken care of |
| 3450 | // transitioning to the new one. Just update the new |
| 3451 | // one to continue using the old name. |
| 3452 | realName = pkg.mRealPackage; |
| 3453 | if (!pkg.packageName.equals(renamed)) { |
| 3454 | // Callers into this function may have already taken |
| 3455 | // care of renaming the package; only do it here if |
| 3456 | // it is not already done. |
| 3457 | pkg.setPackageName(renamed); |
| 3458 | } |
| 3459 | |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3460 | } else { |
| 3461 | for (int i=pkg.mOriginalPackages.size()-1; i>=0; i--) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3462 | if ((origPackage = mSettings.peekPackageLPr( |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3463 | pkg.mOriginalPackages.get(i))) != null) { |
| 3464 | // We do have the package already installed under its |
| 3465 | // original name... should we use it? |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3466 | if (!verifyPackageUpdateLPr(origPackage, pkg)) { |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3467 | // New package is not compatible with original. |
| 3468 | origPackage = null; |
| 3469 | continue; |
| 3470 | } else if (origPackage.sharedUser != null) { |
| 3471 | // Make sure uid is compatible between packages. |
| 3472 | if (!origPackage.sharedUser.name.equals(pkg.mSharedUserId)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3473 | Slog.w(TAG, "Unable to migrate data from " + origPackage.name |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3474 | + " to " + pkg.packageName + ": old uid " |
| 3475 | + origPackage.sharedUser.name |
| 3476 | + " differs from " + pkg.mSharedUserId); |
| 3477 | origPackage = null; |
| 3478 | continue; |
| 3479 | } |
| 3480 | } else { |
| 3481 | if (DEBUG_UPGRADE) Log.v(TAG, "Renaming new package " |
| 3482 | + pkg.packageName + " to old name " + origPackage.name); |
| 3483 | } |
| 3484 | break; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3485 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3486 | } |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | if (mTransferedPackages.contains(pkg.packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3491 | Slog.w(TAG, "Package " + pkg.packageName |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3492 | + " was transferred to another, but its .apk remains"); |
| 3493 | } |
| 3494 | |
Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 3495 | // Just create the setting, don't add it yet. For already existing packages |
| 3496 | // the PkgSetting exists already and doesn't have to be created. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3497 | pkgSetting = mSettings.getPackageLPw(pkg, origPackage, realName, suid, destCodeFile, |
Kenny Root | 806cc13 | 2010-09-12 08:34:19 -0700 | [diff] [blame] | 3498 | destResourceFile, pkg.applicationInfo.nativeLibraryDir, |
| 3499 | pkg.applicationInfo.flags, true, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3500 | if (pkgSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3501 | Slog.w(TAG, "Creating application package " + pkg.packageName + " failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3502 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3503 | return null; |
| 3504 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3505 | |
| 3506 | if (pkgSetting.origPackage != null) { |
| 3507 | // If we are first transitioning from an original package, |
| 3508 | // fix up the new package's name now. We need to do this after |
| 3509 | // looking up the package under its new name, so getPackageLP |
| 3510 | // can take care of fiddling things correctly. |
| 3511 | pkg.setPackageName(origPackage.name); |
| 3512 | |
| 3513 | // File a report about this. |
| 3514 | String msg = "New package " + pkgSetting.realName |
| 3515 | + " renamed to replace old package " + pkgSetting.name; |
| 3516 | reportSettingsProblem(Log.WARN, msg); |
| 3517 | |
| 3518 | // Make a note of it. |
| 3519 | mTransferedPackages.add(origPackage.name); |
| 3520 | |
| 3521 | // No longer need to retain this. |
| 3522 | pkgSetting.origPackage = null; |
| 3523 | } |
| 3524 | |
| 3525 | if (realName != null) { |
| 3526 | // Make a note of it. |
| 3527 | mTransferedPackages.add(pkg.packageName); |
| 3528 | } |
| 3529 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3530 | if (mSettings.mDisabledSysPackages.get(pkg.packageName) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3531 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
| 3532 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3533 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3534 | pkg.applicationInfo.uid = pkgSetting.appId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3535 | pkg.mExtras = pkgSetting; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3536 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3537 | if (!verifySignaturesLP(pkgSetting, pkg)) { |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 3538 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3539 | return null; |
| 3540 | } |
| 3541 | // The signature has changed, but this package is in the system |
| 3542 | // image... let's recover! |
Suchi Amalapurapu | c4dd60f | 2009-03-24 21:10:53 -0700 | [diff] [blame] | 3543 | pkgSetting.signatures.mSignatures = pkg.mSignatures; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3544 | // However... if this package is part of a shared user, but it |
| 3545 | // doesn't match the signature of the shared user, let's fail. |
| 3546 | // What this means is that you can't change the signatures |
| 3547 | // associated with an overall shared user, which doesn't seem all |
| 3548 | // that unreasonable. |
| 3549 | if (pkgSetting.sharedUser != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3550 | if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3551 | pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) { |
| 3552 | Log.w(TAG, "Signature mismatch for shared user : " + pkgSetting.sharedUser); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3553 | mLastScanError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 3554 | return null; |
| 3555 | } |
| 3556 | } |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 3557 | // File a report about this. |
| 3558 | String msg = "System package " + pkg.packageName |
| 3559 | + " signature changed; retaining data."; |
| 3560 | reportSettingsProblem(Log.WARN, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3561 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3562 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3563 | // Verify that this new package doesn't have any content providers |
| 3564 | // that conflict with existing packages. Only do this if the |
| 3565 | // package isn't already installed, since we don't want to break |
| 3566 | // things that are installed. |
| 3567 | if ((scanMode&SCAN_NEW_INSTALL) != 0) { |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 3568 | final int N = pkg.providers.size(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3569 | int i; |
| 3570 | for (i=0; i<N; i++) { |
| 3571 | PackageParser.Provider p = pkg.providers.get(i); |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3572 | if (p.info.authority != null) { |
| 3573 | String names[] = p.info.authority.split(";"); |
| 3574 | for (int j = 0; j < names.length; j++) { |
| 3575 | if (mProviders.containsKey(names[j])) { |
| 3576 | PackageParser.Provider other = mProviders.get(names[j]); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3577 | Slog.w(TAG, "Can't install because provider name " + names[j] + |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3578 | " (in package " + pkg.applicationInfo.packageName + |
| 3579 | ") is already used by " |
| 3580 | + ((other != null && other.getComponentName() != null) |
| 3581 | ? other.getComponentName().getPackageName() : "?")); |
| 3582 | mLastScanError = PackageManager.INSTALL_FAILED_CONFLICTING_PROVIDER; |
| 3583 | return null; |
| 3584 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3585 | } |
| 3586 | } |
| 3587 | } |
| 3588 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3589 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3590 | if (pkg.mAdoptPermissions != null) { |
| 3591 | // This package wants to adopt ownership of permissions from |
| 3592 | // another package. |
| 3593 | for (int i = pkg.mAdoptPermissions.size() - 1; i >= 0; i--) { |
| 3594 | final String origName = pkg.mAdoptPermissions.get(i); |
| 3595 | final PackageSetting orig = mSettings.peekPackageLPr(origName); |
| 3596 | if (orig != null) { |
| 3597 | if (verifyPackageUpdateLPr(orig, pkg)) { |
| 3598 | Slog.i(TAG, "Adopting permissions from " + origName + " to " |
| 3599 | + pkg.packageName); |
| 3600 | mSettings.transferPermissionsLPw(origName, pkg.packageName); |
| 3601 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3602 | } |
| 3603 | } |
| 3604 | } |
| 3605 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3606 | |
| 3607 | final String pkgName = pkg.packageName; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3608 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3609 | final long scanFileTime = scanFile.lastModified(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3610 | final boolean forceDex = (scanMode&SCAN_FORCE_DEX) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3611 | pkg.applicationInfo.processName = fixProcessName( |
| 3612 | pkg.applicationInfo.packageName, |
| 3613 | pkg.applicationInfo.processName, |
| 3614 | pkg.applicationInfo.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3615 | |
| 3616 | File dataPath; |
| 3617 | if (mPlatformPackage == pkg) { |
| 3618 | // The system package is special. |
| 3619 | dataPath = new File (Environment.getDataDirectory(), "system"); |
| 3620 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3621 | } else { |
| 3622 | // This is a normal package, need to make its data directory. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3623 | dataPath = getDataPathForPackage(pkg.packageName, 0); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3624 | |
| 3625 | boolean uidError = false; |
| 3626 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3627 | if (dataPath.exists()) { |
| 3628 | mOutPermissions[1] = 0; |
| 3629 | FileUtils.getPermissions(dataPath.getPath(), mOutPermissions); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3630 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3631 | // If we have mismatched owners for the data path, we have a problem. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 3632 | if (mOutPermissions[1] != pkg.applicationInfo.uid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3633 | boolean recovered = false; |
| 3634 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
| 3635 | // If this is a system app, we can at least delete its |
| 3636 | // current data so the application will still work. |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3637 | int ret = mInstaller.remove(pkgName, 0); |
| 3638 | if (ret >= 0) { |
| 3639 | // TODO: Kill the processes first |
| 3640 | // Remove the data directories for all users |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3641 | sUserManager.removePackageForAllUsers(pkgName); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3642 | // Old data gone! |
| 3643 | String msg = "System package " + pkg.packageName |
| 3644 | + " has changed from uid: " |
| 3645 | + mOutPermissions[1] + " to " |
| 3646 | + pkg.applicationInfo.uid + "; old data erased"; |
| 3647 | reportSettingsProblem(Log.WARN, msg); |
| 3648 | recovered = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3649 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3650 | // And now re-install the app. |
| 3651 | ret = mInstaller.install(pkgName, pkg.applicationInfo.uid, |
| 3652 | pkg.applicationInfo.uid); |
| 3653 | if (ret == -1) { |
| 3654 | // Ack should not happen! |
| 3655 | msg = "System package " + pkg.packageName |
| 3656 | + " could not have data directory re-created after delete."; |
| 3657 | reportSettingsProblem(Log.WARN, msg); |
| 3658 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3659 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3660 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3661 | // Create data directories for all users |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3662 | sUserManager.installPackageForAllUsers(pkgName, |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3663 | pkg.applicationInfo.uid); |
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 | if (!recovered) { |
| 3666 | mHasSystemUidErrors = true; |
| 3667 | } |
| 3668 | } |
| 3669 | if (!recovered) { |
| 3670 | pkg.applicationInfo.dataDir = "/mismatched_uid/settings_" |
| 3671 | + pkg.applicationInfo.uid + "/fs_" |
| 3672 | + mOutPermissions[1]; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3673 | pkg.applicationInfo.nativeLibraryDir = pkg.applicationInfo.dataDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3674 | String msg = "Package " + pkg.packageName |
| 3675 | + " has mismatched uid: " |
| 3676 | + mOutPermissions[1] + " on disk, " |
| 3677 | + pkg.applicationInfo.uid + " in settings"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3678 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3679 | synchronized (mPackages) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3680 | mSettings.mReadMessages.append(msg); |
| 3681 | mSettings.mReadMessages.append('\n'); |
| 3682 | uidError = true; |
| 3683 | if (!pkgSetting.uidError) { |
| 3684 | reportSettingsProblem(Log.ERROR, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3685 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | } |
| 3689 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3690 | } else { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3691 | if (DEBUG_PACKAGE_SCANNING) { |
| 3692 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3693 | Log.v(TAG, "Want this data dir: " + dataPath); |
| 3694 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3695 | //invoke installer to do the actual installation |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3696 | int ret = mInstaller.install(pkgName, pkg.applicationInfo.uid, |
| 3697 | pkg.applicationInfo.uid); |
| 3698 | if (ret < 0) { |
| 3699 | // Error from installer |
| 3700 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3701 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3702 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3703 | // Create data directories for all users |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3704 | sUserManager.installPackageForAllUsers(pkgName, pkg.applicationInfo.uid); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3706 | if (dataPath.exists()) { |
| 3707 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3708 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3709 | Slog.w(TAG, "Unable to create data directory: " + dataPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3710 | pkg.applicationInfo.dataDir = null; |
| 3711 | } |
| 3712 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3713 | |
| 3714 | /* |
| 3715 | * Set the data dir to the default "/data/data/<package name>/lib" |
| 3716 | * if we got here without anyone telling us different (e.g., apps |
| 3717 | * stored on SD card have their native libraries stored in the ASEC |
| 3718 | * container with the APK). |
Kenny Root | 806cc13 | 2010-09-12 08:34:19 -0700 | [diff] [blame] | 3719 | * |
| 3720 | * This happens during an upgrade from a package settings file that |
| 3721 | * doesn't have a native library path attribute at all. |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3722 | */ |
Kenny Root | bd135c1 | 2010-10-05 12:26:27 -0700 | [diff] [blame] | 3723 | if (pkg.applicationInfo.nativeLibraryDir == null && pkg.applicationInfo.dataDir != null) { |
| 3724 | if (pkgSetting.nativeLibraryPathString == null) { |
| 3725 | final String nativeLibraryPath = new File(dataPath, LIB_DIR_NAME).getPath(); |
| 3726 | pkg.applicationInfo.nativeLibraryDir = nativeLibraryPath; |
| 3727 | pkgSetting.nativeLibraryPathString = nativeLibraryPath; |
| 3728 | } else { |
| 3729 | pkg.applicationInfo.nativeLibraryDir = pkgSetting.nativeLibraryPathString; |
| 3730 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3733 | pkgSetting.uidError = uidError; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3734 | } |
| 3735 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3736 | String path = scanFile.getPath(); |
| 3737 | /* Note: We don't want to unpack the native binaries for |
| 3738 | * system applications, unless they have been updated |
| 3739 | * (the binaries are already under /system/lib). |
| 3740 | * Also, don't unpack libs for apps on the external card |
| 3741 | * since they should have their libraries in the ASEC |
| 3742 | * container already. |
| 3743 | * |
| 3744 | * In other words, we're going to unpack the binaries |
| 3745 | * only for non-system apps and system app upgrades. |
| 3746 | */ |
| 3747 | if (pkg.applicationInfo.nativeLibraryDir != null) { |
| 3748 | try { |
| 3749 | final File nativeLibraryDir = new File(pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 5832ead | 2011-08-02 15:47:08 -0700 | [diff] [blame] | 3750 | final String dataPathString = dataPath.getCanonicalPath(); |
Kenny Root | e68d58a | 2010-10-18 16:08:54 -0700 | [diff] [blame] | 3751 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3752 | if (isSystemApp(pkg) && !isUpdatedSystemApp(pkg)) { |
| 3753 | /* |
| 3754 | * Upgrading from a previous version of the OS sometimes |
| 3755 | * leaves native libraries in the /data/data/<app>/lib |
| 3756 | * directory for system apps even when they shouldn't be. |
| 3757 | * Recent changes in the JNI library search path |
| 3758 | * necessitates we remove those to match previous behavior. |
| 3759 | */ |
| 3760 | if (NativeLibraryHelper.removeNativeBinariesFromDirLI(nativeLibraryDir)) { |
| 3761 | Log.i(TAG, "removed obsolete native libraries for system package " |
| 3762 | + path); |
Kenny Root | 831baa2 | 2010-10-05 12:29:25 -0700 | [diff] [blame] | 3763 | } |
Kenny Root | 5832ead | 2011-08-02 15:47:08 -0700 | [diff] [blame] | 3764 | } else if (nativeLibraryDir.getParentFile().getCanonicalPath() |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3765 | .equals(dataPathString)) { |
| 3766 | /* |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 3767 | * Make sure the native library dir isn't a symlink to |
| 3768 | * something. If it is, ask installd to remove it and create |
| 3769 | * a directory so we can copy to it afterwards. |
| 3770 | */ |
| 3771 | boolean isSymLink; |
| 3772 | try { |
| 3773 | isSymLink = S_ISLNK(Libcore.os.lstat(nativeLibraryDir.getPath()).st_mode); |
| 3774 | } catch (ErrnoException e) { |
| 3775 | // This shouldn't happen, but we'll fail-safe. |
| 3776 | isSymLink = true; |
| 3777 | } |
| 3778 | if (isSymLink) { |
| 3779 | mInstaller.unlinkNativeLibraryDirectory(dataPathString); |
| 3780 | } |
| 3781 | |
| 3782 | /* |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3783 | * If this is an internal application or our |
| 3784 | * nativeLibraryPath points to our data directory, unpack |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 3785 | * the libraries if necessary. |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3786 | */ |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 3787 | NativeLibraryHelper.copyNativeBinariesIfNeededLI(scanFile, nativeLibraryDir); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3788 | } else { |
| 3789 | Slog.i(TAG, "Linking native library dir for " + path); |
| 3790 | mInstaller.linkNativeLibraryDirectory(dataPathString, |
| 3791 | pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 831baa2 | 2010-10-05 12:29:25 -0700 | [diff] [blame] | 3792 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3793 | } catch (IOException ioe) { |
| 3794 | Log.e(TAG, "Unable to get canonical file " + ioe.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3795 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3796 | } |
| 3797 | pkg.mScanPath = path; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3798 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3799 | if ((scanMode&SCAN_NO_DEX) == 0) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3800 | if (performDexOptLI(pkg, forceDex, (scanMode&SCAN_DEFER_DEX) != 0) |
| 3801 | == DEX_OPT_FAILED) { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3802 | mLastScanError = PackageManager.INSTALL_FAILED_DEXOPT; |
| 3803 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3804 | } |
| 3805 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3806 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3807 | if (mFactoryTest && pkg.requestedPermissions.contains( |
| 3808 | android.Manifest.permission.FACTORY_TEST)) { |
| 3809 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_FACTORY_TEST; |
| 3810 | } |
| 3811 | |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3812 | // Request the ActivityManager to kill the process(only for existing packages) |
| 3813 | // so that we do not end up in a confused state while the user is still using the older |
| 3814 | // version of the application while the new one gets installed. |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3815 | if ((parseFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3816 | killApplication(pkg.applicationInfo.packageName, |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3817 | pkg.applicationInfo.uid); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3818 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3819 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3820 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3821 | synchronized (mPackages) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 3822 | // We don't expect installation to fail beyond this point, |
| 3823 | if ((scanMode&SCAN_MONITOR) != 0) { |
| 3824 | mAppDirs.put(pkg.mPath, pkg); |
| 3825 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3826 | // Add the new setting to mSettings |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3827 | mSettings.insertPackageSettingLPw(pkgSetting, pkg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3828 | // Add the new setting to mPackages |
Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 3829 | mPackages.put(pkg.applicationInfo.packageName, pkg); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 3830 | // Make sure we don't accidentally delete its data. |
| 3831 | mSettings.mPackagesToBeCleaned.remove(pkgName); |
| 3832 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3833 | // Take care of first install / last update times. |
| 3834 | if (currentTime != 0) { |
| 3835 | if (pkgSetting.firstInstallTime == 0) { |
| 3836 | pkgSetting.firstInstallTime = pkgSetting.lastUpdateTime = currentTime; |
| 3837 | } else if ((scanMode&SCAN_UPDATE_TIME) != 0) { |
| 3838 | pkgSetting.lastUpdateTime = currentTime; |
| 3839 | } |
| 3840 | } else if (pkgSetting.firstInstallTime == 0) { |
| 3841 | // We need *something*. Take time time stamp of the file. |
| 3842 | pkgSetting.firstInstallTime = pkgSetting.lastUpdateTime = scanFileTime; |
| 3843 | } else if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) != 0) { |
| 3844 | if (scanFileTime != pkgSetting.timeStamp) { |
| 3845 | // A package on the system image has changed; consider this |
| 3846 | // to be an update. |
| 3847 | pkgSetting.lastUpdateTime = scanFileTime; |
| 3848 | } |
| 3849 | } |
| 3850 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3851 | int N = pkg.providers.size(); |
| 3852 | StringBuilder r = null; |
| 3853 | int i; |
| 3854 | for (i=0; i<N; i++) { |
| 3855 | PackageParser.Provider p = pkg.providers.get(i); |
| 3856 | p.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 3857 | p.info.processName, pkg.applicationInfo.uid); |
| 3858 | mProvidersByComponent.put(new ComponentName(p.info.packageName, |
| 3859 | p.info.name), p); |
| 3860 | p.syncable = p.info.isSyncable; |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3861 | if (p.info.authority != null) { |
| 3862 | String names[] = p.info.authority.split(";"); |
| 3863 | p.info.authority = null; |
| 3864 | for (int j = 0; j < names.length; j++) { |
| 3865 | if (j == 1 && p.syncable) { |
| 3866 | // We only want the first authority for a provider to possibly be |
| 3867 | // syncable, so if we already added this provider using a different |
| 3868 | // authority clear the syncable flag. We copy the provider before |
| 3869 | // changing it because the mProviders object contains a reference |
| 3870 | // to a provider that we don't want to change. |
| 3871 | // Only do this for the second authority since the resulting provider |
| 3872 | // object can be the same for all future authorities for this provider. |
| 3873 | p = new PackageParser.Provider(p); |
| 3874 | p.syncable = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3875 | } |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3876 | if (!mProviders.containsKey(names[j])) { |
| 3877 | mProviders.put(names[j], p); |
| 3878 | if (p.info.authority == null) { |
| 3879 | p.info.authority = names[j]; |
| 3880 | } else { |
| 3881 | p.info.authority = p.info.authority + ";" + names[j]; |
| 3882 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3883 | if (DEBUG_PACKAGE_SCANNING) { |
| 3884 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3885 | Log.d(TAG, "Registered content provider: " + names[j] |
| 3886 | + ", className = " + p.info.name + ", isSyncable = " |
| 3887 | + p.info.isSyncable); |
| 3888 | } |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3889 | } else { |
| 3890 | PackageParser.Provider other = mProviders.get(names[j]); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3891 | Slog.w(TAG, "Skipping provider name " + names[j] + |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3892 | " (in package " + pkg.applicationInfo.packageName + |
| 3893 | "): name already used by " |
| 3894 | + ((other != null && other.getComponentName() != null) |
| 3895 | ? other.getComponentName().getPackageName() : "?")); |
| 3896 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3897 | } |
| 3898 | } |
| 3899 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3900 | if (r == null) { |
| 3901 | r = new StringBuilder(256); |
| 3902 | } else { |
| 3903 | r.append(' '); |
| 3904 | } |
| 3905 | r.append(p.info.name); |
| 3906 | } |
| 3907 | } |
| 3908 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 3909 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Providers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3910 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3911 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3912 | N = pkg.services.size(); |
| 3913 | r = null; |
| 3914 | for (i=0; i<N; i++) { |
| 3915 | PackageParser.Service s = pkg.services.get(i); |
| 3916 | s.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 3917 | s.info.processName, pkg.applicationInfo.uid); |
| 3918 | mServices.addService(s); |
| 3919 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3920 | if (r == null) { |
| 3921 | r = new StringBuilder(256); |
| 3922 | } else { |
| 3923 | r.append(' '); |
| 3924 | } |
| 3925 | r.append(s.info.name); |
| 3926 | } |
| 3927 | } |
| 3928 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 3929 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Services: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3930 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3931 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3932 | N = pkg.receivers.size(); |
| 3933 | r = null; |
| 3934 | for (i=0; i<N; i++) { |
| 3935 | PackageParser.Activity a = pkg.receivers.get(i); |
| 3936 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 3937 | a.info.processName, pkg.applicationInfo.uid); |
| 3938 | mReceivers.addActivity(a, "receiver"); |
| 3939 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3940 | if (r == null) { |
| 3941 | r = new StringBuilder(256); |
| 3942 | } else { |
| 3943 | r.append(' '); |
| 3944 | } |
| 3945 | r.append(a.info.name); |
| 3946 | } |
| 3947 | } |
| 3948 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 3949 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Receivers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3950 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3951 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3952 | N = pkg.activities.size(); |
| 3953 | r = null; |
| 3954 | for (i=0; i<N; i++) { |
| 3955 | PackageParser.Activity a = pkg.activities.get(i); |
| 3956 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 3957 | a.info.processName, pkg.applicationInfo.uid); |
| 3958 | mActivities.addActivity(a, "activity"); |
| 3959 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3960 | if (r == null) { |
| 3961 | r = new StringBuilder(256); |
| 3962 | } else { |
| 3963 | r.append(' '); |
| 3964 | } |
| 3965 | r.append(a.info.name); |
| 3966 | } |
| 3967 | } |
| 3968 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 3969 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Activities: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3970 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3971 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3972 | N = pkg.permissionGroups.size(); |
| 3973 | r = null; |
| 3974 | for (i=0; i<N; i++) { |
| 3975 | PackageParser.PermissionGroup pg = pkg.permissionGroups.get(i); |
| 3976 | PackageParser.PermissionGroup cur = mPermissionGroups.get(pg.info.name); |
| 3977 | if (cur == null) { |
| 3978 | mPermissionGroups.put(pg.info.name, pg); |
| 3979 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3980 | if (r == null) { |
| 3981 | r = new StringBuilder(256); |
| 3982 | } else { |
| 3983 | r.append(' '); |
| 3984 | } |
| 3985 | r.append(pg.info.name); |
| 3986 | } |
| 3987 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3988 | Slog.w(TAG, "Permission group " + pg.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3989 | + pg.info.packageName + " ignored: original from " |
| 3990 | + cur.info.packageName); |
| 3991 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 3992 | if (r == null) { |
| 3993 | r = new StringBuilder(256); |
| 3994 | } else { |
| 3995 | r.append(' '); |
| 3996 | } |
| 3997 | r.append("DUP:"); |
| 3998 | r.append(pg.info.name); |
| 3999 | } |
| 4000 | } |
| 4001 | } |
| 4002 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4003 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Permission Groups: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4004 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4005 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4006 | N = pkg.permissions.size(); |
| 4007 | r = null; |
| 4008 | for (i=0; i<N; i++) { |
| 4009 | PackageParser.Permission p = pkg.permissions.get(i); |
| 4010 | HashMap<String, BasePermission> permissionMap = |
| 4011 | p.tree ? mSettings.mPermissionTrees |
| 4012 | : mSettings.mPermissions; |
| 4013 | p.group = mPermissionGroups.get(p.info.group); |
| 4014 | if (p.info.group == null || p.group != null) { |
| 4015 | BasePermission bp = permissionMap.get(p.info.name); |
| 4016 | if (bp == null) { |
| 4017 | bp = new BasePermission(p.info.name, p.info.packageName, |
| 4018 | BasePermission.TYPE_NORMAL); |
| 4019 | permissionMap.put(p.info.name, bp); |
| 4020 | } |
| 4021 | if (bp.perm == null) { |
| 4022 | if (bp.sourcePackage == null |
| 4023 | || bp.sourcePackage.equals(p.info.packageName)) { |
| 4024 | BasePermission tree = findPermissionTreeLP(p.info.name); |
| 4025 | if (tree == null |
| 4026 | || tree.sourcePackage.equals(p.info.packageName)) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4027 | bp.packageSetting = pkgSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4028 | bp.perm = p; |
| 4029 | bp.uid = pkg.applicationInfo.uid; |
| 4030 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4031 | if (r == null) { |
| 4032 | r = new StringBuilder(256); |
| 4033 | } else { |
| 4034 | r.append(' '); |
| 4035 | } |
| 4036 | r.append(p.info.name); |
| 4037 | } |
| 4038 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4039 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4040 | + p.info.packageName + " ignored: base tree " |
| 4041 | + tree.name + " is from package " |
| 4042 | + tree.sourcePackage); |
| 4043 | } |
| 4044 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4045 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4046 | + p.info.packageName + " ignored: original from " |
| 4047 | + bp.sourcePackage); |
| 4048 | } |
| 4049 | } else if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4050 | if (r == null) { |
| 4051 | r = new StringBuilder(256); |
| 4052 | } else { |
| 4053 | r.append(' '); |
| 4054 | } |
| 4055 | r.append("DUP:"); |
| 4056 | r.append(p.info.name); |
| 4057 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4058 | if (bp.perm == p) { |
| 4059 | bp.protectionLevel = p.info.protectionLevel; |
| 4060 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4061 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4062 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4063 | + p.info.packageName + " ignored: no group " |
| 4064 | + p.group); |
| 4065 | } |
| 4066 | } |
| 4067 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4068 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Permissions: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4069 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4070 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4071 | N = pkg.instrumentation.size(); |
| 4072 | r = null; |
| 4073 | for (i=0; i<N; i++) { |
| 4074 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
| 4075 | a.info.packageName = pkg.applicationInfo.packageName; |
| 4076 | a.info.sourceDir = pkg.applicationInfo.sourceDir; |
| 4077 | a.info.publicSourceDir = pkg.applicationInfo.publicSourceDir; |
| 4078 | a.info.dataDir = pkg.applicationInfo.dataDir; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4079 | a.info.nativeLibraryDir = pkg.applicationInfo.nativeLibraryDir; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4080 | mInstrumentation.put(a.getComponentName(), a); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4081 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4082 | if (r == null) { |
| 4083 | r = new StringBuilder(256); |
| 4084 | } else { |
| 4085 | r.append(' '); |
| 4086 | } |
| 4087 | r.append(a.info.name); |
| 4088 | } |
| 4089 | } |
| 4090 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4091 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Instrumentation: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4092 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4093 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 4094 | if (pkg.protectedBroadcasts != null) { |
| 4095 | N = pkg.protectedBroadcasts.size(); |
| 4096 | for (i=0; i<N; i++) { |
| 4097 | mProtectedBroadcasts.add(pkg.protectedBroadcasts.get(i)); |
| 4098 | } |
| 4099 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4101 | pkgSetting.setTimeStamp(scanFileTime); |
| 4102 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4103 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4104 | return pkg; |
| 4105 | } |
| 4106 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4107 | private void killApplication(String pkgName, int uid) { |
| 4108 | // Request the ActivityManager to kill the process(only for existing packages) |
| 4109 | // so that we do not end up in a confused state while the user is still using the older |
| 4110 | // version of the application while the new one gets installed. |
| 4111 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 4112 | if (am != null) { |
| 4113 | try { |
| 4114 | am.killApplicationWithUid(pkgName, uid); |
| 4115 | } catch (RemoteException e) { |
| 4116 | } |
| 4117 | } |
| 4118 | } |
| 4119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4120 | void removePackageLI(PackageParser.Package pkg, boolean chatty) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4121 | if (DEBUG_INSTALL) { |
| 4122 | if (chatty) |
| 4123 | Log.d(TAG, "Removing package " + pkg.applicationInfo.packageName); |
| 4124 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4125 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4126 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4127 | synchronized (mPackages) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4128 | mPackages.remove(pkg.applicationInfo.packageName); |
| 4129 | if (pkg.mPath != null) { |
| 4130 | mAppDirs.remove(pkg.mPath); |
| 4131 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4133 | int N = pkg.providers.size(); |
| 4134 | StringBuilder r = null; |
| 4135 | int i; |
| 4136 | for (i=0; i<N; i++) { |
| 4137 | PackageParser.Provider p = pkg.providers.get(i); |
| 4138 | mProvidersByComponent.remove(new ComponentName(p.info.packageName, |
| 4139 | p.info.name)); |
| 4140 | if (p.info.authority == null) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4142 | /* The is another ContentProvider with this authority when |
| 4143 | * this app was installed so this authority is null, |
| 4144 | * Ignore it as we don't have to unregister the provider. |
| 4145 | */ |
| 4146 | continue; |
| 4147 | } |
| 4148 | String names[] = p.info.authority.split(";"); |
| 4149 | for (int j = 0; j < names.length; j++) { |
| 4150 | if (mProviders.get(names[j]) == p) { |
| 4151 | mProviders.remove(names[j]); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4152 | if (DEBUG_REMOVE) { |
| 4153 | if (chatty) |
| 4154 | Log.d(TAG, "Unregistered content provider: " + names[j] |
| 4155 | + ", className = " + p.info.name + ", isSyncable = " |
| 4156 | + p.info.isSyncable); |
| 4157 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4158 | } |
| 4159 | } |
| 4160 | if (chatty) { |
| 4161 | if (r == null) { |
| 4162 | r = new StringBuilder(256); |
| 4163 | } else { |
| 4164 | r.append(' '); |
| 4165 | } |
| 4166 | r.append(p.info.name); |
| 4167 | } |
| 4168 | } |
| 4169 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4170 | if (DEBUG_REMOVE) Log.d(TAG, " Providers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4171 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4173 | N = pkg.services.size(); |
| 4174 | r = null; |
| 4175 | for (i=0; i<N; i++) { |
| 4176 | PackageParser.Service s = pkg.services.get(i); |
| 4177 | mServices.removeService(s); |
| 4178 | if (chatty) { |
| 4179 | if (r == null) { |
| 4180 | r = new StringBuilder(256); |
| 4181 | } else { |
| 4182 | r.append(' '); |
| 4183 | } |
| 4184 | r.append(s.info.name); |
| 4185 | } |
| 4186 | } |
| 4187 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4188 | if (DEBUG_REMOVE) Log.d(TAG, " Services: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4189 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4191 | N = pkg.receivers.size(); |
| 4192 | r = null; |
| 4193 | for (i=0; i<N; i++) { |
| 4194 | PackageParser.Activity a = pkg.receivers.get(i); |
| 4195 | mReceivers.removeActivity(a, "receiver"); |
| 4196 | if (chatty) { |
| 4197 | if (r == null) { |
| 4198 | r = new StringBuilder(256); |
| 4199 | } else { |
| 4200 | r.append(' '); |
| 4201 | } |
| 4202 | r.append(a.info.name); |
| 4203 | } |
| 4204 | } |
| 4205 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4206 | if (DEBUG_REMOVE) Log.d(TAG, " Receivers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4207 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4208 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4209 | N = pkg.activities.size(); |
| 4210 | r = null; |
| 4211 | for (i=0; i<N; i++) { |
| 4212 | PackageParser.Activity a = pkg.activities.get(i); |
| 4213 | mActivities.removeActivity(a, "activity"); |
| 4214 | if (chatty) { |
| 4215 | if (r == null) { |
| 4216 | r = new StringBuilder(256); |
| 4217 | } else { |
| 4218 | r.append(' '); |
| 4219 | } |
| 4220 | r.append(a.info.name); |
| 4221 | } |
| 4222 | } |
| 4223 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4224 | if (DEBUG_REMOVE) Log.d(TAG, " Activities: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4225 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4227 | N = pkg.permissions.size(); |
| 4228 | r = null; |
| 4229 | for (i=0; i<N; i++) { |
| 4230 | PackageParser.Permission p = pkg.permissions.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4231 | BasePermission bp = mSettings.mPermissions.get(p.info.name); |
| 4232 | if (bp == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4233 | bp = mSettings.mPermissionTrees.get(p.info.name); |
| 4234 | } |
| 4235 | if (bp != null && bp.perm == p) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4236 | bp.perm = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4237 | if (chatty) { |
| 4238 | if (r == null) { |
| 4239 | r = new StringBuilder(256); |
| 4240 | } else { |
| 4241 | r.append(' '); |
| 4242 | } |
| 4243 | r.append(p.info.name); |
| 4244 | } |
| 4245 | } |
| 4246 | } |
| 4247 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4248 | if (DEBUG_REMOVE) Log.d(TAG, " Permissions: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4249 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4251 | N = pkg.instrumentation.size(); |
| 4252 | r = null; |
| 4253 | for (i=0; i<N; i++) { |
| 4254 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4255 | mInstrumentation.remove(a.getComponentName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4256 | if (chatty) { |
| 4257 | if (r == null) { |
| 4258 | r = new StringBuilder(256); |
| 4259 | } else { |
| 4260 | r.append(' '); |
| 4261 | } |
| 4262 | r.append(a.info.name); |
| 4263 | } |
| 4264 | } |
| 4265 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4266 | if (DEBUG_REMOVE) Log.d(TAG, " Instrumentation: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4267 | } |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | private static final boolean isPackageFilename(String name) { |
| 4272 | return name != null && name.endsWith(".apk"); |
| 4273 | } |
| 4274 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4275 | private static boolean hasPermission(PackageParser.Package pkgInfo, String perm) { |
| 4276 | for (int i=pkgInfo.permissions.size()-1; i>=0; i--) { |
| 4277 | if (pkgInfo.permissions.get(i).info.name.equals(perm)) { |
| 4278 | return true; |
| 4279 | } |
| 4280 | } |
| 4281 | return false; |
| 4282 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4283 | |
| 4284 | static final int UPDATE_PERMISSIONS_ALL = 1<<0; |
| 4285 | static final int UPDATE_PERMISSIONS_REPLACE_PKG = 1<<1; |
| 4286 | static final int UPDATE_PERMISSIONS_REPLACE_ALL = 1<<2; |
| 4287 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4288 | private void updatePermissionsLPw(String changingPkg, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4289 | PackageParser.Package pkgInfo, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4290 | // Make sure there are no dangling permission trees. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4291 | Iterator<BasePermission> it = mSettings.mPermissionTrees.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4292 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4293 | final BasePermission bp = it.next(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4294 | if (bp.packageSetting == null) { |
| 4295 | // We may not yet have parsed the package, so just see if |
| 4296 | // we still know about its settings. |
| 4297 | bp.packageSetting = mSettings.mPackages.get(bp.sourcePackage); |
| 4298 | } |
| 4299 | if (bp.packageSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4300 | Slog.w(TAG, "Removing dangling permission tree: " + bp.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4301 | + " from package " + bp.sourcePackage); |
| 4302 | it.remove(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4303 | } else if (changingPkg != null && changingPkg.equals(bp.sourcePackage)) { |
| 4304 | if (pkgInfo == null || !hasPermission(pkgInfo, bp.name)) { |
| 4305 | Slog.i(TAG, "Removing old permission tree: " + bp.name |
| 4306 | + " from package " + bp.sourcePackage); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4307 | flags |= UPDATE_PERMISSIONS_ALL; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4308 | it.remove(); |
| 4309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4310 | } |
| 4311 | } |
| 4312 | |
| 4313 | // Make sure all dynamic permissions have been assigned to a package, |
| 4314 | // and make sure there are no dangling permissions. |
| 4315 | it = mSettings.mPermissions.values().iterator(); |
| 4316 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4317 | final BasePermission bp = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4318 | if (bp.type == BasePermission.TYPE_DYNAMIC) { |
| 4319 | if (DEBUG_SETTINGS) Log.v(TAG, "Dynamic permission: name=" |
| 4320 | + bp.name + " pkg=" + bp.sourcePackage |
| 4321 | + " info=" + bp.pendingInfo); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4322 | if (bp.packageSetting == null && bp.pendingInfo != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4323 | final BasePermission tree = findPermissionTreeLP(bp.name); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 4324 | if (tree != null && tree.perm != null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4325 | bp.packageSetting = tree.packageSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4326 | bp.perm = new PackageParser.Permission(tree.perm.owner, |
| 4327 | new PermissionInfo(bp.pendingInfo)); |
| 4328 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 4329 | bp.perm.info.name = bp.name; |
| 4330 | bp.uid = tree.uid; |
| 4331 | } |
| 4332 | } |
| 4333 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4334 | if (bp.packageSetting == null) { |
| 4335 | // We may not yet have parsed the package, so just see if |
| 4336 | // we still know about its settings. |
| 4337 | bp.packageSetting = mSettings.mPackages.get(bp.sourcePackage); |
| 4338 | } |
| 4339 | if (bp.packageSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4340 | Slog.w(TAG, "Removing dangling permission: " + bp.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4341 | + " from package " + bp.sourcePackage); |
| 4342 | it.remove(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4343 | } else if (changingPkg != null && changingPkg.equals(bp.sourcePackage)) { |
| 4344 | if (pkgInfo == null || !hasPermission(pkgInfo, bp.name)) { |
| 4345 | Slog.i(TAG, "Removing old permission: " + bp.name |
| 4346 | + " from package " + bp.sourcePackage); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4347 | flags |= UPDATE_PERMISSIONS_ALL; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4348 | it.remove(); |
| 4349 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4350 | } |
| 4351 | } |
| 4352 | |
| 4353 | // Now update the permissions for all packages, in particular |
| 4354 | // replace the granted permissions of the system packages. |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4355 | if ((flags&UPDATE_PERMISSIONS_ALL) != 0) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4356 | for (PackageParser.Package pkg : mPackages.values()) { |
| 4357 | if (pkg != pkgInfo) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4358 | grantPermissionsLPw(pkg, (flags&UPDATE_PERMISSIONS_REPLACE_ALL) != 0); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4359 | } |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | if (pkgInfo != null) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4364 | grantPermissionsLPw(pkgInfo, (flags&UPDATE_PERMISSIONS_REPLACE_PKG) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4365 | } |
| 4366 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4367 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4368 | private void grantPermissionsLPw(PackageParser.Package pkg, boolean replace) { |
| 4369 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4370 | if (ps == null) { |
| 4371 | return; |
| 4372 | } |
| 4373 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4374 | HashSet<String> origPermissions = gp.grantedPermissions; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4375 | boolean changedPermission = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4376 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4377 | if (replace) { |
| 4378 | ps.permissionsFixed = false; |
| 4379 | if (gp == ps) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4380 | origPermissions = new HashSet<String>(gp.grantedPermissions); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4381 | gp.grantedPermissions.clear(); |
| 4382 | gp.gids = mGlobalGids; |
| 4383 | } |
| 4384 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4385 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4386 | if (gp.gids == null) { |
| 4387 | gp.gids = mGlobalGids; |
| 4388 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4389 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4390 | final int N = pkg.requestedPermissions.size(); |
| 4391 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4392 | final String name = pkg.requestedPermissions.get(i); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4393 | //final boolean required = pkg.requestedPermssionsRequired.get(i); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4394 | final BasePermission bp = mSettings.mPermissions.get(name); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4395 | if (DEBUG_INSTALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4396 | if (gp != ps) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4397 | Log.i(TAG, "Package " + pkg.packageName + " checking " + name + ": " + bp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4398 | } |
| 4399 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4400 | if (bp != null && bp.packageSetting != null) { |
| 4401 | final String perm = bp.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4402 | boolean allowed; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4403 | boolean allowedSig = false; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4404 | final int level = bp.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE; |
| 4405 | if (level == PermissionInfo.PROTECTION_NORMAL |
| 4406 | || level == PermissionInfo.PROTECTION_DANGEROUS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4407 | allowed = true; |
Dianne Hackborn | 6e52b5d | 2010-04-05 14:33:01 -0700 | [diff] [blame] | 4408 | } else if (bp.packageSetting == null) { |
| 4409 | // This permission is invalid; skip it. |
| 4410 | allowed = false; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4411 | } else if (level == PermissionInfo.PROTECTION_SIGNATURE) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4412 | allowed = (compareSignatures( |
Dianne Hackborn | 2fe979f | 2010-10-08 15:07:17 -0700 | [diff] [blame] | 4413 | bp.packageSetting.signatures.mSignatures, pkg.mSignatures) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4414 | == PackageManager.SIGNATURE_MATCH) |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4415 | || (compareSignatures(mPlatformPackage.mSignatures, pkg.mSignatures) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4416 | == PackageManager.SIGNATURE_MATCH); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4417 | if (!allowed && (bp.protectionLevel |
| 4418 | & PermissionInfo.PROTECTION_FLAG_SYSTEM) != 0) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4419 | if (isSystemApp(pkg)) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4420 | // For updated system applications, a system permission |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4421 | // is granted only if it had been defined by the original application. |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4422 | if (isUpdatedSystemApp(pkg)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4423 | final PackageSetting sysPs = mSettings |
| 4424 | .getDisabledSystemPkgLPr(pkg.packageName); |
Dianne Hackborn | 2fe979f | 2010-10-08 15:07:17 -0700 | [diff] [blame] | 4425 | final GrantedPermissions origGp = sysPs.sharedUser != null |
| 4426 | ? sysPs.sharedUser : sysPs; |
| 4427 | if (origGp.grantedPermissions.contains(perm)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4428 | allowed = true; |
| 4429 | } else { |
| 4430 | allowed = false; |
| 4431 | } |
| 4432 | } else { |
| 4433 | allowed = true; |
| 4434 | } |
| 4435 | } |
| 4436 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4437 | if (!allowed && (bp.protectionLevel |
| 4438 | & PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) { |
| 4439 | // For development permissions, a development permission |
| 4440 | // is granted only if it was already granted. |
| 4441 | if (origPermissions.contains(perm)) { |
| 4442 | allowed = true; |
| 4443 | } else { |
| 4444 | allowed = false; |
| 4445 | } |
| 4446 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4447 | if (allowed) { |
| 4448 | allowedSig = true; |
| 4449 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4450 | } else { |
| 4451 | allowed = false; |
| 4452 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4453 | if (DEBUG_INSTALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4454 | if (gp != ps) { |
| 4455 | Log.i(TAG, "Package " + pkg.packageName + " granting " + perm); |
| 4456 | } |
| 4457 | } |
| 4458 | if (allowed) { |
| 4459 | if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0 |
| 4460 | && ps.permissionsFixed) { |
| 4461 | // If this is an existing, non-system package, then |
| 4462 | // we can't add any new permissions to it. |
Dianne Hackborn | f657b63 | 2010-03-22 18:08:07 -0700 | [diff] [blame] | 4463 | if (!allowedSig && !gp.grantedPermissions.contains(perm)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4464 | allowed = false; |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 4465 | // Except... if this is a permission that was added |
| 4466 | // to the platform (note: need to only do this when |
| 4467 | // updating the platform). |
| 4468 | final int NP = PackageParser.NEW_PERMISSIONS.length; |
| 4469 | for (int ip=0; ip<NP; ip++) { |
| 4470 | final PackageParser.NewPermissionInfo npi |
| 4471 | = PackageParser.NEW_PERMISSIONS[ip]; |
| 4472 | if (npi.name.equals(perm) |
| 4473 | && pkg.applicationInfo.targetSdkVersion < npi.sdkVersion) { |
| 4474 | allowed = true; |
Dianne Hackborn | f657b63 | 2010-03-22 18:08:07 -0700 | [diff] [blame] | 4475 | Log.i(TAG, "Auto-granting " + perm + " to old pkg " |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 4476 | + pkg.packageName); |
| 4477 | break; |
| 4478 | } |
| 4479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4480 | } |
| 4481 | } |
| 4482 | if (allowed) { |
| 4483 | if (!gp.grantedPermissions.contains(perm)) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4484 | changedPermission = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4485 | gp.grantedPermissions.add(perm); |
| 4486 | gp.gids = appendInts(gp.gids, bp.gids); |
Dianne Hackborn | bbfd58a | 2010-03-23 11:49:59 -0700 | [diff] [blame] | 4487 | } else if (!ps.haveGids) { |
| 4488 | gp.gids = appendInts(gp.gids, bp.gids); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4489 | } |
| 4490 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4491 | Slog.w(TAG, "Not granting permission " + perm |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4492 | + " to package " + pkg.packageName |
| 4493 | + " because it was previously installed without"); |
| 4494 | } |
| 4495 | } else { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4496 | if (gp.grantedPermissions.remove(perm)) { |
| 4497 | changedPermission = true; |
| 4498 | gp.gids = removeInts(gp.gids, bp.gids); |
| 4499 | Slog.i(TAG, "Un-granting permission " + perm |
| 4500 | + " from package " + pkg.packageName |
| 4501 | + " (protectionLevel=" + bp.protectionLevel |
| 4502 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 4503 | + ")"); |
| 4504 | } else { |
| 4505 | Slog.w(TAG, "Not granting permission " + perm |
| 4506 | + " to package " + pkg.packageName |
| 4507 | + " (protectionLevel=" + bp.protectionLevel |
| 4508 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 4509 | + ")"); |
| 4510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4511 | } |
| 4512 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4513 | Slog.w(TAG, "Unknown permission " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4514 | + " in package " + pkg.packageName); |
| 4515 | } |
| 4516 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4517 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4518 | if ((changedPermission || replace) && !ps.permissionsFixed && |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 4519 | ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) || |
| 4520 | ((ps.pkgFlags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)){ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4521 | // This is the first that we have heard about this package, so the |
| 4522 | // permissions we have now selected are fixed until explicitly |
| 4523 | // changed. |
| 4524 | ps.permissionsFixed = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4525 | } |
Dianne Hackborn | bbfd58a | 2010-03-23 11:49:59 -0700 | [diff] [blame] | 4526 | ps.haveGids = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4527 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4529 | private final class ActivityIntentResolver |
| 4530 | extends IntentResolver<PackageParser.ActivityIntentInfo, ResolveInfo> { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4531 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4532 | boolean defaultOnly, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4533 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4534 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4535 | return super.queryIntent(intent, resolvedType, defaultOnly, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4536 | } |
| 4537 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4538 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, int flags, |
| 4539 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4540 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4541 | mFlags = flags; |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4542 | return super.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4543 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4544 | } |
| 4545 | |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4546 | public List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4547 | int flags, ArrayList<PackageParser.Activity> packageActivities, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4548 | if (!sUserManager.exists(userId)) return null; |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4549 | if (packageActivities == null) { |
| 4550 | return null; |
| 4551 | } |
| 4552 | mFlags = flags; |
| 4553 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4554 | final int N = packageActivities.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4555 | ArrayList<ArrayList<PackageParser.ActivityIntentInfo>> listCut = |
| 4556 | new ArrayList<ArrayList<PackageParser.ActivityIntentInfo>>(N); |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 4557 | |
| 4558 | ArrayList<PackageParser.ActivityIntentInfo> intentFilters; |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4559 | for (int i = 0; i < N; ++i) { |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 4560 | intentFilters = packageActivities.get(i).intents; |
| 4561 | if (intentFilters != null && intentFilters.size() > 0) { |
| 4562 | listCut.add(intentFilters); |
| 4563 | } |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4564 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4565 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4566 | } |
| 4567 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4568 | public final void addActivity(PackageParser.Activity a, String type) { |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 4569 | final boolean systemApp = isSystemApp(a.info.applicationInfo); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4570 | mActivities.put(a.getComponentName(), a); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4571 | if (DEBUG_SHOW_INFO) |
| 4572 | Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4573 | TAG, " " + type + " " + |
| 4574 | (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel : a.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4575 | if (DEBUG_SHOW_INFO) |
| 4576 | Log.v(TAG, " Class=" + a.info.name); |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4577 | final int NI = a.intents.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4578 | for (int j=0; j<NI; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4579 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 4580 | if (!systemApp && intent.getPriority() > 0 && "activity".equals(type)) { |
| 4581 | intent.setPriority(0); |
| 4582 | Log.w(TAG, "Package " + a.info.applicationInfo.packageName + " has activity " |
| 4583 | + a.className + " with priority > 0, forcing to 0"); |
| 4584 | } |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4585 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4586 | Log.v(TAG, " IntentFilter:"); |
| 4587 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4588 | } |
| 4589 | if (!intent.debugCheck()) { |
| 4590 | Log.w(TAG, "==> For Activity " + a.info.name); |
| 4591 | } |
| 4592 | addFilter(intent); |
| 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | public final void removeActivity(PackageParser.Activity a, String type) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4597 | mActivities.remove(a.getComponentName()); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4598 | if (DEBUG_SHOW_INFO) { |
| 4599 | Log.v(TAG, " " + type + " " |
| 4600 | + (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel |
| 4601 | : a.info.name) + ":"); |
| 4602 | Log.v(TAG, " Class=" + a.info.name); |
| 4603 | } |
| 4604 | final int NI = a.intents.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4605 | for (int j=0; j<NI; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4606 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4607 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4608 | Log.v(TAG, " IntentFilter:"); |
| 4609 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4610 | } |
| 4611 | removeFilter(intent); |
| 4612 | } |
| 4613 | } |
| 4614 | |
| 4615 | @Override |
| 4616 | protected boolean allowFilterResult( |
| 4617 | PackageParser.ActivityIntentInfo filter, List<ResolveInfo> dest) { |
| 4618 | ActivityInfo filterAi = filter.activity.info; |
| 4619 | for (int i=dest.size()-1; i>=0; i--) { |
| 4620 | ActivityInfo destAi = dest.get(i).activityInfo; |
| 4621 | if (destAi.name == filterAi.name |
| 4622 | && destAi.packageName == filterAi.packageName) { |
| 4623 | return false; |
| 4624 | } |
| 4625 | } |
| 4626 | return true; |
| 4627 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4629 | @Override |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4630 | protected boolean isFilterStopped(PackageParser.ActivityIntentInfo filter, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4631 | if (!sUserManager.exists(userId)) return true; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 4632 | PackageParser.Package p = filter.activity.owner; |
| 4633 | if (p != null) { |
| 4634 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 4635 | if (ps != null) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 4636 | // System apps are never considered stopped for purposes of |
| 4637 | // filtering, because there may be no way for the user to |
| 4638 | // actually re-launch them. |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4639 | return ps.getStopped(userId) && (ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 4640 | } |
| 4641 | } |
| 4642 | return false; |
| 4643 | } |
| 4644 | |
| 4645 | @Override |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4646 | protected String packageForFilter(PackageParser.ActivityIntentInfo info) { |
| 4647 | return info.activity.owner.packageName; |
| 4648 | } |
| 4649 | |
| 4650 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | protected ResolveInfo newResult(PackageParser.ActivityIntentInfo info, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4652 | int match, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4653 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4654 | if (!mSettings.isEnabledLPr(info.activity.info, mFlags, userId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4655 | return null; |
| 4656 | } |
| 4657 | final PackageParser.Activity activity = info.activity; |
| 4658 | if (mSafeMode && (activity.info.applicationInfo.flags |
| 4659 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 4660 | return null; |
| 4661 | } |
| 4662 | final ResolveInfo res = new ResolveInfo(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4663 | PackageSetting ps = (PackageSetting) activity.owner.mExtras; |
| 4664 | res.activityInfo = PackageParser.generateActivityInfo(activity, mFlags, |
| 4665 | ps != null ? ps.getStopped(userId) : false, |
| 4666 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 4667 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4668 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 4669 | res.filter = info; |
| 4670 | } |
| 4671 | res.priority = info.getPriority(); |
| 4672 | res.preferredOrder = activity.owner.mPreferredOrder; |
| 4673 | //System.out.println("Result: " + res.activityInfo.className + |
| 4674 | // " = " + res.priority); |
| 4675 | res.match = match; |
| 4676 | res.isDefault = info.hasDefault; |
| 4677 | res.labelRes = info.labelRes; |
| 4678 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 4679 | res.icon = info.icon; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 4680 | res.system = isSystemApp(res.activityInfo.applicationInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4681 | return res; |
| 4682 | } |
| 4683 | |
| 4684 | @Override |
| 4685 | protected void sortResults(List<ResolveInfo> results) { |
| 4686 | Collections.sort(results, mResolvePrioritySorter); |
| 4687 | } |
| 4688 | |
| 4689 | @Override |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4690 | protected void dumpFilter(PrintWriter out, String prefix, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4691 | PackageParser.ActivityIntentInfo filter) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4692 | out.print(prefix); out.print( |
| 4693 | Integer.toHexString(System.identityHashCode(filter.activity))); |
| 4694 | out.print(' '); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4695 | out.print(filter.activity.getComponentShortName()); |
| 4696 | out.print(" filter "); |
| 4697 | out.println(Integer.toHexString(System.identityHashCode(filter))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 4701 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 4702 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 4703 | // while (i.hasNext()) { |
| 4704 | // final ResolveInfo resolveInfo = i.next(); |
| 4705 | // if (isEnabledLP(resolveInfo.activityInfo)) { |
| 4706 | // retList.add(resolveInfo); |
| 4707 | // } |
| 4708 | // } |
| 4709 | // return retList; |
| 4710 | // } |
| 4711 | |
| 4712 | // Keys are String (activity class name), values are Activity. |
| 4713 | private final HashMap<ComponentName, PackageParser.Activity> mActivities |
| 4714 | = new HashMap<ComponentName, PackageParser.Activity>(); |
| 4715 | private int mFlags; |
| 4716 | } |
| 4717 | |
| 4718 | private final class ServiceIntentResolver |
| 4719 | extends IntentResolver<PackageParser.ServiceIntentInfo, ResolveInfo> { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4720 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4721 | boolean defaultOnly, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4722 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4723 | return super.queryIntent(intent, resolvedType, defaultOnly, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4724 | } |
| 4725 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4726 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, int flags, |
| 4727 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4728 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4729 | mFlags = flags; |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4730 | return super.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4731 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4732 | } |
| 4733 | |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4734 | public List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4735 | int flags, ArrayList<PackageParser.Service> packageServices, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4736 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4737 | if (packageServices == null) { |
| 4738 | return null; |
| 4739 | } |
| 4740 | mFlags = flags; |
| 4741 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4742 | final int N = packageServices.size(); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4743 | ArrayList<ArrayList<PackageParser.ServiceIntentInfo>> listCut = |
| 4744 | new ArrayList<ArrayList<PackageParser.ServiceIntentInfo>>(N); |
| 4745 | |
| 4746 | ArrayList<PackageParser.ServiceIntentInfo> intentFilters; |
| 4747 | for (int i = 0; i < N; ++i) { |
| 4748 | intentFilters = packageServices.get(i).intents; |
| 4749 | if (intentFilters != null && intentFilters.size() > 0) { |
| 4750 | listCut.add(intentFilters); |
| 4751 | } |
| 4752 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4753 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4754 | } |
| 4755 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4756 | public final void addService(PackageParser.Service s) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4757 | mServices.put(s.getComponentName(), s); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4758 | if (DEBUG_SHOW_INFO) { |
| 4759 | Log.v(TAG, " " |
| 4760 | + (s.info.nonLocalizedLabel != null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4761 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4762 | Log.v(TAG, " Class=" + s.info.name); |
| 4763 | } |
| 4764 | final int NI = s.intents.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4765 | int j; |
| 4766 | for (j=0; j<NI; j++) { |
| 4767 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4768 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4769 | Log.v(TAG, " IntentFilter:"); |
| 4770 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4771 | } |
| 4772 | if (!intent.debugCheck()) { |
| 4773 | Log.w(TAG, "==> For Service " + s.info.name); |
| 4774 | } |
| 4775 | addFilter(intent); |
| 4776 | } |
| 4777 | } |
| 4778 | |
| 4779 | public final void removeService(PackageParser.Service s) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4780 | mServices.remove(s.getComponentName()); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4781 | if (DEBUG_SHOW_INFO) { |
| 4782 | Log.v(TAG, " " + (s.info.nonLocalizedLabel != null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4783 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4784 | Log.v(TAG, " Class=" + s.info.name); |
| 4785 | } |
| 4786 | final int NI = s.intents.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4787 | int j; |
| 4788 | for (j=0; j<NI; j++) { |
| 4789 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4790 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4791 | Log.v(TAG, " IntentFilter:"); |
| 4792 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4793 | } |
| 4794 | removeFilter(intent); |
| 4795 | } |
| 4796 | } |
| 4797 | |
| 4798 | @Override |
| 4799 | protected boolean allowFilterResult( |
| 4800 | PackageParser.ServiceIntentInfo filter, List<ResolveInfo> dest) { |
| 4801 | ServiceInfo filterSi = filter.service.info; |
| 4802 | for (int i=dest.size()-1; i>=0; i--) { |
| 4803 | ServiceInfo destAi = dest.get(i).serviceInfo; |
| 4804 | if (destAi.name == filterSi.name |
| 4805 | && destAi.packageName == filterSi.packageName) { |
| 4806 | return false; |
| 4807 | } |
| 4808 | } |
| 4809 | return true; |
| 4810 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4811 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4812 | @Override |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4813 | protected boolean isFilterStopped(PackageParser.ServiceIntentInfo filter, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4814 | if (!sUserManager.exists(userId)) return true; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 4815 | PackageParser.Package p = filter.service.owner; |
| 4816 | if (p != null) { |
| 4817 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 4818 | if (ps != null) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 4819 | // System apps are never considered stopped for purposes of |
| 4820 | // filtering, because there may be no way for the user to |
| 4821 | // actually re-launch them. |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4822 | return ps.getStopped(userId) |
| 4823 | && (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 4824 | } |
| 4825 | } |
| 4826 | return false; |
| 4827 | } |
| 4828 | |
| 4829 | @Override |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4830 | protected String packageForFilter(PackageParser.ServiceIntentInfo info) { |
| 4831 | return info.service.owner.packageName; |
| 4832 | } |
| 4833 | |
| 4834 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4835 | protected ResolveInfo newResult(PackageParser.ServiceIntentInfo filter, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4836 | int match, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4837 | if (!sUserManager.exists(userId)) return null; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 4838 | final PackageParser.ServiceIntentInfo info = (PackageParser.ServiceIntentInfo)filter; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4839 | if (!mSettings.isEnabledLPr(info.service.info, mFlags, userId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4840 | return null; |
| 4841 | } |
| 4842 | final PackageParser.Service service = info.service; |
| 4843 | if (mSafeMode && (service.info.applicationInfo.flags |
| 4844 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 4845 | return null; |
| 4846 | } |
| 4847 | final ResolveInfo res = new ResolveInfo(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4848 | PackageSetting ps = (PackageSetting) service.owner.mExtras; |
| 4849 | res.serviceInfo = PackageParser.generateServiceInfo(service, mFlags, |
| 4850 | ps != null ? ps.getStopped(userId) : false, |
| 4851 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 4852 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4853 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 4854 | res.filter = filter; |
| 4855 | } |
| 4856 | res.priority = info.getPriority(); |
| 4857 | res.preferredOrder = service.owner.mPreferredOrder; |
| 4858 | //System.out.println("Result: " + res.activityInfo.className + |
| 4859 | // " = " + res.priority); |
| 4860 | res.match = match; |
| 4861 | res.isDefault = info.hasDefault; |
| 4862 | res.labelRes = info.labelRes; |
| 4863 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 4864 | res.icon = info.icon; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 4865 | res.system = isSystemApp(res.serviceInfo.applicationInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4866 | return res; |
| 4867 | } |
| 4868 | |
| 4869 | @Override |
| 4870 | protected void sortResults(List<ResolveInfo> results) { |
| 4871 | Collections.sort(results, mResolvePrioritySorter); |
| 4872 | } |
| 4873 | |
| 4874 | @Override |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4875 | protected void dumpFilter(PrintWriter out, String prefix, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4876 | PackageParser.ServiceIntentInfo filter) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4877 | out.print(prefix); out.print( |
| 4878 | Integer.toHexString(System.identityHashCode(filter.service))); |
| 4879 | out.print(' '); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4880 | out.print(filter.service.getComponentShortName()); |
| 4881 | out.print(" filter "); |
| 4882 | out.println(Integer.toHexString(System.identityHashCode(filter))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 4886 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 4887 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 4888 | // while (i.hasNext()) { |
| 4889 | // final ResolveInfo resolveInfo = (ResolveInfo) i; |
| 4890 | // if (isEnabledLP(resolveInfo.serviceInfo)) { |
| 4891 | // retList.add(resolveInfo); |
| 4892 | // } |
| 4893 | // } |
| 4894 | // return retList; |
| 4895 | // } |
| 4896 | |
| 4897 | // Keys are String (activity class name), values are Activity. |
| 4898 | private final HashMap<ComponentName, PackageParser.Service> mServices |
| 4899 | = new HashMap<ComponentName, PackageParser.Service>(); |
| 4900 | private int mFlags; |
| 4901 | }; |
| 4902 | |
| 4903 | private static final Comparator<ResolveInfo> mResolvePrioritySorter = |
| 4904 | new Comparator<ResolveInfo>() { |
| 4905 | public int compare(ResolveInfo r1, ResolveInfo r2) { |
| 4906 | int v1 = r1.priority; |
| 4907 | int v2 = r2.priority; |
| 4908 | //System.out.println("Comparing: q1=" + q1 + " q2=" + q2); |
| 4909 | if (v1 != v2) { |
| 4910 | return (v1 > v2) ? -1 : 1; |
| 4911 | } |
| 4912 | v1 = r1.preferredOrder; |
| 4913 | v2 = r2.preferredOrder; |
| 4914 | if (v1 != v2) { |
| 4915 | return (v1 > v2) ? -1 : 1; |
| 4916 | } |
| 4917 | if (r1.isDefault != r2.isDefault) { |
| 4918 | return r1.isDefault ? -1 : 1; |
| 4919 | } |
| 4920 | v1 = r1.match; |
| 4921 | v2 = r2.match; |
| 4922 | //System.out.println("Comparing: m1=" + m1 + " m2=" + m2); |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 4923 | if (v1 != v2) { |
| 4924 | return (v1 > v2) ? -1 : 1; |
| 4925 | } |
| 4926 | if (r1.system != r2.system) { |
| 4927 | return r1.system ? -1 : 1; |
| 4928 | } |
| 4929 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4930 | } |
| 4931 | }; |
| 4932 | |
| 4933 | private static final Comparator<ProviderInfo> mProviderInitOrderSorter = |
| 4934 | new Comparator<ProviderInfo>() { |
| 4935 | public int compare(ProviderInfo p1, ProviderInfo p2) { |
| 4936 | final int v1 = p1.initOrder; |
| 4937 | final int v2 = p2.initOrder; |
| 4938 | return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0); |
| 4939 | } |
| 4940 | }; |
| 4941 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4942 | static final void sendPackageBroadcast(String action, String pkg, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4943 | Bundle extras, String targetPkg, IIntentReceiver finishedReceiver, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4944 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 4945 | if (am != null) { |
| 4946 | try { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4947 | int[] userIds = userId == UserId.USER_ALL |
| 4948 | ? sUserManager.getUserIds() |
| 4949 | : new int[] {userId}; |
| 4950 | for (int id : userIds) { |
| 4951 | final Intent intent = new Intent(action, |
| 4952 | pkg != null ? Uri.fromParts("package", pkg, null) : null); |
| 4953 | if (extras != null) { |
| 4954 | intent.putExtras(extras); |
| 4955 | } |
| 4956 | if (targetPkg != null) { |
| 4957 | intent.setPackage(targetPkg); |
| 4958 | } |
| 4959 | // Modify the UID when posting to other users |
| 4960 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 4961 | if (uid > 0 && id > 0) { |
| 4962 | uid = UserId.getUid(id, UserId.getAppId(uid)); |
| 4963 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 4964 | } |
| 4965 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 4966 | am.broadcastIntent(null, intent, null, finishedReceiver, |
| 4967 | 0, null, null, null, finishedReceiver != null, false, id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4968 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4969 | } catch (RemoteException ex) { |
| 4970 | } |
| 4971 | } |
| 4972 | } |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 4973 | |
| 4974 | /** |
| 4975 | * Check if the external storage media is available. This is true if there |
| 4976 | * is a mounted external storage medium or if the external storage is |
| 4977 | * emulated. |
| 4978 | */ |
| 4979 | private boolean isExternalMediaAvailable() { |
| 4980 | return mMediaMounted || Environment.isExternalStorageEmulated(); |
| 4981 | } |
| 4982 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 4983 | public String nextPackageToClean(String lastPackage) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4984 | // writer |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 4985 | synchronized (mPackages) { |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 4986 | if (!isExternalMediaAvailable()) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 4987 | // If the external storage is no longer mounted at this point, |
| 4988 | // the caller may not have been able to delete all of this |
| 4989 | // packages files and can not delete any more. Bail. |
| 4990 | return null; |
| 4991 | } |
| 4992 | if (lastPackage != null) { |
| 4993 | mSettings.mPackagesToBeCleaned.remove(lastPackage); |
| 4994 | } |
| 4995 | return mSettings.mPackagesToBeCleaned.size() > 0 |
| 4996 | ? mSettings.mPackagesToBeCleaned.get(0) : null; |
| 4997 | } |
| 4998 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4999 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5000 | void schedulePackageCleaning(String packageName) { |
| 5001 | mHandler.sendMessage(mHandler.obtainMessage(START_CLEANING_PACKAGE, packageName)); |
| 5002 | } |
| 5003 | |
| 5004 | void startCleaningPackages() { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5005 | // reader |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5006 | synchronized (mPackages) { |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5007 | if (!isExternalMediaAvailable()) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5008 | return; |
| 5009 | } |
| 5010 | if (mSettings.mPackagesToBeCleaned.size() <= 0) { |
| 5011 | return; |
| 5012 | } |
| 5013 | } |
| 5014 | Intent intent = new Intent(PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE); |
| 5015 | intent.setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 5016 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 5017 | if (am != null) { |
| 5018 | try { |
| 5019 | am.startService(null, intent, null); |
| 5020 | } catch (RemoteException e) { |
| 5021 | } |
| 5022 | } |
| 5023 | } |
| 5024 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5025 | private final class AppDirObserver extends FileObserver { |
| 5026 | public AppDirObserver(String path, int mask, boolean isrom) { |
| 5027 | super(path, mask); |
| 5028 | mRootDir = path; |
| 5029 | mIsRom = isrom; |
| 5030 | } |
| 5031 | |
| 5032 | public void onEvent(int event, String path) { |
| 5033 | String removedPackage = null; |
| 5034 | int removedUid = -1; |
| 5035 | String addedPackage = null; |
| 5036 | int addedUid = -1; |
| 5037 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5038 | // TODO post a message to the handler to obtain serial ordering |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5039 | synchronized (mInstallLock) { |
| 5040 | String fullPathStr = null; |
| 5041 | File fullPath = null; |
| 5042 | if (path != null) { |
| 5043 | fullPath = new File(mRootDir, path); |
| 5044 | fullPathStr = fullPath.getPath(); |
| 5045 | } |
| 5046 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5047 | if (DEBUG_APP_DIR_OBSERVER) |
| 5048 | Log.v(TAG, "File " + fullPathStr + " changed: " + Integer.toHexString(event)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5049 | |
| 5050 | if (!isPackageFilename(path)) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5051 | if (DEBUG_APP_DIR_OBSERVER) |
| 5052 | Log.v(TAG, "Ignoring change of non-package file: " + fullPathStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5053 | return; |
| 5054 | } |
| 5055 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5056 | // Ignore packages that are being installed or |
| 5057 | // have just been installed. |
| 5058 | if (ignoreCodePath(fullPathStr)) { |
| 5059 | return; |
| 5060 | } |
| 5061 | PackageParser.Package p = null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5062 | // reader |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5063 | synchronized (mPackages) { |
| 5064 | p = mAppDirs.get(fullPathStr); |
| 5065 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5066 | if ((event&REMOVE_EVENTS) != 0) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5067 | if (p != null) { |
| 5068 | removePackageLI(p, true); |
| 5069 | removedPackage = p.applicationInfo.packageName; |
| 5070 | removedUid = p.applicationInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5071 | } |
| 5072 | } |
| 5073 | |
| 5074 | if ((event&ADD_EVENTS) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5075 | if (p == null) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5076 | p = scanPackageLI(fullPath, |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 5077 | (mIsRom ? PackageParser.PARSE_IS_SYSTEM |
| 5078 | | PackageParser.PARSE_IS_SYSTEM_DIR: 0) | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5079 | PackageParser.PARSE_CHATTY | |
| 5080 | PackageParser.PARSE_MUST_BE_APK, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 5081 | SCAN_MONITOR | SCAN_NO_PATHS | SCAN_UPDATE_TIME, |
| 5082 | System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5083 | if (p != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5084 | /* |
| 5085 | * TODO this seems dangerous as the package may have |
| 5086 | * changed since we last acquired the mPackages |
| 5087 | * lock. |
| 5088 | */ |
| 5089 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5090 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5091 | updatePermissionsLPw(p.packageName, p, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5092 | p.permissions.size() > 0 ? UPDATE_PERMISSIONS_ALL : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5093 | } |
| 5094 | addedPackage = p.applicationInfo.packageName; |
| 5095 | addedUid = p.applicationInfo.uid; |
| 5096 | } |
| 5097 | } |
| 5098 | } |
| 5099 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5100 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5101 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5102 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5103 | } |
| 5104 | } |
| 5105 | |
| 5106 | if (removedPackage != null) { |
| 5107 | Bundle extras = new Bundle(1); |
| 5108 | extras.putInt(Intent.EXTRA_UID, removedUid); |
| 5109 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, false); |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 5110 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5111 | extras, null, null, UserId.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5112 | } |
| 5113 | if (addedPackage != null) { |
| 5114 | Bundle extras = new Bundle(1); |
| 5115 | extras.putInt(Intent.EXTRA_UID, addedUid); |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 5116 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, addedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5117 | extras, null, null, UserId.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5118 | } |
| 5119 | } |
| 5120 | |
| 5121 | private final String mRootDir; |
| 5122 | private final boolean mIsRom; |
| 5123 | } |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5125 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 5126 | public void installPackage( |
| 5127 | final Uri packageURI, final IPackageInstallObserver observer, final int flags) { |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5128 | installPackage(packageURI, observer, flags, null); |
| 5129 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5130 | |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5131 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 5132 | public void installPackage( |
| 5133 | final Uri packageURI, final IPackageInstallObserver observer, final int flags, |
| 5134 | final String installerPackageName) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5135 | installPackageWithVerification(packageURI, observer, flags, installerPackageName, null, |
| 5136 | null); |
| 5137 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5138 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5139 | @Override |
| 5140 | public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer, |
| 5141 | int flags, String installerPackageName, Uri verificationURI, |
| 5142 | ManifestDigest manifestDigest) { |
| 5143 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, null); |
| 5144 | |
| 5145 | final int uid = Binder.getCallingUid(); |
| 5146 | |
| 5147 | final int filteredFlags; |
| 5148 | |
| 5149 | if (uid == Process.SHELL_UID || uid == 0) { |
| 5150 | if (DEBUG_INSTALL) { |
| 5151 | Slog.v(TAG, "Install from ADB"); |
| 5152 | } |
| 5153 | filteredFlags = flags | PackageManager.INSTALL_FROM_ADB; |
| 5154 | } else { |
| 5155 | filteredFlags = flags & ~PackageManager.INSTALL_FROM_ADB; |
| 5156 | } |
| 5157 | |
| 5158 | final Message msg = mHandler.obtainMessage(INIT_COPY); |
| 5159 | msg.obj = new InstallParams(packageURI, observer, filteredFlags, installerPackageName, |
| 5160 | verificationURI, manifestDigest); |
| 5161 | mHandler.sendMessage(msg); |
| 5162 | } |
| 5163 | |
| 5164 | @Override |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5165 | public void verifyPendingInstall(int id, int verificationCode) throws RemoteException { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5166 | final Message msg = mHandler.obtainMessage(PACKAGE_VERIFIED); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5167 | final PackageVerificationResponse response = new PackageVerificationResponse( |
| 5168 | verificationCode, Binder.getCallingUid()); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5169 | msg.arg1 = id; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5170 | msg.obj = response; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5171 | mHandler.sendMessage(msg); |
| 5172 | } |
| 5173 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5174 | private ComponentName matchComponentForVerifier(String packageName, |
| 5175 | List<ResolveInfo> receivers) { |
| 5176 | ActivityInfo targetReceiver = null; |
| 5177 | |
| 5178 | final int NR = receivers.size(); |
| 5179 | for (int i = 0; i < NR; i++) { |
| 5180 | final ResolveInfo info = receivers.get(i); |
| 5181 | if (info.activityInfo == null) { |
| 5182 | continue; |
| 5183 | } |
| 5184 | |
| 5185 | if (packageName.equals(info.activityInfo.packageName)) { |
| 5186 | targetReceiver = info.activityInfo; |
| 5187 | break; |
| 5188 | } |
| 5189 | } |
| 5190 | |
| 5191 | if (targetReceiver == null) { |
| 5192 | return null; |
| 5193 | } |
| 5194 | |
| 5195 | return new ComponentName(targetReceiver.packageName, targetReceiver.name); |
| 5196 | } |
| 5197 | |
| 5198 | private List<ComponentName> matchVerifiers(PackageInfoLite pkgInfo, |
| 5199 | List<ResolveInfo> receivers, final PackageVerificationState verificationState) { |
| 5200 | if (pkgInfo.verifiers.length == 0) { |
| 5201 | return null; |
| 5202 | } |
| 5203 | |
| 5204 | final int N = pkgInfo.verifiers.length; |
| 5205 | final List<ComponentName> sufficientVerifiers = new ArrayList<ComponentName>(N + 1); |
| 5206 | for (int i = 0; i < N; i++) { |
| 5207 | final VerifierInfo verifierInfo = pkgInfo.verifiers[i]; |
| 5208 | |
| 5209 | final ComponentName comp = matchComponentForVerifier(verifierInfo.packageName, |
| 5210 | receivers); |
| 5211 | if (comp == null) { |
| 5212 | continue; |
| 5213 | } |
| 5214 | |
| 5215 | final int verifierUid = getUidForVerifier(verifierInfo); |
| 5216 | if (verifierUid == -1) { |
| 5217 | continue; |
| 5218 | } |
| 5219 | |
| 5220 | if (DEBUG_VERIFY) { |
| 5221 | Slog.d(TAG, "Added sufficient verifier " + verifierInfo.packageName |
| 5222 | + " with the correct signature"); |
| 5223 | } |
| 5224 | sufficientVerifiers.add(comp); |
| 5225 | verificationState.addSufficientVerifier(verifierUid); |
| 5226 | } |
| 5227 | |
| 5228 | return sufficientVerifiers; |
| 5229 | } |
| 5230 | |
| 5231 | private int getUidForVerifier(VerifierInfo verifierInfo) { |
| 5232 | synchronized (mPackages) { |
| 5233 | final PackageParser.Package pkg = mPackages.get(verifierInfo.packageName); |
| 5234 | if (pkg == null) { |
| 5235 | return -1; |
| 5236 | } else if (pkg.mSignatures.length != 1) { |
| 5237 | Slog.i(TAG, "Verifier package " + verifierInfo.packageName |
| 5238 | + " has more than one signature; ignoring"); |
| 5239 | return -1; |
| 5240 | } |
| 5241 | |
| 5242 | /* |
| 5243 | * If the public key of the package's signature does not match |
| 5244 | * our expected public key, then this is a different package and |
| 5245 | * we should skip. |
| 5246 | */ |
| 5247 | |
| 5248 | final byte[] expectedPublicKey; |
| 5249 | try { |
| 5250 | final Signature verifierSig = pkg.mSignatures[0]; |
| 5251 | final PublicKey publicKey = verifierSig.getPublicKey(); |
| 5252 | expectedPublicKey = publicKey.getEncoded(); |
| 5253 | } catch (CertificateException e) { |
| 5254 | return -1; |
| 5255 | } |
| 5256 | |
| 5257 | final byte[] actualPublicKey = verifierInfo.publicKey.getEncoded(); |
| 5258 | |
| 5259 | if (!Arrays.equals(actualPublicKey, expectedPublicKey)) { |
| 5260 | Slog.i(TAG, "Verifier package " + verifierInfo.packageName |
| 5261 | + " does not have the expected public key; ignoring"); |
| 5262 | return -1; |
| 5263 | } |
| 5264 | |
| 5265 | return pkg.applicationInfo.uid; |
| 5266 | } |
| 5267 | } |
| 5268 | |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5269 | public void finishPackageInstall(int token) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 5270 | enforceSystemOrRoot("Only the system is allowed to finish installs"); |
| 5271 | |
| 5272 | if (DEBUG_INSTALL) { |
| 5273 | Slog.v(TAG, "BM finishing package install for " + token); |
| 5274 | } |
| 5275 | |
| 5276 | final Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5277 | mHandler.sendMessage(msg); |
| 5278 | } |
| 5279 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5280 | /** |
| 5281 | * Get the verification agent timeout. |
| 5282 | * |
| 5283 | * @return verification timeout in milliseconds |
| 5284 | */ |
| 5285 | private long getVerificationTimeout() { |
| 5286 | return android.provider.Settings.Secure.getLong(mContext.getContentResolver(), |
| 5287 | android.provider.Settings.Secure.PACKAGE_VERIFIER_TIMEOUT, |
| 5288 | DEFAULT_VERIFICATION_TIMEOUT); |
| 5289 | } |
| 5290 | |
| 5291 | /** |
| 5292 | * Check whether or not package verification has been enabled. |
| 5293 | * |
| 5294 | * @return true if verification should be performed |
| 5295 | */ |
| 5296 | private boolean isVerificationEnabled() { |
| 5297 | return android.provider.Settings.Secure.getInt(mContext.getContentResolver(), |
| 5298 | android.provider.Settings.Secure.PACKAGE_VERIFIER_ENABLE, |
| 5299 | DEFAULT_VERIFY_ENABLE ? 1 : 0) == 1 ? true : false; |
| 5300 | } |
| 5301 | |
Kenny Root | f03b45f | 2011-02-23 17:25:45 -0800 | [diff] [blame] | 5302 | public void setInstallerPackageName(String targetPackage, String installerPackageName) { |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5303 | final int uid = Binder.getCallingUid(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5304 | // writer |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5305 | synchronized (mPackages) { |
| 5306 | PackageSetting targetPackageSetting = mSettings.mPackages.get(targetPackage); |
| 5307 | if (targetPackageSetting == null) { |
| 5308 | throw new IllegalArgumentException("Unknown target package: " + targetPackage); |
| 5309 | } |
| 5310 | |
| 5311 | PackageSetting installerPackageSetting; |
| 5312 | if (installerPackageName != null) { |
| 5313 | installerPackageSetting = mSettings.mPackages.get(installerPackageName); |
| 5314 | if (installerPackageSetting == null) { |
| 5315 | throw new IllegalArgumentException("Unknown installer package: " |
| 5316 | + installerPackageName); |
| 5317 | } |
| 5318 | } else { |
| 5319 | installerPackageSetting = null; |
| 5320 | } |
| 5321 | |
| 5322 | Signature[] callerSignature; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5323 | Object obj = mSettings.getUserIdLPr(uid); |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5324 | if (obj != null) { |
| 5325 | if (obj instanceof SharedUserSetting) { |
| 5326 | callerSignature = ((SharedUserSetting)obj).signatures.mSignatures; |
| 5327 | } else if (obj instanceof PackageSetting) { |
| 5328 | callerSignature = ((PackageSetting)obj).signatures.mSignatures; |
| 5329 | } else { |
| 5330 | throw new SecurityException("Bad object " + obj + " for uid " + uid); |
| 5331 | } |
| 5332 | } else { |
| 5333 | throw new SecurityException("Unknown calling uid " + uid); |
| 5334 | } |
| 5335 | |
| 5336 | // Verify: can't set installerPackageName to a package that is |
| 5337 | // not signed with the same cert as the caller. |
| 5338 | if (installerPackageSetting != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5339 | if (compareSignatures(callerSignature, |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5340 | installerPackageSetting.signatures.mSignatures) |
| 5341 | != PackageManager.SIGNATURE_MATCH) { |
| 5342 | throw new SecurityException( |
| 5343 | "Caller does not have same cert as new installer package " |
| 5344 | + installerPackageName); |
| 5345 | } |
| 5346 | } |
| 5347 | |
| 5348 | // Verify: if target already has an installer package, it must |
| 5349 | // be signed with the same cert as the caller. |
| 5350 | if (targetPackageSetting.installerPackageName != null) { |
| 5351 | PackageSetting setting = mSettings.mPackages.get( |
| 5352 | targetPackageSetting.installerPackageName); |
| 5353 | // If the currently set package isn't valid, then it's always |
| 5354 | // okay to change it. |
| 5355 | if (setting != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5356 | if (compareSignatures(callerSignature, |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5357 | setting.signatures.mSignatures) |
| 5358 | != PackageManager.SIGNATURE_MATCH) { |
| 5359 | throw new SecurityException( |
| 5360 | "Caller does not have same cert as old installer package " |
| 5361 | + targetPackageSetting.installerPackageName); |
| 5362 | } |
| 5363 | } |
| 5364 | } |
| 5365 | |
| 5366 | // Okay! |
| 5367 | targetPackageSetting.installerPackageName = installerPackageName; |
| 5368 | scheduleWriteSettingsLocked(); |
| 5369 | } |
| 5370 | } |
| 5371 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5372 | private void processPendingInstall(final InstallArgs args, final int currentStatus) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5373 | // Queue up an async operation since the package installation may take a little while. |
| 5374 | mHandler.post(new Runnable() { |
| 5375 | public void run() { |
| 5376 | mHandler.removeCallbacks(this); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5377 | // Result object to be returned |
| 5378 | PackageInstalledInfo res = new PackageInstalledInfo(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5379 | res.returnCode = currentStatus; |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5380 | res.uid = -1; |
| 5381 | res.pkg = null; |
| 5382 | res.removedInfo = new PackageRemovedInfo(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5383 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5384 | args.doPreInstall(res.returnCode); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5385 | synchronized (mInstallLock) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5386 | installPackageLI(args, true, res); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5387 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5388 | args.doPostInstall(res.returnCode, res.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5389 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5390 | |
| 5391 | // A restore should be performed at this point if (a) the install |
| 5392 | // succeeded, (b) the operation is not an update, and (c) the new |
| 5393 | // package has a backupAgent defined. |
| 5394 | final boolean update = res.removedInfo.removedPackage != null; |
Christopher Tate | 59eac4b | 2010-02-19 19:25:45 -0800 | [diff] [blame] | 5395 | boolean doRestore = (!update |
| 5396 | && res.pkg != null |
| 5397 | && res.pkg.applicationInfo.backupAgentName != null); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5398 | |
| 5399 | // Set up the post-install work request bookkeeping. This will be used |
| 5400 | // and cleaned up by the post-install event handling regardless of whether |
| 5401 | // there's a restore pass performed. Token values are >= 1. |
| 5402 | int token; |
| 5403 | if (mNextInstallToken < 0) mNextInstallToken = 1; |
| 5404 | token = mNextInstallToken++; |
| 5405 | |
| 5406 | PostInstallData data = new PostInstallData(args, res); |
| 5407 | mRunningInstalls.put(token, data); |
| 5408 | if (DEBUG_INSTALL) Log.v(TAG, "+ starting restore round-trip " + token); |
| 5409 | |
| 5410 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED && doRestore) { |
| 5411 | // Pass responsibility to the Backup Manager. It will perform a |
| 5412 | // restore if appropriate, then pass responsibility back to the |
| 5413 | // Package Manager to run the post-install observer callbacks |
| 5414 | // and broadcasts. |
| 5415 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 5416 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 5417 | if (bm != null) { |
| 5418 | if (DEBUG_INSTALL) Log.v(TAG, "token " + token |
| 5419 | + " to BM for possible restore"); |
| 5420 | try { |
| 5421 | bm.restoreAtInstall(res.pkg.applicationInfo.packageName, token); |
| 5422 | } catch (RemoteException e) { |
| 5423 | // can't happen; the backup manager is local |
| 5424 | } catch (Exception e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5425 | Slog.e(TAG, "Exception trying to enqueue restore", e); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5426 | doRestore = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5427 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5428 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5429 | Slog.e(TAG, "Backup Manager not found!"); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5430 | doRestore = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5431 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5432 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5433 | |
| 5434 | if (!doRestore) { |
| 5435 | // No restore possible, or the Backup Manager was mysteriously not |
| 5436 | // available -- just fire the post-install work request directly. |
| 5437 | if (DEBUG_INSTALL) Log.v(TAG, "No restore - queue post-install for " + token); |
| 5438 | Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
| 5439 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5440 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5441 | } |
| 5442 | }); |
| 5443 | } |
| 5444 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5445 | private abstract class HandlerParams { |
| 5446 | private static final int MAX_RETRIES = 4; |
| 5447 | |
| 5448 | /** |
| 5449 | * Number of times startCopy() has been attempted and had a non-fatal |
| 5450 | * error. |
| 5451 | */ |
| 5452 | private int mRetries = 0; |
| 5453 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5454 | final boolean startCopy() { |
| 5455 | boolean res; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5456 | try { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5457 | if (DEBUG_INSTALL) Slog.i(TAG, "startCopy"); |
| 5458 | |
| 5459 | if (++mRetries > MAX_RETRIES) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5460 | Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5461 | mHandler.sendEmptyMessage(MCS_GIVE_UP); |
| 5462 | handleServiceError(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5463 | return false; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5464 | } else { |
| 5465 | handleStartCopy(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5466 | res = true; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5467 | } |
| 5468 | } catch (RemoteException e) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5469 | if (DEBUG_INSTALL) Slog.i(TAG, "Posting install MCS_RECONNECT"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5470 | mHandler.sendEmptyMessage(MCS_RECONNECT); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5471 | res = false; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5472 | } |
| 5473 | handleReturnCode(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5474 | return res; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | final void serviceError() { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5478 | if (DEBUG_INSTALL) Slog.i(TAG, "serviceError"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5479 | handleServiceError(); |
| 5480 | handleReturnCode(); |
| 5481 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5482 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5483 | abstract void handleStartCopy() throws RemoteException; |
| 5484 | abstract void handleServiceError(); |
| 5485 | abstract void handleReturnCode(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5486 | } |
| 5487 | |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5488 | class MeasureParams extends HandlerParams { |
| 5489 | private final PackageStats mStats; |
| 5490 | private boolean mSuccess; |
| 5491 | |
| 5492 | private final IPackageStatsObserver mObserver; |
| 5493 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 5494 | public MeasureParams(PackageStats stats, boolean success, IPackageStatsObserver observer) { |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5495 | mObserver = observer; |
| 5496 | mStats = stats; |
| 5497 | mSuccess = success; |
| 5498 | } |
| 5499 | |
| 5500 | @Override |
| 5501 | void handleStartCopy() throws RemoteException { |
| 5502 | final boolean mounted; |
| 5503 | |
| 5504 | if (Environment.isExternalStorageEmulated()) { |
| 5505 | mounted = true; |
| 5506 | } else { |
| 5507 | final String status = Environment.getExternalStorageState(); |
| 5508 | |
| 5509 | mounted = status.equals(Environment.MEDIA_MOUNTED) |
| 5510 | || status.equals(Environment.MEDIA_MOUNTED_READ_ONLY); |
| 5511 | } |
| 5512 | |
| 5513 | if (mounted) { |
| 5514 | final File externalCacheDir = Environment |
| 5515 | .getExternalStorageAppCacheDirectory(mStats.packageName); |
| 5516 | final long externalCacheSize = mContainerService |
| 5517 | .calculateDirectorySize(externalCacheDir.getPath()); |
| 5518 | mStats.externalCacheSize = externalCacheSize; |
| 5519 | |
| 5520 | final File externalDataDir = Environment |
| 5521 | .getExternalStorageAppDataDirectory(mStats.packageName); |
| 5522 | long externalDataSize = mContainerService.calculateDirectorySize(externalDataDir |
| 5523 | .getPath()); |
| 5524 | |
| 5525 | if (externalCacheDir.getParentFile().equals(externalDataDir)) { |
| 5526 | externalDataSize -= externalCacheSize; |
| 5527 | } |
| 5528 | mStats.externalDataSize = externalDataSize; |
| 5529 | |
| 5530 | final File externalMediaDir = Environment |
| 5531 | .getExternalStorageAppMediaDirectory(mStats.packageName); |
| 5532 | mStats.externalMediaSize = mContainerService |
Kenny Root | c7624d9 | 2011-02-23 16:25:25 -0800 | [diff] [blame] | 5533 | .calculateDirectorySize(externalMediaDir.getPath()); |
Kenny Root | bcd6c96 | 2011-01-17 11:21:49 -0800 | [diff] [blame] | 5534 | |
| 5535 | final File externalObbDir = Environment |
| 5536 | .getExternalStorageAppObbDirectory(mStats.packageName); |
| 5537 | mStats.externalObbSize = mContainerService.calculateDirectorySize(externalObbDir |
| 5538 | .getPath()); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5539 | } |
| 5540 | } |
| 5541 | |
| 5542 | @Override |
| 5543 | void handleReturnCode() { |
| 5544 | if (mObserver != null) { |
| 5545 | try { |
| 5546 | mObserver.onGetStatsCompleted(mStats, mSuccess); |
| 5547 | } catch (RemoteException e) { |
| 5548 | Slog.i(TAG, "Observer no longer exists."); |
| 5549 | } |
| 5550 | } |
| 5551 | } |
| 5552 | |
| 5553 | @Override |
| 5554 | void handleServiceError() { |
| 5555 | Slog.e(TAG, "Could not measure application " + mStats.packageName |
| 5556 | + " external storage"); |
| 5557 | } |
| 5558 | } |
| 5559 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5560 | class InstallParams extends HandlerParams { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5561 | final IPackageInstallObserver observer; |
| 5562 | int flags; |
| 5563 | final Uri packageURI; |
| 5564 | final String installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5565 | final Uri verificationURI; |
| 5566 | final ManifestDigest manifestDigest; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5567 | private InstallArgs mArgs; |
| 5568 | private int mRet; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5569 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5570 | InstallParams(Uri packageURI, |
| 5571 | IPackageInstallObserver observer, int flags, |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5572 | String installerPackageName, Uri verificationURI, ManifestDigest manifestDigest) { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5573 | this.packageURI = packageURI; |
| 5574 | this.flags = flags; |
| 5575 | this.observer = observer; |
| 5576 | this.installerPackageName = installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5577 | this.verificationURI = verificationURI; |
| 5578 | this.manifestDigest = manifestDigest; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5579 | } |
| 5580 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5581 | private int installLocationPolicy(PackageInfoLite pkgLite, int flags) { |
| 5582 | String packageName = pkgLite.packageName; |
| 5583 | int installLocation = pkgLite.installLocation; |
| 5584 | boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5585 | // reader |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5586 | synchronized (mPackages) { |
| 5587 | PackageParser.Package pkg = mPackages.get(packageName); |
| 5588 | if (pkg != null) { |
| 5589 | if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| 5590 | // Check for updated system application. |
| 5591 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 5592 | if (onSd) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5593 | Slog.w(TAG, "Cannot install update to system app on sdcard"); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5594 | return PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION; |
| 5595 | } |
| 5596 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 5597 | } else { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5598 | if (onSd) { |
| 5599 | // Install flag overrides everything. |
| 5600 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 5601 | } |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 5602 | // If current upgrade specifies particular preference |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5603 | if (installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
| 5604 | // Application explicitly specified internal. |
| 5605 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 5606 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { |
| 5607 | // App explictly prefers external. Let policy decide |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 5608 | } else { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5609 | // Prefer previous location |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 5610 | if (isExternal(pkg)) { |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 5611 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 5612 | } |
| 5613 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5614 | } |
| 5615 | } |
| 5616 | } else { |
| 5617 | // Invalid install. Return error code |
| 5618 | return PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS; |
| 5619 | } |
| 5620 | } |
| 5621 | } |
| 5622 | // All the special cases have been taken care of. |
| 5623 | // Return result based on recommended install location. |
| 5624 | if (onSd) { |
| 5625 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 5626 | } |
| 5627 | return pkgLite.recommendedInstallLocation; |
| 5628 | } |
| 5629 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5630 | /* |
| 5631 | * Invoke remote method to get package information and install |
| 5632 | * location values. Override install location based on default |
| 5633 | * policy if needed and then create install arguments based |
| 5634 | * on the install location. |
| 5635 | */ |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5636 | public void handleStartCopy() throws RemoteException { |
Suchi Amalapurapu | 1f9e1b4 | 2010-02-26 13:14:31 -0800 | [diff] [blame] | 5637 | int ret = PackageManager.INSTALL_SUCCEEDED; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5638 | final boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| 5639 | final boolean onInt = (flags & PackageManager.INSTALL_INTERNAL) != 0; |
| 5640 | PackageInfoLite pkgLite = null; |
| 5641 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5642 | if (onInt && onSd) { |
| 5643 | // Check if both bits are set. |
| 5644 | Slog.w(TAG, "Conflicting flags specified for installing on both internal and external"); |
| 5645 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5646 | } else { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 5647 | final long lowThreshold; |
| 5648 | |
| 5649 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 5650 | .getService(DeviceStorageMonitorService.SERVICE); |
| 5651 | if (dsm == null) { |
| 5652 | Log.w(TAG, "Couldn't get low memory threshold; no free limit imposed"); |
| 5653 | lowThreshold = 0L; |
| 5654 | } else { |
| 5655 | lowThreshold = dsm.getMemoryLowThreshold(); |
| 5656 | } |
| 5657 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5658 | // Remote call to find out default install location |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 5659 | try { |
| 5660 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 5661 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 5662 | pkgLite = mContainerService.getMinimalPackageInfo(packageURI, flags, |
| 5663 | lowThreshold); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 5664 | } finally { |
| 5665 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 5666 | } |
| 5667 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5668 | int loc = pkgLite.recommendedInstallLocation; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5669 | if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5670 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5671 | } else if (loc == PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5672 | ret = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5673 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5674 | ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 5675 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_APK) { |
| 5676 | ret = PackageManager.INSTALL_FAILED_INVALID_APK; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5677 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_URI) { |
| 5678 | ret = PackageManager.INSTALL_FAILED_INVALID_URI; |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5679 | } else if (loc == PackageHelper.RECOMMEND_MEDIA_UNAVAILABLE) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5680 | ret = PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5681 | } else { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5682 | // Override with defaults if needed. |
| 5683 | loc = installLocationPolicy(pkgLite, flags); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5684 | if (!onSd && !onInt) { |
| 5685 | // Override install location with flags |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5686 | if (loc == PackageHelper.RECOMMEND_INSTALL_EXTERNAL) { |
| 5687 | // Set the flag to install on external media. |
| 5688 | flags |= PackageManager.INSTALL_EXTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5689 | flags &= ~PackageManager.INSTALL_INTERNAL; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5690 | } else { |
| 5691 | // Make sure the flag for installing on external |
| 5692 | // media is unset |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5693 | flags |= PackageManager.INSTALL_INTERNAL; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5694 | flags &= ~PackageManager.INSTALL_EXTERNAL; |
| 5695 | } |
| 5696 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5697 | } |
| 5698 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5699 | |
| 5700 | final InstallArgs args = createInstallArgs(this); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5701 | mArgs = args; |
| 5702 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5703 | if (ret == PackageManager.INSTALL_SUCCEEDED) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5704 | /* |
| 5705 | * Determine if we have any installed package verifiers. If we |
| 5706 | * do, then we'll defer to them to verify the packages. |
| 5707 | */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5708 | final int requiredUid = mRequiredVerifierPackage == null ? -1 |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5709 | : getPackageUid(mRequiredVerifierPackage, 0); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5710 | if (requiredUid != -1 && isVerificationEnabled()) { |
Kenny Root | a503a0f | 2011-10-03 14:45:28 -0700 | [diff] [blame] | 5711 | final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION); |
| 5712 | verification.setDataAndType(packageURI, PACKAGE_MIME_TYPE); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5713 | verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5714 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5715 | final List<ResolveInfo> receivers = queryIntentReceivers(verification, null, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5716 | PackageManager.GET_DISABLED_COMPONENTS, 0 /* TODO: Which userId? */); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5717 | |
| 5718 | if (DEBUG_VERIFY) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5719 | Slog.d(TAG, "Found " + receivers.size() + " verifiers for intent " |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5720 | + verification.toString() + " with " + pkgLite.verifiers.length |
| 5721 | + " optional verifiers"); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | final int verificationId = mPendingVerificationToken++; |
| 5725 | |
| 5726 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_ID, verificationId); |
| 5727 | |
| 5728 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_INSTALLER_PACKAGE, |
| 5729 | installerPackageName); |
| 5730 | |
| 5731 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_INSTALL_FLAGS, flags); |
| 5732 | |
| 5733 | if (verificationURI != null) { |
| 5734 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_URI, |
| 5735 | verificationURI); |
| 5736 | } |
| 5737 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5738 | final PackageVerificationState verificationState = new PackageVerificationState( |
| 5739 | requiredUid, args); |
| 5740 | |
| 5741 | mPendingVerification.append(verificationId, verificationState); |
| 5742 | |
| 5743 | final List<ComponentName> sufficientVerifiers = matchVerifiers(pkgLite, |
| 5744 | receivers, verificationState); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5745 | |
| 5746 | /* |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5747 | * If any sufficient verifiers were listed in the package |
| 5748 | * manifest, attempt to ask them. |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5749 | */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5750 | if (sufficientVerifiers != null) { |
| 5751 | final int N = sufficientVerifiers.size(); |
| 5752 | if (N == 0) { |
| 5753 | Slog.i(TAG, "Additional verifiers required, but none installed."); |
| 5754 | ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
| 5755 | } else { |
| 5756 | for (int i = 0; i < N; i++) { |
| 5757 | final ComponentName verifierComponent = sufficientVerifiers.get(i); |
| 5758 | |
| 5759 | final Intent sufficientIntent = new Intent(verification); |
| 5760 | sufficientIntent.setComponent(verifierComponent); |
| 5761 | |
| 5762 | mContext.sendBroadcast(sufficientIntent); |
| 5763 | } |
| 5764 | } |
| 5765 | } |
| 5766 | |
| 5767 | final ComponentName requiredVerifierComponent = matchComponentForVerifier( |
| 5768 | mRequiredVerifierPackage, receivers); |
| 5769 | if (ret == PackageManager.INSTALL_SUCCEEDED |
| 5770 | && mRequiredVerifierPackage != null) { |
| 5771 | /* |
| 5772 | * Send the intent to the required verification agent, |
| 5773 | * but only start the verification timeout after the |
| 5774 | * target BroadcastReceivers have run. |
| 5775 | */ |
| 5776 | verification.setComponent(requiredVerifierComponent); |
| 5777 | mContext.sendOrderedBroadcast(verification, |
| 5778 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 5779 | new BroadcastReceiver() { |
| 5780 | @Override |
| 5781 | public void onReceive(Context context, Intent intent) { |
| 5782 | final Message msg = mHandler |
| 5783 | .obtainMessage(CHECK_PENDING_VERIFICATION); |
| 5784 | msg.arg1 = verificationId; |
| 5785 | mHandler.sendMessageDelayed(msg, getVerificationTimeout()); |
| 5786 | } |
| 5787 | }, null, 0, null, null); |
| 5788 | |
| 5789 | /* |
| 5790 | * We don't want the copy to proceed until verification |
| 5791 | * succeeds, so null out this field. |
| 5792 | */ |
| 5793 | mArgs = null; |
| 5794 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5795 | } else { |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5796 | /* |
| 5797 | * No package verification is enabled, so immediately start |
| 5798 | * the remote call to initiate copy using temporary file. |
| 5799 | */ |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5800 | ret = args.copyApk(mContainerService, true); |
| 5801 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5802 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5803 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5804 | mRet = ret; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5805 | } |
| 5806 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5807 | @Override |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5808 | void handleReturnCode() { |
Kenny Root | 6f89fa0 | 2010-07-30 16:33:47 -0700 | [diff] [blame] | 5809 | // If mArgs is null, then MCS couldn't be reached. When it |
| 5810 | // reconnects, it will try again to install. At that point, this |
| 5811 | // will succeed. |
| 5812 | if (mArgs != null) { |
| 5813 | processPendingInstall(mArgs, mRet); |
| 5814 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5815 | } |
| 5816 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5817 | @Override |
| 5818 | void handleServiceError() { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5819 | mArgs = createInstallArgs(this); |
| 5820 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5821 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5822 | |
| 5823 | public boolean isForwardLocked() { |
| 5824 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 5825 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5826 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5827 | |
| 5828 | /* |
| 5829 | * Utility class used in movePackage api. |
| 5830 | * srcArgs and targetArgs are not set for invalid flags and make |
| 5831 | * sure to do null checks when invoking methods on them. |
| 5832 | * We probably want to return ErrorPrams for both failed installs |
| 5833 | * and moves. |
| 5834 | */ |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5835 | class MoveParams extends HandlerParams { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5836 | final IPackageMoveObserver observer; |
| 5837 | final int flags; |
| 5838 | final String packageName; |
| 5839 | final InstallArgs srcArgs; |
| 5840 | final InstallArgs targetArgs; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5841 | int uid; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5842 | int mRet; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 5843 | |
| 5844 | MoveParams(InstallArgs srcArgs, IPackageMoveObserver observer, int flags, |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5845 | String packageName, String dataDir, int uid) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5846 | this.srcArgs = srcArgs; |
| 5847 | this.observer = observer; |
| 5848 | this.flags = flags; |
| 5849 | this.packageName = packageName; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5850 | this.uid = uid; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5851 | if (srcArgs != null) { |
| 5852 | Uri packageUri = Uri.fromFile(new File(srcArgs.getCodePath())); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 5853 | targetArgs = createInstallArgs(packageUri, flags, packageName, dataDir); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5854 | } else { |
| 5855 | targetArgs = null; |
| 5856 | } |
| 5857 | } |
| 5858 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5859 | public void handleStartCopy() throws RemoteException { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5860 | mRet = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 5861 | // Check for storage space on target medium |
| 5862 | if (!targetArgs.checkFreeStorage(mContainerService)) { |
| 5863 | Log.w(TAG, "Insufficient storage to install"); |
| 5864 | return; |
| 5865 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 5866 | |
| 5867 | mRet = srcArgs.doPreCopy(); |
| 5868 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 5869 | return; |
| 5870 | } |
| 5871 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5872 | mRet = targetArgs.copyApk(mContainerService, false); |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 5873 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 5874 | srcArgs.doPostCopy(uid); |
| 5875 | return; |
| 5876 | } |
| 5877 | |
| 5878 | mRet = srcArgs.doPostCopy(uid); |
| 5879 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 5880 | return; |
| 5881 | } |
| 5882 | |
| 5883 | mRet = targetArgs.doPreInstall(mRet); |
| 5884 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 5885 | return; |
| 5886 | } |
| 5887 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5888 | if (DEBUG_SD_INSTALL) { |
| 5889 | StringBuilder builder = new StringBuilder(); |
| 5890 | if (srcArgs != null) { |
| 5891 | builder.append("src: "); |
| 5892 | builder.append(srcArgs.getCodePath()); |
| 5893 | } |
| 5894 | if (targetArgs != null) { |
| 5895 | builder.append(" target : "); |
| 5896 | builder.append(targetArgs.getCodePath()); |
| 5897 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5898 | Log.i(TAG, builder.toString()); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5899 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5900 | } |
| 5901 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5902 | @Override |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5903 | void handleReturnCode() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5904 | targetArgs.doPostInstall(mRet, uid); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5905 | int currentStatus = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 5906 | if (mRet == PackageManager.INSTALL_SUCCEEDED) { |
| 5907 | currentStatus = PackageManager.MOVE_SUCCEEDED; |
| 5908 | } else if (mRet == PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE){ |
| 5909 | currentStatus = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 5910 | } |
| 5911 | processPendingMove(this, currentStatus); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5912 | } |
| 5913 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5914 | @Override |
| 5915 | void handleServiceError() { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5916 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5917 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5918 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5919 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5920 | /** |
| 5921 | * Used during creation of InstallArgs |
| 5922 | * |
| 5923 | * @param flags package installation flags |
| 5924 | * @return true if should be installed on external storage |
| 5925 | */ |
| 5926 | private static boolean installOnSd(int flags) { |
| 5927 | if ((flags & PackageManager.INSTALL_INTERNAL) != 0) { |
| 5928 | return false; |
| 5929 | } |
| 5930 | if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
| 5931 | return true; |
| 5932 | } |
| 5933 | return false; |
| 5934 | } |
| 5935 | |
| 5936 | /** |
| 5937 | * Used during creation of InstallArgs |
| 5938 | * |
| 5939 | * @param flags package installation flags |
| 5940 | * @return true if should be installed as forward locked |
| 5941 | */ |
| 5942 | private static boolean installForwardLocked(int flags) { |
| 5943 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 5944 | } |
| 5945 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5946 | private InstallArgs createInstallArgs(InstallParams params) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5947 | if (installOnSd(params.flags) || params.isForwardLocked()) { |
| 5948 | return new AsecInstallArgs(params); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5949 | } else { |
| 5950 | return new FileInstallArgs(params); |
| 5951 | } |
| 5952 | } |
| 5953 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 5954 | private InstallArgs createInstallArgs(int flags, String fullCodePath, String fullResourcePath, |
| 5955 | String nativeLibraryPath) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5956 | if (installOnSd(flags) || installForwardLocked(flags)) { |
| 5957 | return new AsecInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath, |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 5958 | installOnSd(flags), installForwardLocked(flags)); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5959 | } else { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 5960 | return new FileInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5961 | } |
| 5962 | } |
| 5963 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 5964 | // Used by package mover |
| 5965 | private InstallArgs createInstallArgs(Uri packageURI, int flags, String pkgName, String dataDir) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5966 | if (installOnSd(flags) || installForwardLocked(flags)) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 5967 | String cid = getNextCodePath(packageURI.getPath(), pkgName, "/" |
| 5968 | + AsecInstallArgs.RES_FILE_NAME); |
| 5969 | return new AsecInstallArgs(packageURI, cid, installOnSd(flags), |
| 5970 | installForwardLocked(flags)); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5971 | } else { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 5972 | return new FileInstallArgs(packageURI, pkgName, dataDir); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5973 | } |
| 5974 | } |
| 5975 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5976 | static abstract class InstallArgs { |
| 5977 | final IPackageInstallObserver observer; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5978 | // Always refers to PackageManager flags only |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5979 | final int flags; |
| 5980 | final Uri packageURI; |
| 5981 | final String installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5982 | final ManifestDigest manifestDigest; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5983 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5984 | InstallArgs(Uri packageURI, IPackageInstallObserver observer, int flags, |
| 5985 | String installerPackageName, ManifestDigest manifestDigest) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5986 | this.packageURI = packageURI; |
| 5987 | this.flags = flags; |
| 5988 | this.observer = observer; |
| 5989 | this.installerPackageName = installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5990 | this.manifestDigest = manifestDigest; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5991 | } |
| 5992 | |
| 5993 | abstract void createCopyFile(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5994 | abstract int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 5995 | abstract int doPreInstall(int status); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5996 | abstract boolean doRename(int status, String pkgName, String oldCodePath); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5997 | |
| 5998 | abstract int doPostInstall(int status, int uid); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5999 | abstract String getCodePath(); |
| 6000 | abstract String getResourcePath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6001 | abstract String getNativeLibraryPath(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6002 | // Need installer lock especially for dex file removal. |
| 6003 | abstract void cleanUpResourcesLI(); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6004 | abstract boolean doPostDeleteLI(boolean delete); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6005 | abstract boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6006 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6007 | /** |
| 6008 | * Called before the source arguments are copied. This is used mostly |
| 6009 | * for MoveParams when it needs to read the source file to put it in the |
| 6010 | * destination. |
| 6011 | */ |
| 6012 | int doPreCopy() { |
| 6013 | return PackageManager.INSTALL_SUCCEEDED; |
| 6014 | } |
| 6015 | |
| 6016 | /** |
| 6017 | * Called after the source arguments are copied. This is used mostly for |
| 6018 | * MoveParams when it needs to read the source file to put it in the |
| 6019 | * destination. |
| 6020 | * |
| 6021 | * @return |
| 6022 | */ |
| 6023 | int doPostCopy(int uid) { |
| 6024 | return PackageManager.INSTALL_SUCCEEDED; |
| 6025 | } |
| 6026 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6027 | protected boolean isFwdLocked() { |
| 6028 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 6029 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6030 | } |
| 6031 | |
| 6032 | class FileInstallArgs extends InstallArgs { |
| 6033 | File installDir; |
| 6034 | String codeFileName; |
| 6035 | String resourceFileName; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6036 | String libraryPath; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6037 | boolean created = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6038 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6039 | FileInstallArgs(InstallParams params) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6040 | super(params.packageURI, params.observer, params.flags, params.installerPackageName, |
| 6041 | params.manifestDigest); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6042 | } |
| 6043 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6044 | FileInstallArgs(String fullCodePath, String fullResourcePath, String nativeLibraryPath) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6045 | super(null, null, 0, null, null); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6046 | File codeFile = new File(fullCodePath); |
| 6047 | installDir = codeFile.getParentFile(); |
| 6048 | codeFileName = fullCodePath; |
| 6049 | resourceFileName = fullResourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6050 | libraryPath = nativeLibraryPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6051 | } |
| 6052 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6053 | FileInstallArgs(Uri packageURI, String pkgName, String dataDir) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6054 | super(packageURI, null, 0, null, null); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6055 | installDir = isFwdLocked() ? mDrmAppPrivateInstallDir : mAppInstallDir; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6056 | String apkName = getNextCodePath(null, pkgName, ".apk"); |
| 6057 | codeFileName = new File(installDir, apkName + ".apk").getPath(); |
| 6058 | resourceFileName = getResourcePathFromCodePath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6059 | libraryPath = new File(dataDir, LIB_DIR_NAME).getPath(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6060 | } |
| 6061 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6062 | boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 6063 | final long lowThreshold; |
| 6064 | |
| 6065 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 6066 | .getService(DeviceStorageMonitorService.SERVICE); |
| 6067 | if (dsm == null) { |
| 6068 | Log.w(TAG, "Couldn't get low memory threshold; no free limit imposed"); |
| 6069 | lowThreshold = 0L; |
| 6070 | } else { |
| 6071 | if (dsm.isMemoryLow()) { |
| 6072 | Log.w(TAG, "Memory is reported as being too low; aborting package install"); |
| 6073 | return false; |
| 6074 | } |
| 6075 | |
| 6076 | lowThreshold = dsm.getMemoryLowThreshold(); |
| 6077 | } |
| 6078 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6079 | try { |
| 6080 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6081 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6082 | return imcs.checkInternalFreeStorage(packageURI, isFwdLocked(), lowThreshold); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6083 | } finally { |
| 6084 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6085 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6086 | } |
| 6087 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6088 | String getCodePath() { |
| 6089 | return codeFileName; |
| 6090 | } |
| 6091 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6092 | void createCopyFile() { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6093 | installDir = isFwdLocked() ? mDrmAppPrivateInstallDir : mAppInstallDir; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6094 | codeFileName = createTempPackageFile(installDir).getPath(); |
| 6095 | resourceFileName = getResourcePathFromCodePath(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6096 | created = true; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6097 | } |
| 6098 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6099 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6100 | if (temp) { |
| 6101 | // Generate temp file name |
| 6102 | createCopyFile(); |
| 6103 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6104 | // Get a ParcelFileDescriptor to write to the output file |
| 6105 | File codeFile = new File(codeFileName); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6106 | if (!created) { |
| 6107 | try { |
| 6108 | codeFile.createNewFile(); |
| 6109 | // Set permissions |
| 6110 | if (!setPermissions()) { |
| 6111 | // Failed setting permissions. |
| 6112 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6113 | } |
| 6114 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6115 | Slog.w(TAG, "Failed to create file " + codeFile); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6116 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6117 | } |
| 6118 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6119 | ParcelFileDescriptor out = null; |
| 6120 | try { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6121 | out = ParcelFileDescriptor.open(codeFile, ParcelFileDescriptor.MODE_READ_WRITE); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6122 | } catch (FileNotFoundException e) { |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 6123 | Slog.e(TAG, "Failed to create file descriptor for : " + codeFileName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6124 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6125 | } |
| 6126 | // Copy the resource now |
| 6127 | int ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6128 | try { |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6129 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6130 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 6131 | ret = imcs.copyResource(packageURI, out); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6132 | } finally { |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6133 | IoUtils.closeQuietly(out); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6134 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6135 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6136 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6137 | if (isFwdLocked()) { |
| 6138 | final File destResourceFile = new File(getResourcePath()); |
| 6139 | |
| 6140 | // Copy the public files |
| 6141 | try { |
| 6142 | PackageHelper.extractPublicFiles(codeFileName, destResourceFile); |
| 6143 | } catch (IOException e) { |
| 6144 | Slog.e(TAG, "Couldn't create a new zip file for the public parts of a" |
| 6145 | + " forward-locked app."); |
| 6146 | destResourceFile.delete(); |
| 6147 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6148 | } |
| 6149 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6150 | return ret; |
| 6151 | } |
| 6152 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6153 | int doPreInstall(int status) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6154 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6155 | cleanUp(); |
| 6156 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6157 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6158 | } |
| 6159 | |
| 6160 | boolean doRename(int status, final String pkgName, String oldCodePath) { |
| 6161 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6162 | cleanUp(); |
| 6163 | return false; |
| 6164 | } else { |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6165 | final File oldCodeFile = new File(getCodePath()); |
| 6166 | final File oldResourceFile = new File(getResourcePath()); |
| 6167 | |
| 6168 | // Rename APK file based on packageName |
| 6169 | final String apkName = getNextCodePath(oldCodePath, pkgName, ".apk"); |
| 6170 | final File newCodeFile = new File(installDir, apkName + ".apk"); |
| 6171 | if (!oldCodeFile.renameTo(newCodeFile)) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6172 | return false; |
| 6173 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6174 | codeFileName = newCodeFile.getPath(); |
| 6175 | |
| 6176 | // Rename public resource file if it's forward-locked. |
| 6177 | final File newResFile = new File(getResourcePathFromCodePath()); |
| 6178 | if (isFwdLocked() && !oldResourceFile.renameTo(newResFile)) { |
| 6179 | return false; |
| 6180 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6181 | resourceFileName = getResourcePathFromCodePath(); |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6182 | |
| 6183 | // Attempt to set permissions |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6184 | if (!setPermissions()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6185 | return false; |
| 6186 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6187 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6188 | return true; |
| 6189 | } |
| 6190 | } |
| 6191 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6192 | int doPostInstall(int status, int uid) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6193 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6194 | cleanUp(); |
| 6195 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6196 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | String getResourcePath() { |
| 6200 | return resourceFileName; |
| 6201 | } |
| 6202 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6203 | private String getResourcePathFromCodePath() { |
| 6204 | final String codePath = getCodePath(); |
| 6205 | if (isFwdLocked()) { |
| 6206 | final StringBuilder sb = new StringBuilder(); |
| 6207 | |
| 6208 | sb.append(mAppInstallDir.getPath()); |
| 6209 | sb.append('/'); |
| 6210 | sb.append(getApkName(codePath)); |
| 6211 | sb.append(".zip"); |
| 6212 | |
| 6213 | /* |
| 6214 | * If our APK is a temporary file, mark the resource as a |
| 6215 | * temporary file as well so it can be cleaned up after |
| 6216 | * catastrophic failure. |
| 6217 | */ |
| 6218 | if (codePath.endsWith(".tmp")) { |
| 6219 | sb.append(".tmp"); |
| 6220 | } |
| 6221 | |
| 6222 | return sb.toString(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6223 | } else { |
| 6224 | return codePath; |
| 6225 | } |
| 6226 | } |
| 6227 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6228 | @Override |
| 6229 | String getNativeLibraryPath() { |
| 6230 | return libraryPath; |
| 6231 | } |
| 6232 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6233 | private boolean cleanUp() { |
| 6234 | boolean ret = true; |
| 6235 | String sourceDir = getCodePath(); |
| 6236 | String publicSourceDir = getResourcePath(); |
| 6237 | if (sourceDir != null) { |
| 6238 | File sourceFile = new File(sourceDir); |
| 6239 | if (!sourceFile.exists()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6240 | Slog.w(TAG, "Package source " + sourceDir + " does not exist."); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6241 | ret = false; |
| 6242 | } |
| 6243 | // Delete application's code and resources |
| 6244 | sourceFile.delete(); |
| 6245 | } |
| 6246 | if (publicSourceDir != null && !publicSourceDir.equals(sourceDir)) { |
| 6247 | final File publicSourceFile = new File(publicSourceDir); |
| 6248 | if (!publicSourceFile.exists()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6249 | Slog.w(TAG, "Package public source " + publicSourceFile + " does not exist."); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6250 | } |
| 6251 | if (publicSourceFile.exists()) { |
| 6252 | publicSourceFile.delete(); |
| 6253 | } |
| 6254 | } |
| 6255 | return ret; |
| 6256 | } |
| 6257 | |
| 6258 | void cleanUpResourcesLI() { |
| 6259 | String sourceDir = getCodePath(); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 6260 | if (cleanUp()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6261 | int retCode = mInstaller.rmdex(sourceDir); |
| 6262 | if (retCode < 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6263 | Slog.w(TAG, "Couldn't remove dex file for package: " |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6264 | + " at location " |
| 6265 | + sourceDir + ", retcode=" + retCode); |
| 6266 | // we don't consider this to be a failure of the core package deletion |
| 6267 | } |
| 6268 | } |
| 6269 | } |
| 6270 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6271 | private boolean setPermissions() { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6272 | // TODO Do this in a more elegant way later on. for now just a hack |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6273 | if (!isFwdLocked()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6274 | final int filePermissions = |
| 6275 | FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP |
| 6276 | |FileUtils.S_IROTH; |
| 6277 | int retCode = FileUtils.setPermissions(getCodePath(), filePermissions, -1, -1); |
| 6278 | if (retCode != 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6279 | Slog.e(TAG, "Couldn't set new package file permissions for " + |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6280 | getCodePath() |
| 6281 | + ". The return code was: " + retCode); |
| 6282 | // TODO Define new internal error |
| 6283 | return false; |
| 6284 | } |
| 6285 | return true; |
| 6286 | } |
| 6287 | return true; |
| 6288 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6289 | |
| 6290 | boolean doPostDeleteLI(boolean delete) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6291 | // XXX err, shouldn't we respect the delete flag? |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6292 | cleanUpResourcesLI(); |
| 6293 | return true; |
| 6294 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6295 | } |
| 6296 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 6297 | /** |
| 6298 | * Extract the MountService "container ID" from the full code path of an |
| 6299 | * .apk. |
| 6300 | */ |
| 6301 | static String cidFromCodePath(String fullCodePath) { |
| 6302 | int eidx = fullCodePath.lastIndexOf("/"); |
| 6303 | String subStr1 = fullCodePath.substring(0, eidx); |
| 6304 | int sidx = subStr1.lastIndexOf("/"); |
| 6305 | return subStr1.substring(sidx+1, eidx); |
| 6306 | } |
| 6307 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6308 | class AsecInstallArgs extends InstallArgs { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6309 | static final String RES_FILE_NAME = "pkg.apk"; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6310 | static final String PUBLIC_RES_FILE_NAME = "res.zip"; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6311 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6312 | String cid; |
| 6313 | String packagePath; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6314 | String resourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6315 | String libraryPath; |
| 6316 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6317 | AsecInstallArgs(InstallParams params) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6318 | super(params.packageURI, params.observer, params.flags, params.installerPackageName, |
| 6319 | params.manifestDigest); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6320 | } |
| 6321 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6322 | AsecInstallArgs(String fullCodePath, String fullResourcePath, String nativeLibraryPath, |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6323 | boolean isExternal, boolean isForwardLocked) { |
| 6324 | super(null, null, (isExternal ? PackageManager.INSTALL_EXTERNAL : 0) |
| 6325 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), null, null); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6326 | // Extract cid from fullCodePath |
| 6327 | int eidx = fullCodePath.lastIndexOf("/"); |
| 6328 | String subStr1 = fullCodePath.substring(0, eidx); |
| 6329 | int sidx = subStr1.lastIndexOf("/"); |
| 6330 | cid = subStr1.substring(sidx+1, eidx); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6331 | setCachePath(subStr1); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6332 | } |
| 6333 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6334 | AsecInstallArgs(String cid) { |
| 6335 | super(null, null, 0, null, null); |
Dianne Hackborn | aa77de1 | 2010-05-14 22:33:54 -0700 | [diff] [blame] | 6336 | this.cid = cid; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6337 | setCachePath(PackageHelper.getSdDir(cid)); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6338 | } |
| 6339 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6340 | AsecInstallArgs(Uri packageURI, String cid, boolean isExternal, boolean isForwardLocked) { |
| 6341 | super(packageURI, null, (isExternal ? PackageManager.INSTALL_EXTERNAL : 0) |
| 6342 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), null, null); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6343 | this.cid = cid; |
| 6344 | } |
| 6345 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6346 | void createCopyFile() { |
| 6347 | cid = getTempContainerId(); |
| 6348 | } |
| 6349 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6350 | boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException { |
| 6351 | try { |
| 6352 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6353 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6354 | return imcs.checkExternalFreeStorage(packageURI, isFwdLocked()); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6355 | } finally { |
| 6356 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6357 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6358 | } |
| 6359 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6360 | private final boolean isExternal() { |
| 6361 | return (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| 6362 | } |
| 6363 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6364 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6365 | if (temp) { |
| 6366 | createCopyFile(); |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 6367 | } else { |
| 6368 | /* |
| 6369 | * Pre-emptively destroy the container since it's destroyed if |
| 6370 | * copying fails due to it existing anyway. |
| 6371 | */ |
| 6372 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6373 | } |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6374 | |
| 6375 | final String newCachePath; |
| 6376 | try { |
| 6377 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6378 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6379 | newCachePath = imcs.copyResourceToContainer(packageURI, cid, getEncryptKey(), |
| 6380 | RES_FILE_NAME, PUBLIC_RES_FILE_NAME, isExternal(), isFwdLocked()); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6381 | } finally { |
| 6382 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6383 | } |
| 6384 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6385 | if (newCachePath != null) { |
| 6386 | setCachePath(newCachePath); |
| 6387 | return PackageManager.INSTALL_SUCCEEDED; |
| 6388 | } else { |
| 6389 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6390 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6391 | } |
| 6392 | |
| 6393 | @Override |
| 6394 | String getCodePath() { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6395 | return packagePath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6396 | } |
| 6397 | |
| 6398 | @Override |
| 6399 | String getResourcePath() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6400 | return resourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6401 | } |
| 6402 | |
| 6403 | @Override |
| 6404 | String getNativeLibraryPath() { |
| 6405 | return libraryPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6406 | } |
| 6407 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6408 | int doPreInstall(int status) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6409 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6410 | // Destroy container |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6411 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6412 | } else { |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6413 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6414 | if (!mounted) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6415 | String newCachePath = PackageHelper.mountSdDir(cid, getEncryptKey(), |
| 6416 | Process.SYSTEM_UID); |
| 6417 | if (newCachePath != null) { |
| 6418 | setCachePath(newCachePath); |
| 6419 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6420 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6421 | } |
| 6422 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6423 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6424 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | boolean doRename(int status, final String pkgName, |
| 6428 | String oldCodePath) { |
| 6429 | String newCacheId = getNextCodePath(oldCodePath, pkgName, "/" + RES_FILE_NAME); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6430 | String newCachePath = null; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6431 | if (PackageHelper.isContainerMounted(cid)) { |
| 6432 | // Unmount the container |
| 6433 | if (!PackageHelper.unMountSdDir(cid)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6434 | Slog.i(TAG, "Failed to unmount " + cid + " before renaming"); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6435 | return false; |
| 6436 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6437 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6438 | if (!PackageHelper.renameSdDir(cid, newCacheId)) { |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 6439 | Slog.e(TAG, "Failed to rename " + cid + " to " + newCacheId + |
| 6440 | " which might be stale. Will try to clean up."); |
| 6441 | // Clean up the stale container and proceed to recreate. |
| 6442 | if (!PackageHelper.destroySdDir(newCacheId)) { |
| 6443 | Slog.e(TAG, "Very strange. Cannot clean up stale container " + newCacheId); |
| 6444 | return false; |
| 6445 | } |
| 6446 | // Successfully cleaned up stale container. Try to rename again. |
| 6447 | if (!PackageHelper.renameSdDir(cid, newCacheId)) { |
| 6448 | Slog.e(TAG, "Failed to rename " + cid + " to " + newCacheId |
| 6449 | + " inspite of cleaning it up."); |
| 6450 | return false; |
| 6451 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6452 | } |
| 6453 | if (!PackageHelper.isContainerMounted(newCacheId)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6454 | Slog.w(TAG, "Mounting container " + newCacheId); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6455 | newCachePath = PackageHelper.mountSdDir(newCacheId, |
| 6456 | getEncryptKey(), Process.SYSTEM_UID); |
| 6457 | } else { |
| 6458 | newCachePath = PackageHelper.getSdDir(newCacheId); |
| 6459 | } |
| 6460 | if (newCachePath == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6461 | Slog.w(TAG, "Failed to get cache path for " + newCacheId); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6462 | return false; |
| 6463 | } |
| 6464 | Log.i(TAG, "Succesfully renamed " + cid + |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6465 | " to " + newCacheId + |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6466 | " at new path: " + newCachePath); |
| 6467 | cid = newCacheId; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6468 | setCachePath(newCachePath); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6469 | return true; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6470 | } |
| 6471 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6472 | private void setCachePath(String newCachePath) { |
| 6473 | File cachePath = new File(newCachePath); |
| 6474 | libraryPath = new File(cachePath, LIB_DIR_NAME).getPath(); |
| 6475 | packagePath = new File(cachePath, RES_FILE_NAME).getPath(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6476 | |
| 6477 | if (isFwdLocked()) { |
| 6478 | resourcePath = new File(cachePath, PUBLIC_RES_FILE_NAME).getPath(); |
| 6479 | } else { |
| 6480 | resourcePath = packagePath; |
| 6481 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6482 | } |
| 6483 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6484 | int doPostInstall(int status, int uid) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6485 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6486 | cleanUp(); |
| 6487 | } else { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6488 | final int groupOwner; |
| 6489 | final String protectedFile; |
| 6490 | if (isFwdLocked()) { |
| 6491 | groupOwner = uid; |
| 6492 | protectedFile = RES_FILE_NAME; |
| 6493 | } else { |
| 6494 | groupOwner = -1; |
| 6495 | protectedFile = null; |
| 6496 | } |
| 6497 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6498 | if (uid < Process.FIRST_APPLICATION_UID |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6499 | || !PackageHelper.fixSdPermissions(cid, groupOwner, protectedFile)) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6500 | Slog.e(TAG, "Failed to finalize " + cid); |
| 6501 | PackageHelper.destroySdDir(cid); |
| 6502 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6503 | } |
| 6504 | |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6505 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6506 | if (!mounted) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6507 | PackageHelper.mountSdDir(cid, getEncryptKey(), Process.myUid()); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6508 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6509 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6510 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6511 | } |
| 6512 | |
| 6513 | private void cleanUp() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6514 | if (DEBUG_SD_INSTALL) Slog.i(TAG, "cleanUp"); |
| 6515 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6516 | // Destroy secure container |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6517 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6518 | } |
| 6519 | |
| 6520 | void cleanUpResourcesLI() { |
| 6521 | String sourceFile = getCodePath(); |
| 6522 | // Remove dex file |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 6523 | int retCode = mInstaller.rmdex(sourceFile); |
| 6524 | if (retCode < 0) { |
| 6525 | Slog.w(TAG, "Couldn't remove dex file for package: " |
| 6526 | + " at location " |
| 6527 | + sourceFile.toString() + ", retcode=" + retCode); |
| 6528 | // we don't consider this to be a failure of the core package deletion |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6529 | } |
| 6530 | cleanUp(); |
| 6531 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6532 | |
| 6533 | boolean matchContainer(String app) { |
| 6534 | if (cid.startsWith(app)) { |
| 6535 | return true; |
| 6536 | } |
| 6537 | return false; |
| 6538 | } |
| 6539 | |
| 6540 | String getPackageName() { |
| 6541 | int idx = cid.lastIndexOf("-"); |
| 6542 | if (idx == -1) { |
| 6543 | return cid; |
| 6544 | } |
| 6545 | return cid.substring(0, idx); |
| 6546 | } |
| 6547 | |
| 6548 | boolean doPostDeleteLI(boolean delete) { |
| 6549 | boolean ret = false; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6550 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6551 | if (mounted) { |
| 6552 | // Unmount first |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6553 | ret = PackageHelper.unMountSdDir(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6554 | } |
| 6555 | if (ret && delete) { |
| 6556 | cleanUpResourcesLI(); |
| 6557 | } |
| 6558 | return ret; |
| 6559 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6560 | |
| 6561 | @Override |
| 6562 | int doPreCopy() { |
| 6563 | if (isFwdLocked()) { |
| 6564 | if (!PackageHelper.fixSdPermissions(cid, |
| 6565 | getPackageUid(DEFAULT_CONTAINER_PACKAGE, 0), RES_FILE_NAME)) { |
| 6566 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6567 | } |
| 6568 | } |
| 6569 | |
| 6570 | return PackageManager.INSTALL_SUCCEEDED; |
| 6571 | } |
| 6572 | |
| 6573 | @Override |
| 6574 | int doPostCopy(int uid) { |
| 6575 | if (isFwdLocked()) { |
| 6576 | PackageHelper.fixSdPermissions(cid, uid, RES_FILE_NAME); |
| 6577 | if (uid < Process.FIRST_APPLICATION_UID |
| 6578 | || !PackageHelper.fixSdPermissions(cid, uid, RES_FILE_NAME)) { |
| 6579 | Slog.e(TAG, "Failed to finalize " + cid); |
| 6580 | PackageHelper.destroySdDir(cid); |
| 6581 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6582 | } |
| 6583 | } |
| 6584 | |
| 6585 | return PackageManager.INSTALL_SUCCEEDED; |
| 6586 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6587 | }; |
| 6588 | |
| 6589 | // Utility method used to create code paths based on package name and available index. |
| 6590 | private static String getNextCodePath(String oldCodePath, String prefix, String suffix) { |
| 6591 | String idxStr = ""; |
| 6592 | int idx = 1; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6593 | // Fall back to default value of idx=1 if prefix is not |
| 6594 | // part of oldCodePath |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6595 | if (oldCodePath != null) { |
Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 6596 | String subStr = oldCodePath; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6597 | // Drop the suffix right away |
Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 6598 | if (subStr.endsWith(suffix)) { |
| 6599 | subStr = subStr.substring(0, subStr.length() - suffix.length()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6600 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6601 | // If oldCodePath already contains prefix find out the |
| 6602 | // ending index to either increment or decrement. |
| 6603 | int sidx = subStr.lastIndexOf(prefix); |
| 6604 | if (sidx != -1) { |
| 6605 | subStr = subStr.substring(sidx + prefix.length()); |
| 6606 | if (subStr != null) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6607 | if (subStr.startsWith(INSTALL_PACKAGE_SUFFIX)) { |
| 6608 | subStr = subStr.substring(INSTALL_PACKAGE_SUFFIX.length()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6609 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6610 | try { |
| 6611 | idx = Integer.parseInt(subStr); |
| 6612 | if (idx <= 1) { |
| 6613 | idx++; |
| 6614 | } else { |
| 6615 | idx--; |
| 6616 | } |
| 6617 | } catch(NumberFormatException e) { |
| 6618 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6619 | } |
| 6620 | } |
| 6621 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6622 | idxStr = INSTALL_PACKAGE_SUFFIX + Integer.toString(idx); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6623 | return prefix + idxStr; |
| 6624 | } |
| 6625 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6626 | // Utility method used to ignore ADD/REMOVE events |
| 6627 | // by directory observer. |
| 6628 | private static boolean ignoreCodePath(String fullPathStr) { |
| 6629 | String apkName = getApkName(fullPathStr); |
| 6630 | int idx = apkName.lastIndexOf(INSTALL_PACKAGE_SUFFIX); |
| 6631 | if (idx != -1 && ((idx+1) < apkName.length())) { |
| 6632 | // Make sure the package ends with a numeral |
| 6633 | String version = apkName.substring(idx+1); |
| 6634 | try { |
| 6635 | Integer.parseInt(version); |
| 6636 | return true; |
| 6637 | } catch (NumberFormatException e) {} |
| 6638 | } |
| 6639 | return false; |
| 6640 | } |
| 6641 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6642 | // Utility method that returns the relative package path with respect |
| 6643 | // to the installation directory. Like say for /data/data/com.test-1.apk |
| 6644 | // string com.test-1 is returned. |
| 6645 | static String getApkName(String codePath) { |
| 6646 | if (codePath == null) { |
| 6647 | return null; |
| 6648 | } |
| 6649 | int sidx = codePath.lastIndexOf("/"); |
| 6650 | int eidx = codePath.lastIndexOf("."); |
| 6651 | if (eidx == -1) { |
| 6652 | eidx = codePath.length(); |
| 6653 | } else if (eidx == 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6654 | Slog.w(TAG, " Invalid code path, "+ codePath + " Not a valid apk name"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6655 | return null; |
| 6656 | } |
| 6657 | return codePath.substring(sidx+1, eidx); |
| 6658 | } |
| 6659 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6660 | class PackageInstalledInfo { |
| 6661 | String name; |
| 6662 | int uid; |
| 6663 | PackageParser.Package pkg; |
| 6664 | int returnCode; |
| 6665 | PackageRemovedInfo removedInfo; |
| 6666 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6667 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6668 | /* |
| 6669 | * Install a non-existing package. |
| 6670 | */ |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6671 | private void installNewPackageLI(PackageParser.Package pkg, |
| 6672 | int parseFlags, |
| 6673 | int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6674 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6675 | // Remember this for later, in case we need to rollback this install |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6676 | String pkgName = pkg.packageName; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6677 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 6678 | boolean dataDirExists = getDataPathForPackage(pkg.packageName, 0).exists(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6679 | res.name = pkgName; |
| 6680 | synchronized(mPackages) { |
Dianne Hackborn | e259bc7 | 2010-03-30 19:24:44 -0700 | [diff] [blame] | 6681 | if (mSettings.mRenamedPackages.containsKey(pkgName)) { |
| 6682 | // A package with the same name is already installed, though |
| 6683 | // it has been renamed to an older name. The package we |
| 6684 | // are trying to install should be installed as an update to |
| 6685 | // the existing one, but that has not been requested, so bail. |
| 6686 | Slog.w(TAG, "Attempt to re-install " + pkgName |
| 6687 | + " without first uninstalling package running as " |
| 6688 | + mSettings.mRenamedPackages.get(pkgName)); |
| 6689 | res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 6690 | return; |
| 6691 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6692 | if (mPackages.containsKey(pkgName) || mAppDirs.containsKey(pkg.mPath)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6693 | // Don't allow installation over an existing package with the same name. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6694 | Slog.w(TAG, "Attempt to re-install " + pkgName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6695 | + " without first uninstalling."); |
| 6696 | res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 6697 | return; |
| 6698 | } |
| 6699 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6700 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6701 | PackageParser.Package newPackage = scanPackageLI(pkg, parseFlags, scanMode, |
| 6702 | System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6703 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6704 | Slog.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] | 6705 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 6706 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 6707 | } |
| 6708 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6709 | updateSettingsLI(newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6710 | installerPackageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6711 | res); |
| 6712 | // delete the partially installed application. the data directory will have to be |
| 6713 | // restored if it was already existing |
| 6714 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
| 6715 | // remove package from internal structures. Note that we want deletePackageX to |
| 6716 | // delete the package data and cache directories that it created in |
| 6717 | // scanPackageLocked, unless those directories existed before we even tried to |
| 6718 | // install. |
| 6719 | deletePackageLI( |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6720 | pkgName, false, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6721 | dataDirExists ? PackageManager.DONT_DELETE_DATA : 0, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 6722 | res.removedInfo, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6723 | } |
| 6724 | } |
| 6725 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6726 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6727 | private void replacePackageLI(PackageParser.Package pkg, |
| 6728 | int parseFlags, |
| 6729 | int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6730 | String installerPackageName, PackageInstalledInfo res) { |
| 6731 | |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 6732 | PackageParser.Package oldPackage; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6733 | String pkgName = pkg.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6734 | // First find the old package info and check signatures |
| 6735 | synchronized(mPackages) { |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 6736 | oldPackage = mPackages.get(pkgName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6737 | if (compareSignatures(oldPackage.mSignatures, pkg.mSignatures) |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 6738 | != PackageManager.SIGNATURE_MATCH) { |
Kenny Root | 8c11f1d | 2012-04-14 12:10:38 -0700 | [diff] [blame] | 6739 | Slog.w(TAG, "New package has a different signature: " + pkgName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6740 | res.returnCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 6741 | return; |
| 6742 | } |
| 6743 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6744 | boolean sysPkg = (isSystemApp(oldPackage)); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 6745 | if (sysPkg) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6746 | replaceSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6747 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6748 | replaceNonSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6749 | } |
| 6750 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6751 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6752 | private void replaceNonSystemPackageLI(PackageParser.Package deletedPackage, |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6753 | PackageParser.Package pkg, |
| 6754 | int parseFlags, int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6755 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6756 | PackageParser.Package newPackage = null; |
| 6757 | String pkgName = deletedPackage.packageName; |
| 6758 | boolean deletedPkg = true; |
| 6759 | boolean updatedSettings = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6760 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6761 | long origUpdateTime; |
| 6762 | if (pkg.mExtras != null) { |
| 6763 | origUpdateTime = ((PackageSetting)pkg.mExtras).lastUpdateTime; |
| 6764 | } else { |
| 6765 | origUpdateTime = 0; |
| 6766 | } |
| 6767 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6768 | // First delete the existing package while retaining the data directory |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6769 | if (!deletePackageLI(pkgName, true, PackageManager.DONT_DELETE_DATA, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 6770 | res.removedInfo, true)) { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6771 | // If the existing package wasn't successfully deleted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6772 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 6773 | deletedPkg = false; |
| 6774 | } else { |
| 6775 | // Successfully deleted the old package. Now proceed with re-installation |
| 6776 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6777 | newPackage = scanPackageLI(pkg, parseFlags, scanMode | SCAN_UPDATE_TIME, |
| 6778 | System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6779 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6780 | Slog.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] | 6781 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 6782 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
Suchi Amalapurapu | 110fea7 | 2010-01-14 17:50:23 -0800 | [diff] [blame] | 6783 | } |
| 6784 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6785 | updateSettingsLI(newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6786 | installerPackageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6787 | res); |
| 6788 | updatedSettings = true; |
| 6789 | } |
| 6790 | } |
| 6791 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 6792 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6793 | // remove package from internal structures. Note that we want deletePackageX to |
| 6794 | // delete the package data and cache directories that it created in |
| 6795 | // scanPackageLocked, unless those directories existed before we even tried to |
| 6796 | // install. |
| 6797 | if(updatedSettings) { |
| 6798 | deletePackageLI( |
| 6799 | pkgName, true, |
| 6800 | PackageManager.DONT_DELETE_DATA, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 6801 | res.removedInfo, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6802 | } |
| 6803 | // Since we failed to install the new package we need to restore the old |
| 6804 | // package that we deleted. |
| 6805 | if(deletedPkg) { |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 6806 | File restoreFile = new File(deletedPackage.mPath); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6807 | // Parse old package |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 6808 | boolean oldOnSd = isExternal(deletedPackage); |
| 6809 | int oldParseFlags = mDefParseFlags | PackageParser.PARSE_CHATTY | |
| 6810 | (isForwardLocked(deletedPackage) ? PackageParser.PARSE_FORWARD_LOCK : 0) | |
| 6811 | (oldOnSd ? PackageParser.PARSE_ON_SDCARD : 0); |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6812 | int oldScanMode = (oldOnSd ? 0 : SCAN_MONITOR) | SCAN_UPDATE_SIGNATURE |
| 6813 | | SCAN_UPDATE_TIME; |
| 6814 | if (scanPackageLI(restoreFile, oldParseFlags, oldScanMode, |
| 6815 | origUpdateTime) == null) { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 6816 | Slog.e(TAG, "Failed to restore package : " + pkgName + " after failed upgrade"); |
| 6817 | return; |
| 6818 | } |
| 6819 | // Restore of old package succeeded. Update permissions. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6820 | // writer |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6821 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6822 | updatePermissionsLPw(deletedPackage.packageName, deletedPackage, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 6823 | UPDATE_PERMISSIONS_ALL); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6824 | // can downgrade to reader |
| 6825 | mSettings.writeLPr(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6826 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 6827 | Slog.i(TAG, "Successfully restored package : " + pkgName + " after failed upgrade"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6828 | } |
| 6829 | } |
| 6830 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6831 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6832 | private void replaceSystemPackageLI(PackageParser.Package deletedPackage, |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6833 | PackageParser.Package pkg, |
| 6834 | int parseFlags, int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6835 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6836 | PackageParser.Package newPackage = null; |
| 6837 | boolean updatedSettings = false; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6838 | parseFlags |= PackageManager.INSTALL_REPLACE_EXISTING | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6839 | PackageParser.PARSE_IS_SYSTEM; |
| 6840 | String packageName = deletedPackage.packageName; |
| 6841 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 6842 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6843 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6844 | return; |
| 6845 | } |
| 6846 | PackageParser.Package oldPkg; |
| 6847 | PackageSetting oldPkgSetting; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6848 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6849 | synchronized (mPackages) { |
| 6850 | oldPkg = mPackages.get(packageName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6851 | oldPkgSetting = mSettings.mPackages.get(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6852 | if((oldPkg == null) || (oldPkg.applicationInfo == null) || |
| 6853 | (oldPkgSetting == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6854 | Slog.w(TAG, "Couldn't find package:"+packageName+" information"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6855 | return; |
| 6856 | } |
| 6857 | } |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 6858 | |
| 6859 | killApplication(packageName, oldPkg.applicationInfo.uid); |
| 6860 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6861 | res.removedInfo.uid = oldPkg.applicationInfo.uid; |
| 6862 | res.removedInfo.removedPackage = packageName; |
| 6863 | // Remove existing system package |
| 6864 | removePackageLI(oldPkg, true); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6865 | // writer |
Dianne Hackborn | 3ec27e0 | 2010-12-16 12:41:56 -0800 | [diff] [blame] | 6866 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6867 | if (!mSettings.disableSystemPackageLPw(packageName) && deletedPackage != null) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 6868 | // We didn't need to disable the .apk as a current system package, |
| 6869 | // which means we are replacing another update that is already |
| 6870 | // installed. We need to make sure to delete the older one's .apk. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6871 | res.removedInfo.args = createInstallArgs(0, |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 6872 | deletedPackage.applicationInfo.sourceDir, |
| 6873 | deletedPackage.applicationInfo.publicSourceDir, |
| 6874 | deletedPackage.applicationInfo.nativeLibraryDir); |
| 6875 | } else { |
| 6876 | res.removedInfo.args = null; |
| 6877 | } |
Dianne Hackborn | 3ec27e0 | 2010-12-16 12:41:56 -0800 | [diff] [blame] | 6878 | } |
| 6879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6880 | // Successfully disabled the old package. Now proceed with re-installation |
| 6881 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
| 6882 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6883 | newPackage = scanPackageLI(pkg, parseFlags, scanMode, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6884 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6885 | Slog.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] | 6886 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 6887 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 6888 | } |
| 6889 | } else { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6890 | if (newPackage.mExtras != null) { |
| 6891 | final PackageSetting newPkgSetting = (PackageSetting)newPackage.mExtras; |
| 6892 | newPkgSetting.firstInstallTime = oldPkgSetting.firstInstallTime; |
| 6893 | newPkgSetting.lastUpdateTime = System.currentTimeMillis(); |
| 6894 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6895 | updateSettingsLI(newPackage, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6896 | updatedSettings = true; |
| 6897 | } |
| 6898 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 6899 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6900 | // Re installation failed. Restore old information |
| 6901 | // Remove new pkg information |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 6902 | if (newPackage != null) { |
| 6903 | removePackageLI(newPackage, true); |
| 6904 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6905 | // Add back the old system package |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6906 | scanPackageLI(oldPkg, parseFlags, SCAN_MONITOR | SCAN_UPDATE_SIGNATURE, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6907 | // Restore the old system information in Settings |
| 6908 | synchronized(mPackages) { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6909 | if (updatedSettings) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6910 | mSettings.enableSystemPackageLPw(packageName); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6911 | mSettings.setInstallerPackageName(packageName, |
| 6912 | oldPkgSetting.installerPackageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6913 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6914 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6915 | } |
| 6916 | } |
| 6917 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6918 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6919 | // Utility method used to move dex files during install. |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 6920 | private int moveDexFilesLI(PackageParser.Package newPackage) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6921 | int retCode; |
| 6922 | if ((newPackage.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) { |
| 6923 | retCode = mInstaller.movedex(newPackage.mScanPath, newPackage.mPath); |
| 6924 | if (retCode != 0) { |
Kenny Root | e2f7417 | 2010-08-04 13:35:33 -0700 | [diff] [blame] | 6925 | if (mNoDexOpt) { |
| 6926 | /* |
| 6927 | * If we're in an engineering build, programs are lazily run |
| 6928 | * through dexopt. If the .dex file doesn't exist yet, it |
| 6929 | * will be created when the program is run next. |
| 6930 | */ |
| 6931 | Slog.i(TAG, "dex file doesn't exist, skipping move: " + newPackage.mPath); |
| 6932 | } else { |
| 6933 | Slog.e(TAG, "Couldn't rename dex file: " + newPackage.mPath); |
| 6934 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6935 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6936 | } |
| 6937 | } |
| 6938 | return PackageManager.INSTALL_SUCCEEDED; |
| 6939 | } |
| 6940 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6941 | private void updateSettingsLI(PackageParser.Package newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6942 | String installerPackageName, PackageInstalledInfo res) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6943 | String pkgName = newPackage.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6944 | synchronized (mPackages) { |
| 6945 | //write settings. the installStatus will be incomplete at this stage. |
| 6946 | //note that the new package setting would have already been |
| 6947 | //added to mPackages. It hasn't been persisted yet. |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 6948 | mSettings.setInstallStatus(pkgName, PackageSettingBase.PKG_INSTALL_INCOMPLETE); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6949 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6950 | } |
| 6951 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 6952 | if ((res.returnCode = moveDexFilesLI(newPackage)) |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6953 | != PackageManager.INSTALL_SUCCEEDED) { |
| 6954 | // Discontinue if moving dex files failed. |
| 6955 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6956 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6957 | |
| 6958 | Log.d(TAG, "New package installed in " + newPackage.mPath); |
| 6959 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6960 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6961 | updatePermissionsLPw(newPackage.packageName, newPackage, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 6962 | UPDATE_PERMISSIONS_REPLACE_PKG | (newPackage.permissions.size() > 0 |
| 6963 | ? UPDATE_PERMISSIONS_ALL : 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6964 | res.name = pkgName; |
| 6965 | res.uid = newPackage.applicationInfo.uid; |
| 6966 | res.pkg = newPackage; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 6967 | mSettings.setInstallStatus(pkgName, PackageSettingBase.PKG_INSTALL_COMPLETE); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6968 | mSettings.setInstallerPackageName(pkgName, installerPackageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6969 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 6970 | //to update install status |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6971 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6972 | } |
| 6973 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6974 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6975 | private void installPackageLI(InstallArgs args, |
| 6976 | boolean newInstall, PackageInstalledInfo res) { |
| 6977 | int pFlags = args.flags; |
| 6978 | String installerPackageName = args.installerPackageName; |
| 6979 | File tmpPackageFile = new File(args.getCodePath()); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6980 | boolean forwardLocked = ((pFlags & PackageManager.INSTALL_FORWARD_LOCK) != 0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6981 | boolean onSd = ((pFlags & PackageManager.INSTALL_EXTERNAL) != 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 6982 | boolean replace = false; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 6983 | int scanMode = (onSd ? 0 : SCAN_MONITOR) | SCAN_FORCE_DEX | SCAN_UPDATE_SIGNATURE |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6984 | | (newInstall ? SCAN_NEW_INSTALL : 0); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 6985 | // Result object to be returned |
| 6986 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 6987 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 6988 | // Retrieve PackageSettings and parse package |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6989 | int parseFlags = mDefParseFlags | PackageParser.PARSE_CHATTY |
| 6990 | | (forwardLocked ? PackageParser.PARSE_FORWARD_LOCK : 0) |
| 6991 | | (onSd ? PackageParser.PARSE_ON_SDCARD : 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 6992 | PackageParser pp = new PackageParser(tmpPackageFile.getPath()); |
| 6993 | pp.setSeparateProcesses(mSeparateProcesses); |
| 6994 | final PackageParser.Package pkg = pp.parsePackage(tmpPackageFile, |
| 6995 | null, mMetrics, parseFlags); |
| 6996 | if (pkg == null) { |
| 6997 | res.returnCode = pp.getParseError(); |
| 6998 | return; |
| 6999 | } |
| 7000 | String pkgName = res.name = pkg.packageName; |
| 7001 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_TEST_ONLY) != 0) { |
| 7002 | if ((pFlags&PackageManager.INSTALL_ALLOW_TEST) == 0) { |
| 7003 | res.returnCode = PackageManager.INSTALL_FAILED_TEST_ONLY; |
| 7004 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7005 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7006 | } |
| 7007 | if (GET_CERTIFICATES && !pp.collectCertificates(pkg, parseFlags)) { |
| 7008 | res.returnCode = pp.getParseError(); |
| 7009 | return; |
| 7010 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7011 | |
| 7012 | /* If the installer passed in a manifest digest, compare it now. */ |
| 7013 | if (args.manifestDigest != null) { |
| 7014 | if (DEBUG_INSTALL) { |
| 7015 | final String parsedManifest = pkg.manifestDigest == null ? "null" |
| 7016 | : pkg.manifestDigest.toString(); |
| 7017 | Slog.d(TAG, "Comparing manifests: " + args.manifestDigest.toString() + " vs. " |
| 7018 | + parsedManifest); |
| 7019 | } |
| 7020 | |
| 7021 | if (!args.manifestDigest.equals(pkg.manifestDigest)) { |
| 7022 | res.returnCode = PackageManager.INSTALL_FAILED_PACKAGE_CHANGED; |
| 7023 | return; |
| 7024 | } |
| 7025 | } else if (DEBUG_INSTALL) { |
| 7026 | final String parsedManifest = pkg.manifestDigest == null |
| 7027 | ? "null" : pkg.manifestDigest.toString(); |
| 7028 | Slog.d(TAG, "manifestDigest was not present, but parser got: " + parsedManifest); |
| 7029 | } |
| 7030 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7031 | // Get rid of all references to package scan path via parser. |
| 7032 | pp = null; |
| 7033 | String oldCodePath = null; |
| 7034 | boolean systemApp = false; |
| 7035 | synchronized (mPackages) { |
| 7036 | // Check if installing already existing package |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7037 | if ((pFlags&PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| 7038 | String oldName = mSettings.mRenamedPackages.get(pkgName); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 7039 | if (pkg.mOriginalPackages != null |
| 7040 | && pkg.mOriginalPackages.contains(oldName) |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7041 | && mPackages.containsKey(oldName)) { |
| 7042 | // This package is derived from an original package, |
| 7043 | // and this device has been updating from that original |
| 7044 | // name. We must continue using the original name, so |
| 7045 | // rename the new package here. |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 7046 | pkg.setPackageName(oldName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7047 | pkgName = pkg.packageName; |
| 7048 | replace = true; |
| 7049 | } else if (mPackages.containsKey(pkgName)) { |
| 7050 | // This package, under its official name, already exists |
| 7051 | // on the device; we should replace it. |
| 7052 | replace = true; |
| 7053 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7054 | } |
| 7055 | PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 7056 | if (ps != null) { |
| 7057 | oldCodePath = mSettings.mPackages.get(pkgName).codePathString; |
| 7058 | if (ps.pkg != null && ps.pkg.applicationInfo != null) { |
| 7059 | systemApp = (ps.pkg.applicationInfo.flags & |
| 7060 | ApplicationInfo.FLAG_SYSTEM) != 0; |
Dianne Hackborn | ade3eca | 2009-05-11 18:54:45 -0700 | [diff] [blame] | 7061 | } |
| 7062 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7063 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7064 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7065 | if (systemApp && onSd) { |
| 7066 | // Disable updates to system apps on sdcard |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7067 | Slog.w(TAG, "Cannot install updates to system apps on sdcard"); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7068 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
| 7069 | return; |
| 7070 | } |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 7071 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7072 | if (!args.doRename(res.returnCode, pkgName, oldCodePath)) { |
| 7073 | res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7074 | return; |
| 7075 | } |
| 7076 | // Set application objects path explicitly after the rename |
| 7077 | setApplicationInfoPaths(pkg, args.getCodePath(), args.getResourcePath()); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7078 | pkg.applicationInfo.nativeLibraryDir = args.getNativeLibraryPath(); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7079 | if (replace) { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7080 | replacePackageLI(pkg, parseFlags, scanMode, |
| 7081 | installerPackageName, res); |
| 7082 | } else { |
| 7083 | installNewPackageLI(pkg, parseFlags, scanMode, |
| 7084 | installerPackageName,res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7085 | } |
| 7086 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7087 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7088 | private static boolean isForwardLocked(PackageParser.Package pkg) { |
| 7089 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7090 | } |
| 7091 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7092 | private static boolean isExternal(PackageParser.Package pkg) { |
| 7093 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; |
| 7094 | } |
| 7095 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7096 | private static boolean isExternal(PackageSetting ps) { |
| 7097 | return (ps.pkgFlags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; |
| 7098 | } |
| 7099 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7100 | private static boolean isSystemApp(PackageParser.Package pkg) { |
| 7101 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 7102 | } |
| 7103 | |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 7104 | private static boolean isSystemApp(ApplicationInfo info) { |
| 7105 | return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 7106 | } |
| 7107 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7108 | private static boolean isUpdatedSystemApp(PackageParser.Package pkg) { |
| 7109 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7110 | } |
| 7111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7112 | private void deleteTempPackageFiles() { |
| 7113 | FilenameFilter filter = new FilenameFilter() { |
| 7114 | public boolean accept(File dir, String name) { |
| 7115 | return name.startsWith("vmdl") && name.endsWith(".tmp"); |
| 7116 | } |
| 7117 | }; |
| 7118 | String tmpFilesList[] = mAppInstallDir.list(filter); |
| 7119 | if(tmpFilesList == null) { |
| 7120 | return; |
| 7121 | } |
| 7122 | for(int i = 0; i < tmpFilesList.length; i++) { |
| 7123 | File tmpFile = new File(mAppInstallDir, tmpFilesList[i]); |
| 7124 | tmpFile.delete(); |
| 7125 | } |
| 7126 | } |
| 7127 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7128 | private File createTempPackageFile(File installDir) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7129 | File tmpPackageFile; |
| 7130 | try { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7131 | tmpPackageFile = File.createTempFile("vmdl", ".tmp", installDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7132 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7133 | Slog.e(TAG, "Couldn't create temp file for downloaded package file."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7134 | return null; |
| 7135 | } |
| 7136 | try { |
| 7137 | FileUtils.setPermissions( |
| 7138 | tmpPackageFile.getCanonicalPath(), FileUtils.S_IRUSR|FileUtils.S_IWUSR, |
| 7139 | -1, -1); |
| 7140 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7141 | Slog.e(TAG, "Trouble getting the canoncical path for a temp file."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7142 | return null; |
| 7143 | } |
| 7144 | return tmpPackageFile; |
| 7145 | } |
| 7146 | |
| 7147 | public void deletePackage(final String packageName, |
| 7148 | final IPackageDeleteObserver observer, |
| 7149 | final int flags) { |
| 7150 | mContext.enforceCallingOrSelfPermission( |
| 7151 | android.Manifest.permission.DELETE_PACKAGES, null); |
| 7152 | // Queue up an async operation since the package deletion may take a little while. |
| 7153 | mHandler.post(new Runnable() { |
| 7154 | public void run() { |
| 7155 | mHandler.removeCallbacks(this); |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7156 | final int returnCode = deletePackageX(packageName, true, true, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7157 | if (observer != null) { |
| 7158 | try { |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7159 | observer.packageDeleted(packageName, returnCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7160 | } catch (RemoteException e) { |
| 7161 | Log.i(TAG, "Observer no longer exists."); |
| 7162 | } //end catch |
| 7163 | } //end if |
| 7164 | } //end run |
| 7165 | }); |
| 7166 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7168 | /** |
| 7169 | * This method is an internal method that could be get invoked either |
| 7170 | * to delete an installed package or to clean up a failed installation. |
| 7171 | * After deleting an installed package, a broadcast is sent to notify any |
| 7172 | * listeners that the package has been installed. For cleaning up a failed |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7173 | * 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] | 7174 | * installation wouldn't have sent the initial broadcast either |
| 7175 | * The key steps in deleting a package are |
| 7176 | * deleting the package information in internal structures like mPackages, |
| 7177 | * deleting the packages base directories through installd |
| 7178 | * updating mSettings to reflect current status |
| 7179 | * persisting settings for later use |
| 7180 | * sending a broadcast if necessary |
| 7181 | */ |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7182 | private int deletePackageX(String packageName, boolean sendBroadCast, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7183 | boolean deleteCodeAndResources, int flags) { |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7184 | final PackageRemovedInfo info = new PackageRemovedInfo(); |
| 7185 | final boolean res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7186 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7187 | IDevicePolicyManager dpm = IDevicePolicyManager.Stub.asInterface( |
| 7188 | ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)); |
| 7189 | try { |
| 7190 | if (dpm != null && dpm.packageHasActiveAdmins(packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7191 | Slog.w(TAG, "Not removing package " + packageName + ": has active device admin"); |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7192 | return PackageManager.DELETE_FAILED_DEVICE_POLICY_MANAGER; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7193 | } |
| 7194 | } catch (RemoteException e) { |
| 7195 | } |
| 7196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7197 | synchronized (mInstallLock) { |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 7198 | res = deletePackageLI(packageName, deleteCodeAndResources, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7199 | flags | REMOVE_CHATTY, info, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7200 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7201 | |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7202 | if (res && sendBroadCast) { |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7203 | boolean systemUpdate = info.isRemovedPackageSystemUpdate; |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 7204 | info.sendBroadcast(deleteCodeAndResources, systemUpdate); |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7205 | |
| 7206 | // If the removed package was a system update, the old system packaged |
| 7207 | // was re-enabled; we need to broadcast this information |
| 7208 | if (systemUpdate) { |
| 7209 | Bundle extras = new Bundle(1); |
| 7210 | extras.putInt(Intent.EXTRA_UID, info.removedUid >= 0 ? info.removedUid : info.uid); |
| 7211 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 7212 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7213 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7214 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7215 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7216 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7217 | sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, null, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7218 | null, packageName, null, UserId.USER_ALL); |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7219 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7220 | } |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 7221 | // Force a gc here. |
| 7222 | Runtime.getRuntime().gc(); |
| 7223 | // Delete the resources here after sending the broadcast to let |
| 7224 | // other processes clean up before deleting resources. |
| 7225 | if (info.args != null) { |
| 7226 | synchronized (mInstallLock) { |
| 7227 | info.args.doPostDeleteLI(deleteCodeAndResources); |
| 7228 | } |
| 7229 | } |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7230 | |
| 7231 | return res ? PackageManager.DELETE_SUCCEEDED : PackageManager.DELETE_FAILED_INTERNAL_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7232 | } |
| 7233 | |
| 7234 | static class PackageRemovedInfo { |
| 7235 | String removedPackage; |
| 7236 | int uid = -1; |
| 7237 | int removedUid = -1; |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7238 | boolean isRemovedPackageSystemUpdate = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7239 | // Clean up resources deleted packages. |
| 7240 | InstallArgs args = null; |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7241 | |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 7242 | void sendBroadcast(boolean fullRemove, boolean replacing) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7243 | Bundle extras = new Bundle(1); |
| 7244 | extras.putInt(Intent.EXTRA_UID, removedUid >= 0 ? removedUid : uid); |
| 7245 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, fullRemove); |
| 7246 | if (replacing) { |
| 7247 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 7248 | } |
| 7249 | if (removedPackage != null) { |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7250 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7251 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | f9abb40 | 2011-08-10 15:00:59 -0700 | [diff] [blame] | 7252 | if (fullRemove && !replacing) { |
| 7253 | sendPackageBroadcast(Intent.ACTION_PACKAGE_FULLY_REMOVED, removedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7254 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | f9abb40 | 2011-08-10 15:00:59 -0700 | [diff] [blame] | 7255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7256 | } |
| 7257 | if (removedUid >= 0) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7258 | sendPackageBroadcast(Intent.ACTION_UID_REMOVED, null, extras, null, null, |
| 7259 | UserId.getUserId(removedUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7260 | } |
| 7261 | } |
| 7262 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7263 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7264 | /* |
| 7265 | * This method deletes the package from internal data structures. If the DONT_DELETE_DATA |
| 7266 | * flag is not set, the data directory is removed as well. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7267 | * 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] | 7268 | * delete a partially installed application. |
| 7269 | */ |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7270 | private void removePackageDataLI(PackageParser.Package p, PackageRemovedInfo outInfo, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7271 | int flags, boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7272 | String packageName = p.packageName; |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7273 | if (outInfo != null) { |
| 7274 | outInfo.removedPackage = packageName; |
| 7275 | } |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 7276 | removePackageLI(p, (flags&REMOVE_CHATTY) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7277 | // Retrieve object to delete permissions for shared user later on |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7278 | final PackageSetting deletedPs; |
| 7279 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7280 | synchronized (mPackages) { |
| 7281 | deletedPs = mSettings.mPackages.get(packageName); |
| 7282 | } |
| 7283 | if ((flags&PackageManager.DONT_DELETE_DATA) == 0) { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7284 | int retCode = mInstaller.remove(packageName, 0); |
| 7285 | if (retCode < 0) { |
| 7286 | Slog.w(TAG, "Couldn't remove app data or cache directory for package: " |
| 7287 | + packageName + ", retcode=" + retCode); |
| 7288 | // we don't consider this to be a failure of the core package deletion |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7289 | } else { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7290 | // TODO: Kill the processes first |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7291 | sUserManager.removePackageForAllUsers(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7292 | } |
Dianne Hackborn | fb1f103 | 2010-07-29 13:57:56 -0700 | [diff] [blame] | 7293 | schedulePackageCleaning(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7294 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7295 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7296 | synchronized (mPackages) { |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 7297 | if (deletedPs != null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7298 | if ((flags&PackageManager.DONT_DELETE_DATA) == 0) { |
| 7299 | if (outInfo != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7300 | outInfo.removedUid = mSettings.removePackageLPw(packageName); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7301 | } |
| 7302 | if (deletedPs != null) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7303 | updatePermissionsLPw(deletedPs.name, null, 0); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7304 | if (deletedPs.sharedUser != null) { |
| 7305 | // remove permissions associated with package |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7306 | mSettings.updateSharedUserPermsLPw(deletedPs, mGlobalGids); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7307 | } |
| 7308 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 7309 | clearPackagePreferredActivitiesLPw(deletedPs.name); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 7310 | } |
| 7311 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7312 | // can downgrade to reader |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7313 | if (writeSettings) { |
| 7314 | // Save settings now |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7315 | mSettings.writeLPr(); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7316 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7317 | } |
| 7318 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7319 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7320 | /* |
| 7321 | * Tries to delete system package. |
| 7322 | */ |
| 7323 | private boolean deleteSystemPackageLI(PackageParser.Package p, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7324 | int flags, PackageRemovedInfo outInfo, boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7325 | ApplicationInfo applicationInfo = p.applicationInfo; |
| 7326 | //applicable for non-partially installed applications only |
| 7327 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7328 | Slog.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7329 | return false; |
| 7330 | } |
| 7331 | PackageSetting ps = null; |
| 7332 | // Confirm if the system package has been updated |
| 7333 | // An updated system app can be deleted. This will also have to restore |
| 7334 | // the system pkg from system partition |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7335 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7336 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7337 | ps = mSettings.getDisabledSystemPkgLPr(p.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7338 | } |
| 7339 | if (ps == null) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7340 | Slog.w(TAG, "Attempt to delete unknown system package "+ p.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7341 | return false; |
| 7342 | } else { |
| 7343 | Log.i(TAG, "Deleting system pkg from data partition"); |
| 7344 | } |
| 7345 | // Delete the updated package |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7346 | outInfo.isRemovedPackageSystemUpdate = true; |
Kenny Root | 7c1bd7f | 2010-09-01 13:44:11 -0700 | [diff] [blame] | 7347 | if (ps.versionCode < p.mVersionCode) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7348 | // Delete data for downgrades |
Kenny Root | 7c1bd7f | 2010-09-01 13:44:11 -0700 | [diff] [blame] | 7349 | flags &= ~PackageManager.DONT_DELETE_DATA; |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7350 | } else { |
| 7351 | // Preserve data by setting flag |
Kenny Root | 7c1bd7f | 2010-09-01 13:44:11 -0700 | [diff] [blame] | 7352 | flags |= PackageManager.DONT_DELETE_DATA; |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7353 | } |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7354 | boolean ret = deleteInstalledPackageLI(p, true, flags, outInfo, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7355 | writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7356 | if (!ret) { |
| 7357 | return false; |
| 7358 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7359 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7360 | synchronized (mPackages) { |
| 7361 | // Reinstate the old system package |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7362 | mSettings.enableSystemPackageLPw(p.packageName); |
Kenny Root | 8f7cc02 | 2010-09-12 09:04:56 -0700 | [diff] [blame] | 7363 | // Remove any native libraries from the upgraded package. |
| 7364 | NativeLibraryHelper.removeNativeBinariesLI(p.applicationInfo.nativeLibraryDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7365 | } |
| 7366 | // Install the system package |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7367 | PackageParser.Package newPkg = scanPackageLI(ps.codePath, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7368 | PackageParser.PARSE_MUST_BE_APK | PackageParser.PARSE_IS_SYSTEM, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7369 | SCAN_MONITOR | SCAN_NO_PATHS, 0); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7371 | if (newPkg == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7372 | Slog.w(TAG, "Failed to restore system package:"+p.packageName+" with error:" + mLastScanError); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7373 | return false; |
| 7374 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7375 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7376 | synchronized (mPackages) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7377 | updatePermissionsLPw(newPkg.packageName, newPkg, |
| 7378 | UPDATE_PERMISSIONS_ALL | UPDATE_PERMISSIONS_REPLACE_PKG); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7379 | // can downgrade to reader here |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7380 | if (writeSettings) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7381 | mSettings.writeLPr(); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7382 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7383 | } |
| 7384 | return true; |
| 7385 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7386 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7387 | private boolean deleteInstalledPackageLI(PackageParser.Package p, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7388 | boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo, |
| 7389 | boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7390 | ApplicationInfo applicationInfo = p.applicationInfo; |
| 7391 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7392 | Slog.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7393 | return false; |
| 7394 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7395 | if (outInfo != null) { |
| 7396 | outInfo.uid = applicationInfo.uid; |
| 7397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7398 | |
| 7399 | // Delete package data from internal structures and also remove data if flag is set |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7400 | removePackageDataLI(p, outInfo, flags, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7401 | |
| 7402 | // Delete application code and resources |
| 7403 | if (deleteCodeAndResources) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7404 | // TODO can pick up from PackageSettings as well |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7405 | int installFlags = isExternal(p) ? PackageManager.INSTALL_EXTERNAL : 0; |
| 7406 | installFlags |= isForwardLocked(p) ? PackageManager.INSTALL_FORWARD_LOCK : 0; |
| 7407 | outInfo.args = createInstallArgs(installFlags, applicationInfo.sourceDir, |
| 7408 | applicationInfo.publicSourceDir, applicationInfo.nativeLibraryDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7409 | } |
| 7410 | return true; |
| 7411 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7413 | /* |
| 7414 | * This method handles package deletion in general |
| 7415 | */ |
| 7416 | private boolean deletePackageLI(String packageName, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7417 | boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo, |
| 7418 | boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7419 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7420 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7421 | return false; |
| 7422 | } |
| 7423 | PackageParser.Package p; |
| 7424 | boolean dataOnly = false; |
| 7425 | synchronized (mPackages) { |
| 7426 | p = mPackages.get(packageName); |
| 7427 | if (p == null) { |
| 7428 | //this retrieves partially installed apps |
| 7429 | dataOnly = true; |
| 7430 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 7431 | if (ps == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7432 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7433 | return false; |
| 7434 | } |
| 7435 | p = ps.pkg; |
| 7436 | } |
| 7437 | } |
| 7438 | if (p == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7439 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7440 | return false; |
| 7441 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7442 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7443 | if (dataOnly) { |
| 7444 | // Delete application data first |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7445 | removePackageDataLI(p, outInfo, flags, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7446 | return true; |
| 7447 | } |
| 7448 | // At this point the package should have ApplicationInfo associated with it |
| 7449 | if (p.applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7450 | Slog.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7451 | return false; |
| 7452 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7453 | boolean ret = false; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7454 | if (isSystemApp(p)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7455 | Log.i(TAG, "Removing system package:"+p.packageName); |
| 7456 | // When an updated system application is deleted we delete the existing resources as well and |
| 7457 | // fall back to existing code in system partition |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7458 | ret = deleteSystemPackageLI(p, flags, outInfo, writeSettings); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7459 | } else { |
| 7460 | Log.i(TAG, "Removing non-system package:"+p.packageName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7461 | // Kill application pre-emptively especially for apps on sd. |
| 7462 | killApplication(packageName, p.applicationInfo.uid); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7463 | ret = deleteInstalledPackageLI(p, deleteCodeAndResources, flags, outInfo, |
| 7464 | writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7465 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7466 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7467 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7468 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7469 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7470 | public void clearApplicationUserData(final String packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7471 | final IPackageDataObserver observer, final int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7472 | mContext.enforceCallingOrSelfPermission( |
| 7473 | android.Manifest.permission.CLEAR_APP_USER_DATA, null); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7474 | checkValidCaller(Binder.getCallingUid(), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7475 | // Queue up an async operation since the package deletion may take a little while. |
| 7476 | mHandler.post(new Runnable() { |
| 7477 | public void run() { |
| 7478 | mHandler.removeCallbacks(this); |
| 7479 | final boolean succeeded; |
| 7480 | synchronized (mInstallLock) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7481 | succeeded = clearApplicationUserDataLI(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7482 | } |
| 7483 | if (succeeded) { |
| 7484 | // invoke DeviceStorageMonitor's update method to clear any notifications |
| 7485 | DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) |
| 7486 | ServiceManager.getService(DeviceStorageMonitorService.SERVICE); |
| 7487 | if (dsm != null) { |
| 7488 | dsm.updateMemory(); |
| 7489 | } |
| 7490 | } |
| 7491 | if(observer != null) { |
| 7492 | try { |
| 7493 | observer.onRemoveCompleted(packageName, succeeded); |
| 7494 | } catch (RemoteException e) { |
| 7495 | Log.i(TAG, "Observer no longer exists."); |
| 7496 | } |
| 7497 | } //end if observer |
| 7498 | } //end run |
| 7499 | }); |
| 7500 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7501 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7502 | private boolean clearApplicationUserDataLI(String packageName, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7503 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7504 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7505 | return false; |
| 7506 | } |
| 7507 | PackageParser.Package p; |
| 7508 | boolean dataOnly = false; |
| 7509 | synchronized (mPackages) { |
| 7510 | p = mPackages.get(packageName); |
| 7511 | if(p == null) { |
| 7512 | dataOnly = true; |
| 7513 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 7514 | if((ps == null) || (ps.pkg == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7515 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7516 | return false; |
| 7517 | } |
| 7518 | p = ps.pkg; |
| 7519 | } |
| 7520 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 7521 | |
Dianne Hackborn | 3b81bc1 | 2011-01-15 11:50:52 -0800 | [diff] [blame] | 7522 | if (!dataOnly) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7523 | //need to check this only for fully installed applications |
| 7524 | if (p == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7525 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7526 | return false; |
| 7527 | } |
| 7528 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 7529 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7530 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7531 | return false; |
| 7532 | } |
| 7533 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7534 | int retCode = mInstaller.clearUserData(packageName, userId); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7535 | if (retCode < 0) { |
| 7536 | Slog.w(TAG, "Couldn't remove cache files for package: " |
| 7537 | + packageName); |
| 7538 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7539 | } |
| 7540 | return true; |
| 7541 | } |
| 7542 | |
| 7543 | public void deleteApplicationCacheFiles(final String packageName, |
| 7544 | final IPackageDataObserver observer) { |
| 7545 | mContext.enforceCallingOrSelfPermission( |
| 7546 | android.Manifest.permission.DELETE_CACHE_FILES, null); |
| 7547 | // Queue up an async operation since the package deletion may take a little while. |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7548 | final int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7549 | mHandler.post(new Runnable() { |
| 7550 | public void run() { |
| 7551 | mHandler.removeCallbacks(this); |
| 7552 | final boolean succeded; |
| 7553 | synchronized (mInstallLock) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7554 | succeded = deleteApplicationCacheFilesLI(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7555 | } |
| 7556 | if(observer != null) { |
| 7557 | try { |
| 7558 | observer.onRemoveCompleted(packageName, succeded); |
| 7559 | } catch (RemoteException e) { |
| 7560 | Log.i(TAG, "Observer no longer exists."); |
| 7561 | } |
| 7562 | } //end if observer |
| 7563 | } //end run |
| 7564 | }); |
| 7565 | } |
| 7566 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7567 | private boolean deleteApplicationCacheFilesLI(String packageName, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7568 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7569 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7570 | return false; |
| 7571 | } |
| 7572 | PackageParser.Package p; |
| 7573 | synchronized (mPackages) { |
| 7574 | p = mPackages.get(packageName); |
| 7575 | } |
| 7576 | if (p == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7577 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7578 | return false; |
| 7579 | } |
| 7580 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 7581 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7582 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7583 | return false; |
| 7584 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7585 | // TODO: Pass userId to deleteCacheFiles |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7586 | int retCode = mInstaller.deleteCacheFiles(packageName); |
| 7587 | if (retCode < 0) { |
| 7588 | Slog.w(TAG, "Couldn't remove cache files for package: " |
| 7589 | + packageName); |
| 7590 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7591 | } |
| 7592 | return true; |
| 7593 | } |
| 7594 | |
| 7595 | public void getPackageSizeInfo(final String packageName, |
| 7596 | final IPackageStatsObserver observer) { |
| 7597 | mContext.enforceCallingOrSelfPermission( |
| 7598 | android.Manifest.permission.GET_PACKAGE_SIZE, null); |
| 7599 | // Queue up an async operation since the package deletion may take a little while. |
| 7600 | mHandler.post(new Runnable() { |
| 7601 | public void run() { |
| 7602 | mHandler.removeCallbacks(this); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 7603 | PackageStats stats = new PackageStats(packageName); |
| 7604 | |
| 7605 | final boolean success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7606 | synchronized (mInstallLock) { |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 7607 | success = getPackageSizeInfoLI(packageName, stats); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7608 | } |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 7609 | |
| 7610 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| 7611 | msg.obj = new MeasureParams(stats, success, observer); |
| 7612 | mHandler.sendMessage(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7613 | } //end run |
| 7614 | }); |
| 7615 | } |
| 7616 | |
| 7617 | private boolean getPackageSizeInfoLI(String packageName, PackageStats pStats) { |
| 7618 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7619 | Slog.w(TAG, "Attempt to get size of null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7620 | return false; |
| 7621 | } |
| 7622 | PackageParser.Package p; |
| 7623 | boolean dataOnly = false; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 7624 | String asecPath = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7625 | synchronized (mPackages) { |
| 7626 | p = mPackages.get(packageName); |
| 7627 | if(p == null) { |
| 7628 | dataOnly = true; |
| 7629 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 7630 | if((ps == null) || (ps.pkg == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7631 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7632 | return false; |
| 7633 | } |
| 7634 | p = ps.pkg; |
| 7635 | } |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 7636 | if (p != null && isExternal(p)) { |
| 7637 | String secureContainerId = cidFromCodePath(p.applicationInfo.sourceDir); |
| 7638 | if (secureContainerId != null) { |
| 7639 | asecPath = PackageHelper.getSdFilesystem(secureContainerId); |
| 7640 | } |
| 7641 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7642 | } |
| 7643 | String publicSrcDir = null; |
| 7644 | if(!dataOnly) { |
| 7645 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 7646 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7647 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7648 | return false; |
| 7649 | } |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 7650 | if (isForwardLocked(p)) { |
| 7651 | publicSrcDir = applicationInfo.publicSourceDir; |
| 7652 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7653 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7654 | int res = mInstaller.getSizeInfo(packageName, p.mPath, publicSrcDir, |
| 7655 | asecPath, pStats); |
| 7656 | if (res < 0) { |
| 7657 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7658 | } |
| 7659 | return true; |
| 7660 | } |
| 7661 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7662 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7663 | public void addPackageToPreferred(String packageName) { |
Dianne Hackborn | 8377fc0 | 2010-06-07 15:54:58 -0700 | [diff] [blame] | 7664 | Slog.w(TAG, "addPackageToPreferred: this is now a no-op"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7665 | } |
| 7666 | |
| 7667 | public void removePackageFromPreferred(String packageName) { |
Dianne Hackborn | 8377fc0 | 2010-06-07 15:54:58 -0700 | [diff] [blame] | 7668 | Slog.w(TAG, "removePackageFromPreferred: this is now a no-op"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7669 | } |
| 7670 | |
| 7671 | public List<PackageInfo> getPreferredPackages(int flags) { |
Dianne Hackborn | a7ca0e5 | 2009-12-01 14:31:55 -0800 | [diff] [blame] | 7672 | return new ArrayList<PackageInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7673 | } |
| 7674 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7675 | private int getUidTargetSdkVersionLockedLPr(int uid) { |
| 7676 | Object obj = mSettings.getUserIdLPr(uid); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7677 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 7678 | final SharedUserSetting sus = (SharedUserSetting) obj; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7679 | int vers = Build.VERSION_CODES.CUR_DEVELOPMENT; |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 7680 | final Iterator<PackageSetting> it = sus.packages.iterator(); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7681 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7682 | final PackageSetting ps = it.next(); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7683 | if (ps.pkg != null) { |
| 7684 | int v = ps.pkg.applicationInfo.targetSdkVersion; |
| 7685 | if (v < vers) vers = v; |
| 7686 | } |
| 7687 | } |
| 7688 | return vers; |
| 7689 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 7690 | final PackageSetting ps = (PackageSetting) obj; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7691 | if (ps.pkg != null) { |
| 7692 | return ps.pkg.applicationInfo.targetSdkVersion; |
| 7693 | } |
| 7694 | } |
| 7695 | return Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 7696 | } |
| 7697 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7698 | public void addPreferredActivity(IntentFilter filter, int match, |
| 7699 | ComponentName[] set, ComponentName activity) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7700 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7701 | synchronized (mPackages) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7702 | if (mContext.checkCallingOrSelfPermission( |
| 7703 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 7704 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7705 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7706 | < Build.VERSION_CODES.FROYO) { |
| 7707 | Slog.w(TAG, "Ignoring addPreferredActivity() from uid " |
| 7708 | + Binder.getCallingUid()); |
| 7709 | return; |
| 7710 | } |
| 7711 | mContext.enforceCallingOrSelfPermission( |
| 7712 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 7713 | } |
| 7714 | |
| 7715 | Slog.i(TAG, "Adding preferred activity " + activity + ":"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7716 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
| 7717 | mSettings.mPreferredActivities.addFilter( |
| 7718 | new PreferredActivity(filter, match, set, activity)); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 7719 | scheduleWriteSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7720 | } |
| 7721 | } |
| 7722 | |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 7723 | public void replacePreferredActivity(IntentFilter filter, int match, |
| 7724 | ComponentName[] set, ComponentName activity) { |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 7725 | if (filter.countActions() != 1) { |
| 7726 | throw new IllegalArgumentException( |
| 7727 | "replacePreferredActivity expects filter to have only 1 action."); |
| 7728 | } |
| 7729 | if (filter.countCategories() != 1) { |
| 7730 | throw new IllegalArgumentException( |
| 7731 | "replacePreferredActivity expects filter to have only 1 category."); |
| 7732 | } |
| 7733 | if (filter.countDataAuthorities() != 0 |
| 7734 | || filter.countDataPaths() != 0 |
| 7735 | || filter.countDataSchemes() != 0 |
| 7736 | || filter.countDataTypes() != 0) { |
| 7737 | throw new IllegalArgumentException( |
| 7738 | "replacePreferredActivity expects filter to have no data authorities, " + |
| 7739 | "paths, schemes or types."); |
| 7740 | } |
| 7741 | synchronized (mPackages) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7742 | if (mContext.checkCallingOrSelfPermission( |
| 7743 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 7744 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7745 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7746 | < Build.VERSION_CODES.FROYO) { |
| 7747 | Slog.w(TAG, "Ignoring replacePreferredActivity() from uid " |
| 7748 | + Binder.getCallingUid()); |
| 7749 | return; |
| 7750 | } |
| 7751 | mContext.enforceCallingOrSelfPermission( |
| 7752 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 7753 | } |
| 7754 | |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 7755 | ArrayList<PreferredActivity> removed = null; |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 7756 | Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
| 7757 | String action = filter.getAction(0); |
| 7758 | String category = filter.getCategory(0); |
| 7759 | while (it.hasNext()) { |
| 7760 | PreferredActivity pa = it.next(); |
| 7761 | if (pa.getAction(0).equals(action) && pa.getCategory(0).equals(category)) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 7762 | if (removed == null) { |
| 7763 | removed = new ArrayList<PreferredActivity>(); |
| 7764 | } |
| 7765 | removed.add(pa); |
| 7766 | Log.i(TAG, "Removing preferred activity " + pa.mPref.mComponent + ":"); |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 7767 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
| 7768 | } |
| 7769 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 7770 | if (removed != null) { |
| 7771 | for (int i=0; i<removed.size(); i++) { |
| 7772 | PreferredActivity pa = removed.get(i); |
| 7773 | mSettings.mPreferredActivities.removeFilter(pa); |
| 7774 | } |
| 7775 | } |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 7776 | addPreferredActivity(filter, match, set, activity); |
| 7777 | } |
| 7778 | } |
| 7779 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7780 | public void clearPackagePreferredActivities(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7781 | final int uid = Binder.getCallingUid(); |
| 7782 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7783 | synchronized (mPackages) { |
Dianne Hackborn | 2ee89ea | 2010-03-10 18:27:09 -0800 | [diff] [blame] | 7784 | PackageParser.Package pkg = mPackages.get(packageName); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7785 | if (pkg == null || pkg.applicationInfo.uid != uid) { |
| 7786 | if (mContext.checkCallingOrSelfPermission( |
| 7787 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 7788 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7789 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7790 | < Build.VERSION_CODES.FROYO) { |
| 7791 | Slog.w(TAG, "Ignoring clearPackagePreferredActivities() from uid " |
| 7792 | + Binder.getCallingUid()); |
| 7793 | return; |
| 7794 | } |
| 7795 | mContext.enforceCallingOrSelfPermission( |
| 7796 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 7797 | } |
Dianne Hackborn | 2ee89ea | 2010-03-10 18:27:09 -0800 | [diff] [blame] | 7798 | } |
| 7799 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7800 | if (clearPackagePreferredActivitiesLPw(packageName)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 7801 | scheduleWriteSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7802 | } |
| 7803 | } |
| 7804 | } |
| 7805 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7806 | boolean clearPackagePreferredActivitiesLPw(String packageName) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 7807 | ArrayList<PreferredActivity> removed = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7808 | Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
| 7809 | while (it.hasNext()) { |
| 7810 | PreferredActivity pa = it.next(); |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 7811 | if (pa.mPref.mComponent.getPackageName().equals(packageName)) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 7812 | if (removed == null) { |
| 7813 | removed = new ArrayList<PreferredActivity>(); |
| 7814 | } |
| 7815 | removed.add(pa); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7816 | } |
| 7817 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 7818 | if (removed != null) { |
| 7819 | for (int i=0; i<removed.size(); i++) { |
| 7820 | PreferredActivity pa = removed.get(i); |
| 7821 | mSettings.mPreferredActivities.removeFilter(pa); |
| 7822 | } |
| 7823 | return true; |
| 7824 | } |
| 7825 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7826 | } |
| 7827 | |
| 7828 | public int getPreferredActivities(List<IntentFilter> outFilters, |
| 7829 | List<ComponentName> outActivities, String packageName) { |
| 7830 | |
| 7831 | int num = 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7832 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7833 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7834 | final Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7835 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7836 | final PreferredActivity pa = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7837 | if (packageName == null |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 7838 | || pa.mPref.mComponent.getPackageName().equals(packageName)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7839 | if (outFilters != null) { |
| 7840 | outFilters.add(new IntentFilter(pa)); |
| 7841 | } |
| 7842 | if (outActivities != null) { |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 7843 | outActivities.add(pa.mPref.mComponent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7844 | } |
| 7845 | } |
| 7846 | } |
| 7847 | } |
| 7848 | |
| 7849 | return num; |
| 7850 | } |
| 7851 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7852 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7853 | public void setApplicationEnabledSetting(String appPackageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7854 | int newState, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7855 | if (!sUserManager.exists(userId)) return; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7856 | setEnabledSetting(appPackageName, null, newState, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7857 | } |
| 7858 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7859 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7860 | public void setComponentEnabledSetting(ComponentName componentName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7861 | int newState, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7862 | if (!sUserManager.exists(userId)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7863 | setEnabledSetting(componentName.getPackageName(), |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7864 | componentName.getClassName(), newState, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7865 | } |
| 7866 | |
| 7867 | private void setEnabledSetting( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7868 | final String packageName, String className, int newState, final int flags, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7869 | if (!(newState == COMPONENT_ENABLED_STATE_DEFAULT |
| 7870 | || newState == COMPONENT_ENABLED_STATE_ENABLED |
Dianne Hackborn | 0ac3031 | 2011-06-17 14:49:23 -0700 | [diff] [blame] | 7871 | || newState == COMPONENT_ENABLED_STATE_DISABLED |
| 7872 | || newState == COMPONENT_ENABLED_STATE_DISABLED_USER)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7873 | throw new IllegalArgumentException("Invalid new component state: " |
| 7874 | + newState); |
| 7875 | } |
| 7876 | PackageSetting pkgSetting; |
| 7877 | final int uid = Binder.getCallingUid(); |
| 7878 | final int permission = mContext.checkCallingPermission( |
| 7879 | android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7880 | checkValidCaller(uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7881 | final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7882 | boolean sendNow = false; |
| 7883 | boolean isApp = (className == null); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 7884 | String componentName = isApp ? packageName : className; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7885 | int packageUid = -1; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 7886 | ArrayList<String> components; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7887 | |
| 7888 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7889 | synchronized (mPackages) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7890 | pkgSetting = mSettings.mPackages.get(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7891 | if (pkgSetting == null) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7892 | if (className == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7893 | throw new IllegalArgumentException( |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7894 | "Unknown package: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7895 | } |
| 7896 | throw new IllegalArgumentException( |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7897 | "Unknown component: " + packageName |
| 7898 | + "/" + className); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7899 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7900 | // Allow root and verify that userId is not being specified by a different user |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7901 | if (!allowedByPermission && !UserId.isSameApp(uid, pkgSetting.appId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7902 | throw new SecurityException( |
| 7903 | "Permission Denial: attempt to change component state from pid=" |
| 7904 | + Binder.getCallingPid() |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7905 | + ", uid=" + uid + ", package uid=" + pkgSetting.appId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7906 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7907 | if (className == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7908 | // We're dealing with an application/package level state change |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7909 | if (pkgSetting.getEnabled(userId) == newState) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 7910 | // Nothing to do |
| 7911 | return; |
| 7912 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7913 | pkgSetting.setEnabled(newState, userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7914 | // pkgSetting.pkg.mSetEnabled = newState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7915 | } else { |
| 7916 | // We're dealing with a component level state change |
Dianne Hackborn | 6569625 | 2012-03-05 18:49:21 -0800 | [diff] [blame] | 7917 | // First, verify that this is a valid class name. |
| 7918 | PackageParser.Package pkg = pkgSetting.pkg; |
| 7919 | if (pkg == null || !pkg.hasComponentClassName(className)) { |
| 7920 | if (pkg.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.JELLY_BEAN) { |
| 7921 | throw new IllegalArgumentException("Component class " + className |
| 7922 | + " does not exist in " + packageName); |
| 7923 | } else { |
| 7924 | Slog.w(TAG, "Failed setComponentEnabledSetting: component class " |
| 7925 | + className + " does not exist in " + packageName); |
| 7926 | } |
| 7927 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7928 | switch (newState) { |
| 7929 | case COMPONENT_ENABLED_STATE_ENABLED: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7930 | if (!pkgSetting.enableComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 7931 | return; |
| 7932 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7933 | break; |
| 7934 | case COMPONENT_ENABLED_STATE_DISABLED: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7935 | if (!pkgSetting.disableComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 7936 | return; |
| 7937 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7938 | break; |
| 7939 | case COMPONENT_ENABLED_STATE_DEFAULT: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7940 | if (!pkgSetting.restoreComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 7941 | return; |
| 7942 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7943 | break; |
| 7944 | default: |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7945 | Slog.e(TAG, "Invalid new component state: " + newState); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7946 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7947 | } |
| 7948 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7949 | mSettings.writePackageRestrictionsLPr(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7950 | packageUid = UserId.getUid(userId, pkgSetting.appId); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 7951 | components = mPendingBroadcasts.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7952 | final boolean newPackage = components == null; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 7953 | if (newPackage) { |
| 7954 | components = new ArrayList<String>(); |
| 7955 | } |
| 7956 | if (!components.contains(componentName)) { |
| 7957 | components.add(componentName); |
| 7958 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7959 | if ((flags&PackageManager.DONT_KILL_APP) == 0) { |
| 7960 | sendNow = true; |
| 7961 | // Purge entry from pending broadcast list if another one exists already |
| 7962 | // since we are sending one right away. |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 7963 | mPendingBroadcasts.remove(packageName); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7964 | } else { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 7965 | if (newPackage) { |
| 7966 | mPendingBroadcasts.put(packageName, components); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7967 | } |
| 7968 | if (!mHandler.hasMessages(SEND_PENDING_BROADCAST)) { |
| 7969 | // Schedule a message |
| 7970 | mHandler.sendEmptyMessageDelayed(SEND_PENDING_BROADCAST, BROADCAST_DELAY); |
| 7971 | } |
| 7972 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7973 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7974 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7975 | long callingId = Binder.clearCallingIdentity(); |
| 7976 | try { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7977 | if (sendNow) { |
| 7978 | sendPackageChangedBroadcast(packageName, |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 7979 | (flags&PackageManager.DONT_KILL_APP) != 0, components, packageUid); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7980 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7981 | } finally { |
| 7982 | Binder.restoreCallingIdentity(callingId); |
| 7983 | } |
| 7984 | } |
| 7985 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7986 | private void sendPackageChangedBroadcast(String packageName, |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 7987 | boolean killFlag, ArrayList<String> componentNames, int packageUid) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 7988 | if (DEBUG_INSTALL) |
| 7989 | Log.v(TAG, "Sending package changed: package=" + packageName + " components=" |
| 7990 | + componentNames); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 7991 | Bundle extras = new Bundle(4); |
| 7992 | extras.putString(Intent.EXTRA_CHANGED_COMPONENT_NAME, componentNames.get(0)); |
| 7993 | String nameList[] = new String[componentNames.size()]; |
| 7994 | componentNames.toArray(nameList); |
| 7995 | extras.putStringArray(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST, nameList); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 7996 | extras.putBoolean(Intent.EXTRA_DONT_KILL_APP, killFlag); |
| 7997 | extras.putInt(Intent.EXTRA_UID, packageUid); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7998 | sendPackageBroadcast(Intent.ACTION_PACKAGE_CHANGED, packageName, extras, null, null, |
| 7999 | UserId.getUserId(packageUid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8000 | } |
| 8001 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8002 | public void setPackageStoppedState(String packageName, boolean stopped, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8003 | if (!sUserManager.exists(userId)) return; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8004 | final int uid = Binder.getCallingUid(); |
| 8005 | final int permission = mContext.checkCallingOrSelfPermission( |
| 8006 | android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); |
| 8007 | final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8008 | checkValidCaller(uid, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8009 | // writer |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8010 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8011 | if (mSettings.setPackageStoppedStateLPw(packageName, stopped, allowedByPermission, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8012 | uid, userId)) { |
| 8013 | scheduleWritePackageRestrictionsLocked(userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8014 | } |
| 8015 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8016 | } |
| 8017 | |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8018 | public String getInstallerPackageName(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8019 | // reader |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8020 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8021 | return mSettings.getInstallerPackageNameLPr(packageName); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8022 | } |
| 8023 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8024 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8025 | @Override |
| 8026 | public int getApplicationEnabledSetting(String packageName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8027 | if (!sUserManager.exists(userId)) return COMPONENT_ENABLED_STATE_DISABLED; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8028 | int uid = Binder.getCallingUid(); |
| 8029 | checkValidCaller(uid, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8030 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8031 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8032 | return mSettings.getApplicationEnabledSettingLPr(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8033 | } |
| 8034 | } |
| 8035 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8036 | @Override |
| 8037 | public int getComponentEnabledSetting(ComponentName componentName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8038 | if (!sUserManager.exists(userId)) return COMPONENT_ENABLED_STATE_DISABLED; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8039 | int uid = Binder.getCallingUid(); |
| 8040 | checkValidCaller(uid, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8041 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8042 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8043 | return mSettings.getComponentEnabledSettingLPr(componentName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8044 | } |
| 8045 | } |
| 8046 | |
| 8047 | public void enterSafeMode() { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 8048 | enforceSystemOrRoot("Only the system can request entering safe mode"); |
| 8049 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8050 | if (!mSystemReady) { |
| 8051 | mSafeMode = true; |
| 8052 | } |
| 8053 | } |
| 8054 | |
| 8055 | public void systemReady() { |
| 8056 | mSystemReady = true; |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8057 | |
| 8058 | // Read the compatibilty setting when the system is ready. |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 8059 | boolean compatibilityModeEnabled = android.provider.Settings.System.getInt( |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8060 | mContext.getContentResolver(), |
| 8061 | android.provider.Settings.System.COMPATIBILITY_MODE, 1) == 1; |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 8062 | PackageParser.setCompatibilityModeEnabled(compatibilityModeEnabled); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8063 | if (DEBUG_SETTINGS) { |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 8064 | Log.d(TAG, "compatibility mode:" + compatibilityModeEnabled); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8065 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8066 | } |
| 8067 | |
| 8068 | public boolean isSafeMode() { |
| 8069 | return mSafeMode; |
| 8070 | } |
| 8071 | |
| 8072 | public boolean hasSystemUidErrors() { |
| 8073 | return mHasSystemUidErrors; |
| 8074 | } |
| 8075 | |
| 8076 | static String arrayToString(int[] array) { |
| 8077 | StringBuffer buf = new StringBuffer(128); |
| 8078 | buf.append('['); |
| 8079 | if (array != null) { |
| 8080 | for (int i=0; i<array.length; i++) { |
| 8081 | if (i > 0) buf.append(", "); |
| 8082 | buf.append(array[i]); |
| 8083 | } |
| 8084 | } |
| 8085 | buf.append(']'); |
| 8086 | return buf.toString(); |
| 8087 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8088 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8089 | static class DumpState { |
| 8090 | public static final int DUMP_LIBS = 1 << 0; |
| 8091 | |
| 8092 | public static final int DUMP_FEATURES = 1 << 1; |
| 8093 | |
| 8094 | public static final int DUMP_RESOLVERS = 1 << 2; |
| 8095 | |
| 8096 | public static final int DUMP_PERMISSIONS = 1 << 3; |
| 8097 | |
| 8098 | public static final int DUMP_PACKAGES = 1 << 4; |
| 8099 | |
| 8100 | public static final int DUMP_SHARED_USERS = 1 << 5; |
| 8101 | |
| 8102 | public static final int DUMP_MESSAGES = 1 << 6; |
| 8103 | |
| 8104 | public static final int DUMP_PROVIDERS = 1 << 7; |
| 8105 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8106 | public static final int DUMP_VERIFIERS = 1 << 8; |
| 8107 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8108 | public static final int OPTION_SHOW_FILTERS = 1 << 0; |
| 8109 | |
| 8110 | private int mTypes; |
| 8111 | |
| 8112 | private int mOptions; |
| 8113 | |
| 8114 | private boolean mTitlePrinted; |
| 8115 | |
| 8116 | private SharedUserSetting mSharedUser; |
| 8117 | |
| 8118 | public boolean isDumping(int type) { |
| 8119 | if (mTypes == 0) { |
| 8120 | return true; |
| 8121 | } |
| 8122 | |
| 8123 | return (mTypes & type) != 0; |
| 8124 | } |
| 8125 | |
| 8126 | public void setDump(int type) { |
| 8127 | mTypes |= type; |
| 8128 | } |
| 8129 | |
| 8130 | public boolean isOptionEnabled(int option) { |
| 8131 | return (mOptions & option) != 0; |
| 8132 | } |
| 8133 | |
| 8134 | public void setOptionEnabled(int option) { |
| 8135 | mOptions |= option; |
| 8136 | } |
| 8137 | |
| 8138 | public boolean onTitlePrinted() { |
| 8139 | final boolean printed = mTitlePrinted; |
| 8140 | mTitlePrinted = true; |
| 8141 | return printed; |
| 8142 | } |
| 8143 | |
| 8144 | public boolean getTitlePrinted() { |
| 8145 | return mTitlePrinted; |
| 8146 | } |
| 8147 | |
| 8148 | public void setTitlePrinted(boolean enabled) { |
| 8149 | mTitlePrinted = enabled; |
| 8150 | } |
| 8151 | |
| 8152 | public SharedUserSetting getSharedUser() { |
| 8153 | return mSharedUser; |
| 8154 | } |
| 8155 | |
| 8156 | public void setSharedUser(SharedUserSetting user) { |
| 8157 | mSharedUser = user; |
| 8158 | } |
| 8159 | } |
| 8160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8161 | @Override |
| 8162 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 8163 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 8164 | != PackageManager.PERMISSION_GRANTED) { |
| 8165 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8166 | + Binder.getCallingPid() |
| 8167 | + ", uid=" + Binder.getCallingUid() |
| 8168 | + " without permission " |
| 8169 | + android.Manifest.permission.DUMP); |
| 8170 | return; |
| 8171 | } |
| 8172 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8173 | DumpState dumpState = new DumpState(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8174 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8175 | String packageName = null; |
| 8176 | |
| 8177 | int opti = 0; |
| 8178 | while (opti < args.length) { |
| 8179 | String opt = args[opti]; |
| 8180 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8181 | break; |
| 8182 | } |
| 8183 | opti++; |
| 8184 | if ("-a".equals(opt)) { |
| 8185 | // Right now we only know how to print all. |
| 8186 | } else if ("-h".equals(opt)) { |
| 8187 | pw.println("Package manager dump options:"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8188 | pw.println(" [-h] [-f] [cmd] ..."); |
| 8189 | pw.println(" -f: print details of intent filters"); |
| 8190 | pw.println(" -h: print this help"); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8191 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8192 | pw.println(" l[ibraries]: list known shared libraries"); |
| 8193 | pw.println(" f[ibraries]: list device features"); |
| 8194 | pw.println(" r[esolvers]: dump intent resolvers"); |
| 8195 | pw.println(" perm[issions]: dump permissions"); |
| 8196 | pw.println(" prov[iders]: dump content providers"); |
| 8197 | pw.println(" p[ackages]: dump installed packages"); |
| 8198 | pw.println(" s[hared-users]: dump shared user IDs"); |
| 8199 | pw.println(" m[essages]: print collected runtime messages"); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8200 | pw.println(" v[erifiers]: print package verifier info"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8201 | pw.println(" <package.name>: info about given package"); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8202 | return; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8203 | } else if ("-f".equals(opt)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8204 | dumpState.setOptionEnabled(DumpState.OPTION_SHOW_FILTERS); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8205 | } else { |
| 8206 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 8207 | } |
| 8208 | } |
| 8209 | |
| 8210 | // Is the caller requesting to dump a particular piece of data? |
| 8211 | if (opti < args.length) { |
| 8212 | String cmd = args[opti]; |
| 8213 | opti++; |
| 8214 | // Is this a package name? |
| 8215 | if ("android".equals(cmd) || cmd.contains(".")) { |
| 8216 | packageName = cmd; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8217 | } else if ("l".equals(cmd) || "libraries".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8218 | dumpState.setDump(DumpState.DUMP_LIBS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8219 | } else if ("f".equals(cmd) || "features".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8220 | dumpState.setDump(DumpState.DUMP_FEATURES); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8221 | } else if ("r".equals(cmd) || "resolvers".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8222 | dumpState.setDump(DumpState.DUMP_RESOLVERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8223 | } else if ("perm".equals(cmd) || "permissions".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8224 | dumpState.setDump(DumpState.DUMP_PERMISSIONS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8225 | } else if ("p".equals(cmd) || "packages".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8226 | dumpState.setDump(DumpState.DUMP_PACKAGES); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8227 | } else if ("s".equals(cmd) || "shared-users".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8228 | dumpState.setDump(DumpState.DUMP_SHARED_USERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8229 | } else if ("prov".equals(cmd) || "providers".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8230 | dumpState.setDump(DumpState.DUMP_PROVIDERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8231 | } else if ("m".equals(cmd) || "messages".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8232 | dumpState.setDump(DumpState.DUMP_MESSAGES); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8233 | } else if ("v".equals(cmd) || "verifiers".equals(cmd)) { |
| 8234 | dumpState.setDump(DumpState.DUMP_VERIFIERS); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8235 | } |
| 8236 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8237 | |
| 8238 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8239 | synchronized (mPackages) { |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8240 | if (dumpState.isDumping(DumpState.DUMP_VERIFIERS) && packageName == null) { |
| 8241 | if (dumpState.onTitlePrinted()) |
| 8242 | pw.println(" "); |
| 8243 | pw.println("Verifiers:"); |
| 8244 | pw.print(" Required: "); |
| 8245 | pw.print(mRequiredVerifierPackage); |
| 8246 | pw.print(" (uid="); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8247 | pw.print(getPackageUid(mRequiredVerifierPackage, 0)); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8248 | pw.println(")"); |
| 8249 | } |
| 8250 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8251 | if (dumpState.isDumping(DumpState.DUMP_LIBS) && packageName == null) { |
| 8252 | if (dumpState.onTitlePrinted()) |
| 8253 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8254 | pw.println("Libraries:"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8255 | final Iterator<String> it = mSharedLibraries.keySet().iterator(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8256 | while (it.hasNext()) { |
| 8257 | String name = it.next(); |
| 8258 | pw.print(" "); |
| 8259 | pw.print(name); |
| 8260 | pw.print(" -> "); |
| 8261 | pw.println(mSharedLibraries.get(name)); |
| 8262 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8263 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8264 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8265 | if (dumpState.isDumping(DumpState.DUMP_FEATURES) && packageName == null) { |
| 8266 | if (dumpState.onTitlePrinted()) |
| 8267 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8268 | pw.println("Features:"); |
| 8269 | Iterator<String> it = mAvailableFeatures.keySet().iterator(); |
| 8270 | while (it.hasNext()) { |
| 8271 | String name = it.next(); |
| 8272 | pw.print(" "); |
| 8273 | pw.println(name); |
| 8274 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8275 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8276 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8277 | if (dumpState.isDumping(DumpState.DUMP_RESOLVERS)) { |
| 8278 | if (mActivities.dump(pw, dumpState.getTitlePrinted() ? "\nActivity Resolver Table:" |
| 8279 | : "Activity Resolver Table:", " ", packageName, |
| 8280 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8281 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8282 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8283 | if (mReceivers.dump(pw, dumpState.getTitlePrinted() ? "\nReceiver Resolver Table:" |
| 8284 | : "Receiver Resolver Table:", " ", packageName, |
| 8285 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8286 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8287 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8288 | if (mServices.dump(pw, dumpState.getTitlePrinted() ? "\nService Resolver Table:" |
| 8289 | : "Service Resolver Table:", " ", packageName, |
| 8290 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8291 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8292 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8293 | if (mSettings.mPreferredActivities.dump(pw, |
| 8294 | dumpState.getTitlePrinted() ? "\nPreferred Activities:" |
| 8295 | : "Preferred Activities:", " ", |
| 8296 | packageName, dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8297 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8298 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8299 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8300 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8301 | if (dumpState.isDumping(DumpState.DUMP_PERMISSIONS)) { |
| 8302 | mSettings.dumpPermissionsLPr(pw, packageName, dumpState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8303 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8304 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8305 | if (dumpState.isDumping(DumpState.DUMP_PROVIDERS)) { |
| 8306 | boolean printedSomething = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8307 | for (PackageParser.Provider p : mProvidersByComponent.values()) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8308 | if (packageName != null && !packageName.equals(p.info.packageName)) { |
| 8309 | continue; |
| 8310 | } |
| 8311 | if (!printedSomething) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8312 | if (dumpState.onTitlePrinted()) |
| 8313 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8314 | pw.println("Registered ContentProviders:"); |
| 8315 | printedSomething = true; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8316 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8317 | pw.print(" "); pw.print(p.getComponentShortName()); pw.println(":"); |
| 8318 | pw.print(" "); pw.println(p.toString()); |
| 8319 | } |
| 8320 | printedSomething = false; |
| 8321 | for (Map.Entry<String, PackageParser.Provider> entry : mProviders.entrySet()) { |
| 8322 | PackageParser.Provider p = entry.getValue(); |
| 8323 | if (packageName != null && !packageName.equals(p.info.packageName)) { |
| 8324 | continue; |
| 8325 | } |
| 8326 | if (!printedSomething) { |
| 8327 | if (dumpState.onTitlePrinted()) |
| 8328 | pw.println(" "); |
| 8329 | pw.println("ContentProvider Authorities:"); |
| 8330 | printedSomething = true; |
| 8331 | } |
| 8332 | pw.print(" ["); pw.print(entry.getKey()); pw.println("]:"); |
| 8333 | pw.print(" "); pw.println(p.toString()); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8334 | } |
| 8335 | } |
| 8336 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8337 | if (dumpState.isDumping(DumpState.DUMP_PACKAGES)) { |
| 8338 | mSettings.dumpPackagesLPr(pw, packageName, dumpState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8339 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8340 | |
| 8341 | if (dumpState.isDumping(DumpState.DUMP_SHARED_USERS)) { |
| 8342 | mSettings.dumpSharedUsersLPr(pw, packageName, dumpState); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8343 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8344 | |
| 8345 | if (dumpState.isDumping(DumpState.DUMP_MESSAGES) && packageName == null) { |
| 8346 | if (dumpState.onTitlePrinted()) |
| 8347 | pw.println(" "); |
| 8348 | mSettings.dumpReadMessagesLPr(pw, dumpState); |
| 8349 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8350 | pw.println(" "); |
| 8351 | pw.println("Package warning messages:"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8352 | final File fname = getSettingsProblemFile(); |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 8353 | FileInputStream in = null; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8354 | try { |
| 8355 | in = new FileInputStream(fname); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8356 | final int avail = in.available(); |
| 8357 | final byte[] data = new byte[avail]; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8358 | in.read(data); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8359 | pw.print(new String(data)); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8360 | } catch (FileNotFoundException e) { |
| 8361 | } catch (IOException e) { |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 8362 | } finally { |
| 8363 | if (in != null) { |
| 8364 | try { |
| 8365 | in.close(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8366 | } catch (IOException e) { |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 8367 | } |
| 8368 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8369 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 8370 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8371 | } |
| 8372 | } |
| 8373 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8374 | // ------- apps on sdcard specific code ------- |
| 8375 | static final boolean DEBUG_SD_INSTALL = false; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8376 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8377 | private static final String SD_ENCRYPTION_KEYSTORE_NAME = "AppsOnSD"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8378 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8379 | private static final String SD_ENCRYPTION_ALGORITHM = "AES"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8380 | |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8381 | private boolean mMediaMounted = false; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8382 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8383 | private String getEncryptKey() { |
| 8384 | try { |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8385 | String sdEncKey = SystemKeyStore.getInstance().retrieveKeyHexString( |
| 8386 | SD_ENCRYPTION_KEYSTORE_NAME); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8387 | if (sdEncKey == null) { |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8388 | sdEncKey = SystemKeyStore.getInstance().generateNewKeyHexString(128, |
| 8389 | SD_ENCRYPTION_ALGORITHM, SD_ENCRYPTION_KEYSTORE_NAME); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8390 | if (sdEncKey == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8391 | Slog.e(TAG, "Failed to create encryption keys"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8392 | return null; |
| 8393 | } |
| 8394 | } |
| 8395 | return sdEncKey; |
| 8396 | } catch (NoSuchAlgorithmException nsae) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8397 | Slog.e(TAG, "Failed to create encryption keys with exception: " + nsae); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8398 | return null; |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 8399 | } catch (IOException ioe) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8400 | Slog.e(TAG, "Failed to retrieve encryption keys with exception: " + ioe); |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 8401 | return null; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8402 | } |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 8403 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8404 | } |
| 8405 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8406 | /* package */static String getTempContainerId() { |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 8407 | int tmpIdx = 1; |
| 8408 | String list[] = PackageHelper.getSecureContainerList(); |
| 8409 | if (list != null) { |
| 8410 | for (final String name : list) { |
| 8411 | // Ignore null and non-temporary container entries |
| 8412 | if (name == null || !name.startsWith(mTempContainerPrefix)) { |
| 8413 | continue; |
| 8414 | } |
| 8415 | |
| 8416 | String subStr = name.substring(mTempContainerPrefix.length()); |
| 8417 | try { |
| 8418 | int cid = Integer.parseInt(subStr); |
| 8419 | if (cid >= tmpIdx) { |
| 8420 | tmpIdx = cid + 1; |
| 8421 | } |
| 8422 | } catch (NumberFormatException e) { |
| 8423 | } |
| 8424 | } |
| 8425 | } |
| 8426 | return mTempContainerPrefix + tmpIdx; |
| 8427 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8428 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8429 | /* |
| 8430 | * Update media status on PackageManager. |
| 8431 | */ |
| 8432 | public void updateExternalMediaStatus(final boolean mediaStatus, final boolean reportStatus) { |
| 8433 | int callingUid = Binder.getCallingUid(); |
| 8434 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 8435 | throw new SecurityException("Media status can only be updated by the system"); |
| 8436 | } |
| 8437 | // reader; this apparently protects mMediaMounted, but should probably |
| 8438 | // be a different lock in that case. |
| 8439 | synchronized (mPackages) { |
| 8440 | Log.i(TAG, "Updating external media status from " |
| 8441 | + (mMediaMounted ? "mounted" : "unmounted") + " to " |
| 8442 | + (mediaStatus ? "mounted" : "unmounted")); |
| 8443 | if (DEBUG_SD_INSTALL) |
| 8444 | Log.i(TAG, "updateExternalMediaStatus:: mediaStatus=" + mediaStatus |
| 8445 | + ", mMediaMounted=" + mMediaMounted); |
| 8446 | if (mediaStatus == mMediaMounted) { |
| 8447 | final Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, reportStatus ? 1 |
| 8448 | : 0, -1); |
| 8449 | mHandler.sendMessage(msg); |
| 8450 | return; |
| 8451 | } |
| 8452 | mMediaMounted = mediaStatus; |
| 8453 | } |
| 8454 | // Queue up an async operation since the package installation may take a |
| 8455 | // little while. |
| 8456 | mHandler.post(new Runnable() { |
| 8457 | public void run() { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8458 | updateExternalMediaStatusInner(mediaStatus, reportStatus); |
| 8459 | } |
| 8460 | }); |
| 8461 | } |
| 8462 | |
| 8463 | /* |
| 8464 | * Collect information of applications on external media, map them against |
| 8465 | * existing containers and update information based on current mount status. |
| 8466 | * Please note that we always have to report status if reportStatus has been |
| 8467 | * set to true especially when unloading packages. |
| 8468 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8469 | private void updateExternalMediaStatusInner(boolean isMounted, boolean reportStatus) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8470 | // Collection of uids |
| 8471 | int uidArr[] = null; |
| 8472 | // Collection of stale containers |
| 8473 | HashSet<String> removeCids = new HashSet<String>(); |
| 8474 | // Collection of packages on external media with valid containers. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8475 | HashMap<AsecInstallArgs, String> processCids = new HashMap<AsecInstallArgs, String>(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8476 | // Get list of secure containers. |
| 8477 | final String list[] = PackageHelper.getSecureContainerList(); |
| 8478 | if (list == null || list.length == 0) { |
| 8479 | Log.i(TAG, "No secure containers on sdcard"); |
| 8480 | } else { |
| 8481 | // Process list of secure containers and categorize them |
| 8482 | // as active or stale based on their package internal state. |
| 8483 | int uidList[] = new int[list.length]; |
| 8484 | int num = 0; |
| 8485 | // reader |
| 8486 | synchronized (mPackages) { |
| 8487 | for (String cid : list) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8488 | AsecInstallArgs args = new AsecInstallArgs(cid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8489 | if (DEBUG_SD_INSTALL) |
| 8490 | Log.i(TAG, "Processing container " + cid); |
| 8491 | String pkgName = args.getPackageName(); |
| 8492 | if (pkgName == null) { |
| 8493 | if (DEBUG_SD_INSTALL) |
| 8494 | Log.i(TAG, "Container : " + cid + " stale"); |
| 8495 | removeCids.add(cid); |
| 8496 | continue; |
| 8497 | } |
| 8498 | if (DEBUG_SD_INSTALL) |
| 8499 | Log.i(TAG, "Looking for pkg : " + pkgName); |
| 8500 | PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 8501 | // The package status is changed only if the code path |
| 8502 | // matches between settings and the container id. |
| 8503 | if (ps != null && ps.codePathString != null |
| 8504 | && ps.codePathString.equals(args.getCodePath())) { |
| 8505 | if (DEBUG_SD_INSTALL) |
| 8506 | Log.i(TAG, "Container : " + cid + " corresponds to pkg : " + pkgName |
| 8507 | + " at code path: " + ps.codePathString); |
| 8508 | // We do have a valid package installed on sdcard |
| 8509 | processCids.put(args, ps.codePathString); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8510 | int uid = ps.appId; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8511 | if (uid != -1) { |
| 8512 | uidList[num++] = uid; |
| 8513 | } |
| 8514 | } else { |
| 8515 | // Stale container on sdcard. Just delete |
| 8516 | if (DEBUG_SD_INSTALL) |
| 8517 | Log.i(TAG, "Container : " + cid + " stale"); |
| 8518 | removeCids.add(cid); |
| 8519 | } |
| 8520 | } |
| 8521 | } |
| 8522 | |
| 8523 | if (num > 0) { |
| 8524 | // Sort uid list |
| 8525 | Arrays.sort(uidList, 0, num); |
| 8526 | // Throw away duplicates |
| 8527 | uidArr = new int[num]; |
| 8528 | uidArr[0] = uidList[0]; |
| 8529 | int di = 0; |
| 8530 | for (int i = 1; i < num; i++) { |
| 8531 | if (uidList[i - 1] != uidList[i]) { |
| 8532 | uidArr[di++] = uidList[i]; |
| 8533 | } |
| 8534 | } |
| 8535 | } |
| 8536 | } |
| 8537 | // Process packages with valid entries. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8538 | if (isMounted) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8539 | if (DEBUG_SD_INSTALL) |
| 8540 | Log.i(TAG, "Loading packages"); |
| 8541 | loadMediaPackages(processCids, uidArr, removeCids); |
| 8542 | startCleaningPackages(); |
| 8543 | } else { |
| 8544 | if (DEBUG_SD_INSTALL) |
| 8545 | Log.i(TAG, "Unloading packages"); |
| 8546 | unloadMediaPackages(processCids, uidArr, reportStatus); |
| 8547 | } |
| 8548 | } |
| 8549 | |
| 8550 | private void sendResourcesChangedBroadcast(boolean mediaStatus, ArrayList<String> pkgList, |
| 8551 | int uidArr[], IIntentReceiver finishedReceiver) { |
| 8552 | int size = pkgList.size(); |
| 8553 | if (size > 0) { |
| 8554 | // Send broadcasts here |
| 8555 | Bundle extras = new Bundle(); |
| 8556 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, pkgList |
| 8557 | .toArray(new String[size])); |
| 8558 | if (uidArr != null) { |
| 8559 | extras.putIntArray(Intent.EXTRA_CHANGED_UID_LIST, uidArr); |
| 8560 | } |
| 8561 | String action = mediaStatus ? Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE |
| 8562 | : Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8563 | sendPackageBroadcast(action, null, extras, null, finishedReceiver, UserId.USER_ALL); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8564 | } |
| 8565 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 8566 | |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 8567 | /* |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8568 | * Look at potentially valid container ids from processCids If package |
| 8569 | * information doesn't match the one on record or package scanning fails, |
| 8570 | * the cid is added to list of removeCids. We currently don't delete stale |
| 8571 | * containers. |
| 8572 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8573 | private void loadMediaPackages(HashMap<AsecInstallArgs, String> processCids, int uidArr[], |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8574 | HashSet<String> removeCids) { |
| 8575 | ArrayList<String> pkgList = new ArrayList<String>(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8576 | Set<AsecInstallArgs> keys = processCids.keySet(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8577 | boolean doGc = false; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8578 | for (AsecInstallArgs args : keys) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8579 | String codePath = processCids.get(args); |
| 8580 | if (DEBUG_SD_INSTALL) |
| 8581 | Log.i(TAG, "Loading container : " + args.cid); |
| 8582 | int retCode = PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 8583 | try { |
| 8584 | // Make sure there are no container errors first. |
| 8585 | if (args.doPreInstall(PackageManager.INSTALL_SUCCEEDED) != PackageManager.INSTALL_SUCCEEDED) { |
| 8586 | Slog.e(TAG, "Failed to mount cid : " + args.cid |
| 8587 | + " when installing from sdcard"); |
| 8588 | continue; |
| 8589 | } |
| 8590 | // Check code path here. |
| 8591 | if (codePath == null || !codePath.equals(args.getCodePath())) { |
| 8592 | Slog.e(TAG, "Container " + args.cid + " cachepath " + args.getCodePath() |
| 8593 | + " does not match one in settings " + codePath); |
| 8594 | continue; |
| 8595 | } |
| 8596 | // Parse package |
| 8597 | int parseFlags = PackageParser.PARSE_ON_SDCARD | mDefParseFlags; |
| 8598 | doGc = true; |
| 8599 | synchronized (mInstallLock) { |
| 8600 | final PackageParser.Package pkg = scanPackageLI(new File(codePath), parseFlags, |
| 8601 | 0, 0); |
| 8602 | // Scan the package |
| 8603 | if (pkg != null) { |
| 8604 | /* |
| 8605 | * TODO why is the lock being held? doPostInstall is |
| 8606 | * called in other places without the lock. This needs |
| 8607 | * to be straightened out. |
| 8608 | */ |
| 8609 | // writer |
| 8610 | synchronized (mPackages) { |
| 8611 | retCode = PackageManager.INSTALL_SUCCEEDED; |
| 8612 | pkgList.add(pkg.packageName); |
| 8613 | // Post process args |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8614 | args.doPostInstall(PackageManager.INSTALL_SUCCEEDED, |
| 8615 | pkg.applicationInfo.uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8616 | } |
| 8617 | } else { |
| 8618 | Slog.i(TAG, "Failed to install pkg from " + codePath + " from sdcard"); |
| 8619 | } |
| 8620 | } |
| 8621 | |
| 8622 | } finally { |
| 8623 | if (retCode != PackageManager.INSTALL_SUCCEEDED) { |
| 8624 | // Don't destroy container here. Wait till gc clears things |
| 8625 | // up. |
| 8626 | removeCids.add(args.cid); |
| 8627 | } |
| 8628 | } |
| 8629 | } |
| 8630 | // writer |
| 8631 | synchronized (mPackages) { |
| 8632 | // If the platform SDK has changed since the last time we booted, |
| 8633 | // we need to re-grant app permission to catch any new ones that |
| 8634 | // appear. This is really a hack, and means that apps can in some |
| 8635 | // cases get permissions that the user didn't initially explicitly |
| 8636 | // allow... it would be nice to have some better way to handle |
| 8637 | // this situation. |
| 8638 | final boolean regrantPermissions = mSettings.mExternalSdkPlatform != mSdkVersion; |
| 8639 | if (regrantPermissions) |
| 8640 | Slog.i(TAG, "Platform changed from " + mSettings.mExternalSdkPlatform + " to " |
| 8641 | + mSdkVersion + "; regranting permissions for external storage"); |
| 8642 | mSettings.mExternalSdkPlatform = mSdkVersion; |
| 8643 | |
| 8644 | // Make sure group IDs have been assigned, and any permission |
| 8645 | // changes in other apps are accounted for |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 8646 | updatePermissionsLPw(null, null, UPDATE_PERMISSIONS_ALL |
| 8647 | | (regrantPermissions |
| 8648 | ? (UPDATE_PERMISSIONS_REPLACE_PKG|UPDATE_PERMISSIONS_REPLACE_ALL) |
| 8649 | : 0)); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8650 | // can downgrade to reader |
| 8651 | // Persist settings |
| 8652 | mSettings.writeLPr(); |
| 8653 | } |
| 8654 | // Send a broadcast to let everyone know we are done processing |
| 8655 | if (pkgList.size() > 0) { |
| 8656 | sendResourcesChangedBroadcast(true, pkgList, uidArr, null); |
| 8657 | } |
| 8658 | // Force gc to avoid any stale parser references that we might have. |
| 8659 | if (doGc) { |
| 8660 | Runtime.getRuntime().gc(); |
| 8661 | } |
| 8662 | // List stale containers and destroy stale temporary containers. |
| 8663 | if (removeCids != null) { |
| 8664 | for (String cid : removeCids) { |
| 8665 | if (cid.startsWith(mTempContainerPrefix)) { |
| 8666 | Log.i(TAG, "Destroying stale temporary container " + cid); |
| 8667 | PackageHelper.destroySdDir(cid); |
| 8668 | } else { |
| 8669 | Log.w(TAG, "Container " + cid + " is stale"); |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8670 | } |
| 8671 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8672 | } |
| 8673 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 8674 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 8675 | /* |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8676 | * Utility method to unload a list of specified containers |
| 8677 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8678 | private void unloadAllContainers(Set<AsecInstallArgs> cidArgs) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8679 | // Just unmount all valid containers. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8680 | for (AsecInstallArgs arg : cidArgs) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8681 | synchronized (mInstallLock) { |
| 8682 | arg.doPostDeleteLI(false); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 8683 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8684 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 8685 | } |
| 8686 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8687 | /* |
| 8688 | * Unload packages mounted on external media. This involves deleting package |
| 8689 | * data from internal structures, sending broadcasts about diabled packages, |
| 8690 | * gc'ing to free up references, unmounting all secure containers |
| 8691 | * corresponding to packages on external media, and posting a |
| 8692 | * UPDATED_MEDIA_STATUS message if status has been requested. Please note |
| 8693 | * that we always have to post this message if status has been requested no |
| 8694 | * matter what. |
| 8695 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8696 | private void unloadMediaPackages(HashMap<AsecInstallArgs, String> processCids, int uidArr[], |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8697 | final boolean reportStatus) { |
| 8698 | if (DEBUG_SD_INSTALL) |
| 8699 | Log.i(TAG, "unloading media packages"); |
| 8700 | ArrayList<String> pkgList = new ArrayList<String>(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8701 | ArrayList<AsecInstallArgs> failedList = new ArrayList<AsecInstallArgs>(); |
| 8702 | final Set<AsecInstallArgs> keys = processCids.keySet(); |
| 8703 | for (AsecInstallArgs args : keys) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8704 | String pkgName = args.getPackageName(); |
| 8705 | if (DEBUG_SD_INSTALL) |
| 8706 | Log.i(TAG, "Trying to unload pkg : " + pkgName); |
| 8707 | // Delete package internally |
| 8708 | PackageRemovedInfo outInfo = new PackageRemovedInfo(); |
| 8709 | synchronized (mInstallLock) { |
| 8710 | boolean res = deletePackageLI(pkgName, false, PackageManager.DONT_DELETE_DATA, |
| 8711 | outInfo, false); |
| 8712 | if (res) { |
| 8713 | pkgList.add(pkgName); |
| 8714 | } else { |
| 8715 | Slog.e(TAG, "Failed to delete pkg from sdcard : " + pkgName); |
| 8716 | failedList.add(args); |
| 8717 | } |
| 8718 | } |
| 8719 | } |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 8720 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8721 | // reader |
| 8722 | synchronized (mPackages) { |
| 8723 | // We didn't update the settings after removing each package; |
| 8724 | // write them now for all packages. |
| 8725 | mSettings.writeLPr(); |
| 8726 | } |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8727 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8728 | // We have to absolutely send UPDATED_MEDIA_STATUS only |
| 8729 | // after confirming that all the receivers processed the ordered |
| 8730 | // broadcast when packages get disabled, force a gc to clean things up. |
| 8731 | // and unload all the containers. |
| 8732 | if (pkgList.size() > 0) { |
| 8733 | sendResourcesChangedBroadcast(false, pkgList, uidArr, new IIntentReceiver.Stub() { |
| 8734 | public void performReceive(Intent intent, int resultCode, String data, |
| 8735 | Bundle extras, boolean ordered, boolean sticky) throws RemoteException { |
| 8736 | Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, |
| 8737 | reportStatus ? 1 : 0, 1, keys); |
| 8738 | mHandler.sendMessage(msg); |
| 8739 | } |
| 8740 | }); |
| 8741 | } else { |
| 8742 | Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, reportStatus ? 1 : 0, -1, |
| 8743 | keys); |
| 8744 | mHandler.sendMessage(msg); |
| 8745 | } |
| 8746 | } |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8747 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8748 | public void movePackage(final String packageName, final IPackageMoveObserver observer, |
| 8749 | final int flags) { |
| 8750 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MOVE_PACKAGE, null); |
| 8751 | int returnCode = PackageManager.MOVE_SUCCEEDED; |
| 8752 | int currFlags = 0; |
| 8753 | int newFlags = 0; |
| 8754 | // reader |
| 8755 | synchronized (mPackages) { |
| 8756 | PackageParser.Package pkg = mPackages.get(packageName); |
| 8757 | if (pkg == null) { |
| 8758 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
| 8759 | } else { |
| 8760 | // Disable moving fwd locked apps and system packages |
| 8761 | if (pkg.applicationInfo != null && isSystemApp(pkg)) { |
| 8762 | Slog.w(TAG, "Cannot move system application"); |
| 8763 | returnCode = PackageManager.MOVE_FAILED_SYSTEM_PACKAGE; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8764 | } else if (pkg.mOperationPending) { |
| 8765 | Slog.w(TAG, "Attempt to move package which has pending operations"); |
| 8766 | returnCode = PackageManager.MOVE_FAILED_OPERATION_PENDING; |
| 8767 | } else { |
| 8768 | // Find install location first |
| 8769 | if ((flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 |
| 8770 | && (flags & PackageManager.MOVE_INTERNAL) != 0) { |
| 8771 | Slog.w(TAG, "Ambigous flags specified for move location."); |
| 8772 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
| 8773 | } else { |
| 8774 | newFlags = (flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 ? PackageManager.INSTALL_EXTERNAL |
| 8775 | : PackageManager.INSTALL_INTERNAL; |
| 8776 | currFlags = isExternal(pkg) ? PackageManager.INSTALL_EXTERNAL |
| 8777 | : PackageManager.INSTALL_INTERNAL; |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 8778 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8779 | if (newFlags == currFlags) { |
| 8780 | Slog.w(TAG, "No move required. Trying to move to same location"); |
| 8781 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 8782 | } else { |
| 8783 | if (isForwardLocked(pkg)) { |
| 8784 | currFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 8785 | newFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 8786 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8787 | } |
| 8788 | } |
| 8789 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 8790 | pkg.mOperationPending = true; |
| 8791 | } |
| 8792 | } |
| 8793 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 8794 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8795 | /* |
| 8796 | * TODO this next block probably shouldn't be inside the lock. We |
| 8797 | * can't guarantee these won't change after this is fired off |
| 8798 | * anyway. |
| 8799 | */ |
| 8800 | if (returnCode != PackageManager.MOVE_SUCCEEDED) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8801 | processPendingMove(new MoveParams(null, observer, 0, packageName, null, -1), |
| 8802 | returnCode); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8803 | } else { |
| 8804 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| 8805 | InstallArgs srcArgs = createInstallArgs(currFlags, pkg.applicationInfo.sourceDir, |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 8806 | pkg.applicationInfo.publicSourceDir, pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8807 | MoveParams mp = new MoveParams(srcArgs, observer, newFlags, packageName, |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8808 | pkg.applicationInfo.dataDir, pkg.applicationInfo.uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8809 | msg.obj = mp; |
| 8810 | mHandler.sendMessage(msg); |
| 8811 | } |
| 8812 | } |
| 8813 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 8814 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8815 | private void processPendingMove(final MoveParams mp, final int currentStatus) { |
| 8816 | // Queue up an async operation since the package deletion may take a |
| 8817 | // little while. |
| 8818 | mHandler.post(new Runnable() { |
| 8819 | public void run() { |
| 8820 | // TODO fix this; this does nothing. |
| 8821 | mHandler.removeCallbacks(this); |
| 8822 | int returnCode = currentStatus; |
| 8823 | if (currentStatus == PackageManager.MOVE_SUCCEEDED) { |
| 8824 | int uidArr[] = null; |
| 8825 | ArrayList<String> pkgList = null; |
| 8826 | synchronized (mPackages) { |
| 8827 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 8828 | if (pkg == null) { |
| 8829 | Slog.w(TAG, " Package " + mp.packageName |
| 8830 | + " doesn't exist. Aborting move"); |
| 8831 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
| 8832 | } else if (!mp.srcArgs.getCodePath().equals(pkg.applicationInfo.sourceDir)) { |
| 8833 | Slog.w(TAG, "Package " + mp.packageName + " code path changed from " |
| 8834 | + mp.srcArgs.getCodePath() + " to " |
| 8835 | + pkg.applicationInfo.sourceDir |
| 8836 | + " Aborting move and returning error"); |
| 8837 | returnCode = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 8838 | } else { |
| 8839 | uidArr = new int[] { |
| 8840 | pkg.applicationInfo.uid |
| 8841 | }; |
| 8842 | pkgList = new ArrayList<String>(); |
| 8843 | pkgList.add(mp.packageName); |
| 8844 | } |
| 8845 | } |
| 8846 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 8847 | // Send resources unavailable broadcast |
| 8848 | sendResourcesChangedBroadcast(false, pkgList, uidArr, null); |
| 8849 | // Update package code and resource paths |
| 8850 | synchronized (mInstallLock) { |
| 8851 | synchronized (mPackages) { |
| 8852 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 8853 | // Recheck for package again. |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 8854 | if (pkg == null) { |
| 8855 | Slog.w(TAG, " Package " + mp.packageName |
| 8856 | + " doesn't exist. Aborting move"); |
| 8857 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8858 | } else if (!mp.srcArgs.getCodePath().equals( |
| 8859 | pkg.applicationInfo.sourceDir)) { |
| 8860 | Slog.w(TAG, "Package " + mp.packageName |
| 8861 | + " code path changed from " + mp.srcArgs.getCodePath() |
| 8862 | + " to " + pkg.applicationInfo.sourceDir |
| 8863 | + " Aborting move and returning error"); |
| 8864 | returnCode = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 8865 | } else { |
| 8866 | final String oldCodePath = pkg.mPath; |
| 8867 | final String newCodePath = mp.targetArgs.getCodePath(); |
| 8868 | final String newResPath = mp.targetArgs.getResourcePath(); |
| 8869 | final String newNativePath = mp.targetArgs |
| 8870 | .getNativeLibraryPath(); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 8871 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 8872 | try { |
| 8873 | final File newNativeDir = new File(newNativePath); |
| 8874 | |
| 8875 | final String libParentDir = newNativeDir.getParentFile() |
| 8876 | .getCanonicalPath(); |
| 8877 | if (newNativeDir.getParentFile().getCanonicalPath() |
| 8878 | .equals(pkg.applicationInfo.dataDir)) { |
| 8879 | if (mInstaller |
| 8880 | .unlinkNativeLibraryDirectory(pkg.applicationInfo.dataDir) < 0) { |
| 8881 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 8882 | } else { |
| 8883 | NativeLibraryHelper.copyNativeBinariesIfNeededLI( |
| 8884 | new File(newCodePath), newNativeDir); |
| 8885 | } |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 8886 | } else { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 8887 | if (mInstaller.linkNativeLibraryDirectory( |
| 8888 | pkg.applicationInfo.dataDir, newNativePath) < 0) { |
| 8889 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 8890 | } |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 8891 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 8892 | } catch (IOException e) { |
| 8893 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 8894 | } |
| 8895 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 8896 | |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 8897 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 8898 | pkg.mPath = newCodePath; |
| 8899 | // Move dex files around |
| 8900 | if (moveDexFilesLI(pkg) != PackageManager.INSTALL_SUCCEEDED) { |
| 8901 | // Moving of dex files failed. Set |
| 8902 | // error code and abort move. |
| 8903 | pkg.mPath = pkg.mScanPath; |
| 8904 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 8905 | } |
| 8906 | } |
| 8907 | |
| 8908 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8909 | pkg.mScanPath = newCodePath; |
| 8910 | pkg.applicationInfo.sourceDir = newCodePath; |
| 8911 | pkg.applicationInfo.publicSourceDir = newResPath; |
| 8912 | pkg.applicationInfo.nativeLibraryDir = newNativePath; |
| 8913 | PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 8914 | ps.codePath = new File(pkg.applicationInfo.sourceDir); |
| 8915 | ps.codePathString = ps.codePath.getPath(); |
| 8916 | ps.resourcePath = new File( |
| 8917 | pkg.applicationInfo.publicSourceDir); |
| 8918 | ps.resourcePathString = ps.resourcePath.getPath(); |
| 8919 | ps.nativeLibraryPathString = newNativePath; |
| 8920 | // Set the application info flag |
| 8921 | // correctly. |
| 8922 | if ((mp.flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
| 8923 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| 8924 | } else { |
| 8925 | pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| 8926 | } |
| 8927 | ps.setFlags(pkg.applicationInfo.flags); |
| 8928 | mAppDirs.remove(oldCodePath); |
| 8929 | mAppDirs.put(newCodePath, pkg); |
| 8930 | // Persist settings |
| 8931 | mSettings.writeLPr(); |
| 8932 | } |
| 8933 | } |
| 8934 | } |
| 8935 | } |
| 8936 | // Send resources available broadcast |
| 8937 | sendResourcesChangedBroadcast(true, pkgList, uidArr, null); |
| 8938 | } |
| 8939 | } |
| 8940 | if (returnCode != PackageManager.MOVE_SUCCEEDED) { |
| 8941 | // Clean up failed installation |
| 8942 | if (mp.targetArgs != null) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8943 | mp.targetArgs.doPostInstall(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, |
| 8944 | -1); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8945 | } |
| 8946 | } else { |
| 8947 | // Force a gc to clear things up. |
| 8948 | Runtime.getRuntime().gc(); |
| 8949 | // Delete older code |
| 8950 | synchronized (mInstallLock) { |
| 8951 | mp.srcArgs.doPostDeleteLI(true); |
| 8952 | } |
| 8953 | } |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 8954 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8955 | // Allow more operations on this file if we didn't fail because |
| 8956 | // an operation was already pending for this package. |
| 8957 | if (returnCode != PackageManager.MOVE_FAILED_OPERATION_PENDING) { |
| 8958 | synchronized (mPackages) { |
| 8959 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 8960 | if (pkg != null) { |
| 8961 | pkg.mOperationPending = false; |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 8962 | } |
| 8963 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8964 | } |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 8965 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8966 | IPackageMoveObserver observer = mp.observer; |
| 8967 | if (observer != null) { |
| 8968 | try { |
| 8969 | observer.packageMoved(mp.packageName, returnCode); |
| 8970 | } catch (RemoteException e) { |
| 8971 | Log.i(TAG, "Observer no longer exists."); |
| 8972 | } |
| 8973 | } |
| 8974 | } |
| 8975 | }); |
| 8976 | } |
| 8977 | |
| 8978 | public boolean setInstallLocation(int loc) { |
| 8979 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS, |
| 8980 | null); |
| 8981 | if (getInstallLocation() == loc) { |
| 8982 | return true; |
| 8983 | } |
| 8984 | if (loc == PackageHelper.APP_INSTALL_AUTO || loc == PackageHelper.APP_INSTALL_INTERNAL |
| 8985 | || loc == PackageHelper.APP_INSTALL_EXTERNAL) { |
| 8986 | android.provider.Settings.System.putInt(mContext.getContentResolver(), |
| 8987 | android.provider.Settings.Secure.DEFAULT_INSTALL_LOCATION, loc); |
| 8988 | return true; |
| 8989 | } |
| 8990 | return false; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 8991 | } |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 8992 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8993 | public int getInstallLocation() { |
| 8994 | return android.provider.Settings.System.getInt(mContext.getContentResolver(), |
| 8995 | android.provider.Settings.Secure.DEFAULT_INSTALL_LOCATION, |
| 8996 | PackageHelper.APP_INSTALL_AUTO); |
| 8997 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 8998 | |
| 8999 | public UserInfo createUser(String name, int flags) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 9000 | // TODO(kroot): Add a real permission for creating users |
| 9001 | enforceSystemOrRoot("Only the system can create users"); |
| 9002 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9003 | UserInfo userInfo = sUserManager.createUser(name, flags); |
| 9004 | if (userInfo != null) { |
| 9005 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 9006 | addedIntent.putExtra(Intent.EXTRA_USERID, userInfo.id); |
| 9007 | mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_ACCOUNTS); |
| 9008 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9009 | return userInfo; |
| 9010 | } |
| 9011 | |
| 9012 | public boolean removeUser(int userId) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 9013 | // TODO(kroot): Add a real permission for removing users |
| 9014 | enforceSystemOrRoot("Only the system can remove users"); |
| 9015 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9016 | if (userId == 0 || !sUserManager.exists(userId)) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9017 | return false; |
| 9018 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9019 | |
| 9020 | cleanUpUser(userId); |
| 9021 | |
| 9022 | if (sUserManager.removeUser(userId)) { |
| 9023 | // Let other services shutdown any activity |
| 9024 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 9025 | addedIntent.putExtra(Intent.EXTRA_USERID, userId); |
| 9026 | mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_ACCOUNTS); |
| 9027 | } |
| 9028 | sUserManager.removePackageFolders(userId); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9029 | return true; |
| 9030 | } |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 9031 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9032 | private void cleanUpUser(int userId) { |
| 9033 | // Disable all the packages for the user first |
| 9034 | synchronized (mPackages) { |
| 9035 | Set<Entry<String, PackageSetting>> entries = mSettings.mPackages.entrySet(); |
| 9036 | for (Entry<String, PackageSetting> entry : entries) { |
| 9037 | entry.getValue().removeUser(userId); |
| 9038 | } |
| 9039 | if (mDirtyUsers.remove(userId)); |
| 9040 | mSettings.removeUserLPr(userId); |
| 9041 | } |
| 9042 | } |
| 9043 | |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 9044 | @Override |
| 9045 | public VerifierDeviceIdentity getVerifierDeviceIdentity() throws RemoteException { |
| 9046 | mContext.enforceCallingOrSelfPermission( |
| 9047 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 9048 | "Only package verification agents can read the verifier device identity"); |
| 9049 | |
| 9050 | synchronized (mPackages) { |
| 9051 | return mSettings.getVerifierDeviceIdentityLPw(); |
| 9052 | } |
| 9053 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9054 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9055 | @Override |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9056 | public List<UserInfo> getUsers() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9057 | enforceSystemOrRoot("Only the system can query users"); |
| 9058 | return sUserManager.getUsers(); |
| 9059 | } |
| 9060 | |
| 9061 | @Override |
| 9062 | public UserInfo getUser(int userId) { |
| 9063 | enforceSystemOrRoot("Only the system can remove users"); |
| 9064 | return sUserManager.getUser(userId); |
| 9065 | } |
| 9066 | |
| 9067 | @Override |
| 9068 | public void updateUserName(int userId, String name) { |
| 9069 | enforceSystemOrRoot("Only the system can rename users"); |
| 9070 | sUserManager.updateUserName(userId, name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9071 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9072 | |
| 9073 | @Override |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9074 | public void setPermissionEnforced(String permission, boolean enforced) { |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9075 | mContext.enforceCallingOrSelfPermission(GRANT_REVOKE_PERMISSIONS, null); |
| 9076 | if (READ_EXTERNAL_STORAGE.equals(permission)) { |
| 9077 | synchronized (mPackages) { |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9078 | if (mSettings.mReadExternalStorageEnforced != enforced) { |
| 9079 | mSettings.mReadExternalStorageEnforced = enforced; |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9080 | mSettings.writeLPr(); |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 9081 | |
| 9082 | // kill any non-foreground processes so we restart them and |
| 9083 | // grant/revoke the GID. |
| 9084 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 9085 | if (am != null) { |
| 9086 | final long token = Binder.clearCallingIdentity(); |
| 9087 | try { |
| 9088 | am.killProcessesBelowForeground("setPermissionEnforcement"); |
| 9089 | } catch (RemoteException e) { |
| 9090 | } finally { |
| 9091 | Binder.restoreCallingIdentity(token); |
| 9092 | } |
| 9093 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9094 | } |
| 9095 | } |
| 9096 | } else { |
| 9097 | throw new IllegalArgumentException("No selective enforcement for " + permission); |
| 9098 | } |
| 9099 | } |
| 9100 | |
| 9101 | @Override |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9102 | public boolean isPermissionEnforced(String permission) { |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9103 | mContext.enforceCallingOrSelfPermission(GRANT_REVOKE_PERMISSIONS, null); |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9104 | synchronized (mPackages) { |
| 9105 | return isPermissionEnforcedLocked(permission); |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9106 | } |
| 9107 | } |
| 9108 | |
| 9109 | private boolean isPermissionEnforcedLocked(String permission) { |
| 9110 | if (READ_EXTERNAL_STORAGE.equals(permission)) { |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9111 | return mSettings.mReadExternalStorageEnforced; |
| 9112 | } else { |
| 9113 | return true; |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9114 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9115 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9116 | } |