forked from openedx/openedx-app-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppActivity.kt
More file actions
281 lines (242 loc) · 10.4 KB
/
AppActivity.kt
File metadata and controls
281 lines (242 loc) · 10.4 KB
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
package org.openedx.app
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.window.layout.WindowMetricsCalculator
import com.braze.support.toStringMap
import io.branch.referral.Branch
import io.branch.referral.Branch.BranchUniversalReferralInitListener
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.openedx.app.databinding.ActivityAppBinding
import org.openedx.app.deeplink.DeepLink
import org.openedx.auth.presentation.logistration.LogistrationFragment
import org.openedx.auth.presentation.signin.SignInFragment
import org.openedx.core.data.storage.CorePreferences
import org.openedx.core.presentation.dialog.downloaddialog.DownloadDialogManager
import org.openedx.core.presentation.global.InsetHolder
import org.openedx.core.presentation.global.WindowSizeHolder
import org.openedx.core.utils.Logger
import org.openedx.core.worker.CalendarSyncScheduler
import org.openedx.foundation.extension.requestApplyInsetsWhenAttached
import org.openedx.foundation.presentation.WindowSize
import org.openedx.foundation.presentation.WindowType
import org.openedx.profile.presentation.ProfileRouter
import org.openedx.whatsnew.WhatsNewManager
import org.openedx.whatsnew.presentation.whatsnew.WhatsNewFragment
class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder {
override val topInset: Int
get() = _insetTop
override val bottomInset: Int
get() = _insetBottom
override val cutoutInset: Int
get() = _insetCutout
override val windowSize: WindowSize
get() = _windowSize
private lateinit var binding: ActivityAppBinding
private val viewModel by viewModel<AppViewModel>()
private val whatsNewManager by inject<WhatsNewManager>()
private val corePreferencesManager by inject<CorePreferences>()
private val profileRouter by inject<ProfileRouter>()
private val downloadDialogManager by inject<DownloadDialogManager>()
private val calendarSyncScheduler by inject<CalendarSyncScheduler>()
private val branchLogger = Logger(BRANCH_TAG)
private var _insetTop = 0
private var _insetBottom = 0
private var _insetCutout = 0
private var _windowSize = WindowSize(WindowType.Compact, WindowType.Compact)
private val authCode: String?
get() {
val data = intent?.data
return null
}
private val branchCallback =
BranchUniversalReferralInitListener { branchUniversalObject, _, error ->
if (branchUniversalObject?.contentMetadata?.customMetadata != null) {
branchLogger.i { "Branch init complete." }
branchLogger.i { branchUniversalObject.contentMetadata.customMetadata.toString() }
viewModel.makeExternalRoute(
fm = supportFragmentManager,
deepLink = DeepLink(branchUniversalObject.contentMetadata.customMetadata)
)
} else if (error != null) {
branchLogger.e { "Branch init failed. Caused by -" + error.message }
}
}
override fun onSaveInstanceState(outState: Bundle) {
outState.putInt(TOP_INSET, topInset)
outState.putInt(BOTTOM_INSET, bottomInset)
outState.putInt(CUTOUT_INSET, cutoutInset)
super.onSaveInstanceState(outState)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
binding = ActivityAppBinding.inflate(layoutInflater)
lifecycle.addObserver(viewModel)
viewModel.logAppLaunchEvent()
setContentView(binding.root)
setupWindowInsets(savedInstanceState)
setupWindowSettings()
setupInitialFragment(savedInstanceState)
observeLogoutEvent()
observeDownloadFailedDialog()
calendarSyncScheduler.scheduleDailySync()
}
private fun setupWindowInsets(savedInstanceState: Bundle?) {
val container = binding.rootLayout
container.addView(object : View(this) {
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
computeWindowSizeClasses()
}
})
computeWindowSizeClasses()
savedInstanceState?.let {
_insetTop = it.getInt(TOP_INSET, 0)
_insetBottom = it.getInt(BOTTOM_INSET, 0)
_insetCutout = it.getInt(CUTOUT_INSET, 0)
}
binding.root.setOnApplyWindowInsetsListener { _, insets ->
val insetsCompat = WindowInsetsCompat.toWindowInsetsCompat(insets)
.getInsets(WindowInsetsCompat.Type.systemBars())
_insetTop = insetsCompat.top
_insetBottom = insetsCompat.bottom
val displayCutout = WindowInsetsCompat.toWindowInsetsCompat(insets).displayCutout
if (displayCutout != null) {
val top = displayCutout.safeInsetTop
val left = displayCutout.safeInsetLeft
val right = displayCutout.safeInsetRight
_insetCutout = maxOf(top, left, right)
}
insets
}
binding.root.requestApplyInsetsWhenAttached()
}
private fun setupWindowSettings() {
window.apply {
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
WindowCompat.setDecorFitsSystemWindows(this, false)
val insetsController = WindowInsetsControllerCompat(this, binding.root)
insetsController.isAppearanceLightStatusBars = !isUsingNightModeResources()
insetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
private fun setupInitialFragment(savedInstanceState: Bundle?) {
if (savedInstanceState == null) {
when {
corePreferencesManager.user == null -> {
val fragment = if (viewModel.isLogistrationEnabled && authCode == null) {
LogistrationFragment()
} else {
SignInFragment.newInstance(null, null)
}
addFragment(fragment)
}
whatsNewManager.shouldShowWhatsNew() -> addFragment(WhatsNewFragment.newInstance())
else -> addFragment(MainFragment.newInstance())
}
intent.extras?.takeIf { it.containsKey(DeepLink.Keys.NOTIFICATION_TYPE.value) }?.let {
handlePushNotification(it)
}
}
}
private fun observeLogoutEvent() {
viewModel.logoutUser.observe(this) {
profileRouter.restartApp(supportFragmentManager, viewModel.isLogistrationEnabled)
}
}
private fun observeDownloadFailedDialog() {
lifecycleScope.launch {
viewModel.downloadFailedDialog.collect {
downloadDialogManager.showDownloadFailedPopup(
downloadModel = it.downloadModel,
fragmentManager = supportFragmentManager,
)
}
}
}
override fun onStart() {
super.onStart()
if (viewModel.isBranchEnabled) {
Branch.sessionBuilder(this)
.withCallback(branchCallback)
.withData(this.intent.data)
.init()
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
this.intent = intent
if (authCode != null) {
addFragment(SignInFragment.newInstance(null, null))
}
val extras = intent.extras
if (extras?.containsKey(DeepLink.Keys.NOTIFICATION_TYPE.value) == true) {
handlePushNotification(extras)
}
if (viewModel.isBranchEnabled) {
if (intent.getBooleanExtra(BRANCH_FORCE_NEW_SESSION, false)) {
Branch.sessionBuilder(this)
.withCallback(branchCallback)
.reInit()
}
}
}
private fun addFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.add(R.id.container, fragment)
.commit()
}
private fun computeWindowSizeClasses() {
val metrics = WindowMetricsCalculator.getOrCreate()
.computeCurrentWindowMetrics(this)
val widthDp = metrics.bounds.width() / resources.displayMetrics.density
val widthWindowSize = when {
widthDp < COMPACT_MAX_WIDTH -> WindowType.Compact
widthDp < MEDIUM_MAX_WIDTH -> WindowType.Medium
else -> WindowType.Expanded
}
val heightDp = metrics.bounds.height() / resources.displayMetrics.density
val heightWindowSize = when {
heightDp < COMPACT_MAX_HEIGHT -> WindowType.Compact
heightDp < MEDIUM_MAX_HEIGHT -> WindowType.Medium
else -> WindowType.Expanded
}
_windowSize = WindowSize(widthWindowSize, heightWindowSize)
}
private fun isUsingNightModeResources(): Boolean {
return when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_YES -> true
Configuration.UI_MODE_NIGHT_NO -> false
Configuration.UI_MODE_NIGHT_UNDEFINED -> false
else -> false
}
}
private fun handlePushNotification(data: Bundle) {
val deepLink = DeepLink(data.toStringMap())
viewModel.makeExternalRoute(supportFragmentManager, deepLink)
}
companion object {
const val TOP_INSET = "topInset"
const val BOTTOM_INSET = "bottomInset"
const val CUTOUT_INSET = "cutoutInset"
const val BRANCH_TAG = "Branch"
const val BRANCH_FORCE_NEW_SESSION = "branch_force_new_session"
internal const val COMPACT_MAX_WIDTH = 600
internal const val MEDIUM_MAX_WIDTH = 840
internal const val COMPACT_MAX_HEIGHT = 480
internal const val MEDIUM_MAX_HEIGHT = 900
}
}