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; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 56 | import android.content.pm.ContainerEncryptionParams; |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 57 | import android.content.pm.FeatureInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.content.pm.IPackageDataObserver; |
| 59 | import android.content.pm.IPackageDeleteObserver; |
| 60 | import android.content.pm.IPackageInstallObserver; |
| 61 | import android.content.pm.IPackageManager; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 62 | import android.content.pm.IPackageMoveObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.content.pm.IPackageStatsObserver; |
| 64 | import android.content.pm.InstrumentationInfo; |
| 65 | import android.content.pm.PackageInfo; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 66 | import android.content.pm.PackageInfoLite; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.content.pm.PackageManager; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 68 | import android.content.pm.PackageParser; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 69 | import android.content.pm.PackageStats; |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 70 | import android.content.pm.ParceledListSlice; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 71 | import android.content.pm.PermissionGroupInfo; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 72 | import android.content.pm.PermissionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.content.pm.ProviderInfo; |
| 74 | import android.content.pm.ResolveInfo; |
| 75 | import android.content.pm.ServiceInfo; |
| 76 | import android.content.pm.Signature; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 77 | import android.content.pm.UserInfo; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 78 | import android.content.pm.ManifestDigest; |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 79 | import android.content.pm.VerifierDeviceIdentity; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 80 | import android.content.pm.VerifierInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import android.net.Uri; |
| 82 | import android.os.Binder; |
Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 83 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.os.Bundle; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 85 | import android.os.Environment; |
| 86 | import android.os.FileObserver; |
| 87 | import android.os.FileUtils; |
| 88 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import android.os.HandlerThread; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 90 | import android.os.IBinder; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 91 | import android.os.Looper; |
| 92 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | import android.os.ParcelFileDescriptor; |
| 95 | import android.os.Process; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 96 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import android.os.ServiceManager; |
| 98 | import android.os.SystemClock; |
| 99 | import android.os.SystemProperties; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 100 | import android.os.UserId; |
Jeff Sharkey | f538577 | 2012-05-11 14:04:41 -0700 | [diff] [blame] | 101 | import android.provider.Settings.Secure; |
Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 102 | import android.security.SystemKeyStore; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 103 | import android.util.DisplayMetrics; |
| 104 | import android.util.EventLog; |
| 105 | import android.util.Log; |
| 106 | import android.util.LogPrinter; |
| 107 | import android.util.Slog; |
| 108 | import android.util.SparseArray; |
| 109 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.view.Display; |
| 111 | import android.view.WindowManager; |
| 112 | |
| 113 | import java.io.File; |
| 114 | import java.io.FileDescriptor; |
| 115 | import java.io.FileInputStream; |
| 116 | import java.io.FileNotFoundException; |
| 117 | import java.io.FileOutputStream; |
| 118 | import java.io.FileReader; |
| 119 | import java.io.FilenameFilter; |
| 120 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | import java.io.PrintWriter; |
Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 122 | import java.security.NoSuchAlgorithmException; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 123 | import java.security.PublicKey; |
| 124 | import java.security.cert.CertificateException; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 125 | import java.text.SimpleDateFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | import java.util.ArrayList; |
| 127 | import java.util.Arrays; |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 128 | import java.util.Collection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | import java.util.Collections; |
| 130 | import java.util.Comparator; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 131 | import java.util.Date; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 140 | import libcore.io.ErrnoException; |
Kenny Root | 3f99afc | 2012-04-14 10:13:10 -0700 | [diff] [blame] | 141 | import libcore.io.IoUtils; |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 142 | import libcore.io.Libcore; |
| 143 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Keep track of all those .apks everywhere. |
| 146 | * |
| 147 | * This is very central to the platform's security; please run the unit |
| 148 | * tests whenever making modifications here: |
| 149 | * |
| 150 | mmm frameworks/base/tests/AndroidTests |
| 151 | adb install -r -f out/target/product/passion/data/app/AndroidTests.apk |
| 152 | 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] | 153 | * |
| 154 | * {@hide} |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 155 | */ |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 156 | public class PackageManagerService extends IPackageManager.Stub { |
| 157 | static final String TAG = "PackageManager"; |
| 158 | static final boolean DEBUG_SETTINGS = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | private static final boolean DEBUG_PREFERRED = false; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 160 | static final boolean DEBUG_UPGRADE = false; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 161 | private static final boolean DEBUG_INSTALL = false; |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 162 | private static final boolean DEBUG_REMOVE = false; |
| 163 | private static final boolean DEBUG_SHOW_INFO = false; |
| 164 | private static final boolean DEBUG_PACKAGE_INFO = false; |
| 165 | private static final boolean DEBUG_INTENT_MATCHING = false; |
| 166 | private static final boolean DEBUG_PACKAGE_SCANNING = false; |
| 167 | private static final boolean DEBUG_APP_DIR_OBSERVER = false; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 168 | private static final boolean DEBUG_VERIFY = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | private static final int RADIO_UID = Process.PHONE_UID; |
Mike Lockwood | d42685d | 2009-09-03 09:25:22 -0400 | [diff] [blame] | 171 | private static final int LOG_UID = Process.LOG_UID; |
Nick Pelly | cd0e839 | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 172 | private static final int NFC_UID = Process.NFC_UID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | private static final boolean GET_CERTIFICATES = true; |
| 175 | |
| 176 | private static final int REMOVE_EVENTS = |
| 177 | FileObserver.CLOSE_WRITE | FileObserver.DELETE | FileObserver.MOVED_FROM; |
| 178 | private static final int ADD_EVENTS = |
| 179 | FileObserver.CLOSE_WRITE /*| FileObserver.CREATE*/ | FileObserver.MOVED_TO; |
| 180 | |
| 181 | private static final int OBSERVER_EVENTS = REMOVE_EVENTS | ADD_EVENTS; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 182 | // Suffix used during package installation when copying/moving |
| 183 | // package apks to install directory. |
| 184 | private static final String INSTALL_PACKAGE_SUFFIX = "-"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | |
| 186 | static final int SCAN_MONITOR = 1<<0; |
| 187 | static final int SCAN_NO_DEX = 1<<1; |
| 188 | static final int SCAN_FORCE_DEX = 1<<2; |
| 189 | static final int SCAN_UPDATE_SIGNATURE = 1<<3; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 190 | static final int SCAN_NEW_INSTALL = 1<<4; |
| 191 | static final int SCAN_NO_PATHS = 1<<5; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 192 | static final int SCAN_UPDATE_TIME = 1<<6; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 193 | static final int SCAN_DEFER_DEX = 1<<7; |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 194 | static final int SCAN_BOOTING = 1<<8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 196 | static final int REMOVE_CHATTY = 1<<16; |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 197 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Whether verification is enabled by default. |
| 200 | */ |
Brett Chabot | ae05e86 | 2011-08-24 18:38:26 -0700 | [diff] [blame] | 201 | // STOPSHIP: change this to true |
| 202 | private static final boolean DEFAULT_VERIFY_ENABLE = false; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * The default maximum time to wait for the verification agent to return in |
| 206 | * milliseconds. |
| 207 | */ |
rich cannings | aa8513e | 2012-05-21 16:15:23 -0700 | [diff] [blame] | 208 | private static final long DEFAULT_VERIFICATION_TIMEOUT = 10 * 1000; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 209 | |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 210 | /** |
| 211 | * The default response for package verification timeout. |
| 212 | * |
| 213 | * This can be either PackageManager.VERIFICATION_ALLOW or |
| 214 | * PackageManager.VERIFICATION_REJECT. |
| 215 | */ |
| 216 | private static final int DEFAULT_VERIFICATION_RESPONSE = PackageManager.VERIFICATION_ALLOW; |
| 217 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 218 | static final String DEFAULT_CONTAINER_PACKAGE = "com.android.defcontainer"; |
| 219 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 220 | static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName( |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 221 | DEFAULT_CONTAINER_PACKAGE, |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 222 | "com.android.defcontainer.DefaultContainerService"); |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 223 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 224 | private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive"; |
| 225 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 226 | private static final String LIB_DIR_NAME = "lib"; |
| 227 | |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 228 | static final String mTempContainerPrefix = "smdl2tmp"; |
| 229 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | final HandlerThread mHandlerThread = new HandlerThread("PackageManager", |
| 231 | Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 232 | final PackageHandler mHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | |
Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 234 | final int mSdkVersion = Build.VERSION.SDK_INT; |
| 235 | final String mSdkCodename = "REL".equals(Build.VERSION.CODENAME) |
| 236 | ? null : Build.VERSION.CODENAME; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | final Context mContext; |
| 239 | final boolean mFactoryTest; |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 240 | final boolean mOnlyCore; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 241 | final boolean mNoDexOpt; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | final DisplayMetrics mMetrics; |
| 243 | final int mDefParseFlags; |
| 244 | final String[] mSeparateProcesses; |
| 245 | |
| 246 | // This is where all application persistent data goes. |
| 247 | final File mAppDataDir; |
| 248 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 249 | // This is where all application persistent data goes for secondary users. |
| 250 | final File mUserAppDataDir; |
| 251 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 252 | /** The location for ASEC container files on internal storage. */ |
| 253 | final String mAsecInternalPath; |
| 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | // This is the object monitoring the framework dir. |
| 256 | final FileObserver mFrameworkInstallObserver; |
| 257 | |
| 258 | // This is the object monitoring the system app dir. |
| 259 | final FileObserver mSystemInstallObserver; |
| 260 | |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 261 | // This is the object monitoring the system app dir. |
| 262 | final FileObserver mVendorInstallObserver; |
| 263 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | // This is the object monitoring mAppInstallDir. |
| 265 | final FileObserver mAppInstallObserver; |
| 266 | |
| 267 | // This is the object monitoring mDrmAppPrivateInstallDir. |
| 268 | final FileObserver mDrmAppInstallObserver; |
| 269 | |
| 270 | // Used for priviledge escalation. MUST NOT BE CALLED WITH mPackages |
| 271 | // LOCK HELD. Can be called with mInstallLock held. |
| 272 | final Installer mInstaller; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | final File mFrameworkDir; |
| 275 | final File mSystemAppDir; |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 276 | final File mVendorAppDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | final File mAppInstallDir; |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 278 | final File mDalvikCacheDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | |
| 280 | // Directory containing the private parts (e.g. code and non-resource assets) of forward-locked |
| 281 | // apps. |
| 282 | final File mDrmAppPrivateInstallDir; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 283 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | // ---------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | // Lock for state used when installing and doing other long running |
| 287 | // operations. Methods that must be called with this lock held have |
| 288 | // the prefix "LI". |
| 289 | final Object mInstallLock = new Object(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 290 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | // These are the directories in the 3rd party applications installed dir |
| 292 | // that we have currently loaded packages from. Keys are the application's |
| 293 | // installed zip file (absolute codePath), and values are Package. |
| 294 | final HashMap<String, PackageParser.Package> mAppDirs = |
| 295 | new HashMap<String, PackageParser.Package>(); |
| 296 | |
| 297 | // Information for the parser to write more useful error messages. |
| 298 | File mScanningPath; |
| 299 | int mLastScanError; |
| 300 | |
| 301 | final int[] mOutPermissions = new int[3]; |
| 302 | |
| 303 | // ---------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 304 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | // Keys are String (package name), values are Package. This also serves |
| 306 | // as the lock for the global state. Methods that must be called with |
| 307 | // this lock held have the prefix "LP". |
| 308 | final HashMap<String, PackageParser.Package> mPackages = |
| 309 | new HashMap<String, PackageParser.Package>(); |
| 310 | |
| 311 | final Settings mSettings; |
| 312 | boolean mRestoredSettings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | |
| 314 | // Group-ids that are given to all packages as read from etc/permissions/*.xml. |
| 315 | int[] mGlobalGids; |
| 316 | |
| 317 | // These are the built-in uid -> permission mappings that were read from the |
| 318 | // etc/permissions.xml file. |
| 319 | final SparseArray<HashSet<String>> mSystemPermissions = |
| 320 | new SparseArray<HashSet<String>>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 321 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | // These are the built-in shared libraries that were read from the |
| 323 | // etc/permissions.xml file. |
| 324 | final HashMap<String, String> mSharedLibraries = new HashMap<String, String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 325 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 326 | // Temporary for building the final shared libraries for an .apk. |
| 327 | String[] mTmpSharedLibraries = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 328 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 329 | // These are the features this devices supports that were read from the |
| 330 | // etc/permissions.xml file. |
| 331 | final HashMap<String, FeatureInfo> mAvailableFeatures = |
| 332 | new HashMap<String, FeatureInfo>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 333 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | // All available activities, for your resolving pleasure. |
| 335 | final ActivityIntentResolver mActivities = |
| 336 | new ActivityIntentResolver(); |
| 337 | |
| 338 | // All available receivers, for your resolving pleasure. |
| 339 | final ActivityIntentResolver mReceivers = |
| 340 | new ActivityIntentResolver(); |
| 341 | |
| 342 | // All available services, for your resolving pleasure. |
| 343 | final ServiceIntentResolver mServices = new ServiceIntentResolver(); |
| 344 | |
| 345 | // Keys are String (provider class name), values are Provider. |
| 346 | final HashMap<ComponentName, PackageParser.Provider> mProvidersByComponent = |
| 347 | new HashMap<ComponentName, PackageParser.Provider>(); |
| 348 | |
| 349 | // Mapping from provider base names (first directory in content URI codePath) |
| 350 | // to the provider information. |
| 351 | final HashMap<String, PackageParser.Provider> mProviders = |
| 352 | new HashMap<String, PackageParser.Provider>(); |
| 353 | |
| 354 | // Mapping from instrumentation class names to info about them. |
| 355 | final HashMap<ComponentName, PackageParser.Instrumentation> mInstrumentation = |
| 356 | new HashMap<ComponentName, PackageParser.Instrumentation>(); |
| 357 | |
| 358 | // Mapping from permission names to info about them. |
| 359 | final HashMap<String, PackageParser.PermissionGroup> mPermissionGroups = |
| 360 | new HashMap<String, PackageParser.PermissionGroup>(); |
| 361 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 362 | // Packages whose data we have transfered into another package, thus |
| 363 | // should no longer exist. |
| 364 | final HashSet<String> mTransferedPackages = new HashSet<String>(); |
| 365 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 366 | // Broadcast actions that are only available to the system. |
| 367 | final HashSet<String> mProtectedBroadcasts = new HashSet<String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 368 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 369 | /** List of packages waiting for verification. */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 370 | final SparseArray<PackageVerificationState> mPendingVerification |
| 371 | = new SparseArray<PackageVerificationState>(); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 372 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 373 | final ArrayList<PackageParser.Package> mDeferredDexOpt = |
| 374 | new ArrayList<PackageParser.Package>(); |
| 375 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 376 | /** Token for keys in mPendingVerification. */ |
| 377 | private int mPendingVerificationToken = 0; |
| 378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | boolean mSystemReady; |
| 380 | boolean mSafeMode; |
| 381 | boolean mHasSystemUidErrors; |
| 382 | |
| 383 | ApplicationInfo mAndroidApplication; |
| 384 | final ActivityInfo mResolveActivity = new ActivityInfo(); |
| 385 | final ResolveInfo mResolveInfo = new ResolveInfo(); |
| 386 | ComponentName mResolveComponentName; |
| 387 | PackageParser.Package mPlatformPackage; |
| 388 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 389 | // Set of pending broadcasts for aggregating enable/disable of components. |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 390 | final HashMap<String, ArrayList<String>> mPendingBroadcasts |
| 391 | = new HashMap<String, ArrayList<String>>(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 392 | // Service Connection to remote media container service to copy |
| 393 | // package uri's from external media onto secure containers |
| 394 | // or internal storage. |
| 395 | private IMediaContainerService mContainerService = null; |
| 396 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 397 | static final int SEND_PENDING_BROADCAST = 1; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 398 | static final int MCS_BOUND = 3; |
| 399 | static final int END_COPY = 4; |
| 400 | static final int INIT_COPY = 5; |
| 401 | static final int MCS_UNBIND = 6; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 402 | static final int START_CLEANING_PACKAGE = 7; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 403 | static final int FIND_INSTALL_LOC = 8; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 404 | static final int POST_INSTALL = 9; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 405 | static final int MCS_RECONNECT = 10; |
| 406 | static final int MCS_GIVE_UP = 11; |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 407 | static final int UPDATED_MEDIA_STATUS = 12; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 408 | static final int WRITE_SETTINGS = 13; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 409 | static final int WRITE_PACKAGE_RESTRICTIONS = 14; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 410 | static final int PACKAGE_VERIFIED = 15; |
| 411 | static final int CHECK_PENDING_VERIFICATION = 16; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 412 | |
| 413 | static final int WRITE_SETTINGS_DELAY = 10*1000; // 10 seconds |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 414 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 415 | // Delay time in millisecs |
| 416 | static final int BROADCAST_DELAY = 10 * 1000; |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 417 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 418 | static UserManager sUserManager; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 419 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 420 | // Stores a list of users whose package restrictions file needs to be updated |
| 421 | private HashSet<Integer> mDirtyUsers = new HashSet<Integer>(); |
| 422 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 423 | final private DefaultContainerConnection mDefContainerConn = |
| 424 | new DefaultContainerConnection(); |
| 425 | class DefaultContainerConnection implements ServiceConnection { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 426 | public void onServiceConnected(ComponentName name, IBinder service) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 427 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceConnected"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 428 | IMediaContainerService imcs = |
| 429 | IMediaContainerService.Stub.asInterface(service); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 430 | mHandler.sendMessage(mHandler.obtainMessage(MCS_BOUND, imcs)); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | public void onServiceDisconnected(ComponentName name) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 434 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceDisconnected"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 435 | } |
| 436 | }; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 437 | |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 438 | // Recordkeeping of restore-after-install operations that are currently in flight |
| 439 | // between the Package Manager and the Backup Manager |
| 440 | class PostInstallData { |
| 441 | public InstallArgs args; |
| 442 | public PackageInstalledInfo res; |
| 443 | |
| 444 | PostInstallData(InstallArgs _a, PackageInstalledInfo _r) { |
| 445 | args = _a; |
| 446 | res = _r; |
| 447 | } |
| 448 | }; |
| 449 | final SparseArray<PostInstallData> mRunningInstalls = new SparseArray<PostInstallData>(); |
| 450 | int mNextInstallToken = 1; // nonzero; will be wrapped back to 1 when ++ overflows |
| 451 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 452 | private final String mRequiredVerifierPackage; |
| 453 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 454 | class PackageHandler extends Handler { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 455 | private boolean mBound = false; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 456 | final ArrayList<HandlerParams> mPendingInstalls = |
| 457 | new ArrayList<HandlerParams>(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 458 | |
| 459 | private boolean connectToService() { |
| 460 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Trying to bind to" + |
| 461 | " DefaultContainerService"); |
| 462 | Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 463 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 464 | if (mContext.bindService(service, mDefContainerConn, |
| 465 | Context.BIND_AUTO_CREATE)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 466 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 467 | mBound = true; |
| 468 | return true; |
| 469 | } |
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 | return false; |
| 472 | } |
| 473 | |
| 474 | private void disconnectService() { |
| 475 | mContainerService = null; |
| 476 | mBound = false; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 477 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 478 | mContext.unbindService(mDefContainerConn); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 479 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 480 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 481 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 482 | PackageHandler(Looper looper) { |
| 483 | super(looper); |
| 484 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 485 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 486 | public void handleMessage(Message msg) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 487 | try { |
| 488 | doHandleMessage(msg); |
| 489 | } finally { |
| 490 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | void doHandleMessage(Message msg) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 495 | switch (msg.what) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 496 | case INIT_COPY: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 497 | if (DEBUG_INSTALL) Slog.i(TAG, "init_copy"); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 498 | HandlerParams params = (HandlerParams) msg.obj; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 499 | int idx = mPendingInstalls.size(); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 500 | if (DEBUG_INSTALL) Slog.i(TAG, "idx=" + idx); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 501 | // If a bind was already initiated we dont really |
| 502 | // need to do anything. The pending install |
| 503 | // will be processed later on. |
| 504 | if (!mBound) { |
| 505 | // If this is the only one pending we might |
| 506 | // have to bind to the service again. |
| 507 | if (!connectToService()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 508 | Slog.e(TAG, "Failed to bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 509 | params.serviceError(); |
| 510 | return; |
| 511 | } else { |
| 512 | // Once we bind to the service, the first |
| 513 | // pending request will be processed. |
| 514 | mPendingInstalls.add(idx, params); |
| 515 | } |
| 516 | } else { |
| 517 | mPendingInstalls.add(idx, params); |
| 518 | // Already bound to the service. Just make |
| 519 | // sure we trigger off processing the first request. |
| 520 | if (idx == 0) { |
| 521 | mHandler.sendEmptyMessage(MCS_BOUND); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | break; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 525 | } |
| 526 | case MCS_BOUND: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 527 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_bound"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 528 | if (msg.obj != null) { |
| 529 | mContainerService = (IMediaContainerService) msg.obj; |
| 530 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 531 | if (mContainerService == null) { |
| 532 | // Something seriously wrong. Bail out |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 533 | Slog.e(TAG, "Cannot bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 534 | for (HandlerParams params : mPendingInstalls) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 535 | // Indicate service bind error |
| 536 | params.serviceError(); |
| 537 | } |
| 538 | mPendingInstalls.clear(); |
| 539 | } else if (mPendingInstalls.size() > 0) { |
| 540 | HandlerParams params = mPendingInstalls.get(0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 541 | if (params != null) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 542 | if (params.startCopy()) { |
| 543 | // We are done... look for more work or to |
| 544 | // go idle. |
| 545 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 546 | "Checking for more work or unbind..."); |
| 547 | // Delete pending install |
| 548 | if (mPendingInstalls.size() > 0) { |
| 549 | mPendingInstalls.remove(0); |
| 550 | } |
| 551 | if (mPendingInstalls.size() == 0) { |
| 552 | if (mBound) { |
| 553 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 554 | "Posting delayed MCS_UNBIND"); |
| 555 | removeMessages(MCS_UNBIND); |
| 556 | Message ubmsg = obtainMessage(MCS_UNBIND); |
| 557 | // Unbind after a little delay, to avoid |
| 558 | // continual thrashing. |
| 559 | sendMessageDelayed(ubmsg, 10000); |
| 560 | } |
| 561 | } else { |
| 562 | // There are more pending requests in queue. |
| 563 | // Just post MCS_BOUND message to trigger processing |
| 564 | // of next pending install. |
| 565 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 566 | "Posting MCS_BOUND for next woek"); |
| 567 | mHandler.sendEmptyMessage(MCS_BOUND); |
| 568 | } |
| 569 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 570 | } |
| 571 | } else { |
| 572 | // Should never happen ideally. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 573 | Slog.w(TAG, "Empty queue"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 574 | } |
| 575 | break; |
| 576 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 577 | case MCS_RECONNECT: { |
| 578 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_reconnect"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 579 | if (mPendingInstalls.size() > 0) { |
| 580 | if (mBound) { |
| 581 | disconnectService(); |
| 582 | } |
| 583 | if (!connectToService()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 584 | Slog.e(TAG, "Failed to bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 585 | for (HandlerParams params : mPendingInstalls) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 586 | // Indicate service bind error |
| 587 | params.serviceError(); |
| 588 | } |
| 589 | mPendingInstalls.clear(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | break; |
| 593 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 594 | case MCS_UNBIND: { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 595 | // If there is no actual work left, then time to unbind. |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 596 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_unbind"); |
| 597 | |
| 598 | if (mPendingInstalls.size() == 0 && mPendingVerification.size() == 0) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 599 | if (mBound) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 600 | if (DEBUG_INSTALL) Slog.i(TAG, "calling disconnectService()"); |
| 601 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 602 | disconnectService(); |
| 603 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 604 | } else if (mPendingInstalls.size() > 0) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 605 | // There are more pending requests in queue. |
| 606 | // Just post MCS_BOUND message to trigger processing |
| 607 | // of next pending install. |
| 608 | mHandler.sendEmptyMessage(MCS_BOUND); |
| 609 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 610 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 611 | break; |
| 612 | } |
| 613 | case MCS_GIVE_UP: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 614 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_giveup too many retries"); |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 615 | mPendingInstalls.remove(0); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 616 | break; |
| 617 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 618 | case SEND_PENDING_BROADCAST: { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 619 | String packages[]; |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 620 | ArrayList<String> components[]; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 621 | int size = 0; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 622 | int uids[]; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 623 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 624 | synchronized (mPackages) { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 625 | if (mPendingBroadcasts == null) { |
| 626 | return; |
| 627 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 628 | size = mPendingBroadcasts.size(); |
| 629 | if (size <= 0) { |
| 630 | // Nothing to be done. Just return |
| 631 | return; |
| 632 | } |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 633 | packages = new String[size]; |
| 634 | components = new ArrayList[size]; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 635 | uids = new int[size]; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 636 | Iterator<Map.Entry<String, ArrayList<String>>> |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 637 | it = mPendingBroadcasts.entrySet().iterator(); |
| 638 | int i = 0; |
| 639 | while (it.hasNext() && i < size) { |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 640 | Map.Entry<String, ArrayList<String>> ent = it.next(); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 641 | packages[i] = ent.getKey(); |
| 642 | components[i] = ent.getValue(); |
| 643 | PackageSetting ps = mSettings.mPackages.get(ent.getKey()); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 644 | uids[i] = (ps != null) ? ps.appId : -1; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 645 | i++; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 646 | } |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 647 | size = i; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 648 | mPendingBroadcasts.clear(); |
| 649 | } |
| 650 | // Send broadcasts |
| 651 | for (int i = 0; i < size; i++) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 652 | sendPackageChangedBroadcast(packages[i], true, components[i], uids[i]); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 653 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 654 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 655 | break; |
| 656 | } |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 657 | case START_CLEANING_PACKAGE: { |
| 658 | String packageName = (String)msg.obj; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 659 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 660 | synchronized (mPackages) { |
| 661 | if (!mSettings.mPackagesToBeCleaned.contains(packageName)) { |
| 662 | mSettings.mPackagesToBeCleaned.add(packageName); |
| 663 | } |
| 664 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 665 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 666 | startCleaningPackages(); |
| 667 | } break; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 668 | case POST_INSTALL: { |
| 669 | if (DEBUG_INSTALL) Log.v(TAG, "Handling post-install for " + msg.arg1); |
| 670 | PostInstallData data = mRunningInstalls.get(msg.arg1); |
| 671 | mRunningInstalls.delete(msg.arg1); |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 672 | boolean deleteOld = false; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 673 | |
| 674 | if (data != null) { |
| 675 | InstallArgs args = data.args; |
| 676 | PackageInstalledInfo res = data.res; |
| 677 | |
| 678 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 679 | res.removedInfo.sendBroadcast(false, true); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 680 | Bundle extras = new Bundle(1); |
| 681 | extras.putInt(Intent.EXTRA_UID, res.uid); |
| 682 | final boolean update = res.removedInfo.removedPackage != null; |
| 683 | if (update) { |
| 684 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 685 | } |
| 686 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, |
| 687 | res.pkg.applicationInfo.packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 688 | extras, null, null, UserId.USER_ALL); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 689 | if (update) { |
| 690 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, |
| 691 | res.pkg.applicationInfo.packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 692 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 693 | sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, |
| 694 | null, null, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 695 | res.pkg.applicationInfo.packageName, null, |
| 696 | UserId.USER_ALL); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 697 | } |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 698 | if (res.removedInfo.args != null) { |
| 699 | // Remove the replaced package's older resources safely now |
| 700 | deleteOld = true; |
| 701 | } |
Matt Finifter | f8a98ed | 2012-05-14 15:43:34 -0700 | [diff] [blame] | 702 | |
| 703 | // Log current value of "unknown sources" setting |
| 704 | EventLog.writeEvent(EventLogTags.UNKNOWN_SOURCES_ENABLED, |
| 705 | getUnknownSourcesSettings()); |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 706 | } |
| 707 | // Force a gc to clear up things |
| 708 | Runtime.getRuntime().gc(); |
| 709 | // We delete after a gc for applications on sdcard. |
| 710 | if (deleteOld) { |
| 711 | synchronized (mInstallLock) { |
| 712 | res.removedInfo.args.doPostDeleteLI(true); |
| 713 | } |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 714 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 715 | if (args.observer != null) { |
| 716 | try { |
| 717 | args.observer.packageInstalled(res.name, res.returnCode); |
| 718 | } catch (RemoteException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 719 | Slog.i(TAG, "Observer no longer exists."); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 723 | Slog.e(TAG, "Bogus post-install token " + msg.arg1); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 724 | } |
| 725 | } break; |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 726 | case UPDATED_MEDIA_STATUS: { |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 727 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Got message UPDATED_MEDIA_STATUS"); |
| 728 | boolean reportStatus = msg.arg1 == 1; |
| 729 | boolean doGc = msg.arg2 == 1; |
| 730 | if (DEBUG_SD_INSTALL) Log.i(TAG, "reportStatus=" + reportStatus + ", doGc = " + doGc); |
| 731 | if (doGc) { |
| 732 | // Force a gc to clear up stale containers. |
| 733 | Runtime.getRuntime().gc(); |
| 734 | } |
| 735 | if (msg.obj != null) { |
Dianne Hackborn | ba24e4d | 2011-09-01 11:17:06 -0700 | [diff] [blame] | 736 | @SuppressWarnings("unchecked") |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 737 | Set<AsecInstallArgs> args = (Set<AsecInstallArgs>) msg.obj; |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 738 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Unloading all containers"); |
| 739 | // Unload containers |
| 740 | unloadAllContainers(args); |
| 741 | } |
| 742 | if (reportStatus) { |
| 743 | try { |
| 744 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Invoking MountService call back"); |
| 745 | PackageHelper.getMountService().finishMediaUpdate(); |
| 746 | } catch (RemoteException e) { |
| 747 | Log.e(TAG, "MountService not running?"); |
| 748 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 749 | } |
| 750 | } break; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 751 | case WRITE_SETTINGS: { |
| 752 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 753 | synchronized (mPackages) { |
| 754 | removeMessages(WRITE_SETTINGS); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 755 | removeMessages(WRITE_PACKAGE_RESTRICTIONS); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 756 | mSettings.writeLPr(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 757 | mDirtyUsers.clear(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 758 | } |
| 759 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 760 | } break; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 761 | case WRITE_PACKAGE_RESTRICTIONS: { |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 762 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 763 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 764 | removeMessages(WRITE_PACKAGE_RESTRICTIONS); |
| 765 | for (int userId : mDirtyUsers) { |
| 766 | mSettings.writePackageRestrictionsLPr(userId); |
| 767 | } |
| 768 | mDirtyUsers.clear(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 769 | } |
| 770 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 771 | } break; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 772 | case CHECK_PENDING_VERIFICATION: { |
| 773 | final int verificationId = msg.arg1; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 774 | final PackageVerificationState state = mPendingVerification.get(verificationId); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 775 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 776 | if (state != null) { |
| 777 | final InstallArgs args = state.getInstallArgs(); |
Kenny Root | a503a0f | 2011-10-03 14:45:28 -0700 | [diff] [blame] | 778 | Slog.i(TAG, "Verification timed out for " + args.packageURI.toString()); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 779 | mPendingVerification.remove(verificationId); |
| 780 | |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 781 | int ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
| 782 | |
| 783 | if (getDefaultVerificationResponse() == PackageManager.VERIFICATION_ALLOW) { |
| 784 | Slog.i(TAG, "Continuing with installation of " + args.packageURI.toString()); |
| 785 | state.setVerifierResponse(Binder.getCallingUid(), PackageManager.VERIFICATION_ALLOW_WITHOUT_SUFFICIENT); |
| 786 | try { |
| 787 | ret = args.copyApk(mContainerService, true); |
| 788 | } catch (RemoteException e) { |
| 789 | Slog.e(TAG, "Could not contact the ContainerService"); |
| 790 | } |
| 791 | } |
| 792 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 793 | processPendingInstall(args, ret); |
| 794 | |
| 795 | mHandler.sendEmptyMessage(MCS_UNBIND); |
| 796 | } |
| 797 | |
| 798 | break; |
| 799 | } |
| 800 | case PACKAGE_VERIFIED: { |
| 801 | final int verificationId = msg.arg1; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 802 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 803 | final PackageVerificationState state = mPendingVerification.get(verificationId); |
| 804 | if (state == null) { |
Kenny Root | a503a0f | 2011-10-03 14:45:28 -0700 | [diff] [blame] | 805 | Slog.w(TAG, "Invalid verification token " + verificationId + " received"); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 806 | break; |
| 807 | } |
| 808 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 809 | final PackageVerificationResponse response = (PackageVerificationResponse) msg.obj; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 810 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 811 | state.setVerifierResponse(response.callerUid, response.code); |
| 812 | |
| 813 | if (state.isVerificationComplete()) { |
| 814 | mPendingVerification.remove(verificationId); |
| 815 | |
| 816 | final InstallArgs args = state.getInstallArgs(); |
| 817 | |
| 818 | int ret; |
| 819 | if (state.isInstallAllowed()) { |
| 820 | ret = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 821 | try { |
| 822 | ret = args.copyApk(mContainerService, true); |
| 823 | } catch (RemoteException e) { |
| 824 | Slog.e(TAG, "Could not contact the ContainerService"); |
| 825 | } |
| 826 | } else { |
| 827 | ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 828 | } |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 829 | |
| 830 | processPendingInstall(args, ret); |
| 831 | |
| 832 | mHandler.sendEmptyMessage(MCS_UNBIND); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 835 | break; |
| 836 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 840 | |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 841 | void scheduleWriteSettingsLocked() { |
| 842 | if (!mHandler.hasMessages(WRITE_SETTINGS)) { |
| 843 | mHandler.sendEmptyMessageDelayed(WRITE_SETTINGS, WRITE_SETTINGS_DELAY); |
| 844 | } |
| 845 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 846 | |
| 847 | void scheduleWritePackageRestrictionsLocked(int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 848 | if (!sUserManager.exists(userId)) return; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 849 | mDirtyUsers.add(userId); |
| 850 | if (!mHandler.hasMessages(WRITE_PACKAGE_RESTRICTIONS)) { |
| 851 | mHandler.sendEmptyMessageDelayed(WRITE_PACKAGE_RESTRICTIONS, WRITE_SETTINGS_DELAY); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 855 | public static final IPackageManager main(Context context, boolean factoryTest, |
| 856 | boolean onlyCore) { |
| 857 | PackageManagerService m = new PackageManagerService(context, factoryTest, onlyCore); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | ServiceManager.addService("package", m); |
| 859 | return m; |
| 860 | } |
| 861 | |
| 862 | static String[] splitString(String str, char sep) { |
| 863 | int count = 1; |
| 864 | int i = 0; |
| 865 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 866 | count++; |
| 867 | i++; |
| 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 | String[] res = new String[count]; |
| 871 | i=0; |
| 872 | count = 0; |
| 873 | int lastI=0; |
| 874 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 875 | res[count] = str.substring(lastI, i); |
| 876 | count++; |
| 877 | i++; |
| 878 | lastI = i; |
| 879 | } |
| 880 | res[count] = str.substring(lastI, str.length()); |
| 881 | return res; |
| 882 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 883 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 884 | public PackageManagerService(Context context, boolean factoryTest, boolean onlyCore) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 885 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_START, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | SystemClock.uptimeMillis()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 887 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | if (mSdkVersion <= 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 889 | Slog.w(TAG, "**** ro.build.version.sdk not set!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 891 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | mContext = context; |
| 893 | mFactoryTest = factoryTest; |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 894 | mOnlyCore = onlyCore; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 895 | mNoDexOpt = "eng".equals(SystemProperties.get("ro.build.type")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 896 | mMetrics = new DisplayMetrics(); |
| 897 | mSettings = new Settings(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 898 | mSettings.addSharedUserLPw("android.uid.system", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | Process.SYSTEM_UID, ApplicationInfo.FLAG_SYSTEM); |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 900 | mSettings.addSharedUserLPw("android.uid.phone", RADIO_UID, ApplicationInfo.FLAG_SYSTEM); |
| 901 | mSettings.addSharedUserLPw("android.uid.log", LOG_UID, ApplicationInfo.FLAG_SYSTEM); |
| 902 | 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] | 903 | |
| 904 | String separateProcesses = SystemProperties.get("debug.separate_processes"); |
| 905 | if (separateProcesses != null && separateProcesses.length() > 0) { |
| 906 | if ("*".equals(separateProcesses)) { |
| 907 | mDefParseFlags = PackageParser.PARSE_IGNORE_PROCESSES; |
| 908 | mSeparateProcesses = null; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 909 | Slog.w(TAG, "Running with debug.separate_processes: * (ALL)"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | } else { |
| 911 | mDefParseFlags = 0; |
| 912 | mSeparateProcesses = separateProcesses.split(","); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 913 | Slog.w(TAG, "Running with debug.separate_processes: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | + separateProcesses); |
| 915 | } |
| 916 | } else { |
| 917 | mDefParseFlags = 0; |
| 918 | mSeparateProcesses = null; |
| 919 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 920 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 921 | mInstaller = new Installer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | |
| 923 | WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); |
| 924 | Display d = wm.getDefaultDisplay(); |
| 925 | d.getMetrics(mMetrics); |
| 926 | |
| 927 | synchronized (mInstallLock) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 928 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | synchronized (mPackages) { |
| 930 | mHandlerThread.start(); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 931 | mHandler = new PackageHandler(mHandlerThread.getLooper()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 932 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | File dataDir = Environment.getDataDirectory(); |
| 934 | mAppDataDir = new File(dataDir, "data"); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 935 | mAsecInternalPath = new File(dataDir, "app-asec").getPath(); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 936 | mUserAppDataDir = new File(dataDir, "user"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | mDrmAppPrivateInstallDir = new File(dataDir, "app-private"); |
| 938 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 939 | sUserManager = new UserManager(mInstaller, mUserAppDataDir); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 940 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | readPermissions(); |
| 942 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 943 | mRestoredSettings = mSettings.readLPw(getUsers()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | long startTime = SystemClock.uptimeMillis(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 945 | |
| 946 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SYSTEM_SCAN_START, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | startTime); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 948 | |
Suchi Amalapurapu | daec172 | 2010-01-14 21:25:16 -0800 | [diff] [blame] | 949 | // Set flag to monitor and not change apk file paths when |
| 950 | // scanning install directories. |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 951 | int scanMode = SCAN_MONITOR | SCAN_NO_PATHS | SCAN_DEFER_DEX | SCAN_BOOTING; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 952 | if (mNoDexOpt) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 953 | Slog.w(TAG, "Running ENG build: no pre-dexopt!"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 954 | scanMode |= SCAN_NO_DEX; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 955 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 956 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | final HashSet<String> libFiles = new HashSet<String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 958 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | mFrameworkDir = new File(Environment.getRootDirectory(), "framework"); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 960 | mDalvikCacheDir = new File(dataDir, "dalvik-cache"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 961 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 962 | boolean didDexOpt = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 963 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 964 | /** |
| 965 | * Out of paranoia, ensure that everything in the boot class |
| 966 | * path has been dexed. |
| 967 | */ |
| 968 | String bootClassPath = System.getProperty("java.boot.class.path"); |
| 969 | if (bootClassPath != null) { |
| 970 | String[] paths = splitString(bootClassPath, ':'); |
| 971 | for (int i=0; i<paths.length; i++) { |
| 972 | try { |
| 973 | if (dalvik.system.DexFile.isDexOptNeeded(paths[i])) { |
| 974 | libFiles.add(paths[i]); |
| 975 | mInstaller.dexopt(paths[i], Process.SYSTEM_UID, true); |
| 976 | didDexOpt = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 978 | } catch (FileNotFoundException e) { |
| 979 | Slog.w(TAG, "Boot class path not found: " + paths[i]); |
| 980 | } catch (IOException e) { |
Kenny Root | fec5e75 | 2011-09-23 10:09:58 -0700 | [diff] [blame] | 981 | Slog.w(TAG, "Cannot dexopt " + paths[i] + "; is it an APK or JAR? " |
| 982 | + e.getMessage()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | } |
| 984 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 985 | } else { |
| 986 | Slog.w(TAG, "No BOOTCLASSPATH found!"); |
| 987 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 988 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 989 | /** |
| 990 | * Also ensure all external libraries have had dexopt run on them. |
| 991 | */ |
| 992 | if (mSharedLibraries.size() > 0) { |
| 993 | Iterator<String> libs = mSharedLibraries.values().iterator(); |
| 994 | while (libs.hasNext()) { |
| 995 | String lib = libs.next(); |
| 996 | try { |
| 997 | if (dalvik.system.DexFile.isDexOptNeeded(lib)) { |
| 998 | libFiles.add(lib); |
| 999 | mInstaller.dexopt(lib, Process.SYSTEM_UID, true); |
| 1000 | didDexOpt = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1002 | } catch (FileNotFoundException e) { |
| 1003 | Slog.w(TAG, "Library not found: " + lib); |
| 1004 | } catch (IOException e) { |
Kenny Root | fec5e75 | 2011-09-23 10:09:58 -0700 | [diff] [blame] | 1005 | Slog.w(TAG, "Cannot dexopt " + lib + "; is it an APK or JAR? " |
| 1006 | + e.getMessage()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | } |
| 1008 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1009 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1010 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1011 | // Gross hack for now: we know this file doesn't contain any |
| 1012 | // code, so don't dexopt it to avoid the resulting log spew. |
| 1013 | libFiles.add(mFrameworkDir.getPath() + "/framework-res.apk"); |
| 1014 | |
| 1015 | /** |
| 1016 | * And there are a number of commands implemented in Java, which |
| 1017 | * we currently need to do the dexopt on so that they can be |
| 1018 | * run from a non-root shell. |
| 1019 | */ |
| 1020 | String[] frameworkFiles = mFrameworkDir.list(); |
| 1021 | if (frameworkFiles != null) { |
| 1022 | for (int i=0; i<frameworkFiles.length; i++) { |
| 1023 | File libPath = new File(mFrameworkDir, frameworkFiles[i]); |
| 1024 | String path = libPath.getPath(); |
| 1025 | // Skip the file if we alrady did it. |
| 1026 | if (libFiles.contains(path)) { |
| 1027 | continue; |
| 1028 | } |
| 1029 | // Skip the file if it is not a type we want to dexopt. |
| 1030 | if (!path.endsWith(".apk") && !path.endsWith(".jar")) { |
| 1031 | continue; |
| 1032 | } |
| 1033 | try { |
| 1034 | if (dalvik.system.DexFile.isDexOptNeeded(path)) { |
| 1035 | mInstaller.dexopt(path, Process.SYSTEM_UID, true); |
| 1036 | didDexOpt = true; |
| 1037 | } |
| 1038 | } catch (FileNotFoundException e) { |
| 1039 | Slog.w(TAG, "Jar not found: " + path); |
| 1040 | } catch (IOException e) { |
| 1041 | Slog.w(TAG, "Exception reading jar: " + path, e); |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | if (didDexOpt) { |
| 1047 | // If we had to do a dexopt of one of the previous |
| 1048 | // things, then something on the system has changed. |
| 1049 | // Consider this significant, and wipe away all other |
| 1050 | // existing dexopt files to ensure we don't leave any |
| 1051 | // dangling around. |
| 1052 | String[] files = mDalvikCacheDir.list(); |
| 1053 | if (files != null) { |
| 1054 | for (int i=0; i<files.length; i++) { |
| 1055 | String fn = files[i]; |
| 1056 | if (fn.startsWith("data@app@") |
| 1057 | || fn.startsWith("data@app-private@")) { |
| 1058 | Slog.i(TAG, "Pruning dalvik file: " + fn); |
| 1059 | (new File(mDalvikCacheDir, fn)).delete(); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1064 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1065 | // Find base frameworks (resource packages without code). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | mFrameworkInstallObserver = new AppDirObserver( |
| 1067 | mFrameworkDir.getPath(), OBSERVER_EVENTS, true); |
| 1068 | mFrameworkInstallObserver.startWatching(); |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 1069 | scanDirLI(mFrameworkDir, PackageParser.PARSE_IS_SYSTEM |
| 1070 | | PackageParser.PARSE_IS_SYSTEM_DIR, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1071 | scanMode | SCAN_NO_DEX, 0); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 1072 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1073 | // Collect all system packages. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | mSystemAppDir = new File(Environment.getRootDirectory(), "app"); |
| 1075 | mSystemInstallObserver = new AppDirObserver( |
| 1076 | mSystemAppDir.getPath(), OBSERVER_EVENTS, true); |
| 1077 | mSystemInstallObserver.startWatching(); |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 1078 | scanDirLI(mSystemAppDir, PackageParser.PARSE_IS_SYSTEM |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1079 | | PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1080 | |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 1081 | // Collect all vendor packages. |
| 1082 | mVendorAppDir = new File("/vendor/app"); |
| 1083 | mVendorInstallObserver = new AppDirObserver( |
| 1084 | mVendorAppDir.getPath(), OBSERVER_EVENTS, true); |
| 1085 | mVendorInstallObserver.startWatching(); |
| 1086 | scanDirLI(mVendorAppDir, PackageParser.PARSE_IS_SYSTEM |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1087 | | PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0); |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 1088 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1089 | if (DEBUG_UPGRADE) Log.v(TAG, "Running installd update commands"); |
| 1090 | mInstaller.moveFiles(); |
| 1091 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1092 | // Prune any system packages that no longer exist. |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1093 | final List<String> possiblyDeletedUpdatedSystemApps = new ArrayList<String>(); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1094 | if (!mOnlyCore) { |
| 1095 | Iterator<PackageSetting> psit = mSettings.mPackages.values().iterator(); |
| 1096 | while (psit.hasNext()) { |
| 1097 | PackageSetting ps = psit.next(); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1098 | |
| 1099 | /* |
| 1100 | * If this is not a system app, it can't be a |
| 1101 | * disable system app. |
| 1102 | */ |
| 1103 | if ((ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 1104 | continue; |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * If the package is scanned, it's not erased. |
| 1109 | */ |
| 1110 | if (mPackages.containsKey(ps.name)) { |
| 1111 | /* |
| 1112 | * If the system app is both scanned and in the |
| 1113 | * disabled packages list, then it must have been |
| 1114 | * added via OTA. Remove it from the currently |
| 1115 | * scanned package so the previously user-installed |
| 1116 | * application can be scanned. |
| 1117 | */ |
| 1118 | if (mSettings.isDisabledSystemPackageLPr(ps.name)) { |
| 1119 | mPackages.remove(ps.name); |
| 1120 | } |
| 1121 | |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1122 | continue; |
| 1123 | } |
| 1124 | |
| 1125 | if (!mSettings.isDisabledSystemPackageLPr(ps.name)) { |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1126 | psit.remove(); |
| 1127 | String msg = "System package " + ps.name |
| 1128 | + " no longer exists; wiping its data"; |
| 1129 | reportSettingsProblem(Log.WARN, msg); |
| 1130 | mInstaller.remove(ps.name, 0); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1131 | sUserManager.removePackageForAllUsers(ps.name); |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1132 | } else { |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1133 | final PackageSetting disabledPs = mSettings.getDisabledSystemPkgLPr(ps.name); |
| 1134 | if (disabledPs.codePath == null || !disabledPs.codePath.exists()) { |
| 1135 | possiblyDeletedUpdatedSystemApps.add(ps.name); |
| 1136 | } |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1137 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1138 | } |
| 1139 | } |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1140 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | mAppInstallDir = new File(dataDir, "app"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | //look for any incomplete package installations |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1143 | ArrayList<PackageSetting> deletePkgsList = mSettings.getListOfIncompleteInstallPackagesLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1144 | //clean up list |
| 1145 | for(int i = 0; i < deletePkgsList.size(); i++) { |
| 1146 | //clean up here |
| 1147 | cleanupInstallFailedPackage(deletePkgsList.get(i)); |
| 1148 | } |
| 1149 | //delete tmp files |
| 1150 | deleteTempPackageFiles(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1151 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1152 | if (!mOnlyCore) { |
| 1153 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_DATA_SCAN_START, |
| 1154 | SystemClock.uptimeMillis()); |
| 1155 | mAppInstallObserver = new AppDirObserver( |
| 1156 | mAppInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 1157 | mAppInstallObserver.startWatching(); |
| 1158 | scanDirLI(mAppInstallDir, 0, scanMode, 0); |
| 1159 | |
| 1160 | mDrmAppInstallObserver = new AppDirObserver( |
| 1161 | mDrmAppPrivateInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 1162 | mDrmAppInstallObserver.startWatching(); |
| 1163 | scanDirLI(mDrmAppPrivateInstallDir, PackageParser.PARSE_FORWARD_LOCK, |
| 1164 | scanMode, 0); |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1165 | |
| 1166 | /** |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1167 | * Remove disable package settings for any updated system |
| 1168 | * apps that were removed via an OTA. If they're not a |
| 1169 | * previously-updated app, remove them completely. |
| 1170 | * Otherwise, just revoke their system-level permissions. |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1171 | */ |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1172 | for (String deletedAppName : possiblyDeletedUpdatedSystemApps) { |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1173 | PackageParser.Package deletedPkg = mPackages.get(deletedAppName); |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1174 | mSettings.removeDisabledSystemPackageLPw(deletedAppName); |
| 1175 | |
| 1176 | String msg; |
| 1177 | if (deletedPkg == null) { |
| 1178 | msg = "Updated system package " + deletedAppName |
| 1179 | + " no longer exists; wiping its data"; |
| 1180 | |
| 1181 | mInstaller.remove(deletedAppName, 0); |
| 1182 | sUserManager.removePackageForAllUsers(deletedAppName); |
| 1183 | } else { |
| 1184 | msg = "Updated system app + " + deletedAppName |
| 1185 | + " no longer present; removing system privileges for " |
| 1186 | + deletedAppName; |
| 1187 | |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1188 | deletedPkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_SYSTEM; |
| 1189 | |
| 1190 | PackageSetting deletedPs = mSettings.mPackages.get(deletedAppName); |
| 1191 | deletedPs.pkgFlags &= ~ApplicationInfo.FLAG_SYSTEM; |
| 1192 | } |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1193 | reportSettingsProblem(Log.WARN, msg); |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1194 | } |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1195 | } else { |
| 1196 | mAppInstallObserver = null; |
| 1197 | mDrmAppInstallObserver = null; |
| 1198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1199 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1200 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SCAN_END, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | SystemClock.uptimeMillis()); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1202 | Slog.i(TAG, "Time to scan packages: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | + ((SystemClock.uptimeMillis()-startTime)/1000f) |
| 1204 | + " seconds"); |
| 1205 | |
Dianne Hackborn | f22221f | 2010-04-05 18:35:42 -0700 | [diff] [blame] | 1206 | // If the platform SDK has changed since the last time we booted, |
| 1207 | // we need to re-grant app permission to catch any new ones that |
| 1208 | // appear. This is really a hack, and means that apps can in some |
| 1209 | // cases get permissions that the user didn't initially explicitly |
| 1210 | // allow... it would be nice to have some better way to handle |
| 1211 | // this situation. |
| 1212 | final boolean regrantPermissions = mSettings.mInternalSdkPlatform |
| 1213 | != mSdkVersion; |
| 1214 | if (regrantPermissions) Slog.i(TAG, "Platform changed from " |
| 1215 | + mSettings.mInternalSdkPlatform + " to " + mSdkVersion |
| 1216 | + "; regranting permissions for internal storage"); |
| 1217 | mSettings.mInternalSdkPlatform = mSdkVersion; |
| 1218 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 1219 | updatePermissionsLPw(null, null, UPDATE_PERMISSIONS_ALL |
| 1220 | | (regrantPermissions |
| 1221 | ? (UPDATE_PERMISSIONS_REPLACE_PKG|UPDATE_PERMISSIONS_REPLACE_ALL) |
| 1222 | : 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1223 | |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 1224 | // Verify that all of the preferred activity components actually |
| 1225 | // exist. It is possible for applications to be updated and at |
| 1226 | // that point remove a previously declared activity component that |
| 1227 | // had been set as a preferred activity. We try to clean this up |
| 1228 | // the next time we encounter that preferred activity, but it is |
| 1229 | // possible for the user flow to never be able to return to that |
| 1230 | // situation so here we do a sanity check to make sure we haven't |
| 1231 | // left any junk around. |
| 1232 | ArrayList<PreferredActivity> removed = new ArrayList<PreferredActivity>(); |
| 1233 | for (PreferredActivity pa : mSettings.mPreferredActivities.filterSet()) { |
| 1234 | if (mActivities.mActivities.get(pa.mPref.mComponent) == null) { |
| 1235 | removed.add(pa); |
| 1236 | } |
| 1237 | } |
| 1238 | for (int i=0; i<removed.size(); i++) { |
| 1239 | PreferredActivity pa = removed.get(i); |
| 1240 | Slog.w(TAG, "Removing dangling preferred activity: " |
| 1241 | + pa.mPref.mComponent); |
| 1242 | mSettings.mPreferredActivities.removeFilter(pa); |
| 1243 | } |
| 1244 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1245 | // can downgrade to reader |
| 1246 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1248 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1249 | SystemClock.uptimeMillis()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | // Now after opening every single application zip, make sure they |
| 1252 | // are all flushed. Not really needed, but keeps things nice and |
| 1253 | // tidy. |
| 1254 | Runtime.getRuntime().gc(); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1255 | |
| 1256 | mRequiredVerifierPackage = getRequiredVerifierLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | } // synchronized (mPackages) |
| 1258 | } // synchronized (mInstallLock) |
| 1259 | } |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 1260 | |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 1261 | public boolean isFirstBoot() { |
| 1262 | return !mRestoredSettings; |
| 1263 | } |
| 1264 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1265 | private String getRequiredVerifierLPr() { |
| 1266 | final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION); |
| 1267 | final List<ResolveInfo> receivers = queryIntentReceivers(verification, PACKAGE_MIME_TYPE, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1268 | PackageManager.GET_DISABLED_COMPONENTS, 0 /* TODO: Which userId? */); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1269 | |
| 1270 | String requiredVerifier = null; |
| 1271 | |
| 1272 | final int N = receivers.size(); |
| 1273 | for (int i = 0; i < N; i++) { |
| 1274 | final ResolveInfo info = receivers.get(i); |
| 1275 | |
| 1276 | if (info.activityInfo == null) { |
| 1277 | continue; |
| 1278 | } |
| 1279 | |
| 1280 | final String packageName = info.activityInfo.packageName; |
| 1281 | |
| 1282 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1283 | if (ps == null) { |
| 1284 | continue; |
| 1285 | } |
| 1286 | |
| 1287 | if (!ps.grantedPermissions |
| 1288 | .contains(android.Manifest.permission.PACKAGE_VERIFICATION_AGENT)) { |
| 1289 | continue; |
| 1290 | } |
| 1291 | |
| 1292 | if (requiredVerifier != null) { |
| 1293 | throw new RuntimeException("There can be only one required verifier"); |
| 1294 | } |
| 1295 | |
| 1296 | requiredVerifier = packageName; |
| 1297 | } |
| 1298 | |
| 1299 | return requiredVerifier; |
| 1300 | } |
| 1301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1302 | @Override |
| 1303 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1304 | throws RemoteException { |
| 1305 | try { |
| 1306 | return super.onTransact(code, data, reply, flags); |
| 1307 | } catch (RuntimeException e) { |
| 1308 | if (!(e instanceof SecurityException) && !(e instanceof IllegalArgumentException)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1309 | Slog.e(TAG, "Package Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | } |
| 1311 | throw e; |
| 1312 | } |
| 1313 | } |
| 1314 | |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1315 | void cleanupInstallFailedPackage(PackageSetting ps) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1316 | Slog.i(TAG, "Cleaning up incompletely installed app: " + ps.name); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1317 | int retCode = mInstaller.remove(ps.name, 0); |
| 1318 | if (retCode < 0) { |
| 1319 | Slog.w(TAG, "Couldn't remove app data directory for package: " |
| 1320 | + ps.name + ", retcode=" + retCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | } else { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1322 | sUserManager.removePackageForAllUsers(ps.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | } |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1324 | if (ps.codePath != null) { |
| 1325 | if (!ps.codePath.delete()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1326 | Slog.w(TAG, "Unable to remove old code file: " + ps.codePath); |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | if (ps.resourcePath != null) { |
| 1330 | if (!ps.resourcePath.delete() && !ps.resourcePath.equals(ps.codePath)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1331 | Slog.w(TAG, "Unable to remove old code file: " + ps.resourcePath); |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1332 | } |
| 1333 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1334 | mSettings.removePackageLPw(ps.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | void readPermissions() { |
| 1338 | // Read permissions from .../etc/permission directory. |
| 1339 | File libraryDir = new File(Environment.getRootDirectory(), "etc/permissions"); |
| 1340 | if (!libraryDir.exists() || !libraryDir.isDirectory()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1341 | Slog.w(TAG, "No directory " + libraryDir + ", skipping"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | return; |
| 1343 | } |
| 1344 | if (!libraryDir.canRead()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1345 | Slog.w(TAG, "Directory " + libraryDir + " cannot be read"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | return; |
| 1347 | } |
| 1348 | |
| 1349 | // Iterate over the files in the directory and scan .xml files |
| 1350 | for (File f : libraryDir.listFiles()) { |
| 1351 | // We'll read platform.xml last |
| 1352 | if (f.getPath().endsWith("etc/permissions/platform.xml")) { |
| 1353 | continue; |
| 1354 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1355 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | if (!f.getPath().endsWith(".xml")) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1357 | 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] | 1358 | continue; |
| 1359 | } |
| 1360 | if (!f.canRead()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1361 | 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] | 1362 | continue; |
| 1363 | } |
| 1364 | |
| 1365 | readPermissionsFromXml(f); |
| 1366 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1368 | // Read permissions from .../etc/permissions/platform.xml last so it will take precedence |
| 1369 | final File permFile = new File(Environment.getRootDirectory(), |
| 1370 | "etc/permissions/platform.xml"); |
| 1371 | readPermissionsFromXml(permFile); |
| 1372 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1373 | |
| 1374 | private void readPermissionsFromXml(File permFile) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | FileReader permReader = null; |
| 1376 | try { |
| 1377 | permReader = new FileReader(permFile); |
| 1378 | } catch (FileNotFoundException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1379 | 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] | 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | try { |
| 1384 | XmlPullParser parser = Xml.newPullParser(); |
| 1385 | parser.setInput(permReader); |
| 1386 | |
| 1387 | XmlUtils.beginDocument(parser, "permissions"); |
| 1388 | |
| 1389 | while (true) { |
| 1390 | XmlUtils.nextElement(parser); |
| 1391 | if (parser.getEventType() == XmlPullParser.END_DOCUMENT) { |
| 1392 | break; |
| 1393 | } |
| 1394 | |
| 1395 | String name = parser.getName(); |
| 1396 | if ("group".equals(name)) { |
| 1397 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1398 | if (gidStr != null) { |
| 1399 | int gid = Integer.parseInt(gidStr); |
| 1400 | mGlobalGids = appendInt(mGlobalGids, gid); |
| 1401 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1402 | Slog.w(TAG, "<group> without gid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | + parser.getPositionDescription()); |
| 1404 | } |
| 1405 | |
| 1406 | XmlUtils.skipCurrentTag(parser); |
| 1407 | continue; |
| 1408 | } else if ("permission".equals(name)) { |
| 1409 | String perm = parser.getAttributeValue(null, "name"); |
| 1410 | if (perm == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1411 | Slog.w(TAG, "<permission> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | + parser.getPositionDescription()); |
| 1413 | XmlUtils.skipCurrentTag(parser); |
| 1414 | continue; |
| 1415 | } |
| 1416 | perm = perm.intern(); |
| 1417 | readPermission(parser, perm); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1418 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1419 | } else if ("assign-permission".equals(name)) { |
| 1420 | String perm = parser.getAttributeValue(null, "name"); |
| 1421 | if (perm == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1422 | Slog.w(TAG, "<assign-permission> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | + parser.getPositionDescription()); |
| 1424 | XmlUtils.skipCurrentTag(parser); |
| 1425 | continue; |
| 1426 | } |
| 1427 | String uidStr = parser.getAttributeValue(null, "uid"); |
| 1428 | if (uidStr == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1429 | Slog.w(TAG, "<assign-permission> without uid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | + parser.getPositionDescription()); |
| 1431 | XmlUtils.skipCurrentTag(parser); |
| 1432 | continue; |
| 1433 | } |
| 1434 | int uid = Process.getUidForName(uidStr); |
| 1435 | if (uid < 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1436 | Slog.w(TAG, "<assign-permission> with unknown uid \"" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1437 | + uidStr + "\" at " |
| 1438 | + parser.getPositionDescription()); |
| 1439 | XmlUtils.skipCurrentTag(parser); |
| 1440 | continue; |
| 1441 | } |
| 1442 | perm = perm.intern(); |
| 1443 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 1444 | if (perms == null) { |
| 1445 | perms = new HashSet<String>(); |
| 1446 | mSystemPermissions.put(uid, perms); |
| 1447 | } |
| 1448 | perms.add(perm); |
| 1449 | XmlUtils.skipCurrentTag(parser); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | } else if ("library".equals(name)) { |
| 1452 | String lname = parser.getAttributeValue(null, "name"); |
| 1453 | String lfile = parser.getAttributeValue(null, "file"); |
| 1454 | if (lname == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1455 | Slog.w(TAG, "<library> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | + parser.getPositionDescription()); |
| 1457 | } else if (lfile == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1458 | Slog.w(TAG, "<library> without file at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1459 | + parser.getPositionDescription()); |
| 1460 | } else { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1461 | //Log.i(TAG, "Got library " + lname + " in " + lfile); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1462 | mSharedLibraries.put(lname, lfile); |
| 1463 | } |
| 1464 | XmlUtils.skipCurrentTag(parser); |
| 1465 | continue; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1466 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1467 | } else if ("feature".equals(name)) { |
| 1468 | String fname = parser.getAttributeValue(null, "name"); |
| 1469 | if (fname == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1470 | Slog.w(TAG, "<feature> without name at " |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1471 | + parser.getPositionDescription()); |
| 1472 | } else { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1473 | //Log.i(TAG, "Got feature " + fname); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1474 | FeatureInfo fi = new FeatureInfo(); |
| 1475 | fi.name = fname; |
| 1476 | mAvailableFeatures.put(fname, fi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | } |
| 1478 | XmlUtils.skipCurrentTag(parser); |
| 1479 | continue; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1480 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | } else { |
| 1482 | XmlUtils.skipCurrentTag(parser); |
| 1483 | continue; |
| 1484 | } |
| 1485 | |
| 1486 | } |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 1487 | permReader.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | } catch (XmlPullParserException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1489 | Slog.w(TAG, "Got execption parsing permissions.", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1491 | Slog.w(TAG, "Got execption parsing permissions.", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | void readPermission(XmlPullParser parser, String name) |
| 1496 | throws IOException, XmlPullParserException { |
| 1497 | |
| 1498 | name = name.intern(); |
| 1499 | |
| 1500 | BasePermission bp = mSettings.mPermissions.get(name); |
| 1501 | if (bp == null) { |
| 1502 | bp = new BasePermission(name, null, BasePermission.TYPE_BUILTIN); |
| 1503 | mSettings.mPermissions.put(name, bp); |
| 1504 | } |
| 1505 | int outerDepth = parser.getDepth(); |
| 1506 | int type; |
| 1507 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 1508 | && (type != XmlPullParser.END_TAG |
| 1509 | || parser.getDepth() > outerDepth)) { |
| 1510 | if (type == XmlPullParser.END_TAG |
| 1511 | || type == XmlPullParser.TEXT) { |
| 1512 | continue; |
| 1513 | } |
| 1514 | |
| 1515 | String tagName = parser.getName(); |
| 1516 | if ("group".equals(tagName)) { |
| 1517 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1518 | if (gidStr != null) { |
| 1519 | int gid = Process.getGidForName(gidStr); |
| 1520 | bp.gids = appendInt(bp.gids, gid); |
| 1521 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1522 | Slog.w(TAG, "<group> without gid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1523 | + parser.getPositionDescription()); |
| 1524 | } |
| 1525 | } |
| 1526 | XmlUtils.skipCurrentTag(parser); |
| 1527 | } |
| 1528 | } |
| 1529 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | static int[] appendInts(int[] cur, int[] add) { |
| 1531 | if (add == null) return cur; |
| 1532 | if (cur == null) return add; |
| 1533 | final int N = add.length; |
| 1534 | for (int i=0; i<N; i++) { |
| 1535 | cur = appendInt(cur, add[i]); |
| 1536 | } |
| 1537 | return cur; |
| 1538 | } |
| 1539 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1540 | static int[] removeInts(int[] cur, int[] rem) { |
| 1541 | if (rem == null) return cur; |
| 1542 | if (cur == null) return cur; |
| 1543 | final int N = rem.length; |
| 1544 | for (int i=0; i<N; i++) { |
| 1545 | cur = removeInt(cur, rem[i]); |
| 1546 | } |
| 1547 | return cur; |
| 1548 | } |
| 1549 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1550 | PackageInfo generatePackageInfo(PackageParser.Package p, int flags, int userId) { |
| 1551 | if (!sUserManager.exists(userId)) return null; |
| 1552 | PackageInfo pi; |
Suchi Amalapurapu | b897cff | 2009-10-14 12:11:48 -0700 | [diff] [blame] | 1553 | if ((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
| 1554 | // The package has been uninstalled but has retained data and resources. |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1555 | pi = PackageParser.generatePackageInfo(p, null, flags, 0, 0, null, false, 0, userId); |
| 1556 | } else { |
| 1557 | final PackageSetting ps = (PackageSetting) p.mExtras; |
| 1558 | if (ps == null) { |
| 1559 | return null; |
| 1560 | } |
| 1561 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 1562 | pi = PackageParser.generatePackageInfo(p, gp.gids, flags, |
| 1563 | ps.firstInstallTime, ps.lastUpdateTime, gp.grantedPermissions, |
| 1564 | ps.getStopped(userId), ps.getEnabled(userId), userId); |
| 1565 | pi.applicationInfo.enabledSetting = ps.getEnabled(userId); |
| 1566 | pi.applicationInfo.enabled = |
| 1567 | pi.applicationInfo.enabledSetting == COMPONENT_ENABLED_STATE_DEFAULT |
| 1568 | || pi.applicationInfo.enabledSetting == COMPONENT_ENABLED_STATE_ENABLED; |
Suchi Amalapurapu | b897cff | 2009-10-14 12:11:48 -0700 | [diff] [blame] | 1569 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1570 | return pi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1573 | @Override |
| 1574 | public PackageInfo getPackageInfo(String packageName, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1575 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1576 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1577 | synchronized (mPackages) { |
| 1578 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1579 | if (DEBUG_PACKAGE_INFO) |
| 1580 | Log.v(TAG, "getPackageInfo " + packageName + ": " + p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1582 | return generatePackageInfo(p, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1583 | } |
| 1584 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1585 | return generatePackageInfoFromSettingsLPw(packageName, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | return null; |
| 1589 | } |
| 1590 | |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1591 | public String[] currentToCanonicalPackageNames(String[] names) { |
| 1592 | String[] out = new String[names.length]; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1593 | // reader |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1594 | synchronized (mPackages) { |
| 1595 | for (int i=names.length-1; i>=0; i--) { |
| 1596 | PackageSetting ps = mSettings.mPackages.get(names[i]); |
| 1597 | out[i] = ps != null && ps.realName != null ? ps.realName : names[i]; |
| 1598 | } |
| 1599 | } |
| 1600 | return out; |
| 1601 | } |
| 1602 | |
| 1603 | public String[] canonicalToCurrentPackageNames(String[] names) { |
| 1604 | String[] out = new String[names.length]; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1605 | // reader |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1606 | synchronized (mPackages) { |
| 1607 | for (int i=names.length-1; i>=0; i--) { |
| 1608 | String cur = mSettings.mRenamedPackages.get(names[i]); |
| 1609 | out[i] = cur != null ? cur : names[i]; |
| 1610 | } |
| 1611 | } |
| 1612 | return out; |
| 1613 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1614 | |
| 1615 | @Override |
| 1616 | public int getPackageUid(String packageName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1617 | if (!sUserManager.exists(userId)) return -1; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1618 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1619 | synchronized (mPackages) { |
| 1620 | PackageParser.Package p = mPackages.get(packageName); |
| 1621 | if(p != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1622 | return UserId.getUid(userId, p.applicationInfo.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1623 | } |
| 1624 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1625 | if((ps == null) || (ps.pkg == null) || (ps.pkg.applicationInfo == null)) { |
| 1626 | return -1; |
| 1627 | } |
| 1628 | p = ps.pkg; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1629 | 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] | 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | public int[] getPackageGids(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1634 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1635 | synchronized (mPackages) { |
| 1636 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1637 | if (DEBUG_PACKAGE_INFO) |
| 1638 | Log.v(TAG, "getPackageGids" + packageName + ": " + p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | if (p != null) { |
| 1640 | final PackageSetting ps = (PackageSetting)p.mExtras; |
| 1641 | final SharedUserSetting suid = ps.sharedUser; |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 1642 | int[] gids = suid != null ? suid.gids : ps.gids; |
| 1643 | |
| 1644 | // include GIDs for any unenforced permissions |
| 1645 | if (!isPermissionEnforcedLocked(READ_EXTERNAL_STORAGE)) { |
| 1646 | final BasePermission basePerm = mSettings.mPermissions.get( |
| 1647 | READ_EXTERNAL_STORAGE); |
| 1648 | gids = appendInts(gids, basePerm.gids); |
| 1649 | } |
| 1650 | |
| 1651 | return gids; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | // stupid thing to indicate an error. |
| 1655 | return new int[0]; |
| 1656 | } |
| 1657 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1658 | static final PermissionInfo generatePermissionInfo( |
| 1659 | BasePermission bp, int flags) { |
| 1660 | if (bp.perm != null) { |
| 1661 | return PackageParser.generatePermissionInfo(bp.perm, flags); |
| 1662 | } |
| 1663 | PermissionInfo pi = new PermissionInfo(); |
| 1664 | pi.name = bp.name; |
| 1665 | pi.packageName = bp.sourcePackage; |
| 1666 | pi.nonLocalizedLabel = bp.name; |
| 1667 | pi.protectionLevel = bp.protectionLevel; |
| 1668 | return pi; |
| 1669 | } |
| 1670 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1671 | public PermissionInfo getPermissionInfo(String name, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1672 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1673 | synchronized (mPackages) { |
| 1674 | final BasePermission p = mSettings.mPermissions.get(name); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1675 | if (p != null) { |
| 1676 | return generatePermissionInfo(p, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1677 | } |
| 1678 | return null; |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | public List<PermissionInfo> queryPermissionsByGroup(String group, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1683 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | synchronized (mPackages) { |
| 1685 | ArrayList<PermissionInfo> out = new ArrayList<PermissionInfo>(10); |
| 1686 | for (BasePermission p : mSettings.mPermissions.values()) { |
| 1687 | if (group == null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1688 | if (p.perm == null || p.perm.info.group == null) { |
| 1689 | out.add(generatePermissionInfo(p, flags)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | } |
| 1691 | } else { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1692 | 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] | 1693 | out.add(PackageParser.generatePermissionInfo(p.perm, flags)); |
| 1694 | } |
| 1695 | } |
| 1696 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1697 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | if (out.size() > 0) { |
| 1699 | return out; |
| 1700 | } |
| 1701 | return mPermissionGroups.containsKey(group) ? out : null; |
| 1702 | } |
| 1703 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1704 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1705 | public PermissionGroupInfo getPermissionGroupInfo(String name, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1706 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | synchronized (mPackages) { |
| 1708 | return PackageParser.generatePermissionGroupInfo( |
| 1709 | mPermissionGroups.get(name), flags); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | public List<PermissionGroupInfo> getAllPermissionGroups(int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1714 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1715 | synchronized (mPackages) { |
| 1716 | final int N = mPermissionGroups.size(); |
| 1717 | ArrayList<PermissionGroupInfo> out |
| 1718 | = new ArrayList<PermissionGroupInfo>(N); |
| 1719 | for (PackageParser.PermissionGroup pg : mPermissionGroups.values()) { |
| 1720 | out.add(PackageParser.generatePermissionGroupInfo(pg, flags)); |
| 1721 | } |
| 1722 | return out; |
| 1723 | } |
| 1724 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1725 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1726 | private ApplicationInfo generateApplicationInfoFromSettingsLPw(String packageName, int flags, |
| 1727 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1728 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1729 | PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1730 | if (ps != null) { |
| 1731 | if (ps.pkg == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1732 | PackageInfo pInfo = generatePackageInfoFromSettingsLPw(packageName, flags, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1733 | if (pInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | return pInfo.applicationInfo; |
| 1735 | } |
| 1736 | return null; |
| 1737 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1738 | return PackageParser.generateApplicationInfo(ps.pkg, flags, ps.getStopped(userId), |
| 1739 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | } |
| 1741 | return null; |
| 1742 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1743 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1744 | private PackageInfo generatePackageInfoFromSettingsLPw(String packageName, int flags, |
| 1745 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1746 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1747 | PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1748 | if (ps != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1749 | PackageParser.Package pkg = new PackageParser.Package(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1750 | if (ps.pkg == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1751 | ps.pkg = new PackageParser.Package(packageName); |
| 1752 | ps.pkg.applicationInfo.packageName = packageName; |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 1753 | ps.pkg.applicationInfo.flags = ps.pkgFlags; |
| 1754 | ps.pkg.applicationInfo.publicSourceDir = ps.resourcePathString; |
| 1755 | ps.pkg.applicationInfo.sourceDir = ps.codePathString; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1756 | ps.pkg.applicationInfo.dataDir = |
| 1757 | getDataPathForPackage(ps.pkg.packageName, 0).getPath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 1758 | ps.pkg.applicationInfo.nativeLibraryDir = ps.nativeLibraryPathString; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1759 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1760 | // ps.pkg.mSetEnabled = ps.getEnabled(userId); |
| 1761 | // ps.pkg.mSetStopped = ps.getStopped(userId); |
| 1762 | return generatePackageInfo(ps.pkg, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1763 | } |
| 1764 | return null; |
| 1765 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1766 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1767 | @Override |
| 1768 | public ApplicationInfo getApplicationInfo(String packageName, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1769 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1770 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | synchronized (mPackages) { |
| 1772 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1773 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1774 | TAG, "getApplicationInfo " + packageName |
| 1775 | + ": " + p); |
| 1776 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1777 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1778 | if (ps == null) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1779 | // Note: isEnabledLP() does not apply here - always return info |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1780 | return PackageParser.generateApplicationInfo(p, flags, ps.getStopped(userId), |
| 1781 | ps.getEnabled(userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1782 | } |
| 1783 | if ("android".equals(packageName)||"system".equals(packageName)) { |
| 1784 | return mAndroidApplication; |
| 1785 | } |
| 1786 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1787 | return generateApplicationInfoFromSettingsLPw(packageName, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | return null; |
| 1791 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1792 | |
| 1793 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1794 | public void freeStorageAndNotify(final long freeStorageSize, final IPackageDataObserver observer) { |
| 1795 | mContext.enforceCallingOrSelfPermission( |
| 1796 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1797 | // Queue up an async operation since clearing cache may take a little while. |
| 1798 | mHandler.post(new Runnable() { |
| 1799 | public void run() { |
| 1800 | mHandler.removeCallbacks(this); |
| 1801 | int retCode = -1; |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1802 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1803 | if (retCode < 0) { |
| 1804 | Slog.w(TAG, "Couldn't clear application caches"); |
| 1805 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1806 | if (observer != null) { |
| 1807 | try { |
| 1808 | observer.onRemoveCompleted(null, (retCode >= 0)); |
| 1809 | } catch (RemoteException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1810 | Slog.w(TAG, "RemoveException when invoking call back"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | }); |
| 1815 | } |
| 1816 | |
Suchi Amalapurapu | bc806f6 | 2009-06-17 15:18:19 -0700 | [diff] [blame] | 1817 | public void freeStorage(final long freeStorageSize, final IntentSender pi) { |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1818 | mContext.enforceCallingOrSelfPermission( |
| 1819 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1820 | // Queue up an async operation since clearing cache may take a little while. |
| 1821 | mHandler.post(new Runnable() { |
| 1822 | public void run() { |
| 1823 | mHandler.removeCallbacks(this); |
| 1824 | int retCode = -1; |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1825 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1826 | if (retCode < 0) { |
| 1827 | Slog.w(TAG, "Couldn't clear application caches"); |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1828 | } |
| 1829 | if(pi != null) { |
| 1830 | try { |
| 1831 | // Callback via pending intent |
| 1832 | int code = (retCode >= 0) ? 1 : 0; |
| 1833 | pi.sendIntent(null, code, null, |
| 1834 | null, null); |
| 1835 | } catch (SendIntentException e1) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1836 | Slog.i(TAG, "Failed to send pending intent"); |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | }); |
| 1841 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1842 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1843 | @Override |
| 1844 | public ActivityInfo getActivityInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1845 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1846 | synchronized (mPackages) { |
| 1847 | PackageParser.Activity a = mActivities.mActivities.get(component); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 1848 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1849 | if (DEBUG_PACKAGE_INFO) Log.v(TAG, "getActivityInfo " + component + ": " + a); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1850 | if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1851 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1852 | if (ps == null) return null; |
| 1853 | return PackageParser.generateActivityInfo(a, flags, ps.getStopped(userId), |
| 1854 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1855 | } |
| 1856 | if (mResolveComponentName.equals(component)) { |
| 1857 | return mResolveActivity; |
| 1858 | } |
| 1859 | } |
| 1860 | return null; |
| 1861 | } |
| 1862 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1863 | @Override |
| 1864 | public ActivityInfo getReceiverInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1865 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1866 | synchronized (mPackages) { |
| 1867 | PackageParser.Activity a = mReceivers.mActivities.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1868 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1869 | TAG, "getReceiverInfo " + component + ": " + a); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1870 | if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1871 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1872 | if (ps == null) return null; |
| 1873 | return PackageParser.generateActivityInfo(a, flags, ps.getStopped(userId), |
| 1874 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | return null; |
| 1878 | } |
| 1879 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1880 | @Override |
| 1881 | public ServiceInfo getServiceInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1882 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1883 | synchronized (mPackages) { |
| 1884 | PackageParser.Service s = mServices.mServices.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1885 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1886 | TAG, "getServiceInfo " + component + ": " + s); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1887 | if (s != null && mSettings.isEnabledLPr(s.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1888 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1889 | if (ps == null) return null; |
| 1890 | return PackageParser.generateServiceInfo(s, flags, ps.getStopped(userId), |
| 1891 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1892 | } |
| 1893 | } |
| 1894 | return null; |
| 1895 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1896 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1897 | @Override |
| 1898 | public ProviderInfo getProviderInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1899 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 1900 | synchronized (mPackages) { |
| 1901 | PackageParser.Provider p = mProvidersByComponent.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1902 | if (DEBUG_PACKAGE_INFO) Log.v( |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 1903 | TAG, "getProviderInfo " + component + ": " + p); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1904 | if (p != null && mSettings.isEnabledLPr(p.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1905 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1906 | if (ps == null) return null; |
| 1907 | return PackageParser.generateProviderInfo(p, flags, ps.getStopped(userId), |
| 1908 | ps.getEnabled(userId), userId); |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 1909 | } |
| 1910 | } |
| 1911 | return null; |
| 1912 | } |
| 1913 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1914 | public String[] getSystemSharedLibraryNames() { |
| 1915 | Set<String> libSet; |
| 1916 | synchronized (mPackages) { |
| 1917 | libSet = mSharedLibraries.keySet(); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1918 | int size = libSet.size(); |
| 1919 | if (size > 0) { |
| 1920 | String[] libs = new String[size]; |
| 1921 | libSet.toArray(libs); |
| 1922 | return libs; |
| 1923 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1924 | } |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1925 | return null; |
| 1926 | } |
| 1927 | |
| 1928 | public FeatureInfo[] getSystemAvailableFeatures() { |
| 1929 | Collection<FeatureInfo> featSet; |
| 1930 | synchronized (mPackages) { |
| 1931 | featSet = mAvailableFeatures.values(); |
| 1932 | int size = featSet.size(); |
| 1933 | if (size > 0) { |
| 1934 | FeatureInfo[] features = new FeatureInfo[size+1]; |
| 1935 | featSet.toArray(features); |
| 1936 | FeatureInfo fi = new FeatureInfo(); |
| 1937 | fi.reqGlEsVersion = SystemProperties.getInt("ro.opengles.version", |
| 1938 | FeatureInfo.GL_ES_VERSION_UNDEFINED); |
| 1939 | features[size] = fi; |
| 1940 | return features; |
| 1941 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1942 | } |
| 1943 | return null; |
| 1944 | } |
| 1945 | |
Dianne Hackborn | 039c68e | 2009-09-26 16:39:23 -0700 | [diff] [blame] | 1946 | public boolean hasSystemFeature(String name) { |
| 1947 | synchronized (mPackages) { |
| 1948 | return mAvailableFeatures.containsKey(name); |
| 1949 | } |
| 1950 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1951 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1952 | private void checkValidCaller(int uid, int userId) { |
| 1953 | if (UserId.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) |
| 1954 | return; |
| 1955 | |
| 1956 | throw new SecurityException("Caller uid=" + uid |
| 1957 | + " is not privileged to communicate with user=" + userId); |
| 1958 | } |
| 1959 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1960 | public int checkPermission(String permName, String pkgName) { |
| 1961 | synchronized (mPackages) { |
| 1962 | PackageParser.Package p = mPackages.get(pkgName); |
| 1963 | if (p != null && p.mExtras != null) { |
| 1964 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 1965 | if (ps.sharedUser != null) { |
| 1966 | if (ps.sharedUser.grantedPermissions.contains(permName)) { |
| 1967 | return PackageManager.PERMISSION_GRANTED; |
| 1968 | } |
| 1969 | } else if (ps.grantedPermissions.contains(permName)) { |
| 1970 | return PackageManager.PERMISSION_GRANTED; |
| 1971 | } |
| 1972 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 1973 | if (!isPermissionEnforcedLocked(permName)) { |
| 1974 | return PackageManager.PERMISSION_GRANTED; |
| 1975 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | } |
| 1977 | return PackageManager.PERMISSION_DENIED; |
| 1978 | } |
| 1979 | |
| 1980 | public int checkUidPermission(String permName, int uid) { |
| 1981 | synchronized (mPackages) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1982 | Object obj = mSettings.getUserIdLPr(UserId.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1983 | if (obj != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1984 | GrantedPermissions gp = (GrantedPermissions)obj; |
| 1985 | if (gp.grantedPermissions.contains(permName)) { |
| 1986 | return PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1987 | } |
| 1988 | } else { |
| 1989 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 1990 | if (perms != null && perms.contains(permName)) { |
| 1991 | return PackageManager.PERMISSION_GRANTED; |
| 1992 | } |
| 1993 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 1994 | if (!isPermissionEnforcedLocked(permName)) { |
| 1995 | return PackageManager.PERMISSION_GRANTED; |
| 1996 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1997 | } |
| 1998 | return PackageManager.PERMISSION_DENIED; |
| 1999 | } |
| 2000 | |
| 2001 | private BasePermission findPermissionTreeLP(String permName) { |
| 2002 | for(BasePermission bp : mSettings.mPermissionTrees.values()) { |
| 2003 | if (permName.startsWith(bp.name) && |
| 2004 | permName.length() > bp.name.length() && |
| 2005 | permName.charAt(bp.name.length()) == '.') { |
| 2006 | return bp; |
| 2007 | } |
| 2008 | } |
| 2009 | return null; |
| 2010 | } |
| 2011 | |
| 2012 | private BasePermission checkPermissionTreeLP(String permName) { |
| 2013 | if (permName != null) { |
| 2014 | BasePermission bp = findPermissionTreeLP(permName); |
| 2015 | if (bp != null) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2016 | if (bp.uid == UserId.getAppId(Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2017 | return bp; |
| 2018 | } |
| 2019 | throw new SecurityException("Calling uid " |
| 2020 | + Binder.getCallingUid() |
| 2021 | + " is not allowed to add to permission tree " |
| 2022 | + bp.name + " owned by uid " + bp.uid); |
| 2023 | } |
| 2024 | } |
| 2025 | throw new SecurityException("No permission tree found for " + permName); |
| 2026 | } |
| 2027 | |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2028 | static boolean compareStrings(CharSequence s1, CharSequence s2) { |
| 2029 | if (s1 == null) { |
| 2030 | return s2 == null; |
| 2031 | } |
| 2032 | if (s2 == null) { |
| 2033 | return false; |
| 2034 | } |
| 2035 | if (s1.getClass() != s2.getClass()) { |
| 2036 | return false; |
| 2037 | } |
| 2038 | return s1.equals(s2); |
| 2039 | } |
| 2040 | |
| 2041 | static boolean comparePermissionInfos(PermissionInfo pi1, PermissionInfo pi2) { |
| 2042 | if (pi1.icon != pi2.icon) return false; |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 2043 | if (pi1.logo != pi2.logo) return false; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2044 | if (pi1.protectionLevel != pi2.protectionLevel) return false; |
| 2045 | if (!compareStrings(pi1.name, pi2.name)) return false; |
| 2046 | if (!compareStrings(pi1.nonLocalizedLabel, pi2.nonLocalizedLabel)) return false; |
| 2047 | // We'll take care of setting this one. |
| 2048 | if (!compareStrings(pi1.packageName, pi2.packageName)) return false; |
| 2049 | // These are not currently stored in settings. |
| 2050 | //if (!compareStrings(pi1.group, pi2.group)) return false; |
| 2051 | //if (!compareStrings(pi1.nonLocalizedDescription, pi2.nonLocalizedDescription)) return false; |
| 2052 | //if (pi1.labelRes != pi2.labelRes) return false; |
| 2053 | //if (pi1.descriptionRes != pi2.descriptionRes) return false; |
| 2054 | return true; |
| 2055 | } |
| 2056 | |
| 2057 | boolean addPermissionLocked(PermissionInfo info, boolean async) { |
| 2058 | if (info.labelRes == 0 && info.nonLocalizedLabel == null) { |
| 2059 | throw new SecurityException("Label must be specified in permission"); |
| 2060 | } |
| 2061 | BasePermission tree = checkPermissionTreeLP(info.name); |
| 2062 | BasePermission bp = mSettings.mPermissions.get(info.name); |
| 2063 | boolean added = bp == null; |
| 2064 | boolean changed = true; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2065 | int fixedLevel = PermissionInfo.fixProtectionLevel(info.protectionLevel); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2066 | if (added) { |
| 2067 | bp = new BasePermission(info.name, tree.sourcePackage, |
| 2068 | BasePermission.TYPE_DYNAMIC); |
| 2069 | } else if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 2070 | throw new SecurityException( |
| 2071 | "Not allowed to modify non-dynamic permission " |
| 2072 | + info.name); |
| 2073 | } else { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2074 | if (bp.protectionLevel == fixedLevel |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2075 | && bp.perm.owner.equals(tree.perm.owner) |
| 2076 | && bp.uid == tree.uid |
| 2077 | && comparePermissionInfos(bp.perm.info, info)) { |
| 2078 | changed = false; |
| 2079 | } |
| 2080 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2081 | bp.protectionLevel = fixedLevel; |
| 2082 | info = new PermissionInfo(info); |
| 2083 | info.protectionLevel = fixedLevel; |
| 2084 | bp.perm = new PackageParser.Permission(tree.perm.owner, info); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2085 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 2086 | bp.uid = tree.uid; |
| 2087 | if (added) { |
| 2088 | mSettings.mPermissions.put(info.name, bp); |
| 2089 | } |
| 2090 | if (changed) { |
| 2091 | if (!async) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2092 | mSettings.writeLPr(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2093 | } else { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2094 | scheduleWriteSettingsLocked(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2095 | } |
| 2096 | } |
| 2097 | return added; |
| 2098 | } |
| 2099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2100 | public boolean addPermission(PermissionInfo info) { |
| 2101 | synchronized (mPackages) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2102 | return addPermissionLocked(info, false); |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | public boolean addPermissionAsync(PermissionInfo info) { |
| 2107 | synchronized (mPackages) { |
| 2108 | return addPermissionLocked(info, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | public void removePermission(String name) { |
| 2113 | synchronized (mPackages) { |
| 2114 | checkPermissionTreeLP(name); |
| 2115 | BasePermission bp = mSettings.mPermissions.get(name); |
| 2116 | if (bp != null) { |
| 2117 | if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 2118 | throw new SecurityException( |
| 2119 | "Not allowed to modify non-dynamic permission " |
| 2120 | + name); |
| 2121 | } |
| 2122 | mSettings.mPermissions.remove(name); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2123 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2128 | public void grantPermission(String packageName, String permissionName) { |
| 2129 | mContext.enforceCallingOrSelfPermission( |
| 2130 | android.Manifest.permission.GRANT_REVOKE_PERMISSIONS, null); |
| 2131 | synchronized (mPackages) { |
| 2132 | final PackageParser.Package pkg = mPackages.get(packageName); |
| 2133 | if (pkg == null) { |
| 2134 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2135 | } |
| 2136 | final BasePermission bp = mSettings.mPermissions.get(permissionName); |
| 2137 | if (bp == null) { |
| 2138 | throw new IllegalArgumentException("Unknown permission: " + packageName); |
| 2139 | } |
| 2140 | if (!pkg.requestedPermissions.contains(permissionName)) { |
| 2141 | throw new SecurityException("Package " + packageName |
| 2142 | + " has not requested permission " + permissionName); |
| 2143 | } |
| 2144 | if ((bp.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) == 0) { |
| 2145 | throw new SecurityException("Permission " + permissionName |
| 2146 | + " is not a development permission"); |
| 2147 | } |
| 2148 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2149 | if (ps == null) { |
| 2150 | return; |
| 2151 | } |
| 2152 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 2153 | if (gp.grantedPermissions.add(permissionName)) { |
| 2154 | if (ps.haveGids) { |
| 2155 | gp.gids = appendInts(gp.gids, bp.gids); |
| 2156 | } |
| 2157 | mSettings.writeLPr(); |
| 2158 | } |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | public void revokePermission(String packageName, String permissionName) { |
| 2163 | synchronized (mPackages) { |
| 2164 | final PackageParser.Package pkg = mPackages.get(packageName); |
| 2165 | if (pkg == null) { |
| 2166 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2167 | } |
| 2168 | if (pkg.applicationInfo.uid != Binder.getCallingUid()) { |
| 2169 | mContext.enforceCallingOrSelfPermission( |
| 2170 | android.Manifest.permission.GRANT_REVOKE_PERMISSIONS, null); |
| 2171 | } |
| 2172 | final BasePermission bp = mSettings.mPermissions.get(permissionName); |
| 2173 | if (bp == null) { |
| 2174 | throw new IllegalArgumentException("Unknown permission: " + packageName); |
| 2175 | } |
| 2176 | if (!pkg.requestedPermissions.contains(permissionName)) { |
| 2177 | throw new SecurityException("Package " + packageName |
| 2178 | + " has not requested permission " + permissionName); |
| 2179 | } |
| 2180 | if ((bp.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) == 0) { |
| 2181 | throw new SecurityException("Permission " + permissionName |
| 2182 | + " is not a development permission"); |
| 2183 | } |
| 2184 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2185 | if (ps == null) { |
| 2186 | return; |
| 2187 | } |
| 2188 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 2189 | if (gp.grantedPermissions.remove(permissionName)) { |
| 2190 | gp.grantedPermissions.remove(permissionName); |
| 2191 | if (ps.haveGids) { |
| 2192 | gp.gids = removeInts(gp.gids, bp.gids); |
| 2193 | } |
| 2194 | mSettings.writeLPr(); |
| 2195 | } |
| 2196 | } |
| 2197 | } |
| 2198 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 2199 | public boolean isProtectedBroadcast(String actionName) { |
| 2200 | synchronized (mPackages) { |
| 2201 | return mProtectedBroadcasts.contains(actionName); |
| 2202 | } |
| 2203 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2204 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2205 | public int checkSignatures(String pkg1, String pkg2) { |
| 2206 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2207 | final PackageParser.Package p1 = mPackages.get(pkg1); |
| 2208 | final PackageParser.Package p2 = mPackages.get(pkg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2209 | if (p1 == null || p1.mExtras == null |
| 2210 | || p2 == null || p2.mExtras == null) { |
| 2211 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2212 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2213 | return compareSignatures(p1.mSignatures, p2.mSignatures); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2217 | public int checkUidSignatures(int uid1, int uid2) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2218 | // Map to base uids. |
| 2219 | uid1 = UserId.getAppId(uid1); |
| 2220 | uid2 = UserId.getAppId(uid2); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2221 | // reader |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2222 | synchronized (mPackages) { |
| 2223 | Signature[] s1; |
| 2224 | Signature[] s2; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2225 | Object obj = mSettings.getUserIdLPr(uid1); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2226 | if (obj != null) { |
| 2227 | if (obj instanceof SharedUserSetting) { |
| 2228 | s1 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 2229 | } else if (obj instanceof PackageSetting) { |
| 2230 | s1 = ((PackageSetting)obj).signatures.mSignatures; |
| 2231 | } else { |
| 2232 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2233 | } |
| 2234 | } else { |
| 2235 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2236 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2237 | obj = mSettings.getUserIdLPr(uid2); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2238 | if (obj != null) { |
| 2239 | if (obj instanceof SharedUserSetting) { |
| 2240 | s2 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 2241 | } else if (obj instanceof PackageSetting) { |
| 2242 | s2 = ((PackageSetting)obj).signatures.mSignatures; |
| 2243 | } else { |
| 2244 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2245 | } |
| 2246 | } else { |
| 2247 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2248 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2249 | return compareSignatures(s1, s2); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2250 | } |
| 2251 | } |
| 2252 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2253 | static int compareSignatures(Signature[] s1, Signature[] s2) { |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2254 | if (s1 == null) { |
| 2255 | return s2 == null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2256 | ? PackageManager.SIGNATURE_NEITHER_SIGNED |
| 2257 | : PackageManager.SIGNATURE_FIRST_NOT_SIGNED; |
| 2258 | } |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2259 | if (s2 == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2260 | return PackageManager.SIGNATURE_SECOND_NOT_SIGNED; |
| 2261 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2262 | HashSet<Signature> set1 = new HashSet<Signature>(); |
| 2263 | for (Signature sig : s1) { |
| 2264 | set1.add(sig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2265 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2266 | HashSet<Signature> set2 = new HashSet<Signature>(); |
| 2267 | for (Signature sig : s2) { |
| 2268 | set2.add(sig); |
| 2269 | } |
| 2270 | // Make sure s2 contains all signatures in s1. |
| 2271 | if (set1.equals(set2)) { |
| 2272 | return PackageManager.SIGNATURE_MATCH; |
| 2273 | } |
| 2274 | return PackageManager.SIGNATURE_NO_MATCH; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | public String[] getPackagesForUid(int uid) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2278 | uid = UserId.getAppId(uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2279 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2280 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2281 | Object obj = mSettings.getUserIdLPr(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2282 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2283 | final SharedUserSetting sus = (SharedUserSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2284 | final int N = sus.packages.size(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2285 | final String[] res = new String[N]; |
| 2286 | final Iterator<PackageSetting> it = sus.packages.iterator(); |
| 2287 | int i = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2288 | while (it.hasNext()) { |
| 2289 | res[i++] = it.next().name; |
| 2290 | } |
| 2291 | return res; |
| 2292 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2293 | final PackageSetting ps = (PackageSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2294 | return new String[] { ps.name }; |
| 2295 | } |
| 2296 | } |
| 2297 | return null; |
| 2298 | } |
| 2299 | |
| 2300 | public String getNameForUid(int uid) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2301 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2302 | synchronized (mPackages) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2303 | Object obj = mSettings.getUserIdLPr(UserId.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2304 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2305 | final SharedUserSetting sus = (SharedUserSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2306 | return sus.name + ":" + sus.userId; |
| 2307 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2308 | final PackageSetting ps = (PackageSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2309 | return ps.name; |
| 2310 | } |
| 2311 | } |
| 2312 | return null; |
| 2313 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2314 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2315 | public int getUidForSharedUser(String sharedUserName) { |
| 2316 | if(sharedUserName == null) { |
| 2317 | return -1; |
| 2318 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2319 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2320 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2321 | final SharedUserSetting suid = mSettings.getSharedUserLPw(sharedUserName, 0, false); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2322 | if (suid == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2323 | return -1; |
| 2324 | } |
| 2325 | return suid.userId; |
| 2326 | } |
| 2327 | } |
| 2328 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2329 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2330 | public ResolveInfo resolveIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2331 | int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2332 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2333 | List<ResolveInfo> query = queryIntentActivities(intent, resolvedType, flags, userId); |
| 2334 | return chooseBestActivity(intent, resolvedType, flags, query, userId); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 2335 | } |
| 2336 | |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 2337 | private ResolveInfo chooseBestActivity(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2338 | int flags, List<ResolveInfo> query, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2339 | if (query != null) { |
| 2340 | final int N = query.size(); |
| 2341 | if (N == 1) { |
| 2342 | return query.get(0); |
| 2343 | } else if (N > 1) { |
| 2344 | // If there is more than one activity with the same priority, |
| 2345 | // then let the user decide between them. |
| 2346 | ResolveInfo r0 = query.get(0); |
| 2347 | ResolveInfo r1 = query.get(1); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 2348 | if (DEBUG_INTENT_MATCHING) { |
| 2349 | Log.d(TAG, r0.activityInfo.name + "=" + r0.priority + " vs " |
| 2350 | + r1.activityInfo.name + "=" + r1.priority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2351 | } |
| 2352 | // If the first activity has a higher priority, or a different |
| 2353 | // default, then it is always desireable to pick it. |
| 2354 | if (r0.priority != r1.priority |
| 2355 | || r0.preferredOrder != r1.preferredOrder |
| 2356 | || r0.isDefault != r1.isDefault) { |
| 2357 | return query.get(0); |
| 2358 | } |
| 2359 | // If we have saved a preference for a preferred activity for |
| 2360 | // this Intent, use that. |
| 2361 | ResolveInfo ri = findPreferredActivity(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2362 | flags, query, r0.priority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2363 | if (ri != null) { |
| 2364 | return ri; |
| 2365 | } |
| 2366 | return mResolveInfo; |
| 2367 | } |
| 2368 | } |
| 2369 | return null; |
| 2370 | } |
| 2371 | |
| 2372 | ResolveInfo findPreferredActivity(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2373 | int flags, List<ResolveInfo> query, int priority, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2374 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2375 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2376 | synchronized (mPackages) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2377 | if (intent.getSelector() != null) { |
| 2378 | intent = intent.getSelector(); |
| 2379 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2380 | if (DEBUG_PREFERRED) intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION); |
| 2381 | List<PreferredActivity> prefs = |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 2382 | mSettings.mPreferredActivities.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2383 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2384 | if (prefs != null && prefs.size() > 0) { |
| 2385 | // First figure out how good the original match set is. |
| 2386 | // We will only allow preferred activities that came |
| 2387 | // from the same match quality. |
| 2388 | int match = 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2389 | |
| 2390 | if (DEBUG_PREFERRED) { |
| 2391 | Log.v(TAG, "Figuring out best match..."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2392 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2393 | |
| 2394 | final int N = query.size(); |
| 2395 | for (int j=0; j<N; j++) { |
| 2396 | final ResolveInfo ri = query.get(j); |
| 2397 | if (DEBUG_PREFERRED) { |
| 2398 | Log.v(TAG, "Match for " + ri.activityInfo + ": 0x" |
| 2399 | + Integer.toHexString(match)); |
| 2400 | } |
| 2401 | if (ri.match > match) { |
| 2402 | match = ri.match; |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | if (DEBUG_PREFERRED) { |
| 2407 | Log.v(TAG, "Best match: 0x" + Integer.toHexString(match)); |
| 2408 | } |
| 2409 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2410 | match &= IntentFilter.MATCH_CATEGORY_MASK; |
| 2411 | final int M = prefs.size(); |
| 2412 | for (int i=0; i<M; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2413 | final PreferredActivity pa = prefs.get(i); |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 2414 | if (pa.mPref.mMatch != match) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2415 | continue; |
| 2416 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2417 | final ActivityInfo ai = getActivityInfo(pa.mPref.mComponent, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2418 | if (DEBUG_PREFERRED) { |
| 2419 | Log.v(TAG, "Got preferred activity:"); |
Marco Nelissen | d85621c | 2010-09-03 09:25:33 -0700 | [diff] [blame] | 2420 | if (ai != null) { |
| 2421 | ai.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 2422 | } else { |
| 2423 | Log.v(TAG, " null"); |
| 2424 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2425 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2426 | if (ai == null) { |
| 2427 | // This previously registered preferred activity |
| 2428 | // component is no longer known. Most likely an update |
| 2429 | // to the app was installed and in the new version this |
| 2430 | // component no longer exists. Clean it up by removing |
| 2431 | // it from the preferred activities list, and skip it. |
| 2432 | Slog.w(TAG, "Removing dangling preferred activity: " |
| 2433 | + pa.mPref.mComponent); |
| 2434 | mSettings.mPreferredActivities.removeFilter(pa); |
| 2435 | continue; |
| 2436 | } |
| 2437 | for (int j=0; j<N; j++) { |
| 2438 | final ResolveInfo ri = query.get(j); |
| 2439 | if (!ri.activityInfo.applicationInfo.packageName |
| 2440 | .equals(ai.applicationInfo.packageName)) { |
| 2441 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2442 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2443 | if (!ri.activityInfo.name.equals(ai.name)) { |
| 2444 | continue; |
| 2445 | } |
| 2446 | |
| 2447 | // Okay we found a previously set preferred app. |
| 2448 | // If the result set is different from when this |
| 2449 | // was created, we need to clear it and re-ask the |
| 2450 | // user their preference. |
| 2451 | if (!pa.mPref.sameSet(query, priority)) { |
| 2452 | Slog.i(TAG, "Result set changed, dropping preferred activity for " |
| 2453 | + intent + " type " + resolvedType); |
| 2454 | mSettings.mPreferredActivities.removeFilter(pa); |
| 2455 | return null; |
| 2456 | } |
| 2457 | |
| 2458 | // Yay! |
| 2459 | return ri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | } |
| 2464 | return null; |
| 2465 | } |
| 2466 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2467 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2468 | public List<ResolveInfo> queryIntentActivities(Intent intent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2469 | String resolvedType, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2470 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2471 | ComponentName comp = intent.getComponent(); |
| 2472 | if (comp == null) { |
| 2473 | if (intent.getSelector() != null) { |
| 2474 | intent = intent.getSelector(); |
| 2475 | comp = intent.getComponent(); |
| 2476 | } |
| 2477 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2478 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2479 | if (comp != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2480 | final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2481 | final ActivityInfo ai = getActivityInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2482 | if (ai != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2483 | final ResolveInfo ri = new ResolveInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2484 | ri.activityInfo = ai; |
| 2485 | list.add(ri); |
| 2486 | } |
| 2487 | return list; |
| 2488 | } |
| 2489 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2490 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2491 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2492 | final String pkgName = intent.getPackage(); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2493 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2494 | return mActivities.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2495 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2496 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2497 | if (pkg != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2498 | return mActivities.queryIntentForPackage(intent, resolvedType, flags, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2499 | pkg.activities, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2500 | } |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 2501 | return new ArrayList<ResolveInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2502 | } |
| 2503 | } |
| 2504 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2505 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2506 | public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller, |
| 2507 | Intent[] specifics, String[] specificTypes, Intent intent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2508 | String resolvedType, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2509 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2510 | final String resultsAction = intent.getAction(); |
| 2511 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2512 | List<ResolveInfo> results = queryIntentActivities(intent, resolvedType, flags |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2513 | | PackageManager.GET_RESOLVED_FILTER, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2514 | |
| 2515 | if (DEBUG_INTENT_MATCHING) { |
| 2516 | Log.v(TAG, "Query " + intent + ": " + results); |
| 2517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2518 | |
| 2519 | int specificsPos = 0; |
| 2520 | int N; |
| 2521 | |
| 2522 | // todo: note that the algorithm used here is O(N^2). This |
| 2523 | // isn't a problem in our current environment, but if we start running |
| 2524 | // into situations where we have more than 5 or 10 matches then this |
| 2525 | // should probably be changed to something smarter... |
| 2526 | |
| 2527 | // First we go through and resolve each of the specific items |
| 2528 | // that were supplied, taking care of removing any corresponding |
| 2529 | // duplicate items in the generic resolve list. |
| 2530 | if (specifics != null) { |
| 2531 | for (int i=0; i<specifics.length; i++) { |
| 2532 | final Intent sintent = specifics[i]; |
| 2533 | if (sintent == null) { |
| 2534 | continue; |
| 2535 | } |
| 2536 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2537 | if (DEBUG_INTENT_MATCHING) { |
| 2538 | Log.v(TAG, "Specific #" + i + ": " + sintent); |
| 2539 | } |
| 2540 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2541 | String action = sintent.getAction(); |
| 2542 | if (resultsAction != null && resultsAction.equals(action)) { |
| 2543 | // If this action was explicitly requested, then don't |
| 2544 | // remove things that have it. |
| 2545 | action = null; |
| 2546 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2547 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2548 | ResolveInfo ri = null; |
| 2549 | ActivityInfo ai = null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2550 | |
| 2551 | ComponentName comp = sintent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2552 | if (comp == null) { |
| 2553 | ri = resolveIntent( |
| 2554 | sintent, |
| 2555 | specificTypes != null ? specificTypes[i] : null, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2556 | flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2557 | if (ri == null) { |
| 2558 | continue; |
| 2559 | } |
| 2560 | if (ri == mResolveInfo) { |
| 2561 | // ACK! Must do something better with this. |
| 2562 | } |
| 2563 | ai = ri.activityInfo; |
| 2564 | comp = new ComponentName(ai.applicationInfo.packageName, |
| 2565 | ai.name); |
| 2566 | } else { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2567 | ai = getActivityInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2568 | if (ai == null) { |
| 2569 | continue; |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | // Look for any generic query activities that are duplicates |
| 2574 | // of this specific one, and remove them from the results. |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2575 | 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] | 2576 | N = results.size(); |
| 2577 | int j; |
| 2578 | for (j=specificsPos; j<N; j++) { |
| 2579 | ResolveInfo sri = results.get(j); |
| 2580 | if ((sri.activityInfo.name.equals(comp.getClassName()) |
| 2581 | && sri.activityInfo.applicationInfo.packageName.equals( |
| 2582 | comp.getPackageName())) |
| 2583 | || (action != null && sri.filter.matchAction(action))) { |
| 2584 | results.remove(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2585 | if (DEBUG_INTENT_MATCHING) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2586 | TAG, "Removing duplicate item from " + j |
| 2587 | + " due to specific " + specificsPos); |
| 2588 | if (ri == null) { |
| 2589 | ri = sri; |
| 2590 | } |
| 2591 | j--; |
| 2592 | N--; |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | // Add this specific item to its proper place. |
| 2597 | if (ri == null) { |
| 2598 | ri = new ResolveInfo(); |
| 2599 | ri.activityInfo = ai; |
| 2600 | } |
| 2601 | results.add(specificsPos, ri); |
| 2602 | ri.specificIndex = i; |
| 2603 | specificsPos++; |
| 2604 | } |
| 2605 | } |
| 2606 | |
| 2607 | // Now we go through the remaining generic results and remove any |
| 2608 | // duplicate actions that are found here. |
| 2609 | N = results.size(); |
| 2610 | for (int i=specificsPos; i<N-1; i++) { |
| 2611 | final ResolveInfo rii = results.get(i); |
| 2612 | if (rii.filter == null) { |
| 2613 | continue; |
| 2614 | } |
| 2615 | |
| 2616 | // Iterate over all of the actions of this result's intent |
| 2617 | // filter... typically this should be just one. |
| 2618 | final Iterator<String> it = rii.filter.actionsIterator(); |
| 2619 | if (it == null) { |
| 2620 | continue; |
| 2621 | } |
| 2622 | while (it.hasNext()) { |
| 2623 | final String action = it.next(); |
| 2624 | if (resultsAction != null && resultsAction.equals(action)) { |
| 2625 | // If this action was explicitly requested, then don't |
| 2626 | // remove things that have it. |
| 2627 | continue; |
| 2628 | } |
| 2629 | for (int j=i+1; j<N; j++) { |
| 2630 | final ResolveInfo rij = results.get(j); |
| 2631 | if (rij.filter != null && rij.filter.hasAction(action)) { |
| 2632 | results.remove(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2633 | if (DEBUG_INTENT_MATCHING) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2634 | TAG, "Removing duplicate item from " + j |
| 2635 | + " due to action " + action + " at " + i); |
| 2636 | j--; |
| 2637 | N--; |
| 2638 | } |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | // If the caller didn't request filter information, drop it now |
| 2643 | // so we don't have to marshall/unmarshall it. |
| 2644 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2645 | rii.filter = null; |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | // Filter out the caller activity if so requested. |
| 2650 | if (caller != null) { |
| 2651 | N = results.size(); |
| 2652 | for (int i=0; i<N; i++) { |
| 2653 | ActivityInfo ainfo = results.get(i).activityInfo; |
| 2654 | if (caller.getPackageName().equals(ainfo.applicationInfo.packageName) |
| 2655 | && caller.getClassName().equals(ainfo.name)) { |
| 2656 | results.remove(i); |
| 2657 | break; |
| 2658 | } |
| 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | // If the caller didn't request filter information, |
| 2663 | // drop them now so we don't have to |
| 2664 | // marshall/unmarshall it. |
| 2665 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2666 | N = results.size(); |
| 2667 | for (int i=0; i<N; i++) { |
| 2668 | results.get(i).filter = null; |
| 2669 | } |
| 2670 | } |
| 2671 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2672 | if (DEBUG_INTENT_MATCHING) Log.v(TAG, "Result: " + results); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2673 | return results; |
| 2674 | } |
| 2675 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2676 | @Override |
| 2677 | public List<ResolveInfo> queryIntentReceivers(Intent intent, String resolvedType, int flags, |
| 2678 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2679 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2680 | ComponentName comp = intent.getComponent(); |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2681 | if (comp == null) { |
| 2682 | if (intent.getSelector() != null) { |
| 2683 | intent = intent.getSelector(); |
| 2684 | comp = intent.getComponent(); |
| 2685 | } |
| 2686 | } |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2687 | if (comp != null) { |
| 2688 | List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2689 | ActivityInfo ai = getReceiverInfo(comp, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2690 | if (ai != null) { |
| 2691 | ResolveInfo ri = new ResolveInfo(); |
| 2692 | ri.activityInfo = ai; |
| 2693 | list.add(ri); |
| 2694 | } |
| 2695 | return list; |
| 2696 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2697 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2698 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2699 | synchronized (mPackages) { |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2700 | String pkgName = intent.getPackage(); |
| 2701 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2702 | return mReceivers.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2703 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2704 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2705 | if (pkg != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2706 | return mReceivers.queryIntentForPackage(intent, resolvedType, flags, pkg.receivers, |
| 2707 | userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2708 | } |
| 2709 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2710 | } |
| 2711 | } |
| 2712 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2713 | @Override |
| 2714 | public ResolveInfo resolveService(Intent intent, String resolvedType, int flags, int userId) { |
| 2715 | List<ResolveInfo> query = queryIntentServices(intent, resolvedType, flags, userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2716 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2717 | if (query != null) { |
| 2718 | if (query.size() >= 1) { |
| 2719 | // If there is more than one service with the same priority, |
| 2720 | // just arbitrarily pick the first one. |
| 2721 | return query.get(0); |
| 2722 | } |
| 2723 | } |
| 2724 | return null; |
| 2725 | } |
| 2726 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2727 | @Override |
| 2728 | public List<ResolveInfo> queryIntentServices(Intent intent, String resolvedType, int flags, |
| 2729 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2730 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2731 | ComponentName comp = intent.getComponent(); |
| 2732 | if (comp == null) { |
| 2733 | if (intent.getSelector() != null) { |
| 2734 | intent = intent.getSelector(); |
| 2735 | comp = intent.getComponent(); |
| 2736 | } |
| 2737 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2738 | if (comp != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2739 | final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2740 | final ServiceInfo si = getServiceInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2741 | if (si != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2742 | final ResolveInfo ri = new ResolveInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2743 | ri.serviceInfo = si; |
| 2744 | list.add(ri); |
| 2745 | } |
| 2746 | return list; |
| 2747 | } |
| 2748 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2749 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2750 | synchronized (mPackages) { |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2751 | String pkgName = intent.getPackage(); |
| 2752 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2753 | return mServices.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2754 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2755 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2756 | if (pkg != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2757 | return mServices.queryIntentForPackage(intent, resolvedType, flags, pkg.services, |
| 2758 | userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2759 | } |
| 2760 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2761 | } |
| 2762 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2763 | |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2764 | private static final int getContinuationPoint(final String[] keys, final String key) { |
| 2765 | final int index; |
| 2766 | if (key == null) { |
| 2767 | index = 0; |
| 2768 | } else { |
| 2769 | final int insertPoint = Arrays.binarySearch(keys, key); |
| 2770 | if (insertPoint < 0) { |
| 2771 | index = -insertPoint; |
| 2772 | } else { |
| 2773 | index = insertPoint + 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2774 | } |
| 2775 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2776 | return index; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2777 | } |
| 2778 | |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2779 | public ParceledListSlice<PackageInfo> getInstalledPackages(int flags, String lastRead) { |
| 2780 | final ParceledListSlice<PackageInfo> list = new ParceledListSlice<PackageInfo>(); |
| 2781 | final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0; |
| 2782 | final String[] keys; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2783 | int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2784 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2785 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2786 | synchronized (mPackages) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2787 | if (listUninstalled) { |
| 2788 | keys = mSettings.mPackages.keySet().toArray(new String[mSettings.mPackages.size()]); |
| 2789 | } else { |
| 2790 | keys = mPackages.keySet().toArray(new String[mPackages.size()]); |
| 2791 | } |
| 2792 | |
| 2793 | Arrays.sort(keys); |
| 2794 | int i = getContinuationPoint(keys, lastRead); |
| 2795 | final int N = keys.length; |
| 2796 | |
| 2797 | while (i < N) { |
| 2798 | final String packageName = keys[i++]; |
| 2799 | |
| 2800 | PackageInfo pi = null; |
| 2801 | if (listUninstalled) { |
| 2802 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
| 2803 | if (ps != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2804 | pi = generatePackageInfoFromSettingsLPw(ps.name, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2805 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2806 | } else { |
| 2807 | final PackageParser.Package p = mPackages.get(packageName); |
| 2808 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2809 | pi = generatePackageInfo(p, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2810 | } |
| 2811 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2812 | |
Henrik Engström | 9771a31 | 2012-02-21 09:05:17 +0100 | [diff] [blame] | 2813 | if (pi != null && list.append(pi)) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2814 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2815 | } |
| 2816 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2817 | |
| 2818 | if (i == N) { |
| 2819 | list.setLastSlice(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2820 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2821 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2822 | |
| 2823 | return list; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2824 | } |
| 2825 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2826 | @Override |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2827 | public ParceledListSlice<ApplicationInfo> getInstalledApplications(int flags, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2828 | String lastRead, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2829 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2830 | final ParceledListSlice<ApplicationInfo> list = new ParceledListSlice<ApplicationInfo>(); |
| 2831 | final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0; |
| 2832 | final String[] keys; |
| 2833 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2834 | // writer |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2835 | synchronized (mPackages) { |
| 2836 | if (listUninstalled) { |
| 2837 | keys = mSettings.mPackages.keySet().toArray(new String[mSettings.mPackages.size()]); |
| 2838 | } else { |
| 2839 | keys = mPackages.keySet().toArray(new String[mPackages.size()]); |
| 2840 | } |
| 2841 | |
| 2842 | Arrays.sort(keys); |
| 2843 | int i = getContinuationPoint(keys, lastRead); |
| 2844 | final int N = keys.length; |
| 2845 | |
| 2846 | while (i < N) { |
| 2847 | final String packageName = keys[i++]; |
| 2848 | |
| 2849 | ApplicationInfo ai = null; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2850 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2851 | if (listUninstalled) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2852 | if (ps != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2853 | ai = generateApplicationInfoFromSettingsLPw(ps.name, flags, userId); |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2854 | } |
| 2855 | } else { |
| 2856 | final PackageParser.Package p = mPackages.get(packageName); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2857 | if (p != null && ps != null) { |
| 2858 | ai = PackageParser.generateApplicationInfo(p, flags, ps.getStopped(userId), |
| 2859 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2860 | } |
| 2861 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2862 | |
Henrik Engström | 9771a31 | 2012-02-21 09:05:17 +0100 | [diff] [blame] | 2863 | if (ai != null && list.append(ai)) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2864 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2865 | } |
| 2866 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2867 | |
| 2868 | if (i == N) { |
| 2869 | list.setLastSlice(true); |
| 2870 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2871 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2872 | |
| 2873 | return list; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | public List<ApplicationInfo> getPersistentApplications(int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2877 | final ArrayList<ApplicationInfo> finalList = new ArrayList<ApplicationInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2878 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2879 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2880 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2881 | final Iterator<PackageParser.Package> i = mPackages.values().iterator(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2882 | final int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2883 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2884 | final PackageParser.Package p = i.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2885 | if (p.applicationInfo != null |
| 2886 | && (p.applicationInfo.flags&ApplicationInfo.FLAG_PERSISTENT) != 0 |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 2887 | && (!mSafeMode || isSystemApp(p))) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2888 | PackageSetting ps = mSettings.mPackages.get(p.packageName); |
| 2889 | finalList.add(PackageParser.generateApplicationInfo(p, flags, |
| 2890 | ps != null ? ps.getStopped(userId) : false, |
| 2891 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2892 | userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2893 | } |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | return finalList; |
| 2898 | } |
| 2899 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2900 | @Override |
| 2901 | public ProviderInfo resolveContentProvider(String name, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2902 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2903 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2904 | synchronized (mPackages) { |
| 2905 | final PackageParser.Provider provider = mProviders.get(name); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2906 | PackageSetting ps = provider != null |
| 2907 | ? mSettings.mPackages.get(provider.owner.packageName) |
| 2908 | : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2909 | return provider != null |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2910 | && mSettings.isEnabledLPr(provider.info, flags, userId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2911 | && (!mSafeMode || (provider.info.applicationInfo.flags |
| 2912 | &ApplicationInfo.FLAG_SYSTEM) != 0) |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2913 | ? PackageParser.generateProviderInfo(provider, flags, |
| 2914 | ps != null ? ps.getStopped(userId) : false, |
| 2915 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2916 | userId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2917 | : null; |
| 2918 | } |
| 2919 | } |
| 2920 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2921 | /** |
| 2922 | * @deprecated |
| 2923 | */ |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 2924 | @Deprecated |
| 2925 | public void querySyncProviders(List<String> outNames, List<ProviderInfo> outInfo) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2926 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2927 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2928 | final Iterator<Map.Entry<String, PackageParser.Provider>> i = mProviders.entrySet() |
| 2929 | .iterator(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2930 | final int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2931 | while (i.hasNext()) { |
| 2932 | Map.Entry<String, PackageParser.Provider> entry = i.next(); |
| 2933 | PackageParser.Provider p = entry.getValue(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2934 | PackageSetting ps = mSettings.mPackages.get(p.owner.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2935 | |
| 2936 | if (p.syncable |
| 2937 | && (!mSafeMode || (p.info.applicationInfo.flags |
| 2938 | &ApplicationInfo.FLAG_SYSTEM) != 0)) { |
| 2939 | outNames.add(entry.getKey()); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2940 | outInfo.add(PackageParser.generateProviderInfo(p, 0, |
| 2941 | ps != null ? ps.getStopped(userId) : false, |
| 2942 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2943 | userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2944 | } |
| 2945 | } |
| 2946 | } |
| 2947 | } |
| 2948 | |
| 2949 | public List<ProviderInfo> queryContentProviders(String processName, |
| 2950 | int uid, int flags) { |
| 2951 | ArrayList<ProviderInfo> finalList = null; |
| 2952 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2953 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2954 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2955 | final Iterator<PackageParser.Provider> i = mProvidersByComponent.values().iterator(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2956 | final int userId = processName != null ? |
| 2957 | UserId.getUserId(uid) : UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2958 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2959 | final PackageParser.Provider p = i.next(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2960 | PackageSetting ps = mSettings.mPackages.get(p.owner.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2961 | if (p.info.authority != null |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2962 | && (processName == null |
| 2963 | || (p.info.processName.equals(processName) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2964 | && UserId.isSameApp(p.info.applicationInfo.uid, uid))) |
| 2965 | && mSettings.isEnabledLPr(p.info, flags, userId) |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2966 | && (!mSafeMode |
| 2967 | || (p.info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2968 | if (finalList == null) { |
| 2969 | finalList = new ArrayList<ProviderInfo>(3); |
| 2970 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2971 | finalList.add(PackageParser.generateProviderInfo(p, flags, |
| 2972 | ps != null ? ps.getStopped(userId) : false, |
| 2973 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2974 | userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2975 | } |
| 2976 | } |
| 2977 | } |
| 2978 | |
| 2979 | if (finalList != null) { |
| 2980 | Collections.sort(finalList, mProviderInitOrderSorter); |
| 2981 | } |
| 2982 | |
| 2983 | return finalList; |
| 2984 | } |
| 2985 | |
| 2986 | public InstrumentationInfo getInstrumentationInfo(ComponentName name, |
| 2987 | int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2988 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2989 | synchronized (mPackages) { |
| 2990 | final PackageParser.Instrumentation i = mInstrumentation.get(name); |
| 2991 | return PackageParser.generateInstrumentationInfo(i, flags); |
| 2992 | } |
| 2993 | } |
| 2994 | |
| 2995 | public List<InstrumentationInfo> queryInstrumentation(String targetPackage, |
| 2996 | int flags) { |
| 2997 | ArrayList<InstrumentationInfo> finalList = |
| 2998 | new ArrayList<InstrumentationInfo>(); |
| 2999 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3000 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3001 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3002 | final Iterator<PackageParser.Instrumentation> i = mInstrumentation.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3003 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3004 | final PackageParser.Instrumentation p = i.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3005 | if (targetPackage == null |
| 3006 | || targetPackage.equals(p.info.targetPackage)) { |
| 3007 | finalList.add(PackageParser.generateInstrumentationInfo(p, |
| 3008 | flags)); |
| 3009 | } |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | return finalList; |
| 3014 | } |
| 3015 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3016 | 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] | 3017 | String[] files = dir.list(); |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 3018 | if (files == null) { |
| 3019 | Log.d(TAG, "No files in app dir " + dir); |
| 3020 | return; |
| 3021 | } |
| 3022 | |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3023 | if (DEBUG_PACKAGE_SCANNING) { |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 3024 | Log.d(TAG, "Scanning app dir " + dir); |
| 3025 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3026 | |
| 3027 | int i; |
| 3028 | for (i=0; i<files.length; i++) { |
| 3029 | File file = new File(dir, files[i]); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 3030 | if (!isPackageFilename(files[i])) { |
| 3031 | // Ignore entries which are not apk's |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 3032 | continue; |
| 3033 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3034 | PackageParser.Package pkg = scanPackageLI(file, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3035 | flags|PackageParser.PARSE_MUST_BE_APK, scanMode, currentTime); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3036 | // Don't mess around with apps in system partition. |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 3037 | if (pkg == null && (flags & PackageParser.PARSE_IS_SYSTEM) == 0 && |
| 3038 | mLastScanError == PackageManager.INSTALL_FAILED_INVALID_APK) { |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3039 | // Delete the apk |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3040 | Slog.w(TAG, "Cleaning up failed install of " + file); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3041 | file.delete(); |
| 3042 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3043 | } |
| 3044 | } |
| 3045 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3046 | private static File getSettingsProblemFile() { |
| 3047 | File dataDir = Environment.getDataDirectory(); |
| 3048 | File systemDir = new File(dataDir, "system"); |
| 3049 | File fname = new File(systemDir, "uiderrors.txt"); |
| 3050 | return fname; |
| 3051 | } |
| 3052 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 3053 | static void reportSettingsProblem(int priority, String msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3054 | try { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3055 | File fname = getSettingsProblemFile(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3056 | FileOutputStream out = new FileOutputStream(fname, true); |
| 3057 | PrintWriter pw = new PrintWriter(out); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3058 | SimpleDateFormat formatter = new SimpleDateFormat(); |
| 3059 | String dateString = formatter.format(new Date(System.currentTimeMillis())); |
| 3060 | pw.println(dateString + ": " + msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3061 | pw.close(); |
| 3062 | FileUtils.setPermissions( |
| 3063 | fname.toString(), |
| 3064 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IROTH, |
| 3065 | -1, -1); |
| 3066 | } catch (java.io.IOException e) { |
| 3067 | } |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3068 | Slog.println(priority, TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | private boolean collectCertificatesLI(PackageParser pp, PackageSetting ps, |
| 3072 | PackageParser.Package pkg, File srcFile, int parseFlags) { |
| 3073 | if (GET_CERTIFICATES) { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3074 | if (ps != null |
| 3075 | && ps.codePath.equals(srcFile) |
Kenny Root | 7d794fb | 2010-09-13 16:29:49 -0700 | [diff] [blame] | 3076 | && ps.timeStamp == srcFile.lastModified()) { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3077 | if (ps.signatures.mSignatures != null |
| 3078 | && ps.signatures.mSignatures.length != 0) { |
| 3079 | // Optimization: reuse the existing cached certificates |
| 3080 | // if the package appears to be unchanged. |
| 3081 | pkg.mSignatures = ps.signatures.mSignatures; |
| 3082 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3083 | } |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3084 | |
| 3085 | 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] | 3086 | } else { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3087 | Log.i(TAG, srcFile.toString() + " changed; collecting certs"); |
| 3088 | } |
| 3089 | |
| 3090 | if (!pp.collectCertificates(pkg, parseFlags)) { |
| 3091 | mLastScanError = pp.getParseError(); |
| 3092 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3093 | } |
| 3094 | } |
| 3095 | return true; |
| 3096 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3097 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3098 | /* |
| 3099 | * Scan a package and return the newly parsed package. |
| 3100 | * Returns null in case of errors and the error code is stored in mLastScanError |
| 3101 | */ |
| 3102 | private PackageParser.Package scanPackageLI(File scanFile, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3103 | int parseFlags, int scanMode, long currentTime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3104 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3105 | String scanPath = scanFile.getPath(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3106 | parseFlags |= mDefParseFlags; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3107 | PackageParser pp = new PackageParser(scanPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3108 | pp.setSeparateProcesses(mSeparateProcesses); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 3109 | pp.setOnlyCoreApps(mOnlyCore); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3110 | final PackageParser.Package pkg = pp.parsePackage(scanFile, |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3111 | scanPath, mMetrics, parseFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3112 | if (pkg == null) { |
| 3113 | mLastScanError = pp.getParseError(); |
| 3114 | return null; |
| 3115 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3116 | PackageSetting ps = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3117 | PackageSetting updatedPkg; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3118 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3119 | synchronized (mPackages) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3120 | // Look to see if we already know about this package. |
| 3121 | String oldName = mSettings.mRenamedPackages.get(pkg.packageName); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3122 | if (pkg.mOriginalPackages != null && pkg.mOriginalPackages.contains(oldName)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3123 | // This package has been renamed to its original name. Let's |
| 3124 | // use that. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3125 | ps = mSettings.peekPackageLPr(oldName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3126 | } |
| 3127 | // If there was no original package, see one for the real package name. |
| 3128 | if (ps == null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3129 | ps = mSettings.peekPackageLPr(pkg.packageName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3130 | } |
| 3131 | // Check to see if this package could be hiding/updating a system |
| 3132 | // package. Must look for it either under the original or real |
| 3133 | // package name depending on our state. |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 3134 | updatedPkg = mSettings.getDisabledSystemPkgLPr(ps != null ? ps.name : pkg.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3135 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3136 | // First check if this is a system package that may involve an update |
| 3137 | if (updatedPkg != null && (parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
Kenny Root | 9ee9274 | 2010-09-01 13:40:57 -0700 | [diff] [blame] | 3138 | if (ps != null && !ps.codePath.equals(scanFile)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3139 | // The path has changed from what was last scanned... check the |
| 3140 | // version of the new path against what we have stored to determine |
| 3141 | // what to do. |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3142 | if (pkg.mVersionCode < ps.versionCode) { |
| 3143 | // 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] | 3144 | // Ignore entry. Skip it. |
| 3145 | Log.i(TAG, "Package " + ps.name + " at " + scanFile |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 3146 | + " ignored: updated version " + ps.versionCode |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3147 | + " better than this " + pkg.mVersionCode); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3148 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3149 | return null; |
| 3150 | } else { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3151 | // The current app on the system partion is better than |
| 3152 | // what we have updated to on the data partition; switch |
| 3153 | // back to the system partition version. |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3154 | // At this point, its safely assumed that package installation for |
| 3155 | // apps in system partition will go through. If not there won't be a working |
| 3156 | // version of the app |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3157 | // writer |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3158 | synchronized (mPackages) { |
| 3159 | // Just remove the loaded entries from package lists. |
| 3160 | mPackages.remove(ps.name); |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 3161 | } |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3162 | Slog.w(TAG, "Package " + ps.name + " at " + scanFile |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3163 | + "reverting from " + ps.codePathString |
| 3164 | + ": new version " + pkg.mVersionCode |
| 3165 | + " better than installed " + ps.versionCode); |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 3166 | |
| 3167 | InstallArgs args = createInstallArgs(packageFlagsToInstallFlags(ps), |
| 3168 | ps.codePathString, ps.resourcePathString, ps.nativeLibraryPathString); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3169 | synchronized (mInstaller) { |
| 3170 | args.cleanUpResourcesLI(); |
| 3171 | } |
| 3172 | synchronized (mPackages) { |
| 3173 | mSettings.enableSystemPackageLPw(ps.name); |
| 3174 | } |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 3175 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3176 | } |
| 3177 | } |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3178 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3179 | if (updatedPkg != null) { |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3180 | // An updated system app will not have the PARSE_IS_SYSTEM flag set |
| 3181 | // initially |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3182 | parseFlags |= PackageParser.PARSE_IS_SYSTEM; |
| 3183 | } |
| 3184 | // Verify certificates against what was last scanned |
| 3185 | if (!collectCertificatesLI(pp, ps, pkg, scanFile, parseFlags)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3186 | Slog.w(TAG, "Failed verifying certificates for package:" + pkg.packageName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3187 | return null; |
| 3188 | } |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3189 | |
| 3190 | /* |
| 3191 | * A new system app appeared, but we already had a non-system one of the |
| 3192 | * same name installed earlier. |
| 3193 | */ |
| 3194 | boolean shouldHideSystemApp = false; |
| 3195 | if (updatedPkg == null && ps != null |
| 3196 | && (parseFlags & PackageParser.PARSE_IS_SYSTEM_DIR) != 0 && !isSystemApp(ps)) { |
| 3197 | /* |
| 3198 | * Check to make sure the signatures match first. If they don't, |
| 3199 | * wipe the installed application and its data. |
| 3200 | */ |
| 3201 | if (compareSignatures(ps.signatures.mSignatures, pkg.mSignatures) |
| 3202 | != PackageManager.SIGNATURE_MATCH) { |
| 3203 | deletePackageLI(pkg.packageName, true, 0, null, false); |
| 3204 | ps = null; |
| 3205 | } else { |
| 3206 | /* |
| 3207 | * If the newly-added system app is an older version than the |
| 3208 | * already installed version, hide it. It will be scanned later |
| 3209 | * and re-added like an update. |
| 3210 | */ |
| 3211 | if (pkg.mVersionCode < ps.versionCode) { |
| 3212 | shouldHideSystemApp = true; |
| 3213 | } else { |
| 3214 | /* |
| 3215 | * The newly found system app is a newer version that the |
| 3216 | * one previously installed. Simply remove the |
| 3217 | * already-installed application and replace it with our own |
| 3218 | * while keeping the application data. |
| 3219 | */ |
| 3220 | Slog.w(TAG, "Package " + ps.name + " at " + scanFile + "reverting from " |
| 3221 | + ps.codePathString + ": new version " + pkg.mVersionCode |
| 3222 | + " better than installed " + ps.versionCode); |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 3223 | InstallArgs args = createInstallArgs(packageFlagsToInstallFlags(ps), |
| 3224 | ps.codePathString, ps.resourcePathString, ps.nativeLibraryPathString); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3225 | synchronized (mInstaller) { |
| 3226 | args.cleanUpResourcesLI(); |
| 3227 | } |
| 3228 | } |
| 3229 | } |
| 3230 | } |
| 3231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3232 | // The apk is forward locked (not public) if its code and resources |
| 3233 | // are kept in different files. |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3234 | // TODO grab this value from PackageSettings |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3235 | if (ps != null && !ps.codePath.equals(ps.resourcePath)) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3236 | parseFlags |= PackageParser.PARSE_FORWARD_LOCK; |
Suchi Amalapurapu | f2c1072 | 2009-07-29 17:19:39 -0700 | [diff] [blame] | 3237 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3238 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3239 | String codePath = null; |
| 3240 | String resPath = null; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3241 | if ((parseFlags & PackageParser.PARSE_FORWARD_LOCK) != 0) { |
| 3242 | if (ps != null && ps.resourcePathString != null) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3243 | resPath = ps.resourcePathString; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3244 | } else { |
| 3245 | // Should not happen at all. Just log an error. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3246 | Slog.e(TAG, "Resource path not set for pkg : " + pkg.packageName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3247 | } |
| 3248 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3249 | resPath = pkg.mScanPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3250 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3251 | codePath = pkg.mScanPath; |
| 3252 | // Set application objects path explicitly. |
| 3253 | setApplicationInfoPaths(pkg, codePath, resPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3254 | // Note that we invoke the following method only if we are about to unpack an application |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3255 | PackageParser.Package scannedPkg = scanPackageLI(pkg, parseFlags, scanMode |
| 3256 | | SCAN_UPDATE_SIGNATURE, currentTime); |
| 3257 | |
| 3258 | /* |
| 3259 | * If the system app should be overridden by a previously installed |
| 3260 | * data, hide the system app now and let the /data/app scan pick it up |
| 3261 | * again. |
| 3262 | */ |
| 3263 | if (shouldHideSystemApp) { |
| 3264 | synchronized (mPackages) { |
| 3265 | /* |
| 3266 | * We have to grant systems permissions before we hide, because |
| 3267 | * grantPermissions will assume the package update is trying to |
| 3268 | * expand its permissions. |
| 3269 | */ |
| 3270 | grantPermissionsLPw(pkg, true); |
| 3271 | mSettings.disableSystemPackageLPw(pkg.packageName); |
| 3272 | } |
| 3273 | } |
| 3274 | |
| 3275 | return scannedPkg; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3276 | } |
| 3277 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3278 | private static void setApplicationInfoPaths(PackageParser.Package pkg, String destCodePath, |
| 3279 | String destResPath) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3280 | pkg.mPath = pkg.mScanPath = destCodePath; |
| 3281 | pkg.applicationInfo.sourceDir = destCodePath; |
| 3282 | pkg.applicationInfo.publicSourceDir = destResPath; |
| 3283 | } |
| 3284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3285 | private static String fixProcessName(String defProcessName, |
| 3286 | String processName, int uid) { |
| 3287 | if (processName == null) { |
| 3288 | return defProcessName; |
| 3289 | } |
| 3290 | return processName; |
| 3291 | } |
| 3292 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3293 | private boolean verifySignaturesLP(PackageSetting pkgSetting, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3294 | PackageParser.Package pkg) { |
| 3295 | if (pkgSetting.signatures.mSignatures != null) { |
| 3296 | // Already existing package. Make sure signatures match |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3297 | if (compareSignatures(pkgSetting.signatures.mSignatures, pkg.mSignatures) != |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3298 | PackageManager.SIGNATURE_MATCH) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3299 | Slog.e(TAG, "Package " + pkg.packageName |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3300 | + " signatures do not match the previously installed version; ignoring!"); |
| 3301 | mLastScanError = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3302 | return false; |
| 3303 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3304 | } |
| 3305 | // Check for shared user signatures |
| 3306 | if (pkgSetting.sharedUser != null && pkgSetting.sharedUser.signatures.mSignatures != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3307 | if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3308 | pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) { |
| 3309 | Slog.e(TAG, "Package " + pkg.packageName |
| 3310 | + " has no signatures that match those in shared user " |
| 3311 | + pkgSetting.sharedUser.name + "; ignoring!"); |
| 3312 | mLastScanError = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; |
| 3313 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3314 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3315 | } |
| 3316 | return true; |
| 3317 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3318 | |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 3319 | /** |
| 3320 | * Enforces that only the system UID or root's UID can call a method exposed |
| 3321 | * via Binder. |
| 3322 | * |
| 3323 | * @param message used as message if SecurityException is thrown |
| 3324 | * @throws SecurityException if the caller is not system or root |
| 3325 | */ |
| 3326 | private static final void enforceSystemOrRoot(String message) { |
| 3327 | final int uid = Binder.getCallingUid(); |
| 3328 | if (uid != Process.SYSTEM_UID && uid != 0) { |
| 3329 | throw new SecurityException(message); |
| 3330 | } |
| 3331 | } |
| 3332 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3333 | public void performBootDexOpt() { |
| 3334 | ArrayList<PackageParser.Package> pkgs = null; |
| 3335 | synchronized (mPackages) { |
| 3336 | if (mDeferredDexOpt.size() > 0) { |
| 3337 | pkgs = new ArrayList<PackageParser.Package>(mDeferredDexOpt); |
| 3338 | mDeferredDexOpt.clear(); |
| 3339 | } |
| 3340 | } |
| 3341 | if (pkgs != null) { |
| 3342 | for (int i=0; i<pkgs.size(); i++) { |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 3343 | if (!isFirstBoot()) { |
| 3344 | try { |
| 3345 | ActivityManagerNative.getDefault().showBootMessage( |
| 3346 | mContext.getResources().getString( |
| 3347 | com.android.internal.R.string.android_upgrading_apk, |
| 3348 | i+1, pkgs.size()), true); |
| 3349 | } catch (RemoteException e) { |
| 3350 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3351 | } |
| 3352 | PackageParser.Package p = pkgs.get(i); |
| 3353 | synchronized (mInstallLock) { |
| 3354 | if (!p.mDidDexOpt) { |
| 3355 | performDexOptLI(p, false, false); |
| 3356 | } |
| 3357 | } |
| 3358 | } |
| 3359 | } |
| 3360 | } |
| 3361 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3362 | public boolean performDexOpt(String packageName) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 3363 | enforceSystemOrRoot("Only the system can request dexopt be performed"); |
| 3364 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3365 | if (!mNoDexOpt) { |
| 3366 | return false; |
| 3367 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3368 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3369 | PackageParser.Package p; |
| 3370 | synchronized (mPackages) { |
| 3371 | p = mPackages.get(packageName); |
| 3372 | if (p == null || p.mDidDexOpt) { |
| 3373 | return false; |
| 3374 | } |
| 3375 | } |
| 3376 | synchronized (mInstallLock) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3377 | return performDexOptLI(p, false, false) == DEX_OPT_PERFORMED; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3378 | } |
| 3379 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3380 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3381 | static final int DEX_OPT_SKIPPED = 0; |
| 3382 | static final int DEX_OPT_PERFORMED = 1; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3383 | static final int DEX_OPT_DEFERRED = 2; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3384 | static final int DEX_OPT_FAILED = -1; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3385 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3386 | private int performDexOptLI(PackageParser.Package pkg, boolean forceDex, boolean defer) { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3387 | boolean performed = false; |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3388 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3389 | String path = pkg.mScanPath; |
| 3390 | int ret = 0; |
| 3391 | try { |
| 3392 | if (forceDex || dalvik.system.DexFile.isDexOptNeeded(path)) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3393 | if (!forceDex && defer) { |
| 3394 | mDeferredDexOpt.add(pkg); |
| 3395 | return DEX_OPT_DEFERRED; |
| 3396 | } else { |
| 3397 | Log.i(TAG, "Running dexopt on: " + pkg.applicationInfo.packageName); |
| 3398 | ret = mInstaller.dexopt(path, pkg.applicationInfo.uid, |
| 3399 | !isForwardLocked(pkg)); |
| 3400 | pkg.mDidDexOpt = true; |
| 3401 | performed = true; |
| 3402 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3403 | } |
| 3404 | } catch (FileNotFoundException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3405 | Slog.w(TAG, "Apk not found for dexopt: " + path); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3406 | ret = -1; |
| 3407 | } catch (IOException e) { |
Suchi Amalapurapu | 73dafa1 | 2010-04-01 16:31:31 -0700 | [diff] [blame] | 3408 | Slog.w(TAG, "IOException reading apk: " + path, e); |
| 3409 | ret = -1; |
| 3410 | } catch (dalvik.system.StaleDexCacheError e) { |
| 3411 | Slog.w(TAG, "StaleDexCacheError when reading apk: " + path, e); |
| 3412 | ret = -1; |
| 3413 | } catch (Exception e) { |
| 3414 | Slog.w(TAG, "Exception when doing dexopt : ", e); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3415 | ret = -1; |
| 3416 | } |
| 3417 | if (ret < 0) { |
| 3418 | //error from installer |
| 3419 | return DEX_OPT_FAILED; |
| 3420 | } |
| 3421 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3422 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3423 | return performed ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 3424 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 3425 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3426 | private boolean verifyPackageUpdateLPr(PackageSetting oldPkg, PackageParser.Package newPkg) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3427 | if ((oldPkg.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3428 | Slog.w(TAG, "Unable to update from " + oldPkg.name |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3429 | + " to " + newPkg.packageName |
| 3430 | + ": old package not in system partition"); |
| 3431 | return false; |
| 3432 | } else if (mPackages.get(oldPkg.name) != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3433 | Slog.w(TAG, "Unable to update from " + oldPkg.name |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3434 | + " to " + newPkg.packageName |
| 3435 | + ": old package still exists"); |
| 3436 | return false; |
| 3437 | } |
| 3438 | return true; |
| 3439 | } |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 3440 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3441 | File getDataPathForUser(int userId) { |
| 3442 | return new File(mUserAppDataDir.getAbsolutePath() + File.separator + userId); |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 3443 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3444 | |
| 3445 | private File getDataPathForPackage(String packageName, int userId) { |
Kenny Root | 9c8625e | 2011-07-25 09:42:22 -0700 | [diff] [blame] | 3446 | /* |
| 3447 | * Until we fully support multiple users, return the directory we |
| 3448 | * previously would have. The PackageManagerTests will need to be |
| 3449 | * revised when this is changed back.. |
| 3450 | */ |
| 3451 | if (userId == 0) { |
| 3452 | return new File(mAppDataDir, packageName); |
| 3453 | } else { |
| 3454 | return new File(mUserAppDataDir.getAbsolutePath() + File.separator + userId |
Amith Yamasani | 0f3f29b | 2011-05-09 14:17:14 -0700 | [diff] [blame] | 3455 | + File.separator + packageName); |
Kenny Root | 9c8625e | 2011-07-25 09:42:22 -0700 | [diff] [blame] | 3456 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3459 | private PackageParser.Package scanPackageLI(PackageParser.Package pkg, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3460 | int parseFlags, int scanMode, long currentTime) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3461 | File scanFile = new File(pkg.mScanPath); |
Suchi Amalapurapu | 7040ce7 | 2010-02-08 23:55:56 -0800 | [diff] [blame] | 3462 | if (scanFile == null || pkg.applicationInfo.sourceDir == null || |
| 3463 | pkg.applicationInfo.publicSourceDir == null) { |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3464 | // Bail out. The resource and code paths haven't been set. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3465 | Slog.w(TAG, " Code and resource paths haven't been set correctly"); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3466 | mLastScanError = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 3467 | return null; |
| 3468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3469 | mScanningPath = scanFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3470 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3471 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
| 3472 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 3473 | } |
| 3474 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3475 | if (pkg.packageName.equals("android")) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3476 | synchronized (mPackages) { |
| 3477 | if (mAndroidApplication != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3478 | Slog.w(TAG, "*************************************************"); |
| 3479 | Slog.w(TAG, "Core android package being redefined. Skipping."); |
| 3480 | Slog.w(TAG, " file=" + mScanningPath); |
| 3481 | Slog.w(TAG, "*************************************************"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3482 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3483 | return null; |
| 3484 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3485 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3486 | // Set up information for our fall-back user intent resolution |
| 3487 | // activity. |
| 3488 | mPlatformPackage = pkg; |
| 3489 | pkg.mVersionCode = mSdkVersion; |
| 3490 | mAndroidApplication = pkg.applicationInfo; |
| 3491 | mResolveActivity.applicationInfo = mAndroidApplication; |
| 3492 | mResolveActivity.name = ResolverActivity.class.getName(); |
| 3493 | mResolveActivity.packageName = mAndroidApplication.packageName; |
| 3494 | mResolveActivity.processName = mAndroidApplication.processName; |
| 3495 | mResolveActivity.launchMode = ActivityInfo.LAUNCH_MULTIPLE; |
| 3496 | mResolveActivity.flags = ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS; |
Joe Onorato | 646f46e | 2010-11-08 15:08:56 -0800 | [diff] [blame] | 3497 | 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] | 3498 | mResolveActivity.exported = true; |
| 3499 | mResolveActivity.enabled = true; |
| 3500 | mResolveInfo.activityInfo = mResolveActivity; |
| 3501 | mResolveInfo.priority = 0; |
| 3502 | mResolveInfo.preferredOrder = 0; |
| 3503 | mResolveInfo.match = 0; |
| 3504 | mResolveComponentName = new ComponentName( |
| 3505 | mAndroidApplication.packageName, mResolveActivity.name); |
| 3506 | } |
| 3507 | } |
| 3508 | |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3509 | if (DEBUG_PACKAGE_SCANNING) { |
| 3510 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3511 | Log.d(TAG, "Scanning package " + pkg.packageName); |
| 3512 | } |
| 3513 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3514 | if (mPackages.containsKey(pkg.packageName) |
| 3515 | || mSharedLibraries.containsKey(pkg.packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3516 | Slog.w(TAG, "Application package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3517 | + " already installed. Skipping duplicate."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3518 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3519 | return null; |
| 3520 | } |
| 3521 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3522 | // Initialize package source and resource directories |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3523 | File destCodeFile = new File(pkg.applicationInfo.sourceDir); |
| 3524 | File destResourceFile = new File(pkg.applicationInfo.publicSourceDir); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3526 | SharedUserSetting suid = null; |
| 3527 | PackageSetting pkgSetting = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3528 | |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 3529 | if (!isSystemApp(pkg)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3530 | // Only system apps can use these features. |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3531 | pkg.mOriginalPackages = null; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3532 | pkg.mRealPackage = null; |
| 3533 | pkg.mAdoptPermissions = null; |
| 3534 | } |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 3535 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3536 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3537 | synchronized (mPackages) { |
| 3538 | // Check all shared libraries and map to their actual file path. |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3539 | if (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null) { |
| 3540 | if (mTmpSharedLibraries == null || |
| 3541 | mTmpSharedLibraries.length < mSharedLibraries.size()) { |
| 3542 | mTmpSharedLibraries = new String[mSharedLibraries.size()]; |
| 3543 | } |
| 3544 | int num = 0; |
| 3545 | int N = pkg.usesLibraries != null ? pkg.usesLibraries.size() : 0; |
| 3546 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3547 | final String file = mSharedLibraries.get(pkg.usesLibraries.get(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3548 | if (file == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3549 | Slog.e(TAG, "Package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3550 | + " requires unavailable shared library " |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3551 | + pkg.usesLibraries.get(i) + "; failing!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3552 | mLastScanError = PackageManager.INSTALL_FAILED_MISSING_SHARED_LIBRARY; |
| 3553 | return null; |
| 3554 | } |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3555 | mTmpSharedLibraries[num] = file; |
| 3556 | num++; |
| 3557 | } |
| 3558 | N = pkg.usesOptionalLibraries != null ? pkg.usesOptionalLibraries.size() : 0; |
| 3559 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3560 | final String file = mSharedLibraries.get(pkg.usesOptionalLibraries.get(i)); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3561 | if (file == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3562 | Slog.w(TAG, "Package " + pkg.packageName |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3563 | + " desires unavailable shared library " |
| 3564 | + pkg.usesOptionalLibraries.get(i) + "; ignoring!"); |
| 3565 | } else { |
| 3566 | mTmpSharedLibraries[num] = file; |
| 3567 | num++; |
| 3568 | } |
| 3569 | } |
| 3570 | if (num > 0) { |
| 3571 | pkg.usesLibraryFiles = new String[num]; |
| 3572 | System.arraycopy(mTmpSharedLibraries, 0, |
| 3573 | pkg.usesLibraryFiles, 0, num); |
| 3574 | } |
Kenny Root | 1683afa | 2011-01-07 14:27:50 -0800 | [diff] [blame] | 3575 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3576 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3577 | if (pkg.mSharedUserId != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3578 | suid = mSettings.getSharedUserLPw(pkg.mSharedUserId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3579 | pkg.applicationInfo.flags, true); |
| 3580 | if (suid == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3581 | Slog.w(TAG, "Creating application package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3582 | + " for shared user failed"); |
| 3583 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3584 | return null; |
| 3585 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3586 | if (DEBUG_PACKAGE_SCANNING) { |
| 3587 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3588 | Log.d(TAG, "Shared UserID " + pkg.mSharedUserId + " (uid=" + suid.userId |
| 3589 | + "): packages=" + suid.packages); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3590 | } |
| 3591 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3592 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3593 | // Check if we are renaming from an original package name. |
| 3594 | PackageSetting origPackage = null; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3595 | String realName = null; |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3596 | if (pkg.mOriginalPackages != null) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3597 | // This package may need to be renamed to a previously |
| 3598 | // installed name. Let's check on that... |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3599 | final String renamed = mSettings.mRenamedPackages.get(pkg.mRealPackage); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3600 | if (pkg.mOriginalPackages.contains(renamed)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3601 | // This package had originally been installed as the |
| 3602 | // original name, and we have already taken care of |
| 3603 | // transitioning to the new one. Just update the new |
| 3604 | // one to continue using the old name. |
| 3605 | realName = pkg.mRealPackage; |
| 3606 | if (!pkg.packageName.equals(renamed)) { |
| 3607 | // Callers into this function may have already taken |
| 3608 | // care of renaming the package; only do it here if |
| 3609 | // it is not already done. |
| 3610 | pkg.setPackageName(renamed); |
| 3611 | } |
| 3612 | |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3613 | } else { |
| 3614 | for (int i=pkg.mOriginalPackages.size()-1; i>=0; i--) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3615 | if ((origPackage = mSettings.peekPackageLPr( |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3616 | pkg.mOriginalPackages.get(i))) != null) { |
| 3617 | // We do have the package already installed under its |
| 3618 | // original name... should we use it? |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3619 | if (!verifyPackageUpdateLPr(origPackage, pkg)) { |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3620 | // New package is not compatible with original. |
| 3621 | origPackage = null; |
| 3622 | continue; |
| 3623 | } else if (origPackage.sharedUser != null) { |
| 3624 | // Make sure uid is compatible between packages. |
| 3625 | if (!origPackage.sharedUser.name.equals(pkg.mSharedUserId)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3626 | Slog.w(TAG, "Unable to migrate data from " + origPackage.name |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3627 | + " to " + pkg.packageName + ": old uid " |
| 3628 | + origPackage.sharedUser.name |
| 3629 | + " differs from " + pkg.mSharedUserId); |
| 3630 | origPackage = null; |
| 3631 | continue; |
| 3632 | } |
| 3633 | } else { |
| 3634 | if (DEBUG_UPGRADE) Log.v(TAG, "Renaming new package " |
| 3635 | + pkg.packageName + " to old name " + origPackage.name); |
| 3636 | } |
| 3637 | break; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3638 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3639 | } |
| 3640 | } |
| 3641 | } |
| 3642 | |
| 3643 | if (mTransferedPackages.contains(pkg.packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3644 | Slog.w(TAG, "Package " + pkg.packageName |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3645 | + " was transferred to another, but its .apk remains"); |
| 3646 | } |
| 3647 | |
Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 3648 | // Just create the setting, don't add it yet. For already existing packages |
| 3649 | // the PkgSetting exists already and doesn't have to be created. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3650 | pkgSetting = mSettings.getPackageLPw(pkg, origPackage, realName, suid, destCodeFile, |
Kenny Root | 806cc13 | 2010-09-12 08:34:19 -0700 | [diff] [blame] | 3651 | destResourceFile, pkg.applicationInfo.nativeLibraryDir, |
| 3652 | pkg.applicationInfo.flags, true, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3653 | if (pkgSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3654 | Slog.w(TAG, "Creating application package " + pkg.packageName + " failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3655 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3656 | return null; |
| 3657 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3658 | |
| 3659 | if (pkgSetting.origPackage != null) { |
| 3660 | // If we are first transitioning from an original package, |
| 3661 | // fix up the new package's name now. We need to do this after |
| 3662 | // looking up the package under its new name, so getPackageLP |
| 3663 | // can take care of fiddling things correctly. |
| 3664 | pkg.setPackageName(origPackage.name); |
| 3665 | |
| 3666 | // File a report about this. |
| 3667 | String msg = "New package " + pkgSetting.realName |
| 3668 | + " renamed to replace old package " + pkgSetting.name; |
| 3669 | reportSettingsProblem(Log.WARN, msg); |
| 3670 | |
| 3671 | // Make a note of it. |
| 3672 | mTransferedPackages.add(origPackage.name); |
| 3673 | |
| 3674 | // No longer need to retain this. |
| 3675 | pkgSetting.origPackage = null; |
| 3676 | } |
| 3677 | |
| 3678 | if (realName != null) { |
| 3679 | // Make a note of it. |
| 3680 | mTransferedPackages.add(pkg.packageName); |
| 3681 | } |
| 3682 | |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 3683 | if (mSettings.isDisabledSystemPackageLPr(pkg.packageName)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3684 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
| 3685 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3686 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3687 | pkg.applicationInfo.uid = pkgSetting.appId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3688 | pkg.mExtras = pkgSetting; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3689 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3690 | if (!verifySignaturesLP(pkgSetting, pkg)) { |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 3691 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3692 | return null; |
| 3693 | } |
| 3694 | // The signature has changed, but this package is in the system |
| 3695 | // image... let's recover! |
Suchi Amalapurapu | c4dd60f | 2009-03-24 21:10:53 -0700 | [diff] [blame] | 3696 | pkgSetting.signatures.mSignatures = pkg.mSignatures; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3697 | // However... if this package is part of a shared user, but it |
| 3698 | // doesn't match the signature of the shared user, let's fail. |
| 3699 | // What this means is that you can't change the signatures |
| 3700 | // associated with an overall shared user, which doesn't seem all |
| 3701 | // that unreasonable. |
| 3702 | if (pkgSetting.sharedUser != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3703 | if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3704 | pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) { |
| 3705 | 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] | 3706 | mLastScanError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 3707 | return null; |
| 3708 | } |
| 3709 | } |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 3710 | // File a report about this. |
| 3711 | String msg = "System package " + pkg.packageName |
| 3712 | + " signature changed; retaining data."; |
| 3713 | reportSettingsProblem(Log.WARN, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3714 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3715 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3716 | // Verify that this new package doesn't have any content providers |
| 3717 | // that conflict with existing packages. Only do this if the |
| 3718 | // package isn't already installed, since we don't want to break |
| 3719 | // things that are installed. |
| 3720 | if ((scanMode&SCAN_NEW_INSTALL) != 0) { |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 3721 | final int N = pkg.providers.size(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3722 | int i; |
| 3723 | for (i=0; i<N; i++) { |
| 3724 | PackageParser.Provider p = pkg.providers.get(i); |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3725 | if (p.info.authority != null) { |
| 3726 | String names[] = p.info.authority.split(";"); |
| 3727 | for (int j = 0; j < names.length; j++) { |
| 3728 | if (mProviders.containsKey(names[j])) { |
| 3729 | PackageParser.Provider other = mProviders.get(names[j]); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3730 | Slog.w(TAG, "Can't install because provider name " + names[j] + |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3731 | " (in package " + pkg.applicationInfo.packageName + |
| 3732 | ") is already used by " |
| 3733 | + ((other != null && other.getComponentName() != null) |
| 3734 | ? other.getComponentName().getPackageName() : "?")); |
| 3735 | mLastScanError = PackageManager.INSTALL_FAILED_CONFLICTING_PROVIDER; |
| 3736 | return null; |
| 3737 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3738 | } |
| 3739 | } |
| 3740 | } |
| 3741 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3742 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3743 | if (pkg.mAdoptPermissions != null) { |
| 3744 | // This package wants to adopt ownership of permissions from |
| 3745 | // another package. |
| 3746 | for (int i = pkg.mAdoptPermissions.size() - 1; i >= 0; i--) { |
| 3747 | final String origName = pkg.mAdoptPermissions.get(i); |
| 3748 | final PackageSetting orig = mSettings.peekPackageLPr(origName); |
| 3749 | if (orig != null) { |
| 3750 | if (verifyPackageUpdateLPr(orig, pkg)) { |
| 3751 | Slog.i(TAG, "Adopting permissions from " + origName + " to " |
| 3752 | + pkg.packageName); |
| 3753 | mSettings.transferPermissionsLPw(origName, pkg.packageName); |
| 3754 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3755 | } |
| 3756 | } |
| 3757 | } |
| 3758 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3759 | |
| 3760 | final String pkgName = pkg.packageName; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3761 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3762 | final long scanFileTime = scanFile.lastModified(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3763 | final boolean forceDex = (scanMode&SCAN_FORCE_DEX) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3764 | pkg.applicationInfo.processName = fixProcessName( |
| 3765 | pkg.applicationInfo.packageName, |
| 3766 | pkg.applicationInfo.processName, |
| 3767 | pkg.applicationInfo.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3768 | |
| 3769 | File dataPath; |
| 3770 | if (mPlatformPackage == pkg) { |
| 3771 | // The system package is special. |
| 3772 | dataPath = new File (Environment.getDataDirectory(), "system"); |
| 3773 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3774 | } else { |
| 3775 | // This is a normal package, need to make its data directory. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3776 | dataPath = getDataPathForPackage(pkg.packageName, 0); |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3777 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3778 | boolean uidError = false; |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3779 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3780 | if (dataPath.exists()) { |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3781 | // XXX should really do this check for each user. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3782 | mOutPermissions[1] = 0; |
| 3783 | FileUtils.getPermissions(dataPath.getPath(), mOutPermissions); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3784 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3785 | // 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] | 3786 | if (mOutPermissions[1] != pkg.applicationInfo.uid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3787 | boolean recovered = false; |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3788 | if (mOutPermissions[1] == 0) { |
| 3789 | // The directory somehow became owned by root. Wow. |
| 3790 | // This is probably because the system was stopped while |
| 3791 | // installd was in the middle of messing with its libs |
| 3792 | // directory. Ask installd to fix that. |
| 3793 | int ret = mInstaller.fixUid(pkgName, pkg.applicationInfo.uid, |
| 3794 | pkg.applicationInfo.uid); |
| 3795 | if (ret >= 0) { |
| 3796 | recovered = true; |
| 3797 | String msg = "Package " + pkg.packageName |
| 3798 | + " unexpectedly changed to uid 0; recovered to " + |
| 3799 | + pkg.applicationInfo.uid; |
| 3800 | reportSettingsProblem(Log.WARN, msg); |
| 3801 | } |
| 3802 | } |
| 3803 | if (!recovered && ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0 |
| 3804 | || (scanMode&SCAN_BOOTING) != 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3805 | // If this is a system app, we can at least delete its |
| 3806 | // current data so the application will still work. |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3807 | int ret = mInstaller.remove(pkgName, 0); |
| 3808 | if (ret >= 0) { |
| 3809 | // TODO: Kill the processes first |
| 3810 | // Remove the data directories for all users |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3811 | sUserManager.removePackageForAllUsers(pkgName); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3812 | // Old data gone! |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3813 | String prefix = (parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0 |
| 3814 | ? "System package " : "Third party package "; |
| 3815 | String msg = prefix + pkg.packageName |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3816 | + " has changed from uid: " |
| 3817 | + mOutPermissions[1] + " to " |
| 3818 | + pkg.applicationInfo.uid + "; old data erased"; |
| 3819 | reportSettingsProblem(Log.WARN, msg); |
| 3820 | recovered = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3821 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3822 | // And now re-install the app. |
| 3823 | ret = mInstaller.install(pkgName, pkg.applicationInfo.uid, |
| 3824 | pkg.applicationInfo.uid); |
| 3825 | if (ret == -1) { |
| 3826 | // Ack should not happen! |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3827 | msg = prefix + pkg.packageName |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3828 | + " could not have data directory re-created after delete."; |
| 3829 | reportSettingsProblem(Log.WARN, msg); |
| 3830 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3831 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3832 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3833 | // Create data directories for all users |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3834 | sUserManager.installPackageForAllUsers(pkgName, |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3835 | pkg.applicationInfo.uid); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3836 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3837 | if (!recovered) { |
| 3838 | mHasSystemUidErrors = true; |
| 3839 | } |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3840 | } else if (!recovered) { |
| 3841 | // If we allow this install to proceed, we will be broken. |
| 3842 | // Abort, abort! |
| 3843 | mLastScanError = PackageManager.INSTALL_FAILED_UID_CHANGED; |
| 3844 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3845 | } |
| 3846 | if (!recovered) { |
| 3847 | pkg.applicationInfo.dataDir = "/mismatched_uid/settings_" |
| 3848 | + pkg.applicationInfo.uid + "/fs_" |
| 3849 | + mOutPermissions[1]; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3850 | pkg.applicationInfo.nativeLibraryDir = pkg.applicationInfo.dataDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3851 | String msg = "Package " + pkg.packageName |
| 3852 | + " has mismatched uid: " |
| 3853 | + mOutPermissions[1] + " on disk, " |
| 3854 | + pkg.applicationInfo.uid + " in settings"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3855 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3856 | synchronized (mPackages) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3857 | mSettings.mReadMessages.append(msg); |
| 3858 | mSettings.mReadMessages.append('\n'); |
| 3859 | uidError = true; |
| 3860 | if (!pkgSetting.uidError) { |
| 3861 | reportSettingsProblem(Log.ERROR, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3862 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3863 | } |
| 3864 | } |
| 3865 | } |
| 3866 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3867 | } else { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3868 | if (DEBUG_PACKAGE_SCANNING) { |
| 3869 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3870 | Log.v(TAG, "Want this data dir: " + dataPath); |
| 3871 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3872 | //invoke installer to do the actual installation |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3873 | int ret = mInstaller.install(pkgName, pkg.applicationInfo.uid, |
| 3874 | pkg.applicationInfo.uid); |
| 3875 | if (ret < 0) { |
| 3876 | // Error from installer |
| 3877 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3878 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3879 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3880 | // Create data directories for all users |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3881 | sUserManager.installPackageForAllUsers(pkgName, pkg.applicationInfo.uid); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3882 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3883 | if (dataPath.exists()) { |
| 3884 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3885 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3886 | Slog.w(TAG, "Unable to create data directory: " + dataPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3887 | pkg.applicationInfo.dataDir = null; |
| 3888 | } |
| 3889 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3890 | |
| 3891 | /* |
| 3892 | * Set the data dir to the default "/data/data/<package name>/lib" |
| 3893 | * if we got here without anyone telling us different (e.g., apps |
| 3894 | * stored on SD card have their native libraries stored in the ASEC |
| 3895 | * container with the APK). |
Kenny Root | 806cc13 | 2010-09-12 08:34:19 -0700 | [diff] [blame] | 3896 | * |
| 3897 | * This happens during an upgrade from a package settings file that |
| 3898 | * doesn't have a native library path attribute at all. |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3899 | */ |
Kenny Root | bd135c1 | 2010-10-05 12:26:27 -0700 | [diff] [blame] | 3900 | if (pkg.applicationInfo.nativeLibraryDir == null && pkg.applicationInfo.dataDir != null) { |
| 3901 | if (pkgSetting.nativeLibraryPathString == null) { |
| 3902 | final String nativeLibraryPath = new File(dataPath, LIB_DIR_NAME).getPath(); |
| 3903 | pkg.applicationInfo.nativeLibraryDir = nativeLibraryPath; |
| 3904 | pkgSetting.nativeLibraryPathString = nativeLibraryPath; |
| 3905 | } else { |
| 3906 | pkg.applicationInfo.nativeLibraryDir = pkgSetting.nativeLibraryPathString; |
| 3907 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3908 | } |
| 3909 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3910 | pkgSetting.uidError = uidError; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3911 | } |
| 3912 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3913 | String path = scanFile.getPath(); |
| 3914 | /* Note: We don't want to unpack the native binaries for |
| 3915 | * system applications, unless they have been updated |
| 3916 | * (the binaries are already under /system/lib). |
| 3917 | * Also, don't unpack libs for apps on the external card |
| 3918 | * since they should have their libraries in the ASEC |
| 3919 | * container already. |
| 3920 | * |
| 3921 | * In other words, we're going to unpack the binaries |
| 3922 | * only for non-system apps and system app upgrades. |
| 3923 | */ |
| 3924 | if (pkg.applicationInfo.nativeLibraryDir != null) { |
| 3925 | try { |
| 3926 | final File nativeLibraryDir = new File(pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 5832ead | 2011-08-02 15:47:08 -0700 | [diff] [blame] | 3927 | final String dataPathString = dataPath.getCanonicalPath(); |
Kenny Root | e68d58a | 2010-10-18 16:08:54 -0700 | [diff] [blame] | 3928 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3929 | if (isSystemApp(pkg) && !isUpdatedSystemApp(pkg)) { |
| 3930 | /* |
| 3931 | * Upgrading from a previous version of the OS sometimes |
| 3932 | * leaves native libraries in the /data/data/<app>/lib |
| 3933 | * directory for system apps even when they shouldn't be. |
| 3934 | * Recent changes in the JNI library search path |
| 3935 | * necessitates we remove those to match previous behavior. |
| 3936 | */ |
| 3937 | if (NativeLibraryHelper.removeNativeBinariesFromDirLI(nativeLibraryDir)) { |
| 3938 | Log.i(TAG, "removed obsolete native libraries for system package " |
| 3939 | + path); |
Kenny Root | 831baa2 | 2010-10-05 12:29:25 -0700 | [diff] [blame] | 3940 | } |
Kenny Root | 5832ead | 2011-08-02 15:47:08 -0700 | [diff] [blame] | 3941 | } else if (nativeLibraryDir.getParentFile().getCanonicalPath() |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3942 | .equals(dataPathString)) { |
| 3943 | /* |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 3944 | * Make sure the native library dir isn't a symlink to |
| 3945 | * something. If it is, ask installd to remove it and create |
| 3946 | * a directory so we can copy to it afterwards. |
| 3947 | */ |
| 3948 | boolean isSymLink; |
| 3949 | try { |
| 3950 | isSymLink = S_ISLNK(Libcore.os.lstat(nativeLibraryDir.getPath()).st_mode); |
| 3951 | } catch (ErrnoException e) { |
| 3952 | // This shouldn't happen, but we'll fail-safe. |
| 3953 | isSymLink = true; |
| 3954 | } |
| 3955 | if (isSymLink) { |
| 3956 | mInstaller.unlinkNativeLibraryDirectory(dataPathString); |
| 3957 | } |
| 3958 | |
| 3959 | /* |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3960 | * If this is an internal application or our |
| 3961 | * nativeLibraryPath points to our data directory, unpack |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 3962 | * the libraries if necessary. |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3963 | */ |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 3964 | NativeLibraryHelper.copyNativeBinariesIfNeededLI(scanFile, nativeLibraryDir); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3965 | } else { |
| 3966 | Slog.i(TAG, "Linking native library dir for " + path); |
| 3967 | mInstaller.linkNativeLibraryDirectory(dataPathString, |
| 3968 | pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 831baa2 | 2010-10-05 12:29:25 -0700 | [diff] [blame] | 3969 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3970 | } catch (IOException ioe) { |
| 3971 | 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] | 3972 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3973 | } |
| 3974 | pkg.mScanPath = path; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3975 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3976 | if ((scanMode&SCAN_NO_DEX) == 0) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3977 | if (performDexOptLI(pkg, forceDex, (scanMode&SCAN_DEFER_DEX) != 0) |
| 3978 | == DEX_OPT_FAILED) { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3979 | mLastScanError = PackageManager.INSTALL_FAILED_DEXOPT; |
| 3980 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3981 | } |
| 3982 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3983 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3984 | if (mFactoryTest && pkg.requestedPermissions.contains( |
| 3985 | android.Manifest.permission.FACTORY_TEST)) { |
| 3986 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_FACTORY_TEST; |
| 3987 | } |
| 3988 | |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3989 | // Request the ActivityManager to kill the process(only for existing packages) |
| 3990 | // so that we do not end up in a confused state while the user is still using the older |
| 3991 | // version of the application while the new one gets installed. |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3992 | if ((parseFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3993 | killApplication(pkg.applicationInfo.packageName, |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3994 | pkg.applicationInfo.uid); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3995 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3996 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3997 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3998 | synchronized (mPackages) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 3999 | // We don't expect installation to fail beyond this point, |
| 4000 | if ((scanMode&SCAN_MONITOR) != 0) { |
| 4001 | mAppDirs.put(pkg.mPath, pkg); |
| 4002 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4003 | // Add the new setting to mSettings |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4004 | mSettings.insertPackageSettingLPw(pkgSetting, pkg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4005 | // Add the new setting to mPackages |
Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 4006 | mPackages.put(pkg.applicationInfo.packageName, pkg); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 4007 | // Make sure we don't accidentally delete its data. |
| 4008 | mSettings.mPackagesToBeCleaned.remove(pkgName); |
| 4009 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 4010 | // Take care of first install / last update times. |
| 4011 | if (currentTime != 0) { |
| 4012 | if (pkgSetting.firstInstallTime == 0) { |
| 4013 | pkgSetting.firstInstallTime = pkgSetting.lastUpdateTime = currentTime; |
| 4014 | } else if ((scanMode&SCAN_UPDATE_TIME) != 0) { |
| 4015 | pkgSetting.lastUpdateTime = currentTime; |
| 4016 | } |
| 4017 | } else if (pkgSetting.firstInstallTime == 0) { |
| 4018 | // We need *something*. Take time time stamp of the file. |
| 4019 | pkgSetting.firstInstallTime = pkgSetting.lastUpdateTime = scanFileTime; |
| 4020 | } else if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) != 0) { |
| 4021 | if (scanFileTime != pkgSetting.timeStamp) { |
| 4022 | // A package on the system image has changed; consider this |
| 4023 | // to be an update. |
| 4024 | pkgSetting.lastUpdateTime = scanFileTime; |
| 4025 | } |
| 4026 | } |
| 4027 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4028 | int N = pkg.providers.size(); |
| 4029 | StringBuilder r = null; |
| 4030 | int i; |
| 4031 | for (i=0; i<N; i++) { |
| 4032 | PackageParser.Provider p = pkg.providers.get(i); |
| 4033 | p.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4034 | p.info.processName, pkg.applicationInfo.uid); |
| 4035 | mProvidersByComponent.put(new ComponentName(p.info.packageName, |
| 4036 | p.info.name), p); |
| 4037 | p.syncable = p.info.isSyncable; |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4038 | if (p.info.authority != null) { |
| 4039 | String names[] = p.info.authority.split(";"); |
| 4040 | p.info.authority = null; |
| 4041 | for (int j = 0; j < names.length; j++) { |
| 4042 | if (j == 1 && p.syncable) { |
| 4043 | // We only want the first authority for a provider to possibly be |
| 4044 | // syncable, so if we already added this provider using a different |
| 4045 | // authority clear the syncable flag. We copy the provider before |
| 4046 | // changing it because the mProviders object contains a reference |
| 4047 | // to a provider that we don't want to change. |
| 4048 | // Only do this for the second authority since the resulting provider |
| 4049 | // object can be the same for all future authorities for this provider. |
| 4050 | p = new PackageParser.Provider(p); |
| 4051 | p.syncable = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4052 | } |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4053 | if (!mProviders.containsKey(names[j])) { |
| 4054 | mProviders.put(names[j], p); |
| 4055 | if (p.info.authority == null) { |
| 4056 | p.info.authority = names[j]; |
| 4057 | } else { |
| 4058 | p.info.authority = p.info.authority + ";" + names[j]; |
| 4059 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4060 | if (DEBUG_PACKAGE_SCANNING) { |
| 4061 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 4062 | Log.d(TAG, "Registered content provider: " + names[j] |
| 4063 | + ", className = " + p.info.name + ", isSyncable = " |
| 4064 | + p.info.isSyncable); |
| 4065 | } |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4066 | } else { |
| 4067 | PackageParser.Provider other = mProviders.get(names[j]); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4068 | Slog.w(TAG, "Skipping provider name " + names[j] + |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4069 | " (in package " + pkg.applicationInfo.packageName + |
| 4070 | "): name already used by " |
| 4071 | + ((other != null && other.getComponentName() != null) |
| 4072 | ? other.getComponentName().getPackageName() : "?")); |
| 4073 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4074 | } |
| 4075 | } |
| 4076 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4077 | if (r == null) { |
| 4078 | r = new StringBuilder(256); |
| 4079 | } else { |
| 4080 | r.append(' '); |
| 4081 | } |
| 4082 | r.append(p.info.name); |
| 4083 | } |
| 4084 | } |
| 4085 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4086 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Providers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4087 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4088 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4089 | N = pkg.services.size(); |
| 4090 | r = null; |
| 4091 | for (i=0; i<N; i++) { |
| 4092 | PackageParser.Service s = pkg.services.get(i); |
| 4093 | s.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4094 | s.info.processName, pkg.applicationInfo.uid); |
| 4095 | mServices.addService(s); |
| 4096 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4097 | if (r == null) { |
| 4098 | r = new StringBuilder(256); |
| 4099 | } else { |
| 4100 | r.append(' '); |
| 4101 | } |
| 4102 | r.append(s.info.name); |
| 4103 | } |
| 4104 | } |
| 4105 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4106 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Services: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4107 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4109 | N = pkg.receivers.size(); |
| 4110 | r = null; |
| 4111 | for (i=0; i<N; i++) { |
| 4112 | PackageParser.Activity a = pkg.receivers.get(i); |
| 4113 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4114 | a.info.processName, pkg.applicationInfo.uid); |
| 4115 | mReceivers.addActivity(a, "receiver"); |
| 4116 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4117 | if (r == null) { |
| 4118 | r = new StringBuilder(256); |
| 4119 | } else { |
| 4120 | r.append(' '); |
| 4121 | } |
| 4122 | r.append(a.info.name); |
| 4123 | } |
| 4124 | } |
| 4125 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4126 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Receivers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4127 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4129 | N = pkg.activities.size(); |
| 4130 | r = null; |
| 4131 | for (i=0; i<N; i++) { |
| 4132 | PackageParser.Activity a = pkg.activities.get(i); |
| 4133 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4134 | a.info.processName, pkg.applicationInfo.uid); |
| 4135 | mActivities.addActivity(a, "activity"); |
| 4136 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4137 | if (r == null) { |
| 4138 | r = new StringBuilder(256); |
| 4139 | } else { |
| 4140 | r.append(' '); |
| 4141 | } |
| 4142 | r.append(a.info.name); |
| 4143 | } |
| 4144 | } |
| 4145 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4146 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Activities: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4147 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4149 | N = pkg.permissionGroups.size(); |
| 4150 | r = null; |
| 4151 | for (i=0; i<N; i++) { |
| 4152 | PackageParser.PermissionGroup pg = pkg.permissionGroups.get(i); |
| 4153 | PackageParser.PermissionGroup cur = mPermissionGroups.get(pg.info.name); |
| 4154 | if (cur == null) { |
| 4155 | mPermissionGroups.put(pg.info.name, pg); |
| 4156 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4157 | if (r == null) { |
| 4158 | r = new StringBuilder(256); |
| 4159 | } else { |
| 4160 | r.append(' '); |
| 4161 | } |
| 4162 | r.append(pg.info.name); |
| 4163 | } |
| 4164 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4165 | 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] | 4166 | + pg.info.packageName + " ignored: original from " |
| 4167 | + cur.info.packageName); |
| 4168 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4169 | if (r == null) { |
| 4170 | r = new StringBuilder(256); |
| 4171 | } else { |
| 4172 | r.append(' '); |
| 4173 | } |
| 4174 | r.append("DUP:"); |
| 4175 | r.append(pg.info.name); |
| 4176 | } |
| 4177 | } |
| 4178 | } |
| 4179 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4180 | 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] | 4181 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4182 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4183 | N = pkg.permissions.size(); |
| 4184 | r = null; |
| 4185 | for (i=0; i<N; i++) { |
| 4186 | PackageParser.Permission p = pkg.permissions.get(i); |
| 4187 | HashMap<String, BasePermission> permissionMap = |
| 4188 | p.tree ? mSettings.mPermissionTrees |
| 4189 | : mSettings.mPermissions; |
| 4190 | p.group = mPermissionGroups.get(p.info.group); |
| 4191 | if (p.info.group == null || p.group != null) { |
| 4192 | BasePermission bp = permissionMap.get(p.info.name); |
| 4193 | if (bp == null) { |
| 4194 | bp = new BasePermission(p.info.name, p.info.packageName, |
| 4195 | BasePermission.TYPE_NORMAL); |
| 4196 | permissionMap.put(p.info.name, bp); |
| 4197 | } |
| 4198 | if (bp.perm == null) { |
| 4199 | if (bp.sourcePackage == null |
| 4200 | || bp.sourcePackage.equals(p.info.packageName)) { |
| 4201 | BasePermission tree = findPermissionTreeLP(p.info.name); |
| 4202 | if (tree == null |
| 4203 | || tree.sourcePackage.equals(p.info.packageName)) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4204 | bp.packageSetting = pkgSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4205 | bp.perm = p; |
| 4206 | bp.uid = pkg.applicationInfo.uid; |
| 4207 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4208 | if (r == null) { |
| 4209 | r = new StringBuilder(256); |
| 4210 | } else { |
| 4211 | r.append(' '); |
| 4212 | } |
| 4213 | r.append(p.info.name); |
| 4214 | } |
| 4215 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4216 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4217 | + p.info.packageName + " ignored: base tree " |
| 4218 | + tree.name + " is from package " |
| 4219 | + tree.sourcePackage); |
| 4220 | } |
| 4221 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4222 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4223 | + p.info.packageName + " ignored: original from " |
| 4224 | + bp.sourcePackage); |
| 4225 | } |
| 4226 | } else if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4227 | if (r == null) { |
| 4228 | r = new StringBuilder(256); |
| 4229 | } else { |
| 4230 | r.append(' '); |
| 4231 | } |
| 4232 | r.append("DUP:"); |
| 4233 | r.append(p.info.name); |
| 4234 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4235 | if (bp.perm == p) { |
| 4236 | bp.protectionLevel = p.info.protectionLevel; |
| 4237 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4238 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4239 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4240 | + p.info.packageName + " ignored: no group " |
| 4241 | + p.group); |
| 4242 | } |
| 4243 | } |
| 4244 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4245 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Permissions: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4246 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4248 | N = pkg.instrumentation.size(); |
| 4249 | r = null; |
| 4250 | for (i=0; i<N; i++) { |
| 4251 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
| 4252 | a.info.packageName = pkg.applicationInfo.packageName; |
| 4253 | a.info.sourceDir = pkg.applicationInfo.sourceDir; |
| 4254 | a.info.publicSourceDir = pkg.applicationInfo.publicSourceDir; |
| 4255 | a.info.dataDir = pkg.applicationInfo.dataDir; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4256 | a.info.nativeLibraryDir = pkg.applicationInfo.nativeLibraryDir; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4257 | mInstrumentation.put(a.getComponentName(), a); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4258 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4259 | if (r == null) { |
| 4260 | r = new StringBuilder(256); |
| 4261 | } else { |
| 4262 | r.append(' '); |
| 4263 | } |
| 4264 | r.append(a.info.name); |
| 4265 | } |
| 4266 | } |
| 4267 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4268 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Instrumentation: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4269 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4270 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 4271 | if (pkg.protectedBroadcasts != null) { |
| 4272 | N = pkg.protectedBroadcasts.size(); |
| 4273 | for (i=0; i<N; i++) { |
| 4274 | mProtectedBroadcasts.add(pkg.protectedBroadcasts.get(i)); |
| 4275 | } |
| 4276 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4278 | pkgSetting.setTimeStamp(scanFileTime); |
| 4279 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4280 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4281 | return pkg; |
| 4282 | } |
| 4283 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4284 | private void killApplication(String pkgName, int uid) { |
| 4285 | // Request the ActivityManager to kill the process(only for existing packages) |
| 4286 | // so that we do not end up in a confused state while the user is still using the older |
| 4287 | // version of the application while the new one gets installed. |
| 4288 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 4289 | if (am != null) { |
| 4290 | try { |
| 4291 | am.killApplicationWithUid(pkgName, uid); |
| 4292 | } catch (RemoteException e) { |
| 4293 | } |
| 4294 | } |
| 4295 | } |
| 4296 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4297 | void removePackageLI(PackageParser.Package pkg, boolean chatty) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4298 | if (DEBUG_INSTALL) { |
| 4299 | if (chatty) |
| 4300 | Log.d(TAG, "Removing package " + pkg.applicationInfo.packageName); |
| 4301 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4302 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4303 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4304 | synchronized (mPackages) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4305 | mPackages.remove(pkg.applicationInfo.packageName); |
| 4306 | if (pkg.mPath != null) { |
| 4307 | mAppDirs.remove(pkg.mPath); |
| 4308 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4310 | int N = pkg.providers.size(); |
| 4311 | StringBuilder r = null; |
| 4312 | int i; |
| 4313 | for (i=0; i<N; i++) { |
| 4314 | PackageParser.Provider p = pkg.providers.get(i); |
| 4315 | mProvidersByComponent.remove(new ComponentName(p.info.packageName, |
| 4316 | p.info.name)); |
| 4317 | if (p.info.authority == null) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4318 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4319 | /* The is another ContentProvider with this authority when |
| 4320 | * this app was installed so this authority is null, |
| 4321 | * Ignore it as we don't have to unregister the provider. |
| 4322 | */ |
| 4323 | continue; |
| 4324 | } |
| 4325 | String names[] = p.info.authority.split(";"); |
| 4326 | for (int j = 0; j < names.length; j++) { |
| 4327 | if (mProviders.get(names[j]) == p) { |
| 4328 | mProviders.remove(names[j]); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4329 | if (DEBUG_REMOVE) { |
| 4330 | if (chatty) |
| 4331 | Log.d(TAG, "Unregistered content provider: " + names[j] |
| 4332 | + ", className = " + p.info.name + ", isSyncable = " |
| 4333 | + p.info.isSyncable); |
| 4334 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4335 | } |
| 4336 | } |
| 4337 | if (chatty) { |
| 4338 | if (r == null) { |
| 4339 | r = new StringBuilder(256); |
| 4340 | } else { |
| 4341 | r.append(' '); |
| 4342 | } |
| 4343 | r.append(p.info.name); |
| 4344 | } |
| 4345 | } |
| 4346 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4347 | if (DEBUG_REMOVE) Log.d(TAG, " Providers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4348 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4350 | N = pkg.services.size(); |
| 4351 | r = null; |
| 4352 | for (i=0; i<N; i++) { |
| 4353 | PackageParser.Service s = pkg.services.get(i); |
| 4354 | mServices.removeService(s); |
| 4355 | if (chatty) { |
| 4356 | if (r == null) { |
| 4357 | r = new StringBuilder(256); |
| 4358 | } else { |
| 4359 | r.append(' '); |
| 4360 | } |
| 4361 | r.append(s.info.name); |
| 4362 | } |
| 4363 | } |
| 4364 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4365 | if (DEBUG_REMOVE) Log.d(TAG, " Services: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4366 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4368 | N = pkg.receivers.size(); |
| 4369 | r = null; |
| 4370 | for (i=0; i<N; i++) { |
| 4371 | PackageParser.Activity a = pkg.receivers.get(i); |
| 4372 | mReceivers.removeActivity(a, "receiver"); |
| 4373 | if (chatty) { |
| 4374 | if (r == null) { |
| 4375 | r = new StringBuilder(256); |
| 4376 | } else { |
| 4377 | r.append(' '); |
| 4378 | } |
| 4379 | r.append(a.info.name); |
| 4380 | } |
| 4381 | } |
| 4382 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4383 | if (DEBUG_REMOVE) Log.d(TAG, " Receivers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | N = pkg.activities.size(); |
| 4387 | r = null; |
| 4388 | for (i=0; i<N; i++) { |
| 4389 | PackageParser.Activity a = pkg.activities.get(i); |
| 4390 | mActivities.removeActivity(a, "activity"); |
| 4391 | if (chatty) { |
| 4392 | if (r == null) { |
| 4393 | r = new StringBuilder(256); |
| 4394 | } else { |
| 4395 | r.append(' '); |
| 4396 | } |
| 4397 | r.append(a.info.name); |
| 4398 | } |
| 4399 | } |
| 4400 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4401 | if (DEBUG_REMOVE) Log.d(TAG, " Activities: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4402 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4404 | N = pkg.permissions.size(); |
| 4405 | r = null; |
| 4406 | for (i=0; i<N; i++) { |
| 4407 | PackageParser.Permission p = pkg.permissions.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4408 | BasePermission bp = mSettings.mPermissions.get(p.info.name); |
| 4409 | if (bp == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4410 | bp = mSettings.mPermissionTrees.get(p.info.name); |
| 4411 | } |
| 4412 | if (bp != null && bp.perm == p) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4413 | bp.perm = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4414 | if (chatty) { |
| 4415 | if (r == null) { |
| 4416 | r = new StringBuilder(256); |
| 4417 | } else { |
| 4418 | r.append(' '); |
| 4419 | } |
| 4420 | r.append(p.info.name); |
| 4421 | } |
| 4422 | } |
| 4423 | } |
| 4424 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4425 | if (DEBUG_REMOVE) Log.d(TAG, " Permissions: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4426 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4427 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4428 | N = pkg.instrumentation.size(); |
| 4429 | r = null; |
| 4430 | for (i=0; i<N; i++) { |
| 4431 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4432 | mInstrumentation.remove(a.getComponentName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4433 | if (chatty) { |
| 4434 | if (r == null) { |
| 4435 | r = new StringBuilder(256); |
| 4436 | } else { |
| 4437 | r.append(' '); |
| 4438 | } |
| 4439 | r.append(a.info.name); |
| 4440 | } |
| 4441 | } |
| 4442 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4443 | if (DEBUG_REMOVE) Log.d(TAG, " Instrumentation: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4444 | } |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | private static final boolean isPackageFilename(String name) { |
| 4449 | return name != null && name.endsWith(".apk"); |
| 4450 | } |
| 4451 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4452 | private static boolean hasPermission(PackageParser.Package pkgInfo, String perm) { |
| 4453 | for (int i=pkgInfo.permissions.size()-1; i>=0; i--) { |
| 4454 | if (pkgInfo.permissions.get(i).info.name.equals(perm)) { |
| 4455 | return true; |
| 4456 | } |
| 4457 | } |
| 4458 | return false; |
| 4459 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4460 | |
| 4461 | static final int UPDATE_PERMISSIONS_ALL = 1<<0; |
| 4462 | static final int UPDATE_PERMISSIONS_REPLACE_PKG = 1<<1; |
| 4463 | static final int UPDATE_PERMISSIONS_REPLACE_ALL = 1<<2; |
| 4464 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4465 | private void updatePermissionsLPw(String changingPkg, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4466 | PackageParser.Package pkgInfo, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4467 | // Make sure there are no dangling permission trees. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4468 | Iterator<BasePermission> it = mSettings.mPermissionTrees.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4469 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4470 | final BasePermission bp = it.next(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4471 | if (bp.packageSetting == null) { |
| 4472 | // We may not yet have parsed the package, so just see if |
| 4473 | // we still know about its settings. |
| 4474 | bp.packageSetting = mSettings.mPackages.get(bp.sourcePackage); |
| 4475 | } |
| 4476 | if (bp.packageSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4477 | Slog.w(TAG, "Removing dangling permission tree: " + bp.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4478 | + " from package " + bp.sourcePackage); |
| 4479 | it.remove(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4480 | } else if (changingPkg != null && changingPkg.equals(bp.sourcePackage)) { |
| 4481 | if (pkgInfo == null || !hasPermission(pkgInfo, bp.name)) { |
| 4482 | Slog.i(TAG, "Removing old permission tree: " + bp.name |
| 4483 | + " from package " + bp.sourcePackage); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4484 | flags |= UPDATE_PERMISSIONS_ALL; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4485 | it.remove(); |
| 4486 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | // Make sure all dynamic permissions have been assigned to a package, |
| 4491 | // and make sure there are no dangling permissions. |
| 4492 | it = mSettings.mPermissions.values().iterator(); |
| 4493 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4494 | final BasePermission bp = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4495 | if (bp.type == BasePermission.TYPE_DYNAMIC) { |
| 4496 | if (DEBUG_SETTINGS) Log.v(TAG, "Dynamic permission: name=" |
| 4497 | + bp.name + " pkg=" + bp.sourcePackage |
| 4498 | + " info=" + bp.pendingInfo); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4499 | if (bp.packageSetting == null && bp.pendingInfo != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4500 | final BasePermission tree = findPermissionTreeLP(bp.name); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 4501 | if (tree != null && tree.perm != null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4502 | bp.packageSetting = tree.packageSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4503 | bp.perm = new PackageParser.Permission(tree.perm.owner, |
| 4504 | new PermissionInfo(bp.pendingInfo)); |
| 4505 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 4506 | bp.perm.info.name = bp.name; |
| 4507 | bp.uid = tree.uid; |
| 4508 | } |
| 4509 | } |
| 4510 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4511 | if (bp.packageSetting == null) { |
| 4512 | // We may not yet have parsed the package, so just see if |
| 4513 | // we still know about its settings. |
| 4514 | bp.packageSetting = mSettings.mPackages.get(bp.sourcePackage); |
| 4515 | } |
| 4516 | if (bp.packageSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4517 | Slog.w(TAG, "Removing dangling permission: " + bp.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4518 | + " from package " + bp.sourcePackage); |
| 4519 | it.remove(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4520 | } else if (changingPkg != null && changingPkg.equals(bp.sourcePackage)) { |
| 4521 | if (pkgInfo == null || !hasPermission(pkgInfo, bp.name)) { |
| 4522 | Slog.i(TAG, "Removing old permission: " + bp.name |
| 4523 | + " from package " + bp.sourcePackage); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4524 | flags |= UPDATE_PERMISSIONS_ALL; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4525 | it.remove(); |
| 4526 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | // Now update the permissions for all packages, in particular |
| 4531 | // replace the granted permissions of the system packages. |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4532 | if ((flags&UPDATE_PERMISSIONS_ALL) != 0) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4533 | for (PackageParser.Package pkg : mPackages.values()) { |
| 4534 | if (pkg != pkgInfo) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4535 | grantPermissionsLPw(pkg, (flags&UPDATE_PERMISSIONS_REPLACE_ALL) != 0); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | } |
| 4539 | |
| 4540 | if (pkgInfo != null) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4541 | grantPermissionsLPw(pkgInfo, (flags&UPDATE_PERMISSIONS_REPLACE_PKG) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4542 | } |
| 4543 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4544 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4545 | private void grantPermissionsLPw(PackageParser.Package pkg, boolean replace) { |
| 4546 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4547 | if (ps == null) { |
| 4548 | return; |
| 4549 | } |
| 4550 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4551 | HashSet<String> origPermissions = gp.grantedPermissions; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4552 | boolean changedPermission = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4553 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4554 | if (replace) { |
| 4555 | ps.permissionsFixed = false; |
| 4556 | if (gp == ps) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4557 | origPermissions = new HashSet<String>(gp.grantedPermissions); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4558 | gp.grantedPermissions.clear(); |
| 4559 | gp.gids = mGlobalGids; |
| 4560 | } |
| 4561 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4562 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4563 | if (gp.gids == null) { |
| 4564 | gp.gids = mGlobalGids; |
| 4565 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4567 | final int N = pkg.requestedPermissions.size(); |
| 4568 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4569 | final String name = pkg.requestedPermissions.get(i); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4570 | //final boolean required = pkg.requestedPermssionsRequired.get(i); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4571 | final BasePermission bp = mSettings.mPermissions.get(name); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4572 | if (DEBUG_INSTALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4573 | if (gp != ps) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4574 | Log.i(TAG, "Package " + pkg.packageName + " checking " + name + ": " + bp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4575 | } |
| 4576 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4577 | if (bp != null && bp.packageSetting != null) { |
| 4578 | final String perm = bp.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4579 | boolean allowed; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4580 | boolean allowedSig = false; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4581 | final int level = bp.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE; |
| 4582 | if (level == PermissionInfo.PROTECTION_NORMAL |
| 4583 | || level == PermissionInfo.PROTECTION_DANGEROUS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4584 | allowed = true; |
Dianne Hackborn | 6e52b5d | 2010-04-05 14:33:01 -0700 | [diff] [blame] | 4585 | } else if (bp.packageSetting == null) { |
| 4586 | // This permission is invalid; skip it. |
| 4587 | allowed = false; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4588 | } else if (level == PermissionInfo.PROTECTION_SIGNATURE) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4589 | allowed = (compareSignatures( |
Dianne Hackborn | 2fe979f | 2010-10-08 15:07:17 -0700 | [diff] [blame] | 4590 | bp.packageSetting.signatures.mSignatures, pkg.mSignatures) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4591 | == PackageManager.SIGNATURE_MATCH) |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4592 | || (compareSignatures(mPlatformPackage.mSignatures, pkg.mSignatures) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4593 | == PackageManager.SIGNATURE_MATCH); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4594 | if (!allowed && (bp.protectionLevel |
| 4595 | & PermissionInfo.PROTECTION_FLAG_SYSTEM) != 0) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4596 | if (isSystemApp(pkg)) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4597 | // For updated system applications, a system permission |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4598 | // is granted only if it had been defined by the original application. |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4599 | if (isUpdatedSystemApp(pkg)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4600 | final PackageSetting sysPs = mSettings |
| 4601 | .getDisabledSystemPkgLPr(pkg.packageName); |
Dianne Hackborn | 2fe979f | 2010-10-08 15:07:17 -0700 | [diff] [blame] | 4602 | final GrantedPermissions origGp = sysPs.sharedUser != null |
| 4603 | ? sysPs.sharedUser : sysPs; |
| 4604 | if (origGp.grantedPermissions.contains(perm)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4605 | allowed = true; |
| 4606 | } else { |
| 4607 | allowed = false; |
| 4608 | } |
| 4609 | } else { |
| 4610 | allowed = true; |
| 4611 | } |
| 4612 | } |
| 4613 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4614 | if (!allowed && (bp.protectionLevel |
| 4615 | & PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) { |
| 4616 | // For development permissions, a development permission |
| 4617 | // is granted only if it was already granted. |
| 4618 | if (origPermissions.contains(perm)) { |
| 4619 | allowed = true; |
| 4620 | } else { |
| 4621 | allowed = false; |
| 4622 | } |
| 4623 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4624 | if (allowed) { |
| 4625 | allowedSig = true; |
| 4626 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4627 | } else { |
| 4628 | allowed = false; |
| 4629 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4630 | if (DEBUG_INSTALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4631 | if (gp != ps) { |
| 4632 | Log.i(TAG, "Package " + pkg.packageName + " granting " + perm); |
| 4633 | } |
| 4634 | } |
| 4635 | if (allowed) { |
| 4636 | if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0 |
| 4637 | && ps.permissionsFixed) { |
| 4638 | // If this is an existing, non-system package, then |
| 4639 | // we can't add any new permissions to it. |
Dianne Hackborn | f657b63 | 2010-03-22 18:08:07 -0700 | [diff] [blame] | 4640 | if (!allowedSig && !gp.grantedPermissions.contains(perm)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4641 | allowed = false; |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 4642 | // Except... if this is a permission that was added |
| 4643 | // to the platform (note: need to only do this when |
| 4644 | // updating the platform). |
| 4645 | final int NP = PackageParser.NEW_PERMISSIONS.length; |
| 4646 | for (int ip=0; ip<NP; ip++) { |
| 4647 | final PackageParser.NewPermissionInfo npi |
| 4648 | = PackageParser.NEW_PERMISSIONS[ip]; |
| 4649 | if (npi.name.equals(perm) |
| 4650 | && pkg.applicationInfo.targetSdkVersion < npi.sdkVersion) { |
| 4651 | allowed = true; |
Dianne Hackborn | f657b63 | 2010-03-22 18:08:07 -0700 | [diff] [blame] | 4652 | Log.i(TAG, "Auto-granting " + perm + " to old pkg " |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 4653 | + pkg.packageName); |
| 4654 | break; |
| 4655 | } |
| 4656 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4657 | } |
| 4658 | } |
| 4659 | if (allowed) { |
| 4660 | if (!gp.grantedPermissions.contains(perm)) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4661 | changedPermission = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4662 | gp.grantedPermissions.add(perm); |
| 4663 | gp.gids = appendInts(gp.gids, bp.gids); |
Dianne Hackborn | bbfd58a | 2010-03-23 11:49:59 -0700 | [diff] [blame] | 4664 | } else if (!ps.haveGids) { |
| 4665 | gp.gids = appendInts(gp.gids, bp.gids); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4666 | } |
| 4667 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4668 | Slog.w(TAG, "Not granting permission " + perm |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4669 | + " to package " + pkg.packageName |
| 4670 | + " because it was previously installed without"); |
| 4671 | } |
| 4672 | } else { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4673 | if (gp.grantedPermissions.remove(perm)) { |
| 4674 | changedPermission = true; |
| 4675 | gp.gids = removeInts(gp.gids, bp.gids); |
| 4676 | Slog.i(TAG, "Un-granting permission " + perm |
| 4677 | + " from package " + pkg.packageName |
| 4678 | + " (protectionLevel=" + bp.protectionLevel |
| 4679 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 4680 | + ")"); |
| 4681 | } else { |
| 4682 | Slog.w(TAG, "Not granting permission " + perm |
| 4683 | + " to package " + pkg.packageName |
| 4684 | + " (protectionLevel=" + bp.protectionLevel |
| 4685 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 4686 | + ")"); |
| 4687 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4688 | } |
| 4689 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4690 | Slog.w(TAG, "Unknown permission " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4691 | + " in package " + pkg.packageName); |
| 4692 | } |
| 4693 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4694 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4695 | if ((changedPermission || replace) && !ps.permissionsFixed && |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 4696 | ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) || |
| 4697 | ((ps.pkgFlags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)){ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4698 | // This is the first that we have heard about this package, so the |
| 4699 | // permissions we have now selected are fixed until explicitly |
| 4700 | // changed. |
| 4701 | ps.permissionsFixed = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4702 | } |
Dianne Hackborn | bbfd58a | 2010-03-23 11:49:59 -0700 | [diff] [blame] | 4703 | ps.haveGids = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4704 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4706 | private final class ActivityIntentResolver |
| 4707 | extends IntentResolver<PackageParser.ActivityIntentInfo, ResolveInfo> { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4708 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4709 | boolean defaultOnly, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4710 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4711 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4712 | return super.queryIntent(intent, resolvedType, defaultOnly, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4713 | } |
| 4714 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4715 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, int flags, |
| 4716 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4717 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4718 | mFlags = flags; |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4719 | return super.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4720 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4721 | } |
| 4722 | |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4723 | public List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4724 | int flags, ArrayList<PackageParser.Activity> packageActivities, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4725 | if (!sUserManager.exists(userId)) return null; |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4726 | if (packageActivities == null) { |
| 4727 | return null; |
| 4728 | } |
| 4729 | mFlags = flags; |
| 4730 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4731 | final int N = packageActivities.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4732 | ArrayList<ArrayList<PackageParser.ActivityIntentInfo>> listCut = |
| 4733 | new ArrayList<ArrayList<PackageParser.ActivityIntentInfo>>(N); |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 4734 | |
| 4735 | ArrayList<PackageParser.ActivityIntentInfo> intentFilters; |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4736 | for (int i = 0; i < N; ++i) { |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 4737 | intentFilters = packageActivities.get(i).intents; |
| 4738 | if (intentFilters != null && intentFilters.size() > 0) { |
| 4739 | listCut.add(intentFilters); |
| 4740 | } |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4741 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4742 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4743 | } |
| 4744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4745 | public final void addActivity(PackageParser.Activity a, String type) { |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 4746 | final boolean systemApp = isSystemApp(a.info.applicationInfo); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4747 | mActivities.put(a.getComponentName(), a); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4748 | if (DEBUG_SHOW_INFO) |
| 4749 | Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4750 | TAG, " " + type + " " + |
| 4751 | (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel : a.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4752 | if (DEBUG_SHOW_INFO) |
| 4753 | Log.v(TAG, " Class=" + a.info.name); |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4754 | final int NI = a.intents.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4755 | for (int j=0; j<NI; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4756 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 4757 | if (!systemApp && intent.getPriority() > 0 && "activity".equals(type)) { |
| 4758 | intent.setPriority(0); |
| 4759 | Log.w(TAG, "Package " + a.info.applicationInfo.packageName + " has activity " |
| 4760 | + a.className + " with priority > 0, forcing to 0"); |
| 4761 | } |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4762 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4763 | Log.v(TAG, " IntentFilter:"); |
| 4764 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4765 | } |
| 4766 | if (!intent.debugCheck()) { |
| 4767 | Log.w(TAG, "==> For Activity " + a.info.name); |
| 4768 | } |
| 4769 | addFilter(intent); |
| 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | public final void removeActivity(PackageParser.Activity a, String type) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4774 | mActivities.remove(a.getComponentName()); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4775 | if (DEBUG_SHOW_INFO) { |
| 4776 | Log.v(TAG, " " + type + " " |
| 4777 | + (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel |
| 4778 | : a.info.name) + ":"); |
| 4779 | Log.v(TAG, " Class=" + a.info.name); |
| 4780 | } |
| 4781 | final int NI = a.intents.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4782 | for (int j=0; j<NI; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4783 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4784 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4785 | Log.v(TAG, " IntentFilter:"); |
| 4786 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4787 | } |
| 4788 | removeFilter(intent); |
| 4789 | } |
| 4790 | } |
| 4791 | |
| 4792 | @Override |
| 4793 | protected boolean allowFilterResult( |
| 4794 | PackageParser.ActivityIntentInfo filter, List<ResolveInfo> dest) { |
| 4795 | ActivityInfo filterAi = filter.activity.info; |
| 4796 | for (int i=dest.size()-1; i>=0; i--) { |
| 4797 | ActivityInfo destAi = dest.get(i).activityInfo; |
| 4798 | if (destAi.name == filterAi.name |
| 4799 | && destAi.packageName == filterAi.packageName) { |
| 4800 | return false; |
| 4801 | } |
| 4802 | } |
| 4803 | return true; |
| 4804 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4805 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4806 | @Override |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4807 | protected boolean isFilterStopped(PackageParser.ActivityIntentInfo filter, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4808 | if (!sUserManager.exists(userId)) return true; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 4809 | PackageParser.Package p = filter.activity.owner; |
| 4810 | if (p != null) { |
| 4811 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 4812 | if (ps != null) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 4813 | // System apps are never considered stopped for purposes of |
| 4814 | // filtering, because there may be no way for the user to |
| 4815 | // actually re-launch them. |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4816 | return ps.getStopped(userId) && (ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 4817 | } |
| 4818 | } |
| 4819 | return false; |
| 4820 | } |
| 4821 | |
| 4822 | @Override |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4823 | protected String packageForFilter(PackageParser.ActivityIntentInfo info) { |
| 4824 | return info.activity.owner.packageName; |
| 4825 | } |
| 4826 | |
| 4827 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4828 | protected ResolveInfo newResult(PackageParser.ActivityIntentInfo info, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4829 | int match, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4830 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4831 | if (!mSettings.isEnabledLPr(info.activity.info, mFlags, userId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4832 | return null; |
| 4833 | } |
| 4834 | final PackageParser.Activity activity = info.activity; |
| 4835 | if (mSafeMode && (activity.info.applicationInfo.flags |
| 4836 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 4837 | return null; |
| 4838 | } |
| 4839 | final ResolveInfo res = new ResolveInfo(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4840 | PackageSetting ps = (PackageSetting) activity.owner.mExtras; |
| 4841 | res.activityInfo = PackageParser.generateActivityInfo(activity, mFlags, |
| 4842 | ps != null ? ps.getStopped(userId) : false, |
| 4843 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 4844 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4845 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 4846 | res.filter = info; |
| 4847 | } |
| 4848 | res.priority = info.getPriority(); |
| 4849 | res.preferredOrder = activity.owner.mPreferredOrder; |
| 4850 | //System.out.println("Result: " + res.activityInfo.className + |
| 4851 | // " = " + res.priority); |
| 4852 | res.match = match; |
| 4853 | res.isDefault = info.hasDefault; |
| 4854 | res.labelRes = info.labelRes; |
| 4855 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 4856 | res.icon = info.icon; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 4857 | res.system = isSystemApp(res.activityInfo.applicationInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4858 | return res; |
| 4859 | } |
| 4860 | |
| 4861 | @Override |
| 4862 | protected void sortResults(List<ResolveInfo> results) { |
| 4863 | Collections.sort(results, mResolvePrioritySorter); |
| 4864 | } |
| 4865 | |
| 4866 | @Override |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4867 | protected void dumpFilter(PrintWriter out, String prefix, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4868 | PackageParser.ActivityIntentInfo filter) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4869 | out.print(prefix); out.print( |
| 4870 | Integer.toHexString(System.identityHashCode(filter.activity))); |
| 4871 | out.print(' '); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4872 | out.print(filter.activity.getComponentShortName()); |
| 4873 | out.print(" filter "); |
| 4874 | out.println(Integer.toHexString(System.identityHashCode(filter))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 4878 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 4879 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 4880 | // while (i.hasNext()) { |
| 4881 | // final ResolveInfo resolveInfo = i.next(); |
| 4882 | // if (isEnabledLP(resolveInfo.activityInfo)) { |
| 4883 | // retList.add(resolveInfo); |
| 4884 | // } |
| 4885 | // } |
| 4886 | // return retList; |
| 4887 | // } |
| 4888 | |
| 4889 | // Keys are String (activity class name), values are Activity. |
| 4890 | private final HashMap<ComponentName, PackageParser.Activity> mActivities |
| 4891 | = new HashMap<ComponentName, PackageParser.Activity>(); |
| 4892 | private int mFlags; |
| 4893 | } |
| 4894 | |
| 4895 | private final class ServiceIntentResolver |
| 4896 | extends IntentResolver<PackageParser.ServiceIntentInfo, ResolveInfo> { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4897 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4898 | boolean defaultOnly, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4899 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4900 | return super.queryIntent(intent, resolvedType, defaultOnly, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4901 | } |
| 4902 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4903 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, int flags, |
| 4904 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4905 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4906 | mFlags = flags; |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4907 | return super.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4908 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4909 | } |
| 4910 | |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4911 | public List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4912 | int flags, ArrayList<PackageParser.Service> packageServices, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4913 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4914 | if (packageServices == null) { |
| 4915 | return null; |
| 4916 | } |
| 4917 | mFlags = flags; |
| 4918 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4919 | final int N = packageServices.size(); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4920 | ArrayList<ArrayList<PackageParser.ServiceIntentInfo>> listCut = |
| 4921 | new ArrayList<ArrayList<PackageParser.ServiceIntentInfo>>(N); |
| 4922 | |
| 4923 | ArrayList<PackageParser.ServiceIntentInfo> intentFilters; |
| 4924 | for (int i = 0; i < N; ++i) { |
| 4925 | intentFilters = packageServices.get(i).intents; |
| 4926 | if (intentFilters != null && intentFilters.size() > 0) { |
| 4927 | listCut.add(intentFilters); |
| 4928 | } |
| 4929 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4930 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4931 | } |
| 4932 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4933 | public final void addService(PackageParser.Service s) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4934 | mServices.put(s.getComponentName(), s); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4935 | if (DEBUG_SHOW_INFO) { |
| 4936 | Log.v(TAG, " " |
| 4937 | + (s.info.nonLocalizedLabel != null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4938 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4939 | Log.v(TAG, " Class=" + s.info.name); |
| 4940 | } |
| 4941 | final int NI = s.intents.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4942 | int j; |
| 4943 | for (j=0; j<NI; j++) { |
| 4944 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4945 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4946 | Log.v(TAG, " IntentFilter:"); |
| 4947 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4948 | } |
| 4949 | if (!intent.debugCheck()) { |
| 4950 | Log.w(TAG, "==> For Service " + s.info.name); |
| 4951 | } |
| 4952 | addFilter(intent); |
| 4953 | } |
| 4954 | } |
| 4955 | |
| 4956 | public final void removeService(PackageParser.Service s) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4957 | mServices.remove(s.getComponentName()); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4958 | if (DEBUG_SHOW_INFO) { |
| 4959 | Log.v(TAG, " " + (s.info.nonLocalizedLabel != null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4960 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4961 | Log.v(TAG, " Class=" + s.info.name); |
| 4962 | } |
| 4963 | final int NI = s.intents.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4964 | int j; |
| 4965 | for (j=0; j<NI; j++) { |
| 4966 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4967 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4968 | Log.v(TAG, " IntentFilter:"); |
| 4969 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4970 | } |
| 4971 | removeFilter(intent); |
| 4972 | } |
| 4973 | } |
| 4974 | |
| 4975 | @Override |
| 4976 | protected boolean allowFilterResult( |
| 4977 | PackageParser.ServiceIntentInfo filter, List<ResolveInfo> dest) { |
| 4978 | ServiceInfo filterSi = filter.service.info; |
| 4979 | for (int i=dest.size()-1; i>=0; i--) { |
| 4980 | ServiceInfo destAi = dest.get(i).serviceInfo; |
| 4981 | if (destAi.name == filterSi.name |
| 4982 | && destAi.packageName == filterSi.packageName) { |
| 4983 | return false; |
| 4984 | } |
| 4985 | } |
| 4986 | return true; |
| 4987 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4988 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4989 | @Override |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4990 | protected boolean isFilterStopped(PackageParser.ServiceIntentInfo filter, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4991 | if (!sUserManager.exists(userId)) return true; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 4992 | PackageParser.Package p = filter.service.owner; |
| 4993 | if (p != null) { |
| 4994 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 4995 | if (ps != null) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 4996 | // System apps are never considered stopped for purposes of |
| 4997 | // filtering, because there may be no way for the user to |
| 4998 | // actually re-launch them. |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4999 | return ps.getStopped(userId) |
| 5000 | && (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5001 | } |
| 5002 | } |
| 5003 | return false; |
| 5004 | } |
| 5005 | |
| 5006 | @Override |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5007 | protected String packageForFilter(PackageParser.ServiceIntentInfo info) { |
| 5008 | return info.service.owner.packageName; |
| 5009 | } |
| 5010 | |
| 5011 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5012 | protected ResolveInfo newResult(PackageParser.ServiceIntentInfo filter, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5013 | int match, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5014 | if (!sUserManager.exists(userId)) return null; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 5015 | final PackageParser.ServiceIntentInfo info = (PackageParser.ServiceIntentInfo)filter; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5016 | if (!mSettings.isEnabledLPr(info.service.info, mFlags, userId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5017 | return null; |
| 5018 | } |
| 5019 | final PackageParser.Service service = info.service; |
| 5020 | if (mSafeMode && (service.info.applicationInfo.flags |
| 5021 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 5022 | return null; |
| 5023 | } |
| 5024 | final ResolveInfo res = new ResolveInfo(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5025 | PackageSetting ps = (PackageSetting) service.owner.mExtras; |
| 5026 | res.serviceInfo = PackageParser.generateServiceInfo(service, mFlags, |
| 5027 | ps != null ? ps.getStopped(userId) : false, |
| 5028 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 5029 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5030 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 5031 | res.filter = filter; |
| 5032 | } |
| 5033 | res.priority = info.getPriority(); |
| 5034 | res.preferredOrder = service.owner.mPreferredOrder; |
| 5035 | //System.out.println("Result: " + res.activityInfo.className + |
| 5036 | // " = " + res.priority); |
| 5037 | res.match = match; |
| 5038 | res.isDefault = info.hasDefault; |
| 5039 | res.labelRes = info.labelRes; |
| 5040 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 5041 | res.icon = info.icon; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 5042 | res.system = isSystemApp(res.serviceInfo.applicationInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5043 | return res; |
| 5044 | } |
| 5045 | |
| 5046 | @Override |
| 5047 | protected void sortResults(List<ResolveInfo> results) { |
| 5048 | Collections.sort(results, mResolvePrioritySorter); |
| 5049 | } |
| 5050 | |
| 5051 | @Override |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5052 | protected void dumpFilter(PrintWriter out, String prefix, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5053 | PackageParser.ServiceIntentInfo filter) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5054 | out.print(prefix); out.print( |
| 5055 | Integer.toHexString(System.identityHashCode(filter.service))); |
| 5056 | out.print(' '); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5057 | out.print(filter.service.getComponentShortName()); |
| 5058 | out.print(" filter "); |
| 5059 | out.println(Integer.toHexString(System.identityHashCode(filter))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 5063 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 5064 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 5065 | // while (i.hasNext()) { |
| 5066 | // final ResolveInfo resolveInfo = (ResolveInfo) i; |
| 5067 | // if (isEnabledLP(resolveInfo.serviceInfo)) { |
| 5068 | // retList.add(resolveInfo); |
| 5069 | // } |
| 5070 | // } |
| 5071 | // return retList; |
| 5072 | // } |
| 5073 | |
| 5074 | // Keys are String (activity class name), values are Activity. |
| 5075 | private final HashMap<ComponentName, PackageParser.Service> mServices |
| 5076 | = new HashMap<ComponentName, PackageParser.Service>(); |
| 5077 | private int mFlags; |
| 5078 | }; |
| 5079 | |
| 5080 | private static final Comparator<ResolveInfo> mResolvePrioritySorter = |
| 5081 | new Comparator<ResolveInfo>() { |
| 5082 | public int compare(ResolveInfo r1, ResolveInfo r2) { |
| 5083 | int v1 = r1.priority; |
| 5084 | int v2 = r2.priority; |
| 5085 | //System.out.println("Comparing: q1=" + q1 + " q2=" + q2); |
| 5086 | if (v1 != v2) { |
| 5087 | return (v1 > v2) ? -1 : 1; |
| 5088 | } |
| 5089 | v1 = r1.preferredOrder; |
| 5090 | v2 = r2.preferredOrder; |
| 5091 | if (v1 != v2) { |
| 5092 | return (v1 > v2) ? -1 : 1; |
| 5093 | } |
| 5094 | if (r1.isDefault != r2.isDefault) { |
| 5095 | return r1.isDefault ? -1 : 1; |
| 5096 | } |
| 5097 | v1 = r1.match; |
| 5098 | v2 = r2.match; |
| 5099 | //System.out.println("Comparing: m1=" + m1 + " m2=" + m2); |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 5100 | if (v1 != v2) { |
| 5101 | return (v1 > v2) ? -1 : 1; |
| 5102 | } |
| 5103 | if (r1.system != r2.system) { |
| 5104 | return r1.system ? -1 : 1; |
| 5105 | } |
| 5106 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5107 | } |
| 5108 | }; |
| 5109 | |
| 5110 | private static final Comparator<ProviderInfo> mProviderInitOrderSorter = |
| 5111 | new Comparator<ProviderInfo>() { |
| 5112 | public int compare(ProviderInfo p1, ProviderInfo p2) { |
| 5113 | final int v1 = p1.initOrder; |
| 5114 | final int v2 = p2.initOrder; |
| 5115 | return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0); |
| 5116 | } |
| 5117 | }; |
| 5118 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5119 | static final void sendPackageBroadcast(String action, String pkg, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5120 | Bundle extras, String targetPkg, IIntentReceiver finishedReceiver, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5121 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 5122 | if (am != null) { |
| 5123 | try { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5124 | int[] userIds = userId == UserId.USER_ALL |
| 5125 | ? sUserManager.getUserIds() |
| 5126 | : new int[] {userId}; |
| 5127 | for (int id : userIds) { |
| 5128 | final Intent intent = new Intent(action, |
| 5129 | pkg != null ? Uri.fromParts("package", pkg, null) : null); |
| 5130 | if (extras != null) { |
| 5131 | intent.putExtras(extras); |
| 5132 | } |
| 5133 | if (targetPkg != null) { |
| 5134 | intent.setPackage(targetPkg); |
| 5135 | } |
| 5136 | // Modify the UID when posting to other users |
| 5137 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 5138 | if (uid > 0 && id > 0) { |
| 5139 | uid = UserId.getUid(id, UserId.getAppId(uid)); |
| 5140 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 5141 | } |
| 5142 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 5143 | am.broadcastIntent(null, intent, null, finishedReceiver, |
| 5144 | 0, null, null, null, finishedReceiver != null, false, id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5146 | } catch (RemoteException ex) { |
| 5147 | } |
| 5148 | } |
| 5149 | } |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5150 | |
| 5151 | /** |
| 5152 | * Check if the external storage media is available. This is true if there |
| 5153 | * is a mounted external storage medium or if the external storage is |
| 5154 | * emulated. |
| 5155 | */ |
| 5156 | private boolean isExternalMediaAvailable() { |
| 5157 | return mMediaMounted || Environment.isExternalStorageEmulated(); |
| 5158 | } |
| 5159 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5160 | public String nextPackageToClean(String lastPackage) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5161 | // writer |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5162 | synchronized (mPackages) { |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5163 | if (!isExternalMediaAvailable()) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5164 | // If the external storage is no longer mounted at this point, |
| 5165 | // the caller may not have been able to delete all of this |
| 5166 | // packages files and can not delete any more. Bail. |
| 5167 | return null; |
| 5168 | } |
| 5169 | if (lastPackage != null) { |
| 5170 | mSettings.mPackagesToBeCleaned.remove(lastPackage); |
| 5171 | } |
| 5172 | return mSettings.mPackagesToBeCleaned.size() > 0 |
| 5173 | ? mSettings.mPackagesToBeCleaned.get(0) : null; |
| 5174 | } |
| 5175 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5176 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5177 | void schedulePackageCleaning(String packageName) { |
| 5178 | mHandler.sendMessage(mHandler.obtainMessage(START_CLEANING_PACKAGE, packageName)); |
| 5179 | } |
| 5180 | |
| 5181 | void startCleaningPackages() { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5182 | // reader |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5183 | synchronized (mPackages) { |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5184 | if (!isExternalMediaAvailable()) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5185 | return; |
| 5186 | } |
| 5187 | if (mSettings.mPackagesToBeCleaned.size() <= 0) { |
| 5188 | return; |
| 5189 | } |
| 5190 | } |
| 5191 | Intent intent = new Intent(PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE); |
| 5192 | intent.setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 5193 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 5194 | if (am != null) { |
| 5195 | try { |
| 5196 | am.startService(null, intent, null); |
| 5197 | } catch (RemoteException e) { |
| 5198 | } |
| 5199 | } |
| 5200 | } |
| 5201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5202 | private final class AppDirObserver extends FileObserver { |
| 5203 | public AppDirObserver(String path, int mask, boolean isrom) { |
| 5204 | super(path, mask); |
| 5205 | mRootDir = path; |
| 5206 | mIsRom = isrom; |
| 5207 | } |
| 5208 | |
| 5209 | public void onEvent(int event, String path) { |
| 5210 | String removedPackage = null; |
| 5211 | int removedUid = -1; |
| 5212 | String addedPackage = null; |
| 5213 | int addedUid = -1; |
| 5214 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5215 | // 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] | 5216 | synchronized (mInstallLock) { |
| 5217 | String fullPathStr = null; |
| 5218 | File fullPath = null; |
| 5219 | if (path != null) { |
| 5220 | fullPath = new File(mRootDir, path); |
| 5221 | fullPathStr = fullPath.getPath(); |
| 5222 | } |
| 5223 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5224 | if (DEBUG_APP_DIR_OBSERVER) |
| 5225 | Log.v(TAG, "File " + fullPathStr + " changed: " + Integer.toHexString(event)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5226 | |
| 5227 | if (!isPackageFilename(path)) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5228 | if (DEBUG_APP_DIR_OBSERVER) |
| 5229 | 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] | 5230 | return; |
| 5231 | } |
| 5232 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5233 | // Ignore packages that are being installed or |
| 5234 | // have just been installed. |
| 5235 | if (ignoreCodePath(fullPathStr)) { |
| 5236 | return; |
| 5237 | } |
| 5238 | PackageParser.Package p = null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5239 | // reader |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5240 | synchronized (mPackages) { |
| 5241 | p = mAppDirs.get(fullPathStr); |
| 5242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5243 | if ((event&REMOVE_EVENTS) != 0) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5244 | if (p != null) { |
| 5245 | removePackageLI(p, true); |
| 5246 | removedPackage = p.applicationInfo.packageName; |
| 5247 | removedUid = p.applicationInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | if ((event&ADD_EVENTS) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5252 | if (p == null) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5253 | p = scanPackageLI(fullPath, |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 5254 | (mIsRom ? PackageParser.PARSE_IS_SYSTEM |
| 5255 | | PackageParser.PARSE_IS_SYSTEM_DIR: 0) | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5256 | PackageParser.PARSE_CHATTY | |
| 5257 | PackageParser.PARSE_MUST_BE_APK, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 5258 | SCAN_MONITOR | SCAN_NO_PATHS | SCAN_UPDATE_TIME, |
| 5259 | System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5260 | if (p != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5261 | /* |
| 5262 | * TODO this seems dangerous as the package may have |
| 5263 | * changed since we last acquired the mPackages |
| 5264 | * lock. |
| 5265 | */ |
| 5266 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5267 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5268 | updatePermissionsLPw(p.packageName, p, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5269 | p.permissions.size() > 0 ? UPDATE_PERMISSIONS_ALL : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5270 | } |
| 5271 | addedPackage = p.applicationInfo.packageName; |
| 5272 | addedUid = p.applicationInfo.uid; |
| 5273 | } |
| 5274 | } |
| 5275 | } |
| 5276 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5277 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5278 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5279 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | if (removedPackage != null) { |
| 5284 | Bundle extras = new Bundle(1); |
| 5285 | extras.putInt(Intent.EXTRA_UID, removedUid); |
| 5286 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, false); |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 5287 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5288 | extras, null, null, UserId.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5289 | } |
| 5290 | if (addedPackage != null) { |
| 5291 | Bundle extras = new Bundle(1); |
| 5292 | extras.putInt(Intent.EXTRA_UID, addedUid); |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 5293 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, addedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5294 | extras, null, null, UserId.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5295 | } |
| 5296 | } |
| 5297 | |
| 5298 | private final String mRootDir; |
| 5299 | private final boolean mIsRom; |
| 5300 | } |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5302 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 5303 | public void installPackage( |
| 5304 | final Uri packageURI, final IPackageInstallObserver observer, final int flags) { |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5305 | installPackage(packageURI, observer, flags, null); |
| 5306 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5307 | |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5308 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 5309 | public void installPackage( |
| 5310 | final Uri packageURI, final IPackageInstallObserver observer, final int flags, |
| 5311 | final String installerPackageName) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5312 | installPackageWithVerification(packageURI, observer, flags, installerPackageName, null, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5313 | null, null); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5314 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5315 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5316 | @Override |
| 5317 | public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer, |
| 5318 | int flags, String installerPackageName, Uri verificationURI, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5319 | ManifestDigest manifestDigest, ContainerEncryptionParams encryptionParams) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5320 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, null); |
| 5321 | |
| 5322 | final int uid = Binder.getCallingUid(); |
| 5323 | |
| 5324 | final int filteredFlags; |
| 5325 | |
| 5326 | if (uid == Process.SHELL_UID || uid == 0) { |
| 5327 | if (DEBUG_INSTALL) { |
| 5328 | Slog.v(TAG, "Install from ADB"); |
| 5329 | } |
| 5330 | filteredFlags = flags | PackageManager.INSTALL_FROM_ADB; |
| 5331 | } else { |
| 5332 | filteredFlags = flags & ~PackageManager.INSTALL_FROM_ADB; |
| 5333 | } |
| 5334 | |
| 5335 | final Message msg = mHandler.obtainMessage(INIT_COPY); |
| 5336 | msg.obj = new InstallParams(packageURI, observer, filteredFlags, installerPackageName, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5337 | verificationURI, manifestDigest, encryptionParams); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5338 | mHandler.sendMessage(msg); |
| 5339 | } |
| 5340 | |
| 5341 | @Override |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5342 | public void verifyPendingInstall(int id, int verificationCode) throws RemoteException { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5343 | final Message msg = mHandler.obtainMessage(PACKAGE_VERIFIED); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5344 | final PackageVerificationResponse response = new PackageVerificationResponse( |
| 5345 | verificationCode, Binder.getCallingUid()); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5346 | msg.arg1 = id; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5347 | msg.obj = response; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5348 | mHandler.sendMessage(msg); |
| 5349 | } |
| 5350 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5351 | private ComponentName matchComponentForVerifier(String packageName, |
| 5352 | List<ResolveInfo> receivers) { |
| 5353 | ActivityInfo targetReceiver = null; |
| 5354 | |
| 5355 | final int NR = receivers.size(); |
| 5356 | for (int i = 0; i < NR; i++) { |
| 5357 | final ResolveInfo info = receivers.get(i); |
| 5358 | if (info.activityInfo == null) { |
| 5359 | continue; |
| 5360 | } |
| 5361 | |
| 5362 | if (packageName.equals(info.activityInfo.packageName)) { |
| 5363 | targetReceiver = info.activityInfo; |
| 5364 | break; |
| 5365 | } |
| 5366 | } |
| 5367 | |
| 5368 | if (targetReceiver == null) { |
| 5369 | return null; |
| 5370 | } |
| 5371 | |
| 5372 | return new ComponentName(targetReceiver.packageName, targetReceiver.name); |
| 5373 | } |
| 5374 | |
| 5375 | private List<ComponentName> matchVerifiers(PackageInfoLite pkgInfo, |
| 5376 | List<ResolveInfo> receivers, final PackageVerificationState verificationState) { |
| 5377 | if (pkgInfo.verifiers.length == 0) { |
| 5378 | return null; |
| 5379 | } |
| 5380 | |
| 5381 | final int N = pkgInfo.verifiers.length; |
| 5382 | final List<ComponentName> sufficientVerifiers = new ArrayList<ComponentName>(N + 1); |
| 5383 | for (int i = 0; i < N; i++) { |
| 5384 | final VerifierInfo verifierInfo = pkgInfo.verifiers[i]; |
| 5385 | |
| 5386 | final ComponentName comp = matchComponentForVerifier(verifierInfo.packageName, |
| 5387 | receivers); |
| 5388 | if (comp == null) { |
| 5389 | continue; |
| 5390 | } |
| 5391 | |
| 5392 | final int verifierUid = getUidForVerifier(verifierInfo); |
| 5393 | if (verifierUid == -1) { |
| 5394 | continue; |
| 5395 | } |
| 5396 | |
| 5397 | if (DEBUG_VERIFY) { |
| 5398 | Slog.d(TAG, "Added sufficient verifier " + verifierInfo.packageName |
| 5399 | + " with the correct signature"); |
| 5400 | } |
| 5401 | sufficientVerifiers.add(comp); |
| 5402 | verificationState.addSufficientVerifier(verifierUid); |
| 5403 | } |
| 5404 | |
| 5405 | return sufficientVerifiers; |
| 5406 | } |
| 5407 | |
| 5408 | private int getUidForVerifier(VerifierInfo verifierInfo) { |
| 5409 | synchronized (mPackages) { |
| 5410 | final PackageParser.Package pkg = mPackages.get(verifierInfo.packageName); |
| 5411 | if (pkg == null) { |
| 5412 | return -1; |
| 5413 | } else if (pkg.mSignatures.length != 1) { |
| 5414 | Slog.i(TAG, "Verifier package " + verifierInfo.packageName |
| 5415 | + " has more than one signature; ignoring"); |
| 5416 | return -1; |
| 5417 | } |
| 5418 | |
| 5419 | /* |
| 5420 | * If the public key of the package's signature does not match |
| 5421 | * our expected public key, then this is a different package and |
| 5422 | * we should skip. |
| 5423 | */ |
| 5424 | |
| 5425 | final byte[] expectedPublicKey; |
| 5426 | try { |
| 5427 | final Signature verifierSig = pkg.mSignatures[0]; |
| 5428 | final PublicKey publicKey = verifierSig.getPublicKey(); |
| 5429 | expectedPublicKey = publicKey.getEncoded(); |
| 5430 | } catch (CertificateException e) { |
| 5431 | return -1; |
| 5432 | } |
| 5433 | |
| 5434 | final byte[] actualPublicKey = verifierInfo.publicKey.getEncoded(); |
| 5435 | |
| 5436 | if (!Arrays.equals(actualPublicKey, expectedPublicKey)) { |
| 5437 | Slog.i(TAG, "Verifier package " + verifierInfo.packageName |
| 5438 | + " does not have the expected public key; ignoring"); |
| 5439 | return -1; |
| 5440 | } |
| 5441 | |
| 5442 | return pkg.applicationInfo.uid; |
| 5443 | } |
| 5444 | } |
| 5445 | |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5446 | public void finishPackageInstall(int token) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 5447 | enforceSystemOrRoot("Only the system is allowed to finish installs"); |
| 5448 | |
| 5449 | if (DEBUG_INSTALL) { |
| 5450 | Slog.v(TAG, "BM finishing package install for " + token); |
| 5451 | } |
| 5452 | |
| 5453 | final Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5454 | mHandler.sendMessage(msg); |
| 5455 | } |
| 5456 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5457 | /** |
| 5458 | * Get the verification agent timeout. |
| 5459 | * |
| 5460 | * @return verification timeout in milliseconds |
| 5461 | */ |
| 5462 | private long getVerificationTimeout() { |
| 5463 | return android.provider.Settings.Secure.getLong(mContext.getContentResolver(), |
| 5464 | android.provider.Settings.Secure.PACKAGE_VERIFIER_TIMEOUT, |
| 5465 | DEFAULT_VERIFICATION_TIMEOUT); |
| 5466 | } |
| 5467 | |
| 5468 | /** |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 5469 | * Get the default verification agent response code. |
| 5470 | * |
| 5471 | * @return default verification response code |
| 5472 | */ |
| 5473 | private int getDefaultVerificationResponse() { |
| 5474 | return android.provider.Settings.Secure.getInt(mContext.getContentResolver(), |
| 5475 | android.provider.Settings.Secure.PACKAGE_VERIFIER_DEFAULT_RESPONSE, |
| 5476 | DEFAULT_VERIFICATION_RESPONSE); |
| 5477 | } |
| 5478 | |
| 5479 | /** |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5480 | * Check whether or not package verification has been enabled. |
| 5481 | * |
| 5482 | * @return true if verification should be performed |
| 5483 | */ |
| 5484 | private boolean isVerificationEnabled() { |
| 5485 | return android.provider.Settings.Secure.getInt(mContext.getContentResolver(), |
| 5486 | android.provider.Settings.Secure.PACKAGE_VERIFIER_ENABLE, |
| 5487 | DEFAULT_VERIFY_ENABLE ? 1 : 0) == 1 ? true : false; |
| 5488 | } |
| 5489 | |
Matt Finifter | f8a98ed | 2012-05-14 15:43:34 -0700 | [diff] [blame] | 5490 | /** |
| 5491 | * Get the "allow unknown sources" setting. |
| 5492 | * |
| 5493 | * @return the current "allow unknown sources" setting |
| 5494 | */ |
| 5495 | private int getUnknownSourcesSettings() { |
| 5496 | return android.provider.Settings.Secure.getInt(mContext.getContentResolver(), |
| 5497 | android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, |
| 5498 | -1); |
| 5499 | } |
| 5500 | |
Kenny Root | f03b45f | 2011-02-23 17:25:45 -0800 | [diff] [blame] | 5501 | public void setInstallerPackageName(String targetPackage, String installerPackageName) { |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5502 | final int uid = Binder.getCallingUid(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5503 | // writer |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5504 | synchronized (mPackages) { |
| 5505 | PackageSetting targetPackageSetting = mSettings.mPackages.get(targetPackage); |
| 5506 | if (targetPackageSetting == null) { |
| 5507 | throw new IllegalArgumentException("Unknown target package: " + targetPackage); |
| 5508 | } |
| 5509 | |
| 5510 | PackageSetting installerPackageSetting; |
| 5511 | if (installerPackageName != null) { |
| 5512 | installerPackageSetting = mSettings.mPackages.get(installerPackageName); |
| 5513 | if (installerPackageSetting == null) { |
| 5514 | throw new IllegalArgumentException("Unknown installer package: " |
| 5515 | + installerPackageName); |
| 5516 | } |
| 5517 | } else { |
| 5518 | installerPackageSetting = null; |
| 5519 | } |
| 5520 | |
| 5521 | Signature[] callerSignature; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5522 | Object obj = mSettings.getUserIdLPr(uid); |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5523 | if (obj != null) { |
| 5524 | if (obj instanceof SharedUserSetting) { |
| 5525 | callerSignature = ((SharedUserSetting)obj).signatures.mSignatures; |
| 5526 | } else if (obj instanceof PackageSetting) { |
| 5527 | callerSignature = ((PackageSetting)obj).signatures.mSignatures; |
| 5528 | } else { |
| 5529 | throw new SecurityException("Bad object " + obj + " for uid " + uid); |
| 5530 | } |
| 5531 | } else { |
| 5532 | throw new SecurityException("Unknown calling uid " + uid); |
| 5533 | } |
| 5534 | |
| 5535 | // Verify: can't set installerPackageName to a package that is |
| 5536 | // not signed with the same cert as the caller. |
| 5537 | if (installerPackageSetting != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5538 | if (compareSignatures(callerSignature, |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5539 | installerPackageSetting.signatures.mSignatures) |
| 5540 | != PackageManager.SIGNATURE_MATCH) { |
| 5541 | throw new SecurityException( |
| 5542 | "Caller does not have same cert as new installer package " |
| 5543 | + installerPackageName); |
| 5544 | } |
| 5545 | } |
| 5546 | |
| 5547 | // Verify: if target already has an installer package, it must |
| 5548 | // be signed with the same cert as the caller. |
| 5549 | if (targetPackageSetting.installerPackageName != null) { |
| 5550 | PackageSetting setting = mSettings.mPackages.get( |
| 5551 | targetPackageSetting.installerPackageName); |
| 5552 | // If the currently set package isn't valid, then it's always |
| 5553 | // okay to change it. |
| 5554 | if (setting != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5555 | if (compareSignatures(callerSignature, |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5556 | setting.signatures.mSignatures) |
| 5557 | != PackageManager.SIGNATURE_MATCH) { |
| 5558 | throw new SecurityException( |
| 5559 | "Caller does not have same cert as old installer package " |
| 5560 | + targetPackageSetting.installerPackageName); |
| 5561 | } |
| 5562 | } |
| 5563 | } |
| 5564 | |
| 5565 | // Okay! |
| 5566 | targetPackageSetting.installerPackageName = installerPackageName; |
| 5567 | scheduleWriteSettingsLocked(); |
| 5568 | } |
| 5569 | } |
| 5570 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5571 | private void processPendingInstall(final InstallArgs args, final int currentStatus) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5572 | // Queue up an async operation since the package installation may take a little while. |
| 5573 | mHandler.post(new Runnable() { |
| 5574 | public void run() { |
| 5575 | mHandler.removeCallbacks(this); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5576 | // Result object to be returned |
| 5577 | PackageInstalledInfo res = new PackageInstalledInfo(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5578 | res.returnCode = currentStatus; |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5579 | res.uid = -1; |
| 5580 | res.pkg = null; |
| 5581 | res.removedInfo = new PackageRemovedInfo(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5582 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5583 | args.doPreInstall(res.returnCode); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5584 | synchronized (mInstallLock) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5585 | installPackageLI(args, true, res); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5586 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5587 | args.doPostInstall(res.returnCode, res.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5588 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5589 | |
| 5590 | // A restore should be performed at this point if (a) the install |
| 5591 | // succeeded, (b) the operation is not an update, and (c) the new |
| 5592 | // package has a backupAgent defined. |
| 5593 | final boolean update = res.removedInfo.removedPackage != null; |
Christopher Tate | 59eac4b | 2010-02-19 19:25:45 -0800 | [diff] [blame] | 5594 | boolean doRestore = (!update |
| 5595 | && res.pkg != null |
| 5596 | && res.pkg.applicationInfo.backupAgentName != null); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5597 | |
| 5598 | // Set up the post-install work request bookkeeping. This will be used |
| 5599 | // and cleaned up by the post-install event handling regardless of whether |
| 5600 | // there's a restore pass performed. Token values are >= 1. |
| 5601 | int token; |
| 5602 | if (mNextInstallToken < 0) mNextInstallToken = 1; |
| 5603 | token = mNextInstallToken++; |
| 5604 | |
| 5605 | PostInstallData data = new PostInstallData(args, res); |
| 5606 | mRunningInstalls.put(token, data); |
| 5607 | if (DEBUG_INSTALL) Log.v(TAG, "+ starting restore round-trip " + token); |
| 5608 | |
| 5609 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED && doRestore) { |
| 5610 | // Pass responsibility to the Backup Manager. It will perform a |
| 5611 | // restore if appropriate, then pass responsibility back to the |
| 5612 | // Package Manager to run the post-install observer callbacks |
| 5613 | // and broadcasts. |
| 5614 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 5615 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 5616 | if (bm != null) { |
| 5617 | if (DEBUG_INSTALL) Log.v(TAG, "token " + token |
| 5618 | + " to BM for possible restore"); |
| 5619 | try { |
| 5620 | bm.restoreAtInstall(res.pkg.applicationInfo.packageName, token); |
| 5621 | } catch (RemoteException e) { |
| 5622 | // can't happen; the backup manager is local |
| 5623 | } catch (Exception e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5624 | Slog.e(TAG, "Exception trying to enqueue restore", e); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5625 | doRestore = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5626 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5627 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5628 | Slog.e(TAG, "Backup Manager not found!"); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5629 | doRestore = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5630 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5631 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5632 | |
| 5633 | if (!doRestore) { |
| 5634 | // No restore possible, or the Backup Manager was mysteriously not |
| 5635 | // available -- just fire the post-install work request directly. |
| 5636 | if (DEBUG_INSTALL) Log.v(TAG, "No restore - queue post-install for " + token); |
| 5637 | Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
| 5638 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5639 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5640 | } |
| 5641 | }); |
| 5642 | } |
| 5643 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5644 | private abstract class HandlerParams { |
| 5645 | private static final int MAX_RETRIES = 4; |
| 5646 | |
| 5647 | /** |
| 5648 | * Number of times startCopy() has been attempted and had a non-fatal |
| 5649 | * error. |
| 5650 | */ |
| 5651 | private int mRetries = 0; |
| 5652 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5653 | final boolean startCopy() { |
| 5654 | boolean res; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5655 | try { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5656 | if (DEBUG_INSTALL) Slog.i(TAG, "startCopy"); |
| 5657 | |
| 5658 | if (++mRetries > MAX_RETRIES) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5659 | 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] | 5660 | mHandler.sendEmptyMessage(MCS_GIVE_UP); |
| 5661 | handleServiceError(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5662 | return false; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5663 | } else { |
| 5664 | handleStartCopy(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5665 | res = true; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5666 | } |
| 5667 | } catch (RemoteException e) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5668 | if (DEBUG_INSTALL) Slog.i(TAG, "Posting install MCS_RECONNECT"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5669 | mHandler.sendEmptyMessage(MCS_RECONNECT); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5670 | res = false; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5671 | } |
| 5672 | handleReturnCode(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5673 | return res; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5674 | } |
| 5675 | |
| 5676 | final void serviceError() { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5677 | if (DEBUG_INSTALL) Slog.i(TAG, "serviceError"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5678 | handleServiceError(); |
| 5679 | handleReturnCode(); |
| 5680 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5681 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5682 | abstract void handleStartCopy() throws RemoteException; |
| 5683 | abstract void handleServiceError(); |
| 5684 | abstract void handleReturnCode(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5685 | } |
| 5686 | |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5687 | class MeasureParams extends HandlerParams { |
| 5688 | private final PackageStats mStats; |
| 5689 | private boolean mSuccess; |
| 5690 | |
| 5691 | private final IPackageStatsObserver mObserver; |
| 5692 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 5693 | public MeasureParams(PackageStats stats, IPackageStatsObserver observer) { |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5694 | mObserver = observer; |
| 5695 | mStats = stats; |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5696 | } |
| 5697 | |
| 5698 | @Override |
| 5699 | void handleStartCopy() throws RemoteException { |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 5700 | synchronized (mInstallLock) { |
| 5701 | mSuccess = getPackageSizeInfoLI(mStats.packageName, mStats); |
| 5702 | } |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5703 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 5704 | final boolean mounted; |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5705 | if (Environment.isExternalStorageEmulated()) { |
| 5706 | mounted = true; |
| 5707 | } else { |
| 5708 | final String status = Environment.getExternalStorageState(); |
| 5709 | |
| 5710 | mounted = status.equals(Environment.MEDIA_MOUNTED) |
| 5711 | || status.equals(Environment.MEDIA_MOUNTED_READ_ONLY); |
| 5712 | } |
| 5713 | |
| 5714 | if (mounted) { |
| 5715 | final File externalCacheDir = Environment |
| 5716 | .getExternalStorageAppCacheDirectory(mStats.packageName); |
| 5717 | final long externalCacheSize = mContainerService |
| 5718 | .calculateDirectorySize(externalCacheDir.getPath()); |
| 5719 | mStats.externalCacheSize = externalCacheSize; |
| 5720 | |
| 5721 | final File externalDataDir = Environment |
| 5722 | .getExternalStorageAppDataDirectory(mStats.packageName); |
| 5723 | long externalDataSize = mContainerService.calculateDirectorySize(externalDataDir |
| 5724 | .getPath()); |
| 5725 | |
| 5726 | if (externalCacheDir.getParentFile().equals(externalDataDir)) { |
| 5727 | externalDataSize -= externalCacheSize; |
| 5728 | } |
| 5729 | mStats.externalDataSize = externalDataSize; |
| 5730 | |
| 5731 | final File externalMediaDir = Environment |
| 5732 | .getExternalStorageAppMediaDirectory(mStats.packageName); |
| 5733 | mStats.externalMediaSize = mContainerService |
Kenny Root | c7624d9 | 2011-02-23 16:25:25 -0800 | [diff] [blame] | 5734 | .calculateDirectorySize(externalMediaDir.getPath()); |
Kenny Root | bcd6c96 | 2011-01-17 11:21:49 -0800 | [diff] [blame] | 5735 | |
| 5736 | final File externalObbDir = Environment |
| 5737 | .getExternalStorageAppObbDirectory(mStats.packageName); |
| 5738 | mStats.externalObbSize = mContainerService.calculateDirectorySize(externalObbDir |
| 5739 | .getPath()); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | @Override |
| 5744 | void handleReturnCode() { |
| 5745 | if (mObserver != null) { |
| 5746 | try { |
| 5747 | mObserver.onGetStatsCompleted(mStats, mSuccess); |
| 5748 | } catch (RemoteException e) { |
| 5749 | Slog.i(TAG, "Observer no longer exists."); |
| 5750 | } |
| 5751 | } |
| 5752 | } |
| 5753 | |
| 5754 | @Override |
| 5755 | void handleServiceError() { |
| 5756 | Slog.e(TAG, "Could not measure application " + mStats.packageName |
| 5757 | + " external storage"); |
| 5758 | } |
| 5759 | } |
| 5760 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5761 | class InstallParams extends HandlerParams { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5762 | final IPackageInstallObserver observer; |
| 5763 | int flags; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5764 | |
| 5765 | private final Uri mPackageURI; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5766 | final String installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5767 | final Uri verificationURI; |
| 5768 | final ManifestDigest manifestDigest; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5769 | private InstallArgs mArgs; |
| 5770 | private int mRet; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5771 | private File mTempPackage; |
| 5772 | final ContainerEncryptionParams encryptionParams; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5773 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5774 | InstallParams(Uri packageURI, |
| 5775 | IPackageInstallObserver observer, int flags, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5776 | String installerPackageName, Uri verificationURI, ManifestDigest manifestDigest, |
| 5777 | ContainerEncryptionParams encryptionParams) { |
| 5778 | this.mPackageURI = packageURI; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5779 | this.flags = flags; |
| 5780 | this.observer = observer; |
| 5781 | this.installerPackageName = installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5782 | this.verificationURI = verificationURI; |
| 5783 | this.manifestDigest = manifestDigest; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5784 | this.encryptionParams = encryptionParams; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5785 | } |
| 5786 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5787 | private int installLocationPolicy(PackageInfoLite pkgLite, int flags) { |
| 5788 | String packageName = pkgLite.packageName; |
| 5789 | int installLocation = pkgLite.installLocation; |
| 5790 | boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5791 | // reader |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5792 | synchronized (mPackages) { |
| 5793 | PackageParser.Package pkg = mPackages.get(packageName); |
| 5794 | if (pkg != null) { |
| 5795 | if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| 5796 | // Check for updated system application. |
| 5797 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 5798 | if (onSd) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5799 | Slog.w(TAG, "Cannot install update to system app on sdcard"); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5800 | return PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION; |
| 5801 | } |
| 5802 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 5803 | } else { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5804 | if (onSd) { |
| 5805 | // Install flag overrides everything. |
| 5806 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 5807 | } |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 5808 | // If current upgrade specifies particular preference |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5809 | if (installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
| 5810 | // Application explicitly specified internal. |
| 5811 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 5812 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { |
| 5813 | // App explictly prefers external. Let policy decide |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 5814 | } else { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5815 | // Prefer previous location |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 5816 | if (isExternal(pkg)) { |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 5817 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 5818 | } |
| 5819 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5820 | } |
| 5821 | } |
| 5822 | } else { |
| 5823 | // Invalid install. Return error code |
| 5824 | return PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS; |
| 5825 | } |
| 5826 | } |
| 5827 | } |
| 5828 | // All the special cases have been taken care of. |
| 5829 | // Return result based on recommended install location. |
| 5830 | if (onSd) { |
| 5831 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 5832 | } |
| 5833 | return pkgLite.recommendedInstallLocation; |
| 5834 | } |
| 5835 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5836 | /* |
| 5837 | * Invoke remote method to get package information and install |
| 5838 | * location values. Override install location based on default |
| 5839 | * policy if needed and then create install arguments based |
| 5840 | * on the install location. |
| 5841 | */ |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5842 | public void handleStartCopy() throws RemoteException { |
Suchi Amalapurapu | 1f9e1b4 | 2010-02-26 13:14:31 -0800 | [diff] [blame] | 5843 | int ret = PackageManager.INSTALL_SUCCEEDED; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5844 | final boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| 5845 | final boolean onInt = (flags & PackageManager.INSTALL_INTERNAL) != 0; |
| 5846 | PackageInfoLite pkgLite = null; |
| 5847 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5848 | if (onInt && onSd) { |
| 5849 | // Check if both bits are set. |
| 5850 | Slog.w(TAG, "Conflicting flags specified for installing on both internal and external"); |
| 5851 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5852 | } else { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 5853 | final long lowThreshold; |
| 5854 | |
| 5855 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 5856 | .getService(DeviceStorageMonitorService.SERVICE); |
| 5857 | if (dsm == null) { |
| 5858 | Log.w(TAG, "Couldn't get low memory threshold; no free limit imposed"); |
| 5859 | lowThreshold = 0L; |
| 5860 | } else { |
| 5861 | lowThreshold = dsm.getMemoryLowThreshold(); |
| 5862 | } |
| 5863 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 5864 | try { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5865 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, mPackageURI, |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 5866 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 5867 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5868 | final File packageFile; |
| 5869 | if (encryptionParams != null || !"file".equals(mPackageURI.getScheme())) { |
| 5870 | ParcelFileDescriptor out = null; |
| 5871 | |
| 5872 | mTempPackage = createTempPackageFile(mDrmAppPrivateInstallDir); |
| 5873 | if (mTempPackage != null) { |
| 5874 | try { |
| 5875 | out = ParcelFileDescriptor.open(mTempPackage, |
| 5876 | ParcelFileDescriptor.MODE_READ_WRITE); |
| 5877 | } catch (FileNotFoundException e) { |
| 5878 | Slog.e(TAG, "Failed to create temporary file for : " + mPackageURI); |
| 5879 | } |
| 5880 | |
| 5881 | // Make a temporary file for decryption. |
| 5882 | ret = mContainerService |
| 5883 | .copyResource(mPackageURI, encryptionParams, out); |
| 5884 | |
| 5885 | packageFile = mTempPackage; |
| 5886 | |
| 5887 | FileUtils.setPermissions(packageFile.getAbsolutePath(), |
| 5888 | FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IROTH, |
| 5889 | -1, -1); |
| 5890 | } else { |
| 5891 | packageFile = null; |
| 5892 | } |
| 5893 | } else { |
| 5894 | packageFile = new File(mPackageURI.getPath()); |
| 5895 | } |
| 5896 | |
| 5897 | if (packageFile != null) { |
| 5898 | // Remote call to find out default install location |
| 5899 | pkgLite = mContainerService.getMinimalPackageInfo( |
| 5900 | packageFile.getAbsolutePath(), flags, lowThreshold); |
| 5901 | } |
| 5902 | } finally { |
| 5903 | mContext.revokeUriPermission(mPackageURI, |
| 5904 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 5905 | } |
| 5906 | } |
| 5907 | |
| 5908 | if (ret == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5909 | int loc = pkgLite.recommendedInstallLocation; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5910 | if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5911 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5912 | } else if (loc == PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5913 | ret = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5914 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5915 | ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 5916 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_APK) { |
| 5917 | ret = PackageManager.INSTALL_FAILED_INVALID_APK; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5918 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_URI) { |
| 5919 | ret = PackageManager.INSTALL_FAILED_INVALID_URI; |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5920 | } else if (loc == PackageHelper.RECOMMEND_MEDIA_UNAVAILABLE) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5921 | ret = PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5922 | } else { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5923 | // Override with defaults if needed. |
| 5924 | loc = installLocationPolicy(pkgLite, flags); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5925 | if (!onSd && !onInt) { |
| 5926 | // Override install location with flags |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5927 | if (loc == PackageHelper.RECOMMEND_INSTALL_EXTERNAL) { |
| 5928 | // Set the flag to install on external media. |
| 5929 | flags |= PackageManager.INSTALL_EXTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5930 | flags &= ~PackageManager.INSTALL_INTERNAL; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5931 | } else { |
| 5932 | // Make sure the flag for installing on external |
| 5933 | // media is unset |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5934 | flags |= PackageManager.INSTALL_INTERNAL; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5935 | flags &= ~PackageManager.INSTALL_EXTERNAL; |
| 5936 | } |
| 5937 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5938 | } |
| 5939 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5940 | |
| 5941 | final InstallArgs args = createInstallArgs(this); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5942 | mArgs = args; |
| 5943 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5944 | if (ret == PackageManager.INSTALL_SUCCEEDED) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5945 | /* |
| 5946 | * Determine if we have any installed package verifiers. If we |
| 5947 | * do, then we'll defer to them to verify the packages. |
| 5948 | */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5949 | final int requiredUid = mRequiredVerifierPackage == null ? -1 |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5950 | : getPackageUid(mRequiredVerifierPackage, 0); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5951 | if (requiredUid != -1 && isVerificationEnabled()) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5952 | final Intent verification = new Intent( |
| 5953 | Intent.ACTION_PACKAGE_NEEDS_VERIFICATION); |
| 5954 | verification.setDataAndType(getPackageUri(), PACKAGE_MIME_TYPE); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5955 | verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5956 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5957 | final List<ResolveInfo> receivers = queryIntentReceivers(verification, null, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5958 | PackageManager.GET_DISABLED_COMPONENTS, 0 /* TODO: Which userId? */); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5959 | |
| 5960 | if (DEBUG_VERIFY) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5961 | Slog.d(TAG, "Found " + receivers.size() + " verifiers for intent " |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5962 | + verification.toString() + " with " + pkgLite.verifiers.length |
| 5963 | + " optional verifiers"); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5964 | } |
| 5965 | |
| 5966 | final int verificationId = mPendingVerificationToken++; |
| 5967 | |
| 5968 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_ID, verificationId); |
| 5969 | |
| 5970 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_INSTALLER_PACKAGE, |
| 5971 | installerPackageName); |
| 5972 | |
| 5973 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_INSTALL_FLAGS, flags); |
| 5974 | |
| 5975 | if (verificationURI != null) { |
| 5976 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_URI, |
| 5977 | verificationURI); |
| 5978 | } |
| 5979 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5980 | final PackageVerificationState verificationState = new PackageVerificationState( |
| 5981 | requiredUid, args); |
| 5982 | |
| 5983 | mPendingVerification.append(verificationId, verificationState); |
| 5984 | |
| 5985 | final List<ComponentName> sufficientVerifiers = matchVerifiers(pkgLite, |
| 5986 | receivers, verificationState); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5987 | |
| 5988 | /* |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5989 | * If any sufficient verifiers were listed in the package |
| 5990 | * manifest, attempt to ask them. |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5991 | */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5992 | if (sufficientVerifiers != null) { |
| 5993 | final int N = sufficientVerifiers.size(); |
| 5994 | if (N == 0) { |
| 5995 | Slog.i(TAG, "Additional verifiers required, but none installed."); |
| 5996 | ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
| 5997 | } else { |
| 5998 | for (int i = 0; i < N; i++) { |
| 5999 | final ComponentName verifierComponent = sufficientVerifiers.get(i); |
| 6000 | |
| 6001 | final Intent sufficientIntent = new Intent(verification); |
| 6002 | sufficientIntent.setComponent(verifierComponent); |
| 6003 | |
| 6004 | mContext.sendBroadcast(sufficientIntent); |
| 6005 | } |
| 6006 | } |
| 6007 | } |
| 6008 | |
| 6009 | final ComponentName requiredVerifierComponent = matchComponentForVerifier( |
| 6010 | mRequiredVerifierPackage, receivers); |
| 6011 | if (ret == PackageManager.INSTALL_SUCCEEDED |
| 6012 | && mRequiredVerifierPackage != null) { |
| 6013 | /* |
| 6014 | * Send the intent to the required verification agent, |
| 6015 | * but only start the verification timeout after the |
| 6016 | * target BroadcastReceivers have run. |
| 6017 | */ |
| 6018 | verification.setComponent(requiredVerifierComponent); |
| 6019 | mContext.sendOrderedBroadcast(verification, |
| 6020 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 6021 | new BroadcastReceiver() { |
| 6022 | @Override |
| 6023 | public void onReceive(Context context, Intent intent) { |
| 6024 | final Message msg = mHandler |
| 6025 | .obtainMessage(CHECK_PENDING_VERIFICATION); |
| 6026 | msg.arg1 = verificationId; |
| 6027 | mHandler.sendMessageDelayed(msg, getVerificationTimeout()); |
| 6028 | } |
| 6029 | }, null, 0, null, null); |
| 6030 | |
| 6031 | /* |
| 6032 | * We don't want the copy to proceed until verification |
| 6033 | * succeeds, so null out this field. |
| 6034 | */ |
| 6035 | mArgs = null; |
| 6036 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6037 | } else { |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6038 | /* |
| 6039 | * No package verification is enabled, so immediately start |
| 6040 | * the remote call to initiate copy using temporary file. |
| 6041 | */ |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6042 | ret = args.copyApk(mContainerService, true); |
| 6043 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6044 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6045 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6046 | mRet = ret; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6047 | } |
| 6048 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6049 | @Override |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6050 | void handleReturnCode() { |
Kenny Root | 6f89fa0 | 2010-07-30 16:33:47 -0700 | [diff] [blame] | 6051 | // If mArgs is null, then MCS couldn't be reached. When it |
| 6052 | // reconnects, it will try again to install. At that point, this |
| 6053 | // will succeed. |
| 6054 | if (mArgs != null) { |
| 6055 | processPendingInstall(mArgs, mRet); |
| 6056 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6057 | |
| 6058 | if (mTempPackage != null) { |
| 6059 | if (!mTempPackage.delete()) { |
| 6060 | Slog.w(TAG, "Couldn't delete temporary file: " |
| 6061 | + mTempPackage.getAbsolutePath()); |
| 6062 | } |
| 6063 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6064 | } |
| 6065 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6066 | @Override |
| 6067 | void handleServiceError() { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6068 | mArgs = createInstallArgs(this); |
| 6069 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6070 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6071 | |
| 6072 | public boolean isForwardLocked() { |
| 6073 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 6074 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6075 | |
| 6076 | public Uri getPackageUri() { |
| 6077 | if (mTempPackage != null) { |
| 6078 | return Uri.fromFile(mTempPackage); |
| 6079 | } else { |
| 6080 | return mPackageURI; |
| 6081 | } |
| 6082 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6083 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6084 | |
| 6085 | /* |
| 6086 | * Utility class used in movePackage api. |
| 6087 | * srcArgs and targetArgs are not set for invalid flags and make |
| 6088 | * sure to do null checks when invoking methods on them. |
| 6089 | * We probably want to return ErrorPrams for both failed installs |
| 6090 | * and moves. |
| 6091 | */ |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6092 | class MoveParams extends HandlerParams { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6093 | final IPackageMoveObserver observer; |
| 6094 | final int flags; |
| 6095 | final String packageName; |
| 6096 | final InstallArgs srcArgs; |
| 6097 | final InstallArgs targetArgs; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6098 | int uid; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6099 | int mRet; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6100 | |
| 6101 | MoveParams(InstallArgs srcArgs, IPackageMoveObserver observer, int flags, |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6102 | String packageName, String dataDir, int uid) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6103 | this.srcArgs = srcArgs; |
| 6104 | this.observer = observer; |
| 6105 | this.flags = flags; |
| 6106 | this.packageName = packageName; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6107 | this.uid = uid; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6108 | if (srcArgs != null) { |
| 6109 | Uri packageUri = Uri.fromFile(new File(srcArgs.getCodePath())); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6110 | targetArgs = createInstallArgs(packageUri, flags, packageName, dataDir); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6111 | } else { |
| 6112 | targetArgs = null; |
| 6113 | } |
| 6114 | } |
| 6115 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6116 | public void handleStartCopy() throws RemoteException { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6117 | mRet = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6118 | // Check for storage space on target medium |
| 6119 | if (!targetArgs.checkFreeStorage(mContainerService)) { |
| 6120 | Log.w(TAG, "Insufficient storage to install"); |
| 6121 | return; |
| 6122 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6123 | |
| 6124 | mRet = srcArgs.doPreCopy(); |
| 6125 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 6126 | return; |
| 6127 | } |
| 6128 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6129 | mRet = targetArgs.copyApk(mContainerService, false); |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6130 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 6131 | srcArgs.doPostCopy(uid); |
| 6132 | return; |
| 6133 | } |
| 6134 | |
| 6135 | mRet = srcArgs.doPostCopy(uid); |
| 6136 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 6137 | return; |
| 6138 | } |
| 6139 | |
| 6140 | mRet = targetArgs.doPreInstall(mRet); |
| 6141 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 6142 | return; |
| 6143 | } |
| 6144 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6145 | if (DEBUG_SD_INSTALL) { |
| 6146 | StringBuilder builder = new StringBuilder(); |
| 6147 | if (srcArgs != null) { |
| 6148 | builder.append("src: "); |
| 6149 | builder.append(srcArgs.getCodePath()); |
| 6150 | } |
| 6151 | if (targetArgs != null) { |
| 6152 | builder.append(" target : "); |
| 6153 | builder.append(targetArgs.getCodePath()); |
| 6154 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6155 | Log.i(TAG, builder.toString()); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6156 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6157 | } |
| 6158 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6159 | @Override |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6160 | void handleReturnCode() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6161 | targetArgs.doPostInstall(mRet, uid); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6162 | int currentStatus = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 6163 | if (mRet == PackageManager.INSTALL_SUCCEEDED) { |
| 6164 | currentStatus = PackageManager.MOVE_SUCCEEDED; |
| 6165 | } else if (mRet == PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE){ |
| 6166 | currentStatus = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 6167 | } |
| 6168 | processPendingMove(this, currentStatus); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6169 | } |
| 6170 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6171 | @Override |
| 6172 | void handleServiceError() { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6173 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6174 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6175 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6176 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6177 | /** |
| 6178 | * Used during creation of InstallArgs |
| 6179 | * |
| 6180 | * @param flags package installation flags |
| 6181 | * @return true if should be installed on external storage |
| 6182 | */ |
| 6183 | private static boolean installOnSd(int flags) { |
| 6184 | if ((flags & PackageManager.INSTALL_INTERNAL) != 0) { |
| 6185 | return false; |
| 6186 | } |
| 6187 | if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
| 6188 | return true; |
| 6189 | } |
| 6190 | return false; |
| 6191 | } |
| 6192 | |
| 6193 | /** |
| 6194 | * Used during creation of InstallArgs |
| 6195 | * |
| 6196 | * @param flags package installation flags |
| 6197 | * @return true if should be installed as forward locked |
| 6198 | */ |
| 6199 | private static boolean installForwardLocked(int flags) { |
| 6200 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 6201 | } |
| 6202 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6203 | private InstallArgs createInstallArgs(InstallParams params) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6204 | if (installOnSd(params.flags) || params.isForwardLocked()) { |
| 6205 | return new AsecInstallArgs(params); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6206 | } else { |
| 6207 | return new FileInstallArgs(params); |
| 6208 | } |
| 6209 | } |
| 6210 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6211 | private InstallArgs createInstallArgs(int flags, String fullCodePath, String fullResourcePath, |
| 6212 | String nativeLibraryPath) { |
Kenny Root | 84e35ea | 2012-06-06 16:05:05 -0700 | [diff] [blame] | 6213 | final boolean isInAsec; |
| 6214 | if (installOnSd(flags)) { |
| 6215 | /* Apps on SD card are always in ASEC containers. */ |
| 6216 | isInAsec = true; |
| 6217 | } else if (installForwardLocked(flags) |
| 6218 | && !fullCodePath.startsWith(mDrmAppPrivateInstallDir.getAbsolutePath())) { |
| 6219 | /* |
| 6220 | * Forward-locked apps are only in ASEC containers if they're the |
| 6221 | * new style |
| 6222 | */ |
| 6223 | isInAsec = true; |
| 6224 | } else { |
| 6225 | isInAsec = false; |
| 6226 | } |
| 6227 | |
| 6228 | if (isInAsec) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6229 | return new AsecInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath, |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6230 | installOnSd(flags), installForwardLocked(flags)); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6231 | } else { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6232 | return new FileInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6233 | } |
| 6234 | } |
| 6235 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6236 | // Used by package mover |
| 6237 | private InstallArgs createInstallArgs(Uri packageURI, int flags, String pkgName, String dataDir) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6238 | if (installOnSd(flags) || installForwardLocked(flags)) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6239 | String cid = getNextCodePath(packageURI.getPath(), pkgName, "/" |
| 6240 | + AsecInstallArgs.RES_FILE_NAME); |
| 6241 | return new AsecInstallArgs(packageURI, cid, installOnSd(flags), |
| 6242 | installForwardLocked(flags)); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6243 | } else { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6244 | return new FileInstallArgs(packageURI, pkgName, dataDir); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6245 | } |
| 6246 | } |
| 6247 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6248 | static abstract class InstallArgs { |
| 6249 | final IPackageInstallObserver observer; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6250 | // Always refers to PackageManager flags only |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6251 | final int flags; |
| 6252 | final Uri packageURI; |
| 6253 | final String installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6254 | final ManifestDigest manifestDigest; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6255 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6256 | InstallArgs(Uri packageURI, IPackageInstallObserver observer, int flags, |
| 6257 | String installerPackageName, ManifestDigest manifestDigest) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6258 | this.packageURI = packageURI; |
| 6259 | this.flags = flags; |
| 6260 | this.observer = observer; |
| 6261 | this.installerPackageName = installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6262 | this.manifestDigest = manifestDigest; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6263 | } |
| 6264 | |
| 6265 | abstract void createCopyFile(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6266 | abstract int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6267 | abstract int doPreInstall(int status); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6268 | abstract boolean doRename(int status, String pkgName, String oldCodePath); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6269 | |
| 6270 | abstract int doPostInstall(int status, int uid); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6271 | abstract String getCodePath(); |
| 6272 | abstract String getResourcePath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6273 | abstract String getNativeLibraryPath(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6274 | // Need installer lock especially for dex file removal. |
| 6275 | abstract void cleanUpResourcesLI(); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6276 | abstract boolean doPostDeleteLI(boolean delete); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6277 | abstract boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6278 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6279 | /** |
| 6280 | * Called before the source arguments are copied. This is used mostly |
| 6281 | * for MoveParams when it needs to read the source file to put it in the |
| 6282 | * destination. |
| 6283 | */ |
| 6284 | int doPreCopy() { |
| 6285 | return PackageManager.INSTALL_SUCCEEDED; |
| 6286 | } |
| 6287 | |
| 6288 | /** |
| 6289 | * Called after the source arguments are copied. This is used mostly for |
| 6290 | * MoveParams when it needs to read the source file to put it in the |
| 6291 | * destination. |
| 6292 | * |
| 6293 | * @return |
| 6294 | */ |
| 6295 | int doPostCopy(int uid) { |
| 6296 | return PackageManager.INSTALL_SUCCEEDED; |
| 6297 | } |
| 6298 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6299 | protected boolean isFwdLocked() { |
| 6300 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 6301 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6302 | } |
| 6303 | |
| 6304 | class FileInstallArgs extends InstallArgs { |
| 6305 | File installDir; |
| 6306 | String codeFileName; |
| 6307 | String resourceFileName; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6308 | String libraryPath; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6309 | boolean created = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6310 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6311 | FileInstallArgs(InstallParams params) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6312 | super(params.getPackageUri(), params.observer, params.flags, |
| 6313 | params.installerPackageName, params.manifestDigest); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6314 | } |
| 6315 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6316 | FileInstallArgs(String fullCodePath, String fullResourcePath, String nativeLibraryPath) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6317 | super(null, null, 0, null, null); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6318 | File codeFile = new File(fullCodePath); |
| 6319 | installDir = codeFile.getParentFile(); |
| 6320 | codeFileName = fullCodePath; |
| 6321 | resourceFileName = fullResourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6322 | libraryPath = nativeLibraryPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6323 | } |
| 6324 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6325 | FileInstallArgs(Uri packageURI, String pkgName, String dataDir) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6326 | super(packageURI, null, 0, null, null); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6327 | installDir = isFwdLocked() ? mDrmAppPrivateInstallDir : mAppInstallDir; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6328 | String apkName = getNextCodePath(null, pkgName, ".apk"); |
| 6329 | codeFileName = new File(installDir, apkName + ".apk").getPath(); |
| 6330 | resourceFileName = getResourcePathFromCodePath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6331 | libraryPath = new File(dataDir, LIB_DIR_NAME).getPath(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6332 | } |
| 6333 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6334 | boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 6335 | final long lowThreshold; |
| 6336 | |
| 6337 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 6338 | .getService(DeviceStorageMonitorService.SERVICE); |
| 6339 | if (dsm == null) { |
| 6340 | Log.w(TAG, "Couldn't get low memory threshold; no free limit imposed"); |
| 6341 | lowThreshold = 0L; |
| 6342 | } else { |
| 6343 | if (dsm.isMemoryLow()) { |
| 6344 | Log.w(TAG, "Memory is reported as being too low; aborting package install"); |
| 6345 | return false; |
| 6346 | } |
| 6347 | |
| 6348 | lowThreshold = dsm.getMemoryLowThreshold(); |
| 6349 | } |
| 6350 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 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.checkInternalFreeStorage(packageURI, isFwdLocked(), lowThreshold); |
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 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6360 | String getCodePath() { |
| 6361 | return codeFileName; |
| 6362 | } |
| 6363 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6364 | void createCopyFile() { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6365 | installDir = isFwdLocked() ? mDrmAppPrivateInstallDir : mAppInstallDir; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6366 | codeFileName = createTempPackageFile(installDir).getPath(); |
| 6367 | resourceFileName = getResourcePathFromCodePath(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6368 | created = true; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6369 | } |
| 6370 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6371 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6372 | if (temp) { |
| 6373 | // Generate temp file name |
| 6374 | createCopyFile(); |
| 6375 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6376 | // Get a ParcelFileDescriptor to write to the output file |
| 6377 | File codeFile = new File(codeFileName); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6378 | if (!created) { |
| 6379 | try { |
| 6380 | codeFile.createNewFile(); |
| 6381 | // Set permissions |
| 6382 | if (!setPermissions()) { |
| 6383 | // Failed setting permissions. |
| 6384 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6385 | } |
| 6386 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6387 | Slog.w(TAG, "Failed to create file " + codeFile); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6388 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6389 | } |
| 6390 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6391 | ParcelFileDescriptor out = null; |
| 6392 | try { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6393 | out = ParcelFileDescriptor.open(codeFile, ParcelFileDescriptor.MODE_READ_WRITE); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6394 | } catch (FileNotFoundException e) { |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 6395 | Slog.e(TAG, "Failed to create file descriptor for : " + codeFileName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6396 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6397 | } |
| 6398 | // Copy the resource now |
| 6399 | int ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6400 | try { |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6401 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6402 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6403 | ret = imcs.copyResource(packageURI, null, out); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6404 | } finally { |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6405 | IoUtils.closeQuietly(out); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6406 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6407 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6408 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6409 | if (isFwdLocked()) { |
| 6410 | final File destResourceFile = new File(getResourcePath()); |
| 6411 | |
| 6412 | // Copy the public files |
| 6413 | try { |
| 6414 | PackageHelper.extractPublicFiles(codeFileName, destResourceFile); |
| 6415 | } catch (IOException e) { |
| 6416 | Slog.e(TAG, "Couldn't create a new zip file for the public parts of a" |
| 6417 | + " forward-locked app."); |
| 6418 | destResourceFile.delete(); |
| 6419 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6420 | } |
| 6421 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6422 | return ret; |
| 6423 | } |
| 6424 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6425 | int doPreInstall(int status) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6426 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6427 | cleanUp(); |
| 6428 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6429 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | boolean doRename(int status, final String pkgName, String oldCodePath) { |
| 6433 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6434 | cleanUp(); |
| 6435 | return false; |
| 6436 | } else { |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6437 | final File oldCodeFile = new File(getCodePath()); |
| 6438 | final File oldResourceFile = new File(getResourcePath()); |
| 6439 | |
| 6440 | // Rename APK file based on packageName |
| 6441 | final String apkName = getNextCodePath(oldCodePath, pkgName, ".apk"); |
| 6442 | final File newCodeFile = new File(installDir, apkName + ".apk"); |
| 6443 | if (!oldCodeFile.renameTo(newCodeFile)) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6444 | return false; |
| 6445 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6446 | codeFileName = newCodeFile.getPath(); |
| 6447 | |
| 6448 | // Rename public resource file if it's forward-locked. |
| 6449 | final File newResFile = new File(getResourcePathFromCodePath()); |
| 6450 | if (isFwdLocked() && !oldResourceFile.renameTo(newResFile)) { |
| 6451 | return false; |
| 6452 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6453 | resourceFileName = getResourcePathFromCodePath(); |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6454 | |
| 6455 | // Attempt to set permissions |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6456 | if (!setPermissions()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6457 | return false; |
| 6458 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6459 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6460 | return true; |
| 6461 | } |
| 6462 | } |
| 6463 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6464 | int doPostInstall(int status, int uid) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6465 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6466 | cleanUp(); |
| 6467 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6468 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6469 | } |
| 6470 | |
| 6471 | String getResourcePath() { |
| 6472 | return resourceFileName; |
| 6473 | } |
| 6474 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6475 | private String getResourcePathFromCodePath() { |
| 6476 | final String codePath = getCodePath(); |
| 6477 | if (isFwdLocked()) { |
| 6478 | final StringBuilder sb = new StringBuilder(); |
| 6479 | |
| 6480 | sb.append(mAppInstallDir.getPath()); |
| 6481 | sb.append('/'); |
| 6482 | sb.append(getApkName(codePath)); |
| 6483 | sb.append(".zip"); |
| 6484 | |
| 6485 | /* |
| 6486 | * If our APK is a temporary file, mark the resource as a |
| 6487 | * temporary file as well so it can be cleaned up after |
| 6488 | * catastrophic failure. |
| 6489 | */ |
| 6490 | if (codePath.endsWith(".tmp")) { |
| 6491 | sb.append(".tmp"); |
| 6492 | } |
| 6493 | |
| 6494 | return sb.toString(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6495 | } else { |
| 6496 | return codePath; |
| 6497 | } |
| 6498 | } |
| 6499 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6500 | @Override |
| 6501 | String getNativeLibraryPath() { |
| 6502 | return libraryPath; |
| 6503 | } |
| 6504 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6505 | private boolean cleanUp() { |
| 6506 | boolean ret = true; |
| 6507 | String sourceDir = getCodePath(); |
| 6508 | String publicSourceDir = getResourcePath(); |
| 6509 | if (sourceDir != null) { |
| 6510 | File sourceFile = new File(sourceDir); |
| 6511 | if (!sourceFile.exists()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6512 | Slog.w(TAG, "Package source " + sourceDir + " does not exist."); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6513 | ret = false; |
| 6514 | } |
| 6515 | // Delete application's code and resources |
| 6516 | sourceFile.delete(); |
| 6517 | } |
| 6518 | if (publicSourceDir != null && !publicSourceDir.equals(sourceDir)) { |
| 6519 | final File publicSourceFile = new File(publicSourceDir); |
| 6520 | if (!publicSourceFile.exists()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6521 | Slog.w(TAG, "Package public source " + publicSourceFile + " does not exist."); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6522 | } |
| 6523 | if (publicSourceFile.exists()) { |
| 6524 | publicSourceFile.delete(); |
| 6525 | } |
| 6526 | } |
| 6527 | return ret; |
| 6528 | } |
| 6529 | |
| 6530 | void cleanUpResourcesLI() { |
| 6531 | String sourceDir = getCodePath(); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 6532 | if (cleanUp()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6533 | int retCode = mInstaller.rmdex(sourceDir); |
| 6534 | if (retCode < 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6535 | Slog.w(TAG, "Couldn't remove dex file for package: " |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6536 | + " at location " |
| 6537 | + sourceDir + ", retcode=" + retCode); |
| 6538 | // we don't consider this to be a failure of the core package deletion |
| 6539 | } |
| 6540 | } |
| 6541 | } |
| 6542 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6543 | private boolean setPermissions() { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6544 | // 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] | 6545 | if (!isFwdLocked()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6546 | final int filePermissions = |
| 6547 | FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP |
| 6548 | |FileUtils.S_IROTH; |
| 6549 | int retCode = FileUtils.setPermissions(getCodePath(), filePermissions, -1, -1); |
| 6550 | if (retCode != 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6551 | Slog.e(TAG, "Couldn't set new package file permissions for " + |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6552 | getCodePath() |
| 6553 | + ". The return code was: " + retCode); |
| 6554 | // TODO Define new internal error |
| 6555 | return false; |
| 6556 | } |
| 6557 | return true; |
| 6558 | } |
| 6559 | return true; |
| 6560 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6561 | |
| 6562 | boolean doPostDeleteLI(boolean delete) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6563 | // XXX err, shouldn't we respect the delete flag? |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6564 | cleanUpResourcesLI(); |
| 6565 | return true; |
| 6566 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6567 | } |
| 6568 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 6569 | private boolean isAsecExternal(String cid) { |
| 6570 | final String asecPath = PackageHelper.getSdFilesystem(cid); |
| 6571 | return !asecPath.startsWith(mAsecInternalPath); |
| 6572 | } |
| 6573 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 6574 | /** |
| 6575 | * Extract the MountService "container ID" from the full code path of an |
| 6576 | * .apk. |
| 6577 | */ |
| 6578 | static String cidFromCodePath(String fullCodePath) { |
| 6579 | int eidx = fullCodePath.lastIndexOf("/"); |
| 6580 | String subStr1 = fullCodePath.substring(0, eidx); |
| 6581 | int sidx = subStr1.lastIndexOf("/"); |
| 6582 | return subStr1.substring(sidx+1, eidx); |
| 6583 | } |
| 6584 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6585 | class AsecInstallArgs extends InstallArgs { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6586 | static final String RES_FILE_NAME = "pkg.apk"; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6587 | static final String PUBLIC_RES_FILE_NAME = "res.zip"; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6588 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6589 | String cid; |
| 6590 | String packagePath; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6591 | String resourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6592 | String libraryPath; |
| 6593 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6594 | AsecInstallArgs(InstallParams params) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6595 | super(params.getPackageUri(), params.observer, params.flags, |
| 6596 | params.installerPackageName, params.manifestDigest); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6597 | } |
| 6598 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6599 | AsecInstallArgs(String fullCodePath, String fullResourcePath, String nativeLibraryPath, |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6600 | boolean isExternal, boolean isForwardLocked) { |
| 6601 | super(null, null, (isExternal ? PackageManager.INSTALL_EXTERNAL : 0) |
| 6602 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), null, null); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6603 | // Extract cid from fullCodePath |
| 6604 | int eidx = fullCodePath.lastIndexOf("/"); |
| 6605 | String subStr1 = fullCodePath.substring(0, eidx); |
| 6606 | int sidx = subStr1.lastIndexOf("/"); |
| 6607 | cid = subStr1.substring(sidx+1, eidx); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6608 | setCachePath(subStr1); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6609 | } |
| 6610 | |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 6611 | AsecInstallArgs(String cid, boolean isForwardLocked) { |
| 6612 | super(null, null, (isAsecExternal(cid) ? PackageManager.INSTALL_EXTERNAL : 0) |
| 6613 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), null, null); |
Dianne Hackborn | aa77de1 | 2010-05-14 22:33:54 -0700 | [diff] [blame] | 6614 | this.cid = cid; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6615 | setCachePath(PackageHelper.getSdDir(cid)); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6616 | } |
| 6617 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6618 | AsecInstallArgs(Uri packageURI, String cid, boolean isExternal, boolean isForwardLocked) { |
| 6619 | super(packageURI, null, (isExternal ? PackageManager.INSTALL_EXTERNAL : 0) |
| 6620 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), null, null); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6621 | this.cid = cid; |
| 6622 | } |
| 6623 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6624 | void createCopyFile() { |
| 6625 | cid = getTempContainerId(); |
| 6626 | } |
| 6627 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6628 | boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException { |
| 6629 | try { |
| 6630 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6631 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6632 | return imcs.checkExternalFreeStorage(packageURI, isFwdLocked()); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6633 | } finally { |
| 6634 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6635 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6636 | } |
| 6637 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6638 | private final boolean isExternal() { |
| 6639 | return (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| 6640 | } |
| 6641 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6642 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6643 | if (temp) { |
| 6644 | createCopyFile(); |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 6645 | } else { |
| 6646 | /* |
| 6647 | * Pre-emptively destroy the container since it's destroyed if |
| 6648 | * copying fails due to it existing anyway. |
| 6649 | */ |
| 6650 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6651 | } |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6652 | |
| 6653 | final String newCachePath; |
| 6654 | try { |
| 6655 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6656 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6657 | newCachePath = imcs.copyResourceToContainer(packageURI, cid, getEncryptKey(), |
| 6658 | RES_FILE_NAME, PUBLIC_RES_FILE_NAME, isExternal(), isFwdLocked()); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6659 | } finally { |
| 6660 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6661 | } |
| 6662 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6663 | if (newCachePath != null) { |
| 6664 | setCachePath(newCachePath); |
| 6665 | return PackageManager.INSTALL_SUCCEEDED; |
| 6666 | } else { |
| 6667 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6668 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6669 | } |
| 6670 | |
| 6671 | @Override |
| 6672 | String getCodePath() { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6673 | return packagePath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | @Override |
| 6677 | String getResourcePath() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6678 | return resourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6679 | } |
| 6680 | |
| 6681 | @Override |
| 6682 | String getNativeLibraryPath() { |
| 6683 | return libraryPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6684 | } |
| 6685 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6686 | int doPreInstall(int status) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6687 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6688 | // Destroy container |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6689 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6690 | } else { |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6691 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6692 | if (!mounted) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6693 | String newCachePath = PackageHelper.mountSdDir(cid, getEncryptKey(), |
| 6694 | Process.SYSTEM_UID); |
| 6695 | if (newCachePath != null) { |
| 6696 | setCachePath(newCachePath); |
| 6697 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6698 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6699 | } |
| 6700 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6701 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6702 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6703 | } |
| 6704 | |
| 6705 | boolean doRename(int status, final String pkgName, |
| 6706 | String oldCodePath) { |
| 6707 | String newCacheId = getNextCodePath(oldCodePath, pkgName, "/" + RES_FILE_NAME); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6708 | String newCachePath = null; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6709 | if (PackageHelper.isContainerMounted(cid)) { |
| 6710 | // Unmount the container |
| 6711 | if (!PackageHelper.unMountSdDir(cid)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6712 | Slog.i(TAG, "Failed to unmount " + cid + " before renaming"); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6713 | return false; |
| 6714 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6715 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6716 | if (!PackageHelper.renameSdDir(cid, newCacheId)) { |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 6717 | Slog.e(TAG, "Failed to rename " + cid + " to " + newCacheId + |
| 6718 | " which might be stale. Will try to clean up."); |
| 6719 | // Clean up the stale container and proceed to recreate. |
| 6720 | if (!PackageHelper.destroySdDir(newCacheId)) { |
| 6721 | Slog.e(TAG, "Very strange. Cannot clean up stale container " + newCacheId); |
| 6722 | return false; |
| 6723 | } |
| 6724 | // Successfully cleaned up stale container. Try to rename again. |
| 6725 | if (!PackageHelper.renameSdDir(cid, newCacheId)) { |
| 6726 | Slog.e(TAG, "Failed to rename " + cid + " to " + newCacheId |
| 6727 | + " inspite of cleaning it up."); |
| 6728 | return false; |
| 6729 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6730 | } |
| 6731 | if (!PackageHelper.isContainerMounted(newCacheId)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6732 | Slog.w(TAG, "Mounting container " + newCacheId); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6733 | newCachePath = PackageHelper.mountSdDir(newCacheId, |
| 6734 | getEncryptKey(), Process.SYSTEM_UID); |
| 6735 | } else { |
| 6736 | newCachePath = PackageHelper.getSdDir(newCacheId); |
| 6737 | } |
| 6738 | if (newCachePath == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6739 | Slog.w(TAG, "Failed to get cache path for " + newCacheId); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6740 | return false; |
| 6741 | } |
| 6742 | Log.i(TAG, "Succesfully renamed " + cid + |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6743 | " to " + newCacheId + |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6744 | " at new path: " + newCachePath); |
| 6745 | cid = newCacheId; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6746 | setCachePath(newCachePath); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6747 | return true; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6748 | } |
| 6749 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6750 | private void setCachePath(String newCachePath) { |
| 6751 | File cachePath = new File(newCachePath); |
| 6752 | libraryPath = new File(cachePath, LIB_DIR_NAME).getPath(); |
| 6753 | packagePath = new File(cachePath, RES_FILE_NAME).getPath(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6754 | |
| 6755 | if (isFwdLocked()) { |
| 6756 | resourcePath = new File(cachePath, PUBLIC_RES_FILE_NAME).getPath(); |
| 6757 | } else { |
| 6758 | resourcePath = packagePath; |
| 6759 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6760 | } |
| 6761 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6762 | int doPostInstall(int status, int uid) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6763 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6764 | cleanUp(); |
| 6765 | } else { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6766 | final int groupOwner; |
| 6767 | final String protectedFile; |
| 6768 | if (isFwdLocked()) { |
| 6769 | groupOwner = uid; |
| 6770 | protectedFile = RES_FILE_NAME; |
| 6771 | } else { |
| 6772 | groupOwner = -1; |
| 6773 | protectedFile = null; |
| 6774 | } |
| 6775 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6776 | if (uid < Process.FIRST_APPLICATION_UID |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6777 | || !PackageHelper.fixSdPermissions(cid, groupOwner, protectedFile)) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6778 | Slog.e(TAG, "Failed to finalize " + cid); |
| 6779 | PackageHelper.destroySdDir(cid); |
| 6780 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6781 | } |
| 6782 | |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6783 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6784 | if (!mounted) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6785 | PackageHelper.mountSdDir(cid, getEncryptKey(), Process.myUid()); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6786 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6787 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6788 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | private void cleanUp() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6792 | if (DEBUG_SD_INSTALL) Slog.i(TAG, "cleanUp"); |
| 6793 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6794 | // Destroy secure container |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6795 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6796 | } |
| 6797 | |
| 6798 | void cleanUpResourcesLI() { |
| 6799 | String sourceFile = getCodePath(); |
| 6800 | // Remove dex file |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 6801 | int retCode = mInstaller.rmdex(sourceFile); |
| 6802 | if (retCode < 0) { |
| 6803 | Slog.w(TAG, "Couldn't remove dex file for package: " |
| 6804 | + " at location " |
| 6805 | + sourceFile.toString() + ", retcode=" + retCode); |
| 6806 | // 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] | 6807 | } |
| 6808 | cleanUp(); |
| 6809 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6810 | |
| 6811 | boolean matchContainer(String app) { |
| 6812 | if (cid.startsWith(app)) { |
| 6813 | return true; |
| 6814 | } |
| 6815 | return false; |
| 6816 | } |
| 6817 | |
| 6818 | String getPackageName() { |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 6819 | return getAsecPackageName(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6820 | } |
| 6821 | |
| 6822 | boolean doPostDeleteLI(boolean delete) { |
| 6823 | boolean ret = false; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6824 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6825 | if (mounted) { |
| 6826 | // Unmount first |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6827 | ret = PackageHelper.unMountSdDir(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6828 | } |
| 6829 | if (ret && delete) { |
| 6830 | cleanUpResourcesLI(); |
| 6831 | } |
| 6832 | return ret; |
| 6833 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6834 | |
| 6835 | @Override |
| 6836 | int doPreCopy() { |
| 6837 | if (isFwdLocked()) { |
| 6838 | if (!PackageHelper.fixSdPermissions(cid, |
| 6839 | getPackageUid(DEFAULT_CONTAINER_PACKAGE, 0), RES_FILE_NAME)) { |
| 6840 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6841 | } |
| 6842 | } |
| 6843 | |
| 6844 | return PackageManager.INSTALL_SUCCEEDED; |
| 6845 | } |
| 6846 | |
| 6847 | @Override |
| 6848 | int doPostCopy(int uid) { |
| 6849 | if (isFwdLocked()) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6850 | if (uid < Process.FIRST_APPLICATION_UID |
| 6851 | || !PackageHelper.fixSdPermissions(cid, uid, RES_FILE_NAME)) { |
| 6852 | Slog.e(TAG, "Failed to finalize " + cid); |
| 6853 | PackageHelper.destroySdDir(cid); |
| 6854 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6855 | } |
| 6856 | } |
| 6857 | |
| 6858 | return PackageManager.INSTALL_SUCCEEDED; |
| 6859 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6860 | }; |
| 6861 | |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 6862 | static String getAsecPackageName(String packageCid) { |
| 6863 | int idx = packageCid.lastIndexOf("-"); |
| 6864 | if (idx == -1) { |
| 6865 | return packageCid; |
| 6866 | } |
| 6867 | return packageCid.substring(0, idx); |
| 6868 | } |
| 6869 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6870 | // Utility method used to create code paths based on package name and available index. |
| 6871 | private static String getNextCodePath(String oldCodePath, String prefix, String suffix) { |
| 6872 | String idxStr = ""; |
| 6873 | int idx = 1; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6874 | // Fall back to default value of idx=1 if prefix is not |
| 6875 | // part of oldCodePath |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6876 | if (oldCodePath != null) { |
Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 6877 | String subStr = oldCodePath; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6878 | // Drop the suffix right away |
Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 6879 | if (subStr.endsWith(suffix)) { |
| 6880 | subStr = subStr.substring(0, subStr.length() - suffix.length()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6881 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6882 | // If oldCodePath already contains prefix find out the |
| 6883 | // ending index to either increment or decrement. |
| 6884 | int sidx = subStr.lastIndexOf(prefix); |
| 6885 | if (sidx != -1) { |
| 6886 | subStr = subStr.substring(sidx + prefix.length()); |
| 6887 | if (subStr != null) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6888 | if (subStr.startsWith(INSTALL_PACKAGE_SUFFIX)) { |
| 6889 | subStr = subStr.substring(INSTALL_PACKAGE_SUFFIX.length()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6890 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6891 | try { |
| 6892 | idx = Integer.parseInt(subStr); |
| 6893 | if (idx <= 1) { |
| 6894 | idx++; |
| 6895 | } else { |
| 6896 | idx--; |
| 6897 | } |
| 6898 | } catch(NumberFormatException e) { |
| 6899 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6900 | } |
| 6901 | } |
| 6902 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6903 | idxStr = INSTALL_PACKAGE_SUFFIX + Integer.toString(idx); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6904 | return prefix + idxStr; |
| 6905 | } |
| 6906 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6907 | // Utility method used to ignore ADD/REMOVE events |
| 6908 | // by directory observer. |
| 6909 | private static boolean ignoreCodePath(String fullPathStr) { |
| 6910 | String apkName = getApkName(fullPathStr); |
| 6911 | int idx = apkName.lastIndexOf(INSTALL_PACKAGE_SUFFIX); |
| 6912 | if (idx != -1 && ((idx+1) < apkName.length())) { |
| 6913 | // Make sure the package ends with a numeral |
| 6914 | String version = apkName.substring(idx+1); |
| 6915 | try { |
| 6916 | Integer.parseInt(version); |
| 6917 | return true; |
| 6918 | } catch (NumberFormatException e) {} |
| 6919 | } |
| 6920 | return false; |
| 6921 | } |
| 6922 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6923 | // Utility method that returns the relative package path with respect |
| 6924 | // to the installation directory. Like say for /data/data/com.test-1.apk |
| 6925 | // string com.test-1 is returned. |
| 6926 | static String getApkName(String codePath) { |
| 6927 | if (codePath == null) { |
| 6928 | return null; |
| 6929 | } |
| 6930 | int sidx = codePath.lastIndexOf("/"); |
| 6931 | int eidx = codePath.lastIndexOf("."); |
| 6932 | if (eidx == -1) { |
| 6933 | eidx = codePath.length(); |
| 6934 | } else if (eidx == 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6935 | Slog.w(TAG, " Invalid code path, "+ codePath + " Not a valid apk name"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6936 | return null; |
| 6937 | } |
| 6938 | return codePath.substring(sidx+1, eidx); |
| 6939 | } |
| 6940 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6941 | class PackageInstalledInfo { |
| 6942 | String name; |
| 6943 | int uid; |
| 6944 | PackageParser.Package pkg; |
| 6945 | int returnCode; |
| 6946 | PackageRemovedInfo removedInfo; |
| 6947 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6948 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6949 | /* |
| 6950 | * Install a non-existing package. |
| 6951 | */ |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6952 | private void installNewPackageLI(PackageParser.Package pkg, |
| 6953 | int parseFlags, |
| 6954 | int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6955 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6956 | // Remember this for later, in case we need to rollback this install |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6957 | String pkgName = pkg.packageName; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6958 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 6959 | boolean dataDirExists = getDataPathForPackage(pkg.packageName, 0).exists(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6960 | res.name = pkgName; |
| 6961 | synchronized(mPackages) { |
Dianne Hackborn | e259bc7 | 2010-03-30 19:24:44 -0700 | [diff] [blame] | 6962 | if (mSettings.mRenamedPackages.containsKey(pkgName)) { |
| 6963 | // A package with the same name is already installed, though |
| 6964 | // it has been renamed to an older name. The package we |
| 6965 | // are trying to install should be installed as an update to |
| 6966 | // the existing one, but that has not been requested, so bail. |
| 6967 | Slog.w(TAG, "Attempt to re-install " + pkgName |
| 6968 | + " without first uninstalling package running as " |
| 6969 | + mSettings.mRenamedPackages.get(pkgName)); |
| 6970 | res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 6971 | return; |
| 6972 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6973 | if (mPackages.containsKey(pkgName) || mAppDirs.containsKey(pkg.mPath)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6974 | // Don't allow installation over an existing package with the same name. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6975 | Slog.w(TAG, "Attempt to re-install " + pkgName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6976 | + " without first uninstalling."); |
| 6977 | res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 6978 | return; |
| 6979 | } |
| 6980 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6981 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6982 | PackageParser.Package newPackage = scanPackageLI(pkg, parseFlags, scanMode, |
| 6983 | System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6984 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6985 | 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] | 6986 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 6987 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 6988 | } |
| 6989 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6990 | updateSettingsLI(newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6991 | installerPackageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6992 | res); |
| 6993 | // delete the partially installed application. the data directory will have to be |
| 6994 | // restored if it was already existing |
| 6995 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
| 6996 | // remove package from internal structures. Note that we want deletePackageX to |
| 6997 | // delete the package data and cache directories that it created in |
| 6998 | // scanPackageLocked, unless those directories existed before we even tried to |
| 6999 | // install. |
| 7000 | deletePackageLI( |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7001 | pkgName, false, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7002 | dataDirExists ? PackageManager.DONT_DELETE_DATA : 0, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7003 | res.removedInfo, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7004 | } |
| 7005 | } |
| 7006 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7007 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7008 | private void replacePackageLI(PackageParser.Package pkg, |
| 7009 | int parseFlags, |
| 7010 | int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7011 | String installerPackageName, PackageInstalledInfo res) { |
| 7012 | |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7013 | PackageParser.Package oldPackage; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7014 | String pkgName = pkg.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7015 | // First find the old package info and check signatures |
| 7016 | synchronized(mPackages) { |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7017 | oldPackage = mPackages.get(pkgName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7018 | if (compareSignatures(oldPackage.mSignatures, pkg.mSignatures) |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 7019 | != PackageManager.SIGNATURE_MATCH) { |
Kenny Root | 8c11f1d | 2012-04-14 12:10:38 -0700 | [diff] [blame] | 7020 | 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] | 7021 | res.returnCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 7022 | return; |
| 7023 | } |
| 7024 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7025 | boolean sysPkg = (isSystemApp(oldPackage)); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7026 | if (sysPkg) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7027 | replaceSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7028 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7029 | replaceNonSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7030 | } |
| 7031 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7032 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7033 | private void replaceNonSystemPackageLI(PackageParser.Package deletedPackage, |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7034 | PackageParser.Package pkg, |
| 7035 | int parseFlags, int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7036 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7037 | PackageParser.Package newPackage = null; |
| 7038 | String pkgName = deletedPackage.packageName; |
| 7039 | boolean deletedPkg = true; |
| 7040 | boolean updatedSettings = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7041 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7042 | long origUpdateTime; |
| 7043 | if (pkg.mExtras != null) { |
| 7044 | origUpdateTime = ((PackageSetting)pkg.mExtras).lastUpdateTime; |
| 7045 | } else { |
| 7046 | origUpdateTime = 0; |
| 7047 | } |
| 7048 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7049 | // First delete the existing package while retaining the data directory |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7050 | if (!deletePackageLI(pkgName, true, PackageManager.DONT_DELETE_DATA, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7051 | res.removedInfo, true)) { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7052 | // If the existing package wasn't successfully deleted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7053 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 7054 | deletedPkg = false; |
| 7055 | } else { |
| 7056 | // Successfully deleted the old package. Now proceed with re-installation |
| 7057 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7058 | newPackage = scanPackageLI(pkg, parseFlags, scanMode | SCAN_UPDATE_TIME, |
| 7059 | System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7060 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7061 | 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] | 7062 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 7063 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
Suchi Amalapurapu | 110fea7 | 2010-01-14 17:50:23 -0800 | [diff] [blame] | 7064 | } |
| 7065 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7066 | updateSettingsLI(newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7067 | installerPackageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7068 | res); |
| 7069 | updatedSettings = true; |
| 7070 | } |
| 7071 | } |
| 7072 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7073 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7074 | // remove package from internal structures. Note that we want deletePackageX to |
| 7075 | // delete the package data and cache directories that it created in |
| 7076 | // scanPackageLocked, unless those directories existed before we even tried to |
| 7077 | // install. |
| 7078 | if(updatedSettings) { |
| 7079 | deletePackageLI( |
| 7080 | pkgName, true, |
| 7081 | PackageManager.DONT_DELETE_DATA, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7082 | res.removedInfo, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7083 | } |
| 7084 | // Since we failed to install the new package we need to restore the old |
| 7085 | // package that we deleted. |
| 7086 | if(deletedPkg) { |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 7087 | File restoreFile = new File(deletedPackage.mPath); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7088 | // Parse old package |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7089 | boolean oldOnSd = isExternal(deletedPackage); |
| 7090 | int oldParseFlags = mDefParseFlags | PackageParser.PARSE_CHATTY | |
| 7091 | (isForwardLocked(deletedPackage) ? PackageParser.PARSE_FORWARD_LOCK : 0) | |
| 7092 | (oldOnSd ? PackageParser.PARSE_ON_SDCARD : 0); |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7093 | int oldScanMode = (oldOnSd ? 0 : SCAN_MONITOR) | SCAN_UPDATE_SIGNATURE |
| 7094 | | SCAN_UPDATE_TIME; |
| 7095 | if (scanPackageLI(restoreFile, oldParseFlags, oldScanMode, |
| 7096 | origUpdateTime) == null) { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7097 | Slog.e(TAG, "Failed to restore package : " + pkgName + " after failed upgrade"); |
| 7098 | return; |
| 7099 | } |
| 7100 | // Restore of old package succeeded. Update permissions. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7101 | // writer |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7102 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7103 | updatePermissionsLPw(deletedPackage.packageName, deletedPackage, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7104 | UPDATE_PERMISSIONS_ALL); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7105 | // can downgrade to reader |
| 7106 | mSettings.writeLPr(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7107 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7108 | 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] | 7109 | } |
| 7110 | } |
| 7111 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7113 | private void replaceSystemPackageLI(PackageParser.Package deletedPackage, |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7114 | PackageParser.Package pkg, |
| 7115 | int parseFlags, int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7116 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7117 | PackageParser.Package newPackage = null; |
| 7118 | boolean updatedSettings = false; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7119 | parseFlags |= PackageManager.INSTALL_REPLACE_EXISTING | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7120 | PackageParser.PARSE_IS_SYSTEM; |
| 7121 | String packageName = deletedPackage.packageName; |
| 7122 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 7123 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7124 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7125 | return; |
| 7126 | } |
| 7127 | PackageParser.Package oldPkg; |
| 7128 | PackageSetting oldPkgSetting; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7129 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7130 | synchronized (mPackages) { |
| 7131 | oldPkg = mPackages.get(packageName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7132 | oldPkgSetting = mSettings.mPackages.get(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7133 | if((oldPkg == null) || (oldPkg.applicationInfo == null) || |
| 7134 | (oldPkgSetting == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7135 | Slog.w(TAG, "Couldn't find package:"+packageName+" information"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7136 | return; |
| 7137 | } |
| 7138 | } |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7139 | |
| 7140 | killApplication(packageName, oldPkg.applicationInfo.uid); |
| 7141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7142 | res.removedInfo.uid = oldPkg.applicationInfo.uid; |
| 7143 | res.removedInfo.removedPackage = packageName; |
| 7144 | // Remove existing system package |
| 7145 | removePackageLI(oldPkg, true); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7146 | // writer |
Dianne Hackborn | 3ec27e0 | 2010-12-16 12:41:56 -0800 | [diff] [blame] | 7147 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7148 | if (!mSettings.disableSystemPackageLPw(packageName) && deletedPackage != null) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7149 | // We didn't need to disable the .apk as a current system package, |
| 7150 | // which means we are replacing another update that is already |
| 7151 | // 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] | 7152 | res.removedInfo.args = createInstallArgs(0, |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7153 | deletedPackage.applicationInfo.sourceDir, |
| 7154 | deletedPackage.applicationInfo.publicSourceDir, |
| 7155 | deletedPackage.applicationInfo.nativeLibraryDir); |
| 7156 | } else { |
| 7157 | res.removedInfo.args = null; |
| 7158 | } |
Dianne Hackborn | 3ec27e0 | 2010-12-16 12:41:56 -0800 | [diff] [blame] | 7159 | } |
| 7160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7161 | // Successfully disabled the old package. Now proceed with re-installation |
| 7162 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
| 7163 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7164 | newPackage = scanPackageLI(pkg, parseFlags, scanMode, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7165 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7166 | 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] | 7167 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 7168 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 7169 | } |
| 7170 | } else { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7171 | if (newPackage.mExtras != null) { |
| 7172 | final PackageSetting newPkgSetting = (PackageSetting)newPackage.mExtras; |
| 7173 | newPkgSetting.firstInstallTime = oldPkgSetting.firstInstallTime; |
| 7174 | newPkgSetting.lastUpdateTime = System.currentTimeMillis(); |
| 7175 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7176 | updateSettingsLI(newPackage, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7177 | updatedSettings = true; |
| 7178 | } |
| 7179 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7180 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7181 | // Re installation failed. Restore old information |
| 7182 | // Remove new pkg information |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 7183 | if (newPackage != null) { |
| 7184 | removePackageLI(newPackage, true); |
| 7185 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7186 | // Add back the old system package |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7187 | scanPackageLI(oldPkg, parseFlags, SCAN_MONITOR | SCAN_UPDATE_SIGNATURE, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7188 | // Restore the old system information in Settings |
| 7189 | synchronized(mPackages) { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7190 | if (updatedSettings) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7191 | mSettings.enableSystemPackageLPw(packageName); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7192 | mSettings.setInstallerPackageName(packageName, |
| 7193 | oldPkgSetting.installerPackageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7194 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7195 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7196 | } |
| 7197 | } |
| 7198 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7199 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7200 | // Utility method used to move dex files during install. |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7201 | private int moveDexFilesLI(PackageParser.Package newPackage) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7202 | int retCode; |
| 7203 | if ((newPackage.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) { |
| 7204 | retCode = mInstaller.movedex(newPackage.mScanPath, newPackage.mPath); |
| 7205 | if (retCode != 0) { |
Kenny Root | e2f7417 | 2010-08-04 13:35:33 -0700 | [diff] [blame] | 7206 | if (mNoDexOpt) { |
| 7207 | /* |
| 7208 | * If we're in an engineering build, programs are lazily run |
| 7209 | * through dexopt. If the .dex file doesn't exist yet, it |
| 7210 | * will be created when the program is run next. |
| 7211 | */ |
| 7212 | Slog.i(TAG, "dex file doesn't exist, skipping move: " + newPackage.mPath); |
| 7213 | } else { |
| 7214 | Slog.e(TAG, "Couldn't rename dex file: " + newPackage.mPath); |
| 7215 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7216 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7217 | } |
| 7218 | } |
| 7219 | return PackageManager.INSTALL_SUCCEEDED; |
| 7220 | } |
| 7221 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7222 | private void updateSettingsLI(PackageParser.Package newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7223 | String installerPackageName, PackageInstalledInfo res) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7224 | String pkgName = newPackage.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7225 | synchronized (mPackages) { |
| 7226 | //write settings. the installStatus will be incomplete at this stage. |
| 7227 | //note that the new package setting would have already been |
| 7228 | //added to mPackages. It hasn't been persisted yet. |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 7229 | mSettings.setInstallStatus(pkgName, PackageSettingBase.PKG_INSTALL_INCOMPLETE); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7230 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7231 | } |
| 7232 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7233 | if ((res.returnCode = moveDexFilesLI(newPackage)) |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7234 | != PackageManager.INSTALL_SUCCEEDED) { |
| 7235 | // Discontinue if moving dex files failed. |
| 7236 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7237 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7238 | |
| 7239 | Log.d(TAG, "New package installed in " + newPackage.mPath); |
| 7240 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7241 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7242 | updatePermissionsLPw(newPackage.packageName, newPackage, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7243 | UPDATE_PERMISSIONS_REPLACE_PKG | (newPackage.permissions.size() > 0 |
| 7244 | ? UPDATE_PERMISSIONS_ALL : 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7245 | res.name = pkgName; |
| 7246 | res.uid = newPackage.applicationInfo.uid; |
| 7247 | res.pkg = newPackage; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 7248 | mSettings.setInstallStatus(pkgName, PackageSettingBase.PKG_INSTALL_COMPLETE); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7249 | mSettings.setInstallerPackageName(pkgName, installerPackageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7250 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 7251 | //to update install status |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7252 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7253 | } |
| 7254 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7255 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7256 | private void installPackageLI(InstallArgs args, |
| 7257 | boolean newInstall, PackageInstalledInfo res) { |
| 7258 | int pFlags = args.flags; |
| 7259 | String installerPackageName = args.installerPackageName; |
| 7260 | File tmpPackageFile = new File(args.getCodePath()); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7261 | boolean forwardLocked = ((pFlags & PackageManager.INSTALL_FORWARD_LOCK) != 0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 7262 | boolean onSd = ((pFlags & PackageManager.INSTALL_EXTERNAL) != 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7263 | boolean replace = false; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7264 | int scanMode = (onSd ? 0 : SCAN_MONITOR) | SCAN_FORCE_DEX | SCAN_UPDATE_SIGNATURE |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7265 | | (newInstall ? SCAN_NEW_INSTALL : 0); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 7266 | // Result object to be returned |
| 7267 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 7268 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7269 | // Retrieve PackageSettings and parse package |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7270 | int parseFlags = mDefParseFlags | PackageParser.PARSE_CHATTY |
| 7271 | | (forwardLocked ? PackageParser.PARSE_FORWARD_LOCK : 0) |
| 7272 | | (onSd ? PackageParser.PARSE_ON_SDCARD : 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7273 | PackageParser pp = new PackageParser(tmpPackageFile.getPath()); |
| 7274 | pp.setSeparateProcesses(mSeparateProcesses); |
| 7275 | final PackageParser.Package pkg = pp.parsePackage(tmpPackageFile, |
| 7276 | null, mMetrics, parseFlags); |
| 7277 | if (pkg == null) { |
| 7278 | res.returnCode = pp.getParseError(); |
| 7279 | return; |
| 7280 | } |
| 7281 | String pkgName = res.name = pkg.packageName; |
| 7282 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_TEST_ONLY) != 0) { |
| 7283 | if ((pFlags&PackageManager.INSTALL_ALLOW_TEST) == 0) { |
| 7284 | res.returnCode = PackageManager.INSTALL_FAILED_TEST_ONLY; |
| 7285 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7286 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7287 | } |
| 7288 | if (GET_CERTIFICATES && !pp.collectCertificates(pkg, parseFlags)) { |
| 7289 | res.returnCode = pp.getParseError(); |
| 7290 | return; |
| 7291 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7292 | |
| 7293 | /* If the installer passed in a manifest digest, compare it now. */ |
| 7294 | if (args.manifestDigest != null) { |
| 7295 | if (DEBUG_INSTALL) { |
| 7296 | final String parsedManifest = pkg.manifestDigest == null ? "null" |
| 7297 | : pkg.manifestDigest.toString(); |
| 7298 | Slog.d(TAG, "Comparing manifests: " + args.manifestDigest.toString() + " vs. " |
| 7299 | + parsedManifest); |
| 7300 | } |
| 7301 | |
| 7302 | if (!args.manifestDigest.equals(pkg.manifestDigest)) { |
| 7303 | res.returnCode = PackageManager.INSTALL_FAILED_PACKAGE_CHANGED; |
| 7304 | return; |
| 7305 | } |
| 7306 | } else if (DEBUG_INSTALL) { |
| 7307 | final String parsedManifest = pkg.manifestDigest == null |
| 7308 | ? "null" : pkg.manifestDigest.toString(); |
| 7309 | Slog.d(TAG, "manifestDigest was not present, but parser got: " + parsedManifest); |
| 7310 | } |
| 7311 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7312 | // Get rid of all references to package scan path via parser. |
| 7313 | pp = null; |
| 7314 | String oldCodePath = null; |
| 7315 | boolean systemApp = false; |
| 7316 | synchronized (mPackages) { |
| 7317 | // Check if installing already existing package |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7318 | if ((pFlags&PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| 7319 | String oldName = mSettings.mRenamedPackages.get(pkgName); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 7320 | if (pkg.mOriginalPackages != null |
| 7321 | && pkg.mOriginalPackages.contains(oldName) |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7322 | && mPackages.containsKey(oldName)) { |
| 7323 | // This package is derived from an original package, |
| 7324 | // and this device has been updating from that original |
| 7325 | // name. We must continue using the original name, so |
| 7326 | // rename the new package here. |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 7327 | pkg.setPackageName(oldName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7328 | pkgName = pkg.packageName; |
| 7329 | replace = true; |
| 7330 | } else if (mPackages.containsKey(pkgName)) { |
| 7331 | // This package, under its official name, already exists |
| 7332 | // on the device; we should replace it. |
| 7333 | replace = true; |
| 7334 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7335 | } |
| 7336 | PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 7337 | if (ps != null) { |
| 7338 | oldCodePath = mSettings.mPackages.get(pkgName).codePathString; |
| 7339 | if (ps.pkg != null && ps.pkg.applicationInfo != null) { |
| 7340 | systemApp = (ps.pkg.applicationInfo.flags & |
| 7341 | ApplicationInfo.FLAG_SYSTEM) != 0; |
Dianne Hackborn | ade3eca | 2009-05-11 18:54:45 -0700 | [diff] [blame] | 7342 | } |
| 7343 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7344 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7345 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7346 | if (systemApp && onSd) { |
| 7347 | // Disable updates to system apps on sdcard |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7348 | Slog.w(TAG, "Cannot install updates to system apps on sdcard"); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7349 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
| 7350 | return; |
| 7351 | } |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 7352 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7353 | if (!args.doRename(res.returnCode, pkgName, oldCodePath)) { |
| 7354 | res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7355 | return; |
| 7356 | } |
| 7357 | // Set application objects path explicitly after the rename |
| 7358 | setApplicationInfoPaths(pkg, args.getCodePath(), args.getResourcePath()); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7359 | pkg.applicationInfo.nativeLibraryDir = args.getNativeLibraryPath(); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7360 | if (replace) { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7361 | replacePackageLI(pkg, parseFlags, scanMode, |
| 7362 | installerPackageName, res); |
| 7363 | } else { |
| 7364 | installNewPackageLI(pkg, parseFlags, scanMode, |
| 7365 | installerPackageName,res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7366 | } |
| 7367 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7368 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7369 | private static boolean isForwardLocked(PackageParser.Package pkg) { |
| 7370 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7371 | } |
| 7372 | |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 7373 | |
| 7374 | private boolean isForwardLocked(PackageSetting ps) { |
| 7375 | return (ps.pkgFlags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0; |
| 7376 | } |
| 7377 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7378 | private static boolean isExternal(PackageParser.Package pkg) { |
| 7379 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; |
| 7380 | } |
| 7381 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7382 | private static boolean isExternal(PackageSetting ps) { |
| 7383 | return (ps.pkgFlags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; |
| 7384 | } |
| 7385 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7386 | private static boolean isSystemApp(PackageParser.Package pkg) { |
| 7387 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 7388 | } |
| 7389 | |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 7390 | private static boolean isSystemApp(ApplicationInfo info) { |
| 7391 | return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 7392 | } |
| 7393 | |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 7394 | private static boolean isSystemApp(PackageSetting ps) { |
| 7395 | return (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 7396 | } |
| 7397 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7398 | private static boolean isUpdatedSystemApp(PackageParser.Package pkg) { |
| 7399 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7400 | } |
| 7401 | |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 7402 | private int packageFlagsToInstallFlags(PackageSetting ps) { |
| 7403 | int installFlags = 0; |
| 7404 | if (isExternal(ps)) { |
| 7405 | installFlags |= PackageManager.INSTALL_EXTERNAL; |
| 7406 | } |
| 7407 | if (isForwardLocked(ps)) { |
| 7408 | installFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 7409 | } |
| 7410 | return installFlags; |
| 7411 | } |
| 7412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7413 | private void deleteTempPackageFiles() { |
| 7414 | FilenameFilter filter = new FilenameFilter() { |
| 7415 | public boolean accept(File dir, String name) { |
| 7416 | return name.startsWith("vmdl") && name.endsWith(".tmp"); |
| 7417 | } |
| 7418 | }; |
| 7419 | String tmpFilesList[] = mAppInstallDir.list(filter); |
| 7420 | if(tmpFilesList == null) { |
| 7421 | return; |
| 7422 | } |
| 7423 | for(int i = 0; i < tmpFilesList.length; i++) { |
| 7424 | File tmpFile = new File(mAppInstallDir, tmpFilesList[i]); |
| 7425 | tmpFile.delete(); |
| 7426 | } |
| 7427 | } |
| 7428 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7429 | private File createTempPackageFile(File installDir) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7430 | File tmpPackageFile; |
| 7431 | try { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7432 | tmpPackageFile = File.createTempFile("vmdl", ".tmp", installDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7433 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7434 | 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] | 7435 | return null; |
| 7436 | } |
| 7437 | try { |
| 7438 | FileUtils.setPermissions( |
| 7439 | tmpPackageFile.getCanonicalPath(), FileUtils.S_IRUSR|FileUtils.S_IWUSR, |
| 7440 | -1, -1); |
| 7441 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7442 | 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] | 7443 | return null; |
| 7444 | } |
| 7445 | return tmpPackageFile; |
| 7446 | } |
| 7447 | |
| 7448 | public void deletePackage(final String packageName, |
| 7449 | final IPackageDeleteObserver observer, |
| 7450 | final int flags) { |
| 7451 | mContext.enforceCallingOrSelfPermission( |
| 7452 | android.Manifest.permission.DELETE_PACKAGES, null); |
| 7453 | // Queue up an async operation since the package deletion may take a little while. |
| 7454 | mHandler.post(new Runnable() { |
| 7455 | public void run() { |
| 7456 | mHandler.removeCallbacks(this); |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7457 | final int returnCode = deletePackageX(packageName, true, true, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7458 | if (observer != null) { |
| 7459 | try { |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7460 | observer.packageDeleted(packageName, returnCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7461 | } catch (RemoteException e) { |
| 7462 | Log.i(TAG, "Observer no longer exists."); |
| 7463 | } //end catch |
| 7464 | } //end if |
| 7465 | } //end run |
| 7466 | }); |
| 7467 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7468 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7469 | /** |
| 7470 | * This method is an internal method that could be get invoked either |
| 7471 | * to delete an installed package or to clean up a failed installation. |
| 7472 | * After deleting an installed package, a broadcast is sent to notify any |
| 7473 | * listeners that the package has been installed. For cleaning up a failed |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7474 | * 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] | 7475 | * installation wouldn't have sent the initial broadcast either |
| 7476 | * The key steps in deleting a package are |
| 7477 | * deleting the package information in internal structures like mPackages, |
| 7478 | * deleting the packages base directories through installd |
| 7479 | * updating mSettings to reflect current status |
| 7480 | * persisting settings for later use |
| 7481 | * sending a broadcast if necessary |
| 7482 | */ |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7483 | private int deletePackageX(String packageName, boolean sendBroadCast, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7484 | boolean deleteCodeAndResources, int flags) { |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7485 | final PackageRemovedInfo info = new PackageRemovedInfo(); |
| 7486 | final boolean res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7487 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7488 | IDevicePolicyManager dpm = IDevicePolicyManager.Stub.asInterface( |
| 7489 | ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)); |
| 7490 | try { |
| 7491 | if (dpm != null && dpm.packageHasActiveAdmins(packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7492 | Slog.w(TAG, "Not removing package " + packageName + ": has active device admin"); |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7493 | return PackageManager.DELETE_FAILED_DEVICE_POLICY_MANAGER; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7494 | } |
| 7495 | } catch (RemoteException e) { |
| 7496 | } |
| 7497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7498 | synchronized (mInstallLock) { |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 7499 | res = deletePackageLI(packageName, deleteCodeAndResources, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7500 | flags | REMOVE_CHATTY, info, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7501 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7502 | |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7503 | if (res && sendBroadCast) { |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7504 | boolean systemUpdate = info.isRemovedPackageSystemUpdate; |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 7505 | info.sendBroadcast(deleteCodeAndResources, systemUpdate); |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7506 | |
| 7507 | // If the removed package was a system update, the old system packaged |
| 7508 | // was re-enabled; we need to broadcast this information |
| 7509 | if (systemUpdate) { |
| 7510 | Bundle extras = new Bundle(1); |
| 7511 | extras.putInt(Intent.EXTRA_UID, info.removedUid >= 0 ? info.removedUid : info.uid); |
| 7512 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 7513 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7514 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7515 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7516 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7517 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7518 | sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, null, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7519 | null, packageName, null, UserId.USER_ALL); |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7520 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7521 | } |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 7522 | // Force a gc here. |
| 7523 | Runtime.getRuntime().gc(); |
| 7524 | // Delete the resources here after sending the broadcast to let |
| 7525 | // other processes clean up before deleting resources. |
| 7526 | if (info.args != null) { |
| 7527 | synchronized (mInstallLock) { |
| 7528 | info.args.doPostDeleteLI(deleteCodeAndResources); |
| 7529 | } |
| 7530 | } |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7531 | |
| 7532 | 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] | 7533 | } |
| 7534 | |
| 7535 | static class PackageRemovedInfo { |
| 7536 | String removedPackage; |
| 7537 | int uid = -1; |
| 7538 | int removedUid = -1; |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7539 | boolean isRemovedPackageSystemUpdate = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7540 | // Clean up resources deleted packages. |
| 7541 | InstallArgs args = null; |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7542 | |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 7543 | void sendBroadcast(boolean fullRemove, boolean replacing) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7544 | Bundle extras = new Bundle(1); |
| 7545 | extras.putInt(Intent.EXTRA_UID, removedUid >= 0 ? removedUid : uid); |
| 7546 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, fullRemove); |
| 7547 | if (replacing) { |
| 7548 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 7549 | } |
| 7550 | if (removedPackage != null) { |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7551 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7552 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | f9abb40 | 2011-08-10 15:00:59 -0700 | [diff] [blame] | 7553 | if (fullRemove && !replacing) { |
| 7554 | sendPackageBroadcast(Intent.ACTION_PACKAGE_FULLY_REMOVED, removedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7555 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | f9abb40 | 2011-08-10 15:00:59 -0700 | [diff] [blame] | 7556 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7557 | } |
| 7558 | if (removedUid >= 0) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7559 | sendPackageBroadcast(Intent.ACTION_UID_REMOVED, null, extras, null, null, |
| 7560 | UserId.getUserId(removedUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7561 | } |
| 7562 | } |
| 7563 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7564 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7565 | /* |
| 7566 | * This method deletes the package from internal data structures. If the DONT_DELETE_DATA |
| 7567 | * flag is not set, the data directory is removed as well. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7568 | * 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] | 7569 | * delete a partially installed application. |
| 7570 | */ |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7571 | private void removePackageDataLI(PackageParser.Package p, PackageRemovedInfo outInfo, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7572 | int flags, boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7573 | String packageName = p.packageName; |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7574 | if (outInfo != null) { |
| 7575 | outInfo.removedPackage = packageName; |
| 7576 | } |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 7577 | removePackageLI(p, (flags&REMOVE_CHATTY) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7578 | // Retrieve object to delete permissions for shared user later on |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7579 | final PackageSetting deletedPs; |
| 7580 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7581 | synchronized (mPackages) { |
| 7582 | deletedPs = mSettings.mPackages.get(packageName); |
| 7583 | } |
| 7584 | if ((flags&PackageManager.DONT_DELETE_DATA) == 0) { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7585 | int retCode = mInstaller.remove(packageName, 0); |
| 7586 | if (retCode < 0) { |
| 7587 | Slog.w(TAG, "Couldn't remove app data or cache directory for package: " |
| 7588 | + packageName + ", retcode=" + retCode); |
| 7589 | // 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] | 7590 | } else { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7591 | // TODO: Kill the processes first |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7592 | sUserManager.removePackageForAllUsers(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7593 | } |
Dianne Hackborn | fb1f103 | 2010-07-29 13:57:56 -0700 | [diff] [blame] | 7594 | schedulePackageCleaning(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7595 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7596 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7597 | synchronized (mPackages) { |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 7598 | if (deletedPs != null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7599 | if ((flags&PackageManager.DONT_DELETE_DATA) == 0) { |
| 7600 | if (outInfo != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7601 | outInfo.removedUid = mSettings.removePackageLPw(packageName); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7602 | } |
| 7603 | if (deletedPs != null) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7604 | updatePermissionsLPw(deletedPs.name, null, 0); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7605 | if (deletedPs.sharedUser != null) { |
| 7606 | // remove permissions associated with package |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7607 | mSettings.updateSharedUserPermsLPw(deletedPs, mGlobalGids); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7608 | } |
| 7609 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 7610 | clearPackagePreferredActivitiesLPw(deletedPs.name); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 7611 | } |
| 7612 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7613 | // can downgrade to reader |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7614 | if (writeSettings) { |
| 7615 | // Save settings now |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7616 | mSettings.writeLPr(); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7617 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7618 | } |
| 7619 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7621 | /* |
| 7622 | * Tries to delete system package. |
| 7623 | */ |
| 7624 | private boolean deleteSystemPackageLI(PackageParser.Package p, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7625 | int flags, PackageRemovedInfo outInfo, boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7626 | ApplicationInfo applicationInfo = p.applicationInfo; |
| 7627 | //applicable for non-partially installed applications only |
| 7628 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7629 | Slog.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7630 | return false; |
| 7631 | } |
| 7632 | PackageSetting ps = null; |
| 7633 | // Confirm if the system package has been updated |
| 7634 | // An updated system app can be deleted. This will also have to restore |
| 7635 | // the system pkg from system partition |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7636 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7637 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7638 | ps = mSettings.getDisabledSystemPkgLPr(p.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7639 | } |
| 7640 | if (ps == null) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7641 | 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] | 7642 | return false; |
| 7643 | } else { |
| 7644 | Log.i(TAG, "Deleting system pkg from data partition"); |
| 7645 | } |
| 7646 | // Delete the updated package |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7647 | outInfo.isRemovedPackageSystemUpdate = true; |
Kenny Root | 7c1bd7f | 2010-09-01 13:44:11 -0700 | [diff] [blame] | 7648 | if (ps.versionCode < p.mVersionCode) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7649 | // Delete data for downgrades |
Kenny Root | 7c1bd7f | 2010-09-01 13:44:11 -0700 | [diff] [blame] | 7650 | flags &= ~PackageManager.DONT_DELETE_DATA; |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7651 | } else { |
| 7652 | // Preserve data by setting flag |
Kenny Root | 7c1bd7f | 2010-09-01 13:44:11 -0700 | [diff] [blame] | 7653 | flags |= PackageManager.DONT_DELETE_DATA; |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7654 | } |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7655 | boolean ret = deleteInstalledPackageLI(p, true, flags, outInfo, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7656 | writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7657 | if (!ret) { |
| 7658 | return false; |
| 7659 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7660 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7661 | synchronized (mPackages) { |
| 7662 | // Reinstate the old system package |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7663 | mSettings.enableSystemPackageLPw(p.packageName); |
Kenny Root | 8f7cc02 | 2010-09-12 09:04:56 -0700 | [diff] [blame] | 7664 | // Remove any native libraries from the upgraded package. |
| 7665 | NativeLibraryHelper.removeNativeBinariesLI(p.applicationInfo.nativeLibraryDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7666 | } |
| 7667 | // Install the system package |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7668 | PackageParser.Package newPkg = scanPackageLI(ps.codePath, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7669 | PackageParser.PARSE_MUST_BE_APK | PackageParser.PARSE_IS_SYSTEM, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7670 | SCAN_MONITOR | SCAN_NO_PATHS, 0); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7672 | if (newPkg == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7673 | 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] | 7674 | return false; |
| 7675 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7676 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7677 | synchronized (mPackages) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7678 | updatePermissionsLPw(newPkg.packageName, newPkg, |
| 7679 | UPDATE_PERMISSIONS_ALL | UPDATE_PERMISSIONS_REPLACE_PKG); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7680 | // can downgrade to reader here |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7681 | if (writeSettings) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7682 | mSettings.writeLPr(); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7683 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7684 | } |
| 7685 | return true; |
| 7686 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7688 | private boolean deleteInstalledPackageLI(PackageParser.Package p, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7689 | boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo, |
| 7690 | boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7691 | ApplicationInfo applicationInfo = p.applicationInfo; |
| 7692 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7693 | Slog.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7694 | return false; |
| 7695 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7696 | if (outInfo != null) { |
| 7697 | outInfo.uid = applicationInfo.uid; |
| 7698 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7699 | |
| 7700 | // 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] | 7701 | removePackageDataLI(p, outInfo, flags, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7702 | |
| 7703 | // Delete application code and resources |
| 7704 | if (deleteCodeAndResources) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7705 | // TODO can pick up from PackageSettings as well |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7706 | int installFlags = isExternal(p) ? PackageManager.INSTALL_EXTERNAL : 0; |
| 7707 | installFlags |= isForwardLocked(p) ? PackageManager.INSTALL_FORWARD_LOCK : 0; |
| 7708 | outInfo.args = createInstallArgs(installFlags, applicationInfo.sourceDir, |
| 7709 | applicationInfo.publicSourceDir, applicationInfo.nativeLibraryDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7710 | } |
| 7711 | return true; |
| 7712 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7713 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7714 | /* |
| 7715 | * This method handles package deletion in general |
| 7716 | */ |
| 7717 | private boolean deletePackageLI(String packageName, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7718 | boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo, |
| 7719 | boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7720 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7721 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7722 | return false; |
| 7723 | } |
| 7724 | PackageParser.Package p; |
| 7725 | boolean dataOnly = false; |
| 7726 | synchronized (mPackages) { |
| 7727 | p = mPackages.get(packageName); |
| 7728 | if (p == null) { |
| 7729 | //this retrieves partially installed apps |
| 7730 | dataOnly = true; |
| 7731 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 7732 | if (ps == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7733 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7734 | return false; |
| 7735 | } |
| 7736 | p = ps.pkg; |
| 7737 | } |
| 7738 | } |
| 7739 | if (p == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7740 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7741 | return false; |
| 7742 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7743 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7744 | if (dataOnly) { |
| 7745 | // Delete application data first |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7746 | removePackageDataLI(p, outInfo, flags, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7747 | return true; |
| 7748 | } |
| 7749 | // At this point the package should have ApplicationInfo associated with it |
| 7750 | if (p.applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7751 | Slog.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7752 | return false; |
| 7753 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7754 | boolean ret = false; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7755 | if (isSystemApp(p)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7756 | Log.i(TAG, "Removing system package:"+p.packageName); |
| 7757 | // When an updated system application is deleted we delete the existing resources as well and |
| 7758 | // fall back to existing code in system partition |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7759 | ret = deleteSystemPackageLI(p, flags, outInfo, writeSettings); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7760 | } else { |
| 7761 | Log.i(TAG, "Removing non-system package:"+p.packageName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7762 | // Kill application pre-emptively especially for apps on sd. |
| 7763 | killApplication(packageName, p.applicationInfo.uid); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7764 | ret = deleteInstalledPackageLI(p, deleteCodeAndResources, flags, outInfo, |
| 7765 | writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7766 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7767 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7768 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7769 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7770 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7771 | public void clearApplicationUserData(final String packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7772 | final IPackageDataObserver observer, final int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7773 | mContext.enforceCallingOrSelfPermission( |
| 7774 | android.Manifest.permission.CLEAR_APP_USER_DATA, null); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7775 | checkValidCaller(Binder.getCallingUid(), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7776 | // Queue up an async operation since the package deletion may take a little while. |
| 7777 | mHandler.post(new Runnable() { |
| 7778 | public void run() { |
| 7779 | mHandler.removeCallbacks(this); |
| 7780 | final boolean succeeded; |
| 7781 | synchronized (mInstallLock) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7782 | succeeded = clearApplicationUserDataLI(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7783 | } |
| 7784 | if (succeeded) { |
| 7785 | // invoke DeviceStorageMonitor's update method to clear any notifications |
| 7786 | DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) |
| 7787 | ServiceManager.getService(DeviceStorageMonitorService.SERVICE); |
| 7788 | if (dsm != null) { |
| 7789 | dsm.updateMemory(); |
| 7790 | } |
| 7791 | } |
| 7792 | if(observer != null) { |
| 7793 | try { |
| 7794 | observer.onRemoveCompleted(packageName, succeeded); |
| 7795 | } catch (RemoteException e) { |
| 7796 | Log.i(TAG, "Observer no longer exists."); |
| 7797 | } |
| 7798 | } //end if observer |
| 7799 | } //end run |
| 7800 | }); |
| 7801 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7802 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7803 | private boolean clearApplicationUserDataLI(String packageName, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7804 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7805 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7806 | return false; |
| 7807 | } |
| 7808 | PackageParser.Package p; |
| 7809 | boolean dataOnly = false; |
| 7810 | synchronized (mPackages) { |
| 7811 | p = mPackages.get(packageName); |
| 7812 | if(p == null) { |
| 7813 | dataOnly = true; |
| 7814 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 7815 | if((ps == null) || (ps.pkg == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7816 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7817 | return false; |
| 7818 | } |
| 7819 | p = ps.pkg; |
| 7820 | } |
| 7821 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 7822 | |
Dianne Hackborn | 3b81bc1 | 2011-01-15 11:50:52 -0800 | [diff] [blame] | 7823 | if (!dataOnly) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7824 | //need to check this only for fully installed applications |
| 7825 | if (p == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7826 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7827 | return false; |
| 7828 | } |
| 7829 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 7830 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7831 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7832 | return false; |
| 7833 | } |
| 7834 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7835 | int retCode = mInstaller.clearUserData(packageName, userId); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7836 | if (retCode < 0) { |
| 7837 | Slog.w(TAG, "Couldn't remove cache files for package: " |
| 7838 | + packageName); |
| 7839 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7840 | } |
| 7841 | return true; |
| 7842 | } |
| 7843 | |
| 7844 | public void deleteApplicationCacheFiles(final String packageName, |
| 7845 | final IPackageDataObserver observer) { |
| 7846 | mContext.enforceCallingOrSelfPermission( |
| 7847 | android.Manifest.permission.DELETE_CACHE_FILES, null); |
| 7848 | // 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] | 7849 | final int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7850 | mHandler.post(new Runnable() { |
| 7851 | public void run() { |
| 7852 | mHandler.removeCallbacks(this); |
| 7853 | final boolean succeded; |
| 7854 | synchronized (mInstallLock) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7855 | succeded = deleteApplicationCacheFilesLI(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7856 | } |
| 7857 | if(observer != null) { |
| 7858 | try { |
| 7859 | observer.onRemoveCompleted(packageName, succeded); |
| 7860 | } catch (RemoteException e) { |
| 7861 | Log.i(TAG, "Observer no longer exists."); |
| 7862 | } |
| 7863 | } //end if observer |
| 7864 | } //end run |
| 7865 | }); |
| 7866 | } |
| 7867 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7868 | private boolean deleteApplicationCacheFilesLI(String packageName, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7869 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7870 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7871 | return false; |
| 7872 | } |
| 7873 | PackageParser.Package p; |
| 7874 | synchronized (mPackages) { |
| 7875 | p = mPackages.get(packageName); |
| 7876 | } |
| 7877 | if (p == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7878 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7879 | return false; |
| 7880 | } |
| 7881 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 7882 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7883 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7884 | return false; |
| 7885 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7886 | // TODO: Pass userId to deleteCacheFiles |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7887 | int retCode = mInstaller.deleteCacheFiles(packageName); |
| 7888 | if (retCode < 0) { |
| 7889 | Slog.w(TAG, "Couldn't remove cache files for package: " |
| 7890 | + packageName); |
| 7891 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7892 | } |
| 7893 | return true; |
| 7894 | } |
| 7895 | |
| 7896 | public void getPackageSizeInfo(final String packageName, |
| 7897 | final IPackageStatsObserver observer) { |
| 7898 | mContext.enforceCallingOrSelfPermission( |
| 7899 | android.Manifest.permission.GET_PACKAGE_SIZE, null); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 7900 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 7901 | PackageStats stats = new PackageStats(packageName); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 7902 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 7903 | /* |
| 7904 | * Queue up an async operation since the package measurement may take a |
| 7905 | * little while. |
| 7906 | */ |
| 7907 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| 7908 | msg.obj = new MeasureParams(stats, observer); |
| 7909 | mHandler.sendMessage(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7910 | } |
| 7911 | |
| 7912 | private boolean getPackageSizeInfoLI(String packageName, PackageStats pStats) { |
| 7913 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7914 | 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] | 7915 | return false; |
| 7916 | } |
| 7917 | PackageParser.Package p; |
| 7918 | boolean dataOnly = false; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 7919 | String asecPath = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7920 | synchronized (mPackages) { |
| 7921 | p = mPackages.get(packageName); |
| 7922 | if(p == null) { |
| 7923 | dataOnly = true; |
| 7924 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 7925 | if((ps == null) || (ps.pkg == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7926 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7927 | return false; |
| 7928 | } |
| 7929 | p = ps.pkg; |
| 7930 | } |
Kenny Root | 2afded1 | 2012-06-04 16:39:47 -0700 | [diff] [blame] | 7931 | if (p != null && (isExternal(p) || isForwardLocked(p))) { |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 7932 | String secureContainerId = cidFromCodePath(p.applicationInfo.sourceDir); |
| 7933 | if (secureContainerId != null) { |
| 7934 | asecPath = PackageHelper.getSdFilesystem(secureContainerId); |
| 7935 | } |
| 7936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7937 | } |
| 7938 | String publicSrcDir = null; |
| 7939 | if(!dataOnly) { |
| 7940 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 7941 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7942 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7943 | return false; |
| 7944 | } |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 7945 | if (isForwardLocked(p)) { |
| 7946 | publicSrcDir = applicationInfo.publicSourceDir; |
| 7947 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7948 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7949 | int res = mInstaller.getSizeInfo(packageName, p.mPath, publicSrcDir, |
| 7950 | asecPath, pStats); |
| 7951 | if (res < 0) { |
| 7952 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7953 | } |
Kenny Root | 2afded1 | 2012-06-04 16:39:47 -0700 | [diff] [blame] | 7954 | |
| 7955 | // Fix-up for forward-locked applications in ASEC containers. |
| 7956 | if (!isExternal(p)) { |
| 7957 | pStats.codeSize += pStats.externalCodeSize; |
| 7958 | pStats.externalCodeSize = 0L; |
| 7959 | } |
| 7960 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7961 | return true; |
| 7962 | } |
| 7963 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7964 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7965 | public void addPackageToPreferred(String packageName) { |
Dianne Hackborn | 8377fc0 | 2010-06-07 15:54:58 -0700 | [diff] [blame] | 7966 | 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] | 7967 | } |
| 7968 | |
| 7969 | public void removePackageFromPreferred(String packageName) { |
Dianne Hackborn | 8377fc0 | 2010-06-07 15:54:58 -0700 | [diff] [blame] | 7970 | 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] | 7971 | } |
| 7972 | |
| 7973 | public List<PackageInfo> getPreferredPackages(int flags) { |
Dianne Hackborn | a7ca0e5 | 2009-12-01 14:31:55 -0800 | [diff] [blame] | 7974 | return new ArrayList<PackageInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7975 | } |
| 7976 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7977 | private int getUidTargetSdkVersionLockedLPr(int uid) { |
| 7978 | Object obj = mSettings.getUserIdLPr(uid); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7979 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 7980 | final SharedUserSetting sus = (SharedUserSetting) obj; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7981 | int vers = Build.VERSION_CODES.CUR_DEVELOPMENT; |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 7982 | final Iterator<PackageSetting> it = sus.packages.iterator(); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7983 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7984 | final PackageSetting ps = it.next(); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7985 | if (ps.pkg != null) { |
| 7986 | int v = ps.pkg.applicationInfo.targetSdkVersion; |
| 7987 | if (v < vers) vers = v; |
| 7988 | } |
| 7989 | } |
| 7990 | return vers; |
| 7991 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 7992 | final PackageSetting ps = (PackageSetting) obj; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7993 | if (ps.pkg != null) { |
| 7994 | return ps.pkg.applicationInfo.targetSdkVersion; |
| 7995 | } |
| 7996 | } |
| 7997 | return Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 7998 | } |
| 7999 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8000 | public void addPreferredActivity(IntentFilter filter, int match, |
| 8001 | ComponentName[] set, ComponentName activity) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8002 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8003 | synchronized (mPackages) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8004 | if (mContext.checkCallingOrSelfPermission( |
| 8005 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 8006 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8007 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8008 | < Build.VERSION_CODES.FROYO) { |
| 8009 | Slog.w(TAG, "Ignoring addPreferredActivity() from uid " |
| 8010 | + Binder.getCallingUid()); |
| 8011 | return; |
| 8012 | } |
| 8013 | mContext.enforceCallingOrSelfPermission( |
| 8014 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 8015 | } |
| 8016 | |
| 8017 | Slog.i(TAG, "Adding preferred activity " + activity + ":"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8018 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
| 8019 | mSettings.mPreferredActivities.addFilter( |
| 8020 | new PreferredActivity(filter, match, set, activity)); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8021 | scheduleWriteSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8022 | } |
| 8023 | } |
| 8024 | |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8025 | public void replacePreferredActivity(IntentFilter filter, int match, |
| 8026 | ComponentName[] set, ComponentName activity) { |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8027 | if (filter.countActions() != 1) { |
| 8028 | throw new IllegalArgumentException( |
| 8029 | "replacePreferredActivity expects filter to have only 1 action."); |
| 8030 | } |
| 8031 | if (filter.countCategories() != 1) { |
| 8032 | throw new IllegalArgumentException( |
| 8033 | "replacePreferredActivity expects filter to have only 1 category."); |
| 8034 | } |
| 8035 | if (filter.countDataAuthorities() != 0 |
| 8036 | || filter.countDataPaths() != 0 |
| 8037 | || filter.countDataSchemes() != 0 |
| 8038 | || filter.countDataTypes() != 0) { |
| 8039 | throw new IllegalArgumentException( |
| 8040 | "replacePreferredActivity expects filter to have no data authorities, " + |
| 8041 | "paths, schemes or types."); |
| 8042 | } |
| 8043 | synchronized (mPackages) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8044 | if (mContext.checkCallingOrSelfPermission( |
| 8045 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 8046 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8047 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8048 | < Build.VERSION_CODES.FROYO) { |
| 8049 | Slog.w(TAG, "Ignoring replacePreferredActivity() from uid " |
| 8050 | + Binder.getCallingUid()); |
| 8051 | return; |
| 8052 | } |
| 8053 | mContext.enforceCallingOrSelfPermission( |
| 8054 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 8055 | } |
| 8056 | |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8057 | ArrayList<PreferredActivity> removed = null; |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8058 | Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
| 8059 | String action = filter.getAction(0); |
| 8060 | String category = filter.getCategory(0); |
| 8061 | while (it.hasNext()) { |
| 8062 | PreferredActivity pa = it.next(); |
| 8063 | if (pa.getAction(0).equals(action) && pa.getCategory(0).equals(category)) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8064 | if (removed == null) { |
| 8065 | removed = new ArrayList<PreferredActivity>(); |
| 8066 | } |
| 8067 | removed.add(pa); |
| 8068 | Log.i(TAG, "Removing preferred activity " + pa.mPref.mComponent + ":"); |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8069 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
| 8070 | } |
| 8071 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8072 | if (removed != null) { |
| 8073 | for (int i=0; i<removed.size(); i++) { |
| 8074 | PreferredActivity pa = removed.get(i); |
| 8075 | mSettings.mPreferredActivities.removeFilter(pa); |
| 8076 | } |
| 8077 | } |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8078 | addPreferredActivity(filter, match, set, activity); |
| 8079 | } |
| 8080 | } |
| 8081 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8082 | public void clearPackagePreferredActivities(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8083 | final int uid = Binder.getCallingUid(); |
| 8084 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8085 | synchronized (mPackages) { |
Dianne Hackborn | 2ee89ea | 2010-03-10 18:27:09 -0800 | [diff] [blame] | 8086 | PackageParser.Package pkg = mPackages.get(packageName); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8087 | if (pkg == null || pkg.applicationInfo.uid != uid) { |
| 8088 | if (mContext.checkCallingOrSelfPermission( |
| 8089 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 8090 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8091 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8092 | < Build.VERSION_CODES.FROYO) { |
| 8093 | Slog.w(TAG, "Ignoring clearPackagePreferredActivities() from uid " |
| 8094 | + Binder.getCallingUid()); |
| 8095 | return; |
| 8096 | } |
| 8097 | mContext.enforceCallingOrSelfPermission( |
| 8098 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 8099 | } |
Dianne Hackborn | 2ee89ea | 2010-03-10 18:27:09 -0800 | [diff] [blame] | 8100 | } |
| 8101 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8102 | if (clearPackagePreferredActivitiesLPw(packageName)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8103 | scheduleWriteSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8104 | } |
| 8105 | } |
| 8106 | } |
| 8107 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8108 | boolean clearPackagePreferredActivitiesLPw(String packageName) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8109 | ArrayList<PreferredActivity> removed = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8110 | Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
| 8111 | while (it.hasNext()) { |
| 8112 | PreferredActivity pa = it.next(); |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 8113 | if (pa.mPref.mComponent.getPackageName().equals(packageName)) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8114 | if (removed == null) { |
| 8115 | removed = new ArrayList<PreferredActivity>(); |
| 8116 | } |
| 8117 | removed.add(pa); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8118 | } |
| 8119 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8120 | if (removed != null) { |
| 8121 | for (int i=0; i<removed.size(); i++) { |
| 8122 | PreferredActivity pa = removed.get(i); |
| 8123 | mSettings.mPreferredActivities.removeFilter(pa); |
| 8124 | } |
| 8125 | return true; |
| 8126 | } |
| 8127 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8128 | } |
| 8129 | |
| 8130 | public int getPreferredActivities(List<IntentFilter> outFilters, |
| 8131 | List<ComponentName> outActivities, String packageName) { |
| 8132 | |
| 8133 | int num = 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8134 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8135 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8136 | final Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8137 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8138 | final PreferredActivity pa = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8139 | if (packageName == null |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 8140 | || pa.mPref.mComponent.getPackageName().equals(packageName)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8141 | if (outFilters != null) { |
| 8142 | outFilters.add(new IntentFilter(pa)); |
| 8143 | } |
| 8144 | if (outActivities != null) { |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 8145 | outActivities.add(pa.mPref.mComponent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8146 | } |
| 8147 | } |
| 8148 | } |
| 8149 | } |
| 8150 | |
| 8151 | return num; |
| 8152 | } |
| 8153 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8154 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8155 | public void setApplicationEnabledSetting(String appPackageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8156 | int newState, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8157 | if (!sUserManager.exists(userId)) return; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8158 | setEnabledSetting(appPackageName, null, newState, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8159 | } |
| 8160 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8161 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8162 | public void setComponentEnabledSetting(ComponentName componentName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8163 | int newState, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8164 | if (!sUserManager.exists(userId)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8165 | setEnabledSetting(componentName.getPackageName(), |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8166 | componentName.getClassName(), newState, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8167 | } |
| 8168 | |
| 8169 | private void setEnabledSetting( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8170 | 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] | 8171 | if (!(newState == COMPONENT_ENABLED_STATE_DEFAULT |
| 8172 | || newState == COMPONENT_ENABLED_STATE_ENABLED |
Dianne Hackborn | 0ac3031 | 2011-06-17 14:49:23 -0700 | [diff] [blame] | 8173 | || newState == COMPONENT_ENABLED_STATE_DISABLED |
| 8174 | || newState == COMPONENT_ENABLED_STATE_DISABLED_USER)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8175 | throw new IllegalArgumentException("Invalid new component state: " |
| 8176 | + newState); |
| 8177 | } |
| 8178 | PackageSetting pkgSetting; |
| 8179 | final int uid = Binder.getCallingUid(); |
| 8180 | final int permission = mContext.checkCallingPermission( |
| 8181 | android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8182 | checkValidCaller(uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8183 | final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8184 | boolean sendNow = false; |
| 8185 | boolean isApp = (className == null); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8186 | String componentName = isApp ? packageName : className; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8187 | int packageUid = -1; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8188 | ArrayList<String> components; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8189 | |
| 8190 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8191 | synchronized (mPackages) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8192 | pkgSetting = mSettings.mPackages.get(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8193 | if (pkgSetting == null) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8194 | if (className == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8195 | throw new IllegalArgumentException( |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8196 | "Unknown package: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8197 | } |
| 8198 | throw new IllegalArgumentException( |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8199 | "Unknown component: " + packageName |
| 8200 | + "/" + className); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8201 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8202 | // 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] | 8203 | if (!allowedByPermission && !UserId.isSameApp(uid, pkgSetting.appId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8204 | throw new SecurityException( |
| 8205 | "Permission Denial: attempt to change component state from pid=" |
| 8206 | + Binder.getCallingPid() |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8207 | + ", uid=" + uid + ", package uid=" + pkgSetting.appId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8208 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8209 | if (className == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8210 | // We're dealing with an application/package level state change |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8211 | if (pkgSetting.getEnabled(userId) == newState) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8212 | // Nothing to do |
| 8213 | return; |
| 8214 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8215 | pkgSetting.setEnabled(newState, userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8216 | // pkgSetting.pkg.mSetEnabled = newState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8217 | } else { |
| 8218 | // We're dealing with a component level state change |
Dianne Hackborn | 6569625 | 2012-03-05 18:49:21 -0800 | [diff] [blame] | 8219 | // First, verify that this is a valid class name. |
| 8220 | PackageParser.Package pkg = pkgSetting.pkg; |
| 8221 | if (pkg == null || !pkg.hasComponentClassName(className)) { |
| 8222 | if (pkg.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.JELLY_BEAN) { |
| 8223 | throw new IllegalArgumentException("Component class " + className |
| 8224 | + " does not exist in " + packageName); |
| 8225 | } else { |
| 8226 | Slog.w(TAG, "Failed setComponentEnabledSetting: component class " |
| 8227 | + className + " does not exist in " + packageName); |
| 8228 | } |
| 8229 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8230 | switch (newState) { |
| 8231 | case COMPONENT_ENABLED_STATE_ENABLED: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8232 | if (!pkgSetting.enableComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8233 | return; |
| 8234 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8235 | break; |
| 8236 | case COMPONENT_ENABLED_STATE_DISABLED: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8237 | if (!pkgSetting.disableComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8238 | return; |
| 8239 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8240 | break; |
| 8241 | case COMPONENT_ENABLED_STATE_DEFAULT: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8242 | if (!pkgSetting.restoreComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8243 | return; |
| 8244 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8245 | break; |
| 8246 | default: |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8247 | Slog.e(TAG, "Invalid new component state: " + newState); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8248 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8249 | } |
| 8250 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8251 | mSettings.writePackageRestrictionsLPr(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8252 | packageUid = UserId.getUid(userId, pkgSetting.appId); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8253 | components = mPendingBroadcasts.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8254 | final boolean newPackage = components == null; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8255 | if (newPackage) { |
| 8256 | components = new ArrayList<String>(); |
| 8257 | } |
| 8258 | if (!components.contains(componentName)) { |
| 8259 | components.add(componentName); |
| 8260 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8261 | if ((flags&PackageManager.DONT_KILL_APP) == 0) { |
| 8262 | sendNow = true; |
| 8263 | // Purge entry from pending broadcast list if another one exists already |
| 8264 | // since we are sending one right away. |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8265 | mPendingBroadcasts.remove(packageName); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8266 | } else { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8267 | if (newPackage) { |
| 8268 | mPendingBroadcasts.put(packageName, components); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8269 | } |
| 8270 | if (!mHandler.hasMessages(SEND_PENDING_BROADCAST)) { |
| 8271 | // Schedule a message |
| 8272 | mHandler.sendEmptyMessageDelayed(SEND_PENDING_BROADCAST, BROADCAST_DELAY); |
| 8273 | } |
| 8274 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8275 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8277 | long callingId = Binder.clearCallingIdentity(); |
| 8278 | try { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8279 | if (sendNow) { |
| 8280 | sendPackageChangedBroadcast(packageName, |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8281 | (flags&PackageManager.DONT_KILL_APP) != 0, components, packageUid); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8282 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8283 | } finally { |
| 8284 | Binder.restoreCallingIdentity(callingId); |
| 8285 | } |
| 8286 | } |
| 8287 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8288 | private void sendPackageChangedBroadcast(String packageName, |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8289 | boolean killFlag, ArrayList<String> componentNames, int packageUid) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 8290 | if (DEBUG_INSTALL) |
| 8291 | Log.v(TAG, "Sending package changed: package=" + packageName + " components=" |
| 8292 | + componentNames); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8293 | Bundle extras = new Bundle(4); |
| 8294 | extras.putString(Intent.EXTRA_CHANGED_COMPONENT_NAME, componentNames.get(0)); |
| 8295 | String nameList[] = new String[componentNames.size()]; |
| 8296 | componentNames.toArray(nameList); |
| 8297 | extras.putStringArray(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST, nameList); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8298 | extras.putBoolean(Intent.EXTRA_DONT_KILL_APP, killFlag); |
| 8299 | extras.putInt(Intent.EXTRA_UID, packageUid); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8300 | sendPackageBroadcast(Intent.ACTION_PACKAGE_CHANGED, packageName, extras, null, null, |
| 8301 | UserId.getUserId(packageUid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8302 | } |
| 8303 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8304 | public void setPackageStoppedState(String packageName, boolean stopped, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8305 | if (!sUserManager.exists(userId)) return; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8306 | final int uid = Binder.getCallingUid(); |
| 8307 | final int permission = mContext.checkCallingOrSelfPermission( |
| 8308 | android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); |
| 8309 | final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8310 | checkValidCaller(uid, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8311 | // writer |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8312 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8313 | if (mSettings.setPackageStoppedStateLPw(packageName, stopped, allowedByPermission, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8314 | uid, userId)) { |
| 8315 | scheduleWritePackageRestrictionsLocked(userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8316 | } |
| 8317 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8318 | } |
| 8319 | |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8320 | public String getInstallerPackageName(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8321 | // reader |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8322 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8323 | return mSettings.getInstallerPackageNameLPr(packageName); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8324 | } |
| 8325 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8326 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8327 | @Override |
| 8328 | public int getApplicationEnabledSetting(String packageName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8329 | if (!sUserManager.exists(userId)) return COMPONENT_ENABLED_STATE_DISABLED; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8330 | int uid = Binder.getCallingUid(); |
| 8331 | checkValidCaller(uid, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8332 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8333 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8334 | return mSettings.getApplicationEnabledSettingLPr(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8335 | } |
| 8336 | } |
| 8337 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8338 | @Override |
| 8339 | public int getComponentEnabledSetting(ComponentName componentName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8340 | if (!sUserManager.exists(userId)) return COMPONENT_ENABLED_STATE_DISABLED; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8341 | int uid = Binder.getCallingUid(); |
| 8342 | checkValidCaller(uid, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8343 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8344 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8345 | return mSettings.getComponentEnabledSettingLPr(componentName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8346 | } |
| 8347 | } |
| 8348 | |
| 8349 | public void enterSafeMode() { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 8350 | enforceSystemOrRoot("Only the system can request entering safe mode"); |
| 8351 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8352 | if (!mSystemReady) { |
| 8353 | mSafeMode = true; |
| 8354 | } |
| 8355 | } |
| 8356 | |
| 8357 | public void systemReady() { |
| 8358 | mSystemReady = true; |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8359 | |
| 8360 | // Read the compatibilty setting when the system is ready. |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 8361 | boolean compatibilityModeEnabled = android.provider.Settings.System.getInt( |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8362 | mContext.getContentResolver(), |
| 8363 | android.provider.Settings.System.COMPATIBILITY_MODE, 1) == 1; |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 8364 | PackageParser.setCompatibilityModeEnabled(compatibilityModeEnabled); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8365 | if (DEBUG_SETTINGS) { |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 8366 | Log.d(TAG, "compatibility mode:" + compatibilityModeEnabled); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8367 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8368 | } |
| 8369 | |
| 8370 | public boolean isSafeMode() { |
| 8371 | return mSafeMode; |
| 8372 | } |
| 8373 | |
| 8374 | public boolean hasSystemUidErrors() { |
| 8375 | return mHasSystemUidErrors; |
| 8376 | } |
| 8377 | |
| 8378 | static String arrayToString(int[] array) { |
| 8379 | StringBuffer buf = new StringBuffer(128); |
| 8380 | buf.append('['); |
| 8381 | if (array != null) { |
| 8382 | for (int i=0; i<array.length; i++) { |
| 8383 | if (i > 0) buf.append(", "); |
| 8384 | buf.append(array[i]); |
| 8385 | } |
| 8386 | } |
| 8387 | buf.append(']'); |
| 8388 | return buf.toString(); |
| 8389 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8390 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8391 | static class DumpState { |
| 8392 | public static final int DUMP_LIBS = 1 << 0; |
| 8393 | |
| 8394 | public static final int DUMP_FEATURES = 1 << 1; |
| 8395 | |
| 8396 | public static final int DUMP_RESOLVERS = 1 << 2; |
| 8397 | |
| 8398 | public static final int DUMP_PERMISSIONS = 1 << 3; |
| 8399 | |
| 8400 | public static final int DUMP_PACKAGES = 1 << 4; |
| 8401 | |
| 8402 | public static final int DUMP_SHARED_USERS = 1 << 5; |
| 8403 | |
| 8404 | public static final int DUMP_MESSAGES = 1 << 6; |
| 8405 | |
| 8406 | public static final int DUMP_PROVIDERS = 1 << 7; |
| 8407 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8408 | public static final int DUMP_VERIFIERS = 1 << 8; |
| 8409 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8410 | public static final int OPTION_SHOW_FILTERS = 1 << 0; |
| 8411 | |
| 8412 | private int mTypes; |
| 8413 | |
| 8414 | private int mOptions; |
| 8415 | |
| 8416 | private boolean mTitlePrinted; |
| 8417 | |
| 8418 | private SharedUserSetting mSharedUser; |
| 8419 | |
| 8420 | public boolean isDumping(int type) { |
| 8421 | if (mTypes == 0) { |
| 8422 | return true; |
| 8423 | } |
| 8424 | |
| 8425 | return (mTypes & type) != 0; |
| 8426 | } |
| 8427 | |
| 8428 | public void setDump(int type) { |
| 8429 | mTypes |= type; |
| 8430 | } |
| 8431 | |
| 8432 | public boolean isOptionEnabled(int option) { |
| 8433 | return (mOptions & option) != 0; |
| 8434 | } |
| 8435 | |
| 8436 | public void setOptionEnabled(int option) { |
| 8437 | mOptions |= option; |
| 8438 | } |
| 8439 | |
| 8440 | public boolean onTitlePrinted() { |
| 8441 | final boolean printed = mTitlePrinted; |
| 8442 | mTitlePrinted = true; |
| 8443 | return printed; |
| 8444 | } |
| 8445 | |
| 8446 | public boolean getTitlePrinted() { |
| 8447 | return mTitlePrinted; |
| 8448 | } |
| 8449 | |
| 8450 | public void setTitlePrinted(boolean enabled) { |
| 8451 | mTitlePrinted = enabled; |
| 8452 | } |
| 8453 | |
| 8454 | public SharedUserSetting getSharedUser() { |
| 8455 | return mSharedUser; |
| 8456 | } |
| 8457 | |
| 8458 | public void setSharedUser(SharedUserSetting user) { |
| 8459 | mSharedUser = user; |
| 8460 | } |
| 8461 | } |
| 8462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8463 | @Override |
| 8464 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 8465 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 8466 | != PackageManager.PERMISSION_GRANTED) { |
| 8467 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8468 | + Binder.getCallingPid() |
| 8469 | + ", uid=" + Binder.getCallingUid() |
| 8470 | + " without permission " |
| 8471 | + android.Manifest.permission.DUMP); |
| 8472 | return; |
| 8473 | } |
| 8474 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8475 | DumpState dumpState = new DumpState(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8476 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8477 | String packageName = null; |
| 8478 | |
| 8479 | int opti = 0; |
| 8480 | while (opti < args.length) { |
| 8481 | String opt = args[opti]; |
| 8482 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8483 | break; |
| 8484 | } |
| 8485 | opti++; |
| 8486 | if ("-a".equals(opt)) { |
| 8487 | // Right now we only know how to print all. |
| 8488 | } else if ("-h".equals(opt)) { |
| 8489 | pw.println("Package manager dump options:"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8490 | pw.println(" [-h] [-f] [cmd] ..."); |
| 8491 | pw.println(" -f: print details of intent filters"); |
| 8492 | pw.println(" -h: print this help"); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8493 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8494 | pw.println(" l[ibraries]: list known shared libraries"); |
| 8495 | pw.println(" f[ibraries]: list device features"); |
| 8496 | pw.println(" r[esolvers]: dump intent resolvers"); |
| 8497 | pw.println(" perm[issions]: dump permissions"); |
| 8498 | pw.println(" prov[iders]: dump content providers"); |
| 8499 | pw.println(" p[ackages]: dump installed packages"); |
| 8500 | pw.println(" s[hared-users]: dump shared user IDs"); |
| 8501 | pw.println(" m[essages]: print collected runtime messages"); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8502 | pw.println(" v[erifiers]: print package verifier info"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8503 | pw.println(" <package.name>: info about given package"); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8504 | return; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8505 | } else if ("-f".equals(opt)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8506 | dumpState.setOptionEnabled(DumpState.OPTION_SHOW_FILTERS); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8507 | } else { |
| 8508 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 8509 | } |
| 8510 | } |
| 8511 | |
| 8512 | // Is the caller requesting to dump a particular piece of data? |
| 8513 | if (opti < args.length) { |
| 8514 | String cmd = args[opti]; |
| 8515 | opti++; |
| 8516 | // Is this a package name? |
| 8517 | if ("android".equals(cmd) || cmd.contains(".")) { |
| 8518 | packageName = cmd; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8519 | } else if ("l".equals(cmd) || "libraries".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8520 | dumpState.setDump(DumpState.DUMP_LIBS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8521 | } else if ("f".equals(cmd) || "features".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8522 | dumpState.setDump(DumpState.DUMP_FEATURES); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8523 | } else if ("r".equals(cmd) || "resolvers".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8524 | dumpState.setDump(DumpState.DUMP_RESOLVERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8525 | } else if ("perm".equals(cmd) || "permissions".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8526 | dumpState.setDump(DumpState.DUMP_PERMISSIONS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8527 | } else if ("p".equals(cmd) || "packages".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8528 | dumpState.setDump(DumpState.DUMP_PACKAGES); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8529 | } else if ("s".equals(cmd) || "shared-users".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8530 | dumpState.setDump(DumpState.DUMP_SHARED_USERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8531 | } else if ("prov".equals(cmd) || "providers".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8532 | dumpState.setDump(DumpState.DUMP_PROVIDERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8533 | } else if ("m".equals(cmd) || "messages".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8534 | dumpState.setDump(DumpState.DUMP_MESSAGES); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8535 | } else if ("v".equals(cmd) || "verifiers".equals(cmd)) { |
| 8536 | dumpState.setDump(DumpState.DUMP_VERIFIERS); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8537 | } |
| 8538 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8539 | |
| 8540 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8541 | synchronized (mPackages) { |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8542 | if (dumpState.isDumping(DumpState.DUMP_VERIFIERS) && packageName == null) { |
| 8543 | if (dumpState.onTitlePrinted()) |
| 8544 | pw.println(" "); |
| 8545 | pw.println("Verifiers:"); |
| 8546 | pw.print(" Required: "); |
| 8547 | pw.print(mRequiredVerifierPackage); |
| 8548 | pw.print(" (uid="); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8549 | pw.print(getPackageUid(mRequiredVerifierPackage, 0)); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8550 | pw.println(")"); |
| 8551 | } |
| 8552 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8553 | if (dumpState.isDumping(DumpState.DUMP_LIBS) && packageName == null) { |
| 8554 | if (dumpState.onTitlePrinted()) |
| 8555 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8556 | pw.println("Libraries:"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8557 | final Iterator<String> it = mSharedLibraries.keySet().iterator(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8558 | while (it.hasNext()) { |
| 8559 | String name = it.next(); |
| 8560 | pw.print(" "); |
| 8561 | pw.print(name); |
| 8562 | pw.print(" -> "); |
| 8563 | pw.println(mSharedLibraries.get(name)); |
| 8564 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8565 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8566 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8567 | if (dumpState.isDumping(DumpState.DUMP_FEATURES) && packageName == null) { |
| 8568 | if (dumpState.onTitlePrinted()) |
| 8569 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8570 | pw.println("Features:"); |
| 8571 | Iterator<String> it = mAvailableFeatures.keySet().iterator(); |
| 8572 | while (it.hasNext()) { |
| 8573 | String name = it.next(); |
| 8574 | pw.print(" "); |
| 8575 | pw.println(name); |
| 8576 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8577 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8578 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8579 | if (dumpState.isDumping(DumpState.DUMP_RESOLVERS)) { |
| 8580 | if (mActivities.dump(pw, dumpState.getTitlePrinted() ? "\nActivity Resolver Table:" |
| 8581 | : "Activity Resolver Table:", " ", packageName, |
| 8582 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8583 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8584 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8585 | if (mReceivers.dump(pw, dumpState.getTitlePrinted() ? "\nReceiver Resolver Table:" |
| 8586 | : "Receiver Resolver Table:", " ", packageName, |
| 8587 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8588 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8589 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8590 | if (mServices.dump(pw, dumpState.getTitlePrinted() ? "\nService Resolver Table:" |
| 8591 | : "Service Resolver Table:", " ", packageName, |
| 8592 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8593 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8594 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8595 | if (mSettings.mPreferredActivities.dump(pw, |
| 8596 | dumpState.getTitlePrinted() ? "\nPreferred Activities:" |
| 8597 | : "Preferred Activities:", " ", |
| 8598 | packageName, dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8599 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8600 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8601 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8602 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8603 | if (dumpState.isDumping(DumpState.DUMP_PERMISSIONS)) { |
| 8604 | mSettings.dumpPermissionsLPr(pw, packageName, dumpState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8605 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8606 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8607 | if (dumpState.isDumping(DumpState.DUMP_PROVIDERS)) { |
| 8608 | boolean printedSomething = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8609 | for (PackageParser.Provider p : mProvidersByComponent.values()) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8610 | if (packageName != null && !packageName.equals(p.info.packageName)) { |
| 8611 | continue; |
| 8612 | } |
| 8613 | if (!printedSomething) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8614 | if (dumpState.onTitlePrinted()) |
| 8615 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8616 | pw.println("Registered ContentProviders:"); |
| 8617 | printedSomething = true; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8618 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8619 | pw.print(" "); pw.print(p.getComponentShortName()); pw.println(":"); |
| 8620 | pw.print(" "); pw.println(p.toString()); |
| 8621 | } |
| 8622 | printedSomething = false; |
| 8623 | for (Map.Entry<String, PackageParser.Provider> entry : mProviders.entrySet()) { |
| 8624 | PackageParser.Provider p = entry.getValue(); |
| 8625 | if (packageName != null && !packageName.equals(p.info.packageName)) { |
| 8626 | continue; |
| 8627 | } |
| 8628 | if (!printedSomething) { |
| 8629 | if (dumpState.onTitlePrinted()) |
| 8630 | pw.println(" "); |
| 8631 | pw.println("ContentProvider Authorities:"); |
| 8632 | printedSomething = true; |
| 8633 | } |
| 8634 | pw.print(" ["); pw.print(entry.getKey()); pw.println("]:"); |
| 8635 | pw.print(" "); pw.println(p.toString()); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8636 | } |
| 8637 | } |
| 8638 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8639 | if (dumpState.isDumping(DumpState.DUMP_PACKAGES)) { |
| 8640 | mSettings.dumpPackagesLPr(pw, packageName, dumpState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8641 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8642 | |
| 8643 | if (dumpState.isDumping(DumpState.DUMP_SHARED_USERS)) { |
| 8644 | mSettings.dumpSharedUsersLPr(pw, packageName, dumpState); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8645 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8646 | |
| 8647 | if (dumpState.isDumping(DumpState.DUMP_MESSAGES) && packageName == null) { |
| 8648 | if (dumpState.onTitlePrinted()) |
| 8649 | pw.println(" "); |
| 8650 | mSettings.dumpReadMessagesLPr(pw, dumpState); |
| 8651 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8652 | pw.println(" "); |
| 8653 | pw.println("Package warning messages:"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8654 | final File fname = getSettingsProblemFile(); |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 8655 | FileInputStream in = null; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8656 | try { |
| 8657 | in = new FileInputStream(fname); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8658 | final int avail = in.available(); |
| 8659 | final byte[] data = new byte[avail]; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8660 | in.read(data); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8661 | pw.print(new String(data)); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8662 | } catch (FileNotFoundException e) { |
| 8663 | } catch (IOException e) { |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 8664 | } finally { |
| 8665 | if (in != null) { |
| 8666 | try { |
| 8667 | in.close(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8668 | } catch (IOException e) { |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 8669 | } |
| 8670 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8671 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 8672 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8673 | } |
| 8674 | } |
| 8675 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8676 | // ------- apps on sdcard specific code ------- |
| 8677 | static final boolean DEBUG_SD_INSTALL = false; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8678 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8679 | private static final String SD_ENCRYPTION_KEYSTORE_NAME = "AppsOnSD"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8680 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8681 | private static final String SD_ENCRYPTION_ALGORITHM = "AES"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8682 | |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8683 | private boolean mMediaMounted = false; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8684 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8685 | private String getEncryptKey() { |
| 8686 | try { |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8687 | String sdEncKey = SystemKeyStore.getInstance().retrieveKeyHexString( |
| 8688 | SD_ENCRYPTION_KEYSTORE_NAME); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8689 | if (sdEncKey == null) { |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8690 | sdEncKey = SystemKeyStore.getInstance().generateNewKeyHexString(128, |
| 8691 | SD_ENCRYPTION_ALGORITHM, SD_ENCRYPTION_KEYSTORE_NAME); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8692 | if (sdEncKey == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8693 | Slog.e(TAG, "Failed to create encryption keys"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8694 | return null; |
| 8695 | } |
| 8696 | } |
| 8697 | return sdEncKey; |
| 8698 | } catch (NoSuchAlgorithmException nsae) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8699 | Slog.e(TAG, "Failed to create encryption keys with exception: " + nsae); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8700 | return null; |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 8701 | } catch (IOException ioe) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8702 | Slog.e(TAG, "Failed to retrieve encryption keys with exception: " + ioe); |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 8703 | return null; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8704 | } |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 8705 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8706 | } |
| 8707 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8708 | /* package */static String getTempContainerId() { |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 8709 | int tmpIdx = 1; |
| 8710 | String list[] = PackageHelper.getSecureContainerList(); |
| 8711 | if (list != null) { |
| 8712 | for (final String name : list) { |
| 8713 | // Ignore null and non-temporary container entries |
| 8714 | if (name == null || !name.startsWith(mTempContainerPrefix)) { |
| 8715 | continue; |
| 8716 | } |
| 8717 | |
| 8718 | String subStr = name.substring(mTempContainerPrefix.length()); |
| 8719 | try { |
| 8720 | int cid = Integer.parseInt(subStr); |
| 8721 | if (cid >= tmpIdx) { |
| 8722 | tmpIdx = cid + 1; |
| 8723 | } |
| 8724 | } catch (NumberFormatException e) { |
| 8725 | } |
| 8726 | } |
| 8727 | } |
| 8728 | return mTempContainerPrefix + tmpIdx; |
| 8729 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8730 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8731 | /* |
| 8732 | * Update media status on PackageManager. |
| 8733 | */ |
| 8734 | public void updateExternalMediaStatus(final boolean mediaStatus, final boolean reportStatus) { |
| 8735 | int callingUid = Binder.getCallingUid(); |
| 8736 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 8737 | throw new SecurityException("Media status can only be updated by the system"); |
| 8738 | } |
| 8739 | // reader; this apparently protects mMediaMounted, but should probably |
| 8740 | // be a different lock in that case. |
| 8741 | synchronized (mPackages) { |
| 8742 | Log.i(TAG, "Updating external media status from " |
| 8743 | + (mMediaMounted ? "mounted" : "unmounted") + " to " |
| 8744 | + (mediaStatus ? "mounted" : "unmounted")); |
| 8745 | if (DEBUG_SD_INSTALL) |
| 8746 | Log.i(TAG, "updateExternalMediaStatus:: mediaStatus=" + mediaStatus |
| 8747 | + ", mMediaMounted=" + mMediaMounted); |
| 8748 | if (mediaStatus == mMediaMounted) { |
| 8749 | final Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, reportStatus ? 1 |
| 8750 | : 0, -1); |
| 8751 | mHandler.sendMessage(msg); |
| 8752 | return; |
| 8753 | } |
| 8754 | mMediaMounted = mediaStatus; |
| 8755 | } |
| 8756 | // Queue up an async operation since the package installation may take a |
| 8757 | // little while. |
| 8758 | mHandler.post(new Runnable() { |
| 8759 | public void run() { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8760 | updateExternalMediaStatusInner(mediaStatus, reportStatus); |
| 8761 | } |
| 8762 | }); |
| 8763 | } |
| 8764 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 8765 | /** |
| 8766 | * Called by MountService when the initial ASECs to scan are available. |
| 8767 | * Should block until all the ASEC containers are finished being scanned. |
| 8768 | */ |
| 8769 | public void scanAvailableAsecs() { |
| 8770 | updateExternalMediaStatusInner(true, false); |
| 8771 | } |
| 8772 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8773 | /* |
| 8774 | * Collect information of applications on external media, map them against |
| 8775 | * existing containers and update information based on current mount status. |
| 8776 | * Please note that we always have to report status if reportStatus has been |
| 8777 | * set to true especially when unloading packages. |
| 8778 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8779 | private void updateExternalMediaStatusInner(boolean isMounted, boolean reportStatus) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8780 | // Collection of uids |
| 8781 | int uidArr[] = null; |
| 8782 | // Collection of stale containers |
| 8783 | HashSet<String> removeCids = new HashSet<String>(); |
| 8784 | // Collection of packages on external media with valid containers. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8785 | HashMap<AsecInstallArgs, String> processCids = new HashMap<AsecInstallArgs, String>(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8786 | // Get list of secure containers. |
| 8787 | final String list[] = PackageHelper.getSecureContainerList(); |
| 8788 | if (list == null || list.length == 0) { |
| 8789 | Log.i(TAG, "No secure containers on sdcard"); |
| 8790 | } else { |
| 8791 | // Process list of secure containers and categorize them |
| 8792 | // as active or stale based on their package internal state. |
| 8793 | int uidList[] = new int[list.length]; |
| 8794 | int num = 0; |
| 8795 | // reader |
| 8796 | synchronized (mPackages) { |
| 8797 | for (String cid : list) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8798 | if (DEBUG_SD_INSTALL) |
| 8799 | Log.i(TAG, "Processing container " + cid); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8800 | String pkgName = getAsecPackageName(cid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8801 | if (pkgName == null) { |
| 8802 | if (DEBUG_SD_INSTALL) |
| 8803 | Log.i(TAG, "Container : " + cid + " stale"); |
| 8804 | removeCids.add(cid); |
| 8805 | continue; |
| 8806 | } |
| 8807 | if (DEBUG_SD_INSTALL) |
| 8808 | Log.i(TAG, "Looking for pkg : " + pkgName); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8809 | |
| 8810 | final PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 8811 | if (ps == null) { |
| 8812 | Log.i(TAG, "Deleting container with no matching settings " + cid); |
| 8813 | removeCids.add(cid); |
| 8814 | continue; |
| 8815 | } |
| 8816 | |
| 8817 | final AsecInstallArgs args = new AsecInstallArgs(cid, isForwardLocked(ps)); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8818 | // The package status is changed only if the code path |
| 8819 | // matches between settings and the container id. |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8820 | if (ps.codePathString != null && ps.codePathString.equals(args.getCodePath())) { |
| 8821 | if (DEBUG_SD_INSTALL) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8822 | Log.i(TAG, "Container : " + cid + " corresponds to pkg : " + pkgName |
| 8823 | + " at code path: " + ps.codePathString); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8824 | } |
| 8825 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8826 | // We do have a valid package installed on sdcard |
| 8827 | processCids.put(args, ps.codePathString); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8828 | final int uid = ps.appId; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8829 | if (uid != -1) { |
| 8830 | uidList[num++] = uid; |
| 8831 | } |
| 8832 | } else { |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8833 | Log.i(TAG, "Deleting stale container for " + cid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8834 | removeCids.add(cid); |
| 8835 | } |
| 8836 | } |
| 8837 | } |
| 8838 | |
| 8839 | if (num > 0) { |
| 8840 | // Sort uid list |
| 8841 | Arrays.sort(uidList, 0, num); |
| 8842 | // Throw away duplicates |
| 8843 | uidArr = new int[num]; |
| 8844 | uidArr[0] = uidList[0]; |
| 8845 | int di = 0; |
| 8846 | for (int i = 1; i < num; i++) { |
| 8847 | if (uidList[i - 1] != uidList[i]) { |
| 8848 | uidArr[di++] = uidList[i]; |
| 8849 | } |
| 8850 | } |
| 8851 | } |
| 8852 | } |
| 8853 | // Process packages with valid entries. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8854 | if (isMounted) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8855 | if (DEBUG_SD_INSTALL) |
| 8856 | Log.i(TAG, "Loading packages"); |
| 8857 | loadMediaPackages(processCids, uidArr, removeCids); |
| 8858 | startCleaningPackages(); |
| 8859 | } else { |
| 8860 | if (DEBUG_SD_INSTALL) |
| 8861 | Log.i(TAG, "Unloading packages"); |
| 8862 | unloadMediaPackages(processCids, uidArr, reportStatus); |
| 8863 | } |
| 8864 | } |
| 8865 | |
| 8866 | private void sendResourcesChangedBroadcast(boolean mediaStatus, ArrayList<String> pkgList, |
| 8867 | int uidArr[], IIntentReceiver finishedReceiver) { |
| 8868 | int size = pkgList.size(); |
| 8869 | if (size > 0) { |
| 8870 | // Send broadcasts here |
| 8871 | Bundle extras = new Bundle(); |
| 8872 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, pkgList |
| 8873 | .toArray(new String[size])); |
| 8874 | if (uidArr != null) { |
| 8875 | extras.putIntArray(Intent.EXTRA_CHANGED_UID_LIST, uidArr); |
| 8876 | } |
| 8877 | String action = mediaStatus ? Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE |
| 8878 | : Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8879 | sendPackageBroadcast(action, null, extras, null, finishedReceiver, UserId.USER_ALL); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8880 | } |
| 8881 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 8882 | |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 8883 | /* |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8884 | * Look at potentially valid container ids from processCids If package |
| 8885 | * information doesn't match the one on record or package scanning fails, |
| 8886 | * the cid is added to list of removeCids. We currently don't delete stale |
| 8887 | * containers. |
| 8888 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8889 | private void loadMediaPackages(HashMap<AsecInstallArgs, String> processCids, int uidArr[], |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8890 | HashSet<String> removeCids) { |
| 8891 | ArrayList<String> pkgList = new ArrayList<String>(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8892 | Set<AsecInstallArgs> keys = processCids.keySet(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8893 | boolean doGc = false; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8894 | for (AsecInstallArgs args : keys) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8895 | String codePath = processCids.get(args); |
| 8896 | if (DEBUG_SD_INSTALL) |
| 8897 | Log.i(TAG, "Loading container : " + args.cid); |
| 8898 | int retCode = PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 8899 | try { |
| 8900 | // Make sure there are no container errors first. |
| 8901 | if (args.doPreInstall(PackageManager.INSTALL_SUCCEEDED) != PackageManager.INSTALL_SUCCEEDED) { |
| 8902 | Slog.e(TAG, "Failed to mount cid : " + args.cid |
| 8903 | + " when installing from sdcard"); |
| 8904 | continue; |
| 8905 | } |
| 8906 | // Check code path here. |
| 8907 | if (codePath == null || !codePath.equals(args.getCodePath())) { |
| 8908 | Slog.e(TAG, "Container " + args.cid + " cachepath " + args.getCodePath() |
| 8909 | + " does not match one in settings " + codePath); |
| 8910 | continue; |
| 8911 | } |
| 8912 | // Parse package |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 8913 | int parseFlags = mDefParseFlags; |
| 8914 | if (args.isExternal()) { |
| 8915 | parseFlags |= PackageParser.PARSE_ON_SDCARD; |
| 8916 | } |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8917 | if (args.isFwdLocked()) { |
| 8918 | parseFlags |= PackageParser.PARSE_FORWARD_LOCK; |
| 8919 | } |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 8920 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8921 | doGc = true; |
| 8922 | synchronized (mInstallLock) { |
| 8923 | final PackageParser.Package pkg = scanPackageLI(new File(codePath), parseFlags, |
| 8924 | 0, 0); |
| 8925 | // Scan the package |
| 8926 | if (pkg != null) { |
| 8927 | /* |
| 8928 | * TODO why is the lock being held? doPostInstall is |
| 8929 | * called in other places without the lock. This needs |
| 8930 | * to be straightened out. |
| 8931 | */ |
| 8932 | // writer |
| 8933 | synchronized (mPackages) { |
| 8934 | retCode = PackageManager.INSTALL_SUCCEEDED; |
| 8935 | pkgList.add(pkg.packageName); |
| 8936 | // Post process args |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8937 | args.doPostInstall(PackageManager.INSTALL_SUCCEEDED, |
| 8938 | pkg.applicationInfo.uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8939 | } |
| 8940 | } else { |
| 8941 | Slog.i(TAG, "Failed to install pkg from " + codePath + " from sdcard"); |
| 8942 | } |
| 8943 | } |
| 8944 | |
| 8945 | } finally { |
| 8946 | if (retCode != PackageManager.INSTALL_SUCCEEDED) { |
| 8947 | // Don't destroy container here. Wait till gc clears things |
| 8948 | // up. |
| 8949 | removeCids.add(args.cid); |
| 8950 | } |
| 8951 | } |
| 8952 | } |
| 8953 | // writer |
| 8954 | synchronized (mPackages) { |
| 8955 | // If the platform SDK has changed since the last time we booted, |
| 8956 | // we need to re-grant app permission to catch any new ones that |
| 8957 | // appear. This is really a hack, and means that apps can in some |
| 8958 | // cases get permissions that the user didn't initially explicitly |
| 8959 | // allow... it would be nice to have some better way to handle |
| 8960 | // this situation. |
| 8961 | final boolean regrantPermissions = mSettings.mExternalSdkPlatform != mSdkVersion; |
| 8962 | if (regrantPermissions) |
| 8963 | Slog.i(TAG, "Platform changed from " + mSettings.mExternalSdkPlatform + " to " |
| 8964 | + mSdkVersion + "; regranting permissions for external storage"); |
| 8965 | mSettings.mExternalSdkPlatform = mSdkVersion; |
| 8966 | |
| 8967 | // Make sure group IDs have been assigned, and any permission |
| 8968 | // changes in other apps are accounted for |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 8969 | updatePermissionsLPw(null, null, UPDATE_PERMISSIONS_ALL |
| 8970 | | (regrantPermissions |
| 8971 | ? (UPDATE_PERMISSIONS_REPLACE_PKG|UPDATE_PERMISSIONS_REPLACE_ALL) |
| 8972 | : 0)); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8973 | // can downgrade to reader |
| 8974 | // Persist settings |
| 8975 | mSettings.writeLPr(); |
| 8976 | } |
| 8977 | // Send a broadcast to let everyone know we are done processing |
| 8978 | if (pkgList.size() > 0) { |
| 8979 | sendResourcesChangedBroadcast(true, pkgList, uidArr, null); |
| 8980 | } |
| 8981 | // Force gc to avoid any stale parser references that we might have. |
| 8982 | if (doGc) { |
| 8983 | Runtime.getRuntime().gc(); |
| 8984 | } |
| 8985 | // List stale containers and destroy stale temporary containers. |
| 8986 | if (removeCids != null) { |
| 8987 | for (String cid : removeCids) { |
| 8988 | if (cid.startsWith(mTempContainerPrefix)) { |
| 8989 | Log.i(TAG, "Destroying stale temporary container " + cid); |
| 8990 | PackageHelper.destroySdDir(cid); |
| 8991 | } else { |
| 8992 | Log.w(TAG, "Container " + cid + " is stale"); |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8993 | } |
| 8994 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8995 | } |
| 8996 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 8997 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 8998 | /* |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8999 | * Utility method to unload a list of specified containers |
| 9000 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9001 | private void unloadAllContainers(Set<AsecInstallArgs> cidArgs) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9002 | // Just unmount all valid containers. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9003 | for (AsecInstallArgs arg : cidArgs) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9004 | synchronized (mInstallLock) { |
| 9005 | arg.doPostDeleteLI(false); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9006 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9007 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9008 | } |
| 9009 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9010 | /* |
| 9011 | * Unload packages mounted on external media. This involves deleting package |
| 9012 | * data from internal structures, sending broadcasts about diabled packages, |
| 9013 | * gc'ing to free up references, unmounting all secure containers |
| 9014 | * corresponding to packages on external media, and posting a |
| 9015 | * UPDATED_MEDIA_STATUS message if status has been requested. Please note |
| 9016 | * that we always have to post this message if status has been requested no |
| 9017 | * matter what. |
| 9018 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9019 | private void unloadMediaPackages(HashMap<AsecInstallArgs, String> processCids, int uidArr[], |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9020 | final boolean reportStatus) { |
| 9021 | if (DEBUG_SD_INSTALL) |
| 9022 | Log.i(TAG, "unloading media packages"); |
| 9023 | ArrayList<String> pkgList = new ArrayList<String>(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9024 | ArrayList<AsecInstallArgs> failedList = new ArrayList<AsecInstallArgs>(); |
| 9025 | final Set<AsecInstallArgs> keys = processCids.keySet(); |
| 9026 | for (AsecInstallArgs args : keys) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9027 | String pkgName = args.getPackageName(); |
| 9028 | if (DEBUG_SD_INSTALL) |
| 9029 | Log.i(TAG, "Trying to unload pkg : " + pkgName); |
| 9030 | // Delete package internally |
| 9031 | PackageRemovedInfo outInfo = new PackageRemovedInfo(); |
| 9032 | synchronized (mInstallLock) { |
| 9033 | boolean res = deletePackageLI(pkgName, false, PackageManager.DONT_DELETE_DATA, |
| 9034 | outInfo, false); |
| 9035 | if (res) { |
| 9036 | pkgList.add(pkgName); |
| 9037 | } else { |
| 9038 | Slog.e(TAG, "Failed to delete pkg from sdcard : " + pkgName); |
| 9039 | failedList.add(args); |
| 9040 | } |
| 9041 | } |
| 9042 | } |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 9043 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9044 | // reader |
| 9045 | synchronized (mPackages) { |
| 9046 | // We didn't update the settings after removing each package; |
| 9047 | // write them now for all packages. |
| 9048 | mSettings.writeLPr(); |
| 9049 | } |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 9050 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9051 | // We have to absolutely send UPDATED_MEDIA_STATUS only |
| 9052 | // after confirming that all the receivers processed the ordered |
| 9053 | // broadcast when packages get disabled, force a gc to clean things up. |
| 9054 | // and unload all the containers. |
| 9055 | if (pkgList.size() > 0) { |
| 9056 | sendResourcesChangedBroadcast(false, pkgList, uidArr, new IIntentReceiver.Stub() { |
| 9057 | public void performReceive(Intent intent, int resultCode, String data, |
| 9058 | Bundle extras, boolean ordered, boolean sticky) throws RemoteException { |
| 9059 | Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, |
| 9060 | reportStatus ? 1 : 0, 1, keys); |
| 9061 | mHandler.sendMessage(msg); |
| 9062 | } |
| 9063 | }); |
| 9064 | } else { |
| 9065 | Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, reportStatus ? 1 : 0, -1, |
| 9066 | keys); |
| 9067 | mHandler.sendMessage(msg); |
| 9068 | } |
| 9069 | } |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 9070 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9071 | public void movePackage(final String packageName, final IPackageMoveObserver observer, |
| 9072 | final int flags) { |
| 9073 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MOVE_PACKAGE, null); |
| 9074 | int returnCode = PackageManager.MOVE_SUCCEEDED; |
| 9075 | int currFlags = 0; |
| 9076 | int newFlags = 0; |
| 9077 | // reader |
| 9078 | synchronized (mPackages) { |
| 9079 | PackageParser.Package pkg = mPackages.get(packageName); |
| 9080 | if (pkg == null) { |
| 9081 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
| 9082 | } else { |
| 9083 | // Disable moving fwd locked apps and system packages |
| 9084 | if (pkg.applicationInfo != null && isSystemApp(pkg)) { |
| 9085 | Slog.w(TAG, "Cannot move system application"); |
| 9086 | returnCode = PackageManager.MOVE_FAILED_SYSTEM_PACKAGE; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9087 | } else if (pkg.mOperationPending) { |
| 9088 | Slog.w(TAG, "Attempt to move package which has pending operations"); |
| 9089 | returnCode = PackageManager.MOVE_FAILED_OPERATION_PENDING; |
| 9090 | } else { |
| 9091 | // Find install location first |
| 9092 | if ((flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 |
| 9093 | && (flags & PackageManager.MOVE_INTERNAL) != 0) { |
| 9094 | Slog.w(TAG, "Ambigous flags specified for move location."); |
| 9095 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
| 9096 | } else { |
| 9097 | newFlags = (flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 ? PackageManager.INSTALL_EXTERNAL |
| 9098 | : PackageManager.INSTALL_INTERNAL; |
| 9099 | currFlags = isExternal(pkg) ? PackageManager.INSTALL_EXTERNAL |
| 9100 | : PackageManager.INSTALL_INTERNAL; |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9101 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9102 | if (newFlags == currFlags) { |
| 9103 | Slog.w(TAG, "No move required. Trying to move to same location"); |
| 9104 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9105 | } else { |
| 9106 | if (isForwardLocked(pkg)) { |
| 9107 | currFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 9108 | newFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 9109 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9110 | } |
| 9111 | } |
| 9112 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 9113 | pkg.mOperationPending = true; |
| 9114 | } |
| 9115 | } |
| 9116 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9117 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9118 | /* |
| 9119 | * TODO this next block probably shouldn't be inside the lock. We |
| 9120 | * can't guarantee these won't change after this is fired off |
| 9121 | * anyway. |
| 9122 | */ |
| 9123 | if (returnCode != PackageManager.MOVE_SUCCEEDED) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9124 | processPendingMove(new MoveParams(null, observer, 0, packageName, null, -1), |
| 9125 | returnCode); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9126 | } else { |
| 9127 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| 9128 | InstallArgs srcArgs = createInstallArgs(currFlags, pkg.applicationInfo.sourceDir, |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 9129 | pkg.applicationInfo.publicSourceDir, pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9130 | MoveParams mp = new MoveParams(srcArgs, observer, newFlags, packageName, |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9131 | pkg.applicationInfo.dataDir, pkg.applicationInfo.uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9132 | msg.obj = mp; |
| 9133 | mHandler.sendMessage(msg); |
| 9134 | } |
| 9135 | } |
| 9136 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9137 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9138 | private void processPendingMove(final MoveParams mp, final int currentStatus) { |
| 9139 | // Queue up an async operation since the package deletion may take a |
| 9140 | // little while. |
| 9141 | mHandler.post(new Runnable() { |
| 9142 | public void run() { |
| 9143 | // TODO fix this; this does nothing. |
| 9144 | mHandler.removeCallbacks(this); |
| 9145 | int returnCode = currentStatus; |
| 9146 | if (currentStatus == PackageManager.MOVE_SUCCEEDED) { |
| 9147 | int uidArr[] = null; |
| 9148 | ArrayList<String> pkgList = null; |
| 9149 | synchronized (mPackages) { |
| 9150 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 9151 | if (pkg == null) { |
| 9152 | Slog.w(TAG, " Package " + mp.packageName |
| 9153 | + " doesn't exist. Aborting move"); |
| 9154 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
| 9155 | } else if (!mp.srcArgs.getCodePath().equals(pkg.applicationInfo.sourceDir)) { |
| 9156 | Slog.w(TAG, "Package " + mp.packageName + " code path changed from " |
| 9157 | + mp.srcArgs.getCodePath() + " to " |
| 9158 | + pkg.applicationInfo.sourceDir |
| 9159 | + " Aborting move and returning error"); |
| 9160 | returnCode = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 9161 | } else { |
| 9162 | uidArr = new int[] { |
| 9163 | pkg.applicationInfo.uid |
| 9164 | }; |
| 9165 | pkgList = new ArrayList<String>(); |
| 9166 | pkgList.add(mp.packageName); |
| 9167 | } |
| 9168 | } |
| 9169 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 9170 | // Send resources unavailable broadcast |
| 9171 | sendResourcesChangedBroadcast(false, pkgList, uidArr, null); |
| 9172 | // Update package code and resource paths |
| 9173 | synchronized (mInstallLock) { |
| 9174 | synchronized (mPackages) { |
| 9175 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 9176 | // Recheck for package again. |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9177 | if (pkg == null) { |
| 9178 | Slog.w(TAG, " Package " + mp.packageName |
| 9179 | + " doesn't exist. Aborting move"); |
| 9180 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9181 | } else if (!mp.srcArgs.getCodePath().equals( |
| 9182 | pkg.applicationInfo.sourceDir)) { |
| 9183 | Slog.w(TAG, "Package " + mp.packageName |
| 9184 | + " code path changed from " + mp.srcArgs.getCodePath() |
| 9185 | + " to " + pkg.applicationInfo.sourceDir |
| 9186 | + " Aborting move and returning error"); |
| 9187 | returnCode = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 9188 | } else { |
| 9189 | final String oldCodePath = pkg.mPath; |
| 9190 | final String newCodePath = mp.targetArgs.getCodePath(); |
| 9191 | final String newResPath = mp.targetArgs.getResourcePath(); |
| 9192 | final String newNativePath = mp.targetArgs |
| 9193 | .getNativeLibraryPath(); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9194 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9195 | try { |
| 9196 | final File newNativeDir = new File(newNativePath); |
| 9197 | |
| 9198 | final String libParentDir = newNativeDir.getParentFile() |
| 9199 | .getCanonicalPath(); |
| 9200 | if (newNativeDir.getParentFile().getCanonicalPath() |
| 9201 | .equals(pkg.applicationInfo.dataDir)) { |
| 9202 | if (mInstaller |
| 9203 | .unlinkNativeLibraryDirectory(pkg.applicationInfo.dataDir) < 0) { |
| 9204 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 9205 | } else { |
| 9206 | NativeLibraryHelper.copyNativeBinariesIfNeededLI( |
| 9207 | new File(newCodePath), newNativeDir); |
| 9208 | } |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9209 | } else { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9210 | if (mInstaller.linkNativeLibraryDirectory( |
| 9211 | pkg.applicationInfo.dataDir, newNativePath) < 0) { |
| 9212 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 9213 | } |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9214 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9215 | } catch (IOException e) { |
| 9216 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9217 | } |
| 9218 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9219 | |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9220 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 9221 | pkg.mPath = newCodePath; |
| 9222 | // Move dex files around |
| 9223 | if (moveDexFilesLI(pkg) != PackageManager.INSTALL_SUCCEEDED) { |
| 9224 | // Moving of dex files failed. Set |
| 9225 | // error code and abort move. |
| 9226 | pkg.mPath = pkg.mScanPath; |
| 9227 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 9228 | } |
| 9229 | } |
| 9230 | |
| 9231 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9232 | pkg.mScanPath = newCodePath; |
| 9233 | pkg.applicationInfo.sourceDir = newCodePath; |
| 9234 | pkg.applicationInfo.publicSourceDir = newResPath; |
| 9235 | pkg.applicationInfo.nativeLibraryDir = newNativePath; |
| 9236 | PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 9237 | ps.codePath = new File(pkg.applicationInfo.sourceDir); |
| 9238 | ps.codePathString = ps.codePath.getPath(); |
| 9239 | ps.resourcePath = new File( |
| 9240 | pkg.applicationInfo.publicSourceDir); |
| 9241 | ps.resourcePathString = ps.resourcePath.getPath(); |
| 9242 | ps.nativeLibraryPathString = newNativePath; |
| 9243 | // Set the application info flag |
| 9244 | // correctly. |
| 9245 | if ((mp.flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
| 9246 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| 9247 | } else { |
| 9248 | pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| 9249 | } |
| 9250 | ps.setFlags(pkg.applicationInfo.flags); |
| 9251 | mAppDirs.remove(oldCodePath); |
| 9252 | mAppDirs.put(newCodePath, pkg); |
| 9253 | // Persist settings |
| 9254 | mSettings.writeLPr(); |
| 9255 | } |
| 9256 | } |
| 9257 | } |
| 9258 | } |
| 9259 | // Send resources available broadcast |
| 9260 | sendResourcesChangedBroadcast(true, pkgList, uidArr, null); |
| 9261 | } |
| 9262 | } |
| 9263 | if (returnCode != PackageManager.MOVE_SUCCEEDED) { |
| 9264 | // Clean up failed installation |
| 9265 | if (mp.targetArgs != null) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9266 | mp.targetArgs.doPostInstall(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, |
| 9267 | -1); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9268 | } |
| 9269 | } else { |
| 9270 | // Force a gc to clear things up. |
| 9271 | Runtime.getRuntime().gc(); |
| 9272 | // Delete older code |
| 9273 | synchronized (mInstallLock) { |
| 9274 | mp.srcArgs.doPostDeleteLI(true); |
| 9275 | } |
| 9276 | } |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 9277 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9278 | // Allow more operations on this file if we didn't fail because |
| 9279 | // an operation was already pending for this package. |
| 9280 | if (returnCode != PackageManager.MOVE_FAILED_OPERATION_PENDING) { |
| 9281 | synchronized (mPackages) { |
| 9282 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 9283 | if (pkg != null) { |
| 9284 | pkg.mOperationPending = false; |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 9285 | } |
| 9286 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9287 | } |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 9288 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9289 | IPackageMoveObserver observer = mp.observer; |
| 9290 | if (observer != null) { |
| 9291 | try { |
| 9292 | observer.packageMoved(mp.packageName, returnCode); |
| 9293 | } catch (RemoteException e) { |
| 9294 | Log.i(TAG, "Observer no longer exists."); |
| 9295 | } |
| 9296 | } |
| 9297 | } |
| 9298 | }); |
| 9299 | } |
| 9300 | |
| 9301 | public boolean setInstallLocation(int loc) { |
| 9302 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS, |
| 9303 | null); |
| 9304 | if (getInstallLocation() == loc) { |
| 9305 | return true; |
| 9306 | } |
| 9307 | if (loc == PackageHelper.APP_INSTALL_AUTO || loc == PackageHelper.APP_INSTALL_INTERNAL |
| 9308 | || loc == PackageHelper.APP_INSTALL_EXTERNAL) { |
| 9309 | android.provider.Settings.System.putInt(mContext.getContentResolver(), |
| 9310 | android.provider.Settings.Secure.DEFAULT_INSTALL_LOCATION, loc); |
| 9311 | return true; |
| 9312 | } |
| 9313 | return false; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9314 | } |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 9315 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9316 | public int getInstallLocation() { |
| 9317 | return android.provider.Settings.System.getInt(mContext.getContentResolver(), |
| 9318 | android.provider.Settings.Secure.DEFAULT_INSTALL_LOCATION, |
| 9319 | PackageHelper.APP_INSTALL_AUTO); |
| 9320 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9321 | |
| 9322 | public UserInfo createUser(String name, int flags) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 9323 | // TODO(kroot): Add a real permission for creating users |
| 9324 | enforceSystemOrRoot("Only the system can create users"); |
| 9325 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9326 | UserInfo userInfo = sUserManager.createUser(name, flags); |
| 9327 | if (userInfo != null) { |
| 9328 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 9329 | addedIntent.putExtra(Intent.EXTRA_USERID, userInfo.id); |
| 9330 | mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_ACCOUNTS); |
| 9331 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9332 | return userInfo; |
| 9333 | } |
| 9334 | |
| 9335 | public boolean removeUser(int userId) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 9336 | // TODO(kroot): Add a real permission for removing users |
| 9337 | enforceSystemOrRoot("Only the system can remove users"); |
| 9338 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9339 | if (userId == 0 || !sUserManager.exists(userId)) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9340 | return false; |
| 9341 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9342 | |
| 9343 | cleanUpUser(userId); |
| 9344 | |
| 9345 | if (sUserManager.removeUser(userId)) { |
| 9346 | // Let other services shutdown any activity |
| 9347 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 9348 | addedIntent.putExtra(Intent.EXTRA_USERID, userId); |
| 9349 | mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_ACCOUNTS); |
| 9350 | } |
| 9351 | sUserManager.removePackageFolders(userId); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9352 | return true; |
| 9353 | } |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 9354 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9355 | private void cleanUpUser(int userId) { |
| 9356 | // Disable all the packages for the user first |
| 9357 | synchronized (mPackages) { |
| 9358 | Set<Entry<String, PackageSetting>> entries = mSettings.mPackages.entrySet(); |
| 9359 | for (Entry<String, PackageSetting> entry : entries) { |
| 9360 | entry.getValue().removeUser(userId); |
| 9361 | } |
| 9362 | if (mDirtyUsers.remove(userId)); |
| 9363 | mSettings.removeUserLPr(userId); |
| 9364 | } |
| 9365 | } |
| 9366 | |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 9367 | @Override |
| 9368 | public VerifierDeviceIdentity getVerifierDeviceIdentity() throws RemoteException { |
| 9369 | mContext.enforceCallingOrSelfPermission( |
| 9370 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 9371 | "Only package verification agents can read the verifier device identity"); |
| 9372 | |
| 9373 | synchronized (mPackages) { |
| 9374 | return mSettings.getVerifierDeviceIdentityLPw(); |
| 9375 | } |
| 9376 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9377 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9378 | @Override |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9379 | public List<UserInfo> getUsers() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9380 | enforceSystemOrRoot("Only the system can query users"); |
| 9381 | return sUserManager.getUsers(); |
| 9382 | } |
| 9383 | |
| 9384 | @Override |
| 9385 | public UserInfo getUser(int userId) { |
Amith Yamasani | 5bb87cd | 2012-06-14 11:32:13 -0700 | [diff] [blame^] | 9386 | enforceSystemOrRoot("Only the system can query user"); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9387 | return sUserManager.getUser(userId); |
| 9388 | } |
| 9389 | |
| 9390 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 9391 | public void setUserName(int userId, String name) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9392 | enforceSystemOrRoot("Only the system can rename users"); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 9393 | sUserManager.setUserName(userId, name); |
| 9394 | } |
| 9395 | |
| 9396 | @Override |
| 9397 | public ParcelFileDescriptor setUserIcon(int userId) { |
| 9398 | enforceSystemOrRoot("Only the system can update users"); |
| 9399 | return sUserManager.setUserIcon(userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9400 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9401 | |
| 9402 | @Override |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9403 | public void setPermissionEnforced(String permission, boolean enforced) { |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9404 | mContext.enforceCallingOrSelfPermission(GRANT_REVOKE_PERMISSIONS, null); |
| 9405 | if (READ_EXTERNAL_STORAGE.equals(permission)) { |
| 9406 | synchronized (mPackages) { |
Jeff Sharkey | f538577 | 2012-05-11 14:04:41 -0700 | [diff] [blame] | 9407 | if (mSettings.mReadExternalStorageEnforced == null |
| 9408 | || mSettings.mReadExternalStorageEnforced != enforced) { |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9409 | mSettings.mReadExternalStorageEnforced = enforced; |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9410 | mSettings.writeLPr(); |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 9411 | |
| 9412 | // kill any non-foreground processes so we restart them and |
| 9413 | // grant/revoke the GID. |
| 9414 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 9415 | if (am != null) { |
| 9416 | final long token = Binder.clearCallingIdentity(); |
| 9417 | try { |
| 9418 | am.killProcessesBelowForeground("setPermissionEnforcement"); |
| 9419 | } catch (RemoteException e) { |
| 9420 | } finally { |
| 9421 | Binder.restoreCallingIdentity(token); |
| 9422 | } |
| 9423 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9424 | } |
| 9425 | } |
| 9426 | } else { |
| 9427 | throw new IllegalArgumentException("No selective enforcement for " + permission); |
| 9428 | } |
| 9429 | } |
| 9430 | |
| 9431 | @Override |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9432 | public boolean isPermissionEnforced(String permission) { |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9433 | synchronized (mPackages) { |
| 9434 | return isPermissionEnforcedLocked(permission); |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9435 | } |
| 9436 | } |
| 9437 | |
| 9438 | private boolean isPermissionEnforcedLocked(String permission) { |
| 9439 | if (READ_EXTERNAL_STORAGE.equals(permission)) { |
Jeff Sharkey | f538577 | 2012-05-11 14:04:41 -0700 | [diff] [blame] | 9440 | if (mSettings.mReadExternalStorageEnforced != null) { |
| 9441 | return mSettings.mReadExternalStorageEnforced; |
| 9442 | } else { |
| 9443 | // if user hasn't defined, fall back to secure default |
| 9444 | return Secure.getInt(mContext.getContentResolver(), |
| 9445 | Secure.READ_EXTERNAL_STORAGE_ENFORCED_DEFAULT, 0) != 0; |
| 9446 | } |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9447 | } else { |
| 9448 | return true; |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9449 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9450 | } |
Jeff Sharkey | f6f7f1d | 2012-06-12 17:12:08 -0700 | [diff] [blame] | 9451 | |
| 9452 | public boolean isStorageLow() { |
| 9453 | final long token = Binder.clearCallingIdentity(); |
| 9454 | try { |
| 9455 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 9456 | .getService(DeviceStorageMonitorService.SERVICE); |
| 9457 | return dsm.isMemoryLow(); |
| 9458 | } finally { |
| 9459 | Binder.restoreCallingIdentity(token); |
| 9460 | } |
| 9461 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9462 | } |