-
Notifications
You must be signed in to change notification settings - Fork 19
/
WireActivity.kt
477 lines (431 loc) · 20.5 KB
/
WireActivity.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Wire
* Copyright (C) 2023 Wire Swiss GmbH
*
* 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 http://www.gnu.org/licenses/.
*
*
*/
package com.wire.android.ui
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.WindowManager
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.ramcosta.composedestinations.spec.Route
import com.wire.android.BuildConfig
import com.wire.android.appLogger
import com.wire.android.config.CustomUiConfigurationProvider
import com.wire.android.config.LocalCustomUiConfigurationProvider
import com.wire.android.feature.NavigationSwitchAccountActions
import com.wire.android.navigation.BackStackMode
import com.wire.android.navigation.NavigationCommand
import com.wire.android.navigation.NavigationGraph
import com.wire.android.navigation.navigateToItem
import com.wire.android.navigation.rememberNavigator
import com.wire.android.ui.calling.ProximitySensorManager
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import com.wire.android.ui.common.topappbar.CommonTopAppBar
import com.wire.android.ui.common.topappbar.CommonTopAppBarViewModel
import com.wire.android.ui.destinations.ConversationScreenDestination
import com.wire.android.ui.destinations.HomeScreenDestination
import com.wire.android.ui.destinations.ImportMediaScreenDestination
import com.wire.android.ui.destinations.IncomingCallScreenDestination
import com.wire.android.ui.destinations.LoginScreenDestination
import com.wire.android.ui.destinations.MigrationScreenDestination
import com.wire.android.ui.destinations.OngoingCallScreenDestination
import com.wire.android.ui.destinations.OtherUserProfileScreenDestination
import com.wire.android.ui.destinations.SelfDevicesScreenDestination
import com.wire.android.ui.destinations.SelfUserProfileScreenDestination
import com.wire.android.ui.destinations.WelcomeScreenDestination
import com.wire.android.ui.home.E2EIRequiredDialog
import com.wire.android.ui.home.E2EISnoozeDialog
import com.wire.android.ui.home.appLock.LockCodeTimeManager
import com.wire.android.ui.home.sync.FeatureFlagNotificationViewModel
import com.wire.android.ui.theme.ThemeOption
import com.wire.android.ui.theme.WireTheme
import com.wire.android.util.CurrentScreenManager
import com.wire.android.util.LocalSyncStateObserver
import com.wire.android.util.SyncStateObserver
import com.wire.android.util.debug.FeatureVisibilityFlags
import com.wire.android.util.debug.LocalFeatureVisibilityFlags
import com.wire.android.util.deeplink.DeepLinkResult
import com.wire.android.util.ui.updateScreenSettings
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.launch
import javax.inject.Inject
@OptIn(ExperimentalComposeUiApi::class)
@AndroidEntryPoint
@Suppress("TooManyFunctions")
class WireActivity : AppCompatActivity() {
@Inject
lateinit var currentScreenManager: CurrentScreenManager
@Inject
lateinit var proximitySensorManager: ProximitySensorManager
@Inject
lateinit var lockCodeTimeManager: LockCodeTimeManager
private val viewModel: WireActivityViewModel by viewModels()
private val featureFlagNotificationViewModel: FeatureFlagNotificationViewModel by viewModels()
private val commonTopAppBarViewModel: CommonTopAppBarViewModel by viewModels()
val navigationCommands: MutableSharedFlow<NavigationCommand> = MutableSharedFlow()
// This flag is used to keep the splash screen open until the first screen is drawn.
private var shouldKeepSplashOpen = true
override fun onCreate(savedInstanceState: Bundle?) {
// We need to keep the splash screen open until the first screen is drawn.
// Otherwise a white screen is displayed.
// It's an API limitation, at some point we may need to remove it
installSplashScreen().setKeepOnScreenCondition {
shouldKeepSplashOpen
}
super.onCreate(savedInstanceState)
proximitySensorManager.initialize()
lifecycle.addObserver(currentScreenManager)
WindowCompat.setDecorFitsSystemWindows(window, false)
viewModel.observePersistentConnectionStatus()
val startDestination = when (viewModel.initialAppState) {
InitialAppState.NOT_MIGRATED -> MigrationScreenDestination
InitialAppState.NOT_LOGGED_IN -> WelcomeScreenDestination
InitialAppState.LOGGED_IN -> HomeScreenDestination
}
setComposableContent(startDestination) {
shouldKeepSplashOpen = false
handleDeepLink(intent, savedInstanceState)
}
}
override fun onNewIntent(intent: Intent?) {
if (viewModel.isSharingIntent(intent)) {
setIntent(intent)
}
handleDeepLink(intent)
super.onNewIntent(intent)
}
private fun setComposableContent(
startDestination: Route,
onComplete: () -> Unit
) {
setContent {
val snackbarHostState = remember { SnackbarHostState() }
LaunchedEffect(viewModel.globalAppState.themeOption) {
when (viewModel.globalAppState.themeOption) {
ThemeOption.SYSTEM -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
ThemeOption.LIGHT -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
ThemeOption.DARK -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
}
CompositionLocalProvider(
LocalFeatureVisibilityFlags provides FeatureVisibilityFlags,
LocalSyncStateObserver provides SyncStateObserver(viewModel.observeSyncFlowState),
LocalCustomUiConfigurationProvider provides CustomUiConfigurationProvider,
LocalSnackbarHostState provides snackbarHostState,
LocalActivity provides this
) {
WireTheme {
Column(modifier = Modifier.statusBarsPadding()) {
val navigator = rememberNavigator(this@WireActivity::finish)
CommonTopAppBar(
connectivityUIState = commonTopAppBarViewModel.connectivityState,
onReturnToCallClick = { establishedCall ->
navigator.navigate(NavigationCommand(OngoingCallScreenDestination(establishedCall.conversationId)))
}
)
NavigationGraph(
navigator = navigator,
startDestination = startDestination
)
// This setup needs to be done after the navigation graph is created, because building the graph takes some time,
// and if any NavigationCommand is executed before the graph is fully built, it will cause a NullPointerException.
setUpNavigation(navigator.navController, onComplete)
handleScreenshotCensoring()
handleDialogs(navigator::navigate)
}
}
}
}
}
@Composable
private fun setUpNavigation(
navController: NavHostController,
onComplete: () -> Unit,
) {
val currentKeyboardController by rememberUpdatedState(LocalSoftwareKeyboardController.current)
val currentNavController by rememberUpdatedState(navController)
LaunchedEffect(Unit) {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
navigationCommands
.onSubscription { onComplete() }
.collectLatest {
currentKeyboardController?.hide()
currentNavController.navigateToItem(it)
}
}
}
}
DisposableEffect(navController) {
val updateScreenSettingsListener = NavController.OnDestinationChangedListener { _, navDestination, _ ->
currentKeyboardController?.hide()
updateScreenSettings(navDestination)
}
navController.addOnDestinationChangedListener(updateScreenSettingsListener)
navController.addOnDestinationChangedListener(currentScreenManager)
onDispose {
navController.removeOnDestinationChangedListener(updateScreenSettingsListener)
navController.removeOnDestinationChangedListener(currentScreenManager)
}
}
}
@Composable
private fun handleScreenshotCensoring() {
LaunchedEffect(viewModel.globalAppState.screenshotCensoringEnabled) {
if (viewModel.globalAppState.screenshotCensoringEnabled) {
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
}
}
@Composable
private fun handleDialogs(navigate: (NavigationCommand) -> Unit) {
with(featureFlagNotificationViewModel.featureFlagState) {
if (shouldShowTeamAppLockDialog) {
TeamAppLockFeatureFlagDialog(
isTeamAppLockEnabled = isTeamAppLockEnabled,
onConfirm = {
featureFlagNotificationViewModel.dismissTeamAppLockDialog()
if (isTeamAppLockEnabled) {
val isUserAppLockSet =
featureFlagNotificationViewModel.isUserAppLockSet()
// No need to setup another app lock if the user already has one
if (!isUserAppLockSet) {
Intent(this@WireActivity, AppLockActivity::class.java)
.apply {
putExtra(AppLockActivity.SET_TEAM_APP_LOCK, true)
}.also {
startActivity(it)
}
} else {
featureFlagNotificationViewModel.markTeamAppLockStatusAsNot()
}
} else {
with(featureFlagNotificationViewModel) {
markTeamAppLockStatusAsNot()
confirmAppLockNotEnforced()
}
}
}
)
} else {
if (showFileSharingDialog) {
FileRestrictionDialog(
isFileSharingEnabled = isFileSharingEnabledState,
hideDialogStatus = featureFlagNotificationViewModel::dismissFileSharingDialog
)
}
if (shouldShowGuestRoomLinkDialog) {
GuestRoomLinkFeatureFlagDialog(
isGuestRoomLinkEnabled = isGuestRoomLinkEnabled,
onDismiss = featureFlagNotificationViewModel::dismissGuestRoomLinkDialog
)
}
if (shouldShowSelfDeletingMessagesDialog) {
SelfDeletingMessagesDialog(
areSelfDeletingMessagesEnabled = areSelfDeletedMessagesEnabled,
enforcedTimeout = enforcedTimeoutDuration,
hideDialogStatus = featureFlagNotificationViewModel::dismissSelfDeletingMessagesDialog
)
}
e2EIRequired?.let {
E2EIRequiredDialog(
result = e2EIRequired,
getCertificate = featureFlagNotificationViewModel::getE2EICertificate,
snoozeDialog = featureFlagNotificationViewModel::snoozeE2EIdRequiredDialog
)
}
e2EISnoozeInfo?.let {
E2EISnoozeDialog(
timeLeft = e2EISnoozeInfo.timeLeft,
dismissDialog = featureFlagNotificationViewModel::dismissSnoozeE2EIdRequiredDialog
)
}
UpdateAppDialog(viewModel.globalAppState.updateAppDialog, ::updateTheApp)
JoinConversationDialog(
viewModel.globalAppState.conversationJoinedDialog,
navigate,
viewModel::onJoinConversationFlowCompleted
)
CustomBackendDialog(
viewModel.globalAppState,
viewModel::dismissCustomBackendDialog
) {
viewModel.customBackendDialogProceedButtonClicked {
navigate(
NavigationCommand(
WelcomeScreenDestination
)
)
}
}
MaxAccountDialog(
shouldShow = viewModel.globalAppState.maxAccountDialog,
onConfirm = {
viewModel.dismissMaxAccountDialog()
navigate(NavigationCommand(SelfUserProfileScreenDestination))
},
onDismiss = viewModel::dismissMaxAccountDialog
)
AccountLoggedOutDialog(
viewModel.globalAppState.blockUserUI
) { viewModel.tryToSwitchAccount(NavigationSwitchAccountActions(navigate)) }
NewClientDialog(
viewModel.globalAppState.newClientDialog,
{ navigate(NavigationCommand(SelfDevicesScreenDestination)) },
{
viewModel.switchAccount(
userId = it,
actions = NavigationSwitchAccountActions(navigate),
onComplete = { navigate(NavigationCommand(SelfDevicesScreenDestination)) })
},
viewModel::dismissNewClientsDialog
)
}
if (showCallEndedBecauseOfConversationDegraded) {
GuestCallWasEndedBecauseOfVerificationDegradedDialog(
featureFlagNotificationViewModel::dismissCallEndedBecauseOfConversationDegraded
)
}
}
}
private fun updateTheApp() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(BuildConfig.UPDATE_APP_URL))
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
override fun onResume() {
super.onResume()
lifecycleScope.launch {
lockCodeTimeManager.observeAppLock()
// Listen to one flow in a lifecycle-aware manner using flowWithLifecycle
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.first().let {
if (it) {
startActivity(
Intent(this@WireActivity, AppLockActivity::class.java)
)
}
}
}
proximitySensorManager.registerListener()
}
override fun onPause() {
super.onPause()
proximitySensorManager.unRegisterListener()
}
override fun onSaveInstanceState(outState: Bundle) {
outState.putBoolean(HANDLED_DEEPLINK_FLAG, true)
super.onSaveInstanceState(outState)
}
@Suppress("ComplexCondition")
private fun handleDeepLink(
intent: Intent?,
savedInstanceState: Bundle? = null
) {
if (intent == null
|| intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0
|| savedInstanceState?.getBoolean(HANDLED_DEEPLINK_FLAG, false) == true
|| intent.getBooleanExtra(HANDLED_DEEPLINK_FLAG, false)
) {
return
} else {
val navigate: (NavigationCommand) -> Unit = { lifecycleScope.launch { navigationCommands.emit(it) } }
viewModel.handleDeepLink(
intent = intent,
onResult = ::handleDeepLinkResult,
onOpenConversation = { navigate(NavigationCommand(ConversationScreenDestination(it), BackStackMode.CLEAR_TILL_START)) },
onIsSharingIntent = { navigate(NavigationCommand(ImportMediaScreenDestination, BackStackMode.UPDATE_EXISTED)) }
)
intent.putExtra(HANDLED_DEEPLINK_FLAG, true)
}
}
private fun handleDeepLinkResult(result: DeepLinkResult) {
val navigate: (NavigationCommand) -> Unit = { lifecycleScope.launch { navigationCommands.emit(it) } }
when (result) {
is DeepLinkResult.SSOLogin -> {
navigate(NavigationCommand(LoginScreenDestination(ssoLoginResult = result), BackStackMode.UPDATE_EXISTED))
}
is DeepLinkResult.MigrationLogin -> {
navigate(NavigationCommand(LoginScreenDestination(result.userHandle), BackStackMode.UPDATE_EXISTED))
}
is DeepLinkResult.CustomServerConfig -> {
// do nothing, already handled in ViewModel
}
is DeepLinkResult.IncomingCall -> {
if (result.switchedAccount) navigate(NavigationCommand(HomeScreenDestination, BackStackMode.CLEAR_WHOLE))
navigate(NavigationCommand(IncomingCallScreenDestination(result.conversationsId)))
}
is DeepLinkResult.OngoingCall -> {
navigate(NavigationCommand(OngoingCallScreenDestination(result.conversationsId)))
}
is DeepLinkResult.OpenConversation -> {
if (result.switchedAccount) navigate(NavigationCommand(HomeScreenDestination, BackStackMode.CLEAR_WHOLE))
navigate(NavigationCommand(ConversationScreenDestination(result.conversationsId), BackStackMode.UPDATE_EXISTED))
}
is DeepLinkResult.OpenOtherUserProfile -> {
if (result.switchedAccount) navigate(NavigationCommand(HomeScreenDestination, BackStackMode.CLEAR_WHOLE))
navigate(NavigationCommand(OtherUserProfileScreenDestination(result.userId), BackStackMode.UPDATE_EXISTED))
}
is DeepLinkResult.JoinConversation -> {
// do nothing, already handled in ViewModel
}
is DeepLinkResult.Unknown -> {
appLogger.e("unknown deeplink result $result")
}
}
}
companion object {
private const val HANDLED_DEEPLINK_FLAG = "deeplink_handled_flag_key"
}
}
val LocalActivity = staticCompositionLocalOf<Activity> {
error("No Activity provided")
}