Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.content.pm; |
| 18 | |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Svet Ganov | 7121e18 | 2015-07-13 22:38:12 -0700 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 22 | import android.annotation.SdkConstant; |
| 23 | import android.annotation.SdkConstant.SdkConstantType; |
Svet Ganov | 7121e18 | 2015-07-13 22:38:12 -0700 | [diff] [blame] | 24 | import android.annotation.SystemApi; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 25 | import android.app.ActivityManager; |
| 26 | import android.content.Context; |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 28 | import android.content.IntentSender; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.net.Uri; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 31 | import android.os.FileBridge; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.Looper; |
| 34 | import android.os.Message; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 35 | import android.os.Parcel; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 36 | import android.os.ParcelFileDescriptor; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 37 | import android.os.Parcelable; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 38 | import android.os.RemoteException; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 39 | import android.util.ExceptionUtils; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 40 | import android.util.Log; |
| 41 | |
| 42 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 43 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 44 | import java.io.Closeable; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 45 | import java.io.IOException; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 46 | import java.io.InputStream; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 47 | import java.io.OutputStream; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 48 | import java.security.MessageDigest; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 49 | import java.util.ArrayList; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 50 | import java.util.Collections; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 51 | import java.util.Iterator; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 52 | import java.util.List; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 53 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Offers the ability to install, upgrade, and remove applications on the |
| 56 | * device. This includes support for apps packaged either as a single |
| 57 | * "monolithic" APK, or apps packaged as multiple "split" APKs. |
| 58 | * <p> |
| 59 | * An app is delivered for installation through a |
| 60 | * {@link PackageInstaller.Session}, which any app can create. Once the session |
| 61 | * is created, the installer can stream one or more APKs into place until it |
| 62 | * decides to either commit or destroy the session. Committing may require user |
| 63 | * intervention to complete the installation. |
| 64 | * <p> |
| 65 | * Sessions can install brand new apps, upgrade existing apps, or add new splits |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 66 | * into an existing app. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 67 | * <p> |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 68 | * Apps packaged as multiple split APKs always consist of a single "base" APK |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 69 | * (with a {@code null} split name) and zero or more "split" APKs (with unique |
| 70 | * split names). Any subset of these APKs can be installed together, as long as |
| 71 | * the following constraints are met: |
| 72 | * <ul> |
| 73 | * <li>All APKs must have the exact same package name, version code, and signing |
| 74 | * certificates. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 75 | * <li>All APKs must have unique split names. |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 76 | * <li>All installations must contain a single base APK. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 77 | * </ul> |
| 78 | */ |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 79 | public class PackageInstaller { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 80 | private static final String TAG = "PackageInstaller"; |
| 81 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Activity Action: Show details about a particular install session. This |
| 84 | * may surface actions such as pause, resume, or cancel. |
| 85 | * <p> |
| 86 | * This should always be scoped to the installer package that owns the |
Jeff Sharkey | de74231 | 2014-09-15 14:04:56 -0700 | [diff] [blame] | 87 | * session. Clients should use {@link SessionInfo#createDetailsIntent()} to |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 88 | * build this intent correctly. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 89 | * <p> |
| 90 | * In some cases, a matching Activity may not exist, so ensure you safeguard |
| 91 | * against this. |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 92 | * <p> |
| 93 | * The session to show details for is defined in {@link #EXTRA_SESSION_ID}. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 94 | */ |
| 95 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 96 | public static final String ACTION_SESSION_DETAILS = "android.content.pm.action.SESSION_DETAILS"; |
| 97 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 98 | /** {@hide} */ |
| 99 | public static final String |
| 100 | ACTION_CONFIRM_PERMISSIONS = "android.content.pm.action.CONFIRM_PERMISSIONS"; |
| 101 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 102 | /** |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 103 | * An integer session ID that an operation is working with. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 104 | * |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 105 | * @see Intent#getIntExtra(String, int) |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 106 | */ |
| 107 | public static final String EXTRA_SESSION_ID = "android.content.pm.extra.SESSION_ID"; |
| 108 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 109 | /** |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 110 | * Package name that an operation is working with. |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 111 | * |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 112 | * @see Intent#getStringExtra(String) |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 113 | */ |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 114 | public static final String EXTRA_PACKAGE_NAME = "android.content.pm.extra.PACKAGE_NAME"; |
| 115 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Current status of an operation. Will be one of |
| 118 | * {@link #STATUS_PENDING_USER_ACTION}, {@link #STATUS_SUCCESS}, |
| 119 | * {@link #STATUS_FAILURE}, {@link #STATUS_FAILURE_ABORTED}, |
| 120 | * {@link #STATUS_FAILURE_BLOCKED}, {@link #STATUS_FAILURE_CONFLICT}, |
| 121 | * {@link #STATUS_FAILURE_INCOMPATIBLE}, {@link #STATUS_FAILURE_INVALID}, or |
| 122 | * {@link #STATUS_FAILURE_STORAGE}. |
| 123 | * <p> |
| 124 | * More information about a status may be available through additional |
| 125 | * extras; see the individual status documentation for details. |
| 126 | * |
| 127 | * @see Intent#getIntExtra(String, int) |
| 128 | */ |
| 129 | public static final String EXTRA_STATUS = "android.content.pm.extra.STATUS"; |
| 130 | |
| 131 | /** |
| 132 | * Detailed string representation of the status, including raw details that |
| 133 | * are useful for debugging. |
| 134 | * |
| 135 | * @see Intent#getStringExtra(String) |
| 136 | */ |
| 137 | public static final String EXTRA_STATUS_MESSAGE = "android.content.pm.extra.STATUS_MESSAGE"; |
| 138 | |
| 139 | /** |
| 140 | * Another package name relevant to a status. This is typically the package |
| 141 | * responsible for causing an operation failure. |
| 142 | * |
| 143 | * @see Intent#getStringExtra(String) |
| 144 | */ |
| 145 | public static final String |
| 146 | EXTRA_OTHER_PACKAGE_NAME = "android.content.pm.extra.OTHER_PACKAGE_NAME"; |
| 147 | |
| 148 | /** |
| 149 | * Storage path relevant to a status. |
| 150 | * |
| 151 | * @see Intent#getStringExtra(String) |
| 152 | */ |
| 153 | public static final String EXTRA_STORAGE_PATH = "android.content.pm.extra.STORAGE_PATH"; |
| 154 | |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 155 | /** {@hide} */ |
| 156 | @Deprecated |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 157 | public static final String EXTRA_PACKAGE_NAMES = "android.content.pm.extra.PACKAGE_NAMES"; |
| 158 | |
| 159 | /** {@hide} */ |
| 160 | public static final String EXTRA_LEGACY_STATUS = "android.content.pm.extra.LEGACY_STATUS"; |
| 161 | /** {@hide} */ |
| 162 | public static final String EXTRA_LEGACY_BUNDLE = "android.content.pm.extra.LEGACY_BUNDLE"; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 163 | /** {@hide} */ |
| 164 | public static final String EXTRA_CALLBACK = "android.content.pm.extra.CALLBACK"; |
| 165 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 166 | /** |
| 167 | * User action is currently required to proceed. You can launch the intent |
| 168 | * activity described by {@link Intent#EXTRA_INTENT} to involve the user and |
| 169 | * continue. |
| 170 | * <p> |
| 171 | * You may choose to immediately launch the intent if the user is actively |
| 172 | * using your app. Otherwise, you should use a notification to guide the |
| 173 | * user back into your app before launching. |
| 174 | * |
| 175 | * @see Intent#getParcelableExtra(String) |
| 176 | */ |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 177 | public static final int STATUS_PENDING_USER_ACTION = -1; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * The operation succeeded. |
| 181 | */ |
| 182 | public static final int STATUS_SUCCESS = 0; |
| 183 | |
| 184 | /** |
| 185 | * The operation failed in a generic way. The system will always try to |
| 186 | * provide a more specific failure reason, but in some rare cases this may |
| 187 | * be delivered. |
| 188 | * |
| 189 | * @see #EXTRA_STATUS_MESSAGE |
| 190 | */ |
| 191 | public static final int STATUS_FAILURE = 1; |
| 192 | |
| 193 | /** |
| 194 | * The operation failed because it was blocked. For example, a device policy |
| 195 | * may be blocking the operation, a package verifier may have blocked the |
| 196 | * operation, or the app may be required for core system operation. |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 197 | * <p> |
| 198 | * The result may also contain {@link #EXTRA_OTHER_PACKAGE_NAME} with the |
| 199 | * specific package blocking the install. |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 200 | * |
| 201 | * @see #EXTRA_STATUS_MESSAGE |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 202 | * @see #EXTRA_OTHER_PACKAGE_NAME |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 203 | */ |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 204 | public static final int STATUS_FAILURE_BLOCKED = 2; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * The operation failed because it was actively aborted. For example, the |
| 208 | * user actively declined requested permissions, or the session was |
| 209 | * abandoned. |
| 210 | * |
| 211 | * @see #EXTRA_STATUS_MESSAGE |
| 212 | */ |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 213 | public static final int STATUS_FAILURE_ABORTED = 3; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * The operation failed because one or more of the APKs was invalid. For |
| 217 | * example, they might be malformed, corrupt, incorrectly signed, |
| 218 | * mismatched, etc. |
| 219 | * |
| 220 | * @see #EXTRA_STATUS_MESSAGE |
| 221 | */ |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 222 | public static final int STATUS_FAILURE_INVALID = 4; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * The operation failed because it conflicts (or is inconsistent with) with |
| 226 | * another package already installed on the device. For example, an existing |
| 227 | * permission, incompatible certificates, etc. The user may be able to |
| 228 | * uninstall another app to fix the issue. |
| 229 | * <p> |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 230 | * The result may also contain {@link #EXTRA_OTHER_PACKAGE_NAME} with the |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 231 | * specific package identified as the cause of the conflict. |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 232 | * |
| 233 | * @see #EXTRA_STATUS_MESSAGE |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 234 | * @see #EXTRA_OTHER_PACKAGE_NAME |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 235 | */ |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 236 | public static final int STATUS_FAILURE_CONFLICT = 5; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * The operation failed because of storage issues. For example, the device |
| 240 | * may be running low on space, or external media may be unavailable. The |
| 241 | * user may be able to help free space or insert different external media. |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 242 | * <p> |
| 243 | * The result may also contain {@link #EXTRA_STORAGE_PATH} with the path to |
| 244 | * the storage device that caused the failure. |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 245 | * |
| 246 | * @see #EXTRA_STATUS_MESSAGE |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 247 | * @see #EXTRA_STORAGE_PATH |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 248 | */ |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 249 | public static final int STATUS_FAILURE_STORAGE = 6; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 250 | |
| 251 | /** |
| 252 | * The operation failed because it is fundamentally incompatible with this |
| 253 | * device. For example, the app may require a hardware feature that doesn't |
| 254 | * exist, it may be missing native code for the ABIs supported by the |
| 255 | * device, or it requires a newer SDK version, etc. |
| 256 | * |
| 257 | * @see #EXTRA_STATUS_MESSAGE |
| 258 | */ |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 259 | public static final int STATUS_FAILURE_INCOMPATIBLE = 7; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 260 | |
| 261 | private final Context mContext; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 262 | private final PackageManager mPm; |
| 263 | private final IPackageInstaller mInstaller; |
| 264 | private final int mUserId; |
| 265 | private final String mInstallerPackageName; |
| 266 | |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 267 | private final ArrayList<SessionCallbackDelegate> mDelegates = new ArrayList<>(); |
| 268 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 269 | /** {@hide} */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 270 | public PackageInstaller(Context context, PackageManager pm, IPackageInstaller installer, |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 271 | String installerPackageName, int userId) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 272 | mContext = context; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 273 | mPm = pm; |
| 274 | mInstaller = installer; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 275 | mInstallerPackageName = installerPackageName; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 276 | mUserId = userId; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 279 | /** |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 280 | * Create a new session using the given parameters, returning a unique ID |
| 281 | * that represents the session. Once created, the session can be opened |
| 282 | * multiple times across multiple device boots. |
| 283 | * <p> |
| 284 | * The system may automatically destroy sessions that have not been |
| 285 | * finalized (either committed or abandoned) within a reasonable period of |
| 286 | * time, typically on the order of a day. |
| 287 | * |
| 288 | * @throws IOException if parameters were unsatisfiable, such as lack of |
| 289 | * disk space or unavailable media. |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 290 | * @throws SecurityException when installation services are unavailable, |
| 291 | * such as when called from a restricted user. |
| 292 | * @throws IllegalArgumentException when {@link SessionParams} is invalid. |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 293 | * @return positive, non-zero unique ID that represents the created session. |
| 294 | * This ID remains consistent across device reboots until the |
| 295 | * session is finalized. IDs are not reused during a given boot. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 296 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 297 | public int createSession(@NonNull SessionParams params) throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 298 | try { |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 299 | return mInstaller.createSession(params, mInstallerPackageName, mUserId); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 300 | } catch (RuntimeException e) { |
| 301 | ExceptionUtils.maybeUnwrapIOException(e); |
| 302 | throw e; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 303 | } catch (RemoteException e) { |
| 304 | throw e.rethrowAsRuntimeException(); |
| 305 | } |
| 306 | } |
| 307 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 308 | /** |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 309 | * Open an existing session to actively perform work. To succeed, the caller |
| 310 | * must be the owner of the install session. |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 311 | * |
| 312 | * @throws IOException if parameters were unsatisfiable, such as lack of |
| 313 | * disk space or unavailable media. |
| 314 | * @throws SecurityException when the caller does not own the session, or |
| 315 | * the session is invalid. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 316 | */ |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 317 | public @NonNull Session openSession(int sessionId) throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 318 | try { |
| 319 | return new Session(mInstaller.openSession(sessionId)); |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 320 | } catch (RuntimeException e) { |
| 321 | ExceptionUtils.maybeUnwrapIOException(e); |
| 322 | throw e; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 323 | } catch (RemoteException e) { |
| 324 | throw e.rethrowAsRuntimeException(); |
| 325 | } |
| 326 | } |
| 327 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 328 | /** |
| 329 | * Update the icon representing the app being installed in a specific |
| 330 | * session. This should be roughly |
| 331 | * {@link ActivityManager#getLauncherLargeIconSize()} in both dimensions. |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 332 | * |
| 333 | * @throws SecurityException when the caller does not own the session, or |
| 334 | * the session is invalid. |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 335 | */ |
| 336 | public void updateSessionAppIcon(int sessionId, @Nullable Bitmap appIcon) { |
| 337 | try { |
| 338 | mInstaller.updateSessionAppIcon(sessionId, appIcon); |
| 339 | } catch (RemoteException e) { |
| 340 | throw e.rethrowAsRuntimeException(); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Update the label representing the app being installed in a specific |
| 346 | * session. |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 347 | * |
| 348 | * @throws SecurityException when the caller does not own the session, or |
| 349 | * the session is invalid. |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 350 | */ |
| 351 | public void updateSessionAppLabel(int sessionId, @Nullable CharSequence appLabel) { |
| 352 | try { |
| 353 | final String val = (appLabel != null) ? appLabel.toString() : null; |
| 354 | mInstaller.updateSessionAppLabel(sessionId, val); |
| 355 | } catch (RemoteException e) { |
| 356 | throw e.rethrowAsRuntimeException(); |
| 357 | } |
| 358 | } |
| 359 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 360 | /** |
| 361 | * Completely abandon the given session, destroying all staged data and |
| 362 | * rendering it invalid. Abandoned sessions will be reported to |
| 363 | * {@link SessionCallback} listeners as failures. This is equivalent to |
| 364 | * opening the session and calling {@link Session#abandon()}. |
| 365 | * |
| 366 | * @throws SecurityException when the caller does not own the session, or |
| 367 | * the session is invalid. |
| 368 | */ |
Jeff Sharkey | 381d94b | 2014-08-24 14:45:56 -0700 | [diff] [blame] | 369 | public void abandonSession(int sessionId) { |
| 370 | try { |
| 371 | mInstaller.abandonSession(sessionId); |
| 372 | } catch (RemoteException e) { |
| 373 | throw e.rethrowAsRuntimeException(); |
| 374 | } |
| 375 | } |
| 376 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 377 | /** |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 378 | * Return details for a specific session. No special permissions are |
| 379 | * required to retrieve these details. |
| 380 | * |
| 381 | * @return details for the requested session, or {@code null} if the session |
| 382 | * does not exist. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 383 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 384 | public @Nullable SessionInfo getSessionInfo(int sessionId) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 385 | try { |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 386 | return mInstaller.getSessionInfo(sessionId); |
| 387 | } catch (RemoteException e) { |
| 388 | throw e.rethrowAsRuntimeException(); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 393 | * Return list of all known install sessions, regardless of the installer. |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 394 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 395 | public @NonNull List<SessionInfo> getAllSessions() { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 396 | final ApplicationInfo info = mContext.getApplicationInfo(); |
| 397 | if ("com.google.android.googlequicksearchbox".equals(info.packageName) |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 398 | && info.versionCode <= 300400110) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 399 | Log.d(TAG, "Ignoring callback request from old prebuilt"); |
| 400 | return Collections.EMPTY_LIST; |
| 401 | } |
| 402 | |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 403 | try { |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 404 | return mInstaller.getAllSessions(mUserId).getList(); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 405 | } catch (RemoteException e) { |
| 406 | throw e.rethrowAsRuntimeException(); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /** |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 411 | * Return list of all known install sessions owned by the calling app. |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 412 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 413 | public @NonNull List<SessionInfo> getMySessions() { |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 414 | try { |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 415 | return mInstaller.getMySessions(mInstallerPackageName, mUserId).getList(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 416 | } catch (RemoteException e) { |
| 417 | throw e.rethrowAsRuntimeException(); |
| 418 | } |
| 419 | } |
| 420 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Uninstall the given package, removing it completely from the device. This |
| 423 | * method is only available to the current "installer of record" for the |
| 424 | * package. |
| 425 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 426 | public void uninstall(@NonNull String packageName, @NonNull IntentSender statusReceiver) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 427 | try { |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 428 | mInstaller.uninstall(packageName, mInstallerPackageName, 0, statusReceiver, mUserId); |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 429 | } catch (RemoteException e) { |
| 430 | throw e.rethrowAsRuntimeException(); |
| 431 | } |
| 432 | } |
| 433 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 434 | /** {@hide} */ |
| 435 | public void setPermissionsResult(int sessionId, boolean accepted) { |
| 436 | try { |
| 437 | mInstaller.setPermissionsResult(sessionId, accepted); |
| 438 | } catch (RemoteException e) { |
| 439 | throw e.rethrowAsRuntimeException(); |
| 440 | } |
| 441 | } |
| 442 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 443 | /** |
| 444 | * Events for observing session lifecycle. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 445 | * <p> |
| 446 | * A typical session lifecycle looks like this: |
| 447 | * <ul> |
| 448 | * <li>An installer creates a session to indicate pending app delivery. All |
| 449 | * install details are available at this point. |
| 450 | * <li>The installer opens the session to deliver APK data. Note that a |
| 451 | * session may be opened and closed multiple times as network connectivity |
| 452 | * changes. The installer may deliver periodic progress updates. |
| 453 | * <li>The installer commits or abandons the session, resulting in the |
| 454 | * session being finished. |
| 455 | * </ul> |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 456 | */ |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 457 | public static abstract class SessionCallback { |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 458 | /** |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 459 | * New session has been created. Details about the session can be |
| 460 | * obtained from {@link PackageInstaller#getSessionInfo(int)}. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 461 | */ |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 462 | public abstract void onCreated(int sessionId); |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 463 | |
| 464 | /** |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 465 | * Badging details for an existing session has changed. For example, the |
| 466 | * app icon or label has been updated. |
| 467 | */ |
| 468 | public abstract void onBadgingChanged(int sessionId); |
| 469 | |
| 470 | /** |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 471 | * Active state for session has been changed. |
| 472 | * <p> |
| 473 | * A session is considered active whenever there is ongoing forward |
| 474 | * progress being made, such as the installer holding an open |
| 475 | * {@link Session} instance while streaming data into place, or the |
| 476 | * system optimizing code as the result of |
| 477 | * {@link Session#commit(IntentSender)}. |
| 478 | * <p> |
| 479 | * If the installer closes the {@link Session} without committing, the |
| 480 | * session is considered inactive until the installer opens the session |
| 481 | * again. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 482 | */ |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 483 | public abstract void onActiveChanged(int sessionId, boolean active); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 484 | |
| 485 | /** |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 486 | * Progress for given session has been updated. |
| 487 | * <p> |
| 488 | * Note that this progress may not directly correspond to the value |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 489 | * reported by |
| 490 | * {@link PackageInstaller.Session#setStagingProgress(float)}, as the |
| 491 | * system may carve out a portion of the overall progress to represent |
| 492 | * its own internal installation work. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 493 | */ |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 494 | public abstract void onProgressChanged(int sessionId, float progress); |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 495 | |
| 496 | /** |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 497 | * Session has completely finished, either with success or failure. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 498 | */ |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 499 | public abstract void onFinished(int sessionId, boolean success); |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 502 | /** {@hide} */ |
| 503 | private static class SessionCallbackDelegate extends IPackageInstallerCallback.Stub implements |
| 504 | Handler.Callback { |
| 505 | private static final int MSG_SESSION_CREATED = 1; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 506 | private static final int MSG_SESSION_BADGING_CHANGED = 2; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 507 | private static final int MSG_SESSION_ACTIVE_CHANGED = 3; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 508 | private static final int MSG_SESSION_PROGRESS_CHANGED = 4; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 509 | private static final int MSG_SESSION_FINISHED = 5; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 510 | |
| 511 | final SessionCallback mCallback; |
| 512 | final Handler mHandler; |
| 513 | |
| 514 | public SessionCallbackDelegate(SessionCallback callback, Looper looper) { |
| 515 | mCallback = callback; |
| 516 | mHandler = new Handler(looper, this); |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public boolean handleMessage(Message msg) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 521 | final int sessionId = msg.arg1; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 522 | switch (msg.what) { |
| 523 | case MSG_SESSION_CREATED: |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 524 | mCallback.onCreated(sessionId); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 525 | return true; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 526 | case MSG_SESSION_BADGING_CHANGED: |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 527 | mCallback.onBadgingChanged(sessionId); |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 528 | return true; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 529 | case MSG_SESSION_ACTIVE_CHANGED: |
| 530 | final boolean active = msg.arg2 != 0; |
| 531 | mCallback.onActiveChanged(sessionId, active); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 532 | return true; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 533 | case MSG_SESSION_PROGRESS_CHANGED: |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 534 | mCallback.onProgressChanged(sessionId, (float) msg.obj); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 535 | return true; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 536 | case MSG_SESSION_FINISHED: |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 537 | mCallback.onFinished(sessionId, msg.arg2 != 0); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 538 | return true; |
| 539 | } |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void onSessionCreated(int sessionId) { |
| 545 | mHandler.obtainMessage(MSG_SESSION_CREATED, sessionId, 0).sendToTarget(); |
| 546 | } |
| 547 | |
| 548 | @Override |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 549 | public void onSessionBadgingChanged(int sessionId) { |
| 550 | mHandler.obtainMessage(MSG_SESSION_BADGING_CHANGED, sessionId, 0).sendToTarget(); |
| 551 | } |
| 552 | |
| 553 | @Override |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 554 | public void onSessionActiveChanged(int sessionId, boolean active) { |
| 555 | mHandler.obtainMessage(MSG_SESSION_ACTIVE_CHANGED, sessionId, active ? 1 : 0) |
| 556 | .sendToTarget(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 560 | public void onSessionProgressChanged(int sessionId, float progress) { |
| 561 | mHandler.obtainMessage(MSG_SESSION_PROGRESS_CHANGED, sessionId, 0, progress) |
| 562 | .sendToTarget(); |
| 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public void onSessionFinished(int sessionId, boolean success) { |
| 567 | mHandler.obtainMessage(MSG_SESSION_FINISHED, sessionId, success ? 1 : 0) |
| 568 | .sendToTarget(); |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 572 | /** {@hide} */ |
| 573 | @Deprecated |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 574 | public void addSessionCallback(@NonNull SessionCallback callback) { |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 575 | registerSessionCallback(callback); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 579 | * Register to watch for session lifecycle events. No special permissions |
| 580 | * are required to watch for these events. |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 581 | */ |
| 582 | public void registerSessionCallback(@NonNull SessionCallback callback) { |
| 583 | registerSessionCallback(callback, new Handler()); |
| 584 | } |
| 585 | |
| 586 | /** {@hide} */ |
| 587 | @Deprecated |
| 588 | public void addSessionCallback(@NonNull SessionCallback callback, @NonNull Handler handler) { |
| 589 | registerSessionCallback(callback, handler); |
| 590 | } |
| 591 | |
| 592 | /** |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 593 | * Register to watch for session lifecycle events. No special permissions |
| 594 | * are required to watch for these events. |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 595 | * |
| 596 | * @param handler to dispatch callback events through, otherwise uses |
| 597 | * calling thread. |
| 598 | */ |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 599 | public void registerSessionCallback(@NonNull SessionCallback callback, @NonNull Handler handler) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 600 | // TODO: remove this temporary guard once we have new prebuilts |
| 601 | final ApplicationInfo info = mContext.getApplicationInfo(); |
| 602 | if ("com.google.android.googlequicksearchbox".equals(info.packageName) |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 603 | && info.versionCode <= 300400110) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 604 | Log.d(TAG, "Ignoring callback request from old prebuilt"); |
| 605 | return; |
| 606 | } |
| 607 | |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 608 | synchronized (mDelegates) { |
| 609 | final SessionCallbackDelegate delegate = new SessionCallbackDelegate(callback, |
| 610 | handler.getLooper()); |
| 611 | try { |
| 612 | mInstaller.registerCallback(delegate, mUserId); |
| 613 | } catch (RemoteException e) { |
| 614 | throw e.rethrowAsRuntimeException(); |
| 615 | } |
| 616 | mDelegates.add(delegate); |
| 617 | } |
| 618 | } |
| 619 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 620 | /** {@hide} */ |
| 621 | @Deprecated |
| 622 | public void removeSessionCallback(@NonNull SessionCallback callback) { |
| 623 | unregisterSessionCallback(callback); |
| 624 | } |
| 625 | |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 626 | /** |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 627 | * Unregister a previously registered callback. |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 628 | */ |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 629 | public void unregisterSessionCallback(@NonNull SessionCallback callback) { |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 630 | synchronized (mDelegates) { |
| 631 | for (Iterator<SessionCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) { |
| 632 | final SessionCallbackDelegate delegate = i.next(); |
| 633 | if (delegate.mCallback == callback) { |
| 634 | try { |
| 635 | mInstaller.unregisterCallback(delegate); |
| 636 | } catch (RemoteException e) { |
| 637 | throw e.rethrowAsRuntimeException(); |
| 638 | } |
| 639 | i.remove(); |
| 640 | } |
| 641 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * An installation that is being actively staged. For an install to succeed, |
| 647 | * all existing and new packages must have identical package names, version |
| 648 | * codes, and signing certificates. |
| 649 | * <p> |
| 650 | * A session may contain any number of split packages. If the application |
| 651 | * does not yet exist, this session must include a base package. |
| 652 | * <p> |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 653 | * If an APK included in this session is already defined by the existing |
| 654 | * installation (for example, the same split name), the APK in this session |
| 655 | * will replace the existing APK. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 656 | */ |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 657 | public static class Session implements Closeable { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 658 | private IPackageInstallerSession mSession; |
| 659 | |
| 660 | /** {@hide} */ |
| 661 | public Session(IPackageInstallerSession session) { |
| 662 | mSession = session; |
| 663 | } |
| 664 | |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 665 | /** {@hide} */ |
| 666 | @Deprecated |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 667 | public void setProgress(float progress) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 668 | setStagingProgress(progress); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Set current progress of staging this session. Valid values are |
| 673 | * anywhere between 0 and 1. |
| 674 | * <p> |
| 675 | * Note that this progress may not directly correspond to the value |
| 676 | * reported by {@link SessionCallback#onProgressChanged(int, float)}, as |
| 677 | * the system may carve out a portion of the overall progress to |
| 678 | * represent its own internal installation work. |
| 679 | */ |
| 680 | public void setStagingProgress(float progress) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 681 | try { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 682 | mSession.setClientProgress(progress); |
| 683 | } catch (RemoteException e) { |
| 684 | throw e.rethrowAsRuntimeException(); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /** {@hide} */ |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 689 | public void addProgress(float progress) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 690 | try { |
| 691 | mSession.addClientProgress(progress); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 692 | } catch (RemoteException e) { |
| 693 | throw e.rethrowAsRuntimeException(); |
| 694 | } |
| 695 | } |
| 696 | |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 697 | /** |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 698 | * Open a stream to write an APK file into the session. |
| 699 | * <p> |
| 700 | * The returned stream will start writing data at the requested offset |
| 701 | * in the underlying file, which can be used to resume a partially |
| 702 | * written file. If a valid file length is specified, the system will |
| 703 | * preallocate the underlying disk space to optimize placement on disk. |
| 704 | * It's strongly recommended to provide a valid file length when known. |
| 705 | * <p> |
| 706 | * You can write data into the returned stream, optionally call |
| 707 | * {@link #fsync(OutputStream)} as needed to ensure bytes have been |
| 708 | * persisted to disk, and then close when finished. All streams must be |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 709 | * closed before calling {@link #commit(IntentSender)}. |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 710 | * |
| 711 | * @param name arbitrary, unique name of your choosing to identify the |
| 712 | * APK being written. You can open a file again for |
| 713 | * additional writes (such as after a reboot) by using the |
| 714 | * same name. This name is only meaningful within the context |
| 715 | * of a single install session. |
| 716 | * @param offsetBytes offset into the file to begin writing at, or 0 to |
| 717 | * start at the beginning of the file. |
| 718 | * @param lengthBytes total size of the file being written, used to |
| 719 | * preallocate the underlying disk space, or -1 if unknown. |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 720 | * The system may clear various caches as needed to allocate |
| 721 | * this space. |
| 722 | * @throws IOException if trouble opening the file for writing, such as |
| 723 | * lack of disk space or unavailable media. |
| 724 | * @throws SecurityException if called after the session has been |
| 725 | * committed or abandoned. |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 726 | */ |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 727 | public @NonNull OutputStream openWrite(@NonNull String name, long offsetBytes, |
| 728 | long lengthBytes) throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 729 | try { |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 730 | final ParcelFileDescriptor clientSocket = mSession.openWrite(name, |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 731 | offsetBytes, lengthBytes); |
Jeff Sharkey | 9a1507a | 2014-08-28 20:38:10 -0700 | [diff] [blame] | 732 | return new FileBridge.FileBridgeOutputStream(clientSocket); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 733 | } catch (RuntimeException e) { |
| 734 | ExceptionUtils.maybeUnwrapIOException(e); |
| 735 | throw e; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 736 | } catch (RemoteException e) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 737 | throw e.rethrowAsRuntimeException(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 741 | /** |
| 742 | * Ensure that any outstanding data for given stream has been committed |
| 743 | * to disk. This is only valid for streams returned from |
| 744 | * {@link #openWrite(String, long, long)}. |
| 745 | */ |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 746 | public void fsync(@NonNull OutputStream out) throws IOException { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 747 | if (out instanceof FileBridge.FileBridgeOutputStream) { |
| 748 | ((FileBridge.FileBridgeOutputStream) out).fsync(); |
| 749 | } else { |
| 750 | throw new IllegalArgumentException("Unrecognized stream"); |
| 751 | } |
| 752 | } |
| 753 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 754 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 755 | * Return all APK names contained in this session. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 756 | * <p> |
| 757 | * This returns all names which have been previously written through |
| 758 | * {@link #openWrite(String, long, long)} as part of this session. |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 759 | * |
| 760 | * @throws SecurityException if called after the session has been |
| 761 | * committed or abandoned. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 762 | */ |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 763 | public @NonNull String[] getNames() throws IOException { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 764 | try { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 765 | return mSession.getNames(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 766 | } catch (RuntimeException e) { |
| 767 | ExceptionUtils.maybeUnwrapIOException(e); |
| 768 | throw e; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 769 | } catch (RemoteException e) { |
| 770 | throw e.rethrowAsRuntimeException(); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Open a stream to read an APK file from the session. |
| 776 | * <p> |
| 777 | * This is only valid for names which have been previously written |
| 778 | * through {@link #openWrite(String, long, long)} as part of this |
| 779 | * session. For example, this stream may be used to calculate a |
| 780 | * {@link MessageDigest} of a written APK before committing. |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 781 | * |
| 782 | * @throws SecurityException if called after the session has been |
| 783 | * committed or abandoned. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 784 | */ |
| 785 | public @NonNull InputStream openRead(@NonNull String name) throws IOException { |
| 786 | try { |
| 787 | final ParcelFileDescriptor pfd = mSession.openRead(name); |
| 788 | return new ParcelFileDescriptor.AutoCloseInputStream(pfd); |
| 789 | } catch (RuntimeException e) { |
| 790 | ExceptionUtils.maybeUnwrapIOException(e); |
| 791 | throw e; |
| 792 | } catch (RemoteException e) { |
| 793 | throw e.rethrowAsRuntimeException(); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | /** |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 798 | * Attempt to commit everything staged in this session. This may require |
| 799 | * user intervention, and so it may not happen immediately. The final |
| 800 | * result of the commit will be reported through the given callback. |
| 801 | * <p> |
| 802 | * Once this method is called, no additional mutations may be performed |
| 803 | * on the session. If the device reboots before the session has been |
| 804 | * finalized, you may commit the session again. |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 805 | * |
| 806 | * @throws SecurityException if streams opened through |
| 807 | * {@link #openWrite(String, long, long)} are still open. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 808 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 809 | public void commit(@NonNull IntentSender statusReceiver) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 810 | try { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 811 | mSession.commit(statusReceiver); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 812 | } catch (RemoteException e) { |
| 813 | throw e.rethrowAsRuntimeException(); |
| 814 | } |
| 815 | } |
| 816 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 817 | /** |
| 818 | * Release this session object. You can open the session again if it |
| 819 | * hasn't been finalized. |
| 820 | */ |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 821 | @Override |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 822 | public void close() { |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 823 | try { |
| 824 | mSession.close(); |
| 825 | } catch (RemoteException e) { |
| 826 | throw e.rethrowAsRuntimeException(); |
| 827 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 830 | /** |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 831 | * Completely abandon this session, destroying all staged data and |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 832 | * rendering it invalid. Abandoned sessions will be reported to |
| 833 | * {@link SessionCallback} listeners as failures. This is equivalent to |
| 834 | * opening the session and calling {@link Session#abandon()}. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 835 | */ |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 836 | public void abandon() { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 837 | try { |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 838 | mSession.abandon(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 839 | } catch (RemoteException e) { |
| 840 | throw e.rethrowAsRuntimeException(); |
| 841 | } |
| 842 | } |
| 843 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 844 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 845 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 846 | * Parameters for creating a new {@link PackageInstaller.Session}. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 847 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 848 | public static class SessionParams implements Parcelable { |
| 849 | |
| 850 | /** {@hide} */ |
| 851 | public static final int MODE_INVALID = -1; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 852 | |
| 853 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 854 | * Mode for an install session whose staged APKs should fully replace any |
| 855 | * existing APKs for the target app. |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 856 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 857 | public static final int MODE_FULL_INSTALL = 1; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 858 | |
| 859 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 860 | * Mode for an install session that should inherit any existing APKs for the |
| 861 | * target app, unless they have been explicitly overridden (based on split |
| 862 | * name) by the session. For example, this can be used to add one or more |
| 863 | * split APKs to an existing installation. |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 864 | * <p> |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 865 | * If there are no existing APKs for the target app, this behaves like |
| 866 | * {@link #MODE_FULL_INSTALL}. |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 867 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 868 | public static final int MODE_INHERIT_EXISTING = 2; |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 869 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 870 | /** {@hide} */ |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 871 | public static final int UID_UNKNOWN = -1; |
| 872 | |
| 873 | /** {@hide} */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 874 | public int mode = MODE_INVALID; |
| 875 | /** {@hide} */ |
| 876 | public int installFlags; |
| 877 | /** {@hide} */ |
| 878 | public int installLocation = PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY; |
| 879 | /** {@hide} */ |
| 880 | public long sizeBytes = -1; |
| 881 | /** {@hide} */ |
| 882 | public String appPackageName; |
| 883 | /** {@hide} */ |
| 884 | public Bitmap appIcon; |
| 885 | /** {@hide} */ |
| 886 | public String appLabel; |
| 887 | /** {@hide} */ |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 888 | public long appIconLastModified = -1; |
| 889 | /** {@hide} */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 890 | public Uri originatingUri; |
| 891 | /** {@hide} */ |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 892 | public int originatingUid = UID_UNKNOWN; |
| 893 | /** {@hide} */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 894 | public Uri referrerUri; |
| 895 | /** {@hide} */ |
| 896 | public String abiOverride; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 897 | /** {@hide} */ |
| 898 | public String volumeUuid; |
Svet Ganov | 7121e18 | 2015-07-13 22:38:12 -0700 | [diff] [blame] | 899 | /** {@hide} */ |
| 900 | public String[] grantedRuntimePermissions; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 901 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 902 | /** |
| 903 | * Construct parameters for a new package install session. |
| 904 | * |
| 905 | * @param mode one of {@link #MODE_FULL_INSTALL} or |
| 906 | * {@link #MODE_INHERIT_EXISTING} describing how the session |
| 907 | * should interact with an existing app. |
| 908 | */ |
| 909 | public SessionParams(int mode) { |
| 910 | this.mode = mode; |
| 911 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 912 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 913 | /** {@hide} */ |
| 914 | public SessionParams(Parcel source) { |
| 915 | mode = source.readInt(); |
| 916 | installFlags = source.readInt(); |
| 917 | installLocation = source.readInt(); |
| 918 | sizeBytes = source.readLong(); |
| 919 | appPackageName = source.readString(); |
| 920 | appIcon = source.readParcelable(null); |
| 921 | appLabel = source.readString(); |
| 922 | originatingUri = source.readParcelable(null); |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 923 | originatingUid = source.readInt(); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 924 | referrerUri = source.readParcelable(null); |
| 925 | abiOverride = source.readString(); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 926 | volumeUuid = source.readString(); |
Svet Ganov | 7121e18 | 2015-07-13 22:38:12 -0700 | [diff] [blame] | 927 | grantedRuntimePermissions = source.readStringArray(); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /** |
| 931 | * Provide value of {@link PackageInfo#installLocation}, which may be used |
| 932 | * to determine where the app will be staged. Defaults to |
| 933 | * {@link PackageInfo#INSTALL_LOCATION_INTERNAL_ONLY}. |
| 934 | */ |
| 935 | public void setInstallLocation(int installLocation) { |
| 936 | this.installLocation = installLocation; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Optionally indicate the total size (in bytes) of all APKs that will be |
| 941 | * delivered in this session. The system may use this to ensure enough disk |
| 942 | * space exists before proceeding, or to estimate container size for |
| 943 | * installations living on external storage. |
| 944 | * |
| 945 | * @see PackageInfo#INSTALL_LOCATION_AUTO |
| 946 | * @see PackageInfo#INSTALL_LOCATION_PREFER_EXTERNAL |
| 947 | */ |
| 948 | public void setSize(long sizeBytes) { |
| 949 | this.sizeBytes = sizeBytes; |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Optionally set the package name of the app being installed. It's strongly |
| 954 | * recommended that you provide this value when known, so that observers can |
| 955 | * communicate installing apps to users. |
| 956 | * <p> |
| 957 | * If the APKs staged in the session aren't consistent with this package |
| 958 | * name, the install will fail. Regardless of this value, all APKs in the |
| 959 | * app must have the same package name. |
| 960 | */ |
| 961 | public void setAppPackageName(@Nullable String appPackageName) { |
| 962 | this.appPackageName = appPackageName; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Optionally set an icon representing the app being installed. This should |
| 967 | * be roughly {@link ActivityManager#getLauncherLargeIconSize()} in both |
| 968 | * dimensions. |
| 969 | */ |
| 970 | public void setAppIcon(@Nullable Bitmap appIcon) { |
| 971 | this.appIcon = appIcon; |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * Optionally set a label representing the app being installed. |
| 976 | */ |
| 977 | public void setAppLabel(@Nullable CharSequence appLabel) { |
| 978 | this.appLabel = (appLabel != null) ? appLabel.toString() : null; |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * Optionally set the URI where this package was downloaded from. Used for |
| 983 | * verification purposes. |
| 984 | * |
| 985 | * @see Intent#EXTRA_ORIGINATING_URI |
| 986 | */ |
| 987 | public void setOriginatingUri(@Nullable Uri originatingUri) { |
| 988 | this.originatingUri = originatingUri; |
| 989 | } |
| 990 | |
| 991 | /** |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 992 | * Sets the UID that initiated package installation. Used for verification purposes. |
| 993 | * |
| 994 | * @see PackageManager#EXTRA_VERIFICATION_INSTALLER_UID |
| 995 | */ |
| 996 | public void setOriginatingUid(int originatingUid) { |
| 997 | this.originatingUid = originatingUid; |
| 998 | } |
| 999 | |
| 1000 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1001 | * Optionally set the URI that referred you to install this package. Used |
| 1002 | * for verification purposes. |
| 1003 | * |
| 1004 | * @see Intent#EXTRA_REFERRER |
| 1005 | */ |
| 1006 | public void setReferrerUri(@Nullable Uri referrerUri) { |
| 1007 | this.referrerUri = referrerUri; |
| 1008 | } |
| 1009 | |
Svet Ganov | 7121e18 | 2015-07-13 22:38:12 -0700 | [diff] [blame] | 1010 | /** |
| 1011 | * Sets which runtime permissions to be granted to the package at installation. |
| 1012 | * Using this API requires holding {@link android.Manifest.permission |
| 1013 | * #INSTALL_GRANT_RUNTIME_PERMISSIONS} |
| 1014 | * |
| 1015 | * @param permissions The permissions to grant or null to grant all runtime |
| 1016 | * permissions. |
| 1017 | * |
| 1018 | * @hide |
| 1019 | */ |
| 1020 | @SystemApi |
| 1021 | @RequiresPermission(android.Manifest.permission.INSTALL_GRANT_RUNTIME_PERMISSIONS) |
| 1022 | public void setGrantedRuntimePermissions(String[] permissions) { |
| 1023 | installFlags |= PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS; |
| 1024 | this.grantedRuntimePermissions = permissions; |
| 1025 | } |
| 1026 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1027 | /** {@hide} */ |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1028 | public void setInstallFlagsInternal() { |
| 1029 | installFlags |= PackageManager.INSTALL_INTERNAL; |
| 1030 | installFlags &= ~PackageManager.INSTALL_EXTERNAL; |
| 1031 | } |
| 1032 | |
| 1033 | /** {@hide} */ |
| 1034 | public void setInstallFlagsExternal() { |
| 1035 | installFlags |= PackageManager.INSTALL_EXTERNAL; |
| 1036 | installFlags &= ~PackageManager.INSTALL_INTERNAL; |
| 1037 | } |
| 1038 | |
| 1039 | /** {@hide} */ |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 1040 | public void setInstallFlagsForcePermissionPrompt() { |
| 1041 | installFlags |= PackageManager.INSTALL_FORCE_PERMISSION_PROMPT; |
| 1042 | } |
| 1043 | |
| 1044 | /** {@hide} */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1045 | public void dump(IndentingPrintWriter pw) { |
| 1046 | pw.printPair("mode", mode); |
| 1047 | pw.printHexPair("installFlags", installFlags); |
| 1048 | pw.printPair("installLocation", installLocation); |
| 1049 | pw.printPair("sizeBytes", sizeBytes); |
| 1050 | pw.printPair("appPackageName", appPackageName); |
| 1051 | pw.printPair("appIcon", (appIcon != null)); |
| 1052 | pw.printPair("appLabel", appLabel); |
| 1053 | pw.printPair("originatingUri", originatingUri); |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 1054 | pw.printPair("originatingUid", originatingUid); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1055 | pw.printPair("referrerUri", referrerUri); |
| 1056 | pw.printPair("abiOverride", abiOverride); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 1057 | pw.printPair("volumeUuid", volumeUuid); |
Svet Ganov | 7121e18 | 2015-07-13 22:38:12 -0700 | [diff] [blame] | 1058 | pw.printPair("grantedRuntimePermissions", grantedRuntimePermissions); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1059 | pw.println(); |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1063 | public int describeContents() { |
| 1064 | return 0; |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1068 | public void writeToParcel(Parcel dest, int flags) { |
| 1069 | dest.writeInt(mode); |
| 1070 | dest.writeInt(installFlags); |
| 1071 | dest.writeInt(installLocation); |
| 1072 | dest.writeLong(sizeBytes); |
| 1073 | dest.writeString(appPackageName); |
| 1074 | dest.writeParcelable(appIcon, flags); |
| 1075 | dest.writeString(appLabel); |
| 1076 | dest.writeParcelable(originatingUri, flags); |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 1077 | dest.writeInt(originatingUid); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1078 | dest.writeParcelable(referrerUri, flags); |
| 1079 | dest.writeString(abiOverride); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 1080 | dest.writeString(volumeUuid); |
Svet Ganov | 7121e18 | 2015-07-13 22:38:12 -0700 | [diff] [blame] | 1081 | dest.writeStringArray(grantedRuntimePermissions); |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1082 | } |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1083 | |
| 1084 | public static final Parcelable.Creator<SessionParams> |
| 1085 | CREATOR = new Parcelable.Creator<SessionParams>() { |
| 1086 | @Override |
| 1087 | public SessionParams createFromParcel(Parcel p) { |
| 1088 | return new SessionParams(p); |
| 1089 | } |
| 1090 | |
| 1091 | @Override |
| 1092 | public SessionParams[] newArray(int size) { |
| 1093 | return new SessionParams[size]; |
| 1094 | } |
| 1095 | }; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 1098 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1099 | * Details for an active install session. |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 1100 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1101 | public static class SessionInfo implements Parcelable { |
| 1102 | |
| 1103 | /** {@hide} */ |
| 1104 | public int sessionId; |
| 1105 | /** {@hide} */ |
| 1106 | public String installerPackageName; |
| 1107 | /** {@hide} */ |
| 1108 | public String resolvedBaseCodePath; |
| 1109 | /** {@hide} */ |
| 1110 | public float progress; |
| 1111 | /** {@hide} */ |
| 1112 | public boolean sealed; |
| 1113 | /** {@hide} */ |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1114 | public boolean active; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1115 | |
| 1116 | /** {@hide} */ |
| 1117 | public int mode; |
| 1118 | /** {@hide} */ |
| 1119 | public long sizeBytes; |
| 1120 | /** {@hide} */ |
| 1121 | public String appPackageName; |
| 1122 | /** {@hide} */ |
| 1123 | public Bitmap appIcon; |
| 1124 | /** {@hide} */ |
| 1125 | public CharSequence appLabel; |
| 1126 | |
| 1127 | /** {@hide} */ |
| 1128 | public SessionInfo() { |
| 1129 | } |
| 1130 | |
| 1131 | /** {@hide} */ |
| 1132 | public SessionInfo(Parcel source) { |
| 1133 | sessionId = source.readInt(); |
| 1134 | installerPackageName = source.readString(); |
| 1135 | resolvedBaseCodePath = source.readString(); |
| 1136 | progress = source.readFloat(); |
| 1137 | sealed = source.readInt() != 0; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1138 | active = source.readInt() != 0; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1139 | |
| 1140 | mode = source.readInt(); |
| 1141 | sizeBytes = source.readLong(); |
| 1142 | appPackageName = source.readString(); |
| 1143 | appIcon = source.readParcelable(null); |
| 1144 | appLabel = source.readString(); |
| 1145 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1146 | |
| 1147 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1148 | * Return the ID for this session. |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1149 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1150 | public int getSessionId() { |
| 1151 | return sessionId; |
| 1152 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1153 | |
| 1154 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1155 | * Return the package name of the app that owns this session. |
| 1156 | */ |
| 1157 | public @Nullable String getInstallerPackageName() { |
| 1158 | return installerPackageName; |
| 1159 | } |
| 1160 | |
| 1161 | /** |
| 1162 | * Return current overall progress of this session, between 0 and 1. |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 1163 | * <p> |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1164 | * Note that this progress may not directly correspond to the value |
| 1165 | * reported by |
| 1166 | * {@link PackageInstaller.Session#setStagingProgress(float)}, as the |
| 1167 | * system may carve out a portion of the overall progress to represent |
| 1168 | * its own internal installation work. |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1169 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1170 | public float getProgress() { |
| 1171 | return progress; |
| 1172 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1173 | |
| 1174 | /** |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1175 | * Return if this session is currently active. |
| 1176 | * <p> |
| 1177 | * A session is considered active whenever there is ongoing forward |
| 1178 | * progress being made, such as the installer holding an open |
| 1179 | * {@link Session} instance while streaming data into place, or the |
| 1180 | * system optimizing code as the result of |
| 1181 | * {@link Session#commit(IntentSender)}. |
| 1182 | * <p> |
| 1183 | * If the installer closes the {@link Session} without committing, the |
| 1184 | * session is considered inactive until the installer opens the session |
| 1185 | * again. |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1186 | */ |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1187 | public boolean isActive() { |
| 1188 | return active; |
| 1189 | } |
| 1190 | |
| 1191 | /** {@hide} */ |
| 1192 | @Deprecated |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1193 | public boolean isOpen() { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1194 | return isActive(); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1195 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1196 | |
| 1197 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1198 | * Return the package name this session is working with. May be {@code null} |
| 1199 | * if unknown. |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1200 | */ |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1201 | public @Nullable String getAppPackageName() { |
| 1202 | return appPackageName; |
| 1203 | } |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 1204 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1205 | /** |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1206 | * Return an icon representing the app being installed. May be {@code null} |
| 1207 | * if unavailable. |
| 1208 | */ |
| 1209 | public @Nullable Bitmap getAppIcon() { |
| 1210 | return appIcon; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * Return a label representing the app being installed. May be {@code null} |
| 1215 | * if unavailable. |
| 1216 | */ |
| 1217 | public @Nullable CharSequence getAppLabel() { |
| 1218 | return appLabel; |
| 1219 | } |
| 1220 | |
| 1221 | /** |
| 1222 | * Return an Intent that can be started to view details about this install |
| 1223 | * session. This may surface actions such as pause, resume, or cancel. |
| 1224 | * <p> |
| 1225 | * In some cases, a matching Activity may not exist, so ensure you safeguard |
| 1226 | * against this. |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1227 | * |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1228 | * @see PackageInstaller#ACTION_SESSION_DETAILS |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1229 | */ |
Jeff Sharkey | de74231 | 2014-09-15 14:04:56 -0700 | [diff] [blame] | 1230 | public @Nullable Intent createDetailsIntent() { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1231 | final Intent intent = new Intent(PackageInstaller.ACTION_SESSION_DETAILS); |
| 1232 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
| 1233 | intent.setPackage(installerPackageName); |
| 1234 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1235 | return intent; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Jeff Sharkey | de74231 | 2014-09-15 14:04:56 -0700 | [diff] [blame] | 1238 | /** {@hide} */ |
| 1239 | @Deprecated |
| 1240 | public @Nullable Intent getDetailsIntent() { |
| 1241 | return createDetailsIntent(); |
| 1242 | } |
| 1243 | |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1244 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1245 | public int describeContents() { |
| 1246 | return 0; |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1250 | public void writeToParcel(Parcel dest, int flags) { |
| 1251 | dest.writeInt(sessionId); |
| 1252 | dest.writeString(installerPackageName); |
| 1253 | dest.writeString(resolvedBaseCodePath); |
| 1254 | dest.writeFloat(progress); |
| 1255 | dest.writeInt(sealed ? 1 : 0); |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1256 | dest.writeInt(active ? 1 : 0); |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1257 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1258 | dest.writeInt(mode); |
| 1259 | dest.writeLong(sizeBytes); |
| 1260 | dest.writeString(appPackageName); |
| 1261 | dest.writeParcelable(appIcon, flags); |
| 1262 | dest.writeString(appLabel != null ? appLabel.toString() : null); |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1263 | } |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1264 | |
| 1265 | public static final Parcelable.Creator<SessionInfo> |
| 1266 | CREATOR = new Parcelable.Creator<SessionInfo>() { |
| 1267 | @Override |
| 1268 | public SessionInfo createFromParcel(Parcel p) { |
| 1269 | return new SessionInfo(p); |
| 1270 | } |
| 1271 | |
| 1272 | @Override |
| 1273 | public SessionInfo[] newArray(int size) { |
| 1274 | return new SessionInfo[size]; |
| 1275 | } |
| 1276 | }; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1277 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1278 | } |