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 | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED; |
Dianne Hackborn | 0ac3031 | 2011-06-17 14:49:23 -0700 | [diff] [blame] | 24 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 25 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 26 | import static com.android.internal.util.ArrayUtils.appendInt; |
| 27 | import static com.android.internal.util.ArrayUtils.removeInt; |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 28 | import static libcore.io.OsConstants.S_IRWXU; |
| 29 | import static libcore.io.OsConstants.S_IRGRP; |
| 30 | import static libcore.io.OsConstants.S_IXGRP; |
| 31 | import static libcore.io.OsConstants.S_IROTH; |
| 32 | import static libcore.io.OsConstants.S_IXOTH; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 33 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 34 | import com.android.internal.app.IMediaContainerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import com.android.internal.app.ResolverActivity; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 36 | import com.android.internal.content.NativeLibraryHelper; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 37 | import com.android.internal.content.PackageHelper; |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 38 | import com.android.internal.util.FastXmlSerializer; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 39 | import com.android.internal.util.XmlUtils; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 40 | import com.android.server.DeviceStorageMonitorService; |
| 41 | import com.android.server.EventLogTags; |
| 42 | import com.android.server.IntentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 44 | import org.xmlpull.v1.XmlPullParser; |
| 45 | import org.xmlpull.v1.XmlPullParserException; |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 46 | import org.xmlpull.v1.XmlSerializer; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 47 | |
rich cannings | a2064df | 2012-09-30 14:52:41 -0700 | [diff] [blame] | 48 | import android.app.ActivityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.app.ActivityManagerNative; |
| 50 | import android.app.IActivityManager; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 51 | import android.app.admin.IDevicePolicyManager; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 52 | import android.app.backup.IBackupManager; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 53 | import android.content.BroadcastReceiver; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 54 | import android.content.ComponentName; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 55 | import android.content.Context; |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 56 | import android.content.IIntentReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.content.Intent; |
| 58 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 59 | import android.content.IntentSender; |
Jason parks | 1125d78 | 2011-01-12 09:47:26 -0600 | [diff] [blame] | 60 | import android.content.ServiceConnection; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 61 | import android.content.IntentSender.SendIntentException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.content.pm.ActivityInfo; |
| 63 | import android.content.pm.ApplicationInfo; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 64 | import android.content.pm.ContainerEncryptionParams; |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 65 | import android.content.pm.FeatureInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.content.pm.IPackageDataObserver; |
| 67 | import android.content.pm.IPackageDeleteObserver; |
| 68 | import android.content.pm.IPackageInstallObserver; |
| 69 | import android.content.pm.IPackageManager; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 70 | import android.content.pm.IPackageMoveObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import android.content.pm.IPackageStatsObserver; |
| 72 | import android.content.pm.InstrumentationInfo; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 73 | import android.content.pm.PackageCleanItem; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | import android.content.pm.PackageInfo; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 75 | import android.content.pm.PackageInfoLite; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.content.pm.PackageManager; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 77 | import android.content.pm.PackageParser; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 78 | import android.content.pm.PackageUserState; |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 79 | import android.content.pm.PackageParser.ActivityIntentInfo; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 80 | import android.content.pm.PackageStats; |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 81 | import android.content.pm.ParceledListSlice; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 82 | import android.content.pm.PermissionGroupInfo; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 83 | import android.content.pm.PermissionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.content.pm.ProviderInfo; |
| 85 | import android.content.pm.ResolveInfo; |
| 86 | import android.content.pm.ServiceInfo; |
| 87 | import android.content.pm.Signature; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 88 | import android.content.pm.ManifestDigest; |
rich cannings | 706e8ba | 2012-08-20 13:20:14 -0700 | [diff] [blame] | 89 | import android.content.pm.VerificationParams; |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 90 | import android.content.pm.VerifierDeviceIdentity; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 91 | import android.content.pm.VerifierInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.net.Uri; |
| 93 | import android.os.Binder; |
Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 94 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.os.Bundle; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 96 | import android.os.Environment; |
| 97 | import android.os.FileObserver; |
| 98 | import android.os.FileUtils; |
| 99 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.os.HandlerThread; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 101 | import android.os.IBinder; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 102 | import android.os.Looper; |
| 103 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.os.ParcelFileDescriptor; |
| 106 | import android.os.Process; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 107 | import android.os.RemoteException; |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 108 | import android.os.SELinux; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | import android.os.ServiceManager; |
| 110 | import android.os.SystemClock; |
| 111 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 112 | import android.os.UserHandle; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 113 | import android.os.Environment.UserEnvironment; |
Kenny Root | d72317a | 2013-04-01 15:59:59 -0700 | [diff] [blame] | 114 | import android.os.UserManager; |
| 115 | import android.provider.Settings.Secure; |
| 116 | import android.security.KeyStore; |
Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 117 | import android.security.SystemKeyStore; |
Kenny Root | 2798942 | 2011-02-23 16:28:26 -0800 | [diff] [blame] | 118 | import android.util.DisplayMetrics; |
| 119 | import android.util.EventLog; |
| 120 | import android.util.Log; |
| 121 | import android.util.LogPrinter; |
| 122 | import android.util.Slog; |
| 123 | import android.util.SparseArray; |
| 124 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | import android.view.Display; |
| 126 | import android.view.WindowManager; |
| 127 | |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 128 | import java.io.BufferedOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | import java.io.File; |
| 130 | import java.io.FileDescriptor; |
| 131 | import java.io.FileInputStream; |
| 132 | import java.io.FileNotFoundException; |
| 133 | import java.io.FileOutputStream; |
| 134 | import java.io.FileReader; |
| 135 | import java.io.FilenameFilter; |
| 136 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | import java.io.PrintWriter; |
Oscar Montemayor | d02546b | 2010-01-14 16:38:40 -0800 | [diff] [blame] | 138 | import java.security.NoSuchAlgorithmException; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 139 | import java.security.PublicKey; |
| 140 | import java.security.cert.CertificateException; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 141 | import java.text.SimpleDateFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | import java.util.ArrayList; |
| 143 | import java.util.Arrays; |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 144 | import java.util.Collection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | import java.util.Collections; |
| 146 | import java.util.Comparator; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 147 | import java.util.Date; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | import java.util.HashMap; |
| 149 | import java.util.HashSet; |
| 150 | import java.util.Iterator; |
| 151 | import java.util.List; |
| 152 | import java.util.Map; |
| 153 | import java.util.Set; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 155 | import libcore.io.ErrnoException; |
Kenny Root | 3f99afc | 2012-04-14 10:13:10 -0700 | [diff] [blame] | 156 | import libcore.io.IoUtils; |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 157 | import libcore.io.Libcore; |
Kenny Root | 786cbca | 2012-08-16 11:10:58 -0700 | [diff] [blame] | 158 | import libcore.io.StructStat; |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 159 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Keep track of all those .apks everywhere. |
| 162 | * |
| 163 | * This is very central to the platform's security; please run the unit |
| 164 | * tests whenever making modifications here: |
| 165 | * |
| 166 | mmm frameworks/base/tests/AndroidTests |
| 167 | adb install -r -f out/target/product/passion/data/app/AndroidTests.apk |
| 168 | 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] | 169 | * |
| 170 | * {@hide} |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 171 | */ |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 172 | public class PackageManagerService extends IPackageManager.Stub { |
| 173 | static final String TAG = "PackageManager"; |
| 174 | static final boolean DEBUG_SETTINGS = false; |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 175 | static final boolean DEBUG_PREFERRED = false; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 176 | static final boolean DEBUG_UPGRADE = false; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 177 | private static final boolean DEBUG_INSTALL = false; |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 178 | private static final boolean DEBUG_REMOVE = false; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 179 | private static final boolean DEBUG_BROADCASTS = false; |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 180 | private static final boolean DEBUG_SHOW_INFO = false; |
| 181 | private static final boolean DEBUG_PACKAGE_INFO = false; |
| 182 | private static final boolean DEBUG_INTENT_MATCHING = false; |
| 183 | private static final boolean DEBUG_PACKAGE_SCANNING = false; |
| 184 | private static final boolean DEBUG_APP_DIR_OBSERVER = false; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 185 | private static final boolean DEBUG_VERIFY = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | private static final int RADIO_UID = Process.PHONE_UID; |
Mike Lockwood | d42685d | 2009-09-03 09:25:22 -0400 | [diff] [blame] | 188 | private static final int LOG_UID = Process.LOG_UID; |
Nick Pelly | cd0e839 | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 189 | private static final int NFC_UID = Process.NFC_UID; |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 190 | private static final int BLUETOOTH_UID = Process.BLUETOOTH_UID; |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 191 | private static final int SHELL_UID = Process.SHELL_UID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | private static final boolean GET_CERTIFICATES = true; |
| 194 | |
| 195 | private static final int REMOVE_EVENTS = |
| 196 | FileObserver.CLOSE_WRITE | FileObserver.DELETE | FileObserver.MOVED_FROM; |
| 197 | private static final int ADD_EVENTS = |
| 198 | FileObserver.CLOSE_WRITE /*| FileObserver.CREATE*/ | FileObserver.MOVED_TO; |
| 199 | |
| 200 | private static final int OBSERVER_EVENTS = REMOVE_EVENTS | ADD_EVENTS; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 201 | // Suffix used during package installation when copying/moving |
| 202 | // package apks to install directory. |
| 203 | private static final String INSTALL_PACKAGE_SUFFIX = "-"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | |
| 205 | static final int SCAN_MONITOR = 1<<0; |
| 206 | static final int SCAN_NO_DEX = 1<<1; |
| 207 | static final int SCAN_FORCE_DEX = 1<<2; |
| 208 | static final int SCAN_UPDATE_SIGNATURE = 1<<3; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 209 | static final int SCAN_NEW_INSTALL = 1<<4; |
| 210 | static final int SCAN_NO_PATHS = 1<<5; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 211 | static final int SCAN_UPDATE_TIME = 1<<6; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 212 | static final int SCAN_DEFER_DEX = 1<<7; |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 213 | static final int SCAN_BOOTING = 1<<8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 215 | static final int REMOVE_CHATTY = 1<<16; |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 216 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Whether verification is enabled by default. |
| 219 | */ |
rich cannings | 36c67c2 | 2012-09-16 16:20:23 -0700 | [diff] [blame] | 220 | private static final boolean DEFAULT_VERIFY_ENABLE = true; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * The default maximum time to wait for the verification agent to return in |
| 224 | * milliseconds. |
| 225 | */ |
rich cannings | aa8513e | 2012-05-21 16:15:23 -0700 | [diff] [blame] | 226 | private static final long DEFAULT_VERIFICATION_TIMEOUT = 10 * 1000; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 227 | |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 228 | /** |
| 229 | * The default response for package verification timeout. |
| 230 | * |
| 231 | * This can be either PackageManager.VERIFICATION_ALLOW or |
| 232 | * PackageManager.VERIFICATION_REJECT. |
| 233 | */ |
| 234 | private static final int DEFAULT_VERIFICATION_RESPONSE = PackageManager.VERIFICATION_ALLOW; |
| 235 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 236 | static final String DEFAULT_CONTAINER_PACKAGE = "com.android.defcontainer"; |
| 237 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 238 | static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName( |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 239 | DEFAULT_CONTAINER_PACKAGE, |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 240 | "com.android.defcontainer.DefaultContainerService"); |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 241 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 242 | private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive"; |
| 243 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 244 | private static final String LIB_DIR_NAME = "lib"; |
| 245 | |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 246 | static final String mTempContainerPrefix = "smdl2tmp"; |
| 247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | final HandlerThread mHandlerThread = new HandlerThread("PackageManager", |
| 249 | Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 250 | final PackageHandler mHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | |
Dianne Hackborn | 851a541 | 2009-05-08 12:06:44 -0700 | [diff] [blame] | 252 | final int mSdkVersion = Build.VERSION.SDK_INT; |
| 253 | final String mSdkCodename = "REL".equals(Build.VERSION.CODENAME) |
| 254 | ? null : Build.VERSION.CODENAME; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | final Context mContext; |
| 257 | final boolean mFactoryTest; |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 258 | final boolean mOnlyCore; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 259 | final boolean mNoDexOpt; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | final DisplayMetrics mMetrics; |
| 261 | final int mDefParseFlags; |
| 262 | final String[] mSeparateProcesses; |
| 263 | |
| 264 | // This is where all application persistent data goes. |
| 265 | final File mAppDataDir; |
| 266 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 267 | // This is where all application persistent data goes for secondary users. |
| 268 | final File mUserAppDataDir; |
| 269 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 270 | /** The location for ASEC container files on internal storage. */ |
| 271 | final String mAsecInternalPath; |
| 272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | // This is the object monitoring the framework dir. |
| 274 | final FileObserver mFrameworkInstallObserver; |
| 275 | |
| 276 | // This is the object monitoring the system app dir. |
| 277 | final FileObserver mSystemInstallObserver; |
| 278 | |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 279 | // This is the object monitoring the system app dir. |
| 280 | final FileObserver mVendorInstallObserver; |
| 281 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | // This is the object monitoring mAppInstallDir. |
| 283 | final FileObserver mAppInstallObserver; |
| 284 | |
| 285 | // This is the object monitoring mDrmAppPrivateInstallDir. |
| 286 | final FileObserver mDrmAppInstallObserver; |
| 287 | |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 288 | // Used for privilege escalation. MUST NOT BE CALLED WITH mPackages |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | // LOCK HELD. Can be called with mInstallLock held. |
| 290 | final Installer mInstaller; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 291 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | final File mFrameworkDir; |
| 293 | final File mSystemAppDir; |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 294 | final File mVendorAppDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | final File mAppInstallDir; |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 296 | final File mDalvikCacheDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Directory to which applications installed internally have native |
| 300 | * libraries copied. |
| 301 | */ |
| 302 | private File mAppLibInstallDir; |
| 303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | // Directory containing the private parts (e.g. code and non-resource assets) of forward-locked |
| 305 | // apps. |
| 306 | final File mDrmAppPrivateInstallDir; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 307 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | // Lock for state used when installing and doing other long running |
| 311 | // operations. Methods that must be called with this lock held have |
| 312 | // the prefix "LI". |
| 313 | final Object mInstallLock = new Object(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 314 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | // These are the directories in the 3rd party applications installed dir |
| 316 | // that we have currently loaded packages from. Keys are the application's |
| 317 | // installed zip file (absolute codePath), and values are Package. |
| 318 | final HashMap<String, PackageParser.Package> mAppDirs = |
| 319 | new HashMap<String, PackageParser.Package>(); |
| 320 | |
| 321 | // Information for the parser to write more useful error messages. |
| 322 | File mScanningPath; |
| 323 | int mLastScanError; |
| 324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | // ---------------------------------------------------------------- |
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 | // Keys are String (package name), values are Package. This also serves |
| 328 | // as the lock for the global state. Methods that must be called with |
| 329 | // this lock held have the prefix "LP". |
| 330 | final HashMap<String, PackageParser.Package> mPackages = |
| 331 | new HashMap<String, PackageParser.Package>(); |
| 332 | |
| 333 | final Settings mSettings; |
| 334 | boolean mRestoredSettings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | |
| 336 | // Group-ids that are given to all packages as read from etc/permissions/*.xml. |
| 337 | int[] mGlobalGids; |
| 338 | |
| 339 | // These are the built-in uid -> permission mappings that were read from the |
| 340 | // etc/permissions.xml file. |
| 341 | final SparseArray<HashSet<String>> mSystemPermissions = |
| 342 | new SparseArray<HashSet<String>>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 343 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 344 | static final class SharedLibraryEntry { |
| 345 | final String path; |
| 346 | final String apk; |
| 347 | |
| 348 | SharedLibraryEntry(String _path, String _apk) { |
| 349 | path = _path; |
| 350 | apk = _apk; |
| 351 | } |
| 352 | } |
| 353 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | // These are the built-in shared libraries that were read from the |
| 355 | // etc/permissions.xml file. |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 356 | final HashMap<String, SharedLibraryEntry> mSharedLibraries |
| 357 | = new HashMap<String, SharedLibraryEntry>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 358 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 359 | // Temporary for building the final shared libraries for an .apk. |
| 360 | String[] mTmpSharedLibraries = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 361 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 362 | // These are the features this devices supports that were read from the |
| 363 | // etc/permissions.xml file. |
| 364 | final HashMap<String, FeatureInfo> mAvailableFeatures = |
| 365 | new HashMap<String, FeatureInfo>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 366 | |
Robert Craig | 0f40dc9 | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 367 | // If mac_permissions.xml was found for seinfo labeling. |
| 368 | boolean mFoundPolicyFile; |
| 369 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | // All available activities, for your resolving pleasure. |
| 371 | final ActivityIntentResolver mActivities = |
| 372 | new ActivityIntentResolver(); |
| 373 | |
| 374 | // All available receivers, for your resolving pleasure. |
| 375 | final ActivityIntentResolver mReceivers = |
| 376 | new ActivityIntentResolver(); |
| 377 | |
| 378 | // All available services, for your resolving pleasure. |
| 379 | final ServiceIntentResolver mServices = new ServiceIntentResolver(); |
| 380 | |
| 381 | // Keys are String (provider class name), values are Provider. |
| 382 | final HashMap<ComponentName, PackageParser.Provider> mProvidersByComponent = |
| 383 | new HashMap<ComponentName, PackageParser.Provider>(); |
| 384 | |
| 385 | // Mapping from provider base names (first directory in content URI codePath) |
| 386 | // to the provider information. |
| 387 | final HashMap<String, PackageParser.Provider> mProviders = |
| 388 | new HashMap<String, PackageParser.Provider>(); |
| 389 | |
| 390 | // Mapping from instrumentation class names to info about them. |
| 391 | final HashMap<ComponentName, PackageParser.Instrumentation> mInstrumentation = |
| 392 | new HashMap<ComponentName, PackageParser.Instrumentation>(); |
| 393 | |
| 394 | // Mapping from permission names to info about them. |
| 395 | final HashMap<String, PackageParser.PermissionGroup> mPermissionGroups = |
| 396 | new HashMap<String, PackageParser.PermissionGroup>(); |
| 397 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 398 | // Packages whose data we have transfered into another package, thus |
| 399 | // should no longer exist. |
| 400 | final HashSet<String> mTransferedPackages = new HashSet<String>(); |
| 401 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 402 | // Broadcast actions that are only available to the system. |
| 403 | final HashSet<String> mProtectedBroadcasts = new HashSet<String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 404 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 405 | /** List of packages waiting for verification. */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 406 | final SparseArray<PackageVerificationState> mPendingVerification |
| 407 | = new SparseArray<PackageVerificationState>(); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 408 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 409 | HashSet<PackageParser.Package> mDeferredDexOpt = null; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 410 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 411 | /** Token for keys in mPendingVerification. */ |
| 412 | private int mPendingVerificationToken = 0; |
| 413 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | boolean mSystemReady; |
| 415 | boolean mSafeMode; |
| 416 | boolean mHasSystemUidErrors; |
| 417 | |
| 418 | ApplicationInfo mAndroidApplication; |
| 419 | final ActivityInfo mResolveActivity = new ActivityInfo(); |
| 420 | final ResolveInfo mResolveInfo = new ResolveInfo(); |
| 421 | ComponentName mResolveComponentName; |
| 422 | PackageParser.Package mPlatformPackage; |
| 423 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 424 | // Set of pending broadcasts for aggregating enable/disable of components. |
Christopher Tate | 1e08f5c | 2013-05-02 13:27:27 -0700 | [diff] [blame] | 425 | static class PendingPackageBroadcasts { |
| 426 | // for each user id, a map of <package name -> components within that package> |
| 427 | final SparseArray<HashMap<String, ArrayList<String>>> mUidMap; |
| 428 | |
| 429 | public PendingPackageBroadcasts() { |
| 430 | mUidMap = new SparseArray<HashMap<String, ArrayList<String>>>(); |
| 431 | } |
| 432 | |
| 433 | public ArrayList<String> get(int userId, String packageName) { |
| 434 | HashMap<String, ArrayList<String>> packages = getOrAllocate(userId); |
| 435 | return packages.get(packageName); |
| 436 | } |
| 437 | |
| 438 | public void put(int userId, String packageName, ArrayList<String> components) { |
| 439 | HashMap<String, ArrayList<String>> packages = getOrAllocate(userId); |
| 440 | packages.put(packageName, components); |
| 441 | } |
| 442 | |
| 443 | public void remove(int userId, String packageName) { |
| 444 | HashMap<String, ArrayList<String>> packages = mUidMap.get(userId); |
| 445 | if (packages != null) { |
| 446 | packages.remove(packageName); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | public void remove(int userId) { |
| 451 | mUidMap.remove(userId); |
| 452 | } |
| 453 | |
| 454 | public int userIdCount() { |
| 455 | return mUidMap.size(); |
| 456 | } |
| 457 | |
| 458 | public int userIdAt(int n) { |
| 459 | return mUidMap.keyAt(n); |
| 460 | } |
| 461 | |
| 462 | public HashMap<String, ArrayList<String>> packagesForUserId(int userId) { |
| 463 | return mUidMap.get(userId); |
| 464 | } |
| 465 | |
| 466 | public int size() { |
| 467 | // total number of pending broadcast entries across all userIds |
| 468 | int num = 0; |
| 469 | for (int i = 0; i< mUidMap.size(); i++) { |
| 470 | num += mUidMap.valueAt(i).size(); |
| 471 | } |
| 472 | return num; |
| 473 | } |
| 474 | |
| 475 | public void clear() { |
| 476 | mUidMap.clear(); |
| 477 | } |
| 478 | |
| 479 | private HashMap<String, ArrayList<String>> getOrAllocate(int userId) { |
| 480 | HashMap<String, ArrayList<String>> map = mUidMap.get(userId); |
| 481 | if (map == null) { |
| 482 | map = new HashMap<String, ArrayList<String>>(); |
| 483 | mUidMap.put(userId, map); |
| 484 | } |
| 485 | return map; |
| 486 | } |
| 487 | } |
| 488 | final PendingPackageBroadcasts mPendingBroadcasts = new PendingPackageBroadcasts(); |
| 489 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 490 | // Service Connection to remote media container service to copy |
| 491 | // package uri's from external media onto secure containers |
| 492 | // or internal storage. |
| 493 | private IMediaContainerService mContainerService = null; |
| 494 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 495 | static final int SEND_PENDING_BROADCAST = 1; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 496 | static final int MCS_BOUND = 3; |
| 497 | static final int END_COPY = 4; |
| 498 | static final int INIT_COPY = 5; |
| 499 | static final int MCS_UNBIND = 6; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 500 | static final int START_CLEANING_PACKAGE = 7; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 501 | static final int FIND_INSTALL_LOC = 8; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 502 | static final int POST_INSTALL = 9; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 503 | static final int MCS_RECONNECT = 10; |
| 504 | static final int MCS_GIVE_UP = 11; |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 505 | static final int UPDATED_MEDIA_STATUS = 12; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 506 | static final int WRITE_SETTINGS = 13; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 507 | static final int WRITE_PACKAGE_RESTRICTIONS = 14; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 508 | static final int PACKAGE_VERIFIED = 15; |
| 509 | static final int CHECK_PENDING_VERIFICATION = 16; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 510 | |
| 511 | static final int WRITE_SETTINGS_DELAY = 10*1000; // 10 seconds |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 512 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 513 | // Delay time in millisecs |
| 514 | static final int BROADCAST_DELAY = 10 * 1000; |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 515 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 516 | static UserManagerService sUserManager; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 517 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 518 | // Stores a list of users whose package restrictions file needs to be updated |
| 519 | private HashSet<Integer> mDirtyUsers = new HashSet<Integer>(); |
| 520 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 521 | final private DefaultContainerConnection mDefContainerConn = |
| 522 | new DefaultContainerConnection(); |
| 523 | class DefaultContainerConnection implements ServiceConnection { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 524 | public void onServiceConnected(ComponentName name, IBinder service) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 525 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceConnected"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 526 | IMediaContainerService imcs = |
| 527 | IMediaContainerService.Stub.asInterface(service); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 528 | mHandler.sendMessage(mHandler.obtainMessage(MCS_BOUND, imcs)); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | public void onServiceDisconnected(ComponentName name) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 532 | if (DEBUG_SD_INSTALL) Log.i(TAG, "onServiceDisconnected"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 533 | } |
| 534 | }; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 535 | |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 536 | // Recordkeeping of restore-after-install operations that are currently in flight |
| 537 | // between the Package Manager and the Backup Manager |
| 538 | class PostInstallData { |
| 539 | public InstallArgs args; |
| 540 | public PackageInstalledInfo res; |
| 541 | |
| 542 | PostInstallData(InstallArgs _a, PackageInstalledInfo _r) { |
| 543 | args = _a; |
| 544 | res = _r; |
| 545 | } |
| 546 | }; |
| 547 | final SparseArray<PostInstallData> mRunningInstalls = new SparseArray<PostInstallData>(); |
| 548 | int mNextInstallToken = 1; // nonzero; will be wrapped back to 1 when ++ overflows |
| 549 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 550 | private final String mRequiredVerifierPackage; |
| 551 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 552 | class PackageHandler extends Handler { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 553 | private boolean mBound = false; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 554 | final ArrayList<HandlerParams> mPendingInstalls = |
| 555 | new ArrayList<HandlerParams>(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 556 | |
| 557 | private boolean connectToService() { |
| 558 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Trying to bind to" + |
| 559 | " DefaultContainerService"); |
| 560 | Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 561 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 562 | if (mContext.bindServiceAsUser(service, mDefContainerConn, |
| 563 | Context.BIND_AUTO_CREATE, UserHandle.OWNER)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 564 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 565 | mBound = true; |
| 566 | return true; |
| 567 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 568 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
| 572 | private void disconnectService() { |
| 573 | mContainerService = null; |
| 574 | mBound = false; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 575 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 576 | mContext.unbindService(mDefContainerConn); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 577 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 578 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 579 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 580 | PackageHandler(Looper looper) { |
| 581 | super(looper); |
| 582 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 583 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 584 | public void handleMessage(Message msg) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 585 | try { |
| 586 | doHandleMessage(msg); |
| 587 | } finally { |
| 588 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | void doHandleMessage(Message msg) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 593 | switch (msg.what) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 594 | case INIT_COPY: { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 595 | HandlerParams params = (HandlerParams) msg.obj; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 596 | int idx = mPendingInstalls.size(); |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 597 | if (DEBUG_INSTALL) Slog.i(TAG, "init_copy idx=" + idx + ": " + params); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 598 | // If a bind was already initiated we dont really |
| 599 | // need to do anything. The pending install |
| 600 | // will be processed later on. |
| 601 | if (!mBound) { |
| 602 | // If this is the only one pending we might |
| 603 | // have to bind to the service again. |
| 604 | if (!connectToService()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 605 | Slog.e(TAG, "Failed to bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 606 | params.serviceError(); |
| 607 | return; |
| 608 | } else { |
| 609 | // Once we bind to the service, the first |
| 610 | // pending request will be processed. |
| 611 | mPendingInstalls.add(idx, params); |
| 612 | } |
| 613 | } else { |
| 614 | mPendingInstalls.add(idx, params); |
| 615 | // Already bound to the service. Just make |
| 616 | // sure we trigger off processing the first request. |
| 617 | if (idx == 0) { |
| 618 | mHandler.sendEmptyMessage(MCS_BOUND); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | break; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 622 | } |
| 623 | case MCS_BOUND: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 624 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_bound"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 625 | if (msg.obj != null) { |
| 626 | mContainerService = (IMediaContainerService) msg.obj; |
| 627 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 628 | if (mContainerService == null) { |
| 629 | // Something seriously wrong. Bail out |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 630 | Slog.e(TAG, "Cannot bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 631 | for (HandlerParams params : mPendingInstalls) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 632 | // Indicate service bind error |
| 633 | params.serviceError(); |
| 634 | } |
| 635 | mPendingInstalls.clear(); |
| 636 | } else if (mPendingInstalls.size() > 0) { |
| 637 | HandlerParams params = mPendingInstalls.get(0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 638 | if (params != null) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 639 | if (params.startCopy()) { |
| 640 | // We are done... look for more work or to |
| 641 | // go idle. |
| 642 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 643 | "Checking for more work or unbind..."); |
| 644 | // Delete pending install |
| 645 | if (mPendingInstalls.size() > 0) { |
| 646 | mPendingInstalls.remove(0); |
| 647 | } |
| 648 | if (mPendingInstalls.size() == 0) { |
| 649 | if (mBound) { |
| 650 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 651 | "Posting delayed MCS_UNBIND"); |
| 652 | removeMessages(MCS_UNBIND); |
| 653 | Message ubmsg = obtainMessage(MCS_UNBIND); |
| 654 | // Unbind after a little delay, to avoid |
| 655 | // continual thrashing. |
| 656 | sendMessageDelayed(ubmsg, 10000); |
| 657 | } |
| 658 | } else { |
| 659 | // There are more pending requests in queue. |
| 660 | // Just post MCS_BOUND message to trigger processing |
| 661 | // of next pending install. |
| 662 | if (DEBUG_SD_INSTALL) Log.i(TAG, |
| 663 | "Posting MCS_BOUND for next woek"); |
| 664 | mHandler.sendEmptyMessage(MCS_BOUND); |
| 665 | } |
| 666 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 667 | } |
| 668 | } else { |
| 669 | // Should never happen ideally. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 670 | Slog.w(TAG, "Empty queue"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 671 | } |
| 672 | break; |
| 673 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 674 | case MCS_RECONNECT: { |
| 675 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_reconnect"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 676 | if (mPendingInstalls.size() > 0) { |
| 677 | if (mBound) { |
| 678 | disconnectService(); |
| 679 | } |
| 680 | if (!connectToService()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 681 | Slog.e(TAG, "Failed to bind to media container service"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 682 | for (HandlerParams params : mPendingInstalls) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 683 | // Indicate service bind error |
| 684 | params.serviceError(); |
| 685 | } |
| 686 | mPendingInstalls.clear(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | break; |
| 690 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 691 | case MCS_UNBIND: { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 692 | // If there is no actual work left, then time to unbind. |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 693 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_unbind"); |
| 694 | |
| 695 | if (mPendingInstalls.size() == 0 && mPendingVerification.size() == 0) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 696 | if (mBound) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 697 | if (DEBUG_INSTALL) Slog.i(TAG, "calling disconnectService()"); |
| 698 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 699 | disconnectService(); |
| 700 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 701 | } else if (mPendingInstalls.size() > 0) { |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 702 | // There are more pending requests in queue. |
| 703 | // Just post MCS_BOUND message to trigger processing |
| 704 | // of next pending install. |
| 705 | mHandler.sendEmptyMessage(MCS_BOUND); |
| 706 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 707 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 708 | break; |
| 709 | } |
| 710 | case MCS_GIVE_UP: { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 711 | if (DEBUG_INSTALL) Slog.i(TAG, "mcs_giveup too many retries"); |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 712 | mPendingInstalls.remove(0); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 713 | break; |
| 714 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 715 | case SEND_PENDING_BROADCAST: { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 716 | String packages[]; |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 717 | ArrayList<String> components[]; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 718 | int size = 0; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 719 | int uids[]; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 720 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 721 | synchronized (mPackages) { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 722 | if (mPendingBroadcasts == null) { |
| 723 | return; |
| 724 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 725 | size = mPendingBroadcasts.size(); |
| 726 | if (size <= 0) { |
| 727 | // Nothing to be done. Just return |
| 728 | return; |
| 729 | } |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 730 | packages = new String[size]; |
| 731 | components = new ArrayList[size]; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 732 | uids = new int[size]; |
Christopher Tate | 1e08f5c | 2013-05-02 13:27:27 -0700 | [diff] [blame] | 733 | int i = 0; // filling out the above arrays |
| 734 | |
| 735 | for (int n = 0; n < mPendingBroadcasts.userIdCount(); n++) { |
| 736 | int packageUserId = mPendingBroadcasts.userIdAt(n); |
| 737 | Iterator<Map.Entry<String, ArrayList<String>>> it |
| 738 | = mPendingBroadcasts.packagesForUserId(packageUserId) |
| 739 | .entrySet().iterator(); |
| 740 | while (it.hasNext() && i < size) { |
| 741 | Map.Entry<String, ArrayList<String>> ent = it.next(); |
| 742 | packages[i] = ent.getKey(); |
| 743 | components[i] = ent.getValue(); |
| 744 | PackageSetting ps = mSettings.mPackages.get(ent.getKey()); |
| 745 | uids[i] = (ps != null) |
| 746 | ? UserHandle.getUid(packageUserId, ps.appId) |
| 747 | : -1; |
| 748 | i++; |
| 749 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 750 | } |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 751 | size = i; |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 752 | mPendingBroadcasts.clear(); |
| 753 | } |
| 754 | // Send broadcasts |
| 755 | for (int i = 0; i < size; i++) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 756 | sendPackageChangedBroadcast(packages[i], true, components[i], uids[i]); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 757 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 758 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 759 | break; |
| 760 | } |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 761 | case START_CLEANING_PACKAGE: { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 762 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 763 | final String packageName = (String)msg.obj; |
| 764 | final int userId = msg.arg1; |
| 765 | final boolean andCode = msg.arg2 != 0; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 766 | synchronized (mPackages) { |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 767 | if (userId == UserHandle.USER_ALL) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 768 | int[] users = sUserManager.getUserIds(); |
| 769 | for (int user : users) { |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 770 | mSettings.addPackageToCleanLPw( |
| 771 | new PackageCleanItem(user, packageName, andCode)); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 772 | } |
| 773 | } else { |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 774 | mSettings.addPackageToCleanLPw( |
| 775 | new PackageCleanItem(userId, packageName, andCode)); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 776 | } |
| 777 | } |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 778 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 779 | startCleaningPackages(); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 780 | } break; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 781 | case POST_INSTALL: { |
| 782 | if (DEBUG_INSTALL) Log.v(TAG, "Handling post-install for " + msg.arg1); |
| 783 | PostInstallData data = mRunningInstalls.get(msg.arg1); |
| 784 | mRunningInstalls.delete(msg.arg1); |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 785 | boolean deleteOld = false; |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 786 | |
| 787 | if (data != null) { |
| 788 | InstallArgs args = data.args; |
| 789 | PackageInstalledInfo res = data.res; |
| 790 | |
| 791 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 792 | res.removedInfo.sendBroadcast(false, true, false); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 793 | Bundle extras = new Bundle(1); |
| 794 | extras.putInt(Intent.EXTRA_UID, res.uid); |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 795 | // Determine the set of users who are adding this |
| 796 | // package for the first time vs. those who are seeing |
| 797 | // an update. |
| 798 | int[] firstUsers; |
| 799 | int[] updateUsers = new int[0]; |
| 800 | if (res.origUsers == null || res.origUsers.length == 0) { |
| 801 | firstUsers = res.newUsers; |
| 802 | } else { |
| 803 | firstUsers = new int[0]; |
| 804 | for (int i=0; i<res.newUsers.length; i++) { |
| 805 | int user = res.newUsers[i]; |
| 806 | boolean isNew = true; |
| 807 | for (int j=0; j<res.origUsers.length; j++) { |
| 808 | if (res.origUsers[j] == user) { |
| 809 | isNew = false; |
| 810 | break; |
| 811 | } |
| 812 | } |
| 813 | if (isNew) { |
| 814 | int[] newFirst = new int[firstUsers.length+1]; |
| 815 | System.arraycopy(firstUsers, 0, newFirst, 0, |
| 816 | firstUsers.length); |
| 817 | newFirst[firstUsers.length] = user; |
| 818 | firstUsers = newFirst; |
| 819 | } else { |
| 820 | int[] newUpdate = new int[updateUsers.length+1]; |
| 821 | System.arraycopy(updateUsers, 0, newUpdate, 0, |
| 822 | updateUsers.length); |
| 823 | newUpdate[updateUsers.length] = user; |
| 824 | updateUsers = newUpdate; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, |
| 829 | res.pkg.applicationInfo.packageName, |
| 830 | extras, null, null, firstUsers); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 831 | final boolean update = res.removedInfo.removedPackage != null; |
| 832 | if (update) { |
| 833 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 834 | } |
| 835 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, |
| 836 | res.pkg.applicationInfo.packageName, |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 837 | extras, null, null, updateUsers); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 838 | if (update) { |
| 839 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, |
| 840 | res.pkg.applicationInfo.packageName, |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 841 | extras, null, null, updateUsers); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 842 | sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, |
| 843 | null, null, |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 844 | res.pkg.applicationInfo.packageName, null, updateUsers); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 845 | } |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 846 | if (res.removedInfo.args != null) { |
| 847 | // Remove the replaced package's older resources safely now |
| 848 | deleteOld = true; |
| 849 | } |
Matt Finifter | f8a98ed | 2012-05-14 15:43:34 -0700 | [diff] [blame] | 850 | |
| 851 | // Log current value of "unknown sources" setting |
| 852 | EventLog.writeEvent(EventLogTags.UNKNOWN_SOURCES_ENABLED, |
| 853 | getUnknownSourcesSettings()); |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 854 | } |
| 855 | // Force a gc to clear up things |
| 856 | Runtime.getRuntime().gc(); |
| 857 | // We delete after a gc for applications on sdcard. |
| 858 | if (deleteOld) { |
| 859 | synchronized (mInstallLock) { |
| 860 | res.removedInfo.args.doPostDeleteLI(true); |
| 861 | } |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 862 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 863 | if (args.observer != null) { |
| 864 | try { |
| 865 | args.observer.packageInstalled(res.name, res.returnCode); |
| 866 | } catch (RemoteException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 867 | Slog.i(TAG, "Observer no longer exists."); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 871 | Slog.e(TAG, "Bogus post-install token " + msg.arg1); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 872 | } |
| 873 | } break; |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 874 | case UPDATED_MEDIA_STATUS: { |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 875 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Got message UPDATED_MEDIA_STATUS"); |
| 876 | boolean reportStatus = msg.arg1 == 1; |
| 877 | boolean doGc = msg.arg2 == 1; |
| 878 | if (DEBUG_SD_INSTALL) Log.i(TAG, "reportStatus=" + reportStatus + ", doGc = " + doGc); |
| 879 | if (doGc) { |
| 880 | // Force a gc to clear up stale containers. |
| 881 | Runtime.getRuntime().gc(); |
| 882 | } |
| 883 | if (msg.obj != null) { |
Dianne Hackborn | ba24e4d | 2011-09-01 11:17:06 -0700 | [diff] [blame] | 884 | @SuppressWarnings("unchecked") |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 885 | Set<AsecInstallArgs> args = (Set<AsecInstallArgs>) msg.obj; |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 886 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Unloading all containers"); |
| 887 | // Unload containers |
| 888 | unloadAllContainers(args); |
| 889 | } |
| 890 | if (reportStatus) { |
| 891 | try { |
| 892 | if (DEBUG_SD_INSTALL) Log.i(TAG, "Invoking MountService call back"); |
| 893 | PackageHelper.getMountService().finishMediaUpdate(); |
| 894 | } catch (RemoteException e) { |
| 895 | Log.e(TAG, "MountService not running?"); |
| 896 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 897 | } |
| 898 | } break; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 899 | case WRITE_SETTINGS: { |
| 900 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 901 | synchronized (mPackages) { |
| 902 | removeMessages(WRITE_SETTINGS); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 903 | removeMessages(WRITE_PACKAGE_RESTRICTIONS); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 904 | mSettings.writeLPr(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 905 | mDirtyUsers.clear(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 906 | } |
| 907 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 908 | } break; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 909 | case WRITE_PACKAGE_RESTRICTIONS: { |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 910 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 911 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 912 | removeMessages(WRITE_PACKAGE_RESTRICTIONS); |
| 913 | for (int userId : mDirtyUsers) { |
| 914 | mSettings.writePackageRestrictionsLPr(userId); |
| 915 | } |
| 916 | mDirtyUsers.clear(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 917 | } |
| 918 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 919 | } break; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 920 | case CHECK_PENDING_VERIFICATION: { |
| 921 | final int verificationId = msg.arg1; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 922 | final PackageVerificationState state = mPendingVerification.get(verificationId); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 923 | |
rich cannings | d9ef3e5 | 2012-08-22 14:28:05 -0700 | [diff] [blame] | 924 | if ((state != null) && !state.timeoutExtended()) { |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 925 | final InstallArgs args = state.getInstallArgs(); |
Kenny Root | a503a0f | 2011-10-03 14:45:28 -0700 | [diff] [blame] | 926 | Slog.i(TAG, "Verification timed out for " + args.packageURI.toString()); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 927 | mPendingVerification.remove(verificationId); |
| 928 | |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 929 | int ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
| 930 | |
| 931 | if (getDefaultVerificationResponse() == PackageManager.VERIFICATION_ALLOW) { |
rich cannings | d9ef3e5 | 2012-08-22 14:28:05 -0700 | [diff] [blame] | 932 | Slog.i(TAG, "Continuing with installation of " |
| 933 | + args.packageURI.toString()); |
| 934 | state.setVerifierResponse(Binder.getCallingUid(), |
| 935 | PackageManager.VERIFICATION_ALLOW_WITHOUT_SUFFICIENT); |
rich cannings | d1b5cfc | 2012-08-29 14:49:51 -0700 | [diff] [blame] | 936 | broadcastPackageVerified(verificationId, args.packageURI, |
rich cannings | d55deb0 | 2012-10-02 14:53:08 -0700 | [diff] [blame] | 937 | PackageManager.VERIFICATION_ALLOW, |
| 938 | state.getInstallArgs().getUser()); |
rich cannings | d9ef3e5 | 2012-08-22 14:28:05 -0700 | [diff] [blame] | 939 | try { |
| 940 | ret = args.copyApk(mContainerService, true); |
| 941 | } catch (RemoteException e) { |
| 942 | Slog.e(TAG, "Could not contact the ContainerService"); |
| 943 | } |
rich cannings | d1b5cfc | 2012-08-29 14:49:51 -0700 | [diff] [blame] | 944 | } else { |
| 945 | broadcastPackageVerified(verificationId, args.packageURI, |
rich cannings | d55deb0 | 2012-10-02 14:53:08 -0700 | [diff] [blame] | 946 | PackageManager.VERIFICATION_REJECT, |
| 947 | state.getInstallArgs().getUser()); |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 950 | processPendingInstall(args, ret); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 951 | mHandler.sendEmptyMessage(MCS_UNBIND); |
| 952 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 953 | break; |
| 954 | } |
| 955 | case PACKAGE_VERIFIED: { |
| 956 | final int verificationId = msg.arg1; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 957 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 958 | final PackageVerificationState state = mPendingVerification.get(verificationId); |
| 959 | if (state == null) { |
Kenny Root | a503a0f | 2011-10-03 14:45:28 -0700 | [diff] [blame] | 960 | Slog.w(TAG, "Invalid verification token " + verificationId + " received"); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 961 | break; |
| 962 | } |
| 963 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 964 | final PackageVerificationResponse response = (PackageVerificationResponse) msg.obj; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 965 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 966 | state.setVerifierResponse(response.callerUid, response.code); |
| 967 | |
| 968 | if (state.isVerificationComplete()) { |
| 969 | mPendingVerification.remove(verificationId); |
| 970 | |
| 971 | final InstallArgs args = state.getInstallArgs(); |
| 972 | |
| 973 | int ret; |
| 974 | if (state.isInstallAllowed()) { |
| 975 | ret = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
rich cannings | d1b5cfc | 2012-08-29 14:49:51 -0700 | [diff] [blame] | 976 | broadcastPackageVerified(verificationId, args.packageURI, |
rich cannings | d55deb0 | 2012-10-02 14:53:08 -0700 | [diff] [blame] | 977 | response.code, state.getInstallArgs().getUser()); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 978 | try { |
| 979 | ret = args.copyApk(mContainerService, true); |
| 980 | } catch (RemoteException e) { |
| 981 | Slog.e(TAG, "Could not contact the ContainerService"); |
| 982 | } |
| 983 | } else { |
| 984 | ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 985 | } |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 986 | |
| 987 | processPendingInstall(args, ret); |
| 988 | |
| 989 | mHandler.sendEmptyMessage(MCS_UNBIND); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 992 | break; |
| 993 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 997 | |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 998 | void scheduleWriteSettingsLocked() { |
| 999 | if (!mHandler.hasMessages(WRITE_SETTINGS)) { |
| 1000 | mHandler.sendEmptyMessageDelayed(WRITE_SETTINGS, WRITE_SETTINGS_DELAY); |
| 1001 | } |
| 1002 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1003 | |
| 1004 | void scheduleWritePackageRestrictionsLocked(int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1005 | if (!sUserManager.exists(userId)) return; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1006 | mDirtyUsers.add(userId); |
| 1007 | if (!mHandler.hasMessages(WRITE_PACKAGE_RESTRICTIONS)) { |
| 1008 | mHandler.sendEmptyMessageDelayed(WRITE_PACKAGE_RESTRICTIONS, WRITE_SETTINGS_DELAY); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
Jeff Brown | f69c812 | 2012-09-12 17:00:34 -0700 | [diff] [blame] | 1012 | public static final IPackageManager main(Context context, Installer installer, |
| 1013 | boolean factoryTest, boolean onlyCore) { |
| 1014 | PackageManagerService m = new PackageManagerService(context, installer, |
| 1015 | factoryTest, onlyCore); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | ServiceManager.addService("package", m); |
| 1017 | return m; |
| 1018 | } |
| 1019 | |
| 1020 | static String[] splitString(String str, char sep) { |
| 1021 | int count = 1; |
| 1022 | int i = 0; |
| 1023 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 1024 | count++; |
| 1025 | i++; |
| 1026 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1027 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | String[] res = new String[count]; |
| 1029 | i=0; |
| 1030 | count = 0; |
| 1031 | int lastI=0; |
| 1032 | while ((i=str.indexOf(sep, i)) >= 0) { |
| 1033 | res[count] = str.substring(lastI, i); |
| 1034 | count++; |
| 1035 | i++; |
| 1036 | lastI = i; |
| 1037 | } |
| 1038 | res[count] = str.substring(lastI, str.length()); |
| 1039 | return res; |
| 1040 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1041 | |
Jeff Brown | f69c812 | 2012-09-12 17:00:34 -0700 | [diff] [blame] | 1042 | public PackageManagerService(Context context, Installer installer, |
| 1043 | boolean factoryTest, boolean onlyCore) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1044 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_START, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | SystemClock.uptimeMillis()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1046 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | if (mSdkVersion <= 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1048 | Slog.w(TAG, "**** ro.build.version.sdk not set!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1050 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | mContext = context; |
| 1052 | mFactoryTest = factoryTest; |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1053 | mOnlyCore = onlyCore; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1054 | mNoDexOpt = "eng".equals(SystemProperties.get("ro.build.type")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | mMetrics = new DisplayMetrics(); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1056 | mSettings = new Settings(context); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1057 | mSettings.addSharedUserLPw("android.uid.system", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | Process.SYSTEM_UID, ApplicationInfo.FLAG_SYSTEM); |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 1059 | mSettings.addSharedUserLPw("android.uid.phone", RADIO_UID, ApplicationInfo.FLAG_SYSTEM); |
| 1060 | mSettings.addSharedUserLPw("android.uid.log", LOG_UID, ApplicationInfo.FLAG_SYSTEM); |
| 1061 | mSettings.addSharedUserLPw("android.uid.nfc", NFC_UID, ApplicationInfo.FLAG_SYSTEM); |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 1062 | mSettings.addSharedUserLPw("android.uid.bluetooth", BLUETOOTH_UID, ApplicationInfo.FLAG_SYSTEM); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1063 | mSettings.addSharedUserLPw("android.uid.shell", SHELL_UID, ApplicationInfo.FLAG_SYSTEM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | |
| 1065 | String separateProcesses = SystemProperties.get("debug.separate_processes"); |
| 1066 | if (separateProcesses != null && separateProcesses.length() > 0) { |
| 1067 | if ("*".equals(separateProcesses)) { |
| 1068 | mDefParseFlags = PackageParser.PARSE_IGNORE_PROCESSES; |
| 1069 | mSeparateProcesses = null; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1070 | Slog.w(TAG, "Running with debug.separate_processes: * (ALL)"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | } else { |
| 1072 | mDefParseFlags = 0; |
| 1073 | mSeparateProcesses = separateProcesses.split(","); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1074 | Slog.w(TAG, "Running with debug.separate_processes: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | + separateProcesses); |
| 1076 | } |
| 1077 | } else { |
| 1078 | mDefParseFlags = 0; |
| 1079 | mSeparateProcesses = null; |
| 1080 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1081 | |
Jeff Brown | f69c812 | 2012-09-12 17:00:34 -0700 | [diff] [blame] | 1082 | mInstaller = installer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | |
| 1084 | WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); |
| 1085 | Display d = wm.getDefaultDisplay(); |
| 1086 | d.getMetrics(mMetrics); |
| 1087 | |
| 1088 | synchronized (mInstallLock) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1089 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1090 | synchronized (mPackages) { |
| 1091 | mHandlerThread.start(); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 1092 | mHandler = new PackageHandler(mHandlerThread.getLooper()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1093 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | File dataDir = Environment.getDataDirectory(); |
| 1095 | mAppDataDir = new File(dataDir, "data"); |
Kenny Root | 584c4cd | 2012-10-29 10:46:42 -0700 | [diff] [blame] | 1096 | mAppInstallDir = new File(dataDir, "app"); |
| 1097 | mAppLibInstallDir = new File(dataDir, "app-lib"); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 1098 | mAsecInternalPath = new File(dataDir, "app-asec").getPath(); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1099 | mUserAppDataDir = new File(dataDir, "user"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | mDrmAppPrivateInstallDir = new File(dataDir, "app-private"); |
| 1101 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1102 | sUserManager = new UserManagerService(context, this, |
| 1103 | mInstallLock, mPackages); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | readPermissions(); |
| 1106 | |
Robert Craig | 0f40dc9 | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 1107 | mFoundPolicyFile = SELinuxMMAC.readInstallPolicy(); |
| 1108 | |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 1109 | mRestoredSettings = mSettings.readLPw(this, sUserManager.getUsers(false), |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 1110 | mSdkVersion, mOnlyCore); |
Robert Craig | 0f40dc9 | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 1111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | long startTime = SystemClock.uptimeMillis(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1113 | |
| 1114 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SYSTEM_SCAN_START, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | startTime); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1116 | |
Suchi Amalapurapu | daec172 | 2010-01-14 21:25:16 -0800 | [diff] [blame] | 1117 | // Set flag to monitor and not change apk file paths when |
| 1118 | // scanning install directories. |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 1119 | int scanMode = SCAN_MONITOR | SCAN_NO_PATHS | SCAN_DEFER_DEX | SCAN_BOOTING; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1120 | if (mNoDexOpt) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1121 | Slog.w(TAG, "Running ENG build: no pre-dexopt!"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1122 | scanMode |= SCAN_NO_DEX; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1123 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | final HashSet<String> libFiles = new HashSet<String>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | mFrameworkDir = new File(Environment.getRootDirectory(), "framework"); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1128 | mDalvikCacheDir = new File(dataDir, "dalvik-cache"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1129 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1130 | boolean didDexOpt = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1131 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1132 | /** |
| 1133 | * Out of paranoia, ensure that everything in the boot class |
| 1134 | * path has been dexed. |
| 1135 | */ |
| 1136 | String bootClassPath = System.getProperty("java.boot.class.path"); |
| 1137 | if (bootClassPath != null) { |
| 1138 | String[] paths = splitString(bootClassPath, ':'); |
| 1139 | for (int i=0; i<paths.length; i++) { |
| 1140 | try { |
| 1141 | if (dalvik.system.DexFile.isDexOptNeeded(paths[i])) { |
| 1142 | libFiles.add(paths[i]); |
| 1143 | mInstaller.dexopt(paths[i], Process.SYSTEM_UID, true); |
| 1144 | didDexOpt = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1146 | } catch (FileNotFoundException e) { |
| 1147 | Slog.w(TAG, "Boot class path not found: " + paths[i]); |
| 1148 | } catch (IOException e) { |
Kenny Root | fec5e75 | 2011-09-23 10:09:58 -0700 | [diff] [blame] | 1149 | Slog.w(TAG, "Cannot dexopt " + paths[i] + "; is it an APK or JAR? " |
| 1150 | + e.getMessage()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | } |
| 1152 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1153 | } else { |
| 1154 | Slog.w(TAG, "No BOOTCLASSPATH found!"); |
| 1155 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1156 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1157 | /** |
| 1158 | * Also ensure all external libraries have had dexopt run on them. |
| 1159 | */ |
| 1160 | if (mSharedLibraries.size() > 0) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 1161 | Iterator<SharedLibraryEntry> libs = mSharedLibraries.values().iterator(); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1162 | while (libs.hasNext()) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 1163 | String lib = libs.next().path; |
| 1164 | if (lib == null) { |
| 1165 | continue; |
| 1166 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1167 | try { |
| 1168 | if (dalvik.system.DexFile.isDexOptNeeded(lib)) { |
| 1169 | libFiles.add(lib); |
| 1170 | mInstaller.dexopt(lib, Process.SYSTEM_UID, true); |
| 1171 | didDexOpt = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1173 | } catch (FileNotFoundException e) { |
| 1174 | Slog.w(TAG, "Library not found: " + lib); |
| 1175 | } catch (IOException e) { |
Kenny Root | fec5e75 | 2011-09-23 10:09:58 -0700 | [diff] [blame] | 1176 | Slog.w(TAG, "Cannot dexopt " + lib + "; is it an APK or JAR? " |
| 1177 | + e.getMessage()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | } |
| 1179 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1180 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1181 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1182 | // Gross hack for now: we know this file doesn't contain any |
| 1183 | // code, so don't dexopt it to avoid the resulting log spew. |
| 1184 | libFiles.add(mFrameworkDir.getPath() + "/framework-res.apk"); |
| 1185 | |
| 1186 | /** |
| 1187 | * And there are a number of commands implemented in Java, which |
| 1188 | * we currently need to do the dexopt on so that they can be |
| 1189 | * run from a non-root shell. |
| 1190 | */ |
| 1191 | String[] frameworkFiles = mFrameworkDir.list(); |
| 1192 | if (frameworkFiles != null) { |
| 1193 | for (int i=0; i<frameworkFiles.length; i++) { |
| 1194 | File libPath = new File(mFrameworkDir, frameworkFiles[i]); |
| 1195 | String path = libPath.getPath(); |
| 1196 | // Skip the file if we alrady did it. |
| 1197 | if (libFiles.contains(path)) { |
| 1198 | continue; |
| 1199 | } |
| 1200 | // Skip the file if it is not a type we want to dexopt. |
| 1201 | if (!path.endsWith(".apk") && !path.endsWith(".jar")) { |
| 1202 | continue; |
| 1203 | } |
| 1204 | try { |
| 1205 | if (dalvik.system.DexFile.isDexOptNeeded(path)) { |
| 1206 | mInstaller.dexopt(path, Process.SYSTEM_UID, true); |
| 1207 | didDexOpt = true; |
| 1208 | } |
| 1209 | } catch (FileNotFoundException e) { |
| 1210 | Slog.w(TAG, "Jar not found: " + path); |
| 1211 | } catch (IOException e) { |
| 1212 | Slog.w(TAG, "Exception reading jar: " + path, e); |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | if (didDexOpt) { |
| 1218 | // If we had to do a dexopt of one of the previous |
| 1219 | // things, then something on the system has changed. |
| 1220 | // Consider this significant, and wipe away all other |
| 1221 | // existing dexopt files to ensure we don't leave any |
| 1222 | // dangling around. |
| 1223 | String[] files = mDalvikCacheDir.list(); |
| 1224 | if (files != null) { |
| 1225 | for (int i=0; i<files.length; i++) { |
| 1226 | String fn = files[i]; |
| 1227 | if (fn.startsWith("data@app@") |
| 1228 | || fn.startsWith("data@app-private@")) { |
| 1229 | Slog.i(TAG, "Pruning dalvik file: " + fn); |
| 1230 | (new File(mDalvikCacheDir, fn)).delete(); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1234 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1235 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1236 | // Find base frameworks (resource packages without code). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | mFrameworkInstallObserver = new AppDirObserver( |
| 1238 | mFrameworkDir.getPath(), OBSERVER_EVENTS, true); |
| 1239 | mFrameworkInstallObserver.startWatching(); |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 1240 | scanDirLI(mFrameworkDir, PackageParser.PARSE_IS_SYSTEM |
| 1241 | | PackageParser.PARSE_IS_SYSTEM_DIR, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1242 | scanMode | SCAN_NO_DEX, 0); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 1243 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1244 | // Collect all system packages. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | mSystemAppDir = new File(Environment.getRootDirectory(), "app"); |
| 1246 | mSystemInstallObserver = new AppDirObserver( |
| 1247 | mSystemAppDir.getPath(), OBSERVER_EVENTS, true); |
| 1248 | mSystemInstallObserver.startWatching(); |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 1249 | scanDirLI(mSystemAppDir, PackageParser.PARSE_IS_SYSTEM |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1250 | | PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1251 | |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 1252 | // Collect all vendor packages. |
| 1253 | mVendorAppDir = new File("/vendor/app"); |
| 1254 | mVendorInstallObserver = new AppDirObserver( |
| 1255 | mVendorAppDir.getPath(), OBSERVER_EVENTS, true); |
| 1256 | mVendorInstallObserver.startWatching(); |
| 1257 | scanDirLI(mVendorAppDir, PackageParser.PARSE_IS_SYSTEM |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 1258 | | PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0); |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 1259 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1260 | if (DEBUG_UPGRADE) Log.v(TAG, "Running installd update commands"); |
| 1261 | mInstaller.moveFiles(); |
| 1262 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1263 | // Prune any system packages that no longer exist. |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1264 | final List<String> possiblyDeletedUpdatedSystemApps = new ArrayList<String>(); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1265 | if (!mOnlyCore) { |
| 1266 | Iterator<PackageSetting> psit = mSettings.mPackages.values().iterator(); |
| 1267 | while (psit.hasNext()) { |
| 1268 | PackageSetting ps = psit.next(); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1269 | |
| 1270 | /* |
| 1271 | * If this is not a system app, it can't be a |
| 1272 | * disable system app. |
| 1273 | */ |
| 1274 | if ((ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 1275 | continue; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * If the package is scanned, it's not erased. |
| 1280 | */ |
Kenny Root | eca64b3 | 2012-06-25 16:37:32 -0700 | [diff] [blame] | 1281 | final PackageParser.Package scannedPkg = mPackages.get(ps.name); |
| 1282 | if (scannedPkg != null) { |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1283 | /* |
| 1284 | * If the system app is both scanned and in the |
| 1285 | * disabled packages list, then it must have been |
| 1286 | * added via OTA. Remove it from the currently |
| 1287 | * scanned package so the previously user-installed |
| 1288 | * application can be scanned. |
| 1289 | */ |
| 1290 | if (mSettings.isDisabledSystemPackageLPr(ps.name)) { |
Kenny Root | eca64b3 | 2012-06-25 16:37:32 -0700 | [diff] [blame] | 1291 | Slog.i(TAG, "Expecting better updatd system app for " + ps.name |
| 1292 | + "; removing system app"); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1293 | removePackageLI(ps, true); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1296 | continue; |
| 1297 | } |
| 1298 | |
| 1299 | if (!mSettings.isDisabledSystemPackageLPr(ps.name)) { |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1300 | psit.remove(); |
| 1301 | String msg = "System package " + ps.name |
| 1302 | + " no longer exists; wiping its data"; |
| 1303 | reportSettingsProblem(Log.WARN, msg); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1304 | removeDataDirsLI(ps.name); |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1305 | } else { |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1306 | final PackageSetting disabledPs = mSettings.getDisabledSystemPkgLPr(ps.name); |
| 1307 | if (disabledPs.codePath == null || !disabledPs.codePath.exists()) { |
| 1308 | possiblyDeletedUpdatedSystemApps.add(ps.name); |
| 1309 | } |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1310 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 1311 | } |
| 1312 | } |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1313 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | //look for any incomplete package installations |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1315 | ArrayList<PackageSetting> deletePkgsList = mSettings.getListOfIncompleteInstallPackagesLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1316 | //clean up list |
| 1317 | for(int i = 0; i < deletePkgsList.size(); i++) { |
| 1318 | //clean up here |
| 1319 | cleanupInstallFailedPackage(deletePkgsList.get(i)); |
| 1320 | } |
| 1321 | //delete tmp files |
| 1322 | deleteTempPackageFiles(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1323 | |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1324 | if (!mOnlyCore) { |
| 1325 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_DATA_SCAN_START, |
| 1326 | SystemClock.uptimeMillis()); |
| 1327 | mAppInstallObserver = new AppDirObserver( |
| 1328 | mAppInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 1329 | mAppInstallObserver.startWatching(); |
| 1330 | scanDirLI(mAppInstallDir, 0, scanMode, 0); |
| 1331 | |
| 1332 | mDrmAppInstallObserver = new AppDirObserver( |
| 1333 | mDrmAppPrivateInstallDir.getPath(), OBSERVER_EVENTS, false); |
| 1334 | mDrmAppInstallObserver.startWatching(); |
| 1335 | scanDirLI(mDrmAppPrivateInstallDir, PackageParser.PARSE_FORWARD_LOCK, |
| 1336 | scanMode, 0); |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1337 | |
| 1338 | /** |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1339 | * Remove disable package settings for any updated system |
| 1340 | * apps that were removed via an OTA. If they're not a |
| 1341 | * previously-updated app, remove them completely. |
| 1342 | * Otherwise, just revoke their system-level permissions. |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1343 | */ |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1344 | for (String deletedAppName : possiblyDeletedUpdatedSystemApps) { |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1345 | PackageParser.Package deletedPkg = mPackages.get(deletedAppName); |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1346 | mSettings.removeDisabledSystemPackageLPw(deletedAppName); |
| 1347 | |
| 1348 | String msg; |
| 1349 | if (deletedPkg == null) { |
| 1350 | msg = "Updated system package " + deletedAppName |
| 1351 | + " no longer exists; wiping its data"; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1352 | removeDataDirsLI(deletedAppName); |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1353 | } else { |
| 1354 | msg = "Updated system app + " + deletedAppName |
| 1355 | + " no longer present; removing system privileges for " |
| 1356 | + deletedAppName; |
| 1357 | |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1358 | deletedPkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_SYSTEM; |
| 1359 | |
| 1360 | PackageSetting deletedPs = mSettings.mPackages.get(deletedAppName); |
| 1361 | deletedPs.pkgFlags &= ~ApplicationInfo.FLAG_SYSTEM; |
| 1362 | } |
Kenny Root | 87b5d7b | 2012-05-16 13:36:18 -0700 | [diff] [blame] | 1363 | reportSettingsProblem(Log.WARN, msg); |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 1364 | } |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 1365 | } else { |
| 1366 | mAppInstallObserver = null; |
| 1367 | mDrmAppInstallObserver = null; |
| 1368 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 1370 | // Now that we know all of the shared libraries, update all clients to have |
| 1371 | // the correct library paths. |
| 1372 | updateAllSharedLibrariesLPw(); |
| 1373 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1374 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SCAN_END, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | SystemClock.uptimeMillis()); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1376 | Slog.i(TAG, "Time to scan packages: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | + ((SystemClock.uptimeMillis()-startTime)/1000f) |
| 1378 | + " seconds"); |
| 1379 | |
Dianne Hackborn | f22221f | 2010-04-05 18:35:42 -0700 | [diff] [blame] | 1380 | // If the platform SDK has changed since the last time we booted, |
| 1381 | // we need to re-grant app permission to catch any new ones that |
| 1382 | // appear. This is really a hack, and means that apps can in some |
| 1383 | // cases get permissions that the user didn't initially explicitly |
| 1384 | // allow... it would be nice to have some better way to handle |
| 1385 | // this situation. |
| 1386 | final boolean regrantPermissions = mSettings.mInternalSdkPlatform |
| 1387 | != mSdkVersion; |
| 1388 | if (regrantPermissions) Slog.i(TAG, "Platform changed from " |
| 1389 | + mSettings.mInternalSdkPlatform + " to " + mSdkVersion |
| 1390 | + "; regranting permissions for internal storage"); |
| 1391 | mSettings.mInternalSdkPlatform = mSdkVersion; |
| 1392 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 1393 | updatePermissionsLPw(null, null, UPDATE_PERMISSIONS_ALL |
| 1394 | | (regrantPermissions |
| 1395 | ? (UPDATE_PERMISSIONS_REPLACE_PKG|UPDATE_PERMISSIONS_REPLACE_ALL) |
| 1396 | : 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | |
Dianne Hackborn | 4d491a6 | 2013-04-01 18:10:51 -0700 | [diff] [blame] | 1398 | // If this is the first boot, and it is a normal boot, then |
| 1399 | // we need to initialize the default preferred apps. |
| 1400 | if (!mRestoredSettings && !onlyCore) { |
| 1401 | mSettings.readDefaultPreferredAppsLPw(this, 0); |
| 1402 | } |
| 1403 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1404 | // can downgrade to reader |
| 1405 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1407 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1408 | SystemClock.uptimeMillis()); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1409 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1410 | // Now after opening every single application zip, make sure they |
| 1411 | // are all flushed. Not really needed, but keeps things nice and |
| 1412 | // tidy. |
| 1413 | Runtime.getRuntime().gc(); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1414 | |
| 1415 | mRequiredVerifierPackage = getRequiredVerifierLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | } // synchronized (mPackages) |
| 1417 | } // synchronized (mInstallLock) |
| 1418 | } |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 1419 | |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 1420 | public boolean isFirstBoot() { |
| 1421 | return !mRestoredSettings; |
| 1422 | } |
| 1423 | |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 1424 | public boolean isOnlyCoreApps() { |
| 1425 | return mOnlyCore; |
| 1426 | } |
| 1427 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1428 | private String getRequiredVerifierLPr() { |
| 1429 | final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION); |
| 1430 | final List<ResolveInfo> receivers = queryIntentReceivers(verification, PACKAGE_MIME_TYPE, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1431 | PackageManager.GET_DISABLED_COMPONENTS, 0 /* TODO: Which userId? */); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1432 | |
| 1433 | String requiredVerifier = null; |
| 1434 | |
| 1435 | final int N = receivers.size(); |
| 1436 | for (int i = 0; i < N; i++) { |
| 1437 | final ResolveInfo info = receivers.get(i); |
| 1438 | |
| 1439 | if (info.activityInfo == null) { |
| 1440 | continue; |
| 1441 | } |
| 1442 | |
| 1443 | final String packageName = info.activityInfo.packageName; |
| 1444 | |
| 1445 | final PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1446 | if (ps == null) { |
| 1447 | continue; |
| 1448 | } |
| 1449 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1450 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 1451 | if (!gp.grantedPermissions |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 1452 | .contains(android.Manifest.permission.PACKAGE_VERIFICATION_AGENT)) { |
| 1453 | continue; |
| 1454 | } |
| 1455 | |
| 1456 | if (requiredVerifier != null) { |
| 1457 | throw new RuntimeException("There can be only one required verifier"); |
| 1458 | } |
| 1459 | |
| 1460 | requiredVerifier = packageName; |
| 1461 | } |
| 1462 | |
| 1463 | return requiredVerifier; |
| 1464 | } |
| 1465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1466 | @Override |
| 1467 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1468 | throws RemoteException { |
| 1469 | try { |
| 1470 | return super.onTransact(code, data, reply, flags); |
| 1471 | } catch (RuntimeException e) { |
| 1472 | if (!(e instanceof SecurityException) && !(e instanceof IllegalArgumentException)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1473 | Slog.e(TAG, "Package Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1474 | } |
| 1475 | throw e; |
| 1476 | } |
| 1477 | } |
| 1478 | |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1479 | void cleanupInstallFailedPackage(PackageSetting ps) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1480 | Slog.i(TAG, "Cleaning up incompletely installed app: " + ps.name); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1481 | removeDataDirsLI(ps.name); |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1482 | if (ps.codePath != null) { |
| 1483 | if (!ps.codePath.delete()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1484 | Slog.w(TAG, "Unable to remove old code file: " + ps.codePath); |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1485 | } |
| 1486 | } |
| 1487 | if (ps.resourcePath != null) { |
| 1488 | if (!ps.resourcePath.delete() && !ps.resourcePath.equals(ps.codePath)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1489 | Slog.w(TAG, "Unable to remove old code file: " + ps.resourcePath); |
Dianne Hackborn | e6620b2 | 2010-01-22 14:46:21 -0800 | [diff] [blame] | 1490 | } |
| 1491 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1492 | mSettings.removePackageLPw(ps.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | void readPermissions() { |
| 1496 | // Read permissions from .../etc/permission directory. |
| 1497 | File libraryDir = new File(Environment.getRootDirectory(), "etc/permissions"); |
| 1498 | if (!libraryDir.exists() || !libraryDir.isDirectory()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1499 | Slog.w(TAG, "No directory " + libraryDir + ", skipping"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1500 | return; |
| 1501 | } |
| 1502 | if (!libraryDir.canRead()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1503 | Slog.w(TAG, "Directory " + libraryDir + " cannot be read"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | return; |
| 1505 | } |
| 1506 | |
| 1507 | // Iterate over the files in the directory and scan .xml files |
| 1508 | for (File f : libraryDir.listFiles()) { |
| 1509 | // We'll read platform.xml last |
| 1510 | if (f.getPath().endsWith("etc/permissions/platform.xml")) { |
| 1511 | continue; |
| 1512 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1513 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1514 | if (!f.getPath().endsWith(".xml")) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1515 | 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] | 1516 | continue; |
| 1517 | } |
| 1518 | if (!f.canRead()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1519 | 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] | 1520 | continue; |
| 1521 | } |
| 1522 | |
| 1523 | readPermissionsFromXml(f); |
| 1524 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | // Read permissions from .../etc/permissions/platform.xml last so it will take precedence |
| 1527 | final File permFile = new File(Environment.getRootDirectory(), |
| 1528 | "etc/permissions/platform.xml"); |
| 1529 | readPermissionsFromXml(permFile); |
| 1530 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1531 | |
| 1532 | private void readPermissionsFromXml(File permFile) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1533 | FileReader permReader = null; |
| 1534 | try { |
| 1535 | permReader = new FileReader(permFile); |
| 1536 | } catch (FileNotFoundException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1537 | 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] | 1538 | return; |
| 1539 | } |
| 1540 | |
| 1541 | try { |
| 1542 | XmlPullParser parser = Xml.newPullParser(); |
| 1543 | parser.setInput(permReader); |
| 1544 | |
| 1545 | XmlUtils.beginDocument(parser, "permissions"); |
| 1546 | |
| 1547 | while (true) { |
| 1548 | XmlUtils.nextElement(parser); |
| 1549 | if (parser.getEventType() == XmlPullParser.END_DOCUMENT) { |
| 1550 | break; |
| 1551 | } |
| 1552 | |
| 1553 | String name = parser.getName(); |
| 1554 | if ("group".equals(name)) { |
| 1555 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1556 | if (gidStr != null) { |
Romain Guy | 01d159d | 2013-02-25 13:22:42 -0800 | [diff] [blame] | 1557 | int gid = Process.getGidForName(gidStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1558 | mGlobalGids = appendInt(mGlobalGids, gid); |
| 1559 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1560 | Slog.w(TAG, "<group> without gid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1561 | + parser.getPositionDescription()); |
| 1562 | } |
| 1563 | |
| 1564 | XmlUtils.skipCurrentTag(parser); |
| 1565 | continue; |
| 1566 | } else if ("permission".equals(name)) { |
| 1567 | String perm = parser.getAttributeValue(null, "name"); |
| 1568 | if (perm == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1569 | Slog.w(TAG, "<permission> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1570 | + parser.getPositionDescription()); |
| 1571 | XmlUtils.skipCurrentTag(parser); |
| 1572 | continue; |
| 1573 | } |
| 1574 | perm = perm.intern(); |
| 1575 | readPermission(parser, perm); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1576 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1577 | } else if ("assign-permission".equals(name)) { |
| 1578 | String perm = parser.getAttributeValue(null, "name"); |
| 1579 | if (perm == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1580 | Slog.w(TAG, "<assign-permission> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | + parser.getPositionDescription()); |
| 1582 | XmlUtils.skipCurrentTag(parser); |
| 1583 | continue; |
| 1584 | } |
| 1585 | String uidStr = parser.getAttributeValue(null, "uid"); |
| 1586 | if (uidStr == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1587 | Slog.w(TAG, "<assign-permission> without uid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1588 | + parser.getPositionDescription()); |
| 1589 | XmlUtils.skipCurrentTag(parser); |
| 1590 | continue; |
| 1591 | } |
| 1592 | int uid = Process.getUidForName(uidStr); |
| 1593 | if (uid < 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1594 | Slog.w(TAG, "<assign-permission> with unknown uid \"" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | + uidStr + "\" at " |
| 1596 | + parser.getPositionDescription()); |
| 1597 | XmlUtils.skipCurrentTag(parser); |
| 1598 | continue; |
| 1599 | } |
| 1600 | perm = perm.intern(); |
| 1601 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 1602 | if (perms == null) { |
| 1603 | perms = new HashSet<String>(); |
| 1604 | mSystemPermissions.put(uid, perms); |
| 1605 | } |
| 1606 | perms.add(perm); |
| 1607 | XmlUtils.skipCurrentTag(parser); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1608 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | } else if ("library".equals(name)) { |
| 1610 | String lname = parser.getAttributeValue(null, "name"); |
| 1611 | String lfile = parser.getAttributeValue(null, "file"); |
| 1612 | if (lname == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1613 | Slog.w(TAG, "<library> without name at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | + parser.getPositionDescription()); |
| 1615 | } else if (lfile == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1616 | Slog.w(TAG, "<library> without file at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1617 | + parser.getPositionDescription()); |
| 1618 | } else { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1619 | //Log.i(TAG, "Got library " + lname + " in " + lfile); |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 1620 | mSharedLibraries.put(lname, new SharedLibraryEntry(lfile, null)); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1621 | } |
| 1622 | XmlUtils.skipCurrentTag(parser); |
| 1623 | continue; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1624 | |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1625 | } else if ("feature".equals(name)) { |
| 1626 | String fname = parser.getAttributeValue(null, "name"); |
| 1627 | if (fname == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1628 | Slog.w(TAG, "<feature> without name at " |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1629 | + parser.getPositionDescription()); |
| 1630 | } else { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 1631 | //Log.i(TAG, "Got feature " + fname); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 1632 | FeatureInfo fi = new FeatureInfo(); |
| 1633 | fi.name = fname; |
| 1634 | mAvailableFeatures.put(fname, fi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1635 | } |
| 1636 | XmlUtils.skipCurrentTag(parser); |
| 1637 | continue; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1638 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | } else { |
| 1640 | XmlUtils.skipCurrentTag(parser); |
| 1641 | continue; |
| 1642 | } |
| 1643 | |
| 1644 | } |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 1645 | permReader.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | } catch (XmlPullParserException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1647 | Slog.w(TAG, "Got execption parsing permissions.", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1648 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1649 | Slog.w(TAG, "Got execption parsing permissions.", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | void readPermission(XmlPullParser parser, String name) |
| 1654 | throws IOException, XmlPullParserException { |
| 1655 | |
| 1656 | name = name.intern(); |
| 1657 | |
| 1658 | BasePermission bp = mSettings.mPermissions.get(name); |
| 1659 | if (bp == null) { |
| 1660 | bp = new BasePermission(name, null, BasePermission.TYPE_BUILTIN); |
| 1661 | mSettings.mPermissions.put(name, bp); |
| 1662 | } |
| 1663 | int outerDepth = parser.getDepth(); |
| 1664 | int type; |
| 1665 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 1666 | && (type != XmlPullParser.END_TAG |
| 1667 | || parser.getDepth() > outerDepth)) { |
| 1668 | if (type == XmlPullParser.END_TAG |
| 1669 | || type == XmlPullParser.TEXT) { |
| 1670 | continue; |
| 1671 | } |
| 1672 | |
| 1673 | String tagName = parser.getName(); |
| 1674 | if ("group".equals(tagName)) { |
| 1675 | String gidStr = parser.getAttributeValue(null, "gid"); |
| 1676 | if (gidStr != null) { |
| 1677 | int gid = Process.getGidForName(gidStr); |
| 1678 | bp.gids = appendInt(bp.gids, gid); |
| 1679 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1680 | Slog.w(TAG, "<group> without gid at " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1681 | + parser.getPositionDescription()); |
| 1682 | } |
| 1683 | } |
| 1684 | XmlUtils.skipCurrentTag(parser); |
| 1685 | } |
| 1686 | } |
| 1687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | static int[] appendInts(int[] cur, int[] add) { |
| 1689 | if (add == null) return cur; |
| 1690 | if (cur == null) return add; |
| 1691 | final int N = add.length; |
| 1692 | for (int i=0; i<N; i++) { |
| 1693 | cur = appendInt(cur, add[i]); |
| 1694 | } |
| 1695 | return cur; |
| 1696 | } |
| 1697 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1698 | static int[] removeInts(int[] cur, int[] rem) { |
| 1699 | if (rem == null) return cur; |
| 1700 | if (cur == null) return cur; |
| 1701 | final int N = rem.length; |
| 1702 | for (int i=0; i<N; i++) { |
| 1703 | cur = removeInt(cur, rem[i]); |
| 1704 | } |
| 1705 | return cur; |
| 1706 | } |
| 1707 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1708 | PackageInfo generatePackageInfo(PackageParser.Package p, int flags, int userId) { |
| 1709 | if (!sUserManager.exists(userId)) return null; |
| 1710 | PackageInfo pi; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1711 | final PackageSetting ps = (PackageSetting) p.mExtras; |
| 1712 | if (ps == null) { |
| 1713 | return null; |
| 1714 | } |
| 1715 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
| 1716 | final PackageUserState state = ps.readUserState(userId); |
Jeff Sharkey | 94d7ddb | 2013-03-06 15:02:12 -0800 | [diff] [blame] | 1717 | return PackageParser.generatePackageInfo(p, gp.gids, flags, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1718 | ps.firstInstallTime, ps.lastUpdateTime, gp.grantedPermissions, |
| 1719 | state, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | } |
| 1721 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1722 | @Override |
| 1723 | public PackageInfo getPackageInfo(String packageName, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1724 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 1725 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, "get package info"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1726 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1727 | synchronized (mPackages) { |
| 1728 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1729 | if (DEBUG_PACKAGE_INFO) |
| 1730 | Log.v(TAG, "getPackageInfo " + packageName + ": " + p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1731 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1732 | return generatePackageInfo(p, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1733 | } |
| 1734 | if((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1735 | return generatePackageInfoFromSettingsLPw(packageName, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | return null; |
| 1739 | } |
| 1740 | |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1741 | public String[] currentToCanonicalPackageNames(String[] names) { |
| 1742 | String[] out = new String[names.length]; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1743 | // reader |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1744 | synchronized (mPackages) { |
| 1745 | for (int i=names.length-1; i>=0; i--) { |
| 1746 | PackageSetting ps = mSettings.mPackages.get(names[i]); |
| 1747 | out[i] = ps != null && ps.realName != null ? ps.realName : names[i]; |
| 1748 | } |
| 1749 | } |
| 1750 | return out; |
| 1751 | } |
| 1752 | |
| 1753 | public String[] canonicalToCurrentPackageNames(String[] names) { |
| 1754 | String[] out = new String[names.length]; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1755 | // reader |
Dianne Hackborn | 4709693 | 2010-02-11 15:57:09 -0800 | [diff] [blame] | 1756 | synchronized (mPackages) { |
| 1757 | for (int i=names.length-1; i>=0; i--) { |
| 1758 | String cur = mSettings.mRenamedPackages.get(names[i]); |
| 1759 | out[i] = cur != null ? cur : names[i]; |
| 1760 | } |
| 1761 | } |
| 1762 | return out; |
| 1763 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1764 | |
| 1765 | @Override |
| 1766 | public int getPackageUid(String packageName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1767 | if (!sUserManager.exists(userId)) return -1; |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 1768 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, "get package uid"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1769 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1770 | synchronized (mPackages) { |
| 1771 | PackageParser.Package p = mPackages.get(packageName); |
| 1772 | if(p != null) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1773 | return UserHandle.getUid(userId, p.applicationInfo.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1774 | } |
| 1775 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1776 | if((ps == null) || (ps.pkg == null) || (ps.pkg.applicationInfo == null)) { |
| 1777 | return -1; |
| 1778 | } |
| 1779 | p = ps.pkg; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1780 | return p != null ? UserHandle.getUid(userId, p.applicationInfo.uid) : -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | public int[] getPackageGids(String packageName) { |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 1785 | final boolean enforcedDefault = isPermissionEnforcedDefault(READ_EXTERNAL_STORAGE); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1786 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1787 | synchronized (mPackages) { |
| 1788 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1789 | if (DEBUG_PACKAGE_INFO) |
| 1790 | Log.v(TAG, "getPackageGids" + packageName + ": " + p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | if (p != null) { |
| 1792 | final PackageSetting ps = (PackageSetting)p.mExtras; |
| 1793 | final SharedUserSetting suid = ps.sharedUser; |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 1794 | int[] gids = suid != null ? suid.gids : ps.gids; |
| 1795 | |
| 1796 | // include GIDs for any unenforced permissions |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 1797 | if (!isPermissionEnforcedLocked(READ_EXTERNAL_STORAGE, enforcedDefault)) { |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 1798 | final BasePermission basePerm = mSettings.mPermissions.get( |
| 1799 | READ_EXTERNAL_STORAGE); |
| 1800 | gids = appendInts(gids, basePerm.gids); |
| 1801 | } |
| 1802 | |
| 1803 | return gids; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | // stupid thing to indicate an error. |
| 1807 | return new int[0]; |
| 1808 | } |
| 1809 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1810 | static final PermissionInfo generatePermissionInfo( |
| 1811 | BasePermission bp, int flags) { |
| 1812 | if (bp.perm != null) { |
| 1813 | return PackageParser.generatePermissionInfo(bp.perm, flags); |
| 1814 | } |
| 1815 | PermissionInfo pi = new PermissionInfo(); |
| 1816 | pi.name = bp.name; |
| 1817 | pi.packageName = bp.sourcePackage; |
| 1818 | pi.nonLocalizedLabel = bp.name; |
| 1819 | pi.protectionLevel = bp.protectionLevel; |
| 1820 | return pi; |
| 1821 | } |
| 1822 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | public PermissionInfo getPermissionInfo(String name, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1824 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1825 | synchronized (mPackages) { |
| 1826 | final BasePermission p = mSettings.mPermissions.get(name); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1827 | if (p != null) { |
| 1828 | return generatePermissionInfo(p, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1829 | } |
| 1830 | return null; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | public List<PermissionInfo> queryPermissionsByGroup(String group, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1835 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1836 | synchronized (mPackages) { |
| 1837 | ArrayList<PermissionInfo> out = new ArrayList<PermissionInfo>(10); |
| 1838 | for (BasePermission p : mSettings.mPermissions.values()) { |
| 1839 | if (group == null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1840 | if (p.perm == null || p.perm.info.group == null) { |
| 1841 | out.add(generatePermissionInfo(p, flags)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1842 | } |
| 1843 | } else { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 1844 | 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] | 1845 | out.add(PackageParser.generatePermissionInfo(p.perm, flags)); |
| 1846 | } |
| 1847 | } |
| 1848 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1849 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1850 | if (out.size() > 0) { |
| 1851 | return out; |
| 1852 | } |
| 1853 | return mPermissionGroups.containsKey(group) ? out : null; |
| 1854 | } |
| 1855 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1856 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | public PermissionGroupInfo getPermissionGroupInfo(String name, int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1858 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1859 | synchronized (mPackages) { |
| 1860 | return PackageParser.generatePermissionGroupInfo( |
| 1861 | mPermissionGroups.get(name), flags); |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | public List<PermissionGroupInfo> getAllPermissionGroups(int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1866 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1867 | synchronized (mPackages) { |
| 1868 | final int N = mPermissionGroups.size(); |
| 1869 | ArrayList<PermissionGroupInfo> out |
| 1870 | = new ArrayList<PermissionGroupInfo>(N); |
| 1871 | for (PackageParser.PermissionGroup pg : mPermissionGroups.values()) { |
| 1872 | out.add(PackageParser.generatePermissionGroupInfo(pg, flags)); |
| 1873 | } |
| 1874 | return out; |
| 1875 | } |
| 1876 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1877 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1878 | private ApplicationInfo generateApplicationInfoFromSettingsLPw(String packageName, int flags, |
| 1879 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1880 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1881 | PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1882 | if (ps != null) { |
| 1883 | if (ps.pkg == null) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1884 | PackageInfo pInfo = generatePackageInfoFromSettingsLPw(packageName, |
| 1885 | flags, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1886 | if (pInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1887 | return pInfo.applicationInfo; |
| 1888 | } |
| 1889 | return null; |
| 1890 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1891 | return PackageParser.generateApplicationInfo(ps.pkg, flags, |
| 1892 | ps.readUserState(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1893 | } |
| 1894 | return null; |
| 1895 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1896 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1897 | private PackageInfo generatePackageInfoFromSettingsLPw(String packageName, int flags, |
| 1898 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1899 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1900 | PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1901 | if (ps != null) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1902 | PackageParser.Package pkg = ps.pkg; |
| 1903 | if (pkg == null) { |
| 1904 | if ((flags & PackageManager.GET_UNINSTALLED_PACKAGES) == 0) { |
| 1905 | return null; |
| 1906 | } |
| 1907 | pkg = new PackageParser.Package(packageName); |
| 1908 | pkg.applicationInfo.packageName = packageName; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1909 | pkg.applicationInfo.flags = ps.pkgFlags | ApplicationInfo.FLAG_IS_DATA_ONLY; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1910 | pkg.applicationInfo.publicSourceDir = ps.resourcePathString; |
| 1911 | pkg.applicationInfo.sourceDir = ps.codePathString; |
| 1912 | pkg.applicationInfo.dataDir = |
Amith Yamasani | 317046a | 2012-08-29 18:48:08 -0700 | [diff] [blame] | 1913 | getDataPathForPackage(packageName, 0).getPath(); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1914 | pkg.applicationInfo.nativeLibraryDir = ps.nativeLibraryPathString; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1915 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1916 | // pkg.mSetEnabled = ps.getEnabled(userId); |
| 1917 | // pkg.mSetStopped = ps.getStopped(userId); |
| 1918 | return generatePackageInfo(pkg, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1919 | } |
| 1920 | return null; |
| 1921 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1922 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1923 | @Override |
| 1924 | public ApplicationInfo getApplicationInfo(String packageName, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1925 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 1926 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, "get application info"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 1927 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1928 | synchronized (mPackages) { |
| 1929 | PackageParser.Package p = mPackages.get(packageName); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 1930 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1931 | TAG, "getApplicationInfo " + packageName |
| 1932 | + ": " + p); |
| 1933 | if (p != null) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1934 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 1935 | if (ps == null) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1936 | // Note: isEnabledLP() does not apply here - always return info |
Jeff Sharkey | 38f8688 | 2012-10-11 16:10:23 -0700 | [diff] [blame] | 1937 | return PackageParser.generateApplicationInfo( |
| 1938 | p, flags, ps.readUserState(userId), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1939 | } |
| 1940 | if ("android".equals(packageName)||"system".equals(packageName)) { |
| 1941 | return mAndroidApplication; |
| 1942 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1943 | if ((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1944 | return generateApplicationInfoFromSettingsLPw(packageName, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1945 | } |
| 1946 | } |
| 1947 | return null; |
| 1948 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1949 | |
| 1950 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1951 | public void freeStorageAndNotify(final long freeStorageSize, final IPackageDataObserver observer) { |
| 1952 | mContext.enforceCallingOrSelfPermission( |
| 1953 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1954 | // Queue up an async operation since clearing cache may take a little while. |
| 1955 | mHandler.post(new Runnable() { |
| 1956 | public void run() { |
| 1957 | mHandler.removeCallbacks(this); |
| 1958 | int retCode = -1; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1959 | synchronized (mInstallLock) { |
| 1960 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1961 | if (retCode < 0) { |
| 1962 | Slog.w(TAG, "Couldn't clear application caches"); |
| 1963 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 1964 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1965 | if (observer != null) { |
| 1966 | try { |
| 1967 | observer.onRemoveCompleted(null, (retCode >= 0)); |
| 1968 | } catch (RemoteException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1969 | Slog.w(TAG, "RemoveException when invoking call back"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1970 | } |
| 1971 | } |
| 1972 | } |
| 1973 | }); |
| 1974 | } |
| 1975 | |
Suchi Amalapurapu | bc806f6 | 2009-06-17 15:18:19 -0700 | [diff] [blame] | 1976 | public void freeStorage(final long freeStorageSize, final IntentSender pi) { |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1977 | mContext.enforceCallingOrSelfPermission( |
| 1978 | android.Manifest.permission.CLEAR_APP_CACHE, null); |
| 1979 | // Queue up an async operation since clearing cache may take a little while. |
| 1980 | mHandler.post(new Runnable() { |
| 1981 | public void run() { |
| 1982 | mHandler.removeCallbacks(this); |
| 1983 | int retCode = -1; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1984 | synchronized (mInstallLock) { |
| 1985 | retCode = mInstaller.freeCache(freeStorageSize); |
| 1986 | if (retCode < 0) { |
| 1987 | Slog.w(TAG, "Couldn't clear application caches"); |
| 1988 | } |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1989 | } |
| 1990 | if(pi != null) { |
| 1991 | try { |
| 1992 | // Callback via pending intent |
| 1993 | int code = (retCode >= 0) ? 1 : 0; |
| 1994 | pi.sendIntent(null, code, null, |
| 1995 | null, null); |
| 1996 | } catch (SendIntentException e1) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 1997 | Slog.i(TAG, "Failed to send pending intent"); |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 1998 | } |
| 1999 | } |
| 2000 | } |
| 2001 | }); |
| 2002 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2003 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2004 | @Override |
| 2005 | public ActivityInfo getActivityInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2006 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2007 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, "get activity info"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2008 | synchronized (mPackages) { |
| 2009 | PackageParser.Activity a = mActivities.mActivities.get(component); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 2010 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2011 | if (DEBUG_PACKAGE_INFO) Log.v(TAG, "getActivityInfo " + component + ": " + a); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2012 | if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2013 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 2014 | if (ps == null) return null; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 2015 | return PackageParser.generateActivityInfo(a, flags, ps.readUserState(userId), |
| 2016 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2017 | } |
| 2018 | if (mResolveComponentName.equals(component)) { |
| 2019 | return mResolveActivity; |
| 2020 | } |
| 2021 | } |
| 2022 | return null; |
| 2023 | } |
| 2024 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2025 | @Override |
| 2026 | public ActivityInfo getReceiverInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2027 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2028 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, "get receiver info"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2029 | synchronized (mPackages) { |
| 2030 | PackageParser.Activity a = mReceivers.mActivities.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2031 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2032 | TAG, "getReceiverInfo " + component + ": " + a); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2033 | if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2034 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 2035 | if (ps == null) return null; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 2036 | return PackageParser.generateActivityInfo(a, flags, ps.readUserState(userId), |
| 2037 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | return null; |
| 2041 | } |
| 2042 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2043 | @Override |
| 2044 | public ServiceInfo getServiceInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2045 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2046 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, "get service info"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2047 | synchronized (mPackages) { |
| 2048 | PackageParser.Service s = mServices.mServices.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2049 | if (DEBUG_PACKAGE_INFO) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2050 | TAG, "getServiceInfo " + component + ": " + s); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2051 | if (s != null && mSettings.isEnabledLPr(s.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2052 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 2053 | if (ps == null) return null; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 2054 | return PackageParser.generateServiceInfo(s, flags, ps.readUserState(userId), |
| 2055 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | return null; |
| 2059 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2060 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2061 | @Override |
| 2062 | public ProviderInfo getProviderInfo(ComponentName component, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2063 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2064 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, "get provider info"); |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 2065 | synchronized (mPackages) { |
| 2066 | PackageParser.Provider p = mProvidersByComponent.get(component); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2067 | if (DEBUG_PACKAGE_INFO) Log.v( |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 2068 | TAG, "getProviderInfo " + component + ": " + p); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2069 | if (p != null && mSettings.isEnabledLPr(p.info, flags, userId)) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2070 | PackageSetting ps = mSettings.mPackages.get(component.getPackageName()); |
| 2071 | if (ps == null) return null; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 2072 | return PackageParser.generateProviderInfo(p, flags, ps.readUserState(userId), |
| 2073 | userId); |
Dianne Hackborn | 361199b | 2010-08-30 17:42:07 -0700 | [diff] [blame] | 2074 | } |
| 2075 | } |
| 2076 | return null; |
| 2077 | } |
| 2078 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2079 | public String[] getSystemSharedLibraryNames() { |
| 2080 | Set<String> libSet; |
| 2081 | synchronized (mPackages) { |
| 2082 | libSet = mSharedLibraries.keySet(); |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 2083 | int size = libSet.size(); |
| 2084 | if (size > 0) { |
| 2085 | String[] libs = new String[size]; |
| 2086 | libSet.toArray(libs); |
| 2087 | return libs; |
| 2088 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2089 | } |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 2090 | return null; |
| 2091 | } |
| 2092 | |
| 2093 | public FeatureInfo[] getSystemAvailableFeatures() { |
| 2094 | Collection<FeatureInfo> featSet; |
| 2095 | synchronized (mPackages) { |
| 2096 | featSet = mAvailableFeatures.values(); |
| 2097 | int size = featSet.size(); |
| 2098 | if (size > 0) { |
| 2099 | FeatureInfo[] features = new FeatureInfo[size+1]; |
| 2100 | featSet.toArray(features); |
| 2101 | FeatureInfo fi = new FeatureInfo(); |
| 2102 | fi.reqGlEsVersion = SystemProperties.getInt("ro.opengles.version", |
| 2103 | FeatureInfo.GL_ES_VERSION_UNDEFINED); |
| 2104 | features[size] = fi; |
| 2105 | return features; |
| 2106 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2107 | } |
| 2108 | return null; |
| 2109 | } |
| 2110 | |
Dianne Hackborn | 039c68e | 2009-09-26 16:39:23 -0700 | [diff] [blame] | 2111 | public boolean hasSystemFeature(String name) { |
| 2112 | synchronized (mPackages) { |
| 2113 | return mAvailableFeatures.containsKey(name); |
| 2114 | } |
| 2115 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2116 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2117 | private void checkValidCaller(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2118 | if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2119 | return; |
| 2120 | |
| 2121 | throw new SecurityException("Caller uid=" + uid |
| 2122 | + " is not privileged to communicate with user=" + userId); |
| 2123 | } |
| 2124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2125 | public int checkPermission(String permName, String pkgName) { |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 2126 | final boolean enforcedDefault = isPermissionEnforcedDefault(permName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2127 | synchronized (mPackages) { |
| 2128 | PackageParser.Package p = mPackages.get(pkgName); |
| 2129 | if (p != null && p.mExtras != null) { |
| 2130 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 2131 | if (ps.sharedUser != null) { |
| 2132 | if (ps.sharedUser.grantedPermissions.contains(permName)) { |
| 2133 | return PackageManager.PERMISSION_GRANTED; |
| 2134 | } |
| 2135 | } else if (ps.grantedPermissions.contains(permName)) { |
| 2136 | return PackageManager.PERMISSION_GRANTED; |
| 2137 | } |
| 2138 | } |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 2139 | if (!isPermissionEnforcedLocked(permName, enforcedDefault)) { |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 2140 | return PackageManager.PERMISSION_GRANTED; |
| 2141 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2142 | } |
| 2143 | return PackageManager.PERMISSION_DENIED; |
| 2144 | } |
| 2145 | |
| 2146 | public int checkUidPermission(String permName, int uid) { |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 2147 | final boolean enforcedDefault = isPermissionEnforcedDefault(permName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2148 | synchronized (mPackages) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2149 | Object obj = mSettings.getUserIdLPr(UserHandle.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2150 | if (obj != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 2151 | GrantedPermissions gp = (GrantedPermissions)obj; |
| 2152 | if (gp.grantedPermissions.contains(permName)) { |
| 2153 | return PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2154 | } |
| 2155 | } else { |
| 2156 | HashSet<String> perms = mSystemPermissions.get(uid); |
| 2157 | if (perms != null && perms.contains(permName)) { |
| 2158 | return PackageManager.PERMISSION_GRANTED; |
| 2159 | } |
| 2160 | } |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 2161 | if (!isPermissionEnforcedLocked(permName, enforcedDefault)) { |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 2162 | return PackageManager.PERMISSION_GRANTED; |
| 2163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2164 | } |
| 2165 | return PackageManager.PERMISSION_DENIED; |
| 2166 | } |
| 2167 | |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2168 | /** |
| 2169 | * Checks if the request is from the system or an app that has INTERACT_ACROSS_USERS |
| 2170 | * or INTERACT_ACROSS_USERS_FULL permissions, if the userid is not for the caller. |
| 2171 | * @param message the message to log on security exception |
| 2172 | * @return |
| 2173 | */ |
| 2174 | private void enforceCrossUserPermission(int callingUid, int userId, |
| 2175 | boolean requireFullPermission, String message) { |
| 2176 | if (userId < 0) { |
| 2177 | throw new IllegalArgumentException("Invalid userId " + userId); |
| 2178 | } |
| 2179 | if (userId == UserHandle.getUserId(callingUid)) return; |
| 2180 | if (callingUid != Process.SYSTEM_UID && callingUid != 0) { |
| 2181 | if (requireFullPermission) { |
| 2182 | mContext.enforceCallingOrSelfPermission( |
| 2183 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, message); |
| 2184 | } else { |
| 2185 | try { |
| 2186 | mContext.enforceCallingOrSelfPermission( |
| 2187 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, message); |
| 2188 | } catch (SecurityException se) { |
| 2189 | mContext.enforceCallingOrSelfPermission( |
| 2190 | android.Manifest.permission.INTERACT_ACROSS_USERS, message); |
| 2191 | } |
| 2192 | } |
| 2193 | } |
| 2194 | } |
| 2195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2196 | private BasePermission findPermissionTreeLP(String permName) { |
| 2197 | for(BasePermission bp : mSettings.mPermissionTrees.values()) { |
| 2198 | if (permName.startsWith(bp.name) && |
| 2199 | permName.length() > bp.name.length() && |
| 2200 | permName.charAt(bp.name.length()) == '.') { |
| 2201 | return bp; |
| 2202 | } |
| 2203 | } |
| 2204 | return null; |
| 2205 | } |
| 2206 | |
| 2207 | private BasePermission checkPermissionTreeLP(String permName) { |
| 2208 | if (permName != null) { |
| 2209 | BasePermission bp = findPermissionTreeLP(permName); |
| 2210 | if (bp != null) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2211 | if (bp.uid == UserHandle.getAppId(Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2212 | return bp; |
| 2213 | } |
| 2214 | throw new SecurityException("Calling uid " |
| 2215 | + Binder.getCallingUid() |
| 2216 | + " is not allowed to add to permission tree " |
| 2217 | + bp.name + " owned by uid " + bp.uid); |
| 2218 | } |
| 2219 | } |
| 2220 | throw new SecurityException("No permission tree found for " + permName); |
| 2221 | } |
| 2222 | |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2223 | static boolean compareStrings(CharSequence s1, CharSequence s2) { |
| 2224 | if (s1 == null) { |
| 2225 | return s2 == null; |
| 2226 | } |
| 2227 | if (s2 == null) { |
| 2228 | return false; |
| 2229 | } |
| 2230 | if (s1.getClass() != s2.getClass()) { |
| 2231 | return false; |
| 2232 | } |
| 2233 | return s1.equals(s2); |
| 2234 | } |
| 2235 | |
| 2236 | static boolean comparePermissionInfos(PermissionInfo pi1, PermissionInfo pi2) { |
| 2237 | if (pi1.icon != pi2.icon) return false; |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 2238 | if (pi1.logo != pi2.logo) return false; |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2239 | if (pi1.protectionLevel != pi2.protectionLevel) return false; |
| 2240 | if (!compareStrings(pi1.name, pi2.name)) return false; |
| 2241 | if (!compareStrings(pi1.nonLocalizedLabel, pi2.nonLocalizedLabel)) return false; |
| 2242 | // We'll take care of setting this one. |
| 2243 | if (!compareStrings(pi1.packageName, pi2.packageName)) return false; |
| 2244 | // These are not currently stored in settings. |
| 2245 | //if (!compareStrings(pi1.group, pi2.group)) return false; |
| 2246 | //if (!compareStrings(pi1.nonLocalizedDescription, pi2.nonLocalizedDescription)) return false; |
| 2247 | //if (pi1.labelRes != pi2.labelRes) return false; |
| 2248 | //if (pi1.descriptionRes != pi2.descriptionRes) return false; |
| 2249 | return true; |
| 2250 | } |
| 2251 | |
| 2252 | boolean addPermissionLocked(PermissionInfo info, boolean async) { |
| 2253 | if (info.labelRes == 0 && info.nonLocalizedLabel == null) { |
| 2254 | throw new SecurityException("Label must be specified in permission"); |
| 2255 | } |
| 2256 | BasePermission tree = checkPermissionTreeLP(info.name); |
| 2257 | BasePermission bp = mSettings.mPermissions.get(info.name); |
| 2258 | boolean added = bp == null; |
| 2259 | boolean changed = true; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2260 | int fixedLevel = PermissionInfo.fixProtectionLevel(info.protectionLevel); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2261 | if (added) { |
| 2262 | bp = new BasePermission(info.name, tree.sourcePackage, |
| 2263 | BasePermission.TYPE_DYNAMIC); |
| 2264 | } else if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 2265 | throw new SecurityException( |
| 2266 | "Not allowed to modify non-dynamic permission " |
| 2267 | + info.name); |
| 2268 | } else { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2269 | if (bp.protectionLevel == fixedLevel |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2270 | && bp.perm.owner.equals(tree.perm.owner) |
| 2271 | && bp.uid == tree.uid |
| 2272 | && comparePermissionInfos(bp.perm.info, info)) { |
| 2273 | changed = false; |
| 2274 | } |
| 2275 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2276 | bp.protectionLevel = fixedLevel; |
| 2277 | info = new PermissionInfo(info); |
| 2278 | info.protectionLevel = fixedLevel; |
| 2279 | bp.perm = new PackageParser.Permission(tree.perm.owner, info); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2280 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 2281 | bp.uid = tree.uid; |
| 2282 | if (added) { |
| 2283 | mSettings.mPermissions.put(info.name, bp); |
| 2284 | } |
| 2285 | if (changed) { |
| 2286 | if (!async) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2287 | mSettings.writeLPr(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2288 | } else { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2289 | scheduleWriteSettingsLocked(); |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | return added; |
| 2293 | } |
| 2294 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2295 | public boolean addPermission(PermissionInfo info) { |
| 2296 | synchronized (mPackages) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 2297 | return addPermissionLocked(info, false); |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | public boolean addPermissionAsync(PermissionInfo info) { |
| 2302 | synchronized (mPackages) { |
| 2303 | return addPermissionLocked(info, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | public void removePermission(String name) { |
| 2308 | synchronized (mPackages) { |
| 2309 | checkPermissionTreeLP(name); |
| 2310 | BasePermission bp = mSettings.mPermissions.get(name); |
| 2311 | if (bp != null) { |
| 2312 | if (bp.type != BasePermission.TYPE_DYNAMIC) { |
| 2313 | throw new SecurityException( |
| 2314 | "Not allowed to modify non-dynamic permission " |
| 2315 | + name); |
| 2316 | } |
| 2317 | mSettings.mPermissions.remove(name); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2318 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | |
Nick Kralevich | c498614 | 2013-03-24 14:27:17 -0700 | [diff] [blame] | 2323 | private static void checkGrantRevokePermissions(PackageParser.Package pkg, BasePermission bp) { |
| 2324 | int index = pkg.requestedPermissions.indexOf(bp.name); |
| 2325 | if (index == -1) { |
| 2326 | throw new SecurityException("Package " + pkg.packageName |
| 2327 | + " has not requested permission " + bp.name); |
| 2328 | } |
| 2329 | boolean isNormal = |
| 2330 | ((bp.protectionLevel&PermissionInfo.PROTECTION_MASK_BASE) |
| 2331 | == PermissionInfo.PROTECTION_NORMAL); |
| 2332 | boolean isDangerous = |
| 2333 | ((bp.protectionLevel&PermissionInfo.PROTECTION_MASK_BASE) |
| 2334 | == PermissionInfo.PROTECTION_DANGEROUS); |
| 2335 | boolean isDevelopment = |
| 2336 | ((bp.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0); |
| 2337 | |
| 2338 | if (!isNormal && !isDangerous && !isDevelopment) { |
| 2339 | throw new SecurityException("Permission " + bp.name |
| 2340 | + " is not a changeable permission type"); |
| 2341 | } |
| 2342 | |
| 2343 | if (isNormal || isDangerous) { |
| 2344 | if (pkg.requestedPermissionsRequired.get(index)) { |
| 2345 | throw new SecurityException("Can't change " + bp.name |
| 2346 | + ". It is required by the application"); |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2351 | public void grantPermission(String packageName, String permissionName) { |
| 2352 | mContext.enforceCallingOrSelfPermission( |
| 2353 | android.Manifest.permission.GRANT_REVOKE_PERMISSIONS, null); |
| 2354 | synchronized (mPackages) { |
| 2355 | final PackageParser.Package pkg = mPackages.get(packageName); |
| 2356 | if (pkg == null) { |
| 2357 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2358 | } |
| 2359 | final BasePermission bp = mSettings.mPermissions.get(permissionName); |
| 2360 | if (bp == null) { |
Nick Kralevich | c498614 | 2013-03-24 14:27:17 -0700 | [diff] [blame] | 2361 | throw new IllegalArgumentException("Unknown permission: " + permissionName); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2362 | } |
Nick Kralevich | c498614 | 2013-03-24 14:27:17 -0700 | [diff] [blame] | 2363 | |
| 2364 | checkGrantRevokePermissions(pkg, bp); |
| 2365 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2366 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2367 | if (ps == null) { |
| 2368 | return; |
| 2369 | } |
Nick Kralevich | c498614 | 2013-03-24 14:27:17 -0700 | [diff] [blame] | 2370 | final GrantedPermissions gp = (ps.sharedUser != null) ? ps.sharedUser : ps; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2371 | if (gp.grantedPermissions.add(permissionName)) { |
| 2372 | if (ps.haveGids) { |
| 2373 | gp.gids = appendInts(gp.gids, bp.gids); |
| 2374 | } |
| 2375 | mSettings.writeLPr(); |
| 2376 | } |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | public void revokePermission(String packageName, String permissionName) { |
Dianne Hackborn | f1b7824 | 2013-04-08 22:28:59 -0700 | [diff] [blame] | 2381 | int changedAppId = -1; |
| 2382 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2383 | synchronized (mPackages) { |
| 2384 | final PackageParser.Package pkg = mPackages.get(packageName); |
| 2385 | if (pkg == null) { |
| 2386 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2387 | } |
| 2388 | if (pkg.applicationInfo.uid != Binder.getCallingUid()) { |
| 2389 | mContext.enforceCallingOrSelfPermission( |
| 2390 | android.Manifest.permission.GRANT_REVOKE_PERMISSIONS, null); |
| 2391 | } |
| 2392 | final BasePermission bp = mSettings.mPermissions.get(permissionName); |
| 2393 | if (bp == null) { |
Nick Kralevich | c498614 | 2013-03-24 14:27:17 -0700 | [diff] [blame] | 2394 | throw new IllegalArgumentException("Unknown permission: " + permissionName); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2395 | } |
Nick Kralevich | c498614 | 2013-03-24 14:27:17 -0700 | [diff] [blame] | 2396 | |
| 2397 | checkGrantRevokePermissions(pkg, bp); |
| 2398 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2399 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2400 | if (ps == null) { |
| 2401 | return; |
| 2402 | } |
Nick Kralevich | c498614 | 2013-03-24 14:27:17 -0700 | [diff] [blame] | 2403 | final GrantedPermissions gp = (ps.sharedUser != null) ? ps.sharedUser : ps; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2404 | if (gp.grantedPermissions.remove(permissionName)) { |
| 2405 | gp.grantedPermissions.remove(permissionName); |
| 2406 | if (ps.haveGids) { |
| 2407 | gp.gids = removeInts(gp.gids, bp.gids); |
| 2408 | } |
| 2409 | mSettings.writeLPr(); |
Dianne Hackborn | f1b7824 | 2013-04-08 22:28:59 -0700 | [diff] [blame] | 2410 | changedAppId = ps.appId; |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | if (changedAppId >= 0) { |
| 2415 | // We changed the perm on someone, kill its processes. |
| 2416 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 2417 | if (am != null) { |
| 2418 | final int callingUserId = UserHandle.getCallingUserId(); |
| 2419 | final long ident = Binder.clearCallingIdentity(); |
| 2420 | try { |
| 2421 | //XXX we should only revoke for the calling user's app permissions, |
| 2422 | // but for now we impact all users. |
| 2423 | //am.killUid(UserHandle.getUid(callingUserId, changedAppId), |
| 2424 | // "revoke " + permissionName); |
| 2425 | int[] users = sUserManager.getUserIds(); |
| 2426 | for (int user : users) { |
| 2427 | am.killUid(UserHandle.getUid(user, changedAppId), |
| 2428 | "revoke " + permissionName); |
| 2429 | } |
| 2430 | } catch (RemoteException e) { |
| 2431 | } finally { |
| 2432 | Binder.restoreCallingIdentity(ident); |
| 2433 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 2438 | public boolean isProtectedBroadcast(String actionName) { |
| 2439 | synchronized (mPackages) { |
| 2440 | return mProtectedBroadcasts.contains(actionName); |
| 2441 | } |
| 2442 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2444 | public int checkSignatures(String pkg1, String pkg2) { |
| 2445 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2446 | final PackageParser.Package p1 = mPackages.get(pkg1); |
| 2447 | final PackageParser.Package p2 = mPackages.get(pkg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2448 | if (p1 == null || p1.mExtras == null |
| 2449 | || p2 == null || p2.mExtras == null) { |
| 2450 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2451 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2452 | return compareSignatures(p1.mSignatures, p2.mSignatures); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2456 | public int checkUidSignatures(int uid1, int uid2) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2457 | // Map to base uids. |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2458 | uid1 = UserHandle.getAppId(uid1); |
| 2459 | uid2 = UserHandle.getAppId(uid2); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2460 | // reader |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2461 | synchronized (mPackages) { |
| 2462 | Signature[] s1; |
| 2463 | Signature[] s2; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2464 | Object obj = mSettings.getUserIdLPr(uid1); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2465 | if (obj != null) { |
| 2466 | if (obj instanceof SharedUserSetting) { |
| 2467 | s1 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 2468 | } else if (obj instanceof PackageSetting) { |
| 2469 | s1 = ((PackageSetting)obj).signatures.mSignatures; |
| 2470 | } else { |
| 2471 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2472 | } |
| 2473 | } else { |
| 2474 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2475 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2476 | obj = mSettings.getUserIdLPr(uid2); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2477 | if (obj != null) { |
| 2478 | if (obj instanceof SharedUserSetting) { |
| 2479 | s2 = ((SharedUserSetting)obj).signatures.mSignatures; |
| 2480 | } else if (obj instanceof PackageSetting) { |
| 2481 | s2 = ((PackageSetting)obj).signatures.mSignatures; |
| 2482 | } else { |
| 2483 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2484 | } |
| 2485 | } else { |
| 2486 | return PackageManager.SIGNATURE_UNKNOWN_PACKAGE; |
| 2487 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2488 | return compareSignatures(s1, s2); |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2489 | } |
| 2490 | } |
| 2491 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2492 | static int compareSignatures(Signature[] s1, Signature[] s2) { |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2493 | if (s1 == null) { |
| 2494 | return s2 == null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2495 | ? PackageManager.SIGNATURE_NEITHER_SIGNED |
| 2496 | : PackageManager.SIGNATURE_FIRST_NOT_SIGNED; |
| 2497 | } |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 2498 | if (s2 == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2499 | return PackageManager.SIGNATURE_SECOND_NOT_SIGNED; |
| 2500 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2501 | HashSet<Signature> set1 = new HashSet<Signature>(); |
| 2502 | for (Signature sig : s1) { |
| 2503 | set1.add(sig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2504 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 2505 | HashSet<Signature> set2 = new HashSet<Signature>(); |
| 2506 | for (Signature sig : s2) { |
| 2507 | set2.add(sig); |
| 2508 | } |
| 2509 | // Make sure s2 contains all signatures in s1. |
| 2510 | if (set1.equals(set2)) { |
| 2511 | return PackageManager.SIGNATURE_MATCH; |
| 2512 | } |
| 2513 | return PackageManager.SIGNATURE_NO_MATCH; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | public String[] getPackagesForUid(int uid) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2517 | uid = UserHandle.getAppId(uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2518 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2519 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2520 | Object obj = mSettings.getUserIdLPr(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2521 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2522 | final SharedUserSetting sus = (SharedUserSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2523 | final int N = sus.packages.size(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2524 | final String[] res = new String[N]; |
| 2525 | final Iterator<PackageSetting> it = sus.packages.iterator(); |
| 2526 | int i = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2527 | while (it.hasNext()) { |
| 2528 | res[i++] = it.next().name; |
| 2529 | } |
| 2530 | return res; |
| 2531 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2532 | final PackageSetting ps = (PackageSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2533 | return new String[] { ps.name }; |
| 2534 | } |
| 2535 | } |
| 2536 | return null; |
| 2537 | } |
| 2538 | |
| 2539 | public String getNameForUid(int uid) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2540 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2541 | synchronized (mPackages) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2542 | Object obj = mSettings.getUserIdLPr(UserHandle.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2543 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2544 | final SharedUserSetting sus = (SharedUserSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2545 | return sus.name + ":" + sus.userId; |
| 2546 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2547 | final PackageSetting ps = (PackageSetting) obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2548 | return ps.name; |
| 2549 | } |
| 2550 | } |
| 2551 | return null; |
| 2552 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2553 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2554 | public int getUidForSharedUser(String sharedUserName) { |
| 2555 | if(sharedUserName == null) { |
| 2556 | return -1; |
| 2557 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2558 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2559 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2560 | final SharedUserSetting suid = mSettings.getSharedUserLPw(sharedUserName, 0, false); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2561 | if (suid == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2562 | return -1; |
| 2563 | } |
| 2564 | return suid.userId; |
| 2565 | } |
| 2566 | } |
| 2567 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2568 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2569 | public ResolveInfo resolveIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2570 | int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2571 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2572 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, "resolve intent"); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2573 | List<ResolveInfo> query = queryIntentActivities(intent, resolvedType, flags, userId); |
| 2574 | return chooseBestActivity(intent, resolvedType, flags, query, userId); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 2575 | } |
| 2576 | |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 2577 | private ResolveInfo chooseBestActivity(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2578 | int flags, List<ResolveInfo> query, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2579 | if (query != null) { |
| 2580 | final int N = query.size(); |
| 2581 | if (N == 1) { |
| 2582 | return query.get(0); |
| 2583 | } else if (N > 1) { |
| 2584 | // If there is more than one activity with the same priority, |
| 2585 | // then let the user decide between them. |
| 2586 | ResolveInfo r0 = query.get(0); |
| 2587 | ResolveInfo r1 = query.get(1); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 2588 | if (DEBUG_INTENT_MATCHING) { |
| 2589 | Log.d(TAG, r0.activityInfo.name + "=" + r0.priority + " vs " |
| 2590 | + r1.activityInfo.name + "=" + r1.priority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2591 | } |
| 2592 | // If the first activity has a higher priority, or a different |
| 2593 | // default, then it is always desireable to pick it. |
| 2594 | if (r0.priority != r1.priority |
| 2595 | || r0.preferredOrder != r1.preferredOrder |
| 2596 | || r0.isDefault != r1.isDefault) { |
| 2597 | return query.get(0); |
| 2598 | } |
| 2599 | // If we have saved a preference for a preferred activity for |
| 2600 | // this Intent, use that. |
| 2601 | ResolveInfo ri = findPreferredActivity(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2602 | flags, query, r0.priority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2603 | if (ri != null) { |
| 2604 | return ri; |
| 2605 | } |
Dianne Hackborn | 8da429e | 2012-09-23 12:52:19 -0700 | [diff] [blame] | 2606 | if (userId != 0) { |
| 2607 | ri = new ResolveInfo(mResolveInfo); |
| 2608 | ri.activityInfo = new ActivityInfo(ri.activityInfo); |
| 2609 | ri.activityInfo.applicationInfo = new ApplicationInfo( |
| 2610 | ri.activityInfo.applicationInfo); |
| 2611 | ri.activityInfo.applicationInfo.uid = UserHandle.getUid(userId, |
| 2612 | UserHandle.getAppId(ri.activityInfo.applicationInfo.uid)); |
| 2613 | return ri; |
| 2614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | return mResolveInfo; |
| 2616 | } |
| 2617 | } |
| 2618 | return null; |
| 2619 | } |
| 2620 | |
| 2621 | ResolveInfo findPreferredActivity(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2622 | int flags, List<ResolveInfo> query, int priority, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2623 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2624 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2625 | synchronized (mPackages) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2626 | if (intent.getSelector() != null) { |
| 2627 | intent = intent.getSelector(); |
| 2628 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2629 | if (DEBUG_PREFERRED) intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION); |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 2630 | PreferredIntentResolver pir = mSettings.mPreferredActivities.get(userId); |
| 2631 | List<PreferredActivity> prefs = pir != null |
| 2632 | ? pir.queryIntent(intent, resolvedType, |
| 2633 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId) |
| 2634 | : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2635 | if (prefs != null && prefs.size() > 0) { |
| 2636 | // First figure out how good the original match set is. |
| 2637 | // We will only allow preferred activities that came |
| 2638 | // from the same match quality. |
| 2639 | int match = 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2640 | |
| 2641 | if (DEBUG_PREFERRED) { |
| 2642 | Log.v(TAG, "Figuring out best match..."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2643 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2644 | |
| 2645 | final int N = query.size(); |
| 2646 | for (int j=0; j<N; j++) { |
| 2647 | final ResolveInfo ri = query.get(j); |
| 2648 | if (DEBUG_PREFERRED) { |
| 2649 | Log.v(TAG, "Match for " + ri.activityInfo + ": 0x" |
| 2650 | + Integer.toHexString(match)); |
| 2651 | } |
| 2652 | if (ri.match > match) { |
| 2653 | match = ri.match; |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | if (DEBUG_PREFERRED) { |
| 2658 | Log.v(TAG, "Best match: 0x" + Integer.toHexString(match)); |
| 2659 | } |
| 2660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2661 | match &= IntentFilter.MATCH_CATEGORY_MASK; |
| 2662 | final int M = prefs.size(); |
| 2663 | for (int i=0; i<M; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2664 | final PreferredActivity pa = prefs.get(i); |
Dianne Hackborn | f8b8a3f | 2011-03-04 00:05:31 -0800 | [diff] [blame] | 2665 | if (pa.mPref.mMatch != match) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2666 | continue; |
| 2667 | } |
Dianne Hackborn | 77119bc | 2012-10-23 14:32:48 -0700 | [diff] [blame] | 2668 | final ActivityInfo ai = getActivityInfo(pa.mPref.mComponent, |
| 2669 | flags | PackageManager.GET_DISABLED_COMPONENTS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2670 | if (DEBUG_PREFERRED) { |
| 2671 | Log.v(TAG, "Got preferred activity:"); |
Marco Nelissen | d85621c | 2010-09-03 09:25:33 -0700 | [diff] [blame] | 2672 | if (ai != null) { |
| 2673 | ai.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 2674 | } else { |
| 2675 | Log.v(TAG, " null"); |
| 2676 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2677 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2678 | if (ai == null) { |
| 2679 | // This previously registered preferred activity |
| 2680 | // component is no longer known. Most likely an update |
| 2681 | // to the app was installed and in the new version this |
| 2682 | // component no longer exists. Clean it up by removing |
| 2683 | // it from the preferred activities list, and skip it. |
| 2684 | Slog.w(TAG, "Removing dangling preferred activity: " |
| 2685 | + pa.mPref.mComponent); |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 2686 | pir.removeFilter(pa); |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2687 | continue; |
| 2688 | } |
| 2689 | for (int j=0; j<N; j++) { |
| 2690 | final ResolveInfo ri = query.get(j); |
| 2691 | if (!ri.activityInfo.applicationInfo.packageName |
| 2692 | .equals(ai.applicationInfo.packageName)) { |
| 2693 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2694 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2695 | if (!ri.activityInfo.name.equals(ai.name)) { |
| 2696 | continue; |
| 2697 | } |
| 2698 | |
| 2699 | // Okay we found a previously set preferred app. |
| 2700 | // If the result set is different from when this |
| 2701 | // was created, we need to clear it and re-ask the |
| 2702 | // user their preference. |
| 2703 | if (!pa.mPref.sameSet(query, priority)) { |
| 2704 | Slog.i(TAG, "Result set changed, dropping preferred activity for " |
| 2705 | + intent + " type " + resolvedType); |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 2706 | pir.removeFilter(pa); |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 2707 | return null; |
| 2708 | } |
| 2709 | |
| 2710 | // Yay! |
| 2711 | return ri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2712 | } |
| 2713 | } |
| 2714 | } |
| 2715 | } |
| 2716 | return null; |
| 2717 | } |
| 2718 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2719 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | public List<ResolveInfo> queryIntentActivities(Intent intent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2721 | String resolvedType, int flags, int userId) { |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2722 | if (!sUserManager.exists(userId)) return Collections.emptyList(); |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2723 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, "query intent activities"); |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2724 | ComponentName comp = intent.getComponent(); |
| 2725 | if (comp == null) { |
| 2726 | if (intent.getSelector() != null) { |
| 2727 | intent = intent.getSelector(); |
| 2728 | comp = intent.getComponent(); |
| 2729 | } |
| 2730 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2731 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2732 | if (comp != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2733 | final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2734 | final ActivityInfo ai = getActivityInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2735 | if (ai != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2736 | final ResolveInfo ri = new ResolveInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | ri.activityInfo = ai; |
| 2738 | list.add(ri); |
| 2739 | } |
| 2740 | return list; |
| 2741 | } |
| 2742 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2743 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2744 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2745 | final String pkgName = intent.getPackage(); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2746 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2747 | return mActivities.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2748 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2749 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2750 | if (pkg != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2751 | return mActivities.queryIntentForPackage(intent, resolvedType, flags, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2752 | pkg.activities, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2753 | } |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 2754 | return new ArrayList<ResolveInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2755 | } |
| 2756 | } |
| 2757 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2758 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2759 | public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller, |
| 2760 | Intent[] specifics, String[] specificTypes, Intent intent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2761 | String resolvedType, int flags, int userId) { |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2762 | if (!sUserManager.exists(userId)) return Collections.emptyList(); |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2763 | enforceCrossUserPermission(Binder.getCallingUid(), userId, false, |
| 2764 | "query intent activity options"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2765 | final String resultsAction = intent.getAction(); |
| 2766 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2767 | List<ResolveInfo> results = queryIntentActivities(intent, resolvedType, flags |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2768 | | PackageManager.GET_RESOLVED_FILTER, userId); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2769 | |
| 2770 | if (DEBUG_INTENT_MATCHING) { |
| 2771 | Log.v(TAG, "Query " + intent + ": " + results); |
| 2772 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2773 | |
| 2774 | int specificsPos = 0; |
| 2775 | int N; |
| 2776 | |
| 2777 | // todo: note that the algorithm used here is O(N^2). This |
| 2778 | // isn't a problem in our current environment, but if we start running |
| 2779 | // into situations where we have more than 5 or 10 matches then this |
| 2780 | // should probably be changed to something smarter... |
| 2781 | |
| 2782 | // First we go through and resolve each of the specific items |
| 2783 | // that were supplied, taking care of removing any corresponding |
| 2784 | // duplicate items in the generic resolve list. |
| 2785 | if (specifics != null) { |
| 2786 | for (int i=0; i<specifics.length; i++) { |
| 2787 | final Intent sintent = specifics[i]; |
| 2788 | if (sintent == null) { |
| 2789 | continue; |
| 2790 | } |
| 2791 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2792 | if (DEBUG_INTENT_MATCHING) { |
| 2793 | Log.v(TAG, "Specific #" + i + ": " + sintent); |
| 2794 | } |
| 2795 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2796 | String action = sintent.getAction(); |
| 2797 | if (resultsAction != null && resultsAction.equals(action)) { |
| 2798 | // If this action was explicitly requested, then don't |
| 2799 | // remove things that have it. |
| 2800 | action = null; |
| 2801 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2803 | ResolveInfo ri = null; |
| 2804 | ActivityInfo ai = null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2805 | |
| 2806 | ComponentName comp = sintent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2807 | if (comp == null) { |
| 2808 | ri = resolveIntent( |
| 2809 | sintent, |
| 2810 | specificTypes != null ? specificTypes[i] : null, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2811 | flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2812 | if (ri == null) { |
| 2813 | continue; |
| 2814 | } |
| 2815 | if (ri == mResolveInfo) { |
| 2816 | // ACK! Must do something better with this. |
| 2817 | } |
| 2818 | ai = ri.activityInfo; |
| 2819 | comp = new ComponentName(ai.applicationInfo.packageName, |
| 2820 | ai.name); |
| 2821 | } else { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2822 | ai = getActivityInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2823 | if (ai == null) { |
| 2824 | continue; |
| 2825 | } |
| 2826 | } |
| 2827 | |
| 2828 | // Look for any generic query activities that are duplicates |
| 2829 | // of this specific one, and remove them from the results. |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2830 | 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] | 2831 | N = results.size(); |
| 2832 | int j; |
| 2833 | for (j=specificsPos; j<N; j++) { |
| 2834 | ResolveInfo sri = results.get(j); |
| 2835 | if ((sri.activityInfo.name.equals(comp.getClassName()) |
| 2836 | && sri.activityInfo.applicationInfo.packageName.equals( |
| 2837 | comp.getPackageName())) |
| 2838 | || (action != null && sri.filter.matchAction(action))) { |
| 2839 | results.remove(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2840 | if (DEBUG_INTENT_MATCHING) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2841 | TAG, "Removing duplicate item from " + j |
| 2842 | + " due to specific " + specificsPos); |
| 2843 | if (ri == null) { |
| 2844 | ri = sri; |
| 2845 | } |
| 2846 | j--; |
| 2847 | N--; |
| 2848 | } |
| 2849 | } |
| 2850 | |
| 2851 | // Add this specific item to its proper place. |
| 2852 | if (ri == null) { |
| 2853 | ri = new ResolveInfo(); |
| 2854 | ri.activityInfo = ai; |
| 2855 | } |
| 2856 | results.add(specificsPos, ri); |
| 2857 | ri.specificIndex = i; |
| 2858 | specificsPos++; |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | // Now we go through the remaining generic results and remove any |
| 2863 | // duplicate actions that are found here. |
| 2864 | N = results.size(); |
| 2865 | for (int i=specificsPos; i<N-1; i++) { |
| 2866 | final ResolveInfo rii = results.get(i); |
| 2867 | if (rii.filter == null) { |
| 2868 | continue; |
| 2869 | } |
| 2870 | |
| 2871 | // Iterate over all of the actions of this result's intent |
| 2872 | // filter... typically this should be just one. |
| 2873 | final Iterator<String> it = rii.filter.actionsIterator(); |
| 2874 | if (it == null) { |
| 2875 | continue; |
| 2876 | } |
| 2877 | while (it.hasNext()) { |
| 2878 | final String action = it.next(); |
| 2879 | if (resultsAction != null && resultsAction.equals(action)) { |
| 2880 | // If this action was explicitly requested, then don't |
| 2881 | // remove things that have it. |
| 2882 | continue; |
| 2883 | } |
| 2884 | for (int j=i+1; j<N; j++) { |
| 2885 | final ResolveInfo rij = results.get(j); |
| 2886 | if (rij.filter != null && rij.filter.hasAction(action)) { |
| 2887 | results.remove(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2888 | if (DEBUG_INTENT_MATCHING) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2889 | TAG, "Removing duplicate item from " + j |
| 2890 | + " due to action " + action + " at " + i); |
| 2891 | j--; |
| 2892 | N--; |
| 2893 | } |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | // If the caller didn't request filter information, drop it now |
| 2898 | // so we don't have to marshall/unmarshall it. |
| 2899 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2900 | rii.filter = null; |
| 2901 | } |
| 2902 | } |
| 2903 | |
| 2904 | // Filter out the caller activity if so requested. |
| 2905 | if (caller != null) { |
| 2906 | N = results.size(); |
| 2907 | for (int i=0; i<N; i++) { |
| 2908 | ActivityInfo ainfo = results.get(i).activityInfo; |
| 2909 | if (caller.getPackageName().equals(ainfo.applicationInfo.packageName) |
| 2910 | && caller.getClassName().equals(ainfo.name)) { |
| 2911 | results.remove(i); |
| 2912 | break; |
| 2913 | } |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | // If the caller didn't request filter information, |
| 2918 | // drop them now so we don't have to |
| 2919 | // marshall/unmarshall it. |
| 2920 | if ((flags&PackageManager.GET_RESOLVED_FILTER) == 0) { |
| 2921 | N = results.size(); |
| 2922 | for (int i=0; i<N; i++) { |
| 2923 | results.get(i).filter = null; |
| 2924 | } |
| 2925 | } |
| 2926 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 2927 | if (DEBUG_INTENT_MATCHING) Log.v(TAG, "Result: " + results); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2928 | return results; |
| 2929 | } |
| 2930 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2931 | @Override |
| 2932 | public List<ResolveInfo> queryIntentReceivers(Intent intent, String resolvedType, int flags, |
| 2933 | int userId) { |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2934 | if (!sUserManager.exists(userId)) return Collections.emptyList(); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2935 | ComponentName comp = intent.getComponent(); |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2936 | if (comp == null) { |
| 2937 | if (intent.getSelector() != null) { |
| 2938 | intent = intent.getSelector(); |
| 2939 | comp = intent.getComponent(); |
| 2940 | } |
| 2941 | } |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2942 | if (comp != null) { |
| 2943 | List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2944 | ActivityInfo ai = getReceiverInfo(comp, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2945 | if (ai != null) { |
| 2946 | ResolveInfo ri = new ResolveInfo(); |
| 2947 | ri.activityInfo = ai; |
| 2948 | list.add(ri); |
| 2949 | } |
| 2950 | return list; |
| 2951 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2952 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2953 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2954 | synchronized (mPackages) { |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2955 | String pkgName = intent.getPackage(); |
| 2956 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2957 | return mReceivers.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2958 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2959 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2960 | if (pkg != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2961 | return mReceivers.queryIntentForPackage(intent, resolvedType, flags, pkg.receivers, |
| 2962 | userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 2963 | } |
| 2964 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2965 | } |
| 2966 | } |
| 2967 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2968 | @Override |
| 2969 | public ResolveInfo resolveService(Intent intent, String resolvedType, int flags, int userId) { |
| 2970 | List<ResolveInfo> query = queryIntentServices(intent, resolvedType, flags, userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2971 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2972 | if (query != null) { |
| 2973 | if (query.size() >= 1) { |
| 2974 | // If there is more than one service with the same priority, |
| 2975 | // just arbitrarily pick the first one. |
| 2976 | return query.get(0); |
| 2977 | } |
| 2978 | } |
| 2979 | return null; |
| 2980 | } |
| 2981 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2982 | @Override |
| 2983 | public List<ResolveInfo> queryIntentServices(Intent intent, String resolvedType, int flags, |
| 2984 | int userId) { |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2985 | if (!sUserManager.exists(userId)) return Collections.emptyList(); |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 2986 | ComponentName comp = intent.getComponent(); |
| 2987 | if (comp == null) { |
| 2988 | if (intent.getSelector() != null) { |
| 2989 | intent = intent.getSelector(); |
| 2990 | comp = intent.getComponent(); |
| 2991 | } |
| 2992 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2993 | if (comp != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2994 | final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2995 | final ServiceInfo si = getServiceInfo(comp, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2996 | if (si != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 2997 | final ResolveInfo ri = new ResolveInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2998 | ri.serviceInfo = si; |
| 2999 | list.add(ri); |
| 3000 | } |
| 3001 | return list; |
| 3002 | } |
| 3003 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3004 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3005 | synchronized (mPackages) { |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 3006 | String pkgName = intent.getPackage(); |
| 3007 | if (pkgName == null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3008 | return mServices.queryIntent(intent, resolvedType, flags, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 3009 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3010 | final PackageParser.Package pkg = mPackages.get(pkgName); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 3011 | if (pkg != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3012 | return mServices.queryIntentForPackage(intent, resolvedType, flags, pkg.services, |
| 3013 | userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 3014 | } |
| 3015 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3016 | } |
| 3017 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3018 | |
Amith Yamasani | 151ec4c | 2012-09-07 19:25:16 -0700 | [diff] [blame] | 3019 | @Override |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3020 | public ParceledListSlice<PackageInfo> getInstalledPackages(int flags, int userId) { |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 3021 | final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0; |
Amith Yamasani | 151ec4c | 2012-09-07 19:25:16 -0700 | [diff] [blame] | 3022 | |
| 3023 | enforceCrossUserPermission(Binder.getCallingUid(), userId, true, "get installed packages"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3024 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3025 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3026 | synchronized (mPackages) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3027 | ArrayList<PackageInfo> list; |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 3028 | if (listUninstalled) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3029 | list = new ArrayList<PackageInfo>(mSettings.mPackages.size()); |
| 3030 | for (PackageSetting ps : mSettings.mPackages.values()) { |
| 3031 | PackageInfo pi; |
| 3032 | if (ps.pkg != null) { |
| 3033 | pi = generatePackageInfo(ps.pkg, flags, userId); |
| 3034 | } else { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3035 | pi = generatePackageInfoFromSettingsLPw(ps.name, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3036 | } |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3037 | if (pi != null) { |
| 3038 | list.add(pi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3039 | } |
| 3040 | } |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3041 | } else { |
| 3042 | list = new ArrayList<PackageInfo>(mPackages.size()); |
| 3043 | for (PackageParser.Package p : mPackages.values()) { |
| 3044 | PackageInfo pi = generatePackageInfo(p, flags, userId); |
| 3045 | if (pi != null) { |
| 3046 | list.add(pi); |
| 3047 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3048 | } |
| 3049 | } |
Kenny Root | 0e2c0f3 | 2011-04-15 17:50:10 -0700 | [diff] [blame] | 3050 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3051 | return new ParceledListSlice<PackageInfo>(list); |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | private void addPackageHoldingPermissions(ArrayList<PackageInfo> list, PackageSetting ps, |
| 3056 | String[] permissions, boolean[] tmp, int flags, int userId) { |
| 3057 | int numMatch = 0; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3058 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3059 | for (int i=0; i<permissions.length; i++) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3060 | if (gp.grantedPermissions.contains(permissions[i])) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3061 | tmp[i] = true; |
| 3062 | numMatch++; |
| 3063 | } else { |
| 3064 | tmp[i] = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3065 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3066 | } |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3067 | if (numMatch == 0) { |
| 3068 | return; |
| 3069 | } |
| 3070 | PackageInfo pi; |
| 3071 | if (ps.pkg != null) { |
| 3072 | pi = generatePackageInfo(ps.pkg, flags, userId); |
| 3073 | } else { |
| 3074 | pi = generatePackageInfoFromSettingsLPw(ps.name, flags, userId); |
| 3075 | } |
| 3076 | if ((flags&PackageManager.GET_PERMISSIONS) == 0) { |
| 3077 | if (numMatch == permissions.length) { |
| 3078 | pi.requestedPermissions = permissions; |
| 3079 | } else { |
| 3080 | pi.requestedPermissions = new String[numMatch]; |
| 3081 | numMatch = 0; |
| 3082 | for (int i=0; i<permissions.length; i++) { |
| 3083 | if (tmp[i]) { |
| 3084 | pi.requestedPermissions[numMatch] = permissions[i]; |
| 3085 | numMatch++; |
| 3086 | } |
| 3087 | } |
| 3088 | } |
| 3089 | } |
| 3090 | list.add(pi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3091 | } |
| 3092 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3093 | @Override |
Dianne Hackborn | e799175 | 2013-01-16 17:56:46 -0800 | [diff] [blame] | 3094 | public ParceledListSlice<PackageInfo> getPackagesHoldingPermissions( |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3095 | String[] permissions, int flags, int userId) { |
Dianne Hackborn | e799175 | 2013-01-16 17:56:46 -0800 | [diff] [blame] | 3096 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | e799175 | 2013-01-16 17:56:46 -0800 | [diff] [blame] | 3097 | final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0; |
| 3098 | |
| 3099 | // writer |
| 3100 | synchronized (mPackages) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3101 | ArrayList<PackageInfo> list = new ArrayList<PackageInfo>(); |
| 3102 | boolean[] tmpBools = new boolean[permissions.length]; |
Dianne Hackborn | e799175 | 2013-01-16 17:56:46 -0800 | [diff] [blame] | 3103 | if (listUninstalled) { |
| 3104 | for (PackageSetting ps : mSettings.mPackages.values()) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3105 | addPackageHoldingPermissions(list, ps, permissions, tmpBools, flags, userId); |
Dianne Hackborn | e799175 | 2013-01-16 17:56:46 -0800 | [diff] [blame] | 3106 | } |
| 3107 | } else { |
| 3108 | for (PackageParser.Package pkg : mPackages.values()) { |
| 3109 | PackageSetting ps = (PackageSetting)pkg.mExtras; |
| 3110 | if (ps != null) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3111 | addPackageHoldingPermissions(list, ps, permissions, tmpBools, flags, |
| 3112 | userId); |
| 3113 | } |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | return new ParceledListSlice<PackageInfo>(list); |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | @Override |
| 3122 | public ParceledListSlice<ApplicationInfo> getInstalledApplications(int flags, int userId) { |
| 3123 | if (!sUserManager.exists(userId)) return null; |
| 3124 | final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0; |
| 3125 | |
| 3126 | // writer |
| 3127 | synchronized (mPackages) { |
| 3128 | ArrayList<ApplicationInfo> list; |
| 3129 | if (listUninstalled) { |
| 3130 | list = new ArrayList<ApplicationInfo>(mSettings.mPackages.size()); |
| 3131 | for (PackageSetting ps : mSettings.mPackages.values()) { |
| 3132 | ApplicationInfo ai; |
| 3133 | if (ps.pkg != null) { |
| 3134 | ai = PackageParser.generateApplicationInfo(ps.pkg, flags, |
| 3135 | ps.readUserState(userId), userId); |
| 3136 | } else { |
| 3137 | ai = generateApplicationInfoFromSettingsLPw(ps.name, flags, userId); |
| 3138 | } |
| 3139 | if (ai != null) { |
| 3140 | list.add(ai); |
| 3141 | } |
| 3142 | } |
| 3143 | } else { |
| 3144 | list = new ArrayList<ApplicationInfo>(mPackages.size()); |
| 3145 | for (PackageParser.Package p : mPackages.values()) { |
| 3146 | if (p.mExtras != null) { |
| 3147 | ApplicationInfo ai = PackageParser.generateApplicationInfo(p, flags, |
| 3148 | ((PackageSetting)p.mExtras).readUserState(userId), userId); |
| 3149 | if (ai != null) { |
| 3150 | list.add(ai); |
Dianne Hackborn | e799175 | 2013-01-16 17:56:46 -0800 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | } |
| 3154 | } |
| 3155 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 3156 | return new ParceledListSlice<ApplicationInfo>(list); |
Dianne Hackborn | e799175 | 2013-01-16 17:56:46 -0800 | [diff] [blame] | 3157 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | public List<ApplicationInfo> getPersistentApplications(int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3161 | final ArrayList<ApplicationInfo> finalList = new ArrayList<ApplicationInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3162 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3163 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3164 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3165 | final Iterator<PackageParser.Package> i = mPackages.values().iterator(); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3166 | final int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3167 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3168 | final PackageParser.Package p = i.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3169 | if (p.applicationInfo != null |
| 3170 | && (p.applicationInfo.flags&ApplicationInfo.FLAG_PERSISTENT) != 0 |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3171 | && (!mSafeMode || isSystemApp(p))) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3172 | PackageSetting ps = mSettings.mPackages.get(p.packageName); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3173 | if (ps != null) { |
| 3174 | ApplicationInfo ai = PackageParser.generateApplicationInfo(p, flags, |
| 3175 | ps.readUserState(userId), userId); |
| 3176 | if (ai != null) { |
| 3177 | finalList.add(ai); |
| 3178 | } |
| 3179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | return finalList; |
| 3185 | } |
| 3186 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3187 | @Override |
| 3188 | public ProviderInfo resolveContentProvider(String name, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3189 | if (!sUserManager.exists(userId)) return null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3190 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3191 | synchronized (mPackages) { |
| 3192 | final PackageParser.Provider provider = mProviders.get(name); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3193 | PackageSetting ps = provider != null |
| 3194 | ? mSettings.mPackages.get(provider.owner.packageName) |
| 3195 | : null; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3196 | return ps != null |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3197 | && mSettings.isEnabledLPr(provider.info, flags, userId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3198 | && (!mSafeMode || (provider.info.applicationInfo.flags |
| 3199 | &ApplicationInfo.FLAG_SYSTEM) != 0) |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3200 | ? PackageParser.generateProviderInfo(provider, flags, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3201 | ps.readUserState(userId), userId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3202 | : null; |
| 3203 | } |
| 3204 | } |
| 3205 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3206 | /** |
| 3207 | * @deprecated |
| 3208 | */ |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 3209 | @Deprecated |
| 3210 | public void querySyncProviders(List<String> outNames, List<ProviderInfo> outInfo) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3211 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3212 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3213 | final Iterator<Map.Entry<String, PackageParser.Provider>> i = mProviders.entrySet() |
| 3214 | .iterator(); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3215 | final int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3216 | while (i.hasNext()) { |
| 3217 | Map.Entry<String, PackageParser.Provider> entry = i.next(); |
| 3218 | PackageParser.Provider p = entry.getValue(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3219 | PackageSetting ps = mSettings.mPackages.get(p.owner.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3220 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3221 | if (ps != null && p.syncable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3222 | && (!mSafeMode || (p.info.applicationInfo.flags |
| 3223 | &ApplicationInfo.FLAG_SYSTEM) != 0)) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3224 | ProviderInfo info = PackageParser.generateProviderInfo(p, 0, |
| 3225 | ps.readUserState(userId), userId); |
| 3226 | if (info != null) { |
| 3227 | outNames.add(entry.getKey()); |
| 3228 | outInfo.add(info); |
| 3229 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3230 | } |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | |
| 3235 | public List<ProviderInfo> queryContentProviders(String processName, |
| 3236 | int uid, int flags) { |
| 3237 | ArrayList<ProviderInfo> finalList = null; |
| 3238 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3239 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3240 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3241 | final Iterator<PackageParser.Provider> i = mProvidersByComponent.values().iterator(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3242 | final int userId = processName != null ? |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3243 | UserHandle.getUserId(uid) : UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3244 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3245 | final PackageParser.Provider p = i.next(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3246 | PackageSetting ps = mSettings.mPackages.get(p.owner.packageName); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3247 | if (ps != null && p.info.authority != null |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3248 | && (processName == null |
| 3249 | || (p.info.processName.equals(processName) |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3250 | && UserHandle.isSameApp(p.info.applicationInfo.uid, uid))) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3251 | && mSettings.isEnabledLPr(p.info, flags, userId) |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3252 | && (!mSafeMode |
| 3253 | || (p.info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3254 | if (finalList == null) { |
| 3255 | finalList = new ArrayList<ProviderInfo>(3); |
| 3256 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3257 | ProviderInfo info = PackageParser.generateProviderInfo(p, flags, |
| 3258 | ps.readUserState(userId), userId); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 3259 | if (info != null) { |
| 3260 | finalList.add(info); |
| 3261 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3262 | } |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | if (finalList != null) { |
| 3267 | Collections.sort(finalList, mProviderInitOrderSorter); |
| 3268 | } |
| 3269 | |
| 3270 | return finalList; |
| 3271 | } |
| 3272 | |
| 3273 | public InstrumentationInfo getInstrumentationInfo(ComponentName name, |
| 3274 | int flags) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3275 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3276 | synchronized (mPackages) { |
| 3277 | final PackageParser.Instrumentation i = mInstrumentation.get(name); |
| 3278 | return PackageParser.generateInstrumentationInfo(i, flags); |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | public List<InstrumentationInfo> queryInstrumentation(String targetPackage, |
| 3283 | int flags) { |
| 3284 | ArrayList<InstrumentationInfo> finalList = |
| 3285 | new ArrayList<InstrumentationInfo>(); |
| 3286 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3287 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3288 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3289 | final Iterator<PackageParser.Instrumentation> i = mInstrumentation.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3290 | while (i.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3291 | final PackageParser.Instrumentation p = i.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3292 | if (targetPackage == null |
| 3293 | || targetPackage.equals(p.info.targetPackage)) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 3294 | InstrumentationInfo ii = PackageParser.generateInstrumentationInfo(p, |
| 3295 | flags); |
| 3296 | if (ii != null) { |
| 3297 | finalList.add(ii); |
| 3298 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3299 | } |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | return finalList; |
| 3304 | } |
| 3305 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 3306 | 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] | 3307 | String[] files = dir.list(); |
Dianne Hackborn | 3b4bac7 | 2010-09-20 11:37:52 -0700 | [diff] [blame] | 3308 | if (files == null) { |
| 3309 | Log.d(TAG, "No files in app dir " + dir); |
| 3310 | return; |
| 3311 | } |
| 3312 | |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 3313 | if (DEBUG_PACKAGE_SCANNING) { |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 3314 | Log.d(TAG, "Scanning app dir " + dir); |
| 3315 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3316 | |
| 3317 | int i; |
| 3318 | for (i=0; i<files.length; i++) { |
| 3319 | File file = new File(dir, files[i]); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 3320 | if (!isPackageFilename(files[i])) { |
| 3321 | // Ignore entries which are not apk's |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 3322 | continue; |
| 3323 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3324 | PackageParser.Package pkg = scanPackageLI(file, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3325 | flags|PackageParser.PARSE_MUST_BE_APK, scanMode, currentTime, null); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3326 | // Don't mess around with apps in system partition. |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 3327 | if (pkg == null && (flags & PackageParser.PARSE_IS_SYSTEM) == 0 && |
| 3328 | mLastScanError == PackageManager.INSTALL_FAILED_INVALID_APK) { |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3329 | // Delete the apk |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3330 | Slog.w(TAG, "Cleaning up failed install of " + file); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3331 | file.delete(); |
| 3332 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3333 | } |
| 3334 | } |
| 3335 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3336 | private static File getSettingsProblemFile() { |
| 3337 | File dataDir = Environment.getDataDirectory(); |
| 3338 | File systemDir = new File(dataDir, "system"); |
| 3339 | File fname = new File(systemDir, "uiderrors.txt"); |
| 3340 | return fname; |
| 3341 | } |
| 3342 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 3343 | static void reportSettingsProblem(int priority, String msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3344 | try { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3345 | File fname = getSettingsProblemFile(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3346 | FileOutputStream out = new FileOutputStream(fname, true); |
| 3347 | PrintWriter pw = new PrintWriter(out); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3348 | SimpleDateFormat formatter = new SimpleDateFormat(); |
| 3349 | String dateString = formatter.format(new Date(System.currentTimeMillis())); |
| 3350 | pw.println(dateString + ": " + msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3351 | pw.close(); |
| 3352 | FileUtils.setPermissions( |
| 3353 | fname.toString(), |
| 3354 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IROTH, |
| 3355 | -1, -1); |
| 3356 | } catch (java.io.IOException e) { |
| 3357 | } |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3358 | Slog.println(priority, TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | private boolean collectCertificatesLI(PackageParser pp, PackageSetting ps, |
| 3362 | PackageParser.Package pkg, File srcFile, int parseFlags) { |
| 3363 | if (GET_CERTIFICATES) { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3364 | if (ps != null |
| 3365 | && ps.codePath.equals(srcFile) |
Kenny Root | 7d794fb | 2010-09-13 16:29:49 -0700 | [diff] [blame] | 3366 | && ps.timeStamp == srcFile.lastModified()) { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3367 | if (ps.signatures.mSignatures != null |
| 3368 | && ps.signatures.mSignatures.length != 0) { |
| 3369 | // Optimization: reuse the existing cached certificates |
| 3370 | // if the package appears to be unchanged. |
| 3371 | pkg.mSignatures = ps.signatures.mSignatures; |
| 3372 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3373 | } |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3374 | |
| 3375 | 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] | 3376 | } else { |
Jeff Brown | e760072 | 2010-04-07 18:28:23 -0700 | [diff] [blame] | 3377 | Log.i(TAG, srcFile.toString() + " changed; collecting certs"); |
| 3378 | } |
| 3379 | |
| 3380 | if (!pp.collectCertificates(pkg, parseFlags)) { |
| 3381 | mLastScanError = pp.getParseError(); |
| 3382 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3383 | } |
| 3384 | } |
| 3385 | return true; |
| 3386 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3387 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3388 | /* |
| 3389 | * Scan a package and return the newly parsed package. |
| 3390 | * Returns null in case of errors and the error code is stored in mLastScanError |
| 3391 | */ |
| 3392 | private PackageParser.Package scanPackageLI(File scanFile, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3393 | int parseFlags, int scanMode, long currentTime, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3394 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3395 | String scanPath = scanFile.getPath(); |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3396 | if (DEBUG_INSTALL) Slog.d(TAG, "Parsing: " + scanPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3397 | parseFlags |= mDefParseFlags; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3398 | PackageParser pp = new PackageParser(scanPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3399 | pp.setSeparateProcesses(mSeparateProcesses); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 3400 | pp.setOnlyCoreApps(mOnlyCore); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3401 | final PackageParser.Package pkg = pp.parsePackage(scanFile, |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3402 | scanPath, mMetrics, parseFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3403 | if (pkg == null) { |
| 3404 | mLastScanError = pp.getParseError(); |
| 3405 | return null; |
| 3406 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3407 | PackageSetting ps = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3408 | PackageSetting updatedPkg; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3409 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3410 | synchronized (mPackages) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3411 | // Look to see if we already know about this package. |
| 3412 | String oldName = mSettings.mRenamedPackages.get(pkg.packageName); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 3413 | if (pkg.mOriginalPackages != null && pkg.mOriginalPackages.contains(oldName)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3414 | // This package has been renamed to its original name. Let's |
| 3415 | // use that. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3416 | ps = mSettings.peekPackageLPr(oldName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3417 | } |
| 3418 | // If there was no original package, see one for the real package name. |
| 3419 | if (ps == null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3420 | ps = mSettings.peekPackageLPr(pkg.packageName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3421 | } |
| 3422 | // Check to see if this package could be hiding/updating a system |
| 3423 | // package. Must look for it either under the original or real |
| 3424 | // package name depending on our state. |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 3425 | updatedPkg = mSettings.getDisabledSystemPkgLPr(ps != null ? ps.name : pkg.packageName); |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3426 | if (DEBUG_INSTALL && updatedPkg != null) Slog.d(TAG, "updatedPkg = " + updatedPkg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3427 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3428 | // First check if this is a system package that may involve an update |
| 3429 | if (updatedPkg != null && (parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
Kenny Root | 9ee9274 | 2010-09-01 13:40:57 -0700 | [diff] [blame] | 3430 | if (ps != null && !ps.codePath.equals(scanFile)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3431 | // The path has changed from what was last scanned... check the |
| 3432 | // version of the new path against what we have stored to determine |
| 3433 | // what to do. |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3434 | if (DEBUG_INSTALL) Slog.d(TAG, "Path changing from " + ps.codePath); |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3435 | if (pkg.mVersionCode < ps.versionCode) { |
| 3436 | // 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] | 3437 | // Ignore entry. Skip it. |
| 3438 | Log.i(TAG, "Package " + ps.name + " at " + scanFile |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 3439 | + " ignored: updated version " + ps.versionCode |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3440 | + " better than this " + pkg.mVersionCode); |
Dianne Hackborn | c4d27a7 | 2013-01-22 18:30:17 -0800 | [diff] [blame] | 3441 | if (!updatedPkg.codePath.equals(scanFile)) { |
| 3442 | Slog.w(PackageManagerService.TAG, "Code path for hidden system pkg : " |
| 3443 | + ps.name + " changing from " + updatedPkg.codePathString |
| 3444 | + " to " + scanFile); |
| 3445 | updatedPkg.codePath = scanFile; |
| 3446 | updatedPkg.codePathString = scanFile.toString(); |
| 3447 | } |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3448 | updatedPkg.pkg = pkg; |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3449 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3450 | return null; |
| 3451 | } else { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3452 | // The current app on the system partion is better than |
| 3453 | // what we have updated to on the data partition; switch |
| 3454 | // back to the system partition version. |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3455 | // At this point, its safely assumed that package installation for |
| 3456 | // apps in system partition will go through. If not there won't be a working |
| 3457 | // version of the app |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3458 | // writer |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 3459 | synchronized (mPackages) { |
| 3460 | // Just remove the loaded entries from package lists. |
| 3461 | mPackages.remove(ps.name); |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 3462 | } |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3463 | Slog.w(TAG, "Package " + ps.name + " at " + scanFile |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3464 | + "reverting from " + ps.codePathString |
| 3465 | + ": new version " + pkg.mVersionCode |
| 3466 | + " better than installed " + ps.versionCode); |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 3467 | |
| 3468 | InstallArgs args = createInstallArgs(packageFlagsToInstallFlags(ps), |
| 3469 | ps.codePathString, ps.resourcePathString, ps.nativeLibraryPathString); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 3470 | synchronized (mInstallLock) { |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3471 | args.cleanUpResourcesLI(); |
| 3472 | } |
| 3473 | synchronized (mPackages) { |
| 3474 | mSettings.enableSystemPackageLPw(ps.name); |
| 3475 | } |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 3476 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3477 | } |
| 3478 | } |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3479 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3480 | if (updatedPkg != null) { |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3481 | // An updated system app will not have the PARSE_IS_SYSTEM flag set |
| 3482 | // initially |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3483 | parseFlags |= PackageParser.PARSE_IS_SYSTEM; |
| 3484 | } |
| 3485 | // Verify certificates against what was last scanned |
| 3486 | if (!collectCertificatesLI(pp, ps, pkg, scanFile, parseFlags)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3487 | Slog.w(TAG, "Failed verifying certificates for package:" + pkg.packageName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3488 | return null; |
| 3489 | } |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3490 | |
| 3491 | /* |
| 3492 | * A new system app appeared, but we already had a non-system one of the |
| 3493 | * same name installed earlier. |
| 3494 | */ |
| 3495 | boolean shouldHideSystemApp = false; |
| 3496 | if (updatedPkg == null && ps != null |
| 3497 | && (parseFlags & PackageParser.PARSE_IS_SYSTEM_DIR) != 0 && !isSystemApp(ps)) { |
| 3498 | /* |
| 3499 | * Check to make sure the signatures match first. If they don't, |
| 3500 | * wipe the installed application and its data. |
| 3501 | */ |
| 3502 | if (compareSignatures(ps.signatures.mSignatures, pkg.mSignatures) |
| 3503 | != PackageManager.SIGNATURE_MATCH) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3504 | if (DEBUG_INSTALL) Slog.d(TAG, "Signature mismatch!"); |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 3505 | deletePackageLI(pkg.packageName, null, true, null, null, 0, null, false); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3506 | ps = null; |
| 3507 | } else { |
| 3508 | /* |
| 3509 | * If the newly-added system app is an older version than the |
| 3510 | * already installed version, hide it. It will be scanned later |
| 3511 | * and re-added like an update. |
| 3512 | */ |
| 3513 | if (pkg.mVersionCode < ps.versionCode) { |
| 3514 | shouldHideSystemApp = true; |
| 3515 | } else { |
| 3516 | /* |
| 3517 | * The newly found system app is a newer version that the |
| 3518 | * one previously installed. Simply remove the |
| 3519 | * already-installed application and replace it with our own |
| 3520 | * while keeping the application data. |
| 3521 | */ |
| 3522 | Slog.w(TAG, "Package " + ps.name + " at " + scanFile + "reverting from " |
| 3523 | + ps.codePathString + ": new version " + pkg.mVersionCode |
| 3524 | + " better than installed " + ps.versionCode); |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 3525 | InstallArgs args = createInstallArgs(packageFlagsToInstallFlags(ps), |
| 3526 | ps.codePathString, ps.resourcePathString, ps.nativeLibraryPathString); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 3527 | synchronized (mInstallLock) { |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3528 | args.cleanUpResourcesLI(); |
| 3529 | } |
| 3530 | } |
| 3531 | } |
| 3532 | } |
| 3533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3534 | // The apk is forward locked (not public) if its code and resources |
Masanori Ogino | 2d91007 | 2012-05-07 16:44:48 +0900 | [diff] [blame] | 3535 | // are kept in different files. (except for app in either system or |
| 3536 | // vendor path). |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3537 | // TODO grab this value from PackageSettings |
Masanori Ogino | 2d91007 | 2012-05-07 16:44:48 +0900 | [diff] [blame] | 3538 | if ((parseFlags & PackageParser.PARSE_IS_SYSTEM_DIR) == 0) { |
| 3539 | if (ps != null && !ps.codePath.equals(ps.resourcePath)) { |
| 3540 | parseFlags |= PackageParser.PARSE_FORWARD_LOCK; |
| 3541 | } |
Suchi Amalapurapu | f2c1072 | 2009-07-29 17:19:39 -0700 | [diff] [blame] | 3542 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3543 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3544 | String codePath = null; |
| 3545 | String resPath = null; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3546 | if ((parseFlags & PackageParser.PARSE_FORWARD_LOCK) != 0) { |
| 3547 | if (ps != null && ps.resourcePathString != null) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3548 | resPath = ps.resourcePathString; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3549 | } else { |
| 3550 | // Should not happen at all. Just log an error. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3551 | Slog.e(TAG, "Resource path not set for pkg : " + pkg.packageName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3552 | } |
| 3553 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3554 | resPath = pkg.mScanPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 3555 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3556 | codePath = pkg.mScanPath; |
| 3557 | // Set application objects path explicitly. |
| 3558 | setApplicationInfoPaths(pkg, codePath, resPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3559 | // 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] | 3560 | PackageParser.Package scannedPkg = scanPackageLI(pkg, parseFlags, scanMode |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3561 | | SCAN_UPDATE_SIGNATURE, currentTime, user); |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 3562 | |
| 3563 | /* |
| 3564 | * If the system app should be overridden by a previously installed |
| 3565 | * data, hide the system app now and let the /data/app scan pick it up |
| 3566 | * again. |
| 3567 | */ |
| 3568 | if (shouldHideSystemApp) { |
| 3569 | synchronized (mPackages) { |
| 3570 | /* |
| 3571 | * We have to grant systems permissions before we hide, because |
| 3572 | * grantPermissions will assume the package update is trying to |
| 3573 | * expand its permissions. |
| 3574 | */ |
| 3575 | grantPermissionsLPw(pkg, true); |
| 3576 | mSettings.disableSystemPackageLPw(pkg.packageName); |
| 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | return scannedPkg; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3581 | } |
| 3582 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 3583 | private static void setApplicationInfoPaths(PackageParser.Package pkg, String destCodePath, |
| 3584 | String destResPath) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3585 | pkg.mPath = pkg.mScanPath = destCodePath; |
| 3586 | pkg.applicationInfo.sourceDir = destCodePath; |
| 3587 | pkg.applicationInfo.publicSourceDir = destResPath; |
| 3588 | } |
| 3589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3590 | private static String fixProcessName(String defProcessName, |
| 3591 | String processName, int uid) { |
| 3592 | if (processName == null) { |
| 3593 | return defProcessName; |
| 3594 | } |
| 3595 | return processName; |
| 3596 | } |
| 3597 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3598 | private boolean verifySignaturesLP(PackageSetting pkgSetting, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3599 | PackageParser.Package pkg) { |
| 3600 | if (pkgSetting.signatures.mSignatures != null) { |
| 3601 | // Already existing package. Make sure signatures match |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3602 | if (compareSignatures(pkgSetting.signatures.mSignatures, pkg.mSignatures) != |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3603 | PackageManager.SIGNATURE_MATCH) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3604 | Slog.e(TAG, "Package " + pkg.packageName |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3605 | + " signatures do not match the previously installed version; ignoring!"); |
| 3606 | mLastScanError = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3607 | return false; |
| 3608 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3609 | } |
| 3610 | // Check for shared user signatures |
| 3611 | if (pkgSetting.sharedUser != null && pkgSetting.sharedUser.signatures.mSignatures != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3612 | if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 3613 | pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) { |
| 3614 | Slog.e(TAG, "Package " + pkg.packageName |
| 3615 | + " has no signatures that match those in shared user " |
| 3616 | + pkgSetting.sharedUser.name + "; ignoring!"); |
| 3617 | mLastScanError = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE; |
| 3618 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3619 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3620 | } |
| 3621 | return true; |
| 3622 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3623 | |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 3624 | /** |
| 3625 | * Enforces that only the system UID or root's UID can call a method exposed |
| 3626 | * via Binder. |
| 3627 | * |
| 3628 | * @param message used as message if SecurityException is thrown |
| 3629 | * @throws SecurityException if the caller is not system or root |
| 3630 | */ |
| 3631 | private static final void enforceSystemOrRoot(String message) { |
| 3632 | final int uid = Binder.getCallingUid(); |
| 3633 | if (uid != Process.SYSTEM_UID && uid != 0) { |
| 3634 | throw new SecurityException(message); |
| 3635 | } |
| 3636 | } |
| 3637 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3638 | public void performBootDexOpt() { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3639 | HashSet<PackageParser.Package> pkgs = null; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3640 | synchronized (mPackages) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3641 | pkgs = mDeferredDexOpt; |
| 3642 | mDeferredDexOpt = null; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3643 | } |
| 3644 | if (pkgs != null) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3645 | int i = 0; |
| 3646 | for (PackageParser.Package pkg : pkgs) { |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 3647 | if (!isFirstBoot()) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3648 | i++; |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 3649 | try { |
| 3650 | ActivityManagerNative.getDefault().showBootMessage( |
| 3651 | mContext.getResources().getString( |
| 3652 | com.android.internal.R.string.android_upgrading_apk, |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3653 | i, pkgs.size()), true); |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 3654 | } catch (RemoteException e) { |
| 3655 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3656 | } |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3657 | PackageParser.Package p = pkg; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3658 | synchronized (mInstallLock) { |
| 3659 | if (!p.mDidDexOpt) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3660 | performDexOptLI(p, false, false, true); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3661 | } |
| 3662 | } |
| 3663 | } |
| 3664 | } |
| 3665 | } |
| 3666 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3667 | public boolean performDexOpt(String packageName) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 3668 | enforceSystemOrRoot("Only the system can request dexopt be performed"); |
| 3669 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3670 | if (!mNoDexOpt) { |
| 3671 | return false; |
| 3672 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3673 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3674 | PackageParser.Package p; |
| 3675 | synchronized (mPackages) { |
| 3676 | p = mPackages.get(packageName); |
| 3677 | if (p == null || p.mDidDexOpt) { |
| 3678 | return false; |
| 3679 | } |
| 3680 | } |
| 3681 | synchronized (mInstallLock) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3682 | return performDexOptLI(p, false, false, true) == DEX_OPT_PERFORMED; |
| 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | private void performDexOptLibsLI(ArrayList<String> libs, boolean forceDex, boolean defer, |
| 3687 | HashSet<String> done) { |
| 3688 | for (int i=0; i<libs.size(); i++) { |
| 3689 | PackageParser.Package libPkg; |
| 3690 | String libName; |
| 3691 | synchronized (mPackages) { |
| 3692 | libName = libs.get(i); |
| 3693 | SharedLibraryEntry lib = mSharedLibraries.get(libName); |
| 3694 | if (lib != null && lib.apk != null) { |
| 3695 | libPkg = mPackages.get(lib.apk); |
| 3696 | } else { |
| 3697 | libPkg = null; |
| 3698 | } |
| 3699 | } |
| 3700 | if (libPkg != null && !done.contains(libName)) { |
| 3701 | performDexOptLI(libPkg, forceDex, defer, done); |
| 3702 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3703 | } |
| 3704 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3705 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3706 | static final int DEX_OPT_SKIPPED = 0; |
| 3707 | static final int DEX_OPT_PERFORMED = 1; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3708 | static final int DEX_OPT_DEFERRED = 2; |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3709 | static final int DEX_OPT_FAILED = -1; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3710 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3711 | private int performDexOptLI(PackageParser.Package pkg, boolean forceDex, boolean defer, |
| 3712 | HashSet<String> done) { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3713 | boolean performed = false; |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3714 | if (done != null) { |
| 3715 | done.add(pkg.packageName); |
| 3716 | if (pkg.usesLibraries != null) { |
| 3717 | performDexOptLibsLI(pkg.usesLibraries, forceDex, defer, done); |
| 3718 | } |
| 3719 | if (pkg.usesOptionalLibraries != null) { |
| 3720 | performDexOptLibsLI(pkg.usesOptionalLibraries, forceDex, defer, done); |
| 3721 | } |
| 3722 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 3723 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3724 | String path = pkg.mScanPath; |
| 3725 | int ret = 0; |
| 3726 | try { |
| 3727 | if (forceDex || dalvik.system.DexFile.isDexOptNeeded(path)) { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3728 | if (!forceDex && defer) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3729 | if (mDeferredDexOpt == null) { |
| 3730 | mDeferredDexOpt = new HashSet<PackageParser.Package>(); |
| 3731 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3732 | mDeferredDexOpt.add(pkg); |
| 3733 | return DEX_OPT_DEFERRED; |
| 3734 | } else { |
| 3735 | Log.i(TAG, "Running dexopt on: " + pkg.applicationInfo.packageName); |
Kenny Root | 6a9b2cb | 2012-09-17 14:02:39 -0700 | [diff] [blame] | 3736 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
| 3737 | ret = mInstaller.dexopt(path, sharedGid, !isForwardLocked(pkg)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3738 | pkg.mDidDexOpt = true; |
| 3739 | performed = true; |
| 3740 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3741 | } |
| 3742 | } catch (FileNotFoundException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3743 | Slog.w(TAG, "Apk not found for dexopt: " + path); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3744 | ret = -1; |
| 3745 | } catch (IOException e) { |
Suchi Amalapurapu | 73dafa1 | 2010-04-01 16:31:31 -0700 | [diff] [blame] | 3746 | Slog.w(TAG, "IOException reading apk: " + path, e); |
| 3747 | ret = -1; |
| 3748 | } catch (dalvik.system.StaleDexCacheError e) { |
| 3749 | Slog.w(TAG, "StaleDexCacheError when reading apk: " + path, e); |
| 3750 | ret = -1; |
| 3751 | } catch (Exception e) { |
| 3752 | Slog.w(TAG, "Exception when doing dexopt : ", e); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3753 | ret = -1; |
| 3754 | } |
| 3755 | if (ret < 0) { |
| 3756 | //error from installer |
| 3757 | return DEX_OPT_FAILED; |
| 3758 | } |
| 3759 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3760 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3761 | return performed ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 3762 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 3763 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3764 | private int performDexOptLI(PackageParser.Package pkg, boolean forceDex, boolean defer, |
| 3765 | boolean inclDependencies) { |
| 3766 | HashSet<String> done; |
| 3767 | boolean performed = false; |
| 3768 | if (inclDependencies && (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null)) { |
| 3769 | done = new HashSet<String>(); |
| 3770 | done.add(pkg.packageName); |
| 3771 | } else { |
| 3772 | done = null; |
| 3773 | } |
| 3774 | return performDexOptLI(pkg, forceDex, defer, done); |
| 3775 | } |
| 3776 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 3777 | private boolean verifyPackageUpdateLPr(PackageSetting oldPkg, PackageParser.Package newPkg) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3778 | if ((oldPkg.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3779 | Slog.w(TAG, "Unable to update from " + oldPkg.name |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3780 | + " to " + newPkg.packageName |
| 3781 | + ": old package not in system partition"); |
| 3782 | return false; |
| 3783 | } else if (mPackages.get(oldPkg.name) != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3784 | Slog.w(TAG, "Unable to update from " + oldPkg.name |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 3785 | + " to " + newPkg.packageName |
| 3786 | + ": old package still exists"); |
| 3787 | return false; |
| 3788 | } |
| 3789 | return true; |
| 3790 | } |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 3791 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3792 | File getDataPathForUser(int userId) { |
| 3793 | return new File(mUserAppDataDir.getAbsolutePath() + File.separator + userId); |
Suchi Amalapurapu | d32c802 | 2010-03-04 17:18:14 -0800 | [diff] [blame] | 3794 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3795 | |
| 3796 | private File getDataPathForPackage(String packageName, int userId) { |
Kenny Root | 9c8625e | 2011-07-25 09:42:22 -0700 | [diff] [blame] | 3797 | /* |
| 3798 | * Until we fully support multiple users, return the directory we |
| 3799 | * previously would have. The PackageManagerTests will need to be |
| 3800 | * revised when this is changed back.. |
| 3801 | */ |
| 3802 | if (userId == 0) { |
| 3803 | return new File(mAppDataDir, packageName); |
| 3804 | } else { |
| 3805 | return new File(mUserAppDataDir.getAbsolutePath() + File.separator + userId |
Amith Yamasani | 0f3f29b | 2011-05-09 14:17:14 -0700 | [diff] [blame] | 3806 | + File.separator + packageName); |
Kenny Root | 9c8625e | 2011-07-25 09:42:22 -0700 | [diff] [blame] | 3807 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3808 | } |
| 3809 | |
Robert Craig | 0f40dc9 | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 3810 | private int createDataDirsLI(String packageName, int uid, String seinfo) { |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 3811 | int[] users = sUserManager.getUserIds(); |
Robert Craig | 0f40dc9 | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 3812 | int res = mInstaller.install(packageName, uid, uid, seinfo); |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 3813 | if (res < 0) { |
| 3814 | return res; |
| 3815 | } |
| 3816 | for (int user : users) { |
| 3817 | if (user != 0) { |
| 3818 | res = mInstaller.createUserData(packageName, |
Nick Kralevich | 1506921 | 2013-01-09 15:54:56 -0800 | [diff] [blame] | 3819 | UserHandle.getUid(user, uid), user); |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 3820 | if (res < 0) { |
| 3821 | return res; |
| 3822 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 3823 | } |
| 3824 | } |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 3825 | return res; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | private int removeDataDirsLI(String packageName) { |
| 3829 | int[] users = sUserManager.getUserIds(); |
| 3830 | int res = 0; |
| 3831 | for (int user : users) { |
| 3832 | int resInner = mInstaller.remove(packageName, user); |
| 3833 | if (resInner < 0) { |
| 3834 | res = resInner; |
| 3835 | } |
| 3836 | } |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 3837 | |
| 3838 | final File nativeLibraryFile = new File(mAppLibInstallDir, packageName); |
| 3839 | NativeLibraryHelper.removeNativeBinariesFromDirLI(nativeLibraryFile); |
| 3840 | if (!nativeLibraryFile.delete()) { |
| 3841 | Slog.w(TAG, "Couldn't delete native library directory " + nativeLibraryFile.getPath()); |
| 3842 | } |
| 3843 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 3844 | return res; |
| 3845 | } |
| 3846 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 3847 | private int addSharedLibraryLPw(final SharedLibraryEntry file, int num, |
| 3848 | PackageParser.Package changingLib) { |
| 3849 | if (file.path != null) { |
| 3850 | mTmpSharedLibraries[num] = file.path; |
| 3851 | return num+1; |
| 3852 | } |
| 3853 | PackageParser.Package p = mPackages.get(file.apk); |
| 3854 | if (changingLib != null && changingLib.packageName.equals(file.apk)) { |
| 3855 | // If we are doing this while in the middle of updating a library apk, |
| 3856 | // then we need to make sure to use that new apk for determining the |
| 3857 | // dependencies here. (We haven't yet finished committing the new apk |
| 3858 | // to the package manager state.) |
| 3859 | if (p == null || p.packageName.equals(changingLib.packageName)) { |
| 3860 | p = changingLib; |
| 3861 | } |
| 3862 | } |
| 3863 | if (p != null) { |
| 3864 | String path = p.mPath; |
| 3865 | for (int i=0; i<num; i++) { |
| 3866 | if (mTmpSharedLibraries[i].equals(path)) { |
| 3867 | return num; |
| 3868 | } |
| 3869 | } |
| 3870 | mTmpSharedLibraries[num] = p.mPath; |
| 3871 | return num+1; |
| 3872 | } |
| 3873 | return num; |
| 3874 | } |
| 3875 | |
| 3876 | private boolean updateSharedLibrariesLPw(PackageParser.Package pkg, |
| 3877 | PackageParser.Package changingLib) { |
| 3878 | if (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null) { |
| 3879 | if (mTmpSharedLibraries == null || |
| 3880 | mTmpSharedLibraries.length < mSharedLibraries.size()) { |
| 3881 | mTmpSharedLibraries = new String[mSharedLibraries.size()]; |
| 3882 | } |
| 3883 | int num = 0; |
| 3884 | int N = pkg.usesLibraries != null ? pkg.usesLibraries.size() : 0; |
| 3885 | for (int i=0; i<N; i++) { |
| 3886 | final SharedLibraryEntry file = mSharedLibraries.get(pkg.usesLibraries.get(i)); |
| 3887 | if (file == null) { |
| 3888 | Slog.e(TAG, "Package " + pkg.packageName |
| 3889 | + " requires unavailable shared library " |
| 3890 | + pkg.usesLibraries.get(i) + "; failing!"); |
| 3891 | mLastScanError = PackageManager.INSTALL_FAILED_MISSING_SHARED_LIBRARY; |
| 3892 | return false; |
| 3893 | } |
| 3894 | num = addSharedLibraryLPw(file, num, changingLib); |
| 3895 | } |
| 3896 | N = pkg.usesOptionalLibraries != null ? pkg.usesOptionalLibraries.size() : 0; |
| 3897 | for (int i=0; i<N; i++) { |
| 3898 | final SharedLibraryEntry file = mSharedLibraries.get(pkg.usesOptionalLibraries.get(i)); |
| 3899 | if (file == null) { |
| 3900 | Slog.w(TAG, "Package " + pkg.packageName |
| 3901 | + " desires unavailable shared library " |
| 3902 | + pkg.usesOptionalLibraries.get(i) + "; ignoring!"); |
| 3903 | } else { |
| 3904 | num = addSharedLibraryLPw(file, num, changingLib); |
| 3905 | } |
| 3906 | } |
| 3907 | if (num > 0) { |
| 3908 | pkg.usesLibraryFiles = new String[num]; |
| 3909 | System.arraycopy(mTmpSharedLibraries, 0, |
| 3910 | pkg.usesLibraryFiles, 0, num); |
| 3911 | } else { |
| 3912 | pkg.usesLibraryFiles = null; |
| 3913 | } |
| 3914 | } |
| 3915 | return true; |
| 3916 | } |
| 3917 | |
| 3918 | private static boolean hasString(List<String> list, List<String> which) { |
| 3919 | if (list == null) { |
| 3920 | return false; |
| 3921 | } |
| 3922 | for (int i=list.size()-1; i>=0; i--) { |
| 3923 | for (int j=which.size()-1; j>=0; j--) { |
| 3924 | if (which.get(j).equals(list.get(i))) { |
| 3925 | return true; |
| 3926 | } |
| 3927 | } |
| 3928 | } |
| 3929 | return false; |
| 3930 | } |
| 3931 | |
| 3932 | private void updateAllSharedLibrariesLPw() { |
| 3933 | for (PackageParser.Package pkg : mPackages.values()) { |
| 3934 | updateSharedLibrariesLPw(pkg, null); |
| 3935 | } |
| 3936 | } |
| 3937 | |
| 3938 | private ArrayList<PackageParser.Package> updateAllSharedLibrariesLPw( |
| 3939 | PackageParser.Package changingPkg) { |
| 3940 | ArrayList<PackageParser.Package> res = null; |
| 3941 | for (PackageParser.Package pkg : mPackages.values()) { |
| 3942 | if (hasString(pkg.usesLibraries, changingPkg.libraryNames) |
| 3943 | || hasString(pkg.usesOptionalLibraries, changingPkg.libraryNames)) { |
| 3944 | if (res == null) { |
| 3945 | res = new ArrayList<PackageParser.Package>(); |
| 3946 | } |
| 3947 | res.add(pkg); |
| 3948 | updateSharedLibrariesLPw(pkg, changingPkg); |
| 3949 | } |
| 3950 | } |
| 3951 | return res; |
| 3952 | } |
| 3953 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3954 | private PackageParser.Package scanPackageLI(PackageParser.Package pkg, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 3955 | int parseFlags, int scanMode, long currentTime, UserHandle user) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 3956 | File scanFile = new File(pkg.mScanPath); |
Suchi Amalapurapu | 7040ce7 | 2010-02-08 23:55:56 -0800 | [diff] [blame] | 3957 | if (scanFile == null || pkg.applicationInfo.sourceDir == null || |
| 3958 | pkg.applicationInfo.publicSourceDir == null) { |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3959 | // Bail out. The resource and code paths haven't been set. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3960 | Slog.w(TAG, " Code and resource paths haven't been set correctly"); |
Suchi Amalapurapu | 08be55b | 2010-02-08 16:30:06 -0800 | [diff] [blame] | 3961 | mLastScanError = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 3962 | return null; |
| 3963 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3964 | mScanningPath = scanFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3965 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3966 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0) { |
| 3967 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 3968 | } |
| 3969 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 3970 | if (pkg.packageName.equals("android")) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3971 | synchronized (mPackages) { |
| 3972 | if (mAndroidApplication != null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 3973 | Slog.w(TAG, "*************************************************"); |
| 3974 | Slog.w(TAG, "Core android package being redefined. Skipping."); |
| 3975 | Slog.w(TAG, " file=" + mScanningPath); |
| 3976 | Slog.w(TAG, "*************************************************"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3977 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 3978 | return null; |
| 3979 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3980 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3981 | // Set up information for our fall-back user intent resolution |
| 3982 | // activity. |
| 3983 | mPlatformPackage = pkg; |
| 3984 | pkg.mVersionCode = mSdkVersion; |
| 3985 | mAndroidApplication = pkg.applicationInfo; |
| 3986 | mResolveActivity.applicationInfo = mAndroidApplication; |
| 3987 | mResolveActivity.name = ResolverActivity.class.getName(); |
| 3988 | mResolveActivity.packageName = mAndroidApplication.packageName; |
Dianne Hackborn | 8da429e | 2012-09-23 12:52:19 -0700 | [diff] [blame] | 3989 | mResolveActivity.processName = "system:ui"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3990 | mResolveActivity.launchMode = ActivityInfo.LAUNCH_MULTIPLE; |
| 3991 | mResolveActivity.flags = ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS; |
Joe Onorato | 646f46e | 2010-11-08 15:08:56 -0800 | [diff] [blame] | 3992 | 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] | 3993 | mResolveActivity.exported = true; |
| 3994 | mResolveActivity.enabled = true; |
| 3995 | mResolveInfo.activityInfo = mResolveActivity; |
| 3996 | mResolveInfo.priority = 0; |
| 3997 | mResolveInfo.preferredOrder = 0; |
| 3998 | mResolveInfo.match = 0; |
| 3999 | mResolveComponentName = new ComponentName( |
| 4000 | mAndroidApplication.packageName, mResolveActivity.name); |
| 4001 | } |
| 4002 | } |
| 4003 | |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4004 | if (DEBUG_PACKAGE_SCANNING) { |
| 4005 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 4006 | Log.d(TAG, "Scanning package " + pkg.packageName); |
| 4007 | } |
| 4008 | |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4009 | if (mPackages.containsKey(pkg.packageName) |
| 4010 | || mSharedLibraries.containsKey(pkg.packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4011 | Slog.w(TAG, "Application package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4012 | + " already installed. Skipping duplicate."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4013 | mLastScanError = PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE; |
| 4014 | return null; |
| 4015 | } |
| 4016 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 4017 | // Initialize package source and resource directories |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4018 | File destCodeFile = new File(pkg.applicationInfo.sourceDir); |
| 4019 | File destResourceFile = new File(pkg.applicationInfo.publicSourceDir); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 4020 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4021 | SharedUserSetting suid = null; |
| 4022 | PackageSetting pkgSetting = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4023 | |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 4024 | if (!isSystemApp(pkg)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4025 | // Only system apps can use these features. |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 4026 | pkg.mOriginalPackages = null; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4027 | pkg.mRealPackage = null; |
| 4028 | pkg.mAdoptPermissions = null; |
| 4029 | } |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 4030 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4031 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4032 | synchronized (mPackages) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 4033 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) == 0) { |
| 4034 | // Check all shared libraries and map to their actual file path. |
| 4035 | // We only do this here for apps not on a system dir, because those |
| 4036 | // are the only ones that can fail an install due to this. We |
| 4037 | // will take care of the system apps by updating all of their |
| 4038 | // library paths after the scan is done. |
| 4039 | if (!updateSharedLibrariesLPw(pkg, null)) { |
| 4040 | return null; |
Dianne Hackborn | 4923734 | 2009-08-27 20:08:01 -0700 | [diff] [blame] | 4041 | } |
Kenny Root | 1683afa | 2011-01-07 14:27:50 -0800 | [diff] [blame] | 4042 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4043 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4044 | if (pkg.mSharedUserId != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4045 | suid = mSettings.getSharedUserLPw(pkg.mSharedUserId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4046 | pkg.applicationInfo.flags, true); |
| 4047 | if (suid == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4048 | Slog.w(TAG, "Creating application package " + pkg.packageName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4049 | + " for shared user failed"); |
| 4050 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 4051 | return null; |
| 4052 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4053 | if (DEBUG_PACKAGE_SCANNING) { |
| 4054 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 4055 | Log.d(TAG, "Shared UserID " + pkg.mSharedUserId + " (uid=" + suid.userId |
| 4056 | + "): packages=" + suid.packages); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4057 | } |
| 4058 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4059 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 4060 | // Check if we are renaming from an original package name. |
| 4061 | PackageSetting origPackage = null; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4062 | String realName = null; |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 4063 | if (pkg.mOriginalPackages != null) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4064 | // This package may need to be renamed to a previously |
| 4065 | // installed name. Let's check on that... |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4066 | final String renamed = mSettings.mRenamedPackages.get(pkg.mRealPackage); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 4067 | if (pkg.mOriginalPackages.contains(renamed)) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4068 | // This package had originally been installed as the |
| 4069 | // original name, and we have already taken care of |
| 4070 | // transitioning to the new one. Just update the new |
| 4071 | // one to continue using the old name. |
| 4072 | realName = pkg.mRealPackage; |
| 4073 | if (!pkg.packageName.equals(renamed)) { |
| 4074 | // Callers into this function may have already taken |
| 4075 | // care of renaming the package; only do it here if |
| 4076 | // it is not already done. |
| 4077 | pkg.setPackageName(renamed); |
| 4078 | } |
| 4079 | |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 4080 | } else { |
| 4081 | for (int i=pkg.mOriginalPackages.size()-1; i>=0; i--) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4082 | if ((origPackage = mSettings.peekPackageLPr( |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 4083 | pkg.mOriginalPackages.get(i))) != null) { |
| 4084 | // We do have the package already installed under its |
| 4085 | // original name... should we use it? |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4086 | if (!verifyPackageUpdateLPr(origPackage, pkg)) { |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 4087 | // New package is not compatible with original. |
| 4088 | origPackage = null; |
| 4089 | continue; |
| 4090 | } else if (origPackage.sharedUser != null) { |
| 4091 | // Make sure uid is compatible between packages. |
| 4092 | if (!origPackage.sharedUser.name.equals(pkg.mSharedUserId)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4093 | Slog.w(TAG, "Unable to migrate data from " + origPackage.name |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 4094 | + " to " + pkg.packageName + ": old uid " |
| 4095 | + origPackage.sharedUser.name |
| 4096 | + " differs from " + pkg.mSharedUserId); |
| 4097 | origPackage = null; |
| 4098 | continue; |
| 4099 | } |
| 4100 | } else { |
| 4101 | if (DEBUG_UPGRADE) Log.v(TAG, "Renaming new package " |
| 4102 | + pkg.packageName + " to old name " + origPackage.name); |
| 4103 | } |
| 4104 | break; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 4105 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 4106 | } |
| 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | if (mTransferedPackages.contains(pkg.packageName)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4111 | Slog.w(TAG, "Package " + pkg.packageName |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 4112 | + " was transferred to another, but its .apk remains"); |
| 4113 | } |
| 4114 | |
Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 4115 | // Just create the setting, don't add it yet. For already existing packages |
| 4116 | // the PkgSetting exists already and doesn't have to be created. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4117 | pkgSetting = mSettings.getPackageLPw(pkg, origPackage, realName, suid, destCodeFile, |
Kenny Root | 806cc13 | 2010-09-12 08:34:19 -0700 | [diff] [blame] | 4118 | destResourceFile, pkg.applicationInfo.nativeLibraryDir, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 4119 | pkg.applicationInfo.flags, user, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4120 | if (pkgSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4121 | Slog.w(TAG, "Creating application package " + pkg.packageName + " failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4122 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 4123 | return null; |
| 4124 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4125 | |
| 4126 | if (pkgSetting.origPackage != null) { |
| 4127 | // If we are first transitioning from an original package, |
| 4128 | // fix up the new package's name now. We need to do this after |
| 4129 | // looking up the package under its new name, so getPackageLP |
| 4130 | // can take care of fiddling things correctly. |
| 4131 | pkg.setPackageName(origPackage.name); |
| 4132 | |
| 4133 | // File a report about this. |
| 4134 | String msg = "New package " + pkgSetting.realName |
| 4135 | + " renamed to replace old package " + pkgSetting.name; |
| 4136 | reportSettingsProblem(Log.WARN, msg); |
| 4137 | |
| 4138 | // Make a note of it. |
| 4139 | mTransferedPackages.add(origPackage.name); |
| 4140 | |
| 4141 | // No longer need to retain this. |
| 4142 | pkgSetting.origPackage = null; |
| 4143 | } |
| 4144 | |
| 4145 | if (realName != null) { |
| 4146 | // Make a note of it. |
| 4147 | mTransferedPackages.add(pkg.packageName); |
| 4148 | } |
| 4149 | |
Kenny Root | c52d6fd | 2012-05-07 23:04:52 -0700 | [diff] [blame] | 4150 | if (mSettings.isDisabledSystemPackageLPr(pkg.packageName)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4151 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
| 4152 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4153 | |
Robert Craig | 0f40dc9 | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 4154 | if (mFoundPolicyFile) { |
| 4155 | SELinuxMMAC.assignSeinfoValue(pkg); |
| 4156 | } |
| 4157 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4158 | pkg.applicationInfo.uid = pkgSetting.appId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4159 | pkg.mExtras = pkgSetting; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4160 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 4161 | if (!verifySignaturesLP(pkgSetting, pkg)) { |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 4162 | if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4163 | return null; |
| 4164 | } |
| 4165 | // The signature has changed, but this package is in the system |
| 4166 | // image... let's recover! |
Suchi Amalapurapu | c4dd60f | 2009-03-24 21:10:53 -0700 | [diff] [blame] | 4167 | pkgSetting.signatures.mSignatures = pkg.mSignatures; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4168 | // However... if this package is part of a shared user, but it |
| 4169 | // doesn't match the signature of the shared user, let's fail. |
| 4170 | // What this means is that you can't change the signatures |
| 4171 | // associated with an overall shared user, which doesn't seem all |
| 4172 | // that unreasonable. |
| 4173 | if (pkgSetting.sharedUser != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4174 | if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures, |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 4175 | pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) { |
| 4176 | 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] | 4177 | mLastScanError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 4178 | return null; |
| 4179 | } |
| 4180 | } |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 4181 | // File a report about this. |
| 4182 | String msg = "System package " + pkg.packageName |
| 4183 | + " signature changed; retaining data."; |
| 4184 | reportSettingsProblem(Log.WARN, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4185 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4186 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4187 | // Verify that this new package doesn't have any content providers |
| 4188 | // that conflict with existing packages. Only do this if the |
| 4189 | // package isn't already installed, since we don't want to break |
| 4190 | // things that are installed. |
| 4191 | if ((scanMode&SCAN_NEW_INSTALL) != 0) { |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 4192 | final int N = pkg.providers.size(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4193 | int i; |
| 4194 | for (i=0; i<N; i++) { |
| 4195 | PackageParser.Provider p = pkg.providers.get(i); |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4196 | if (p.info.authority != null) { |
| 4197 | String names[] = p.info.authority.split(";"); |
| 4198 | for (int j = 0; j < names.length; j++) { |
| 4199 | if (mProviders.containsKey(names[j])) { |
| 4200 | PackageParser.Provider other = mProviders.get(names[j]); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4201 | Slog.w(TAG, "Can't install because provider name " + names[j] + |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4202 | " (in package " + pkg.applicationInfo.packageName + |
| 4203 | ") is already used by " |
| 4204 | + ((other != null && other.getComponentName() != null) |
| 4205 | ? other.getComponentName().getPackageName() : "?")); |
| 4206 | mLastScanError = PackageManager.INSTALL_FAILED_CONFLICTING_PROVIDER; |
| 4207 | return null; |
| 4208 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4209 | } |
| 4210 | } |
| 4211 | } |
| 4212 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4213 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4214 | if (pkg.mAdoptPermissions != null) { |
| 4215 | // This package wants to adopt ownership of permissions from |
| 4216 | // another package. |
| 4217 | for (int i = pkg.mAdoptPermissions.size() - 1; i >= 0; i--) { |
| 4218 | final String origName = pkg.mAdoptPermissions.get(i); |
| 4219 | final PackageSetting orig = mSettings.peekPackageLPr(origName); |
| 4220 | if (orig != null) { |
| 4221 | if (verifyPackageUpdateLPr(orig, pkg)) { |
| 4222 | Slog.i(TAG, "Adopting permissions from " + origName + " to " |
| 4223 | + pkg.packageName); |
| 4224 | mSettings.transferPermissionsLPw(origName, pkg.packageName); |
| 4225 | } |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | } |
| 4229 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4230 | |
| 4231 | final String pkgName = pkg.packageName; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4232 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 4233 | final long scanFileTime = scanFile.lastModified(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4234 | final boolean forceDex = (scanMode&SCAN_FORCE_DEX) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4235 | pkg.applicationInfo.processName = fixProcessName( |
| 4236 | pkg.applicationInfo.packageName, |
| 4237 | pkg.applicationInfo.processName, |
| 4238 | pkg.applicationInfo.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4239 | |
| 4240 | File dataPath; |
| 4241 | if (mPlatformPackage == pkg) { |
| 4242 | // The system package is special. |
| 4243 | dataPath = new File (Environment.getDataDirectory(), "system"); |
| 4244 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 4245 | } else { |
| 4246 | // This is a normal package, need to make its data directory. |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 4247 | dataPath = getDataPathForPackage(pkg.packageName, 0); |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 4248 | |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 4249 | boolean uidError = false; |
| 4250 | |
| 4251 | if (dataPath.exists()) { |
| 4252 | int currentUid = 0; |
| 4253 | try { |
| 4254 | StructStat stat = Libcore.os.stat(dataPath.getPath()); |
| 4255 | currentUid = stat.st_uid; |
| 4256 | } catch (ErrnoException e) { |
| 4257 | Slog.e(TAG, "Couldn't stat path " + dataPath.getPath(), e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4258 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 4259 | |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 4260 | // If we have mismatched owners for the data path, we have a problem. |
| 4261 | if (currentUid != pkg.applicationInfo.uid) { |
| 4262 | boolean recovered = false; |
| 4263 | if (currentUid == 0) { |
| 4264 | // The directory somehow became owned by root. Wow. |
| 4265 | // This is probably because the system was stopped while |
| 4266 | // installd was in the middle of messing with its libs |
| 4267 | // directory. Ask installd to fix that. |
| 4268 | int ret = mInstaller.fixUid(pkgName, pkg.applicationInfo.uid, |
| 4269 | pkg.applicationInfo.uid); |
| 4270 | if (ret >= 0) { |
| 4271 | recovered = true; |
| 4272 | String msg = "Package " + pkg.packageName |
| 4273 | + " unexpectedly changed to uid 0; recovered to " + |
| 4274 | + pkg.applicationInfo.uid; |
| 4275 | reportSettingsProblem(Log.WARN, msg); |
| 4276 | } |
| 4277 | } |
| 4278 | if (!recovered && ((parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0 |
| 4279 | || (scanMode&SCAN_BOOTING) != 0)) { |
| 4280 | // If this is a system app, we can at least delete its |
| 4281 | // current data so the application will still work. |
| 4282 | int ret = removeDataDirsLI(pkgName); |
| 4283 | if (ret >= 0) { |
| 4284 | // TODO: Kill the processes first |
| 4285 | // Old data gone! |
| 4286 | String prefix = (parseFlags&PackageParser.PARSE_IS_SYSTEM) != 0 |
| 4287 | ? "System package " : "Third party package "; |
| 4288 | String msg = prefix + pkg.packageName |
| 4289 | + " has changed from uid: " |
| 4290 | + currentUid + " to " |
| 4291 | + pkg.applicationInfo.uid + "; old data erased"; |
| 4292 | reportSettingsProblem(Log.WARN, msg); |
| 4293 | recovered = true; |
| 4294 | |
| 4295 | // And now re-install the app. |
Robert Craig | 0f40dc9 | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 4296 | ret = createDataDirsLI(pkgName, pkg.applicationInfo.uid, |
| 4297 | pkg.applicationInfo.seinfo); |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 4298 | if (ret == -1) { |
| 4299 | // Ack should not happen! |
| 4300 | msg = prefix + pkg.packageName |
| 4301 | + " could not have data directory re-created after delete."; |
| 4302 | reportSettingsProblem(Log.WARN, msg); |
| 4303 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 4304 | return null; |
| 4305 | } |
| 4306 | } |
| 4307 | if (!recovered) { |
| 4308 | mHasSystemUidErrors = true; |
| 4309 | } |
| 4310 | } else if (!recovered) { |
| 4311 | // If we allow this install to proceed, we will be broken. |
| 4312 | // Abort, abort! |
| 4313 | mLastScanError = PackageManager.INSTALL_FAILED_UID_CHANGED; |
| 4314 | return null; |
| 4315 | } |
| 4316 | if (!recovered) { |
| 4317 | pkg.applicationInfo.dataDir = "/mismatched_uid/settings_" |
| 4318 | + pkg.applicationInfo.uid + "/fs_" |
| 4319 | + currentUid; |
| 4320 | pkg.applicationInfo.nativeLibraryDir = pkg.applicationInfo.dataDir; |
| 4321 | String msg = "Package " + pkg.packageName |
| 4322 | + " has mismatched uid: " |
| 4323 | + currentUid + " on disk, " |
| 4324 | + pkg.applicationInfo.uid + " in settings"; |
| 4325 | // writer |
| 4326 | synchronized (mPackages) { |
| 4327 | mSettings.mReadMessages.append(msg); |
| 4328 | mSettings.mReadMessages.append('\n'); |
| 4329 | uidError = true; |
| 4330 | if (!pkgSetting.uidError) { |
| 4331 | reportSettingsProblem(Log.ERROR, msg); |
| 4332 | } |
| 4333 | } |
| 4334 | } |
| 4335 | } |
Kenny Root | 4525f5b | 2012-10-22 14:24:23 -0700 | [diff] [blame] | 4336 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 4337 | } else { |
| 4338 | if (DEBUG_PACKAGE_SCANNING) { |
| 4339 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 4340 | Log.v(TAG, "Want this data dir: " + dataPath); |
| 4341 | } |
Nick Kralevich | 1506921 | 2013-01-09 15:54:56 -0800 | [diff] [blame] | 4342 | //invoke installer to do the actual installation |
Robert Craig | 0f40dc9 | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 4343 | int ret = createDataDirsLI(pkgName, pkg.applicationInfo.uid, |
| 4344 | pkg.applicationInfo.seinfo); |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 4345 | if (ret < 0) { |
| 4346 | // Error from installer |
| 4347 | mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 4348 | return null; |
| 4349 | } |
| 4350 | |
| 4351 | if (dataPath.exists()) { |
| 4352 | pkg.applicationInfo.dataDir = dataPath.getPath(); |
| 4353 | } else { |
| 4354 | Slog.w(TAG, "Unable to create data directory: " + dataPath); |
| 4355 | pkg.applicationInfo.dataDir = null; |
| 4356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4357 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4358 | |
| 4359 | /* |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 4360 | * Set the data dir to the default "/data/data/<package name>/lib" |
| 4361 | * if we got here without anyone telling us different (e.g., apps |
| 4362 | * stored on SD card have their native libraries stored in the ASEC |
| 4363 | * container with the APK). |
| 4364 | * |
| 4365 | * This happens during an upgrade from a package settings file that |
| 4366 | * doesn't have a native library path attribute at all. |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4367 | */ |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 4368 | if (pkg.applicationInfo.nativeLibraryDir == null && pkg.applicationInfo.dataDir != null) { |
Kenny Root | bd135c1 | 2010-10-05 12:26:27 -0700 | [diff] [blame] | 4369 | if (pkgSetting.nativeLibraryPathString == null) { |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 4370 | setInternalAppNativeLibraryPath(pkg, pkgSetting); |
Kenny Root | bd135c1 | 2010-10-05 12:26:27 -0700 | [diff] [blame] | 4371 | } else { |
| 4372 | pkg.applicationInfo.nativeLibraryDir = pkgSetting.nativeLibraryPathString; |
| 4373 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4374 | } |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 4375 | |
| 4376 | pkgSetting.uidError = uidError; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4377 | } |
| 4378 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 4379 | String path = scanFile.getPath(); |
| 4380 | /* Note: We don't want to unpack the native binaries for |
| 4381 | * system applications, unless they have been updated |
| 4382 | * (the binaries are already under /system/lib). |
| 4383 | * Also, don't unpack libs for apps on the external card |
| 4384 | * since they should have their libraries in the ASEC |
| 4385 | * container already. |
| 4386 | * |
| 4387 | * In other words, we're going to unpack the binaries |
| 4388 | * only for non-system apps and system app upgrades. |
| 4389 | */ |
| 4390 | if (pkg.applicationInfo.nativeLibraryDir != null) { |
| 4391 | try { |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 4392 | File nativeLibraryDir = new File(pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 5832ead | 2011-08-02 15:47:08 -0700 | [diff] [blame] | 4393 | final String dataPathString = dataPath.getCanonicalPath(); |
Kenny Root | e68d58a | 2010-10-18 16:08:54 -0700 | [diff] [blame] | 4394 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 4395 | if (isSystemApp(pkg) && !isUpdatedSystemApp(pkg)) { |
| 4396 | /* |
| 4397 | * Upgrading from a previous version of the OS sometimes |
| 4398 | * leaves native libraries in the /data/data/<app>/lib |
| 4399 | * directory for system apps even when they shouldn't be. |
| 4400 | * Recent changes in the JNI library search path |
| 4401 | * necessitates we remove those to match previous behavior. |
| 4402 | */ |
| 4403 | if (NativeLibraryHelper.removeNativeBinariesFromDirLI(nativeLibraryDir)) { |
| 4404 | Log.i(TAG, "removed obsolete native libraries for system package " |
| 4405 | + path); |
Kenny Root | 831baa2 | 2010-10-05 12:29:25 -0700 | [diff] [blame] | 4406 | } |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 4407 | } else { |
| 4408 | if (!isForwardLocked(pkg) && !isExternal(pkg)) { |
| 4409 | /* |
| 4410 | * Update native library dir if it starts with |
| 4411 | * /data/data |
| 4412 | */ |
| 4413 | if (nativeLibraryDir.getPath().startsWith(dataPathString)) { |
| 4414 | setInternalAppNativeLibraryPath(pkg, pkgSetting); |
| 4415 | nativeLibraryDir = new File(pkg.applicationInfo.nativeLibraryDir); |
| 4416 | } |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 4417 | |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 4418 | try { |
| 4419 | if (copyNativeLibrariesForInternalApp(scanFile, nativeLibraryDir) != PackageManager.INSTALL_SUCCEEDED) { |
| 4420 | Slog.e(TAG, "Unable to copy native libraries"); |
| 4421 | mLastScanError = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 4422 | return null; |
| 4423 | } |
| 4424 | } catch (IOException e) { |
| 4425 | Slog.e(TAG, "Unable to copy native libraries", e); |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 4426 | mLastScanError = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 4427 | return null; |
| 4428 | } |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 4429 | } |
| 4430 | |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 4431 | if (DEBUG_INSTALL) Slog.i(TAG, "Linking native library dir for " + path); |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 4432 | final int[] userIds = sUserManager.getUserIds(); |
| 4433 | synchronized (mInstallLock) { |
| 4434 | for (int userId : userIds) { |
| 4435 | if (mInstaller.linkNativeLibraryDirectory(pkg.packageName, |
| 4436 | pkg.applicationInfo.nativeLibraryDir, userId) < 0) { |
| 4437 | Slog.w(TAG, "Failed linking native library dir (user=" + userId |
| 4438 | + ")"); |
| 4439 | mLastScanError = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 4440 | return null; |
| 4441 | } |
| 4442 | } |
Nick Kralevich | 7de350a | 2012-09-07 15:48:11 -0700 | [diff] [blame] | 4443 | } |
Kenny Root | 831baa2 | 2010-10-05 12:29:25 -0700 | [diff] [blame] | 4444 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 4445 | } catch (IOException ioe) { |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 4446 | Slog.e(TAG, "Unable to get canonical file " + ioe.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4447 | } |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 4448 | } |
| 4449 | pkg.mScanPath = path; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4450 | |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 4451 | if ((scanMode&SCAN_NO_DEX) == 0) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 4452 | if (performDexOptLI(pkg, forceDex, (scanMode&SCAN_DEFER_DEX) != 0, false) |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4453 | == DEX_OPT_FAILED) { |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 4454 | mLastScanError = PackageManager.INSTALL_FAILED_DEXOPT; |
| 4455 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4456 | } |
| 4457 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4459 | if (mFactoryTest && pkg.requestedPermissions.contains( |
| 4460 | android.Manifest.permission.FACTORY_TEST)) { |
| 4461 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_FACTORY_TEST; |
| 4462 | } |
| 4463 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 4464 | ArrayList<PackageParser.Package> clientLibPkgs = null; |
| 4465 | |
| 4466 | // writer |
| 4467 | synchronized (mPackages) { |
| 4468 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 4469 | // Only system apps can add new shared libraries. |
| 4470 | if (pkg.libraryNames != null) { |
| 4471 | for (int i=0; i<pkg.libraryNames.size(); i++) { |
| 4472 | String name = pkg.libraryNames.get(i); |
| 4473 | boolean allowed = false; |
| 4474 | if (isUpdatedSystemApp(pkg)) { |
| 4475 | // New library entries can only be added through the |
| 4476 | // system image. This is important to get rid of a lot |
| 4477 | // of nasty edge cases: for example if we allowed a non- |
| 4478 | // system update of the app to add a library, then uninstalling |
| 4479 | // the update would make the library go away, and assumptions |
| 4480 | // we made such as through app install filtering would now |
| 4481 | // have allowed apps on the device which aren't compatible |
| 4482 | // with it. Better to just have the restriction here, be |
| 4483 | // conservative, and create many fewer cases that can negatively |
| 4484 | // impact the user experience. |
| 4485 | final PackageSetting sysPs = mSettings |
| 4486 | .getDisabledSystemPkgLPr(pkg.packageName); |
| 4487 | if (sysPs.pkg != null && sysPs.pkg.libraryNames != null) { |
| 4488 | for (int j=0; j<sysPs.pkg.libraryNames.size(); j++) { |
| 4489 | if (name.equals(sysPs.pkg.libraryNames.get(j))) { |
| 4490 | allowed = true; |
| 4491 | allowed = true; |
| 4492 | break; |
| 4493 | } |
| 4494 | } |
| 4495 | } |
| 4496 | } else { |
| 4497 | allowed = true; |
| 4498 | } |
| 4499 | if (allowed) { |
| 4500 | if (!mSharedLibraries.containsKey(name)) { |
| 4501 | mSharedLibraries.put(name, new SharedLibraryEntry(null, |
| 4502 | pkg.packageName)); |
| 4503 | } else if (!name.equals(pkg.packageName)) { |
| 4504 | Slog.w(TAG, "Package " + pkg.packageName + " library " |
| 4505 | + name + " already exists; skipping"); |
| 4506 | } |
| 4507 | } else { |
| 4508 | Slog.w(TAG, "Package " + pkg.packageName + " declares lib " |
| 4509 | + name + " that is not declared on system image; skipping"); |
| 4510 | } |
| 4511 | } |
| 4512 | if ((scanMode&SCAN_BOOTING) == 0) { |
| 4513 | // If we are not booting, we need to update any applications |
| 4514 | // that are clients of our shared library. If we are booting, |
| 4515 | // this will all be done once the scan is complete. |
| 4516 | clientLibPkgs = updateAllSharedLibrariesLPw(pkg); |
| 4517 | } |
| 4518 | } |
| 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | // We also need to dexopt any apps that are dependent on this library. Note that |
| 4523 | // if these fail, we should abort the install since installing the library will |
| 4524 | // result in some apps being broken. |
| 4525 | if (clientLibPkgs != null) { |
| 4526 | if ((scanMode&SCAN_NO_DEX) == 0) { |
| 4527 | for (int i=0; i<clientLibPkgs.size(); i++) { |
| 4528 | PackageParser.Package clientPkg = clientLibPkgs.get(i); |
| 4529 | if (performDexOptLI(clientPkg, forceDex, (scanMode&SCAN_DEFER_DEX) != 0, false) |
| 4530 | == DEX_OPT_FAILED) { |
| 4531 | mLastScanError = PackageManager.INSTALL_FAILED_DEXOPT; |
| 4532 | return null; |
| 4533 | } |
| 4534 | } |
| 4535 | } |
| 4536 | } |
| 4537 | |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 4538 | // Request the ActivityManager to kill the process(only for existing packages) |
| 4539 | // so that we do not end up in a confused state while the user is still using the older |
| 4540 | // version of the application while the new one gets installed. |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4541 | if ((parseFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4542 | killApplication(pkg.applicationInfo.packageName, |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 4543 | pkg.applicationInfo.uid); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 4544 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4545 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 4546 | // Also need to kill any apps that are dependent on the library. |
| 4547 | if (clientLibPkgs != null) { |
| 4548 | for (int i=0; i<clientLibPkgs.size(); i++) { |
| 4549 | PackageParser.Package clientPkg = clientLibPkgs.get(i); |
| 4550 | killApplication(clientPkg.applicationInfo.packageName, |
| 4551 | clientPkg.applicationInfo.uid); |
| 4552 | } |
| 4553 | } |
| 4554 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4555 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4556 | synchronized (mPackages) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 4557 | // We don't expect installation to fail beyond this point, |
| 4558 | if ((scanMode&SCAN_MONITOR) != 0) { |
| 4559 | mAppDirs.put(pkg.mPath, pkg); |
| 4560 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4561 | // Add the new setting to mSettings |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4562 | mSettings.insertPackageSettingLPw(pkgSetting, pkg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4563 | // Add the new setting to mPackages |
Suchi Amalapurapu | ea5c044 | 2009-07-13 10:36:15 -0700 | [diff] [blame] | 4564 | mPackages.put(pkg.applicationInfo.packageName, pkg); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 4565 | // Make sure we don't accidentally delete its data. |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 4566 | final Iterator<PackageCleanItem> iter = mSettings.mPackagesToBeCleaned.iterator(); |
| 4567 | while (iter.hasNext()) { |
| 4568 | PackageCleanItem item = iter.next(); |
| 4569 | if (pkgName.equals(item.packageName)) { |
| 4570 | iter.remove(); |
| 4571 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 4572 | } |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 4573 | |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 4574 | // Take care of first install / last update times. |
| 4575 | if (currentTime != 0) { |
| 4576 | if (pkgSetting.firstInstallTime == 0) { |
| 4577 | pkgSetting.firstInstallTime = pkgSetting.lastUpdateTime = currentTime; |
| 4578 | } else if ((scanMode&SCAN_UPDATE_TIME) != 0) { |
| 4579 | pkgSetting.lastUpdateTime = currentTime; |
| 4580 | } |
| 4581 | } else if (pkgSetting.firstInstallTime == 0) { |
| 4582 | // We need *something*. Take time time stamp of the file. |
| 4583 | pkgSetting.firstInstallTime = pkgSetting.lastUpdateTime = scanFileTime; |
| 4584 | } else if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) != 0) { |
| 4585 | if (scanFileTime != pkgSetting.timeStamp) { |
| 4586 | // A package on the system image has changed; consider this |
| 4587 | // to be an update. |
| 4588 | pkgSetting.lastUpdateTime = scanFileTime; |
| 4589 | } |
| 4590 | } |
| 4591 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4592 | int N = pkg.providers.size(); |
| 4593 | StringBuilder r = null; |
| 4594 | int i; |
| 4595 | for (i=0; i<N; i++) { |
| 4596 | PackageParser.Provider p = pkg.providers.get(i); |
| 4597 | p.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4598 | p.info.processName, pkg.applicationInfo.uid); |
| 4599 | mProvidersByComponent.put(new ComponentName(p.info.packageName, |
| 4600 | p.info.name), p); |
| 4601 | p.syncable = p.info.isSyncable; |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4602 | if (p.info.authority != null) { |
| 4603 | String names[] = p.info.authority.split(";"); |
| 4604 | p.info.authority = null; |
| 4605 | for (int j = 0; j < names.length; j++) { |
| 4606 | if (j == 1 && p.syncable) { |
| 4607 | // We only want the first authority for a provider to possibly be |
| 4608 | // syncable, so if we already added this provider using a different |
| 4609 | // authority clear the syncable flag. We copy the provider before |
| 4610 | // changing it because the mProviders object contains a reference |
| 4611 | // to a provider that we don't want to change. |
| 4612 | // Only do this for the second authority since the resulting provider |
| 4613 | // object can be the same for all future authorities for this provider. |
| 4614 | p = new PackageParser.Provider(p); |
| 4615 | p.syncable = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4616 | } |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4617 | if (!mProviders.containsKey(names[j])) { |
| 4618 | mProviders.put(names[j], p); |
| 4619 | if (p.info.authority == null) { |
| 4620 | p.info.authority = names[j]; |
| 4621 | } else { |
| 4622 | p.info.authority = p.info.authority + ";" + names[j]; |
| 4623 | } |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4624 | if (DEBUG_PACKAGE_SCANNING) { |
| 4625 | if ((parseFlags & PackageParser.PARSE_CHATTY) != 0) |
| 4626 | Log.d(TAG, "Registered content provider: " + names[j] |
| 4627 | + ", className = " + p.info.name + ", isSyncable = " |
| 4628 | + p.info.isSyncable); |
| 4629 | } |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4630 | } else { |
| 4631 | PackageParser.Provider other = mProviders.get(names[j]); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4632 | Slog.w(TAG, "Skipping provider name " + names[j] + |
Suchi Amalapurapu | 9444bea | 2010-03-05 10:46:11 -0800 | [diff] [blame] | 4633 | " (in package " + pkg.applicationInfo.packageName + |
| 4634 | "): name already used by " |
| 4635 | + ((other != null && other.getComponentName() != null) |
| 4636 | ? other.getComponentName().getPackageName() : "?")); |
| 4637 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4638 | } |
| 4639 | } |
| 4640 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4641 | if (r == null) { |
| 4642 | r = new StringBuilder(256); |
| 4643 | } else { |
| 4644 | r.append(' '); |
| 4645 | } |
| 4646 | r.append(p.info.name); |
| 4647 | } |
| 4648 | } |
| 4649 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4650 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Providers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4653 | N = pkg.services.size(); |
| 4654 | r = null; |
| 4655 | for (i=0; i<N; i++) { |
| 4656 | PackageParser.Service s = pkg.services.get(i); |
| 4657 | s.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4658 | s.info.processName, pkg.applicationInfo.uid); |
| 4659 | mServices.addService(s); |
| 4660 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4661 | if (r == null) { |
| 4662 | r = new StringBuilder(256); |
| 4663 | } else { |
| 4664 | r.append(' '); |
| 4665 | } |
| 4666 | r.append(s.info.name); |
| 4667 | } |
| 4668 | } |
| 4669 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4670 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Services: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4671 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4672 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4673 | N = pkg.receivers.size(); |
| 4674 | r = null; |
| 4675 | for (i=0; i<N; i++) { |
| 4676 | PackageParser.Activity a = pkg.receivers.get(i); |
| 4677 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4678 | a.info.processName, pkg.applicationInfo.uid); |
| 4679 | mReceivers.addActivity(a, "receiver"); |
| 4680 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4681 | if (r == null) { |
| 4682 | r = new StringBuilder(256); |
| 4683 | } else { |
| 4684 | r.append(' '); |
| 4685 | } |
| 4686 | r.append(a.info.name); |
| 4687 | } |
| 4688 | } |
| 4689 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4690 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Receivers: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4691 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4693 | N = pkg.activities.size(); |
| 4694 | r = null; |
| 4695 | for (i=0; i<N; i++) { |
| 4696 | PackageParser.Activity a = pkg.activities.get(i); |
| 4697 | a.info.processName = fixProcessName(pkg.applicationInfo.processName, |
| 4698 | a.info.processName, pkg.applicationInfo.uid); |
| 4699 | mActivities.addActivity(a, "activity"); |
| 4700 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4701 | if (r == null) { |
| 4702 | r = new StringBuilder(256); |
| 4703 | } else { |
| 4704 | r.append(' '); |
| 4705 | } |
| 4706 | r.append(a.info.name); |
| 4707 | } |
| 4708 | } |
| 4709 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4710 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Activities: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4711 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4712 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4713 | N = pkg.permissionGroups.size(); |
| 4714 | r = null; |
| 4715 | for (i=0; i<N; i++) { |
| 4716 | PackageParser.PermissionGroup pg = pkg.permissionGroups.get(i); |
| 4717 | PackageParser.PermissionGroup cur = mPermissionGroups.get(pg.info.name); |
| 4718 | if (cur == null) { |
| 4719 | mPermissionGroups.put(pg.info.name, pg); |
| 4720 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4721 | if (r == null) { |
| 4722 | r = new StringBuilder(256); |
| 4723 | } else { |
| 4724 | r.append(' '); |
| 4725 | } |
| 4726 | r.append(pg.info.name); |
| 4727 | } |
| 4728 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4729 | 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] | 4730 | + pg.info.packageName + " ignored: original from " |
| 4731 | + cur.info.packageName); |
| 4732 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4733 | if (r == null) { |
| 4734 | r = new StringBuilder(256); |
| 4735 | } else { |
| 4736 | r.append(' '); |
| 4737 | } |
| 4738 | r.append("DUP:"); |
| 4739 | r.append(pg.info.name); |
| 4740 | } |
| 4741 | } |
| 4742 | } |
| 4743 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4744 | 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] | 4745 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4746 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4747 | N = pkg.permissions.size(); |
| 4748 | r = null; |
| 4749 | for (i=0; i<N; i++) { |
| 4750 | PackageParser.Permission p = pkg.permissions.get(i); |
| 4751 | HashMap<String, BasePermission> permissionMap = |
| 4752 | p.tree ? mSettings.mPermissionTrees |
| 4753 | : mSettings.mPermissions; |
| 4754 | p.group = mPermissionGroups.get(p.info.group); |
| 4755 | if (p.info.group == null || p.group != null) { |
| 4756 | BasePermission bp = permissionMap.get(p.info.name); |
| 4757 | if (bp == null) { |
| 4758 | bp = new BasePermission(p.info.name, p.info.packageName, |
| 4759 | BasePermission.TYPE_NORMAL); |
| 4760 | permissionMap.put(p.info.name, bp); |
| 4761 | } |
| 4762 | if (bp.perm == null) { |
| 4763 | if (bp.sourcePackage == null |
| 4764 | || bp.sourcePackage.equals(p.info.packageName)) { |
| 4765 | BasePermission tree = findPermissionTreeLP(p.info.name); |
| 4766 | if (tree == null |
| 4767 | || tree.sourcePackage.equals(p.info.packageName)) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4768 | bp.packageSetting = pkgSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4769 | bp.perm = p; |
| 4770 | bp.uid = pkg.applicationInfo.uid; |
| 4771 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4772 | if (r == null) { |
| 4773 | r = new StringBuilder(256); |
| 4774 | } else { |
| 4775 | r.append(' '); |
| 4776 | } |
| 4777 | r.append(p.info.name); |
| 4778 | } |
| 4779 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4780 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4781 | + p.info.packageName + " ignored: base tree " |
| 4782 | + tree.name + " is from package " |
| 4783 | + tree.sourcePackage); |
| 4784 | } |
| 4785 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4786 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4787 | + p.info.packageName + " ignored: original from " |
| 4788 | + bp.sourcePackage); |
| 4789 | } |
| 4790 | } else if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4791 | if (r == null) { |
| 4792 | r = new StringBuilder(256); |
| 4793 | } else { |
| 4794 | r.append(' '); |
| 4795 | } |
| 4796 | r.append("DUP:"); |
| 4797 | r.append(p.info.name); |
| 4798 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 4799 | if (bp.perm == p) { |
| 4800 | bp.protectionLevel = p.info.protectionLevel; |
| 4801 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4802 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 4803 | Slog.w(TAG, "Permission " + p.info.name + " from package " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4804 | + p.info.packageName + " ignored: no group " |
| 4805 | + p.group); |
| 4806 | } |
| 4807 | } |
| 4808 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4809 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Permissions: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4810 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4811 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4812 | N = pkg.instrumentation.size(); |
| 4813 | r = null; |
| 4814 | for (i=0; i<N; i++) { |
| 4815 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
| 4816 | a.info.packageName = pkg.applicationInfo.packageName; |
| 4817 | a.info.sourceDir = pkg.applicationInfo.sourceDir; |
| 4818 | a.info.publicSourceDir = pkg.applicationInfo.publicSourceDir; |
| 4819 | a.info.dataDir = pkg.applicationInfo.dataDir; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 4820 | a.info.nativeLibraryDir = pkg.applicationInfo.nativeLibraryDir; |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 4821 | mInstrumentation.put(a.getComponentName(), a); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4822 | if ((parseFlags&PackageParser.PARSE_CHATTY) != 0) { |
| 4823 | if (r == null) { |
| 4824 | r = new StringBuilder(256); |
| 4825 | } else { |
| 4826 | r.append(' '); |
| 4827 | } |
| 4828 | r.append(a.info.name); |
| 4829 | } |
| 4830 | } |
| 4831 | if (r != null) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 4832 | if (DEBUG_PACKAGE_SCANNING) Log.d(TAG, " Instrumentation: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4833 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4834 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 4835 | if (pkg.protectedBroadcasts != null) { |
| 4836 | N = pkg.protectedBroadcasts.size(); |
| 4837 | for (i=0; i<N; i++) { |
| 4838 | mProtectedBroadcasts.add(pkg.protectedBroadcasts.get(i)); |
| 4839 | } |
| 4840 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4841 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4842 | pkgSetting.setTimeStamp(scanFileTime); |
| 4843 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4844 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4845 | return pkg; |
| 4846 | } |
| 4847 | |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 4848 | private void setInternalAppNativeLibraryPath(PackageParser.Package pkg, |
| 4849 | PackageSetting pkgSetting) { |
| 4850 | final String apkLibPath = getApkName(pkgSetting.codePathString); |
| 4851 | final String nativeLibraryPath = new File(mAppLibInstallDir, apkLibPath).getPath(); |
| 4852 | pkg.applicationInfo.nativeLibraryDir = nativeLibraryPath; |
| 4853 | pkgSetting.nativeLibraryPathString = nativeLibraryPath; |
| 4854 | } |
| 4855 | |
| 4856 | private static int copyNativeLibrariesForInternalApp(File scanFile, final File nativeLibraryDir) |
| 4857 | throws IOException { |
| 4858 | if (!nativeLibraryDir.isDirectory()) { |
| 4859 | nativeLibraryDir.delete(); |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 4860 | |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 4861 | if (!nativeLibraryDir.mkdir()) { |
| 4862 | throw new IOException("Cannot create " + nativeLibraryDir.getPath()); |
| 4863 | } |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 4864 | |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 4865 | try { |
| 4866 | Libcore.os.chmod(nativeLibraryDir.getPath(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH |
| 4867 | | S_IXOTH); |
| 4868 | } catch (ErrnoException e) { |
| 4869 | throw new IOException("Cannot chmod native library directory " |
| 4870 | + nativeLibraryDir.getPath(), e); |
| 4871 | } |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 4872 | } else if (!SELinux.restorecon(nativeLibraryDir)) { |
| 4873 | throw new IOException("Cannot set SELinux context for " + nativeLibraryDir.getPath()); |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | /* |
| 4877 | * If this is an internal application or our nativeLibraryPath points to |
| 4878 | * the app-lib directory, unpack the libraries if necessary. |
| 4879 | */ |
| 4880 | return NativeLibraryHelper.copyNativeBinariesIfNeededLI(scanFile, nativeLibraryDir); |
| 4881 | } |
| 4882 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4883 | private void killApplication(String pkgName, int appId) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4884 | // Request the ActivityManager to kill the process(only for existing packages) |
| 4885 | // so that we do not end up in a confused state while the user is still using the older |
| 4886 | // version of the application while the new one gets installed. |
| 4887 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 4888 | if (am != null) { |
| 4889 | try { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4890 | am.killApplicationWithAppId(pkgName, appId); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 4891 | } catch (RemoteException e) { |
| 4892 | } |
| 4893 | } |
| 4894 | } |
| 4895 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4896 | void removePackageLI(PackageSetting ps, boolean chatty) { |
| 4897 | if (DEBUG_INSTALL) { |
| 4898 | if (chatty) |
| 4899 | Log.d(TAG, "Removing package " + ps.name); |
| 4900 | } |
| 4901 | |
| 4902 | // writer |
| 4903 | synchronized (mPackages) { |
| 4904 | mPackages.remove(ps.name); |
| 4905 | if (ps.codePathString != null) { |
| 4906 | mAppDirs.remove(ps.codePathString); |
| 4907 | } |
| 4908 | |
| 4909 | final PackageParser.Package pkg = ps.pkg; |
| 4910 | if (pkg != null) { |
| 4911 | cleanPackageDataStructuresLILPw(pkg, chatty); |
| 4912 | } |
| 4913 | } |
| 4914 | } |
| 4915 | |
| 4916 | void removeInstalledPackageLI(PackageParser.Package pkg, boolean chatty) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 4917 | if (DEBUG_INSTALL) { |
| 4918 | if (chatty) |
| 4919 | Log.d(TAG, "Removing package " + pkg.applicationInfo.packageName); |
| 4920 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4921 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 4922 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4923 | synchronized (mPackages) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4924 | mPackages.remove(pkg.applicationInfo.packageName); |
| 4925 | if (pkg.mPath != null) { |
| 4926 | mAppDirs.remove(pkg.mPath); |
| 4927 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4928 | cleanPackageDataStructuresLILPw(pkg, chatty); |
| 4929 | } |
| 4930 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4931 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4932 | void cleanPackageDataStructuresLILPw(PackageParser.Package pkg, boolean chatty) { |
| 4933 | int N = pkg.providers.size(); |
| 4934 | StringBuilder r = null; |
| 4935 | int i; |
| 4936 | for (i=0; i<N; i++) { |
| 4937 | PackageParser.Provider p = pkg.providers.get(i); |
| 4938 | mProvidersByComponent.remove(new ComponentName(p.info.packageName, |
| 4939 | p.info.name)); |
| 4940 | if (p.info.authority == null) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4941 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4942 | /* There was another ContentProvider with this authority when |
| 4943 | * this app was installed so this authority is null, |
| 4944 | * Ignore it as we don't have to unregister the provider. |
| 4945 | */ |
| 4946 | continue; |
| 4947 | } |
| 4948 | String names[] = p.info.authority.split(";"); |
| 4949 | for (int j = 0; j < names.length; j++) { |
| 4950 | if (mProviders.get(names[j]) == p) { |
| 4951 | mProviders.remove(names[j]); |
| 4952 | if (DEBUG_REMOVE) { |
| 4953 | if (chatty) |
| 4954 | Log.d(TAG, "Unregistered content provider: " + names[j] |
| 4955 | + ", className = " + p.info.name + ", isSyncable = " |
| 4956 | + p.info.isSyncable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4957 | } |
| 4958 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4959 | } |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 4960 | if (DEBUG_REMOVE && chatty) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4961 | if (r == null) { |
| 4962 | r = new StringBuilder(256); |
| 4963 | } else { |
| 4964 | r.append(' '); |
| 4965 | } |
| 4966 | r.append(p.info.name); |
| 4967 | } |
| 4968 | } |
| 4969 | if (r != null) { |
| 4970 | if (DEBUG_REMOVE) Log.d(TAG, " Providers: " + r); |
| 4971 | } |
| 4972 | |
| 4973 | N = pkg.services.size(); |
| 4974 | r = null; |
| 4975 | for (i=0; i<N; i++) { |
| 4976 | PackageParser.Service s = pkg.services.get(i); |
| 4977 | mServices.removeService(s); |
| 4978 | if (chatty) { |
| 4979 | if (r == null) { |
| 4980 | r = new StringBuilder(256); |
| 4981 | } else { |
| 4982 | r.append(' '); |
| 4983 | } |
| 4984 | r.append(s.info.name); |
| 4985 | } |
| 4986 | } |
| 4987 | if (r != null) { |
| 4988 | if (DEBUG_REMOVE) Log.d(TAG, " Services: " + r); |
| 4989 | } |
| 4990 | |
| 4991 | N = pkg.receivers.size(); |
| 4992 | r = null; |
| 4993 | for (i=0; i<N; i++) { |
| 4994 | PackageParser.Activity a = pkg.receivers.get(i); |
| 4995 | mReceivers.removeActivity(a, "receiver"); |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 4996 | if (DEBUG_REMOVE && chatty) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4997 | if (r == null) { |
| 4998 | r = new StringBuilder(256); |
| 4999 | } else { |
| 5000 | r.append(' '); |
| 5001 | } |
| 5002 | r.append(a.info.name); |
| 5003 | } |
| 5004 | } |
| 5005 | if (r != null) { |
| 5006 | if (DEBUG_REMOVE) Log.d(TAG, " Receivers: " + r); |
| 5007 | } |
| 5008 | |
| 5009 | N = pkg.activities.size(); |
| 5010 | r = null; |
| 5011 | for (i=0; i<N; i++) { |
| 5012 | PackageParser.Activity a = pkg.activities.get(i); |
| 5013 | mActivities.removeActivity(a, "activity"); |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 5014 | if (DEBUG_REMOVE && chatty) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5015 | if (r == null) { |
| 5016 | r = new StringBuilder(256); |
| 5017 | } else { |
| 5018 | r.append(' '); |
| 5019 | } |
| 5020 | r.append(a.info.name); |
| 5021 | } |
| 5022 | } |
| 5023 | if (r != null) { |
| 5024 | if (DEBUG_REMOVE) Log.d(TAG, " Activities: " + r); |
| 5025 | } |
| 5026 | |
| 5027 | N = pkg.permissions.size(); |
| 5028 | r = null; |
| 5029 | for (i=0; i<N; i++) { |
| 5030 | PackageParser.Permission p = pkg.permissions.get(i); |
| 5031 | BasePermission bp = mSettings.mPermissions.get(p.info.name); |
| 5032 | if (bp == null) { |
| 5033 | bp = mSettings.mPermissionTrees.get(p.info.name); |
| 5034 | } |
| 5035 | if (bp != null && bp.perm == p) { |
| 5036 | bp.perm = null; |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 5037 | if (DEBUG_REMOVE && chatty) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5038 | if (r == null) { |
| 5039 | r = new StringBuilder(256); |
| 5040 | } else { |
| 5041 | r.append(' '); |
| 5042 | } |
| 5043 | r.append(p.info.name); |
| 5044 | } |
| 5045 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5046 | } |
| 5047 | if (r != null) { |
| 5048 | if (DEBUG_REMOVE) Log.d(TAG, " Permissions: " + r); |
| 5049 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5050 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5051 | N = pkg.instrumentation.size(); |
| 5052 | r = null; |
| 5053 | for (i=0; i<N; i++) { |
| 5054 | PackageParser.Instrumentation a = pkg.instrumentation.get(i); |
| 5055 | mInstrumentation.remove(a.getComponentName()); |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 5056 | if (DEBUG_REMOVE && chatty) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5057 | if (r == null) { |
| 5058 | r = new StringBuilder(256); |
| 5059 | } else { |
| 5060 | r.append(' '); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5061 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5062 | r.append(a.info.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5063 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5064 | } |
| 5065 | if (r != null) { |
| 5066 | if (DEBUG_REMOVE) Log.d(TAG, " Instrumentation: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5067 | } |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 5068 | |
| 5069 | r = null; |
| 5070 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 5071 | // Only system apps can hold shared libraries. |
| 5072 | if (pkg.libraryNames != null) { |
| 5073 | for (i=0; i<pkg.libraryNames.size(); i++) { |
| 5074 | String name = pkg.libraryNames.get(i); |
| 5075 | SharedLibraryEntry cur = mSharedLibraries.get(name); |
| 5076 | if (cur != null && cur.apk != null && cur.apk.equals(pkg.packageName)) { |
| 5077 | mSharedLibraries.remove(name); |
| 5078 | if (DEBUG_REMOVE && chatty) { |
| 5079 | if (r == null) { |
| 5080 | r = new StringBuilder(256); |
| 5081 | } else { |
| 5082 | r.append(' '); |
| 5083 | } |
| 5084 | r.append(name); |
| 5085 | } |
| 5086 | } |
| 5087 | } |
| 5088 | } |
| 5089 | } |
| 5090 | if (r != null) { |
| 5091 | if (DEBUG_REMOVE) Log.d(TAG, " Libraries: " + r); |
| 5092 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | private static final boolean isPackageFilename(String name) { |
| 5096 | return name != null && name.endsWith(".apk"); |
| 5097 | } |
| 5098 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5099 | private static boolean hasPermission(PackageParser.Package pkgInfo, String perm) { |
| 5100 | for (int i=pkgInfo.permissions.size()-1; i>=0; i--) { |
| 5101 | if (pkgInfo.permissions.get(i).info.name.equals(perm)) { |
| 5102 | return true; |
| 5103 | } |
| 5104 | } |
| 5105 | return false; |
| 5106 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5107 | |
| 5108 | static final int UPDATE_PERMISSIONS_ALL = 1<<0; |
| 5109 | static final int UPDATE_PERMISSIONS_REPLACE_PKG = 1<<1; |
| 5110 | static final int UPDATE_PERMISSIONS_REPLACE_ALL = 1<<2; |
| 5111 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5112 | private void updatePermissionsLPw(String changingPkg, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5113 | PackageParser.Package pkgInfo, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5114 | // Make sure there are no dangling permission trees. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5115 | Iterator<BasePermission> it = mSettings.mPermissionTrees.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5116 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5117 | final BasePermission bp = it.next(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5118 | if (bp.packageSetting == null) { |
| 5119 | // We may not yet have parsed the package, so just see if |
| 5120 | // we still know about its settings. |
| 5121 | bp.packageSetting = mSettings.mPackages.get(bp.sourcePackage); |
| 5122 | } |
| 5123 | if (bp.packageSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5124 | Slog.w(TAG, "Removing dangling permission tree: " + bp.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5125 | + " from package " + bp.sourcePackage); |
| 5126 | it.remove(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5127 | } else if (changingPkg != null && changingPkg.equals(bp.sourcePackage)) { |
| 5128 | if (pkgInfo == null || !hasPermission(pkgInfo, bp.name)) { |
| 5129 | Slog.i(TAG, "Removing old permission tree: " + bp.name |
| 5130 | + " from package " + bp.sourcePackage); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5131 | flags |= UPDATE_PERMISSIONS_ALL; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5132 | it.remove(); |
| 5133 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5134 | } |
| 5135 | } |
| 5136 | |
| 5137 | // Make sure all dynamic permissions have been assigned to a package, |
| 5138 | // and make sure there are no dangling permissions. |
| 5139 | it = mSettings.mPermissions.values().iterator(); |
| 5140 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5141 | final BasePermission bp = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5142 | if (bp.type == BasePermission.TYPE_DYNAMIC) { |
| 5143 | if (DEBUG_SETTINGS) Log.v(TAG, "Dynamic permission: name=" |
| 5144 | + bp.name + " pkg=" + bp.sourcePackage |
| 5145 | + " info=" + bp.pendingInfo); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5146 | if (bp.packageSetting == null && bp.pendingInfo != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5147 | final BasePermission tree = findPermissionTreeLP(bp.name); |
Dianne Hackborn | d2509fd | 2011-09-12 12:29:43 -0700 | [diff] [blame] | 5148 | if (tree != null && tree.perm != null) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5149 | bp.packageSetting = tree.packageSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5150 | bp.perm = new PackageParser.Permission(tree.perm.owner, |
| 5151 | new PermissionInfo(bp.pendingInfo)); |
| 5152 | bp.perm.info.packageName = tree.perm.info.packageName; |
| 5153 | bp.perm.info.name = bp.name; |
| 5154 | bp.uid = tree.uid; |
| 5155 | } |
| 5156 | } |
| 5157 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5158 | if (bp.packageSetting == null) { |
| 5159 | // We may not yet have parsed the package, so just see if |
| 5160 | // we still know about its settings. |
| 5161 | bp.packageSetting = mSettings.mPackages.get(bp.sourcePackage); |
| 5162 | } |
| 5163 | if (bp.packageSetting == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 5164 | Slog.w(TAG, "Removing dangling permission: " + bp.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5165 | + " from package " + bp.sourcePackage); |
| 5166 | it.remove(); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5167 | } else if (changingPkg != null && changingPkg.equals(bp.sourcePackage)) { |
| 5168 | if (pkgInfo == null || !hasPermission(pkgInfo, bp.name)) { |
| 5169 | Slog.i(TAG, "Removing old permission: " + bp.name |
| 5170 | + " from package " + bp.sourcePackage); |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5171 | flags |= UPDATE_PERMISSIONS_ALL; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5172 | it.remove(); |
| 5173 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5174 | } |
| 5175 | } |
| 5176 | |
| 5177 | // Now update the permissions for all packages, in particular |
| 5178 | // replace the granted permissions of the system packages. |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5179 | if ((flags&UPDATE_PERMISSIONS_ALL) != 0) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5180 | for (PackageParser.Package pkg : mPackages.values()) { |
| 5181 | if (pkg != pkgInfo) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5182 | grantPermissionsLPw(pkg, (flags&UPDATE_PERMISSIONS_REPLACE_ALL) != 0); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5183 | } |
| 5184 | } |
| 5185 | } |
| 5186 | |
| 5187 | if (pkgInfo != null) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5188 | grantPermissionsLPw(pkgInfo, (flags&UPDATE_PERMISSIONS_REPLACE_PKG) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5189 | } |
| 5190 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5191 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5192 | private void grantPermissionsLPw(PackageParser.Package pkg, boolean replace) { |
| 5193 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5194 | if (ps == null) { |
| 5195 | return; |
| 5196 | } |
| 5197 | final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5198 | HashSet<String> origPermissions = gp.grantedPermissions; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5199 | boolean changedPermission = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5201 | if (replace) { |
| 5202 | ps.permissionsFixed = false; |
| 5203 | if (gp == ps) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 5204 | origPermissions = new HashSet<String>(gp.grantedPermissions); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5205 | gp.grantedPermissions.clear(); |
| 5206 | gp.gids = mGlobalGids; |
| 5207 | } |
| 5208 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5210 | if (gp.gids == null) { |
| 5211 | gp.gids = mGlobalGids; |
| 5212 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5214 | final int N = pkg.requestedPermissions.size(); |
| 5215 | for (int i=0; i<N; i++) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5216 | final String name = pkg.requestedPermissions.get(i); |
Nick Kralevich | 1cd6c6e | 2013-03-18 12:56:33 -0700 | [diff] [blame] | 5217 | final boolean required = pkg.requestedPermissionsRequired.get(i); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5218 | final BasePermission bp = mSettings.mPermissions.get(name); |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 5219 | if (DEBUG_INSTALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5220 | if (gp != ps) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 5221 | Log.i(TAG, "Package " + pkg.packageName + " checking " + name + ": " + bp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5222 | } |
| 5223 | } |
Nick Kralevich | daae541 | 2013-04-01 13:12:26 -0700 | [diff] [blame] | 5224 | |
| 5225 | if (bp == null || bp.packageSetting == null) { |
| 5226 | Slog.w(TAG, "Unknown permission " + name |
| 5227 | + " in package " + pkg.packageName); |
| 5228 | continue; |
| 5229 | } |
| 5230 | |
| 5231 | final String perm = bp.name; |
| 5232 | boolean allowed; |
| 5233 | boolean allowedSig = false; |
| 5234 | final int level = bp.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE; |
| 5235 | if (level == PermissionInfo.PROTECTION_NORMAL |
| 5236 | || level == PermissionInfo.PROTECTION_DANGEROUS) { |
Nick Kralevich | 6079285 | 2013-04-03 14:12:24 -0700 | [diff] [blame] | 5237 | // We grant a normal or dangerous permission if any of the following |
| 5238 | // are true: |
| 5239 | // 1) The permission is required |
| 5240 | // 2) The permission is optional, but was granted in the past |
| 5241 | // 3) The permission is optional, but was requested by an |
| 5242 | // app in /system (not /data) |
| 5243 | // |
| 5244 | // Otherwise, reject the permission. |
| 5245 | allowed = (required || origPermissions.contains(perm) |
| 5246 | || (isSystemApp(ps) && !isUpdatedSystemApp(ps))); |
Nick Kralevich | daae541 | 2013-04-01 13:12:26 -0700 | [diff] [blame] | 5247 | } else if (bp.packageSetting == null) { |
| 5248 | // This permission is invalid; skip it. |
| 5249 | allowed = false; |
| 5250 | } else if (level == PermissionInfo.PROTECTION_SIGNATURE) { |
Nick Kralevich | 94b8c93 | 2013-04-02 09:27:27 -0700 | [diff] [blame] | 5251 | allowed = grantSignaturePermission(perm, pkg, bp, origPermissions); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5252 | if (allowed) { |
Nick Kralevich | daae541 | 2013-04-01 13:12:26 -0700 | [diff] [blame] | 5253 | allowedSig = true; |
| 5254 | } |
| 5255 | } else { |
| 5256 | allowed = false; |
| 5257 | } |
| 5258 | if (DEBUG_INSTALL) { |
| 5259 | if (gp != ps) { |
| 5260 | Log.i(TAG, "Package " + pkg.packageName + " granting " + perm); |
| 5261 | } |
| 5262 | } |
| 5263 | if (allowed) { |
Nick Kralevich | 6079285 | 2013-04-03 14:12:24 -0700 | [diff] [blame] | 5264 | if (!isSystemApp(ps) && ps.permissionsFixed) { |
Nick Kralevich | daae541 | 2013-04-01 13:12:26 -0700 | [diff] [blame] | 5265 | // If this is an existing, non-system package, then |
| 5266 | // we can't add any new permissions to it. |
| 5267 | if (!allowedSig && !gp.grantedPermissions.contains(perm)) { |
Nick Kralevich | daae541 | 2013-04-01 13:12:26 -0700 | [diff] [blame] | 5268 | // Except... if this is a permission that was added |
| 5269 | // to the platform (note: need to only do this when |
| 5270 | // updating the platform). |
Nick Kralevich | 3337dbf | 2013-04-01 13:27:30 -0700 | [diff] [blame] | 5271 | allowed = isNewPlatformPermissionForPackage(perm, pkg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5272 | } |
Nick Kralevich | daae541 | 2013-04-01 13:12:26 -0700 | [diff] [blame] | 5273 | } |
| 5274 | if (allowed) { |
| 5275 | if (!gp.grantedPermissions.contains(perm)) { |
| 5276 | changedPermission = true; |
| 5277 | gp.grantedPermissions.add(perm); |
| 5278 | gp.gids = appendInts(gp.gids, bp.gids); |
| 5279 | } else if (!ps.haveGids) { |
| 5280 | gp.gids = appendInts(gp.gids, bp.gids); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5281 | } |
| 5282 | } else { |
Nick Kralevich | daae541 | 2013-04-01 13:12:26 -0700 | [diff] [blame] | 5283 | Slog.w(TAG, "Not granting permission " + perm |
| 5284 | + " to package " + pkg.packageName |
| 5285 | + " because it was previously installed without"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5286 | } |
| 5287 | } else { |
Nick Kralevich | daae541 | 2013-04-01 13:12:26 -0700 | [diff] [blame] | 5288 | if (gp.grantedPermissions.remove(perm)) { |
| 5289 | changedPermission = true; |
| 5290 | gp.gids = removeInts(gp.gids, bp.gids); |
| 5291 | Slog.i(TAG, "Un-granting permission " + perm |
| 5292 | + " from package " + pkg.packageName |
| 5293 | + " (protectionLevel=" + bp.protectionLevel |
| 5294 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 5295 | + ")"); |
| 5296 | } else { |
| 5297 | Slog.w(TAG, "Not granting permission " + perm |
| 5298 | + " to package " + pkg.packageName |
| 5299 | + " (protectionLevel=" + bp.protectionLevel |
| 5300 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 5301 | + ")"); |
| 5302 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5303 | } |
| 5304 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5305 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5306 | if ((changedPermission || replace) && !ps.permissionsFixed && |
Nick Kralevich | 6079285 | 2013-04-03 14:12:24 -0700 | [diff] [blame] | 5307 | !isSystemApp(ps) || isUpdatedSystemApp(ps)){ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5308 | // This is the first that we have heard about this package, so the |
| 5309 | // permissions we have now selected are fixed until explicitly |
| 5310 | // changed. |
| 5311 | ps.permissionsFixed = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5312 | } |
Dianne Hackborn | bbfd58a | 2010-03-23 11:49:59 -0700 | [diff] [blame] | 5313 | ps.haveGids = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5314 | } |
Nick Kralevich | 35bee33d | 2013-04-01 13:08:00 -0700 | [diff] [blame] | 5315 | |
Nick Kralevich | 3337dbf | 2013-04-01 13:27:30 -0700 | [diff] [blame] | 5316 | private boolean isNewPlatformPermissionForPackage(String perm, PackageParser.Package pkg) { |
| 5317 | boolean allowed = false; |
| 5318 | final int NP = PackageParser.NEW_PERMISSIONS.length; |
| 5319 | for (int ip=0; ip<NP; ip++) { |
| 5320 | final PackageParser.NewPermissionInfo npi |
| 5321 | = PackageParser.NEW_PERMISSIONS[ip]; |
| 5322 | if (npi.name.equals(perm) |
| 5323 | && pkg.applicationInfo.targetSdkVersion < npi.sdkVersion) { |
| 5324 | allowed = true; |
| 5325 | Log.i(TAG, "Auto-granting " + perm + " to old pkg " |
| 5326 | + pkg.packageName); |
| 5327 | break; |
| 5328 | } |
| 5329 | } |
| 5330 | return allowed; |
| 5331 | } |
| 5332 | |
Nick Kralevich | 94b8c93 | 2013-04-02 09:27:27 -0700 | [diff] [blame] | 5333 | private boolean grantSignaturePermission(String perm, PackageParser.Package pkg, |
Nick Kralevich | 35bee33d | 2013-04-01 13:08:00 -0700 | [diff] [blame] | 5334 | BasePermission bp, HashSet<String> origPermissions) { |
| 5335 | boolean allowed; |
| 5336 | allowed = (compareSignatures( |
| 5337 | bp.packageSetting.signatures.mSignatures, pkg.mSignatures) |
| 5338 | == PackageManager.SIGNATURE_MATCH) |
| 5339 | || (compareSignatures(mPlatformPackage.mSignatures, pkg.mSignatures) |
| 5340 | == PackageManager.SIGNATURE_MATCH); |
| 5341 | if (!allowed && (bp.protectionLevel |
| 5342 | & PermissionInfo.PROTECTION_FLAG_SYSTEM) != 0) { |
| 5343 | if (isSystemApp(pkg)) { |
| 5344 | // For updated system applications, a system permission |
| 5345 | // is granted only if it had been defined by the original application. |
| 5346 | if (isUpdatedSystemApp(pkg)) { |
| 5347 | final PackageSetting sysPs = mSettings |
| 5348 | .getDisabledSystemPkgLPr(pkg.packageName); |
| 5349 | final GrantedPermissions origGp = sysPs.sharedUser != null |
| 5350 | ? sysPs.sharedUser : sysPs; |
| 5351 | if (origGp.grantedPermissions.contains(perm)) { |
| 5352 | allowed = true; |
| 5353 | } else { |
| 5354 | // The system apk may have been updated with an older |
| 5355 | // version of the one on the data partition, but which |
| 5356 | // granted a new system permission that it didn't have |
| 5357 | // before. In this case we do want to allow the app to |
| 5358 | // now get the new permission, because it is allowed by |
| 5359 | // the system image. |
| 5360 | allowed = false; |
| 5361 | if (sysPs.pkg != null) { |
| 5362 | for (int j=0; |
| 5363 | j<sysPs.pkg.requestedPermissions.size(); j++) { |
| 5364 | if (perm.equals( |
| 5365 | sysPs.pkg.requestedPermissions.get(j))) { |
| 5366 | allowed = true; |
| 5367 | break; |
| 5368 | } |
| 5369 | } |
| 5370 | } |
| 5371 | } |
| 5372 | } else { |
| 5373 | allowed = true; |
| 5374 | } |
| 5375 | } |
| 5376 | } |
| 5377 | if (!allowed && (bp.protectionLevel |
| 5378 | & PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) { |
| 5379 | // For development permissions, a development permission |
| 5380 | // is granted only if it was already granted. |
| 5381 | allowed = origPermissions.contains(perm); |
| 5382 | } |
| 5383 | return allowed; |
| 5384 | } |
| 5385 | |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 5386 | final class ActivityIntentResolver |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5387 | extends IntentResolver<PackageParser.ActivityIntentInfo, ResolveInfo> { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 5388 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5389 | boolean defaultOnly, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5390 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5391 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5392 | return super.queryIntent(intent, resolvedType, defaultOnly, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5393 | } |
| 5394 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5395 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, int flags, |
| 5396 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5397 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5398 | mFlags = flags; |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 5399 | return super.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5400 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5401 | } |
| 5402 | |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 5403 | public List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5404 | int flags, ArrayList<PackageParser.Activity> packageActivities, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5405 | if (!sUserManager.exists(userId)) return null; |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 5406 | if (packageActivities == null) { |
| 5407 | return null; |
| 5408 | } |
| 5409 | mFlags = flags; |
| 5410 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 5411 | final int N = packageActivities.size(); |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 5412 | ArrayList<PackageParser.ActivityIntentInfo[]> listCut = |
| 5413 | new ArrayList<PackageParser.ActivityIntentInfo[]>(N); |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 5414 | |
| 5415 | ArrayList<PackageParser.ActivityIntentInfo> intentFilters; |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 5416 | for (int i = 0; i < N; ++i) { |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 5417 | intentFilters = packageActivities.get(i).intents; |
| 5418 | if (intentFilters != null && intentFilters.size() > 0) { |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 5419 | PackageParser.ActivityIntentInfo[] array = |
| 5420 | new PackageParser.ActivityIntentInfo[intentFilters.size()]; |
| 5421 | intentFilters.toArray(array); |
| 5422 | listCut.add(array); |
Mihai Preda | c3320db | 2009-05-18 20:15:32 +0200 | [diff] [blame] | 5423 | } |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 5424 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5425 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 5426 | } |
| 5427 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5428 | public final void addActivity(PackageParser.Activity a, String type) { |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 5429 | final boolean systemApp = isSystemApp(a.info.applicationInfo); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5430 | mActivities.put(a.getComponentName(), a); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5431 | if (DEBUG_SHOW_INFO) |
| 5432 | Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5433 | TAG, " " + type + " " + |
| 5434 | (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel : a.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5435 | if (DEBUG_SHOW_INFO) |
| 5436 | Log.v(TAG, " Class=" + a.info.name); |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 5437 | final int NI = a.intents.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 5438 | for (int j=0; j<NI; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5439 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 5440 | if (!systemApp && intent.getPriority() > 0 && "activity".equals(type)) { |
| 5441 | intent.setPriority(0); |
| 5442 | Log.w(TAG, "Package " + a.info.applicationInfo.packageName + " has activity " |
| 5443 | + a.className + " with priority > 0, forcing to 0"); |
| 5444 | } |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5445 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5446 | Log.v(TAG, " IntentFilter:"); |
| 5447 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 5448 | } |
| 5449 | if (!intent.debugCheck()) { |
| 5450 | Log.w(TAG, "==> For Activity " + a.info.name); |
| 5451 | } |
| 5452 | addFilter(intent); |
| 5453 | } |
| 5454 | } |
| 5455 | |
| 5456 | public final void removeActivity(PackageParser.Activity a, String type) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5457 | mActivities.remove(a.getComponentName()); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5458 | if (DEBUG_SHOW_INFO) { |
| 5459 | Log.v(TAG, " " + type + " " |
| 5460 | + (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel |
| 5461 | : a.info.name) + ":"); |
| 5462 | Log.v(TAG, " Class=" + a.info.name); |
| 5463 | } |
| 5464 | final int NI = a.intents.size(); |
Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 5465 | for (int j=0; j<NI; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5466 | PackageParser.ActivityIntentInfo intent = a.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5467 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5468 | Log.v(TAG, " IntentFilter:"); |
| 5469 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 5470 | } |
| 5471 | removeFilter(intent); |
| 5472 | } |
| 5473 | } |
| 5474 | |
| 5475 | @Override |
| 5476 | protected boolean allowFilterResult( |
| 5477 | PackageParser.ActivityIntentInfo filter, List<ResolveInfo> dest) { |
| 5478 | ActivityInfo filterAi = filter.activity.info; |
| 5479 | for (int i=dest.size()-1; i>=0; i--) { |
| 5480 | ActivityInfo destAi = dest.get(i).activityInfo; |
| 5481 | if (destAi.name == filterAi.name |
| 5482 | && destAi.packageName == filterAi.packageName) { |
| 5483 | return false; |
| 5484 | } |
| 5485 | } |
| 5486 | return true; |
| 5487 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5489 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 5490 | protected ActivityIntentInfo[] newArray(int size) { |
| 5491 | return new ActivityIntentInfo[size]; |
| 5492 | } |
| 5493 | |
| 5494 | @Override |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5495 | protected boolean isFilterStopped(PackageParser.ActivityIntentInfo filter, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5496 | if (!sUserManager.exists(userId)) return true; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5497 | PackageParser.Package p = filter.activity.owner; |
| 5498 | if (p != null) { |
| 5499 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 5500 | if (ps != null) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 5501 | // System apps are never considered stopped for purposes of |
| 5502 | // filtering, because there may be no way for the user to |
| 5503 | // actually re-launch them. |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5504 | return (ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0 |
| 5505 | && ps.getStopped(userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5506 | } |
| 5507 | } |
| 5508 | return false; |
| 5509 | } |
| 5510 | |
| 5511 | @Override |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 5512 | protected boolean isPackageForFilter(String packageName, |
| 5513 | PackageParser.ActivityIntentInfo info) { |
| 5514 | return packageName.equals(info.activity.owner.packageName); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5518 | protected ResolveInfo newResult(PackageParser.ActivityIntentInfo info, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5519 | int match, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5520 | if (!sUserManager.exists(userId)) return null; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5521 | if (!mSettings.isEnabledLPr(info.activity.info, mFlags, userId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5522 | return null; |
| 5523 | } |
| 5524 | final PackageParser.Activity activity = info.activity; |
| 5525 | if (mSafeMode && (activity.info.applicationInfo.flags |
| 5526 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 5527 | return null; |
| 5528 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5529 | PackageSetting ps = (PackageSetting) activity.owner.mExtras; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5530 | if (ps == null) { |
| 5531 | return null; |
| 5532 | } |
| 5533 | ActivityInfo ai = PackageParser.generateActivityInfo(activity, mFlags, |
| 5534 | ps.readUserState(userId), userId); |
| 5535 | if (ai == null) { |
| 5536 | return null; |
| 5537 | } |
| 5538 | final ResolveInfo res = new ResolveInfo(); |
| 5539 | res.activityInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5540 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 5541 | res.filter = info; |
| 5542 | } |
| 5543 | res.priority = info.getPriority(); |
| 5544 | res.preferredOrder = activity.owner.mPreferredOrder; |
| 5545 | //System.out.println("Result: " + res.activityInfo.className + |
| 5546 | // " = " + res.priority); |
| 5547 | res.match = match; |
| 5548 | res.isDefault = info.hasDefault; |
| 5549 | res.labelRes = info.labelRes; |
| 5550 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 5551 | res.icon = info.icon; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 5552 | res.system = isSystemApp(res.activityInfo.applicationInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5553 | return res; |
| 5554 | } |
| 5555 | |
| 5556 | @Override |
| 5557 | protected void sortResults(List<ResolveInfo> results) { |
| 5558 | Collections.sort(results, mResolvePrioritySorter); |
| 5559 | } |
| 5560 | |
| 5561 | @Override |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5562 | protected void dumpFilter(PrintWriter out, String prefix, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5563 | PackageParser.ActivityIntentInfo filter) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5564 | out.print(prefix); out.print( |
| 5565 | Integer.toHexString(System.identityHashCode(filter.activity))); |
| 5566 | out.print(' '); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5567 | out.print(filter.activity.getComponentShortName()); |
| 5568 | out.print(" filter "); |
| 5569 | out.println(Integer.toHexString(System.identityHashCode(filter))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 5573 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 5574 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 5575 | // while (i.hasNext()) { |
| 5576 | // final ResolveInfo resolveInfo = i.next(); |
| 5577 | // if (isEnabledLP(resolveInfo.activityInfo)) { |
| 5578 | // retList.add(resolveInfo); |
| 5579 | // } |
| 5580 | // } |
| 5581 | // return retList; |
| 5582 | // } |
| 5583 | |
| 5584 | // Keys are String (activity class name), values are Activity. |
| 5585 | private final HashMap<ComponentName, PackageParser.Activity> mActivities |
| 5586 | = new HashMap<ComponentName, PackageParser.Activity>(); |
| 5587 | private int mFlags; |
| 5588 | } |
| 5589 | |
| 5590 | private final class ServiceIntentResolver |
| 5591 | extends IntentResolver<PackageParser.ServiceIntentInfo, ResolveInfo> { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 5592 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5593 | boolean defaultOnly, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5594 | mFlags = defaultOnly ? PackageManager.MATCH_DEFAULT_ONLY : 0; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5595 | return super.queryIntent(intent, resolvedType, defaultOnly, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5596 | } |
| 5597 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5598 | public List<ResolveInfo> queryIntent(Intent intent, String resolvedType, int flags, |
| 5599 | int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5600 | if (!sUserManager.exists(userId)) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5601 | mFlags = flags; |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 5602 | return super.queryIntent(intent, resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5603 | (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5604 | } |
| 5605 | |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 5606 | public List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5607 | int flags, ArrayList<PackageParser.Service> packageServices, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5608 | if (!sUserManager.exists(userId)) return null; |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 5609 | if (packageServices == null) { |
| 5610 | return null; |
| 5611 | } |
| 5612 | mFlags = flags; |
| 5613 | final boolean defaultOnly = (flags&PackageManager.MATCH_DEFAULT_ONLY) != 0; |
Kenny Root | 62cc690 | 2011-02-23 16:49:21 -0800 | [diff] [blame] | 5614 | final int N = packageServices.size(); |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 5615 | ArrayList<PackageParser.ServiceIntentInfo[]> listCut = |
| 5616 | new ArrayList<PackageParser.ServiceIntentInfo[]>(N); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 5617 | |
| 5618 | ArrayList<PackageParser.ServiceIntentInfo> intentFilters; |
| 5619 | for (int i = 0; i < N; ++i) { |
| 5620 | intentFilters = packageServices.get(i).intents; |
| 5621 | if (intentFilters != null && intentFilters.size() > 0) { |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 5622 | PackageParser.ServiceIntentInfo[] array = |
| 5623 | new PackageParser.ServiceIntentInfo[intentFilters.size()]; |
| 5624 | intentFilters.toArray(array); |
| 5625 | listCut.add(array); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 5626 | } |
| 5627 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5628 | return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId); |
Dianne Hackborn | c14b9cc | 2009-06-17 18:02:12 -0700 | [diff] [blame] | 5629 | } |
| 5630 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5631 | public final void addService(PackageParser.Service s) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5632 | mServices.put(s.getComponentName(), s); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5633 | if (DEBUG_SHOW_INFO) { |
| 5634 | Log.v(TAG, " " |
| 5635 | + (s.info.nonLocalizedLabel != null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5636 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5637 | Log.v(TAG, " Class=" + s.info.name); |
| 5638 | } |
| 5639 | final int NI = s.intents.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5640 | int j; |
| 5641 | for (j=0; j<NI; j++) { |
| 5642 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5643 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5644 | Log.v(TAG, " IntentFilter:"); |
| 5645 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 5646 | } |
| 5647 | if (!intent.debugCheck()) { |
| 5648 | Log.w(TAG, "==> For Service " + s.info.name); |
| 5649 | } |
| 5650 | addFilter(intent); |
| 5651 | } |
| 5652 | } |
| 5653 | |
| 5654 | public final void removeService(PackageParser.Service s) { |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 5655 | mServices.remove(s.getComponentName()); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5656 | if (DEBUG_SHOW_INFO) { |
| 5657 | Log.v(TAG, " " + (s.info.nonLocalizedLabel != null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5658 | ? s.info.nonLocalizedLabel : s.info.name) + ":"); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5659 | Log.v(TAG, " Class=" + s.info.name); |
| 5660 | } |
| 5661 | final int NI = s.intents.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5662 | int j; |
| 5663 | for (j=0; j<NI; j++) { |
| 5664 | PackageParser.ServiceIntentInfo intent = s.intents.get(j); |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5665 | if (DEBUG_SHOW_INFO) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5666 | Log.v(TAG, " IntentFilter:"); |
| 5667 | intent.dump(new LogPrinter(Log.VERBOSE, TAG), " "); |
| 5668 | } |
| 5669 | removeFilter(intent); |
| 5670 | } |
| 5671 | } |
| 5672 | |
| 5673 | @Override |
| 5674 | protected boolean allowFilterResult( |
| 5675 | PackageParser.ServiceIntentInfo filter, List<ResolveInfo> dest) { |
| 5676 | ServiceInfo filterSi = filter.service.info; |
| 5677 | for (int i=dest.size()-1; i>=0; i--) { |
| 5678 | ServiceInfo destAi = dest.get(i).serviceInfo; |
| 5679 | if (destAi.name == filterSi.name |
| 5680 | && destAi.packageName == filterSi.packageName) { |
| 5681 | return false; |
| 5682 | } |
| 5683 | } |
| 5684 | return true; |
| 5685 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5686 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5687 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 5688 | protected PackageParser.ServiceIntentInfo[] newArray(int size) { |
| 5689 | return new PackageParser.ServiceIntentInfo[size]; |
| 5690 | } |
| 5691 | |
| 5692 | @Override |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5693 | protected boolean isFilterStopped(PackageParser.ServiceIntentInfo filter, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5694 | if (!sUserManager.exists(userId)) return true; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5695 | PackageParser.Package p = filter.service.owner; |
| 5696 | if (p != null) { |
| 5697 | PackageSetting ps = (PackageSetting)p.mExtras; |
| 5698 | if (ps != null) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 5699 | // System apps are never considered stopped for purposes of |
| 5700 | // filtering, because there may be no way for the user to |
| 5701 | // actually re-launch them. |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5702 | return (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0 |
| 5703 | && ps.getStopped(userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5704 | } |
| 5705 | } |
| 5706 | return false; |
| 5707 | } |
| 5708 | |
| 5709 | @Override |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 5710 | protected boolean isPackageForFilter(String packageName, |
| 5711 | PackageParser.ServiceIntentInfo info) { |
| 5712 | return packageName.equals(info.service.owner.packageName); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5713 | } |
| 5714 | |
| 5715 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5716 | protected ResolveInfo newResult(PackageParser.ServiceIntentInfo filter, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5717 | int match, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5718 | if (!sUserManager.exists(userId)) return null; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 5719 | final PackageParser.ServiceIntentInfo info = (PackageParser.ServiceIntentInfo)filter; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5720 | if (!mSettings.isEnabledLPr(info.service.info, mFlags, userId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5721 | return null; |
| 5722 | } |
| 5723 | final PackageParser.Service service = info.service; |
| 5724 | if (mSafeMode && (service.info.applicationInfo.flags |
| 5725 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 5726 | return null; |
| 5727 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5728 | PackageSetting ps = (PackageSetting) service.owner.mExtras; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5729 | if (ps == null) { |
| 5730 | return null; |
| 5731 | } |
| 5732 | ServiceInfo si = PackageParser.generateServiceInfo(service, mFlags, |
| 5733 | ps.readUserState(userId), userId); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 5734 | if (si == null) { |
| 5735 | return null; |
| 5736 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5737 | final ResolveInfo res = new ResolveInfo(); |
| 5738 | res.serviceInfo = si; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5739 | if ((mFlags&PackageManager.GET_RESOLVED_FILTER) != 0) { |
| 5740 | res.filter = filter; |
| 5741 | } |
| 5742 | res.priority = info.getPriority(); |
| 5743 | res.preferredOrder = service.owner.mPreferredOrder; |
| 5744 | //System.out.println("Result: " + res.activityInfo.className + |
| 5745 | // " = " + res.priority); |
| 5746 | res.match = match; |
| 5747 | res.isDefault = info.hasDefault; |
| 5748 | res.labelRes = info.labelRes; |
| 5749 | res.nonLocalizedLabel = info.nonLocalizedLabel; |
| 5750 | res.icon = info.icon; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 5751 | res.system = isSystemApp(res.serviceInfo.applicationInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5752 | return res; |
| 5753 | } |
| 5754 | |
| 5755 | @Override |
| 5756 | protected void sortResults(List<ResolveInfo> results) { |
| 5757 | Collections.sort(results, mResolvePrioritySorter); |
| 5758 | } |
| 5759 | |
| 5760 | @Override |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5761 | protected void dumpFilter(PrintWriter out, String prefix, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5762 | PackageParser.ServiceIntentInfo filter) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 5763 | out.print(prefix); out.print( |
| 5764 | Integer.toHexString(System.identityHashCode(filter.service))); |
| 5765 | out.print(' '); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 5766 | out.print(filter.service.getComponentShortName()); |
| 5767 | out.print(" filter "); |
| 5768 | out.println(Integer.toHexString(System.identityHashCode(filter))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | // List<ResolveInfo> filterEnabled(List<ResolveInfo> resolveInfoList) { |
| 5772 | // final Iterator<ResolveInfo> i = resolveInfoList.iterator(); |
| 5773 | // final List<ResolveInfo> retList = Lists.newArrayList(); |
| 5774 | // while (i.hasNext()) { |
| 5775 | // final ResolveInfo resolveInfo = (ResolveInfo) i; |
| 5776 | // if (isEnabledLP(resolveInfo.serviceInfo)) { |
| 5777 | // retList.add(resolveInfo); |
| 5778 | // } |
| 5779 | // } |
| 5780 | // return retList; |
| 5781 | // } |
| 5782 | |
| 5783 | // Keys are String (activity class name), values are Activity. |
| 5784 | private final HashMap<ComponentName, PackageParser.Service> mServices |
| 5785 | = new HashMap<ComponentName, PackageParser.Service>(); |
| 5786 | private int mFlags; |
| 5787 | }; |
| 5788 | |
| 5789 | private static final Comparator<ResolveInfo> mResolvePrioritySorter = |
| 5790 | new Comparator<ResolveInfo>() { |
| 5791 | public int compare(ResolveInfo r1, ResolveInfo r2) { |
| 5792 | int v1 = r1.priority; |
| 5793 | int v2 = r2.priority; |
| 5794 | //System.out.println("Comparing: q1=" + q1 + " q2=" + q2); |
| 5795 | if (v1 != v2) { |
| 5796 | return (v1 > v2) ? -1 : 1; |
| 5797 | } |
| 5798 | v1 = r1.preferredOrder; |
| 5799 | v2 = r2.preferredOrder; |
| 5800 | if (v1 != v2) { |
| 5801 | return (v1 > v2) ? -1 : 1; |
| 5802 | } |
| 5803 | if (r1.isDefault != r2.isDefault) { |
| 5804 | return r1.isDefault ? -1 : 1; |
| 5805 | } |
| 5806 | v1 = r1.match; |
| 5807 | v2 = r2.match; |
| 5808 | //System.out.println("Comparing: m1=" + m1 + " m2=" + m2); |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 5809 | if (v1 != v2) { |
| 5810 | return (v1 > v2) ? -1 : 1; |
| 5811 | } |
| 5812 | if (r1.system != r2.system) { |
| 5813 | return r1.system ? -1 : 1; |
| 5814 | } |
| 5815 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5816 | } |
| 5817 | }; |
| 5818 | |
| 5819 | private static final Comparator<ProviderInfo> mProviderInitOrderSorter = |
| 5820 | new Comparator<ProviderInfo>() { |
| 5821 | public int compare(ProviderInfo p1, ProviderInfo p2) { |
| 5822 | final int v1 = p1.initOrder; |
| 5823 | final int v2 = p2.initOrder; |
| 5824 | return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0); |
| 5825 | } |
| 5826 | }; |
| 5827 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5828 | static final void sendPackageBroadcast(String action, String pkg, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5829 | Bundle extras, String targetPkg, IIntentReceiver finishedReceiver, |
| 5830 | int[] userIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5831 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 5832 | if (am != null) { |
| 5833 | try { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5834 | if (userIds == null) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 5835 | userIds = am.getRunningUserIds(); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5836 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5837 | for (int id : userIds) { |
| 5838 | final Intent intent = new Intent(action, |
| 5839 | pkg != null ? Uri.fromParts("package", pkg, null) : null); |
| 5840 | if (extras != null) { |
| 5841 | intent.putExtras(extras); |
| 5842 | } |
| 5843 | if (targetPkg != null) { |
| 5844 | intent.setPackage(targetPkg); |
| 5845 | } |
| 5846 | // Modify the UID when posting to other users |
| 5847 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5848 | if (uid > 0 && UserHandle.getUserId(uid) != id) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5849 | uid = UserHandle.getUid(id, UserHandle.getAppId(uid)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5850 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 5851 | } |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 5852 | intent.putExtra(Intent.EXTRA_USER_HANDLE, id); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5853 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5854 | if (DEBUG_BROADCASTS) { |
| 5855 | RuntimeException here = new RuntimeException("here"); |
| 5856 | here.fillInStackTrace(); |
| 5857 | Slog.d(TAG, "Sending to user " + id + ": " |
| 5858 | + intent.toShortString(false, true, false, false) |
| 5859 | + " " + intent.getExtras(), here); |
| 5860 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 5861 | am.broadcastIntent(null, intent, null, finishedReceiver, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 5862 | 0, null, null, null, android.app.AppOpsManager.OP_NONE, |
| 5863 | finishedReceiver != null, false, id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5864 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5865 | } catch (RemoteException ex) { |
| 5866 | } |
| 5867 | } |
| 5868 | } |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5869 | |
| 5870 | /** |
| 5871 | * Check if the external storage media is available. This is true if there |
| 5872 | * is a mounted external storage medium or if the external storage is |
| 5873 | * emulated. |
| 5874 | */ |
| 5875 | private boolean isExternalMediaAvailable() { |
| 5876 | return mMediaMounted || Environment.isExternalStorageEmulated(); |
| 5877 | } |
| 5878 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5879 | public PackageCleanItem nextPackageToClean(PackageCleanItem lastPackage) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5880 | // writer |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5881 | synchronized (mPackages) { |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5882 | if (!isExternalMediaAvailable()) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5883 | // If the external storage is no longer mounted at this point, |
| 5884 | // the caller may not have been able to delete all of this |
| 5885 | // packages files and can not delete any more. Bail. |
| 5886 | return null; |
| 5887 | } |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 5888 | final ArrayList<PackageCleanItem> pkgs = mSettings.mPackagesToBeCleaned; |
| 5889 | if (lastPackage != null) { |
| 5890 | pkgs.remove(lastPackage); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5891 | } |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 5892 | if (pkgs.size() > 0) { |
| 5893 | return pkgs.get(0); |
| 5894 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5895 | } |
| 5896 | return null; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5897 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5898 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 5899 | void schedulePackageCleaning(String packageName, int userId, boolean andCode) { |
| 5900 | if (false) { |
| 5901 | RuntimeException here = new RuntimeException("here"); |
| 5902 | here.fillInStackTrace(); |
| 5903 | Slog.d(TAG, "Schedule cleaning " + packageName + " user=" + userId |
| 5904 | + " andCode=" + andCode, here); |
| 5905 | } |
| 5906 | mHandler.sendMessage(mHandler.obtainMessage(START_CLEANING_PACKAGE, |
| 5907 | userId, andCode ? 1 : 0, packageName)); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5908 | } |
| 5909 | |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 5910 | void startCleaningPackages() { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5911 | // reader |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5912 | synchronized (mPackages) { |
Kenny Root | 300c13a | 2011-01-18 13:04:40 -0800 | [diff] [blame] | 5913 | if (!isExternalMediaAvailable()) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5914 | return; |
| 5915 | } |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 5916 | if (mSettings.mPackagesToBeCleaned.isEmpty()) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5917 | return; |
| 5918 | } |
| 5919 | } |
| 5920 | Intent intent = new Intent(PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE); |
| 5921 | intent.setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 5922 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 5923 | if (am != null) { |
| 5924 | try { |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 5925 | am.startService(null, intent, null, UserHandle.USER_OWNER); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 5926 | } catch (RemoteException e) { |
| 5927 | } |
| 5928 | } |
| 5929 | } |
| 5930 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5931 | private final class AppDirObserver extends FileObserver { |
| 5932 | public AppDirObserver(String path, int mask, boolean isrom) { |
| 5933 | super(path, mask); |
| 5934 | mRootDir = path; |
| 5935 | mIsRom = isrom; |
| 5936 | } |
| 5937 | |
| 5938 | public void onEvent(int event, String path) { |
| 5939 | String removedPackage = null; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5940 | int removedAppId = -1; |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 5941 | int[] removedUsers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5942 | String addedPackage = null; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5943 | int addedAppId = -1; |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 5944 | int[] addedUsers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5945 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5946 | // 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] | 5947 | synchronized (mInstallLock) { |
| 5948 | String fullPathStr = null; |
| 5949 | File fullPath = null; |
| 5950 | if (path != null) { |
| 5951 | fullPath = new File(mRootDir, path); |
| 5952 | fullPathStr = fullPath.getPath(); |
| 5953 | } |
| 5954 | |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5955 | if (DEBUG_APP_DIR_OBSERVER) |
| 5956 | Log.v(TAG, "File " + fullPathStr + " changed: " + Integer.toHexString(event)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5957 | |
| 5958 | if (!isPackageFilename(path)) { |
Kenny Root | 9718cf5 | 2011-02-23 16:45:26 -0800 | [diff] [blame] | 5959 | if (DEBUG_APP_DIR_OBSERVER) |
| 5960 | 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] | 5961 | return; |
| 5962 | } |
| 5963 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5964 | // Ignore packages that are being installed or |
| 5965 | // have just been installed. |
| 5966 | if (ignoreCodePath(fullPathStr)) { |
| 5967 | return; |
| 5968 | } |
| 5969 | PackageParser.Package p = null; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5970 | PackageSetting ps = null; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 5971 | // reader |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5972 | synchronized (mPackages) { |
| 5973 | p = mAppDirs.get(fullPathStr); |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 5974 | if (p != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5975 | ps = mSettings.mPackages.get(p.applicationInfo.packageName); |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 5976 | if (ps != null) { |
| 5977 | removedUsers = ps.queryInstalledUsers(sUserManager.getUserIds(), true); |
| 5978 | } else { |
| 5979 | removedUsers = sUserManager.getUserIds(); |
| 5980 | } |
| 5981 | } |
| 5982 | addedUsers = sUserManager.getUserIds(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 5983 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5984 | if ((event&REMOVE_EVENTS) != 0) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5985 | if (ps != null) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 5986 | if (DEBUG_REMOVE) Slog.d(TAG, "Package disappeared: " + ps); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 5987 | removePackageLI(ps, true); |
| 5988 | removedPackage = ps.name; |
| 5989 | removedAppId = ps.appId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
| 5993 | if ((event&ADD_EVENTS) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5994 | if (p == null) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 5995 | if (DEBUG_INSTALL) Slog.d(TAG, "New file appeared: " + fullPath); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 5996 | p = scanPackageLI(fullPath, |
Dianne Hackborn | 806da1d | 2010-03-18 16:50:07 -0700 | [diff] [blame] | 5997 | (mIsRom ? PackageParser.PARSE_IS_SYSTEM |
| 5998 | | PackageParser.PARSE_IS_SYSTEM_DIR: 0) | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5999 | PackageParser.PARSE_CHATTY | |
| 6000 | PackageParser.PARSE_MUST_BE_APK, |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 6001 | SCAN_MONITOR | SCAN_NO_PATHS | SCAN_UPDATE_TIME, |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 6002 | System.currentTimeMillis(), UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6003 | if (p != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6004 | /* |
| 6005 | * TODO this seems dangerous as the package may have |
| 6006 | * changed since we last acquired the mPackages |
| 6007 | * lock. |
| 6008 | */ |
| 6009 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6010 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6011 | updatePermissionsLPw(p.packageName, p, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 6012 | p.permissions.size() > 0 ? UPDATE_PERMISSIONS_ALL : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6013 | } |
| 6014 | addedPackage = p.applicationInfo.packageName; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6015 | addedAppId = UserHandle.getAppId(p.applicationInfo.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6016 | } |
| 6017 | } |
| 6018 | } |
| 6019 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6020 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6021 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6022 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6023 | } |
| 6024 | } |
| 6025 | |
| 6026 | if (removedPackage != null) { |
| 6027 | Bundle extras = new Bundle(1); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6028 | extras.putInt(Intent.EXTRA_UID, removedAppId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6029 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, false); |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 6030 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 6031 | extras, null, null, removedUsers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6032 | } |
| 6033 | if (addedPackage != null) { |
| 6034 | Bundle extras = new Bundle(1); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6035 | extras.putInt(Intent.EXTRA_UID, addedAppId); |
Dianne Hackborn | ecb0e63 | 2010-04-07 20:22:55 -0700 | [diff] [blame] | 6036 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, addedPackage, |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 6037 | extras, null, null, addedUsers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6038 | } |
| 6039 | } |
| 6040 | |
| 6041 | private final String mRootDir; |
| 6042 | private final boolean mIsRom; |
| 6043 | } |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6044 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6045 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 6046 | public void installPackage( |
| 6047 | final Uri packageURI, final IPackageInstallObserver observer, final int flags) { |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6048 | installPackage(packageURI, observer, flags, null); |
| 6049 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6050 | |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 6051 | /* Called when a downloaded package installation has been confirmed by the user */ |
| 6052 | public void installPackage( |
| 6053 | final Uri packageURI, final IPackageInstallObserver observer, final int flags, |
| 6054 | final String installerPackageName) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6055 | installPackageWithVerification(packageURI, observer, flags, installerPackageName, null, |
Rich Cannings | e1d7c71 | 2012-08-08 12:46:06 -0700 | [diff] [blame] | 6056 | null, null); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6057 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6058 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6059 | @Override |
| 6060 | public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer, |
| 6061 | int flags, String installerPackageName, Uri verificationURI, |
Rich Cannings | e1d7c71 | 2012-08-08 12:46:06 -0700 | [diff] [blame] | 6062 | ManifestDigest manifestDigest, ContainerEncryptionParams encryptionParams) { |
rich cannings | 706e8ba | 2012-08-20 13:20:14 -0700 | [diff] [blame] | 6063 | VerificationParams verificationParams = new VerificationParams(verificationURI, null, null, |
Ben Gruver | 37d83a3 | 2012-09-27 13:02:06 -0700 | [diff] [blame] | 6064 | VerificationParams.NO_UID, manifestDigest); |
rich cannings | 706e8ba | 2012-08-20 13:20:14 -0700 | [diff] [blame] | 6065 | installPackageWithVerificationAndEncryption(packageURI, observer, flags, |
| 6066 | installerPackageName, verificationParams, encryptionParams); |
| 6067 | } |
| 6068 | |
| 6069 | public void installPackageWithVerificationAndEncryption(Uri packageURI, |
| 6070 | IPackageInstallObserver observer, int flags, String installerPackageName, |
| 6071 | VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) { |
| 6072 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, |
| 6073 | null); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6074 | |
| 6075 | final int uid = Binder.getCallingUid(); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 6076 | if (isUserRestricted(UserHandle.getUserId(uid), UserManager.DISALLOW_INSTALL_APPS)) { |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 6077 | try { |
| 6078 | observer.packageInstalled("", PackageManager.INSTALL_FAILED_USER_RESTRICTED); |
| 6079 | } catch (RemoteException re) { |
| 6080 | } |
| 6081 | return; |
| 6082 | } |
| 6083 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 6084 | UserHandle user; |
| 6085 | if ((flags&PackageManager.INSTALL_ALL_USERS) != 0) { |
| 6086 | user = UserHandle.ALL; |
| 6087 | } else { |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 6088 | user = new UserHandle(UserHandle.getUserId(uid)); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 6089 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6090 | |
| 6091 | final int filteredFlags; |
| 6092 | |
| 6093 | if (uid == Process.SHELL_UID || uid == 0) { |
| 6094 | if (DEBUG_INSTALL) { |
| 6095 | Slog.v(TAG, "Install from ADB"); |
| 6096 | } |
| 6097 | filteredFlags = flags | PackageManager.INSTALL_FROM_ADB; |
| 6098 | } else { |
| 6099 | filteredFlags = flags & ~PackageManager.INSTALL_FROM_ADB; |
| 6100 | } |
| 6101 | |
rich cannings | 13d428e | 2012-09-13 13:43:07 -0700 | [diff] [blame] | 6102 | verificationParams.setInstallerUid(uid); |
| 6103 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6104 | final Message msg = mHandler.obtainMessage(INIT_COPY); |
| 6105 | msg.obj = new InstallParams(packageURI, observer, filteredFlags, installerPackageName, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 6106 | verificationParams, encryptionParams, user); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6107 | mHandler.sendMessage(msg); |
| 6108 | } |
| 6109 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 6110 | /** |
| 6111 | * @hide |
| 6112 | */ |
| 6113 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 6114 | public int installExistingPackageAsUser(String packageName, int userId) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 6115 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, |
| 6116 | null); |
| 6117 | PackageSetting pkgSetting; |
| 6118 | final int uid = Binder.getCallingUid(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 6119 | if (UserHandle.getUserId(uid) != userId) { |
| 6120 | mContext.enforceCallingPermission( |
| 6121 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 6122 | "installExistingPackage for user " + userId); |
| 6123 | } |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 6124 | if (isUserRestricted(userId, UserManager.DISALLOW_INSTALL_APPS)) { |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 6125 | return PackageManager.INSTALL_FAILED_USER_RESTRICTED; |
| 6126 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 6127 | |
| 6128 | long callingId = Binder.clearCallingIdentity(); |
| 6129 | try { |
| 6130 | boolean sendAdded = false; |
| 6131 | Bundle extras = new Bundle(1); |
| 6132 | |
| 6133 | // writer |
| 6134 | synchronized (mPackages) { |
| 6135 | pkgSetting = mSettings.mPackages.get(packageName); |
| 6136 | if (pkgSetting == null) { |
| 6137 | return PackageManager.INSTALL_FAILED_INVALID_URI; |
| 6138 | } |
| 6139 | if (!pkgSetting.getInstalled(userId)) { |
| 6140 | pkgSetting.setInstalled(true, userId); |
| 6141 | mSettings.writePackageRestrictionsLPr(userId); |
| 6142 | extras.putInt(Intent.EXTRA_UID, UserHandle.getUid(userId, pkgSetting.appId)); |
| 6143 | sendAdded = true; |
| 6144 | } |
| 6145 | } |
| 6146 | |
| 6147 | if (sendAdded) { |
| 6148 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, |
| 6149 | packageName, extras, null, null, new int[] {userId}); |
Christopher Tate | 3335644 | 2013-05-01 14:17:53 -0700 | [diff] [blame] | 6150 | try { |
| 6151 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 6152 | final boolean isSystem = |
| 6153 | isSystemApp(pkgSetting) || isUpdatedSystemApp(pkgSetting); |
| 6154 | if (isSystem && am.isUserRunning(userId, false)) { |
| 6155 | // The just-installed/enabled app is bundled on the system, so presumed |
| 6156 | // to be able to run automatically without needing an explicit launch. |
| 6157 | // Send it a BOOT_COMPLETED if it would ordinarily have gotten one. |
| 6158 | Intent bcIntent = new Intent(Intent.ACTION_BOOT_COMPLETED) |
| 6159 | .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) |
| 6160 | .setPackage(packageName); |
Christopher Tate | 95a9bfe | 2013-04-30 17:40:32 -0700 | [diff] [blame] | 6161 | am.broadcastIntent(null, bcIntent, null, null, 0, null, null, null, |
| 6162 | android.app.AppOpsManager.OP_NONE, false, false, userId); |
Christopher Tate | 95a9bfe | 2013-04-30 17:40:32 -0700 | [diff] [blame] | 6163 | } |
Christopher Tate | 3335644 | 2013-05-01 14:17:53 -0700 | [diff] [blame] | 6164 | } catch (RemoteException e) { |
| 6165 | // shouldn't happen |
| 6166 | Slog.w(TAG, "Unable to bootstrap installed package", e); |
Christopher Tate | 95a9bfe | 2013-04-30 17:40:32 -0700 | [diff] [blame] | 6167 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 6168 | } |
| 6169 | } finally { |
| 6170 | Binder.restoreCallingIdentity(callingId); |
| 6171 | } |
| 6172 | |
| 6173 | return PackageManager.INSTALL_SUCCEEDED; |
| 6174 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 6175 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 6176 | private boolean isUserRestricted(int userId, String restrictionKey) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 6177 | Bundle restrictions = sUserManager.getUserRestrictions(userId); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 6178 | if (restrictions.getBoolean(restrictionKey, false)) { |
| 6179 | Log.w(TAG, "User is restricted: " + restrictionKey); |
| 6180 | return true; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 6181 | } |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 6182 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 6183 | } |
| 6184 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6185 | @Override |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6186 | public void verifyPendingInstall(int id, int verificationCode) throws RemoteException { |
rich cannings | 7e67151 | 2012-08-27 14:44:16 -0700 | [diff] [blame] | 6187 | mContext.enforceCallingOrSelfPermission( |
| 6188 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 6189 | "Only package verification agents can verify applications"); |
| 6190 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6191 | final Message msg = mHandler.obtainMessage(PACKAGE_VERIFIED); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6192 | final PackageVerificationResponse response = new PackageVerificationResponse( |
| 6193 | verificationCode, Binder.getCallingUid()); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6194 | msg.arg1 = id; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6195 | msg.obj = response; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6196 | mHandler.sendMessage(msg); |
| 6197 | } |
| 6198 | |
rich cannings | d9ef3e5 | 2012-08-22 14:28:05 -0700 | [diff] [blame] | 6199 | @Override |
| 6200 | public void extendVerificationTimeout(int id, int verificationCodeAtTimeout, |
| 6201 | long millisecondsToDelay) { |
rich cannings | 7e67151 | 2012-08-27 14:44:16 -0700 | [diff] [blame] | 6202 | mContext.enforceCallingOrSelfPermission( |
| 6203 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 6204 | "Only package verification agents can extend verification timeouts"); |
| 6205 | |
rich cannings | d9ef3e5 | 2012-08-22 14:28:05 -0700 | [diff] [blame] | 6206 | final PackageVerificationState state = mPendingVerification.get(id); |
| 6207 | final PackageVerificationResponse response = new PackageVerificationResponse( |
| 6208 | verificationCodeAtTimeout, Binder.getCallingUid()); |
| 6209 | |
rich cannings | d1b5cfc | 2012-08-29 14:49:51 -0700 | [diff] [blame] | 6210 | if (millisecondsToDelay > PackageManager.MAXIMUM_VERIFICATION_TIMEOUT) { |
| 6211 | millisecondsToDelay = PackageManager.MAXIMUM_VERIFICATION_TIMEOUT; |
| 6212 | } |
| 6213 | if (millisecondsToDelay < 0) { |
| 6214 | millisecondsToDelay = 0; |
rich cannings | d9ef3e5 | 2012-08-22 14:28:05 -0700 | [diff] [blame] | 6215 | } |
| 6216 | if ((verificationCodeAtTimeout != PackageManager.VERIFICATION_ALLOW) |
rich cannings | d1b5cfc | 2012-08-29 14:49:51 -0700 | [diff] [blame] | 6217 | && (verificationCodeAtTimeout != PackageManager.VERIFICATION_REJECT)) { |
| 6218 | verificationCodeAtTimeout = PackageManager.VERIFICATION_REJECT; |
rich cannings | d9ef3e5 | 2012-08-22 14:28:05 -0700 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | if ((state != null) && !state.timeoutExtended()) { |
| 6222 | state.extendTimeout(); |
| 6223 | |
| 6224 | final Message msg = mHandler.obtainMessage(PACKAGE_VERIFIED); |
| 6225 | msg.arg1 = id; |
| 6226 | msg.obj = response; |
| 6227 | mHandler.sendMessageDelayed(msg, millisecondsToDelay); |
| 6228 | } |
| 6229 | } |
| 6230 | |
rich cannings | d1b5cfc | 2012-08-29 14:49:51 -0700 | [diff] [blame] | 6231 | private void broadcastPackageVerified(int verificationId, Uri packageUri, |
rich cannings | d55deb0 | 2012-10-02 14:53:08 -0700 | [diff] [blame] | 6232 | int verificationCode, UserHandle user) { |
rich cannings | d1b5cfc | 2012-08-29 14:49:51 -0700 | [diff] [blame] | 6233 | final Intent intent = new Intent(Intent.ACTION_PACKAGE_VERIFIED); |
| 6234 | intent.setDataAndType(packageUri, PACKAGE_MIME_TYPE); |
| 6235 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6236 | intent.putExtra(PackageManager.EXTRA_VERIFICATION_ID, verificationId); |
| 6237 | intent.putExtra(PackageManager.EXTRA_VERIFICATION_RESULT, verificationCode); |
| 6238 | |
rich cannings | d55deb0 | 2012-10-02 14:53:08 -0700 | [diff] [blame] | 6239 | mContext.sendBroadcastAsUser(intent, user, |
| 6240 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT); |
rich cannings | d1b5cfc | 2012-08-29 14:49:51 -0700 | [diff] [blame] | 6241 | } |
| 6242 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6243 | private ComponentName matchComponentForVerifier(String packageName, |
| 6244 | List<ResolveInfo> receivers) { |
| 6245 | ActivityInfo targetReceiver = null; |
| 6246 | |
| 6247 | final int NR = receivers.size(); |
| 6248 | for (int i = 0; i < NR; i++) { |
| 6249 | final ResolveInfo info = receivers.get(i); |
| 6250 | if (info.activityInfo == null) { |
| 6251 | continue; |
| 6252 | } |
| 6253 | |
| 6254 | if (packageName.equals(info.activityInfo.packageName)) { |
| 6255 | targetReceiver = info.activityInfo; |
| 6256 | break; |
| 6257 | } |
| 6258 | } |
| 6259 | |
| 6260 | if (targetReceiver == null) { |
| 6261 | return null; |
| 6262 | } |
| 6263 | |
| 6264 | return new ComponentName(targetReceiver.packageName, targetReceiver.name); |
| 6265 | } |
| 6266 | |
| 6267 | private List<ComponentName> matchVerifiers(PackageInfoLite pkgInfo, |
| 6268 | List<ResolveInfo> receivers, final PackageVerificationState verificationState) { |
| 6269 | if (pkgInfo.verifiers.length == 0) { |
| 6270 | return null; |
| 6271 | } |
| 6272 | |
| 6273 | final int N = pkgInfo.verifiers.length; |
| 6274 | final List<ComponentName> sufficientVerifiers = new ArrayList<ComponentName>(N + 1); |
| 6275 | for (int i = 0; i < N; i++) { |
| 6276 | final VerifierInfo verifierInfo = pkgInfo.verifiers[i]; |
| 6277 | |
| 6278 | final ComponentName comp = matchComponentForVerifier(verifierInfo.packageName, |
| 6279 | receivers); |
| 6280 | if (comp == null) { |
| 6281 | continue; |
| 6282 | } |
| 6283 | |
| 6284 | final int verifierUid = getUidForVerifier(verifierInfo); |
| 6285 | if (verifierUid == -1) { |
| 6286 | continue; |
| 6287 | } |
| 6288 | |
| 6289 | if (DEBUG_VERIFY) { |
| 6290 | Slog.d(TAG, "Added sufficient verifier " + verifierInfo.packageName |
| 6291 | + " with the correct signature"); |
| 6292 | } |
| 6293 | sufficientVerifiers.add(comp); |
| 6294 | verificationState.addSufficientVerifier(verifierUid); |
| 6295 | } |
| 6296 | |
| 6297 | return sufficientVerifiers; |
| 6298 | } |
| 6299 | |
| 6300 | private int getUidForVerifier(VerifierInfo verifierInfo) { |
| 6301 | synchronized (mPackages) { |
| 6302 | final PackageParser.Package pkg = mPackages.get(verifierInfo.packageName); |
| 6303 | if (pkg == null) { |
| 6304 | return -1; |
| 6305 | } else if (pkg.mSignatures.length != 1) { |
| 6306 | Slog.i(TAG, "Verifier package " + verifierInfo.packageName |
| 6307 | + " has more than one signature; ignoring"); |
| 6308 | return -1; |
| 6309 | } |
| 6310 | |
| 6311 | /* |
| 6312 | * If the public key of the package's signature does not match |
| 6313 | * our expected public key, then this is a different package and |
| 6314 | * we should skip. |
| 6315 | */ |
| 6316 | |
| 6317 | final byte[] expectedPublicKey; |
| 6318 | try { |
| 6319 | final Signature verifierSig = pkg.mSignatures[0]; |
| 6320 | final PublicKey publicKey = verifierSig.getPublicKey(); |
| 6321 | expectedPublicKey = publicKey.getEncoded(); |
| 6322 | } catch (CertificateException e) { |
| 6323 | return -1; |
| 6324 | } |
| 6325 | |
| 6326 | final byte[] actualPublicKey = verifierInfo.publicKey.getEncoded(); |
| 6327 | |
| 6328 | if (!Arrays.equals(actualPublicKey, expectedPublicKey)) { |
| 6329 | Slog.i(TAG, "Verifier package " + verifierInfo.packageName |
| 6330 | + " does not have the expected public key; ignoring"); |
| 6331 | return -1; |
| 6332 | } |
| 6333 | |
| 6334 | return pkg.applicationInfo.uid; |
| 6335 | } |
| 6336 | } |
| 6337 | |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 6338 | public void finishPackageInstall(int token) { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 6339 | enforceSystemOrRoot("Only the system is allowed to finish installs"); |
| 6340 | |
| 6341 | if (DEBUG_INSTALL) { |
| 6342 | Slog.v(TAG, "BM finishing package install for " + token); |
| 6343 | } |
| 6344 | |
| 6345 | final Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 6346 | mHandler.sendMessage(msg); |
| 6347 | } |
| 6348 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6349 | /** |
| 6350 | * Get the verification agent timeout. |
| 6351 | * |
| 6352 | * @return verification timeout in milliseconds |
| 6353 | */ |
| 6354 | private long getVerificationTimeout() { |
rich cannings | 4d8fc79 | 2012-09-07 14:43:43 -0700 | [diff] [blame] | 6355 | return android.provider.Settings.Global.getLong(mContext.getContentResolver(), |
| 6356 | android.provider.Settings.Global.PACKAGE_VERIFIER_TIMEOUT, |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6357 | DEFAULT_VERIFICATION_TIMEOUT); |
| 6358 | } |
| 6359 | |
| 6360 | /** |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 6361 | * Get the default verification agent response code. |
| 6362 | * |
| 6363 | * @return default verification response code |
| 6364 | */ |
| 6365 | private int getDefaultVerificationResponse() { |
rich cannings | 4d8fc79 | 2012-09-07 14:43:43 -0700 | [diff] [blame] | 6366 | return android.provider.Settings.Global.getInt(mContext.getContentResolver(), |
| 6367 | android.provider.Settings.Global.PACKAGE_VERIFIER_DEFAULT_RESPONSE, |
rich cannings | a6cfe52 | 2012-05-21 15:50:56 -0700 | [diff] [blame] | 6368 | DEFAULT_VERIFICATION_RESPONSE); |
| 6369 | } |
| 6370 | |
| 6371 | /** |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6372 | * Check whether or not package verification has been enabled. |
| 6373 | * |
| 6374 | * @return true if verification should be performed |
| 6375 | */ |
rich cannings | 4e5753f | 2012-09-19 16:03:56 -0700 | [diff] [blame] | 6376 | private boolean isVerificationEnabled(int flags) { |
rich cannings | e4bf0ea | 2012-09-14 13:18:55 -0700 | [diff] [blame] | 6377 | if (!DEFAULT_VERIFY_ENABLE) { |
| 6378 | return false; |
| 6379 | } |
| 6380 | |
rich cannings | 4e5753f | 2012-09-19 16:03:56 -0700 | [diff] [blame] | 6381 | // Check if installing from ADB |
| 6382 | if ((flags & PackageManager.INSTALL_FROM_ADB) != 0) { |
rich cannings | a2064df | 2012-09-30 14:52:41 -0700 | [diff] [blame] | 6383 | // Do not run verification in a test harness environment |
| 6384 | if (ActivityManager.isRunningInTestHarness()) { |
| 6385 | return false; |
| 6386 | } |
rich cannings | 4e5753f | 2012-09-19 16:03:56 -0700 | [diff] [blame] | 6387 | // Check if the developer does not want package verification for ADB installs |
| 6388 | if (android.provider.Settings.Global.getInt(mContext.getContentResolver(), |
| 6389 | android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, 1) == 0) { |
| 6390 | return false; |
| 6391 | } |
| 6392 | } |
| 6393 | |
rich cannings | 4d8fc79 | 2012-09-07 14:43:43 -0700 | [diff] [blame] | 6394 | return android.provider.Settings.Global.getInt(mContext.getContentResolver(), |
rich cannings | e4bf0ea | 2012-09-14 13:18:55 -0700 | [diff] [blame] | 6395 | android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, 1) == 1; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6396 | } |
| 6397 | |
Matt Finifter | f8a98ed | 2012-05-14 15:43:34 -0700 | [diff] [blame] | 6398 | /** |
| 6399 | * Get the "allow unknown sources" setting. |
| 6400 | * |
| 6401 | * @return the current "allow unknown sources" setting |
| 6402 | */ |
| 6403 | private int getUnknownSourcesSettings() { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 6404 | return android.provider.Settings.Global.getInt(mContext.getContentResolver(), |
| 6405 | android.provider.Settings.Global.INSTALL_NON_MARKET_APPS, |
Matt Finifter | f8a98ed | 2012-05-14 15:43:34 -0700 | [diff] [blame] | 6406 | -1); |
| 6407 | } |
| 6408 | |
Kenny Root | f03b45f | 2011-02-23 17:25:45 -0800 | [diff] [blame] | 6409 | public void setInstallerPackageName(String targetPackage, String installerPackageName) { |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 6410 | final int uid = Binder.getCallingUid(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6411 | // writer |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 6412 | synchronized (mPackages) { |
| 6413 | PackageSetting targetPackageSetting = mSettings.mPackages.get(targetPackage); |
| 6414 | if (targetPackageSetting == null) { |
| 6415 | throw new IllegalArgumentException("Unknown target package: " + targetPackage); |
| 6416 | } |
| 6417 | |
| 6418 | PackageSetting installerPackageSetting; |
| 6419 | if (installerPackageName != null) { |
| 6420 | installerPackageSetting = mSettings.mPackages.get(installerPackageName); |
| 6421 | if (installerPackageSetting == null) { |
| 6422 | throw new IllegalArgumentException("Unknown installer package: " |
| 6423 | + installerPackageName); |
| 6424 | } |
| 6425 | } else { |
| 6426 | installerPackageSetting = null; |
| 6427 | } |
| 6428 | |
| 6429 | Signature[] callerSignature; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6430 | Object obj = mSettings.getUserIdLPr(uid); |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 6431 | if (obj != null) { |
| 6432 | if (obj instanceof SharedUserSetting) { |
| 6433 | callerSignature = ((SharedUserSetting)obj).signatures.mSignatures; |
| 6434 | } else if (obj instanceof PackageSetting) { |
| 6435 | callerSignature = ((PackageSetting)obj).signatures.mSignatures; |
| 6436 | } else { |
| 6437 | throw new SecurityException("Bad object " + obj + " for uid " + uid); |
| 6438 | } |
| 6439 | } else { |
| 6440 | throw new SecurityException("Unknown calling uid " + uid); |
| 6441 | } |
| 6442 | |
| 6443 | // Verify: can't set installerPackageName to a package that is |
| 6444 | // not signed with the same cert as the caller. |
| 6445 | if (installerPackageSetting != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6446 | if (compareSignatures(callerSignature, |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 6447 | installerPackageSetting.signatures.mSignatures) |
| 6448 | != PackageManager.SIGNATURE_MATCH) { |
| 6449 | throw new SecurityException( |
| 6450 | "Caller does not have same cert as new installer package " |
| 6451 | + installerPackageName); |
| 6452 | } |
| 6453 | } |
| 6454 | |
| 6455 | // Verify: if target already has an installer package, it must |
| 6456 | // be signed with the same cert as the caller. |
| 6457 | if (targetPackageSetting.installerPackageName != null) { |
| 6458 | PackageSetting setting = mSettings.mPackages.get( |
| 6459 | targetPackageSetting.installerPackageName); |
| 6460 | // If the currently set package isn't valid, then it's always |
| 6461 | // okay to change it. |
| 6462 | if (setting != null) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6463 | if (compareSignatures(callerSignature, |
Dianne Hackborn | 880119b | 2010-11-18 22:26:40 -0800 | [diff] [blame] | 6464 | setting.signatures.mSignatures) |
| 6465 | != PackageManager.SIGNATURE_MATCH) { |
| 6466 | throw new SecurityException( |
| 6467 | "Caller does not have same cert as old installer package " |
| 6468 | + targetPackageSetting.installerPackageName); |
| 6469 | } |
| 6470 | } |
| 6471 | } |
| 6472 | |
| 6473 | // Okay! |
| 6474 | targetPackageSetting.installerPackageName = installerPackageName; |
| 6475 | scheduleWriteSettingsLocked(); |
| 6476 | } |
| 6477 | } |
| 6478 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6479 | private void processPendingInstall(final InstallArgs args, final int currentStatus) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6480 | // Queue up an async operation since the package installation may take a little while. |
| 6481 | mHandler.post(new Runnable() { |
| 6482 | public void run() { |
| 6483 | mHandler.removeCallbacks(this); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 6484 | // Result object to be returned |
| 6485 | PackageInstalledInfo res = new PackageInstalledInfo(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6486 | res.returnCode = currentStatus; |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 6487 | res.uid = -1; |
| 6488 | res.pkg = null; |
| 6489 | res.removedInfo = new PackageRemovedInfo(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6490 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 6491 | args.doPreInstall(res.returnCode); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 6492 | synchronized (mInstallLock) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6493 | installPackageLI(args, true, res); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 6494 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 6495 | args.doPostInstall(res.returnCode, res.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6496 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 6497 | |
| 6498 | // A restore should be performed at this point if (a) the install |
| 6499 | // succeeded, (b) the operation is not an update, and (c) the new |
| 6500 | // package has a backupAgent defined. |
| 6501 | final boolean update = res.removedInfo.removedPackage != null; |
Christopher Tate | 59eac4b | 2010-02-19 19:25:45 -0800 | [diff] [blame] | 6502 | boolean doRestore = (!update |
| 6503 | && res.pkg != null |
| 6504 | && res.pkg.applicationInfo.backupAgentName != null); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 6505 | |
| 6506 | // Set up the post-install work request bookkeeping. This will be used |
| 6507 | // and cleaned up by the post-install event handling regardless of whether |
| 6508 | // there's a restore pass performed. Token values are >= 1. |
| 6509 | int token; |
| 6510 | if (mNextInstallToken < 0) mNextInstallToken = 1; |
| 6511 | token = mNextInstallToken++; |
| 6512 | |
| 6513 | PostInstallData data = new PostInstallData(args, res); |
| 6514 | mRunningInstalls.put(token, data); |
| 6515 | if (DEBUG_INSTALL) Log.v(TAG, "+ starting restore round-trip " + token); |
| 6516 | |
| 6517 | if (res.returnCode == PackageManager.INSTALL_SUCCEEDED && doRestore) { |
| 6518 | // Pass responsibility to the Backup Manager. It will perform a |
| 6519 | // restore if appropriate, then pass responsibility back to the |
| 6520 | // Package Manager to run the post-install observer callbacks |
| 6521 | // and broadcasts. |
| 6522 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 6523 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 6524 | if (bm != null) { |
| 6525 | if (DEBUG_INSTALL) Log.v(TAG, "token " + token |
| 6526 | + " to BM for possible restore"); |
| 6527 | try { |
| 6528 | bm.restoreAtInstall(res.pkg.applicationInfo.packageName, token); |
| 6529 | } catch (RemoteException e) { |
| 6530 | // can't happen; the backup manager is local |
| 6531 | } catch (Exception e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6532 | Slog.e(TAG, "Exception trying to enqueue restore", e); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 6533 | doRestore = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6534 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 6535 | } else { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6536 | Slog.e(TAG, "Backup Manager not found!"); |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 6537 | doRestore = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 6538 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6539 | } |
Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 6540 | |
| 6541 | if (!doRestore) { |
| 6542 | // No restore possible, or the Backup Manager was mysteriously not |
| 6543 | // available -- just fire the post-install work request directly. |
| 6544 | if (DEBUG_INSTALL) Log.v(TAG, "No restore - queue post-install for " + token); |
| 6545 | Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); |
| 6546 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6547 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6548 | } |
| 6549 | }); |
| 6550 | } |
| 6551 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6552 | private abstract class HandlerParams { |
| 6553 | private static final int MAX_RETRIES = 4; |
| 6554 | |
| 6555 | /** |
| 6556 | * Number of times startCopy() has been attempted and had a non-fatal |
| 6557 | * error. |
| 6558 | */ |
| 6559 | private int mRetries = 0; |
| 6560 | |
Amith Yamasani | 6ec1012 | 2012-08-23 13:49:25 -0700 | [diff] [blame] | 6561 | /** User handle for the user requesting the information or installation. */ |
| 6562 | private final UserHandle mUser; |
| 6563 | |
| 6564 | HandlerParams(UserHandle user) { |
| 6565 | mUser = user; |
| 6566 | } |
| 6567 | |
| 6568 | UserHandle getUser() { |
| 6569 | return mUser; |
| 6570 | } |
| 6571 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6572 | final boolean startCopy() { |
| 6573 | boolean res; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6574 | try { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 6575 | if (DEBUG_INSTALL) Slog.i(TAG, "startCopy " + mUser + ": " + this); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6576 | |
| 6577 | if (++mRetries > MAX_RETRIES) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6578 | 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] | 6579 | mHandler.sendEmptyMessage(MCS_GIVE_UP); |
| 6580 | handleServiceError(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6581 | return false; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6582 | } else { |
| 6583 | handleStartCopy(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6584 | res = true; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6585 | } |
| 6586 | } catch (RemoteException e) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6587 | if (DEBUG_INSTALL) Slog.i(TAG, "Posting install MCS_RECONNECT"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6588 | mHandler.sendEmptyMessage(MCS_RECONNECT); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6589 | res = false; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6590 | } |
| 6591 | handleReturnCode(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6592 | return res; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6593 | } |
| 6594 | |
| 6595 | final void serviceError() { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6596 | if (DEBUG_INSTALL) Slog.i(TAG, "serviceError"); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6597 | handleServiceError(); |
| 6598 | handleReturnCode(); |
| 6599 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6600 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6601 | abstract void handleStartCopy() throws RemoteException; |
| 6602 | abstract void handleServiceError(); |
| 6603 | abstract void handleReturnCode(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6604 | } |
| 6605 | |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6606 | class MeasureParams extends HandlerParams { |
| 6607 | private final PackageStats mStats; |
| 6608 | private boolean mSuccess; |
| 6609 | |
| 6610 | private final IPackageStatsObserver mObserver; |
| 6611 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 6612 | public MeasureParams(PackageStats stats, IPackageStatsObserver observer) { |
Amith Yamasani | 6ec1012 | 2012-08-23 13:49:25 -0700 | [diff] [blame] | 6613 | super(new UserHandle(stats.userHandle)); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6614 | mObserver = observer; |
| 6615 | mStats = stats; |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6616 | } |
| 6617 | |
| 6618 | @Override |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 6619 | public String toString() { |
| 6620 | return "MeasureParams{" |
| 6621 | + Integer.toHexString(System.identityHashCode(this)) |
| 6622 | + " " + mStats.packageName + "}"; |
| 6623 | } |
| 6624 | |
| 6625 | @Override |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6626 | void handleStartCopy() throws RemoteException { |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 6627 | synchronized (mInstallLock) { |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 6628 | mSuccess = getPackageSizeInfoLI(mStats.packageName, mStats.userHandle, mStats); |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 6629 | } |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6630 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 6631 | final boolean mounted; |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6632 | if (Environment.isExternalStorageEmulated()) { |
| 6633 | mounted = true; |
| 6634 | } else { |
| 6635 | final String status = Environment.getExternalStorageState(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 6636 | mounted = (Environment.MEDIA_MOUNTED.equals(status) |
| 6637 | || Environment.MEDIA_MOUNTED_READ_ONLY.equals(status)); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6638 | } |
| 6639 | |
| 6640 | if (mounted) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 6641 | final UserEnvironment userEnv = new UserEnvironment(mStats.userHandle); |
| 6642 | |
| 6643 | final File externalCacheDir = userEnv |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6644 | .getExternalStorageAppCacheDirectory(mStats.packageName); |
| 6645 | final long externalCacheSize = mContainerService |
| 6646 | .calculateDirectorySize(externalCacheDir.getPath()); |
| 6647 | mStats.externalCacheSize = externalCacheSize; |
| 6648 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 6649 | final File externalDataDir = userEnv |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6650 | .getExternalStorageAppDataDirectory(mStats.packageName); |
| 6651 | long externalDataSize = mContainerService.calculateDirectorySize(externalDataDir |
| 6652 | .getPath()); |
| 6653 | |
| 6654 | if (externalCacheDir.getParentFile().equals(externalDataDir)) { |
| 6655 | externalDataSize -= externalCacheSize; |
| 6656 | } |
| 6657 | mStats.externalDataSize = externalDataSize; |
| 6658 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 6659 | final File externalMediaDir = userEnv |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6660 | .getExternalStorageAppMediaDirectory(mStats.packageName); |
| 6661 | mStats.externalMediaSize = mContainerService |
Kenny Root | c7624d9 | 2011-02-23 16:25:25 -0800 | [diff] [blame] | 6662 | .calculateDirectorySize(externalMediaDir.getPath()); |
Kenny Root | bcd6c96 | 2011-01-17 11:21:49 -0800 | [diff] [blame] | 6663 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 6664 | final File externalObbDir = userEnv |
Kenny Root | bcd6c96 | 2011-01-17 11:21:49 -0800 | [diff] [blame] | 6665 | .getExternalStorageAppObbDirectory(mStats.packageName); |
| 6666 | mStats.externalObbSize = mContainerService.calculateDirectorySize(externalObbDir |
| 6667 | .getPath()); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 6668 | } |
| 6669 | } |
| 6670 | |
| 6671 | @Override |
| 6672 | void handleReturnCode() { |
| 6673 | if (mObserver != null) { |
| 6674 | try { |
| 6675 | mObserver.onGetStatsCompleted(mStats, mSuccess); |
| 6676 | } catch (RemoteException e) { |
| 6677 | Slog.i(TAG, "Observer no longer exists."); |
| 6678 | } |
| 6679 | } |
| 6680 | } |
| 6681 | |
| 6682 | @Override |
| 6683 | void handleServiceError() { |
| 6684 | Slog.e(TAG, "Could not measure application " + mStats.packageName |
| 6685 | + " external storage"); |
| 6686 | } |
| 6687 | } |
| 6688 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6689 | class InstallParams extends HandlerParams { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6690 | final IPackageInstallObserver observer; |
| 6691 | int flags; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6692 | |
| 6693 | private final Uri mPackageURI; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6694 | final String installerPackageName; |
rich cannings | 706e8ba | 2012-08-20 13:20:14 -0700 | [diff] [blame] | 6695 | final VerificationParams verificationParams; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6696 | private InstallArgs mArgs; |
| 6697 | private int mRet; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6698 | private File mTempPackage; |
| 6699 | final ContainerEncryptionParams encryptionParams; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6700 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6701 | InstallParams(Uri packageURI, |
| 6702 | IPackageInstallObserver observer, int flags, |
rich cannings | 706e8ba | 2012-08-20 13:20:14 -0700 | [diff] [blame] | 6703 | String installerPackageName, VerificationParams verificationParams, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 6704 | ContainerEncryptionParams encryptionParams, UserHandle user) { |
Amith Yamasani | 6ec1012 | 2012-08-23 13:49:25 -0700 | [diff] [blame] | 6705 | super(user); |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6706 | this.mPackageURI = packageURI; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6707 | this.flags = flags; |
| 6708 | this.observer = observer; |
| 6709 | this.installerPackageName = installerPackageName; |
rich cannings | 706e8ba | 2012-08-20 13:20:14 -0700 | [diff] [blame] | 6710 | this.verificationParams = verificationParams; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6711 | this.encryptionParams = encryptionParams; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 6712 | } |
| 6713 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 6714 | @Override |
| 6715 | public String toString() { |
| 6716 | return "InstallParams{" |
| 6717 | + Integer.toHexString(System.identityHashCode(this)) |
| 6718 | + " " + mPackageURI + "}"; |
| 6719 | } |
| 6720 | |
rich cannings | 706e8ba | 2012-08-20 13:20:14 -0700 | [diff] [blame] | 6721 | public ManifestDigest getManifestDigest() { |
| 6722 | if (verificationParams == null) { |
| 6723 | return null; |
| 6724 | } |
| 6725 | return verificationParams.getManifestDigest(); |
| 6726 | } |
| 6727 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6728 | private int installLocationPolicy(PackageInfoLite pkgLite, int flags) { |
| 6729 | String packageName = pkgLite.packageName; |
| 6730 | int installLocation = pkgLite.installLocation; |
| 6731 | boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 6732 | // reader |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6733 | synchronized (mPackages) { |
| 6734 | PackageParser.Package pkg = mPackages.get(packageName); |
| 6735 | if (pkg != null) { |
| 6736 | if ((flags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 6737 | // Check for downgrading. |
| 6738 | if ((flags & PackageManager.INSTALL_ALLOW_DOWNGRADE) == 0) { |
| 6739 | if (pkgLite.versionCode < pkg.mVersionCode) { |
| 6740 | Slog.w(TAG, "Can't install update of " + packageName |
| 6741 | + " update version " + pkgLite.versionCode |
| 6742 | + " is older than installed version " |
| 6743 | + pkg.mVersionCode); |
| 6744 | return PackageHelper.RECOMMEND_FAILED_VERSION_DOWNGRADE; |
| 6745 | } |
| 6746 | } |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6747 | // Check for updated system application. |
| 6748 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 6749 | if (onSd) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 6750 | Slog.w(TAG, "Cannot install update to system app on sdcard"); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6751 | return PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION; |
| 6752 | } |
| 6753 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 6754 | } else { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6755 | if (onSd) { |
| 6756 | // Install flag overrides everything. |
| 6757 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 6758 | } |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 6759 | // If current upgrade specifies particular preference |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6760 | if (installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
| 6761 | // Application explicitly specified internal. |
| 6762 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 6763 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { |
| 6764 | // App explictly prefers external. Let policy decide |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 6765 | } else { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6766 | // Prefer previous location |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 6767 | if (isExternal(pkg)) { |
Suchi Amalapurapu | 90d8ee6 | 2010-03-18 11:38:35 -0700 | [diff] [blame] | 6768 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 6769 | } |
| 6770 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6771 | } |
| 6772 | } |
| 6773 | } else { |
| 6774 | // Invalid install. Return error code |
| 6775 | return PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS; |
| 6776 | } |
| 6777 | } |
| 6778 | } |
| 6779 | // All the special cases have been taken care of. |
| 6780 | // Return result based on recommended install location. |
| 6781 | if (onSd) { |
| 6782 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
| 6783 | } |
| 6784 | return pkgLite.recommendedInstallLocation; |
| 6785 | } |
| 6786 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6787 | /* |
| 6788 | * Invoke remote method to get package information and install |
| 6789 | * location values. Override install location based on default |
| 6790 | * policy if needed and then create install arguments based |
| 6791 | * on the install location. |
| 6792 | */ |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6793 | public void handleStartCopy() throws RemoteException { |
Suchi Amalapurapu | 1f9e1b4 | 2010-02-26 13:14:31 -0800 | [diff] [blame] | 6794 | int ret = PackageManager.INSTALL_SUCCEEDED; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6795 | final boolean onSd = (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| 6796 | final boolean onInt = (flags & PackageManager.INSTALL_INTERNAL) != 0; |
| 6797 | PackageInfoLite pkgLite = null; |
| 6798 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 6799 | if (onInt && onSd) { |
| 6800 | // Check if both bits are set. |
| 6801 | Slog.w(TAG, "Conflicting flags specified for installing on both internal and external"); |
| 6802 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 6803 | } else { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 6804 | final long lowThreshold; |
| 6805 | |
| 6806 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 6807 | .getService(DeviceStorageMonitorService.SERVICE); |
| 6808 | if (dsm == null) { |
| 6809 | Log.w(TAG, "Couldn't get low memory threshold; no free limit imposed"); |
| 6810 | lowThreshold = 0L; |
| 6811 | } else { |
| 6812 | lowThreshold = dsm.getMemoryLowThreshold(); |
| 6813 | } |
| 6814 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6815 | try { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6816 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, mPackageURI, |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6817 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 6818 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6819 | final File packageFile; |
| 6820 | if (encryptionParams != null || !"file".equals(mPackageURI.getScheme())) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6821 | mTempPackage = createTempPackageFile(mDrmAppPrivateInstallDir); |
| 6822 | if (mTempPackage != null) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 6823 | ParcelFileDescriptor out; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6824 | try { |
| 6825 | out = ParcelFileDescriptor.open(mTempPackage, |
| 6826 | ParcelFileDescriptor.MODE_READ_WRITE); |
| 6827 | } catch (FileNotFoundException e) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 6828 | out = null; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6829 | Slog.e(TAG, "Failed to create temporary file for : " + mPackageURI); |
| 6830 | } |
| 6831 | |
| 6832 | // Make a temporary file for decryption. |
| 6833 | ret = mContainerService |
| 6834 | .copyResource(mPackageURI, encryptionParams, out); |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 6835 | IoUtils.closeQuietly(out); |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6836 | |
| 6837 | packageFile = mTempPackage; |
| 6838 | |
| 6839 | FileUtils.setPermissions(packageFile.getAbsolutePath(), |
Kenny Root | 0b2fa8c | 2012-09-14 10:26:16 -0700 | [diff] [blame] | 6840 | FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP |
| 6841 | | FileUtils.S_IROTH, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6842 | -1, -1); |
| 6843 | } else { |
| 6844 | packageFile = null; |
| 6845 | } |
| 6846 | } else { |
| 6847 | packageFile = new File(mPackageURI.getPath()); |
| 6848 | } |
| 6849 | |
| 6850 | if (packageFile != null) { |
| 6851 | // Remote call to find out default install location |
Kenny Root | cea3743 | 2012-10-18 14:57:33 -0700 | [diff] [blame] | 6852 | final String packageFilePath = packageFile.getAbsolutePath(); |
| 6853 | pkgLite = mContainerService.getMinimalPackageInfo(packageFilePath, flags, |
| 6854 | lowThreshold); |
| 6855 | |
| 6856 | /* |
| 6857 | * If we have too little free space, try to free cache |
| 6858 | * before giving up. |
| 6859 | */ |
| 6860 | if (pkgLite.recommendedInstallLocation |
| 6861 | == PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE) { |
| 6862 | final long size = mContainerService.calculateInstalledSize( |
| 6863 | packageFilePath, isForwardLocked()); |
| 6864 | if (mInstaller.freeCache(size + lowThreshold) >= 0) { |
| 6865 | pkgLite = mContainerService.getMinimalPackageInfo(packageFilePath, |
| 6866 | flags, lowThreshold); |
| 6867 | } |
Kenny Root | 3f96d0e | 2012-12-11 12:48:08 -0800 | [diff] [blame] | 6868 | /* |
| 6869 | * The cache free must have deleted the file we |
| 6870 | * downloaded to install. |
| 6871 | * |
| 6872 | * TODO: fix the "freeCache" call to not delete |
| 6873 | * the file we care about. |
| 6874 | */ |
| 6875 | if (pkgLite.recommendedInstallLocation |
| 6876 | == PackageHelper.RECOMMEND_FAILED_INVALID_URI) { |
| 6877 | pkgLite.recommendedInstallLocation |
| 6878 | = PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE; |
| 6879 | } |
Kenny Root | cea3743 | 2012-10-18 14:57:33 -0700 | [diff] [blame] | 6880 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6881 | } |
| 6882 | } finally { |
| 6883 | mContext.revokeUriPermission(mPackageURI, |
| 6884 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 6885 | } |
| 6886 | } |
| 6887 | |
| 6888 | if (ret == PackageManager.INSTALL_SUCCEEDED) { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6889 | int loc = pkgLite.recommendedInstallLocation; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 6890 | if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_LOCATION) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6891 | ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 6892 | } else if (loc == PackageHelper.RECOMMEND_FAILED_ALREADY_EXISTS) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 6893 | ret = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 6894 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6895 | ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 6896 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_APK) { |
| 6897 | ret = PackageManager.INSTALL_FAILED_INVALID_APK; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 6898 | } else if (loc == PackageHelper.RECOMMEND_FAILED_INVALID_URI) { |
| 6899 | ret = PackageManager.INSTALL_FAILED_INVALID_URI; |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6900 | } else if (loc == PackageHelper.RECOMMEND_MEDIA_UNAVAILABLE) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 6901 | ret = PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6902 | } else { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 6903 | // Override with defaults if needed. |
| 6904 | loc = installLocationPolicy(pkgLite, flags); |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 6905 | if (loc == PackageHelper.RECOMMEND_FAILED_VERSION_DOWNGRADE) { |
| 6906 | ret = PackageManager.INSTALL_FAILED_VERSION_DOWNGRADE; |
| 6907 | } else if (!onSd && !onInt) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 6908 | // Override install location with flags |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6909 | if (loc == PackageHelper.RECOMMEND_INSTALL_EXTERNAL) { |
| 6910 | // Set the flag to install on external media. |
| 6911 | flags |= PackageManager.INSTALL_EXTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 6912 | flags &= ~PackageManager.INSTALL_INTERNAL; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6913 | } else { |
| 6914 | // Make sure the flag for installing on external |
| 6915 | // media is unset |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 6916 | flags |= PackageManager.INSTALL_INTERNAL; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6917 | flags &= ~PackageManager.INSTALL_EXTERNAL; |
| 6918 | } |
| 6919 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6920 | } |
| 6921 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6922 | |
| 6923 | final InstallArgs args = createInstallArgs(this); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6924 | mArgs = args; |
| 6925 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 6926 | if (ret == PackageManager.INSTALL_SUCCEEDED) { |
rich cannings | 36070b4 | 2012-10-09 11:50:15 -0700 | [diff] [blame] | 6927 | /* |
| 6928 | * ADB installs appear as UserHandle.USER_ALL, and can only be performed by |
| 6929 | * UserHandle.USER_OWNER, so use the package verifier for UserHandle.USER_OWNER. |
| 6930 | */ |
| 6931 | int userIdentifier = getUser().getIdentifier(); |
| 6932 | if (userIdentifier == UserHandle.USER_ALL |
| 6933 | && ((flags & PackageManager.INSTALL_FROM_ADB) != 0)) { |
| 6934 | userIdentifier = UserHandle.USER_OWNER; |
| 6935 | } |
| 6936 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6937 | /* |
| 6938 | * Determine if we have any installed package verifiers. If we |
| 6939 | * do, then we'll defer to them to verify the packages. |
| 6940 | */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6941 | final int requiredUid = mRequiredVerifierPackage == null ? -1 |
rich cannings | 36070b4 | 2012-10-09 11:50:15 -0700 | [diff] [blame] | 6942 | : getPackageUid(mRequiredVerifierPackage, userIdentifier); |
rich cannings | 4e5753f | 2012-09-19 16:03:56 -0700 | [diff] [blame] | 6943 | if (requiredUid != -1 && isVerificationEnabled(flags)) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 6944 | final Intent verification = new Intent( |
| 6945 | Intent.ACTION_PACKAGE_NEEDS_VERIFICATION); |
| 6946 | verification.setDataAndType(getPackageUri(), PACKAGE_MIME_TYPE); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6947 | verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6948 | |
Andy Stadler | 250ce28 | 2012-08-24 16:09:03 -0700 | [diff] [blame] | 6949 | final List<ResolveInfo> receivers = queryIntentReceivers(verification, |
| 6950 | PACKAGE_MIME_TYPE, PackageManager.GET_DISABLED_COMPONENTS, |
| 6951 | 0 /* TODO: Which userId? */); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6952 | |
| 6953 | if (DEBUG_VERIFY) { |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6954 | Slog.d(TAG, "Found " + receivers.size() + " verifiers for intent " |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6955 | + verification.toString() + " with " + pkgLite.verifiers.length |
| 6956 | + " optional verifiers"); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6957 | } |
| 6958 | |
| 6959 | final int verificationId = mPendingVerificationToken++; |
| 6960 | |
| 6961 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_ID, verificationId); |
| 6962 | |
| 6963 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_INSTALLER_PACKAGE, |
| 6964 | installerPackageName); |
| 6965 | |
| 6966 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_INSTALL_FLAGS, flags); |
| 6967 | |
rich cannings | 13d428e | 2012-09-13 13:43:07 -0700 | [diff] [blame] | 6968 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_PACKAGE_NAME, |
| 6969 | pkgLite.packageName); |
| 6970 | |
| 6971 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_VERSION_CODE, |
| 6972 | pkgLite.versionCode); |
| 6973 | |
rich cannings | 706e8ba | 2012-08-20 13:20:14 -0700 | [diff] [blame] | 6974 | if (verificationParams != null) { |
| 6975 | if (verificationParams.getVerificationURI() != null) { |
| 6976 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_URI, |
| 6977 | verificationParams.getVerificationURI()); |
| 6978 | } |
| 6979 | if (verificationParams.getOriginatingURI() != null) { |
| 6980 | verification.putExtra(Intent.EXTRA_ORIGINATING_URI, |
| 6981 | verificationParams.getOriginatingURI()); |
| 6982 | } |
| 6983 | if (verificationParams.getReferrer() != null) { |
| 6984 | verification.putExtra(Intent.EXTRA_REFERRER, |
| 6985 | verificationParams.getReferrer()); |
| 6986 | } |
Ben Gruver | 37d83a3 | 2012-09-27 13:02:06 -0700 | [diff] [blame] | 6987 | if (verificationParams.getOriginatingUid() >= 0) { |
| 6988 | verification.putExtra(Intent.EXTRA_ORIGINATING_UID, |
| 6989 | verificationParams.getOriginatingUid()); |
| 6990 | } |
rich cannings | 13d428e | 2012-09-13 13:43:07 -0700 | [diff] [blame] | 6991 | if (verificationParams.getInstallerUid() >= 0) { |
| 6992 | verification.putExtra(PackageManager.EXTRA_VERIFICATION_INSTALLER_UID, |
| 6993 | verificationParams.getInstallerUid()); |
| 6994 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 6995 | } |
| 6996 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 6997 | final PackageVerificationState verificationState = new PackageVerificationState( |
| 6998 | requiredUid, args); |
| 6999 | |
| 7000 | mPendingVerification.append(verificationId, verificationState); |
| 7001 | |
| 7002 | final List<ComponentName> sufficientVerifiers = matchVerifiers(pkgLite, |
| 7003 | receivers, verificationState); |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7004 | |
| 7005 | /* |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 7006 | * If any sufficient verifiers were listed in the package |
| 7007 | * manifest, attempt to ask them. |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7008 | */ |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 7009 | if (sufficientVerifiers != null) { |
| 7010 | final int N = sufficientVerifiers.size(); |
| 7011 | if (N == 0) { |
| 7012 | Slog.i(TAG, "Additional verifiers required, but none installed."); |
| 7013 | ret = PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE; |
| 7014 | } else { |
| 7015 | for (int i = 0; i < N; i++) { |
| 7016 | final ComponentName verifierComponent = sufficientVerifiers.get(i); |
| 7017 | |
| 7018 | final Intent sufficientIntent = new Intent(verification); |
| 7019 | sufficientIntent.setComponent(verifierComponent); |
| 7020 | |
rich cannings | d55deb0 | 2012-10-02 14:53:08 -0700 | [diff] [blame] | 7021 | mContext.sendBroadcastAsUser(sufficientIntent, getUser()); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 7022 | } |
| 7023 | } |
| 7024 | } |
| 7025 | |
| 7026 | final ComponentName requiredVerifierComponent = matchComponentForVerifier( |
| 7027 | mRequiredVerifierPackage, receivers); |
| 7028 | if (ret == PackageManager.INSTALL_SUCCEEDED |
| 7029 | && mRequiredVerifierPackage != null) { |
| 7030 | /* |
| 7031 | * Send the intent to the required verification agent, |
| 7032 | * but only start the verification timeout after the |
| 7033 | * target BroadcastReceivers have run. |
| 7034 | */ |
| 7035 | verification.setComponent(requiredVerifierComponent); |
rich cannings | d55deb0 | 2012-10-02 14:53:08 -0700 | [diff] [blame] | 7036 | mContext.sendOrderedBroadcastAsUser(verification, getUser(), |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 7037 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 7038 | new BroadcastReceiver() { |
| 7039 | @Override |
| 7040 | public void onReceive(Context context, Intent intent) { |
| 7041 | final Message msg = mHandler |
| 7042 | .obtainMessage(CHECK_PENDING_VERIFICATION); |
| 7043 | msg.arg1 = verificationId; |
| 7044 | mHandler.sendMessageDelayed(msg, getVerificationTimeout()); |
| 7045 | } |
| 7046 | }, null, 0, null, null); |
| 7047 | |
| 7048 | /* |
| 7049 | * We don't want the copy to proceed until verification |
| 7050 | * succeeds, so null out this field. |
| 7051 | */ |
| 7052 | mArgs = null; |
| 7053 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7054 | } else { |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 7055 | /* |
| 7056 | * No package verification is enabled, so immediately start |
| 7057 | * the remote call to initiate copy using temporary file. |
| 7058 | */ |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7059 | ret = args.copyApk(mContainerService, true); |
| 7060 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7061 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7062 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7063 | mRet = ret; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7064 | } |
| 7065 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7066 | @Override |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7067 | void handleReturnCode() { |
Kenny Root | 6f89fa0 | 2010-07-30 16:33:47 -0700 | [diff] [blame] | 7068 | // If mArgs is null, then MCS couldn't be reached. When it |
| 7069 | // reconnects, it will try again to install. At that point, this |
| 7070 | // will succeed. |
| 7071 | if (mArgs != null) { |
| 7072 | processPendingInstall(mArgs, mRet); |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 7073 | |
Kenny Root | 0b2fa8c | 2012-09-14 10:26:16 -0700 | [diff] [blame] | 7074 | if (mTempPackage != null) { |
| 7075 | if (!mTempPackage.delete()) { |
| 7076 | Slog.w(TAG, "Couldn't delete temporary file: " + |
| 7077 | mTempPackage.getAbsolutePath()); |
| 7078 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 7079 | } |
| 7080 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7081 | } |
| 7082 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7083 | @Override |
| 7084 | void handleServiceError() { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7085 | mArgs = createInstallArgs(this); |
| 7086 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7087 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7088 | |
| 7089 | public boolean isForwardLocked() { |
| 7090 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 7091 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 7092 | |
| 7093 | public Uri getPackageUri() { |
| 7094 | if (mTempPackage != null) { |
| 7095 | return Uri.fromFile(mTempPackage); |
| 7096 | } else { |
| 7097 | return mPackageURI; |
| 7098 | } |
| 7099 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7100 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7101 | |
| 7102 | /* |
| 7103 | * Utility class used in movePackage api. |
| 7104 | * srcArgs and targetArgs are not set for invalid flags and make |
| 7105 | * sure to do null checks when invoking methods on them. |
| 7106 | * We probably want to return ErrorPrams for both failed installs |
| 7107 | * and moves. |
| 7108 | */ |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7109 | class MoveParams extends HandlerParams { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7110 | final IPackageMoveObserver observer; |
| 7111 | final int flags; |
| 7112 | final String packageName; |
| 7113 | final InstallArgs srcArgs; |
| 7114 | final InstallArgs targetArgs; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7115 | int uid; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7116 | int mRet; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7117 | |
| 7118 | MoveParams(InstallArgs srcArgs, IPackageMoveObserver observer, int flags, |
Amith Yamasani | 6ec1012 | 2012-08-23 13:49:25 -0700 | [diff] [blame] | 7119 | String packageName, String dataDir, int uid, UserHandle user) { |
| 7120 | super(user); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7121 | this.srcArgs = srcArgs; |
| 7122 | this.observer = observer; |
| 7123 | this.flags = flags; |
| 7124 | this.packageName = packageName; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7125 | this.uid = uid; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7126 | if (srcArgs != null) { |
| 7127 | Uri packageUri = Uri.fromFile(new File(srcArgs.getCodePath())); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7128 | targetArgs = createInstallArgs(packageUri, flags, packageName, dataDir); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7129 | } else { |
| 7130 | targetArgs = null; |
| 7131 | } |
| 7132 | } |
| 7133 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 7134 | @Override |
| 7135 | public String toString() { |
| 7136 | return "MoveParams{" |
| 7137 | + Integer.toHexString(System.identityHashCode(this)) |
| 7138 | + " " + packageName + "}"; |
| 7139 | } |
| 7140 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7141 | public void handleStartCopy() throws RemoteException { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 7142 | mRet = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7143 | // Check for storage space on target medium |
| 7144 | if (!targetArgs.checkFreeStorage(mContainerService)) { |
| 7145 | Log.w(TAG, "Insufficient storage to install"); |
| 7146 | return; |
| 7147 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7148 | |
| 7149 | mRet = srcArgs.doPreCopy(); |
| 7150 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 7151 | return; |
| 7152 | } |
| 7153 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7154 | mRet = targetArgs.copyApk(mContainerService, false); |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7155 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 7156 | srcArgs.doPostCopy(uid); |
| 7157 | return; |
| 7158 | } |
| 7159 | |
| 7160 | mRet = srcArgs.doPostCopy(uid); |
| 7161 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 7162 | return; |
| 7163 | } |
| 7164 | |
| 7165 | mRet = targetArgs.doPreInstall(mRet); |
| 7166 | if (mRet != PackageManager.INSTALL_SUCCEEDED) { |
| 7167 | return; |
| 7168 | } |
| 7169 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7170 | if (DEBUG_SD_INSTALL) { |
| 7171 | StringBuilder builder = new StringBuilder(); |
| 7172 | if (srcArgs != null) { |
| 7173 | builder.append("src: "); |
| 7174 | builder.append(srcArgs.getCodePath()); |
| 7175 | } |
| 7176 | if (targetArgs != null) { |
| 7177 | builder.append(" target : "); |
| 7178 | builder.append(targetArgs.getCodePath()); |
| 7179 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 7180 | Log.i(TAG, builder.toString()); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7181 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7182 | } |
| 7183 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7184 | @Override |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7185 | void handleReturnCode() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7186 | targetArgs.doPostInstall(mRet, uid); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 7187 | int currentStatus = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 7188 | if (mRet == PackageManager.INSTALL_SUCCEEDED) { |
| 7189 | currentStatus = PackageManager.MOVE_SUCCEEDED; |
| 7190 | } else if (mRet == PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE){ |
| 7191 | currentStatus = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 7192 | } |
| 7193 | processPendingMove(this, currentStatus); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7194 | } |
| 7195 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7196 | @Override |
| 7197 | void handleServiceError() { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7198 | mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7199 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7200 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 7201 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7202 | /** |
| 7203 | * Used during creation of InstallArgs |
| 7204 | * |
| 7205 | * @param flags package installation flags |
| 7206 | * @return true if should be installed on external storage |
| 7207 | */ |
| 7208 | private static boolean installOnSd(int flags) { |
| 7209 | if ((flags & PackageManager.INSTALL_INTERNAL) != 0) { |
| 7210 | return false; |
| 7211 | } |
| 7212 | if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
| 7213 | return true; |
| 7214 | } |
| 7215 | return false; |
| 7216 | } |
| 7217 | |
| 7218 | /** |
| 7219 | * Used during creation of InstallArgs |
| 7220 | * |
| 7221 | * @param flags package installation flags |
| 7222 | * @return true if should be installed as forward locked |
| 7223 | */ |
| 7224 | private static boolean installForwardLocked(int flags) { |
| 7225 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 7226 | } |
| 7227 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 7228 | private InstallArgs createInstallArgs(InstallParams params) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7229 | if (installOnSd(params.flags) || params.isForwardLocked()) { |
| 7230 | return new AsecInstallArgs(params); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 7231 | } else { |
| 7232 | return new FileInstallArgs(params); |
| 7233 | } |
| 7234 | } |
| 7235 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7236 | private InstallArgs createInstallArgs(int flags, String fullCodePath, String fullResourcePath, |
| 7237 | String nativeLibraryPath) { |
Kenny Root | 84e35ea | 2012-06-06 16:05:05 -0700 | [diff] [blame] | 7238 | final boolean isInAsec; |
| 7239 | if (installOnSd(flags)) { |
| 7240 | /* Apps on SD card are always in ASEC containers. */ |
| 7241 | isInAsec = true; |
| 7242 | } else if (installForwardLocked(flags) |
| 7243 | && !fullCodePath.startsWith(mDrmAppPrivateInstallDir.getAbsolutePath())) { |
| 7244 | /* |
| 7245 | * Forward-locked apps are only in ASEC containers if they're the |
| 7246 | * new style |
| 7247 | */ |
| 7248 | isInAsec = true; |
| 7249 | } else { |
| 7250 | isInAsec = false; |
| 7251 | } |
| 7252 | |
| 7253 | if (isInAsec) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7254 | return new AsecInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath, |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7255 | installOnSd(flags), installForwardLocked(flags)); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 7256 | } else { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7257 | return new FileInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 7258 | } |
| 7259 | } |
| 7260 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7261 | // Used by package mover |
| 7262 | private InstallArgs createInstallArgs(Uri packageURI, int flags, String pkgName, String dataDir) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7263 | if (installOnSd(flags) || installForwardLocked(flags)) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7264 | String cid = getNextCodePath(packageURI.getPath(), pkgName, "/" |
| 7265 | + AsecInstallArgs.RES_FILE_NAME); |
| 7266 | return new AsecInstallArgs(packageURI, cid, installOnSd(flags), |
| 7267 | installForwardLocked(flags)); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7268 | } else { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7269 | return new FileInstallArgs(packageURI, pkgName, dataDir); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7270 | } |
| 7271 | } |
| 7272 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7273 | static abstract class InstallArgs { |
| 7274 | final IPackageInstallObserver observer; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7275 | // Always refers to PackageManager flags only |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7276 | final int flags; |
| 7277 | final Uri packageURI; |
| 7278 | final String installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7279 | final ManifestDigest manifestDigest; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7280 | final UserHandle user; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7281 | |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7282 | InstallArgs(Uri packageURI, IPackageInstallObserver observer, int flags, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7283 | String installerPackageName, ManifestDigest manifestDigest, |
| 7284 | UserHandle user) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7285 | this.packageURI = packageURI; |
| 7286 | this.flags = flags; |
| 7287 | this.observer = observer; |
| 7288 | this.installerPackageName = installerPackageName; |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 7289 | this.manifestDigest = manifestDigest; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7290 | this.user = user; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7291 | } |
| 7292 | |
| 7293 | abstract void createCopyFile(); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7294 | abstract int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7295 | abstract int doPreInstall(int status); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7296 | abstract boolean doRename(int status, String pkgName, String oldCodePath); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7297 | |
| 7298 | abstract int doPostInstall(int status, int uid); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7299 | abstract String getCodePath(); |
| 7300 | abstract String getResourcePath(); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7301 | abstract String getNativeLibraryPath(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7302 | // Need installer lock especially for dex file removal. |
| 7303 | abstract void cleanUpResourcesLI(); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7304 | abstract boolean doPostDeleteLI(boolean delete); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 7305 | abstract boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7306 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7307 | /** |
| 7308 | * Called before the source arguments are copied. This is used mostly |
| 7309 | * for MoveParams when it needs to read the source file to put it in the |
| 7310 | * destination. |
| 7311 | */ |
| 7312 | int doPreCopy() { |
| 7313 | return PackageManager.INSTALL_SUCCEEDED; |
| 7314 | } |
| 7315 | |
| 7316 | /** |
| 7317 | * Called after the source arguments are copied. This is used mostly for |
| 7318 | * MoveParams when it needs to read the source file to put it in the |
| 7319 | * destination. |
| 7320 | * |
| 7321 | * @return |
| 7322 | */ |
| 7323 | int doPostCopy(int uid) { |
| 7324 | return PackageManager.INSTALL_SUCCEEDED; |
| 7325 | } |
| 7326 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7327 | protected boolean isFwdLocked() { |
| 7328 | return (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 7329 | } |
rich cannings | d55deb0 | 2012-10-02 14:53:08 -0700 | [diff] [blame] | 7330 | |
| 7331 | UserHandle getUser() { |
| 7332 | return user; |
| 7333 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7334 | } |
| 7335 | |
| 7336 | class FileInstallArgs extends InstallArgs { |
| 7337 | File installDir; |
| 7338 | String codeFileName; |
| 7339 | String resourceFileName; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7340 | String libraryPath; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7341 | boolean created = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7342 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 7343 | FileInstallArgs(InstallParams params) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 7344 | super(params.getPackageUri(), params.observer, params.flags, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7345 | params.installerPackageName, params.getManifestDigest(), |
| 7346 | params.getUser()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7347 | } |
| 7348 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7349 | FileInstallArgs(String fullCodePath, String fullResourcePath, String nativeLibraryPath) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7350 | super(null, null, 0, null, null, null); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7351 | File codeFile = new File(fullCodePath); |
| 7352 | installDir = codeFile.getParentFile(); |
| 7353 | codeFileName = fullCodePath; |
| 7354 | resourceFileName = fullResourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7355 | libraryPath = nativeLibraryPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7356 | } |
| 7357 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7358 | FileInstallArgs(Uri packageURI, String pkgName, String dataDir) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7359 | super(packageURI, null, 0, null, null, null); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7360 | installDir = isFwdLocked() ? mDrmAppPrivateInstallDir : mAppInstallDir; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7361 | String apkName = getNextCodePath(null, pkgName, ".apk"); |
| 7362 | codeFileName = new File(installDir, apkName + ".apk").getPath(); |
| 7363 | resourceFileName = getResourcePathFromCodePath(); |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 7364 | libraryPath = new File(mAppLibInstallDir, pkgName).getPath(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7365 | } |
| 7366 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 7367 | boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 7368 | final long lowThreshold; |
| 7369 | |
| 7370 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 7371 | .getService(DeviceStorageMonitorService.SERVICE); |
| 7372 | if (dsm == null) { |
| 7373 | Log.w(TAG, "Couldn't get low memory threshold; no free limit imposed"); |
| 7374 | lowThreshold = 0L; |
| 7375 | } else { |
| 7376 | if (dsm.isMemoryLow()) { |
| 7377 | Log.w(TAG, "Memory is reported as being too low; aborting package install"); |
| 7378 | return false; |
| 7379 | } |
| 7380 | |
| 7381 | lowThreshold = dsm.getMemoryLowThreshold(); |
| 7382 | } |
| 7383 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 7384 | try { |
| 7385 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 7386 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7387 | return imcs.checkInternalFreeStorage(packageURI, isFwdLocked(), lowThreshold); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 7388 | } finally { |
| 7389 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 7390 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 7391 | } |
| 7392 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 7393 | String getCodePath() { |
| 7394 | return codeFileName; |
| 7395 | } |
| 7396 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7397 | void createCopyFile() { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7398 | installDir = isFwdLocked() ? mDrmAppPrivateInstallDir : mAppInstallDir; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7399 | codeFileName = createTempPackageFile(installDir).getPath(); |
| 7400 | resourceFileName = getResourcePathFromCodePath(); |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 7401 | libraryPath = getLibraryPathFromCodePath(); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7402 | created = true; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7403 | } |
| 7404 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7405 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7406 | if (temp) { |
| 7407 | // Generate temp file name |
| 7408 | createCopyFile(); |
| 7409 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7410 | // Get a ParcelFileDescriptor to write to the output file |
| 7411 | File codeFile = new File(codeFileName); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7412 | if (!created) { |
| 7413 | try { |
| 7414 | codeFile.createNewFile(); |
| 7415 | // Set permissions |
| 7416 | if (!setPermissions()) { |
| 7417 | // Failed setting permissions. |
| 7418 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7419 | } |
| 7420 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7421 | Slog.w(TAG, "Failed to create file " + codeFile); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7422 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7423 | } |
| 7424 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7425 | ParcelFileDescriptor out = null; |
| 7426 | try { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7427 | out = ParcelFileDescriptor.open(codeFile, ParcelFileDescriptor.MODE_READ_WRITE); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7428 | } catch (FileNotFoundException e) { |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 7429 | Slog.e(TAG, "Failed to create file descriptor for : " + codeFileName); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7430 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7431 | } |
| 7432 | // Copy the resource now |
| 7433 | int ret = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7434 | try { |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 7435 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 7436 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 7437 | ret = imcs.copyResource(packageURI, null, out); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7438 | } finally { |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 7439 | IoUtils.closeQuietly(out); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 7440 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7441 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7442 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 7443 | if (isFwdLocked()) { |
| 7444 | final File destResourceFile = new File(getResourcePath()); |
| 7445 | |
| 7446 | // Copy the public files |
| 7447 | try { |
| 7448 | PackageHelper.extractPublicFiles(codeFileName, destResourceFile); |
| 7449 | } catch (IOException e) { |
| 7450 | Slog.e(TAG, "Couldn't create a new zip file for the public parts of a" |
| 7451 | + " forward-locked app."); |
| 7452 | destResourceFile.delete(); |
| 7453 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 7454 | } |
| 7455 | } |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 7456 | |
| 7457 | final File nativeLibraryFile = new File(getNativeLibraryPath()); |
| 7458 | Slog.i(TAG, "Copying native libraries to " + nativeLibraryFile.getPath()); |
| 7459 | if (nativeLibraryFile.exists()) { |
| 7460 | NativeLibraryHelper.removeNativeBinariesFromDirLI(nativeLibraryFile); |
| 7461 | nativeLibraryFile.delete(); |
| 7462 | } |
| 7463 | try { |
| 7464 | int copyRet = copyNativeLibrariesForInternalApp(codeFile, nativeLibraryFile); |
| 7465 | if (copyRet != PackageManager.INSTALL_SUCCEEDED) { |
| 7466 | return copyRet; |
| 7467 | } |
| 7468 | } catch (IOException e) { |
| 7469 | Slog.e(TAG, "Copying native libraries failed", e); |
| 7470 | ret = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 7471 | } |
| 7472 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7473 | return ret; |
| 7474 | } |
| 7475 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7476 | int doPreInstall(int status) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7477 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 7478 | cleanUp(); |
| 7479 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7480 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7481 | } |
| 7482 | |
| 7483 | boolean doRename(int status, final String pkgName, String oldCodePath) { |
| 7484 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 7485 | cleanUp(); |
| 7486 | return false; |
| 7487 | } else { |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 7488 | final File oldCodeFile = new File(getCodePath()); |
| 7489 | final File oldResourceFile = new File(getResourcePath()); |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 7490 | final File oldLibraryFile = new File(getNativeLibraryPath()); |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 7491 | |
| 7492 | // Rename APK file based on packageName |
| 7493 | final String apkName = getNextCodePath(oldCodePath, pkgName, ".apk"); |
| 7494 | final File newCodeFile = new File(installDir, apkName + ".apk"); |
| 7495 | if (!oldCodeFile.renameTo(newCodeFile)) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7496 | return false; |
| 7497 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 7498 | codeFileName = newCodeFile.getPath(); |
| 7499 | |
| 7500 | // Rename public resource file if it's forward-locked. |
| 7501 | final File newResFile = new File(getResourcePathFromCodePath()); |
| 7502 | if (isFwdLocked() && !oldResourceFile.renameTo(newResFile)) { |
| 7503 | return false; |
| 7504 | } |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 7505 | resourceFileName = newResFile.getPath(); |
| 7506 | |
| 7507 | // Rename library path |
| 7508 | final File newLibraryFile = new File(getLibraryPathFromCodePath()); |
| 7509 | if (newLibraryFile.exists()) { |
| 7510 | NativeLibraryHelper.removeNativeBinariesFromDirLI(newLibraryFile); |
| 7511 | newLibraryFile.delete(); |
| 7512 | } |
| 7513 | if (!oldLibraryFile.renameTo(newLibraryFile)) { |
| 7514 | Slog.e(TAG, "Cannot rename native library directory " |
| 7515 | + oldLibraryFile.getPath() + " to " + newLibraryFile.getPath()); |
| 7516 | return false; |
| 7517 | } |
| 7518 | libraryPath = newLibraryFile.getPath(); |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 7519 | |
| 7520 | // Attempt to set permissions |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7521 | if (!setPermissions()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7522 | return false; |
| 7523 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 7524 | |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 7525 | if (!SELinux.restorecon(newCodeFile)) { |
| 7526 | return false; |
| 7527 | } |
| 7528 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7529 | return true; |
| 7530 | } |
| 7531 | } |
| 7532 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7533 | int doPostInstall(int status, int uid) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7534 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 7535 | cleanUp(); |
| 7536 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7537 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7538 | } |
| 7539 | |
| 7540 | String getResourcePath() { |
| 7541 | return resourceFileName; |
| 7542 | } |
| 7543 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 7544 | private String getResourcePathFromCodePath() { |
| 7545 | final String codePath = getCodePath(); |
| 7546 | if (isFwdLocked()) { |
| 7547 | final StringBuilder sb = new StringBuilder(); |
| 7548 | |
| 7549 | sb.append(mAppInstallDir.getPath()); |
| 7550 | sb.append('/'); |
| 7551 | sb.append(getApkName(codePath)); |
| 7552 | sb.append(".zip"); |
| 7553 | |
| 7554 | /* |
| 7555 | * If our APK is a temporary file, mark the resource as a |
| 7556 | * temporary file as well so it can be cleaned up after |
| 7557 | * catastrophic failure. |
| 7558 | */ |
| 7559 | if (codePath.endsWith(".tmp")) { |
| 7560 | sb.append(".tmp"); |
| 7561 | } |
| 7562 | |
| 7563 | return sb.toString(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7564 | } else { |
| 7565 | return codePath; |
| 7566 | } |
| 7567 | } |
| 7568 | |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 7569 | private String getLibraryPathFromCodePath() { |
| 7570 | return new File(mAppLibInstallDir, getApkName(getCodePath())).getPath(); |
| 7571 | } |
| 7572 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7573 | @Override |
| 7574 | String getNativeLibraryPath() { |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 7575 | if (libraryPath == null) { |
| 7576 | libraryPath = getLibraryPathFromCodePath(); |
| 7577 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7578 | return libraryPath; |
| 7579 | } |
| 7580 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7581 | private boolean cleanUp() { |
| 7582 | boolean ret = true; |
| 7583 | String sourceDir = getCodePath(); |
| 7584 | String publicSourceDir = getResourcePath(); |
| 7585 | if (sourceDir != null) { |
| 7586 | File sourceFile = new File(sourceDir); |
| 7587 | if (!sourceFile.exists()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7588 | Slog.w(TAG, "Package source " + sourceDir + " does not exist."); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7589 | ret = false; |
| 7590 | } |
| 7591 | // Delete application's code and resources |
| 7592 | sourceFile.delete(); |
| 7593 | } |
| 7594 | if (publicSourceDir != null && !publicSourceDir.equals(sourceDir)) { |
| 7595 | final File publicSourceFile = new File(publicSourceDir); |
| 7596 | if (!publicSourceFile.exists()) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7597 | Slog.w(TAG, "Package public source " + publicSourceFile + " does not exist."); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7598 | } |
| 7599 | if (publicSourceFile.exists()) { |
| 7600 | publicSourceFile.delete(); |
| 7601 | } |
| 7602 | } |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 7603 | |
| 7604 | if (libraryPath != null) { |
| 7605 | File nativeLibraryFile = new File(libraryPath); |
| 7606 | NativeLibraryHelper.removeNativeBinariesFromDirLI(nativeLibraryFile); |
| 7607 | if (!nativeLibraryFile.delete()) { |
| 7608 | Slog.w(TAG, "Couldn't delete native library directory " + libraryPath); |
| 7609 | } |
| 7610 | } |
| 7611 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7612 | return ret; |
| 7613 | } |
| 7614 | |
| 7615 | void cleanUpResourcesLI() { |
| 7616 | String sourceDir = getCodePath(); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7617 | if (cleanUp()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7618 | int retCode = mInstaller.rmdex(sourceDir); |
| 7619 | if (retCode < 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7620 | Slog.w(TAG, "Couldn't remove dex file for package: " |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7621 | + " at location " |
| 7622 | + sourceDir + ", retcode=" + retCode); |
| 7623 | // we don't consider this to be a failure of the core package deletion |
| 7624 | } |
| 7625 | } |
| 7626 | } |
| 7627 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7628 | private boolean setPermissions() { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7629 | // 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] | 7630 | if (!isFwdLocked()) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7631 | final int filePermissions = |
| 7632 | FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP |
| 7633 | |FileUtils.S_IROTH; |
| 7634 | int retCode = FileUtils.setPermissions(getCodePath(), filePermissions, -1, -1); |
| 7635 | if (retCode != 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7636 | Slog.e(TAG, "Couldn't set new package file permissions for " + |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7637 | getCodePath() |
| 7638 | + ". The return code was: " + retCode); |
| 7639 | // TODO Define new internal error |
| 7640 | return false; |
| 7641 | } |
| 7642 | return true; |
| 7643 | } |
| 7644 | return true; |
| 7645 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7646 | |
| 7647 | boolean doPostDeleteLI(boolean delete) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7648 | // XXX err, shouldn't we respect the delete flag? |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7649 | cleanUpResourcesLI(); |
| 7650 | return true; |
| 7651 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7652 | } |
| 7653 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 7654 | private boolean isAsecExternal(String cid) { |
| 7655 | final String asecPath = PackageHelper.getSdFilesystem(cid); |
| 7656 | return !asecPath.startsWith(mAsecInternalPath); |
| 7657 | } |
| 7658 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 7659 | /** |
| 7660 | * Extract the MountService "container ID" from the full code path of an |
| 7661 | * .apk. |
| 7662 | */ |
| 7663 | static String cidFromCodePath(String fullCodePath) { |
| 7664 | int eidx = fullCodePath.lastIndexOf("/"); |
| 7665 | String subStr1 = fullCodePath.substring(0, eidx); |
| 7666 | int sidx = subStr1.lastIndexOf("/"); |
| 7667 | return subStr1.substring(sidx+1, eidx); |
| 7668 | } |
| 7669 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7670 | class AsecInstallArgs extends InstallArgs { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7671 | static final String RES_FILE_NAME = "pkg.apk"; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7672 | static final String PUBLIC_RES_FILE_NAME = "res.zip"; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7673 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7674 | String cid; |
| 7675 | String packagePath; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7676 | String resourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7677 | String libraryPath; |
| 7678 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7679 | AsecInstallArgs(InstallParams params) { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 7680 | super(params.getPackageUri(), params.observer, params.flags, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7681 | params.installerPackageName, params.getManifestDigest(), |
| 7682 | params.getUser()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7683 | } |
| 7684 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7685 | AsecInstallArgs(String fullCodePath, String fullResourcePath, String nativeLibraryPath, |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7686 | boolean isExternal, boolean isForwardLocked) { |
| 7687 | super(null, null, (isExternal ? PackageManager.INSTALL_EXTERNAL : 0) |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7688 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), |
| 7689 | null, null, null); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7690 | // Extract cid from fullCodePath |
| 7691 | int eidx = fullCodePath.lastIndexOf("/"); |
| 7692 | String subStr1 = fullCodePath.substring(0, eidx); |
| 7693 | int sidx = subStr1.lastIndexOf("/"); |
| 7694 | cid = subStr1.substring(sidx+1, eidx); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7695 | setCachePath(subStr1); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7696 | } |
| 7697 | |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 7698 | AsecInstallArgs(String cid, boolean isForwardLocked) { |
| 7699 | super(null, null, (isAsecExternal(cid) ? PackageManager.INSTALL_EXTERNAL : 0) |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7700 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), |
| 7701 | null, null, null); |
Dianne Hackborn | aa77de1 | 2010-05-14 22:33:54 -0700 | [diff] [blame] | 7702 | this.cid = cid; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7703 | setCachePath(PackageHelper.getSdDir(cid)); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7704 | } |
| 7705 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7706 | AsecInstallArgs(Uri packageURI, String cid, boolean isExternal, boolean isForwardLocked) { |
| 7707 | super(packageURI, null, (isExternal ? PackageManager.INSTALL_EXTERNAL : 0) |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 7708 | | (isForwardLocked ? PackageManager.INSTALL_FORWARD_LOCK : 0), |
| 7709 | null, null, null); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7710 | this.cid = cid; |
| 7711 | } |
| 7712 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7713 | void createCopyFile() { |
| 7714 | cid = getTempContainerId(); |
| 7715 | } |
| 7716 | |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 7717 | boolean checkFreeStorage(IMediaContainerService imcs) throws RemoteException { |
| 7718 | try { |
| 7719 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 7720 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7721 | return imcs.checkExternalFreeStorage(packageURI, isFwdLocked()); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 7722 | } finally { |
| 7723 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 7724 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 7725 | } |
| 7726 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7727 | private final boolean isExternal() { |
| 7728 | return (flags & PackageManager.INSTALL_EXTERNAL) != 0; |
| 7729 | } |
| 7730 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7731 | int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7732 | if (temp) { |
| 7733 | createCopyFile(); |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 7734 | } else { |
| 7735 | /* |
| 7736 | * Pre-emptively destroy the container since it's destroyed if |
| 7737 | * copying fails due to it existing anyway. |
| 7738 | */ |
| 7739 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7740 | } |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 7741 | |
| 7742 | final String newCachePath; |
| 7743 | try { |
| 7744 | mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, packageURI, |
| 7745 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7746 | newCachePath = imcs.copyResourceToContainer(packageURI, cid, getEncryptKey(), |
| 7747 | RES_FILE_NAME, PUBLIC_RES_FILE_NAME, isExternal(), isFwdLocked()); |
Kenny Root | 1112857 | 2010-10-11 10:51:32 -0700 | [diff] [blame] | 7748 | } finally { |
| 7749 | mContext.revokeUriPermission(packageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 7750 | } |
| 7751 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7752 | if (newCachePath != null) { |
| 7753 | setCachePath(newCachePath); |
| 7754 | return PackageManager.INSTALL_SUCCEEDED; |
| 7755 | } else { |
| 7756 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 7757 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7758 | } |
| 7759 | |
| 7760 | @Override |
| 7761 | String getCodePath() { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7762 | return packagePath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7763 | } |
| 7764 | |
| 7765 | @Override |
| 7766 | String getResourcePath() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7767 | return resourcePath; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7768 | } |
| 7769 | |
| 7770 | @Override |
| 7771 | String getNativeLibraryPath() { |
| 7772 | return libraryPath; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7773 | } |
| 7774 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7775 | int doPreInstall(int status) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7776 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 7777 | // Destroy container |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 7778 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7779 | } else { |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 7780 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7781 | if (!mounted) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7782 | String newCachePath = PackageHelper.mountSdDir(cid, getEncryptKey(), |
| 7783 | Process.SYSTEM_UID); |
| 7784 | if (newCachePath != null) { |
| 7785 | setCachePath(newCachePath); |
| 7786 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7787 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 7788 | } |
| 7789 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7790 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7791 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7792 | } |
| 7793 | |
| 7794 | boolean doRename(int status, final String pkgName, |
| 7795 | String oldCodePath) { |
| 7796 | String newCacheId = getNextCodePath(oldCodePath, pkgName, "/" + RES_FILE_NAME); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7797 | String newCachePath = null; |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7798 | if (PackageHelper.isContainerMounted(cid)) { |
| 7799 | // Unmount the container |
| 7800 | if (!PackageHelper.unMountSdDir(cid)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7801 | Slog.i(TAG, "Failed to unmount " + cid + " before renaming"); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7802 | return false; |
| 7803 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7804 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7805 | if (!PackageHelper.renameSdDir(cid, newCacheId)) { |
Suchi Amalapurapu | c7537ee | 2010-03-24 09:27:19 -0700 | [diff] [blame] | 7806 | Slog.e(TAG, "Failed to rename " + cid + " to " + newCacheId + |
| 7807 | " which might be stale. Will try to clean up."); |
| 7808 | // Clean up the stale container and proceed to recreate. |
| 7809 | if (!PackageHelper.destroySdDir(newCacheId)) { |
| 7810 | Slog.e(TAG, "Very strange. Cannot clean up stale container " + newCacheId); |
| 7811 | return false; |
| 7812 | } |
| 7813 | // Successfully cleaned up stale container. Try to rename again. |
| 7814 | if (!PackageHelper.renameSdDir(cid, newCacheId)) { |
| 7815 | Slog.e(TAG, "Failed to rename " + cid + " to " + newCacheId |
| 7816 | + " inspite of cleaning it up."); |
| 7817 | return false; |
| 7818 | } |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7819 | } |
| 7820 | if (!PackageHelper.isContainerMounted(newCacheId)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7821 | Slog.w(TAG, "Mounting container " + newCacheId); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7822 | newCachePath = PackageHelper.mountSdDir(newCacheId, |
| 7823 | getEncryptKey(), Process.SYSTEM_UID); |
| 7824 | } else { |
| 7825 | newCachePath = PackageHelper.getSdDir(newCacheId); |
| 7826 | } |
| 7827 | if (newCachePath == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 7828 | Slog.w(TAG, "Failed to get cache path for " + newCacheId); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7829 | return false; |
| 7830 | } |
| 7831 | Log.i(TAG, "Succesfully renamed " + cid + |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7832 | " to " + newCacheId + |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7833 | " at new path: " + newCachePath); |
| 7834 | cid = newCacheId; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7835 | setCachePath(newCachePath); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 7836 | return true; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7837 | } |
| 7838 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7839 | private void setCachePath(String newCachePath) { |
| 7840 | File cachePath = new File(newCachePath); |
| 7841 | libraryPath = new File(cachePath, LIB_DIR_NAME).getPath(); |
| 7842 | packagePath = new File(cachePath, RES_FILE_NAME).getPath(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7843 | |
| 7844 | if (isFwdLocked()) { |
| 7845 | resourcePath = new File(cachePath, PUBLIC_RES_FILE_NAME).getPath(); |
| 7846 | } else { |
| 7847 | resourcePath = packagePath; |
| 7848 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 7849 | } |
| 7850 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7851 | int doPostInstall(int status, int uid) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7852 | if (status != PackageManager.INSTALL_SUCCEEDED) { |
| 7853 | cleanUp(); |
| 7854 | } else { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7855 | final int groupOwner; |
| 7856 | final String protectedFile; |
| 7857 | if (isFwdLocked()) { |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 7858 | groupOwner = UserHandle.getSharedAppGid(uid); |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7859 | protectedFile = RES_FILE_NAME; |
| 7860 | } else { |
| 7861 | groupOwner = -1; |
| 7862 | protectedFile = null; |
| 7863 | } |
| 7864 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7865 | if (uid < Process.FIRST_APPLICATION_UID |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7866 | || !PackageHelper.fixSdPermissions(cid, groupOwner, protectedFile)) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7867 | Slog.e(TAG, "Failed to finalize " + cid); |
| 7868 | PackageHelper.destroySdDir(cid); |
| 7869 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 7870 | } |
| 7871 | |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 7872 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7873 | if (!mounted) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7874 | PackageHelper.mountSdDir(cid, getEncryptKey(), Process.myUid()); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7875 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7876 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7877 | return status; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7878 | } |
| 7879 | |
| 7880 | private void cleanUp() { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 7881 | if (DEBUG_SD_INSTALL) Slog.i(TAG, "cleanUp"); |
| 7882 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7883 | // Destroy secure container |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 7884 | PackageHelper.destroySdDir(cid); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7885 | } |
| 7886 | |
| 7887 | void cleanUpResourcesLI() { |
| 7888 | String sourceFile = getCodePath(); |
| 7889 | // Remove dex file |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 7890 | int retCode = mInstaller.rmdex(sourceFile); |
| 7891 | if (retCode < 0) { |
| 7892 | Slog.w(TAG, "Couldn't remove dex file for package: " |
| 7893 | + " at location " |
| 7894 | + sourceFile.toString() + ", retcode=" + retCode); |
| 7895 | // 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] | 7896 | } |
| 7897 | cleanUp(); |
| 7898 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7899 | |
| 7900 | boolean matchContainer(String app) { |
| 7901 | if (cid.startsWith(app)) { |
| 7902 | return true; |
| 7903 | } |
| 7904 | return false; |
| 7905 | } |
| 7906 | |
| 7907 | String getPackageName() { |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 7908 | return getAsecPackageName(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7909 | } |
| 7910 | |
| 7911 | boolean doPostDeleteLI(boolean delete) { |
| 7912 | boolean ret = false; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 7913 | boolean mounted = PackageHelper.isContainerMounted(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7914 | if (mounted) { |
| 7915 | // Unmount first |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 7916 | ret = PackageHelper.unMountSdDir(cid); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7917 | } |
| 7918 | if (ret && delete) { |
| 7919 | cleanUpResourcesLI(); |
| 7920 | } |
| 7921 | return ret; |
| 7922 | } |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7923 | |
| 7924 | @Override |
| 7925 | int doPreCopy() { |
| 7926 | if (isFwdLocked()) { |
| 7927 | if (!PackageHelper.fixSdPermissions(cid, |
| 7928 | getPackageUid(DEFAULT_CONTAINER_PACKAGE, 0), RES_FILE_NAME)) { |
| 7929 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 7930 | } |
| 7931 | } |
| 7932 | |
| 7933 | return PackageManager.INSTALL_SUCCEEDED; |
| 7934 | } |
| 7935 | |
| 7936 | @Override |
| 7937 | int doPostCopy(int uid) { |
| 7938 | if (isFwdLocked()) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7939 | if (uid < Process.FIRST_APPLICATION_UID |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 7940 | || !PackageHelper.fixSdPermissions(cid, UserHandle.getSharedAppGid(uid), |
| 7941 | RES_FILE_NAME)) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 7942 | Slog.e(TAG, "Failed to finalize " + cid); |
| 7943 | PackageHelper.destroySdDir(cid); |
| 7944 | return PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 7945 | } |
| 7946 | } |
| 7947 | |
| 7948 | return PackageManager.INSTALL_SUCCEEDED; |
| 7949 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7950 | }; |
| 7951 | |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 7952 | static String getAsecPackageName(String packageCid) { |
| 7953 | int idx = packageCid.lastIndexOf("-"); |
| 7954 | if (idx == -1) { |
| 7955 | return packageCid; |
| 7956 | } |
| 7957 | return packageCid.substring(0, idx); |
| 7958 | } |
| 7959 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7960 | // Utility method used to create code paths based on package name and available index. |
| 7961 | private static String getNextCodePath(String oldCodePath, String prefix, String suffix) { |
| 7962 | String idxStr = ""; |
| 7963 | int idx = 1; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7964 | // Fall back to default value of idx=1 if prefix is not |
| 7965 | // part of oldCodePath |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7966 | if (oldCodePath != null) { |
Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 7967 | String subStr = oldCodePath; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7968 | // Drop the suffix right away |
Bjorn Bringert | 5fd5bfe | 2010-01-29 12:11:30 +0000 | [diff] [blame] | 7969 | if (subStr.endsWith(suffix)) { |
| 7970 | subStr = subStr.substring(0, subStr.length() - suffix.length()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7971 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7972 | // If oldCodePath already contains prefix find out the |
| 7973 | // ending index to either increment or decrement. |
| 7974 | int sidx = subStr.lastIndexOf(prefix); |
| 7975 | if (sidx != -1) { |
| 7976 | subStr = subStr.substring(sidx + prefix.length()); |
| 7977 | if (subStr != null) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7978 | if (subStr.startsWith(INSTALL_PACKAGE_SUFFIX)) { |
| 7979 | subStr = subStr.substring(INSTALL_PACKAGE_SUFFIX.length()); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7980 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 7981 | try { |
| 7982 | idx = Integer.parseInt(subStr); |
| 7983 | if (idx <= 1) { |
| 7984 | idx++; |
| 7985 | } else { |
| 7986 | idx--; |
| 7987 | } |
| 7988 | } catch(NumberFormatException e) { |
| 7989 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7990 | } |
| 7991 | } |
| 7992 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7993 | idxStr = INSTALL_PACKAGE_SUFFIX + Integer.toString(idx); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 7994 | return prefix + idxStr; |
| 7995 | } |
| 7996 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 7997 | // Utility method used to ignore ADD/REMOVE events |
| 7998 | // by directory observer. |
| 7999 | private static boolean ignoreCodePath(String fullPathStr) { |
| 8000 | String apkName = getApkName(fullPathStr); |
| 8001 | int idx = apkName.lastIndexOf(INSTALL_PACKAGE_SUFFIX); |
| 8002 | if (idx != -1 && ((idx+1) < apkName.length())) { |
| 8003 | // Make sure the package ends with a numeral |
| 8004 | String version = apkName.substring(idx+1); |
| 8005 | try { |
| 8006 | Integer.parseInt(version); |
| 8007 | return true; |
| 8008 | } catch (NumberFormatException e) {} |
| 8009 | } |
| 8010 | return false; |
| 8011 | } |
| 8012 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8013 | // Utility method that returns the relative package path with respect |
| 8014 | // to the installation directory. Like say for /data/data/com.test-1.apk |
| 8015 | // string com.test-1 is returned. |
| 8016 | static String getApkName(String codePath) { |
| 8017 | if (codePath == null) { |
| 8018 | return null; |
| 8019 | } |
| 8020 | int sidx = codePath.lastIndexOf("/"); |
| 8021 | int eidx = codePath.lastIndexOf("."); |
| 8022 | if (eidx == -1) { |
| 8023 | eidx = codePath.length(); |
| 8024 | } else if (eidx == 0) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8025 | Slog.w(TAG, " Invalid code path, "+ codePath + " Not a valid apk name"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8026 | return null; |
| 8027 | } |
| 8028 | return codePath.substring(sidx+1, eidx); |
| 8029 | } |
| 8030 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8031 | class PackageInstalledInfo { |
| 8032 | String name; |
| 8033 | int uid; |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 8034 | // The set of users that originally had this package installed. |
| 8035 | int[] origUsers; |
| 8036 | // The set of users that now have this package installed. |
| 8037 | int[] newUsers; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8038 | PackageParser.Package pkg; |
| 8039 | int returnCode; |
| 8040 | PackageRemovedInfo removedInfo; |
| 8041 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8042 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8043 | /* |
| 8044 | * Install a non-existing package. |
| 8045 | */ |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8046 | private void installNewPackageLI(PackageParser.Package pkg, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8047 | int parseFlags, int scanMode, UserHandle user, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8048 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8049 | // Remember this for later, in case we need to rollback this install |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8050 | String pkgName = pkg.packageName; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 8051 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8052 | if (DEBUG_INSTALL) Slog.d(TAG, "installNewPackageLI: " + pkg); |
Dave Burke | 383fa18 | 2012-10-23 23:12:19 -0700 | [diff] [blame] | 8053 | boolean dataDirExists = getDataPathForPackage(pkg.packageName, 0).exists(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8054 | synchronized(mPackages) { |
Dianne Hackborn | e259bc7 | 2010-03-30 19:24:44 -0700 | [diff] [blame] | 8055 | if (mSettings.mRenamedPackages.containsKey(pkgName)) { |
| 8056 | // A package with the same name is already installed, though |
| 8057 | // it has been renamed to an older name. The package we |
| 8058 | // are trying to install should be installed as an update to |
| 8059 | // the existing one, but that has not been requested, so bail. |
| 8060 | Slog.w(TAG, "Attempt to re-install " + pkgName |
| 8061 | + " without first uninstalling package running as " |
| 8062 | + mSettings.mRenamedPackages.get(pkgName)); |
| 8063 | res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 8064 | return; |
| 8065 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8066 | if (mPackages.containsKey(pkgName) || mAppDirs.containsKey(pkg.mPath)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8067 | // Don't allow installation over an existing package with the same name. |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8068 | Slog.w(TAG, "Attempt to re-install " + pkgName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8069 | + " without first uninstalling."); |
| 8070 | res.returnCode = PackageManager.INSTALL_FAILED_ALREADY_EXISTS; |
| 8071 | return; |
| 8072 | } |
| 8073 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8074 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 8075 | PackageParser.Package newPackage = scanPackageLI(pkg, parseFlags, scanMode, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8076 | System.currentTimeMillis(), user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8077 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8078 | 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] | 8079 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 8080 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 8081 | } |
| 8082 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8083 | updateSettingsLI(newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8084 | installerPackageName, |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8085 | null, null, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8086 | res); |
| 8087 | // delete the partially installed application. the data directory will have to be |
| 8088 | // restored if it was already existing |
| 8089 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
| 8090 | // remove package from internal structures. Note that we want deletePackageX to |
| 8091 | // delete the package data and cache directories that it created in |
| 8092 | // scanPackageLocked, unless those directories existed before we even tried to |
| 8093 | // install. |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8094 | deletePackageLI(pkgName, UserHandle.ALL, false, null, null, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8095 | dataDirExists ? PackageManager.DELETE_KEEP_DATA : 0, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8096 | res.removedInfo, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8097 | } |
| 8098 | } |
| 8099 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8100 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8101 | private void replacePackageLI(PackageParser.Package pkg, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8102 | int parseFlags, int scanMode, UserHandle user, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8103 | String installerPackageName, PackageInstalledInfo res) { |
| 8104 | |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8105 | PackageParser.Package oldPackage; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8106 | String pkgName = pkg.packageName; |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8107 | int[] allUsers; |
| 8108 | boolean[] perUserInstalled; |
| 8109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8110 | // First find the old package info and check signatures |
| 8111 | synchronized(mPackages) { |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8112 | oldPackage = mPackages.get(pkgName); |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8113 | if (DEBUG_INSTALL) Slog.d(TAG, "replacePackageLI: new=" + pkg + ", old=" + oldPackage); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8114 | if (compareSignatures(oldPackage.mSignatures, pkg.mSignatures) |
Dianne Hackborn | 766cbfe | 2009-08-12 18:33:39 -0700 | [diff] [blame] | 8115 | != PackageManager.SIGNATURE_MATCH) { |
Kenny Root | 8c11f1d | 2012-04-14 12:10:38 -0700 | [diff] [blame] | 8116 | 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] | 8117 | res.returnCode = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES; |
| 8118 | return; |
| 8119 | } |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8120 | |
| 8121 | // In case of rollback, remember per-user/profile install state |
| 8122 | PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 8123 | allUsers = sUserManager.getUserIds(); |
| 8124 | perUserInstalled = new boolean[allUsers.length]; |
| 8125 | for (int i = 0; i < allUsers.length; i++) { |
Dianne Hackborn | 2c44c74 | 2013-05-17 12:07:21 -0700 | [diff] [blame] | 8126 | perUserInstalled[i] = ps != null ? ps.getInstalled(allUsers[i]) : false; |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8127 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8128 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 8129 | boolean sysPkg = (isSystemApp(oldPackage)); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8130 | if (sysPkg) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8131 | replaceSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8132 | user, allUsers, perUserInstalled, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8133 | } else { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8134 | replaceNonSystemPackageLI(oldPackage, pkg, parseFlags, scanMode, |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8135 | user, allUsers, perUserInstalled, installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8136 | } |
| 8137 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8139 | private void replaceNonSystemPackageLI(PackageParser.Package deletedPackage, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8140 | PackageParser.Package pkg, int parseFlags, int scanMode, UserHandle user, |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8141 | int[] allUsers, boolean[] perUserInstalled, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8142 | String installerPackageName, PackageInstalledInfo res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8143 | PackageParser.Package newPackage = null; |
| 8144 | String pkgName = deletedPackage.packageName; |
| 8145 | boolean deletedPkg = true; |
| 8146 | boolean updatedSettings = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8147 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8148 | if (DEBUG_INSTALL) Slog.d(TAG, "replaceNonSystemPackageLI: new=" + pkg + ", old=" |
| 8149 | + deletedPackage); |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 8150 | long origUpdateTime; |
| 8151 | if (pkg.mExtras != null) { |
| 8152 | origUpdateTime = ((PackageSetting)pkg.mExtras).lastUpdateTime; |
| 8153 | } else { |
| 8154 | origUpdateTime = 0; |
| 8155 | } |
| 8156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8157 | // First delete the existing package while retaining the data directory |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8158 | if (!deletePackageLI(pkgName, null, true, null, null, PackageManager.DELETE_KEEP_DATA, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8159 | res.removedInfo, true)) { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 8160 | // If the existing package wasn't successfully deleted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8161 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 8162 | deletedPkg = false; |
| 8163 | } else { |
| 8164 | // Successfully deleted the old package. Now proceed with re-installation |
| 8165 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 8166 | newPackage = scanPackageLI(pkg, parseFlags, scanMode | SCAN_UPDATE_TIME, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8167 | System.currentTimeMillis(), user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8168 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8169 | 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] | 8170 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 8171 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
Suchi Amalapurapu | 110fea7 | 2010-01-14 17:50:23 -0800 | [diff] [blame] | 8172 | } |
| 8173 | } else { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8174 | updateSettingsLI(newPackage, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8175 | installerPackageName, |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8176 | allUsers, perUserInstalled, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8177 | res); |
| 8178 | updatedSettings = true; |
| 8179 | } |
| 8180 | } |
| 8181 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 8182 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8183 | // remove package from internal structures. Note that we want deletePackageX to |
| 8184 | // delete the package data and cache directories that it created in |
| 8185 | // scanPackageLocked, unless those directories existed before we even tried to |
| 8186 | // install. |
| 8187 | if(updatedSettings) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8188 | if (DEBUG_INSTALL) Slog.d(TAG, "Install failed, rolling pack: " + pkgName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8189 | deletePackageLI( |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8190 | pkgName, null, true, allUsers, perUserInstalled, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8191 | PackageManager.DELETE_KEEP_DATA, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8192 | res.removedInfo, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8193 | } |
| 8194 | // Since we failed to install the new package we need to restore the old |
| 8195 | // package that we deleted. |
| 8196 | if(deletedPkg) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8197 | if (DEBUG_INSTALL) Slog.d(TAG, "Install failed, reinstalling: " + deletedPackage); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 8198 | File restoreFile = new File(deletedPackage.mPath); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8199 | // Parse old package |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 8200 | boolean oldOnSd = isExternal(deletedPackage); |
| 8201 | int oldParseFlags = mDefParseFlags | PackageParser.PARSE_CHATTY | |
| 8202 | (isForwardLocked(deletedPackage) ? PackageParser.PARSE_FORWARD_LOCK : 0) | |
| 8203 | (oldOnSd ? PackageParser.PARSE_ON_SDCARD : 0); |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 8204 | int oldScanMode = (oldOnSd ? 0 : SCAN_MONITOR) | SCAN_UPDATE_SIGNATURE |
| 8205 | | SCAN_UPDATE_TIME; |
| 8206 | if (scanPackageLI(restoreFile, oldParseFlags, oldScanMode, |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 8207 | origUpdateTime, null) == null) { |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 8208 | Slog.e(TAG, "Failed to restore package : " + pkgName + " after failed upgrade"); |
| 8209 | return; |
| 8210 | } |
| 8211 | // Restore of old package succeeded. Update permissions. |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8212 | // writer |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8213 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8214 | updatePermissionsLPw(deletedPackage.packageName, deletedPackage, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 8215 | UPDATE_PERMISSIONS_ALL); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8216 | // can downgrade to reader |
| 8217 | mSettings.writeLPr(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8218 | } |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 8219 | 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] | 8220 | } |
| 8221 | } |
| 8222 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8223 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8224 | private void replaceSystemPackageLI(PackageParser.Package deletedPackage, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8225 | PackageParser.Package pkg, int parseFlags, int scanMode, UserHandle user, |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8226 | int[] allUsers, boolean[] perUserInstalled, |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8227 | String installerPackageName, PackageInstalledInfo res) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8228 | if (DEBUG_INSTALL) Slog.d(TAG, "replaceSystemPackageLI: new=" + pkg |
| 8229 | + ", old=" + deletedPackage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8230 | PackageParser.Package newPackage = null; |
| 8231 | boolean updatedSettings = false; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8232 | parseFlags |= PackageManager.INSTALL_REPLACE_EXISTING | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8233 | PackageParser.PARSE_IS_SYSTEM; |
| 8234 | String packageName = deletedPackage.packageName; |
| 8235 | res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE; |
| 8236 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8237 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8238 | return; |
| 8239 | } |
| 8240 | PackageParser.Package oldPkg; |
| 8241 | PackageSetting oldPkgSetting; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8242 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8243 | synchronized (mPackages) { |
| 8244 | oldPkg = mPackages.get(packageName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8245 | oldPkgSetting = mSettings.mPackages.get(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8246 | if((oldPkg == null) || (oldPkg.applicationInfo == null) || |
| 8247 | (oldPkgSetting == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8248 | Slog.w(TAG, "Couldn't find package:"+packageName+" information"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8249 | return; |
| 8250 | } |
| 8251 | } |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 8252 | |
| 8253 | killApplication(packageName, oldPkg.applicationInfo.uid); |
| 8254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8255 | res.removedInfo.uid = oldPkg.applicationInfo.uid; |
| 8256 | res.removedInfo.removedPackage = packageName; |
| 8257 | // Remove existing system package |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8258 | removePackageLI(oldPkgSetting, true); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8259 | // writer |
Dianne Hackborn | 3ec27e0 | 2010-12-16 12:41:56 -0800 | [diff] [blame] | 8260 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8261 | if (!mSettings.disableSystemPackageLPw(packageName) && deletedPackage != null) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 8262 | // We didn't need to disable the .apk as a current system package, |
| 8263 | // which means we are replacing another update that is already |
| 8264 | // 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] | 8265 | res.removedInfo.args = createInstallArgs(0, |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 8266 | deletedPackage.applicationInfo.sourceDir, |
| 8267 | deletedPackage.applicationInfo.publicSourceDir, |
| 8268 | deletedPackage.applicationInfo.nativeLibraryDir); |
| 8269 | } else { |
| 8270 | res.removedInfo.args = null; |
| 8271 | } |
Dianne Hackborn | 3ec27e0 | 2010-12-16 12:41:56 -0800 | [diff] [blame] | 8272 | } |
| 8273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8274 | // Successfully disabled the old package. Now proceed with re-installation |
| 8275 | mLastScanError = PackageManager.INSTALL_SUCCEEDED; |
| 8276 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8277 | newPackage = scanPackageLI(pkg, parseFlags, scanMode, 0, user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8278 | if (newPackage == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8279 | 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] | 8280 | if ((res.returnCode=mLastScanError) == PackageManager.INSTALL_SUCCEEDED) { |
| 8281 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_APK; |
| 8282 | } |
| 8283 | } else { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 8284 | if (newPackage.mExtras != null) { |
| 8285 | final PackageSetting newPkgSetting = (PackageSetting)newPackage.mExtras; |
| 8286 | newPkgSetting.firstInstallTime = oldPkgSetting.firstInstallTime; |
| 8287 | newPkgSetting.lastUpdateTime = System.currentTimeMillis(); |
| 8288 | } |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8289 | updateSettingsLI(newPackage, installerPackageName, allUsers, perUserInstalled, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8290 | updatedSettings = true; |
| 8291 | } |
| 8292 | |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 8293 | if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8294 | // Re installation failed. Restore old information |
| 8295 | // Remove new pkg information |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 8296 | if (newPackage != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8297 | removeInstalledPackageLI(newPackage, true); |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 8298 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8299 | // Add back the old system package |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8300 | scanPackageLI(oldPkg, parseFlags, SCAN_MONITOR | SCAN_UPDATE_SIGNATURE, 0, user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8301 | // Restore the old system information in Settings |
| 8302 | synchronized(mPackages) { |
Dianne Hackborn | 78d688369 | 2010-10-07 01:12:46 -0700 | [diff] [blame] | 8303 | if (updatedSettings) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8304 | mSettings.enableSystemPackageLPw(packageName); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8305 | mSettings.setInstallerPackageName(packageName, |
| 8306 | oldPkgSetting.installerPackageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8307 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8308 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8309 | } |
| 8310 | } |
| 8311 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8312 | |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 8313 | // Utility method used to move dex files during install. |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8314 | private int moveDexFilesLI(PackageParser.Package newPackage) { |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 8315 | int retCode; |
| 8316 | if ((newPackage.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) { |
| 8317 | retCode = mInstaller.movedex(newPackage.mScanPath, newPackage.mPath); |
| 8318 | if (retCode != 0) { |
Kenny Root | e2f7417 | 2010-08-04 13:35:33 -0700 | [diff] [blame] | 8319 | if (mNoDexOpt) { |
| 8320 | /* |
| 8321 | * If we're in an engineering build, programs are lazily run |
| 8322 | * through dexopt. If the .dex file doesn't exist yet, it |
| 8323 | * will be created when the program is run next. |
| 8324 | */ |
| 8325 | Slog.i(TAG, "dex file doesn't exist, skipping move: " + newPackage.mPath); |
| 8326 | } else { |
| 8327 | Slog.e(TAG, "Couldn't rename dex file: " + newPackage.mPath); |
| 8328 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 8329 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 8330 | } |
| 8331 | } |
| 8332 | return PackageManager.INSTALL_SUCCEEDED; |
| 8333 | } |
| 8334 | |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8335 | private void updateSettingsLI(PackageParser.Package newPackage, String installerPackageName, |
| 8336 | int[] allUsers, boolean[] perUserInstalled, |
| 8337 | PackageInstalledInfo res) { |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8338 | String pkgName = newPackage.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8339 | synchronized (mPackages) { |
| 8340 | //write settings. the installStatus will be incomplete at this stage. |
| 8341 | //note that the new package setting would have already been |
| 8342 | //added to mPackages. It hasn't been persisted yet. |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 8343 | mSettings.setInstallStatus(pkgName, PackageSettingBase.PKG_INSTALL_INCOMPLETE); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8344 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8345 | } |
| 8346 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8347 | if ((res.returnCode = moveDexFilesLI(newPackage)) |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 8348 | != PackageManager.INSTALL_SUCCEEDED) { |
| 8349 | // Discontinue if moving dex files failed. |
| 8350 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8351 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8352 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8353 | if (DEBUG_INSTALL) Slog.d(TAG, "New package installed in " + newPackage.mPath); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8354 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8355 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8356 | updatePermissionsLPw(newPackage.packageName, newPackage, |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 8357 | UPDATE_PERMISSIONS_REPLACE_PKG | (newPackage.permissions.size() > 0 |
| 8358 | ? UPDATE_PERMISSIONS_ALL : 0)); |
Christopher Tate | 32a019d | 2013-04-04 15:40:04 -0700 | [diff] [blame] | 8359 | // For system-bundled packages, we assume that installing an upgraded version |
| 8360 | // of the package implies that the user actually wants to run that new code, |
| 8361 | // so we enable the package. |
| 8362 | if (isSystemApp(newPackage)) { |
| 8363 | // NB: implicit assumption that system package upgrades apply to all users |
| 8364 | if (DEBUG_INSTALL) { |
| 8365 | Slog.d(TAG, "Implicitly enabling system package on upgrade: " + pkgName); |
| 8366 | } |
| 8367 | PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 8368 | if (ps != null) { |
| 8369 | if (res.origUsers != null) { |
| 8370 | for (int userHandle : res.origUsers) { |
| 8371 | ps.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, |
| 8372 | userHandle, installerPackageName); |
| 8373 | } |
| 8374 | } |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8375 | // Also convey the prior install/uninstall state |
| 8376 | if (allUsers != null && perUserInstalled != null) { |
| 8377 | for (int i = 0; i < allUsers.length; i++) { |
| 8378 | if (DEBUG_INSTALL) { |
| 8379 | Slog.d(TAG, " user " + allUsers[i] |
| 8380 | + " => " + perUserInstalled[i]); |
| 8381 | } |
| 8382 | ps.setInstalled(perUserInstalled[i], allUsers[i]); |
| 8383 | } |
| 8384 | // these install state changes will be persisted in the |
| 8385 | // upcoming call to mSettings.writeLPr(). |
| 8386 | } |
Christopher Tate | 32a019d | 2013-04-04 15:40:04 -0700 | [diff] [blame] | 8387 | } |
| 8388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8389 | res.name = pkgName; |
| 8390 | res.uid = newPackage.applicationInfo.uid; |
| 8391 | res.pkg = newPackage; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 8392 | mSettings.setInstallStatus(pkgName, PackageSettingBase.PKG_INSTALL_COMPLETE); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 8393 | mSettings.setInstallerPackageName(pkgName, installerPackageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8394 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 8395 | //to update install status |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8396 | mSettings.writeLPr(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8397 | } |
| 8398 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8399 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8400 | private void installPackageLI(InstallArgs args, |
| 8401 | boolean newInstall, PackageInstalledInfo res) { |
| 8402 | int pFlags = args.flags; |
| 8403 | String installerPackageName = args.installerPackageName; |
| 8404 | File tmpPackageFile = new File(args.getCodePath()); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8405 | boolean forwardLocked = ((pFlags & PackageManager.INSTALL_FORWARD_LOCK) != 0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 8406 | boolean onSd = ((pFlags & PackageManager.INSTALL_EXTERNAL) != 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8407 | boolean replace = false; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 8408 | int scanMode = (onSd ? 0 : SCAN_MONITOR) | SCAN_FORCE_DEX | SCAN_UPDATE_SIGNATURE |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 8409 | | (newInstall ? SCAN_NEW_INSTALL : 0); |
Suchi Amalapurapu | ee5ece4 | 2009-09-15 13:41:47 -0700 | [diff] [blame] | 8410 | // Result object to be returned |
| 8411 | res.returnCode = PackageManager.INSTALL_SUCCEEDED; |
| 8412 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8413 | if (DEBUG_INSTALL) Slog.d(TAG, "installPackageLI: path=" + tmpPackageFile); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8414 | // Retrieve PackageSettings and parse package |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8415 | int parseFlags = mDefParseFlags | PackageParser.PARSE_CHATTY |
| 8416 | | (forwardLocked ? PackageParser.PARSE_FORWARD_LOCK : 0) |
| 8417 | | (onSd ? PackageParser.PARSE_ON_SDCARD : 0); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8418 | PackageParser pp = new PackageParser(tmpPackageFile.getPath()); |
| 8419 | pp.setSeparateProcesses(mSeparateProcesses); |
| 8420 | final PackageParser.Package pkg = pp.parsePackage(tmpPackageFile, |
| 8421 | null, mMetrics, parseFlags); |
| 8422 | if (pkg == null) { |
| 8423 | res.returnCode = pp.getParseError(); |
| 8424 | return; |
| 8425 | } |
| 8426 | String pkgName = res.name = pkg.packageName; |
| 8427 | if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_TEST_ONLY) != 0) { |
| 8428 | if ((pFlags&PackageManager.INSTALL_ALLOW_TEST) == 0) { |
| 8429 | res.returnCode = PackageManager.INSTALL_FAILED_TEST_ONLY; |
| 8430 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8431 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8432 | } |
| 8433 | if (GET_CERTIFICATES && !pp.collectCertificates(pkg, parseFlags)) { |
| 8434 | res.returnCode = pp.getParseError(); |
| 8435 | return; |
| 8436 | } |
Kenny Root | 5ab2157 | 2011-07-27 11:11:19 -0700 | [diff] [blame] | 8437 | |
| 8438 | /* If the installer passed in a manifest digest, compare it now. */ |
| 8439 | if (args.manifestDigest != null) { |
| 8440 | if (DEBUG_INSTALL) { |
| 8441 | final String parsedManifest = pkg.manifestDigest == null ? "null" |
| 8442 | : pkg.manifestDigest.toString(); |
| 8443 | Slog.d(TAG, "Comparing manifests: " + args.manifestDigest.toString() + " vs. " |
| 8444 | + parsedManifest); |
| 8445 | } |
| 8446 | |
| 8447 | if (!args.manifestDigest.equals(pkg.manifestDigest)) { |
| 8448 | res.returnCode = PackageManager.INSTALL_FAILED_PACKAGE_CHANGED; |
| 8449 | return; |
| 8450 | } |
| 8451 | } else if (DEBUG_INSTALL) { |
| 8452 | final String parsedManifest = pkg.manifestDigest == null |
| 8453 | ? "null" : pkg.manifestDigest.toString(); |
| 8454 | Slog.d(TAG, "manifestDigest was not present, but parser got: " + parsedManifest); |
| 8455 | } |
| 8456 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8457 | // Get rid of all references to package scan path via parser. |
| 8458 | pp = null; |
| 8459 | String oldCodePath = null; |
| 8460 | boolean systemApp = false; |
| 8461 | synchronized (mPackages) { |
| 8462 | // Check if installing already existing package |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8463 | if ((pFlags&PackageManager.INSTALL_REPLACE_EXISTING) != 0) { |
| 8464 | String oldName = mSettings.mRenamedPackages.get(pkgName); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 8465 | if (pkg.mOriginalPackages != null |
| 8466 | && pkg.mOriginalPackages.contains(oldName) |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8467 | && mPackages.containsKey(oldName)) { |
| 8468 | // This package is derived from an original package, |
| 8469 | // and this device has been updating from that original |
| 8470 | // name. We must continue using the original name, so |
| 8471 | // rename the new package here. |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 8472 | pkg.setPackageName(oldName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8473 | pkgName = pkg.packageName; |
| 8474 | replace = true; |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8475 | if (DEBUG_INSTALL) Slog.d(TAG, "Replacing existing renamed package: oldName=" |
| 8476 | + oldName + " pkgName=" + pkgName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8477 | } else if (mPackages.containsKey(pkgName)) { |
| 8478 | // This package, under its official name, already exists |
| 8479 | // on the device; we should replace it. |
| 8480 | replace = true; |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8481 | if (DEBUG_INSTALL) Slog.d(TAG, "Replace existing pacakge: " + pkgName); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8482 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8483 | } |
| 8484 | PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 8485 | if (ps != null) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8486 | if (DEBUG_INSTALL) Slog.d(TAG, "Existing package: " + ps); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8487 | oldCodePath = mSettings.mPackages.get(pkgName).codePathString; |
| 8488 | if (ps.pkg != null && ps.pkg.applicationInfo != null) { |
| 8489 | systemApp = (ps.pkg.applicationInfo.flags & |
| 8490 | ApplicationInfo.FLAG_SYSTEM) != 0; |
Dianne Hackborn | ade3eca | 2009-05-11 18:54:45 -0700 | [diff] [blame] | 8491 | } |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 8492 | res.origUsers = ps.queryInstalledUsers(sUserManager.getUserIds(), true); |
Dianne Hackborn | ade3eca | 2009-05-11 18:54:45 -0700 | [diff] [blame] | 8493 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8494 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8495 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8496 | if (systemApp && onSd) { |
| 8497 | // Disable updates to system apps on sdcard |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8498 | Slog.w(TAG, "Cannot install updates to system apps on sdcard"); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8499 | res.returnCode = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; |
| 8500 | return; |
| 8501 | } |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 8502 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8503 | if (!args.doRename(res.returnCode, pkgName, oldCodePath)) { |
| 8504 | res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
| 8505 | return; |
| 8506 | } |
| 8507 | // Set application objects path explicitly after the rename |
| 8508 | setApplicationInfoPaths(pkg, args.getCodePath(), args.getResourcePath()); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 8509 | pkg.applicationInfo.nativeLibraryDir = args.getNativeLibraryPath(); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 8510 | if (replace) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8511 | replacePackageLI(pkg, parseFlags, scanMode, args.user, |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 8512 | installerPackageName, res); |
| 8513 | } else { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8514 | installNewPackageLI(pkg, parseFlags, scanMode, args.user, |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 8515 | installerPackageName, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8516 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8517 | synchronized (mPackages) { |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 8518 | final PackageSetting ps = mSettings.mPackages.get(pkgName); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8519 | if (ps != null) { |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 8520 | res.newUsers = ps.queryInstalledUsers(sUserManager.getUserIds(), true); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8521 | } |
| 8522 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8523 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8524 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 8525 | private static boolean isForwardLocked(PackageParser.Package pkg) { |
| 8526 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8527 | } |
| 8528 | |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 8529 | |
| 8530 | private boolean isForwardLocked(PackageSetting ps) { |
| 8531 | return (ps.pkgFlags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0; |
| 8532 | } |
| 8533 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 8534 | private static boolean isExternal(PackageParser.Package pkg) { |
| 8535 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; |
| 8536 | } |
| 8537 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 8538 | private static boolean isExternal(PackageSetting ps) { |
| 8539 | return (ps.pkgFlags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; |
| 8540 | } |
| 8541 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 8542 | private static boolean isSystemApp(PackageParser.Package pkg) { |
| 8543 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 8544 | } |
| 8545 | |
Kenny Root | 502e9a4 | 2011-01-10 13:48:15 -0800 | [diff] [blame] | 8546 | private static boolean isSystemApp(ApplicationInfo info) { |
| 8547 | return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 8548 | } |
| 8549 | |
Kenny Root | 208d341 | 2012-05-07 19:42:35 -0700 | [diff] [blame] | 8550 | private static boolean isSystemApp(PackageSetting ps) { |
| 8551 | return (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 8552 | } |
| 8553 | |
Nick Kralevich | 6079285 | 2013-04-03 14:12:24 -0700 | [diff] [blame] | 8554 | private static boolean isUpdatedSystemApp(PackageSetting ps) { |
| 8555 | return (ps.pkgFlags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0; |
| 8556 | } |
| 8557 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 8558 | private static boolean isUpdatedSystemApp(PackageParser.Package pkg) { |
| 8559 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0; |
Suchi Amalapurapu | ae18171 | 2010-03-30 14:01:02 -0700 | [diff] [blame] | 8560 | } |
| 8561 | |
Kenny Root | 88670d8 | 2012-05-09 15:47:35 -0700 | [diff] [blame] | 8562 | private int packageFlagsToInstallFlags(PackageSetting ps) { |
| 8563 | int installFlags = 0; |
| 8564 | if (isExternal(ps)) { |
| 8565 | installFlags |= PackageManager.INSTALL_EXTERNAL; |
| 8566 | } |
| 8567 | if (isForwardLocked(ps)) { |
| 8568 | installFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 8569 | } |
| 8570 | return installFlags; |
| 8571 | } |
| 8572 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8573 | private void deleteTempPackageFiles() { |
Kenny Root | 0b2fa8c | 2012-09-14 10:26:16 -0700 | [diff] [blame] | 8574 | final FilenameFilter filter = new FilenameFilter() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8575 | public boolean accept(File dir, String name) { |
| 8576 | return name.startsWith("vmdl") && name.endsWith(".tmp"); |
| 8577 | } |
| 8578 | }; |
Kenny Root | 0b2fa8c | 2012-09-14 10:26:16 -0700 | [diff] [blame] | 8579 | deleteTempPackageFilesInDirectory(mAppInstallDir, filter); |
| 8580 | deleteTempPackageFilesInDirectory(mDrmAppPrivateInstallDir, filter); |
| 8581 | } |
| 8582 | |
| 8583 | private static final void deleteTempPackageFilesInDirectory(File directory, |
| 8584 | FilenameFilter filter) { |
| 8585 | final String[] tmpFilesList = directory.list(filter); |
| 8586 | if (tmpFilesList == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8587 | return; |
| 8588 | } |
Kenny Root | 0b2fa8c | 2012-09-14 10:26:16 -0700 | [diff] [blame] | 8589 | for (int i = 0; i < tmpFilesList.length; i++) { |
| 8590 | final File tmpFile = new File(directory, tmpFilesList[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8591 | tmpFile.delete(); |
| 8592 | } |
| 8593 | } |
| 8594 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8595 | private File createTempPackageFile(File installDir) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8596 | File tmpPackageFile; |
| 8597 | try { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8598 | tmpPackageFile = File.createTempFile("vmdl", ".tmp", installDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8599 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8600 | 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] | 8601 | return null; |
| 8602 | } |
| 8603 | try { |
| 8604 | FileUtils.setPermissions( |
| 8605 | tmpPackageFile.getCanonicalPath(), FileUtils.S_IRUSR|FileUtils.S_IWUSR, |
| 8606 | -1, -1); |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 8607 | if (!SELinux.restorecon(tmpPackageFile)) { |
| 8608 | return null; |
| 8609 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8610 | } catch (IOException e) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8611 | 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] | 8612 | return null; |
| 8613 | } |
| 8614 | return tmpPackageFile; |
| 8615 | } |
| 8616 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 8617 | @Override |
| 8618 | public void deletePackageAsUser(final String packageName, |
| 8619 | final IPackageDeleteObserver observer, |
| 8620 | final int userId, final int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8621 | mContext.enforceCallingOrSelfPermission( |
| 8622 | android.Manifest.permission.DELETE_PACKAGES, null); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8623 | final int uid = Binder.getCallingUid(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 8624 | if (UserHandle.getUserId(uid) != userId) { |
| 8625 | mContext.enforceCallingPermission( |
| 8626 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8627 | "deletePackage for user " + userId); |
| 8628 | } |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 8629 | if (isUserRestricted(userId, UserManager.DISALLOW_UNINSTALL_APPS)) { |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 8630 | try { |
| 8631 | observer.packageDeleted(packageName, PackageManager.DELETE_FAILED_USER_RESTRICTED); |
| 8632 | } catch (RemoteException re) { |
| 8633 | } |
| 8634 | return; |
| 8635 | } |
| 8636 | |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8637 | if (DEBUG_REMOVE) Slog.d(TAG, "deletePackageAsUser: pkg=" + packageName + " user=" + userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 8638 | // Queue up an async operation since the package deletion may take a little while. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8639 | mHandler.post(new Runnable() { |
| 8640 | public void run() { |
| 8641 | mHandler.removeCallbacks(this); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 8642 | final int returnCode = deletePackageX(packageName, userId, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8643 | if (observer != null) { |
| 8644 | try { |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 8645 | observer.packageDeleted(packageName, returnCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8646 | } catch (RemoteException e) { |
| 8647 | Log.i(TAG, "Observer no longer exists."); |
| 8648 | } //end catch |
| 8649 | } //end if |
| 8650 | } //end run |
| 8651 | }); |
| 8652 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8654 | /** |
| 8655 | * This method is an internal method that could be get invoked either |
| 8656 | * to delete an installed package or to clean up a failed installation. |
| 8657 | * After deleting an installed package, a broadcast is sent to notify any |
| 8658 | * listeners that the package has been installed. For cleaning up a failed |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8659 | * 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] | 8660 | * installation wouldn't have sent the initial broadcast either |
| 8661 | * The key steps in deleting a package are |
| 8662 | * deleting the package information in internal structures like mPackages, |
| 8663 | * deleting the packages base directories through installd |
| 8664 | * updating mSettings to reflect current status |
| 8665 | * persisting settings for later use |
| 8666 | * sending a broadcast if necessary |
| 8667 | */ |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 8668 | private int deletePackageX(String packageName, int userId, int flags) { |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 8669 | final PackageRemovedInfo info = new PackageRemovedInfo(); |
| 8670 | final boolean res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8671 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 8672 | IDevicePolicyManager dpm = IDevicePolicyManager.Stub.asInterface( |
| 8673 | ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)); |
| 8674 | try { |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 8675 | if (dpm != null && (dpm.packageHasActiveAdmins(packageName, userId) |
| 8676 | || dpm.isDeviceOwner(packageName))) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8677 | Slog.w(TAG, "Not removing package " + packageName + ": has active device admin"); |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 8678 | return PackageManager.DELETE_FAILED_DEVICE_POLICY_MANAGER; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 8679 | } |
| 8680 | } catch (RemoteException e) { |
| 8681 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 8682 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 8683 | boolean removedForAllUsers = false; |
| 8684 | boolean systemUpdate = false; |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8685 | |
| 8686 | // for the uninstall-updates case and restricted profiles, remember the per- |
| 8687 | // userhandle installed state |
| 8688 | int[] allUsers; |
| 8689 | boolean[] perUserInstalled; |
| 8690 | synchronized (mPackages) { |
| 8691 | PackageSetting ps = mSettings.mPackages.get(packageName); |
| 8692 | allUsers = sUserManager.getUserIds(); |
| 8693 | perUserInstalled = new boolean[allUsers.length]; |
| 8694 | for (int i = 0; i < allUsers.length; i++) { |
Dianne Hackborn | 2c44c74 | 2013-05-17 12:07:21 -0700 | [diff] [blame] | 8695 | perUserInstalled[i] = ps != null ? ps.getInstalled(allUsers[i]) : false; |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8696 | } |
| 8697 | } |
| 8698 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8699 | synchronized (mInstallLock) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8700 | if (DEBUG_REMOVE) Slog.d(TAG, "deletePackageX: pkg=" + packageName + " user=" + userId); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8701 | res = deletePackageLI(packageName, |
| 8702 | (flags & PackageManager.DELETE_ALL_USERS) != 0 |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 8703 | ? UserHandle.ALL : new UserHandle(userId), |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8704 | true, allUsers, perUserInstalled, |
| 8705 | flags | REMOVE_CHATTY, info, true); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 8706 | systemUpdate = info.isRemovedPackageSystemUpdate; |
| 8707 | if (res && !systemUpdate && mPackages.get(packageName) == null) { |
| 8708 | removedForAllUsers = true; |
| 8709 | } |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8710 | if (DEBUG_REMOVE) Slog.d(TAG, "delete res: systemUpdate=" + systemUpdate |
| 8711 | + " removedForAllUsers=" + removedForAllUsers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8712 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8713 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8714 | if (res) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 8715 | info.sendBroadcast(true, systemUpdate, removedForAllUsers); |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 8716 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 8717 | // If the removed package was a system update, the old system package |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 8718 | // was re-enabled; we need to broadcast this information |
| 8719 | if (systemUpdate) { |
| 8720 | Bundle extras = new Bundle(1); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8721 | extras.putInt(Intent.EXTRA_UID, info.removedAppId >= 0 |
| 8722 | ? info.removedAppId : info.uid); |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 8723 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 8724 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8725 | sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, packageName, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8726 | extras, null, null, null); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8727 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8728 | extras, null, null, null); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8729 | sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, null, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8730 | null, packageName, null, null); |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 8731 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8732 | } |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 8733 | // Force a gc here. |
| 8734 | Runtime.getRuntime().gc(); |
| 8735 | // Delete the resources here after sending the broadcast to let |
| 8736 | // other processes clean up before deleting resources. |
| 8737 | if (info.args != null) { |
| 8738 | synchronized (mInstallLock) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8739 | info.args.doPostDeleteLI(true); |
Suchi Amalapurapu | deb69370 | 2010-04-14 19:17:14 -0700 | [diff] [blame] | 8740 | } |
| 8741 | } |
Kenny Root | c39bb4a | 2011-02-28 13:27:19 -0800 | [diff] [blame] | 8742 | |
| 8743 | 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] | 8744 | } |
| 8745 | |
| 8746 | static class PackageRemovedInfo { |
| 8747 | String removedPackage; |
| 8748 | int uid = -1; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8749 | int removedAppId = -1; |
| 8750 | int[] removedUsers = null; |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 8751 | boolean isRemovedPackageSystemUpdate = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 8752 | // Clean up resources deleted packages. |
| 8753 | InstallArgs args = null; |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 8754 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 8755 | void sendBroadcast(boolean fullRemove, boolean replacing, boolean removedForAllUsers) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8756 | Bundle extras = new Bundle(1); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8757 | extras.putInt(Intent.EXTRA_UID, removedAppId >= 0 ? removedAppId : uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8758 | extras.putBoolean(Intent.EXTRA_DATA_REMOVED, fullRemove); |
| 8759 | if (replacing) { |
| 8760 | extras.putBoolean(Intent.EXTRA_REPLACING, true); |
| 8761 | } |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 8762 | extras.putBoolean(Intent.EXTRA_REMOVED_FOR_ALL_USERS, removedForAllUsers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8763 | if (removedPackage != null) { |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 8764 | sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED, removedPackage, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8765 | extras, null, null, removedUsers); |
Dianne Hackborn | f9abb40 | 2011-08-10 15:00:59 -0700 | [diff] [blame] | 8766 | if (fullRemove && !replacing) { |
| 8767 | sendPackageBroadcast(Intent.ACTION_PACKAGE_FULLY_REMOVED, removedPackage, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8768 | extras, null, null, removedUsers); |
Dianne Hackborn | f9abb40 | 2011-08-10 15:00:59 -0700 | [diff] [blame] | 8769 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8770 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8771 | if (removedAppId >= 0) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 8772 | sendPackageBroadcast(Intent.ACTION_UID_REMOVED, null, extras, null, null, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8773 | removedUsers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8774 | } |
| 8775 | } |
| 8776 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8778 | /* |
| 8779 | * This method deletes the package from internal data structures. If the DONT_DELETE_DATA |
| 8780 | * flag is not set, the data directory is removed as well. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8781 | * 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] | 8782 | * delete a partially installed application. |
| 8783 | */ |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8784 | private void removePackageDataLI(PackageSetting ps, |
| 8785 | int[] allUserHandles, boolean[] perUserInstalled, |
| 8786 | PackageRemovedInfo outInfo, int flags, boolean writeSettings) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8787 | String packageName = ps.name; |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8788 | if (DEBUG_REMOVE) Slog.d(TAG, "removePackageDataLI: " + ps); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8789 | removePackageLI(ps, (flags&REMOVE_CHATTY) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8790 | // Retrieve object to delete permissions for shared user later on |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8791 | final PackageSetting deletedPs; |
| 8792 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8793 | synchronized (mPackages) { |
| 8794 | deletedPs = mSettings.mPackages.get(packageName); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8795 | if (outInfo != null) { |
| 8796 | outInfo.removedPackage = packageName; |
| 8797 | outInfo.removedUsers = deletedPs != null |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 8798 | ? deletedPs.queryInstalledUsers(sUserManager.getUserIds(), true) |
| 8799 | : null; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8800 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8801 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8802 | if ((flags&PackageManager.DELETE_KEEP_DATA) == 0) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 8803 | removeDataDirsLI(packageName); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8804 | schedulePackageCleaning(packageName, UserHandle.USER_ALL, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8805 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8806 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8807 | synchronized (mPackages) { |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 8808 | if (deletedPs != null) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8809 | if ((flags&PackageManager.DELETE_KEEP_DATA) == 0) { |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8810 | if (outInfo != null) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8811 | outInfo.removedAppId = mSettings.removePackageLPw(packageName); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8812 | } |
| 8813 | if (deletedPs != null) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 8814 | updatePermissionsLPw(deletedPs.name, null, 0); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8815 | if (deletedPs.sharedUser != null) { |
| 8816 | // remove permissions associated with package |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8817 | mSettings.updateSharedUserPermsLPw(deletedPs, mGlobalGids); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 8818 | } |
| 8819 | } |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 8820 | clearPackagePreferredActivitiesLPw(deletedPs.name, UserHandle.USER_ALL); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 8821 | } |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8822 | // make sure to preserve per-user disabled state if this removal was just |
| 8823 | // a downgrade of a system app to the factory package |
| 8824 | if (allUserHandles != null && perUserInstalled != null) { |
| 8825 | if (DEBUG_REMOVE) { |
| 8826 | Slog.d(TAG, "Propagating install state across downgrade"); |
| 8827 | } |
| 8828 | for (int i = 0; i < allUserHandles.length; i++) { |
| 8829 | if (DEBUG_REMOVE) { |
| 8830 | Slog.d(TAG, " user " + allUserHandles[i] |
| 8831 | + " => " + perUserInstalled[i]); |
| 8832 | } |
| 8833 | ps.setInstalled(perUserInstalled[i], allUserHandles[i]); |
| 8834 | } |
| 8835 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 8836 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8837 | // can downgrade to reader |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8838 | if (writeSettings) { |
| 8839 | // Save settings now |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8840 | mSettings.writeLPr(); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8841 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8842 | } |
Kenny Root | 3b9933d | 2013-04-08 16:05:00 -0700 | [diff] [blame] | 8843 | if (outInfo != null) { |
| 8844 | // A user ID was deleted here. Go through all users and remove it |
| 8845 | // from KeyStore. |
Kenny Root | 6fd1c85d | 2013-04-12 10:06:51 -0700 | [diff] [blame] | 8846 | removeKeystoreDataIfNeeded(UserHandle.USER_ALL, outInfo.removedAppId); |
Kenny Root | d72317a | 2013-04-01 15:59:59 -0700 | [diff] [blame] | 8847 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8848 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8849 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8850 | /* |
| 8851 | * Tries to delete system package. |
| 8852 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8853 | private boolean deleteSystemPackageLI(PackageSetting newPs, |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8854 | int[] allUserHandles, boolean[] perUserInstalled, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8855 | int flags, PackageRemovedInfo outInfo, boolean writeSettings) { |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8856 | final boolean applyUserRestrictions |
| 8857 | = (allUserHandles != null) && (perUserInstalled != null); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8858 | PackageSetting disabledPs = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8859 | // Confirm if the system package has been updated |
| 8860 | // An updated system app can be deleted. This will also have to restore |
| 8861 | // the system pkg from system partition |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8862 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8863 | synchronized (mPackages) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8864 | disabledPs = mSettings.getDisabledSystemPkgLPr(newPs.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8865 | } |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8866 | if (DEBUG_REMOVE) Slog.d(TAG, "deleteSystemPackageLI: newPs=" + newPs |
| 8867 | + " disabledPs=" + disabledPs); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8868 | if (disabledPs == null) { |
| 8869 | Slog.w(TAG, "Attempt to delete unknown system package "+ newPs.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8870 | return false; |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8871 | } else if (DEBUG_REMOVE) { |
| 8872 | Slog.d(TAG, "Deleting system pkg from data partition"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8873 | } |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8874 | if (DEBUG_REMOVE) { |
| 8875 | if (applyUserRestrictions) { |
| 8876 | Slog.d(TAG, "Remembering install states:"); |
| 8877 | for (int i = 0; i < allUserHandles.length; i++) { |
| 8878 | Slog.d(TAG, " u=" + allUserHandles[i] + " inst=" + perUserInstalled[i]); |
| 8879 | } |
| 8880 | } |
| 8881 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8882 | // Delete the updated package |
Romain Guy | 96f4357 | 2009-03-24 20:27:49 -0700 | [diff] [blame] | 8883 | outInfo.isRemovedPackageSystemUpdate = true; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8884 | if (disabledPs.versionCode < newPs.versionCode) { |
Dianne Hackborn | 690d20b | 2010-12-22 14:03:29 -0800 | [diff] [blame] | 8885 | // Delete data for downgrades |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8886 | flags &= ~PackageManager.DELETE_KEEP_DATA; |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8887 | } else { |
| 8888 | // Preserve data by setting flag |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8889 | flags |= PackageManager.DELETE_KEEP_DATA; |
Suchi Amalapurapu | c2af31f | 2009-05-08 14:44:41 -0700 | [diff] [blame] | 8890 | } |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8891 | boolean ret = deleteInstalledPackageLI(newPs, true, flags, |
| 8892 | allUserHandles, perUserInstalled, outInfo, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8893 | if (!ret) { |
| 8894 | return false; |
| 8895 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8896 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8897 | synchronized (mPackages) { |
| 8898 | // Reinstate the old system package |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8899 | mSettings.enableSystemPackageLPw(newPs.name); |
Kenny Root | 8f7cc02 | 2010-09-12 09:04:56 -0700 | [diff] [blame] | 8900 | // Remove any native libraries from the upgraded package. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8901 | NativeLibraryHelper.removeNativeBinariesLI(newPs.nativeLibraryPathString); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8902 | } |
| 8903 | // Install the system package |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8904 | if (DEBUG_REMOVE) Slog.d(TAG, "Re-installing system package: " + disabledPs); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8905 | PackageParser.Package newPkg = scanPackageLI(disabledPs.codePath, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8906 | PackageParser.PARSE_MUST_BE_APK | PackageParser.PARSE_IS_SYSTEM, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8907 | SCAN_MONITOR | SCAN_NO_PATHS, 0, null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8908 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8909 | if (newPkg == null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8910 | Slog.w(TAG, "Failed to restore system package:" + newPs.name |
| 8911 | + " with error:" + mLastScanError); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8912 | return false; |
| 8913 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8914 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8915 | synchronized (mPackages) { |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 8916 | updatePermissionsLPw(newPkg.packageName, newPkg, |
| 8917 | UPDATE_PERMISSIONS_ALL | UPDATE_PERMISSIONS_REPLACE_PKG); |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8918 | if (applyUserRestrictions) { |
| 8919 | if (DEBUG_REMOVE) { |
| 8920 | Slog.d(TAG, "Propagating install state across reinstall"); |
| 8921 | } |
| 8922 | PackageSetting ps = mSettings.mPackages.get(newPkg.packageName); |
| 8923 | for (int i = 0; i < allUserHandles.length; i++) { |
| 8924 | if (DEBUG_REMOVE) { |
| 8925 | Slog.d(TAG, " user " + allUserHandles[i] |
| 8926 | + " => " + perUserInstalled[i]); |
| 8927 | } |
| 8928 | ps.setInstalled(perUserInstalled[i], allUserHandles[i]); |
| 8929 | } |
| 8930 | // Regardless of writeSettings we need to ensure that this restriction |
| 8931 | // state propagation is persisted |
| 8932 | mSettings.writeAllUsersPackageRestrictionsLPr(); |
| 8933 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8934 | // can downgrade to reader here |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8935 | if (writeSettings) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 8936 | mSettings.writeLPr(); |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8937 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8938 | } |
| 8939 | return true; |
| 8940 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 8941 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8942 | private boolean deleteInstalledPackageLI(PackageSetting ps, |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8943 | boolean deleteCodeAndResources, int flags, |
| 8944 | int[] allUserHandles, boolean[] perUserInstalled, |
| 8945 | PackageRemovedInfo outInfo, boolean writeSettings) { |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 8946 | if (outInfo != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8947 | outInfo.uid = ps.appId; |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 8948 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8949 | |
| 8950 | // Delete package data from internal structures and also remove data if flag is set |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8951 | removePackageDataLI(ps, allUserHandles, perUserInstalled, outInfo, flags, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8952 | |
| 8953 | // Delete application code and resources |
Kenny Root | 92bc9b3 | 2012-10-23 17:42:40 -0700 | [diff] [blame] | 8954 | if (deleteCodeAndResources && (outInfo != null)) { |
Kenny Root | 5455f68 | 2012-09-09 14:52:10 -0700 | [diff] [blame] | 8955 | outInfo.args = createInstallArgs(packageFlagsToInstallFlags(ps), ps.codePathString, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8956 | ps.resourcePathString, ps.nativeLibraryPathString); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8957 | } |
| 8958 | return true; |
| 8959 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8960 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8961 | /* |
| 8962 | * This method handles package deletion in general |
| 8963 | */ |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8964 | private boolean deletePackageLI(String packageName, UserHandle user, |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 8965 | boolean deleteCodeAndResources, int[] allUserHandles, boolean[] perUserInstalled, |
| 8966 | int flags, PackageRemovedInfo outInfo, |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 8967 | boolean writeSettings) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8968 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 8969 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8970 | return false; |
| 8971 | } |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8972 | if (DEBUG_REMOVE) Slog.d(TAG, "deletePackageLI: " + packageName + " user " + user); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8973 | PackageSetting ps; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8974 | boolean dataOnly = false; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8975 | int removeUser = -1; |
| 8976 | int appId = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8977 | synchronized (mPackages) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 8978 | ps = mSettings.mPackages.get(packageName); |
| 8979 | if (ps == null) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8980 | Slog.w(TAG, "Package named '" + packageName + "' doesn't exist."); |
| 8981 | return false; |
| 8982 | } |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8983 | if ((!isSystemApp(ps) || (flags&PackageManager.DELETE_SYSTEM_APP) != 0) && user != null |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8984 | && user.getIdentifier() != UserHandle.USER_ALL) { |
| 8985 | // The caller is asking that the package only be deleted for a single |
| 8986 | // user. To do this, we just mark its uninstalled state and delete |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 8987 | // its data. If this is a system app, we only allow this to happen if |
| 8988 | // they have set the special DELETE_SYSTEM_APP which requests different |
| 8989 | // semantics than normal for uninstalling system apps. |
| 8990 | if (DEBUG_REMOVE) Slog.d(TAG, "Only deleting for single user"); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 8991 | ps.setUserState(user.getIdentifier(), |
| 8992 | COMPONENT_ENABLED_STATE_DEFAULT, |
| 8993 | false, //installed |
| 8994 | true, //stopped |
| 8995 | true, //notLaunched |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 8996 | null, null, null); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 8997 | if (!isSystemApp(ps)) { |
| 8998 | if (ps.isAnyInstalled(sUserManager.getUserIds())) { |
| 8999 | // Other user still have this package installed, so all |
| 9000 | // we need to do is clear this user's data and save that |
| 9001 | // it is uninstalled. |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 9002 | if (DEBUG_REMOVE) Slog.d(TAG, "Still installed by other users"); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 9003 | removeUser = user.getIdentifier(); |
| 9004 | appId = ps.appId; |
| 9005 | mSettings.writePackageRestrictionsLPr(removeUser); |
| 9006 | } else { |
| 9007 | // We need to set it back to 'installed' so the uninstall |
| 9008 | // broadcasts will be sent correctly. |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 9009 | if (DEBUG_REMOVE) Slog.d(TAG, "Not installed by other users, full delete"); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 9010 | ps.setInstalled(true, user.getIdentifier()); |
| 9011 | } |
| 9012 | } else { |
| 9013 | // This is a system app, so we assume that the |
| 9014 | // other users still have this package installed, so all |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9015 | // we need to do is clear this user's data and save that |
| 9016 | // it is uninstalled. |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 9017 | if (DEBUG_REMOVE) Slog.d(TAG, "Deleting system app"); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9018 | removeUser = user.getIdentifier(); |
| 9019 | appId = ps.appId; |
| 9020 | mSettings.writePackageRestrictionsLPr(removeUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9021 | } |
| 9022 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9023 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9024 | |
| 9025 | if (removeUser >= 0) { |
| 9026 | // From above, we determined that we are deleting this only |
| 9027 | // for a single user. Continue the work here. |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 9028 | if (DEBUG_REMOVE) Slog.d(TAG, "Updating install state for user: " + removeUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9029 | if (outInfo != null) { |
| 9030 | outInfo.removedPackage = packageName; |
| 9031 | outInfo.removedAppId = appId; |
| 9032 | outInfo.removedUsers = new int[] {removeUser}; |
| 9033 | } |
| 9034 | mInstaller.clearUserData(packageName, removeUser); |
Kenny Root | 6fd1c85d | 2013-04-12 10:06:51 -0700 | [diff] [blame] | 9035 | removeKeystoreDataIfNeeded(removeUser, appId); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9036 | schedulePackageCleaning(packageName, removeUser, false); |
| 9037 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9038 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 9039 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9040 | if (dataOnly) { |
| 9041 | // Delete application data first |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 9042 | if (DEBUG_REMOVE) Slog.d(TAG, "Removing package data only"); |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 9043 | removePackageDataLI(ps, null, null, outInfo, flags, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9044 | return true; |
| 9045 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 9046 | boolean ret = false; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9047 | if (isSystemApp(ps)) { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 9048 | if (DEBUG_REMOVE) Slog.d(TAG, "Removing system package:" + ps.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9049 | // When an updated system application is deleted we delete the existing resources as well and |
| 9050 | // fall back to existing code in system partition |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 9051 | ret = deleteSystemPackageLI(ps, allUserHandles, perUserInstalled, |
| 9052 | flags, outInfo, writeSettings); |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 9053 | } else { |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 9054 | if (DEBUG_REMOVE) Slog.d(TAG, "Removing non-system package:" + ps.name); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 9055 | // Kill application pre-emptively especially for apps on sd. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9056 | killApplication(packageName, ps.appId); |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 9057 | ret = deleteInstalledPackageLI(ps, deleteCodeAndResources, flags, |
| 9058 | allUserHandles, perUserInstalled, |
| 9059 | outInfo, writeSettings); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9060 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 9061 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9062 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 9063 | |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 9064 | private final class ClearStorageConnection implements ServiceConnection { |
| 9065 | IMediaContainerService mContainerService; |
| 9066 | |
| 9067 | @Override |
| 9068 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 9069 | synchronized (this) { |
| 9070 | mContainerService = IMediaContainerService.Stub.asInterface(service); |
| 9071 | notifyAll(); |
| 9072 | } |
| 9073 | } |
| 9074 | |
| 9075 | @Override |
| 9076 | public void onServiceDisconnected(ComponentName name) { |
| 9077 | } |
| 9078 | } |
| 9079 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9080 | private void clearExternalStorageDataSync(String packageName, int userId, boolean allData) { |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 9081 | final boolean mounted; |
| 9082 | if (Environment.isExternalStorageEmulated()) { |
| 9083 | mounted = true; |
| 9084 | } else { |
| 9085 | final String status = Environment.getExternalStorageState(); |
| 9086 | |
| 9087 | mounted = status.equals(Environment.MEDIA_MOUNTED) |
| 9088 | || status.equals(Environment.MEDIA_MOUNTED_READ_ONLY); |
| 9089 | } |
| 9090 | |
| 9091 | if (!mounted) { |
| 9092 | return; |
| 9093 | } |
| 9094 | |
| 9095 | final Intent containerIntent = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9096 | int[] users; |
| 9097 | if (userId == UserHandle.USER_ALL) { |
| 9098 | users = sUserManager.getUserIds(); |
| 9099 | } else { |
| 9100 | users = new int[] { userId }; |
| 9101 | } |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 9102 | final ClearStorageConnection conn = new ClearStorageConnection(); |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 9103 | if (mContext.bindServiceAsUser( |
| 9104 | containerIntent, conn, Context.BIND_AUTO_CREATE, UserHandle.OWNER)) { |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 9105 | try { |
| 9106 | for (int curUser : users) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9107 | long timeout = SystemClock.uptimeMillis() + 5000; |
| 9108 | synchronized (conn) { |
| 9109 | long now = SystemClock.uptimeMillis(); |
| 9110 | while (conn.mContainerService == null && now < timeout) { |
| 9111 | try { |
| 9112 | conn.wait(timeout - now); |
| 9113 | } catch (InterruptedException e) { |
| 9114 | } |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 9115 | } |
| 9116 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9117 | if (conn.mContainerService == null) { |
| 9118 | return; |
| 9119 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 9120 | |
| 9121 | final UserEnvironment userEnv = new UserEnvironment(curUser); |
| 9122 | final File externalCacheDir = userEnv |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9123 | .getExternalStorageAppCacheDirectory(packageName); |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 9124 | try { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9125 | conn.mContainerService.clearDirectory(externalCacheDir.toString()); |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 9126 | } catch (RemoteException e) { |
| 9127 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9128 | if (allData) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 9129 | final File externalDataDir = userEnv |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9130 | .getExternalStorageAppDataDirectory(packageName); |
| 9131 | try { |
| 9132 | conn.mContainerService.clearDirectory(externalDataDir.toString()); |
| 9133 | } catch (RemoteException e) { |
| 9134 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 9135 | final File externalMediaDir = userEnv |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9136 | .getExternalStorageAppMediaDirectory(packageName); |
| 9137 | try { |
| 9138 | conn.mContainerService.clearDirectory(externalMediaDir.toString()); |
| 9139 | } catch (RemoteException e) { |
| 9140 | } |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 9141 | } |
| 9142 | } |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 9143 | } finally { |
| 9144 | mContext.unbindService(conn); |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 9145 | } |
| 9146 | } |
| 9147 | } |
| 9148 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9149 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9150 | public void clearApplicationUserData(final String packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9151 | final IPackageDataObserver observer, final int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9152 | mContext.enforceCallingOrSelfPermission( |
| 9153 | android.Manifest.permission.CLEAR_APP_USER_DATA, null); |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 9154 | enforceCrossUserPermission(Binder.getCallingUid(), userId, true, "clear application data"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9155 | // Queue up an async operation since the package deletion may take a little while. |
| 9156 | mHandler.post(new Runnable() { |
| 9157 | public void run() { |
| 9158 | mHandler.removeCallbacks(this); |
| 9159 | final boolean succeeded; |
| 9160 | synchronized (mInstallLock) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9161 | succeeded = clearApplicationUserDataLI(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9162 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9163 | clearExternalStorageDataSync(packageName, userId, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9164 | if (succeeded) { |
| 9165 | // invoke DeviceStorageMonitor's update method to clear any notifications |
| 9166 | DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) |
| 9167 | ServiceManager.getService(DeviceStorageMonitorService.SERVICE); |
| 9168 | if (dsm != null) { |
| 9169 | dsm.updateMemory(); |
| 9170 | } |
| 9171 | } |
| 9172 | if(observer != null) { |
| 9173 | try { |
| 9174 | observer.onRemoveCompleted(packageName, succeeded); |
| 9175 | } catch (RemoteException e) { |
| 9176 | Log.i(TAG, "Observer no longer exists."); |
| 9177 | } |
| 9178 | } //end if observer |
| 9179 | } //end run |
| 9180 | }); |
| 9181 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 9182 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9183 | private boolean clearApplicationUserDataLI(String packageName, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9184 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9185 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9186 | return false; |
| 9187 | } |
| 9188 | PackageParser.Package p; |
| 9189 | boolean dataOnly = false; |
Kenny Root | 6fd1c85d | 2013-04-12 10:06:51 -0700 | [diff] [blame] | 9190 | final int appId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9191 | synchronized (mPackages) { |
| 9192 | p = mPackages.get(packageName); |
Kenny Root | 6fd1c85d | 2013-04-12 10:06:51 -0700 | [diff] [blame] | 9193 | if (p == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9194 | dataOnly = true; |
| 9195 | PackageSetting ps = mSettings.mPackages.get(packageName); |
Kenny Root | 6fd1c85d | 2013-04-12 10:06:51 -0700 | [diff] [blame] | 9196 | if ((ps == null) || (ps.pkg == null)) { |
| 9197 | Slog.w(TAG, "Package named '" + packageName + "' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9198 | return false; |
| 9199 | } |
| 9200 | p = ps.pkg; |
| 9201 | } |
Kenny Root | 6fd1c85d | 2013-04-12 10:06:51 -0700 | [diff] [blame] | 9202 | if (!dataOnly) { |
| 9203 | // need to check this only for fully installed applications |
| 9204 | if (p == null) { |
| 9205 | Slog.w(TAG, "Package named '" + packageName + "' doesn't exist."); |
| 9206 | return false; |
| 9207 | } |
| 9208 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 9209 | if (applicationInfo == null) { |
| 9210 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
| 9211 | return false; |
| 9212 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9213 | } |
Kenny Root | 6fd1c85d | 2013-04-12 10:06:51 -0700 | [diff] [blame] | 9214 | if (p != null && p.applicationInfo != null) { |
| 9215 | appId = p.applicationInfo.uid; |
| 9216 | } else { |
| 9217 | appId = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9218 | } |
| 9219 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9220 | int retCode = mInstaller.clearUserData(packageName, userId); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 9221 | if (retCode < 0) { |
| 9222 | Slog.w(TAG, "Couldn't remove cache files for package: " |
| 9223 | + packageName); |
| 9224 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9225 | } |
Kenny Root | 6fd1c85d | 2013-04-12 10:06:51 -0700 | [diff] [blame] | 9226 | removeKeystoreDataIfNeeded(userId, appId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9227 | return true; |
| 9228 | } |
| 9229 | |
Kenny Root | 6fd1c85d | 2013-04-12 10:06:51 -0700 | [diff] [blame] | 9230 | /** |
| 9231 | * Remove entries from the keystore daemon. Will only remove it if the |
| 9232 | * {@code appId} is valid. |
| 9233 | */ |
| 9234 | private static void removeKeystoreDataIfNeeded(int userId, int appId) { |
| 9235 | if (appId < 0) { |
| 9236 | return; |
| 9237 | } |
| 9238 | |
| 9239 | final KeyStore keyStore = KeyStore.getInstance(); |
| 9240 | if (keyStore != null) { |
| 9241 | if (userId == UserHandle.USER_ALL) { |
| 9242 | for (final int individual : sUserManager.getUserIds()) { |
| 9243 | keyStore.clearUid(UserHandle.getUid(individual, appId)); |
| 9244 | } |
| 9245 | } else { |
| 9246 | keyStore.clearUid(UserHandle.getUid(userId, appId)); |
| 9247 | } |
| 9248 | } else { |
| 9249 | Slog.w(TAG, "Could not contact keystore to clear entries for app id " + appId); |
| 9250 | } |
| 9251 | } |
| 9252 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9253 | public void deleteApplicationCacheFiles(final String packageName, |
| 9254 | final IPackageDataObserver observer) { |
| 9255 | mContext.enforceCallingOrSelfPermission( |
| 9256 | android.Manifest.permission.DELETE_CACHE_FILES, null); |
| 9257 | // Queue up an async operation since the package deletion may take a little while. |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 9258 | final int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9259 | mHandler.post(new Runnable() { |
| 9260 | public void run() { |
| 9261 | mHandler.removeCallbacks(this); |
| 9262 | final boolean succeded; |
| 9263 | synchronized (mInstallLock) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9264 | succeded = deleteApplicationCacheFilesLI(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9265 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9266 | clearExternalStorageDataSync(packageName, userId, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9267 | if(observer != null) { |
| 9268 | try { |
| 9269 | observer.onRemoveCompleted(packageName, succeded); |
| 9270 | } catch (RemoteException e) { |
| 9271 | Log.i(TAG, "Observer no longer exists."); |
| 9272 | } |
| 9273 | } //end if observer |
| 9274 | } //end run |
| 9275 | }); |
| 9276 | } |
| 9277 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9278 | private boolean deleteApplicationCacheFilesLI(String packageName, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9279 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9280 | Slog.w(TAG, "Attempt to delete null packageName."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9281 | return false; |
| 9282 | } |
| 9283 | PackageParser.Package p; |
| 9284 | synchronized (mPackages) { |
| 9285 | p = mPackages.get(packageName); |
| 9286 | } |
| 9287 | if (p == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9288 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9289 | return false; |
| 9290 | } |
| 9291 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 9292 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9293 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9294 | return false; |
| 9295 | } |
Amith Yamasani | 54289b8 | 2012-10-01 10:39:14 -0700 | [diff] [blame] | 9296 | int retCode = mInstaller.deleteCacheFiles(packageName, userId); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 9297 | if (retCode < 0) { |
| 9298 | Slog.w(TAG, "Couldn't remove cache files for package: " |
Amith Yamasani | 54289b8 | 2012-10-01 10:39:14 -0700 | [diff] [blame] | 9299 | + packageName + " u" + userId); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 9300 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9301 | } |
| 9302 | return true; |
| 9303 | } |
| 9304 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 9305 | public void getPackageSizeInfo(final String packageName, int userHandle, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9306 | final IPackageStatsObserver observer) { |
| 9307 | mContext.enforceCallingOrSelfPermission( |
| 9308 | android.Manifest.permission.GET_PACKAGE_SIZE, null); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 9309 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 9310 | PackageStats stats = new PackageStats(packageName, userHandle); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 9311 | |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 9312 | /* |
| 9313 | * Queue up an async operation since the package measurement may take a |
| 9314 | * little while. |
| 9315 | */ |
| 9316 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| 9317 | msg.obj = new MeasureParams(stats, observer); |
| 9318 | mHandler.sendMessage(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9319 | } |
| 9320 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 9321 | private boolean getPackageSizeInfoLI(String packageName, int userHandle, |
| 9322 | PackageStats pStats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9323 | if (packageName == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9324 | 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] | 9325 | return false; |
| 9326 | } |
| 9327 | PackageParser.Package p; |
| 9328 | boolean dataOnly = false; |
Dianne Hackborn | ecc8d6f | 2013-05-01 18:54:11 -0700 | [diff] [blame] | 9329 | String libDirPath = null; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 9330 | String asecPath = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9331 | synchronized (mPackages) { |
| 9332 | p = mPackages.get(packageName); |
Dianne Hackborn | ecc8d6f | 2013-05-01 18:54:11 -0700 | [diff] [blame] | 9333 | PackageSetting ps = mSettings.mPackages.get(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9334 | if(p == null) { |
| 9335 | dataOnly = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9336 | if((ps == null) || (ps.pkg == null)) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9337 | Slog.w(TAG, "Package named '" + packageName +"' doesn't exist."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9338 | return false; |
| 9339 | } |
| 9340 | p = ps.pkg; |
| 9341 | } |
Dianne Hackborn | ecc8d6f | 2013-05-01 18:54:11 -0700 | [diff] [blame] | 9342 | if (ps != null) { |
| 9343 | libDirPath = ps.nativeLibraryPathString; |
| 9344 | } |
Kenny Root | 2afded1 | 2012-06-04 16:39:47 -0700 | [diff] [blame] | 9345 | if (p != null && (isExternal(p) || isForwardLocked(p))) { |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 9346 | String secureContainerId = cidFromCodePath(p.applicationInfo.sourceDir); |
| 9347 | if (secureContainerId != null) { |
| 9348 | asecPath = PackageHelper.getSdFilesystem(secureContainerId); |
| 9349 | } |
| 9350 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9351 | } |
| 9352 | String publicSrcDir = null; |
| 9353 | if(!dataOnly) { |
| 9354 | final ApplicationInfo applicationInfo = p.applicationInfo; |
| 9355 | if (applicationInfo == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9356 | Slog.w(TAG, "Package " + packageName + " has no applicationInfo."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9357 | return false; |
| 9358 | } |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 9359 | if (isForwardLocked(p)) { |
| 9360 | publicSrcDir = applicationInfo.publicSourceDir; |
| 9361 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9362 | } |
Dianne Hackborn | ecc8d6f | 2013-05-01 18:54:11 -0700 | [diff] [blame] | 9363 | int res = mInstaller.getSizeInfo(packageName, userHandle, p.mPath, libDirPath, |
| 9364 | publicSrcDir, asecPath, pStats); |
Jeff Brown | 5d6d90f | 2011-07-11 13:04:53 -0700 | [diff] [blame] | 9365 | if (res < 0) { |
| 9366 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9367 | } |
Kenny Root | 2afded1 | 2012-06-04 16:39:47 -0700 | [diff] [blame] | 9368 | |
| 9369 | // Fix-up for forward-locked applications in ASEC containers. |
| 9370 | if (!isExternal(p)) { |
| 9371 | pStats.codeSize += pStats.externalCodeSize; |
| 9372 | pStats.externalCodeSize = 0L; |
| 9373 | } |
| 9374 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9375 | return true; |
| 9376 | } |
| 9377 | |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 9378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9379 | public void addPackageToPreferred(String packageName) { |
Dianne Hackborn | 8377fc0 | 2010-06-07 15:54:58 -0700 | [diff] [blame] | 9380 | 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] | 9381 | } |
| 9382 | |
| 9383 | public void removePackageFromPreferred(String packageName) { |
Dianne Hackborn | 8377fc0 | 2010-06-07 15:54:58 -0700 | [diff] [blame] | 9384 | 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] | 9385 | } |
| 9386 | |
| 9387 | public List<PackageInfo> getPreferredPackages(int flags) { |
Dianne Hackborn | a7ca0e5 | 2009-12-01 14:31:55 -0800 | [diff] [blame] | 9388 | return new ArrayList<PackageInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9389 | } |
| 9390 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9391 | private int getUidTargetSdkVersionLockedLPr(int uid) { |
| 9392 | Object obj = mSettings.getUserIdLPr(uid); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9393 | if (obj instanceof SharedUserSetting) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 9394 | final SharedUserSetting sus = (SharedUserSetting) obj; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9395 | int vers = Build.VERSION_CODES.CUR_DEVELOPMENT; |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 9396 | final Iterator<PackageSetting> it = sus.packages.iterator(); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9397 | while (it.hasNext()) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9398 | final PackageSetting ps = it.next(); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9399 | if (ps.pkg != null) { |
| 9400 | int v = ps.pkg.applicationInfo.targetSdkVersion; |
| 9401 | if (v < vers) vers = v; |
| 9402 | } |
| 9403 | } |
| 9404 | return vers; |
| 9405 | } else if (obj instanceof PackageSetting) { |
Kenny Root | 60f7ad8 | 2011-03-22 12:49:06 -0700 | [diff] [blame] | 9406 | final PackageSetting ps = (PackageSetting) obj; |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9407 | if (ps.pkg != null) { |
| 9408 | return ps.pkg.applicationInfo.targetSdkVersion; |
| 9409 | } |
| 9410 | } |
| 9411 | return Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 9412 | } |
| 9413 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9414 | public void addPreferredActivity(IntentFilter filter, int match, |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9415 | ComponentName[] set, ComponentName activity, int userId) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9416 | // writer |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9417 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 9418 | enforceCrossUserPermission(callingUid, userId, true, "add preferred activity"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9419 | synchronized (mPackages) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9420 | if (mContext.checkCallingOrSelfPermission( |
| 9421 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 9422 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9423 | if (getUidTargetSdkVersionLockedLPr(callingUid) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9424 | < Build.VERSION_CODES.FROYO) { |
| 9425 | Slog.w(TAG, "Ignoring addPreferredActivity() from uid " |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9426 | + callingUid); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9427 | return; |
| 9428 | } |
| 9429 | mContext.enforceCallingOrSelfPermission( |
| 9430 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 9431 | } |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9432 | |
| 9433 | Slog.i(TAG, "Adding preferred activity " + activity + " for user " + userId + " :"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9434 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9435 | mSettings.editPreferredActivitiesLPw(userId).addFilter( |
| 9436 | new PreferredActivity(filter, match, set, activity)); |
| 9437 | mSettings.writePackageRestrictionsLPr(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9438 | } |
| 9439 | } |
| 9440 | |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 9441 | public void replacePreferredActivity(IntentFilter filter, int match, |
| 9442 | ComponentName[] set, ComponentName activity) { |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 9443 | if (filter.countActions() != 1) { |
| 9444 | throw new IllegalArgumentException( |
| 9445 | "replacePreferredActivity expects filter to have only 1 action."); |
| 9446 | } |
| 9447 | if (filter.countCategories() != 1) { |
| 9448 | throw new IllegalArgumentException( |
| 9449 | "replacePreferredActivity expects filter to have only 1 category."); |
| 9450 | } |
| 9451 | if (filter.countDataAuthorities() != 0 |
| 9452 | || filter.countDataPaths() != 0 |
| 9453 | || filter.countDataSchemes() != 0 |
| 9454 | || filter.countDataTypes() != 0) { |
| 9455 | throw new IllegalArgumentException( |
| 9456 | "replacePreferredActivity expects filter to have no data authorities, " + |
| 9457 | "paths, schemes or types."); |
| 9458 | } |
| 9459 | synchronized (mPackages) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9460 | if (mContext.checkCallingOrSelfPermission( |
| 9461 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 9462 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9463 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9464 | < Build.VERSION_CODES.FROYO) { |
| 9465 | Slog.w(TAG, "Ignoring replacePreferredActivity() from uid " |
| 9466 | + Binder.getCallingUid()); |
| 9467 | return; |
| 9468 | } |
| 9469 | mContext.enforceCallingOrSelfPermission( |
| 9470 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 9471 | } |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9472 | |
| 9473 | final int callingUserId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 9474 | ArrayList<PreferredActivity> removed = null; |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9475 | PreferredIntentResolver pir = mSettings.mPreferredActivities.get(callingUserId); |
| 9476 | if (pir != null) { |
| 9477 | Iterator<PreferredActivity> it = pir.filterIterator(); |
| 9478 | String action = filter.getAction(0); |
| 9479 | String category = filter.getCategory(0); |
| 9480 | while (it.hasNext()) { |
| 9481 | PreferredActivity pa = it.next(); |
| 9482 | if (pa.getAction(0).equals(action) && pa.getCategory(0).equals(category)) { |
| 9483 | if (removed == null) { |
| 9484 | removed = new ArrayList<PreferredActivity>(); |
| 9485 | } |
| 9486 | removed.add(pa); |
| 9487 | Log.i(TAG, "Removing preferred activity " + pa.mPref.mComponent + ":"); |
| 9488 | filter.dump(new LogPrinter(Log.INFO, TAG), " "); |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 9489 | } |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 9490 | } |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9491 | if (removed != null) { |
| 9492 | for (int i=0; i<removed.size(); i++) { |
| 9493 | PreferredActivity pa = removed.get(i); |
| 9494 | pir.removeFilter(pa); |
| 9495 | } |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 9496 | } |
| 9497 | } |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9498 | addPreferredActivity(filter, match, set, activity, callingUserId); |
Satish Sampath | 8dbe612 | 2009-06-02 23:35:54 +0100 | [diff] [blame] | 9499 | } |
| 9500 | } |
| 9501 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9502 | public void clearPackagePreferredActivities(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9503 | final int uid = Binder.getCallingUid(); |
| 9504 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9505 | synchronized (mPackages) { |
Dianne Hackborn | 2ee89ea | 2010-03-10 18:27:09 -0800 | [diff] [blame] | 9506 | PackageParser.Package pkg = mPackages.get(packageName); |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9507 | if (pkg == null || pkg.applicationInfo.uid != uid) { |
| 9508 | if (mContext.checkCallingOrSelfPermission( |
| 9509 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS) |
| 9510 | != PackageManager.PERMISSION_GRANTED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9511 | if (getUidTargetSdkVersionLockedLPr(Binder.getCallingUid()) |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9512 | < Build.VERSION_CODES.FROYO) { |
| 9513 | Slog.w(TAG, "Ignoring clearPackagePreferredActivities() from uid " |
| 9514 | + Binder.getCallingUid()); |
| 9515 | return; |
| 9516 | } |
| 9517 | mContext.enforceCallingOrSelfPermission( |
| 9518 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 9519 | } |
Dianne Hackborn | 2ee89ea | 2010-03-10 18:27:09 -0800 | [diff] [blame] | 9520 | } |
| 9521 | |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 9522 | int user = UserHandle.getCallingUserId(); |
| 9523 | if (clearPackagePreferredActivitiesLPw(packageName, user)) { |
| 9524 | mSettings.writePackageRestrictionsLPr(user); |
| 9525 | scheduleWriteSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9526 | } |
| 9527 | } |
| 9528 | } |
| 9529 | |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9530 | /** This method takes a specific user id as well as UserHandle.USER_ALL. */ |
| 9531 | boolean clearPackagePreferredActivitiesLPw(String packageName, int userId) { |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 9532 | ArrayList<PreferredActivity> removed = null; |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9533 | boolean changed = false; |
| 9534 | for (int i=0; i<mSettings.mPreferredActivities.size(); i++) { |
| 9535 | final int thisUserId = mSettings.mPreferredActivities.keyAt(i); |
| 9536 | PreferredIntentResolver pir = mSettings.mPreferredActivities.valueAt(i); |
| 9537 | if (userId != UserHandle.USER_ALL && userId != thisUserId) { |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9538 | continue; |
| 9539 | } |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9540 | Iterator<PreferredActivity> it = pir.filterIterator(); |
| 9541 | while (it.hasNext()) { |
| 9542 | PreferredActivity pa = it.next(); |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 9543 | if (packageName == null || |
| 9544 | pa.mPref.mComponent.getPackageName().equals(packageName)) { |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9545 | if (removed == null) { |
| 9546 | removed = new ArrayList<PreferredActivity>(); |
| 9547 | } |
| 9548 | removed.add(pa); |
Dianne Hackborn | 0450510 | 2012-02-29 12:34:04 -0800 | [diff] [blame] | 9549 | } |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9550 | } |
| 9551 | if (removed != null) { |
| 9552 | for (int j=0; j<removed.size(); j++) { |
| 9553 | PreferredActivity pa = removed.get(j); |
| 9554 | pir.removeFilter(pa); |
| 9555 | } |
| 9556 | changed = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9557 | } |
| 9558 | } |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9559 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9560 | } |
| 9561 | |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 9562 | public void resetPreferredActivities(int userId) { |
| 9563 | mContext.enforceCallingOrSelfPermission( |
| 9564 | android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null); |
| 9565 | // writer |
| 9566 | synchronized (mPackages) { |
| 9567 | int user = UserHandle.getCallingUserId(); |
| 9568 | clearPackagePreferredActivitiesLPw(null, user); |
| 9569 | mSettings.readDefaultPreferredAppsLPw(this, user); |
| 9570 | mSettings.writePackageRestrictionsLPr(user); |
| 9571 | scheduleWriteSettingsLocked(); |
| 9572 | } |
| 9573 | } |
| 9574 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9575 | public int getPreferredActivities(List<IntentFilter> outFilters, |
| 9576 | List<ComponentName> outActivities, String packageName) { |
| 9577 | |
| 9578 | int num = 0; |
Amith Yamasani | a3f133a | 2012-08-09 17:11:28 -0700 | [diff] [blame] | 9579 | final int userId = UserHandle.getCallingUserId(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9580 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9581 | synchronized (mPackages) { |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9582 | PreferredIntentResolver pir = mSettings.mPreferredActivities.get(userId); |
| 9583 | if (pir != null) { |
| 9584 | final Iterator<PreferredActivity> it = pir.filterIterator(); |
| 9585 | while (it.hasNext()) { |
| 9586 | final PreferredActivity pa = it.next(); |
| 9587 | if (packageName == null |
| 9588 | || pa.mPref.mComponent.getPackageName().equals(packageName)) { |
| 9589 | if (outFilters != null) { |
| 9590 | outFilters.add(new IntentFilter(pa)); |
| 9591 | } |
| 9592 | if (outActivities != null) { |
| 9593 | outActivities.add(pa.mPref.mComponent); |
| 9594 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9595 | } |
| 9596 | } |
| 9597 | } |
| 9598 | } |
| 9599 | |
| 9600 | return num; |
| 9601 | } |
| 9602 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9603 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9604 | public void setApplicationEnabledSetting(String appPackageName, |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 9605 | int newState, int flags, int userId, String callingPackage) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9606 | if (!sUserManager.exists(userId)) return; |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 9607 | if (callingPackage == null) { |
| 9608 | callingPackage = Integer.toString(Binder.getCallingUid()); |
| 9609 | } |
| 9610 | setEnabledSetting(appPackageName, null, newState, flags, userId, callingPackage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9611 | } |
| 9612 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9613 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9614 | public void setComponentEnabledSetting(ComponentName componentName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9615 | int newState, int flags, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9616 | if (!sUserManager.exists(userId)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9617 | setEnabledSetting(componentName.getPackageName(), |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 9618 | componentName.getClassName(), newState, flags, userId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9619 | } |
| 9620 | |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 9621 | private void setEnabledSetting(final String packageName, String className, int newState, |
| 9622 | final int flags, int userId, String callingPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9623 | if (!(newState == COMPONENT_ENABLED_STATE_DEFAULT |
| 9624 | || newState == COMPONENT_ENABLED_STATE_ENABLED |
Dianne Hackborn | 0ac3031 | 2011-06-17 14:49:23 -0700 | [diff] [blame] | 9625 | || newState == COMPONENT_ENABLED_STATE_DISABLED |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 9626 | || newState == COMPONENT_ENABLED_STATE_DISABLED_USER |
| 9627 | || newState == COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9628 | throw new IllegalArgumentException("Invalid new component state: " |
| 9629 | + newState); |
| 9630 | } |
| 9631 | PackageSetting pkgSetting; |
| 9632 | final int uid = Binder.getCallingUid(); |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 9633 | final int permission = mContext.checkCallingOrSelfPermission( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9634 | android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 9635 | enforceCrossUserPermission(uid, userId, false, "set enabled"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9636 | final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9637 | boolean sendNow = false; |
| 9638 | boolean isApp = (className == null); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 9639 | String componentName = isApp ? packageName : className; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9640 | int packageUid = -1; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 9641 | ArrayList<String> components; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9642 | |
| 9643 | // writer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9644 | synchronized (mPackages) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9645 | pkgSetting = mSettings.mPackages.get(packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9646 | if (pkgSetting == null) { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9647 | if (className == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9648 | throw new IllegalArgumentException( |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9649 | "Unknown package: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9650 | } |
| 9651 | throw new IllegalArgumentException( |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9652 | "Unknown component: " + packageName |
| 9653 | + "/" + className); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9654 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9655 | // Allow root and verify that userId is not being specified by a different user |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 9656 | if (!allowedByPermission && !UserHandle.isSameApp(uid, pkgSetting.appId)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9657 | throw new SecurityException( |
| 9658 | "Permission Denial: attempt to change component state from pid=" |
| 9659 | + Binder.getCallingPid() |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9660 | + ", uid=" + uid + ", package uid=" + pkgSetting.appId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9661 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9662 | if (className == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9663 | // We're dealing with an application/package level state change |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9664 | if (pkgSetting.getEnabled(userId) == newState) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 9665 | // Nothing to do |
| 9666 | return; |
| 9667 | } |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 9668 | if (newState == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT |
| 9669 | || newState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) { |
| 9670 | // Don't care about who enables an app. |
| 9671 | callingPackage = null; |
| 9672 | } |
| 9673 | pkgSetting.setEnabled(newState, userId, callingPackage); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9674 | // pkgSetting.pkg.mSetEnabled = newState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9675 | } else { |
| 9676 | // We're dealing with a component level state change |
Dianne Hackborn | 6569625 | 2012-03-05 18:49:21 -0800 | [diff] [blame] | 9677 | // First, verify that this is a valid class name. |
| 9678 | PackageParser.Package pkg = pkgSetting.pkg; |
| 9679 | if (pkg == null || !pkg.hasComponentClassName(className)) { |
| 9680 | if (pkg.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.JELLY_BEAN) { |
| 9681 | throw new IllegalArgumentException("Component class " + className |
| 9682 | + " does not exist in " + packageName); |
| 9683 | } else { |
| 9684 | Slog.w(TAG, "Failed setComponentEnabledSetting: component class " |
| 9685 | + className + " does not exist in " + packageName); |
| 9686 | } |
| 9687 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9688 | switch (newState) { |
| 9689 | case COMPONENT_ENABLED_STATE_ENABLED: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9690 | if (!pkgSetting.enableComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 9691 | return; |
| 9692 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9693 | break; |
| 9694 | case COMPONENT_ENABLED_STATE_DISABLED: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9695 | if (!pkgSetting.disableComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 9696 | return; |
| 9697 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9698 | break; |
| 9699 | case COMPONENT_ENABLED_STATE_DEFAULT: |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9700 | if (!pkgSetting.restoreComponentLPw(className, userId)) { |
Dianne Hackborn | d7c0968 | 2010-03-30 10:42:20 -0700 | [diff] [blame] | 9701 | return; |
| 9702 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9703 | break; |
| 9704 | default: |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 9705 | Slog.e(TAG, "Invalid new component state: " + newState); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9706 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9707 | } |
| 9708 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9709 | mSettings.writePackageRestrictionsLPr(userId); |
Christopher Tate | 1e08f5c | 2013-05-02 13:27:27 -0700 | [diff] [blame] | 9710 | components = mPendingBroadcasts.get(userId, packageName); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9711 | final boolean newPackage = components == null; |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 9712 | if (newPackage) { |
| 9713 | components = new ArrayList<String>(); |
| 9714 | } |
| 9715 | if (!components.contains(componentName)) { |
| 9716 | components.add(componentName); |
| 9717 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9718 | if ((flags&PackageManager.DONT_KILL_APP) == 0) { |
| 9719 | sendNow = true; |
| 9720 | // Purge entry from pending broadcast list if another one exists already |
| 9721 | // since we are sending one right away. |
Christopher Tate | 1e08f5c | 2013-05-02 13:27:27 -0700 | [diff] [blame] | 9722 | mPendingBroadcasts.remove(userId, packageName); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9723 | } else { |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 9724 | if (newPackage) { |
Christopher Tate | 1e08f5c | 2013-05-02 13:27:27 -0700 | [diff] [blame] | 9725 | mPendingBroadcasts.put(userId, packageName, components); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9726 | } |
| 9727 | if (!mHandler.hasMessages(SEND_PENDING_BROADCAST)) { |
| 9728 | // Schedule a message |
| 9729 | mHandler.sendEmptyMessageDelayed(SEND_PENDING_BROADCAST, BROADCAST_DELAY); |
| 9730 | } |
| 9731 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9732 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9733 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9734 | long callingId = Binder.clearCallingIdentity(); |
| 9735 | try { |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9736 | if (sendNow) { |
Christopher Tate | 1e08f5c | 2013-05-02 13:27:27 -0700 | [diff] [blame] | 9737 | packageUid = UserHandle.getUid(userId, pkgSetting.appId); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9738 | sendPackageChangedBroadcast(packageName, |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 9739 | (flags&PackageManager.DONT_KILL_APP) != 0, components, packageUid); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9740 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9741 | } finally { |
| 9742 | Binder.restoreCallingIdentity(callingId); |
| 9743 | } |
| 9744 | } |
| 9745 | |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9746 | private void sendPackageChangedBroadcast(String packageName, |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 9747 | boolean killFlag, ArrayList<String> componentNames, int packageUid) { |
Kenny Root | 4dfe6ff | 2011-02-23 17:00:43 -0800 | [diff] [blame] | 9748 | if (DEBUG_INSTALL) |
| 9749 | Log.v(TAG, "Sending package changed: package=" + packageName + " components=" |
| 9750 | + componentNames); |
Dianne Hackborn | 86a72da | 2009-11-11 20:12:41 -0800 | [diff] [blame] | 9751 | Bundle extras = new Bundle(4); |
| 9752 | extras.putString(Intent.EXTRA_CHANGED_COMPONENT_NAME, componentNames.get(0)); |
| 9753 | String nameList[] = new String[componentNames.size()]; |
| 9754 | componentNames.toArray(nameList); |
| 9755 | extras.putStringArray(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST, nameList); |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9756 | extras.putBoolean(Intent.EXTRA_DONT_KILL_APP, killFlag); |
| 9757 | extras.putInt(Intent.EXTRA_UID, packageUid); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9758 | sendPackageBroadcast(Intent.ACTION_PACKAGE_CHANGED, packageName, extras, null, null, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 9759 | new int[] {UserHandle.getUserId(packageUid)}); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 9760 | } |
| 9761 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9762 | public void setPackageStoppedState(String packageName, boolean stopped, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9763 | if (!sUserManager.exists(userId)) return; |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 9764 | final int uid = Binder.getCallingUid(); |
| 9765 | final int permission = mContext.checkCallingOrSelfPermission( |
| 9766 | android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); |
| 9767 | final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 9768 | enforceCrossUserPermission(uid, userId, true, "stop package"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9769 | // writer |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 9770 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9771 | if (mSettings.setPackageStoppedStateLPw(packageName, stopped, allowedByPermission, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9772 | uid, userId)) { |
| 9773 | scheduleWritePackageRestrictionsLocked(userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 9774 | } |
| 9775 | } |
Suchi Amalapurapu | 0214e94 | 2009-09-02 11:03:18 -0700 | [diff] [blame] | 9776 | } |
| 9777 | |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 9778 | public String getInstallerPackageName(String packageName) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9779 | // reader |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 9780 | synchronized (mPackages) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9781 | return mSettings.getInstallerPackageNameLPr(packageName); |
Jacek Surazski | 65e1317 | 2009-04-28 15:26:38 +0200 | [diff] [blame] | 9782 | } |
| 9783 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 9784 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9785 | @Override |
| 9786 | public int getApplicationEnabledSetting(String packageName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9787 | if (!sUserManager.exists(userId)) return COMPONENT_ENABLED_STATE_DISABLED; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9788 | int uid = Binder.getCallingUid(); |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 9789 | enforceCrossUserPermission(uid, userId, false, "get enabled"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9790 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9791 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9792 | return mSettings.getApplicationEnabledSettingLPr(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9793 | } |
| 9794 | } |
| 9795 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9796 | @Override |
| 9797 | public int getComponentEnabledSetting(ComponentName componentName, int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 9798 | if (!sUserManager.exists(userId)) return COMPONENT_ENABLED_STATE_DISABLED; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9799 | int uid = Binder.getCallingUid(); |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 9800 | enforceCrossUserPermission(uid, userId, false, "get component enabled"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9801 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9802 | synchronized (mPackages) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 9803 | return mSettings.getComponentEnabledSettingLPr(componentName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9804 | } |
| 9805 | } |
| 9806 | |
| 9807 | public void enterSafeMode() { |
Kenny Root | 461ff1f | 2011-08-09 09:43:03 -0700 | [diff] [blame] | 9808 | enforceSystemOrRoot("Only the system can request entering safe mode"); |
| 9809 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9810 | if (!mSystemReady) { |
| 9811 | mSafeMode = true; |
| 9812 | } |
| 9813 | } |
| 9814 | |
| 9815 | public void systemReady() { |
| 9816 | mSystemReady = true; |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 9817 | |
| 9818 | // Read the compatibilty setting when the system is ready. |
Jeff Sharkey | 6e2bee7 | 2012-10-01 13:39:08 -0700 | [diff] [blame] | 9819 | boolean compatibilityModeEnabled = android.provider.Settings.Global.getInt( |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 9820 | mContext.getContentResolver(), |
Jeff Sharkey | 6e2bee7 | 2012-10-01 13:39:08 -0700 | [diff] [blame] | 9821 | android.provider.Settings.Global.COMPATIBILITY_MODE, 1) == 1; |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 9822 | PackageParser.setCompatibilityModeEnabled(compatibilityModeEnabled); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 9823 | if (DEBUG_SETTINGS) { |
Mitsuru Oshima | 69fff4a | 2009-07-21 09:51:05 -0700 | [diff] [blame] | 9824 | Log.d(TAG, "compatibility mode:" + compatibilityModeEnabled); |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 9825 | } |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9826 | |
| 9827 | synchronized (mPackages) { |
| 9828 | // Verify that all of the preferred activity components actually |
| 9829 | // exist. It is possible for applications to be updated and at |
| 9830 | // that point remove a previously declared activity component that |
| 9831 | // had been set as a preferred activity. We try to clean this up |
| 9832 | // the next time we encounter that preferred activity, but it is |
| 9833 | // possible for the user flow to never be able to return to that |
| 9834 | // situation so here we do a sanity check to make sure we haven't |
| 9835 | // left any junk around. |
| 9836 | ArrayList<PreferredActivity> removed = new ArrayList<PreferredActivity>(); |
| 9837 | for (int i=0; i<mSettings.mPreferredActivities.size(); i++) { |
| 9838 | PreferredIntentResolver pir = mSettings.mPreferredActivities.valueAt(i); |
| 9839 | removed.clear(); |
| 9840 | for (PreferredActivity pa : pir.filterSet()) { |
| 9841 | if (mActivities.mActivities.get(pa.mPref.mComponent) == null) { |
| 9842 | removed.add(pa); |
| 9843 | } |
| 9844 | } |
| 9845 | if (removed.size() > 0) { |
| 9846 | for (int j=0; j<removed.size(); j++) { |
| 9847 | PreferredActivity pa = removed.get(i); |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9848 | Slog.w(TAG, "Removing dangling preferred activity: " |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 9849 | + pa.mPref.mComponent); |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 9850 | pir.removeFilter(pa); |
| 9851 | } |
| 9852 | mSettings.writePackageRestrictionsLPr( |
| 9853 | mSettings.mPreferredActivities.keyAt(i)); |
| 9854 | } |
| 9855 | } |
| 9856 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9857 | } |
| 9858 | |
| 9859 | public boolean isSafeMode() { |
| 9860 | return mSafeMode; |
| 9861 | } |
| 9862 | |
| 9863 | public boolean hasSystemUidErrors() { |
| 9864 | return mHasSystemUidErrors; |
| 9865 | } |
| 9866 | |
| 9867 | static String arrayToString(int[] array) { |
| 9868 | StringBuffer buf = new StringBuffer(128); |
| 9869 | buf.append('['); |
| 9870 | if (array != null) { |
| 9871 | for (int i=0; i<array.length; i++) { |
| 9872 | if (i > 0) buf.append(", "); |
| 9873 | buf.append(array[i]); |
| 9874 | } |
| 9875 | } |
| 9876 | buf.append(']'); |
| 9877 | return buf.toString(); |
| 9878 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 9879 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9880 | static class DumpState { |
| 9881 | public static final int DUMP_LIBS = 1 << 0; |
| 9882 | |
| 9883 | public static final int DUMP_FEATURES = 1 << 1; |
| 9884 | |
| 9885 | public static final int DUMP_RESOLVERS = 1 << 2; |
| 9886 | |
| 9887 | public static final int DUMP_PERMISSIONS = 1 << 3; |
| 9888 | |
| 9889 | public static final int DUMP_PACKAGES = 1 << 4; |
| 9890 | |
| 9891 | public static final int DUMP_SHARED_USERS = 1 << 5; |
| 9892 | |
| 9893 | public static final int DUMP_MESSAGES = 1 << 6; |
| 9894 | |
| 9895 | public static final int DUMP_PROVIDERS = 1 << 7; |
| 9896 | |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 9897 | public static final int DUMP_VERIFIERS = 1 << 8; |
| 9898 | |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 9899 | public static final int DUMP_PREFERRED = 1 << 9; |
| 9900 | |
| 9901 | public static final int DUMP_PREFERRED_XML = 1 << 10; |
| 9902 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9903 | public static final int OPTION_SHOW_FILTERS = 1 << 0; |
| 9904 | |
| 9905 | private int mTypes; |
| 9906 | |
| 9907 | private int mOptions; |
| 9908 | |
| 9909 | private boolean mTitlePrinted; |
| 9910 | |
| 9911 | private SharedUserSetting mSharedUser; |
| 9912 | |
| 9913 | public boolean isDumping(int type) { |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 9914 | if (mTypes == 0 && type != DUMP_PREFERRED_XML) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9915 | return true; |
| 9916 | } |
| 9917 | |
| 9918 | return (mTypes & type) != 0; |
| 9919 | } |
| 9920 | |
| 9921 | public void setDump(int type) { |
| 9922 | mTypes |= type; |
| 9923 | } |
| 9924 | |
| 9925 | public boolean isOptionEnabled(int option) { |
| 9926 | return (mOptions & option) != 0; |
| 9927 | } |
| 9928 | |
| 9929 | public void setOptionEnabled(int option) { |
| 9930 | mOptions |= option; |
| 9931 | } |
| 9932 | |
| 9933 | public boolean onTitlePrinted() { |
| 9934 | final boolean printed = mTitlePrinted; |
| 9935 | mTitlePrinted = true; |
| 9936 | return printed; |
| 9937 | } |
| 9938 | |
| 9939 | public boolean getTitlePrinted() { |
| 9940 | return mTitlePrinted; |
| 9941 | } |
| 9942 | |
| 9943 | public void setTitlePrinted(boolean enabled) { |
| 9944 | mTitlePrinted = enabled; |
| 9945 | } |
| 9946 | |
| 9947 | public SharedUserSetting getSharedUser() { |
| 9948 | return mSharedUser; |
| 9949 | } |
| 9950 | |
| 9951 | public void setSharedUser(SharedUserSetting user) { |
| 9952 | mSharedUser = user; |
| 9953 | } |
| 9954 | } |
| 9955 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9956 | @Override |
| 9957 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 9958 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 9959 | != PackageManager.PERMISSION_GRANTED) { |
| 9960 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 9961 | + Binder.getCallingPid() |
| 9962 | + ", uid=" + Binder.getCallingUid() |
| 9963 | + " without permission " |
| 9964 | + android.Manifest.permission.DUMP); |
| 9965 | return; |
| 9966 | } |
| 9967 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 9968 | DumpState dumpState = new DumpState(); |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 9969 | boolean fullPreferred = false; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 9970 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 9971 | String packageName = null; |
| 9972 | |
| 9973 | int opti = 0; |
| 9974 | while (opti < args.length) { |
| 9975 | String opt = args[opti]; |
| 9976 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 9977 | break; |
| 9978 | } |
| 9979 | opti++; |
| 9980 | if ("-a".equals(opt)) { |
| 9981 | // Right now we only know how to print all. |
| 9982 | } else if ("-h".equals(opt)) { |
| 9983 | pw.println("Package manager dump options:"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 9984 | pw.println(" [-h] [-f] [cmd] ..."); |
| 9985 | pw.println(" -f: print details of intent filters"); |
| 9986 | pw.println(" -h: print this help"); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 9987 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 9988 | pw.println(" l[ibraries]: list known shared libraries"); |
| 9989 | pw.println(" f[ibraries]: list device features"); |
| 9990 | pw.println(" r[esolvers]: dump intent resolvers"); |
| 9991 | pw.println(" perm[issions]: dump permissions"); |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 9992 | pw.println(" pref[erred]: print preferred package settings"); |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 9993 | pw.println(" preferred-xml [--full]: print preferred package settings as xml"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 9994 | pw.println(" prov[iders]: dump content providers"); |
| 9995 | pw.println(" p[ackages]: dump installed packages"); |
| 9996 | pw.println(" s[hared-users]: dump shared user IDs"); |
| 9997 | pw.println(" m[essages]: print collected runtime messages"); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 9998 | pw.println(" v[erifiers]: print package verifier info"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 9999 | pw.println(" <package.name>: info about given package"); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10000 | return; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10001 | } else if ("-f".equals(opt)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10002 | dumpState.setOptionEnabled(DumpState.OPTION_SHOW_FILTERS); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10003 | } else { |
| 10004 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 10005 | } |
| 10006 | } |
| 10007 | |
| 10008 | // Is the caller requesting to dump a particular piece of data? |
| 10009 | if (opti < args.length) { |
| 10010 | String cmd = args[opti]; |
| 10011 | opti++; |
| 10012 | // Is this a package name? |
| 10013 | if ("android".equals(cmd) || cmd.contains(".")) { |
| 10014 | packageName = cmd; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10015 | } else if ("l".equals(cmd) || "libraries".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10016 | dumpState.setDump(DumpState.DUMP_LIBS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10017 | } else if ("f".equals(cmd) || "features".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10018 | dumpState.setDump(DumpState.DUMP_FEATURES); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10019 | } else if ("r".equals(cmd) || "resolvers".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10020 | dumpState.setDump(DumpState.DUMP_RESOLVERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10021 | } else if ("perm".equals(cmd) || "permissions".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10022 | dumpState.setDump(DumpState.DUMP_PERMISSIONS); |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 10023 | } else if ("pref".equals(cmd) || "preferred".equals(cmd)) { |
| 10024 | dumpState.setDump(DumpState.DUMP_PREFERRED); |
| 10025 | } else if ("preferred-xml".equals(cmd)) { |
| 10026 | dumpState.setDump(DumpState.DUMP_PREFERRED_XML); |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 10027 | if (opti < args.length && "--full".equals(args[opti])) { |
| 10028 | fullPreferred = true; |
Dianne Hackborn | d4d9364 | 2013-01-29 16:12:48 -0800 | [diff] [blame] | 10029 | opti++; |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 10030 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10031 | } else if ("p".equals(cmd) || "packages".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10032 | dumpState.setDump(DumpState.DUMP_PACKAGES); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10033 | } else if ("s".equals(cmd) || "shared-users".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10034 | dumpState.setDump(DumpState.DUMP_SHARED_USERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10035 | } else if ("prov".equals(cmd) || "providers".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10036 | dumpState.setDump(DumpState.DUMP_PROVIDERS); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10037 | } else if ("m".equals(cmd) || "messages".equals(cmd)) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10038 | dumpState.setDump(DumpState.DUMP_MESSAGES); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 10039 | } else if ("v".equals(cmd) || "verifiers".equals(cmd)) { |
| 10040 | dumpState.setDump(DumpState.DUMP_VERIFIERS); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10041 | } |
| 10042 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10043 | |
| 10044 | // reader |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10045 | synchronized (mPackages) { |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 10046 | if (dumpState.isDumping(DumpState.DUMP_VERIFIERS) && packageName == null) { |
| 10047 | if (dumpState.onTitlePrinted()) |
| 10048 | pw.println(" "); |
| 10049 | pw.println("Verifiers:"); |
| 10050 | pw.print(" Required: "); |
| 10051 | pw.print(mRequiredVerifierPackage); |
| 10052 | pw.print(" (uid="); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 10053 | pw.print(getPackageUid(mRequiredVerifierPackage, 0)); |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 10054 | pw.println(")"); |
| 10055 | } |
| 10056 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10057 | if (dumpState.isDumping(DumpState.DUMP_LIBS) && packageName == null) { |
| 10058 | if (dumpState.onTitlePrinted()) |
| 10059 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10060 | pw.println("Libraries:"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10061 | final Iterator<String> it = mSharedLibraries.keySet().iterator(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10062 | while (it.hasNext()) { |
| 10063 | String name = it.next(); |
| 10064 | pw.print(" "); |
| 10065 | pw.print(name); |
| 10066 | pw.print(" -> "); |
Dianne Hackborn | c895be7 | 2013-03-11 17:48:43 -0700 | [diff] [blame] | 10067 | SharedLibraryEntry ent = mSharedLibraries.get(name); |
| 10068 | if (ent.path != null) { |
| 10069 | pw.print("(jar) "); |
| 10070 | pw.print(ent.path); |
| 10071 | } else { |
| 10072 | pw.print("(apk) "); |
| 10073 | pw.print(ent.apk); |
| 10074 | } |
| 10075 | pw.println(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10076 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10077 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10078 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10079 | if (dumpState.isDumping(DumpState.DUMP_FEATURES) && packageName == null) { |
| 10080 | if (dumpState.onTitlePrinted()) |
| 10081 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10082 | pw.println("Features:"); |
| 10083 | Iterator<String> it = mAvailableFeatures.keySet().iterator(); |
| 10084 | while (it.hasNext()) { |
| 10085 | String name = it.next(); |
| 10086 | pw.print(" "); |
| 10087 | pw.println(name); |
| 10088 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10089 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10090 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10091 | if (dumpState.isDumping(DumpState.DUMP_RESOLVERS)) { |
| 10092 | if (mActivities.dump(pw, dumpState.getTitlePrinted() ? "\nActivity Resolver Table:" |
| 10093 | : "Activity Resolver Table:", " ", packageName, |
| 10094 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 10095 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10096 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10097 | if (mReceivers.dump(pw, dumpState.getTitlePrinted() ? "\nReceiver Resolver Table:" |
| 10098 | : "Receiver Resolver Table:", " ", packageName, |
| 10099 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 10100 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10101 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10102 | if (mServices.dump(pw, dumpState.getTitlePrinted() ? "\nService Resolver Table:" |
| 10103 | : "Service Resolver Table:", " ", packageName, |
| 10104 | dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 10105 | dumpState.setTitlePrinted(true); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10106 | } |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 10107 | } |
| 10108 | |
| 10109 | if (dumpState.isDumping(DumpState.DUMP_PREFERRED)) { |
Dianne Hackborn | 6309271 | 2012-10-07 14:45:35 -0700 | [diff] [blame] | 10110 | for (int i=0; i<mSettings.mPreferredActivities.size(); i++) { |
| 10111 | PreferredIntentResolver pir = mSettings.mPreferredActivities.valueAt(i); |
| 10112 | int user = mSettings.mPreferredActivities.keyAt(i); |
| 10113 | if (pir.dump(pw, |
| 10114 | dumpState.getTitlePrinted() |
| 10115 | ? "\nPreferred Activities User " + user + ":" |
| 10116 | : "Preferred Activities User " + user + ":", " ", |
| 10117 | packageName, dumpState.isOptionEnabled(DumpState.OPTION_SHOW_FILTERS))) { |
| 10118 | dumpState.setTitlePrinted(true); |
| 10119 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10120 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10121 | } |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 10122 | |
| 10123 | if (dumpState.isDumping(DumpState.DUMP_PREFERRED_XML)) { |
| 10124 | pw.flush(); |
| 10125 | FileOutputStream fout = new FileOutputStream(fd); |
| 10126 | BufferedOutputStream str = new BufferedOutputStream(fout); |
| 10127 | XmlSerializer serializer = new FastXmlSerializer(); |
| 10128 | try { |
| 10129 | serializer.setOutput(str, "utf-8"); |
| 10130 | serializer.startDocument(null, true); |
| 10131 | serializer.setFeature( |
| 10132 | "http://xmlpull.org/v1/doc/features.html#indent-output", true); |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 10133 | mSettings.writePreferredActivitiesLPr(serializer, 0, fullPreferred); |
Dianne Hackborn | fc8b7fe | 2012-06-18 15:38:12 -0700 | [diff] [blame] | 10134 | serializer.endDocument(); |
| 10135 | serializer.flush(); |
| 10136 | } catch (IllegalArgumentException e) { |
| 10137 | pw.println("Failed writing: " + e); |
| 10138 | } catch (IllegalStateException e) { |
| 10139 | pw.println("Failed writing: " + e); |
| 10140 | } catch (IOException e) { |
| 10141 | pw.println("Failed writing: " + e); |
| 10142 | } |
| 10143 | } |
| 10144 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10145 | if (dumpState.isDumping(DumpState.DUMP_PERMISSIONS)) { |
| 10146 | mSettings.dumpPermissionsLPr(pw, packageName, dumpState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10147 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10148 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10149 | if (dumpState.isDumping(DumpState.DUMP_PROVIDERS)) { |
| 10150 | boolean printedSomething = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10151 | for (PackageParser.Provider p : mProvidersByComponent.values()) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10152 | if (packageName != null && !packageName.equals(p.info.packageName)) { |
| 10153 | continue; |
| 10154 | } |
| 10155 | if (!printedSomething) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10156 | if (dumpState.onTitlePrinted()) |
| 10157 | pw.println(" "); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10158 | pw.println("Registered ContentProviders:"); |
| 10159 | printedSomething = true; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10160 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10161 | pw.print(" "); pw.print(p.getComponentShortName()); pw.println(":"); |
| 10162 | pw.print(" "); pw.println(p.toString()); |
| 10163 | } |
| 10164 | printedSomething = false; |
| 10165 | for (Map.Entry<String, PackageParser.Provider> entry : mProviders.entrySet()) { |
| 10166 | PackageParser.Provider p = entry.getValue(); |
| 10167 | if (packageName != null && !packageName.equals(p.info.packageName)) { |
| 10168 | continue; |
| 10169 | } |
| 10170 | if (!printedSomething) { |
| 10171 | if (dumpState.onTitlePrinted()) |
| 10172 | pw.println(" "); |
| 10173 | pw.println("ContentProvider Authorities:"); |
| 10174 | printedSomething = true; |
| 10175 | } |
| 10176 | pw.print(" ["); pw.print(entry.getKey()); pw.println("]:"); |
| 10177 | pw.print(" "); pw.println(p.toString()); |
Kenny Root | eca64b3 | 2012-06-25 16:37:32 -0700 | [diff] [blame] | 10178 | if (p.info != null && p.info.applicationInfo != null) { |
| 10179 | final String appInfo = p.info.applicationInfo.toString(); |
| 10180 | pw.print(" applicationInfo="); pw.println(appInfo); |
| 10181 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10182 | } |
| 10183 | } |
| 10184 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10185 | if (dumpState.isDumping(DumpState.DUMP_PACKAGES)) { |
| 10186 | mSettings.dumpPackagesLPr(pw, packageName, dumpState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10187 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10188 | |
| 10189 | if (dumpState.isDumping(DumpState.DUMP_SHARED_USERS)) { |
| 10190 | mSettings.dumpSharedUsersLPr(pw, packageName, dumpState); |
Dianne Hackborn | 6dee18c | 2010-02-09 23:59:16 -0800 | [diff] [blame] | 10191 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10192 | |
| 10193 | if (dumpState.isDumping(DumpState.DUMP_MESSAGES) && packageName == null) { |
| 10194 | if (dumpState.onTitlePrinted()) |
| 10195 | pw.println(" "); |
| 10196 | mSettings.dumpReadMessagesLPr(pw, dumpState); |
| 10197 | |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10198 | pw.println(" "); |
| 10199 | pw.println("Package warning messages:"); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10200 | final File fname = getSettingsProblemFile(); |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 10201 | FileInputStream in = null; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10202 | try { |
| 10203 | in = new FileInputStream(fname); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10204 | final int avail = in.available(); |
| 10205 | final byte[] data = new byte[avail]; |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10206 | in.read(data); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 10207 | pw.print(new String(data)); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10208 | } catch (FileNotFoundException e) { |
| 10209 | } catch (IOException e) { |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 10210 | } finally { |
| 10211 | if (in != null) { |
| 10212 | try { |
| 10213 | in.close(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10214 | } catch (IOException e) { |
Brad Fitzpatrick | 6689ac8 | 2010-11-15 16:26:04 -0800 | [diff] [blame] | 10215 | } |
| 10216 | } |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 10217 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 10218 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10219 | } |
| 10220 | } |
| 10221 | |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 10222 | // ------- apps on sdcard specific code ------- |
| 10223 | static final boolean DEBUG_SD_INSTALL = false; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10224 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 10225 | private static final String SD_ENCRYPTION_KEYSTORE_NAME = "AppsOnSD"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10226 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 10227 | private static final String SD_ENCRYPTION_ALGORITHM = "AES"; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10228 | |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 10229 | private boolean mMediaMounted = false; |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 10230 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 10231 | private String getEncryptKey() { |
| 10232 | try { |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 10233 | String sdEncKey = SystemKeyStore.getInstance().retrieveKeyHexString( |
| 10234 | SD_ENCRYPTION_KEYSTORE_NAME); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 10235 | if (sdEncKey == null) { |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 10236 | sdEncKey = SystemKeyStore.getInstance().generateNewKeyHexString(128, |
| 10237 | SD_ENCRYPTION_ALGORITHM, SD_ENCRYPTION_KEYSTORE_NAME); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 10238 | if (sdEncKey == null) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 10239 | Slog.e(TAG, "Failed to create encryption keys"); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 10240 | return null; |
| 10241 | } |
| 10242 | } |
| 10243 | return sdEncKey; |
| 10244 | } catch (NoSuchAlgorithmException nsae) { |
Dianne Hackborn | a2fd9d1 | 2010-03-11 23:40:24 -0800 | [diff] [blame] | 10245 | Slog.e(TAG, "Failed to create encryption keys with exception: " + nsae); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 10246 | return null; |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 10247 | } catch (IOException ioe) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10248 | Slog.e(TAG, "Failed to retrieve encryption keys with exception: " + ioe); |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 10249 | return null; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 10250 | } |
Rich Cannings | 8d57883 | 2010-09-09 15:12:40 -0700 | [diff] [blame] | 10251 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 10252 | } |
| 10253 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10254 | /* package */static String getTempContainerId() { |
Kenny Root | c78a807 | 2010-07-27 15:18:38 -0700 | [diff] [blame] | 10255 | int tmpIdx = 1; |
| 10256 | String list[] = PackageHelper.getSecureContainerList(); |
| 10257 | if (list != null) { |
| 10258 | for (final String name : list) { |
| 10259 | // Ignore null and non-temporary container entries |
| 10260 | if (name == null || !name.startsWith(mTempContainerPrefix)) { |
| 10261 | continue; |
| 10262 | } |
| 10263 | |
| 10264 | String subStr = name.substring(mTempContainerPrefix.length()); |
| 10265 | try { |
| 10266 | int cid = Integer.parseInt(subStr); |
| 10267 | if (cid >= tmpIdx) { |
| 10268 | tmpIdx = cid + 1; |
| 10269 | } |
| 10270 | } catch (NumberFormatException e) { |
| 10271 | } |
| 10272 | } |
| 10273 | } |
| 10274 | return mTempContainerPrefix + tmpIdx; |
| 10275 | } |
Suchi Amalapurapu | af8e9f4 | 2010-01-12 10:17:28 -0800 | [diff] [blame] | 10276 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10277 | /* |
| 10278 | * Update media status on PackageManager. |
| 10279 | */ |
| 10280 | public void updateExternalMediaStatus(final boolean mediaStatus, final boolean reportStatus) { |
| 10281 | int callingUid = Binder.getCallingUid(); |
| 10282 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 10283 | throw new SecurityException("Media status can only be updated by the system"); |
| 10284 | } |
| 10285 | // reader; this apparently protects mMediaMounted, but should probably |
| 10286 | // be a different lock in that case. |
| 10287 | synchronized (mPackages) { |
| 10288 | Log.i(TAG, "Updating external media status from " |
| 10289 | + (mMediaMounted ? "mounted" : "unmounted") + " to " |
| 10290 | + (mediaStatus ? "mounted" : "unmounted")); |
| 10291 | if (DEBUG_SD_INSTALL) |
| 10292 | Log.i(TAG, "updateExternalMediaStatus:: mediaStatus=" + mediaStatus |
| 10293 | + ", mMediaMounted=" + mMediaMounted); |
| 10294 | if (mediaStatus == mMediaMounted) { |
| 10295 | final Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, reportStatus ? 1 |
| 10296 | : 0, -1); |
| 10297 | mHandler.sendMessage(msg); |
| 10298 | return; |
| 10299 | } |
| 10300 | mMediaMounted = mediaStatus; |
| 10301 | } |
| 10302 | // Queue up an async operation since the package installation may take a |
| 10303 | // little while. |
| 10304 | mHandler.post(new Runnable() { |
| 10305 | public void run() { |
Kenny Root | 438a0ef | 2012-08-09 13:36:16 -0700 | [diff] [blame] | 10306 | updateExternalMediaStatusInner(mediaStatus, reportStatus, true); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10307 | } |
| 10308 | }); |
| 10309 | } |
| 10310 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 10311 | /** |
| 10312 | * Called by MountService when the initial ASECs to scan are available. |
| 10313 | * Should block until all the ASEC containers are finished being scanned. |
| 10314 | */ |
| 10315 | public void scanAvailableAsecs() { |
Kenny Root | 438a0ef | 2012-08-09 13:36:16 -0700 | [diff] [blame] | 10316 | updateExternalMediaStatusInner(true, false, false); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 10317 | } |
| 10318 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10319 | /* |
| 10320 | * Collect information of applications on external media, map them against |
| 10321 | * existing containers and update information based on current mount status. |
| 10322 | * Please note that we always have to report status if reportStatus has been |
| 10323 | * set to true especially when unloading packages. |
| 10324 | */ |
Kenny Root | 438a0ef | 2012-08-09 13:36:16 -0700 | [diff] [blame] | 10325 | private void updateExternalMediaStatusInner(boolean isMounted, boolean reportStatus, |
| 10326 | boolean externalStorage) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10327 | // Collection of uids |
| 10328 | int uidArr[] = null; |
| 10329 | // Collection of stale containers |
| 10330 | HashSet<String> removeCids = new HashSet<String>(); |
| 10331 | // Collection of packages on external media with valid containers. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10332 | HashMap<AsecInstallArgs, String> processCids = new HashMap<AsecInstallArgs, String>(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10333 | // Get list of secure containers. |
| 10334 | final String list[] = PackageHelper.getSecureContainerList(); |
| 10335 | if (list == null || list.length == 0) { |
| 10336 | Log.i(TAG, "No secure containers on sdcard"); |
| 10337 | } else { |
| 10338 | // Process list of secure containers and categorize them |
| 10339 | // as active or stale based on their package internal state. |
| 10340 | int uidList[] = new int[list.length]; |
| 10341 | int num = 0; |
| 10342 | // reader |
| 10343 | synchronized (mPackages) { |
| 10344 | for (String cid : list) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10345 | if (DEBUG_SD_INSTALL) |
| 10346 | Log.i(TAG, "Processing container " + cid); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 10347 | String pkgName = getAsecPackageName(cid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10348 | if (pkgName == null) { |
| 10349 | if (DEBUG_SD_INSTALL) |
| 10350 | Log.i(TAG, "Container : " + cid + " stale"); |
| 10351 | removeCids.add(cid); |
| 10352 | continue; |
| 10353 | } |
| 10354 | if (DEBUG_SD_INSTALL) |
| 10355 | Log.i(TAG, "Looking for pkg : " + pkgName); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 10356 | |
| 10357 | final PackageSetting ps = mSettings.mPackages.get(pkgName); |
| 10358 | if (ps == null) { |
| 10359 | Log.i(TAG, "Deleting container with no matching settings " + cid); |
| 10360 | removeCids.add(cid); |
| 10361 | continue; |
| 10362 | } |
| 10363 | |
Kenny Root | 438a0ef | 2012-08-09 13:36:16 -0700 | [diff] [blame] | 10364 | /* |
| 10365 | * Skip packages that are not external if we're unmounting |
| 10366 | * external storage. |
| 10367 | */ |
| 10368 | if (externalStorage && !isMounted && !isExternal(ps)) { |
| 10369 | continue; |
| 10370 | } |
| 10371 | |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 10372 | final AsecInstallArgs args = new AsecInstallArgs(cid, isForwardLocked(ps)); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10373 | // The package status is changed only if the code path |
| 10374 | // matches between settings and the container id. |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 10375 | if (ps.codePathString != null && ps.codePathString.equals(args.getCodePath())) { |
| 10376 | if (DEBUG_SD_INSTALL) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10377 | Log.i(TAG, "Container : " + cid + " corresponds to pkg : " + pkgName |
| 10378 | + " at code path: " + ps.codePathString); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 10379 | } |
| 10380 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10381 | // We do have a valid package installed on sdcard |
| 10382 | processCids.put(args, ps.codePathString); |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 10383 | final int uid = ps.appId; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10384 | if (uid != -1) { |
| 10385 | uidList[num++] = uid; |
| 10386 | } |
| 10387 | } else { |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 10388 | Log.i(TAG, "Deleting stale container for " + cid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10389 | removeCids.add(cid); |
| 10390 | } |
| 10391 | } |
| 10392 | } |
| 10393 | |
| 10394 | if (num > 0) { |
| 10395 | // Sort uid list |
| 10396 | Arrays.sort(uidList, 0, num); |
| 10397 | // Throw away duplicates |
| 10398 | uidArr = new int[num]; |
| 10399 | uidArr[0] = uidList[0]; |
| 10400 | int di = 0; |
| 10401 | for (int i = 1; i < num; i++) { |
| 10402 | if (uidList[i - 1] != uidList[i]) { |
| 10403 | uidArr[di++] = uidList[i]; |
| 10404 | } |
| 10405 | } |
| 10406 | } |
| 10407 | } |
| 10408 | // Process packages with valid entries. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10409 | if (isMounted) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10410 | if (DEBUG_SD_INSTALL) |
| 10411 | Log.i(TAG, "Loading packages"); |
| 10412 | loadMediaPackages(processCids, uidArr, removeCids); |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 10413 | startCleaningPackages(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10414 | } else { |
| 10415 | if (DEBUG_SD_INSTALL) |
| 10416 | Log.i(TAG, "Unloading packages"); |
| 10417 | unloadMediaPackages(processCids, uidArr, reportStatus); |
| 10418 | } |
| 10419 | } |
| 10420 | |
| 10421 | private void sendResourcesChangedBroadcast(boolean mediaStatus, ArrayList<String> pkgList, |
| 10422 | int uidArr[], IIntentReceiver finishedReceiver) { |
| 10423 | int size = pkgList.size(); |
| 10424 | if (size > 0) { |
| 10425 | // Send broadcasts here |
| 10426 | Bundle extras = new Bundle(); |
| 10427 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, pkgList |
| 10428 | .toArray(new String[size])); |
| 10429 | if (uidArr != null) { |
| 10430 | extras.putIntArray(Intent.EXTRA_CHANGED_UID_LIST, uidArr); |
| 10431 | } |
| 10432 | String action = mediaStatus ? Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE |
| 10433 | : Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10434 | sendPackageBroadcast(action, null, extras, null, finishedReceiver, null); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10435 | } |
| 10436 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10437 | |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 10438 | /* |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10439 | * Look at potentially valid container ids from processCids If package |
| 10440 | * information doesn't match the one on record or package scanning fails, |
| 10441 | * the cid is added to list of removeCids. We currently don't delete stale |
| 10442 | * containers. |
| 10443 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10444 | private void loadMediaPackages(HashMap<AsecInstallArgs, String> processCids, int uidArr[], |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10445 | HashSet<String> removeCids) { |
| 10446 | ArrayList<String> pkgList = new ArrayList<String>(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10447 | Set<AsecInstallArgs> keys = processCids.keySet(); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10448 | boolean doGc = false; |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10449 | for (AsecInstallArgs args : keys) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10450 | String codePath = processCids.get(args); |
| 10451 | if (DEBUG_SD_INSTALL) |
| 10452 | Log.i(TAG, "Loading container : " + args.cid); |
| 10453 | int retCode = PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
| 10454 | try { |
| 10455 | // Make sure there are no container errors first. |
| 10456 | if (args.doPreInstall(PackageManager.INSTALL_SUCCEEDED) != PackageManager.INSTALL_SUCCEEDED) { |
| 10457 | Slog.e(TAG, "Failed to mount cid : " + args.cid |
| 10458 | + " when installing from sdcard"); |
| 10459 | continue; |
| 10460 | } |
| 10461 | // Check code path here. |
| 10462 | if (codePath == null || !codePath.equals(args.getCodePath())) { |
| 10463 | Slog.e(TAG, "Container " + args.cid + " cachepath " + args.getCodePath() |
| 10464 | + " does not match one in settings " + codePath); |
| 10465 | continue; |
| 10466 | } |
| 10467 | // Parse package |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 10468 | int parseFlags = mDefParseFlags; |
| 10469 | if (args.isExternal()) { |
| 10470 | parseFlags |= PackageParser.PARSE_ON_SDCARD; |
| 10471 | } |
Kenny Root | c7a8999 | 2012-06-05 15:13:17 -0700 | [diff] [blame] | 10472 | if (args.isFwdLocked()) { |
| 10473 | parseFlags |= PackageParser.PARSE_FORWARD_LOCK; |
| 10474 | } |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 10475 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10476 | doGc = true; |
| 10477 | synchronized (mInstallLock) { |
| 10478 | final PackageParser.Package pkg = scanPackageLI(new File(codePath), parseFlags, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10479 | 0, 0, null); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10480 | // Scan the package |
| 10481 | if (pkg != null) { |
| 10482 | /* |
| 10483 | * TODO why is the lock being held? doPostInstall is |
| 10484 | * called in other places without the lock. This needs |
| 10485 | * to be straightened out. |
| 10486 | */ |
| 10487 | // writer |
| 10488 | synchronized (mPackages) { |
| 10489 | retCode = PackageManager.INSTALL_SUCCEEDED; |
| 10490 | pkgList.add(pkg.packageName); |
| 10491 | // Post process args |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10492 | args.doPostInstall(PackageManager.INSTALL_SUCCEEDED, |
| 10493 | pkg.applicationInfo.uid); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10494 | } |
| 10495 | } else { |
| 10496 | Slog.i(TAG, "Failed to install pkg from " + codePath + " from sdcard"); |
| 10497 | } |
| 10498 | } |
| 10499 | |
| 10500 | } finally { |
| 10501 | if (retCode != PackageManager.INSTALL_SUCCEEDED) { |
| 10502 | // Don't destroy container here. Wait till gc clears things |
| 10503 | // up. |
| 10504 | removeCids.add(args.cid); |
| 10505 | } |
| 10506 | } |
| 10507 | } |
| 10508 | // writer |
| 10509 | synchronized (mPackages) { |
| 10510 | // If the platform SDK has changed since the last time we booted, |
| 10511 | // we need to re-grant app permission to catch any new ones that |
| 10512 | // appear. This is really a hack, and means that apps can in some |
| 10513 | // cases get permissions that the user didn't initially explicitly |
| 10514 | // allow... it would be nice to have some better way to handle |
| 10515 | // this situation. |
| 10516 | final boolean regrantPermissions = mSettings.mExternalSdkPlatform != mSdkVersion; |
| 10517 | if (regrantPermissions) |
| 10518 | Slog.i(TAG, "Platform changed from " + mSettings.mExternalSdkPlatform + " to " |
| 10519 | + mSdkVersion + "; regranting permissions for external storage"); |
| 10520 | mSettings.mExternalSdkPlatform = mSdkVersion; |
| 10521 | |
| 10522 | // Make sure group IDs have been assigned, and any permission |
| 10523 | // changes in other apps are accounted for |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 10524 | updatePermissionsLPw(null, null, UPDATE_PERMISSIONS_ALL |
| 10525 | | (regrantPermissions |
| 10526 | ? (UPDATE_PERMISSIONS_REPLACE_PKG|UPDATE_PERMISSIONS_REPLACE_ALL) |
| 10527 | : 0)); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10528 | // can downgrade to reader |
| 10529 | // Persist settings |
| 10530 | mSettings.writeLPr(); |
| 10531 | } |
| 10532 | // Send a broadcast to let everyone know we are done processing |
| 10533 | if (pkgList.size() > 0) { |
| 10534 | sendResourcesChangedBroadcast(true, pkgList, uidArr, null); |
| 10535 | } |
| 10536 | // Force gc to avoid any stale parser references that we might have. |
| 10537 | if (doGc) { |
| 10538 | Runtime.getRuntime().gc(); |
| 10539 | } |
| 10540 | // List stale containers and destroy stale temporary containers. |
| 10541 | if (removeCids != null) { |
| 10542 | for (String cid : removeCids) { |
| 10543 | if (cid.startsWith(mTempContainerPrefix)) { |
| 10544 | Log.i(TAG, "Destroying stale temporary container " + cid); |
| 10545 | PackageHelper.destroySdDir(cid); |
| 10546 | } else { |
| 10547 | Log.w(TAG, "Container " + cid + " is stale"); |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 10548 | } |
| 10549 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10550 | } |
| 10551 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10552 | |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 10553 | /* |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10554 | * Utility method to unload a list of specified containers |
| 10555 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10556 | private void unloadAllContainers(Set<AsecInstallArgs> cidArgs) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10557 | // Just unmount all valid containers. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10558 | for (AsecInstallArgs arg : cidArgs) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10559 | synchronized (mInstallLock) { |
| 10560 | arg.doPostDeleteLI(false); |
Suchi Amalapurapu | 9b10ef5 | 2010-03-03 09:45:24 -0800 | [diff] [blame] | 10561 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 10562 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10563 | } |
| 10564 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10565 | /* |
| 10566 | * Unload packages mounted on external media. This involves deleting package |
| 10567 | * data from internal structures, sending broadcasts about diabled packages, |
| 10568 | * gc'ing to free up references, unmounting all secure containers |
| 10569 | * corresponding to packages on external media, and posting a |
| 10570 | * UPDATED_MEDIA_STATUS message if status has been requested. Please note |
| 10571 | * that we always have to post this message if status has been requested no |
| 10572 | * matter what. |
| 10573 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10574 | private void unloadMediaPackages(HashMap<AsecInstallArgs, String> processCids, int uidArr[], |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10575 | final boolean reportStatus) { |
| 10576 | if (DEBUG_SD_INSTALL) |
| 10577 | Log.i(TAG, "unloading media packages"); |
| 10578 | ArrayList<String> pkgList = new ArrayList<String>(); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10579 | ArrayList<AsecInstallArgs> failedList = new ArrayList<AsecInstallArgs>(); |
| 10580 | final Set<AsecInstallArgs> keys = processCids.keySet(); |
| 10581 | for (AsecInstallArgs args : keys) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10582 | String pkgName = args.getPackageName(); |
| 10583 | if (DEBUG_SD_INSTALL) |
| 10584 | Log.i(TAG, "Trying to unload pkg : " + pkgName); |
| 10585 | // Delete package internally |
| 10586 | PackageRemovedInfo outInfo = new PackageRemovedInfo(); |
| 10587 | synchronized (mInstallLock) { |
Christopher Tate | acee6e9 | 2013-05-14 16:11:44 -0700 | [diff] [blame] | 10588 | boolean res = deletePackageLI(pkgName, null, false, null, null, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10589 | PackageManager.DELETE_KEEP_DATA, outInfo, false); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10590 | if (res) { |
| 10591 | pkgList.add(pkgName); |
| 10592 | } else { |
| 10593 | Slog.e(TAG, "Failed to delete pkg from sdcard : " + pkgName); |
| 10594 | failedList.add(args); |
| 10595 | } |
| 10596 | } |
| 10597 | } |
Suchi Amalapurapu | 3d24425 | 2010-04-08 14:37:05 -0700 | [diff] [blame] | 10598 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10599 | // reader |
| 10600 | synchronized (mPackages) { |
| 10601 | // We didn't update the settings after removing each package; |
| 10602 | // write them now for all packages. |
| 10603 | mSettings.writeLPr(); |
| 10604 | } |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 10605 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10606 | // We have to absolutely send UPDATED_MEDIA_STATUS only |
| 10607 | // after confirming that all the receivers processed the ordered |
| 10608 | // broadcast when packages get disabled, force a gc to clean things up. |
| 10609 | // and unload all the containers. |
| 10610 | if (pkgList.size() > 0) { |
| 10611 | sendResourcesChangedBroadcast(false, pkgList, uidArr, new IIntentReceiver.Stub() { |
| 10612 | public void performReceive(Intent intent, int resultCode, String data, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 10613 | Bundle extras, boolean ordered, boolean sticky, |
| 10614 | int sendingUser) throws RemoteException { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10615 | Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, |
| 10616 | reportStatus ? 1 : 0, 1, keys); |
| 10617 | mHandler.sendMessage(msg); |
| 10618 | } |
| 10619 | }); |
| 10620 | } else { |
| 10621 | Message msg = mHandler.obtainMessage(UPDATED_MEDIA_STATUS, reportStatus ? 1 : 0, -1, |
| 10622 | keys); |
| 10623 | mHandler.sendMessage(msg); |
| 10624 | } |
| 10625 | } |
Dianne Hackborn | 3aeee33 | 2010-10-02 18:56:33 -0700 | [diff] [blame] | 10626 | |
Amith Yamasani | 6ec1012 | 2012-08-23 13:49:25 -0700 | [diff] [blame] | 10627 | /** Binder call */ |
| 10628 | @Override |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10629 | public void movePackage(final String packageName, final IPackageMoveObserver observer, |
| 10630 | final int flags) { |
| 10631 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MOVE_PACKAGE, null); |
Amith Yamasani | 6ec1012 | 2012-08-23 13:49:25 -0700 | [diff] [blame] | 10632 | UserHandle user = new UserHandle(UserHandle.getCallingUserId()); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10633 | int returnCode = PackageManager.MOVE_SUCCEEDED; |
| 10634 | int currFlags = 0; |
| 10635 | int newFlags = 0; |
| 10636 | // reader |
| 10637 | synchronized (mPackages) { |
| 10638 | PackageParser.Package pkg = mPackages.get(packageName); |
| 10639 | if (pkg == null) { |
| 10640 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
| 10641 | } else { |
| 10642 | // Disable moving fwd locked apps and system packages |
| 10643 | if (pkg.applicationInfo != null && isSystemApp(pkg)) { |
| 10644 | Slog.w(TAG, "Cannot move system application"); |
| 10645 | returnCode = PackageManager.MOVE_FAILED_SYSTEM_PACKAGE; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10646 | } else if (pkg.mOperationPending) { |
| 10647 | Slog.w(TAG, "Attempt to move package which has pending operations"); |
| 10648 | returnCode = PackageManager.MOVE_FAILED_OPERATION_PENDING; |
| 10649 | } else { |
| 10650 | // Find install location first |
| 10651 | if ((flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 |
| 10652 | && (flags & PackageManager.MOVE_INTERNAL) != 0) { |
| 10653 | Slog.w(TAG, "Ambigous flags specified for move location."); |
| 10654 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
| 10655 | } else { |
| 10656 | newFlags = (flags & PackageManager.MOVE_EXTERNAL_MEDIA) != 0 ? PackageManager.INSTALL_EXTERNAL |
| 10657 | : PackageManager.INSTALL_INTERNAL; |
| 10658 | currFlags = isExternal(pkg) ? PackageManager.INSTALL_EXTERNAL |
| 10659 | : PackageManager.INSTALL_INTERNAL; |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 10660 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10661 | if (newFlags == currFlags) { |
| 10662 | Slog.w(TAG, "No move required. Trying to move to same location"); |
| 10663 | returnCode = PackageManager.MOVE_FAILED_INVALID_LOCATION; |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 10664 | } else { |
| 10665 | if (isForwardLocked(pkg)) { |
| 10666 | currFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 10667 | newFlags |= PackageManager.INSTALL_FORWARD_LOCK; |
| 10668 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10669 | } |
| 10670 | } |
| 10671 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 10672 | pkg.mOperationPending = true; |
| 10673 | } |
| 10674 | } |
| 10675 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 10676 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10677 | /* |
| 10678 | * TODO this next block probably shouldn't be inside the lock. We |
| 10679 | * can't guarantee these won't change after this is fired off |
| 10680 | * anyway. |
| 10681 | */ |
| 10682 | if (returnCode != PackageManager.MOVE_SUCCEEDED) { |
Amith Yamasani | 6ec1012 | 2012-08-23 13:49:25 -0700 | [diff] [blame] | 10683 | processPendingMove(new MoveParams(null, observer, 0, packageName, |
| 10684 | null, -1, user), |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10685 | returnCode); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10686 | } else { |
| 10687 | Message msg = mHandler.obtainMessage(INIT_COPY); |
| 10688 | InstallArgs srcArgs = createInstallArgs(currFlags, pkg.applicationInfo.sourceDir, |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 10689 | pkg.applicationInfo.publicSourceDir, pkg.applicationInfo.nativeLibraryDir); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10690 | MoveParams mp = new MoveParams(srcArgs, observer, newFlags, packageName, |
Amith Yamasani | 6ec1012 | 2012-08-23 13:49:25 -0700 | [diff] [blame] | 10691 | pkg.applicationInfo.dataDir, pkg.applicationInfo.uid, user); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10692 | msg.obj = mp; |
| 10693 | mHandler.sendMessage(msg); |
| 10694 | } |
| 10695 | } |
| 10696 | } |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 10697 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10698 | private void processPendingMove(final MoveParams mp, final int currentStatus) { |
| 10699 | // Queue up an async operation since the package deletion may take a |
| 10700 | // little while. |
| 10701 | mHandler.post(new Runnable() { |
| 10702 | public void run() { |
| 10703 | // TODO fix this; this does nothing. |
| 10704 | mHandler.removeCallbacks(this); |
| 10705 | int returnCode = currentStatus; |
| 10706 | if (currentStatus == PackageManager.MOVE_SUCCEEDED) { |
| 10707 | int uidArr[] = null; |
| 10708 | ArrayList<String> pkgList = null; |
| 10709 | synchronized (mPackages) { |
| 10710 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 10711 | if (pkg == null) { |
| 10712 | Slog.w(TAG, " Package " + mp.packageName |
| 10713 | + " doesn't exist. Aborting move"); |
| 10714 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
| 10715 | } else if (!mp.srcArgs.getCodePath().equals(pkg.applicationInfo.sourceDir)) { |
| 10716 | Slog.w(TAG, "Package " + mp.packageName + " code path changed from " |
| 10717 | + mp.srcArgs.getCodePath() + " to " |
| 10718 | + pkg.applicationInfo.sourceDir |
| 10719 | + " Aborting move and returning error"); |
| 10720 | returnCode = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 10721 | } else { |
| 10722 | uidArr = new int[] { |
| 10723 | pkg.applicationInfo.uid |
| 10724 | }; |
| 10725 | pkgList = new ArrayList<String>(); |
| 10726 | pkgList.add(mp.packageName); |
| 10727 | } |
| 10728 | } |
| 10729 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 10730 | // Send resources unavailable broadcast |
| 10731 | sendResourcesChangedBroadcast(false, pkgList, uidArr, null); |
| 10732 | // Update package code and resource paths |
| 10733 | synchronized (mInstallLock) { |
| 10734 | synchronized (mPackages) { |
| 10735 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 10736 | // Recheck for package again. |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 10737 | if (pkg == null) { |
| 10738 | Slog.w(TAG, " Package " + mp.packageName |
| 10739 | + " doesn't exist. Aborting move"); |
| 10740 | returnCode = PackageManager.MOVE_FAILED_DOESNT_EXIST; |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10741 | } else if (!mp.srcArgs.getCodePath().equals( |
| 10742 | pkg.applicationInfo.sourceDir)) { |
| 10743 | Slog.w(TAG, "Package " + mp.packageName |
| 10744 | + " code path changed from " + mp.srcArgs.getCodePath() |
| 10745 | + " to " + pkg.applicationInfo.sourceDir |
| 10746 | + " Aborting move and returning error"); |
| 10747 | returnCode = PackageManager.MOVE_FAILED_INTERNAL_ERROR; |
| 10748 | } else { |
| 10749 | final String oldCodePath = pkg.mPath; |
| 10750 | final String newCodePath = mp.targetArgs.getCodePath(); |
| 10751 | final String newResPath = mp.targetArgs.getResourcePath(); |
| 10752 | final String newNativePath = mp.targetArgs |
| 10753 | .getNativeLibraryPath(); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 10754 | |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 10755 | final File newNativeDir = new File(newNativePath); |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 10756 | |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 10757 | if (!isForwardLocked(pkg) && !isExternal(pkg)) { |
Kenny Root | a3e9079 | 2012-10-18 10:58:36 -0700 | [diff] [blame] | 10758 | NativeLibraryHelper.copyNativeBinariesIfNeededLI( |
| 10759 | new File(newCodePath), newNativeDir); |
| 10760 | } |
| 10761 | final int[] users = sUserManager.getUserIds(); |
| 10762 | for (int user : users) { |
| 10763 | if (mInstaller.linkNativeLibraryDirectory(pkg.packageName, |
| 10764 | newNativePath, user) < 0) { |
| 10765 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
Kenny Root | ddbe50d | 2012-09-06 13:18:37 -0700 | [diff] [blame] | 10766 | } |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 10767 | } |
| 10768 | |
| 10769 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
| 10770 | pkg.mPath = newCodePath; |
| 10771 | // Move dex files around |
| 10772 | if (moveDexFilesLI(pkg) != PackageManager.INSTALL_SUCCEEDED) { |
| 10773 | // Moving of dex files failed. Set |
| 10774 | // error code and abort move. |
| 10775 | pkg.mPath = pkg.mScanPath; |
| 10776 | returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE; |
| 10777 | } |
| 10778 | } |
| 10779 | |
| 10780 | if (returnCode == PackageManager.MOVE_SUCCEEDED) { |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10781 | pkg.mScanPath = newCodePath; |
| 10782 | pkg.applicationInfo.sourceDir = newCodePath; |
| 10783 | pkg.applicationInfo.publicSourceDir = newResPath; |
| 10784 | pkg.applicationInfo.nativeLibraryDir = newNativePath; |
| 10785 | PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 10786 | ps.codePath = new File(pkg.applicationInfo.sourceDir); |
| 10787 | ps.codePathString = ps.codePath.getPath(); |
| 10788 | ps.resourcePath = new File( |
| 10789 | pkg.applicationInfo.publicSourceDir); |
| 10790 | ps.resourcePathString = ps.resourcePath.getPath(); |
| 10791 | ps.nativeLibraryPathString = newNativePath; |
| 10792 | // Set the application info flag |
| 10793 | // correctly. |
| 10794 | if ((mp.flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
| 10795 | pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| 10796 | } else { |
| 10797 | pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_EXTERNAL_STORAGE; |
| 10798 | } |
| 10799 | ps.setFlags(pkg.applicationInfo.flags); |
| 10800 | mAppDirs.remove(oldCodePath); |
| 10801 | mAppDirs.put(newCodePath, pkg); |
| 10802 | // Persist settings |
| 10803 | mSettings.writeLPr(); |
| 10804 | } |
| 10805 | } |
| 10806 | } |
| 10807 | } |
| 10808 | // Send resources available broadcast |
| 10809 | sendResourcesChangedBroadcast(true, pkgList, uidArr, null); |
| 10810 | } |
| 10811 | } |
| 10812 | if (returnCode != PackageManager.MOVE_SUCCEEDED) { |
| 10813 | // Clean up failed installation |
| 10814 | if (mp.targetArgs != null) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 10815 | mp.targetArgs.doPostInstall(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, |
| 10816 | -1); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10817 | } |
| 10818 | } else { |
| 10819 | // Force a gc to clear things up. |
| 10820 | Runtime.getRuntime().gc(); |
| 10821 | // Delete older code |
| 10822 | synchronized (mInstallLock) { |
| 10823 | mp.srcArgs.doPostDeleteLI(true); |
| 10824 | } |
| 10825 | } |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 10826 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10827 | // Allow more operations on this file if we didn't fail because |
| 10828 | // an operation was already pending for this package. |
| 10829 | if (returnCode != PackageManager.MOVE_FAILED_OPERATION_PENDING) { |
| 10830 | synchronized (mPackages) { |
| 10831 | PackageParser.Package pkg = mPackages.get(mp.packageName); |
| 10832 | if (pkg != null) { |
| 10833 | pkg.mOperationPending = false; |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 10834 | } |
| 10835 | } |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10836 | } |
Kenny Root | deb1126 | 2010-08-02 11:36:21 -0700 | [diff] [blame] | 10837 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10838 | IPackageMoveObserver observer = mp.observer; |
| 10839 | if (observer != null) { |
| 10840 | try { |
| 10841 | observer.packageMoved(mp.packageName, returnCode); |
| 10842 | } catch (RemoteException e) { |
| 10843 | Log.i(TAG, "Observer no longer exists."); |
| 10844 | } |
| 10845 | } |
| 10846 | } |
| 10847 | }); |
| 10848 | } |
| 10849 | |
| 10850 | public boolean setInstallLocation(int loc) { |
| 10851 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS, |
| 10852 | null); |
| 10853 | if (getInstallLocation() == loc) { |
| 10854 | return true; |
| 10855 | } |
| 10856 | if (loc == PackageHelper.APP_INSTALL_AUTO || loc == PackageHelper.APP_INSTALL_INTERNAL |
| 10857 | || loc == PackageHelper.APP_INSTALL_EXTERNAL) { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 10858 | android.provider.Settings.Global.putInt(mContext.getContentResolver(), |
| 10859 | android.provider.Settings.Global.DEFAULT_INSTALL_LOCATION, loc); |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10860 | return true; |
| 10861 | } |
| 10862 | return false; |
Suchi Amalapurapu | 8946dd3 | 2010-02-19 09:19:34 -0800 | [diff] [blame] | 10863 | } |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 10864 | |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10865 | public int getInstallLocation() { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 10866 | return android.provider.Settings.Global.getInt(mContext.getContentResolver(), |
| 10867 | android.provider.Settings.Global.DEFAULT_INSTALL_LOCATION, |
Kenny Root | 447106f | 2011-03-23 11:00:15 -0700 | [diff] [blame] | 10868 | PackageHelper.APP_INSTALL_AUTO); |
| 10869 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 10870 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 10871 | /** Called by UserManagerService */ |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 10872 | void cleanUpUserLILPw(int userHandle) { |
Christopher Tate | 1e08f5c | 2013-05-02 13:27:27 -0700 | [diff] [blame] | 10873 | mDirtyUsers.remove(userHandle); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 10874 | mSettings.removeUserLPr(userHandle); |
Christopher Tate | 1e08f5c | 2013-05-02 13:27:27 -0700 | [diff] [blame] | 10875 | mPendingBroadcasts.remove(userHandle); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 10876 | if (mInstaller != null) { |
| 10877 | // Technically, we shouldn't be doing this with the package lock |
| 10878 | // held. However, this is very rare, and there is already so much |
| 10879 | // other disk I/O going on, that we'll let it slide for now. |
| 10880 | mInstaller.removeUserDataDirs(userHandle); |
| 10881 | } |
| 10882 | } |
| 10883 | |
| 10884 | /** Called by UserManagerService */ |
| 10885 | void createNewUserLILPw(int userHandle, File path) { |
| 10886 | if (mInstaller != null) { |
Dianne Hackborn | 8a2ed1d | 2013-01-29 15:18:29 -0800 | [diff] [blame] | 10887 | mSettings.createNewUserLILPw(this, mInstaller, userHandle, path); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 10888 | } |
| 10889 | } |
| 10890 | |
Kenny Root | 0aaa0d9 | 2011-09-12 16:42:55 -0700 | [diff] [blame] | 10891 | @Override |
| 10892 | public VerifierDeviceIdentity getVerifierDeviceIdentity() throws RemoteException { |
| 10893 | mContext.enforceCallingOrSelfPermission( |
| 10894 | android.Manifest.permission.PACKAGE_VERIFICATION_AGENT, |
| 10895 | "Only package verification agents can read the verifier device identity"); |
| 10896 | |
| 10897 | synchronized (mPackages) { |
| 10898 | return mSettings.getVerifierDeviceIdentityLPw(); |
| 10899 | } |
| 10900 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10901 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 10902 | @Override |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 10903 | public void setPermissionEnforced(String permission, boolean enforced) { |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 10904 | mContext.enforceCallingOrSelfPermission(GRANT_REVOKE_PERMISSIONS, null); |
| 10905 | if (READ_EXTERNAL_STORAGE.equals(permission)) { |
| 10906 | synchronized (mPackages) { |
Jeff Sharkey | f538577 | 2012-05-11 14:04:41 -0700 | [diff] [blame] | 10907 | if (mSettings.mReadExternalStorageEnforced == null |
| 10908 | || mSettings.mReadExternalStorageEnforced != enforced) { |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 10909 | mSettings.mReadExternalStorageEnforced = enforced; |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 10910 | mSettings.writeLPr(); |
Dianne Hackborn | f1b7824 | 2013-04-08 22:28:59 -0700 | [diff] [blame] | 10911 | } |
| 10912 | } |
| 10913 | // kill any non-foreground processes so we restart them and |
| 10914 | // grant/revoke the GID. |
| 10915 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 10916 | if (am != null) { |
| 10917 | final long token = Binder.clearCallingIdentity(); |
| 10918 | try { |
| 10919 | am.killProcessesBelowForeground("setPermissionEnforcement"); |
| 10920 | } catch (RemoteException e) { |
| 10921 | } finally { |
| 10922 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 10923 | } |
| 10924 | } |
| 10925 | } else { |
| 10926 | throw new IllegalArgumentException("No selective enforcement for " + permission); |
| 10927 | } |
| 10928 | } |
| 10929 | |
| 10930 | @Override |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 10931 | public boolean isPermissionEnforced(String permission) { |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 10932 | final boolean enforcedDefault = isPermissionEnforcedDefault(permission); |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 10933 | synchronized (mPackages) { |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 10934 | return isPermissionEnforcedLocked(permission, enforcedDefault); |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 10935 | } |
| 10936 | } |
| 10937 | |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 10938 | /** |
| 10939 | * Check if given permission should be enforced by default. Should always be |
| 10940 | * called outside of {@link #mPackages} lock. |
| 10941 | */ |
| 10942 | private boolean isPermissionEnforcedDefault(String permission) { |
| 10943 | if (READ_EXTERNAL_STORAGE.equals(permission)) { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 10944 | return android.provider.Settings.Global.getInt(mContext.getContentResolver(), |
| 10945 | android.provider.Settings.Global.READ_EXTERNAL_STORAGE_ENFORCED_DEFAULT, 0) |
| 10946 | != 0; |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 10947 | } else { |
| 10948 | return true; |
| 10949 | } |
| 10950 | } |
| 10951 | |
| 10952 | /** |
| 10953 | * Check if user has requested that given permission be enforced, using |
| 10954 | * given default if undefined. |
| 10955 | */ |
| 10956 | private boolean isPermissionEnforcedLocked(String permission, boolean enforcedDefault) { |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 10957 | if (READ_EXTERNAL_STORAGE.equals(permission)) { |
Jeff Sharkey | f538577 | 2012-05-11 14:04:41 -0700 | [diff] [blame] | 10958 | if (mSettings.mReadExternalStorageEnforced != null) { |
| 10959 | return mSettings.mReadExternalStorageEnforced; |
| 10960 | } else { |
Jeff Sharkey | c50f31d | 2012-09-22 17:14:02 -0700 | [diff] [blame] | 10961 | // User hasn't defined; fall back to secure default |
| 10962 | return enforcedDefault; |
Jeff Sharkey | f538577 | 2012-05-11 14:04:41 -0700 | [diff] [blame] | 10963 | } |
Jeff Sharkey | 5d32e77 | 2012-04-12 15:59:23 -0700 | [diff] [blame] | 10964 | } else { |
| 10965 | return true; |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 10966 | } |
Jeff Sharkey | edc84ee8 | 2012-03-19 16:52:26 -0700 | [diff] [blame] | 10967 | } |
Jeff Sharkey | f6f7f1d | 2012-06-12 17:12:08 -0700 | [diff] [blame] | 10968 | |
| 10969 | public boolean isStorageLow() { |
| 10970 | final long token = Binder.clearCallingIdentity(); |
| 10971 | try { |
| 10972 | final DeviceStorageMonitorService dsm = (DeviceStorageMonitorService) ServiceManager |
| 10973 | .getService(DeviceStorageMonitorService.SERVICE); |
| 10974 | return dsm.isMemoryLow(); |
| 10975 | } finally { |
| 10976 | Binder.restoreCallingIdentity(token); |
| 10977 | } |
| 10978 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10979 | } |