Merge pull request #1593 from vector-im/feature/fix_crash_on_shortcuts

Clear dynamic shortcuts when user logs out
This commit is contained in:
Benoit Marty 2020-07-01 20:33:00 +02:00 committed by GitHub
commit 01e42838ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -32,6 +32,7 @@ import im.vector.riotx.core.extensions.startSyncing
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.home.ShortcutsHandler
import im.vector.riotx.features.login.LoginActivity
import im.vector.riotx.features.notifications.NotificationDrawerManager
import im.vector.riotx.features.settings.VectorPreferences
@ -82,6 +83,7 @@ class MainActivity : VectorBaseActivity() {
@Inject lateinit var errorFormatter: ErrorFormatter
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var uiStateRepository: UiStateRepository
@Inject lateinit var shortcutsHandler: ShortcutsHandler
override fun injectWith(injector: ScreenComponent) {
injector.inject(this)
@ -105,6 +107,9 @@ class MainActivity : VectorBaseActivity() {
// Dismiss all notifications
notificationDrawerManager.clearAllEvents()
notificationDrawerManager.persistInfo()
// Also clear the dynamic shortcuts
shortcutsHandler.clearShortcuts()
}
private fun parseArgs(): MainActivityArgs {

View File

@ -17,6 +17,7 @@
package im.vector.riotx.features.home
import android.content.Context
import android.content.pm.ShortcutManager
import android.graphics.Bitmap
import android.os.Build
import androidx.core.content.pm.ShortcutInfoCompat
@ -26,6 +27,7 @@ import im.vector.matrix.android.api.util.toMatrixItem
import im.vector.riotx.core.glide.GlideApp
import im.vector.riotx.core.utils.DimensionConverter
import im.vector.riotx.features.home.room.detail.RoomDetailActivity
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject
@ -51,6 +53,11 @@ class ShortcutsHandler @Inject constructor(
}
fun observeRoomsAndBuildShortcuts(): Disposable {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
// No op
return Observable.empty<Unit>().subscribe()
}
return homeRoomListStore
.observe()
.distinct()
@ -78,6 +85,25 @@ class ShortcutsHandler @Inject constructor(
}
}
fun clearShortcuts() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
// No op
return
}
ShortcutManagerCompat.removeAllDynamicShortcuts(context)
// We can only disabled pinned shortcuts with the API, but at least it will prevent the crash
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
context.getSystemService(ShortcutManager::class.java)
?.let {
it.disableShortcuts(it.pinnedShortcuts.map { pinnedShortcut -> pinnedShortcut.id })
}
}
}
}
// PRIVATE API *********************************************************************************
private fun Bitmap.toProfileImageIcon(): IconCompat {