/
LauncherActivity.kt
321 lines (306 loc) · 12.6 KB
/
LauncherActivity.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* 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 23/03/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.activities
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenCreated
import androidx.lifecycle.whenStarted
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.play.core.splitcompat.SplitCompat
import com.google.android.play.core.splitinstall.SplitInstallManagerFactory
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.ktx.Firebase
import com.google.firebase.perf.FirebasePerformance
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.application.HandwashingApplication
import com.javinator9889.handwashingreminder.emoji.EmojiLoader
import com.javinator9889.handwashingreminder.gms.ads.AdLoader
import com.javinator9889.handwashingreminder.gms.ads.AdsEnabler
import com.javinator9889.handwashingreminder.gms.vendor.BillingService
import com.javinator9889.handwashingreminder.jobs.workers.WorkHandler
import com.javinator9889.handwashingreminder.utils.*
import com.javinator9889.handwashingreminder.utils.Preferences.Companion.ADS_ENABLED
import com.javinator9889.handwashingreminder.utils.Preferences.Companion.APP_INIT_KEY
import com.javinator9889.handwashingreminder.utils.RemoteConfig.Keys.SPECIAL_EVENT
import com.mikepenz.iconics.Iconics
import javinator9889.localemanager.utils.languagesupport.LanguagesSupport
import kotlinx.android.synthetic.main.splash_screen.*
import kotlinx.coroutines.*
import org.conscrypt.Conscrypt
import timber.log.Timber
import java.security.Security
import java.util.*
import kotlin.collections.ArrayList
import com.javinator9889.handwashingreminder.utils.Firebase as FirebaseConf
internal const val FAST_START_KEY = "intent:fast_start"
internal const val PENDING_INTENT_CODE = 201
class LauncherActivity : AppCompatActivity() {
private var launchOnInstall = false
private var launchFromNotification = false
private var canFinishActivity = false
private lateinit var app: HandwashingApplication
private lateinit var initDeferred: Deferred<Unit>
init {
lifecycleScope.launch {
whenCreated {
app = HandwashingApplication.getInstance()
with(intent) {
notNull {
launchFromNotification =
it.getBooleanExtra(FAST_START_KEY, false)
}
}
initDeferred = async { initVariables() }
}
whenStarted {
try {
withContext(Dispatchers.Main) { displayWelcomeScreen() }
withContext(Dispatchers.Main) { installRequiredModules() }
} finally {
initDeferred.await()
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.splash_screen)
}
private suspend fun displayWelcomeScreen() {
app.firebaseInitDeferred.await()
val isThereAnySpecialEvent = with(Firebase.remoteConfig) {
getBoolean(SPECIAL_EVENT) && !launchFromNotification
}
var sleepDuration = 0L
var animationLoaded = false
val fadeInAnimation =
AnimationUtils.loadAnimation(this, android.R.anim.fade_in)
fadeInAnimation.duration = 300L
fadeInAnimation.setAnimationListener(object :
Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {}
override fun onAnimationRepeat(animation: Animation?) {}
override fun onAnimationEnd(animation: Animation?) {
logo.playAnimation()
animationLoaded = true
}
})
if (isThereAnySpecialEvent) {
logo.setAnimation(AnimatedResources.STAY_SAFE_STAY_HOME.res)
logo.enableMergePathsForKitKatAndAbove(true)
logo.addLottieOnCompositionLoadedListener {
logo.startAnimation(fadeInAnimation)
sleepDuration = logo.duration
}
while (!animationLoaded)
delay(10L)
delay(sleepDuration)
} else {
logo.setImageResource(R.drawable.handwashing_app_logo)
logo.startAnimation(fadeInAnimation)
}
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == DYNAMIC_FEATURE_INSTALL_RESULT_CODE) {
EmojiLoader.get(this)
if (app.sharedPreferences.getBoolean(ADS_ENABLED, true)) {
when (resultCode) {
Activity.RESULT_OK -> {
initAds()
data.notNull {
createPackageContext(packageName, 0).also {
SplitCompat.install(it)
}
if (launchFromNotification)
data!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(data)
finish()
}
}
Activity.RESULT_CANCELED -> app.adLoader = null
}
}
if (!launchOnInstall) {
Intent(this, MainActivity::class.java).also {
if (launchFromNotification)
it.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(it)
overridePendingTransition(0, android.R.anim.fade_out)
}
}
if (canFinishActivity)
finish()
else
canFinishActivity = true
}
}
override fun finish() {
Timber.d("Calling finish")
super.finish()
}
private fun installRequiredModules() {
val modules = ArrayList<String>(MODULE_COUNT)
val googleApi = GoogleApiAvailability.getInstance()
if (app.sharedPreferences.getBoolean(ADS_ENABLED, true))
modules += Ads.MODULE_NAME
if (!app.sharedPreferences.getBoolean(APP_INIT_KEY, false)) {
modules += AppIntro.MODULE_NAME
launchOnInstall = true
}
/*modules += if (isAtLeast(AndroidVersion.LOLLIPOP))
OkHttp.MODULE_NAME
else
OkHttpLegacy.MODULE_NAME*/
if (googleApi.isGooglePlayServicesAvailable(
this,
GOOGLE_PLAY_SERVICES_MIN_VERSION
) != ConnectionResult.SUCCESS
)
modules += BundledEmoji.MODULE_NAME
else
with(SplitInstallManagerFactory.create(this)) {
deferredUninstall(listOf(BundledEmoji.MODULE_NAME))
}
modules.trimToSize()
val intent = if (launchOnInstall) {
createDynamicFeatureActivityIntent(
modules.toTypedArray(),
launchOnInstall,
AppIntro.MAIN_ACTIVITY_NAME,
AppIntro.PACKAGE_NAME
)
} else {
createDynamicFeatureActivityIntent(modules.toTypedArray())
}
startActivityForResult(intent, DYNAMIC_FEATURE_INSTALL_RESULT_CODE)
}
private fun initAds() {
val className = "${Ads.PACKAGE_NAME}.${Ads
.CLASS_NAME}\$${Ads.PROVIDER_NAME}"
val adProvider = Class.forName(className).kotlin
.objectInstance as AdLoader.Provider
app.adLoader = adProvider.instance(app)
val adsEnabler = AdsEnabler(app)
adsEnabler.enableAds()
}
private fun createDynamicFeatureActivityIntent(
modules: Array<String>,
launchOnInstall: Boolean = false,
className: String = "",
packageName: String = ""
): Intent = Intent(this, DynamicFeatureProgress::class.java).also {
it.putExtra(DynamicFeatureProgress.MODULES, modules)
it.putExtra(DynamicFeatureProgress.LAUNCH_ON_INSTALL, launchOnInstall)
it.putExtra(DynamicFeatureProgress.CLASS_NAME, className)
it.putExtra(DynamicFeatureProgress.PACKAGE_NAME, packageName)
}
private suspend fun initVariables() {
app.firebaseInitDeferred.await()
Timber.d("Firebase initialized correctly")
Timber.d("Initializing Iconics")
Iconics.init(this)
Timber.d("Setting-up security providers")
Security.insertProviderAt(Conscrypt.newProvider(), 1)
Timber.d("Setting-up activity recognition")
if (app.sharedPreferences.getBoolean(
Preferences.ACTIVITY_TRACKING_ENABLED, false
) && with(GoogleApiAvailability.getInstance()) {
isGooglePlayServicesAvailable(this@LauncherActivity) ==
ConnectionResult.SUCCESS
}
) {
app.activityHandler.startTrackingActivity()
} else {
app.activityHandler.disableActivityTracker()
}
Timber.d("Initializing Billing Service")
app.billingService = BillingService(this)
with(WorkHandler(this)) {
enqueuePeriodicNotificationsWorker()
}
Timber.d("Adding periodic notifications if not enqueued yet")
Timber.d("Setting-up Firebase custom properties")
setupFirebaseProperties()
}
private fun setupFirebaseProperties() {
val firebaseAnalytics = FirebaseAnalytics.getInstance(this)
val firebaseRemoteConfig = Firebase.remoteConfig
val firebasePerformance = FirebasePerformance.getInstance()
val config = with(FirebaseRemoteConfigSettings.Builder()) {
minimumFetchIntervalInSeconds = 10
fetchTimeoutInSeconds = 5
build()
}
with(firebaseRemoteConfig) {
Timber.d("Initializing Firebase Remote Config")
setConfigSettingsAsync(config)
setDefaultsAsync(
when (Locale.getDefault().language) {
Locale(LanguagesSupport.Language.SPANISH).language -> {
firebaseAnalytics.setUserProperty(
FirebaseConf.Properties.LANGUAGE,
LanguagesSupport.Language.SPANISH
)
R.xml.remote_config_defaults_es
}
else -> {
firebaseAnalytics.setUserProperty(
FirebaseConf.Properties.LANGUAGE,
LanguagesSupport.Language.ENGLISH
)
R.xml.remote_config_defaults
}
}
)
fetchAndActivate().addOnSuccessListener {
if (canFinishActivity)
finish()
else
canFinishActivity = true
}
}
firebaseAnalytics.setAnalyticsCollectionEnabled(
app.sharedPreferences.getBoolean(
Preferences.ANALYTICS_ENABLED,
true
)
)
firebasePerformance.isPerformanceCollectionEnabled =
app.sharedPreferences.getBoolean(
Preferences.PERFORMANCE_ENABLED,
true
)
Timber.d("Performance enabled: ${firebasePerformance.isPerformanceCollectionEnabled}")
}
}