Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented in-app update functionality #105

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions app/src/main/java/com/andryoga/safebox/ui/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.databinding.DataBindingUtil
Expand All @@ -20,8 +21,16 @@ import com.andryoga.safebox.common.Constants.APP_GITHUB_URL
import com.andryoga.safebox.common.CrashlyticsKeys
import com.andryoga.safebox.databinding.ActivityMainBinding
import com.andryoga.safebox.ui.common.Utils.hideSoftKeyboard
import com.andryoga.safebox.ui.view.MainActivity.Constants.AUTO_UPDATE_REQUEST_CODE
import com.andryoga.safebox.ui.view.MainActivity.Constants.LAST_INTERACTED_TIME
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.google.android.play.core.appupdate.AppUpdateManager
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.install.InstallStateUpdatedListener
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.android.play.core.review.ReviewInfo
import com.google.android.play.core.review.ReviewManager
import com.google.android.play.core.review.ReviewManagerFactory
Expand Down Expand Up @@ -55,8 +64,11 @@ class MainActivity : AppCompatActivity() {
object Constants {
const val LAST_INTERACTED_TIME = "last_interacted_time"
const val MAX_USER_AWAY_MILLI_SECONDS = 20000L
const val AUTO_UPDATE_REQUEST_CODE = 11
}

private lateinit var appUpdateManager: AppUpdateManager
Ni3verma marked this conversation as resolved.
Show resolved Hide resolved

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Timber.i("on create activity")
Expand All @@ -65,6 +77,7 @@ class MainActivity : AppCompatActivity() {
// could be USER_PREFERENCE in future
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
appUpdateManager = AppUpdateManagerFactory.create(this)

lastInteractionTime = savedInstanceState?.getLong(LAST_INTERACTED_TIME)
?: System.currentTimeMillis()
Expand Down Expand Up @@ -100,6 +113,44 @@ class MainActivity : AppCompatActivity() {

CrashlyticsKeys(this).setDefaultKeys()
prefetchReviewInfo()

appUpdate()

}

private fun appUpdate(){
Ni3verma marked this conversation as resolved.
Show resolved Hide resolved
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnSuccessListener{ appUpdateInfo ->
if ( appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)){
appUpdateManager.startUpdateFlowForResult(appUpdateInfo,AppUpdateType.FLEXIBLE, this, AUTO_UPDATE_REQUEST_CODE)
appUpdateManager.registerListener(listener)
}
else if ( appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)){
appUpdateManager.startUpdateFlowForResult(appUpdateInfo,AppUpdateType.IMMEDIATE, this, AUTO_UPDATE_REQUEST_CODE)
}
}
}

private val listener = InstallStateUpdatedListener{ state ->
Ni3verma marked this conversation as resolved.
Show resolved Hide resolved
if (state.installStatus() == InstallStatus.DOWNLOADED){
snackbarForCompleteUpdate()
}
}

private fun snackbarForCompleteUpdate(){
Ni3verma marked this conversation as resolved.
Show resolved Hide resolved
Snackbar.make(findViewById(R.id.content), "App update has been downloaded", Snackbar.LENGTH_INDEFINITE).
setAction("Install"){
Ni3verma marked this conversation as resolved.
Show resolved Hide resolved
appUpdateManager.completeUpdate()
}.show()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == AUTO_UPDATE_REQUEST_CODE){
if (resultCode != RESULT_OK )
Toast.makeText(this, "Update Failed", Toast.LENGTH_SHORT).show()
Ni3verma marked this conversation as resolved.
Show resolved Hide resolved
}
}

override fun onPause() {
Expand All @@ -109,6 +160,11 @@ class MainActivity : AppCompatActivity() {

override fun onResume() {
super.onResume()
appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS){
appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, this, AUTO_UPDATE_REQUEST_CODE)
}
}
checkUserAwayTimeout()
}

Expand Down