/
DynamicFeatureProgress.kt
189 lines (181 loc) · 7.6 KB
/
DynamicFeatureProgress.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright © 2020 - present | Handwashing reminder by Javinator9889
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*
* Created by Javinator9889 on 2/04/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.activities
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.format.Formatter
import android.widget.Toast
import com.google.android.play.core.ktx.bytesDownloaded
import com.google.android.play.core.ktx.errorCode
import com.google.android.play.core.ktx.totalBytesToDownload
import com.google.android.play.core.splitinstall.SplitInstallHelper
import com.google.android.play.core.splitinstall.SplitInstallRequest
import com.google.android.play.core.splitinstall.SplitInstallSessionState
import com.google.android.play.core.splitinstall.SplitInstallStateUpdatedListener
import com.google.android.play.core.splitinstall.model.SplitInstallSessionStatus
import com.google.firebase.analytics.FirebaseAnalytics
import com.javinator9889.handwashingreminder.BuildConfig
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.activities.base.SplitCompatBaseActivity
import com.javinator9889.handwashingreminder.utils.AndroidVersion
import com.javinator9889.handwashingreminder.utils.CONFIRMATION_REQUEST_CODE
import com.javinator9889.handwashingreminder.utils.filterNotEmpty
import com.javinator9889.handwashingreminder.utils.isAtLeast
import kotlinx.android.synthetic.main.dynamic_content_pb.*
import timber.log.Timber
class DynamicFeatureProgress : SplitCompatBaseActivity(),
SplitInstallStateUpdatedListener {
companion object {
const val MODULES = "modules"
const val LAUNCH_ON_INSTALL = "modules:launch_on_install"
const val PACKAGE_NAME = "modules:package_name"
const val CLASS_NAME = "modules:class_name"
}
private var moduleCount = 0
private var currentModule = 1
private var launchOnInstall = false
private lateinit var modules: Array<String>
private lateinit var launchActivityName: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
splitInstallManager.registerListener(this)
with(FirebaseAnalytics.getInstance(this)) {
setCurrentScreen(
this@DynamicFeatureProgress, "Dynamic module", null
)
}
modules =
intent.getStringArrayExtra(MODULES) ?: throw NullPointerException(
"An array of modules must be provided for " +
"instantiating this class"
)
launchOnInstall = intent.getBooleanExtra(LAUNCH_ON_INSTALL, false)
if (launchOnInstall) {
val packageName = intent.getStringExtra(PACKAGE_NAME)
val className = intent.getStringExtra(CLASS_NAME)
launchActivityName = "$packageName.$className"
}
val installRequestBuilder = SplitInstallRequest.newBuilder()
modules.filterNotEmpty().forEach { module ->
if (module !in splitInstallManager.installedModules) {
installRequestBuilder.addModule(module)
moduleCount++
}
}
if (moduleCount > 0 && modules.isNotEmpty()) {
setContentView(R.layout.dynamic_content_pb)
overridePendingTransition(android.R.anim.fade_in, 0)
val installRequest = installRequestBuilder.build()
splitInstallManager.startInstall(installRequest)
} else {
setResultWithIntent(Activity.RESULT_OK)
finish()
}
}
private fun setResultWithIntent(resultCode: Int) {
val intent = if (launchOnInstall)
Intent().setClassName(
BuildConfig.APPLICATION_ID,
launchActivityName
)
else
null
setResult(resultCode, intent)
}
override fun finish() {
splitInstallManager.unregisterListener(this)
super.finish()
}
@SuppressLint("SetTextI18n")
override fun onStateUpdate(state: SplitInstallSessionState?) {
when (state?.status()) {
SplitInstallSessionStatus.FAILED -> {
Toast.makeText(
this, getString(
R.string.dynamic_module_loading_error, state.errorCode),
Toast.LENGTH_LONG
).show()
Timber.e(
"Installation failed - error code: ${state.errorCode}"
)
}
SplitInstallSessionStatus.REQUIRES_USER_CONFIRMATION -> {
splitInstallManager
.startConfirmationDialogForResult(
state,
this,
CONFIRMATION_REQUEST_CODE
)
}
SplitInstallSessionStatus.CANCELED -> {
setResultWithIntent(Activity.RESULT_CANCELED)
finish()
}
SplitInstallSessionStatus.PENDING -> {
install_progress.isIndeterminate = true
percentage.text = getString(R.string.preparing)
}
SplitInstallSessionStatus.DOWNLOADING -> {
val downloadedBytes =
Formatter.formatFileSize(this, state.bytesDownloaded)
val bytesToDownload =
Formatter.formatFileSize(this, state.totalBytesToDownload)
bytesInfo.text = "$downloadedBytes / $bytesToDownload"
install_progress.isIndeterminate = false
install_progress.max = state.totalBytesToDownload.toInt()
val progress = state.bytesDownloaded.toInt()
if (isAtLeast(AndroidVersion.N))
install_progress.setProgress(progress, true)
else
install_progress.progress = progress
val currentPercentage =
(state.bytesDownloaded * 100 / state.totalBytesToDownload)
.toInt()
percentage.text = "$currentPercentage %"
}
SplitInstallSessionStatus.INSTALLING -> {
install_progress.isIndeterminate = true
bytesInfo.text = ""
percentage.text = getString(R.string.installing)
}
SplitInstallSessionStatus.INSTALLED -> {
SplitInstallHelper.updateAppInfo(this)
if (++currentModule >= moduleCount) {
dynamic_content_title.text = getString(R.string.done)
setResultWithIntent(Activity.RESULT_OK)
finish()
}
}
else -> return
}
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CONFIRMATION_REQUEST_CODE) {
setResultWithIntent(Activity.RESULT_CANCELED)
finish()
}
}
}