Skip to content

Commit 8b5aba1

Browse files
Added Auto Updater Play
1 parent 1fa5818 commit 8b5aba1

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ dependencies {
4242
implementation 'com.google.android.material:material:1.2.1'
4343
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
4444

45+
//Play Update Manager
46+
implementation 'com.google.android.play:core:1.9.1'
47+
4548
testImplementation 'junit:junit:4.13.1'
4649
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
4750
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

app/src/main/java/com/aaxena/calculator/MainActivity.kt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.aaxena.calculator
22

33
import android.content.Context
4-
import android.content.Intent
4+
import android.content.IntentSender.SendIntentException
55
import android.os.Bundle
66
import android.os.Handler
77
import android.os.Looper
@@ -11,13 +11,22 @@ import android.widget.Toast
1111
import android.widget.Toast.LENGTH_SHORT
1212
import androidx.appcompat.app.AlertDialog
1313
import androidx.appcompat.app.AppCompatActivity
14+
import com.google.android.play.core.appupdate.AppUpdateManager
15+
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
16+
import com.google.android.play.core.install.InstallState
17+
import com.google.android.play.core.install.InstallStateUpdatedListener
18+
import com.google.android.play.core.install.model.AppUpdateType
19+
import com.google.android.play.core.install.model.InstallStatus
20+
import com.google.android.play.core.install.model.UpdateAvailability
1421
import kotlinx.android.synthetic.main.activity_main.*
1522
import net.objecthunter.exp4j.ExpressionBuilder
1623

1724

1825
class MainActivity : AppCompatActivity() {
1926
val SHARED_PREFS = "sharedPrefs"
2027
val TEXT = "text"
28+
private var mAppUpdateManager: AppUpdateManager? = null
29+
private val RC_APP_UPDATE = 11
2130
override fun onCreate(savedInstanceState: Bundle?) {
2231
super.onCreate(savedInstanceState)
2332
setContentView(R.layout.activity_main)
@@ -178,4 +187,31 @@ class MainActivity : AppCompatActivity() {
178187
}
179188
return !ranBefore
180189
}
190+
override fun onStart() {
191+
super.onStart()
192+
mAppUpdateManager = AppUpdateManagerFactory.create(this)
193+
mAppUpdateManager!!.registerListener(installStateUpdatedListener)
194+
mAppUpdateManager!!.getAppUpdateInfo().addOnSuccessListener { appUpdateInfo ->
195+
if (appUpdateInfo.updateAvailability() === UpdateAvailability.UPDATE_AVAILABLE
196+
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE /*AppUpdateType.IMMEDIATE*/)) {
197+
try {
198+
mAppUpdateManager!!.startUpdateFlowForResult(
199+
appUpdateInfo, AppUpdateType.FLEXIBLE /*AppUpdateType.IMMEDIATE*/, this, RC_APP_UPDATE)
200+
} catch (e: SendIntentException) {
201+
e.printStackTrace()
202+
}
203+
} else {
204+
}
205+
}
206+
}
207+
208+
var installStateUpdatedListener: InstallStateUpdatedListener = object : InstallStateUpdatedListener {
209+
override fun onStateUpdate(state: InstallState) {
210+
if (state.installStatus() === InstallStatus.INSTALLED) {
211+
mAppUpdateManager?.unregisterListener(this)
212+
} else {
213+
//App Is Fully Updated Nothing To Do, Continuing Normal WorkFlow but do not erase the else func
214+
}
215+
}
216+
}
181217
}

0 commit comments

Comments
 (0)