-
Notifications
You must be signed in to change notification settings - Fork 782
Realm migration bg #6548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Realm migration bg #6548
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0dd9e94
Delegate the creation of the Session to MainActivity instead of Vecto…
bmarty c8a2bfc
Always start MainActivity to ensure that current session is set.
bmarty b7826c0
Start SDK before handling permalink or sharing to the app.
bmarty 0bedfc8
Add a message when migrating the data (after 1 seconds)
bmarty b294c9a
Do not always start MainActivity first when launching HomeActivity
bmarty 9dda647
Add Foreground service when data are updated.
bmarty 3063c0d
Avoid updating the UI every seconds
bmarty b83f6f2
Changelog
bmarty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Move initialization of the Session to a background thread. MainActivity is restoring the session now, instead of VectorApplication. Useful when for instance a long migration of a database is required. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
vector/src/main/java/im/vector/app/core/di/ActiveSessionSetter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2022 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package im.vector.app.core.di | ||
|
||
import android.content.Context | ||
import im.vector.app.core.extensions.configureAndStart | ||
import org.matrix.android.sdk.api.auth.AuthenticationService | ||
import javax.inject.Inject | ||
|
||
class ActiveSessionSetter @Inject constructor( | ||
private val activeSessionHolder: ActiveSessionHolder, | ||
private val authenticationService: AuthenticationService, | ||
private val applicationContext: Context, | ||
) { | ||
fun shouldSetActionSession(): Boolean { | ||
return authenticationService.hasAuthenticatedSessions() && !activeSessionHolder.hasActiveSession() | ||
} | ||
|
||
fun tryToSetActiveSession(startSync: Boolean) { | ||
if (shouldSetActionSession()) { | ||
val lastAuthenticatedSession = authenticationService.getLastAuthenticatedSession()!! | ||
activeSessionHolder.setActiveSession(lastAuthenticatedSession) | ||
lastAuthenticatedSession.configureAndStart(applicationContext, startSyncing = startSync) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,15 @@ | |
package im.vector.app.features | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.os.Parcelable | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.view.isVisible | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import com.airbnb.mvrx.viewModel | ||
import com.bumptech.glide.Glide | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
@@ -44,9 +48,16 @@ import im.vector.app.features.popup.PopupAlertManager | |
import im.vector.app.features.session.VectorSessionStore | ||
import im.vector.app.features.settings.VectorPreferences | ||
import im.vector.app.features.signout.hard.SignedOutActivity | ||
import im.vector.app.features.start.StartAppAction | ||
import im.vector.app.features.start.StartAppAndroidService | ||
import im.vector.app.features.start.StartAppViewEvent | ||
import im.vector.app.features.start.StartAppViewModel | ||
import im.vector.app.features.start.StartAppViewState | ||
import im.vector.app.features.themes.ActivityOtherThemes | ||
import im.vector.app.features.ui.UiStateRepository | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.parcelize.Parcelize | ||
|
@@ -73,6 +84,8 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity | |
|
||
companion object { | ||
private const val EXTRA_ARGS = "EXTRA_ARGS" | ||
private const val EXTRA_NEXT_INTENT = "EXTRA_NEXT_INTENT" | ||
private const val EXTRA_INIT_SESSION = "EXTRA_INIT_SESSION" | ||
|
||
// Special action to clear cache and/or clear credentials | ||
fun restartApp(activity: Activity, args: MainActivityArgs) { | ||
|
@@ -82,8 +95,22 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity | |
intent.putExtra(EXTRA_ARGS, args) | ||
activity.startActivity(intent) | ||
} | ||
|
||
fun getIntentToInitSession(activity: Activity): Intent { | ||
val intent = Intent(activity, MainActivity::class.java) | ||
intent.putExtra(EXTRA_INIT_SESSION, true) | ||
return intent | ||
} | ||
|
||
fun getIntentWithNextIntent(context: Context, nextIntent: Intent): Intent { | ||
val intent = Intent(context, MainActivity::class.java) | ||
intent.putExtra(EXTRA_NEXT_INTENT, nextIntent) | ||
return intent | ||
} | ||
} | ||
|
||
private val startAppViewModel: StartAppViewModel by viewModel() | ||
|
||
override fun getBinding() = ActivityMainBinding.inflate(layoutInflater) | ||
|
||
override fun getOtherThemes() = ActivityOtherThemes.Launcher | ||
|
@@ -103,15 +130,58 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity | |
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
args = parseArgs() | ||
if (args.clearCredentials || args.isUserLoggedOut || args.clearCache) { | ||
clearNotifications() | ||
|
||
startAppViewModel.onEach { | ||
renderState(it) | ||
} | ||
startAppViewModel.viewEvents.stream() | ||
.onEach(::handleViewEvents) | ||
.launchIn(lifecycleScope) | ||
|
||
startAppViewModel.handle(StartAppAction.StartApp) | ||
} | ||
|
||
private fun renderState(state: StartAppViewState) { | ||
if (state.mayBeLongToProcess) { | ||
views.status.setText(R.string.updating_your_data) | ||
} | ||
views.status.isVisible = state.mayBeLongToProcess | ||
} | ||
|
||
private fun handleViewEvents(event: StartAppViewEvent) { | ||
when (event) { | ||
StartAppViewEvent.StartForegroundService -> handleStartForegroundService() | ||
StartAppViewEvent.AppStarted -> handleAppStarted() | ||
} | ||
} | ||
|
||
private fun handleStartForegroundService() { | ||
if (startAppViewModel.shouldStartApp()) { | ||
// Start foreground service, because the operation may take a while | ||
val intent = Intent(this, StartAppAndroidService::class.java) | ||
ContextCompat.startForegroundService(this, intent) | ||
} | ||
// Handle some wanted cleanup | ||
if (args.clearCache || args.clearCredentials) { | ||
doCleanUp() | ||
} | ||
|
||
private fun handleAppStarted() { | ||
if (intent.hasExtra(EXTRA_NEXT_INTENT)) { | ||
// Start the next Activity | ||
val nextIntent = intent.getParcelableExtra<Intent>(EXTRA_NEXT_INTENT) | ||
startIntentAndFinish(nextIntent) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is working but when going back, we can see the MainActivity screen for a few ms... |
||
} else if (intent.hasExtra(EXTRA_INIT_SESSION)) { | ||
setResult(RESULT_OK) | ||
finish() | ||
} else { | ||
startNextActivityAndFinish() | ||
args = parseArgs() | ||
if (args.clearCredentials || args.isUserLoggedOut || args.clearCache) { | ||
clearNotifications() | ||
} | ||
// Handle some wanted cleanup | ||
if (args.clearCache || args.clearCredentials) { | ||
doCleanUp() | ||
} else { | ||
startNextActivityAndFinish() | ||
} | ||
} | ||
} | ||
|
||
|
@@ -241,7 +311,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity | |
// We have a session. | ||
// Check it can be opened | ||
if (sessionHolder.getActiveSession().isOpenable) { | ||
HomeActivity.newIntent(this, existingSession = true) | ||
HomeActivity.newIntent(this, firstStartMainActivity = false, existingSession = true) | ||
} else { | ||
// The token is still invalid | ||
navigator.softLogout(this) | ||
|
@@ -253,6 +323,10 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity | |
null | ||
} | ||
} | ||
startIntentAndFinish(intent) | ||
} | ||
|
||
private fun startIntentAndFinish(intent: Intent?) { | ||
intent?.let { startActivity(it) } | ||
finish() | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will refresh the UI every second, maybe not necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, at the beginning I wanted to add more complex UI (timer, different wording depending on the duration, etc.). I may iterate on this, but honestly does it really worth it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have done it in 912d71b