17
17
package im.vector.app.features
18
18
19
19
import android.app.Activity
20
+ import android.content.Context
20
21
import android.content.Intent
21
22
import android.os.Bundle
22
23
import android.os.Parcelable
24
+ import androidx.core.content.ContextCompat
25
+ import androidx.core.view.isVisible
23
26
import androidx.lifecycle.Lifecycle
24
27
import androidx.lifecycle.lifecycleScope
28
+ import com.airbnb.mvrx.viewModel
25
29
import com.bumptech.glide.Glide
26
30
import com.google.android.material.dialog.MaterialAlertDialogBuilder
27
31
import dagger.hilt.android.AndroidEntryPoint
@@ -44,9 +48,16 @@ import im.vector.app.features.popup.PopupAlertManager
44
48
import im.vector.app.features.session.VectorSessionStore
45
49
import im.vector.app.features.settings.VectorPreferences
46
50
import im.vector.app.features.signout.hard.SignedOutActivity
51
+ import im.vector.app.features.start.StartAppAction
52
+ import im.vector.app.features.start.StartAppAndroidService
53
+ import im.vector.app.features.start.StartAppViewEvent
54
+ import im.vector.app.features.start.StartAppViewModel
55
+ import im.vector.app.features.start.StartAppViewState
47
56
import im.vector.app.features.themes.ActivityOtherThemes
48
57
import im.vector.app.features.ui.UiStateRepository
49
58
import kotlinx.coroutines.Dispatchers
59
+ import kotlinx.coroutines.flow.launchIn
60
+ import kotlinx.coroutines.flow.onEach
50
61
import kotlinx.coroutines.launch
51
62
import kotlinx.coroutines.withContext
52
63
import kotlinx.parcelize.Parcelize
@@ -73,6 +84,8 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
73
84
74
85
companion object {
75
86
private const val EXTRA_ARGS = " EXTRA_ARGS"
87
+ private const val EXTRA_NEXT_INTENT = " EXTRA_NEXT_INTENT"
88
+ private const val EXTRA_INIT_SESSION = " EXTRA_INIT_SESSION"
76
89
77
90
// Special action to clear cache and/or clear credentials
78
91
fun restartApp (activity : Activity , args : MainActivityArgs ) {
@@ -82,8 +95,22 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
82
95
intent.putExtra(EXTRA_ARGS , args)
83
96
activity.startActivity(intent)
84
97
}
98
+
99
+ fun getIntentToInitSession (activity : Activity ): Intent {
100
+ val intent = Intent (activity, MainActivity ::class .java)
101
+ intent.putExtra(EXTRA_INIT_SESSION , true )
102
+ return intent
103
+ }
104
+
105
+ fun getIntentWithNextIntent (context : Context , nextIntent : Intent ): Intent {
106
+ val intent = Intent (context, MainActivity ::class .java)
107
+ intent.putExtra(EXTRA_NEXT_INTENT , nextIntent)
108
+ return intent
109
+ }
85
110
}
86
111
112
+ private val startAppViewModel: StartAppViewModel by viewModel()
113
+
87
114
override fun getBinding () = ActivityMainBinding .inflate(layoutInflater)
88
115
89
116
override fun getOtherThemes () = ActivityOtherThemes .Launcher
@@ -103,15 +130,58 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
103
130
104
131
override fun onCreate (savedInstanceState : Bundle ? ) {
105
132
super .onCreate(savedInstanceState)
106
- args = parseArgs()
107
- if (args.clearCredentials || args.isUserLoggedOut || args.clearCache) {
108
- clearNotifications()
133
+
134
+ startAppViewModel.onEach {
135
+ renderState(it)
136
+ }
137
+ startAppViewModel.viewEvents.stream()
138
+ .onEach(::handleViewEvents)
139
+ .launchIn(lifecycleScope)
140
+
141
+ startAppViewModel.handle(StartAppAction .StartApp )
142
+ }
143
+
144
+ private fun renderState (state : StartAppViewState ) {
145
+ if (state.mayBeLongToProcess) {
146
+ views.status.setText(R .string.updating_your_data)
147
+ }
148
+ views.status.isVisible = state.mayBeLongToProcess
149
+ }
150
+
151
+ private fun handleViewEvents (event : StartAppViewEvent ) {
152
+ when (event) {
153
+ StartAppViewEvent .StartForegroundService -> handleStartForegroundService()
154
+ StartAppViewEvent .AppStarted -> handleAppStarted()
155
+ }
156
+ }
157
+
158
+ private fun handleStartForegroundService () {
159
+ if (startAppViewModel.shouldStartApp()) {
160
+ // Start foreground service, because the operation may take a while
161
+ val intent = Intent (this , StartAppAndroidService ::class .java)
162
+ ContextCompat .startForegroundService(this , intent)
109
163
}
110
- // Handle some wanted cleanup
111
- if (args.clearCache || args.clearCredentials) {
112
- doCleanUp()
164
+ }
165
+
166
+ private fun handleAppStarted () {
167
+ if (intent.hasExtra(EXTRA_NEXT_INTENT )) {
168
+ // Start the next Activity
169
+ val nextIntent = intent.getParcelableExtra<Intent >(EXTRA_NEXT_INTENT )
170
+ startIntentAndFinish(nextIntent)
171
+ } else if (intent.hasExtra(EXTRA_INIT_SESSION )) {
172
+ setResult(RESULT_OK )
173
+ finish()
113
174
} else {
114
- startNextActivityAndFinish()
175
+ args = parseArgs()
176
+ if (args.clearCredentials || args.isUserLoggedOut || args.clearCache) {
177
+ clearNotifications()
178
+ }
179
+ // Handle some wanted cleanup
180
+ if (args.clearCache || args.clearCredentials) {
181
+ doCleanUp()
182
+ } else {
183
+ startNextActivityAndFinish()
184
+ }
115
185
}
116
186
}
117
187
@@ -241,7 +311,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
241
311
// We have a session.
242
312
// Check it can be opened
243
313
if (sessionHolder.getActiveSession().isOpenable) {
244
- HomeActivity .newIntent(this , existingSession = true )
314
+ HomeActivity .newIntent(this , firstStartMainActivity = false , existingSession = true )
245
315
} else {
246
316
// The token is still invalid
247
317
navigator.softLogout(this )
@@ -253,6 +323,10 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
253
323
null
254
324
}
255
325
}
326
+ startIntentAndFinish(intent)
327
+ }
328
+
329
+ private fun startIntentAndFinish (intent : Intent ? ) {
256
330
intent?.let { startActivity(it) }
257
331
finish()
258
332
}
0 commit comments