forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In-App update implemented with Remote config.
- Loading branch information
1 parent
f0e1e3b
commit ec385b4
Showing
12 changed files
with
313 additions
and
8 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
12 changes: 12 additions & 0 deletions
12
app/src/generic/java/com/nmc.android.appupdate/InAppUpdateHelperImpl.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,12 @@ | ||
package com.nmc.android.appupdate | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class InAppUpdateHelperImpl(private val activity: AppCompatActivity) : InAppUpdateHelper { | ||
|
||
override fun onResume() { | ||
} | ||
|
||
override fun onDestroy() { | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/generic/java/com/nmc/android/remoteconfig/RemoteConfigInit.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,6 @@ | ||
package com.nmc.android.remoteconfig | ||
|
||
/** | ||
* class to fetch and activate remote config for the app update feature | ||
*/ | ||
class RemoteConfigInit |
166 changes: 166 additions & 0 deletions
166
app/src/gplay/java/com/nmc/android/appupdate/InAppUpdateHelperImpl.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,166 @@ | ||
package com.nmc.android.appupdate | ||
|
||
import android.app.Activity | ||
import androidx.activity.result.ActivityResult | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.IntentSenderRequest | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.google.android.material.snackbar.Snackbar | ||
import com.google.android.play.core.appupdate.AppUpdateInfo | ||
import com.google.android.play.core.appupdate.AppUpdateManagerFactory | ||
import com.google.android.play.core.appupdate.AppUpdateOptions | ||
import com.google.android.play.core.install.InstallState | ||
import com.google.android.play.core.install.InstallStateUpdatedListener | ||
import com.google.android.play.core.install.model.ActivityResult.RESULT_IN_APP_UPDATE_FAILED | ||
import com.google.android.play.core.install.model.AppUpdateType | ||
import com.google.android.play.core.install.model.InstallStatus | ||
import com.google.android.play.core.install.model.UpdateAvailability | ||
import com.google.firebase.ktx.Firebase | ||
import com.google.firebase.remoteconfig.ktx.get | ||
import com.google.firebase.remoteconfig.ktx.remoteConfig | ||
import com.nmc.android.remoteconfig.RemoteConfigInit.Companion.APP_VERSION_KEY | ||
import com.nmc.android.remoteconfig.RemoteConfigInit.Companion.FORCE_UPDATE_KEY | ||
import com.owncloud.android.R | ||
import com.owncloud.android.lib.common.utils.Log_OC | ||
import com.owncloud.android.utils.DisplayUtils | ||
|
||
class InAppUpdateHelperImpl(private val activity: AppCompatActivity) : InAppUpdateHelper, InstallStateUpdatedListener { | ||
|
||
companion object { | ||
private val TAG = InAppUpdateHelperImpl::class.java.simpleName | ||
} | ||
|
||
private val remoteConfig = Firebase.remoteConfig | ||
private val isForceUpdate = remoteConfig[FORCE_UPDATE_KEY].asBoolean() | ||
private val appVersionCode = remoteConfig[APP_VERSION_KEY].asLong() | ||
|
||
private val appUpdateManager = AppUpdateManagerFactory.create(activity) | ||
|
||
@AppUpdateType | ||
private var updateType = if (isForceUpdate) AppUpdateType.IMMEDIATE else AppUpdateType.FLEXIBLE | ||
|
||
init { | ||
Log_OC.d(TAG, "App Update Remote Config Values : Force Update- $isForceUpdate -- Version Code- $appVersionCode") | ||
|
||
val appUpdateInfoTask = appUpdateManager.appUpdateInfo | ||
|
||
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> | ||
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) { | ||
Log_OC.d(TAG, "App update is available.") | ||
|
||
// if app version in remote config is not equal to the latest app version code in play store | ||
// then do the flexible update instead of reading the value from remote config | ||
if (appUpdateInfo.availableVersionCode() != appVersionCode.toInt()) { | ||
Log_OC.d( | ||
TAG, | ||
"Available app version code mismatch with remote config. Setting update type to optional." | ||
) | ||
updateType = AppUpdateType.FLEXIBLE | ||
} | ||
|
||
if (appUpdateInfo.isUpdateTypeAllowed(updateType)) { | ||
// Request the update. | ||
startAppUpdate( | ||
appUpdateInfo, | ||
updateType | ||
) | ||
} | ||
} else { | ||
Log_OC.d(TAG, "No app update available.") | ||
} | ||
} | ||
} | ||
|
||
private fun startAppUpdate( | ||
appUpdateInfo: AppUpdateInfo, | ||
@AppUpdateType updateType: Int | ||
) { | ||
|
||
if (updateType == AppUpdateType.FLEXIBLE) { | ||
// Before starting an update, register a listener for updates. | ||
appUpdateManager.registerListener(this) | ||
} | ||
|
||
Log_OC.d(TAG, "App update dialog showing to the user.") | ||
|
||
appUpdateManager.startUpdateFlowForResult( | ||
appUpdateInfo, | ||
appUpdateResultLauncher, | ||
AppUpdateOptions.newBuilder(updateType).build() | ||
) | ||
} | ||
|
||
private val appUpdateResultLauncher: ActivityResultLauncher<IntentSenderRequest> = | ||
activity.registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result: ActivityResult -> | ||
when (result.resultCode) { | ||
Activity.RESULT_OK -> { | ||
Log_OC.d(TAG, "The user has accepted to download the update or the download finished.") | ||
} | ||
|
||
Activity.RESULT_CANCELED -> { | ||
Log_OC.e(TAG, "Update flow failed: The user has denied or canceled the update.") | ||
} | ||
|
||
RESULT_IN_APP_UPDATE_FAILED -> { | ||
Log_OC.e( | ||
TAG, | ||
"Update flow failed: Some other error prevented either the user from providing consent or the update from proceeding." | ||
) | ||
} | ||
} | ||
|
||
} | ||
|
||
private fun flexibleUpdateDownloadCompleted() { | ||
DisplayUtils.createSnackbar( | ||
activity.findViewById(android.R.id.content), | ||
R.string.app_update_downloaded, | ||
Snackbar.LENGTH_INDEFINITE | ||
).apply { | ||
setAction(R.string.common_restart) { appUpdateManager.completeUpdate() } | ||
show() | ||
} | ||
} | ||
|
||
override fun onResume() { | ||
appUpdateManager | ||
.appUpdateInfo | ||
.addOnSuccessListener { appUpdateInfo: AppUpdateInfo -> | ||
// for AppUpdateType.IMMEDIATE only, already executing updater | ||
if (updateType == AppUpdateType.IMMEDIATE) { | ||
if (appUpdateInfo.updateAvailability() | ||
== UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS | ||
) { | ||
Log_OC.d(TAG, "Resume the Immediate update if in-app update is already running.") | ||
// If an in-app update is already running, resume the update. | ||
startAppUpdate( | ||
appUpdateInfo, | ||
AppUpdateType.IMMEDIATE | ||
) | ||
} | ||
} else if (updateType == AppUpdateType.FLEXIBLE) { | ||
// If the update is downloaded but not installed, | ||
// notify the user to complete the update. | ||
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) { | ||
Log_OC.d(TAG, "Resume: Flexible update is downloaded but not installed. User is notified.") | ||
flexibleUpdateDownloadCompleted() | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onDestroy() { | ||
appUpdateManager.unregisterListener(this) | ||
} | ||
|
||
override fun onStateUpdate(state: InstallState) { | ||
if (state.installStatus() == InstallStatus.DOWNLOADED) { | ||
Log_OC.d(TAG, "Flexible update is downloaded. User is notified to restart the app.") | ||
|
||
// After the update is downloaded, notifying user via snackbar | ||
// and request user confirmation to restart the app. | ||
flexibleUpdateDownloadCompleted() | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
app/src/gplay/java/com/nmc/android/remoteconfig/RemoteConfigInit.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,57 @@ | ||
package com.nmc.android.remoteconfig | ||
|
||
import com.google.firebase.ktx.Firebase | ||
import com.google.firebase.remoteconfig.ktx.remoteConfig | ||
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings | ||
import com.owncloud.android.BuildConfig | ||
import com.owncloud.android.R | ||
import com.owncloud.android.lib.common.utils.Log_OC | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* class to fetch and activate remote config for the app update feature | ||
*/ | ||
class RemoteConfigInit { | ||
|
||
companion object { | ||
private val TAG = RemoteConfigInit::class.java.simpleName | ||
|
||
const val FORCE_UPDATE_KEY = "android_force_update" | ||
const val APP_VERSION_KEY = "android_app_version" | ||
|
||
private const val INTERVAL_FOR_DEVELOPMENT = 0L //0 sec for immediate update | ||
|
||
// by default the sync value is 12 hours which is not required in our case | ||
// as we will be only using this for app update and since the app updates are done in few months | ||
// so fetching the data in 1 day | ||
private val INTERVAL_FOR_PROD = TimeUnit.DAYS.toSeconds(1) //1 day | ||
|
||
private fun getMinimumTimeToFetchConfigs(): Long { | ||
return if (BuildConfig.DEBUG) INTERVAL_FOR_DEVELOPMENT else INTERVAL_FOR_PROD | ||
} | ||
} | ||
|
||
private val remoteConfig = Firebase.remoteConfig | ||
|
||
init { | ||
val configSettings = remoteConfigSettings { | ||
minimumFetchIntervalInSeconds = getMinimumTimeToFetchConfigs() | ||
} | ||
remoteConfig.setConfigSettingsAsync(configSettings) | ||
remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults) | ||
|
||
fetchAndActivateConfigs() | ||
} | ||
|
||
private fun fetchAndActivateConfigs() { | ||
remoteConfig.fetchAndActivate() | ||
.addOnCompleteListener { task -> | ||
if (task.isSuccessful) { | ||
val updated = task.result | ||
Log_OC.d(TAG, "Config params updated: $updated\nFetch and activate succeeded.") | ||
} else { | ||
Log_OC.e(TAG, "Fetch failed.") | ||
} | ||
} | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/nmc/android/appupdate/InAppUpdateHelper.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,15 @@ | ||
package com.nmc.android.appupdate | ||
|
||
interface InAppUpdateHelper { | ||
/** | ||
* function should be called from activity onResume | ||
* to check if the update is downloaded or still in progress | ||
*/ | ||
fun onResume() | ||
|
||
/** | ||
* function should be called from activity onDestroy | ||
* this will unregister the update listener attached for Flexible update | ||
*/ | ||
fun onDestroy() | ||
} |
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
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
Oops, something went wrong.