From c0623726aea9f36341873bf05cc406b3e4674060 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 1 Jul 2020 16:51:53 +0200 Subject: [PATCH] Clear dynamic shortcuts when user log out --- .../im/vector/riotx/features/MainActivity.kt | 5 ++++ .../riotx/features/home/ShortcutsHandler.kt | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/vector/src/main/java/im/vector/riotx/features/MainActivity.kt b/vector/src/main/java/im/vector/riotx/features/MainActivity.kt index a9a0cee0d6..a5fa8fc4e4 100644 --- a/vector/src/main/java/im/vector/riotx/features/MainActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/MainActivity.kt @@ -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 { diff --git a/vector/src/main/java/im/vector/riotx/features/home/ShortcutsHandler.kt b/vector/src/main/java/im/vector/riotx/features/home/ShortcutsHandler.kt index 34f2b7bd76..805014a81e 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/ShortcutsHandler.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/ShortcutsHandler.kt @@ -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().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 {