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 | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 33 | import com.android.internal.util.FastXmlSerializer; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 34 | import com.android.internal.util.XmlUtils; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 35 | import com.android.server.DeviceStorageMonitorService; |
| 36 | import com.android.server.EventLogTags; |
| 37 | import com.android.server.IntentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 39 | import org.xmlpull.v1.XmlPullParser; |
| 40 | import org.xmlpull.v1.XmlPullParserException; |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 41 | import org.xmlpull.v1.XmlSerializer; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.app.ActivityManagerNative; |
| 44 | import android.app.IActivityManager; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 45 | import android.app.admin.IDevicePolicyManager; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 46 | import android.app.backup.IBackupManager; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 47 | import android.content.BroadcastReceiver; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 48 | import android.content.ComponentName; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 49 | import android.content.Context; |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 50 | import android.content.IIntentReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.content.Intent; |
| 52 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 53 | import android.content.IntentSender; |
Jason parks | 1125d78 | 2011-01-12 09:47:26 -0600 | [diff] [blame] | 54 | import android.content.ServiceConnection; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 55 | import android.content.IntentSender.SendIntentException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.content.pm.ActivityInfo; |
| 57 | import android.content.pm.ApplicationInfo; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 58 | import android.content.pm.ContainerEncryptionParams; |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 59 | import android.content.pm.FeatureInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.content.pm.IPackageDataObserver; |
| 61 | import android.content.pm.IPackageDeleteObserver; |
| 62 | import android.content.pm.IPackageInstallObserver; |
| 63 | import android.content.pm.IPackageManager; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 64 | import android.content.pm.IPackageMoveObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.content.pm.IPackageStatsObserver; |
| 66 | import android.content.pm.InstrumentationInfo; |
| 67 | import android.content.pm.PackageInfo; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 68 | import android.content.pm.PackageInfoLite; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | import android.content.pm.PackageManager; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 70 | import android.content.pm.PackageParser; |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame^] | 71 | import android.content.pm.PackageParser.ActivityIntentInfo; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 72 | import android.content.pm.PackageStats; |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 73 | import android.content.pm.ParceledListSlice; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 74 | import android.content.pm.PermissionGroupInfo; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 75 | import android.content.pm.PermissionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.content.pm.ProviderInfo; |
| 77 | import android.content.pm.ResolveInfo; |
| 78 | import android.content.pm.ServiceInfo; |
| 79 | import android.content.pm.Signature; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 80 | import android.content.pm.UserInfo; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 81 | import android.content.pm.ManifestDigest; |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 82 | import android.content.pm.VerifierDeviceIdentity; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 83 | import android.content.pm.VerifierInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.net.Uri; |
| 85 | import android.os.Binder; |
Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 86 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import android.os.Bundle; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 88 | import android.os.Environment; |
| 89 | import android.os.FileObserver; |
| 90 | import android.os.FileUtils; |
| 91 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.os.HandlerThread; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 93 | import android.os.IBinder; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 94 | import android.os.Looper; |
| 95 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import android.os.ParcelFileDescriptor; |
| 98 | import android.os.Process; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 99 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.os.ServiceManager; |
| 101 | import android.os.SystemClock; |
| 102 | import android.os.SystemProperties; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 103 | import android.os.UserId; |
Jeff Sharkey | f538577 | 2012-05-11 14:04:41 -0700 | [diff] [blame] | 104 | import android.provider.Settings.Secure; |
Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 105 | import android.security.SystemKeyStore; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 106 | import android.util.DisplayMetrics; |
| 107 | import android.util.EventLog; |
| 108 | import android.util.Log; |
| 109 | import android.util.LogPrinter; |
| 110 | import android.util.Slog; |
| 111 | import android.util.SparseArray; |
| 112 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import android.view.Display; |
| 114 | import android.view.WindowManager; |
| 115 | |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 116 | import java.io.BufferedOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | import java.io.File; |
| 118 | import java.io.FileDescriptor; |
| 119 | import java.io.FileInputStream; |
| 120 | import java.io.FileNotFoundException; |
| 121 | import java.io.FileOutputStream; |
| 122 | import java.io.FileReader; |
| 123 | import java.io.FilenameFilter; |
| 124 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | import java.io.PrintWriter; |
Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 126 | import java.security.NoSuchAlgorithmException; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 127 | import java.security.PublicKey; |
| 128 | import java.security.cert.CertificateException; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 129 | import java.text.SimpleDateFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | import java.util.ArrayList; |
| 131 | import java.util.Arrays; |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 132 | import java.util.Collection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | import java.util.Collections; |
| 134 | import java.util.Comparator; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 135 | import java.util.Date; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | import java.util.HashMap; |
| 137 | import java.util.HashSet; |
| 138 | import java.util.Iterator; |
| 139 | import java.util.List; |
| 140 | import java.util.Map; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 141 | import java.util.Map.Entry; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | import java.util.Set; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 144 | import libcore.io.ErrnoException; |
Kenny Root | 3f99afc | 2012-04-14 10:13:10 -0700 | [diff] [blame] | 145 | import libcore.io.IoUtils; |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 146 | import libcore.io.Libcore; |
| 147 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Keep track of all those .apks everywhere. |
| 150 | * |
| 151 | * This is very central to the platform's security; please run the unit |
| 152 | * tests whenever making modifications here: |
| 153 | * |
| 154 | mmm frameworks/base/tests/AndroidTests |
| 155 | adb install -r -f out/target/product/passion/data/app/AndroidTests.apk |
| 156 | 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] | 157 | * |
| 158 | * {@hide} |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 159 | */ |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 160 | public class PackageManagerService extends IPackageManager.Stub { |
| 161 | static final String TAG = "PackageManager"; |
| 162 | static final boolean DEBUG_SETTINGS = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | private static final boolean DEBUG_PREFERRED = false; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 164 | static final boolean DEBUG_UPGRADE = false; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 165 | private static final boolean DEBUG_INSTALL = false; |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 166 | private static final boolean DEBUG_REMOVE = false; |
| 167 | private static final boolean DEBUG_SHOW_INFO = false; |
| 168 | private static final boolean DEBUG_PACKAGE_INFO = false; |
| 169 | private static final boolean DEBUG_INTENT_MATCHING = false; |
| 170 | private static final boolean DEBUG_PACKAGE_SCANNING = false; |
| 171 | private static final boolean DEBUG_APP_DIR_OBSERVER = false; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 172 | private static final boolean DEBUG_VERIFY = false; |
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 int RADIO_UID = Process.PHONE_UID; |
Mike Lockwood | d42685d | 2009-09-03 09:25:22 -0400 | [diff] [blame] | 175 | private static final int LOG_UID = Process.LOG_UID; |
Nick Pelly | cd0e839 | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 176 | private static final int NFC_UID = Process.NFC_UID; |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 177 | private static final int BLUETOOTH_UID = Process.BLUETOOTH_UID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | private static final boolean GET_CERTIFICATES = true; |
| 180 | |
| 181 | private static final int REMOVE_EVENTS = |
| 182 | FileObserver.CLOSE_WRITE | FileObserver.DELETE | FileObserver.MOVED_FROM; |
| 183 | private static final int ADD_EVENTS = |
| 184 | FileObserver.CLOSE_WRITE /*| FileObserver.CREATE*/ | FileObserver.MOVED_TO; |
| 185 | |
| 186 | private static final int OBSERVER_EVENTS = REMOVE_EVENTS | ADD_EVENTS; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 187 | // Suffix used during package installation when copying/moving |
| 188 | // package apks to install directory. |
| 189 | private static final String INSTALL_PACKAGE_SUFFIX = "-"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
| 191 | static final int SCAN_MONITOR = 1<<0; |
| 192 | static final int SCAN_NO_DEX = 1<<1; |
| 193 | static final int SCAN_FORCE_DEX = 1<<2; |
| 194 | static final int SCAN_UPDATE_SIGNATURE = 1<<3; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 195 | static final int SCAN_NEW_INSTALL = 1<<4; |
| 196 | static final int SCAN_NO_PATHS = 1<<5; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 197 | static final int SCAN_UPDATE_TIME = 1<<6; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 198 | static final int SCAN_DEFER_DEX = 1<<7; |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 199 | static final int SCAN_BOOTING = 1<<8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 201 | static final int REMOVE_CHATTY = 1<<16; |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 202 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 203 | /** |
| 204 | * Whether verification is enabled by default. |
| 205 | */ |
Brett Chabot | ae05e86 | 2011-08-24 18:38:26 -0700 | [diff] [blame] | 206 | // STOPSHIP: change this to true |
| 207 | private static final boolean DEFAULT_VERIFY_ENABLE = false; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * The default maximum time to wait for the verification agent to return in |
| 211 | * milliseconds. |
| 212 | */ |
rich cannings | aa8513e | 2012-05-21 16:15:23 -0700 | [diff] [blame] | 213 | private static final long DEFAULT_VERIFICATION_TIMEOUT = 10 * 1000; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 214 | |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 215 | /** |
| 216 | * The default response for package verification timeout. |
| 217 | * |
| 218 | * This can be either PackageManager.VERIFICATION_ALLOW or |
| 219 | * PackageManager.VERIFICATION_REJECT. |
| 220 | */ |
| 221 | private static final int DEFAULT_VERIFICATION_RESPONSE = PackageManager.VERIFICATION_ALLOW; |
| 222 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 223 | static final String DEFAULT_CONTAINER_PACKAGE = "com.android.defcontainer"; |
| 224 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 225 | static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName( |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 226 | DEFAULT_CONTAINER_PACKAGE, |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 227 | "com.android.defcontainer.DefaultContainerService"); |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 228 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 229 | private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive"; |
| 230 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 231 | private static final String LIB_DIR_NAME = "lib"; |
| 232 | |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 233 | static final String mTempContainerPrefix = "smdl2tmp"; |
| 234 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | final HandlerThread mHandlerThread = new HandlerThread("PackageManager", |
| 236 | Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 237 | final PackageHandler mHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | |
Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 239 | final int mSdkVersion = Build.VERSION.SDK_INT; |
| 240 | final String mSdkCodename = "REL".equals(Build.VERSION.CODENAME) |
| 241 | ? null : Build.VERSION.CODENAME; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 242 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | final Context mContext; |
| 244 | final boolean mFactoryTest; |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 245 | final boolean mOnlyCore; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 246 | final boolean mNoDexOpt; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | final DisplayMetrics mMetrics; |
| 248 | final int mDefParseFlags; |
| 249 | final String[] mSeparateProcesses; |
| 250 | |
| 251 | // This is where all application persistent data goes. |
| 252 | final File mAppDataDir; |
| 253 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 254 | // This is where all application persistent data goes for secondary users. |
| 255 | final File mUserAppDataDir; |
| 256 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 257 | /** The location for ASEC container files on internal storage. */ |
| 258 | final String mAsecInternalPath; |
| 259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | // This is the object monitoring the framework dir. |
| 261 | final FileObserver mFrameworkInstallObserver; |
| 262 | |
| 263 | // This is the object monitoring the system app dir. |
| 264 | final FileObserver mSystemInstallObserver; |
| 265 | |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 266 | // This is the object monitoring the system app dir. |
| 267 | final FileObserver mVendorInstallObserver; |
| 268 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | // This is the object monitoring mAppInstallDir. |
| 270 | final FileObserver mAppInstallObserver; |
| 271 | |
| 272 | // This is the object monitoring mDrmAppPrivateInstallDir. |
| 273 | final FileObserver mDrmAppInstallObserver; |
| 274 | |
| 275 | // Used for priviledge escalation. MUST NOT BE CALLED WITH mPackages |
| 276 | // LOCK HELD. Can be called with mInstallLock held. |
| 277 | final Installer mInstaller; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 278 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | final File mFrameworkDir; |
| 280 | final File mSystemAppDir; |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 281 | final File mVendorAppDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | final File mAppInstallDir; |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 283 | final File mDalvikCacheDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | |
| 285 | // Directory containing the private parts (e.g. code and non-resource assets) of forward-locked |
| 286 | // apps. |
| 287 | final File mDrmAppPrivateInstallDir; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | // ---------------------------------------------------------------- |
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 | // Lock for state used when installing and doing other long running |
| 292 | // operations. Methods that must be called with this lock held have |
| 293 | // the prefix "LI". |
| 294 | final Object mInstallLock = new Object(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 295 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | // These are the directories in the 3rd party applications installed dir |
| 297 | // that we have currently loaded packages from. Keys are the application's |
| 298 | // installed zip file (absolute codePath), and values are Package. |
| 299 | final HashMap<String, PackageParser.Package> mAppDirs = |
| 300 | new HashMap<String, PackageParser.Package>(); |
| 301 | |
| 302 | // Information for the parser to write more useful error messages. |
| 303 | File mScanningPath; |
| 304 | int mLastScanError; |
| 305 | |
| 306 | final int[] mOutPermissions = new int[3]; |
| 307 | |
| 308 | // ---------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | // Keys are String (package name), values are Package. This also serves |
| 311 | // as the lock for the global state. Methods that must be called with |
| 312 | // this lock held have the prefix "LP". |
| 313 | final HashMap<String, PackageParser.Package> mPackages = |
| 314 | new HashMap<String, PackageParser.Package>(); |
| 315 | |
| 316 | final Settings mSettings; |
| 317 | boolean mRestoredSettings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | |
| 319 | // Group-ids that are given to all packages as read from etc/permissions/*.xml. |
| 320 | int[] mGlobalGids; |
| 321 | |
| 322 | // These are the built-in uid -> permission mappings that were read from the |
| 323 | // etc/permissions.xml file. |
| 324 | final SparseArray<HashSet<String>> mSystemPermissions = |
| 325 | new SparseArray<HashSet<String>>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | // These are the built-in shared libraries that were read from the |
| 328 | // etc/permissions.xml file. |
| 329 | final HashMap<String, String> mSharedLibraries = new HashMap<String, String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 330 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 331 | // Temporary for building the final shared libraries for an .apk. |
| 332 | String[] mTmpSharedLibraries = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 333 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 334 | // These are the features this devices supports that were read from the |
| 335 | // etc/permissions.xml file. |
| 336 | final HashMap<String, FeatureInfo> mAvailableFeatures = |
| 337 | new HashMap<String, FeatureInfo>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 338 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | // All available activities, for your resolving pleasure. |
| 340 | final ActivityIntentResolver mActivities = |
| 341 | new ActivityIntentResolver(); |
| 342 | |
| 343 | // All available receivers, for your resolving pleasure. |
| 344 | final ActivityIntentResolver mReceivers = |
| 345 | new ActivityIntentResolver(); |
| 346 | |
| 347 | // All available services, for your resolving pleasure. |
| 348 | final ServiceIntentResolver mServices = new ServiceIntentResolver(); |
| 349 | |
| 350 | // Keys are String (provider class name), values are Provider. |
| 351 | final HashMap<ComponentName, PackageParser.Provider> mProvidersByComponent = |
| 352 | new HashMap<ComponentName, PackageParser.Provider>(); |
| 353 | |
| 354 | // Mapping from provider base names (first directory in content URI codePath) |
| 355 | // to the provider information. |
| 356 | final HashMap<String, PackageParser.Provider> mProviders = |
| 357 | new HashMap<String, PackageParser.Provider>(); |
| 358 | |
| 359 | // Mapping from instrumentation class names to info about them. |
| 360 | final HashMap<ComponentName, PackageParser.Instrumentation> mInstrumentation = |
| 361 | new HashMap<ComponentName, PackageParser.Instrumentation>(); |
| 362 | |
| 363 | // Mapping from permission names to info about them. |
| 364 | final HashMap<String, PackageParser.PermissionGroup> mPermissionGroups = |
| 365 | new HashMap<String, PackageParser.PermissionGroup>(); |
| 366 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 367 | // Packages whose data we have transfered into another package, thus |
| 368 | // should no longer exist. |
| 369 | final HashSet<String> mTransferedPackages = new HashSet<String>(); |
| 370 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 371 | // Broadcast actions that are only available to the system. |
| 372 | final HashSet<String> mProtectedBroadcasts = new HashSet<String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 373 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 374 | /** List of packages waiting for verification. */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 375 | final SparseArray<PackageVerificationState> mPendingVerification |
| 376 | = new SparseArray<PackageVerificationState>(); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 377 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 378 | final ArrayList<PackageParser.Package> mDeferredDexOpt = |
| 379 | new ArrayList<PackageParser.Package>(); |
| 380 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 381 | /** Token for keys in mPendingVerification. */ |
| 382 | private int mPendingVerificationToken = 0; |
| 383 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | boolean mSystemReady; |
| 385 | boolean mSafeMode; |
| 386 | boolean mHasSystemUidErrors; |
| 387 | |
| 388 | ApplicationInfo mAndroidApplication; |
| 389 | final ActivityInfo mResolveActivity = new ActivityInfo(); |
| 390 | final ResolveInfo mResolveInfo = new ResolveInfo(); |
| 391 | ComponentName mResolveComponentName; |
| 392 | PackageParser.Package mPlatformPackage; |
| 393 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 394 | // Set of pending broadcasts for aggregating enable/disable of components. |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 395 | final HashMap<String, ArrayList<String>> mPendingBroadcasts |
| 396 | = new HashMap<String, ArrayList<String>>(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 397 | // Service Connection to remote media container service to copy |
| 398 | // package uri's from external media onto secure containers |
| 399 | // or internal storage. |
| 400 | private IMediaContainerService mContainerService = null; |
| 401 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 402 | static final int SEND_PENDING_BROADCAST = 1; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 403 | static final int MCS_BOUND = 3; |
| 404 | static final int END_COPY = 4; |
| 405 | static final int INIT_COPY = 5; |
| 406 | static final int MCS_UNBIND = 6; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 407 | static final int START_CLEANING_PACKAGE = 7; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 408 | static final int FIND_INSTALL_LOC = 8; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 409 | static final int POST_INSTALL = 9; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 410 | static final int MCS_RECONNECT = 10; |
| 411 | static final int MCS_GIVE_UP = 11; |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 412 | static final int UPDATED_MEDIA_STATUS = 12; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 413 | static final int WRITE_SETTINGS = 13; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 414 | static final int WRITE_PACKAGE_RESTRICTIONS = 14; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 415 | static final int PACKAGE_VERIFIED = 15; |
| 416 | static final int CHECK_PENDING_VERIFICATION = 16; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 417 | |
| 418 | static final int WRITE_SETTINGS_DELAY = 10*1000; // 10 seconds |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 419 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 420 | // Delay time in millisecs |
| 421 | static final int BROADCAST_DELAY = 10 * 1000; |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 422 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 423 | static UserManager sUserManager; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 424 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 425 | // Stores a list of users whose package restrictions file needs to be updated |
| 426 | private HashSet<Integer> mDirtyUsers = new HashSet<Integer>(); |
| 427 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 428 | final private DefaultContainerConnection mDefContainerConn = |
| 429 | new DefaultContainerConnection(); |
| 430 | class DefaultContainerConnection implements ServiceConnection { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 431 | public void onServiceConnected(ComponentName name, IBinder service) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 432 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceConnected"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 433 | IMediaContainerService imcs = |
| 434 | IMediaContainerService.Stub.asInterface(service); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 435 | mHandler.sendMessage(mHandler.obtainMessage(MCS_BOUND, imcs)); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | public void onServiceDisconnected(ComponentName name) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 439 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceDisconnected"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 440 | } |
| 441 | }; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 442 | |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 443 | // Recordkeeping of restore-after-install operations that are currently in flight |
| 444 | // between the Package Manager and the Backup Manager |
| 445 | class PostInstallData { |
| 446 | public InstallArgs args; |
| 447 | public PackageInstalledInfo res; |
| 448 | |
| 449 | PostInstallData(InstallArgs _a, PackageInstalledInfo _r) { |
| 450 | args = _a; |
| 451 | res = _r; |
| 452 | } |
| 453 | }; |
| 454 | final SparseArray<PostInstallData> mRunningInstalls = new SparseArray<PostInstallData>(); |
| 455 | int mNextInstallToken = 1; // nonzero; will be wrapped back to 1 when ++ overflows |
| 456 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 457 | private final String mRequiredVerifierPackage; |
| 458 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 459 | class PackageHandler extends Handler { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 460 | private boolean mBound = false; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 461 | final ArrayList<HandlerParams> mPendingInstalls = |
| 462 | new ArrayList<HandlerParams>(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 463 | |
| 464 | private boolean connectToService() { |
| 465 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Trying to bind to" + |
| 466 | " DefaultContainerService"); |
| 467 | Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 468 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 469 | if (mContext.bindService(service, mDefContainerConn, |
| 470 | Context.BIND_AUTO_CREATE)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 471 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 472 | mBound = true; |
| 473 | return true; |
| 474 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 475 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | |
| 479 | private void disconnectService() { |
| 480 | mContainerService = null; |
| 481 | mBound = false; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 482 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 483 | mContext.unbindService(mDefContainerConn); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 484 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 485 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 486 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 487 | PackageHandler(Looper looper) { |
| 488 | super(looper); |
| 489 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 490 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 491 | public void handleMessage(Message msg) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 492 | try { |
| 493 | doHandleMessage(msg); |
| 494 | } finally { |
| 495 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | void doHandleMessage(Message msg) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 500 | switch (msg.what) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 501 | case INIT_COPY: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 502 | if (DEBUG_INSTALL) Slog.i(TAG, "init_copy"); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 503 | HandlerParams params = (HandlerParams) msg.obj; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 504 | int idx = mPendingInstalls.size(); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 505 | if (DEBUG_INSTALL) Slog.i(TAG, "idx=" + idx); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 506 | // If a bind was already initiated we dont really |
| 507 | // need to do anything. The pending install |
| 508 | // will be processed later on. |
| 509 | if (!mBound) { |
| 510 | // If this is the only one pending we might |
| 511 | // have to bind to the service again. |
| 512 | if (!connectToService()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 513 | Slog.e(TAG, "Failed to bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 514 | params.serviceError(); |
| 515 | return; |
| 516 | } else { |
| 517 | // Once we bind to the service, the first |
| 518 | // pending request will be processed. |
| 519 | mPendingInstalls.add(idx, params); |
| 520 | } |
| 521 | } else { |
| 522 | mPendingInstalls.add(idx, params); |
| 523 | // Already bound to the service. Just make |
| 524 | // sure we trigger off processing the first request. |
| 525 | if (idx == 0) { |
| 526 | mHandler.sendEmptyMessage(MCS_BOUND); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | break; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 530 | } |
| 531 | case MCS_BOUND: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 532 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_bound"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 533 | if (msg.obj != null) { |
| 534 | mContainerService = (IMediaContainerService) msg.obj; |
| 535 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 536 | if (mContainerService == null) { |
| 537 | // Something seriously wrong. Bail out |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 538 | Slog.e(TAG, "Cannot bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 539 | for (HandlerParams params : mPendingInstalls) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 540 | // Indicate service bind error |
| 541 | params.serviceError(); |
| 542 | } |
| 543 | mPendingInstalls.clear(); |
| 544 | } else if (mPendingInstalls.size() > 0) { |
| 545 | HandlerParams params = mPendingInstalls.get(0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 546 | if (params != null) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 547 | if (params.startCopy()) { |
| 548 | // We are done... look for more work or to |
| 549 | // go idle. |
| 550 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 551 | "Checking for more work or unbind..."); |
| 552 | // Delete pending install |
| 553 | if (mPendingInstalls.size() > 0) { |
| 554 | mPendingInstalls.remove(0); |
| 555 | } |
| 556 | if (mPendingInstalls.size() == 0) { |
| 557 | if (mBound) { |
| 558 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 559 | "Posting delayed MCS_UNBIND"); |
| 560 | removeMessages(MCS_UNBIND); |
| 561 | Message ubmsg = obtainMessage(MCS_UNBIND); |
| 562 | // Unbind after a little delay, to avoid |
| 563 | // continual thrashing. |
| 564 | sendMessageDelayed(ubmsg, 10000); |
| 565 | } |
| 566 | } else { |
| 567 | // There are more pending requests in queue. |
| 568 | // Just post MCS_BOUND message to trigger processing |
| 569 | // of next pending install. |
| 570 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 571 | "Posting MCS_BOUND for next woek"); |
| 572 | mHandler.sendEmptyMessage(MCS_BOUND); |
| 573 | } |
| 574 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 575 | } |
| 576 | } else { |
| 577 | // Should never happen ideally. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 578 | Slog.w(TAG, "Empty queue"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 579 | } |
| 580 | break; |
| 581 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 582 | case MCS_RECONNECT: { |
| 583 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_reconnect"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 584 | if (mPendingInstalls.size() > 0) { |
| 585 | if (mBound) { |
| 586 | disconnectService(); |
| 587 | } |
| 588 | if (!connectToService()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 589 | Slog.e(TAG, "Failed to bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 590 | for (HandlerParams params : mPendingInstalls) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 591 | // Indicate service bind error |
| 592 | params.serviceError(); |
| 593 | } |
| 594 | mPendingInstalls.clear(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | break; |
| 598 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 599 | case MCS_UNBIND: { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 600 | // If there is no actual work left, then time to unbind. |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 601 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_unbind"); |
| 602 | |
| 603 | if (mPendingInstalls.size() == 0 && mPendingVerification.size() == 0) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 604 | if (mBound) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 605 | if (DEBUG_INSTALL) Slog.i(TAG, "calling disconnectService()"); |
| 606 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 607 | disconnectService(); |
| 608 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 609 | } else if (mPendingInstalls.size() > 0) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 610 | // There are more pending requests in queue. |
| 611 | // Just post MCS_BOUND message to trigger processing |
| 612 | // of next pending install. |
| 613 | mHandler.sendEmptyMessage(MCS_BOUND); |
| 614 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 615 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 616 | break; |
| 617 | } |
| 618 | case MCS_GIVE_UP: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 619 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_giveup too many retries"); |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 620 | mPendingInstalls.remove(0); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 621 | break; |
| 622 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 623 | case SEND_PENDING_BROADCAST: { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 624 | String packages[]; |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 625 | ArrayList<String> components[]; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 626 | int size = 0; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 627 | int uids[]; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 628 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 629 | synchronized (mPackages) { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 630 | if (mPendingBroadcasts == null) { |
| 631 | return; |
| 632 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 633 | size = mPendingBroadcasts.size(); |
| 634 | if (size <= 0) { |
| 635 | // Nothing to be done. Just return |
| 636 | return; |
| 637 | } |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 638 | packages = new String[size]; |
| 639 | components = new ArrayList[size]; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 640 | uids = new int[size]; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 641 | Iterator<Map.Entry<String, ArrayList<String>>> |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 642 | it = mPendingBroadcasts.entrySet().iterator(); |
| 643 | int i = 0; |
| 644 | while (it.hasNext() && i < size) { |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 645 | Map.Entry<String, ArrayList<String>> ent = it.next(); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 646 | packages[i] = ent.getKey(); |
| 647 | components[i] = ent.getValue(); |
| 648 | PackageSetting ps = mSettings.mPackages.get(ent.getKey()); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 649 | uids[i] = (ps != null) ? ps.appId : -1; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 650 | i++; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 651 | } |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 652 | size = i; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 653 | mPendingBroadcasts.clear(); |
| 654 | } |
| 655 | // Send broadcasts |
| 656 | for (int i = 0; i < size; i++) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 657 | sendPackageChangedBroadcast(packages[i], true, components[i], uids[i]); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 658 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 659 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 660 | break; |
| 661 | } |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 662 | case START_CLEANING_PACKAGE: { |
| 663 | String packageName = (String)msg.obj; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 664 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 665 | synchronized (mPackages) { |
| 666 | if (!mSettings.mPackagesToBeCleaned.contains(packageName)) { |
| 667 | mSettings.mPackagesToBeCleaned.add(packageName); |
| 668 | } |
| 669 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 670 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 671 | startCleaningPackages(); |
| 672 | } break; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 673 | case POST_INSTALL: { |
| 674 | if (DEBUG_INSTALL) Log.v(TAG, "Handling post-install for " + msg.arg1); |
| 675 | PostInstallData data = mRunningInstalls.get(msg.arg1); |
| 676 | mRunningInstalls.delete(msg.arg1); |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 677 | boolean deleteOld = false; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 678 | |
| 679 | if (data != null) { |
| 680 | InstallArgs args = data.args; |
| 681 | PackageInstalledInfo res = data.res; |
| 682 | |
| 683 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 684 | res.removedInfo.sendBroadcast(false, true); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 685 | Bundle extras = new Bundle(1); |
| 686 | extras.putInt(Intent.EXTRA_UID, res.uid); |
| 687 | final boolean update = res.removedInfo.removedPackage != null; |
| 688 | if (update) { |
| 689 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 690 | } |
| 691 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, |
| 692 | res.pkg.applicationInfo.packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 693 | extras, null, null, UserId.USER_ALL); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 694 | if (update) { |
| 695 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, |
| 696 | res.pkg.applicationInfo.packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 697 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 698 | sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, |
| 699 | null, null, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 700 | res.pkg.applicationInfo.packageName, null, |
| 701 | UserId.USER_ALL); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 702 | } |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 703 | if (res.removedInfo.args != null) { |
| 704 | // Remove the replaced package's older resources safely now |
| 705 | deleteOld = true; |
| 706 | } |
Matt Finifter | f8a98ed | 2012-05-14 15:43:34 -0700 | [diff] [blame] | 707 | |
| 708 | // Log current value of "unknown sources" setting |
| 709 | EventLog.writeEvent(EventLogTags.UNKNOWN_SOURCES_ENABLED, |
| 710 | getUnknownSourcesSettings()); |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 711 | } |
| 712 | // Force a gc to clear up things |
| 713 | Runtime.getRuntime().gc(); |
| 714 | // We delete after a gc for applications on sdcard. |
| 715 | if (deleteOld) { |
| 716 | synchronized (mInstallLock) { |
| 717 | res.removedInfo.args.doPostDeleteLI(true); |
| 718 | } |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 719 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 720 | if (args.observer != null) { |
| 721 | try { |
| 722 | args.observer.packageInstalled(res.name, res.returnCode); |
| 723 | } catch (RemoteException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 724 | Slog.i(TAG, "Observer no longer exists."); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 728 | Slog.e(TAG, "Bogus post-install token " + msg.arg1); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 729 | } |
| 730 | } break; |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 731 | case UPDATED_MEDIA_STATUS: { |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 732 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Got message UPDATED_MEDIA_STATUS"); |
| 733 | boolean reportStatus = msg.arg1 == 1; |
| 734 | boolean doGc = msg.arg2 == 1; |
| 735 | if (DEBUG_SD_INSTALL) Log.i(TAG, "reportStatus=" + reportStatus + ", doGc = " + doGc); |
| 736 | if (doGc) { |
| 737 | // Force a gc to clear up stale containers. |
| 738 | Runtime.getRuntime().gc(); |
| 739 | } |
| 740 | if (msg.obj != null) { |
Dianne Hackborn | ba24e4d | 2011-09-01 11:17:06 -0700 | [diff] [blame] | 741 | @SuppressWarnings("unchecked") |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 742 | Set<AsecInstallArgs> args = (Set<AsecInstallArgs>) msg.obj; |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 743 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Unloading all containers"); |
| 744 | // Unload containers |
| 745 | unloadAllContainers(args); |
| 746 | } |
| 747 | if (reportStatus) { |
| 748 | try { |
| 749 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Invoking MountService call back"); |
| 750 | PackageHelper.getMountService().finishMediaUpdate(); |
| 751 | } catch (RemoteException e) { |
| 752 | Log.e(TAG, "MountService not running?"); |
| 753 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 754 | } |
| 755 | } break; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 756 | case WRITE_SETTINGS: { |
| 757 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 758 | synchronized (mPackages) { |
| 759 | removeMessages(WRITE_SETTINGS); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 760 | removeMessages(WRITE_PACKAGE_RESTRICTIONS); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 761 | mSettings.writeLPr(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 762 | mDirtyUsers.clear(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 763 | } |
| 764 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 765 | } break; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 766 | case WRITE_PACKAGE_RESTRICTIONS: { |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 767 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 768 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 769 | removeMessages(WRITE_PACKAGE_RESTRICTIONS); |
| 770 | for (int userId : mDirtyUsers) { |
| 771 | mSettings.writePackageRestrictionsLPr(userId); |
| 772 | } |
| 773 | mDirtyUsers.clear(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 774 | } |
| 775 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 776 | } break; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 777 | case CHECK_PENDING_VERIFICATION: { |
| 778 | final int verificationId = msg.arg1; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 779 | final PackageVerificationState state = mPendingVerification.get(verificationId); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 780 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 781 | if (state != null) { |
| 782 | final InstallArgs args = state.getInstallArgs(); |
Kenny Root | a503a0f | 2011-10-03 14:45:28 -0700 | [diff] [blame] | 783 | Slog.i(TAG, "Verification timed out for " + args.packageURI.toString()); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 784 | mPendingVerification.remove(verificationId); |
| 785 | |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 786 | int ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
| 787 | |
| 788 | if (getDefaultVerificationResponse() == PackageManager.VERIFICATION_ALLOW) { |
| 789 | Slog.i(TAG, "Continuing with installation of " + args.packageURI.toString()); |
| 790 | state.setVerifierResponse(Binder.getCallingUid(), PackageManager.VERIFICATION_ALLOW_WITHOUT_SUFFICIENT); |
| 791 | try { |
| 792 | ret = args.copyApk(mContainerService, true); |
| 793 | } catch (RemoteException e) { |
| 794 | Slog.e(TAG, "Could not contact the ContainerService"); |
| 795 | } |
| 796 | } |
| 797 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 798 | processPendingInstall(args, ret); |
| 799 | |
| 800 | mHandler.sendEmptyMessage(MCS_UNBIND); |
| 801 | } |
| 802 | |
| 803 | break; |
| 804 | } |
| 805 | case PACKAGE_VERIFIED: { |
| 806 | final int verificationId = msg.arg1; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 807 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 808 | final PackageVerificationState state = mPendingVerification.get(verificationId); |
| 809 | if (state == null) { |
Kenny Root | a503a0f | 2011-10-03 14:45:28 -0700 | [diff] [blame] | 810 | Slog.w(TAG, "Invalid verification token " + verificationId + " received"); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 811 | break; |
| 812 | } |
| 813 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 814 | final PackageVerificationResponse response = (PackageVerificationResponse) msg.obj; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 815 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 816 | state.setVerifierResponse(response.callerUid, response.code); |
| 817 | |
| 818 | if (state.isVerificationComplete()) { |
| 819 | mPendingVerification.remove(verificationId); |
| 820 | |
| 821 | final InstallArgs args = state.getInstallArgs(); |
| 822 | |
| 823 | int ret; |
| 824 | if (state.isInstallAllowed()) { |
| 825 | ret = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 826 | try { |
| 827 | ret = args.copyApk(mContainerService, true); |
| 828 | } catch (RemoteException e) { |
| 829 | Slog.e(TAG, "Could not contact the ContainerService"); |
| 830 | } |
| 831 | } else { |
| 832 | ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 833 | } |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 834 | |
| 835 | processPendingInstall(args, ret); |
| 836 | |
| 837 | mHandler.sendEmptyMessage(MCS_UNBIND); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 840 | break; |
| 841 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 845 | |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 846 | void scheduleWriteSettingsLocked() { |
| 847 | if (!mHandler.hasMessages(WRITE_SETTINGS)) { |
| 848 | mHandler.sendEmptyMessageDelayed(WRITE_SETTINGS, WRITE_SETTINGS_DELAY); |
| 849 | } |
| 850 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 851 | |
| 852 | void scheduleWritePackageRestrictionsLocked(int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 853 | if (!sUserManager.exists(userId)) return; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 854 | mDirtyUsers.add(userId); |
| 855 | if (!mHandler.hasMessages(WRITE_PACKAGE_RESTRICTIONS)) { |
| 856 | mHandler.sendEmptyMessageDelayed(WRITE_PACKAGE_RESTRICTIONS, WRITE_SETTINGS_DELAY); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 860 | public static final IPackageManager main(Context context, boolean factoryTest, |
| 861 | boolean onlyCore) { |
| 862 | PackageManagerService m = new PackageManagerService(context, factoryTest, onlyCore); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | ServiceManager.addService("package", m); |
| 864 | return m; |
| 865 | } |
| 866 | |
| 867 | static String[] splitString(String str, char sep) { |
| 868 | int count = 1; |
| 869 | int i = 0; |
| 870 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 871 | count++; |
| 872 | i++; |
| 873 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 874 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 875 | String[] res = new String[count]; |
| 876 | i=0; |
| 877 | count = 0; |
| 878 | int lastI=0; |
| 879 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 880 | res[count] = str.substring(lastI, i); |
| 881 | count++; |
| 882 | i++; |
| 883 | lastI = i; |
| 884 | } |
| 885 | res[count] = str.substring(lastI, str.length()); |
| 886 | return res; |
| 887 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 888 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 889 | public PackageManagerService(Context context, boolean factoryTest, boolean onlyCore) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 890 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_START, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | SystemClock.uptimeMillis()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 892 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | if (mSdkVersion <= 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 894 | Slog.w(TAG, "**** ro.build.version.sdk not set!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | mContext = context; |
| 898 | mFactoryTest = factoryTest; |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 899 | mOnlyCore = onlyCore; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 900 | mNoDexOpt = "eng".equals(SystemProperties.get("ro.build.type")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | mMetrics = new DisplayMetrics(); |
| 902 | mSettings = new Settings(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 903 | mSettings.addSharedUserLPw("android.uid.system", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | Process.SYSTEM_UID, ApplicationInfo.FLAG_SYSTEM); |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 905 | mSettings.addSharedUserLPw("android.uid.phone", RADIO_UID, ApplicationInfo.FLAG_SYSTEM); |
| 906 | mSettings.addSharedUserLPw("android.uid.log", LOG_UID, ApplicationInfo.FLAG_SYSTEM); |
| 907 | mSettings.addSharedUserLPw("android.uid.nfc", NFC_UID, ApplicationInfo.FLAG_SYSTEM); |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 908 | mSettings.addSharedUserLPw("android.uid.bluetooth", BLUETOOTH_UID, ApplicationInfo.FLAG_SYSTEM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | |
| 910 | String separateProcesses = SystemProperties.get("debug.separate_processes"); |
| 911 | if (separateProcesses != null && separateProcesses.length() > 0) { |
| 912 | if ("*".equals(separateProcesses)) { |
| 913 | mDefParseFlags = PackageParser.PARSE_IGNORE_PROCESSES; |
| 914 | mSeparateProcesses = null; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 915 | Slog.w(TAG, "Running with debug.separate_processes: * (ALL)"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | } else { |
| 917 | mDefParseFlags = 0; |
| 918 | mSeparateProcesses = separateProcesses.split(","); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 919 | Slog.w(TAG, "Running with debug.separate_processes: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | + separateProcesses); |
| 921 | } |
| 922 | } else { |
| 923 | mDefParseFlags = 0; |
| 924 | mSeparateProcesses = null; |
| 925 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 926 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 927 | mInstaller = new Installer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | |
| 929 | WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); |
| 930 | Display d = wm.getDefaultDisplay(); |
| 931 | d.getMetrics(mMetrics); |
| 932 | |
| 933 | synchronized (mInstallLock) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 934 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | synchronized (mPackages) { |
| 936 | mHandlerThread.start(); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 937 | mHandler = new PackageHandler(mHandlerThread.getLooper()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 938 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | File dataDir = Environment.getDataDirectory(); |
| 940 | mAppDataDir = new File(dataDir, "data"); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 941 | mAsecInternalPath = new File(dataDir, "app-asec").getPath(); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 942 | mUserAppDataDir = new File(dataDir, "user"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | mDrmAppPrivateInstallDir = new File(dataDir, "app-private"); |
| 944 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 945 | sUserManager = new UserManager(mInstaller, mUserAppDataDir); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 946 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | readPermissions(); |
| 948 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 949 | mRestoredSettings = mSettings.readLPw(getUsers()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | long startTime = SystemClock.uptimeMillis(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 951 | |
| 952 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SYSTEM_SCAN_START, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | startTime); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 954 | |
Suchi Amalapurapu | daec172 | 2010-01-14 21:25:16 -0800 | [diff] [blame] | 955 | // Set flag to monitor and not change apk file paths when |
| 956 | // scanning install directories. |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 957 | int scanMode = SCAN_MONITOR | SCAN_NO_PATHS | SCAN_DEFER_DEX | SCAN_BOOTING; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 958 | if (mNoDexOpt) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 959 | Slog.w(TAG, "Running ENG build: no pre-dexopt!"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 960 | scanMode |= SCAN_NO_DEX; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 961 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 962 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | final HashSet<String> libFiles = new HashSet<String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 964 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | mFrameworkDir = new File(Environment.getRootDirectory(), "framework"); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 966 | mDalvikCacheDir = new File(dataDir, "dalvik-cache"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 967 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 968 | boolean didDexOpt = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 969 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 970 | /** |
| 971 | * Out of paranoia, ensure that everything in the boot class |
| 972 | * path has been dexed. |
| 973 | */ |
| 974 | String bootClassPath = System.getProperty("java.boot.class.path"); |
| 975 | if (bootClassPath != null) { |
| 976 | String[] paths = splitString(bootClassPath, ':'); |
| 977 | for (int i=0; i<paths.length; i++) { |
| 978 | try { |
| 979 | if (dalvik.system.DexFile.isDexOptNeeded(paths[i])) { |
| 980 | libFiles.add(paths[i]); |
| 981 | mInstaller.dexopt(paths[i], Process.SYSTEM_UID, true); |
| 982 | didDexOpt = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 984 | } catch (FileNotFoundException e) { |
| 985 | Slog.w(TAG, "Boot class path not found: " + paths[i]); |
| 986 | } catch (IOException e) { |
Kenny Root | fec5e75 | 2011-09-23 10:09:58 -0700 | [diff] [blame] | 987 | Slog.w(TAG, "Cannot dexopt " + paths[i] + "; is it an APK or JAR? " |
| 988 | + e.getMessage()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | } |
| 990 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 991 | } else { |
| 992 | Slog.w(TAG, "No BOOTCLASSPATH found!"); |
| 993 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 994 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 995 | /** |
| 996 | * Also ensure all external libraries have had dexopt run on them. |
| 997 | */ |
| 998 | if (mSharedLibraries.size() > 0) { |
| 999 | Iterator<String> libs = mSharedLibraries.values().iterator(); |
| 1000 | while (libs.hasNext()) { |
| 1001 | String lib = libs.next(); |
| 1002 | try { |
| 1003 | if (dalvik.system.DexFile.isDexOptNeeded(lib)) { |
| 1004 | libFiles.add(lib); |
| 1005 | mInstaller.dexopt(lib, Process.SYSTEM_UID, true); |
| 1006 | didDexOpt = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1008 | } catch (FileNotFoundException e) { |
| 1009 | Slog.w(TAG, "Library not found: " + lib); |
| 1010 | } catch (IOException e) { |
Kenny Root | fec5e75 | 2011-09-23 10:09:58 -0700 | [diff] [blame] | 1011 | Slog.w(TAG, "Cannot dexopt " + lib + "; is it an APK or JAR? " |
| 1012 | + e.getMessage()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | } |
| 1014 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1015 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1016 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1017 | // Gross hack for now: we know this file doesn't contain any |
| 1018 | // code, so don't dexopt it to avoid the resulting log spew. |
| 1019 | libFiles.add(mFrameworkDir.getPath() + "/framework-res.apk"); |
| 1020 | |
| 1021 | /** |
| 1022 | * And there are a number of commands implemented in Java, which |
| 1023 | * we currently need to do the dexopt on so that they can be |
| 1024 | * run from a non-root shell. |
| 1025 | */ |
| 1026 | String[] frameworkFiles = mFrameworkDir.list(); |
| 1027 | if (frameworkFiles != null) { |
| 1028 | for (int i=0; i<frameworkFiles.length; i++) { |
| 1029 | File libPath = new File(mFrameworkDir, frameworkFiles[i]); |
| 1030 | String path = libPath.getPath(); |
| 1031 | // Skip the file if we alrady did it. |
| 1032 | if (libFiles.contains(path)) { |
| 1033 | continue; |
| 1034 | } |
| 1035 | // Skip the file if it is not a type we want to dexopt. |
| 1036 | if (!path.endsWith(".apk") && !path.endsWith(".jar")) { |
| 1037 | continue; |
| 1038 | } |
| 1039 | try { |
| 1040 | if (dalvik.system.DexFile.isDexOptNeeded(path)) { |
| 1041 | mInstaller.dexopt(path, Process.SYSTEM_UID, true); |
| 1042 | didDexOpt = true; |
| 1043 | } |
| 1044 | } catch (FileNotFoundException e) { |
| 1045 | Slog.w(TAG, "Jar not found: " + path); |
| 1046 | } catch (IOException e) { |
| 1047 | Slog.w(TAG, "Exception reading jar: " + path, e); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | if (didDexOpt) { |
| 1053 | // If we had to do a dexopt of one of the previous |
| 1054 | // things, then something on the system has changed. |
| 1055 | // Consider this significant, and wipe away all other |
| 1056 | // existing dexopt files to ensure we don't leave any |
| 1057 | // dangling around. |
| 1058 | String[] files = mDalvikCacheDir.list(); |
| 1059 | if (files != null) { |
| 1060 | for (int i=0; i<files.length; i++) { |
| 1061 | String fn = files[i]; |
| 1062 | if (fn.startsWith("data@app@") |
| 1063 | || fn.startsWith("data@app-private@")) { |
| 1064 | Slog.i(TAG, "Pruning dalvik file: " + fn); |
| 1065 | (new File(mDalvikCacheDir, fn)).delete(); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1070 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1071 | // Find base frameworks (resource packages without code). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | mFrameworkInstallObserver = new AppDirObserver( |
| 1073 | mFrameworkDir.getPath(), OBSERVER_EVENTS, true); |
| 1074 | mFrameworkInstallObserver.startWatching(); |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 1075 | scanDirLI(mFrameworkDir, PackageParser.PARSE_IS_SYSTEM |
| 1076 | | PackageParser.PARSE_IS_SYSTEM_DIR, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1077 | scanMode | SCAN_NO_DEX, 0); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 1078 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1079 | // Collect all system packages. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | mSystemAppDir = new File(Environment.getRootDirectory(), "app"); |
| 1081 | mSystemInstallObserver = new AppDirObserver( |
| 1082 | mSystemAppDir.getPath(), OBSERVER_EVENTS, true); |
| 1083 | mSystemInstallObserver.startWatching(); |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 1084 | scanDirLI(mSystemAppDir, PackageParser.PARSE_IS_SYSTEM |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1085 | | PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1086 | |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 1087 | // Collect all vendor packages. |
| 1088 | mVendorAppDir = new File("/vendor/app"); |
| 1089 | mVendorInstallObserver = new AppDirObserver( |
| 1090 | mVendorAppDir.getPath(), OBSERVER_EVENTS, true); |
| 1091 | mVendorInstallObserver.startWatching(); |
| 1092 | scanDirLI(mVendorAppDir, PackageParser.PARSE_IS_SYSTEM |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1093 | | PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0); |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 1094 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1095 | if (DEBUG_UPGRADE) Log.v(TAG, "Running installd update commands"); |
| 1096 | mInstaller.moveFiles(); |
| 1097 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1098 | // Prune any system packages that no longer exist. |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1099 | final List<String> possiblyDeletedUpdatedSystemApps = new ArrayList<String>(); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1100 | if (!mOnlyCore) { |
| 1101 | Iterator<PackageSetting> psit = mSettings.mPackages.values().iterator(); |
| 1102 | while (psit.hasNext()) { |
| 1103 | PackageSetting ps = psit.next(); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1104 | |
| 1105 | /* |
| 1106 | * If this is not a system app, it can't be a |
| 1107 | * disable system app. |
| 1108 | */ |
| 1109 | if ((ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 1110 | continue; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * If the package is scanned, it's not erased. |
| 1115 | */ |
Kenny Root | eca64b3 | 2012-06-25 16:37:32 -0700 | [diff] [blame] | 1116 | final PackageParser.Package scannedPkg = mPackages.get(ps.name); |
| 1117 | if (scannedPkg != null) { |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1118 | /* |
| 1119 | * If the system app is both scanned and in the |
| 1120 | * disabled packages list, then it must have been |
| 1121 | * added via OTA. Remove it from the currently |
| 1122 | * scanned package so the previously user-installed |
| 1123 | * application can be scanned. |
| 1124 | */ |
| 1125 | if (mSettings.isDisabledSystemPackageLPr(ps.name)) { |
Kenny Root | eca64b3 | 2012-06-25 16:37:32 -0700 | [diff] [blame] | 1126 | Slog.i(TAG, "Expecting better updatd system app for " + ps.name |
| 1127 | + "; removing system app"); |
| 1128 | removePackageLI(scannedPkg, true); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1131 | continue; |
| 1132 | } |
| 1133 | |
| 1134 | if (!mSettings.isDisabledSystemPackageLPr(ps.name)) { |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1135 | psit.remove(); |
| 1136 | String msg = "System package " + ps.name |
| 1137 | + " no longer exists; wiping its data"; |
| 1138 | reportSettingsProblem(Log.WARN, msg); |
| 1139 | mInstaller.remove(ps.name, 0); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1140 | sUserManager.removePackageForAllUsers(ps.name); |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1141 | } else { |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1142 | final PackageSetting disabledPs = mSettings.getDisabledSystemPkgLPr(ps.name); |
| 1143 | if (disabledPs.codePath == null || !disabledPs.codePath.exists()) { |
| 1144 | possiblyDeletedUpdatedSystemApps.add(ps.name); |
| 1145 | } |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1146 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1147 | } |
| 1148 | } |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | mAppInstallDir = new File(dataDir, "app"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | //look for any incomplete package installations |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1152 | ArrayList<PackageSetting> deletePkgsList = mSettings.getListOfIncompleteInstallPackagesLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | //clean up list |
| 1154 | for(int i = 0; i < deletePkgsList.size(); i++) { |
| 1155 | //clean up here |
| 1156 | cleanupInstallFailedPackage(deletePkgsList.get(i)); |
| 1157 | } |
| 1158 | //delete tmp files |
| 1159 | deleteTempPackageFiles(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1160 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1161 | if (!mOnlyCore) { |
| 1162 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_DATA_SCAN_START, |
| 1163 | SystemClock.uptimeMillis()); |
| 1164 | mAppInstallObserver = new AppDirObserver( |
| 1165 | mAppInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 1166 | mAppInstallObserver.startWatching(); |
| 1167 | scanDirLI(mAppInstallDir, 0, scanMode, 0); |
| 1168 | |
| 1169 | mDrmAppInstallObserver = new AppDirObserver( |
| 1170 | mDrmAppPrivateInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 1171 | mDrmAppInstallObserver.startWatching(); |
| 1172 | scanDirLI(mDrmAppPrivateInstallDir, PackageParser.PARSE_FORWARD_LOCK, |
| 1173 | scanMode, 0); |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1174 | |
| 1175 | /** |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1176 | * Remove disable package settings for any updated system |
| 1177 | * apps that were removed via an OTA. If they're not a |
| 1178 | * previously-updated app, remove them completely. |
| 1179 | * Otherwise, just revoke their system-level permissions. |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1180 | */ |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1181 | for (String deletedAppName : possiblyDeletedUpdatedSystemApps) { |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1182 | PackageParser.Package deletedPkg = mPackages.get(deletedAppName); |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1183 | mSettings.removeDisabledSystemPackageLPw(deletedAppName); |
| 1184 | |
| 1185 | String msg; |
| 1186 | if (deletedPkg == null) { |
| 1187 | msg = "Updated system package " + deletedAppName |
| 1188 | + " no longer exists; wiping its data"; |
| 1189 | |
| 1190 | mInstaller.remove(deletedAppName, 0); |
| 1191 | sUserManager.removePackageForAllUsers(deletedAppName); |
| 1192 | } else { |
| 1193 | msg = "Updated system app + " + deletedAppName |
| 1194 | + " no longer present; removing system privileges for " |
| 1195 | + deletedAppName; |
| 1196 | |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1197 | deletedPkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_SYSTEM; |
| 1198 | |
| 1199 | PackageSetting deletedPs = mSettings.mPackages.get(deletedAppName); |
| 1200 | deletedPs.pkgFlags &= ~ApplicationInfo.FLAG_SYSTEM; |
| 1201 | } |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1202 | reportSettingsProblem(Log.WARN, msg); |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1203 | } |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1204 | } else { |
| 1205 | mAppInstallObserver = null; |
| 1206 | mDrmAppInstallObserver = null; |
| 1207 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1209 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SCAN_END, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | SystemClock.uptimeMillis()); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1211 | Slog.i(TAG, "Time to scan packages: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | + ((SystemClock.uptimeMillis()-startTime)/1000f) |
| 1213 | + " seconds"); |
| 1214 | |
Dianne Hackborn | f22221f | 2010-04-05 18:35:42 -0700 | [diff] [blame] | 1215 | // If the platform SDK has changed since the last time we booted, |
| 1216 | // we need to re-grant app permission to catch any new ones that |
| 1217 | // appear. This is really a hack, and means that apps can in some |
| 1218 | // cases get permissions that the user didn't initially explicitly |
| 1219 | // allow... it would be nice to have some better way to handle |
| 1220 | // this situation. |
| 1221 | final boolean regrantPermissions = mSettings.mInternalSdkPlatform |
| 1222 | != mSdkVersion; |
| 1223 | if (regrantPermissions) Slog.i(TAG, "Platform changed from " |
| 1224 | + mSettings.mInternalSdkPlatform + " to " + mSdkVersion |
| 1225 | + "; regranting permissions for internal storage"); |
| 1226 | mSettings.mInternalSdkPlatform = mSdkVersion; |
| 1227 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 1228 | updatePermissionsLPw(null, null, UPDATE_PERMISSIONS_ALL |
| 1229 | | (regrantPermissions |
| 1230 | ? (UPDATE_PERMISSIONS_REPLACE_PKG|UPDATE_PERMISSIONS_REPLACE_ALL) |
| 1231 | : 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1232 | |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 1233 | // Verify that all of the preferred activity components actually |
| 1234 | // exist. It is possible for applications to be updated and at |
| 1235 | // that point remove a previously declared activity component that |
| 1236 | // had been set as a preferred activity. We try to clean this up |
| 1237 | // the next time we encounter that preferred activity, but it is |
| 1238 | // possible for the user flow to never be able to return to that |
| 1239 | // situation so here we do a sanity check to make sure we haven't |
| 1240 | // left any junk around. |
| 1241 | ArrayList<PreferredActivity> removed = new ArrayList<PreferredActivity>(); |
| 1242 | for (PreferredActivity pa : mSettings.mPreferredActivities.filterSet()) { |
| 1243 | if (mActivities.mActivities.get(pa.mPref.mComponent) == null) { |
| 1244 | removed.add(pa); |
| 1245 | } |
| 1246 | } |
| 1247 | for (int i=0; i<removed.size(); i++) { |
| 1248 | PreferredActivity pa = removed.get(i); |
| 1249 | Slog.w(TAG, "Removing dangling preferred activity: " |
| 1250 | + pa.mPref.mComponent); |
| 1251 | mSettings.mPreferredActivities.removeFilter(pa); |
| 1252 | } |
| 1253 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1254 | // can downgrade to reader |
| 1255 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1257 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | SystemClock.uptimeMillis()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | // Now after opening every single application zip, make sure they |
| 1261 | // are all flushed. Not really needed, but keeps things nice and |
| 1262 | // tidy. |
| 1263 | Runtime.getRuntime().gc(); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1264 | |
| 1265 | mRequiredVerifierPackage = getRequiredVerifierLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1266 | } // synchronized (mPackages) |
| 1267 | } // synchronized (mInstallLock) |
| 1268 | } |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 1269 | |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 1270 | public boolean isFirstBoot() { |
| 1271 | return !mRestoredSettings; |
| 1272 | } |
| 1273 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1274 | private String getRequiredVerifierLPr() { |
| 1275 | final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION); |
| 1276 | final List<ResolveInfo> receivers = queryIntentReceivers(verification, PACKAGE_MIME_TYPE, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1277 | PackageManager.GET_DISABLED_COMPONENTS, 0 /* TODO: Which userId? */); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1278 | |
| 1279 | String requiredVerifier = null; |
| 1280 | |
| 1281 | final int N = receivers.size(); |
| 1282 | for (int i = 0; i < N; i++) { |
| 1283 | final ResolveInfo info = receivers.get(i); |
| 1284 | |
| 1285 | if (info.activityInfo == null) { |
| 1286 | continue; |
| 1287 | } |
| 1288 | |
| 1289 | final String packageName = info.activityInfo.packageName; |
| 1290 | |
| 1291 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1292 | if (ps == null) { |
| 1293 | continue; |
| 1294 | } |
| 1295 | |
| 1296 | if (!ps.grantedPermissions |
| 1297 | .contains(android.Manifest.permission.PACKAGE_VERIFICATION_AGENT)) { |
| 1298 | continue; |
| 1299 | } |
| 1300 | |
| 1301 | if (requiredVerifier != null) { |
| 1302 | throw new RuntimeException("There can be only one required verifier"); |
| 1303 | } |
| 1304 | |
| 1305 | requiredVerifier = packageName; |
| 1306 | } |
| 1307 | |
| 1308 | return requiredVerifier; |
| 1309 | } |
| 1310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | @Override |
| 1312 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1313 | throws RemoteException { |
| 1314 | try { |
| 1315 | return super.onTransact(code, data, reply, flags); |
| 1316 | } catch (RuntimeException e) { |
| 1317 | if (!(e instanceof SecurityException) && !(e instanceof IllegalArgumentException)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1318 | Slog.e(TAG, "Package Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1319 | } |
| 1320 | throw e; |
| 1321 | } |
| 1322 | } |
| 1323 | |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1324 | void cleanupInstallFailedPackage(PackageSetting ps) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1325 | Slog.i(TAG, "Cleaning up incompletely installed app: " + ps.name); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1326 | int retCode = mInstaller.remove(ps.name, 0); |
| 1327 | if (retCode < 0) { |
| 1328 | Slog.w(TAG, "Couldn't remove app data directory for package: " |
| 1329 | + ps.name + ", retcode=" + retCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1330 | } else { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1331 | sUserManager.removePackageForAllUsers(ps.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | } |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1333 | if (ps.codePath != null) { |
| 1334 | if (!ps.codePath.delete()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1335 | Slog.w(TAG, "Unable to remove old code file: " + ps.codePath); |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | if (ps.resourcePath != null) { |
| 1339 | if (!ps.resourcePath.delete() && !ps.resourcePath.equals(ps.codePath)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1340 | Slog.w(TAG, "Unable to remove old code file: " + ps.resourcePath); |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1341 | } |
| 1342 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1343 | mSettings.removePackageLPw(ps.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | void readPermissions() { |
| 1347 | // Read permissions from .../etc/permission directory. |
| 1348 | File libraryDir = new File(Environment.getRootDirectory(), "etc/permissions"); |
| 1349 | if (!libraryDir.exists() || !libraryDir.isDirectory()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1350 | Slog.w(TAG, "No directory " + libraryDir + ", skipping"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | return; |
| 1352 | } |
| 1353 | if (!libraryDir.canRead()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1354 | Slog.w(TAG, "Directory " + libraryDir + " cannot be read"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | return; |
| 1356 | } |
| 1357 | |
| 1358 | // Iterate over the files in the directory and scan .xml files |
| 1359 | for (File f : libraryDir.listFiles()) { |
| 1360 | // We'll read platform.xml last |
| 1361 | if (f.getPath().endsWith("etc/permissions/platform.xml")) { |
| 1362 | continue; |
| 1363 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1364 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | if (!f.getPath().endsWith(".xml")) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1366 | 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] | 1367 | continue; |
| 1368 | } |
| 1369 | if (!f.canRead()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1370 | 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] | 1371 | continue; |
| 1372 | } |
| 1373 | |
| 1374 | readPermissionsFromXml(f); |
| 1375 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1376 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | // Read permissions from .../etc/permissions/platform.xml last so it will take precedence |
| 1378 | final File permFile = new File(Environment.getRootDirectory(), |
| 1379 | "etc/permissions/platform.xml"); |
| 1380 | readPermissionsFromXml(permFile); |
| 1381 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1382 | |
| 1383 | private void readPermissionsFromXml(File permFile) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | FileReader permReader = null; |
| 1385 | try { |
| 1386 | permReader = new FileReader(permFile); |
| 1387 | } catch (FileNotFoundException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1388 | 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] | 1389 | return; |
| 1390 | } |
| 1391 | |
| 1392 | try { |
| 1393 | XmlPullParser parser = Xml.newPullParser(); |
| 1394 | parser.setInput(permReader); |
| 1395 | |
| 1396 | XmlUtils.beginDocument(parser, "permissions"); |
| 1397 | |
| 1398 | while (true) { |
| 1399 | XmlUtils.nextElement(parser); |
| 1400 | if (parser.getEventType() == XmlPullParser.END_DOCUMENT) { |
| 1401 | break; |
| 1402 | } |
| 1403 | |
| 1404 | String name = parser.getName(); |
| 1405 | if ("group".equals(name)) { |
| 1406 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1407 | if (gidStr != null) { |
| 1408 | int gid = Integer.parseInt(gidStr); |
| 1409 | mGlobalGids = appendInt(mGlobalGids, gid); |
| 1410 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1411 | Slog.w(TAG, "<group> without gid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | + parser.getPositionDescription()); |
| 1413 | } |
| 1414 | |
| 1415 | XmlUtils.skipCurrentTag(parser); |
| 1416 | continue; |
| 1417 | } else if ("permission".equals(name)) { |
| 1418 | String perm = parser.getAttributeValue(null, "name"); |
| 1419 | if (perm == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1420 | Slog.w(TAG, "<permission> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | + parser.getPositionDescription()); |
| 1422 | XmlUtils.skipCurrentTag(parser); |
| 1423 | continue; |
| 1424 | } |
| 1425 | perm = perm.intern(); |
| 1426 | readPermission(parser, perm); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1427 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | } else if ("assign-permission".equals(name)) { |
| 1429 | String perm = parser.getAttributeValue(null, "name"); |
| 1430 | if (perm == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1431 | Slog.w(TAG, "<assign-permission> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | + parser.getPositionDescription()); |
| 1433 | XmlUtils.skipCurrentTag(parser); |
| 1434 | continue; |
| 1435 | } |
| 1436 | String uidStr = parser.getAttributeValue(null, "uid"); |
| 1437 | if (uidStr == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1438 | Slog.w(TAG, "<assign-permission> without uid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | + parser.getPositionDescription()); |
| 1440 | XmlUtils.skipCurrentTag(parser); |
| 1441 | continue; |
| 1442 | } |
| 1443 | int uid = Process.getUidForName(uidStr); |
| 1444 | if (uid < 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1445 | Slog.w(TAG, "<assign-permission> with unknown uid \"" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1446 | + uidStr + "\" at " |
| 1447 | + parser.getPositionDescription()); |
| 1448 | XmlUtils.skipCurrentTag(parser); |
| 1449 | continue; |
| 1450 | } |
| 1451 | perm = perm.intern(); |
| 1452 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 1453 | if (perms == null) { |
| 1454 | perms = new HashSet<String>(); |
| 1455 | mSystemPermissions.put(uid, perms); |
| 1456 | } |
| 1457 | perms.add(perm); |
| 1458 | XmlUtils.skipCurrentTag(parser); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1459 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | } else if ("library".equals(name)) { |
| 1461 | String lname = parser.getAttributeValue(null, "name"); |
| 1462 | String lfile = parser.getAttributeValue(null, "file"); |
| 1463 | if (lname == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1464 | Slog.w(TAG, "<library> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1465 | + parser.getPositionDescription()); |
| 1466 | } else if (lfile == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1467 | Slog.w(TAG, "<library> without file at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | + parser.getPositionDescription()); |
| 1469 | } else { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1470 | //Log.i(TAG, "Got library " + lname + " in " + lfile); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1471 | mSharedLibraries.put(lname, lfile); |
| 1472 | } |
| 1473 | XmlUtils.skipCurrentTag(parser); |
| 1474 | continue; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1475 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1476 | } else if ("feature".equals(name)) { |
| 1477 | String fname = parser.getAttributeValue(null, "name"); |
| 1478 | if (fname == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1479 | Slog.w(TAG, "<feature> without name at " |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1480 | + parser.getPositionDescription()); |
| 1481 | } else { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1482 | //Log.i(TAG, "Got feature " + fname); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1483 | FeatureInfo fi = new FeatureInfo(); |
| 1484 | fi.name = fname; |
| 1485 | mAvailableFeatures.put(fname, fi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | } |
| 1487 | XmlUtils.skipCurrentTag(parser); |
| 1488 | continue; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1489 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | } else { |
| 1491 | XmlUtils.skipCurrentTag(parser); |
| 1492 | continue; |
| 1493 | } |
| 1494 | |
| 1495 | } |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 1496 | permReader.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | } catch (XmlPullParserException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1498 | Slog.w(TAG, "Got execption parsing permissions.", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1500 | Slog.w(TAG, "Got execption parsing permissions.", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | void readPermission(XmlPullParser parser, String name) |
| 1505 | throws IOException, XmlPullParserException { |
| 1506 | |
| 1507 | name = name.intern(); |
| 1508 | |
| 1509 | BasePermission bp = mSettings.mPermissions.get(name); |
| 1510 | if (bp == null) { |
| 1511 | bp = new BasePermission(name, null, BasePermission.TYPE_BUILTIN); |
| 1512 | mSettings.mPermissions.put(name, bp); |
| 1513 | } |
| 1514 | int outerDepth = parser.getDepth(); |
| 1515 | int type; |
| 1516 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 1517 | && (type != XmlPullParser.END_TAG |
| 1518 | || parser.getDepth() > outerDepth)) { |
| 1519 | if (type == XmlPullParser.END_TAG |
| 1520 | || type == XmlPullParser.TEXT) { |
| 1521 | continue; |
| 1522 | } |
| 1523 | |
| 1524 | String tagName = parser.getName(); |
| 1525 | if ("group".equals(tagName)) { |
| 1526 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1527 | if (gidStr != null) { |
| 1528 | int gid = Process.getGidForName(gidStr); |
| 1529 | bp.gids = appendInt(bp.gids, gid); |
| 1530 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1531 | Slog.w(TAG, "<group> without gid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1532 | + parser.getPositionDescription()); |
| 1533 | } |
| 1534 | } |
| 1535 | XmlUtils.skipCurrentTag(parser); |
| 1536 | } |
| 1537 | } |
| 1538 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | static int[] appendInts(int[] cur, int[] add) { |
| 1540 | if (add == null) return cur; |
| 1541 | if (cur == null) return add; |
| 1542 | final int N = add.length; |
| 1543 | for (int i=0; i<N; i++) { |
| 1544 | cur = appendInt(cur, add[i]); |
| 1545 | } |
| 1546 | return cur; |
| 1547 | } |
| 1548 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1549 | static int[] removeInts(int[] cur, int[] rem) { |
| 1550 | if (rem == null) return cur; |
| 1551 | if (cur == null) return cur; |
| 1552 | final int N = rem.length; |
| 1553 | for (int i=0; i<N; i++) { |
| 1554 | cur = removeInt(cur, rem[i]); |
| 1555 | } |
| 1556 | return cur; |
| 1557 | } |
| 1558 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1559 | PackageInfo generatePackageInfo(PackageParser.Package p, int flags, int userId) { |
| 1560 | if (!sUserManager.exists(userId)) return null; |
| 1561 | PackageInfo pi; |
Suchi Amalapurapu | b897cff | 2009-10-14 12:11:48 -0700 | [diff] [blame] | 1562 | if ((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
| 1563 | // The package has been uninstalled but has retained data and resources. |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1564 | pi = PackageParser.generatePackageInfo(p, null, flags, 0, 0, null, false, 0, userId); |
| 1565 | } else { |
| 1566 | final PackageSetting ps = (PackageSetting) p.mExtras; |
| 1567 | if (ps == null) { |
| 1568 | return null; |
| 1569 | } |
| 1570 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 1571 | pi = PackageParser.generatePackageInfo(p, gp.gids, flags, |
| 1572 | ps.firstInstallTime, ps.lastUpdateTime, gp.grantedPermissions, |
| 1573 | ps.getStopped(userId), ps.getEnabled(userId), userId); |
| 1574 | pi.applicationInfo.enabledSetting = ps.getEnabled(userId); |
| 1575 | pi.applicationInfo.enabled = |
| 1576 | pi.applicationInfo.enabledSetting == COMPONENT_ENABLED_STATE_DEFAULT |
| 1577 | || pi.applicationInfo.enabledSetting == COMPONENT_ENABLED_STATE_ENABLED; |
Suchi Amalapurapu | b897cff | 2009-10-14 12:11:48 -0700 | [diff] [blame] | 1578 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1579 | return pi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1580 | } |
| 1581 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1582 | @Override |
| 1583 | public PackageInfo getPackageInfo(String packageName, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1584 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1585 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | synchronized (mPackages) { |
| 1587 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1588 | if (DEBUG_PACKAGE_INFO) |
| 1589 | Log.v(TAG, "getPackageInfo " + packageName + ": " + p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1591 | return generatePackageInfo(p, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | } |
| 1593 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1594 | return generatePackageInfoFromSettingsLPw(packageName, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | return null; |
| 1598 | } |
| 1599 | |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1600 | public String[] currentToCanonicalPackageNames(String[] names) { |
| 1601 | String[] out = new String[names.length]; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1602 | // reader |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1603 | synchronized (mPackages) { |
| 1604 | for (int i=names.length-1; i>=0; i--) { |
| 1605 | PackageSetting ps = mSettings.mPackages.get(names[i]); |
| 1606 | out[i] = ps != null && ps.realName != null ? ps.realName : names[i]; |
| 1607 | } |
| 1608 | } |
| 1609 | return out; |
| 1610 | } |
| 1611 | |
| 1612 | public String[] canonicalToCurrentPackageNames(String[] names) { |
| 1613 | String[] out = new String[names.length]; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1614 | // reader |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1615 | synchronized (mPackages) { |
| 1616 | for (int i=names.length-1; i>=0; i--) { |
| 1617 | String cur = mSettings.mRenamedPackages.get(names[i]); |
| 1618 | out[i] = cur != null ? cur : names[i]; |
| 1619 | } |
| 1620 | } |
| 1621 | return out; |
| 1622 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1623 | |
| 1624 | @Override |
| 1625 | public int getPackageUid(String packageName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1626 | if (!sUserManager.exists(userId)) return -1; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1627 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | synchronized (mPackages) { |
| 1629 | PackageParser.Package p = mPackages.get(packageName); |
| 1630 | if(p != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1631 | return UserId.getUid(userId, p.applicationInfo.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1632 | } |
| 1633 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1634 | if((ps == null) || (ps.pkg == null) || (ps.pkg.applicationInfo == null)) { |
| 1635 | return -1; |
| 1636 | } |
| 1637 | p = ps.pkg; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1638 | 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] | 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | public int[] getPackageGids(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1643 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | synchronized (mPackages) { |
| 1645 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1646 | if (DEBUG_PACKAGE_INFO) |
| 1647 | Log.v(TAG, "getPackageGids" + packageName + ": " + p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1648 | if (p != null) { |
| 1649 | final PackageSetting ps = (PackageSetting)p.mExtras; |
| 1650 | final SharedUserSetting suid = ps.sharedUser; |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 1651 | int[] gids = suid != null ? suid.gids : ps.gids; |
| 1652 | |
| 1653 | // include GIDs for any unenforced permissions |
| 1654 | if (!isPermissionEnforcedLocked(READ_EXTERNAL_STORAGE)) { |
| 1655 | final BasePermission basePerm = mSettings.mPermissions.get( |
| 1656 | READ_EXTERNAL_STORAGE); |
| 1657 | gids = appendInts(gids, basePerm.gids); |
| 1658 | } |
| 1659 | |
| 1660 | return gids; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | // stupid thing to indicate an error. |
| 1664 | return new int[0]; |
| 1665 | } |
| 1666 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1667 | static final PermissionInfo generatePermissionInfo( |
| 1668 | BasePermission bp, int flags) { |
| 1669 | if (bp.perm != null) { |
| 1670 | return PackageParser.generatePermissionInfo(bp.perm, flags); |
| 1671 | } |
| 1672 | PermissionInfo pi = new PermissionInfo(); |
| 1673 | pi.name = bp.name; |
| 1674 | pi.packageName = bp.sourcePackage; |
| 1675 | pi.nonLocalizedLabel = bp.name; |
| 1676 | pi.protectionLevel = bp.protectionLevel; |
| 1677 | return pi; |
| 1678 | } |
| 1679 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1680 | public PermissionInfo getPermissionInfo(String name, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1681 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | synchronized (mPackages) { |
| 1683 | final BasePermission p = mSettings.mPermissions.get(name); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1684 | if (p != null) { |
| 1685 | return generatePermissionInfo(p, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | } |
| 1687 | return null; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | public List<PermissionInfo> queryPermissionsByGroup(String group, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1692 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1693 | synchronized (mPackages) { |
| 1694 | ArrayList<PermissionInfo> out = new ArrayList<PermissionInfo>(10); |
| 1695 | for (BasePermission p : mSettings.mPermissions.values()) { |
| 1696 | if (group == null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1697 | if (p.perm == null || p.perm.info.group == null) { |
| 1698 | out.add(generatePermissionInfo(p, flags)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1699 | } |
| 1700 | } else { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1701 | 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] | 1702 | out.add(PackageParser.generatePermissionInfo(p.perm, flags)); |
| 1703 | } |
| 1704 | } |
| 1705 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | if (out.size() > 0) { |
| 1708 | return out; |
| 1709 | } |
| 1710 | return mPermissionGroups.containsKey(group) ? out : null; |
| 1711 | } |
| 1712 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1713 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1714 | public PermissionGroupInfo getPermissionGroupInfo(String name, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1715 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1716 | synchronized (mPackages) { |
| 1717 | return PackageParser.generatePermissionGroupInfo( |
| 1718 | mPermissionGroups.get(name), flags); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | public List<PermissionGroupInfo> getAllPermissionGroups(int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1723 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1724 | synchronized (mPackages) { |
| 1725 | final int N = mPermissionGroups.size(); |
| 1726 | ArrayList<PermissionGroupInfo> out |
| 1727 | = new ArrayList<PermissionGroupInfo>(N); |
| 1728 | for (PackageParser.PermissionGroup pg : mPermissionGroups.values()) { |
| 1729 | out.add(PackageParser.generatePermissionGroupInfo(pg, flags)); |
| 1730 | } |
| 1731 | return out; |
| 1732 | } |
| 1733 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1734 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1735 | private ApplicationInfo generateApplicationInfoFromSettingsLPw(String packageName, int flags, |
| 1736 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1737 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1739 | if (ps != null) { |
| 1740 | if (ps.pkg == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1741 | PackageInfo pInfo = generatePackageInfoFromSettingsLPw(packageName, flags, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1742 | if (pInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | return pInfo.applicationInfo; |
| 1744 | } |
| 1745 | return null; |
| 1746 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1747 | return PackageParser.generateApplicationInfo(ps.pkg, flags, ps.getStopped(userId), |
| 1748 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1749 | } |
| 1750 | return null; |
| 1751 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1752 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1753 | private PackageInfo generatePackageInfoFromSettingsLPw(String packageName, int flags, |
| 1754 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1755 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1756 | PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1757 | if (ps != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1758 | PackageParser.Package pkg = new PackageParser.Package(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1759 | if (ps.pkg == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1760 | ps.pkg = new PackageParser.Package(packageName); |
| 1761 | ps.pkg.applicationInfo.packageName = packageName; |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 1762 | ps.pkg.applicationInfo.flags = ps.pkgFlags; |
| 1763 | ps.pkg.applicationInfo.publicSourceDir = ps.resourcePathString; |
| 1764 | ps.pkg.applicationInfo.sourceDir = ps.codePathString; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1765 | ps.pkg.applicationInfo.dataDir = |
| 1766 | getDataPathForPackage(ps.pkg.packageName, 0).getPath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 1767 | ps.pkg.applicationInfo.nativeLibraryDir = ps.nativeLibraryPathString; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1768 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1769 | // ps.pkg.mSetEnabled = ps.getEnabled(userId); |
| 1770 | // ps.pkg.mSetStopped = ps.getStopped(userId); |
| 1771 | return generatePackageInfo(ps.pkg, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1772 | } |
| 1773 | return null; |
| 1774 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1775 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1776 | @Override |
| 1777 | public ApplicationInfo getApplicationInfo(String packageName, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1778 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1779 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1780 | synchronized (mPackages) { |
| 1781 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1782 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1783 | TAG, "getApplicationInfo " + packageName |
| 1784 | + ": " + p); |
| 1785 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1786 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1787 | if (ps == null) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1788 | // Note: isEnabledLP() does not apply here - always return info |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1789 | return PackageParser.generateApplicationInfo(p, flags, ps.getStopped(userId), |
| 1790 | ps.getEnabled(userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | } |
| 1792 | if ("android".equals(packageName)||"system".equals(packageName)) { |
| 1793 | return mAndroidApplication; |
| 1794 | } |
| 1795 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1796 | return generateApplicationInfoFromSettingsLPw(packageName, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | return null; |
| 1800 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1801 | |
| 1802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1803 | public void freeStorageAndNotify(final long freeStorageSize, final IPackageDataObserver observer) { |
| 1804 | mContext.enforceCallingOrSelfPermission( |
| 1805 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1806 | // Queue up an async operation since clearing cache may take a little while. |
| 1807 | mHandler.post(new Runnable() { |
| 1808 | public void run() { |
| 1809 | mHandler.removeCallbacks(this); |
| 1810 | int retCode = -1; |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1811 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1812 | if (retCode < 0) { |
| 1813 | Slog.w(TAG, "Couldn't clear application caches"); |
| 1814 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1815 | if (observer != null) { |
| 1816 | try { |
| 1817 | observer.onRemoveCompleted(null, (retCode >= 0)); |
| 1818 | } catch (RemoteException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1819 | Slog.w(TAG, "RemoveException when invoking call back"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | }); |
| 1824 | } |
| 1825 | |
Suchi Amalapurapu | bc806f6 | 2009-06-17 15:18:19 -0700 | [diff] [blame] | 1826 | public void freeStorage(final long freeStorageSize, final IntentSender pi) { |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1827 | mContext.enforceCallingOrSelfPermission( |
| 1828 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1829 | // Queue up an async operation since clearing cache may take a little while. |
| 1830 | mHandler.post(new Runnable() { |
| 1831 | public void run() { |
| 1832 | mHandler.removeCallbacks(this); |
| 1833 | int retCode = -1; |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1834 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1835 | if (retCode < 0) { |
| 1836 | Slog.w(TAG, "Couldn't clear application caches"); |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1837 | } |
| 1838 | if(pi != null) { |
| 1839 | try { |
| 1840 | // Callback via pending intent |
| 1841 | int code = (retCode >= 0) ? 1 : 0; |
| 1842 | pi.sendIntent(null, code, null, |
| 1843 | null, null); |
| 1844 | } catch (SendIntentException e1) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1845 | Slog.i(TAG, "Failed to send pending intent"); |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1846 | } |
| 1847 | } |
| 1848 | } |
| 1849 | }); |
| 1850 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1851 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1852 | @Override |
| 1853 | public ActivityInfo getActivityInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1854 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1855 | synchronized (mPackages) { |
| 1856 | PackageParser.Activity a = mActivities.mActivities.get(component); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 1857 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1858 | if (DEBUG_PACKAGE_INFO) Log.v(TAG, "getActivityInfo " + component + ": " + a); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1859 | if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1860 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1861 | if (ps == null) return null; |
| 1862 | return PackageParser.generateActivityInfo(a, flags, ps.getStopped(userId), |
| 1863 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1864 | } |
| 1865 | if (mResolveComponentName.equals(component)) { |
| 1866 | return mResolveActivity; |
| 1867 | } |
| 1868 | } |
| 1869 | return null; |
| 1870 | } |
| 1871 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1872 | @Override |
| 1873 | public ActivityInfo getReceiverInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1874 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1875 | synchronized (mPackages) { |
| 1876 | PackageParser.Activity a = mReceivers.mActivities.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1877 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1878 | TAG, "getReceiverInfo " + component + ": " + a); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1879 | if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1880 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1881 | if (ps == null) return null; |
| 1882 | return PackageParser.generateActivityInfo(a, flags, ps.getStopped(userId), |
| 1883 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | return null; |
| 1887 | } |
| 1888 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1889 | @Override |
| 1890 | public ServiceInfo getServiceInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1891 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1892 | synchronized (mPackages) { |
| 1893 | PackageParser.Service s = mServices.mServices.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1894 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1895 | TAG, "getServiceInfo " + component + ": " + s); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1896 | if (s != null && mSettings.isEnabledLPr(s.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1897 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1898 | if (ps == null) return null; |
| 1899 | return PackageParser.generateServiceInfo(s, flags, ps.getStopped(userId), |
| 1900 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | return null; |
| 1904 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1905 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1906 | @Override |
| 1907 | public ProviderInfo getProviderInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1908 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 1909 | synchronized (mPackages) { |
| 1910 | PackageParser.Provider p = mProvidersByComponent.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1911 | if (DEBUG_PACKAGE_INFO) Log.v( |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 1912 | TAG, "getProviderInfo " + component + ": " + p); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1913 | if (p != null && mSettings.isEnabledLPr(p.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1914 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 1915 | if (ps == null) return null; |
| 1916 | return PackageParser.generateProviderInfo(p, flags, ps.getStopped(userId), |
| 1917 | ps.getEnabled(userId), userId); |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 1918 | } |
| 1919 | } |
| 1920 | return null; |
| 1921 | } |
| 1922 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1923 | public String[] getSystemSharedLibraryNames() { |
| 1924 | Set<String> libSet; |
| 1925 | synchronized (mPackages) { |
| 1926 | libSet = mSharedLibraries.keySet(); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1927 | int size = libSet.size(); |
| 1928 | if (size > 0) { |
| 1929 | String[] libs = new String[size]; |
| 1930 | libSet.toArray(libs); |
| 1931 | return libs; |
| 1932 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1933 | } |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1934 | return null; |
| 1935 | } |
| 1936 | |
| 1937 | public FeatureInfo[] getSystemAvailableFeatures() { |
| 1938 | Collection<FeatureInfo> featSet; |
| 1939 | synchronized (mPackages) { |
| 1940 | featSet = mAvailableFeatures.values(); |
| 1941 | int size = featSet.size(); |
| 1942 | if (size > 0) { |
| 1943 | FeatureInfo[] features = new FeatureInfo[size+1]; |
| 1944 | featSet.toArray(features); |
| 1945 | FeatureInfo fi = new FeatureInfo(); |
| 1946 | fi.reqGlEsVersion = SystemProperties.getInt("ro.opengles.version", |
| 1947 | FeatureInfo.GL_ES_VERSION_UNDEFINED); |
| 1948 | features[size] = fi; |
| 1949 | return features; |
| 1950 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1951 | } |
| 1952 | return null; |
| 1953 | } |
| 1954 | |
Dianne Hackborn | 039c68e | 2009-09-26 16:39:23 -0700 | [diff] [blame] | 1955 | public boolean hasSystemFeature(String name) { |
| 1956 | synchronized (mPackages) { |
| 1957 | return mAvailableFeatures.containsKey(name); |
| 1958 | } |
| 1959 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1960 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1961 | private void checkValidCaller(int uid, int userId) { |
| 1962 | if (UserId.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) |
| 1963 | return; |
| 1964 | |
| 1965 | throw new SecurityException("Caller uid=" + uid |
| 1966 | + " is not privileged to communicate with user=" + userId); |
| 1967 | } |
| 1968 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | public int checkPermission(String permName, String pkgName) { |
| 1970 | synchronized (mPackages) { |
| 1971 | PackageParser.Package p = mPackages.get(pkgName); |
| 1972 | if (p != null && p.mExtras != null) { |
| 1973 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 1974 | if (ps.sharedUser != null) { |
| 1975 | if (ps.sharedUser.grantedPermissions.contains(permName)) { |
| 1976 | return PackageManager.PERMISSION_GRANTED; |
| 1977 | } |
| 1978 | } else if (ps.grantedPermissions.contains(permName)) { |
| 1979 | return PackageManager.PERMISSION_GRANTED; |
| 1980 | } |
| 1981 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 1982 | if (!isPermissionEnforcedLocked(permName)) { |
| 1983 | return PackageManager.PERMISSION_GRANTED; |
| 1984 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1985 | } |
| 1986 | return PackageManager.PERMISSION_DENIED; |
| 1987 | } |
| 1988 | |
| 1989 | public int checkUidPermission(String permName, int uid) { |
| 1990 | synchronized (mPackages) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1991 | Object obj = mSettings.getUserIdLPr(UserId.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1992 | if (obj != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1993 | GrantedPermissions gp = (GrantedPermissions)obj; |
| 1994 | if (gp.grantedPermissions.contains(permName)) { |
| 1995 | return PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | } |
| 1997 | } else { |
| 1998 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 1999 | if (perms != null && perms.contains(permName)) { |
| 2000 | return PackageManager.PERMISSION_GRANTED; |
| 2001 | } |
| 2002 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 2003 | if (!isPermissionEnforcedLocked(permName)) { |
| 2004 | return PackageManager.PERMISSION_GRANTED; |
| 2005 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2006 | } |
| 2007 | return PackageManager.PERMISSION_DENIED; |
| 2008 | } |
| 2009 | |
| 2010 | private BasePermission findPermissionTreeLP(String permName) { |
| 2011 | for(BasePermission bp : mSettings.mPermissionTrees.values()) { |
| 2012 | if (permName.startsWith(bp.name) && |
| 2013 | permName.length() > bp.name.length() && |
| 2014 | permName.charAt(bp.name.length()) == '.') { |
| 2015 | return bp; |
| 2016 | } |
| 2017 | } |
| 2018 | return null; |
| 2019 | } |
| 2020 | |
| 2021 | private BasePermission checkPermissionTreeLP(String permName) { |
| 2022 | if (permName != null) { |
| 2023 | BasePermission bp = findPermissionTreeLP(permName); |
| 2024 | if (bp != null) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2025 | if (bp.uid == UserId.getAppId(Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2026 | return bp; |
| 2027 | } |
| 2028 | throw new SecurityException("Calling uid " |
| 2029 | + Binder.getCallingUid() |
| 2030 | + " is not allowed to add to permission tree " |
| 2031 | + bp.name + " owned by uid " + bp.uid); |
| 2032 | } |
| 2033 | } |
| 2034 | throw new SecurityException("No permission tree found for " + permName); |
| 2035 | } |
| 2036 | |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2037 | static boolean compareStrings(CharSequence s1, CharSequence s2) { |
| 2038 | if (s1 == null) { |
| 2039 | return s2 == null; |
| 2040 | } |
| 2041 | if (s2 == null) { |
| 2042 | return false; |
| 2043 | } |
| 2044 | if (s1.getClass() != s2.getClass()) { |
| 2045 | return false; |
| 2046 | } |
| 2047 | return s1.equals(s2); |
| 2048 | } |
| 2049 | |
| 2050 | static boolean comparePermissionInfos(PermissionInfo pi1, PermissionInfo pi2) { |
| 2051 | if (pi1.icon != pi2.icon) return false; |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 2052 | if (pi1.logo != pi2.logo) return false; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2053 | if (pi1.protectionLevel != pi2.protectionLevel) return false; |
| 2054 | if (!compareStrings(pi1.name, pi2.name)) return false; |
| 2055 | if (!compareStrings(pi1.nonLocalizedLabel, pi2.nonLocalizedLabel)) return false; |
| 2056 | // We'll take care of setting this one. |
| 2057 | if (!compareStrings(pi1.packageName, pi2.packageName)) return false; |
| 2058 | // These are not currently stored in settings. |
| 2059 | //if (!compareStrings(pi1.group, pi2.group)) return false; |
| 2060 | //if (!compareStrings(pi1.nonLocalizedDescription, pi2.nonLocalizedDescription)) return false; |
| 2061 | //if (pi1.labelRes != pi2.labelRes) return false; |
| 2062 | //if (pi1.descriptionRes != pi2.descriptionRes) return false; |
| 2063 | return true; |
| 2064 | } |
| 2065 | |
| 2066 | boolean addPermissionLocked(PermissionInfo info, boolean async) { |
| 2067 | if (info.labelRes == 0 && info.nonLocalizedLabel == null) { |
| 2068 | throw new SecurityException("Label must be specified in permission"); |
| 2069 | } |
| 2070 | BasePermission tree = checkPermissionTreeLP(info.name); |
| 2071 | BasePermission bp = mSettings.mPermissions.get(info.name); |
| 2072 | boolean added = bp == null; |
| 2073 | boolean changed = true; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2074 | int fixedLevel = PermissionInfo.fixProtectionLevel(info.protectionLevel); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2075 | if (added) { |
| 2076 | bp = new BasePermission(info.name, tree.sourcePackage, |
| 2077 | BasePermission.TYPE_DYNAMIC); |
| 2078 | } else if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 2079 | throw new SecurityException( |
| 2080 | "Not allowed to modify non-dynamic permission " |
| 2081 | + info.name); |
| 2082 | } else { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2083 | if (bp.protectionLevel == fixedLevel |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2084 | && bp.perm.owner.equals(tree.perm.owner) |
| 2085 | && bp.uid == tree.uid |
| 2086 | && comparePermissionInfos(bp.perm.info, info)) { |
| 2087 | changed = false; |
| 2088 | } |
| 2089 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2090 | bp.protectionLevel = fixedLevel; |
| 2091 | info = new PermissionInfo(info); |
| 2092 | info.protectionLevel = fixedLevel; |
| 2093 | bp.perm = new PackageParser.Permission(tree.perm.owner, info); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2094 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 2095 | bp.uid = tree.uid; |
| 2096 | if (added) { |
| 2097 | mSettings.mPermissions.put(info.name, bp); |
| 2098 | } |
| 2099 | if (changed) { |
| 2100 | if (!async) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2101 | mSettings.writeLPr(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2102 | } else { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2103 | scheduleWriteSettingsLocked(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | return added; |
| 2107 | } |
| 2108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2109 | public boolean addPermission(PermissionInfo info) { |
| 2110 | synchronized (mPackages) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2111 | return addPermissionLocked(info, false); |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | public boolean addPermissionAsync(PermissionInfo info) { |
| 2116 | synchronized (mPackages) { |
| 2117 | return addPermissionLocked(info, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | public void removePermission(String name) { |
| 2122 | synchronized (mPackages) { |
| 2123 | checkPermissionTreeLP(name); |
| 2124 | BasePermission bp = mSettings.mPermissions.get(name); |
| 2125 | if (bp != null) { |
| 2126 | if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 2127 | throw new SecurityException( |
| 2128 | "Not allowed to modify non-dynamic permission " |
| 2129 | + name); |
| 2130 | } |
| 2131 | mSettings.mPermissions.remove(name); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2132 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2137 | public void grantPermission(String packageName, String permissionName) { |
| 2138 | mContext.enforceCallingOrSelfPermission( |
| 2139 | android.Manifest.permission.GRANT_REVOKE_PERMISSIONS, null); |
| 2140 | synchronized (mPackages) { |
| 2141 | final PackageParser.Package pkg = mPackages.get(packageName); |
| 2142 | if (pkg == null) { |
| 2143 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2144 | } |
| 2145 | final BasePermission bp = mSettings.mPermissions.get(permissionName); |
| 2146 | if (bp == null) { |
| 2147 | throw new IllegalArgumentException("Unknown permission: " + packageName); |
| 2148 | } |
| 2149 | if (!pkg.requestedPermissions.contains(permissionName)) { |
| 2150 | throw new SecurityException("Package " + packageName |
| 2151 | + " has not requested permission " + permissionName); |
| 2152 | } |
| 2153 | if ((bp.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) == 0) { |
| 2154 | throw new SecurityException("Permission " + permissionName |
| 2155 | + " is not a development permission"); |
| 2156 | } |
| 2157 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2158 | if (ps == null) { |
| 2159 | return; |
| 2160 | } |
| 2161 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 2162 | if (gp.grantedPermissions.add(permissionName)) { |
| 2163 | if (ps.haveGids) { |
| 2164 | gp.gids = appendInts(gp.gids, bp.gids); |
| 2165 | } |
| 2166 | mSettings.writeLPr(); |
| 2167 | } |
| 2168 | } |
| 2169 | } |
| 2170 | |
| 2171 | public void revokePermission(String packageName, String permissionName) { |
| 2172 | synchronized (mPackages) { |
| 2173 | final PackageParser.Package pkg = mPackages.get(packageName); |
| 2174 | if (pkg == null) { |
| 2175 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2176 | } |
| 2177 | if (pkg.applicationInfo.uid != Binder.getCallingUid()) { |
| 2178 | mContext.enforceCallingOrSelfPermission( |
| 2179 | android.Manifest.permission.GRANT_REVOKE_PERMISSIONS, null); |
| 2180 | } |
| 2181 | final BasePermission bp = mSettings.mPermissions.get(permissionName); |
| 2182 | if (bp == null) { |
| 2183 | throw new IllegalArgumentException("Unknown permission: " + packageName); |
| 2184 | } |
| 2185 | if (!pkg.requestedPermissions.contains(permissionName)) { |
| 2186 | throw new SecurityException("Package " + packageName |
| 2187 | + " has not requested permission " + permissionName); |
| 2188 | } |
| 2189 | if ((bp.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) == 0) { |
| 2190 | throw new SecurityException("Permission " + permissionName |
| 2191 | + " is not a development permission"); |
| 2192 | } |
| 2193 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2194 | if (ps == null) { |
| 2195 | return; |
| 2196 | } |
| 2197 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 2198 | if (gp.grantedPermissions.remove(permissionName)) { |
| 2199 | gp.grantedPermissions.remove(permissionName); |
| 2200 | if (ps.haveGids) { |
| 2201 | gp.gids = removeInts(gp.gids, bp.gids); |
| 2202 | } |
| 2203 | mSettings.writeLPr(); |
| 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 2208 | public boolean isProtectedBroadcast(String actionName) { |
| 2209 | synchronized (mPackages) { |
| 2210 | return mProtectedBroadcasts.contains(actionName); |
| 2211 | } |
| 2212 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2214 | public int checkSignatures(String pkg1, String pkg2) { |
| 2215 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2216 | final PackageParser.Package p1 = mPackages.get(pkg1); |
| 2217 | final PackageParser.Package p2 = mPackages.get(pkg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2218 | if (p1 == null || p1.mExtras == null |
| 2219 | || p2 == null || p2.mExtras == null) { |
| 2220 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2221 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2222 | return compareSignatures(p1.mSignatures, p2.mSignatures); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2226 | public int checkUidSignatures(int uid1, int uid2) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2227 | // Map to base uids. |
| 2228 | uid1 = UserId.getAppId(uid1); |
| 2229 | uid2 = UserId.getAppId(uid2); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2230 | // reader |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2231 | synchronized (mPackages) { |
| 2232 | Signature[] s1; |
| 2233 | Signature[] s2; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2234 | Object obj = mSettings.getUserIdLPr(uid1); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2235 | if (obj != null) { |
| 2236 | if (obj instanceof SharedUserSetting) { |
| 2237 | s1 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 2238 | } else if (obj instanceof PackageSetting) { |
| 2239 | s1 = ((PackageSetting)obj).signatures.mSignatures; |
| 2240 | } else { |
| 2241 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2242 | } |
| 2243 | } else { |
| 2244 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2245 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2246 | obj = mSettings.getUserIdLPr(uid2); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2247 | if (obj != null) { |
| 2248 | if (obj instanceof SharedUserSetting) { |
| 2249 | s2 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 2250 | } else if (obj instanceof PackageSetting) { |
| 2251 | s2 = ((PackageSetting)obj).signatures.mSignatures; |
| 2252 | } else { |
| 2253 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2254 | } |
| 2255 | } else { |
| 2256 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2257 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2258 | return compareSignatures(s1, s2); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2262 | static int compareSignatures(Signature[] s1, Signature[] s2) { |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2263 | if (s1 == null) { |
| 2264 | return s2 == null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2265 | ? PackageManager.SIGNATURE_NEITHER_SIGNED |
| 2266 | : PackageManager.SIGNATURE_FIRST_NOT_SIGNED; |
| 2267 | } |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2268 | if (s2 == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2269 | return PackageManager.SIGNATURE_SECOND_NOT_SIGNED; |
| 2270 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2271 | HashSet<Signature> set1 = new HashSet<Signature>(); |
| 2272 | for (Signature sig : s1) { |
| 2273 | set1.add(sig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2274 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2275 | HashSet<Signature> set2 = new HashSet<Signature>(); |
| 2276 | for (Signature sig : s2) { |
| 2277 | set2.add(sig); |
| 2278 | } |
| 2279 | // Make sure s2 contains all signatures in s1. |
| 2280 | if (set1.equals(set2)) { |
| 2281 | return PackageManager.SIGNATURE_MATCH; |
| 2282 | } |
| 2283 | return PackageManager.SIGNATURE_NO_MATCH; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | public String[] getPackagesForUid(int uid) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2287 | uid = UserId.getAppId(uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2288 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2289 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2290 | Object obj = mSettings.getUserIdLPr(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2291 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2292 | final SharedUserSetting sus = (SharedUserSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2293 | final int N = sus.packages.size(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2294 | final String[] res = new String[N]; |
| 2295 | final Iterator<PackageSetting> it = sus.packages.iterator(); |
| 2296 | int i = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2297 | while (it.hasNext()) { |
| 2298 | res[i++] = it.next().name; |
| 2299 | } |
| 2300 | return res; |
| 2301 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2302 | final PackageSetting ps = (PackageSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2303 | return new String[] { ps.name }; |
| 2304 | } |
| 2305 | } |
| 2306 | return null; |
| 2307 | } |
| 2308 | |
| 2309 | public String getNameForUid(int uid) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2310 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2311 | synchronized (mPackages) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2312 | Object obj = mSettings.getUserIdLPr(UserId.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2313 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2314 | final SharedUserSetting sus = (SharedUserSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2315 | return sus.name + ":" + sus.userId; |
| 2316 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2317 | final PackageSetting ps = (PackageSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2318 | return ps.name; |
| 2319 | } |
| 2320 | } |
| 2321 | return null; |
| 2322 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2324 | public int getUidForSharedUser(String sharedUserName) { |
| 2325 | if(sharedUserName == null) { |
| 2326 | return -1; |
| 2327 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2328 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2329 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2330 | final SharedUserSetting suid = mSettings.getSharedUserLPw(sharedUserName, 0, false); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2331 | if (suid == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2332 | return -1; |
| 2333 | } |
| 2334 | return suid.userId; |
| 2335 | } |
| 2336 | } |
| 2337 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2338 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2339 | public ResolveInfo resolveIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2340 | int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2341 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2342 | List<ResolveInfo> query = queryIntentActivities(intent, resolvedType, flags, userId); |
| 2343 | return chooseBestActivity(intent, resolvedType, flags, query, userId); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 2344 | } |
| 2345 | |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 2346 | private ResolveInfo chooseBestActivity(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2347 | int flags, List<ResolveInfo> query, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2348 | if (query != null) { |
| 2349 | final int N = query.size(); |
| 2350 | if (N == 1) { |
| 2351 | return query.get(0); |
| 2352 | } else if (N > 1) { |
| 2353 | // If there is more than one activity with the same priority, |
| 2354 | // then let the user decide between them. |
| 2355 | ResolveInfo r0 = query.get(0); |
| 2356 | ResolveInfo r1 = query.get(1); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 2357 | if (DEBUG_INTENT_MATCHING) { |
| 2358 | Log.d(TAG, r0.activityInfo.name + "=" + r0.priority + " vs " |
| 2359 | + r1.activityInfo.name + "=" + r1.priority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2360 | } |
| 2361 | // If the first activity has a higher priority, or a different |
| 2362 | // default, then it is always desireable to pick it. |
| 2363 | if (r0.priority != r1.priority |
| 2364 | || r0.preferredOrder != r1.preferredOrder |
| 2365 | || r0.isDefault != r1.isDefault) { |
| 2366 | return query.get(0); |
| 2367 | } |
| 2368 | // If we have saved a preference for a preferred activity for |
| 2369 | // this Intent, use that. |
| 2370 | ResolveInfo ri = findPreferredActivity(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2371 | flags, query, r0.priority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2372 | if (ri != null) { |
| 2373 | return ri; |
| 2374 | } |
| 2375 | return mResolveInfo; |
| 2376 | } |
| 2377 | } |
| 2378 | return null; |
| 2379 | } |
| 2380 | |
| 2381 | ResolveInfo findPreferredActivity(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2382 | int flags, List<ResolveInfo> query, int priority, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2383 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2384 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2385 | synchronized (mPackages) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2386 | if (intent.getSelector() != null) { |
| 2387 | intent = intent.getSelector(); |
| 2388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2389 | if (DEBUG_PREFERRED) intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION); |
| 2390 | List<PreferredActivity> prefs = |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 2391 | mSettings.mPreferredActivities.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2392 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2393 | if (prefs != null && prefs.size() > 0) { |
| 2394 | // First figure out how good the original match set is. |
| 2395 | // We will only allow preferred activities that came |
| 2396 | // from the same match quality. |
| 2397 | int match = 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2398 | |
| 2399 | if (DEBUG_PREFERRED) { |
| 2400 | Log.v(TAG, "Figuring out best match..."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2401 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2402 | |
| 2403 | final int N = query.size(); |
| 2404 | for (int j=0; j<N; j++) { |
| 2405 | final ResolveInfo ri = query.get(j); |
| 2406 | if (DEBUG_PREFERRED) { |
| 2407 | Log.v(TAG, "Match for " + ri.activityInfo + ": 0x" |
| 2408 | + Integer.toHexString(match)); |
| 2409 | } |
| 2410 | if (ri.match > match) { |
| 2411 | match = ri.match; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | if (DEBUG_PREFERRED) { |
| 2416 | Log.v(TAG, "Best match: 0x" + Integer.toHexString(match)); |
| 2417 | } |
| 2418 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2419 | match &= IntentFilter.MATCH_CATEGORY_MASK; |
| 2420 | final int M = prefs.size(); |
| 2421 | for (int i=0; i<M; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2422 | final PreferredActivity pa = prefs.get(i); |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 2423 | if (pa.mPref.mMatch != match) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2424 | continue; |
| 2425 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2426 | final ActivityInfo ai = getActivityInfo(pa.mPref.mComponent, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2427 | if (DEBUG_PREFERRED) { |
| 2428 | Log.v(TAG, "Got preferred activity:"); |
Marco Nelissen | d85621c | 2010-09-03 09:25:33 -0700 | [diff] [blame] | 2429 | if (ai != null) { |
| 2430 | ai.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 2431 | } else { |
| 2432 | Log.v(TAG, " null"); |
| 2433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2434 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2435 | if (ai == null) { |
| 2436 | // This previously registered preferred activity |
| 2437 | // component is no longer known. Most likely an update |
| 2438 | // to the app was installed and in the new version this |
| 2439 | // component no longer exists. Clean it up by removing |
| 2440 | // it from the preferred activities list, and skip it. |
| 2441 | Slog.w(TAG, "Removing dangling preferred activity: " |
| 2442 | + pa.mPref.mComponent); |
| 2443 | mSettings.mPreferredActivities.removeFilter(pa); |
| 2444 | continue; |
| 2445 | } |
| 2446 | for (int j=0; j<N; j++) { |
| 2447 | final ResolveInfo ri = query.get(j); |
| 2448 | if (!ri.activityInfo.applicationInfo.packageName |
| 2449 | .equals(ai.applicationInfo.packageName)) { |
| 2450 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2451 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2452 | if (!ri.activityInfo.name.equals(ai.name)) { |
| 2453 | continue; |
| 2454 | } |
| 2455 | |
| 2456 | // Okay we found a previously set preferred app. |
| 2457 | // If the result set is different from when this |
| 2458 | // was created, we need to clear it and re-ask the |
| 2459 | // user their preference. |
| 2460 | if (!pa.mPref.sameSet(query, priority)) { |
| 2461 | Slog.i(TAG, "Result set changed, dropping preferred activity for " |
| 2462 | + intent + " type " + resolvedType); |
| 2463 | mSettings.mPreferredActivities.removeFilter(pa); |
| 2464 | return null; |
| 2465 | } |
| 2466 | |
| 2467 | // Yay! |
| 2468 | return ri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2469 | } |
| 2470 | } |
| 2471 | } |
| 2472 | } |
| 2473 | return null; |
| 2474 | } |
| 2475 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2476 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2477 | public List<ResolveInfo> queryIntentActivities(Intent intent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2478 | String resolvedType, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2479 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2480 | ComponentName comp = intent.getComponent(); |
| 2481 | if (comp == null) { |
| 2482 | if (intent.getSelector() != null) { |
| 2483 | intent = intent.getSelector(); |
| 2484 | comp = intent.getComponent(); |
| 2485 | } |
| 2486 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2488 | if (comp != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2489 | final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2490 | final ActivityInfo ai = getActivityInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2491 | if (ai != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2492 | final ResolveInfo ri = new ResolveInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2493 | ri.activityInfo = ai; |
| 2494 | list.add(ri); |
| 2495 | } |
| 2496 | return list; |
| 2497 | } |
| 2498 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2499 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2500 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2501 | final String pkgName = intent.getPackage(); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2502 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2503 | return mActivities.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2504 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2505 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2506 | if (pkg != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2507 | return mActivities.queryIntentForPackage(intent, resolvedType, flags, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2508 | pkg.activities, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2509 | } |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 2510 | return new ArrayList<ResolveInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2511 | } |
| 2512 | } |
| 2513 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2514 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2515 | public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller, |
| 2516 | Intent[] specifics, String[] specificTypes, Intent intent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2517 | String resolvedType, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2518 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2519 | final String resultsAction = intent.getAction(); |
| 2520 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2521 | List<ResolveInfo> results = queryIntentActivities(intent, resolvedType, flags |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2522 | | PackageManager.GET_RESOLVED_FILTER, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2523 | |
| 2524 | if (DEBUG_INTENT_MATCHING) { |
| 2525 | Log.v(TAG, "Query " + intent + ": " + results); |
| 2526 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2527 | |
| 2528 | int specificsPos = 0; |
| 2529 | int N; |
| 2530 | |
| 2531 | // todo: note that the algorithm used here is O(N^2). This |
| 2532 | // isn't a problem in our current environment, but if we start running |
| 2533 | // into situations where we have more than 5 or 10 matches then this |
| 2534 | // should probably be changed to something smarter... |
| 2535 | |
| 2536 | // First we go through and resolve each of the specific items |
| 2537 | // that were supplied, taking care of removing any corresponding |
| 2538 | // duplicate items in the generic resolve list. |
| 2539 | if (specifics != null) { |
| 2540 | for (int i=0; i<specifics.length; i++) { |
| 2541 | final Intent sintent = specifics[i]; |
| 2542 | if (sintent == null) { |
| 2543 | continue; |
| 2544 | } |
| 2545 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2546 | if (DEBUG_INTENT_MATCHING) { |
| 2547 | Log.v(TAG, "Specific #" + i + ": " + sintent); |
| 2548 | } |
| 2549 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2550 | String action = sintent.getAction(); |
| 2551 | if (resultsAction != null && resultsAction.equals(action)) { |
| 2552 | // If this action was explicitly requested, then don't |
| 2553 | // remove things that have it. |
| 2554 | action = null; |
| 2555 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2556 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2557 | ResolveInfo ri = null; |
| 2558 | ActivityInfo ai = null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2559 | |
| 2560 | ComponentName comp = sintent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2561 | if (comp == null) { |
| 2562 | ri = resolveIntent( |
| 2563 | sintent, |
| 2564 | specificTypes != null ? specificTypes[i] : null, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2565 | flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2566 | if (ri == null) { |
| 2567 | continue; |
| 2568 | } |
| 2569 | if (ri == mResolveInfo) { |
| 2570 | // ACK! Must do something better with this. |
| 2571 | } |
| 2572 | ai = ri.activityInfo; |
| 2573 | comp = new ComponentName(ai.applicationInfo.packageName, |
| 2574 | ai.name); |
| 2575 | } else { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2576 | ai = getActivityInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2577 | if (ai == null) { |
| 2578 | continue; |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | // Look for any generic query activities that are duplicates |
| 2583 | // of this specific one, and remove them from the results. |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2584 | 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] | 2585 | N = results.size(); |
| 2586 | int j; |
| 2587 | for (j=specificsPos; j<N; j++) { |
| 2588 | ResolveInfo sri = results.get(j); |
| 2589 | if ((sri.activityInfo.name.equals(comp.getClassName()) |
| 2590 | && sri.activityInfo.applicationInfo.packageName.equals( |
| 2591 | comp.getPackageName())) |
| 2592 | || (action != null && sri.filter.matchAction(action))) { |
| 2593 | results.remove(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2594 | if (DEBUG_INTENT_MATCHING) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2595 | TAG, "Removing duplicate item from " + j |
| 2596 | + " due to specific " + specificsPos); |
| 2597 | if (ri == null) { |
| 2598 | ri = sri; |
| 2599 | } |
| 2600 | j--; |
| 2601 | N--; |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | // Add this specific item to its proper place. |
| 2606 | if (ri == null) { |
| 2607 | ri = new ResolveInfo(); |
| 2608 | ri.activityInfo = ai; |
| 2609 | } |
| 2610 | results.add(specificsPos, ri); |
| 2611 | ri.specificIndex = i; |
| 2612 | specificsPos++; |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | // Now we go through the remaining generic results and remove any |
| 2617 | // duplicate actions that are found here. |
| 2618 | N = results.size(); |
| 2619 | for (int i=specificsPos; i<N-1; i++) { |
| 2620 | final ResolveInfo rii = results.get(i); |
| 2621 | if (rii.filter == null) { |
| 2622 | continue; |
| 2623 | } |
| 2624 | |
| 2625 | // Iterate over all of the actions of this result's intent |
| 2626 | // filter... typically this should be just one. |
| 2627 | final Iterator<String> it = rii.filter.actionsIterator(); |
| 2628 | if (it == null) { |
| 2629 | continue; |
| 2630 | } |
| 2631 | while (it.hasNext()) { |
| 2632 | final String action = it.next(); |
| 2633 | if (resultsAction != null && resultsAction.equals(action)) { |
| 2634 | // If this action was explicitly requested, then don't |
| 2635 | // remove things that have it. |
| 2636 | continue; |
| 2637 | } |
| 2638 | for (int j=i+1; j<N; j++) { |
| 2639 | final ResolveInfo rij = results.get(j); |
| 2640 | if (rij.filter != null && rij.filter.hasAction(action)) { |
| 2641 | results.remove(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2642 | if (DEBUG_INTENT_MATCHING) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2643 | TAG, "Removing duplicate item from " + j |
| 2644 | + " due to action " + action + " at " + i); |
| 2645 | j--; |
| 2646 | N--; |
| 2647 | } |
| 2648 | } |
| 2649 | } |
| 2650 | |
| 2651 | // If the caller didn't request filter information, drop it now |
| 2652 | // so we don't have to marshall/unmarshall it. |
| 2653 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2654 | rii.filter = null; |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | // Filter out the caller activity if so requested. |
| 2659 | if (caller != null) { |
| 2660 | N = results.size(); |
| 2661 | for (int i=0; i<N; i++) { |
| 2662 | ActivityInfo ainfo = results.get(i).activityInfo; |
| 2663 | if (caller.getPackageName().equals(ainfo.applicationInfo.packageName) |
| 2664 | && caller.getClassName().equals(ainfo.name)) { |
| 2665 | results.remove(i); |
| 2666 | break; |
| 2667 | } |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | // If the caller didn't request filter information, |
| 2672 | // drop them now so we don't have to |
| 2673 | // marshall/unmarshall it. |
| 2674 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2675 | N = results.size(); |
| 2676 | for (int i=0; i<N; i++) { |
| 2677 | results.get(i).filter = null; |
| 2678 | } |
| 2679 | } |
| 2680 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2681 | if (DEBUG_INTENT_MATCHING) Log.v(TAG, "Result: " + results); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2682 | return results; |
| 2683 | } |
| 2684 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2685 | @Override |
| 2686 | public List<ResolveInfo> queryIntentReceivers(Intent intent, String resolvedType, int flags, |
| 2687 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2688 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2689 | ComponentName comp = intent.getComponent(); |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2690 | if (comp == null) { |
| 2691 | if (intent.getSelector() != null) { |
| 2692 | intent = intent.getSelector(); |
| 2693 | comp = intent.getComponent(); |
| 2694 | } |
| 2695 | } |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2696 | if (comp != null) { |
| 2697 | List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2698 | ActivityInfo ai = getReceiverInfo(comp, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2699 | if (ai != null) { |
| 2700 | ResolveInfo ri = new ResolveInfo(); |
| 2701 | ri.activityInfo = ai; |
| 2702 | list.add(ri); |
| 2703 | } |
| 2704 | return list; |
| 2705 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2706 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2707 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2708 | synchronized (mPackages) { |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2709 | String pkgName = intent.getPackage(); |
| 2710 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2711 | return mReceivers.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2712 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2713 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2714 | if (pkg != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2715 | return mReceivers.queryIntentForPackage(intent, resolvedType, flags, pkg.receivers, |
| 2716 | userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2717 | } |
| 2718 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2722 | @Override |
| 2723 | public ResolveInfo resolveService(Intent intent, String resolvedType, int flags, int userId) { |
| 2724 | List<ResolveInfo> query = queryIntentServices(intent, resolvedType, flags, userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2725 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2726 | if (query != null) { |
| 2727 | if (query.size() >= 1) { |
| 2728 | // If there is more than one service with the same priority, |
| 2729 | // just arbitrarily pick the first one. |
| 2730 | return query.get(0); |
| 2731 | } |
| 2732 | } |
| 2733 | return null; |
| 2734 | } |
| 2735 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2736 | @Override |
| 2737 | public List<ResolveInfo> queryIntentServices(Intent intent, String resolvedType, int flags, |
| 2738 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2739 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2740 | ComponentName comp = intent.getComponent(); |
| 2741 | if (comp == null) { |
| 2742 | if (intent.getSelector() != null) { |
| 2743 | intent = intent.getSelector(); |
| 2744 | comp = intent.getComponent(); |
| 2745 | } |
| 2746 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2747 | if (comp != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2748 | final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2749 | final ServiceInfo si = getServiceInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2750 | if (si != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2751 | final ResolveInfo ri = new ResolveInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2752 | ri.serviceInfo = si; |
| 2753 | list.add(ri); |
| 2754 | } |
| 2755 | return list; |
| 2756 | } |
| 2757 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2758 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2759 | synchronized (mPackages) { |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2760 | String pkgName = intent.getPackage(); |
| 2761 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2762 | return mServices.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2763 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2764 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2765 | if (pkg != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2766 | return mServices.queryIntentForPackage(intent, resolvedType, flags, pkg.services, |
| 2767 | userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2768 | } |
| 2769 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2770 | } |
| 2771 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2772 | |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2773 | private static final int getContinuationPoint(final String[] keys, final String key) { |
| 2774 | final int index; |
| 2775 | if (key == null) { |
| 2776 | index = 0; |
| 2777 | } else { |
| 2778 | final int insertPoint = Arrays.binarySearch(keys, key); |
| 2779 | if (insertPoint < 0) { |
| 2780 | index = -insertPoint; |
| 2781 | } else { |
| 2782 | index = insertPoint + 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2783 | } |
| 2784 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2785 | return index; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2786 | } |
| 2787 | |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2788 | public ParceledListSlice<PackageInfo> getInstalledPackages(int flags, String lastRead) { |
| 2789 | final ParceledListSlice<PackageInfo> list = new ParceledListSlice<PackageInfo>(); |
| 2790 | final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0; |
| 2791 | final String[] keys; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2792 | int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2793 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2794 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2795 | synchronized (mPackages) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2796 | if (listUninstalled) { |
| 2797 | keys = mSettings.mPackages.keySet().toArray(new String[mSettings.mPackages.size()]); |
| 2798 | } else { |
| 2799 | keys = mPackages.keySet().toArray(new String[mPackages.size()]); |
| 2800 | } |
| 2801 | |
| 2802 | Arrays.sort(keys); |
| 2803 | int i = getContinuationPoint(keys, lastRead); |
| 2804 | final int N = keys.length; |
| 2805 | |
| 2806 | while (i < N) { |
| 2807 | final String packageName = keys[i++]; |
| 2808 | |
| 2809 | PackageInfo pi = null; |
| 2810 | if (listUninstalled) { |
| 2811 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
| 2812 | if (ps != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2813 | pi = generatePackageInfoFromSettingsLPw(ps.name, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2814 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2815 | } else { |
| 2816 | final PackageParser.Package p = mPackages.get(packageName); |
| 2817 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2818 | pi = generatePackageInfo(p, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2819 | } |
| 2820 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2821 | |
Henrik Engström | 9771a31 | 2012-02-21 09:05:17 +0100 | [diff] [blame] | 2822 | if (pi != null && list.append(pi)) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2823 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2824 | } |
| 2825 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2826 | |
| 2827 | if (i == N) { |
| 2828 | list.setLastSlice(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2829 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2830 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2831 | |
| 2832 | return list; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2833 | } |
| 2834 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2835 | @Override |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2836 | public ParceledListSlice<ApplicationInfo> getInstalledApplications(int flags, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2837 | String lastRead, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2838 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2839 | final ParceledListSlice<ApplicationInfo> list = new ParceledListSlice<ApplicationInfo>(); |
| 2840 | final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0; |
| 2841 | final String[] keys; |
| 2842 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2843 | // writer |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2844 | synchronized (mPackages) { |
| 2845 | if (listUninstalled) { |
| 2846 | keys = mSettings.mPackages.keySet().toArray(new String[mSettings.mPackages.size()]); |
| 2847 | } else { |
| 2848 | keys = mPackages.keySet().toArray(new String[mPackages.size()]); |
| 2849 | } |
| 2850 | |
| 2851 | Arrays.sort(keys); |
| 2852 | int i = getContinuationPoint(keys, lastRead); |
| 2853 | final int N = keys.length; |
| 2854 | |
| 2855 | while (i < N) { |
| 2856 | final String packageName = keys[i++]; |
| 2857 | |
| 2858 | ApplicationInfo ai = null; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2859 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2860 | if (listUninstalled) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2861 | if (ps != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2862 | ai = generateApplicationInfoFromSettingsLPw(ps.name, flags, userId); |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2863 | } |
| 2864 | } else { |
| 2865 | final PackageParser.Package p = mPackages.get(packageName); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2866 | if (p != null && ps != null) { |
| 2867 | ai = PackageParser.generateApplicationInfo(p, flags, ps.getStopped(userId), |
| 2868 | ps.getEnabled(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2869 | } |
| 2870 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2871 | |
Henrik Engström | 9771a31 | 2012-02-21 09:05:17 +0100 | [diff] [blame] | 2872 | if (ai != null && list.append(ai)) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2873 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2874 | } |
| 2875 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2876 | |
| 2877 | if (i == N) { |
| 2878 | list.setLastSlice(true); |
| 2879 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2880 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 2881 | |
| 2882 | return list; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | public List<ApplicationInfo> getPersistentApplications(int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2886 | final ArrayList<ApplicationInfo> finalList = new ArrayList<ApplicationInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2887 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2888 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2889 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2890 | final Iterator<PackageParser.Package> i = mPackages.values().iterator(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2891 | final int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2892 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2893 | final PackageParser.Package p = i.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2894 | if (p.applicationInfo != null |
| 2895 | && (p.applicationInfo.flags&ApplicationInfo.FLAG_PERSISTENT) != 0 |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 2896 | && (!mSafeMode || isSystemApp(p))) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2897 | PackageSetting ps = mSettings.mPackages.get(p.packageName); |
| 2898 | finalList.add(PackageParser.generateApplicationInfo(p, flags, |
| 2899 | ps != null ? ps.getStopped(userId) : false, |
| 2900 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2901 | userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2902 | } |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | return finalList; |
| 2907 | } |
| 2908 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2909 | @Override |
| 2910 | public ProviderInfo resolveContentProvider(String name, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2911 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2912 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2913 | synchronized (mPackages) { |
| 2914 | final PackageParser.Provider provider = mProviders.get(name); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2915 | PackageSetting ps = provider != null |
| 2916 | ? mSettings.mPackages.get(provider.owner.packageName) |
| 2917 | : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2918 | return provider != null |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2919 | && mSettings.isEnabledLPr(provider.info, flags, userId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2920 | && (!mSafeMode || (provider.info.applicationInfo.flags |
| 2921 | &ApplicationInfo.FLAG_SYSTEM) != 0) |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2922 | ? PackageParser.generateProviderInfo(provider, flags, |
| 2923 | ps != null ? ps.getStopped(userId) : false, |
| 2924 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2925 | userId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2926 | : null; |
| 2927 | } |
| 2928 | } |
| 2929 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2930 | /** |
| 2931 | * @deprecated |
| 2932 | */ |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 2933 | @Deprecated |
| 2934 | public void querySyncProviders(List<String> outNames, List<ProviderInfo> outInfo) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2935 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2936 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2937 | final Iterator<Map.Entry<String, PackageParser.Provider>> i = mProviders.entrySet() |
| 2938 | .iterator(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2939 | final int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2940 | while (i.hasNext()) { |
| 2941 | Map.Entry<String, PackageParser.Provider> entry = i.next(); |
| 2942 | PackageParser.Provider p = entry.getValue(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2943 | PackageSetting ps = mSettings.mPackages.get(p.owner.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2944 | |
| 2945 | if (p.syncable |
| 2946 | && (!mSafeMode || (p.info.applicationInfo.flags |
| 2947 | &ApplicationInfo.FLAG_SYSTEM) != 0)) { |
| 2948 | outNames.add(entry.getKey()); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2949 | outInfo.add(PackageParser.generateProviderInfo(p, 0, |
| 2950 | ps != null ? ps.getStopped(userId) : false, |
| 2951 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2952 | userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2953 | } |
| 2954 | } |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | public List<ProviderInfo> queryContentProviders(String processName, |
| 2959 | int uid, int flags) { |
| 2960 | ArrayList<ProviderInfo> finalList = null; |
| 2961 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2962 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2963 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2964 | final Iterator<PackageParser.Provider> i = mProvidersByComponent.values().iterator(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2965 | final int userId = processName != null ? |
| 2966 | UserId.getUserId(uid) : UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2967 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2968 | final PackageParser.Provider p = i.next(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2969 | PackageSetting ps = mSettings.mPackages.get(p.owner.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2970 | if (p.info.authority != null |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2971 | && (processName == null |
| 2972 | || (p.info.processName.equals(processName) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2973 | && UserId.isSameApp(p.info.applicationInfo.uid, uid))) |
| 2974 | && mSettings.isEnabledLPr(p.info, flags, userId) |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2975 | && (!mSafeMode |
| 2976 | || (p.info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2977 | if (finalList == null) { |
| 2978 | finalList = new ArrayList<ProviderInfo>(3); |
| 2979 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2980 | finalList.add(PackageParser.generateProviderInfo(p, flags, |
| 2981 | ps != null ? ps.getStopped(userId) : false, |
| 2982 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 2983 | userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2984 | } |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | if (finalList != null) { |
| 2989 | Collections.sort(finalList, mProviderInitOrderSorter); |
| 2990 | } |
| 2991 | |
| 2992 | return finalList; |
| 2993 | } |
| 2994 | |
| 2995 | public InstrumentationInfo getInstrumentationInfo(ComponentName name, |
| 2996 | int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2997 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2998 | synchronized (mPackages) { |
| 2999 | final PackageParser.Instrumentation i = mInstrumentation.get(name); |
| 3000 | return PackageParser.generateInstrumentationInfo(i, flags); |
| 3001 | } |
| 3002 | } |
| 3003 | |
| 3004 | public List<InstrumentationInfo> queryInstrumentation(String targetPackage, |
| 3005 | int flags) { |
| 3006 | ArrayList<InstrumentationInfo> finalList = |
| 3007 | new ArrayList<InstrumentationInfo>(); |
| 3008 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3009 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3010 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3011 | final Iterator<PackageParser.Instrumentation> i = mInstrumentation.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3012 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3013 | final PackageParser.Instrumentation p = i.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3014 | if (targetPackage == null |
| 3015 | || targetPackage.equals(p.info.targetPackage)) { |
| 3016 | finalList.add(PackageParser.generateInstrumentationInfo(p, |
| 3017 | flags)); |
| 3018 | } |
| 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | return finalList; |
| 3023 | } |
| 3024 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3025 | 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] | 3026 | String[] files = dir.list(); |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 3027 | if (files == null) { |
| 3028 | Log.d(TAG, "No files in app dir " + dir); |
| 3029 | return; |
| 3030 | } |
| 3031 | |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3032 | if (DEBUG_PACKAGE_SCANNING) { |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 3033 | Log.d(TAG, "Scanning app dir " + dir); |
| 3034 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3035 | |
| 3036 | int i; |
| 3037 | for (i=0; i<files.length; i++) { |
| 3038 | File file = new File(dir, files[i]); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 3039 | if (!isPackageFilename(files[i])) { |
| 3040 | // Ignore entries which are not apk's |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 3041 | continue; |
| 3042 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3043 | PackageParser.Package pkg = scanPackageLI(file, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3044 | flags|PackageParser.PARSE_MUST_BE_APK, scanMode, currentTime); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3045 | // Don't mess around with apps in system partition. |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 3046 | if (pkg == null && (flags & PackageParser.PARSE_IS_SYSTEM) == 0 && |
| 3047 | mLastScanError == PackageManager.INSTALL_FAILED_INVALID_APK) { |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3048 | // Delete the apk |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3049 | Slog.w(TAG, "Cleaning up failed install of " + file); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3050 | file.delete(); |
| 3051 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3052 | } |
| 3053 | } |
| 3054 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3055 | private static File getSettingsProblemFile() { |
| 3056 | File dataDir = Environment.getDataDirectory(); |
| 3057 | File systemDir = new File(dataDir, "system"); |
| 3058 | File fname = new File(systemDir, "uiderrors.txt"); |
| 3059 | return fname; |
| 3060 | } |
| 3061 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 3062 | static void reportSettingsProblem(int priority, String msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3063 | try { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3064 | File fname = getSettingsProblemFile(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3065 | FileOutputStream out = new FileOutputStream(fname, true); |
| 3066 | PrintWriter pw = new PrintWriter(out); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3067 | SimpleDateFormat formatter = new SimpleDateFormat(); |
| 3068 | String dateString = formatter.format(new Date(System.currentTimeMillis())); |
| 3069 | pw.println(dateString + ": " + msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3070 | pw.close(); |
| 3071 | FileUtils.setPermissions( |
| 3072 | fname.toString(), |
| 3073 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IROTH, |
| 3074 | -1, -1); |
| 3075 | } catch (java.io.IOException e) { |
| 3076 | } |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3077 | Slog.println(priority, TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | private boolean collectCertificatesLI(PackageParser pp, PackageSetting ps, |
| 3081 | PackageParser.Package pkg, File srcFile, int parseFlags) { |
| 3082 | if (GET_CERTIFICATES) { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3083 | if (ps != null |
| 3084 | && ps.codePath.equals(srcFile) |
Kenny Root | 7d794fb | 2010-09-13 16:29:49 -0700 | [diff] [blame] | 3085 | && ps.timeStamp == srcFile.lastModified()) { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3086 | if (ps.signatures.mSignatures != null |
| 3087 | && ps.signatures.mSignatures.length != 0) { |
| 3088 | // Optimization: reuse the existing cached certificates |
| 3089 | // if the package appears to be unchanged. |
| 3090 | pkg.mSignatures = ps.signatures.mSignatures; |
| 3091 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3092 | } |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3093 | |
| 3094 | 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] | 3095 | } else { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3096 | Log.i(TAG, srcFile.toString() + " changed; collecting certs"); |
| 3097 | } |
| 3098 | |
| 3099 | if (!pp.collectCertificates(pkg, parseFlags)) { |
| 3100 | mLastScanError = pp.getParseError(); |
| 3101 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3102 | } |
| 3103 | } |
| 3104 | return true; |
| 3105 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3107 | /* |
| 3108 | * Scan a package and return the newly parsed package. |
| 3109 | * Returns null in case of errors and the error code is stored in mLastScanError |
| 3110 | */ |
| 3111 | private PackageParser.Package scanPackageLI(File scanFile, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3112 | int parseFlags, int scanMode, long currentTime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3113 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3114 | String scanPath = scanFile.getPath(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3115 | parseFlags |= mDefParseFlags; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3116 | PackageParser pp = new PackageParser(scanPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3117 | pp.setSeparateProcesses(mSeparateProcesses); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 3118 | pp.setOnlyCoreApps(mOnlyCore); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3119 | final PackageParser.Package pkg = pp.parsePackage(scanFile, |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3120 | scanPath, mMetrics, parseFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3121 | if (pkg == null) { |
| 3122 | mLastScanError = pp.getParseError(); |
| 3123 | return null; |
| 3124 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3125 | PackageSetting ps = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3126 | PackageSetting updatedPkg; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3127 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3128 | synchronized (mPackages) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3129 | // Look to see if we already know about this package. |
| 3130 | String oldName = mSettings.mRenamedPackages.get(pkg.packageName); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3131 | if (pkg.mOriginalPackages != null && pkg.mOriginalPackages.contains(oldName)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3132 | // This package has been renamed to its original name. Let's |
| 3133 | // use that. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3134 | ps = mSettings.peekPackageLPr(oldName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3135 | } |
| 3136 | // If there was no original package, see one for the real package name. |
| 3137 | if (ps == null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3138 | ps = mSettings.peekPackageLPr(pkg.packageName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3139 | } |
| 3140 | // Check to see if this package could be hiding/updating a system |
| 3141 | // package. Must look for it either under the original or real |
| 3142 | // package name depending on our state. |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 3143 | updatedPkg = mSettings.getDisabledSystemPkgLPr(ps != null ? ps.name : pkg.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3144 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3145 | // First check if this is a system package that may involve an update |
| 3146 | if (updatedPkg != null && (parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
Kenny Root | 9ee9274 | 2010-09-01 13:40:57 -0700 | [diff] [blame] | 3147 | if (ps != null && !ps.codePath.equals(scanFile)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3148 | // The path has changed from what was last scanned... check the |
| 3149 | // version of the new path against what we have stored to determine |
| 3150 | // what to do. |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3151 | if (pkg.mVersionCode < ps.versionCode) { |
| 3152 | // 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] | 3153 | // Ignore entry. Skip it. |
| 3154 | Log.i(TAG, "Package " + ps.name + " at " + scanFile |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 3155 | + " ignored: updated version " + ps.versionCode |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3156 | + " better than this " + pkg.mVersionCode); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3157 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3158 | return null; |
| 3159 | } else { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3160 | // The current app on the system partion is better than |
| 3161 | // what we have updated to on the data partition; switch |
| 3162 | // back to the system partition version. |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3163 | // At this point, its safely assumed that package installation for |
| 3164 | // apps in system partition will go through. If not there won't be a working |
| 3165 | // version of the app |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3166 | // writer |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3167 | synchronized (mPackages) { |
| 3168 | // Just remove the loaded entries from package lists. |
| 3169 | mPackages.remove(ps.name); |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 3170 | } |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3171 | Slog.w(TAG, "Package " + ps.name + " at " + scanFile |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3172 | + "reverting from " + ps.codePathString |
| 3173 | + ": new version " + pkg.mVersionCode |
| 3174 | + " better than installed " + ps.versionCode); |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 3175 | |
| 3176 | InstallArgs args = createInstallArgs(packageFlagsToInstallFlags(ps), |
| 3177 | ps.codePathString, ps.resourcePathString, ps.nativeLibraryPathString); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3178 | synchronized (mInstaller) { |
| 3179 | args.cleanUpResourcesLI(); |
| 3180 | } |
| 3181 | synchronized (mPackages) { |
| 3182 | mSettings.enableSystemPackageLPw(ps.name); |
| 3183 | } |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 3184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3185 | } |
| 3186 | } |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3187 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3188 | if (updatedPkg != null) { |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3189 | // An updated system app will not have the PARSE_IS_SYSTEM flag set |
| 3190 | // initially |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3191 | parseFlags |= PackageParser.PARSE_IS_SYSTEM; |
| 3192 | } |
| 3193 | // Verify certificates against what was last scanned |
| 3194 | if (!collectCertificatesLI(pp, ps, pkg, scanFile, parseFlags)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3195 | Slog.w(TAG, "Failed verifying certificates for package:" + pkg.packageName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3196 | return null; |
| 3197 | } |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3198 | |
| 3199 | /* |
| 3200 | * A new system app appeared, but we already had a non-system one of the |
| 3201 | * same name installed earlier. |
| 3202 | */ |
| 3203 | boolean shouldHideSystemApp = false; |
| 3204 | if (updatedPkg == null && ps != null |
| 3205 | && (parseFlags & PackageParser.PARSE_IS_SYSTEM_DIR) != 0 && !isSystemApp(ps)) { |
| 3206 | /* |
| 3207 | * Check to make sure the signatures match first. If they don't, |
| 3208 | * wipe the installed application and its data. |
| 3209 | */ |
| 3210 | if (compareSignatures(ps.signatures.mSignatures, pkg.mSignatures) |
| 3211 | != PackageManager.SIGNATURE_MATCH) { |
| 3212 | deletePackageLI(pkg.packageName, true, 0, null, false); |
| 3213 | ps = null; |
| 3214 | } else { |
| 3215 | /* |
| 3216 | * If the newly-added system app is an older version than the |
| 3217 | * already installed version, hide it. It will be scanned later |
| 3218 | * and re-added like an update. |
| 3219 | */ |
| 3220 | if (pkg.mVersionCode < ps.versionCode) { |
| 3221 | shouldHideSystemApp = true; |
| 3222 | } else { |
| 3223 | /* |
| 3224 | * The newly found system app is a newer version that the |
| 3225 | * one previously installed. Simply remove the |
| 3226 | * already-installed application and replace it with our own |
| 3227 | * while keeping the application data. |
| 3228 | */ |
| 3229 | Slog.w(TAG, "Package " + ps.name + " at " + scanFile + "reverting from " |
| 3230 | + ps.codePathString + ": new version " + pkg.mVersionCode |
| 3231 | + " better than installed " + ps.versionCode); |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 3232 | InstallArgs args = createInstallArgs(packageFlagsToInstallFlags(ps), |
| 3233 | ps.codePathString, ps.resourcePathString, ps.nativeLibraryPathString); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3234 | synchronized (mInstaller) { |
| 3235 | args.cleanUpResourcesLI(); |
| 3236 | } |
| 3237 | } |
| 3238 | } |
| 3239 | } |
| 3240 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3241 | // The apk is forward locked (not public) if its code and resources |
| 3242 | // are kept in different files. |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3243 | // TODO grab this value from PackageSettings |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3244 | if (ps != null && !ps.codePath.equals(ps.resourcePath)) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3245 | parseFlags |= PackageParser.PARSE_FORWARD_LOCK; |
Suchi Amalapurapu | f2c1072 | 2009-07-29 17:19:39 -0700 | [diff] [blame] | 3246 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3247 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3248 | String codePath = null; |
| 3249 | String resPath = null; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3250 | if ((parseFlags & PackageParser.PARSE_FORWARD_LOCK) != 0) { |
| 3251 | if (ps != null && ps.resourcePathString != null) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3252 | resPath = ps.resourcePathString; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3253 | } else { |
| 3254 | // Should not happen at all. Just log an error. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3255 | Slog.e(TAG, "Resource path not set for pkg : " + pkg.packageName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3256 | } |
| 3257 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3258 | resPath = pkg.mScanPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3259 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3260 | codePath = pkg.mScanPath; |
| 3261 | // Set application objects path explicitly. |
| 3262 | setApplicationInfoPaths(pkg, codePath, resPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3263 | // 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] | 3264 | PackageParser.Package scannedPkg = scanPackageLI(pkg, parseFlags, scanMode |
| 3265 | | SCAN_UPDATE_SIGNATURE, currentTime); |
| 3266 | |
| 3267 | /* |
| 3268 | * If the system app should be overridden by a previously installed |
| 3269 | * data, hide the system app now and let the /data/app scan pick it up |
| 3270 | * again. |
| 3271 | */ |
| 3272 | if (shouldHideSystemApp) { |
| 3273 | synchronized (mPackages) { |
| 3274 | /* |
| 3275 | * We have to grant systems permissions before we hide, because |
| 3276 | * grantPermissions will assume the package update is trying to |
| 3277 | * expand its permissions. |
| 3278 | */ |
| 3279 | grantPermissionsLPw(pkg, true); |
| 3280 | mSettings.disableSystemPackageLPw(pkg.packageName); |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | return scannedPkg; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3285 | } |
| 3286 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3287 | private static void setApplicationInfoPaths(PackageParser.Package pkg, String destCodePath, |
| 3288 | String destResPath) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3289 | pkg.mPath = pkg.mScanPath = destCodePath; |
| 3290 | pkg.applicationInfo.sourceDir = destCodePath; |
| 3291 | pkg.applicationInfo.publicSourceDir = destResPath; |
| 3292 | } |
| 3293 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3294 | private static String fixProcessName(String defProcessName, |
| 3295 | String processName, int uid) { |
| 3296 | if (processName == null) { |
| 3297 | return defProcessName; |
| 3298 | } |
| 3299 | return processName; |
| 3300 | } |
| 3301 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3302 | private boolean verifySignaturesLP(PackageSetting pkgSetting, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3303 | PackageParser.Package pkg) { |
| 3304 | if (pkgSetting.signatures.mSignatures != null) { |
| 3305 | // Already existing package. Make sure signatures match |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3306 | if (compareSignatures(pkgSetting.signatures.mSignatures, pkg.mSignatures) != |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3307 | PackageManager.SIGNATURE_MATCH) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3308 | Slog.e(TAG, "Package " + pkg.packageName |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3309 | + " signatures do not match the previously installed version; ignoring!"); |
| 3310 | mLastScanError = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3311 | return false; |
| 3312 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3313 | } |
| 3314 | // Check for shared user signatures |
| 3315 | if (pkgSetting.sharedUser != null && pkgSetting.sharedUser.signatures.mSignatures != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3316 | if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3317 | pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) { |
| 3318 | Slog.e(TAG, "Package " + pkg.packageName |
| 3319 | + " has no signatures that match those in shared user " |
| 3320 | + pkgSetting.sharedUser.name + "; ignoring!"); |
| 3321 | mLastScanError = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; |
| 3322 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3323 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3324 | } |
| 3325 | return true; |
| 3326 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3327 | |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 3328 | /** |
| 3329 | * Enforces that only the system UID or root's UID can call a method exposed |
| 3330 | * via Binder. |
| 3331 | * |
| 3332 | * @param message used as message if SecurityException is thrown |
| 3333 | * @throws SecurityException if the caller is not system or root |
| 3334 | */ |
| 3335 | private static final void enforceSystemOrRoot(String message) { |
| 3336 | final int uid = Binder.getCallingUid(); |
| 3337 | if (uid != Process.SYSTEM_UID && uid != 0) { |
| 3338 | throw new SecurityException(message); |
| 3339 | } |
| 3340 | } |
| 3341 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3342 | public void performBootDexOpt() { |
| 3343 | ArrayList<PackageParser.Package> pkgs = null; |
| 3344 | synchronized (mPackages) { |
| 3345 | if (mDeferredDexOpt.size() > 0) { |
| 3346 | pkgs = new ArrayList<PackageParser.Package>(mDeferredDexOpt); |
| 3347 | mDeferredDexOpt.clear(); |
| 3348 | } |
| 3349 | } |
| 3350 | if (pkgs != null) { |
| 3351 | for (int i=0; i<pkgs.size(); i++) { |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 3352 | if (!isFirstBoot()) { |
| 3353 | try { |
| 3354 | ActivityManagerNative.getDefault().showBootMessage( |
| 3355 | mContext.getResources().getString( |
| 3356 | com.android.internal.R.string.android_upgrading_apk, |
| 3357 | i+1, pkgs.size()), true); |
| 3358 | } catch (RemoteException e) { |
| 3359 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3360 | } |
| 3361 | PackageParser.Package p = pkgs.get(i); |
| 3362 | synchronized (mInstallLock) { |
| 3363 | if (!p.mDidDexOpt) { |
| 3364 | performDexOptLI(p, false, false); |
| 3365 | } |
| 3366 | } |
| 3367 | } |
| 3368 | } |
| 3369 | } |
| 3370 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3371 | public boolean performDexOpt(String packageName) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 3372 | enforceSystemOrRoot("Only the system can request dexopt be performed"); |
| 3373 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3374 | if (!mNoDexOpt) { |
| 3375 | return false; |
| 3376 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3377 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3378 | PackageParser.Package p; |
| 3379 | synchronized (mPackages) { |
| 3380 | p = mPackages.get(packageName); |
| 3381 | if (p == null || p.mDidDexOpt) { |
| 3382 | return false; |
| 3383 | } |
| 3384 | } |
| 3385 | synchronized (mInstallLock) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3386 | return performDexOptLI(p, false, false) == DEX_OPT_PERFORMED; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3387 | } |
| 3388 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3389 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3390 | static final int DEX_OPT_SKIPPED = 0; |
| 3391 | static final int DEX_OPT_PERFORMED = 1; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3392 | static final int DEX_OPT_DEFERRED = 2; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3393 | static final int DEX_OPT_FAILED = -1; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3394 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3395 | private int performDexOptLI(PackageParser.Package pkg, boolean forceDex, boolean defer) { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3396 | boolean performed = false; |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3397 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3398 | String path = pkg.mScanPath; |
| 3399 | int ret = 0; |
| 3400 | try { |
| 3401 | if (forceDex || dalvik.system.DexFile.isDexOptNeeded(path)) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3402 | if (!forceDex && defer) { |
| 3403 | mDeferredDexOpt.add(pkg); |
| 3404 | return DEX_OPT_DEFERRED; |
| 3405 | } else { |
| 3406 | Log.i(TAG, "Running dexopt on: " + pkg.applicationInfo.packageName); |
| 3407 | ret = mInstaller.dexopt(path, pkg.applicationInfo.uid, |
| 3408 | !isForwardLocked(pkg)); |
| 3409 | pkg.mDidDexOpt = true; |
| 3410 | performed = true; |
| 3411 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3412 | } |
| 3413 | } catch (FileNotFoundException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3414 | Slog.w(TAG, "Apk not found for dexopt: " + path); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3415 | ret = -1; |
| 3416 | } catch (IOException e) { |
Suchi Amalapurapu | 73dafa1 | 2010-04-01 16:31:31 -0700 | [diff] [blame] | 3417 | Slog.w(TAG, "IOException reading apk: " + path, e); |
| 3418 | ret = -1; |
| 3419 | } catch (dalvik.system.StaleDexCacheError e) { |
| 3420 | Slog.w(TAG, "StaleDexCacheError when reading apk: " + path, e); |
| 3421 | ret = -1; |
| 3422 | } catch (Exception e) { |
| 3423 | Slog.w(TAG, "Exception when doing dexopt : ", e); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3424 | ret = -1; |
| 3425 | } |
| 3426 | if (ret < 0) { |
| 3427 | //error from installer |
| 3428 | return DEX_OPT_FAILED; |
| 3429 | } |
| 3430 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3431 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3432 | return performed ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 3433 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 3434 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3435 | private boolean verifyPackageUpdateLPr(PackageSetting oldPkg, PackageParser.Package newPkg) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3436 | if ((oldPkg.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3437 | Slog.w(TAG, "Unable to update from " + oldPkg.name |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3438 | + " to " + newPkg.packageName |
| 3439 | + ": old package not in system partition"); |
| 3440 | return false; |
| 3441 | } else if (mPackages.get(oldPkg.name) != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3442 | Slog.w(TAG, "Unable to update from " + oldPkg.name |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3443 | + " to " + newPkg.packageName |
| 3444 | + ": old package still exists"); |
| 3445 | return false; |
| 3446 | } |
| 3447 | return true; |
| 3448 | } |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 3449 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3450 | File getDataPathForUser(int userId) { |
| 3451 | return new File(mUserAppDataDir.getAbsolutePath() + File.separator + userId); |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 3452 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3453 | |
| 3454 | private File getDataPathForPackage(String packageName, int userId) { |
Kenny Root | 9c8625e | 2011-07-25 09:42:22 -0700 | [diff] [blame] | 3455 | /* |
| 3456 | * Until we fully support multiple users, return the directory we |
| 3457 | * previously would have. The PackageManagerTests will need to be |
| 3458 | * revised when this is changed back.. |
| 3459 | */ |
| 3460 | if (userId == 0) { |
| 3461 | return new File(mAppDataDir, packageName); |
| 3462 | } else { |
| 3463 | return new File(mUserAppDataDir.getAbsolutePath() + File.separator + userId |
Amith Yamasani | 0f3f29b | 2011-05-09 14:17:14 -0700 | [diff] [blame] | 3464 | + File.separator + packageName); |
Kenny Root | 9c8625e | 2011-07-25 09:42:22 -0700 | [diff] [blame] | 3465 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3466 | } |
| 3467 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3468 | private PackageParser.Package scanPackageLI(PackageParser.Package pkg, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3469 | int parseFlags, int scanMode, long currentTime) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3470 | File scanFile = new File(pkg.mScanPath); |
Suchi Amalapurapu | 7040ce7 | 2010-02-08 23:55:56 -0800 | [diff] [blame] | 3471 | if (scanFile == null || pkg.applicationInfo.sourceDir == null || |
| 3472 | pkg.applicationInfo.publicSourceDir == null) { |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3473 | // Bail out. The resource and code paths haven't been set. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3474 | Slog.w(TAG, " Code and resource paths haven't been set correctly"); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3475 | mLastScanError = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 3476 | return null; |
| 3477 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3478 | mScanningPath = scanFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3480 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
| 3481 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 3482 | } |
| 3483 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3484 | if (pkg.packageName.equals("android")) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3485 | synchronized (mPackages) { |
| 3486 | if (mAndroidApplication != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3487 | Slog.w(TAG, "*************************************************"); |
| 3488 | Slog.w(TAG, "Core android package being redefined. Skipping."); |
| 3489 | Slog.w(TAG, " file=" + mScanningPath); |
| 3490 | Slog.w(TAG, "*************************************************"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3491 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3492 | return null; |
| 3493 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3494 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3495 | // Set up information for our fall-back user intent resolution |
| 3496 | // activity. |
| 3497 | mPlatformPackage = pkg; |
| 3498 | pkg.mVersionCode = mSdkVersion; |
| 3499 | mAndroidApplication = pkg.applicationInfo; |
| 3500 | mResolveActivity.applicationInfo = mAndroidApplication; |
| 3501 | mResolveActivity.name = ResolverActivity.class.getName(); |
| 3502 | mResolveActivity.packageName = mAndroidApplication.packageName; |
| 3503 | mResolveActivity.processName = mAndroidApplication.processName; |
| 3504 | mResolveActivity.launchMode = ActivityInfo.LAUNCH_MULTIPLE; |
| 3505 | mResolveActivity.flags = ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS; |
Joe Onorato | 646f46e | 2010-11-08 15:08:56 -0800 | [diff] [blame] | 3506 | 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] | 3507 | mResolveActivity.exported = true; |
| 3508 | mResolveActivity.enabled = true; |
| 3509 | mResolveInfo.activityInfo = mResolveActivity; |
| 3510 | mResolveInfo.priority = 0; |
| 3511 | mResolveInfo.preferredOrder = 0; |
| 3512 | mResolveInfo.match = 0; |
| 3513 | mResolveComponentName = new ComponentName( |
| 3514 | mAndroidApplication.packageName, mResolveActivity.name); |
| 3515 | } |
| 3516 | } |
| 3517 | |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3518 | if (DEBUG_PACKAGE_SCANNING) { |
| 3519 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3520 | Log.d(TAG, "Scanning package " + pkg.packageName); |
| 3521 | } |
| 3522 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3523 | if (mPackages.containsKey(pkg.packageName) |
| 3524 | || mSharedLibraries.containsKey(pkg.packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3525 | Slog.w(TAG, "Application package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3526 | + " already installed. Skipping duplicate."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3527 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3528 | return null; |
| 3529 | } |
| 3530 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3531 | // Initialize package source and resource directories |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3532 | File destCodeFile = new File(pkg.applicationInfo.sourceDir); |
| 3533 | File destResourceFile = new File(pkg.applicationInfo.publicSourceDir); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3535 | SharedUserSetting suid = null; |
| 3536 | PackageSetting pkgSetting = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3537 | |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 3538 | if (!isSystemApp(pkg)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3539 | // Only system apps can use these features. |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3540 | pkg.mOriginalPackages = null; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3541 | pkg.mRealPackage = null; |
| 3542 | pkg.mAdoptPermissions = null; |
| 3543 | } |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 3544 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3545 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3546 | synchronized (mPackages) { |
| 3547 | // Check all shared libraries and map to their actual file path. |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3548 | if (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null) { |
| 3549 | if (mTmpSharedLibraries == null || |
| 3550 | mTmpSharedLibraries.length < mSharedLibraries.size()) { |
| 3551 | mTmpSharedLibraries = new String[mSharedLibraries.size()]; |
| 3552 | } |
| 3553 | int num = 0; |
| 3554 | int N = pkg.usesLibraries != null ? pkg.usesLibraries.size() : 0; |
| 3555 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3556 | final String file = mSharedLibraries.get(pkg.usesLibraries.get(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3557 | if (file == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3558 | Slog.e(TAG, "Package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3559 | + " requires unavailable shared library " |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3560 | + pkg.usesLibraries.get(i) + "; failing!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3561 | mLastScanError = PackageManager.INSTALL_FAILED_MISSING_SHARED_LIBRARY; |
| 3562 | return null; |
| 3563 | } |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3564 | mTmpSharedLibraries[num] = file; |
| 3565 | num++; |
| 3566 | } |
| 3567 | N = pkg.usesOptionalLibraries != null ? pkg.usesOptionalLibraries.size() : 0; |
| 3568 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3569 | final String file = mSharedLibraries.get(pkg.usesOptionalLibraries.get(i)); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3570 | if (file == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3571 | Slog.w(TAG, "Package " + pkg.packageName |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 3572 | + " desires unavailable shared library " |
| 3573 | + pkg.usesOptionalLibraries.get(i) + "; ignoring!"); |
| 3574 | } else { |
| 3575 | mTmpSharedLibraries[num] = file; |
| 3576 | num++; |
| 3577 | } |
| 3578 | } |
| 3579 | if (num > 0) { |
| 3580 | pkg.usesLibraryFiles = new String[num]; |
| 3581 | System.arraycopy(mTmpSharedLibraries, 0, |
| 3582 | pkg.usesLibraryFiles, 0, num); |
| 3583 | } |
Kenny Root | 1683afa | 2011-01-07 14:27:50 -0800 | [diff] [blame] | 3584 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3585 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3586 | if (pkg.mSharedUserId != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3587 | suid = mSettings.getSharedUserLPw(pkg.mSharedUserId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3588 | pkg.applicationInfo.flags, true); |
| 3589 | if (suid == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3590 | Slog.w(TAG, "Creating application package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3591 | + " for shared user failed"); |
| 3592 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3593 | return null; |
| 3594 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3595 | if (DEBUG_PACKAGE_SCANNING) { |
| 3596 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3597 | Log.d(TAG, "Shared UserID " + pkg.mSharedUserId + " (uid=" + suid.userId |
| 3598 | + "): packages=" + suid.packages); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3599 | } |
| 3600 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3601 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3602 | // Check if we are renaming from an original package name. |
| 3603 | PackageSetting origPackage = null; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3604 | String realName = null; |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3605 | if (pkg.mOriginalPackages != null) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3606 | // This package may need to be renamed to a previously |
| 3607 | // installed name. Let's check on that... |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3608 | final String renamed = mSettings.mRenamedPackages.get(pkg.mRealPackage); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3609 | if (pkg.mOriginalPackages.contains(renamed)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3610 | // This package had originally been installed as the |
| 3611 | // original name, and we have already taken care of |
| 3612 | // transitioning to the new one. Just update the new |
| 3613 | // one to continue using the old name. |
| 3614 | realName = pkg.mRealPackage; |
| 3615 | if (!pkg.packageName.equals(renamed)) { |
| 3616 | // Callers into this function may have already taken |
| 3617 | // care of renaming the package; only do it here if |
| 3618 | // it is not already done. |
| 3619 | pkg.setPackageName(renamed); |
| 3620 | } |
| 3621 | |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3622 | } else { |
| 3623 | for (int i=pkg.mOriginalPackages.size()-1; i>=0; i--) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3624 | if ((origPackage = mSettings.peekPackageLPr( |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3625 | pkg.mOriginalPackages.get(i))) != null) { |
| 3626 | // We do have the package already installed under its |
| 3627 | // original name... should we use it? |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3628 | if (!verifyPackageUpdateLPr(origPackage, pkg)) { |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3629 | // New package is not compatible with original. |
| 3630 | origPackage = null; |
| 3631 | continue; |
| 3632 | } else if (origPackage.sharedUser != null) { |
| 3633 | // Make sure uid is compatible between packages. |
| 3634 | if (!origPackage.sharedUser.name.equals(pkg.mSharedUserId)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3635 | Slog.w(TAG, "Unable to migrate data from " + origPackage.name |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3636 | + " to " + pkg.packageName + ": old uid " |
| 3637 | + origPackage.sharedUser.name |
| 3638 | + " differs from " + pkg.mSharedUserId); |
| 3639 | origPackage = null; |
| 3640 | continue; |
| 3641 | } |
| 3642 | } else { |
| 3643 | if (DEBUG_UPGRADE) Log.v(TAG, "Renaming new package " |
| 3644 | + pkg.packageName + " to old name " + origPackage.name); |
| 3645 | } |
| 3646 | break; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3647 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3648 | } |
| 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | if (mTransferedPackages.contains(pkg.packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3653 | Slog.w(TAG, "Package " + pkg.packageName |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3654 | + " was transferred to another, but its .apk remains"); |
| 3655 | } |
| 3656 | |
Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 3657 | // Just create the setting, don't add it yet. For already existing packages |
| 3658 | // the PkgSetting exists already and doesn't have to be created. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3659 | pkgSetting = mSettings.getPackageLPw(pkg, origPackage, realName, suid, destCodeFile, |
Kenny Root | 806cc13 | 2010-09-12 08:34:19 -0700 | [diff] [blame] | 3660 | destResourceFile, pkg.applicationInfo.nativeLibraryDir, |
| 3661 | pkg.applicationInfo.flags, true, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3662 | if (pkgSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3663 | Slog.w(TAG, "Creating application package " + pkg.packageName + " failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3664 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3665 | return null; |
| 3666 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3667 | |
| 3668 | if (pkgSetting.origPackage != null) { |
| 3669 | // If we are first transitioning from an original package, |
| 3670 | // fix up the new package's name now. We need to do this after |
| 3671 | // looking up the package under its new name, so getPackageLP |
| 3672 | // can take care of fiddling things correctly. |
| 3673 | pkg.setPackageName(origPackage.name); |
| 3674 | |
| 3675 | // File a report about this. |
| 3676 | String msg = "New package " + pkgSetting.realName |
| 3677 | + " renamed to replace old package " + pkgSetting.name; |
| 3678 | reportSettingsProblem(Log.WARN, msg); |
| 3679 | |
| 3680 | // Make a note of it. |
| 3681 | mTransferedPackages.add(origPackage.name); |
| 3682 | |
| 3683 | // No longer need to retain this. |
| 3684 | pkgSetting.origPackage = null; |
| 3685 | } |
| 3686 | |
| 3687 | if (realName != null) { |
| 3688 | // Make a note of it. |
| 3689 | mTransferedPackages.add(pkg.packageName); |
| 3690 | } |
| 3691 | |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 3692 | if (mSettings.isDisabledSystemPackageLPr(pkg.packageName)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3693 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
| 3694 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3695 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3696 | pkg.applicationInfo.uid = pkgSetting.appId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3697 | pkg.mExtras = pkgSetting; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3698 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3699 | if (!verifySignaturesLP(pkgSetting, pkg)) { |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 3700 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3701 | return null; |
| 3702 | } |
| 3703 | // The signature has changed, but this package is in the system |
| 3704 | // image... let's recover! |
Suchi Amalapurapu | c4dd60f | 2009-03-24 21:10:53 -0700 | [diff] [blame] | 3705 | pkgSetting.signatures.mSignatures = pkg.mSignatures; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3706 | // However... if this package is part of a shared user, but it |
| 3707 | // doesn't match the signature of the shared user, let's fail. |
| 3708 | // What this means is that you can't change the signatures |
| 3709 | // associated with an overall shared user, which doesn't seem all |
| 3710 | // that unreasonable. |
| 3711 | if (pkgSetting.sharedUser != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3712 | if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3713 | pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) { |
| 3714 | 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] | 3715 | mLastScanError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 3716 | return null; |
| 3717 | } |
| 3718 | } |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 3719 | // File a report about this. |
| 3720 | String msg = "System package " + pkg.packageName |
| 3721 | + " signature changed; retaining data."; |
| 3722 | reportSettingsProblem(Log.WARN, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3723 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3724 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3725 | // Verify that this new package doesn't have any content providers |
| 3726 | // that conflict with existing packages. Only do this if the |
| 3727 | // package isn't already installed, since we don't want to break |
| 3728 | // things that are installed. |
| 3729 | if ((scanMode&SCAN_NEW_INSTALL) != 0) { |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 3730 | final int N = pkg.providers.size(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3731 | int i; |
| 3732 | for (i=0; i<N; i++) { |
| 3733 | PackageParser.Provider p = pkg.providers.get(i); |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3734 | if (p.info.authority != null) { |
| 3735 | String names[] = p.info.authority.split(";"); |
| 3736 | for (int j = 0; j < names.length; j++) { |
| 3737 | if (mProviders.containsKey(names[j])) { |
| 3738 | PackageParser.Provider other = mProviders.get(names[j]); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3739 | Slog.w(TAG, "Can't install because provider name " + names[j] + |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 3740 | " (in package " + pkg.applicationInfo.packageName + |
| 3741 | ") is already used by " |
| 3742 | + ((other != null && other.getComponentName() != null) |
| 3743 | ? other.getComponentName().getPackageName() : "?")); |
| 3744 | mLastScanError = PackageManager.INSTALL_FAILED_CONFLICTING_PROVIDER; |
| 3745 | return null; |
| 3746 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3747 | } |
| 3748 | } |
| 3749 | } |
| 3750 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3751 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3752 | if (pkg.mAdoptPermissions != null) { |
| 3753 | // This package wants to adopt ownership of permissions from |
| 3754 | // another package. |
| 3755 | for (int i = pkg.mAdoptPermissions.size() - 1; i >= 0; i--) { |
| 3756 | final String origName = pkg.mAdoptPermissions.get(i); |
| 3757 | final PackageSetting orig = mSettings.peekPackageLPr(origName); |
| 3758 | if (orig != null) { |
| 3759 | if (verifyPackageUpdateLPr(orig, pkg)) { |
| 3760 | Slog.i(TAG, "Adopting permissions from " + origName + " to " |
| 3761 | + pkg.packageName); |
| 3762 | mSettings.transferPermissionsLPw(origName, pkg.packageName); |
| 3763 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3764 | } |
| 3765 | } |
| 3766 | } |
| 3767 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3768 | |
| 3769 | final String pkgName = pkg.packageName; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3770 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3771 | final long scanFileTime = scanFile.lastModified(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3772 | final boolean forceDex = (scanMode&SCAN_FORCE_DEX) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3773 | pkg.applicationInfo.processName = fixProcessName( |
| 3774 | pkg.applicationInfo.packageName, |
| 3775 | pkg.applicationInfo.processName, |
| 3776 | pkg.applicationInfo.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3777 | |
| 3778 | File dataPath; |
| 3779 | if (mPlatformPackage == pkg) { |
| 3780 | // The system package is special. |
| 3781 | dataPath = new File (Environment.getDataDirectory(), "system"); |
| 3782 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3783 | } else { |
| 3784 | // This is a normal package, need to make its data directory. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3785 | dataPath = getDataPathForPackage(pkg.packageName, 0); |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3786 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3787 | boolean uidError = false; |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3789 | if (dataPath.exists()) { |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3790 | // XXX should really do this check for each user. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3791 | mOutPermissions[1] = 0; |
| 3792 | FileUtils.getPermissions(dataPath.getPath(), mOutPermissions); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3793 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3794 | // 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] | 3795 | if (mOutPermissions[1] != pkg.applicationInfo.uid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3796 | boolean recovered = false; |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3797 | if (mOutPermissions[1] == 0) { |
| 3798 | // The directory somehow became owned by root. Wow. |
| 3799 | // This is probably because the system was stopped while |
| 3800 | // installd was in the middle of messing with its libs |
| 3801 | // directory. Ask installd to fix that. |
| 3802 | int ret = mInstaller.fixUid(pkgName, pkg.applicationInfo.uid, |
| 3803 | pkg.applicationInfo.uid); |
| 3804 | if (ret >= 0) { |
| 3805 | recovered = true; |
| 3806 | String msg = "Package " + pkg.packageName |
| 3807 | + " unexpectedly changed to uid 0; recovered to " + |
| 3808 | + pkg.applicationInfo.uid; |
| 3809 | reportSettingsProblem(Log.WARN, msg); |
| 3810 | } |
| 3811 | } |
| 3812 | if (!recovered && ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0 |
| 3813 | || (scanMode&SCAN_BOOTING) != 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3814 | // If this is a system app, we can at least delete its |
| 3815 | // current data so the application will still work. |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3816 | int ret = mInstaller.remove(pkgName, 0); |
| 3817 | if (ret >= 0) { |
| 3818 | // TODO: Kill the processes first |
| 3819 | // Remove the data directories for all users |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3820 | sUserManager.removePackageForAllUsers(pkgName); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3821 | // Old data gone! |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3822 | String prefix = (parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0 |
| 3823 | ? "System package " : "Third party package "; |
| 3824 | String msg = prefix + pkg.packageName |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3825 | + " has changed from uid: " |
| 3826 | + mOutPermissions[1] + " to " |
| 3827 | + pkg.applicationInfo.uid + "; old data erased"; |
| 3828 | reportSettingsProblem(Log.WARN, msg); |
| 3829 | recovered = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3830 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3831 | // And now re-install the app. |
| 3832 | ret = mInstaller.install(pkgName, pkg.applicationInfo.uid, |
| 3833 | pkg.applicationInfo.uid); |
| 3834 | if (ret == -1) { |
| 3835 | // Ack should not happen! |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3836 | msg = prefix + pkg.packageName |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3837 | + " could not have data directory re-created after delete."; |
| 3838 | reportSettingsProblem(Log.WARN, msg); |
| 3839 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3840 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3841 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3842 | // Create data directories for all users |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3843 | sUserManager.installPackageForAllUsers(pkgName, |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3844 | pkg.applicationInfo.uid); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3845 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3846 | if (!recovered) { |
| 3847 | mHasSystemUidErrors = true; |
| 3848 | } |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 3849 | } else if (!recovered) { |
| 3850 | // If we allow this install to proceed, we will be broken. |
| 3851 | // Abort, abort! |
| 3852 | mLastScanError = PackageManager.INSTALL_FAILED_UID_CHANGED; |
| 3853 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3854 | } |
| 3855 | if (!recovered) { |
| 3856 | pkg.applicationInfo.dataDir = "/mismatched_uid/settings_" |
| 3857 | + pkg.applicationInfo.uid + "/fs_" |
| 3858 | + mOutPermissions[1]; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3859 | pkg.applicationInfo.nativeLibraryDir = pkg.applicationInfo.dataDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3860 | String msg = "Package " + pkg.packageName |
| 3861 | + " has mismatched uid: " |
| 3862 | + mOutPermissions[1] + " on disk, " |
| 3863 | + pkg.applicationInfo.uid + " in settings"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3864 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3865 | synchronized (mPackages) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3866 | mSettings.mReadMessages.append(msg); |
| 3867 | mSettings.mReadMessages.append('\n'); |
| 3868 | uidError = true; |
| 3869 | if (!pkgSetting.uidError) { |
| 3870 | reportSettingsProblem(Log.ERROR, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3871 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3872 | } |
| 3873 | } |
| 3874 | } |
| 3875 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3876 | } else { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3877 | if (DEBUG_PACKAGE_SCANNING) { |
| 3878 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 3879 | Log.v(TAG, "Want this data dir: " + dataPath); |
| 3880 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3881 | //invoke installer to do the actual installation |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3882 | int ret = mInstaller.install(pkgName, pkg.applicationInfo.uid, |
| 3883 | pkg.applicationInfo.uid); |
| 3884 | if (ret < 0) { |
| 3885 | // Error from installer |
| 3886 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 3887 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3888 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3889 | // Create data directories for all users |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3890 | sUserManager.installPackageForAllUsers(pkgName, pkg.applicationInfo.uid); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3891 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3892 | if (dataPath.exists()) { |
| 3893 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 3894 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3895 | Slog.w(TAG, "Unable to create data directory: " + dataPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3896 | pkg.applicationInfo.dataDir = null; |
| 3897 | } |
| 3898 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3899 | |
| 3900 | /* |
| 3901 | * Set the data dir to the default "/data/data/<package name>/lib" |
| 3902 | * if we got here without anyone telling us different (e.g., apps |
| 3903 | * stored on SD card have their native libraries stored in the ASEC |
| 3904 | * container with the APK). |
Kenny Root | 806cc13 | 2010-09-12 08:34:19 -0700 | [diff] [blame] | 3905 | * |
| 3906 | * This happens during an upgrade from a package settings file that |
| 3907 | * doesn't have a native library path attribute at all. |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3908 | */ |
Kenny Root | bd135c1 | 2010-10-05 12:26:27 -0700 | [diff] [blame] | 3909 | if (pkg.applicationInfo.nativeLibraryDir == null && pkg.applicationInfo.dataDir != null) { |
| 3910 | if (pkgSetting.nativeLibraryPathString == null) { |
| 3911 | final String nativeLibraryPath = new File(dataPath, LIB_DIR_NAME).getPath(); |
| 3912 | pkg.applicationInfo.nativeLibraryDir = nativeLibraryPath; |
| 3913 | pkgSetting.nativeLibraryPathString = nativeLibraryPath; |
| 3914 | } else { |
| 3915 | pkg.applicationInfo.nativeLibraryDir = pkgSetting.nativeLibraryPathString; |
| 3916 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3917 | } |
| 3918 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3919 | pkgSetting.uidError = uidError; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3920 | } |
| 3921 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3922 | String path = scanFile.getPath(); |
| 3923 | /* Note: We don't want to unpack the native binaries for |
| 3924 | * system applications, unless they have been updated |
| 3925 | * (the binaries are already under /system/lib). |
| 3926 | * Also, don't unpack libs for apps on the external card |
| 3927 | * since they should have their libraries in the ASEC |
| 3928 | * container already. |
| 3929 | * |
| 3930 | * In other words, we're going to unpack the binaries |
| 3931 | * only for non-system apps and system app upgrades. |
| 3932 | */ |
| 3933 | if (pkg.applicationInfo.nativeLibraryDir != null) { |
| 3934 | try { |
| 3935 | final File nativeLibraryDir = new File(pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 5832ead | 2011-08-02 15:47:08 -0700 | [diff] [blame] | 3936 | final String dataPathString = dataPath.getCanonicalPath(); |
Kenny Root | e68d58a | 2010-10-18 16:08:54 -0700 | [diff] [blame] | 3937 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3938 | if (isSystemApp(pkg) && !isUpdatedSystemApp(pkg)) { |
| 3939 | /* |
| 3940 | * Upgrading from a previous version of the OS sometimes |
| 3941 | * leaves native libraries in the /data/data/<app>/lib |
| 3942 | * directory for system apps even when they shouldn't be. |
| 3943 | * Recent changes in the JNI library search path |
| 3944 | * necessitates we remove those to match previous behavior. |
| 3945 | */ |
| 3946 | if (NativeLibraryHelper.removeNativeBinariesFromDirLI(nativeLibraryDir)) { |
| 3947 | Log.i(TAG, "removed obsolete native libraries for system package " |
| 3948 | + path); |
Kenny Root | 831baa2 | 2010-10-05 12:29:25 -0700 | [diff] [blame] | 3949 | } |
Kenny Root | 5832ead | 2011-08-02 15:47:08 -0700 | [diff] [blame] | 3950 | } else if (nativeLibraryDir.getParentFile().getCanonicalPath() |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3951 | .equals(dataPathString)) { |
| 3952 | /* |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 3953 | * Make sure the native library dir isn't a symlink to |
| 3954 | * something. If it is, ask installd to remove it and create |
| 3955 | * a directory so we can copy to it afterwards. |
| 3956 | */ |
| 3957 | boolean isSymLink; |
| 3958 | try { |
| 3959 | isSymLink = S_ISLNK(Libcore.os.lstat(nativeLibraryDir.getPath()).st_mode); |
| 3960 | } catch (ErrnoException e) { |
| 3961 | // This shouldn't happen, but we'll fail-safe. |
| 3962 | isSymLink = true; |
| 3963 | } |
| 3964 | if (isSymLink) { |
| 3965 | mInstaller.unlinkNativeLibraryDirectory(dataPathString); |
| 3966 | } |
| 3967 | |
| 3968 | /* |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3969 | * If this is an internal application or our |
| 3970 | * nativeLibraryPath points to our data directory, unpack |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 3971 | * the libraries if necessary. |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3972 | */ |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 3973 | NativeLibraryHelper.copyNativeBinariesIfNeededLI(scanFile, nativeLibraryDir); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3974 | } else { |
| 3975 | Slog.i(TAG, "Linking native library dir for " + path); |
| 3976 | mInstaller.linkNativeLibraryDirectory(dataPathString, |
| 3977 | pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 831baa2 | 2010-10-05 12:29:25 -0700 | [diff] [blame] | 3978 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3979 | } catch (IOException ioe) { |
| 3980 | 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] | 3981 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3982 | } |
| 3983 | pkg.mScanPath = path; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3984 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3985 | if ((scanMode&SCAN_NO_DEX) == 0) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3986 | if (performDexOptLI(pkg, forceDex, (scanMode&SCAN_DEFER_DEX) != 0) |
| 3987 | == DEX_OPT_FAILED) { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3988 | mLastScanError = PackageManager.INSTALL_FAILED_DEXOPT; |
| 3989 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3990 | } |
| 3991 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3992 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3993 | if (mFactoryTest && pkg.requestedPermissions.contains( |
| 3994 | android.Manifest.permission.FACTORY_TEST)) { |
| 3995 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_FACTORY_TEST; |
| 3996 | } |
| 3997 | |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3998 | // Request the ActivityManager to kill the process(only for existing packages) |
| 3999 | // so that we do not end up in a confused state while the user is still using the older |
| 4000 | // version of the application while the new one gets installed. |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4001 | if ((parseFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4002 | killApplication(pkg.applicationInfo.packageName, |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 4003 | pkg.applicationInfo.uid); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 4004 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4005 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4006 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4007 | synchronized (mPackages) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4008 | // We don't expect installation to fail beyond this point, |
| 4009 | if ((scanMode&SCAN_MONITOR) != 0) { |
| 4010 | mAppDirs.put(pkg.mPath, pkg); |
| 4011 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4012 | // Add the new setting to mSettings |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4013 | mSettings.insertPackageSettingLPw(pkgSetting, pkg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4014 | // Add the new setting to mPackages |
Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 4015 | mPackages.put(pkg.applicationInfo.packageName, pkg); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 4016 | // Make sure we don't accidentally delete its data. |
| 4017 | mSettings.mPackagesToBeCleaned.remove(pkgName); |
| 4018 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 4019 | // Take care of first install / last update times. |
| 4020 | if (currentTime != 0) { |
| 4021 | if (pkgSetting.firstInstallTime == 0) { |
| 4022 | pkgSetting.firstInstallTime = pkgSetting.lastUpdateTime = currentTime; |
| 4023 | } else if ((scanMode&SCAN_UPDATE_TIME) != 0) { |
| 4024 | pkgSetting.lastUpdateTime = currentTime; |
| 4025 | } |
| 4026 | } else if (pkgSetting.firstInstallTime == 0) { |
| 4027 | // We need *something*. Take time time stamp of the file. |
| 4028 | pkgSetting.firstInstallTime = pkgSetting.lastUpdateTime = scanFileTime; |
| 4029 | } else if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) != 0) { |
| 4030 | if (scanFileTime != pkgSetting.timeStamp) { |
| 4031 | // A package on the system image has changed; consider this |
| 4032 | // to be an update. |
| 4033 | pkgSetting.lastUpdateTime = scanFileTime; |
| 4034 | } |
| 4035 | } |
| 4036 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4037 | int N = pkg.providers.size(); |
| 4038 | StringBuilder r = null; |
| 4039 | int i; |
| 4040 | for (i=0; i<N; i++) { |
| 4041 | PackageParser.Provider p = pkg.providers.get(i); |
| 4042 | p.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4043 | p.info.processName, pkg.applicationInfo.uid); |
| 4044 | mProvidersByComponent.put(new ComponentName(p.info.packageName, |
| 4045 | p.info.name), p); |
| 4046 | p.syncable = p.info.isSyncable; |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4047 | if (p.info.authority != null) { |
| 4048 | String names[] = p.info.authority.split(";"); |
| 4049 | p.info.authority = null; |
| 4050 | for (int j = 0; j < names.length; j++) { |
| 4051 | if (j == 1 && p.syncable) { |
| 4052 | // We only want the first authority for a provider to possibly be |
| 4053 | // syncable, so if we already added this provider using a different |
| 4054 | // authority clear the syncable flag. We copy the provider before |
| 4055 | // changing it because the mProviders object contains a reference |
| 4056 | // to a provider that we don't want to change. |
| 4057 | // Only do this for the second authority since the resulting provider |
| 4058 | // object can be the same for all future authorities for this provider. |
| 4059 | p = new PackageParser.Provider(p); |
| 4060 | p.syncable = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4061 | } |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4062 | if (!mProviders.containsKey(names[j])) { |
| 4063 | mProviders.put(names[j], p); |
| 4064 | if (p.info.authority == null) { |
| 4065 | p.info.authority = names[j]; |
| 4066 | } else { |
| 4067 | p.info.authority = p.info.authority + ";" + names[j]; |
| 4068 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4069 | if (DEBUG_PACKAGE_SCANNING) { |
| 4070 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 4071 | Log.d(TAG, "Registered content provider: " + names[j] |
| 4072 | + ", className = " + p.info.name + ", isSyncable = " |
| 4073 | + p.info.isSyncable); |
| 4074 | } |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4075 | } else { |
| 4076 | PackageParser.Provider other = mProviders.get(names[j]); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4077 | Slog.w(TAG, "Skipping provider name " + names[j] + |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4078 | " (in package " + pkg.applicationInfo.packageName + |
| 4079 | "): name already used by " |
| 4080 | + ((other != null && other.getComponentName() != null) |
| 4081 | ? other.getComponentName().getPackageName() : "?")); |
| 4082 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4083 | } |
| 4084 | } |
| 4085 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4086 | if (r == null) { |
| 4087 | r = new StringBuilder(256); |
| 4088 | } else { |
| 4089 | r.append(' '); |
| 4090 | } |
| 4091 | r.append(p.info.name); |
| 4092 | } |
| 4093 | } |
| 4094 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4095 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Providers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4096 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4097 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4098 | N = pkg.services.size(); |
| 4099 | r = null; |
| 4100 | for (i=0; i<N; i++) { |
| 4101 | PackageParser.Service s = pkg.services.get(i); |
| 4102 | s.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4103 | s.info.processName, pkg.applicationInfo.uid); |
| 4104 | mServices.addService(s); |
| 4105 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4106 | if (r == null) { |
| 4107 | r = new StringBuilder(256); |
| 4108 | } else { |
| 4109 | r.append(' '); |
| 4110 | } |
| 4111 | r.append(s.info.name); |
| 4112 | } |
| 4113 | } |
| 4114 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4115 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Services: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4116 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4118 | N = pkg.receivers.size(); |
| 4119 | r = null; |
| 4120 | for (i=0; i<N; i++) { |
| 4121 | PackageParser.Activity a = pkg.receivers.get(i); |
| 4122 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4123 | a.info.processName, pkg.applicationInfo.uid); |
| 4124 | mReceivers.addActivity(a, "receiver"); |
| 4125 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4126 | if (r == null) { |
| 4127 | r = new StringBuilder(256); |
| 4128 | } else { |
| 4129 | r.append(' '); |
| 4130 | } |
| 4131 | r.append(a.info.name); |
| 4132 | } |
| 4133 | } |
| 4134 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4135 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Receivers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4136 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4138 | N = pkg.activities.size(); |
| 4139 | r = null; |
| 4140 | for (i=0; i<N; i++) { |
| 4141 | PackageParser.Activity a = pkg.activities.get(i); |
| 4142 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4143 | a.info.processName, pkg.applicationInfo.uid); |
| 4144 | mActivities.addActivity(a, "activity"); |
| 4145 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4146 | if (r == null) { |
| 4147 | r = new StringBuilder(256); |
| 4148 | } else { |
| 4149 | r.append(' '); |
| 4150 | } |
| 4151 | r.append(a.info.name); |
| 4152 | } |
| 4153 | } |
| 4154 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4155 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Activities: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4156 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4157 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4158 | N = pkg.permissionGroups.size(); |
| 4159 | r = null; |
| 4160 | for (i=0; i<N; i++) { |
| 4161 | PackageParser.PermissionGroup pg = pkg.permissionGroups.get(i); |
| 4162 | PackageParser.PermissionGroup cur = mPermissionGroups.get(pg.info.name); |
| 4163 | if (cur == null) { |
| 4164 | mPermissionGroups.put(pg.info.name, pg); |
| 4165 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4166 | if (r == null) { |
| 4167 | r = new StringBuilder(256); |
| 4168 | } else { |
| 4169 | r.append(' '); |
| 4170 | } |
| 4171 | r.append(pg.info.name); |
| 4172 | } |
| 4173 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4174 | 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] | 4175 | + pg.info.packageName + " ignored: original from " |
| 4176 | + cur.info.packageName); |
| 4177 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4178 | if (r == null) { |
| 4179 | r = new StringBuilder(256); |
| 4180 | } else { |
| 4181 | r.append(' '); |
| 4182 | } |
| 4183 | r.append("DUP:"); |
| 4184 | r.append(pg.info.name); |
| 4185 | } |
| 4186 | } |
| 4187 | } |
| 4188 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4189 | 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] | 4190 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4192 | N = pkg.permissions.size(); |
| 4193 | r = null; |
| 4194 | for (i=0; i<N; i++) { |
| 4195 | PackageParser.Permission p = pkg.permissions.get(i); |
| 4196 | HashMap<String, BasePermission> permissionMap = |
| 4197 | p.tree ? mSettings.mPermissionTrees |
| 4198 | : mSettings.mPermissions; |
| 4199 | p.group = mPermissionGroups.get(p.info.group); |
| 4200 | if (p.info.group == null || p.group != null) { |
| 4201 | BasePermission bp = permissionMap.get(p.info.name); |
| 4202 | if (bp == null) { |
| 4203 | bp = new BasePermission(p.info.name, p.info.packageName, |
| 4204 | BasePermission.TYPE_NORMAL); |
| 4205 | permissionMap.put(p.info.name, bp); |
| 4206 | } |
| 4207 | if (bp.perm == null) { |
| 4208 | if (bp.sourcePackage == null |
| 4209 | || bp.sourcePackage.equals(p.info.packageName)) { |
| 4210 | BasePermission tree = findPermissionTreeLP(p.info.name); |
| 4211 | if (tree == null |
| 4212 | || tree.sourcePackage.equals(p.info.packageName)) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4213 | bp.packageSetting = pkgSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4214 | bp.perm = p; |
| 4215 | bp.uid = pkg.applicationInfo.uid; |
| 4216 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4217 | if (r == null) { |
| 4218 | r = new StringBuilder(256); |
| 4219 | } else { |
| 4220 | r.append(' '); |
| 4221 | } |
| 4222 | r.append(p.info.name); |
| 4223 | } |
| 4224 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4225 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4226 | + p.info.packageName + " ignored: base tree " |
| 4227 | + tree.name + " is from package " |
| 4228 | + tree.sourcePackage); |
| 4229 | } |
| 4230 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4231 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4232 | + p.info.packageName + " ignored: original from " |
| 4233 | + bp.sourcePackage); |
| 4234 | } |
| 4235 | } else if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4236 | if (r == null) { |
| 4237 | r = new StringBuilder(256); |
| 4238 | } else { |
| 4239 | r.append(' '); |
| 4240 | } |
| 4241 | r.append("DUP:"); |
| 4242 | r.append(p.info.name); |
| 4243 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4244 | if (bp.perm == p) { |
| 4245 | bp.protectionLevel = p.info.protectionLevel; |
| 4246 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4247 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4248 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4249 | + p.info.packageName + " ignored: no group " |
| 4250 | + p.group); |
| 4251 | } |
| 4252 | } |
| 4253 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4254 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Permissions: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4255 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4257 | N = pkg.instrumentation.size(); |
| 4258 | r = null; |
| 4259 | for (i=0; i<N; i++) { |
| 4260 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
| 4261 | a.info.packageName = pkg.applicationInfo.packageName; |
| 4262 | a.info.sourceDir = pkg.applicationInfo.sourceDir; |
| 4263 | a.info.publicSourceDir = pkg.applicationInfo.publicSourceDir; |
| 4264 | a.info.dataDir = pkg.applicationInfo.dataDir; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4265 | a.info.nativeLibraryDir = pkg.applicationInfo.nativeLibraryDir; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4266 | mInstrumentation.put(a.getComponentName(), a); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4267 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4268 | if (r == null) { |
| 4269 | r = new StringBuilder(256); |
| 4270 | } else { |
| 4271 | r.append(' '); |
| 4272 | } |
| 4273 | r.append(a.info.name); |
| 4274 | } |
| 4275 | } |
| 4276 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4277 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Instrumentation: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4278 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4279 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 4280 | if (pkg.protectedBroadcasts != null) { |
| 4281 | N = pkg.protectedBroadcasts.size(); |
| 4282 | for (i=0; i<N; i++) { |
| 4283 | mProtectedBroadcasts.add(pkg.protectedBroadcasts.get(i)); |
| 4284 | } |
| 4285 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4287 | pkgSetting.setTimeStamp(scanFileTime); |
| 4288 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4290 | return pkg; |
| 4291 | } |
| 4292 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4293 | private void killApplication(String pkgName, int uid) { |
| 4294 | // Request the ActivityManager to kill the process(only for existing packages) |
| 4295 | // so that we do not end up in a confused state while the user is still using the older |
| 4296 | // version of the application while the new one gets installed. |
| 4297 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 4298 | if (am != null) { |
| 4299 | try { |
| 4300 | am.killApplicationWithUid(pkgName, uid); |
| 4301 | } catch (RemoteException e) { |
| 4302 | } |
| 4303 | } |
| 4304 | } |
| 4305 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4306 | void removePackageLI(PackageParser.Package pkg, boolean chatty) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4307 | if (DEBUG_INSTALL) { |
| 4308 | if (chatty) |
| 4309 | Log.d(TAG, "Removing package " + pkg.applicationInfo.packageName); |
| 4310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4311 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4312 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4313 | synchronized (mPackages) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4314 | mPackages.remove(pkg.applicationInfo.packageName); |
| 4315 | if (pkg.mPath != null) { |
| 4316 | mAppDirs.remove(pkg.mPath); |
| 4317 | } |
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 | int N = pkg.providers.size(); |
| 4320 | StringBuilder r = null; |
| 4321 | int i; |
| 4322 | for (i=0; i<N; i++) { |
| 4323 | PackageParser.Provider p = pkg.providers.get(i); |
| 4324 | mProvidersByComponent.remove(new ComponentName(p.info.packageName, |
| 4325 | p.info.name)); |
| 4326 | if (p.info.authority == null) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4327 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4328 | /* The is another ContentProvider with this authority when |
| 4329 | * this app was installed so this authority is null, |
| 4330 | * Ignore it as we don't have to unregister the provider. |
| 4331 | */ |
| 4332 | continue; |
| 4333 | } |
| 4334 | String names[] = p.info.authority.split(";"); |
| 4335 | for (int j = 0; j < names.length; j++) { |
| 4336 | if (mProviders.get(names[j]) == p) { |
| 4337 | mProviders.remove(names[j]); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4338 | if (DEBUG_REMOVE) { |
| 4339 | if (chatty) |
| 4340 | Log.d(TAG, "Unregistered content provider: " + names[j] |
| 4341 | + ", className = " + p.info.name + ", isSyncable = " |
| 4342 | + p.info.isSyncable); |
| 4343 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4344 | } |
| 4345 | } |
| 4346 | if (chatty) { |
| 4347 | if (r == null) { |
| 4348 | r = new StringBuilder(256); |
| 4349 | } else { |
| 4350 | r.append(' '); |
| 4351 | } |
| 4352 | r.append(p.info.name); |
| 4353 | } |
| 4354 | } |
| 4355 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4356 | if (DEBUG_REMOVE) Log.d(TAG, " Providers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4357 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4358 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4359 | N = pkg.services.size(); |
| 4360 | r = null; |
| 4361 | for (i=0; i<N; i++) { |
| 4362 | PackageParser.Service s = pkg.services.get(i); |
| 4363 | mServices.removeService(s); |
| 4364 | if (chatty) { |
| 4365 | if (r == null) { |
| 4366 | r = new StringBuilder(256); |
| 4367 | } else { |
| 4368 | r.append(' '); |
| 4369 | } |
| 4370 | r.append(s.info.name); |
| 4371 | } |
| 4372 | } |
| 4373 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4374 | if (DEBUG_REMOVE) Log.d(TAG, " Services: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4375 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4376 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4377 | N = pkg.receivers.size(); |
| 4378 | r = null; |
| 4379 | for (i=0; i<N; i++) { |
| 4380 | PackageParser.Activity a = pkg.receivers.get(i); |
| 4381 | mReceivers.removeActivity(a, "receiver"); |
| 4382 | if (chatty) { |
| 4383 | if (r == null) { |
| 4384 | r = new StringBuilder(256); |
| 4385 | } else { |
| 4386 | r.append(' '); |
| 4387 | } |
| 4388 | r.append(a.info.name); |
| 4389 | } |
| 4390 | } |
| 4391 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4392 | if (DEBUG_REMOVE) Log.d(TAG, " Receivers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4393 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4395 | N = pkg.activities.size(); |
| 4396 | r = null; |
| 4397 | for (i=0; i<N; i++) { |
| 4398 | PackageParser.Activity a = pkg.activities.get(i); |
| 4399 | mActivities.removeActivity(a, "activity"); |
| 4400 | if (chatty) { |
| 4401 | if (r == null) { |
| 4402 | r = new StringBuilder(256); |
| 4403 | } else { |
| 4404 | r.append(' '); |
| 4405 | } |
| 4406 | r.append(a.info.name); |
| 4407 | } |
| 4408 | } |
| 4409 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4410 | if (DEBUG_REMOVE) Log.d(TAG, " Activities: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4411 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4413 | N = pkg.permissions.size(); |
| 4414 | r = null; |
| 4415 | for (i=0; i<N; i++) { |
| 4416 | PackageParser.Permission p = pkg.permissions.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4417 | BasePermission bp = mSettings.mPermissions.get(p.info.name); |
| 4418 | if (bp == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4419 | bp = mSettings.mPermissionTrees.get(p.info.name); |
| 4420 | } |
| 4421 | if (bp != null && bp.perm == p) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4422 | bp.perm = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4423 | if (chatty) { |
| 4424 | if (r == null) { |
| 4425 | r = new StringBuilder(256); |
| 4426 | } else { |
| 4427 | r.append(' '); |
| 4428 | } |
| 4429 | r.append(p.info.name); |
| 4430 | } |
| 4431 | } |
| 4432 | } |
| 4433 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4434 | if (DEBUG_REMOVE) Log.d(TAG, " Permissions: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4435 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4436 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4437 | N = pkg.instrumentation.size(); |
| 4438 | r = null; |
| 4439 | for (i=0; i<N; i++) { |
| 4440 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4441 | mInstrumentation.remove(a.getComponentName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4442 | if (chatty) { |
| 4443 | if (r == null) { |
| 4444 | r = new StringBuilder(256); |
| 4445 | } else { |
| 4446 | r.append(' '); |
| 4447 | } |
| 4448 | r.append(a.info.name); |
| 4449 | } |
| 4450 | } |
| 4451 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4452 | if (DEBUG_REMOVE) Log.d(TAG, " Instrumentation: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4453 | } |
| 4454 | } |
| 4455 | } |
| 4456 | |
| 4457 | private static final boolean isPackageFilename(String name) { |
| 4458 | return name != null && name.endsWith(".apk"); |
| 4459 | } |
| 4460 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4461 | private static boolean hasPermission(PackageParser.Package pkgInfo, String perm) { |
| 4462 | for (int i=pkgInfo.permissions.size()-1; i>=0; i--) { |
| 4463 | if (pkgInfo.permissions.get(i).info.name.equals(perm)) { |
| 4464 | return true; |
| 4465 | } |
| 4466 | } |
| 4467 | return false; |
| 4468 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4469 | |
| 4470 | static final int UPDATE_PERMISSIONS_ALL = 1<<0; |
| 4471 | static final int UPDATE_PERMISSIONS_REPLACE_PKG = 1<<1; |
| 4472 | static final int UPDATE_PERMISSIONS_REPLACE_ALL = 1<<2; |
| 4473 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4474 | private void updatePermissionsLPw(String changingPkg, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4475 | PackageParser.Package pkgInfo, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4476 | // Make sure there are no dangling permission trees. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4477 | Iterator<BasePermission> it = mSettings.mPermissionTrees.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4478 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4479 | final BasePermission bp = it.next(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4480 | if (bp.packageSetting == null) { |
| 4481 | // We may not yet have parsed the package, so just see if |
| 4482 | // we still know about its settings. |
| 4483 | bp.packageSetting = mSettings.mPackages.get(bp.sourcePackage); |
| 4484 | } |
| 4485 | if (bp.packageSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4486 | Slog.w(TAG, "Removing dangling permission tree: " + bp.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4487 | + " from package " + bp.sourcePackage); |
| 4488 | it.remove(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4489 | } else if (changingPkg != null && changingPkg.equals(bp.sourcePackage)) { |
| 4490 | if (pkgInfo == null || !hasPermission(pkgInfo, bp.name)) { |
| 4491 | Slog.i(TAG, "Removing old permission tree: " + bp.name |
| 4492 | + " from package " + bp.sourcePackage); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4493 | flags |= UPDATE_PERMISSIONS_ALL; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4494 | it.remove(); |
| 4495 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4496 | } |
| 4497 | } |
| 4498 | |
| 4499 | // Make sure all dynamic permissions have been assigned to a package, |
| 4500 | // and make sure there are no dangling permissions. |
| 4501 | it = mSettings.mPermissions.values().iterator(); |
| 4502 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4503 | final BasePermission bp = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4504 | if (bp.type == BasePermission.TYPE_DYNAMIC) { |
| 4505 | if (DEBUG_SETTINGS) Log.v(TAG, "Dynamic permission: name=" |
| 4506 | + bp.name + " pkg=" + bp.sourcePackage |
| 4507 | + " info=" + bp.pendingInfo); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4508 | if (bp.packageSetting == null && bp.pendingInfo != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4509 | final BasePermission tree = findPermissionTreeLP(bp.name); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 4510 | if (tree != null && tree.perm != null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4511 | bp.packageSetting = tree.packageSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4512 | bp.perm = new PackageParser.Permission(tree.perm.owner, |
| 4513 | new PermissionInfo(bp.pendingInfo)); |
| 4514 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 4515 | bp.perm.info.name = bp.name; |
| 4516 | bp.uid = tree.uid; |
| 4517 | } |
| 4518 | } |
| 4519 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4520 | if (bp.packageSetting == null) { |
| 4521 | // We may not yet have parsed the package, so just see if |
| 4522 | // we still know about its settings. |
| 4523 | bp.packageSetting = mSettings.mPackages.get(bp.sourcePackage); |
| 4524 | } |
| 4525 | if (bp.packageSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4526 | Slog.w(TAG, "Removing dangling permission: " + bp.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4527 | + " from package " + bp.sourcePackage); |
| 4528 | it.remove(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4529 | } else if (changingPkg != null && changingPkg.equals(bp.sourcePackage)) { |
| 4530 | if (pkgInfo == null || !hasPermission(pkgInfo, bp.name)) { |
| 4531 | Slog.i(TAG, "Removing old permission: " + bp.name |
| 4532 | + " from package " + bp.sourcePackage); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4533 | flags |= UPDATE_PERMISSIONS_ALL; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4534 | it.remove(); |
| 4535 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | |
| 4539 | // Now update the permissions for all packages, in particular |
| 4540 | // replace the granted permissions of the system packages. |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4541 | if ((flags&UPDATE_PERMISSIONS_ALL) != 0) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4542 | for (PackageParser.Package pkg : mPackages.values()) { |
| 4543 | if (pkg != pkgInfo) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4544 | grantPermissionsLPw(pkg, (flags&UPDATE_PERMISSIONS_REPLACE_ALL) != 0); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4545 | } |
| 4546 | } |
| 4547 | } |
| 4548 | |
| 4549 | if (pkgInfo != null) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4550 | grantPermissionsLPw(pkgInfo, (flags&UPDATE_PERMISSIONS_REPLACE_PKG) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4551 | } |
| 4552 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4553 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4554 | private void grantPermissionsLPw(PackageParser.Package pkg, boolean replace) { |
| 4555 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4556 | if (ps == null) { |
| 4557 | return; |
| 4558 | } |
| 4559 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4560 | HashSet<String> origPermissions = gp.grantedPermissions; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4561 | boolean changedPermission = false; |
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 (replace) { |
| 4564 | ps.permissionsFixed = false; |
| 4565 | if (gp == ps) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4566 | origPermissions = new HashSet<String>(gp.grantedPermissions); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4567 | gp.grantedPermissions.clear(); |
| 4568 | gp.gids = mGlobalGids; |
| 4569 | } |
| 4570 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4571 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4572 | if (gp.gids == null) { |
| 4573 | gp.gids = mGlobalGids; |
| 4574 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4575 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4576 | final int N = pkg.requestedPermissions.size(); |
| 4577 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4578 | final String name = pkg.requestedPermissions.get(i); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4579 | //final boolean required = pkg.requestedPermssionsRequired.get(i); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4580 | final BasePermission bp = mSettings.mPermissions.get(name); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4581 | if (DEBUG_INSTALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4582 | if (gp != ps) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4583 | Log.i(TAG, "Package " + pkg.packageName + " checking " + name + ": " + bp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4584 | } |
| 4585 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4586 | if (bp != null && bp.packageSetting != null) { |
| 4587 | final String perm = bp.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4588 | boolean allowed; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4589 | boolean allowedSig = false; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4590 | final int level = bp.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE; |
| 4591 | if (level == PermissionInfo.PROTECTION_NORMAL |
| 4592 | || level == PermissionInfo.PROTECTION_DANGEROUS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4593 | allowed = true; |
Dianne Hackborn | 6e52b5d | 2010-04-05 14:33:01 -0700 | [diff] [blame] | 4594 | } else if (bp.packageSetting == null) { |
| 4595 | // This permission is invalid; skip it. |
| 4596 | allowed = false; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4597 | } else if (level == PermissionInfo.PROTECTION_SIGNATURE) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4598 | allowed = (compareSignatures( |
Dianne Hackborn | 2fe979f | 2010-10-08 15:07:17 -0700 | [diff] [blame] | 4599 | bp.packageSetting.signatures.mSignatures, pkg.mSignatures) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4600 | == PackageManager.SIGNATURE_MATCH) |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4601 | || (compareSignatures(mPlatformPackage.mSignatures, pkg.mSignatures) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4602 | == PackageManager.SIGNATURE_MATCH); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4603 | if (!allowed && (bp.protectionLevel |
| 4604 | & PermissionInfo.PROTECTION_FLAG_SYSTEM) != 0) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4605 | if (isSystemApp(pkg)) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4606 | // For updated system applications, a system permission |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4607 | // is granted only if it had been defined by the original application. |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4608 | if (isUpdatedSystemApp(pkg)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4609 | final PackageSetting sysPs = mSettings |
| 4610 | .getDisabledSystemPkgLPr(pkg.packageName); |
Dianne Hackborn | 2fe979f | 2010-10-08 15:07:17 -0700 | [diff] [blame] | 4611 | final GrantedPermissions origGp = sysPs.sharedUser != null |
| 4612 | ? sysPs.sharedUser : sysPs; |
| 4613 | if (origGp.grantedPermissions.contains(perm)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4614 | allowed = true; |
| 4615 | } else { |
| 4616 | allowed = false; |
| 4617 | } |
| 4618 | } else { |
| 4619 | allowed = true; |
| 4620 | } |
| 4621 | } |
| 4622 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 4623 | if (!allowed && (bp.protectionLevel |
| 4624 | & PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) { |
| 4625 | // For development permissions, a development permission |
| 4626 | // is granted only if it was already granted. |
| 4627 | if (origPermissions.contains(perm)) { |
| 4628 | allowed = true; |
| 4629 | } else { |
| 4630 | allowed = false; |
| 4631 | } |
| 4632 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4633 | if (allowed) { |
| 4634 | allowedSig = true; |
| 4635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4636 | } else { |
| 4637 | allowed = false; |
| 4638 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4639 | if (DEBUG_INSTALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4640 | if (gp != ps) { |
| 4641 | Log.i(TAG, "Package " + pkg.packageName + " granting " + perm); |
| 4642 | } |
| 4643 | } |
| 4644 | if (allowed) { |
| 4645 | if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0 |
| 4646 | && ps.permissionsFixed) { |
| 4647 | // If this is an existing, non-system package, then |
| 4648 | // we can't add any new permissions to it. |
Dianne Hackborn | f657b63 | 2010-03-22 18:08:07 -0700 | [diff] [blame] | 4649 | if (!allowedSig && !gp.grantedPermissions.contains(perm)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4650 | allowed = false; |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 4651 | // Except... if this is a permission that was added |
| 4652 | // to the platform (note: need to only do this when |
| 4653 | // updating the platform). |
| 4654 | final int NP = PackageParser.NEW_PERMISSIONS.length; |
| 4655 | for (int ip=0; ip<NP; ip++) { |
| 4656 | final PackageParser.NewPermissionInfo npi |
| 4657 | = PackageParser.NEW_PERMISSIONS[ip]; |
| 4658 | if (npi.name.equals(perm) |
| 4659 | && pkg.applicationInfo.targetSdkVersion < npi.sdkVersion) { |
| 4660 | allowed = true; |
Dianne Hackborn | f657b63 | 2010-03-22 18:08:07 -0700 | [diff] [blame] | 4661 | Log.i(TAG, "Auto-granting " + perm + " to old pkg " |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 4662 | + pkg.packageName); |
| 4663 | break; |
| 4664 | } |
| 4665 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4666 | } |
| 4667 | } |
| 4668 | if (allowed) { |
| 4669 | if (!gp.grantedPermissions.contains(perm)) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4670 | changedPermission = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4671 | gp.grantedPermissions.add(perm); |
| 4672 | gp.gids = appendInts(gp.gids, bp.gids); |
Dianne Hackborn | bbfd58a | 2010-03-23 11:49:59 -0700 | [diff] [blame] | 4673 | } else if (!ps.haveGids) { |
| 4674 | gp.gids = appendInts(gp.gids, bp.gids); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4675 | } |
| 4676 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4677 | Slog.w(TAG, "Not granting permission " + perm |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4678 | + " to package " + pkg.packageName |
| 4679 | + " because it was previously installed without"); |
| 4680 | } |
| 4681 | } else { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4682 | if (gp.grantedPermissions.remove(perm)) { |
| 4683 | changedPermission = true; |
| 4684 | gp.gids = removeInts(gp.gids, bp.gids); |
| 4685 | Slog.i(TAG, "Un-granting permission " + perm |
| 4686 | + " from package " + pkg.packageName |
| 4687 | + " (protectionLevel=" + bp.protectionLevel |
| 4688 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 4689 | + ")"); |
| 4690 | } else { |
| 4691 | Slog.w(TAG, "Not granting permission " + perm |
| 4692 | + " to package " + pkg.packageName |
| 4693 | + " (protectionLevel=" + bp.protectionLevel |
| 4694 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 4695 | + ")"); |
| 4696 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4697 | } |
| 4698 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4699 | Slog.w(TAG, "Unknown permission " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4700 | + " in package " + pkg.packageName); |
| 4701 | } |
| 4702 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4703 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4704 | if ((changedPermission || replace) && !ps.permissionsFixed && |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 4705 | ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) || |
| 4706 | ((ps.pkgFlags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)){ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4707 | // This is the first that we have heard about this package, so the |
| 4708 | // permissions we have now selected are fixed until explicitly |
| 4709 | // changed. |
| 4710 | ps.permissionsFixed = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4711 | } |
Dianne Hackborn | bbfd58a | 2010-03-23 11:49:59 -0700 | [diff] [blame] | 4712 | ps.haveGids = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4713 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4714 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4715 | private final class ActivityIntentResolver |
| 4716 | extends IntentResolver<PackageParser.ActivityIntentInfo, ResolveInfo> { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4717 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4718 | boolean defaultOnly, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4719 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4720 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4721 | return super.queryIntent(intent, resolvedType, defaultOnly, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4722 | } |
| 4723 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4724 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, int flags, |
| 4725 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4726 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4727 | mFlags = flags; |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4728 | return super.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4729 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4730 | } |
| 4731 | |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4732 | public List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4733 | int flags, ArrayList<PackageParser.Activity> packageActivities, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4734 | if (!sUserManager.exists(userId)) return null; |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4735 | if (packageActivities == null) { |
| 4736 | return null; |
| 4737 | } |
| 4738 | mFlags = flags; |
| 4739 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4740 | final int N = packageActivities.size(); |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame^] | 4741 | ArrayList<PackageParser.ActivityIntentInfo[]> listCut = |
| 4742 | new ArrayList<PackageParser.ActivityIntentInfo[]>(N); |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 4743 | |
| 4744 | ArrayList<PackageParser.ActivityIntentInfo> intentFilters; |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4745 | for (int i = 0; i < N; ++i) { |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 4746 | intentFilters = packageActivities.get(i).intents; |
| 4747 | if (intentFilters != null && intentFilters.size() > 0) { |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame^] | 4748 | PackageParser.ActivityIntentInfo[] array = |
| 4749 | new PackageParser.ActivityIntentInfo[intentFilters.size()]; |
| 4750 | intentFilters.toArray(array); |
| 4751 | listCut.add(array); |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 4752 | } |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4753 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4754 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4755 | } |
| 4756 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4757 | public final void addActivity(PackageParser.Activity a, String type) { |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 4758 | final boolean systemApp = isSystemApp(a.info.applicationInfo); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4759 | mActivities.put(a.getComponentName(), a); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4760 | if (DEBUG_SHOW_INFO) |
| 4761 | Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4762 | TAG, " " + type + " " + |
| 4763 | (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel : a.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4764 | if (DEBUG_SHOW_INFO) |
| 4765 | Log.v(TAG, " Class=" + a.info.name); |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4766 | final int NI = a.intents.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4767 | for (int j=0; j<NI; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4768 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 4769 | if (!systemApp && intent.getPriority() > 0 && "activity".equals(type)) { |
| 4770 | intent.setPriority(0); |
| 4771 | Log.w(TAG, "Package " + a.info.applicationInfo.packageName + " has activity " |
| 4772 | + a.className + " with priority > 0, forcing to 0"); |
| 4773 | } |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4774 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4775 | Log.v(TAG, " IntentFilter:"); |
| 4776 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4777 | } |
| 4778 | if (!intent.debugCheck()) { |
| 4779 | Log.w(TAG, "==> For Activity " + a.info.name); |
| 4780 | } |
| 4781 | addFilter(intent); |
| 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | public final void removeActivity(PackageParser.Activity a, String type) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4786 | mActivities.remove(a.getComponentName()); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4787 | if (DEBUG_SHOW_INFO) { |
| 4788 | Log.v(TAG, " " + type + " " |
| 4789 | + (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel |
| 4790 | : a.info.name) + ":"); |
| 4791 | Log.v(TAG, " Class=" + a.info.name); |
| 4792 | } |
| 4793 | final int NI = a.intents.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 4794 | for (int j=0; j<NI; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4795 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4796 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4797 | Log.v(TAG, " IntentFilter:"); |
| 4798 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4799 | } |
| 4800 | removeFilter(intent); |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | @Override |
| 4805 | protected boolean allowFilterResult( |
| 4806 | PackageParser.ActivityIntentInfo filter, List<ResolveInfo> dest) { |
| 4807 | ActivityInfo filterAi = filter.activity.info; |
| 4808 | for (int i=dest.size()-1; i>=0; i--) { |
| 4809 | ActivityInfo destAi = dest.get(i).activityInfo; |
| 4810 | if (destAi.name == filterAi.name |
| 4811 | && destAi.packageName == filterAi.packageName) { |
| 4812 | return false; |
| 4813 | } |
| 4814 | } |
| 4815 | return true; |
| 4816 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4817 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4818 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame^] | 4819 | protected ActivityIntentInfo[] newArray(int size) { |
| 4820 | return new ActivityIntentInfo[size]; |
| 4821 | } |
| 4822 | |
| 4823 | @Override |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4824 | protected boolean isFilterStopped(PackageParser.ActivityIntentInfo filter, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4825 | if (!sUserManager.exists(userId)) return true; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 4826 | PackageParser.Package p = filter.activity.owner; |
| 4827 | if (p != null) { |
| 4828 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 4829 | if (ps != null) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 4830 | // System apps are never considered stopped for purposes of |
| 4831 | // filtering, because there may be no way for the user to |
| 4832 | // actually re-launch them. |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4833 | return ps.getStopped(userId) && (ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 4834 | } |
| 4835 | } |
| 4836 | return false; |
| 4837 | } |
| 4838 | |
| 4839 | @Override |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4840 | protected String packageForFilter(PackageParser.ActivityIntentInfo info) { |
| 4841 | return info.activity.owner.packageName; |
| 4842 | } |
| 4843 | |
| 4844 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4845 | protected ResolveInfo newResult(PackageParser.ActivityIntentInfo info, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4846 | int match, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4847 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4848 | if (!mSettings.isEnabledLPr(info.activity.info, mFlags, userId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4849 | return null; |
| 4850 | } |
| 4851 | final PackageParser.Activity activity = info.activity; |
| 4852 | if (mSafeMode && (activity.info.applicationInfo.flags |
| 4853 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 4854 | return null; |
| 4855 | } |
| 4856 | final ResolveInfo res = new ResolveInfo(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4857 | PackageSetting ps = (PackageSetting) activity.owner.mExtras; |
| 4858 | res.activityInfo = PackageParser.generateActivityInfo(activity, mFlags, |
| 4859 | ps != null ? ps.getStopped(userId) : false, |
| 4860 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 4861 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4862 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 4863 | res.filter = info; |
| 4864 | } |
| 4865 | res.priority = info.getPriority(); |
| 4866 | res.preferredOrder = activity.owner.mPreferredOrder; |
| 4867 | //System.out.println("Result: " + res.activityInfo.className + |
| 4868 | // " = " + res.priority); |
| 4869 | res.match = match; |
| 4870 | res.isDefault = info.hasDefault; |
| 4871 | res.labelRes = info.labelRes; |
| 4872 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 4873 | res.icon = info.icon; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 4874 | res.system = isSystemApp(res.activityInfo.applicationInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4875 | return res; |
| 4876 | } |
| 4877 | |
| 4878 | @Override |
| 4879 | protected void sortResults(List<ResolveInfo> results) { |
| 4880 | Collections.sort(results, mResolvePrioritySorter); |
| 4881 | } |
| 4882 | |
| 4883 | @Override |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4884 | protected void dumpFilter(PrintWriter out, String prefix, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4885 | PackageParser.ActivityIntentInfo filter) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 4886 | out.print(prefix); out.print( |
| 4887 | Integer.toHexString(System.identityHashCode(filter.activity))); |
| 4888 | out.print(' '); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4889 | out.print(filter.activity.getComponentShortName()); |
| 4890 | out.print(" filter "); |
| 4891 | out.println(Integer.toHexString(System.identityHashCode(filter))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 4895 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 4896 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 4897 | // while (i.hasNext()) { |
| 4898 | // final ResolveInfo resolveInfo = i.next(); |
| 4899 | // if (isEnabledLP(resolveInfo.activityInfo)) { |
| 4900 | // retList.add(resolveInfo); |
| 4901 | // } |
| 4902 | // } |
| 4903 | // return retList; |
| 4904 | // } |
| 4905 | |
| 4906 | // Keys are String (activity class name), values are Activity. |
| 4907 | private final HashMap<ComponentName, PackageParser.Activity> mActivities |
| 4908 | = new HashMap<ComponentName, PackageParser.Activity>(); |
| 4909 | private int mFlags; |
| 4910 | } |
| 4911 | |
| 4912 | private final class ServiceIntentResolver |
| 4913 | extends IntentResolver<PackageParser.ServiceIntentInfo, ResolveInfo> { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4914 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4915 | boolean defaultOnly, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4916 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4917 | return super.queryIntent(intent, resolvedType, defaultOnly, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4918 | } |
| 4919 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4920 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, int flags, |
| 4921 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4922 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4923 | mFlags = flags; |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 4924 | return super.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4925 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4926 | } |
| 4927 | |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 4928 | public List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4929 | int flags, ArrayList<PackageParser.Service> packageServices, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4930 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4931 | if (packageServices == null) { |
| 4932 | return null; |
| 4933 | } |
| 4934 | mFlags = flags; |
| 4935 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4936 | final int N = packageServices.size(); |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame^] | 4937 | ArrayList<PackageParser.ServiceIntentInfo[]> listCut = |
| 4938 | new ArrayList<PackageParser.ServiceIntentInfo[]>(N); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4939 | |
| 4940 | ArrayList<PackageParser.ServiceIntentInfo> intentFilters; |
| 4941 | for (int i = 0; i < N; ++i) { |
| 4942 | intentFilters = packageServices.get(i).intents; |
| 4943 | if (intentFilters != null && intentFilters.size() > 0) { |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame^] | 4944 | PackageParser.ServiceIntentInfo[] array = |
| 4945 | new PackageParser.ServiceIntentInfo[intentFilters.size()]; |
| 4946 | intentFilters.toArray(array); |
| 4947 | listCut.add(array); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4948 | } |
| 4949 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4950 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 4951 | } |
| 4952 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4953 | public final void addService(PackageParser.Service s) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4954 | mServices.put(s.getComponentName(), s); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4955 | if (DEBUG_SHOW_INFO) { |
| 4956 | Log.v(TAG, " " |
| 4957 | + (s.info.nonLocalizedLabel != null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4958 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4959 | Log.v(TAG, " Class=" + s.info.name); |
| 4960 | } |
| 4961 | final int NI = s.intents.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4962 | int j; |
| 4963 | for (j=0; j<NI; j++) { |
| 4964 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4965 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4966 | Log.v(TAG, " IntentFilter:"); |
| 4967 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4968 | } |
| 4969 | if (!intent.debugCheck()) { |
| 4970 | Log.w(TAG, "==> For Service " + s.info.name); |
| 4971 | } |
| 4972 | addFilter(intent); |
| 4973 | } |
| 4974 | } |
| 4975 | |
| 4976 | public final void removeService(PackageParser.Service s) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4977 | mServices.remove(s.getComponentName()); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4978 | if (DEBUG_SHOW_INFO) { |
| 4979 | Log.v(TAG, " " + (s.info.nonLocalizedLabel != null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4980 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4981 | Log.v(TAG, " Class=" + s.info.name); |
| 4982 | } |
| 4983 | final int NI = s.intents.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4984 | int j; |
| 4985 | for (j=0; j<NI; j++) { |
| 4986 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4987 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4988 | Log.v(TAG, " IntentFilter:"); |
| 4989 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 4990 | } |
| 4991 | removeFilter(intent); |
| 4992 | } |
| 4993 | } |
| 4994 | |
| 4995 | @Override |
| 4996 | protected boolean allowFilterResult( |
| 4997 | PackageParser.ServiceIntentInfo filter, List<ResolveInfo> dest) { |
| 4998 | ServiceInfo filterSi = filter.service.info; |
| 4999 | for (int i=dest.size()-1; i>=0; i--) { |
| 5000 | ServiceInfo destAi = dest.get(i).serviceInfo; |
| 5001 | if (destAi.name == filterSi.name |
| 5002 | && destAi.packageName == filterSi.packageName) { |
| 5003 | return false; |
| 5004 | } |
| 5005 | } |
| 5006 | return true; |
| 5007 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5008 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5009 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame^] | 5010 | protected PackageParser.ServiceIntentInfo[] newArray(int size) { |
| 5011 | return new PackageParser.ServiceIntentInfo[size]; |
| 5012 | } |
| 5013 | |
| 5014 | @Override |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5015 | protected boolean isFilterStopped(PackageParser.ServiceIntentInfo filter, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5016 | if (!sUserManager.exists(userId)) return true; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5017 | PackageParser.Package p = filter.service.owner; |
| 5018 | if (p != null) { |
| 5019 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 5020 | if (ps != null) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 5021 | // System apps are never considered stopped for purposes of |
| 5022 | // filtering, because there may be no way for the user to |
| 5023 | // actually re-launch them. |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5024 | return ps.getStopped(userId) |
| 5025 | && (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5026 | } |
| 5027 | } |
| 5028 | return false; |
| 5029 | } |
| 5030 | |
| 5031 | @Override |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5032 | protected String packageForFilter(PackageParser.ServiceIntentInfo info) { |
| 5033 | return info.service.owner.packageName; |
| 5034 | } |
| 5035 | |
| 5036 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5037 | protected ResolveInfo newResult(PackageParser.ServiceIntentInfo filter, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5038 | int match, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5039 | if (!sUserManager.exists(userId)) return null; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 5040 | final PackageParser.ServiceIntentInfo info = (PackageParser.ServiceIntentInfo)filter; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5041 | if (!mSettings.isEnabledLPr(info.service.info, mFlags, userId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5042 | return null; |
| 5043 | } |
| 5044 | final PackageParser.Service service = info.service; |
| 5045 | if (mSafeMode && (service.info.applicationInfo.flags |
| 5046 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 5047 | return null; |
| 5048 | } |
| 5049 | final ResolveInfo res = new ResolveInfo(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5050 | PackageSetting ps = (PackageSetting) service.owner.mExtras; |
| 5051 | res.serviceInfo = PackageParser.generateServiceInfo(service, mFlags, |
| 5052 | ps != null ? ps.getStopped(userId) : false, |
| 5053 | ps != null ? ps.getEnabled(userId) : COMPONENT_ENABLED_STATE_DEFAULT, |
| 5054 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5055 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 5056 | res.filter = filter; |
| 5057 | } |
| 5058 | res.priority = info.getPriority(); |
| 5059 | res.preferredOrder = service.owner.mPreferredOrder; |
| 5060 | //System.out.println("Result: " + res.activityInfo.className + |
| 5061 | // " = " + res.priority); |
| 5062 | res.match = match; |
| 5063 | res.isDefault = info.hasDefault; |
| 5064 | res.labelRes = info.labelRes; |
| 5065 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 5066 | res.icon = info.icon; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 5067 | res.system = isSystemApp(res.serviceInfo.applicationInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5068 | return res; |
| 5069 | } |
| 5070 | |
| 5071 | @Override |
| 5072 | protected void sortResults(List<ResolveInfo> results) { |
| 5073 | Collections.sort(results, mResolvePrioritySorter); |
| 5074 | } |
| 5075 | |
| 5076 | @Override |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5077 | protected void dumpFilter(PrintWriter out, String prefix, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5078 | PackageParser.ServiceIntentInfo filter) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5079 | out.print(prefix); out.print( |
| 5080 | Integer.toHexString(System.identityHashCode(filter.service))); |
| 5081 | out.print(' '); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5082 | out.print(filter.service.getComponentShortName()); |
| 5083 | out.print(" filter "); |
| 5084 | out.println(Integer.toHexString(System.identityHashCode(filter))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 5088 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 5089 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 5090 | // while (i.hasNext()) { |
| 5091 | // final ResolveInfo resolveInfo = (ResolveInfo) i; |
| 5092 | // if (isEnabledLP(resolveInfo.serviceInfo)) { |
| 5093 | // retList.add(resolveInfo); |
| 5094 | // } |
| 5095 | // } |
| 5096 | // return retList; |
| 5097 | // } |
| 5098 | |
| 5099 | // Keys are String (activity class name), values are Activity. |
| 5100 | private final HashMap<ComponentName, PackageParser.Service> mServices |
| 5101 | = new HashMap<ComponentName, PackageParser.Service>(); |
| 5102 | private int mFlags; |
| 5103 | }; |
| 5104 | |
| 5105 | private static final Comparator<ResolveInfo> mResolvePrioritySorter = |
| 5106 | new Comparator<ResolveInfo>() { |
| 5107 | public int compare(ResolveInfo r1, ResolveInfo r2) { |
| 5108 | int v1 = r1.priority; |
| 5109 | int v2 = r2.priority; |
| 5110 | //System.out.println("Comparing: q1=" + q1 + " q2=" + q2); |
| 5111 | if (v1 != v2) { |
| 5112 | return (v1 > v2) ? -1 : 1; |
| 5113 | } |
| 5114 | v1 = r1.preferredOrder; |
| 5115 | v2 = r2.preferredOrder; |
| 5116 | if (v1 != v2) { |
| 5117 | return (v1 > v2) ? -1 : 1; |
| 5118 | } |
| 5119 | if (r1.isDefault != r2.isDefault) { |
| 5120 | return r1.isDefault ? -1 : 1; |
| 5121 | } |
| 5122 | v1 = r1.match; |
| 5123 | v2 = r2.match; |
| 5124 | //System.out.println("Comparing: m1=" + m1 + " m2=" + m2); |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 5125 | if (v1 != v2) { |
| 5126 | return (v1 > v2) ? -1 : 1; |
| 5127 | } |
| 5128 | if (r1.system != r2.system) { |
| 5129 | return r1.system ? -1 : 1; |
| 5130 | } |
| 5131 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5132 | } |
| 5133 | }; |
| 5134 | |
| 5135 | private static final Comparator<ProviderInfo> mProviderInitOrderSorter = |
| 5136 | new Comparator<ProviderInfo>() { |
| 5137 | public int compare(ProviderInfo p1, ProviderInfo p2) { |
| 5138 | final int v1 = p1.initOrder; |
| 5139 | final int v2 = p2.initOrder; |
| 5140 | return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0); |
| 5141 | } |
| 5142 | }; |
| 5143 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5144 | static final void sendPackageBroadcast(String action, String pkg, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5145 | Bundle extras, String targetPkg, IIntentReceiver finishedReceiver, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5146 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 5147 | if (am != null) { |
| 5148 | try { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5149 | int[] userIds = userId == UserId.USER_ALL |
| 5150 | ? sUserManager.getUserIds() |
| 5151 | : new int[] {userId}; |
| 5152 | for (int id : userIds) { |
| 5153 | final Intent intent = new Intent(action, |
| 5154 | pkg != null ? Uri.fromParts("package", pkg, null) : null); |
| 5155 | if (extras != null) { |
| 5156 | intent.putExtras(extras); |
| 5157 | } |
| 5158 | if (targetPkg != null) { |
| 5159 | intent.setPackage(targetPkg); |
| 5160 | } |
| 5161 | // Modify the UID when posting to other users |
| 5162 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 5163 | if (uid > 0 && id > 0) { |
| 5164 | uid = UserId.getUid(id, UserId.getAppId(uid)); |
| 5165 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 5166 | } |
| 5167 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 5168 | am.broadcastIntent(null, intent, null, finishedReceiver, |
| 5169 | 0, null, null, null, finishedReceiver != null, false, id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5170 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5171 | } catch (RemoteException ex) { |
| 5172 | } |
| 5173 | } |
| 5174 | } |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5175 | |
| 5176 | /** |
| 5177 | * Check if the external storage media is available. This is true if there |
| 5178 | * is a mounted external storage medium or if the external storage is |
| 5179 | * emulated. |
| 5180 | */ |
| 5181 | private boolean isExternalMediaAvailable() { |
| 5182 | return mMediaMounted || Environment.isExternalStorageEmulated(); |
| 5183 | } |
| 5184 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5185 | public String nextPackageToClean(String lastPackage) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5186 | // writer |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5187 | synchronized (mPackages) { |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5188 | if (!isExternalMediaAvailable()) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5189 | // If the external storage is no longer mounted at this point, |
| 5190 | // the caller may not have been able to delete all of this |
| 5191 | // packages files and can not delete any more. Bail. |
| 5192 | return null; |
| 5193 | } |
| 5194 | if (lastPackage != null) { |
| 5195 | mSettings.mPackagesToBeCleaned.remove(lastPackage); |
| 5196 | } |
| 5197 | return mSettings.mPackagesToBeCleaned.size() > 0 |
| 5198 | ? mSettings.mPackagesToBeCleaned.get(0) : null; |
| 5199 | } |
| 5200 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5201 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5202 | void schedulePackageCleaning(String packageName) { |
| 5203 | mHandler.sendMessage(mHandler.obtainMessage(START_CLEANING_PACKAGE, packageName)); |
| 5204 | } |
| 5205 | |
| 5206 | void startCleaningPackages() { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5207 | // reader |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5208 | synchronized (mPackages) { |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5209 | if (!isExternalMediaAvailable()) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5210 | return; |
| 5211 | } |
| 5212 | if (mSettings.mPackagesToBeCleaned.size() <= 0) { |
| 5213 | return; |
| 5214 | } |
| 5215 | } |
| 5216 | Intent intent = new Intent(PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE); |
| 5217 | intent.setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 5218 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 5219 | if (am != null) { |
| 5220 | try { |
| 5221 | am.startService(null, intent, null); |
| 5222 | } catch (RemoteException e) { |
| 5223 | } |
| 5224 | } |
| 5225 | } |
| 5226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5227 | private final class AppDirObserver extends FileObserver { |
| 5228 | public AppDirObserver(String path, int mask, boolean isrom) { |
| 5229 | super(path, mask); |
| 5230 | mRootDir = path; |
| 5231 | mIsRom = isrom; |
| 5232 | } |
| 5233 | |
| 5234 | public void onEvent(int event, String path) { |
| 5235 | String removedPackage = null; |
| 5236 | int removedUid = -1; |
| 5237 | String addedPackage = null; |
| 5238 | int addedUid = -1; |
| 5239 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5240 | // 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] | 5241 | synchronized (mInstallLock) { |
| 5242 | String fullPathStr = null; |
| 5243 | File fullPath = null; |
| 5244 | if (path != null) { |
| 5245 | fullPath = new File(mRootDir, path); |
| 5246 | fullPathStr = fullPath.getPath(); |
| 5247 | } |
| 5248 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5249 | if (DEBUG_APP_DIR_OBSERVER) |
| 5250 | Log.v(TAG, "File " + fullPathStr + " changed: " + Integer.toHexString(event)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5251 | |
| 5252 | if (!isPackageFilename(path)) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5253 | if (DEBUG_APP_DIR_OBSERVER) |
| 5254 | 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] | 5255 | return; |
| 5256 | } |
| 5257 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5258 | // Ignore packages that are being installed or |
| 5259 | // have just been installed. |
| 5260 | if (ignoreCodePath(fullPathStr)) { |
| 5261 | return; |
| 5262 | } |
| 5263 | PackageParser.Package p = null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5264 | // reader |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5265 | synchronized (mPackages) { |
| 5266 | p = mAppDirs.get(fullPathStr); |
| 5267 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5268 | if ((event&REMOVE_EVENTS) != 0) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5269 | if (p != null) { |
| 5270 | removePackageLI(p, true); |
| 5271 | removedPackage = p.applicationInfo.packageName; |
| 5272 | removedUid = p.applicationInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5273 | } |
| 5274 | } |
| 5275 | |
| 5276 | if ((event&ADD_EVENTS) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5277 | if (p == null) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5278 | p = scanPackageLI(fullPath, |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 5279 | (mIsRom ? PackageParser.PARSE_IS_SYSTEM |
| 5280 | | PackageParser.PARSE_IS_SYSTEM_DIR: 0) | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5281 | PackageParser.PARSE_CHATTY | |
| 5282 | PackageParser.PARSE_MUST_BE_APK, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 5283 | SCAN_MONITOR | SCAN_NO_PATHS | SCAN_UPDATE_TIME, |
| 5284 | System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5285 | if (p != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5286 | /* |
| 5287 | * TODO this seems dangerous as the package may have |
| 5288 | * changed since we last acquired the mPackages |
| 5289 | * lock. |
| 5290 | */ |
| 5291 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5292 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5293 | updatePermissionsLPw(p.packageName, p, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5294 | p.permissions.size() > 0 ? UPDATE_PERMISSIONS_ALL : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5295 | } |
| 5296 | addedPackage = p.applicationInfo.packageName; |
| 5297 | addedUid = p.applicationInfo.uid; |
| 5298 | } |
| 5299 | } |
| 5300 | } |
| 5301 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5302 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5303 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5304 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5305 | } |
| 5306 | } |
| 5307 | |
| 5308 | if (removedPackage != null) { |
| 5309 | Bundle extras = new Bundle(1); |
| 5310 | extras.putInt(Intent.EXTRA_UID, removedUid); |
| 5311 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, false); |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 5312 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5313 | extras, null, null, UserId.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5314 | } |
| 5315 | if (addedPackage != null) { |
| 5316 | Bundle extras = new Bundle(1); |
| 5317 | extras.putInt(Intent.EXTRA_UID, addedUid); |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 5318 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, addedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5319 | extras, null, null, UserId.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5320 | } |
| 5321 | } |
| 5322 | |
| 5323 | private final String mRootDir; |
| 5324 | private final boolean mIsRom; |
| 5325 | } |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5327 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 5328 | public void installPackage( |
| 5329 | final Uri packageURI, final IPackageInstallObserver observer, final int flags) { |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5330 | installPackage(packageURI, observer, flags, null); |
| 5331 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5332 | |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 5333 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 5334 | public void installPackage( |
| 5335 | final Uri packageURI, final IPackageInstallObserver observer, final int flags, |
| 5336 | final String installerPackageName) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5337 | installPackageWithVerification(packageURI, observer, flags, installerPackageName, null, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5338 | null, null); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5339 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5340 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5341 | @Override |
| 5342 | public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer, |
| 5343 | int flags, String installerPackageName, Uri verificationURI, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5344 | ManifestDigest manifestDigest, ContainerEncryptionParams encryptionParams) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5345 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, null); |
| 5346 | |
| 5347 | final int uid = Binder.getCallingUid(); |
| 5348 | |
| 5349 | final int filteredFlags; |
| 5350 | |
| 5351 | if (uid == Process.SHELL_UID || uid == 0) { |
| 5352 | if (DEBUG_INSTALL) { |
| 5353 | Slog.v(TAG, "Install from ADB"); |
| 5354 | } |
| 5355 | filteredFlags = flags | PackageManager.INSTALL_FROM_ADB; |
| 5356 | } else { |
| 5357 | filteredFlags = flags & ~PackageManager.INSTALL_FROM_ADB; |
| 5358 | } |
| 5359 | |
| 5360 | final Message msg = mHandler.obtainMessage(INIT_COPY); |
| 5361 | msg.obj = new InstallParams(packageURI, observer, filteredFlags, installerPackageName, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5362 | verificationURI, manifestDigest, encryptionParams); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5363 | mHandler.sendMessage(msg); |
| 5364 | } |
| 5365 | |
| 5366 | @Override |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5367 | public void verifyPendingInstall(int id, int verificationCode) throws RemoteException { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5368 | final Message msg = mHandler.obtainMessage(PACKAGE_VERIFIED); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5369 | final PackageVerificationResponse response = new PackageVerificationResponse( |
| 5370 | verificationCode, Binder.getCallingUid()); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5371 | msg.arg1 = id; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5372 | msg.obj = response; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5373 | mHandler.sendMessage(msg); |
| 5374 | } |
| 5375 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5376 | private ComponentName matchComponentForVerifier(String packageName, |
| 5377 | List<ResolveInfo> receivers) { |
| 5378 | ActivityInfo targetReceiver = null; |
| 5379 | |
| 5380 | final int NR = receivers.size(); |
| 5381 | for (int i = 0; i < NR; i++) { |
| 5382 | final ResolveInfo info = receivers.get(i); |
| 5383 | if (info.activityInfo == null) { |
| 5384 | continue; |
| 5385 | } |
| 5386 | |
| 5387 | if (packageName.equals(info.activityInfo.packageName)) { |
| 5388 | targetReceiver = info.activityInfo; |
| 5389 | break; |
| 5390 | } |
| 5391 | } |
| 5392 | |
| 5393 | if (targetReceiver == null) { |
| 5394 | return null; |
| 5395 | } |
| 5396 | |
| 5397 | return new ComponentName(targetReceiver.packageName, targetReceiver.name); |
| 5398 | } |
| 5399 | |
| 5400 | private List<ComponentName> matchVerifiers(PackageInfoLite pkgInfo, |
| 5401 | List<ResolveInfo> receivers, final PackageVerificationState verificationState) { |
| 5402 | if (pkgInfo.verifiers.length == 0) { |
| 5403 | return null; |
| 5404 | } |
| 5405 | |
| 5406 | final int N = pkgInfo.verifiers.length; |
| 5407 | final List<ComponentName> sufficientVerifiers = new ArrayList<ComponentName>(N + 1); |
| 5408 | for (int i = 0; i < N; i++) { |
| 5409 | final VerifierInfo verifierInfo = pkgInfo.verifiers[i]; |
| 5410 | |
| 5411 | final ComponentName comp = matchComponentForVerifier(verifierInfo.packageName, |
| 5412 | receivers); |
| 5413 | if (comp == null) { |
| 5414 | continue; |
| 5415 | } |
| 5416 | |
| 5417 | final int verifierUid = getUidForVerifier(verifierInfo); |
| 5418 | if (verifierUid == -1) { |
| 5419 | continue; |
| 5420 | } |
| 5421 | |
| 5422 | if (DEBUG_VERIFY) { |
| 5423 | Slog.d(TAG, "Added sufficient verifier " + verifierInfo.packageName |
| 5424 | + " with the correct signature"); |
| 5425 | } |
| 5426 | sufficientVerifiers.add(comp); |
| 5427 | verificationState.addSufficientVerifier(verifierUid); |
| 5428 | } |
| 5429 | |
| 5430 | return sufficientVerifiers; |
| 5431 | } |
| 5432 | |
| 5433 | private int getUidForVerifier(VerifierInfo verifierInfo) { |
| 5434 | synchronized (mPackages) { |
| 5435 | final PackageParser.Package pkg = mPackages.get(verifierInfo.packageName); |
| 5436 | if (pkg == null) { |
| 5437 | return -1; |
| 5438 | } else if (pkg.mSignatures.length != 1) { |
| 5439 | Slog.i(TAG, "Verifier package " + verifierInfo.packageName |
| 5440 | + " has more than one signature; ignoring"); |
| 5441 | return -1; |
| 5442 | } |
| 5443 | |
| 5444 | /* |
| 5445 | * If the public key of the package's signature does not match |
| 5446 | * our expected public key, then this is a different package and |
| 5447 | * we should skip. |
| 5448 | */ |
| 5449 | |
| 5450 | final byte[] expectedPublicKey; |
| 5451 | try { |
| 5452 | final Signature verifierSig = pkg.mSignatures[0]; |
| 5453 | final PublicKey publicKey = verifierSig.getPublicKey(); |
| 5454 | expectedPublicKey = publicKey.getEncoded(); |
| 5455 | } catch (CertificateException e) { |
| 5456 | return -1; |
| 5457 | } |
| 5458 | |
| 5459 | final byte[] actualPublicKey = verifierInfo.publicKey.getEncoded(); |
| 5460 | |
| 5461 | if (!Arrays.equals(actualPublicKey, expectedPublicKey)) { |
| 5462 | Slog.i(TAG, "Verifier package " + verifierInfo.packageName |
| 5463 | + " does not have the expected public key; ignoring"); |
| 5464 | return -1; |
| 5465 | } |
| 5466 | |
| 5467 | return pkg.applicationInfo.uid; |
| 5468 | } |
| 5469 | } |
| 5470 | |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5471 | public void finishPackageInstall(int token) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 5472 | enforceSystemOrRoot("Only the system is allowed to finish installs"); |
| 5473 | |
| 5474 | if (DEBUG_INSTALL) { |
| 5475 | Slog.v(TAG, "BM finishing package install for " + token); |
| 5476 | } |
| 5477 | |
| 5478 | final Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5479 | mHandler.sendMessage(msg); |
| 5480 | } |
| 5481 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5482 | /** |
| 5483 | * Get the verification agent timeout. |
| 5484 | * |
| 5485 | * @return verification timeout in milliseconds |
| 5486 | */ |
| 5487 | private long getVerificationTimeout() { |
| 5488 | return android.provider.Settings.Secure.getLong(mContext.getContentResolver(), |
| 5489 | android.provider.Settings.Secure.PACKAGE_VERIFIER_TIMEOUT, |
| 5490 | DEFAULT_VERIFICATION_TIMEOUT); |
| 5491 | } |
| 5492 | |
| 5493 | /** |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 5494 | * Get the default verification agent response code. |
| 5495 | * |
| 5496 | * @return default verification response code |
| 5497 | */ |
| 5498 | private int getDefaultVerificationResponse() { |
| 5499 | return android.provider.Settings.Secure.getInt(mContext.getContentResolver(), |
| 5500 | android.provider.Settings.Secure.PACKAGE_VERIFIER_DEFAULT_RESPONSE, |
| 5501 | DEFAULT_VERIFICATION_RESPONSE); |
| 5502 | } |
| 5503 | |
| 5504 | /** |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5505 | * Check whether or not package verification has been enabled. |
| 5506 | * |
| 5507 | * @return true if verification should be performed |
| 5508 | */ |
| 5509 | private boolean isVerificationEnabled() { |
| 5510 | return android.provider.Settings.Secure.getInt(mContext.getContentResolver(), |
| 5511 | android.provider.Settings.Secure.PACKAGE_VERIFIER_ENABLE, |
| 5512 | DEFAULT_VERIFY_ENABLE ? 1 : 0) == 1 ? true : false; |
| 5513 | } |
| 5514 | |
Matt Finifter | f8a98ed | 2012-05-14 15:43:34 -0700 | [diff] [blame] | 5515 | /** |
| 5516 | * Get the "allow unknown sources" setting. |
| 5517 | * |
| 5518 | * @return the current "allow unknown sources" setting |
| 5519 | */ |
| 5520 | private int getUnknownSourcesSettings() { |
| 5521 | return android.provider.Settings.Secure.getInt(mContext.getContentResolver(), |
| 5522 | android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, |
| 5523 | -1); |
| 5524 | } |
| 5525 | |
Kenny Root | f03b45f | 2011-02-23 17:25:45 -0800 | [diff] [blame] | 5526 | public void setInstallerPackageName(String targetPackage, String installerPackageName) { |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5527 | final int uid = Binder.getCallingUid(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5528 | // writer |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5529 | synchronized (mPackages) { |
| 5530 | PackageSetting targetPackageSetting = mSettings.mPackages.get(targetPackage); |
| 5531 | if (targetPackageSetting == null) { |
| 5532 | throw new IllegalArgumentException("Unknown target package: " + targetPackage); |
| 5533 | } |
| 5534 | |
| 5535 | PackageSetting installerPackageSetting; |
| 5536 | if (installerPackageName != null) { |
| 5537 | installerPackageSetting = mSettings.mPackages.get(installerPackageName); |
| 5538 | if (installerPackageSetting == null) { |
| 5539 | throw new IllegalArgumentException("Unknown installer package: " |
| 5540 | + installerPackageName); |
| 5541 | } |
| 5542 | } else { |
| 5543 | installerPackageSetting = null; |
| 5544 | } |
| 5545 | |
| 5546 | Signature[] callerSignature; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5547 | Object obj = mSettings.getUserIdLPr(uid); |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5548 | if (obj != null) { |
| 5549 | if (obj instanceof SharedUserSetting) { |
| 5550 | callerSignature = ((SharedUserSetting)obj).signatures.mSignatures; |
| 5551 | } else if (obj instanceof PackageSetting) { |
| 5552 | callerSignature = ((PackageSetting)obj).signatures.mSignatures; |
| 5553 | } else { |
| 5554 | throw new SecurityException("Bad object " + obj + " for uid " + uid); |
| 5555 | } |
| 5556 | } else { |
| 5557 | throw new SecurityException("Unknown calling uid " + uid); |
| 5558 | } |
| 5559 | |
| 5560 | // Verify: can't set installerPackageName to a package that is |
| 5561 | // not signed with the same cert as the caller. |
| 5562 | if (installerPackageSetting != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5563 | if (compareSignatures(callerSignature, |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5564 | installerPackageSetting.signatures.mSignatures) |
| 5565 | != PackageManager.SIGNATURE_MATCH) { |
| 5566 | throw new SecurityException( |
| 5567 | "Caller does not have same cert as new installer package " |
| 5568 | + installerPackageName); |
| 5569 | } |
| 5570 | } |
| 5571 | |
| 5572 | // Verify: if target already has an installer package, it must |
| 5573 | // be signed with the same cert as the caller. |
| 5574 | if (targetPackageSetting.installerPackageName != null) { |
| 5575 | PackageSetting setting = mSettings.mPackages.get( |
| 5576 | targetPackageSetting.installerPackageName); |
| 5577 | // If the currently set package isn't valid, then it's always |
| 5578 | // okay to change it. |
| 5579 | if (setting != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5580 | if (compareSignatures(callerSignature, |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 5581 | setting.signatures.mSignatures) |
| 5582 | != PackageManager.SIGNATURE_MATCH) { |
| 5583 | throw new SecurityException( |
| 5584 | "Caller does not have same cert as old installer package " |
| 5585 | + targetPackageSetting.installerPackageName); |
| 5586 | } |
| 5587 | } |
| 5588 | } |
| 5589 | |
| 5590 | // Okay! |
| 5591 | targetPackageSetting.installerPackageName = installerPackageName; |
| 5592 | scheduleWriteSettingsLocked(); |
| 5593 | } |
| 5594 | } |
| 5595 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5596 | private void processPendingInstall(final InstallArgs args, final int currentStatus) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5597 | // Queue up an async operation since the package installation may take a little while. |
| 5598 | mHandler.post(new Runnable() { |
| 5599 | public void run() { |
| 5600 | mHandler.removeCallbacks(this); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5601 | // Result object to be returned |
| 5602 | PackageInstalledInfo res = new PackageInstalledInfo(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5603 | res.returnCode = currentStatus; |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5604 | res.uid = -1; |
| 5605 | res.pkg = null; |
| 5606 | res.removedInfo = new PackageRemovedInfo(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5607 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5608 | args.doPreInstall(res.returnCode); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5609 | synchronized (mInstallLock) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5610 | installPackageLI(args, true, res); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 5611 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 5612 | args.doPostInstall(res.returnCode, res.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5613 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5614 | |
| 5615 | // A restore should be performed at this point if (a) the install |
| 5616 | // succeeded, (b) the operation is not an update, and (c) the new |
| 5617 | // package has a backupAgent defined. |
| 5618 | final boolean update = res.removedInfo.removedPackage != null; |
Christopher Tate | 59eac4b | 2010-02-19 19:25:45 -0800 | [diff] [blame] | 5619 | boolean doRestore = (!update |
| 5620 | && res.pkg != null |
| 5621 | && res.pkg.applicationInfo.backupAgentName != null); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5622 | |
| 5623 | // Set up the post-install work request bookkeeping. This will be used |
| 5624 | // and cleaned up by the post-install event handling regardless of whether |
| 5625 | // there's a restore pass performed. Token values are >= 1. |
| 5626 | int token; |
| 5627 | if (mNextInstallToken < 0) mNextInstallToken = 1; |
| 5628 | token = mNextInstallToken++; |
| 5629 | |
| 5630 | PostInstallData data = new PostInstallData(args, res); |
| 5631 | mRunningInstalls.put(token, data); |
| 5632 | if (DEBUG_INSTALL) Log.v(TAG, "+ starting restore round-trip " + token); |
| 5633 | |
| 5634 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED && doRestore) { |
| 5635 | // Pass responsibility to the Backup Manager. It will perform a |
| 5636 | // restore if appropriate, then pass responsibility back to the |
| 5637 | // Package Manager to run the post-install observer callbacks |
| 5638 | // and broadcasts. |
| 5639 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 5640 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 5641 | if (bm != null) { |
| 5642 | if (DEBUG_INSTALL) Log.v(TAG, "token " + token |
| 5643 | + " to BM for possible restore"); |
| 5644 | try { |
| 5645 | bm.restoreAtInstall(res.pkg.applicationInfo.packageName, token); |
| 5646 | } catch (RemoteException e) { |
| 5647 | // can't happen; the backup manager is local |
| 5648 | } catch (Exception e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5649 | Slog.e(TAG, "Exception trying to enqueue restore", e); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5650 | doRestore = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5651 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5652 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5653 | Slog.e(TAG, "Backup Manager not found!"); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5654 | doRestore = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 5655 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5656 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5657 | |
| 5658 | if (!doRestore) { |
| 5659 | // No restore possible, or the Backup Manager was mysteriously not |
| 5660 | // available -- just fire the post-install work request directly. |
| 5661 | if (DEBUG_INSTALL) Log.v(TAG, "No restore - queue post-install for " + token); |
| 5662 | Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
| 5663 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5664 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5665 | } |
| 5666 | }); |
| 5667 | } |
| 5668 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5669 | private abstract class HandlerParams { |
| 5670 | private static final int MAX_RETRIES = 4; |
| 5671 | |
| 5672 | /** |
| 5673 | * Number of times startCopy() has been attempted and had a non-fatal |
| 5674 | * error. |
| 5675 | */ |
| 5676 | private int mRetries = 0; |
| 5677 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5678 | final boolean startCopy() { |
| 5679 | boolean res; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5680 | try { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5681 | if (DEBUG_INSTALL) Slog.i(TAG, "startCopy"); |
| 5682 | |
| 5683 | if (++mRetries > MAX_RETRIES) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5684 | 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] | 5685 | mHandler.sendEmptyMessage(MCS_GIVE_UP); |
| 5686 | handleServiceError(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5687 | return false; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5688 | } else { |
| 5689 | handleStartCopy(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5690 | res = true; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5691 | } |
| 5692 | } catch (RemoteException e) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5693 | if (DEBUG_INSTALL) Slog.i(TAG, "Posting install MCS_RECONNECT"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5694 | mHandler.sendEmptyMessage(MCS_RECONNECT); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5695 | res = false; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5696 | } |
| 5697 | handleReturnCode(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5698 | return res; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | final void serviceError() { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5702 | if (DEBUG_INSTALL) Slog.i(TAG, "serviceError"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5703 | handleServiceError(); |
| 5704 | handleReturnCode(); |
| 5705 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5706 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5707 | abstract void handleStartCopy() throws RemoteException; |
| 5708 | abstract void handleServiceError(); |
| 5709 | abstract void handleReturnCode(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5710 | } |
| 5711 | |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5712 | class MeasureParams extends HandlerParams { |
| 5713 | private final PackageStats mStats; |
| 5714 | private boolean mSuccess; |
| 5715 | |
| 5716 | private final IPackageStatsObserver mObserver; |
| 5717 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 5718 | public MeasureParams(PackageStats stats, IPackageStatsObserver observer) { |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5719 | mObserver = observer; |
| 5720 | mStats = stats; |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5721 | } |
| 5722 | |
| 5723 | @Override |
| 5724 | void handleStartCopy() throws RemoteException { |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 5725 | synchronized (mInstallLock) { |
| 5726 | mSuccess = getPackageSizeInfoLI(mStats.packageName, mStats); |
| 5727 | } |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5728 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 5729 | final boolean mounted; |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5730 | if (Environment.isExternalStorageEmulated()) { |
| 5731 | mounted = true; |
| 5732 | } else { |
| 5733 | final String status = Environment.getExternalStorageState(); |
| 5734 | |
| 5735 | mounted = status.equals(Environment.MEDIA_MOUNTED) |
| 5736 | || status.equals(Environment.MEDIA_MOUNTED_READ_ONLY); |
| 5737 | } |
| 5738 | |
| 5739 | if (mounted) { |
| 5740 | final File externalCacheDir = Environment |
| 5741 | .getExternalStorageAppCacheDirectory(mStats.packageName); |
| 5742 | final long externalCacheSize = mContainerService |
| 5743 | .calculateDirectorySize(externalCacheDir.getPath()); |
| 5744 | mStats.externalCacheSize = externalCacheSize; |
| 5745 | |
| 5746 | final File externalDataDir = Environment |
| 5747 | .getExternalStorageAppDataDirectory(mStats.packageName); |
| 5748 | long externalDataSize = mContainerService.calculateDirectorySize(externalDataDir |
| 5749 | .getPath()); |
| 5750 | |
| 5751 | if (externalCacheDir.getParentFile().equals(externalDataDir)) { |
| 5752 | externalDataSize -= externalCacheSize; |
| 5753 | } |
| 5754 | mStats.externalDataSize = externalDataSize; |
| 5755 | |
| 5756 | final File externalMediaDir = Environment |
| 5757 | .getExternalStorageAppMediaDirectory(mStats.packageName); |
| 5758 | mStats.externalMediaSize = mContainerService |
Kenny Root | c7624d9 | 2011-02-23 16:25:25 -0800 | [diff] [blame] | 5759 | .calculateDirectorySize(externalMediaDir.getPath()); |
Kenny Root | bcd6c96 | 2011-01-17 11:21:49 -0800 | [diff] [blame] | 5760 | |
| 5761 | final File externalObbDir = Environment |
| 5762 | .getExternalStorageAppObbDirectory(mStats.packageName); |
| 5763 | mStats.externalObbSize = mContainerService.calculateDirectorySize(externalObbDir |
| 5764 | .getPath()); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 5765 | } |
| 5766 | } |
| 5767 | |
| 5768 | @Override |
| 5769 | void handleReturnCode() { |
| 5770 | if (mObserver != null) { |
| 5771 | try { |
| 5772 | mObserver.onGetStatsCompleted(mStats, mSuccess); |
| 5773 | } catch (RemoteException e) { |
| 5774 | Slog.i(TAG, "Observer no longer exists."); |
| 5775 | } |
| 5776 | } |
| 5777 | } |
| 5778 | |
| 5779 | @Override |
| 5780 | void handleServiceError() { |
| 5781 | Slog.e(TAG, "Could not measure application " + mStats.packageName |
| 5782 | + " external storage"); |
| 5783 | } |
| 5784 | } |
| 5785 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5786 | class InstallParams extends HandlerParams { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5787 | final IPackageInstallObserver observer; |
| 5788 | int flags; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5789 | |
| 5790 | private final Uri mPackageURI; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5791 | final String installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5792 | final Uri verificationURI; |
| 5793 | final ManifestDigest manifestDigest; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5794 | private InstallArgs mArgs; |
| 5795 | private int mRet; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5796 | private File mTempPackage; |
| 5797 | final ContainerEncryptionParams encryptionParams; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5798 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5799 | InstallParams(Uri packageURI, |
| 5800 | IPackageInstallObserver observer, int flags, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5801 | String installerPackageName, Uri verificationURI, ManifestDigest manifestDigest, |
| 5802 | ContainerEncryptionParams encryptionParams) { |
| 5803 | this.mPackageURI = packageURI; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5804 | this.flags = flags; |
| 5805 | this.observer = observer; |
| 5806 | this.installerPackageName = installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5807 | this.verificationURI = verificationURI; |
| 5808 | this.manifestDigest = manifestDigest; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5809 | this.encryptionParams = encryptionParams; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 5810 | } |
| 5811 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5812 | private int installLocationPolicy(PackageInfoLite pkgLite, int flags) { |
| 5813 | String packageName = pkgLite.packageName; |
| 5814 | int installLocation = pkgLite.installLocation; |
| 5815 | boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5816 | // reader |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5817 | synchronized (mPackages) { |
| 5818 | PackageParser.Package pkg = mPackages.get(packageName); |
| 5819 | if (pkg != null) { |
| 5820 | if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| 5821 | // Check for updated system application. |
| 5822 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 5823 | if (onSd) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5824 | Slog.w(TAG, "Cannot install update to system app on sdcard"); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5825 | return PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION; |
| 5826 | } |
| 5827 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 5828 | } else { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5829 | if (onSd) { |
| 5830 | // Install flag overrides everything. |
| 5831 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 5832 | } |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 5833 | // If current upgrade specifies particular preference |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5834 | if (installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
| 5835 | // Application explicitly specified internal. |
| 5836 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 5837 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { |
| 5838 | // App explictly prefers external. Let policy decide |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 5839 | } else { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5840 | // Prefer previous location |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 5841 | if (isExternal(pkg)) { |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 5842 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 5843 | } |
| 5844 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5845 | } |
| 5846 | } |
| 5847 | } else { |
| 5848 | // Invalid install. Return error code |
| 5849 | return PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS; |
| 5850 | } |
| 5851 | } |
| 5852 | } |
| 5853 | // All the special cases have been taken care of. |
| 5854 | // Return result based on recommended install location. |
| 5855 | if (onSd) { |
| 5856 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 5857 | } |
| 5858 | return pkgLite.recommendedInstallLocation; |
| 5859 | } |
| 5860 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5861 | /* |
| 5862 | * Invoke remote method to get package information and install |
| 5863 | * location values. Override install location based on default |
| 5864 | * policy if needed and then create install arguments based |
| 5865 | * on the install location. |
| 5866 | */ |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5867 | public void handleStartCopy() throws RemoteException { |
Suchi Amalapurapu | 1f9e1b4 | 2010-02-26 13:14:31 -0800 | [diff] [blame] | 5868 | int ret = PackageManager.INSTALL_SUCCEEDED; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5869 | final boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| 5870 | final boolean onInt = (flags & PackageManager.INSTALL_INTERNAL) != 0; |
| 5871 | PackageInfoLite pkgLite = null; |
| 5872 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5873 | if (onInt && onSd) { |
| 5874 | // Check if both bits are set. |
| 5875 | Slog.w(TAG, "Conflicting flags specified for installing on both internal and external"); |
| 5876 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 5877 | } else { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 5878 | final long lowThreshold; |
| 5879 | |
| 5880 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 5881 | .getService(DeviceStorageMonitorService.SERVICE); |
| 5882 | if (dsm == null) { |
| 5883 | Log.w(TAG, "Couldn't get low memory threshold; no free limit imposed"); |
| 5884 | lowThreshold = 0L; |
| 5885 | } else { |
| 5886 | lowThreshold = dsm.getMemoryLowThreshold(); |
| 5887 | } |
| 5888 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 5889 | try { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5890 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, mPackageURI, |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 5891 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 5892 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5893 | final File packageFile; |
| 5894 | if (encryptionParams != null || !"file".equals(mPackageURI.getScheme())) { |
| 5895 | ParcelFileDescriptor out = null; |
| 5896 | |
| 5897 | mTempPackage = createTempPackageFile(mDrmAppPrivateInstallDir); |
| 5898 | if (mTempPackage != null) { |
| 5899 | try { |
| 5900 | out = ParcelFileDescriptor.open(mTempPackage, |
| 5901 | ParcelFileDescriptor.MODE_READ_WRITE); |
| 5902 | } catch (FileNotFoundException e) { |
| 5903 | Slog.e(TAG, "Failed to create temporary file for : " + mPackageURI); |
| 5904 | } |
| 5905 | |
| 5906 | // Make a temporary file for decryption. |
| 5907 | ret = mContainerService |
| 5908 | .copyResource(mPackageURI, encryptionParams, out); |
| 5909 | |
| 5910 | packageFile = mTempPackage; |
| 5911 | |
| 5912 | FileUtils.setPermissions(packageFile.getAbsolutePath(), |
| 5913 | FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IROTH, |
| 5914 | -1, -1); |
| 5915 | } else { |
| 5916 | packageFile = null; |
| 5917 | } |
| 5918 | } else { |
| 5919 | packageFile = new File(mPackageURI.getPath()); |
| 5920 | } |
| 5921 | |
| 5922 | if (packageFile != null) { |
| 5923 | // Remote call to find out default install location |
| 5924 | pkgLite = mContainerService.getMinimalPackageInfo( |
| 5925 | packageFile.getAbsolutePath(), flags, lowThreshold); |
| 5926 | } |
| 5927 | } finally { |
| 5928 | mContext.revokeUriPermission(mPackageURI, |
| 5929 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 5930 | } |
| 5931 | } |
| 5932 | |
| 5933 | if (ret == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5934 | int loc = pkgLite.recommendedInstallLocation; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5935 | if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5936 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5937 | } else if (loc == PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 5938 | ret = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5939 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5940 | ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 5941 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_APK) { |
| 5942 | ret = PackageManager.INSTALL_FAILED_INVALID_APK; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5943 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_URI) { |
| 5944 | ret = PackageManager.INSTALL_FAILED_INVALID_URI; |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5945 | } else if (loc == PackageHelper.RECOMMEND_MEDIA_UNAVAILABLE) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 5946 | ret = PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5947 | } else { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 5948 | // Override with defaults if needed. |
| 5949 | loc = installLocationPolicy(pkgLite, flags); |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5950 | if (!onSd && !onInt) { |
| 5951 | // Override install location with flags |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5952 | if (loc == PackageHelper.RECOMMEND_INSTALL_EXTERNAL) { |
| 5953 | // Set the flag to install on external media. |
| 5954 | flags |= PackageManager.INSTALL_EXTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5955 | flags &= ~PackageManager.INSTALL_INTERNAL; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5956 | } else { |
| 5957 | // Make sure the flag for installing on external |
| 5958 | // media is unset |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 5959 | flags |= PackageManager.INSTALL_INTERNAL; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5960 | flags &= ~PackageManager.INSTALL_EXTERNAL; |
| 5961 | } |
| 5962 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5963 | } |
| 5964 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5965 | |
| 5966 | final InstallArgs args = createInstallArgs(this); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5967 | mArgs = args; |
| 5968 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5969 | if (ret == PackageManager.INSTALL_SUCCEEDED) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5970 | /* |
| 5971 | * Determine if we have any installed package verifiers. If we |
| 5972 | * do, then we'll defer to them to verify the packages. |
| 5973 | */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5974 | final int requiredUid = mRequiredVerifierPackage == null ? -1 |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5975 | : getPackageUid(mRequiredVerifierPackage, 0); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5976 | if (requiredUid != -1 && isVerificationEnabled()) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 5977 | final Intent verification = new Intent( |
| 5978 | Intent.ACTION_PACKAGE_NEEDS_VERIFICATION); |
| 5979 | verification.setDataAndType(getPackageUri(), PACKAGE_MIME_TYPE); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5980 | verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5981 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5982 | final List<ResolveInfo> receivers = queryIntentReceivers(verification, null, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5983 | PackageManager.GET_DISABLED_COMPONENTS, 0 /* TODO: Which userId? */); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5984 | |
| 5985 | if (DEBUG_VERIFY) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5986 | Slog.d(TAG, "Found " + receivers.size() + " verifiers for intent " |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 5987 | + verification.toString() + " with " + pkgLite.verifiers.length |
| 5988 | + " optional verifiers"); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | final int verificationId = mPendingVerificationToken++; |
| 5992 | |
| 5993 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_ID, verificationId); |
| 5994 | |
| 5995 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_INSTALLER_PACKAGE, |
| 5996 | installerPackageName); |
| 5997 | |
| 5998 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_INSTALL_FLAGS, flags); |
| 5999 | |
| 6000 | if (verificationURI != null) { |
| 6001 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_URI, |
| 6002 | verificationURI); |
| 6003 | } |
| 6004 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6005 | final PackageVerificationState verificationState = new PackageVerificationState( |
| 6006 | requiredUid, args); |
| 6007 | |
| 6008 | mPendingVerification.append(verificationId, verificationState); |
| 6009 | |
| 6010 | final List<ComponentName> sufficientVerifiers = matchVerifiers(pkgLite, |
| 6011 | receivers, verificationState); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6012 | |
| 6013 | /* |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6014 | * If any sufficient verifiers were listed in the package |
| 6015 | * manifest, attempt to ask them. |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6016 | */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6017 | if (sufficientVerifiers != null) { |
| 6018 | final int N = sufficientVerifiers.size(); |
| 6019 | if (N == 0) { |
| 6020 | Slog.i(TAG, "Additional verifiers required, but none installed."); |
| 6021 | ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
| 6022 | } else { |
| 6023 | for (int i = 0; i < N; i++) { |
| 6024 | final ComponentName verifierComponent = sufficientVerifiers.get(i); |
| 6025 | |
| 6026 | final Intent sufficientIntent = new Intent(verification); |
| 6027 | sufficientIntent.setComponent(verifierComponent); |
| 6028 | |
| 6029 | mContext.sendBroadcast(sufficientIntent); |
| 6030 | } |
| 6031 | } |
| 6032 | } |
| 6033 | |
| 6034 | final ComponentName requiredVerifierComponent = matchComponentForVerifier( |
| 6035 | mRequiredVerifierPackage, receivers); |
| 6036 | if (ret == PackageManager.INSTALL_SUCCEEDED |
| 6037 | && mRequiredVerifierPackage != null) { |
| 6038 | /* |
| 6039 | * Send the intent to the required verification agent, |
| 6040 | * but only start the verification timeout after the |
| 6041 | * target BroadcastReceivers have run. |
| 6042 | */ |
| 6043 | verification.setComponent(requiredVerifierComponent); |
| 6044 | mContext.sendOrderedBroadcast(verification, |
| 6045 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 6046 | new BroadcastReceiver() { |
| 6047 | @Override |
| 6048 | public void onReceive(Context context, Intent intent) { |
| 6049 | final Message msg = mHandler |
| 6050 | .obtainMessage(CHECK_PENDING_VERIFICATION); |
| 6051 | msg.arg1 = verificationId; |
| 6052 | mHandler.sendMessageDelayed(msg, getVerificationTimeout()); |
| 6053 | } |
| 6054 | }, null, 0, null, null); |
| 6055 | |
| 6056 | /* |
| 6057 | * We don't want the copy to proceed until verification |
| 6058 | * succeeds, so null out this field. |
| 6059 | */ |
| 6060 | mArgs = null; |
| 6061 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6062 | } else { |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6063 | /* |
| 6064 | * No package verification is enabled, so immediately start |
| 6065 | * the remote call to initiate copy using temporary file. |
| 6066 | */ |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6067 | ret = args.copyApk(mContainerService, true); |
| 6068 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6069 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6070 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6071 | mRet = ret; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6072 | } |
| 6073 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6074 | @Override |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6075 | void handleReturnCode() { |
Kenny Root | 6f89fa0 | 2010-07-30 16:33:47 -0700 | [diff] [blame] | 6076 | // If mArgs is null, then MCS couldn't be reached. When it |
| 6077 | // reconnects, it will try again to install. At that point, this |
| 6078 | // will succeed. |
| 6079 | if (mArgs != null) { |
| 6080 | processPendingInstall(mArgs, mRet); |
| 6081 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6082 | |
| 6083 | if (mTempPackage != null) { |
| 6084 | if (!mTempPackage.delete()) { |
| 6085 | Slog.w(TAG, "Couldn't delete temporary file: " |
| 6086 | + mTempPackage.getAbsolutePath()); |
| 6087 | } |
| 6088 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6089 | } |
| 6090 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6091 | @Override |
| 6092 | void handleServiceError() { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6093 | mArgs = createInstallArgs(this); |
| 6094 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6095 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6096 | |
| 6097 | public boolean isForwardLocked() { |
| 6098 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 6099 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6100 | |
| 6101 | public Uri getPackageUri() { |
| 6102 | if (mTempPackage != null) { |
| 6103 | return Uri.fromFile(mTempPackage); |
| 6104 | } else { |
| 6105 | return mPackageURI; |
| 6106 | } |
| 6107 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6108 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6109 | |
| 6110 | /* |
| 6111 | * Utility class used in movePackage api. |
| 6112 | * srcArgs and targetArgs are not set for invalid flags and make |
| 6113 | * sure to do null checks when invoking methods on them. |
| 6114 | * We probably want to return ErrorPrams for both failed installs |
| 6115 | * and moves. |
| 6116 | */ |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6117 | class MoveParams extends HandlerParams { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6118 | final IPackageMoveObserver observer; |
| 6119 | final int flags; |
| 6120 | final String packageName; |
| 6121 | final InstallArgs srcArgs; |
| 6122 | final InstallArgs targetArgs; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6123 | int uid; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6124 | int mRet; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6125 | |
| 6126 | MoveParams(InstallArgs srcArgs, IPackageMoveObserver observer, int flags, |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6127 | String packageName, String dataDir, int uid) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6128 | this.srcArgs = srcArgs; |
| 6129 | this.observer = observer; |
| 6130 | this.flags = flags; |
| 6131 | this.packageName = packageName; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6132 | this.uid = uid; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6133 | if (srcArgs != null) { |
| 6134 | Uri packageUri = Uri.fromFile(new File(srcArgs.getCodePath())); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6135 | targetArgs = createInstallArgs(packageUri, flags, packageName, dataDir); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6136 | } else { |
| 6137 | targetArgs = null; |
| 6138 | } |
| 6139 | } |
| 6140 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6141 | public void handleStartCopy() throws RemoteException { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6142 | mRet = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6143 | // Check for storage space on target medium |
| 6144 | if (!targetArgs.checkFreeStorage(mContainerService)) { |
| 6145 | Log.w(TAG, "Insufficient storage to install"); |
| 6146 | return; |
| 6147 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6148 | |
| 6149 | mRet = srcArgs.doPreCopy(); |
| 6150 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 6151 | return; |
| 6152 | } |
| 6153 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6154 | mRet = targetArgs.copyApk(mContainerService, false); |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6155 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 6156 | srcArgs.doPostCopy(uid); |
| 6157 | return; |
| 6158 | } |
| 6159 | |
| 6160 | mRet = srcArgs.doPostCopy(uid); |
| 6161 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 6162 | return; |
| 6163 | } |
| 6164 | |
| 6165 | mRet = targetArgs.doPreInstall(mRet); |
| 6166 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 6167 | return; |
| 6168 | } |
| 6169 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6170 | if (DEBUG_SD_INSTALL) { |
| 6171 | StringBuilder builder = new StringBuilder(); |
| 6172 | if (srcArgs != null) { |
| 6173 | builder.append("src: "); |
| 6174 | builder.append(srcArgs.getCodePath()); |
| 6175 | } |
| 6176 | if (targetArgs != null) { |
| 6177 | builder.append(" target : "); |
| 6178 | builder.append(targetArgs.getCodePath()); |
| 6179 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6180 | Log.i(TAG, builder.toString()); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6181 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6182 | } |
| 6183 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6184 | @Override |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6185 | void handleReturnCode() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6186 | targetArgs.doPostInstall(mRet, uid); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6187 | int currentStatus = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 6188 | if (mRet == PackageManager.INSTALL_SUCCEEDED) { |
| 6189 | currentStatus = PackageManager.MOVE_SUCCEEDED; |
| 6190 | } else if (mRet == PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE){ |
| 6191 | currentStatus = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 6192 | } |
| 6193 | processPendingMove(this, currentStatus); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6194 | } |
| 6195 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6196 | @Override |
| 6197 | void handleServiceError() { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6198 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6199 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6200 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6201 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6202 | /** |
| 6203 | * Used during creation of InstallArgs |
| 6204 | * |
| 6205 | * @param flags package installation flags |
| 6206 | * @return true if should be installed on external storage |
| 6207 | */ |
| 6208 | private static boolean installOnSd(int flags) { |
| 6209 | if ((flags & PackageManager.INSTALL_INTERNAL) != 0) { |
| 6210 | return false; |
| 6211 | } |
| 6212 | if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
| 6213 | return true; |
| 6214 | } |
| 6215 | return false; |
| 6216 | } |
| 6217 | |
| 6218 | /** |
| 6219 | * Used during creation of InstallArgs |
| 6220 | * |
| 6221 | * @param flags package installation flags |
| 6222 | * @return true if should be installed as forward locked |
| 6223 | */ |
| 6224 | private static boolean installForwardLocked(int flags) { |
| 6225 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 6226 | } |
| 6227 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6228 | private InstallArgs createInstallArgs(InstallParams params) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6229 | if (installOnSd(params.flags) || params.isForwardLocked()) { |
| 6230 | return new AsecInstallArgs(params); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6231 | } else { |
| 6232 | return new FileInstallArgs(params); |
| 6233 | } |
| 6234 | } |
| 6235 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6236 | private InstallArgs createInstallArgs(int flags, String fullCodePath, String fullResourcePath, |
| 6237 | String nativeLibraryPath) { |
Kenny Root | 84e35ea | 2012-06-06 16:05:05 -0700 | [diff] [blame] | 6238 | final boolean isInAsec; |
| 6239 | if (installOnSd(flags)) { |
| 6240 | /* Apps on SD card are always in ASEC containers. */ |
| 6241 | isInAsec = true; |
| 6242 | } else if (installForwardLocked(flags) |
| 6243 | && !fullCodePath.startsWith(mDrmAppPrivateInstallDir.getAbsolutePath())) { |
| 6244 | /* |
| 6245 | * Forward-locked apps are only in ASEC containers if they're the |
| 6246 | * new style |
| 6247 | */ |
| 6248 | isInAsec = true; |
| 6249 | } else { |
| 6250 | isInAsec = false; |
| 6251 | } |
| 6252 | |
| 6253 | if (isInAsec) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6254 | return new AsecInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath, |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6255 | installOnSd(flags), installForwardLocked(flags)); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6256 | } else { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6257 | return new FileInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6258 | } |
| 6259 | } |
| 6260 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6261 | // Used by package mover |
| 6262 | private InstallArgs createInstallArgs(Uri packageURI, int flags, String pkgName, String dataDir) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6263 | if (installOnSd(flags) || installForwardLocked(flags)) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6264 | String cid = getNextCodePath(packageURI.getPath(), pkgName, "/" |
| 6265 | + AsecInstallArgs.RES_FILE_NAME); |
| 6266 | return new AsecInstallArgs(packageURI, cid, installOnSd(flags), |
| 6267 | installForwardLocked(flags)); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6268 | } else { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6269 | return new FileInstallArgs(packageURI, pkgName, dataDir); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6270 | } |
| 6271 | } |
| 6272 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6273 | static abstract class InstallArgs { |
| 6274 | final IPackageInstallObserver observer; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6275 | // Always refers to PackageManager flags only |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6276 | final int flags; |
| 6277 | final Uri packageURI; |
| 6278 | final String installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6279 | final ManifestDigest manifestDigest; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6280 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6281 | InstallArgs(Uri packageURI, IPackageInstallObserver observer, int flags, |
| 6282 | String installerPackageName, ManifestDigest manifestDigest) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6283 | this.packageURI = packageURI; |
| 6284 | this.flags = flags; |
| 6285 | this.observer = observer; |
| 6286 | this.installerPackageName = installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6287 | this.manifestDigest = manifestDigest; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6288 | } |
| 6289 | |
| 6290 | abstract void createCopyFile(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6291 | abstract int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6292 | abstract int doPreInstall(int status); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6293 | abstract boolean doRename(int status, String pkgName, String oldCodePath); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6294 | |
| 6295 | abstract int doPostInstall(int status, int uid); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6296 | abstract String getCodePath(); |
| 6297 | abstract String getResourcePath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6298 | abstract String getNativeLibraryPath(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6299 | // Need installer lock especially for dex file removal. |
| 6300 | abstract void cleanUpResourcesLI(); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6301 | abstract boolean doPostDeleteLI(boolean delete); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6302 | abstract boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6303 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6304 | /** |
| 6305 | * Called before the source arguments are copied. This is used mostly |
| 6306 | * for MoveParams when it needs to read the source file to put it in the |
| 6307 | * destination. |
| 6308 | */ |
| 6309 | int doPreCopy() { |
| 6310 | return PackageManager.INSTALL_SUCCEEDED; |
| 6311 | } |
| 6312 | |
| 6313 | /** |
| 6314 | * Called after the source arguments are copied. This is used mostly for |
| 6315 | * MoveParams when it needs to read the source file to put it in the |
| 6316 | * destination. |
| 6317 | * |
| 6318 | * @return |
| 6319 | */ |
| 6320 | int doPostCopy(int uid) { |
| 6321 | return PackageManager.INSTALL_SUCCEEDED; |
| 6322 | } |
| 6323 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6324 | protected boolean isFwdLocked() { |
| 6325 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 6326 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6327 | } |
| 6328 | |
| 6329 | class FileInstallArgs extends InstallArgs { |
| 6330 | File installDir; |
| 6331 | String codeFileName; |
| 6332 | String resourceFileName; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6333 | String libraryPath; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6334 | boolean created = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6335 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6336 | FileInstallArgs(InstallParams params) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6337 | super(params.getPackageUri(), params.observer, params.flags, |
| 6338 | params.installerPackageName, params.manifestDigest); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6339 | } |
| 6340 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6341 | FileInstallArgs(String fullCodePath, String fullResourcePath, String nativeLibraryPath) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6342 | super(null, null, 0, null, null); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6343 | File codeFile = new File(fullCodePath); |
| 6344 | installDir = codeFile.getParentFile(); |
| 6345 | codeFileName = fullCodePath; |
| 6346 | resourceFileName = fullResourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6347 | libraryPath = nativeLibraryPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6348 | } |
| 6349 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6350 | FileInstallArgs(Uri packageURI, String pkgName, String dataDir) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6351 | super(packageURI, null, 0, null, null); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6352 | installDir = isFwdLocked() ? mDrmAppPrivateInstallDir : mAppInstallDir; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6353 | String apkName = getNextCodePath(null, pkgName, ".apk"); |
| 6354 | codeFileName = new File(installDir, apkName + ".apk").getPath(); |
| 6355 | resourceFileName = getResourcePathFromCodePath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6356 | libraryPath = new File(dataDir, LIB_DIR_NAME).getPath(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6357 | } |
| 6358 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6359 | boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 6360 | final long lowThreshold; |
| 6361 | |
| 6362 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 6363 | .getService(DeviceStorageMonitorService.SERVICE); |
| 6364 | if (dsm == null) { |
| 6365 | Log.w(TAG, "Couldn't get low memory threshold; no free limit imposed"); |
| 6366 | lowThreshold = 0L; |
| 6367 | } else { |
| 6368 | if (dsm.isMemoryLow()) { |
| 6369 | Log.w(TAG, "Memory is reported as being too low; aborting package install"); |
| 6370 | return false; |
| 6371 | } |
| 6372 | |
| 6373 | lowThreshold = dsm.getMemoryLowThreshold(); |
| 6374 | } |
| 6375 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6376 | try { |
| 6377 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6378 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6379 | return imcs.checkInternalFreeStorage(packageURI, isFwdLocked(), lowThreshold); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6380 | } finally { |
| 6381 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6382 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6383 | } |
| 6384 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6385 | String getCodePath() { |
| 6386 | return codeFileName; |
| 6387 | } |
| 6388 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6389 | void createCopyFile() { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6390 | installDir = isFwdLocked() ? mDrmAppPrivateInstallDir : mAppInstallDir; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6391 | codeFileName = createTempPackageFile(installDir).getPath(); |
| 6392 | resourceFileName = getResourcePathFromCodePath(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6393 | created = true; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6394 | } |
| 6395 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6396 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6397 | if (temp) { |
| 6398 | // Generate temp file name |
| 6399 | createCopyFile(); |
| 6400 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6401 | // Get a ParcelFileDescriptor to write to the output file |
| 6402 | File codeFile = new File(codeFileName); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6403 | if (!created) { |
| 6404 | try { |
| 6405 | codeFile.createNewFile(); |
| 6406 | // Set permissions |
| 6407 | if (!setPermissions()) { |
| 6408 | // Failed setting permissions. |
| 6409 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6410 | } |
| 6411 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6412 | Slog.w(TAG, "Failed to create file " + codeFile); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6413 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6414 | } |
| 6415 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6416 | ParcelFileDescriptor out = null; |
| 6417 | try { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6418 | out = ParcelFileDescriptor.open(codeFile, ParcelFileDescriptor.MODE_READ_WRITE); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6419 | } catch (FileNotFoundException e) { |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 6420 | Slog.e(TAG, "Failed to create file descriptor for : " + codeFileName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6421 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6422 | } |
| 6423 | // Copy the resource now |
| 6424 | int ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6425 | try { |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6426 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6427 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6428 | ret = imcs.copyResource(packageURI, null, out); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6429 | } finally { |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6430 | IoUtils.closeQuietly(out); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6431 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6432 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6433 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6434 | if (isFwdLocked()) { |
| 6435 | final File destResourceFile = new File(getResourcePath()); |
| 6436 | |
| 6437 | // Copy the public files |
| 6438 | try { |
| 6439 | PackageHelper.extractPublicFiles(codeFileName, destResourceFile); |
| 6440 | } catch (IOException e) { |
| 6441 | Slog.e(TAG, "Couldn't create a new zip file for the public parts of a" |
| 6442 | + " forward-locked app."); |
| 6443 | destResourceFile.delete(); |
| 6444 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6445 | } |
| 6446 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6447 | return ret; |
| 6448 | } |
| 6449 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6450 | int doPreInstall(int status) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6451 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6452 | cleanUp(); |
| 6453 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6454 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6455 | } |
| 6456 | |
| 6457 | boolean doRename(int status, final String pkgName, String oldCodePath) { |
| 6458 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6459 | cleanUp(); |
| 6460 | return false; |
| 6461 | } else { |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6462 | final File oldCodeFile = new File(getCodePath()); |
| 6463 | final File oldResourceFile = new File(getResourcePath()); |
| 6464 | |
| 6465 | // Rename APK file based on packageName |
| 6466 | final String apkName = getNextCodePath(oldCodePath, pkgName, ".apk"); |
| 6467 | final File newCodeFile = new File(installDir, apkName + ".apk"); |
| 6468 | if (!oldCodeFile.renameTo(newCodeFile)) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6469 | return false; |
| 6470 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6471 | codeFileName = newCodeFile.getPath(); |
| 6472 | |
| 6473 | // Rename public resource file if it's forward-locked. |
| 6474 | final File newResFile = new File(getResourcePathFromCodePath()); |
| 6475 | if (isFwdLocked() && !oldResourceFile.renameTo(newResFile)) { |
| 6476 | return false; |
| 6477 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6478 | resourceFileName = getResourcePathFromCodePath(); |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6479 | |
| 6480 | // Attempt to set permissions |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6481 | if (!setPermissions()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6482 | return false; |
| 6483 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6484 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6485 | return true; |
| 6486 | } |
| 6487 | } |
| 6488 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6489 | int doPostInstall(int status, int uid) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6490 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6491 | cleanUp(); |
| 6492 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6493 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6494 | } |
| 6495 | |
| 6496 | String getResourcePath() { |
| 6497 | return resourceFileName; |
| 6498 | } |
| 6499 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 6500 | private String getResourcePathFromCodePath() { |
| 6501 | final String codePath = getCodePath(); |
| 6502 | if (isFwdLocked()) { |
| 6503 | final StringBuilder sb = new StringBuilder(); |
| 6504 | |
| 6505 | sb.append(mAppInstallDir.getPath()); |
| 6506 | sb.append('/'); |
| 6507 | sb.append(getApkName(codePath)); |
| 6508 | sb.append(".zip"); |
| 6509 | |
| 6510 | /* |
| 6511 | * If our APK is a temporary file, mark the resource as a |
| 6512 | * temporary file as well so it can be cleaned up after |
| 6513 | * catastrophic failure. |
| 6514 | */ |
| 6515 | if (codePath.endsWith(".tmp")) { |
| 6516 | sb.append(".tmp"); |
| 6517 | } |
| 6518 | |
| 6519 | return sb.toString(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6520 | } else { |
| 6521 | return codePath; |
| 6522 | } |
| 6523 | } |
| 6524 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6525 | @Override |
| 6526 | String getNativeLibraryPath() { |
| 6527 | return libraryPath; |
| 6528 | } |
| 6529 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6530 | private boolean cleanUp() { |
| 6531 | boolean ret = true; |
| 6532 | String sourceDir = getCodePath(); |
| 6533 | String publicSourceDir = getResourcePath(); |
| 6534 | if (sourceDir != null) { |
| 6535 | File sourceFile = new File(sourceDir); |
| 6536 | if (!sourceFile.exists()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6537 | Slog.w(TAG, "Package source " + sourceDir + " does not exist."); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6538 | ret = false; |
| 6539 | } |
| 6540 | // Delete application's code and resources |
| 6541 | sourceFile.delete(); |
| 6542 | } |
| 6543 | if (publicSourceDir != null && !publicSourceDir.equals(sourceDir)) { |
| 6544 | final File publicSourceFile = new File(publicSourceDir); |
| 6545 | if (!publicSourceFile.exists()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6546 | Slog.w(TAG, "Package public source " + publicSourceFile + " does not exist."); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6547 | } |
| 6548 | if (publicSourceFile.exists()) { |
| 6549 | publicSourceFile.delete(); |
| 6550 | } |
| 6551 | } |
| 6552 | return ret; |
| 6553 | } |
| 6554 | |
| 6555 | void cleanUpResourcesLI() { |
| 6556 | String sourceDir = getCodePath(); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 6557 | if (cleanUp()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6558 | int retCode = mInstaller.rmdex(sourceDir); |
| 6559 | if (retCode < 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6560 | Slog.w(TAG, "Couldn't remove dex file for package: " |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6561 | + " at location " |
| 6562 | + sourceDir + ", retcode=" + retCode); |
| 6563 | // we don't consider this to be a failure of the core package deletion |
| 6564 | } |
| 6565 | } |
| 6566 | } |
| 6567 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6568 | private boolean setPermissions() { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6569 | // 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] | 6570 | if (!isFwdLocked()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6571 | final int filePermissions = |
| 6572 | FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP |
| 6573 | |FileUtils.S_IROTH; |
| 6574 | int retCode = FileUtils.setPermissions(getCodePath(), filePermissions, -1, -1); |
| 6575 | if (retCode != 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6576 | Slog.e(TAG, "Couldn't set new package file permissions for " + |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6577 | getCodePath() |
| 6578 | + ". The return code was: " + retCode); |
| 6579 | // TODO Define new internal error |
| 6580 | return false; |
| 6581 | } |
| 6582 | return true; |
| 6583 | } |
| 6584 | return true; |
| 6585 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6586 | |
| 6587 | boolean doPostDeleteLI(boolean delete) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6588 | // XXX err, shouldn't we respect the delete flag? |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6589 | cleanUpResourcesLI(); |
| 6590 | return true; |
| 6591 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6592 | } |
| 6593 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 6594 | private boolean isAsecExternal(String cid) { |
| 6595 | final String asecPath = PackageHelper.getSdFilesystem(cid); |
| 6596 | return !asecPath.startsWith(mAsecInternalPath); |
| 6597 | } |
| 6598 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 6599 | /** |
| 6600 | * Extract the MountService "container ID" from the full code path of an |
| 6601 | * .apk. |
| 6602 | */ |
| 6603 | static String cidFromCodePath(String fullCodePath) { |
| 6604 | int eidx = fullCodePath.lastIndexOf("/"); |
| 6605 | String subStr1 = fullCodePath.substring(0, eidx); |
| 6606 | int sidx = subStr1.lastIndexOf("/"); |
| 6607 | return subStr1.substring(sidx+1, eidx); |
| 6608 | } |
| 6609 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6610 | class AsecInstallArgs extends InstallArgs { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6611 | static final String RES_FILE_NAME = "pkg.apk"; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6612 | static final String PUBLIC_RES_FILE_NAME = "res.zip"; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6613 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6614 | String cid; |
| 6615 | String packagePath; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6616 | String resourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6617 | String libraryPath; |
| 6618 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6619 | AsecInstallArgs(InstallParams params) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6620 | super(params.getPackageUri(), params.observer, params.flags, |
| 6621 | params.installerPackageName, params.manifestDigest); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6622 | } |
| 6623 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6624 | AsecInstallArgs(String fullCodePath, String fullResourcePath, String nativeLibraryPath, |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6625 | boolean isExternal, boolean isForwardLocked) { |
| 6626 | super(null, null, (isExternal ? PackageManager.INSTALL_EXTERNAL : 0) |
| 6627 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), null, null); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6628 | // Extract cid from fullCodePath |
| 6629 | int eidx = fullCodePath.lastIndexOf("/"); |
| 6630 | String subStr1 = fullCodePath.substring(0, eidx); |
| 6631 | int sidx = subStr1.lastIndexOf("/"); |
| 6632 | cid = subStr1.substring(sidx+1, eidx); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6633 | setCachePath(subStr1); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6634 | } |
| 6635 | |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 6636 | AsecInstallArgs(String cid, boolean isForwardLocked) { |
| 6637 | super(null, null, (isAsecExternal(cid) ? PackageManager.INSTALL_EXTERNAL : 0) |
| 6638 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), null, null); |
Dianne Hackborn | aa77de1 | 2010-05-14 22:33:54 -0700 | [diff] [blame] | 6639 | this.cid = cid; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6640 | setCachePath(PackageHelper.getSdDir(cid)); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6641 | } |
| 6642 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6643 | AsecInstallArgs(Uri packageURI, String cid, boolean isExternal, boolean isForwardLocked) { |
| 6644 | super(packageURI, null, (isExternal ? PackageManager.INSTALL_EXTERNAL : 0) |
| 6645 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), null, null); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6646 | this.cid = cid; |
| 6647 | } |
| 6648 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6649 | void createCopyFile() { |
| 6650 | cid = getTempContainerId(); |
| 6651 | } |
| 6652 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6653 | boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException { |
| 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 | return imcs.checkExternalFreeStorage(packageURI, isFwdLocked()); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6658 | } finally { |
| 6659 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6660 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6661 | } |
| 6662 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6663 | private final boolean isExternal() { |
| 6664 | return (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| 6665 | } |
| 6666 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6667 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6668 | if (temp) { |
| 6669 | createCopyFile(); |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 6670 | } else { |
| 6671 | /* |
| 6672 | * Pre-emptively destroy the container since it's destroyed if |
| 6673 | * copying fails due to it existing anyway. |
| 6674 | */ |
| 6675 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6676 | } |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6677 | |
| 6678 | final String newCachePath; |
| 6679 | try { |
| 6680 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 6681 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6682 | newCachePath = imcs.copyResourceToContainer(packageURI, cid, getEncryptKey(), |
| 6683 | RES_FILE_NAME, PUBLIC_RES_FILE_NAME, isExternal(), isFwdLocked()); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6684 | } finally { |
| 6685 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6686 | } |
| 6687 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6688 | if (newCachePath != null) { |
| 6689 | setCachePath(newCachePath); |
| 6690 | return PackageManager.INSTALL_SUCCEEDED; |
| 6691 | } else { |
| 6692 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6693 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6694 | } |
| 6695 | |
| 6696 | @Override |
| 6697 | String getCodePath() { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6698 | return packagePath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | @Override |
| 6702 | String getResourcePath() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6703 | return resourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6704 | } |
| 6705 | |
| 6706 | @Override |
| 6707 | String getNativeLibraryPath() { |
| 6708 | return libraryPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6709 | } |
| 6710 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6711 | int doPreInstall(int status) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6712 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6713 | // Destroy container |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6714 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6715 | } else { |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6716 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6717 | if (!mounted) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6718 | String newCachePath = PackageHelper.mountSdDir(cid, getEncryptKey(), |
| 6719 | Process.SYSTEM_UID); |
| 6720 | if (newCachePath != null) { |
| 6721 | setCachePath(newCachePath); |
| 6722 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6723 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6724 | } |
| 6725 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6726 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6727 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6728 | } |
| 6729 | |
| 6730 | boolean doRename(int status, final String pkgName, |
| 6731 | String oldCodePath) { |
| 6732 | String newCacheId = getNextCodePath(oldCodePath, pkgName, "/" + RES_FILE_NAME); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6733 | String newCachePath = null; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6734 | if (PackageHelper.isContainerMounted(cid)) { |
| 6735 | // Unmount the container |
| 6736 | if (!PackageHelper.unMountSdDir(cid)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6737 | Slog.i(TAG, "Failed to unmount " + cid + " before renaming"); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6738 | return false; |
| 6739 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6740 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6741 | if (!PackageHelper.renameSdDir(cid, newCacheId)) { |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 6742 | Slog.e(TAG, "Failed to rename " + cid + " to " + newCacheId + |
| 6743 | " which might be stale. Will try to clean up."); |
| 6744 | // Clean up the stale container and proceed to recreate. |
| 6745 | if (!PackageHelper.destroySdDir(newCacheId)) { |
| 6746 | Slog.e(TAG, "Very strange. Cannot clean up stale container " + newCacheId); |
| 6747 | return false; |
| 6748 | } |
| 6749 | // Successfully cleaned up stale container. Try to rename again. |
| 6750 | if (!PackageHelper.renameSdDir(cid, newCacheId)) { |
| 6751 | Slog.e(TAG, "Failed to rename " + cid + " to " + newCacheId |
| 6752 | + " inspite of cleaning it up."); |
| 6753 | return false; |
| 6754 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6755 | } |
| 6756 | if (!PackageHelper.isContainerMounted(newCacheId)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6757 | Slog.w(TAG, "Mounting container " + newCacheId); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6758 | newCachePath = PackageHelper.mountSdDir(newCacheId, |
| 6759 | getEncryptKey(), Process.SYSTEM_UID); |
| 6760 | } else { |
| 6761 | newCachePath = PackageHelper.getSdDir(newCacheId); |
| 6762 | } |
| 6763 | if (newCachePath == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6764 | Slog.w(TAG, "Failed to get cache path for " + newCacheId); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6765 | return false; |
| 6766 | } |
| 6767 | Log.i(TAG, "Succesfully renamed " + cid + |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6768 | " to " + newCacheId + |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6769 | " at new path: " + newCachePath); |
| 6770 | cid = newCacheId; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6771 | setCachePath(newCachePath); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6772 | return true; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6773 | } |
| 6774 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6775 | private void setCachePath(String newCachePath) { |
| 6776 | File cachePath = new File(newCachePath); |
| 6777 | libraryPath = new File(cachePath, LIB_DIR_NAME).getPath(); |
| 6778 | packagePath = new File(cachePath, RES_FILE_NAME).getPath(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6779 | |
| 6780 | if (isFwdLocked()) { |
| 6781 | resourcePath = new File(cachePath, PUBLIC_RES_FILE_NAME).getPath(); |
| 6782 | } else { |
| 6783 | resourcePath = packagePath; |
| 6784 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6785 | } |
| 6786 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6787 | int doPostInstall(int status, int uid) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6788 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 6789 | cleanUp(); |
| 6790 | } else { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6791 | final int groupOwner; |
| 6792 | final String protectedFile; |
| 6793 | if (isFwdLocked()) { |
| 6794 | groupOwner = uid; |
| 6795 | protectedFile = RES_FILE_NAME; |
| 6796 | } else { |
| 6797 | groupOwner = -1; |
| 6798 | protectedFile = null; |
| 6799 | } |
| 6800 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6801 | if (uid < Process.FIRST_APPLICATION_UID |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6802 | || !PackageHelper.fixSdPermissions(cid, groupOwner, protectedFile)) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6803 | Slog.e(TAG, "Failed to finalize " + cid); |
| 6804 | PackageHelper.destroySdDir(cid); |
| 6805 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6806 | } |
| 6807 | |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6808 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6809 | if (!mounted) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6810 | PackageHelper.mountSdDir(cid, getEncryptKey(), Process.myUid()); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6811 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6812 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6813 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6814 | } |
| 6815 | |
| 6816 | private void cleanUp() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6817 | if (DEBUG_SD_INSTALL) Slog.i(TAG, "cleanUp"); |
| 6818 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6819 | // Destroy secure container |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6820 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6821 | } |
| 6822 | |
| 6823 | void cleanUpResourcesLI() { |
| 6824 | String sourceFile = getCodePath(); |
| 6825 | // Remove dex file |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 6826 | int retCode = mInstaller.rmdex(sourceFile); |
| 6827 | if (retCode < 0) { |
| 6828 | Slog.w(TAG, "Couldn't remove dex file for package: " |
| 6829 | + " at location " |
| 6830 | + sourceFile.toString() + ", retcode=" + retCode); |
| 6831 | // 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] | 6832 | } |
| 6833 | cleanUp(); |
| 6834 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6835 | |
| 6836 | boolean matchContainer(String app) { |
| 6837 | if (cid.startsWith(app)) { |
| 6838 | return true; |
| 6839 | } |
| 6840 | return false; |
| 6841 | } |
| 6842 | |
| 6843 | String getPackageName() { |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 6844 | return getAsecPackageName(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6845 | } |
| 6846 | |
| 6847 | boolean doPostDeleteLI(boolean delete) { |
| 6848 | boolean ret = false; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6849 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6850 | if (mounted) { |
| 6851 | // Unmount first |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 6852 | ret = PackageHelper.unMountSdDir(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6853 | } |
| 6854 | if (ret && delete) { |
| 6855 | cleanUpResourcesLI(); |
| 6856 | } |
| 6857 | return ret; |
| 6858 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6859 | |
| 6860 | @Override |
| 6861 | int doPreCopy() { |
| 6862 | if (isFwdLocked()) { |
| 6863 | if (!PackageHelper.fixSdPermissions(cid, |
| 6864 | getPackageUid(DEFAULT_CONTAINER_PACKAGE, 0), RES_FILE_NAME)) { |
| 6865 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6866 | } |
| 6867 | } |
| 6868 | |
| 6869 | return PackageManager.INSTALL_SUCCEEDED; |
| 6870 | } |
| 6871 | |
| 6872 | @Override |
| 6873 | int doPostCopy(int uid) { |
| 6874 | if (isFwdLocked()) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 6875 | if (uid < Process.FIRST_APPLICATION_UID |
| 6876 | || !PackageHelper.fixSdPermissions(cid, uid, RES_FILE_NAME)) { |
| 6877 | Slog.e(TAG, "Failed to finalize " + cid); |
| 6878 | PackageHelper.destroySdDir(cid); |
| 6879 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 6880 | } |
| 6881 | } |
| 6882 | |
| 6883 | return PackageManager.INSTALL_SUCCEEDED; |
| 6884 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6885 | }; |
| 6886 | |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 6887 | static String getAsecPackageName(String packageCid) { |
| 6888 | int idx = packageCid.lastIndexOf("-"); |
| 6889 | if (idx == -1) { |
| 6890 | return packageCid; |
| 6891 | } |
| 6892 | return packageCid.substring(0, idx); |
| 6893 | } |
| 6894 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6895 | // Utility method used to create code paths based on package name and available index. |
| 6896 | private static String getNextCodePath(String oldCodePath, String prefix, String suffix) { |
| 6897 | String idxStr = ""; |
| 6898 | int idx = 1; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6899 | // Fall back to default value of idx=1 if prefix is not |
| 6900 | // part of oldCodePath |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6901 | if (oldCodePath != null) { |
Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 6902 | String subStr = oldCodePath; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6903 | // Drop the suffix right away |
Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 6904 | if (subStr.endsWith(suffix)) { |
| 6905 | subStr = subStr.substring(0, subStr.length() - suffix.length()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6906 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6907 | // If oldCodePath already contains prefix find out the |
| 6908 | // ending index to either increment or decrement. |
| 6909 | int sidx = subStr.lastIndexOf(prefix); |
| 6910 | if (sidx != -1) { |
| 6911 | subStr = subStr.substring(sidx + prefix.length()); |
| 6912 | if (subStr != null) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6913 | if (subStr.startsWith(INSTALL_PACKAGE_SUFFIX)) { |
| 6914 | subStr = subStr.substring(INSTALL_PACKAGE_SUFFIX.length()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6915 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 6916 | try { |
| 6917 | idx = Integer.parseInt(subStr); |
| 6918 | if (idx <= 1) { |
| 6919 | idx++; |
| 6920 | } else { |
| 6921 | idx--; |
| 6922 | } |
| 6923 | } catch(NumberFormatException e) { |
| 6924 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6925 | } |
| 6926 | } |
| 6927 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6928 | idxStr = INSTALL_PACKAGE_SUFFIX + Integer.toString(idx); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6929 | return prefix + idxStr; |
| 6930 | } |
| 6931 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6932 | // Utility method used to ignore ADD/REMOVE events |
| 6933 | // by directory observer. |
| 6934 | private static boolean ignoreCodePath(String fullPathStr) { |
| 6935 | String apkName = getApkName(fullPathStr); |
| 6936 | int idx = apkName.lastIndexOf(INSTALL_PACKAGE_SUFFIX); |
| 6937 | if (idx != -1 && ((idx+1) < apkName.length())) { |
| 6938 | // Make sure the package ends with a numeral |
| 6939 | String version = apkName.substring(idx+1); |
| 6940 | try { |
| 6941 | Integer.parseInt(version); |
| 6942 | return true; |
| 6943 | } catch (NumberFormatException e) {} |
| 6944 | } |
| 6945 | return false; |
| 6946 | } |
| 6947 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6948 | // Utility method that returns the relative package path with respect |
| 6949 | // to the installation directory. Like say for /data/data/com.test-1.apk |
| 6950 | // string com.test-1 is returned. |
| 6951 | static String getApkName(String codePath) { |
| 6952 | if (codePath == null) { |
| 6953 | return null; |
| 6954 | } |
| 6955 | int sidx = codePath.lastIndexOf("/"); |
| 6956 | int eidx = codePath.lastIndexOf("."); |
| 6957 | if (eidx == -1) { |
| 6958 | eidx = codePath.length(); |
| 6959 | } else if (eidx == 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6960 | Slog.w(TAG, " Invalid code path, "+ codePath + " Not a valid apk name"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6961 | return null; |
| 6962 | } |
| 6963 | return codePath.substring(sidx+1, eidx); |
| 6964 | } |
| 6965 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6966 | class PackageInstalledInfo { |
| 6967 | String name; |
| 6968 | int uid; |
| 6969 | PackageParser.Package pkg; |
| 6970 | int returnCode; |
| 6971 | PackageRemovedInfo removedInfo; |
| 6972 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6973 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6974 | /* |
| 6975 | * Install a non-existing package. |
| 6976 | */ |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6977 | private void installNewPackageLI(PackageParser.Package pkg, |
| 6978 | int parseFlags, |
| 6979 | int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6980 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6981 | // Remember this for later, in case we need to rollback this install |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6982 | String pkgName = pkg.packageName; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 6983 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 6984 | boolean dataDirExists = getDataPathForPackage(pkg.packageName, 0).exists(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6985 | res.name = pkgName; |
| 6986 | synchronized(mPackages) { |
Dianne Hackborn | e259bc7 | 2010-03-30 19:24:44 -0700 | [diff] [blame] | 6987 | if (mSettings.mRenamedPackages.containsKey(pkgName)) { |
| 6988 | // A package with the same name is already installed, though |
| 6989 | // it has been renamed to an older name. The package we |
| 6990 | // are trying to install should be installed as an update to |
| 6991 | // the existing one, but that has not been requested, so bail. |
| 6992 | Slog.w(TAG, "Attempt to re-install " + pkgName |
| 6993 | + " without first uninstalling package running as " |
| 6994 | + mSettings.mRenamedPackages.get(pkgName)); |
| 6995 | res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 6996 | return; |
| 6997 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 6998 | if (mPackages.containsKey(pkgName) || mAppDirs.containsKey(pkg.mPath)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6999 | // Don't allow installation over an existing package with the same name. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7000 | Slog.w(TAG, "Attempt to re-install " + pkgName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7001 | + " without first uninstalling."); |
| 7002 | res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 7003 | return; |
| 7004 | } |
| 7005 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7006 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7007 | PackageParser.Package newPackage = scanPackageLI(pkg, parseFlags, scanMode, |
| 7008 | System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7009 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7010 | 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] | 7011 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 7012 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 7013 | } |
| 7014 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7015 | updateSettingsLI(newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7016 | installerPackageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7017 | res); |
| 7018 | // delete the partially installed application. the data directory will have to be |
| 7019 | // restored if it was already existing |
| 7020 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
| 7021 | // remove package from internal structures. Note that we want deletePackageX to |
| 7022 | // delete the package data and cache directories that it created in |
| 7023 | // scanPackageLocked, unless those directories existed before we even tried to |
| 7024 | // install. |
| 7025 | deletePackageLI( |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7026 | pkgName, false, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7027 | dataDirExists ? PackageManager.DONT_DELETE_DATA : 0, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7028 | res.removedInfo, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7029 | } |
| 7030 | } |
| 7031 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7032 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7033 | private void replacePackageLI(PackageParser.Package pkg, |
| 7034 | int parseFlags, |
| 7035 | int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7036 | String installerPackageName, PackageInstalledInfo res) { |
| 7037 | |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7038 | PackageParser.Package oldPackage; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7039 | String pkgName = pkg.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7040 | // First find the old package info and check signatures |
| 7041 | synchronized(mPackages) { |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7042 | oldPackage = mPackages.get(pkgName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7043 | if (compareSignatures(oldPackage.mSignatures, pkg.mSignatures) |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 7044 | != PackageManager.SIGNATURE_MATCH) { |
Kenny Root | 8c11f1d | 2012-04-14 12:10:38 -0700 | [diff] [blame] | 7045 | 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] | 7046 | res.returnCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 7047 | return; |
| 7048 | } |
| 7049 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7050 | boolean sysPkg = (isSystemApp(oldPackage)); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7051 | if (sysPkg) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7052 | replaceSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7053 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7054 | replaceNonSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7055 | } |
| 7056 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7057 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7058 | private void replaceNonSystemPackageLI(PackageParser.Package deletedPackage, |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7059 | PackageParser.Package pkg, |
| 7060 | int parseFlags, int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7061 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7062 | PackageParser.Package newPackage = null; |
| 7063 | String pkgName = deletedPackage.packageName; |
| 7064 | boolean deletedPkg = true; |
| 7065 | boolean updatedSettings = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7066 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7067 | long origUpdateTime; |
| 7068 | if (pkg.mExtras != null) { |
| 7069 | origUpdateTime = ((PackageSetting)pkg.mExtras).lastUpdateTime; |
| 7070 | } else { |
| 7071 | origUpdateTime = 0; |
| 7072 | } |
| 7073 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7074 | // First delete the existing package while retaining the data directory |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7075 | if (!deletePackageLI(pkgName, true, PackageManager.DONT_DELETE_DATA, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7076 | res.removedInfo, true)) { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7077 | // If the existing package wasn't successfully deleted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7078 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 7079 | deletedPkg = false; |
| 7080 | } else { |
| 7081 | // Successfully deleted the old package. Now proceed with re-installation |
| 7082 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7083 | newPackage = scanPackageLI(pkg, parseFlags, scanMode | SCAN_UPDATE_TIME, |
| 7084 | System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7085 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7086 | 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] | 7087 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 7088 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
Suchi Amalapurapu | 110fea7 | 2010-01-14 17:50:23 -0800 | [diff] [blame] | 7089 | } |
| 7090 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7091 | updateSettingsLI(newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7092 | installerPackageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7093 | res); |
| 7094 | updatedSettings = true; |
| 7095 | } |
| 7096 | } |
| 7097 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7098 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7099 | // remove package from internal structures. Note that we want deletePackageX to |
| 7100 | // delete the package data and cache directories that it created in |
| 7101 | // scanPackageLocked, unless those directories existed before we even tried to |
| 7102 | // install. |
| 7103 | if(updatedSettings) { |
| 7104 | deletePackageLI( |
| 7105 | pkgName, true, |
| 7106 | PackageManager.DONT_DELETE_DATA, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7107 | res.removedInfo, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7108 | } |
| 7109 | // Since we failed to install the new package we need to restore the old |
| 7110 | // package that we deleted. |
| 7111 | if(deletedPkg) { |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 7112 | File restoreFile = new File(deletedPackage.mPath); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7113 | // Parse old package |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7114 | boolean oldOnSd = isExternal(deletedPackage); |
| 7115 | int oldParseFlags = mDefParseFlags | PackageParser.PARSE_CHATTY | |
| 7116 | (isForwardLocked(deletedPackage) ? PackageParser.PARSE_FORWARD_LOCK : 0) | |
| 7117 | (oldOnSd ? PackageParser.PARSE_ON_SDCARD : 0); |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7118 | int oldScanMode = (oldOnSd ? 0 : SCAN_MONITOR) | SCAN_UPDATE_SIGNATURE |
| 7119 | | SCAN_UPDATE_TIME; |
| 7120 | if (scanPackageLI(restoreFile, oldParseFlags, oldScanMode, |
| 7121 | origUpdateTime) == null) { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7122 | Slog.e(TAG, "Failed to restore package : " + pkgName + " after failed upgrade"); |
| 7123 | return; |
| 7124 | } |
| 7125 | // Restore of old package succeeded. Update permissions. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7126 | // writer |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7127 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7128 | updatePermissionsLPw(deletedPackage.packageName, deletedPackage, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7129 | UPDATE_PERMISSIONS_ALL); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7130 | // can downgrade to reader |
| 7131 | mSettings.writeLPr(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7132 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7133 | 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] | 7134 | } |
| 7135 | } |
| 7136 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7138 | private void replaceSystemPackageLI(PackageParser.Package deletedPackage, |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7139 | PackageParser.Package pkg, |
| 7140 | int parseFlags, int scanMode, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7141 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7142 | PackageParser.Package newPackage = null; |
| 7143 | boolean updatedSettings = false; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7144 | parseFlags |= PackageManager.INSTALL_REPLACE_EXISTING | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7145 | PackageParser.PARSE_IS_SYSTEM; |
| 7146 | String packageName = deletedPackage.packageName; |
| 7147 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 7148 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7149 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7150 | return; |
| 7151 | } |
| 7152 | PackageParser.Package oldPkg; |
| 7153 | PackageSetting oldPkgSetting; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7154 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7155 | synchronized (mPackages) { |
| 7156 | oldPkg = mPackages.get(packageName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7157 | oldPkgSetting = mSettings.mPackages.get(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7158 | if((oldPkg == null) || (oldPkg.applicationInfo == null) || |
| 7159 | (oldPkgSetting == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7160 | Slog.w(TAG, "Couldn't find package:"+packageName+" information"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7161 | return; |
| 7162 | } |
| 7163 | } |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7164 | |
| 7165 | killApplication(packageName, oldPkg.applicationInfo.uid); |
| 7166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7167 | res.removedInfo.uid = oldPkg.applicationInfo.uid; |
| 7168 | res.removedInfo.removedPackage = packageName; |
| 7169 | // Remove existing system package |
| 7170 | removePackageLI(oldPkg, true); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7171 | // writer |
Dianne Hackborn | 3ec27e0 | 2010-12-16 12:41:56 -0800 | [diff] [blame] | 7172 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7173 | if (!mSettings.disableSystemPackageLPw(packageName) && deletedPackage != null) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7174 | // We didn't need to disable the .apk as a current system package, |
| 7175 | // which means we are replacing another update that is already |
| 7176 | // 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] | 7177 | res.removedInfo.args = createInstallArgs(0, |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7178 | deletedPackage.applicationInfo.sourceDir, |
| 7179 | deletedPackage.applicationInfo.publicSourceDir, |
| 7180 | deletedPackage.applicationInfo.nativeLibraryDir); |
| 7181 | } else { |
| 7182 | res.removedInfo.args = null; |
| 7183 | } |
Dianne Hackborn | 3ec27e0 | 2010-12-16 12:41:56 -0800 | [diff] [blame] | 7184 | } |
| 7185 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7186 | // Successfully disabled the old package. Now proceed with re-installation |
| 7187 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
| 7188 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7189 | newPackage = scanPackageLI(pkg, parseFlags, scanMode, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7190 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7191 | 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] | 7192 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 7193 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 7194 | } |
| 7195 | } else { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7196 | if (newPackage.mExtras != null) { |
| 7197 | final PackageSetting newPkgSetting = (PackageSetting)newPackage.mExtras; |
| 7198 | newPkgSetting.firstInstallTime = oldPkgSetting.firstInstallTime; |
| 7199 | newPkgSetting.lastUpdateTime = System.currentTimeMillis(); |
| 7200 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7201 | updateSettingsLI(newPackage, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7202 | updatedSettings = true; |
| 7203 | } |
| 7204 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7205 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7206 | // Re installation failed. Restore old information |
| 7207 | // Remove new pkg information |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 7208 | if (newPackage != null) { |
| 7209 | removePackageLI(newPackage, true); |
| 7210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7211 | // Add back the old system package |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7212 | scanPackageLI(oldPkg, parseFlags, SCAN_MONITOR | SCAN_UPDATE_SIGNATURE, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7213 | // Restore the old system information in Settings |
| 7214 | synchronized(mPackages) { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7215 | if (updatedSettings) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7216 | mSettings.enableSystemPackageLPw(packageName); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7217 | mSettings.setInstallerPackageName(packageName, |
| 7218 | oldPkgSetting.installerPackageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7219 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7220 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7221 | } |
| 7222 | } |
| 7223 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7224 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7225 | // Utility method used to move dex files during install. |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7226 | private int moveDexFilesLI(PackageParser.Package newPackage) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7227 | int retCode; |
| 7228 | if ((newPackage.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) { |
| 7229 | retCode = mInstaller.movedex(newPackage.mScanPath, newPackage.mPath); |
| 7230 | if (retCode != 0) { |
Kenny Root | e2f7417 | 2010-08-04 13:35:33 -0700 | [diff] [blame] | 7231 | if (mNoDexOpt) { |
| 7232 | /* |
| 7233 | * If we're in an engineering build, programs are lazily run |
| 7234 | * through dexopt. If the .dex file doesn't exist yet, it |
| 7235 | * will be created when the program is run next. |
| 7236 | */ |
| 7237 | Slog.i(TAG, "dex file doesn't exist, skipping move: " + newPackage.mPath); |
| 7238 | } else { |
| 7239 | Slog.e(TAG, "Couldn't rename dex file: " + newPackage.mPath); |
| 7240 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7241 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7242 | } |
| 7243 | } |
| 7244 | return PackageManager.INSTALL_SUCCEEDED; |
| 7245 | } |
| 7246 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7247 | private void updateSettingsLI(PackageParser.Package newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7248 | String installerPackageName, PackageInstalledInfo res) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7249 | String pkgName = newPackage.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7250 | synchronized (mPackages) { |
| 7251 | //write settings. the installStatus will be incomplete at this stage. |
| 7252 | //note that the new package setting would have already been |
| 7253 | //added to mPackages. It hasn't been persisted yet. |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 7254 | mSettings.setInstallStatus(pkgName, PackageSettingBase.PKG_INSTALL_INCOMPLETE); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7255 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7256 | } |
| 7257 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7258 | if ((res.returnCode = moveDexFilesLI(newPackage)) |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7259 | != PackageManager.INSTALL_SUCCEEDED) { |
| 7260 | // Discontinue if moving dex files failed. |
| 7261 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7262 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7263 | |
| 7264 | Log.d(TAG, "New package installed in " + newPackage.mPath); |
| 7265 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7266 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7267 | updatePermissionsLPw(newPackage.packageName, newPackage, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7268 | UPDATE_PERMISSIONS_REPLACE_PKG | (newPackage.permissions.size() > 0 |
| 7269 | ? UPDATE_PERMISSIONS_ALL : 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7270 | res.name = pkgName; |
| 7271 | res.uid = newPackage.applicationInfo.uid; |
| 7272 | res.pkg = newPackage; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 7273 | mSettings.setInstallStatus(pkgName, PackageSettingBase.PKG_INSTALL_COMPLETE); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 7274 | mSettings.setInstallerPackageName(pkgName, installerPackageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7275 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 7276 | //to update install status |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7277 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7278 | } |
| 7279 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7280 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7281 | private void installPackageLI(InstallArgs args, |
| 7282 | boolean newInstall, PackageInstalledInfo res) { |
| 7283 | int pFlags = args.flags; |
| 7284 | String installerPackageName = args.installerPackageName; |
| 7285 | File tmpPackageFile = new File(args.getCodePath()); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7286 | boolean forwardLocked = ((pFlags & PackageManager.INSTALL_FORWARD_LOCK) != 0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 7287 | boolean onSd = ((pFlags & PackageManager.INSTALL_EXTERNAL) != 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7288 | boolean replace = false; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7289 | int scanMode = (onSd ? 0 : SCAN_MONITOR) | SCAN_FORCE_DEX | SCAN_UPDATE_SIGNATURE |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7290 | | (newInstall ? SCAN_NEW_INSTALL : 0); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 7291 | // Result object to be returned |
| 7292 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 7293 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7294 | // Retrieve PackageSettings and parse package |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7295 | int parseFlags = mDefParseFlags | PackageParser.PARSE_CHATTY |
| 7296 | | (forwardLocked ? PackageParser.PARSE_FORWARD_LOCK : 0) |
| 7297 | | (onSd ? PackageParser.PARSE_ON_SDCARD : 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7298 | PackageParser pp = new PackageParser(tmpPackageFile.getPath()); |
| 7299 | pp.setSeparateProcesses(mSeparateProcesses); |
| 7300 | final PackageParser.Package pkg = pp.parsePackage(tmpPackageFile, |
| 7301 | null, mMetrics, parseFlags); |
| 7302 | if (pkg == null) { |
| 7303 | res.returnCode = pp.getParseError(); |
| 7304 | return; |
| 7305 | } |
| 7306 | String pkgName = res.name = pkg.packageName; |
| 7307 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_TEST_ONLY) != 0) { |
| 7308 | if ((pFlags&PackageManager.INSTALL_ALLOW_TEST) == 0) { |
| 7309 | res.returnCode = PackageManager.INSTALL_FAILED_TEST_ONLY; |
| 7310 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7311 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7312 | } |
| 7313 | if (GET_CERTIFICATES && !pp.collectCertificates(pkg, parseFlags)) { |
| 7314 | res.returnCode = pp.getParseError(); |
| 7315 | return; |
| 7316 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7317 | |
| 7318 | /* If the installer passed in a manifest digest, compare it now. */ |
| 7319 | if (args.manifestDigest != null) { |
| 7320 | if (DEBUG_INSTALL) { |
| 7321 | final String parsedManifest = pkg.manifestDigest == null ? "null" |
| 7322 | : pkg.manifestDigest.toString(); |
| 7323 | Slog.d(TAG, "Comparing manifests: " + args.manifestDigest.toString() + " vs. " |
| 7324 | + parsedManifest); |
| 7325 | } |
| 7326 | |
| 7327 | if (!args.manifestDigest.equals(pkg.manifestDigest)) { |
| 7328 | res.returnCode = PackageManager.INSTALL_FAILED_PACKAGE_CHANGED; |
| 7329 | return; |
| 7330 | } |
| 7331 | } else if (DEBUG_INSTALL) { |
| 7332 | final String parsedManifest = pkg.manifestDigest == null |
| 7333 | ? "null" : pkg.manifestDigest.toString(); |
| 7334 | Slog.d(TAG, "manifestDigest was not present, but parser got: " + parsedManifest); |
| 7335 | } |
| 7336 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7337 | // Get rid of all references to package scan path via parser. |
| 7338 | pp = null; |
| 7339 | String oldCodePath = null; |
| 7340 | boolean systemApp = false; |
| 7341 | synchronized (mPackages) { |
| 7342 | // Check if installing already existing package |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7343 | if ((pFlags&PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| 7344 | String oldName = mSettings.mRenamedPackages.get(pkgName); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 7345 | if (pkg.mOriginalPackages != null |
| 7346 | && pkg.mOriginalPackages.contains(oldName) |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7347 | && mPackages.containsKey(oldName)) { |
| 7348 | // This package is derived from an original package, |
| 7349 | // and this device has been updating from that original |
| 7350 | // name. We must continue using the original name, so |
| 7351 | // rename the new package here. |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 7352 | pkg.setPackageName(oldName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7353 | pkgName = pkg.packageName; |
| 7354 | replace = true; |
| 7355 | } else if (mPackages.containsKey(pkgName)) { |
| 7356 | // This package, under its official name, already exists |
| 7357 | // on the device; we should replace it. |
| 7358 | replace = true; |
| 7359 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7360 | } |
| 7361 | PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 7362 | if (ps != null) { |
| 7363 | oldCodePath = mSettings.mPackages.get(pkgName).codePathString; |
| 7364 | if (ps.pkg != null && ps.pkg.applicationInfo != null) { |
| 7365 | systemApp = (ps.pkg.applicationInfo.flags & |
| 7366 | ApplicationInfo.FLAG_SYSTEM) != 0; |
Dianne Hackborn | ade3eca | 2009-05-11 18:54:45 -0700 | [diff] [blame] | 7367 | } |
| 7368 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7369 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7370 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7371 | if (systemApp && onSd) { |
| 7372 | // Disable updates to system apps on sdcard |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7373 | Slog.w(TAG, "Cannot install updates to system apps on sdcard"); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7374 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
| 7375 | return; |
| 7376 | } |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 7377 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7378 | if (!args.doRename(res.returnCode, pkgName, oldCodePath)) { |
| 7379 | res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7380 | return; |
| 7381 | } |
| 7382 | // Set application objects path explicitly after the rename |
| 7383 | setApplicationInfoPaths(pkg, args.getCodePath(), args.getResourcePath()); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7384 | pkg.applicationInfo.nativeLibraryDir = args.getNativeLibraryPath(); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 7385 | if (replace) { |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 7386 | replacePackageLI(pkg, parseFlags, scanMode, |
| 7387 | installerPackageName, res); |
| 7388 | } else { |
| 7389 | installNewPackageLI(pkg, parseFlags, scanMode, |
| 7390 | installerPackageName,res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7391 | } |
| 7392 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7393 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7394 | private static boolean isForwardLocked(PackageParser.Package pkg) { |
| 7395 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7396 | } |
| 7397 | |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 7398 | |
| 7399 | private boolean isForwardLocked(PackageSetting ps) { |
| 7400 | return (ps.pkgFlags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0; |
| 7401 | } |
| 7402 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7403 | private static boolean isExternal(PackageParser.Package pkg) { |
| 7404 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; |
| 7405 | } |
| 7406 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7407 | private static boolean isExternal(PackageSetting ps) { |
| 7408 | return (ps.pkgFlags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; |
| 7409 | } |
| 7410 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7411 | private static boolean isSystemApp(PackageParser.Package pkg) { |
| 7412 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 7413 | } |
| 7414 | |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 7415 | private static boolean isSystemApp(ApplicationInfo info) { |
| 7416 | return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 7417 | } |
| 7418 | |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 7419 | private static boolean isSystemApp(PackageSetting ps) { |
| 7420 | return (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 7421 | } |
| 7422 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7423 | private static boolean isUpdatedSystemApp(PackageParser.Package pkg) { |
| 7424 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 7425 | } |
| 7426 | |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 7427 | private int packageFlagsToInstallFlags(PackageSetting ps) { |
| 7428 | int installFlags = 0; |
| 7429 | if (isExternal(ps)) { |
| 7430 | installFlags |= PackageManager.INSTALL_EXTERNAL; |
| 7431 | } |
| 7432 | if (isForwardLocked(ps)) { |
| 7433 | installFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 7434 | } |
| 7435 | return installFlags; |
| 7436 | } |
| 7437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7438 | private void deleteTempPackageFiles() { |
| 7439 | FilenameFilter filter = new FilenameFilter() { |
| 7440 | public boolean accept(File dir, String name) { |
| 7441 | return name.startsWith("vmdl") && name.endsWith(".tmp"); |
| 7442 | } |
| 7443 | }; |
| 7444 | String tmpFilesList[] = mAppInstallDir.list(filter); |
| 7445 | if(tmpFilesList == null) { |
| 7446 | return; |
| 7447 | } |
| 7448 | for(int i = 0; i < tmpFilesList.length; i++) { |
| 7449 | File tmpFile = new File(mAppInstallDir, tmpFilesList[i]); |
| 7450 | tmpFile.delete(); |
| 7451 | } |
| 7452 | } |
| 7453 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7454 | private File createTempPackageFile(File installDir) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7455 | File tmpPackageFile; |
| 7456 | try { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7457 | tmpPackageFile = File.createTempFile("vmdl", ".tmp", installDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7458 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7459 | 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] | 7460 | return null; |
| 7461 | } |
| 7462 | try { |
| 7463 | FileUtils.setPermissions( |
| 7464 | tmpPackageFile.getCanonicalPath(), FileUtils.S_IRUSR|FileUtils.S_IWUSR, |
| 7465 | -1, -1); |
| 7466 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7467 | 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] | 7468 | return null; |
| 7469 | } |
| 7470 | return tmpPackageFile; |
| 7471 | } |
| 7472 | |
| 7473 | public void deletePackage(final String packageName, |
| 7474 | final IPackageDeleteObserver observer, |
| 7475 | final int flags) { |
| 7476 | mContext.enforceCallingOrSelfPermission( |
| 7477 | android.Manifest.permission.DELETE_PACKAGES, null); |
| 7478 | // Queue up an async operation since the package deletion may take a little while. |
| 7479 | mHandler.post(new Runnable() { |
| 7480 | public void run() { |
| 7481 | mHandler.removeCallbacks(this); |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7482 | final int returnCode = deletePackageX(packageName, true, true, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7483 | if (observer != null) { |
| 7484 | try { |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7485 | observer.packageDeleted(packageName, returnCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7486 | } catch (RemoteException e) { |
| 7487 | Log.i(TAG, "Observer no longer exists."); |
| 7488 | } //end catch |
| 7489 | } //end if |
| 7490 | } //end run |
| 7491 | }); |
| 7492 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7493 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7494 | /** |
| 7495 | * This method is an internal method that could be get invoked either |
| 7496 | * to delete an installed package or to clean up a failed installation. |
| 7497 | * After deleting an installed package, a broadcast is sent to notify any |
| 7498 | * listeners that the package has been installed. For cleaning up a failed |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7499 | * 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] | 7500 | * installation wouldn't have sent the initial broadcast either |
| 7501 | * The key steps in deleting a package are |
| 7502 | * deleting the package information in internal structures like mPackages, |
| 7503 | * deleting the packages base directories through installd |
| 7504 | * updating mSettings to reflect current status |
| 7505 | * persisting settings for later use |
| 7506 | * sending a broadcast if necessary |
| 7507 | */ |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7508 | private int deletePackageX(String packageName, boolean sendBroadCast, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7509 | boolean deleteCodeAndResources, int flags) { |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7510 | final PackageRemovedInfo info = new PackageRemovedInfo(); |
| 7511 | final boolean res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7512 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7513 | IDevicePolicyManager dpm = IDevicePolicyManager.Stub.asInterface( |
| 7514 | ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)); |
| 7515 | try { |
| 7516 | if (dpm != null && dpm.packageHasActiveAdmins(packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7517 | Slog.w(TAG, "Not removing package " + packageName + ": has active device admin"); |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7518 | return PackageManager.DELETE_FAILED_DEVICE_POLICY_MANAGER; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7519 | } |
| 7520 | } catch (RemoteException e) { |
| 7521 | } |
| 7522 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7523 | synchronized (mInstallLock) { |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 7524 | res = deletePackageLI(packageName, deleteCodeAndResources, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7525 | flags | REMOVE_CHATTY, info, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7526 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7527 | |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7528 | if (res && sendBroadCast) { |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7529 | boolean systemUpdate = info.isRemovedPackageSystemUpdate; |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 7530 | info.sendBroadcast(deleteCodeAndResources, systemUpdate); |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7531 | |
| 7532 | // If the removed package was a system update, the old system packaged |
| 7533 | // was re-enabled; we need to broadcast this information |
| 7534 | if (systemUpdate) { |
| 7535 | Bundle extras = new Bundle(1); |
| 7536 | extras.putInt(Intent.EXTRA_UID, info.removedUid >= 0 ? info.removedUid : info.uid); |
| 7537 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 7538 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7539 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7540 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7541 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7542 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7543 | sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, null, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7544 | null, packageName, null, UserId.USER_ALL); |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7545 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7546 | } |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 7547 | // Force a gc here. |
| 7548 | Runtime.getRuntime().gc(); |
| 7549 | // Delete the resources here after sending the broadcast to let |
| 7550 | // other processes clean up before deleting resources. |
| 7551 | if (info.args != null) { |
| 7552 | synchronized (mInstallLock) { |
| 7553 | info.args.doPostDeleteLI(deleteCodeAndResources); |
| 7554 | } |
| 7555 | } |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 7556 | |
| 7557 | 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] | 7558 | } |
| 7559 | |
| 7560 | static class PackageRemovedInfo { |
| 7561 | String removedPackage; |
| 7562 | int uid = -1; |
| 7563 | int removedUid = -1; |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7564 | boolean isRemovedPackageSystemUpdate = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7565 | // Clean up resources deleted packages. |
| 7566 | InstallArgs args = null; |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7567 | |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 7568 | void sendBroadcast(boolean fullRemove, boolean replacing) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7569 | Bundle extras = new Bundle(1); |
| 7570 | extras.putInt(Intent.EXTRA_UID, removedUid >= 0 ? removedUid : uid); |
| 7571 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, fullRemove); |
| 7572 | if (replacing) { |
| 7573 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 7574 | } |
| 7575 | if (removedPackage != null) { |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7576 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7577 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | f9abb40 | 2011-08-10 15:00:59 -0700 | [diff] [blame] | 7578 | if (fullRemove && !replacing) { |
| 7579 | sendPackageBroadcast(Intent.ACTION_PACKAGE_FULLY_REMOVED, removedPackage, |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7580 | extras, null, null, UserId.USER_ALL); |
Dianne Hackborn | f9abb40 | 2011-08-10 15:00:59 -0700 | [diff] [blame] | 7581 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7582 | } |
| 7583 | if (removedUid >= 0) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7584 | sendPackageBroadcast(Intent.ACTION_UID_REMOVED, null, extras, null, null, |
| 7585 | UserId.getUserId(removedUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7586 | } |
| 7587 | } |
| 7588 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7590 | /* |
| 7591 | * This method deletes the package from internal data structures. If the DONT_DELETE_DATA |
| 7592 | * flag is not set, the data directory is removed as well. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7593 | * 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] | 7594 | * delete a partially installed application. |
| 7595 | */ |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7596 | private void removePackageDataLI(PackageParser.Package p, PackageRemovedInfo outInfo, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7597 | int flags, boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7598 | String packageName = p.packageName; |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7599 | if (outInfo != null) { |
| 7600 | outInfo.removedPackage = packageName; |
| 7601 | } |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 7602 | removePackageLI(p, (flags&REMOVE_CHATTY) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7603 | // Retrieve object to delete permissions for shared user later on |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7604 | final PackageSetting deletedPs; |
| 7605 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7606 | synchronized (mPackages) { |
| 7607 | deletedPs = mSettings.mPackages.get(packageName); |
| 7608 | } |
| 7609 | if ((flags&PackageManager.DONT_DELETE_DATA) == 0) { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7610 | int retCode = mInstaller.remove(packageName, 0); |
| 7611 | if (retCode < 0) { |
| 7612 | Slog.w(TAG, "Couldn't remove app data or cache directory for package: " |
| 7613 | + packageName + ", retcode=" + retCode); |
| 7614 | // 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] | 7615 | } else { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7616 | // TODO: Kill the processes first |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 7617 | sUserManager.removePackageForAllUsers(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7618 | } |
Dianne Hackborn | fb1f103 | 2010-07-29 13:57:56 -0700 | [diff] [blame] | 7619 | schedulePackageCleaning(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7620 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7621 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7622 | synchronized (mPackages) { |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 7623 | if (deletedPs != null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7624 | if ((flags&PackageManager.DONT_DELETE_DATA) == 0) { |
| 7625 | if (outInfo != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7626 | outInfo.removedUid = mSettings.removePackageLPw(packageName); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7627 | } |
| 7628 | if (deletedPs != null) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7629 | updatePermissionsLPw(deletedPs.name, null, 0); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7630 | if (deletedPs.sharedUser != null) { |
| 7631 | // remove permissions associated with package |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7632 | mSettings.updateSharedUserPermsLPw(deletedPs, mGlobalGids); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7633 | } |
| 7634 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 7635 | clearPackagePreferredActivitiesLPw(deletedPs.name); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 7636 | } |
| 7637 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7638 | // can downgrade to reader |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7639 | if (writeSettings) { |
| 7640 | // Save settings now |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7641 | mSettings.writeLPr(); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7642 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7643 | } |
| 7644 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7645 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7646 | /* |
| 7647 | * Tries to delete system package. |
| 7648 | */ |
| 7649 | private boolean deleteSystemPackageLI(PackageParser.Package p, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7650 | int flags, PackageRemovedInfo outInfo, boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7651 | ApplicationInfo applicationInfo = p.applicationInfo; |
| 7652 | //applicable for non-partially installed applications only |
| 7653 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7654 | Slog.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7655 | return false; |
| 7656 | } |
| 7657 | PackageSetting ps = null; |
| 7658 | // Confirm if the system package has been updated |
| 7659 | // An updated system app can be deleted. This will also have to restore |
| 7660 | // the system pkg from system partition |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7661 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7662 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7663 | ps = mSettings.getDisabledSystemPkgLPr(p.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7664 | } |
| 7665 | if (ps == null) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7666 | 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] | 7667 | return false; |
| 7668 | } else { |
| 7669 | Log.i(TAG, "Deleting system pkg from data partition"); |
| 7670 | } |
| 7671 | // Delete the updated package |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 7672 | outInfo.isRemovedPackageSystemUpdate = true; |
Kenny Root | 7c1bd7f | 2010-09-01 13:44:11 -0700 | [diff] [blame] | 7673 | if (ps.versionCode < p.mVersionCode) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7674 | // Delete data for downgrades |
Kenny Root | 7c1bd7f | 2010-09-01 13:44:11 -0700 | [diff] [blame] | 7675 | flags &= ~PackageManager.DONT_DELETE_DATA; |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7676 | } else { |
| 7677 | // Preserve data by setting flag |
Kenny Root | 7c1bd7f | 2010-09-01 13:44:11 -0700 | [diff] [blame] | 7678 | flags |= PackageManager.DONT_DELETE_DATA; |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 7679 | } |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 7680 | boolean ret = deleteInstalledPackageLI(p, true, flags, outInfo, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7681 | writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7682 | if (!ret) { |
| 7683 | return false; |
| 7684 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7685 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7686 | synchronized (mPackages) { |
| 7687 | // Reinstate the old system package |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7688 | mSettings.enableSystemPackageLPw(p.packageName); |
Kenny Root | 8f7cc02 | 2010-09-12 09:04:56 -0700 | [diff] [blame] | 7689 | // Remove any native libraries from the upgraded package. |
| 7690 | NativeLibraryHelper.removeNativeBinariesLI(p.applicationInfo.nativeLibraryDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7691 | } |
| 7692 | // Install the system package |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7693 | PackageParser.Package newPkg = scanPackageLI(ps.codePath, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7694 | PackageParser.PARSE_MUST_BE_APK | PackageParser.PARSE_IS_SYSTEM, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 7695 | SCAN_MONITOR | SCAN_NO_PATHS, 0); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7697 | if (newPkg == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7698 | 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] | 7699 | return false; |
| 7700 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7701 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7702 | synchronized (mPackages) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 7703 | updatePermissionsLPw(newPkg.packageName, newPkg, |
| 7704 | UPDATE_PERMISSIONS_ALL | UPDATE_PERMISSIONS_REPLACE_PKG); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7705 | // can downgrade to reader here |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7706 | if (writeSettings) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 7707 | mSettings.writeLPr(); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7708 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7709 | } |
| 7710 | return true; |
| 7711 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7712 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7713 | private boolean deleteInstalledPackageLI(PackageParser.Package p, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7714 | boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo, |
| 7715 | boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7716 | ApplicationInfo applicationInfo = p.applicationInfo; |
| 7717 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7718 | Slog.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7719 | return false; |
| 7720 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 7721 | if (outInfo != null) { |
| 7722 | outInfo.uid = applicationInfo.uid; |
| 7723 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7724 | |
| 7725 | // 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] | 7726 | removePackageDataLI(p, outInfo, flags, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7727 | |
| 7728 | // Delete application code and resources |
| 7729 | if (deleteCodeAndResources) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7730 | // TODO can pick up from PackageSettings as well |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7731 | int installFlags = isExternal(p) ? PackageManager.INSTALL_EXTERNAL : 0; |
| 7732 | installFlags |= isForwardLocked(p) ? PackageManager.INSTALL_FORWARD_LOCK : 0; |
| 7733 | outInfo.args = createInstallArgs(installFlags, applicationInfo.sourceDir, |
| 7734 | applicationInfo.publicSourceDir, applicationInfo.nativeLibraryDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7735 | } |
| 7736 | return true; |
| 7737 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7738 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7739 | /* |
| 7740 | * This method handles package deletion in general |
| 7741 | */ |
| 7742 | private boolean deletePackageLI(String packageName, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7743 | boolean deleteCodeAndResources, int flags, PackageRemovedInfo outInfo, |
| 7744 | boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7745 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7746 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7747 | return false; |
| 7748 | } |
| 7749 | PackageParser.Package p; |
| 7750 | boolean dataOnly = false; |
| 7751 | synchronized (mPackages) { |
| 7752 | p = mPackages.get(packageName); |
| 7753 | if (p == null) { |
| 7754 | //this retrieves partially installed apps |
| 7755 | dataOnly = true; |
| 7756 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 7757 | if (ps == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7758 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7759 | return false; |
| 7760 | } |
| 7761 | p = ps.pkg; |
| 7762 | } |
| 7763 | } |
| 7764 | if (p == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7765 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7766 | return false; |
| 7767 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7768 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7769 | if (dataOnly) { |
| 7770 | // Delete application data first |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7771 | removePackageDataLI(p, outInfo, flags, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7772 | return true; |
| 7773 | } |
| 7774 | // At this point the package should have ApplicationInfo associated with it |
| 7775 | if (p.applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7776 | Slog.w(TAG, "Package " + p.packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7777 | return false; |
| 7778 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7779 | boolean ret = false; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7780 | if (isSystemApp(p)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7781 | Log.i(TAG, "Removing system package:"+p.packageName); |
| 7782 | // When an updated system application is deleted we delete the existing resources as well and |
| 7783 | // fall back to existing code in system partition |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7784 | ret = deleteSystemPackageLI(p, flags, outInfo, writeSettings); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7785 | } else { |
| 7786 | Log.i(TAG, "Removing non-system package:"+p.packageName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7787 | // Kill application pre-emptively especially for apps on sd. |
| 7788 | killApplication(packageName, p.applicationInfo.uid); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 7789 | ret = deleteInstalledPackageLI(p, deleteCodeAndResources, flags, outInfo, |
| 7790 | writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7791 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 7792 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7793 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7794 | |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 7795 | private final class ClearStorageConnection implements ServiceConnection { |
| 7796 | IMediaContainerService mContainerService; |
| 7797 | |
| 7798 | @Override |
| 7799 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 7800 | synchronized (this) { |
| 7801 | mContainerService = IMediaContainerService.Stub.asInterface(service); |
| 7802 | notifyAll(); |
| 7803 | } |
| 7804 | } |
| 7805 | |
| 7806 | @Override |
| 7807 | public void onServiceDisconnected(ComponentName name) { |
| 7808 | } |
| 7809 | } |
| 7810 | |
| 7811 | private void clearExternalStorageDataSync(String packageName, boolean allData) { |
| 7812 | final boolean mounted; |
| 7813 | if (Environment.isExternalStorageEmulated()) { |
| 7814 | mounted = true; |
| 7815 | } else { |
| 7816 | final String status = Environment.getExternalStorageState(); |
| 7817 | |
| 7818 | mounted = status.equals(Environment.MEDIA_MOUNTED) |
| 7819 | || status.equals(Environment.MEDIA_MOUNTED_READ_ONLY); |
| 7820 | } |
| 7821 | |
| 7822 | if (!mounted) { |
| 7823 | return; |
| 7824 | } |
| 7825 | |
| 7826 | final Intent containerIntent = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 7827 | ClearStorageConnection conn = new ClearStorageConnection(); |
| 7828 | if (mContext.bindService(containerIntent, conn, Context.BIND_AUTO_CREATE)) { |
| 7829 | try { |
| 7830 | long timeout = SystemClock.uptimeMillis() + 5000; |
| 7831 | synchronized (conn) { |
| 7832 | long now = SystemClock.uptimeMillis(); |
| 7833 | while (conn.mContainerService == null && now < timeout) { |
| 7834 | try { |
| 7835 | conn.wait(timeout - now); |
| 7836 | } catch (InterruptedException e) { |
| 7837 | } |
| 7838 | } |
| 7839 | } |
| 7840 | if (conn.mContainerService == null) { |
| 7841 | return; |
| 7842 | } |
| 7843 | final File externalCacheDir = Environment |
| 7844 | .getExternalStorageAppCacheDirectory(packageName); |
| 7845 | try { |
| 7846 | conn.mContainerService.clearDirectory(externalCacheDir.toString()); |
| 7847 | } catch (RemoteException e) { |
| 7848 | } |
| 7849 | if (allData) { |
| 7850 | final File externalDataDir = Environment |
| 7851 | .getExternalStorageAppDataDirectory(packageName); |
| 7852 | try { |
| 7853 | conn.mContainerService.clearDirectory(externalDataDir.toString()); |
| 7854 | } catch (RemoteException e) { |
| 7855 | } |
| 7856 | final File externalMediaDir = Environment |
| 7857 | .getExternalStorageAppMediaDirectory(packageName); |
| 7858 | try { |
| 7859 | conn.mContainerService.clearDirectory(externalMediaDir.toString()); |
| 7860 | } catch (RemoteException e) { |
| 7861 | } |
| 7862 | } |
| 7863 | } finally { |
| 7864 | mContext.unbindService(conn); |
| 7865 | } |
| 7866 | } |
| 7867 | } |
| 7868 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7869 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7870 | public void clearApplicationUserData(final String packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7871 | final IPackageDataObserver observer, final int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7872 | mContext.enforceCallingOrSelfPermission( |
| 7873 | android.Manifest.permission.CLEAR_APP_USER_DATA, null); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7874 | checkValidCaller(Binder.getCallingUid(), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7875 | // Queue up an async operation since the package deletion may take a little while. |
| 7876 | mHandler.post(new Runnable() { |
| 7877 | public void run() { |
| 7878 | mHandler.removeCallbacks(this); |
| 7879 | final boolean succeeded; |
| 7880 | synchronized (mInstallLock) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7881 | succeeded = clearApplicationUserDataLI(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7882 | } |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 7883 | clearExternalStorageDataSync(packageName, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7884 | if (succeeded) { |
| 7885 | // invoke DeviceStorageMonitor's update method to clear any notifications |
| 7886 | DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) |
| 7887 | ServiceManager.getService(DeviceStorageMonitorService.SERVICE); |
| 7888 | if (dsm != null) { |
| 7889 | dsm.updateMemory(); |
| 7890 | } |
| 7891 | } |
| 7892 | if(observer != null) { |
| 7893 | try { |
| 7894 | observer.onRemoveCompleted(packageName, succeeded); |
| 7895 | } catch (RemoteException e) { |
| 7896 | Log.i(TAG, "Observer no longer exists."); |
| 7897 | } |
| 7898 | } //end if observer |
| 7899 | } //end run |
| 7900 | }); |
| 7901 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7902 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7903 | private boolean clearApplicationUserDataLI(String packageName, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7904 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7905 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7906 | return false; |
| 7907 | } |
| 7908 | PackageParser.Package p; |
| 7909 | boolean dataOnly = false; |
| 7910 | synchronized (mPackages) { |
| 7911 | p = mPackages.get(packageName); |
| 7912 | if(p == null) { |
| 7913 | dataOnly = true; |
| 7914 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 7915 | if((ps == null) || (ps.pkg == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7916 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7917 | return false; |
| 7918 | } |
| 7919 | p = ps.pkg; |
| 7920 | } |
| 7921 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 7922 | |
Dianne Hackborn | 3b81bc1 | 2011-01-15 11:50:52 -0800 | [diff] [blame] | 7923 | if (!dataOnly) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7924 | //need to check this only for fully installed applications |
| 7925 | if (p == 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 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 7930 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7931 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7932 | return false; |
| 7933 | } |
| 7934 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7935 | int retCode = mInstaller.clearUserData(packageName, userId); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7936 | if (retCode < 0) { |
| 7937 | Slog.w(TAG, "Couldn't remove cache files for package: " |
| 7938 | + packageName); |
| 7939 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7940 | } |
| 7941 | return true; |
| 7942 | } |
| 7943 | |
| 7944 | public void deleteApplicationCacheFiles(final String packageName, |
| 7945 | final IPackageDataObserver observer) { |
| 7946 | mContext.enforceCallingOrSelfPermission( |
| 7947 | android.Manifest.permission.DELETE_CACHE_FILES, null); |
| 7948 | // 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] | 7949 | final int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7950 | mHandler.post(new Runnable() { |
| 7951 | public void run() { |
| 7952 | mHandler.removeCallbacks(this); |
| 7953 | final boolean succeded; |
| 7954 | synchronized (mInstallLock) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7955 | succeded = deleteApplicationCacheFilesLI(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7956 | } |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 7957 | clearExternalStorageDataSync(packageName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7958 | if(observer != null) { |
| 7959 | try { |
| 7960 | observer.onRemoveCompleted(packageName, succeded); |
| 7961 | } catch (RemoteException e) { |
| 7962 | Log.i(TAG, "Observer no longer exists."); |
| 7963 | } |
| 7964 | } //end if observer |
| 7965 | } //end run |
| 7966 | }); |
| 7967 | } |
| 7968 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7969 | private boolean deleteApplicationCacheFilesLI(String packageName, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7970 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7971 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7972 | return false; |
| 7973 | } |
| 7974 | PackageParser.Package p; |
| 7975 | synchronized (mPackages) { |
| 7976 | p = mPackages.get(packageName); |
| 7977 | } |
| 7978 | if (p == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7979 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7980 | return false; |
| 7981 | } |
| 7982 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 7983 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7984 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7985 | return false; |
| 7986 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7987 | // TODO: Pass userId to deleteCacheFiles |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7988 | int retCode = mInstaller.deleteCacheFiles(packageName); |
| 7989 | if (retCode < 0) { |
| 7990 | Slog.w(TAG, "Couldn't remove cache files for package: " |
| 7991 | + packageName); |
| 7992 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7993 | } |
| 7994 | return true; |
| 7995 | } |
| 7996 | |
| 7997 | public void getPackageSizeInfo(final String packageName, |
| 7998 | final IPackageStatsObserver observer) { |
| 7999 | mContext.enforceCallingOrSelfPermission( |
| 8000 | android.Manifest.permission.GET_PACKAGE_SIZE, null); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 8001 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 8002 | PackageStats stats = new PackageStats(packageName); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 8003 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 8004 | /* |
| 8005 | * Queue up an async operation since the package measurement may take a |
| 8006 | * little while. |
| 8007 | */ |
| 8008 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| 8009 | msg.obj = new MeasureParams(stats, observer); |
| 8010 | mHandler.sendMessage(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8011 | } |
| 8012 | |
| 8013 | private boolean getPackageSizeInfoLI(String packageName, PackageStats pStats) { |
| 8014 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8015 | 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] | 8016 | return false; |
| 8017 | } |
| 8018 | PackageParser.Package p; |
| 8019 | boolean dataOnly = false; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 8020 | String asecPath = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8021 | synchronized (mPackages) { |
| 8022 | p = mPackages.get(packageName); |
| 8023 | if(p == null) { |
| 8024 | dataOnly = true; |
| 8025 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 8026 | if((ps == null) || (ps.pkg == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8027 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8028 | return false; |
| 8029 | } |
| 8030 | p = ps.pkg; |
| 8031 | } |
Kenny Root | 2afded1 | 2012-06-04 16:39:47 -0700 | [diff] [blame] | 8032 | if (p != null && (isExternal(p) || isForwardLocked(p))) { |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 8033 | String secureContainerId = cidFromCodePath(p.applicationInfo.sourceDir); |
| 8034 | if (secureContainerId != null) { |
| 8035 | asecPath = PackageHelper.getSdFilesystem(secureContainerId); |
| 8036 | } |
| 8037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8038 | } |
| 8039 | String publicSrcDir = null; |
| 8040 | if(!dataOnly) { |
| 8041 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 8042 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8043 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8044 | return false; |
| 8045 | } |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 8046 | if (isForwardLocked(p)) { |
| 8047 | publicSrcDir = applicationInfo.publicSourceDir; |
| 8048 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8049 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 8050 | int res = mInstaller.getSizeInfo(packageName, p.mPath, publicSrcDir, |
| 8051 | asecPath, pStats); |
| 8052 | if (res < 0) { |
| 8053 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8054 | } |
Kenny Root | 2afded1 | 2012-06-04 16:39:47 -0700 | [diff] [blame] | 8055 | |
| 8056 | // Fix-up for forward-locked applications in ASEC containers. |
| 8057 | if (!isExternal(p)) { |
| 8058 | pStats.codeSize += pStats.externalCodeSize; |
| 8059 | pStats.externalCodeSize = 0L; |
| 8060 | } |
| 8061 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8062 | return true; |
| 8063 | } |
| 8064 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8065 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8066 | public void addPackageToPreferred(String packageName) { |
Dianne Hackborn | 8377fc0 | 2010-06-07 15:54:58 -0700 | [diff] [blame] | 8067 | 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] | 8068 | } |
| 8069 | |
| 8070 | public void removePackageFromPreferred(String packageName) { |
Dianne Hackborn | 8377fc0 | 2010-06-07 15:54:58 -0700 | [diff] [blame] | 8071 | 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] | 8072 | } |
| 8073 | |
| 8074 | public List<PackageInfo> getPreferredPackages(int flags) { |
Dianne Hackborn | a7ca0e5 | 2009-12-01 14:31:55 -0800 | [diff] [blame] | 8075 | return new ArrayList<PackageInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8076 | } |
| 8077 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8078 | private int getUidTargetSdkVersionLockedLPr(int uid) { |
| 8079 | Object obj = mSettings.getUserIdLPr(uid); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8080 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 8081 | final SharedUserSetting sus = (SharedUserSetting) obj; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8082 | int vers = Build.VERSION_CODES.CUR_DEVELOPMENT; |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 8083 | final Iterator<PackageSetting> it = sus.packages.iterator(); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8084 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8085 | final PackageSetting ps = it.next(); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8086 | if (ps.pkg != null) { |
| 8087 | int v = ps.pkg.applicationInfo.targetSdkVersion; |
| 8088 | if (v < vers) vers = v; |
| 8089 | } |
| 8090 | } |
| 8091 | return vers; |
| 8092 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 8093 | final PackageSetting ps = (PackageSetting) obj; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8094 | if (ps.pkg != null) { |
| 8095 | return ps.pkg.applicationInfo.targetSdkVersion; |
| 8096 | } |
| 8097 | } |
| 8098 | return Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 8099 | } |
| 8100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8101 | public void addPreferredActivity(IntentFilter filter, int match, |
| 8102 | ComponentName[] set, ComponentName activity) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8103 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8104 | synchronized (mPackages) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8105 | if (mContext.checkCallingOrSelfPermission( |
| 8106 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 8107 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8108 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8109 | < Build.VERSION_CODES.FROYO) { |
| 8110 | Slog.w(TAG, "Ignoring addPreferredActivity() from uid " |
| 8111 | + Binder.getCallingUid()); |
| 8112 | return; |
| 8113 | } |
| 8114 | mContext.enforceCallingOrSelfPermission( |
| 8115 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 8116 | } |
| 8117 | |
| 8118 | Slog.i(TAG, "Adding preferred activity " + activity + ":"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8119 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
| 8120 | mSettings.mPreferredActivities.addFilter( |
| 8121 | new PreferredActivity(filter, match, set, activity)); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8122 | scheduleWriteSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8123 | } |
| 8124 | } |
| 8125 | |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8126 | public void replacePreferredActivity(IntentFilter filter, int match, |
| 8127 | ComponentName[] set, ComponentName activity) { |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8128 | if (filter.countActions() != 1) { |
| 8129 | throw new IllegalArgumentException( |
| 8130 | "replacePreferredActivity expects filter to have only 1 action."); |
| 8131 | } |
| 8132 | if (filter.countCategories() != 1) { |
| 8133 | throw new IllegalArgumentException( |
| 8134 | "replacePreferredActivity expects filter to have only 1 category."); |
| 8135 | } |
| 8136 | if (filter.countDataAuthorities() != 0 |
| 8137 | || filter.countDataPaths() != 0 |
| 8138 | || filter.countDataSchemes() != 0 |
| 8139 | || filter.countDataTypes() != 0) { |
| 8140 | throw new IllegalArgumentException( |
| 8141 | "replacePreferredActivity expects filter to have no data authorities, " + |
| 8142 | "paths, schemes or types."); |
| 8143 | } |
| 8144 | synchronized (mPackages) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8145 | if (mContext.checkCallingOrSelfPermission( |
| 8146 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 8147 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8148 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8149 | < Build.VERSION_CODES.FROYO) { |
| 8150 | Slog.w(TAG, "Ignoring replacePreferredActivity() from uid " |
| 8151 | + Binder.getCallingUid()); |
| 8152 | return; |
| 8153 | } |
| 8154 | mContext.enforceCallingOrSelfPermission( |
| 8155 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 8156 | } |
| 8157 | |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8158 | ArrayList<PreferredActivity> removed = null; |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8159 | Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
| 8160 | String action = filter.getAction(0); |
| 8161 | String category = filter.getCategory(0); |
| 8162 | while (it.hasNext()) { |
| 8163 | PreferredActivity pa = it.next(); |
| 8164 | if (pa.getAction(0).equals(action) && pa.getCategory(0).equals(category)) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8165 | if (removed == null) { |
| 8166 | removed = new ArrayList<PreferredActivity>(); |
| 8167 | } |
| 8168 | removed.add(pa); |
| 8169 | Log.i(TAG, "Removing preferred activity " + pa.mPref.mComponent + ":"); |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8170 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
| 8171 | } |
| 8172 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8173 | if (removed != null) { |
| 8174 | for (int i=0; i<removed.size(); i++) { |
| 8175 | PreferredActivity pa = removed.get(i); |
| 8176 | mSettings.mPreferredActivities.removeFilter(pa); |
| 8177 | } |
| 8178 | } |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 8179 | addPreferredActivity(filter, match, set, activity); |
| 8180 | } |
| 8181 | } |
| 8182 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8183 | public void clearPackagePreferredActivities(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8184 | final int uid = Binder.getCallingUid(); |
| 8185 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8186 | synchronized (mPackages) { |
Dianne Hackborn | 2ee89ea | 2010-03-10 18:27:09 -0800 | [diff] [blame] | 8187 | PackageParser.Package pkg = mPackages.get(packageName); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8188 | if (pkg == null || pkg.applicationInfo.uid != uid) { |
| 8189 | if (mContext.checkCallingOrSelfPermission( |
| 8190 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 8191 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8192 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8193 | < Build.VERSION_CODES.FROYO) { |
| 8194 | Slog.w(TAG, "Ignoring clearPackagePreferredActivities() from uid " |
| 8195 | + Binder.getCallingUid()); |
| 8196 | return; |
| 8197 | } |
| 8198 | mContext.enforceCallingOrSelfPermission( |
| 8199 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 8200 | } |
Dianne Hackborn | 2ee89ea | 2010-03-10 18:27:09 -0800 | [diff] [blame] | 8201 | } |
| 8202 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8203 | if (clearPackagePreferredActivitiesLPw(packageName)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8204 | scheduleWriteSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8205 | } |
| 8206 | } |
| 8207 | } |
| 8208 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8209 | boolean clearPackagePreferredActivitiesLPw(String packageName) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8210 | ArrayList<PreferredActivity> removed = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8211 | Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
| 8212 | while (it.hasNext()) { |
| 8213 | PreferredActivity pa = it.next(); |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 8214 | if (pa.mPref.mComponent.getPackageName().equals(packageName)) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8215 | if (removed == null) { |
| 8216 | removed = new ArrayList<PreferredActivity>(); |
| 8217 | } |
| 8218 | removed.add(pa); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8219 | } |
| 8220 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 8221 | if (removed != null) { |
| 8222 | for (int i=0; i<removed.size(); i++) { |
| 8223 | PreferredActivity pa = removed.get(i); |
| 8224 | mSettings.mPreferredActivities.removeFilter(pa); |
| 8225 | } |
| 8226 | return true; |
| 8227 | } |
| 8228 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8229 | } |
| 8230 | |
| 8231 | public int getPreferredActivities(List<IntentFilter> outFilters, |
| 8232 | List<ComponentName> outActivities, String packageName) { |
| 8233 | |
| 8234 | int num = 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8235 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8236 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8237 | final Iterator<PreferredActivity> it = mSettings.mPreferredActivities.filterIterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8238 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8239 | final PreferredActivity pa = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8240 | if (packageName == null |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 8241 | || pa.mPref.mComponent.getPackageName().equals(packageName)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8242 | if (outFilters != null) { |
| 8243 | outFilters.add(new IntentFilter(pa)); |
| 8244 | } |
| 8245 | if (outActivities != null) { |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 8246 | outActivities.add(pa.mPref.mComponent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8247 | } |
| 8248 | } |
| 8249 | } |
| 8250 | } |
| 8251 | |
| 8252 | return num; |
| 8253 | } |
| 8254 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8255 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8256 | public void setApplicationEnabledSetting(String appPackageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8257 | int newState, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8258 | if (!sUserManager.exists(userId)) return; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8259 | setEnabledSetting(appPackageName, null, newState, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8260 | } |
| 8261 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8262 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8263 | public void setComponentEnabledSetting(ComponentName componentName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8264 | int newState, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8265 | if (!sUserManager.exists(userId)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8266 | setEnabledSetting(componentName.getPackageName(), |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8267 | componentName.getClassName(), newState, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8268 | } |
| 8269 | |
| 8270 | private void setEnabledSetting( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8271 | 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] | 8272 | if (!(newState == COMPONENT_ENABLED_STATE_DEFAULT |
| 8273 | || newState == COMPONENT_ENABLED_STATE_ENABLED |
Dianne Hackborn | 0ac3031 | 2011-06-17 14:49:23 -0700 | [diff] [blame] | 8274 | || newState == COMPONENT_ENABLED_STATE_DISABLED |
| 8275 | || newState == COMPONENT_ENABLED_STATE_DISABLED_USER)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8276 | throw new IllegalArgumentException("Invalid new component state: " |
| 8277 | + newState); |
| 8278 | } |
| 8279 | PackageSetting pkgSetting; |
| 8280 | final int uid = Binder.getCallingUid(); |
| 8281 | final int permission = mContext.checkCallingPermission( |
| 8282 | android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8283 | checkValidCaller(uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8284 | final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8285 | boolean sendNow = false; |
| 8286 | boolean isApp = (className == null); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8287 | String componentName = isApp ? packageName : className; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8288 | int packageUid = -1; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8289 | ArrayList<String> components; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8290 | |
| 8291 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8292 | synchronized (mPackages) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8293 | pkgSetting = mSettings.mPackages.get(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8294 | if (pkgSetting == null) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8295 | if (className == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8296 | throw new IllegalArgumentException( |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8297 | "Unknown package: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8298 | } |
| 8299 | throw new IllegalArgumentException( |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8300 | "Unknown component: " + packageName |
| 8301 | + "/" + className); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8302 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8303 | // 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] | 8304 | if (!allowedByPermission && !UserId.isSameApp(uid, pkgSetting.appId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8305 | throw new SecurityException( |
| 8306 | "Permission Denial: attempt to change component state from pid=" |
| 8307 | + Binder.getCallingPid() |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8308 | + ", uid=" + uid + ", package uid=" + pkgSetting.appId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8309 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8310 | if (className == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8311 | // We're dealing with an application/package level state change |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8312 | if (pkgSetting.getEnabled(userId) == newState) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8313 | // Nothing to do |
| 8314 | return; |
| 8315 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8316 | pkgSetting.setEnabled(newState, userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8317 | // pkgSetting.pkg.mSetEnabled = newState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8318 | } else { |
| 8319 | // We're dealing with a component level state change |
Dianne Hackborn | 6569625 | 2012-03-05 18:49:21 -0800 | [diff] [blame] | 8320 | // First, verify that this is a valid class name. |
| 8321 | PackageParser.Package pkg = pkgSetting.pkg; |
| 8322 | if (pkg == null || !pkg.hasComponentClassName(className)) { |
| 8323 | if (pkg.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.JELLY_BEAN) { |
| 8324 | throw new IllegalArgumentException("Component class " + className |
| 8325 | + " does not exist in " + packageName); |
| 8326 | } else { |
| 8327 | Slog.w(TAG, "Failed setComponentEnabledSetting: component class " |
| 8328 | + className + " does not exist in " + packageName); |
| 8329 | } |
| 8330 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8331 | switch (newState) { |
| 8332 | case COMPONENT_ENABLED_STATE_ENABLED: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8333 | if (!pkgSetting.enableComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8334 | return; |
| 8335 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8336 | break; |
| 8337 | case COMPONENT_ENABLED_STATE_DISABLED: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8338 | if (!pkgSetting.disableComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8339 | return; |
| 8340 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8341 | break; |
| 8342 | case COMPONENT_ENABLED_STATE_DEFAULT: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8343 | if (!pkgSetting.restoreComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 8344 | return; |
| 8345 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8346 | break; |
| 8347 | default: |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8348 | Slog.e(TAG, "Invalid new component state: " + newState); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8349 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8350 | } |
| 8351 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8352 | mSettings.writePackageRestrictionsLPr(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8353 | packageUid = UserId.getUid(userId, pkgSetting.appId); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8354 | components = mPendingBroadcasts.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8355 | final boolean newPackage = components == null; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8356 | if (newPackage) { |
| 8357 | components = new ArrayList<String>(); |
| 8358 | } |
| 8359 | if (!components.contains(componentName)) { |
| 8360 | components.add(componentName); |
| 8361 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8362 | if ((flags&PackageManager.DONT_KILL_APP) == 0) { |
| 8363 | sendNow = true; |
| 8364 | // Purge entry from pending broadcast list if another one exists already |
| 8365 | // since we are sending one right away. |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8366 | mPendingBroadcasts.remove(packageName); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8367 | } else { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8368 | if (newPackage) { |
| 8369 | mPendingBroadcasts.put(packageName, components); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8370 | } |
| 8371 | if (!mHandler.hasMessages(SEND_PENDING_BROADCAST)) { |
| 8372 | // Schedule a message |
| 8373 | mHandler.sendEmptyMessageDelayed(SEND_PENDING_BROADCAST, BROADCAST_DELAY); |
| 8374 | } |
| 8375 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8376 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8377 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8378 | long callingId = Binder.clearCallingIdentity(); |
| 8379 | try { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8380 | if (sendNow) { |
| 8381 | sendPackageChangedBroadcast(packageName, |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8382 | (flags&PackageManager.DONT_KILL_APP) != 0, components, packageUid); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8384 | } finally { |
| 8385 | Binder.restoreCallingIdentity(callingId); |
| 8386 | } |
| 8387 | } |
| 8388 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8389 | private void sendPackageChangedBroadcast(String packageName, |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8390 | boolean killFlag, ArrayList<String> componentNames, int packageUid) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 8391 | if (DEBUG_INSTALL) |
| 8392 | Log.v(TAG, "Sending package changed: package=" + packageName + " components=" |
| 8393 | + componentNames); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 8394 | Bundle extras = new Bundle(4); |
| 8395 | extras.putString(Intent.EXTRA_CHANGED_COMPONENT_NAME, componentNames.get(0)); |
| 8396 | String nameList[] = new String[componentNames.size()]; |
| 8397 | componentNames.toArray(nameList); |
| 8398 | extras.putStringArray(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST, nameList); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8399 | extras.putBoolean(Intent.EXTRA_DONT_KILL_APP, killFlag); |
| 8400 | extras.putInt(Intent.EXTRA_UID, packageUid); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8401 | sendPackageBroadcast(Intent.ACTION_PACKAGE_CHANGED, packageName, extras, null, null, |
| 8402 | UserId.getUserId(packageUid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8403 | } |
| 8404 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8405 | public void setPackageStoppedState(String packageName, boolean stopped, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8406 | if (!sUserManager.exists(userId)) return; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8407 | final int uid = Binder.getCallingUid(); |
| 8408 | final int permission = mContext.checkCallingOrSelfPermission( |
| 8409 | android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); |
| 8410 | final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8411 | checkValidCaller(uid, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8412 | // writer |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8413 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8414 | if (mSettings.setPackageStoppedStateLPw(packageName, stopped, allowedByPermission, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8415 | uid, userId)) { |
| 8416 | scheduleWritePackageRestrictionsLocked(userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8417 | } |
| 8418 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 8419 | } |
| 8420 | |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8421 | public String getInstallerPackageName(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8422 | // reader |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8423 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8424 | return mSettings.getInstallerPackageNameLPr(packageName); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8425 | } |
| 8426 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8427 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8428 | @Override |
| 8429 | public int getApplicationEnabledSetting(String packageName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8430 | if (!sUserManager.exists(userId)) return COMPONENT_ENABLED_STATE_DISABLED; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8431 | int uid = Binder.getCallingUid(); |
| 8432 | checkValidCaller(uid, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8433 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8434 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8435 | return mSettings.getApplicationEnabledSettingLPr(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8436 | } |
| 8437 | } |
| 8438 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8439 | @Override |
| 8440 | public int getComponentEnabledSetting(ComponentName componentName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8441 | if (!sUserManager.exists(userId)) return COMPONENT_ENABLED_STATE_DISABLED; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8442 | int uid = Binder.getCallingUid(); |
| 8443 | checkValidCaller(uid, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8444 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8445 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8446 | return mSettings.getComponentEnabledSettingLPr(componentName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8447 | } |
| 8448 | } |
| 8449 | |
| 8450 | public void enterSafeMode() { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 8451 | enforceSystemOrRoot("Only the system can request entering safe mode"); |
| 8452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8453 | if (!mSystemReady) { |
| 8454 | mSafeMode = true; |
| 8455 | } |
| 8456 | } |
| 8457 | |
| 8458 | public void systemReady() { |
| 8459 | mSystemReady = true; |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8460 | |
| 8461 | // Read the compatibilty setting when the system is ready. |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 8462 | boolean compatibilityModeEnabled = android.provider.Settings.System.getInt( |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8463 | mContext.getContentResolver(), |
| 8464 | android.provider.Settings.System.COMPATIBILITY_MODE, 1) == 1; |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 8465 | PackageParser.setCompatibilityModeEnabled(compatibilityModeEnabled); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8466 | if (DEBUG_SETTINGS) { |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 8467 | Log.d(TAG, "compatibility mode:" + compatibilityModeEnabled); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 8468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8469 | } |
| 8470 | |
| 8471 | public boolean isSafeMode() { |
| 8472 | return mSafeMode; |
| 8473 | } |
| 8474 | |
| 8475 | public boolean hasSystemUidErrors() { |
| 8476 | return mHasSystemUidErrors; |
| 8477 | } |
| 8478 | |
| 8479 | static String arrayToString(int[] array) { |
| 8480 | StringBuffer buf = new StringBuffer(128); |
| 8481 | buf.append('['); |
| 8482 | if (array != null) { |
| 8483 | for (int i=0; i<array.length; i++) { |
| 8484 | if (i > 0) buf.append(", "); |
| 8485 | buf.append(array[i]); |
| 8486 | } |
| 8487 | } |
| 8488 | buf.append(']'); |
| 8489 | return buf.toString(); |
| 8490 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8491 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8492 | static class DumpState { |
| 8493 | public static final int DUMP_LIBS = 1 << 0; |
| 8494 | |
| 8495 | public static final int DUMP_FEATURES = 1 << 1; |
| 8496 | |
| 8497 | public static final int DUMP_RESOLVERS = 1 << 2; |
| 8498 | |
| 8499 | public static final int DUMP_PERMISSIONS = 1 << 3; |
| 8500 | |
| 8501 | public static final int DUMP_PACKAGES = 1 << 4; |
| 8502 | |
| 8503 | public static final int DUMP_SHARED_USERS = 1 << 5; |
| 8504 | |
| 8505 | public static final int DUMP_MESSAGES = 1 << 6; |
| 8506 | |
| 8507 | public static final int DUMP_PROVIDERS = 1 << 7; |
| 8508 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8509 | public static final int DUMP_VERIFIERS = 1 << 8; |
| 8510 | |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 8511 | public static final int DUMP_PREFERRED = 1 << 9; |
| 8512 | |
| 8513 | public static final int DUMP_PREFERRED_XML = 1 << 10; |
| 8514 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8515 | public static final int OPTION_SHOW_FILTERS = 1 << 0; |
| 8516 | |
| 8517 | private int mTypes; |
| 8518 | |
| 8519 | private int mOptions; |
| 8520 | |
| 8521 | private boolean mTitlePrinted; |
| 8522 | |
| 8523 | private SharedUserSetting mSharedUser; |
| 8524 | |
| 8525 | public boolean isDumping(int type) { |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 8526 | if (mTypes == 0 && type != DUMP_PREFERRED_XML) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8527 | return true; |
| 8528 | } |
| 8529 | |
| 8530 | return (mTypes & type) != 0; |
| 8531 | } |
| 8532 | |
| 8533 | public void setDump(int type) { |
| 8534 | mTypes |= type; |
| 8535 | } |
| 8536 | |
| 8537 | public boolean isOptionEnabled(int option) { |
| 8538 | return (mOptions & option) != 0; |
| 8539 | } |
| 8540 | |
| 8541 | public void setOptionEnabled(int option) { |
| 8542 | mOptions |= option; |
| 8543 | } |
| 8544 | |
| 8545 | public boolean onTitlePrinted() { |
| 8546 | final boolean printed = mTitlePrinted; |
| 8547 | mTitlePrinted = true; |
| 8548 | return printed; |
| 8549 | } |
| 8550 | |
| 8551 | public boolean getTitlePrinted() { |
| 8552 | return mTitlePrinted; |
| 8553 | } |
| 8554 | |
| 8555 | public void setTitlePrinted(boolean enabled) { |
| 8556 | mTitlePrinted = enabled; |
| 8557 | } |
| 8558 | |
| 8559 | public SharedUserSetting getSharedUser() { |
| 8560 | return mSharedUser; |
| 8561 | } |
| 8562 | |
| 8563 | public void setSharedUser(SharedUserSetting user) { |
| 8564 | mSharedUser = user; |
| 8565 | } |
| 8566 | } |
| 8567 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8568 | @Override |
| 8569 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 8570 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 8571 | != PackageManager.PERMISSION_GRANTED) { |
| 8572 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8573 | + Binder.getCallingPid() |
| 8574 | + ", uid=" + Binder.getCallingUid() |
| 8575 | + " without permission " |
| 8576 | + android.Manifest.permission.DUMP); |
| 8577 | return; |
| 8578 | } |
| 8579 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8580 | DumpState dumpState = new DumpState(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8581 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8582 | String packageName = null; |
| 8583 | |
| 8584 | int opti = 0; |
| 8585 | while (opti < args.length) { |
| 8586 | String opt = args[opti]; |
| 8587 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8588 | break; |
| 8589 | } |
| 8590 | opti++; |
| 8591 | if ("-a".equals(opt)) { |
| 8592 | // Right now we only know how to print all. |
| 8593 | } else if ("-h".equals(opt)) { |
| 8594 | pw.println("Package manager dump options:"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8595 | pw.println(" [-h] [-f] [cmd] ..."); |
| 8596 | pw.println(" -f: print details of intent filters"); |
| 8597 | pw.println(" -h: print this help"); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8598 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8599 | pw.println(" l[ibraries]: list known shared libraries"); |
| 8600 | pw.println(" f[ibraries]: list device features"); |
| 8601 | pw.println(" r[esolvers]: dump intent resolvers"); |
| 8602 | pw.println(" perm[issions]: dump permissions"); |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 8603 | pw.println(" pref[erred]: print preferred package settings"); |
| 8604 | pw.println(" preferred-xml: print preferred package settings as xml"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8605 | pw.println(" prov[iders]: dump content providers"); |
| 8606 | pw.println(" p[ackages]: dump installed packages"); |
| 8607 | pw.println(" s[hared-users]: dump shared user IDs"); |
| 8608 | pw.println(" m[essages]: print collected runtime messages"); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8609 | pw.println(" v[erifiers]: print package verifier info"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8610 | pw.println(" <package.name>: info about given package"); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8611 | return; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8612 | } else if ("-f".equals(opt)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8613 | dumpState.setOptionEnabled(DumpState.OPTION_SHOW_FILTERS); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8614 | } else { |
| 8615 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 8616 | } |
| 8617 | } |
| 8618 | |
| 8619 | // Is the caller requesting to dump a particular piece of data? |
| 8620 | if (opti < args.length) { |
| 8621 | String cmd = args[opti]; |
| 8622 | opti++; |
| 8623 | // Is this a package name? |
| 8624 | if ("android".equals(cmd) || cmd.contains(".")) { |
| 8625 | packageName = cmd; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8626 | } else if ("l".equals(cmd) || "libraries".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8627 | dumpState.setDump(DumpState.DUMP_LIBS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8628 | } else if ("f".equals(cmd) || "features".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8629 | dumpState.setDump(DumpState.DUMP_FEATURES); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8630 | } else if ("r".equals(cmd) || "resolvers".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8631 | dumpState.setDump(DumpState.DUMP_RESOLVERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8632 | } else if ("perm".equals(cmd) || "permissions".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8633 | dumpState.setDump(DumpState.DUMP_PERMISSIONS); |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 8634 | } else if ("pref".equals(cmd) || "preferred".equals(cmd)) { |
| 8635 | dumpState.setDump(DumpState.DUMP_PREFERRED); |
| 8636 | } else if ("preferred-xml".equals(cmd)) { |
| 8637 | dumpState.setDump(DumpState.DUMP_PREFERRED_XML); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8638 | } else if ("p".equals(cmd) || "packages".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8639 | dumpState.setDump(DumpState.DUMP_PACKAGES); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8640 | } else if ("s".equals(cmd) || "shared-users".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8641 | dumpState.setDump(DumpState.DUMP_SHARED_USERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8642 | } else if ("prov".equals(cmd) || "providers".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8643 | dumpState.setDump(DumpState.DUMP_PROVIDERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8644 | } else if ("m".equals(cmd) || "messages".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8645 | dumpState.setDump(DumpState.DUMP_MESSAGES); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8646 | } else if ("v".equals(cmd) || "verifiers".equals(cmd)) { |
| 8647 | dumpState.setDump(DumpState.DUMP_VERIFIERS); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8648 | } |
| 8649 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8650 | |
| 8651 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8652 | synchronized (mPackages) { |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8653 | if (dumpState.isDumping(DumpState.DUMP_VERIFIERS) && packageName == null) { |
| 8654 | if (dumpState.onTitlePrinted()) |
| 8655 | pw.println(" "); |
| 8656 | pw.println("Verifiers:"); |
| 8657 | pw.print(" Required: "); |
| 8658 | pw.print(mRequiredVerifierPackage); |
| 8659 | pw.print(" (uid="); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8660 | pw.print(getPackageUid(mRequiredVerifierPackage, 0)); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 8661 | pw.println(")"); |
| 8662 | } |
| 8663 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8664 | if (dumpState.isDumping(DumpState.DUMP_LIBS) && packageName == null) { |
| 8665 | if (dumpState.onTitlePrinted()) |
| 8666 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8667 | pw.println("Libraries:"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8668 | final Iterator<String> it = mSharedLibraries.keySet().iterator(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8669 | while (it.hasNext()) { |
| 8670 | String name = it.next(); |
| 8671 | pw.print(" "); |
| 8672 | pw.print(name); |
| 8673 | pw.print(" -> "); |
| 8674 | pw.println(mSharedLibraries.get(name)); |
| 8675 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8676 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8677 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8678 | if (dumpState.isDumping(DumpState.DUMP_FEATURES) && packageName == null) { |
| 8679 | if (dumpState.onTitlePrinted()) |
| 8680 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8681 | pw.println("Features:"); |
| 8682 | Iterator<String> it = mAvailableFeatures.keySet().iterator(); |
| 8683 | while (it.hasNext()) { |
| 8684 | String name = it.next(); |
| 8685 | pw.print(" "); |
| 8686 | pw.println(name); |
| 8687 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8688 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8689 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8690 | if (dumpState.isDumping(DumpState.DUMP_RESOLVERS)) { |
| 8691 | if (mActivities.dump(pw, dumpState.getTitlePrinted() ? "\nActivity Resolver Table:" |
| 8692 | : "Activity Resolver Table:", " ", packageName, |
| 8693 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8694 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8695 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8696 | if (mReceivers.dump(pw, dumpState.getTitlePrinted() ? "\nReceiver Resolver Table:" |
| 8697 | : "Receiver Resolver Table:", " ", packageName, |
| 8698 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8699 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8700 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8701 | if (mServices.dump(pw, dumpState.getTitlePrinted() ? "\nService Resolver Table:" |
| 8702 | : "Service Resolver Table:", " ", packageName, |
| 8703 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8704 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8705 | } |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 8706 | } |
| 8707 | |
| 8708 | if (dumpState.isDumping(DumpState.DUMP_PREFERRED)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8709 | if (mSettings.mPreferredActivities.dump(pw, |
| 8710 | dumpState.getTitlePrinted() ? "\nPreferred Activities:" |
| 8711 | : "Preferred Activities:", " ", |
| 8712 | packageName, dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 8713 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8714 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8715 | } |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 8716 | |
| 8717 | if (dumpState.isDumping(DumpState.DUMP_PREFERRED_XML)) { |
| 8718 | pw.flush(); |
| 8719 | FileOutputStream fout = new FileOutputStream(fd); |
| 8720 | BufferedOutputStream str = new BufferedOutputStream(fout); |
| 8721 | XmlSerializer serializer = new FastXmlSerializer(); |
| 8722 | try { |
| 8723 | serializer.setOutput(str, "utf-8"); |
| 8724 | serializer.startDocument(null, true); |
| 8725 | serializer.setFeature( |
| 8726 | "http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 8727 | mSettings.writePreferredActivitiesLPr(serializer); |
| 8728 | serializer.endDocument(); |
| 8729 | serializer.flush(); |
| 8730 | } catch (IllegalArgumentException e) { |
| 8731 | pw.println("Failed writing: " + e); |
| 8732 | } catch (IllegalStateException e) { |
| 8733 | pw.println("Failed writing: " + e); |
| 8734 | } catch (IOException e) { |
| 8735 | pw.println("Failed writing: " + e); |
| 8736 | } |
| 8737 | } |
| 8738 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8739 | if (dumpState.isDumping(DumpState.DUMP_PERMISSIONS)) { |
| 8740 | mSettings.dumpPermissionsLPr(pw, packageName, dumpState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8741 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8742 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8743 | if (dumpState.isDumping(DumpState.DUMP_PROVIDERS)) { |
| 8744 | boolean printedSomething = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8745 | for (PackageParser.Provider p : mProvidersByComponent.values()) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8746 | if (packageName != null && !packageName.equals(p.info.packageName)) { |
| 8747 | continue; |
| 8748 | } |
| 8749 | if (!printedSomething) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8750 | if (dumpState.onTitlePrinted()) |
| 8751 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8752 | pw.println("Registered ContentProviders:"); |
| 8753 | printedSomething = true; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8754 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8755 | pw.print(" "); pw.print(p.getComponentShortName()); pw.println(":"); |
| 8756 | pw.print(" "); pw.println(p.toString()); |
| 8757 | } |
| 8758 | printedSomething = false; |
| 8759 | for (Map.Entry<String, PackageParser.Provider> entry : mProviders.entrySet()) { |
| 8760 | PackageParser.Provider p = entry.getValue(); |
| 8761 | if (packageName != null && !packageName.equals(p.info.packageName)) { |
| 8762 | continue; |
| 8763 | } |
| 8764 | if (!printedSomething) { |
| 8765 | if (dumpState.onTitlePrinted()) |
| 8766 | pw.println(" "); |
| 8767 | pw.println("ContentProvider Authorities:"); |
| 8768 | printedSomething = true; |
| 8769 | } |
| 8770 | pw.print(" ["); pw.print(entry.getKey()); pw.println("]:"); |
| 8771 | pw.print(" "); pw.println(p.toString()); |
Kenny Root | eca64b3 | 2012-06-25 16:37:32 -0700 | [diff] [blame] | 8772 | if (p.info != null && p.info.applicationInfo != null) { |
| 8773 | final String appInfo = p.info.applicationInfo.toString(); |
| 8774 | pw.print(" applicationInfo="); pw.println(appInfo); |
| 8775 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8776 | } |
| 8777 | } |
| 8778 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8779 | if (dumpState.isDumping(DumpState.DUMP_PACKAGES)) { |
| 8780 | mSettings.dumpPackagesLPr(pw, packageName, dumpState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8781 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8782 | |
| 8783 | if (dumpState.isDumping(DumpState.DUMP_SHARED_USERS)) { |
| 8784 | mSettings.dumpSharedUsersLPr(pw, packageName, dumpState); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8785 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8786 | |
| 8787 | if (dumpState.isDumping(DumpState.DUMP_MESSAGES) && packageName == null) { |
| 8788 | if (dumpState.onTitlePrinted()) |
| 8789 | pw.println(" "); |
| 8790 | mSettings.dumpReadMessagesLPr(pw, dumpState); |
| 8791 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8792 | pw.println(" "); |
| 8793 | pw.println("Package warning messages:"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8794 | final File fname = getSettingsProblemFile(); |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 8795 | FileInputStream in = null; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8796 | try { |
| 8797 | in = new FileInputStream(fname); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8798 | final int avail = in.available(); |
| 8799 | final byte[] data = new byte[avail]; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8800 | in.read(data); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 8801 | pw.print(new String(data)); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8802 | } catch (FileNotFoundException e) { |
| 8803 | } catch (IOException e) { |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 8804 | } finally { |
| 8805 | if (in != null) { |
| 8806 | try { |
| 8807 | in.close(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8808 | } catch (IOException e) { |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 8809 | } |
| 8810 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8811 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 8812 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8813 | } |
| 8814 | } |
| 8815 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8816 | // ------- apps on sdcard specific code ------- |
| 8817 | static final boolean DEBUG_SD_INSTALL = false; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8818 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8819 | private static final String SD_ENCRYPTION_KEYSTORE_NAME = "AppsOnSD"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8820 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8821 | private static final String SD_ENCRYPTION_ALGORITHM = "AES"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8822 | |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 8823 | private boolean mMediaMounted = false; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8824 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8825 | private String getEncryptKey() { |
| 8826 | try { |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8827 | String sdEncKey = SystemKeyStore.getInstance().retrieveKeyHexString( |
| 8828 | SD_ENCRYPTION_KEYSTORE_NAME); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8829 | if (sdEncKey == null) { |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 8830 | sdEncKey = SystemKeyStore.getInstance().generateNewKeyHexString(128, |
| 8831 | SD_ENCRYPTION_ALGORITHM, SD_ENCRYPTION_KEYSTORE_NAME); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8832 | if (sdEncKey == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8833 | Slog.e(TAG, "Failed to create encryption keys"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8834 | return null; |
| 8835 | } |
| 8836 | } |
| 8837 | return sdEncKey; |
| 8838 | } catch (NoSuchAlgorithmException nsae) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8839 | Slog.e(TAG, "Failed to create encryption keys with exception: " + nsae); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8840 | return null; |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 8841 | } catch (IOException ioe) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8842 | Slog.e(TAG, "Failed to retrieve encryption keys with exception: " + ioe); |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 8843 | return null; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8844 | } |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 8845 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8846 | } |
| 8847 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8848 | /* package */static String getTempContainerId() { |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 8849 | int tmpIdx = 1; |
| 8850 | String list[] = PackageHelper.getSecureContainerList(); |
| 8851 | if (list != null) { |
| 8852 | for (final String name : list) { |
| 8853 | // Ignore null and non-temporary container entries |
| 8854 | if (name == null || !name.startsWith(mTempContainerPrefix)) { |
| 8855 | continue; |
| 8856 | } |
| 8857 | |
| 8858 | String subStr = name.substring(mTempContainerPrefix.length()); |
| 8859 | try { |
| 8860 | int cid = Integer.parseInt(subStr); |
| 8861 | if (cid >= tmpIdx) { |
| 8862 | tmpIdx = cid + 1; |
| 8863 | } |
| 8864 | } catch (NumberFormatException e) { |
| 8865 | } |
| 8866 | } |
| 8867 | } |
| 8868 | return mTempContainerPrefix + tmpIdx; |
| 8869 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8870 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8871 | /* |
| 8872 | * Update media status on PackageManager. |
| 8873 | */ |
| 8874 | public void updateExternalMediaStatus(final boolean mediaStatus, final boolean reportStatus) { |
| 8875 | int callingUid = Binder.getCallingUid(); |
| 8876 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 8877 | throw new SecurityException("Media status can only be updated by the system"); |
| 8878 | } |
| 8879 | // reader; this apparently protects mMediaMounted, but should probably |
| 8880 | // be a different lock in that case. |
| 8881 | synchronized (mPackages) { |
| 8882 | Log.i(TAG, "Updating external media status from " |
| 8883 | + (mMediaMounted ? "mounted" : "unmounted") + " to " |
| 8884 | + (mediaStatus ? "mounted" : "unmounted")); |
| 8885 | if (DEBUG_SD_INSTALL) |
| 8886 | Log.i(TAG, "updateExternalMediaStatus:: mediaStatus=" + mediaStatus |
| 8887 | + ", mMediaMounted=" + mMediaMounted); |
| 8888 | if (mediaStatus == mMediaMounted) { |
| 8889 | final Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, reportStatus ? 1 |
| 8890 | : 0, -1); |
| 8891 | mHandler.sendMessage(msg); |
| 8892 | return; |
| 8893 | } |
| 8894 | mMediaMounted = mediaStatus; |
| 8895 | } |
| 8896 | // Queue up an async operation since the package installation may take a |
| 8897 | // little while. |
| 8898 | mHandler.post(new Runnable() { |
| 8899 | public void run() { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8900 | updateExternalMediaStatusInner(mediaStatus, reportStatus); |
| 8901 | } |
| 8902 | }); |
| 8903 | } |
| 8904 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 8905 | /** |
| 8906 | * Called by MountService when the initial ASECs to scan are available. |
| 8907 | * Should block until all the ASEC containers are finished being scanned. |
| 8908 | */ |
| 8909 | public void scanAvailableAsecs() { |
| 8910 | updateExternalMediaStatusInner(true, false); |
| 8911 | } |
| 8912 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8913 | /* |
| 8914 | * Collect information of applications on external media, map them against |
| 8915 | * existing containers and update information based on current mount status. |
| 8916 | * Please note that we always have to report status if reportStatus has been |
| 8917 | * set to true especially when unloading packages. |
| 8918 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8919 | private void updateExternalMediaStatusInner(boolean isMounted, boolean reportStatus) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8920 | // Collection of uids |
| 8921 | int uidArr[] = null; |
| 8922 | // Collection of stale containers |
| 8923 | HashSet<String> removeCids = new HashSet<String>(); |
| 8924 | // Collection of packages on external media with valid containers. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8925 | HashMap<AsecInstallArgs, String> processCids = new HashMap<AsecInstallArgs, String>(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8926 | // Get list of secure containers. |
| 8927 | final String list[] = PackageHelper.getSecureContainerList(); |
| 8928 | if (list == null || list.length == 0) { |
| 8929 | Log.i(TAG, "No secure containers on sdcard"); |
| 8930 | } else { |
| 8931 | // Process list of secure containers and categorize them |
| 8932 | // as active or stale based on their package internal state. |
| 8933 | int uidList[] = new int[list.length]; |
| 8934 | int num = 0; |
| 8935 | // reader |
| 8936 | synchronized (mPackages) { |
| 8937 | for (String cid : list) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8938 | if (DEBUG_SD_INSTALL) |
| 8939 | Log.i(TAG, "Processing container " + cid); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8940 | String pkgName = getAsecPackageName(cid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8941 | if (pkgName == null) { |
| 8942 | if (DEBUG_SD_INSTALL) |
| 8943 | Log.i(TAG, "Container : " + cid + " stale"); |
| 8944 | removeCids.add(cid); |
| 8945 | continue; |
| 8946 | } |
| 8947 | if (DEBUG_SD_INSTALL) |
| 8948 | Log.i(TAG, "Looking for pkg : " + pkgName); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8949 | |
| 8950 | final PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 8951 | if (ps == null) { |
| 8952 | Log.i(TAG, "Deleting container with no matching settings " + cid); |
| 8953 | removeCids.add(cid); |
| 8954 | continue; |
| 8955 | } |
| 8956 | |
| 8957 | final AsecInstallArgs args = new AsecInstallArgs(cid, isForwardLocked(ps)); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8958 | // The package status is changed only if the code path |
| 8959 | // matches between settings and the container id. |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8960 | if (ps.codePathString != null && ps.codePathString.equals(args.getCodePath())) { |
| 8961 | if (DEBUG_SD_INSTALL) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8962 | Log.i(TAG, "Container : " + cid + " corresponds to pkg : " + pkgName |
| 8963 | + " at code path: " + ps.codePathString); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8964 | } |
| 8965 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8966 | // We do have a valid package installed on sdcard |
| 8967 | processCids.put(args, ps.codePathString); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8968 | final int uid = ps.appId; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8969 | if (uid != -1) { |
| 8970 | uidList[num++] = uid; |
| 8971 | } |
| 8972 | } else { |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 8973 | Log.i(TAG, "Deleting stale container for " + cid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8974 | removeCids.add(cid); |
| 8975 | } |
| 8976 | } |
| 8977 | } |
| 8978 | |
| 8979 | if (num > 0) { |
| 8980 | // Sort uid list |
| 8981 | Arrays.sort(uidList, 0, num); |
| 8982 | // Throw away duplicates |
| 8983 | uidArr = new int[num]; |
| 8984 | uidArr[0] = uidList[0]; |
| 8985 | int di = 0; |
| 8986 | for (int i = 1; i < num; i++) { |
| 8987 | if (uidList[i - 1] != uidList[i]) { |
| 8988 | uidArr[di++] = uidList[i]; |
| 8989 | } |
| 8990 | } |
| 8991 | } |
| 8992 | } |
| 8993 | // Process packages with valid entries. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8994 | if (isMounted) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8995 | if (DEBUG_SD_INSTALL) |
| 8996 | Log.i(TAG, "Loading packages"); |
| 8997 | loadMediaPackages(processCids, uidArr, removeCids); |
| 8998 | startCleaningPackages(); |
| 8999 | } else { |
| 9000 | if (DEBUG_SD_INSTALL) |
| 9001 | Log.i(TAG, "Unloading packages"); |
| 9002 | unloadMediaPackages(processCids, uidArr, reportStatus); |
| 9003 | } |
| 9004 | } |
| 9005 | |
| 9006 | private void sendResourcesChangedBroadcast(boolean mediaStatus, ArrayList<String> pkgList, |
| 9007 | int uidArr[], IIntentReceiver finishedReceiver) { |
| 9008 | int size = pkgList.size(); |
| 9009 | if (size > 0) { |
| 9010 | // Send broadcasts here |
| 9011 | Bundle extras = new Bundle(); |
| 9012 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, pkgList |
| 9013 | .toArray(new String[size])); |
| 9014 | if (uidArr != null) { |
| 9015 | extras.putIntArray(Intent.EXTRA_CHANGED_UID_LIST, uidArr); |
| 9016 | } |
| 9017 | String action = mediaStatus ? Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE |
| 9018 | : Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9019 | sendPackageBroadcast(action, null, extras, null, finishedReceiver, UserId.USER_ALL); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9020 | } |
| 9021 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9022 | |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 9023 | /* |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9024 | * Look at potentially valid container ids from processCids If package |
| 9025 | * information doesn't match the one on record or package scanning fails, |
| 9026 | * the cid is added to list of removeCids. We currently don't delete stale |
| 9027 | * containers. |
| 9028 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9029 | private void loadMediaPackages(HashMap<AsecInstallArgs, String> processCids, int uidArr[], |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9030 | HashSet<String> removeCids) { |
| 9031 | ArrayList<String> pkgList = new ArrayList<String>(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9032 | Set<AsecInstallArgs> keys = processCids.keySet(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9033 | boolean doGc = false; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9034 | for (AsecInstallArgs args : keys) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9035 | String codePath = processCids.get(args); |
| 9036 | if (DEBUG_SD_INSTALL) |
| 9037 | Log.i(TAG, "Loading container : " + args.cid); |
| 9038 | int retCode = PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 9039 | try { |
| 9040 | // Make sure there are no container errors first. |
| 9041 | if (args.doPreInstall(PackageManager.INSTALL_SUCCEEDED) != PackageManager.INSTALL_SUCCEEDED) { |
| 9042 | Slog.e(TAG, "Failed to mount cid : " + args.cid |
| 9043 | + " when installing from sdcard"); |
| 9044 | continue; |
| 9045 | } |
| 9046 | // Check code path here. |
| 9047 | if (codePath == null || !codePath.equals(args.getCodePath())) { |
| 9048 | Slog.e(TAG, "Container " + args.cid + " cachepath " + args.getCodePath() |
| 9049 | + " does not match one in settings " + codePath); |
| 9050 | continue; |
| 9051 | } |
| 9052 | // Parse package |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 9053 | int parseFlags = mDefParseFlags; |
| 9054 | if (args.isExternal()) { |
| 9055 | parseFlags |= PackageParser.PARSE_ON_SDCARD; |
| 9056 | } |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 9057 | if (args.isFwdLocked()) { |
| 9058 | parseFlags |= PackageParser.PARSE_FORWARD_LOCK; |
| 9059 | } |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 9060 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9061 | doGc = true; |
| 9062 | synchronized (mInstallLock) { |
| 9063 | final PackageParser.Package pkg = scanPackageLI(new File(codePath), parseFlags, |
| 9064 | 0, 0); |
| 9065 | // Scan the package |
| 9066 | if (pkg != null) { |
| 9067 | /* |
| 9068 | * TODO why is the lock being held? doPostInstall is |
| 9069 | * called in other places without the lock. This needs |
| 9070 | * to be straightened out. |
| 9071 | */ |
| 9072 | // writer |
| 9073 | synchronized (mPackages) { |
| 9074 | retCode = PackageManager.INSTALL_SUCCEEDED; |
| 9075 | pkgList.add(pkg.packageName); |
| 9076 | // Post process args |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9077 | args.doPostInstall(PackageManager.INSTALL_SUCCEEDED, |
| 9078 | pkg.applicationInfo.uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9079 | } |
| 9080 | } else { |
| 9081 | Slog.i(TAG, "Failed to install pkg from " + codePath + " from sdcard"); |
| 9082 | } |
| 9083 | } |
| 9084 | |
| 9085 | } finally { |
| 9086 | if (retCode != PackageManager.INSTALL_SUCCEEDED) { |
| 9087 | // Don't destroy container here. Wait till gc clears things |
| 9088 | // up. |
| 9089 | removeCids.add(args.cid); |
| 9090 | } |
| 9091 | } |
| 9092 | } |
| 9093 | // writer |
| 9094 | synchronized (mPackages) { |
| 9095 | // If the platform SDK has changed since the last time we booted, |
| 9096 | // we need to re-grant app permission to catch any new ones that |
| 9097 | // appear. This is really a hack, and means that apps can in some |
| 9098 | // cases get permissions that the user didn't initially explicitly |
| 9099 | // allow... it would be nice to have some better way to handle |
| 9100 | // this situation. |
| 9101 | final boolean regrantPermissions = mSettings.mExternalSdkPlatform != mSdkVersion; |
| 9102 | if (regrantPermissions) |
| 9103 | Slog.i(TAG, "Platform changed from " + mSettings.mExternalSdkPlatform + " to " |
| 9104 | + mSdkVersion + "; regranting permissions for external storage"); |
| 9105 | mSettings.mExternalSdkPlatform = mSdkVersion; |
| 9106 | |
| 9107 | // Make sure group IDs have been assigned, and any permission |
| 9108 | // changes in other apps are accounted for |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 9109 | updatePermissionsLPw(null, null, UPDATE_PERMISSIONS_ALL |
| 9110 | | (regrantPermissions |
| 9111 | ? (UPDATE_PERMISSIONS_REPLACE_PKG|UPDATE_PERMISSIONS_REPLACE_ALL) |
| 9112 | : 0)); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9113 | // can downgrade to reader |
| 9114 | // Persist settings |
| 9115 | mSettings.writeLPr(); |
| 9116 | } |
| 9117 | // Send a broadcast to let everyone know we are done processing |
| 9118 | if (pkgList.size() > 0) { |
| 9119 | sendResourcesChangedBroadcast(true, pkgList, uidArr, null); |
| 9120 | } |
| 9121 | // Force gc to avoid any stale parser references that we might have. |
| 9122 | if (doGc) { |
| 9123 | Runtime.getRuntime().gc(); |
| 9124 | } |
| 9125 | // List stale containers and destroy stale temporary containers. |
| 9126 | if (removeCids != null) { |
| 9127 | for (String cid : removeCids) { |
| 9128 | if (cid.startsWith(mTempContainerPrefix)) { |
| 9129 | Log.i(TAG, "Destroying stale temporary container " + cid); |
| 9130 | PackageHelper.destroySdDir(cid); |
| 9131 | } else { |
| 9132 | Log.w(TAG, "Container " + cid + " is stale"); |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 9133 | } |
| 9134 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9135 | } |
| 9136 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9137 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9138 | /* |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9139 | * Utility method to unload a list of specified containers |
| 9140 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9141 | private void unloadAllContainers(Set<AsecInstallArgs> cidArgs) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9142 | // Just unmount all valid containers. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9143 | for (AsecInstallArgs arg : cidArgs) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9144 | synchronized (mInstallLock) { |
| 9145 | arg.doPostDeleteLI(false); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 9146 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 9147 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 9148 | } |
| 9149 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9150 | /* |
| 9151 | * Unload packages mounted on external media. This involves deleting package |
| 9152 | * data from internal structures, sending broadcasts about diabled packages, |
| 9153 | * gc'ing to free up references, unmounting all secure containers |
| 9154 | * corresponding to packages on external media, and posting a |
| 9155 | * UPDATED_MEDIA_STATUS message if status has been requested. Please note |
| 9156 | * that we always have to post this message if status has been requested no |
| 9157 | * matter what. |
| 9158 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9159 | private void unloadMediaPackages(HashMap<AsecInstallArgs, String> processCids, int uidArr[], |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9160 | final boolean reportStatus) { |
| 9161 | if (DEBUG_SD_INSTALL) |
| 9162 | Log.i(TAG, "unloading media packages"); |
| 9163 | ArrayList<String> pkgList = new ArrayList<String>(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9164 | ArrayList<AsecInstallArgs> failedList = new ArrayList<AsecInstallArgs>(); |
| 9165 | final Set<AsecInstallArgs> keys = processCids.keySet(); |
| 9166 | for (AsecInstallArgs args : keys) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9167 | String pkgName = args.getPackageName(); |
| 9168 | if (DEBUG_SD_INSTALL) |
| 9169 | Log.i(TAG, "Trying to unload pkg : " + pkgName); |
| 9170 | // Delete package internally |
| 9171 | PackageRemovedInfo outInfo = new PackageRemovedInfo(); |
| 9172 | synchronized (mInstallLock) { |
| 9173 | boolean res = deletePackageLI(pkgName, false, PackageManager.DONT_DELETE_DATA, |
| 9174 | outInfo, false); |
| 9175 | if (res) { |
| 9176 | pkgList.add(pkgName); |
| 9177 | } else { |
| 9178 | Slog.e(TAG, "Failed to delete pkg from sdcard : " + pkgName); |
| 9179 | failedList.add(args); |
| 9180 | } |
| 9181 | } |
| 9182 | } |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 9183 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9184 | // reader |
| 9185 | synchronized (mPackages) { |
| 9186 | // We didn't update the settings after removing each package; |
| 9187 | // write them now for all packages. |
| 9188 | mSettings.writeLPr(); |
| 9189 | } |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 9190 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9191 | // We have to absolutely send UPDATED_MEDIA_STATUS only |
| 9192 | // after confirming that all the receivers processed the ordered |
| 9193 | // broadcast when packages get disabled, force a gc to clean things up. |
| 9194 | // and unload all the containers. |
| 9195 | if (pkgList.size() > 0) { |
| 9196 | sendResourcesChangedBroadcast(false, pkgList, uidArr, new IIntentReceiver.Stub() { |
| 9197 | public void performReceive(Intent intent, int resultCode, String data, |
| 9198 | Bundle extras, boolean ordered, boolean sticky) throws RemoteException { |
| 9199 | Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, |
| 9200 | reportStatus ? 1 : 0, 1, keys); |
| 9201 | mHandler.sendMessage(msg); |
| 9202 | } |
| 9203 | }); |
| 9204 | } else { |
| 9205 | Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, reportStatus ? 1 : 0, -1, |
| 9206 | keys); |
| 9207 | mHandler.sendMessage(msg); |
| 9208 | } |
| 9209 | } |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 9210 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9211 | public void movePackage(final String packageName, final IPackageMoveObserver observer, |
| 9212 | final int flags) { |
| 9213 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MOVE_PACKAGE, null); |
| 9214 | int returnCode = PackageManager.MOVE_SUCCEEDED; |
| 9215 | int currFlags = 0; |
| 9216 | int newFlags = 0; |
| 9217 | // reader |
| 9218 | synchronized (mPackages) { |
| 9219 | PackageParser.Package pkg = mPackages.get(packageName); |
| 9220 | if (pkg == null) { |
| 9221 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
| 9222 | } else { |
| 9223 | // Disable moving fwd locked apps and system packages |
| 9224 | if (pkg.applicationInfo != null && isSystemApp(pkg)) { |
| 9225 | Slog.w(TAG, "Cannot move system application"); |
| 9226 | returnCode = PackageManager.MOVE_FAILED_SYSTEM_PACKAGE; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9227 | } else if (pkg.mOperationPending) { |
| 9228 | Slog.w(TAG, "Attempt to move package which has pending operations"); |
| 9229 | returnCode = PackageManager.MOVE_FAILED_OPERATION_PENDING; |
| 9230 | } else { |
| 9231 | // Find install location first |
| 9232 | if ((flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 |
| 9233 | && (flags & PackageManager.MOVE_INTERNAL) != 0) { |
| 9234 | Slog.w(TAG, "Ambigous flags specified for move location."); |
| 9235 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
| 9236 | } else { |
| 9237 | newFlags = (flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 ? PackageManager.INSTALL_EXTERNAL |
| 9238 | : PackageManager.INSTALL_INTERNAL; |
| 9239 | currFlags = isExternal(pkg) ? PackageManager.INSTALL_EXTERNAL |
| 9240 | : PackageManager.INSTALL_INTERNAL; |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9241 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9242 | if (newFlags == currFlags) { |
| 9243 | Slog.w(TAG, "No move required. Trying to move to same location"); |
| 9244 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9245 | } else { |
| 9246 | if (isForwardLocked(pkg)) { |
| 9247 | currFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 9248 | newFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 9249 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9250 | } |
| 9251 | } |
| 9252 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 9253 | pkg.mOperationPending = true; |
| 9254 | } |
| 9255 | } |
| 9256 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9257 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9258 | /* |
| 9259 | * TODO this next block probably shouldn't be inside the lock. We |
| 9260 | * can't guarantee these won't change after this is fired off |
| 9261 | * anyway. |
| 9262 | */ |
| 9263 | if (returnCode != PackageManager.MOVE_SUCCEEDED) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9264 | processPendingMove(new MoveParams(null, observer, 0, packageName, null, -1), |
| 9265 | returnCode); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9266 | } else { |
| 9267 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| 9268 | InstallArgs srcArgs = createInstallArgs(currFlags, pkg.applicationInfo.sourceDir, |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 9269 | pkg.applicationInfo.publicSourceDir, pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9270 | MoveParams mp = new MoveParams(srcArgs, observer, newFlags, packageName, |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9271 | pkg.applicationInfo.dataDir, pkg.applicationInfo.uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9272 | msg.obj = mp; |
| 9273 | mHandler.sendMessage(msg); |
| 9274 | } |
| 9275 | } |
| 9276 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9277 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9278 | private void processPendingMove(final MoveParams mp, final int currentStatus) { |
| 9279 | // Queue up an async operation since the package deletion may take a |
| 9280 | // little while. |
| 9281 | mHandler.post(new Runnable() { |
| 9282 | public void run() { |
| 9283 | // TODO fix this; this does nothing. |
| 9284 | mHandler.removeCallbacks(this); |
| 9285 | int returnCode = currentStatus; |
| 9286 | if (currentStatus == PackageManager.MOVE_SUCCEEDED) { |
| 9287 | int uidArr[] = null; |
| 9288 | ArrayList<String> pkgList = null; |
| 9289 | synchronized (mPackages) { |
| 9290 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 9291 | if (pkg == null) { |
| 9292 | Slog.w(TAG, " Package " + mp.packageName |
| 9293 | + " doesn't exist. Aborting move"); |
| 9294 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
| 9295 | } else if (!mp.srcArgs.getCodePath().equals(pkg.applicationInfo.sourceDir)) { |
| 9296 | Slog.w(TAG, "Package " + mp.packageName + " code path changed from " |
| 9297 | + mp.srcArgs.getCodePath() + " to " |
| 9298 | + pkg.applicationInfo.sourceDir |
| 9299 | + " Aborting move and returning error"); |
| 9300 | returnCode = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 9301 | } else { |
| 9302 | uidArr = new int[] { |
| 9303 | pkg.applicationInfo.uid |
| 9304 | }; |
| 9305 | pkgList = new ArrayList<String>(); |
| 9306 | pkgList.add(mp.packageName); |
| 9307 | } |
| 9308 | } |
| 9309 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 9310 | // Send resources unavailable broadcast |
| 9311 | sendResourcesChangedBroadcast(false, pkgList, uidArr, null); |
| 9312 | // Update package code and resource paths |
| 9313 | synchronized (mInstallLock) { |
| 9314 | synchronized (mPackages) { |
| 9315 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 9316 | // Recheck for package again. |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9317 | if (pkg == null) { |
| 9318 | Slog.w(TAG, " Package " + mp.packageName |
| 9319 | + " doesn't exist. Aborting move"); |
| 9320 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9321 | } else if (!mp.srcArgs.getCodePath().equals( |
| 9322 | pkg.applicationInfo.sourceDir)) { |
| 9323 | Slog.w(TAG, "Package " + mp.packageName |
| 9324 | + " code path changed from " + mp.srcArgs.getCodePath() |
| 9325 | + " to " + pkg.applicationInfo.sourceDir |
| 9326 | + " Aborting move and returning error"); |
| 9327 | returnCode = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 9328 | } else { |
| 9329 | final String oldCodePath = pkg.mPath; |
| 9330 | final String newCodePath = mp.targetArgs.getCodePath(); |
| 9331 | final String newResPath = mp.targetArgs.getResourcePath(); |
| 9332 | final String newNativePath = mp.targetArgs |
| 9333 | .getNativeLibraryPath(); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9334 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9335 | try { |
| 9336 | final File newNativeDir = new File(newNativePath); |
| 9337 | |
| 9338 | final String libParentDir = newNativeDir.getParentFile() |
| 9339 | .getCanonicalPath(); |
| 9340 | if (newNativeDir.getParentFile().getCanonicalPath() |
| 9341 | .equals(pkg.applicationInfo.dataDir)) { |
| 9342 | if (mInstaller |
| 9343 | .unlinkNativeLibraryDirectory(pkg.applicationInfo.dataDir) < 0) { |
| 9344 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 9345 | } else { |
| 9346 | NativeLibraryHelper.copyNativeBinariesIfNeededLI( |
| 9347 | new File(newCodePath), newNativeDir); |
| 9348 | } |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9349 | } else { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9350 | if (mInstaller.linkNativeLibraryDirectory( |
| 9351 | pkg.applicationInfo.dataDir, newNativePath) < 0) { |
| 9352 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 9353 | } |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9354 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9355 | } catch (IOException e) { |
| 9356 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9357 | } |
| 9358 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 9359 | |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 9360 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 9361 | pkg.mPath = newCodePath; |
| 9362 | // Move dex files around |
| 9363 | if (moveDexFilesLI(pkg) != PackageManager.INSTALL_SUCCEEDED) { |
| 9364 | // Moving of dex files failed. Set |
| 9365 | // error code and abort move. |
| 9366 | pkg.mPath = pkg.mScanPath; |
| 9367 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 9368 | } |
| 9369 | } |
| 9370 | |
| 9371 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9372 | pkg.mScanPath = newCodePath; |
| 9373 | pkg.applicationInfo.sourceDir = newCodePath; |
| 9374 | pkg.applicationInfo.publicSourceDir = newResPath; |
| 9375 | pkg.applicationInfo.nativeLibraryDir = newNativePath; |
| 9376 | PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 9377 | ps.codePath = new File(pkg.applicationInfo.sourceDir); |
| 9378 | ps.codePathString = ps.codePath.getPath(); |
| 9379 | ps.resourcePath = new File( |
| 9380 | pkg.applicationInfo.publicSourceDir); |
| 9381 | ps.resourcePathString = ps.resourcePath.getPath(); |
| 9382 | ps.nativeLibraryPathString = newNativePath; |
| 9383 | // Set the application info flag |
| 9384 | // correctly. |
| 9385 | if ((mp.flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
| 9386 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| 9387 | } else { |
| 9388 | pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| 9389 | } |
| 9390 | ps.setFlags(pkg.applicationInfo.flags); |
| 9391 | mAppDirs.remove(oldCodePath); |
| 9392 | mAppDirs.put(newCodePath, pkg); |
| 9393 | // Persist settings |
| 9394 | mSettings.writeLPr(); |
| 9395 | } |
| 9396 | } |
| 9397 | } |
| 9398 | } |
| 9399 | // Send resources available broadcast |
| 9400 | sendResourcesChangedBroadcast(true, pkgList, uidArr, null); |
| 9401 | } |
| 9402 | } |
| 9403 | if (returnCode != PackageManager.MOVE_SUCCEEDED) { |
| 9404 | // Clean up failed installation |
| 9405 | if (mp.targetArgs != null) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 9406 | mp.targetArgs.doPostInstall(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, |
| 9407 | -1); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9408 | } |
| 9409 | } else { |
| 9410 | // Force a gc to clear things up. |
| 9411 | Runtime.getRuntime().gc(); |
| 9412 | // Delete older code |
| 9413 | synchronized (mInstallLock) { |
| 9414 | mp.srcArgs.doPostDeleteLI(true); |
| 9415 | } |
| 9416 | } |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 9417 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9418 | // Allow more operations on this file if we didn't fail because |
| 9419 | // an operation was already pending for this package. |
| 9420 | if (returnCode != PackageManager.MOVE_FAILED_OPERATION_PENDING) { |
| 9421 | synchronized (mPackages) { |
| 9422 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 9423 | if (pkg != null) { |
| 9424 | pkg.mOperationPending = false; |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 9425 | } |
| 9426 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9427 | } |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 9428 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9429 | IPackageMoveObserver observer = mp.observer; |
| 9430 | if (observer != null) { |
| 9431 | try { |
| 9432 | observer.packageMoved(mp.packageName, returnCode); |
| 9433 | } catch (RemoteException e) { |
| 9434 | Log.i(TAG, "Observer no longer exists."); |
| 9435 | } |
| 9436 | } |
| 9437 | } |
| 9438 | }); |
| 9439 | } |
| 9440 | |
| 9441 | public boolean setInstallLocation(int loc) { |
| 9442 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS, |
| 9443 | null); |
| 9444 | if (getInstallLocation() == loc) { |
| 9445 | return true; |
| 9446 | } |
| 9447 | if (loc == PackageHelper.APP_INSTALL_AUTO || loc == PackageHelper.APP_INSTALL_INTERNAL |
| 9448 | || loc == PackageHelper.APP_INSTALL_EXTERNAL) { |
| 9449 | android.provider.Settings.System.putInt(mContext.getContentResolver(), |
| 9450 | android.provider.Settings.Secure.DEFAULT_INSTALL_LOCATION, loc); |
| 9451 | return true; |
| 9452 | } |
| 9453 | return false; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 9454 | } |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 9455 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9456 | public int getInstallLocation() { |
| 9457 | return android.provider.Settings.System.getInt(mContext.getContentResolver(), |
| 9458 | android.provider.Settings.Secure.DEFAULT_INSTALL_LOCATION, |
| 9459 | PackageHelper.APP_INSTALL_AUTO); |
| 9460 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9461 | |
| 9462 | public UserInfo createUser(String name, int flags) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 9463 | // TODO(kroot): Add a real permission for creating users |
| 9464 | enforceSystemOrRoot("Only the system can create users"); |
| 9465 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9466 | UserInfo userInfo = sUserManager.createUser(name, flags); |
| 9467 | if (userInfo != null) { |
| 9468 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 9469 | addedIntent.putExtra(Intent.EXTRA_USERID, userInfo.id); |
| 9470 | mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_ACCOUNTS); |
| 9471 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9472 | return userInfo; |
| 9473 | } |
| 9474 | |
| 9475 | public boolean removeUser(int userId) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 9476 | // TODO(kroot): Add a real permission for removing users |
| 9477 | enforceSystemOrRoot("Only the system can remove users"); |
| 9478 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9479 | if (userId == 0 || !sUserManager.exists(userId)) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9480 | return false; |
| 9481 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9482 | |
| 9483 | cleanUpUser(userId); |
| 9484 | |
| 9485 | if (sUserManager.removeUser(userId)) { |
| 9486 | // Let other services shutdown any activity |
| 9487 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 9488 | addedIntent.putExtra(Intent.EXTRA_USERID, userId); |
| 9489 | mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_ACCOUNTS); |
| 9490 | } |
| 9491 | sUserManager.removePackageFolders(userId); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 9492 | return true; |
| 9493 | } |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 9494 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9495 | private void cleanUpUser(int userId) { |
| 9496 | // Disable all the packages for the user first |
| 9497 | synchronized (mPackages) { |
| 9498 | Set<Entry<String, PackageSetting>> entries = mSettings.mPackages.entrySet(); |
| 9499 | for (Entry<String, PackageSetting> entry : entries) { |
| 9500 | entry.getValue().removeUser(userId); |
| 9501 | } |
| 9502 | if (mDirtyUsers.remove(userId)); |
| 9503 | mSettings.removeUserLPr(userId); |
| 9504 | } |
| 9505 | } |
| 9506 | |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 9507 | @Override |
| 9508 | public VerifierDeviceIdentity getVerifierDeviceIdentity() throws RemoteException { |
| 9509 | mContext.enforceCallingOrSelfPermission( |
| 9510 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 9511 | "Only package verification agents can read the verifier device identity"); |
| 9512 | |
| 9513 | synchronized (mPackages) { |
| 9514 | return mSettings.getVerifierDeviceIdentityLPw(); |
| 9515 | } |
| 9516 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9517 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9518 | @Override |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9519 | public List<UserInfo> getUsers() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9520 | enforceSystemOrRoot("Only the system can query users"); |
| 9521 | return sUserManager.getUsers(); |
| 9522 | } |
| 9523 | |
| 9524 | @Override |
| 9525 | public UserInfo getUser(int userId) { |
Amith Yamasani | 5bb87cd | 2012-06-14 11:32:13 -0700 | [diff] [blame] | 9526 | enforceSystemOrRoot("Only the system can query user"); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9527 | return sUserManager.getUser(userId); |
| 9528 | } |
| 9529 | |
| 9530 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 9531 | public void setUserName(int userId, String name) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9532 | enforceSystemOrRoot("Only the system can rename users"); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 9533 | sUserManager.setUserName(userId, name); |
| 9534 | } |
| 9535 | |
| 9536 | @Override |
| 9537 | public ParcelFileDescriptor setUserIcon(int userId) { |
| 9538 | enforceSystemOrRoot("Only the system can update users"); |
| 9539 | return sUserManager.setUserIcon(userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9540 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9541 | |
| 9542 | @Override |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9543 | public void setPermissionEnforced(String permission, boolean enforced) { |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9544 | mContext.enforceCallingOrSelfPermission(GRANT_REVOKE_PERMISSIONS, null); |
| 9545 | if (READ_EXTERNAL_STORAGE.equals(permission)) { |
| 9546 | synchronized (mPackages) { |
Jeff Sharkey | f538577 | 2012-05-11 14:04:41 -0700 | [diff] [blame] | 9547 | if (mSettings.mReadExternalStorageEnforced == null |
| 9548 | || mSettings.mReadExternalStorageEnforced != enforced) { |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9549 | mSettings.mReadExternalStorageEnforced = enforced; |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9550 | mSettings.writeLPr(); |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 9551 | |
| 9552 | // kill any non-foreground processes so we restart them and |
| 9553 | // grant/revoke the GID. |
| 9554 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 9555 | if (am != null) { |
| 9556 | final long token = Binder.clearCallingIdentity(); |
| 9557 | try { |
| 9558 | am.killProcessesBelowForeground("setPermissionEnforcement"); |
| 9559 | } catch (RemoteException e) { |
| 9560 | } finally { |
| 9561 | Binder.restoreCallingIdentity(token); |
| 9562 | } |
| 9563 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9564 | } |
| 9565 | } |
| 9566 | } else { |
| 9567 | throw new IllegalArgumentException("No selective enforcement for " + permission); |
| 9568 | } |
| 9569 | } |
| 9570 | |
| 9571 | @Override |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9572 | public boolean isPermissionEnforced(String permission) { |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9573 | synchronized (mPackages) { |
| 9574 | return isPermissionEnforcedLocked(permission); |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9575 | } |
| 9576 | } |
| 9577 | |
| 9578 | private boolean isPermissionEnforcedLocked(String permission) { |
| 9579 | if (READ_EXTERNAL_STORAGE.equals(permission)) { |
Jeff Sharkey | f538577 | 2012-05-11 14:04:41 -0700 | [diff] [blame] | 9580 | if (mSettings.mReadExternalStorageEnforced != null) { |
| 9581 | return mSettings.mReadExternalStorageEnforced; |
| 9582 | } else { |
| 9583 | // if user hasn't defined, fall back to secure default |
| 9584 | return Secure.getInt(mContext.getContentResolver(), |
| 9585 | Secure.READ_EXTERNAL_STORAGE_ENFORCED_DEFAULT, 0) != 0; |
| 9586 | } |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 9587 | } else { |
| 9588 | return true; |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9589 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 9590 | } |
Jeff Sharkey | f6f7f1d | 2012-06-12 17:12:08 -0700 | [diff] [blame] | 9591 | |
| 9592 | public boolean isStorageLow() { |
| 9593 | final long token = Binder.clearCallingIdentity(); |
| 9594 | try { |
| 9595 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 9596 | .getService(DeviceStorageMonitorService.SERVICE); |
| 9597 | return dsm.isMemoryLow(); |
| 9598 | } finally { |
| 9599 | Binder.restoreCallingIdentity(token); |
| 9600 | } |
| 9601 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9602 | } |