-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82e6ca0
commit b5f4e62
Showing
6 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
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
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
3 changes: 3 additions & 0 deletions
3
app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateAppException.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.github.odaridavid.weatherapp.ui.update | ||
|
||
data class UpdateAppException(val throwable: Throwable) : Exception() |
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.github.odaridavid.weatherapp.ui.update | ||
|
||
import android.content.Context | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.IntentSenderRequest | ||
import com.github.odaridavid.weatherapp.core.api.Logger | ||
import com.google.android.play.core.appupdate.AppUpdateInfo | ||
import com.google.android.play.core.appupdate.AppUpdateManager | ||
import com.google.android.play.core.appupdate.AppUpdateManagerFactory | ||
import com.google.android.play.core.appupdate.AppUpdateOptions | ||
import com.google.android.play.core.install.model.AppUpdateType | ||
import com.google.android.play.core.install.model.UpdateAvailability | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
|
||
class UpdateManager @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
private val logger: Logger, | ||
private val updateStateFactory: UpdateStateFactory, | ||
) { | ||
|
||
private val appUpdateManager: AppUpdateManager by lazy { | ||
AppUpdateManagerFactory.create(context) | ||
} | ||
|
||
fun checkForUpdates( | ||
activityResultLauncher: ActivityResultLauncher<IntentSenderRequest> | ||
) { | ||
val appUpdateInfoTask = appUpdateManager.appUpdateInfo | ||
|
||
appUpdateManager.registerListener(updateStateFactory.getUpdateStateListener( | ||
onDownloaded = { | ||
// TODO: Notify the user that the update is ready to be installed.Don't do it this way. | ||
appUpdateManager.completeUpdate() | ||
} | ||
)) | ||
|
||
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> | ||
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE | ||
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE) | ||
) { | ||
update( | ||
appUpdateManager = appUpdateManager, | ||
appUpdateInfo = appUpdateInfo, | ||
activityResultLauncher = activityResultLauncher, | ||
) | ||
} | ||
}.addOnFailureListener { exception -> | ||
logger.logException(UpdateAppException(exception)) | ||
} | ||
} | ||
|
||
fun unregisterListeners() { | ||
appUpdateManager.unregisterListener(updateStateFactory.getUpdateStateListener()) | ||
} | ||
|
||
private fun update( | ||
appUpdateManager: AppUpdateManager, | ||
appUpdateInfo: AppUpdateInfo, | ||
activityResultLauncher: ActivityResultLauncher<IntentSenderRequest> | ||
) { | ||
appUpdateManager.startUpdateFlowForResult( | ||
appUpdateInfo, | ||
activityResultLauncher, | ||
AppUpdateOptions.newBuilder(AppUpdateType.FLEXIBLE).build() | ||
) | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateStateFactory.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.github.odaridavid.weatherapp.ui.update | ||
|
||
import com.google.android.play.core.install.InstallStateUpdatedListener | ||
import com.google.android.play.core.install.model.InstallStatus | ||
import javax.inject.Inject | ||
|
||
class UpdateStateFactory @Inject constructor() { | ||
|
||
fun getUpdateStateListener( | ||
onDownloading: ((bytesDownloaded: Long, totalBytesToDownload: Long) -> Unit)? = null, | ||
onDownloaded: (() -> Unit)? = null, | ||
) = InstallStateUpdatedListener { state -> | ||
when (state.installStatus()) { | ||
InstallStatus.DOWNLOADING -> { | ||
val bytesDownloaded = state.bytesDownloaded() | ||
val totalBytesToDownload = state.totalBytesToDownload() | ||
if (onDownloading != null) { | ||
onDownloading(bytesDownloaded, totalBytesToDownload) | ||
} | ||
// Show update progress bar. | ||
} | ||
InstallStatus.DOWNLOADED -> { | ||
// Notify the user that the update is ready to be installed. | ||
if (onDownloaded != null) { | ||
onDownloaded() | ||
} | ||
|
||
} | ||
InstallStatus.INSTALLING, | ||
InstallStatus.INSTALLED, | ||
InstallStatus.FAILED, | ||
InstallStatus.CANCELED, | ||
InstallStatus.PENDING, | ||
InstallStatus.UNKNOWN -> { | ||
// No-op | ||
} | ||
else -> { | ||
// No-op | ||
} | ||
} | ||
} | ||
|
||
} |
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