Session
open class Session : Closeable
kotlin.Any | |
↳ | android.content.pm.PackageInstaller.Session |
An installation that is being actively staged. For an install to succeed, all existing and new packages must have identical package names, version codes, and signing certificates.
A session may contain any number of split packages. If the application does not yet exist, this session must include a base package.
If an APK included in this session is already defined by the existing installation (for example, the same split name), the APK in this session will replace the existing APK.
In such a case that multiple packages need to be committed simultaneously, multiple sessions can be referenced by a single multi-package session. This session is created with no package name and calling SessionParams#setMultiPackage()
. The individual session IDs can be added with addChildSessionId(int)
and commit of the multi-package session will result in all child sessions being committed atomically.
Summary
Public methods | |
---|---|
open Unit |
abandon() Completely abandon this session, destroying all staged data and rendering it invalid. |
open Unit |
addChildSessionId(sessionId: Int) Adds a session ID to the set of sessions that will be committed atomically when this session is committed. |
open Unit |
close() Release this session object. |
open Unit |
commit(statusReceiver: IntentSender) Attempt to commit everything staged in this session. |
open Unit |
fsync(out: OutputStream) Ensure that any outstanding data for given stream has been committed to disk. |
open PersistableBundle | |
open IntArray | |
open Array<String!> |
getNames() Return all APK names contained in this session. |
open Int | |
open Boolean | |
open Boolean | |
open Boolean | |
open Boolean |
isStaged() |
open InputStream |
Open a stream to read an APK file from the session. |
open OutputStream |
Open a stream to write an APK file into the session. |
open Unit |
removeChildSessionId(sessionId: Int) Removes a session ID from the set of sessions that will be committed atomically when this session is committed. |
open Unit |
removeSplit(splitName: String) Removes a split. |
open Unit |
requestChecksums(name: String, required: Int, trustedInstallers: MutableList<Certificate!>, executor: Executor, onChecksumsReadyListener: PackageManager.OnChecksumsReadyListener) Requests checksums for the APK file in session. |
open Unit |
requestUserPreapproval(details: PackageInstaller.PreapprovalDetails, statusReceiver: IntentSender) Attempt to request the approval before committing this session. |
open Unit |
setAppMetadata(data: PersistableBundle?) Optionally set the app metadata. |
open Unit |
setChecksums(name: String, checksums: MutableList<Checksum!>, signature: ByteArray?) Sets installer-provided checksums for the APK file in session. |
open Unit |
setStagingProgress(progress: Float) Set current progress of staging this session. |
open Unit |
Transfer the session to a new owner. |
Public methods
abandon
open fun abandon(): Unit
Completely abandon this session, destroying all staged data and rendering it invalid. Abandoned sessions will be reported to SessionCallback
listeners as failures. This is equivalent to abandonSession(int)
.
If the parent is abandoned, all children will also be abandoned. Any written data would be destroyed and the created Session
information will be discarded.
addChildSessionId
open fun addChildSessionId(sessionId: Int): Unit
Adds a session ID to the set of sessions that will be committed atomically when this session is committed.
If the parent is staged or has rollback enabled, all children must have the same properties.
If the parent is abandoned, all children will also be abandoned.
Parameters | |
---|---|
sessionId |
Int: the session ID to add to this multi-package session. |
close
open fun close(): Unit
Release this session object. You can open the session again if it hasn't been finalized.
Exceptions | |
---|---|
java.lang.Exception |
if this resource cannot be closed |
java.io.IOException |
if an I/O error occurs |
commit
open fun commit(statusReceiver: IntentSender): Unit
Attempt to commit everything staged in this session. This may require user intervention, and so it may not happen immediately. The final result of the commit will be reported through the given callback.
Once this method is called, the session is sealed and no additional mutations may be performed on the session. In case of device reboot or data loader transient failure before the session has been finalized, you may commit the session again.
If the installer is the device owner, the affiliated profile owner, or has received user pre-approval of this session, there will be no user intervention.
Parameters | |
---|---|
statusReceiver |
IntentSender: Called when the state of the session changes. Intents sent to this receiver contain EXTRA_STATUS . Refer to the individual status codes on how to handle them. This value cannot be null . |
Exceptions | |
---|---|
java.lang.SecurityException |
if streams opened through openWrite(java.lang.String,long,long) are still open. |
java.lang.IllegalArgumentException |
if the statusReceiver from an immutable android.app.PendingIntent when caller has a target SDK of API version 35 or above. |
fsync
open fun fsync(out: OutputStream): Unit
Ensure that any outstanding data for given stream has been committed to disk. This is only valid for streams returned from openWrite(java.lang.String,long,long)
.
Parameters | |
---|---|
out |
OutputStream: This value cannot be null . |
getAppMetadata
open fun getAppMetadata(): PersistableBundle
Return | |
---|---|
PersistableBundle |
A PersistableBundle containing the app metadata set with Session#setAppMetadata(PersistableBundle) . In the case where this data does not exist, an empty PersistableBundle is returned. This value cannot be null . |
getChildSessionIds
open fun getChildSessionIds(): IntArray
Return | |
---|---|
IntArray |
the set of session IDs that will be committed atomically when this session is committed if this is a multi-package session or null if none exist. |
getNames
open fun getNames(): Array<String!>
Return all APK names contained in this session.
This returns all names which have been previously written through openWrite(java.lang.String,long,long)
as part of this session.
Return | |
---|---|
Array<String!> |
This value cannot be null . |
Exceptions | |
---|---|
java.lang.SecurityException |
if called after the session has been abandoned. |
getParentSessionId
open fun getParentSessionId(): Int
Return | |
---|---|
Int |
the session ID of the multi-package session that this belongs to or SessionInfo#INVALID_ID if it does not belong to a multi-package session. |
isApplicationEnabledSettingPersistent
open fun isApplicationEnabledSettingPersistent(): Boolean
Return | |
---|---|
Boolean |
true if this session will keep the existing application enabled setting after installation. |
isMultiPackage
open fun isMultiPackage(): Boolean
Return | |
---|---|
Boolean |
true if this session will commit more than one package when it is committed. |
isRequestUpdateOwnership
open fun isRequestUpdateOwnership(): Boolean
Return | |
---|---|
Boolean |
true if the installer requested the update ownership enforcement for the packages in this session. |
isStaged
open fun isStaged(): Boolean
Return | |
---|---|
Boolean |
true if this session will be staged and applied at next reboot. |
openRead
open fun openRead(name: String): InputStream
Open a stream to read an APK file from the session.
This is only valid for names which have been previously written through openWrite(java.lang.String,long,long)
as part of this session. For example, this stream may be used to calculate a MessageDigest
of a written APK before committing.
Parameters | |
---|---|
name |
String: This value cannot be null . |
Return | |
---|---|
InputStream |
This value cannot be null . |
Exceptions | |
---|---|
java.lang.SecurityException |
if called after the session has been committed or abandoned. |
openWrite
open fun openWrite(
name: String,
offsetBytes: Long,
lengthBytes: Long
): OutputStream
Open a stream to write an APK file into the session.
The returned stream will start writing data at the requested offset in the underlying file, which can be used to resume a partially written file. If a valid file length is specified, the system will preallocate the underlying disk space to optimize placement on disk. It's strongly recommended to provide a valid file length when known.
You can write data into the returned stream, optionally call fsync(java.io.OutputStream)
as needed to ensure bytes have been persisted to disk, and then close when finished. All streams must be closed before calling commit(android.content.IntentSender)
.
Parameters | |
---|---|
name |
String: arbitrary, unique name of your choosing to identify the APK being written. You can open a file again for additional writes (such as after a reboot) by using the same name. This name is only meaningful within the context of a single install session. This value cannot be null . |
offsetBytes |
Long: offset into the file to begin writing at, or 0 to start at the beginning of the file. |
lengthBytes |
Long: total size of the file being written, used to preallocate the underlying disk space, or -1 if unknown. The system may clear various caches as needed to allocate this space. |
Return | |
---|---|
OutputStream |
This value cannot be null . |
Exceptions | |
---|---|
java.io.IOException |
if trouble opening the file for writing, such as lack of disk space or unavailable media. |
java.lang.SecurityException |
if called after the session has been sealed or abandoned |
removeChildSessionId
open fun removeChildSessionId(sessionId: Int): Unit
Removes a session ID from the set of sessions that will be committed atomically when this session is committed.
Parameters | |
---|---|
sessionId |
Int: the session ID to remove from this multi-package session. |
removeSplit
open fun removeSplit(splitName: String): Unit
Removes a split.
Split removals occur prior to adding new APKs. If upgrading a feature split, it is not expected nor desirable to remove the split prior to upgrading.
When split removal is bundled with new APKs, the packageName must be identical.
Parameters | |
---|---|
splitName |
String: This value cannot be null . |
requestChecksums
open fun requestChecksums(
name: String,
required: Int,
trustedInstallers: MutableList<Certificate!>,
executor: Executor,
onChecksumsReadyListener: PackageManager.OnChecksumsReadyListener
): Unit
Requests checksums for the APK file in session.
A possible use case is replying to Intent#ACTION_PACKAGE_NEEDS_VERIFICATION
broadcast. The checksums will be returned asynchronously via onChecksumsReadyListener.
By default returns all readily available checksums:
- enforced by platform,
- enforced by the installer.
Caution: Android can not verify installer-provided checksums. Make sure you specify trusted installers.
Parameters | |
---|---|
name |
String: previously written as part of this session. openWrite This value cannot be null . |
required |
Int: to explicitly request the checksum types. Will incur significant CPU/memory/disk usage. Value is either 0 or a combination of android.content.pm.Checksum#TYPE_WHOLE_MERKLE_ROOT_4K_SHA256 , android.content.pm.Checksum#TYPE_WHOLE_MD5 , android.content.pm.Checksum#TYPE_WHOLE_SHA1 , android.content.pm.Checksum#TYPE_WHOLE_SHA256 , android.content.pm.Checksum#TYPE_WHOLE_SHA512 , android.content.pm.Checksum#TYPE_PARTIAL_MERKLE_ROOT_1M_SHA256 , and android.content.pm.Checksum#TYPE_PARTIAL_MERKLE_ROOT_1M_SHA512 |
trustedInstallers |
MutableList<Certificate!>: for checksums enforced by installer, which installers are to be trusted. PackageManager#TRUST_ALL will return checksums from any installer, PackageManager#TRUST_NONE disables optimized installer-enforced checksums, otherwise the list has to be a non-empty list of certificates. This value cannot be null . |
executor |
Executor: the Executor on which to invoke the callback This value cannot be null . Callback and listener events are dispatched through this Executor , providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor() . Otherwise, provide an Executor that dispatches to an appropriate thread. |
onChecksumsReadyListener |
PackageManager.OnChecksumsReadyListener: called once when the results are available. This value cannot be null . |
Exceptions | |
---|---|
java.security.cert.CertificateEncodingException |
if an encoding error occurs for trustedInstallers. |
java.io.FileNotFoundException |
if the file does not exist. |
java.lang.IllegalArgumentException |
if the list of trusted installer certificates is empty. |
requestUserPreapproval
open fun requestUserPreapproval(
details: PackageInstaller.PreapprovalDetails,
statusReceiver: IntentSender
): Unit
Attempt to request the approval before committing this session. For installers that have been granted the REQUEST_INSTALL_PACKAGES
permission, they can request the approval from users before Session#commit(IntentSender)
is called. This may require user intervention as well. When user intervention is required, installers will receive a STATUS_PENDING_USER_ACTION
callback, and STATUS_SUCCESS
otherwise. In case that requesting user pre-approval is not available, installers will receive STATUS_FAILURE_BLOCKED
instead. Note that if the users decline the request, this session will be abandoned. If user intervention is required but never resolved, or requesting user pre-approval is not available, you may still call Session#commit(IntentSender)
as the typical installation.
Parameters | |
---|---|
details |
PackageInstaller.PreapprovalDetails: the adequate context to this session for requesting the approval from users prior to commit. This value cannot be null . |
statusReceiver |
IntentSender: called when the state of the session changes. Intents sent to this receiver contain EXTRA_STATUS and the EXTRA_PRE_APPROVAL would be true . Refer to the individual status codes on how to handle them. This value cannot be null . |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if IntentSender is null . |
java.lang.IllegalStateException |
if called again after this method has been called on this session. |
java.lang.SecurityException |
if called after the session has been committed or abandoned. |
setAppMetadata
open fun setAppMetadata(data: PersistableBundle?): Unit
Optionally set the app metadata. The size of this data cannot exceed the maximum allowed. Any existing data from the previous install will not be retained even if no data is set for the current install session. Setting data to null or an empty PersistableBundle will remove any metadata that has previously been set in the same session.
Parameters | |
---|---|
data |
PersistableBundle?: a PersistableBundle containing the app metadata. This value may be null . |
Exceptions | |
---|---|
java.io.IOException |
if writing the data fails. |
setChecksums
open funsetChecksums(
name: String,
checksums: MutableList<Checksum!>,
signature: ByteArray?
): Unit
Deprecated: do not use installer-provided checksums, use platform-enforced checksums e.g. Checksum#TYPE_WHOLE_MERKLE_ROOT_4K_SHA256
in PackageManager#requestChecksums
.
Sets installer-provided checksums for the APK file in session.
Parameters | |
---|---|
name |
String: previously written as part of this session. openWrite This value cannot be null . |
checksums |
MutableList<Checksum!>: installer intends to make available via PackageManager#requestChecksums or requestChecksums . This value cannot be null . |
signature |
ByteArray?: DER PKCS#7 detached signature bytes over binary serialized checksums to enable integrity checking for the checksums or null for no integrity checking. PackageManager#requestChecksums will return the certificate used to create signature. Binary format for checksums:
<code>DataOutputStream dos; dos.writeInt(checksum.getType()); dos.writeInt(checksum.getValue().length); dos.write(checksum.getValue());</code>If using openssl cms, make sure to specify -binary -nosmimecap. |
Exceptions | |
---|---|
java.lang.SecurityException |
if called after the session has been committed or abandoned. |
java.lang.IllegalStateException |
if checksums for this file have already been added. |
See Also
setStagingProgress
open fun setStagingProgress(progress: Float): Unit
Set current progress of staging this session. Valid values are anywhere between 0 and 1.
Note that this progress may not directly correspond to the value reported by SessionCallback#onProgressChanged(int, float)
, as the system may carve out a portion of the overall progress to represent its own internal installation work.
transfer
open fun transfer(packageName: String): Unit
Transfer the session to a new owner.
Only sessions that update the installing app can be transferred.
After the transfer to a package with a different uid all method calls on the session will cause SecurityException
s.
Once this method is called, the session is sealed and no additional mutations beside committing it may be performed on the session.
Parameters | |
---|---|
packageName |
String: The package of the new owner. Needs to hold the INSTALL_PACKAGES permission. This value cannot be null . |
Exceptions | |
---|---|
android.content.pm.PackageManager.NameNotFoundException |
if the new owner could not be found. |
java.lang.SecurityException |
if called after the session has been committed or abandoned. |
java.lang.IllegalStateException |
if streams opened through are still open. |