Skip to content

Instantly share code, notes, and snippets.

View gb103's full-sized avatar

Gaurav Bansal gb103

View GitHub Profile
@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.
@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 / 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 / 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 / UserConfirmation.kt
Created January 2, 2021 04:23
To handle user confirmation on dynamic feature download dialog
override fun onSessionStateUpdate(state: SplitInstallSessionState) {
if (state.status() == SplitInstallSessionStatus.REQUIRES_USER_CONFIRMATION) {
// Displays a dialog for the user to either “Download”
// or “Cancel” the request.
splitInstallManager.startConfirmationDialogForResult(
state,
/* activity = */ this,
// You use this request code to later retrieve the user's decision.
/* requestCode = */ MY_REQUEST_CODE)
}
@gb103
gb103 / CancelRequest.kt
Created January 2, 2021 04:25
to cancel install request
SplitInstallManager
// Cancels the request for the given session ID.
.cancelInstall(mySessionId)
@gb103
gb103 / AndroidManifest.xml
Created January 2, 2021 04:27
manifest entry to support split compat modules
<application
...
android:name="com.google.android.play.core.splitcompat.SplitCompatApplication">
</application>
@gb103
gb103 / MyApplication.kt
Created January 2, 2021 04:29
Add entry into application class to install split compat features.
class MyApplication : SplitCompatApplication() {
...
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
// Emulates installation of future on demand modules using SplitCompat.
SplitCompat.install(this)
}
@gb103
gb103 / AccessInstallModule.kt
Created January 2, 2021 04:30
Access installed module
// Generate a new context as soon as a request for a new module
// reports as INSTALLED.
override fun onStateUpdate(state: SplitInstallSessionState ) {
if (state.sessionId() == mySessionId) {
when (state.status()) {
...
SplitInstallSessionStatus.INSTALLED -> {
val newContext = context.createPackageContext(context.packageName, 0)
// If you use AssetManager to access your app’s raw asset files, you’ll need
// to generate a new AssetManager instance from the updated context.
@gb103
gb103 / BasicUtility.kt
Created January 2, 2021 04:31
Basic utility methods
//To check if a module is already installed
val installedModules: Set<String> = splitInstallManager.installedModules
//To Uninstall modules
splitInstallManager.deferredUninstall(listOf("pictureMessages", "promotionalFilters"))