This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SplitInstallManager | |
// Cancels the request for the given session ID. | |
.cancelInstall(mySessionId) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<application | |
... | |
android:name="com.google.android.play.core.splitcompat.SplitCompatApplication"> | |
</application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyApplication : SplitCompatApplication() { | |
... | |
} | |
override fun attachBaseContext(base: Context) { | |
super.attachBaseContext(base) | |
// Emulates installation of future on demand modules using SplitCompat. | |
SplitCompat.install(this) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//To check if a module is already installed | |
val installedModules: Set<String> = splitInstallManager.installedModules | |
//To Uninstall modules | |
splitInstallManager.deferredUninstall(listOf("pictureMessages", "promotionalFilters")) |
OlderNewer