Skip to content

Instantly share code, notes, and snippets.

View gb103's full-sized avatar

Gaurav Bansal gb103

View GitHub Profile
@gb103
gb103 / HandleStateUpdate.kt
Created January 2, 2021 04:22
Handle downloading state updates.
override fun onStateUpdate(state : SplitInstallSessionState) {
if (state.status() == SplitInstallSessionStatus.FAILED
&& state.errorCode() == SplitInstallErrorCode.SERVICE_DIES) {
// Retry the request.
return
}
if (state.sessionId() == mySessionId) {
when (state.status()) {
SplitInstallSessionStatus.DOWNLOADING -> {
val totalBytes = state.totalBytesToDownload()
@gb103
gb103 / HandlerError.kt
Created January 2, 2021 04:20
Handle error in the dynamic feature download
splitInstallManager
.startInstall(request)
.addOnFailureListener { exception ->
when ((exception as SplitInstallException).errorCode) {
SplitInstallErrorCode.NETWORK_ERROR -> {
// Display a message that requests the user to establish a
// network connection.
}
SplitInstallErrorCode.ACTIVE_SESSIONS_LIMIT_EXCEEDED -> checkForActiveDownloads()
...
@gb103
gb103 / monitor.kt
Last active January 2, 2021 03:49
Monitor the install progress
// Initializes a variable to later track the session ID for a given request.
var mySessionId = 0
// Creates a listener for request status updates.
val listener = SplitInstallStateUpdatedListener { state ->
if (state.sessionId() == mySessionId) {
// Read the status of the request to handle the state update.
}
}
@gb103
gb103 / BaseSplitInstallActivity.kt
Created January 2, 2021 03:43
Activity code for having feature download code
// Creates an instance of SplitInstallManager.
val splitInstallManager = SplitInstallManagerFactory.create(context)
// Creates a request to install a module.
val request =
SplitInstallRequest
.newBuilder()
// You can download multiple on demand modules per
// request by invoking the following method for each
// module you want to install.