13a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey/*
23a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey * Copyright (C) 2014 The Android Open Source Project
33a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey *
43a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
53a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey * you may not use this file except in compliance with the License.
63a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey * You may obtain a copy of the License at
73a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey *
83a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
93a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey *
103a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
113a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
123a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey * See the License for the specific language governing permissions and
143a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey * limitations under the License.
153a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey */
163a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
173a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkeypackage android.content.pm;
183a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
1916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkeyimport android.annotation.NonNull;
2016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkeyimport android.annotation.Nullable;
211cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkeyimport android.annotation.SdkConstant;
221cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkeyimport android.annotation.SdkConstant.SdkConstantType;
23a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkeyimport android.app.ActivityManager;
24a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkeyimport android.content.Context;
25fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkeyimport android.content.Intent;
26a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkeyimport android.content.IntentSender;
27a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkeyimport android.graphics.Bitmap;
28a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkeyimport android.net.Uri;
2978cc340c2de873d6995c283b777476f7237d690fJeff Sharkeyimport android.os.FileBridge;
3016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkeyimport android.os.Handler;
3116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkeyimport android.os.Looper;
3216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkeyimport android.os.Message;
33a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkeyimport android.os.Parcel;
343a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkeyimport android.os.ParcelFileDescriptor;
35a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkeyimport android.os.Parcelable;
363a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkeyimport android.os.RemoteException;
37a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkeyimport android.util.ExceptionUtils;
38a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkeyimport android.util.Log;
39a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
40a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkeyimport com.android.internal.util.IndentingPrintWriter;
413a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
42ec55ef0934b8e0d1bb705434947de817f7be57f1Jeff Sharkeyimport java.io.Closeable;
43a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkeyimport java.io.IOException;
441cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkeyimport java.io.InputStream;
4578cc340c2de873d6995c283b777476f7237d690fJeff Sharkeyimport java.io.OutputStream;
461cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkeyimport java.security.MessageDigest;
4716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkeyimport java.util.ArrayList;
48941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkeyimport java.util.Collections;
4916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkeyimport java.util.Iterator;
50bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkeyimport java.util.List;
5178cc340c2de873d6995c283b777476f7237d690fJeff Sharkey
526c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey/**
536c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * Offers the ability to install, upgrade, and remove applications on the
546c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * device. This includes support for apps packaged either as a single
556c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * "monolithic" APK, or apps packaged as multiple "split" APKs.
566c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * <p>
576c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * An app is delivered for installation through a
586c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * {@link PackageInstaller.Session}, which any app can create. Once the session
596c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * is created, the installer can stream one or more APKs into place until it
606c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * decides to either commit or destroy the session. Committing may require user
616c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * intervention to complete the installation.
626c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * <p>
636c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * Sessions can install brand new apps, upgrade existing apps, or add new splits
64da96e137bcc8191c584ada7b5de31eaae92f244fJeff Sharkey * into an existing app.
656c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * <p>
66da96e137bcc8191c584ada7b5de31eaae92f244fJeff Sharkey * Apps packaged as multiple split APKs always consist of a single "base" APK
676c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * (with a {@code null} split name) and zero or more "split" APKs (with unique
686c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * split names). Any subset of these APKs can be installed together, as long as
696c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * the following constraints are met:
706c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * <ul>
716c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * <li>All APKs must have the exact same package name, version code, and signing
726c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * certificates.
736c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * <li>All APKs must have unique split names.
74da96e137bcc8191c584ada7b5de31eaae92f244fJeff Sharkey * <li>All installations must contain a single base APK.
756c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey * </ul>
766c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey */
773a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkeypublic class PackageInstaller {
78a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    private static final String TAG = "PackageInstaller";
79a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
801cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey    /**
811cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * Activity Action: Show details about a particular install session. This
821cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * may surface actions such as pause, resume, or cancel.
831cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * <p>
841cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * This should always be scoped to the installer package that owns the
85de74231f245c90e5861ec84a9880b5b4ec247480Jeff Sharkey     * session. Clients should use {@link SessionInfo#createDetailsIntent()} to
86a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * build this intent correctly.
871cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * <p>
881cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * In some cases, a matching Activity may not exist, so ensure you safeguard
891cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * against this.
90941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * <p>
91941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * The session to show details for is defined in {@link #EXTRA_SESSION_ID}.
921cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     */
931cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
941cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey    public static final String ACTION_SESSION_DETAILS = "android.content.pm.action.SESSION_DETAILS";
951cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey
967328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey    /** {@hide} */
977328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey    public static final String
987328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey            ACTION_CONFIRM_PERMISSIONS = "android.content.pm.action.CONFIRM_PERMISSIONS";
997328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey
1001cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey    /**
101941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * An integer session ID that an operation is working with.
1021cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     *
103941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * @see Intent#getIntExtra(String, int)
1041cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     */
1051cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey    public static final String EXTRA_SESSION_ID = "android.content.pm.extra.SESSION_ID";
1061cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey
107941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey    /**
108941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * Package name that an operation is working with.
109941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     *
110941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * @see Intent#getStringExtra(String)
111941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     */
112941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey    public static final String EXTRA_PACKAGE_NAME = "android.content.pm.extra.PACKAGE_NAME";
113941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey
114941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey    /**
115941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * Current status of an operation. Will be one of
116941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * {@link #STATUS_PENDING_USER_ACTION}, {@link #STATUS_SUCCESS},
117941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * {@link #STATUS_FAILURE}, {@link #STATUS_FAILURE_ABORTED},
118941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * {@link #STATUS_FAILURE_BLOCKED}, {@link #STATUS_FAILURE_CONFLICT},
119941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * {@link #STATUS_FAILURE_INCOMPATIBLE}, {@link #STATUS_FAILURE_INVALID}, or
120941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * {@link #STATUS_FAILURE_STORAGE}.
121941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * <p>
122941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * More information about a status may be available through additional
123941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * extras; see the individual status documentation for details.
124941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     *
125941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * @see Intent#getIntExtra(String, int)
126941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     */
127a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public static final String EXTRA_STATUS = "android.content.pm.extra.STATUS";
128941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey
129941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey    /**
130941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * Detailed string representation of the status, including raw details that
131941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * are useful for debugging.
132941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     *
133941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * @see Intent#getStringExtra(String)
134941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     */
135a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public static final String EXTRA_STATUS_MESSAGE = "android.content.pm.extra.STATUS_MESSAGE";
136a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
137a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
138941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * Another package name relevant to a status. This is typically the package
139941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * responsible for causing an operation failure.
140a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     *
141bb7b7bea19223c1eba74f525c7fe87ca3911813bJeff Sharkey     * @see Intent#getStringExtra(String)
142a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
143941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey    public static final String
144941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey            EXTRA_OTHER_PACKAGE_NAME = "android.content.pm.extra.OTHER_PACKAGE_NAME";
145941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey
146941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey    /**
147941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * Storage path relevant to a status.
148941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     *
149941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * @see Intent#getStringExtra(String)
150941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     */
151941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey    public static final String EXTRA_STORAGE_PATH = "android.content.pm.extra.STORAGE_PATH";
152bb7b7bea19223c1eba74f525c7fe87ca3911813bJeff Sharkey
153bb7b7bea19223c1eba74f525c7fe87ca3911813bJeff Sharkey    /** {@hide} */
154bb7b7bea19223c1eba74f525c7fe87ca3911813bJeff Sharkey    @Deprecated
155a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public static final String EXTRA_PACKAGE_NAMES = "android.content.pm.extra.PACKAGE_NAMES";
156a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
157a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /** {@hide} */
158a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public static final String EXTRA_LEGACY_STATUS = "android.content.pm.extra.LEGACY_STATUS";
159a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /** {@hide} */
160a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public static final String EXTRA_LEGACY_BUNDLE = "android.content.pm.extra.LEGACY_BUNDLE";
161f06009542390472872da986486d385001e91a2a7Jeff Sharkey    /** {@hide} */
162f06009542390472872da986486d385001e91a2a7Jeff Sharkey    public static final String EXTRA_CALLBACK = "android.content.pm.extra.CALLBACK";
163f06009542390472872da986486d385001e91a2a7Jeff Sharkey
164a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
165a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * User action is currently required to proceed. You can launch the intent
166a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * activity described by {@link Intent#EXTRA_INTENT} to involve the user and
167a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * continue.
168a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * <p>
169a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * You may choose to immediately launch the intent if the user is actively
170a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * using your app. Otherwise, you should use a notification to guide the
171a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * user back into your app before launching.
172a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     *
173a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * @see Intent#getParcelableExtra(String)
174a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
175742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey    public static final int STATUS_PENDING_USER_ACTION = -1;
176a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
177a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
178a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * The operation succeeded.
179a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
180a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public static final int STATUS_SUCCESS = 0;
181a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
182a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
183a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * The operation failed in a generic way. The system will always try to
184a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * provide a more specific failure reason, but in some rare cases this may
185a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * be delivered.
186a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     *
187a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * @see #EXTRA_STATUS_MESSAGE
188a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
189a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public static final int STATUS_FAILURE = 1;
190a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
191a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
192a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * The operation failed because it was blocked. For example, a device policy
193a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * may be blocking the operation, a package verifier may have blocked the
194a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * operation, or the app may be required for core system operation.
195941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * <p>
196941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * The result may also contain {@link #EXTRA_OTHER_PACKAGE_NAME} with the
197941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * specific package blocking the install.
198a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     *
199a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * @see #EXTRA_STATUS_MESSAGE
200941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * @see #EXTRA_OTHER_PACKAGE_NAME
201a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
202742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey    public static final int STATUS_FAILURE_BLOCKED = 2;
203a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
204a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
205a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * The operation failed because it was actively aborted. For example, the
206a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * user actively declined requested permissions, or the session was
207a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * abandoned.
208a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     *
209a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * @see #EXTRA_STATUS_MESSAGE
210a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
211742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey    public static final int STATUS_FAILURE_ABORTED = 3;
212a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
213a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
214a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * The operation failed because one or more of the APKs was invalid. For
215a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * example, they might be malformed, corrupt, incorrectly signed,
216a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * mismatched, etc.
217a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     *
218a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * @see #EXTRA_STATUS_MESSAGE
219a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
220742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey    public static final int STATUS_FAILURE_INVALID = 4;
221a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
222a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
223a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * The operation failed because it conflicts (or is inconsistent with) with
224a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * another package already installed on the device. For example, an existing
225a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * permission, incompatible certificates, etc. The user may be able to
226a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * uninstall another app to fix the issue.
227a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * <p>
228941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * The result may also contain {@link #EXTRA_OTHER_PACKAGE_NAME} with the
229bb7b7bea19223c1eba74f525c7fe87ca3911813bJeff Sharkey     * specific package identified as the cause of the conflict.
230a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     *
231a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * @see #EXTRA_STATUS_MESSAGE
232941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * @see #EXTRA_OTHER_PACKAGE_NAME
233a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
234742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey    public static final int STATUS_FAILURE_CONFLICT = 5;
235a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
236a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
237a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * The operation failed because of storage issues. For example, the device
238a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * may be running low on space, or external media may be unavailable. The
239a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * user may be able to help free space or insert different external media.
240941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * <p>
241941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * The result may also contain {@link #EXTRA_STORAGE_PATH} with the path to
242941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * the storage device that caused the failure.
243a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     *
244a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * @see #EXTRA_STATUS_MESSAGE
245941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey     * @see #EXTRA_STORAGE_PATH
246a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
247742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey    public static final int STATUS_FAILURE_STORAGE = 6;
248a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
249a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    /**
250a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * The operation failed because it is fundamentally incompatible with this
251a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * device. For example, the app may require a hardware feature that doesn't
252a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * exist, it may be missing native code for the ABIs supported by the
253a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * device, or it requires a newer SDK version, etc.
254a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     *
255a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * @see #EXTRA_STATUS_MESSAGE
256a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     */
257742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey    public static final int STATUS_FAILURE_INCOMPATIBLE = 7;
258a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
259a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    private final Context mContext;
2603a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    private final PackageManager mPm;
2613a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    private final IPackageInstaller mInstaller;
2623a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    private final int mUserId;
2633a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    private final String mInstallerPackageName;
2643a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
26516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    private final ArrayList<SessionCallbackDelegate> mDelegates = new ArrayList<>();
26616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
2673a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    /** {@hide} */
268a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public PackageInstaller(Context context, PackageManager pm, IPackageInstaller installer,
269ec55ef0934b8e0d1bb705434947de817f7be57f1Jeff Sharkey            String installerPackageName, int userId) {
270a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        mContext = context;
2713a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        mPm = pm;
2723a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        mInstaller = installer;
2733a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        mInstallerPackageName = installerPackageName;
274ec55ef0934b8e0d1bb705434947de817f7be57f1Jeff Sharkey        mUserId = userId;
2753a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    }
2763a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
2776c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey    /**
2786c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * Create a new session using the given parameters, returning a unique ID
2796c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * that represents the session. Once created, the session can be opened
2806c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * multiple times across multiple device boots.
2816c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * <p>
2826c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * The system may automatically destroy sessions that have not been
2836c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * finalized (either committed or abandoned) within a reasonable period of
2846c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * time, typically on the order of a day.
2856c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     *
2866c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * @throws IOException if parameters were unsatisfiable, such as lack of
2876c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     *             disk space or unavailable media.
28877d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * @throws SecurityException when installation services are unavailable,
28977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *             such as when called from a restricted user.
29077d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * @throws IllegalArgumentException when {@link SessionParams} is invalid.
291f174c6e6de6ba863179401aa7b3d55d91ceed707Jeff Sharkey     * @return positive, non-zero unique ID that represents the created session.
292f174c6e6de6ba863179401aa7b3d55d91ceed707Jeff Sharkey     *         This ID remains consistent across device reboots until the
293f174c6e6de6ba863179401aa7b3d55d91ceed707Jeff Sharkey     *         session is finalized. IDs are not reused during a given boot.
2946c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     */
295a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public int createSession(@NonNull SessionParams params) throws IOException {
2963a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        try {
29716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            return mInstaller.createSession(params, mInstallerPackageName, mUserId);
298a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey        } catch (RuntimeException e) {
299a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey            ExceptionUtils.maybeUnwrapIOException(e);
300a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey            throw e;
3013a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        } catch (RemoteException e) {
3023a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            throw e.rethrowAsRuntimeException();
3033a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
3043a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    }
3053a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
3066c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey    /**
30716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     * Open an existing session to actively perform work. To succeed, the caller
30816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     * must be the owner of the install session.
30977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *
31077d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * @throws IOException if parameters were unsatisfiable, such as lack of
31177d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *             disk space or unavailable media.
31277d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * @throws SecurityException when the caller does not own the session, or
31377d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *             the session is invalid.
3146c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     */
315bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey    public @NonNull Session openSession(int sessionId) throws IOException {
3163a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        try {
3173a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            return new Session(mInstaller.openSession(sessionId));
318bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        } catch (RuntimeException e) {
319bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey            ExceptionUtils.maybeUnwrapIOException(e);
320bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey            throw e;
3213a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        } catch (RemoteException e) {
3223a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            throw e.rethrowAsRuntimeException();
3233a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
324381d94b712605112b35d7f70064b0d18bd877877Jeff Sharkey    }
325381d94b712605112b35d7f70064b0d18bd877877Jeff Sharkey
326ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    /**
327ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey     * Update the icon representing the app being installed in a specific
328ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey     * session. This should be roughly
329ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey     * {@link ActivityManager#getLauncherLargeIconSize()} in both dimensions.
33077d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *
33177d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * @throws SecurityException when the caller does not own the session, or
33277d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *             the session is invalid.
333ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey     */
334ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    public void updateSessionAppIcon(int sessionId, @Nullable Bitmap appIcon) {
335ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        try {
336ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey            mInstaller.updateSessionAppIcon(sessionId, appIcon);
337ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        } catch (RemoteException e) {
338ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey            throw e.rethrowAsRuntimeException();
339ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        }
340ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    }
341ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey
342ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    /**
343ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey     * Update the label representing the app being installed in a specific
344ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey     * session.
34577d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *
34677d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * @throws SecurityException when the caller does not own the session, or
34777d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *             the session is invalid.
348ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey     */
349ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    public void updateSessionAppLabel(int sessionId, @Nullable CharSequence appLabel) {
350ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        try {
351ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey            final String val = (appLabel != null) ? appLabel.toString() : null;
352ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey            mInstaller.updateSessionAppLabel(sessionId, val);
353ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        } catch (RemoteException e) {
354ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey            throw e.rethrowAsRuntimeException();
355ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        }
356ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    }
357ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey
35877d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey    /**
35977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * Completely abandon the given session, destroying all staged data and
36077d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * rendering it invalid. Abandoned sessions will be reported to
36177d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * {@link SessionCallback} listeners as failures. This is equivalent to
36277d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * opening the session and calling {@link Session#abandon()}.
36377d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *
36477d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * @throws SecurityException when the caller does not own the session, or
36577d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *             the session is invalid.
36677d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     */
367381d94b712605112b35d7f70064b0d18bd877877Jeff Sharkey    public void abandonSession(int sessionId) {
368381d94b712605112b35d7f70064b0d18bd877877Jeff Sharkey        try {
369381d94b712605112b35d7f70064b0d18bd877877Jeff Sharkey            mInstaller.abandonSession(sessionId);
370381d94b712605112b35d7f70064b0d18bd877877Jeff Sharkey        } catch (RemoteException e) {
371381d94b712605112b35d7f70064b0d18bd877877Jeff Sharkey            throw e.rethrowAsRuntimeException();
372381d94b712605112b35d7f70064b0d18bd877877Jeff Sharkey        }
3733a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    }
3743a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
3756c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey    /**
37677d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * Return details for a specific session. No special permissions are
37777d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * required to retrieve these details.
37877d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *
37977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * @return details for the requested session, or {@code null} if the session
38077d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     *         does not exist.
3816c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     */
382a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public @Nullable SessionInfo getSessionInfo(int sessionId) {
3833a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        try {
38416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            return mInstaller.getSessionInfo(sessionId);
38516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        } catch (RemoteException e) {
38616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            throw e.rethrowAsRuntimeException();
38716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        }
38816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    }
38916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
39016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    /**
39177d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * Return list of all known install sessions, regardless of the installer.
39216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     */
393a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public @NonNull List<SessionInfo> getAllSessions() {
394941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey        final ApplicationInfo info = mContext.getApplicationInfo();
395941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey        if ("com.google.android.googlequicksearchbox".equals(info.packageName)
396bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                && info.versionCode <= 300400110) {
397941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey            Log.d(TAG, "Ignoring callback request from old prebuilt");
398941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey            return Collections.EMPTY_LIST;
399941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey        }
400941a8ba1a6043cf84a7bf622e44a0b4f7abd0178Jeff Sharkey
40116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        try {
40297d47ed036ff7bd3d7d2ddc1c6df1104ec237559Jeff Sharkey            return mInstaller.getAllSessions(mUserId).getList();
40316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        } catch (RemoteException e) {
40416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            throw e.rethrowAsRuntimeException();
40516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        }
40616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    }
40716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
40816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    /**
40977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * Return list of all known install sessions owned by the calling app.
41016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     */
411a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public @NonNull List<SessionInfo> getMySessions() {
41216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        try {
41397d47ed036ff7bd3d7d2ddc1c6df1104ec237559Jeff Sharkey            return mInstaller.getMySessions(mInstallerPackageName, mUserId).getList();
4143a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        } catch (RemoteException e) {
4153a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            throw e.rethrowAsRuntimeException();
4163a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
4173a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    }
4183a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
4196c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey    /**
4206c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * Uninstall the given package, removing it completely from the device. This
4216c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * method is only available to the current "installer of record" for the
4226c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * package.
4236c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     */
424a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public void uninstall(@NonNull String packageName, @NonNull IntentSender statusReceiver) {
4253a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        try {
426a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            mInstaller.uninstall(packageName, 0, statusReceiver, mUserId);
427bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        } catch (RemoteException e) {
428bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey            throw e.rethrowAsRuntimeException();
429bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        }
430bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey    }
431bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
4327328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey    /** {@hide} */
4337328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey    public void setPermissionsResult(int sessionId, boolean accepted) {
4347328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey        try {
4357328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey            mInstaller.setPermissionsResult(sessionId, accepted);
4367328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey        } catch (RemoteException e) {
4377328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey            throw e.rethrowAsRuntimeException();
4387328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey        }
4397328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey    }
4407328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey
4416c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey    /**
4426c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     * Events for observing session lifecycle.
4431cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * <p>
4441cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * A typical session lifecycle looks like this:
4451cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * <ul>
4461cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * <li>An installer creates a session to indicate pending app delivery. All
4471cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * install details are available at this point.
4481cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * <li>The installer opens the session to deliver APK data. Note that a
4491cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * session may be opened and closed multiple times as network connectivity
4501cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * changes. The installer may deliver periodic progress updates.
4511cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * <li>The installer commits or abandons the session, resulting in the
4521cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * session being finished.
4531cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey     * </ul>
4546c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     */
45516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    public static abstract class SessionCallback {
4566c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey        /**
4571cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * New session has been created. Details about the session can be
4581cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * obtained from {@link PackageInstaller#getSessionInfo(int)}.
4596c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         */
46016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public abstract void onCreated(int sessionId);
4616c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey
4626c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey        /**
463ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey         * Badging details for an existing session has changed. For example, the
464ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey         * app icon or label has been updated.
465ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey         */
466ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        public abstract void onBadgingChanged(int sessionId);
467ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey
468ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        /**
469bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * Active state for session has been changed.
470bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * <p>
471bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * A session is considered active whenever there is ongoing forward
472bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * progress being made, such as the installer holding an open
473bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * {@link Session} instance while streaming data into place, or the
474bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * system optimizing code as the result of
475bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * {@link Session#commit(IntentSender)}.
476bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * <p>
477bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * If the installer closes the {@link Session} without committing, the
478bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * session is considered inactive until the installer opens the session
479bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * again.
4801cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         */
481bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        public abstract void onActiveChanged(int sessionId, boolean active);
4821cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey
4831cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey        /**
4846c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * Progress for given session has been updated.
4856c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * <p>
4866c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * Note that this progress may not directly correspond to the value
487bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * reported by
488bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * {@link PackageInstaller.Session#setStagingProgress(float)}, as the
489bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * system may carve out a portion of the overall progress to represent
490bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * its own internal installation work.
4916c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         */
49216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public abstract void onProgressChanged(int sessionId, float progress);
4936c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey
4946c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey        /**
49516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * Session has completely finished, either with success or failure.
4966c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         */
49716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public abstract void onFinished(int sessionId, boolean success);
49816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    }
49916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
50016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    /** {@hide} */
50116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    private static class SessionCallbackDelegate extends IPackageInstallerCallback.Stub implements
50216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            Handler.Callback {
50316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        private static final int MSG_SESSION_CREATED = 1;
504ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        private static final int MSG_SESSION_BADGING_CHANGED = 2;
505bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        private static final int MSG_SESSION_ACTIVE_CHANGED = 3;
506ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        private static final int MSG_SESSION_PROGRESS_CHANGED = 4;
507bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        private static final int MSG_SESSION_FINISHED = 5;
50816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
50916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        final SessionCallback mCallback;
51016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        final Handler mHandler;
51116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
51216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public SessionCallbackDelegate(SessionCallback callback, Looper looper) {
51316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            mCallback = callback;
51416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            mHandler = new Handler(looper, this);
51516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        }
51616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
51716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        @Override
51816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public boolean handleMessage(Message msg) {
519bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey            final int sessionId = msg.arg1;
52016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            switch (msg.what) {
52116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                case MSG_SESSION_CREATED:
522bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                    mCallback.onCreated(sessionId);
52316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                    return true;
524ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey                case MSG_SESSION_BADGING_CHANGED:
525bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                    mCallback.onBadgingChanged(sessionId);
526ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey                    return true;
527bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                case MSG_SESSION_ACTIVE_CHANGED:
528bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                    final boolean active = msg.arg2 != 0;
529bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                    mCallback.onActiveChanged(sessionId, active);
5301cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey                    return true;
53116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                case MSG_SESSION_PROGRESS_CHANGED:
532bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                    mCallback.onProgressChanged(sessionId, (float) msg.obj);
5331cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey                    return true;
53416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                case MSG_SESSION_FINISHED:
535bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                    mCallback.onFinished(sessionId, msg.arg2 != 0);
53616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                    return true;
53716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            }
53816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            return false;
53916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        }
54016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
54116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        @Override
54216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public void onSessionCreated(int sessionId) {
54316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            mHandler.obtainMessage(MSG_SESSION_CREATED, sessionId, 0).sendToTarget();
54416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        }
54516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
54616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        @Override
547ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        public void onSessionBadgingChanged(int sessionId) {
548ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey            mHandler.obtainMessage(MSG_SESSION_BADGING_CHANGED, sessionId, 0).sendToTarget();
549ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        }
550ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey
551ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        @Override
552bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        public void onSessionActiveChanged(int sessionId, boolean active) {
553bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey            mHandler.obtainMessage(MSG_SESSION_ACTIVE_CHANGED, sessionId, active ? 1 : 0)
554bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                    .sendToTarget();
5551cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey        }
5561cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey
5571cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey        @Override
55816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public void onSessionProgressChanged(int sessionId, float progress) {
55916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            mHandler.obtainMessage(MSG_SESSION_PROGRESS_CHANGED, sessionId, 0, progress)
56016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                    .sendToTarget();
56116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        }
56216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
56316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        @Override
56416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public void onSessionFinished(int sessionId, boolean success) {
56516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            mHandler.obtainMessage(MSG_SESSION_FINISHED, sessionId, success ? 1 : 0)
56616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                    .sendToTarget();
56716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        }
568bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey    }
569bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
570ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    /** {@hide} */
571ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    @Deprecated
572ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    public void addSessionCallback(@NonNull SessionCallback callback) {
573ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        registerSessionCallback(callback);
574ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    }
575ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey
5766c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey    /**
57777d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * Register to watch for session lifecycle events. No special permissions
57877d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * are required to watch for these events.
5796c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     */
580ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    public void registerSessionCallback(@NonNull SessionCallback callback) {
581ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        registerSessionCallback(callback, new Handler());
582ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    }
583ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey
584ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    /** {@hide} */
585ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    @Deprecated
586ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    public void addSessionCallback(@NonNull SessionCallback callback, @NonNull Handler handler) {
587ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        registerSessionCallback(callback, handler);
58816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    }
58916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
59016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey    /**
59177d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * Register to watch for session lifecycle events. No special permissions
59277d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * are required to watch for these events.
59316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     *
59416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     * @param handler to dispatch callback events through, otherwise uses
59516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     *            calling thread.
59616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     */
597ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    public void registerSessionCallback(@NonNull SessionCallback callback, @NonNull Handler handler) {
598a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        // TODO: remove this temporary guard once we have new prebuilts
599a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        final ApplicationInfo info = mContext.getApplicationInfo();
600a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        if ("com.google.android.googlequicksearchbox".equals(info.packageName)
601bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey                && info.versionCode <= 300400110) {
602a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            Log.d(TAG, "Ignoring callback request from old prebuilt");
603a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return;
604a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
605a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
60616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        synchronized (mDelegates) {
60716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            final SessionCallbackDelegate delegate = new SessionCallbackDelegate(callback,
60816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                    handler.getLooper());
60916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            try {
61016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                mInstaller.registerCallback(delegate, mUserId);
61116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            } catch (RemoteException e) {
61216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                throw e.rethrowAsRuntimeException();
61316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            }
61416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            mDelegates.add(delegate);
615bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        }
616bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey    }
617bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
618ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    /** {@hide} */
619ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    @Deprecated
620ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    public void removeSessionCallback(@NonNull SessionCallback callback) {
621ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey        unregisterSessionCallback(callback);
622ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    }
623ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey
6246c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey    /**
62577d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey     * Unregister a previously registered callback.
6266c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     */
627ec9bad2015c6d3bc91bab66f0824043c1e24d013Jeff Sharkey    public void unregisterSessionCallback(@NonNull SessionCallback callback) {
62816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        synchronized (mDelegates) {
62916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            for (Iterator<SessionCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
63016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                final SessionCallbackDelegate delegate = i.next();
63116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                if (delegate.mCallback == callback) {
63216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                    try {
63316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                        mInstaller.unregisterCallback(delegate);
63416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                    } catch (RemoteException e) {
63516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                        throw e.rethrowAsRuntimeException();
63616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                    }
63716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                    i.remove();
63816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                }
63916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            }
6403a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
6413a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    }
6423a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
6433a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    /**
6443a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey     * An installation that is being actively staged. For an install to succeed,
6453a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey     * all existing and new packages must have identical package names, version
6463a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey     * codes, and signing certificates.
6473a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey     * <p>
6483a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey     * A session may contain any number of split packages. If the application
6493a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey     * does not yet exist, this session must include a base package.
6503a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey     * <p>
65116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     * If an APK included in this session is already defined by the existing
65216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     * installation (for example, the same split name), the APK in this session
65316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey     * will replace the existing APK.
6543a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey     */
655ec55ef0934b8e0d1bb705434947de817f7be57f1Jeff Sharkey    public static class Session implements Closeable {
6563a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        private IPackageInstallerSession mSession;
6573a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
6583a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        /** {@hide} */
6593a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        public Session(IPackageInstallerSession session) {
6603a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            mSession = session;
6613a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
6623a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
663bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        /** {@hide} */
664bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        @Deprecated
665bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        public void setProgress(float progress) {
666bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey            setStagingProgress(progress);
667bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        }
668bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey
6696c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey        /**
670bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * Set current progress of staging this session. Valid values are
671bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * anywhere between 0 and 1.
672bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * <p>
673bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * Note that this progress may not directly correspond to the value
674bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * reported by {@link SessionCallback#onProgressChanged(int, float)}, as
675bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * the system may carve out a portion of the overall progress to
676bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * represent its own internal installation work.
6776c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         */
678bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        public void setStagingProgress(float progress) {
6793a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            try {
680a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey                mSession.setClientProgress(progress);
681a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey            } catch (RemoteException e) {
682a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey                throw e.rethrowAsRuntimeException();
683a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey            }
684a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey        }
685a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey
686a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey        /** {@hide} */
68716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public void addProgress(float progress) {
688a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey            try {
689a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey                mSession.addClientProgress(progress);
6903a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            } catch (RemoteException e) {
6913a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey                throw e.rethrowAsRuntimeException();
6923a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            }
6933a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
6943a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
69578cc340c2de873d6995c283b777476f7237d690fJeff Sharkey        /**
69616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * Open a stream to write an APK file into the session.
69716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * <p>
69816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * The returned stream will start writing data at the requested offset
69916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * in the underlying file, which can be used to resume a partially
70016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * written file. If a valid file length is specified, the system will
70116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * preallocate the underlying disk space to optimize placement on disk.
70216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * It's strongly recommended to provide a valid file length when known.
70316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * <p>
70416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * You can write data into the returned stream, optionally call
70516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * {@link #fsync(OutputStream)} as needed to ensure bytes have been
70616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * persisted to disk, and then close when finished. All streams must be
707a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * closed before calling {@link #commit(IntentSender)}.
70816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         *
70916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * @param name arbitrary, unique name of your choosing to identify the
71016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         *            APK being written. You can open a file again for
71116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         *            additional writes (such as after a reboot) by using the
71216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         *            same name. This name is only meaningful within the context
71316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         *            of a single install session.
71416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * @param offsetBytes offset into the file to begin writing at, or 0 to
71516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         *            start at the beginning of the file.
71616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * @param lengthBytes total size of the file being written, used to
71716c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         *            preallocate the underlying disk space, or -1 if unknown.
71877d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *            The system may clear various caches as needed to allocate
71977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *            this space.
72077d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         * @throws IOException if trouble opening the file for writing, such as
72177d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *             lack of disk space or unavailable media.
72277d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         * @throws SecurityException if called after the session has been
72377d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *             committed or abandoned.
72478cc340c2de873d6995c283b777476f7237d690fJeff Sharkey         */
72516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public @NonNull OutputStream openWrite(@NonNull String name, long offsetBytes,
72616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                long lengthBytes) throws IOException {
7273a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            try {
72816c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                final ParcelFileDescriptor clientSocket = mSession.openWrite(name,
72978cc340c2de873d6995c283b777476f7237d690fJeff Sharkey                        offsetBytes, lengthBytes);
7309a1507aa10577badabcbe00396613a967302e456Jeff Sharkey                return new FileBridge.FileBridgeOutputStream(clientSocket);
731a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey            } catch (RuntimeException e) {
732a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey                ExceptionUtils.maybeUnwrapIOException(e);
733a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey                throw e;
7343a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            } catch (RemoteException e) {
7351cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey                throw e.rethrowAsRuntimeException();
7363a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            }
7373a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
7383a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
7396c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey        /**
7406c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * Ensure that any outstanding data for given stream has been committed
7416c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * to disk. This is only valid for streams returned from
7426c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * {@link #openWrite(String, long, long)}.
7436c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         */
74416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public void fsync(@NonNull OutputStream out) throws IOException {
745a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey            if (out instanceof FileBridge.FileBridgeOutputStream) {
746a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey                ((FileBridge.FileBridgeOutputStream) out).fsync();
747a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey            } else {
748a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey                throw new IllegalArgumentException("Unrecognized stream");
749a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey            }
750a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey        }
751a10311434778ea1be1621c2251c0c8c2966f337bJeff Sharkey
7526c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey        /**
753a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Return all APK names contained in this session.
7541cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * <p>
7551cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * This returns all names which have been previously written through
7561cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * {@link #openWrite(String, long, long)} as part of this session.
75777d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *
75877d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         * @throws SecurityException if called after the session has been
75977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *             committed or abandoned.
7601cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         */
761742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey        public @NonNull String[] getNames() throws IOException {
7621cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey            try {
763a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                return mSession.getNames();
764742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey            } catch (RuntimeException e) {
765742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey                ExceptionUtils.maybeUnwrapIOException(e);
766742e790294b3441b79f715fe447069b63c6065dbJeff Sharkey                throw e;
7671cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey            } catch (RemoteException e) {
7681cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey                throw e.rethrowAsRuntimeException();
7691cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey            }
7701cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey        }
7711cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey
7721cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey        /**
7731cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * Open a stream to read an APK file from the session.
7741cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * <p>
7751cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * This is only valid for names which have been previously written
7761cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * through {@link #openWrite(String, long, long)} as part of this
7771cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * session. For example, this stream may be used to calculate a
7781cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         * {@link MessageDigest} of a written APK before committing.
77977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *
78077d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         * @throws SecurityException if called after the session has been
78177d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *             committed or abandoned.
7821cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey         */
7831cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey        public @NonNull InputStream openRead(@NonNull String name) throws IOException {
7841cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey            try {
7851cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey                final ParcelFileDescriptor pfd = mSession.openRead(name);
7861cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey                return new ParcelFileDescriptor.AutoCloseInputStream(pfd);
7871cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey            } catch (RuntimeException e) {
7881cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey                ExceptionUtils.maybeUnwrapIOException(e);
7891cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey                throw e;
7901cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey            } catch (RemoteException e) {
7911cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey                throw e.rethrowAsRuntimeException();
7921cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey            }
7931cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey        }
7941cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey
7951cb2d0d4bba387665128c62c342e59103ea4be26Jeff Sharkey        /**
7966c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * Attempt to commit everything staged in this session. This may require
7976c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * user intervention, and so it may not happen immediately. The final
7986c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * result of the commit will be reported through the given callback.
7996c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * <p>
8006c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * Once this method is called, no additional mutations may be performed
8016c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * on the session. If the device reboots before the session has been
8026c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * finalized, you may commit the session again.
80377d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *
80477d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         * @throws SecurityException if streams opened through
80577d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         *             {@link #openWrite(String, long, long)} are still open.
8066c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         */
807a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void commit(@NonNull IntentSender statusReceiver) {
8083a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            try {
809a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                mSession.commit(statusReceiver);
8103a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            } catch (RemoteException e) {
8113a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey                throw e.rethrowAsRuntimeException();
8123a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            }
8133a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
8143a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
8156c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey        /**
8166c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * Release this session object. You can open the session again if it
8176c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         * hasn't been finalized.
8186c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         */
819ec55ef0934b8e0d1bb705434947de817f7be57f1Jeff Sharkey        @Override
8203a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        public void close() {
82116c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            try {
82216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                mSession.close();
82316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            } catch (RemoteException e) {
82416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                throw e.rethrowAsRuntimeException();
82516c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey            }
8263a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
8273a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey
8286c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey        /**
82916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * Completely abandon this session, destroying all staged data and
83077d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         * rendering it invalid. Abandoned sessions will be reported to
83177d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         * {@link SessionCallback} listeners as failures. This is equivalent to
83277d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey         * opening the session and calling {@link Session#abandon()}.
8336c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         */
83416c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey        public void abandon() {
8353a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            try {
83616c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey                mSession.abandon();
8373a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            } catch (RemoteException e) {
8383a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey                throw e.rethrowAsRuntimeException();
8393a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey            }
8403a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey        }
8413a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey    }
842bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
8436c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey    /**
844a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * Parameters for creating a new {@link PackageInstaller.Session}.
8456c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     */
846a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public static class SessionParams implements Parcelable {
847a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
848a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
849a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public static final int MODE_INVALID = -1;
850a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
851a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /**
852a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Mode for an install session whose staged APKs should fully replace any
853a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * existing APKs for the target app.
854a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         */
855a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public static final int MODE_FULL_INSTALL = 1;
856a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
857a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /**
858a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Mode for an install session that should inherit any existing APKs for the
859a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * target app, unless they have been explicitly overridden (based on split
860a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * name) by the session. For example, this can be used to add one or more
861a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * split APKs to an existing installation.
862a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * <p>
863a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * If there are no existing APKs for the target app, this behaves like
864a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * {@link #MODE_FULL_INSTALL}.
865a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         */
866a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public static final int MODE_INHERIT_EXISTING = 2;
867a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
868a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
869a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public int mode = MODE_INVALID;
870a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
871a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public int installFlags;
872a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
873a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public int installLocation = PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY;
874a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
875a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public long sizeBytes = -1;
876a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
877a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public String appPackageName;
878a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
879a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public Bitmap appIcon;
880a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
881a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public String appLabel;
882a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
88302bd78490d8594d225ecc70a74b2058cb968a657Jeff Sharkey        public long appIconLastModified = -1;
88402bd78490d8594d225ecc70a74b2058cb968a657Jeff Sharkey        /** {@hide} */
885a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public Uri originatingUri;
886a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
887a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public Uri referrerUri;
888a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
889a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public String abiOverride;
890a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
891fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey        /**
892a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Construct parameters for a new package install session.
893a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         *
894a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * @param mode one of {@link #MODE_FULL_INSTALL} or
895a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         *            {@link #MODE_INHERIT_EXISTING} describing how the session
896a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         *            should interact with an existing app.
897f06009542390472872da986486d385001e91a2a7Jeff Sharkey         */
898a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public SessionParams(int mode) {
899a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            this.mode = mode;
900a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
901a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
902a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
903a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public SessionParams(Parcel source) {
904a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            mode = source.readInt();
905a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            installFlags = source.readInt();
906a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            installLocation = source.readInt();
907a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            sizeBytes = source.readLong();
908a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            appPackageName = source.readString();
909a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            appIcon = source.readParcelable(null);
910a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            appLabel = source.readString();
911a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            originatingUri = source.readParcelable(null);
912a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            referrerUri = source.readParcelable(null);
913a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            abiOverride = source.readString();
914a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
915f06009542390472872da986486d385001e91a2a7Jeff Sharkey
916f06009542390472872da986486d385001e91a2a7Jeff Sharkey        /**
917a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Provide value of {@link PackageInfo#installLocation}, which may be used
918a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * to determine where the app will be staged. Defaults to
919a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * {@link PackageInfo#INSTALL_LOCATION_INTERNAL_ONLY}.
920f06009542390472872da986486d385001e91a2a7Jeff Sharkey         */
921a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void setInstallLocation(int installLocation) {
922a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            this.installLocation = installLocation;
923a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
924f06009542390472872da986486d385001e91a2a7Jeff Sharkey
925f06009542390472872da986486d385001e91a2a7Jeff Sharkey        /**
926a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Optionally indicate the total size (in bytes) of all APKs that will be
927a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * delivered in this session. The system may use this to ensure enough disk
928a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * space exists before proceeding, or to estimate container size for
929a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * installations living on external storage.
930a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         *
931a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * @see PackageInfo#INSTALL_LOCATION_AUTO
932a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * @see PackageInfo#INSTALL_LOCATION_PREFER_EXTERNAL
933f06009542390472872da986486d385001e91a2a7Jeff Sharkey         */
934a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void setSize(long sizeBytes) {
935a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            this.sizeBytes = sizeBytes;
936a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
937f06009542390472872da986486d385001e91a2a7Jeff Sharkey
938f06009542390472872da986486d385001e91a2a7Jeff Sharkey        /**
939a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Optionally set the package name of the app being installed. It's strongly
940a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * recommended that you provide this value when known, so that observers can
941a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * communicate installing apps to users.
942fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey         * <p>
943a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * If the APKs staged in the session aren't consistent with this package
944a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * name, the install will fail. Regardless of this value, all APKs in the
945a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * app must have the same package name.
946fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey         */
947a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void setAppPackageName(@Nullable String appPackageName) {
948a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            this.appPackageName = appPackageName;
949a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
950fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey
951a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /**
952a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Optionally set an icon representing the app being installed. This should
953a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * be roughly {@link ActivityManager#getLauncherLargeIconSize()} in both
954a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * dimensions.
955a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         */
956a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void setAppIcon(@Nullable Bitmap appIcon) {
957a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            this.appIcon = appIcon;
958a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
959bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
960a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /**
961a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Optionally set a label representing the app being installed.
962a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         */
963a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void setAppLabel(@Nullable CharSequence appLabel) {
964a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            this.appLabel = (appLabel != null) ? appLabel.toString() : null;
965a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
966bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
967a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /**
968a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Optionally set the URI where this package was downloaded from. Used for
969a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * verification purposes.
970a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         *
971a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * @see Intent#EXTRA_ORIGINATING_URI
972a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         */
973a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void setOriginatingUri(@Nullable Uri originatingUri) {
974a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            this.originatingUri = originatingUri;
975bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        }
976bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
977a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /**
978a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Optionally set the URI that referred you to install this package. Used
979a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * for verification purposes.
980a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         *
981a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * @see Intent#EXTRA_REFERRER
982a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         */
983a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void setReferrerUri(@Nullable Uri referrerUri) {
984a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            this.referrerUri = referrerUri;
985a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
986a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
987a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
98877d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey        public void setInstallFlagsInternal() {
98977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey            installFlags |= PackageManager.INSTALL_INTERNAL;
99077d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey            installFlags &= ~PackageManager.INSTALL_EXTERNAL;
99177d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey        }
99277d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey
99377d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey        /** {@hide} */
99477d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey        public void setInstallFlagsExternal() {
99577d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey            installFlags |= PackageManager.INSTALL_EXTERNAL;
99677d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey            installFlags &= ~PackageManager.INSTALL_INTERNAL;
99777d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey        }
99877d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey
99977d218e1869e69c8d436b09cd11dcfe45e50b2cfJeff Sharkey        /** {@hide} */
1000a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void dump(IndentingPrintWriter pw) {
1001a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printPair("mode", mode);
1002a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printHexPair("installFlags", installFlags);
1003a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printPair("installLocation", installLocation);
1004a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printPair("sizeBytes", sizeBytes);
1005a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printPair("appPackageName", appPackageName);
1006a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printPair("appIcon", (appIcon != null));
1007a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printPair("appLabel", appLabel);
1008a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printPair("originatingUri", originatingUri);
1009a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printPair("referrerUri", referrerUri);
1010a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.printPair("abiOverride", abiOverride);
1011a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            pw.println();
1012fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey        }
1013fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey
1014fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey        @Override
1015a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public int describeContents() {
1016a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return 0;
1017bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        }
1018a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1019a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        @Override
1020a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void writeToParcel(Parcel dest, int flags) {
1021a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeInt(mode);
1022a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeInt(installFlags);
1023a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeInt(installLocation);
1024a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeLong(sizeBytes);
1025a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeString(appPackageName);
1026a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeParcelable(appIcon, flags);
1027a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeString(appLabel);
1028a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeParcelable(originatingUri, flags);
1029a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeParcelable(referrerUri, flags);
1030a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeString(abiOverride);
1031a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
1032a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1033a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public static final Parcelable.Creator<SessionParams>
1034a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                CREATOR = new Parcelable.Creator<SessionParams>() {
1035a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    @Override
1036a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    public SessionParams createFromParcel(Parcel p) {
1037a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                        return new SessionParams(p);
1038a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    }
1039a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1040a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    @Override
1041a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    public SessionParams[] newArray(int size) {
1042a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                        return new SessionParams[size];
1043a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    }
1044a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                };
1045bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey    }
1046bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
10476c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey    /**
1048a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey     * Details for an active install session.
10496c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey     */
1050a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey    public static class SessionInfo implements Parcelable {
1051a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1052a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1053a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public int sessionId;
1054a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1055a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public String installerPackageName;
1056a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1057a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public String resolvedBaseCodePath;
1058a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1059a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public float progress;
1060a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1061a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public boolean sealed;
1062a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1063bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        public boolean active;
1064a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1065a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1066a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public int mode;
1067a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1068a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public long sizeBytes;
1069a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1070a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public String appPackageName;
1071a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1072a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public Bitmap appIcon;
1073a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1074a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public CharSequence appLabel;
1075a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1076a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1077a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public SessionInfo() {
1078a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
1079a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1080a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /** {@hide} */
1081a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public SessionInfo(Parcel source) {
1082a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            sessionId = source.readInt();
1083a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            installerPackageName = source.readString();
1084a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            resolvedBaseCodePath = source.readString();
1085a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            progress = source.readFloat();
1086a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            sealed = source.readInt() != 0;
1087bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey            active = source.readInt() != 0;
1088a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1089a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            mode = source.readInt();
1090a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            sizeBytes = source.readLong();
1091a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            appPackageName = source.readString();
1092a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            appIcon = source.readParcelable(null);
1093a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            appLabel = source.readString();
1094a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
1095a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
10966c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey        /**
1097a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Return the ID for this session.
10986c833e07a05c48ca60ee4d72421bf8b1e78dc710Jeff Sharkey         */
1099a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public int getSessionId() {
1100a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return sessionId;
1101a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
1102bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
1103bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        /**
1104a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Return the package name of the app that owns this session.
1105bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey         */
1106a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public @Nullable String getInstallerPackageName() {
1107a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return installerPackageName;
1108a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
1109bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
1110bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        /**
1111a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Return current overall progress of this session, between 0 and 1.
111216c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey         * <p>
1113bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * Note that this progress may not directly correspond to the value
1114bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * reported by
1115bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * {@link PackageInstaller.Session#setStagingProgress(float)}, as the
1116bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * system may carve out a portion of the overall progress to represent
1117bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * its own internal installation work.
1118bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey         */
1119a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public float getProgress() {
1120a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return progress;
1121a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
1122bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
1123bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        /**
1124bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * Return if this session is currently active.
1125bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * <p>
1126bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * A session is considered active whenever there is ongoing forward
1127bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * progress being made, such as the installer holding an open
1128bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * {@link Session} instance while streaming data into place, or the
1129bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * system optimizing code as the result of
1130bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * {@link Session#commit(IntentSender)}.
1131bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * <p>
1132bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * If the installer closes the {@link Session} without committing, the
1133bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * session is considered inactive until the installer opens the session
1134bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey         * again.
1135bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey         */
1136bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        public boolean isActive() {
1137bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey            return active;
1138bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        }
1139bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey
1140bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        /** {@hide} */
1141bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey        @Deprecated
1142a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public boolean isOpen() {
1143bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey            return isActive();
1144a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
1145bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
1146bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        /**
1147a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Return the package name this session is working with. May be {@code null}
1148a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * if unknown.
1149bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey         */
1150a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public @Nullable String getAppPackageName() {
1151a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return appPackageName;
1152a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
115316c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
11547328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey        /**
1155a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Return an icon representing the app being installed. May be {@code null}
1156a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * if unavailable.
11577328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey         */
1158a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public @Nullable Bitmap getAppIcon() {
1159a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return appIcon;
1160a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
11617328a1b39b3dae1c0cd390c0a3695c6a46b8e9d8Jeff Sharkey
1162a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        /**
1163a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Return a label representing the app being installed. May be {@code null}
1164a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * if unavailable.
1165a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         */
1166a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public @Nullable CharSequence getAppLabel() {
1167a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return appLabel;
1168a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
116916c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
1170fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey        /**
1171a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * Return an Intent that can be started to view details about this install
1172a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * session. This may surface actions such as pause, resume, or cancel.
1173fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey         * <p>
1174a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * In some cases, a matching Activity may not exist, so ensure you safeguard
1175a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * against this.
1176a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         *
1177a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey         * @see PackageInstaller#ACTION_SESSION_DETAILS
1178fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey         */
1179de74231f245c90e5861ec84a9880b5b4ec247480Jeff Sharkey        public @Nullable Intent createDetailsIntent() {
1180a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            final Intent intent = new Intent(PackageInstaller.ACTION_SESSION_DETAILS);
1181a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId);
1182a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            intent.setPackage(installerPackageName);
1183a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1184a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return intent;
1185bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        }
1186bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey
1187de74231f245c90e5861ec84a9880b5b4ec247480Jeff Sharkey        /** {@hide} */
1188de74231f245c90e5861ec84a9880b5b4ec247480Jeff Sharkey        @Deprecated
1189de74231f245c90e5861ec84a9880b5b4ec247480Jeff Sharkey        public @Nullable Intent getDetailsIntent() {
1190de74231f245c90e5861ec84a9880b5b4ec247480Jeff Sharkey            return createDetailsIntent();
1191de74231f245c90e5861ec84a9880b5b4ec247480Jeff Sharkey        }
1192de74231f245c90e5861ec84a9880b5b4ec247480Jeff Sharkey
1193bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey        @Override
1194a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public int describeContents() {
1195a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            return 0;
1196fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey        }
1197fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey
1198fbd0e9fa37fc17ccd25e4c1f16195bbd27de3c4cJeff Sharkey        @Override
1199a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public void writeToParcel(Parcel dest, int flags) {
1200a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeInt(sessionId);
1201a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeString(installerPackageName);
1202a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeString(resolvedBaseCodePath);
1203a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeFloat(progress);
1204a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeInt(sealed ? 1 : 0);
1205bc7bce38b2e4733a14f6296c75f983bd50f996d1Jeff Sharkey            dest.writeInt(active ? 1 : 0);
1206a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1207a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeInt(mode);
1208a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeLong(sizeBytes);
1209a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeString(appPackageName);
1210a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeParcelable(appIcon, flags);
1211a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey            dest.writeString(appLabel != null ? appLabel.toString() : null);
1212a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        }
1213a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey
1214a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey        public static final Parcelable.Creator<SessionInfo>
1215a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                CREATOR = new Parcelable.Creator<SessionInfo>() {
1216a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    @Override
1217a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    public SessionInfo createFromParcel(Parcel p) {
1218a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                        return new SessionInfo(p);
1219a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    }
122016c8e3f49497b6046972ae650772f65768366be8Jeff Sharkey
1221a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    @Override
1222a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    public SessionInfo[] newArray(int size) {
1223a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                        return new SessionInfo[size];
1224a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                    }
1225a0907436c01fd8c545a6b5c7b28bc3bc9db59270Jeff Sharkey                };
1226bb580670350b76fa2fcc5ee873f99b7970759cbfJeff Sharkey    }
12273a44f3f1b446315ef894e01d2ab9b5388c2bd8c4Jeff Sharkey}
1228