InvalidToken: Rework MainActivity args

This commit is contained in:
Benoit Marty 2019-12-10 14:24:04 +01:00
parent 18649ebddb
commit 29087d4a87
6 changed files with 44 additions and 17 deletions

View File

@ -38,8 +38,8 @@ data class MatrixError(
@Json(name = "admin_contact") val adminUri: String? = null,
// For M_LIMIT_EXCEEDED
@Json(name = "retry_after_ms") val retryAfterMillis: Long? = null,
// For M_UNAUTHORIZED
@Json(name = "soft_logout") val isSoftLogout: Boolean? = null
// For M_UNKNOWN_TOKEN
@Json(name = "soft_logout") val isSoftLogout: Boolean = false
) {
companion object {

View File

@ -32,6 +32,7 @@ import retrofit2.Callback
import retrofit2.Response
import timber.log.Timber
import java.io.IOException
import java.net.HttpURLConnection
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
@ -100,10 +101,11 @@ private fun toFailure(errorBody: ResponseBody?, httpCode: Int): Failure {
if (matrixError.code == MatrixError.M_CONSENT_NOT_GIVEN && !matrixError.consentUri.isNullOrBlank()) {
// Also send this error to the bus, for a global management
EventBus.getDefault().post(GlobalError.ConsentNotGivenError(matrixError.consentUri))
} else if (matrixError.code == MatrixError.M_UNAUTHORIZED) {
} else if (httpCode == HttpURLConnection.HTTP_UNAUTHORIZED /* 401 */
&& matrixError.code == MatrixError.M_UNKNOWN_TOKEN) {
// TODO Check that this is ok during the login flow
// Also send this error to the bus, for a global management
EventBus.getDefault().post(GlobalError.InvalidToken(matrixError.isSoftLogout == true))
EventBus.getDefault().post(GlobalError.InvalidToken(matrixError.isSoftLogout))
}
return Failure.ServerError(matrixError, httpCode)

View File

@ -73,10 +73,12 @@ internal class DefaultSyncTask @Inject constructor(private val syncAPI: SyncAPI,
}
} catch (throwable: Throwable) {
// Intercept 401
if (throwable is Failure.ServerError
&& throwable.error.code == MatrixError.M_UNKNOWN_TOKEN) {
sessionParamsStore.delete(userId)
}
// TODO Remove?
//if (throwable is Failure.ServerError
// && throwable.error.code == MatrixError.M_UNKNOWN_TOKEN
// && !throwable.error.isSoftLogout) {
// sessionParamsStore.delete(userId)
//}
throw throwable
}
syncResponseHandler.handleResponse(syncResponse, token)

View File

@ -19,6 +19,7 @@ package im.vector.riotx.features
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.appcompat.app.AlertDialog
import com.bumptech.glide.Glide
import im.vector.matrix.android.api.MatrixCallback
@ -30,6 +31,7 @@ import im.vector.riotx.core.platform.VectorBaseActivity
import im.vector.riotx.core.utils.deleteAllFiles
import im.vector.riotx.features.home.HomeActivity
import im.vector.riotx.features.login.LoginActivity
import kotlinx.android.parcel.Parcelize
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@ -37,23 +39,30 @@ import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject
@Parcelize
data class MainActivityArgs(
val clearCache: Boolean = false,
val clearCredentials: Boolean = false,
val isUserLoggedOut: Boolean = false,
val isSoftLogout: Boolean = false
) : Parcelable
class MainActivity : VectorBaseActivity() {
companion object {
private const val EXTRA_CLEAR_CACHE = "EXTRA_CLEAR_CACHE"
private const val EXTRA_CLEAR_CREDENTIALS = "EXTRA_CLEAR_CREDENTIALS"
private const val EXTRA_ARGS = "EXTRA_ARGS"
// Special action to clear cache and/or clear credentials
fun restartApp(activity: Activity, clearCache: Boolean = false, clearCredentials: Boolean = false) {
fun restartApp(activity: Activity, args: MainActivityArgs) {
val intent = Intent(activity, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
intent.putExtra(EXTRA_CLEAR_CACHE, clearCache)
intent.putExtra(EXTRA_CLEAR_CREDENTIALS, clearCredentials)
intent.putExtra(EXTRA_ARGS, args)
activity.startActivity(intent)
}
}
private var args: MainActivityArgs? = null
@Inject lateinit var sessionHolder: ActiveSessionHolder
@Inject lateinit var errorFormatter: ErrorFormatter
@ -63,8 +72,10 @@ class MainActivity : VectorBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val clearCache = intent.getBooleanExtra(EXTRA_CLEAR_CACHE, false)
val clearCredentials = intent.getBooleanExtra(EXTRA_CLEAR_CREDENTIALS, false)
args = intent.getParcelableExtra(EXTRA_ARGS)
val clearCache = args?.clearCache ?: false
val clearCredentials = args?.clearCredentials ?: false
// Handle some wanted cleanup
if (clearCache || clearCredentials) {

View File

@ -43,6 +43,7 @@ import im.vector.riotx.core.preference.UserAvatarPreference
import im.vector.riotx.core.preference.VectorPreference
import im.vector.riotx.core.utils.*
import im.vector.riotx.features.MainActivity
import im.vector.riotx.features.MainActivityArgs
import im.vector.riotx.features.themes.ThemeUtils
import im.vector.riotx.features.workers.signout.SignOutUiWorker
import kotlinx.coroutines.Dispatchers
@ -176,7 +177,12 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() {
it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
displayLoadingView()
MainActivity.restartApp(activity!!, clearCache = true, clearCredentials = false)
MainActivity.restartApp(activity!!,
MainActivityArgs(
clearCache = true,
clearCredentials = false
)
)
false
}
}

View File

@ -23,6 +23,7 @@ import im.vector.riotx.R
import im.vector.riotx.core.di.ActiveSessionHolder
import im.vector.riotx.core.extensions.vectorComponent
import im.vector.riotx.features.MainActivity
import im.vector.riotx.features.MainActivityArgs
import im.vector.riotx.features.notifications.NotificationDrawerManager
class SignOutUiWorker(private val activity: FragmentActivity) {
@ -58,6 +59,11 @@ class SignOutUiWorker(private val activity: FragmentActivity) {
notificationDrawerManager.clearAllEvents()
notificationDrawerManager.persistInfo()
MainActivity.restartApp(activity, clearCache = true, clearCredentials = true)
MainActivity.restartApp(activity,
MainActivityArgs(
clearCache = true,
clearCredentials = true
)
)
}
}