Created
December 17, 2019 06:51
-
-
Save yudikarma/2fe259e55ea23ff8061583ec84b0b10f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package es.hyrax.zonapets.ui | |
import android.os.Bundle | |
import androidx.lifecycle.ViewModelProviders | |
import es.hyrax.zonapets.R | |
import es.hyrax.zonapets.ui.base.BaseActivityViewModel | |
import es.hyrax.zonapets.ui.login.LoginActivity | |
import es.hyrax.zonapets.ui.user_private.MainPrivateActivity | |
import com.google.android.play.core.install.InstallStateUpdatedListener | |
import com.google.android.play.core.appupdate.AppUpdateManager | |
import androidx.core.app.ComponentActivity.ExtraData | |
import androidx.core.content.ContextCompat.getSystemService | |
import android.icu.lang.UCharacter.GraphemeClusterBreak.T | |
import com.google.android.play.core.install.model.UpdateAvailability | |
import com.google.android.play.core.install.model.InstallStatus | |
import androidx.core.view.accessibility.AccessibilityEventCompat.setAction | |
import com.google.android.material.snackbar.Snackbar | |
import android.content.IntentSender | |
import com.google.android.play.core.install.model.AppUpdateType | |
import com.google.android.play.core.appupdate.AppUpdateInfo | |
import com.google.android.play.core.install.InstallState | |
import com.google.android.play.core.appupdate.AppUpdateManagerFactory | |
import androidx.core.content.ContextCompat.getSystemService | |
import android.icu.lang.UCharacter.GraphemeClusterBreak.T | |
import android.content.Intent | |
import com.airbnb.lottie.L | |
import androidx.core.content.ContextCompat.getSystemService | |
import android.icu.lang.UCharacter.GraphemeClusterBreak.T | |
import android.app.Activity | |
import android.view.View | |
import es.hyrax.zonapets.utils.Version | |
import kotlinx.android.synthetic.main.activity_edit_profil.* | |
import kotlinx.android.synthetic.main.activity_splash.* | |
import org.jetbrains.anko.* | |
import timber.log.Timber | |
/** | |
* Actiivty for check Is user already login when fisrt open App | |
*/ | |
class SplashActivity : BaseActivityViewModel() { | |
private lateinit var model: SplashActivityViewModel | |
private var appUpdateManager: AppUpdateManager? = null | |
private var updateInfo : AppUpdateInfo? = null | |
private var installStateUpdatedListener: InstallStateUpdatedListener? = null | |
companion object{ | |
const val REQ_CODE_VERSION_UPDATE_FLEKSIBEL = 530 | |
const val REQ_CODE_VERSION_UPDATE_IMMEDIATE = 531 | |
} | |
override fun setupViewModel() { | |
model = ViewModelProviders.of(this, viewModelFactory).get(SplashActivityViewModel::class.java) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_splash) | |
//In app update code. Only works for Android 5.0 and above. | |
if (Version.isLollipop) { | |
checkForAppUpdate() | |
}else{ | |
runningSplashTime() | |
} | |
} | |
private fun runningSplashTime() { | |
val splashTime = 1000 // set waktu 5 detik = 5000 | |
val splashTread = object : Thread() { | |
override fun run() { | |
try { | |
var waited = 0 | |
while (waited < splashTime) { // Splash screen pause time | |
sleep(100) | |
waited += 100 | |
} | |
decideActivityToOpen() | |
} catch (e: InterruptedException) { // do nothing | |
} finally { // do nothing | |
} | |
} | |
} | |
splashTread.start() | |
} | |
override fun onResume() { | |
super.onResume() | |
if (Version.isLollipop) { | |
checkNewAppVersionState() | |
} | |
} | |
override fun onDestroy() { | |
if (Version.isLollipop) { | |
unregisterInstallStateUpdListener() | |
} | |
super.onDestroy() | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
when (requestCode) { | |
REQ_CODE_VERSION_UPDATE_FLEKSIBEL -> if (resultCode !== Activity.RESULT_OK) { //RESULT_OK / RESULT_CANCELED / RESULT_IN_APP_UPDATE_FAILED | |
Timber.d("Update flow failed! Result code: $resultCode") | |
// If the update is cancelled or fails, | |
// you can request to start the update again. | |
unregisterInstallStateUpdListener() | |
//user not wanna update | |
runningSplashTime() | |
} | |
REQ_CODE_VERSION_UPDATE_IMMEDIATE -> if (resultCode !== Activity.RESULT_OK) { //RESULT_OK / RESULT_CANCELED / RESULT_IN_APP_UPDATE_FAILED | |
Timber.d("Update flow failed! Result code: $resultCode") | |
// If the update is cancelled or fails, | |
// you can request to start the update again. | |
unregisterInstallStateUpdListener() | |
//user must update | |
popUpUserMustbeUpdate() | |
} | |
} | |
} | |
private fun popUpUserMustbeUpdate() { | |
Snackbar.make( | |
imageView | |
, | |
"your application needed to update latest version.", | |
Snackbar.LENGTH_LONG | |
).apply { | |
setAction("UPDATE") { checkForAppUpdate() } | |
setActionTextColor(resources.getColor(R.color.colorPrimary)) | |
show() | |
} | |
} | |
private fun decideActivityToOpen() { | |
if (model.isLoggedIn()) { | |
startActivity(intentFor<MainPrivateActivity>().clearTask().clearTop().newTask()) | |
} else { | |
startActivity(intentFor<LoginActivity>().clearTask().clearTop().newTask()) | |
} | |
} | |
private fun checkForAppUpdate() { | |
// Creates instance of the manager. | |
appUpdateManager = AppUpdateManagerFactory.create(this) | |
// Returns an intent object that you use to check for an update. | |
val appUpdateInfoTask = appUpdateManager?.appUpdateInfo | |
// Create a listener to track request state updates. | |
installStateUpdatedListener = InstallStateUpdatedListener { installState -> | |
// Show module progress, log state, or install the update. | |
if (installState.installStatus() == InstallStatus.DOWNLOADED) { | |
// After the update is downloaded, show a notification | |
// and request user confirmation to restart the app. | |
popupSnackbarForCompleteUpdateAndUnregister() | |
} | |
//you cannot move from this activity cause listener at this activity | |
} | |
// Before starting an update, register a listener for updates. | |
appUpdateManager?.registerListener(installStateUpdatedListener) | |
// Checks that the platform will allow the specified type of update. | |
appUpdateInfoTask?.addOnSuccessListener { appUpdateInfo -> | |
this.updateInfo = appUpdateInfo | |
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) { | |
// Request the update. | |
if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) { | |
// Start an update. | |
startAppUpdateFlexible(appUpdateInfo) | |
} else if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) { | |
// Start an update. | |
startAppUpdateImmediate(appUpdateInfo) | |
} | |
}else{ | |
//no update | |
runningSplashTime() | |
} | |
} | |
appUpdateInfoTask?.addOnFailureListener { | |
//runnig splash if have no update or failure | |
runningSplashTime() | |
} | |
} | |
private fun startAppUpdateImmediate(appUpdateInfo: AppUpdateInfo) { | |
try { | |
appUpdateManager?.startUpdateFlowForResult( | |
appUpdateInfo, | |
AppUpdateType.IMMEDIATE, | |
// The current activity making the update request. | |
this, | |
// Include a request code to later monitor this update request. | |
REQ_CODE_VERSION_UPDATE_IMMEDIATE | |
) | |
} catch (e: IntentSender.SendIntentException) { | |
e.printStackTrace() | |
unregisterInstallStateUpdListener() | |
} | |
} | |
private fun startAppUpdateFlexible(appUpdateInfo: AppUpdateInfo) { | |
try { | |
appUpdateManager?.startUpdateFlowForResult( | |
appUpdateInfo, | |
AppUpdateType.FLEXIBLE, | |
// The current activity making the update request. | |
this, | |
// Include a request code to later monitor this update request. | |
REQ_CODE_VERSION_UPDATE_FLEKSIBEL | |
) | |
} catch (e: IntentSender.SendIntentException) { | |
e.printStackTrace() | |
unregisterInstallStateUpdListener() | |
} | |
} | |
/** | |
* Displays the snackbar notification and call to action. | |
* Needed only for Flexible app update | |
*/ | |
private fun popupSnackbarForCompleteUpdateAndUnregister() { | |
Snackbar.make(imageView | |
, | |
"An update has just been downloaded.", | |
Snackbar.LENGTH_LONG | |
).apply { | |
setAction("INSTALL") { | |
appUpdateManager?.completeUpdate() | |
unregisterInstallStateUpdListener() | |
} | |
setActionTextColor(resources.getColor(R.color.colorPrimary)) | |
show() | |
} | |
//appUpdateManager?.completeUpdate() | |
} | |
/** | |
* Checks that the update is not stalled during 'onResume()'. | |
* However, you should execute this check at all app entry points. | |
*/ | |
private fun checkNewAppVersionState() { | |
appUpdateManager | |
?.getAppUpdateInfo() | |
?.addOnSuccessListener { appUpdateInfo -> | |
this.updateInfo = appUpdateInfo | |
//FLEXIBLE: | |
// If the update is downloaded but not installed, | |
// notify the user to complete the update. | |
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) { | |
//popupSnackbarForCompleteUpdateAndUnregister() | |
appUpdateManager?.completeUpdate() | |
}else if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) { | |
//IMMEDIATE: | |
if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) { | |
// If an in-app update is already running, resume the update. | |
startAppUpdateImmediate(appUpdateInfo) | |
} | |
} | |
} | |
?.addOnFailureListener { | |
toast("${it.message}") | |
runningSplashTime() | |
} | |
} | |
/** | |
* Needed only for FLEXIBLE update | |
*/ | |
private fun unregisterInstallStateUpdListener() { | |
if (appUpdateManager != null && installStateUpdatedListener != null) | |
appUpdateManager?.unregisterListener(installStateUpdatedListener) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment