1
+ /* *
2
+ *
3
+ * Copyright 2020 David Odari
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6
+ * in compliance with the License. You may obtain a copy of the License at
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
+ * or implied. See the License for the specific language governing permissions and limitations under
11
+ * the License.
12
+ *
13
+ **/
14
+ package com.github.odaridavid.designpatterns
15
+
16
+ import android.app.Activity
17
+ import android.content.Context
18
+ import com.google.android.play.core.appupdate.AppUpdateInfo
19
+ import com.google.android.play.core.appupdate.AppUpdateManager
20
+ import com.google.android.play.core.appupdate.AppUpdateManagerFactory
21
+ import com.google.android.play.core.install.model.AppUpdateType
22
+ import com.google.android.play.core.install.model.UpdateAvailability
23
+
24
+
25
+ internal class InAppUpdateManager (private val context : Context , private val activity : Activity ) {
26
+
27
+ fun checkForUpdate () {
28
+ val appUpdateManager: AppUpdateManager = AppUpdateManagerFactory .create(context)
29
+ val appUpdateInfo = appUpdateManager.appUpdateInfo
30
+ appUpdateInfo?.addOnSuccessListener { info ->
31
+ handleUpdateImmediately(appUpdateManager, info)
32
+ }
33
+ }
34
+
35
+ private fun handleUpdateImmediately (
36
+ appUpdateManager : AppUpdateManager ,
37
+ appUpdateInfo : AppUpdateInfo
38
+ ) {
39
+ val updateAvailability = appUpdateInfo.updateAvailability()
40
+ if ((updateAvailability == UpdateAvailability .UPDATE_AVAILABLE || updateAvailability == UpdateAvailability .DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS ) &&
41
+ appUpdateInfo.isUpdateTypeAllowed(AppUpdateType .IMMEDIATE )
42
+ ) {
43
+ appUpdateManager.startUpdateFlowForResult(
44
+ appUpdateInfo,
45
+ AppUpdateType .IMMEDIATE ,
46
+ activity,
47
+ RQ_REQUEST_UPDATE
48
+ )
49
+ }
50
+ }
51
+
52
+ companion object {
53
+ const val RQ_REQUEST_UPDATE = 4000
54
+ }
55
+ }
0 commit comments