Migrate previous pinned Shortcut to the new Intent

This commit is contained in:
Benoit Marty 2022-07-28 18:14:54 +02:00
parent 6e0ca3958b
commit eee418d1fd
3 changed files with 39 additions and 2 deletions

View File

@ -141,6 +141,8 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
shortcutsHandler.updateShortcutsWithPreviousIntent()
startAppViewModel.onEach {
renderState(it)
}

View File

@ -17,14 +17,20 @@
package im.vector.app.features.home
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.ShortcutManager
import android.os.Build
import androidx.core.content.edit
import androidx.core.content.getSystemService
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.DefaultPreferences
import im.vector.app.core.dispatchers.CoroutineDispatchers
import im.vector.app.core.resources.StringProvider
import im.vector.app.features.MainActivity
import im.vector.app.features.home.room.detail.RoomDetailActivity
import im.vector.app.features.pin.PinCodeStore
import im.vector.app.features.pin.PinCodeStoreListener
import kotlinx.coroutines.CoroutineScope
@ -35,6 +41,7 @@ import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.room.RoomSortOrder
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.model.RoomSummary
@ -50,7 +57,9 @@ class ShortcutsHandler @Inject constructor(
private val appDispatchers: CoroutineDispatchers,
private val shortcutCreator: ShortcutCreator,
private val activeSessionHolder: ActiveSessionHolder,
private val pinCodeStore: PinCodeStore
private val pinCodeStore: PinCodeStore,
@DefaultPreferences
private val sharedPreferences: SharedPreferences,
) : PinCodeStoreListener {
private val isRequestPinShortcutSupported = ShortcutManagerCompat.isRequestPinShortcutSupported(context)
@ -87,6 +96,25 @@ class ShortcutsHandler @Inject constructor(
.launchIn(coroutineScope)
}
fun updateShortcutsWithPreviousIntent() {
// Check if it's been already done
if (sharedPreferences.getBoolean(SHARED_PREF_KEY, false)) return
ShortcutManagerCompat.getShortcuts(context, ShortcutManagerCompat.FLAG_MATCH_PINNED)
.filter { it.intent.component?.className == RoomDetailActivity::class.qualifiedName }
.mapNotNull {
it.intent.getStringExtra("EXTRA_ROOM_ID")?.let { roomId ->
ShortcutInfoCompat.Builder(context, it.toShortcutInfo())
.setIntent(MainActivity.shortcutIntent(context, roomId))
.build()
}
}
.takeIf { it.isNotEmpty() }
?.also { Timber.d("Update ${it.size} shortcut(s)") }
?.let { tryOrNull("Error") { ShortcutManagerCompat.updateShortcuts(context, it) } }
?.also { Timber.d("Update shortcuts with success: $it") }
sharedPreferences.edit { putBoolean(SHARED_PREF_KEY, true) }
}
private fun removeDeadShortcuts(roomIds: List<String>) {
val deadShortcutIds = ShortcutManagerCompat.getShortcuts(context, ShortcutManagerCompat.FLAG_MATCH_DYNAMIC)
.map { it.id }
@ -163,4 +191,8 @@ class ShortcutsHandler @Inject constructor(
// Else shortcut will be created next time any room summary is updated, or
// next time the app is started which is acceptable
}
companion object {
const val SHARED_PREF_KEY = "ROOM_DETAIL_ACTIVITY_SHORTCUT_UPDATED"
}
}

View File

@ -28,6 +28,7 @@ import im.vector.app.R
import im.vector.app.core.di.DefaultSharedPreferences
import im.vector.app.core.time.Clock
import im.vector.app.features.disclaimer.SHARED_PREF_KEY
import im.vector.app.features.home.ShortcutsHandler
import im.vector.app.features.homeserver.ServerUrlsRepository
import im.vector.app.features.themes.ThemeUtils
import org.matrix.android.sdk.api.extensions.tryOrNull
@ -268,7 +269,9 @@ class VectorPreferences @Inject constructor(
SETTINGS_DEVELOPER_MODE_FAIL_FAST_PREFERENCE_KEY,
SETTINGS_USE_RAGE_SHAKE_KEY,
SETTINGS_SECURITY_USE_FLAG_SECURE
SETTINGS_SECURITY_USE_FLAG_SECURE,
ShortcutsHandler.SHARED_PREF_KEY,
)
}