aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2021-01-31 13:45:14 +0530
committerGitHub <noreply@github.com>2021-01-31 13:45:14 +0530
commit4694c5ac8575a7e85e6b950eaed99049719b6151 (patch)
tree2129891d7f090760d3349db12a8dcfc496f5d8a3
parentfe29aa0cb0900683b83f47e70e1ba3967f32d560 (diff)
Fix app shortcuts not updating (#1292)
-rw-r--r--CHANGELOG.md1
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt45
2 files changed, 31 insertions, 15 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f060d143..1a34ae3b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,7 @@ All notable changes to this project will be documented in this file.
- A change to the remote host key for a server would prevent the user from being able to connect to it
- Pressing the back button in the navigation bar and the one in the toolbar behaved differently
- Editing a password allowed accidentally overwriting an existing one
+- App shortcuts would never update once the first 4 were set
### Changed
diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt b/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt
index e25a3cb3..bee8a594 100644
--- a/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt
@@ -20,6 +20,7 @@ import android.view.MenuItem.OnActionExpandListener
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.activity.viewModels
+import androidx.annotation.RequiresApi
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.core.content.edit
@@ -452,22 +453,32 @@ class PasswordStore : BaseGitActivity() {
// Adds shortcut
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
- val shortcutManager: ShortcutManager = getSystemService() ?: return
- val shortcut = Builder(this, item.fullPathToParent)
- .setShortLabel(item.toString())
- .setLongLabel(item.fullPathToParent + item.toString())
- .setIcon(Icon.createWithResource(this, R.drawable.ic_lock_open_24px))
- .setIntent(authDecryptIntent)
- .build()
- val shortcuts = shortcutManager.dynamicShortcuts
- if (shortcuts.size >= shortcutManager.maxShortcutCountPerActivity && shortcuts.size > 0) {
- shortcuts.removeAt(shortcuts.size - 1)
- shortcuts.add(0, shortcut)
- shortcutManager.dynamicShortcuts = shortcuts
- } else {
- shortcutManager.addDynamicShortcuts(listOf(shortcut))
- }
+ addShortcut(item, authDecryptIntent)
+ }
+ }
+
+ @RequiresApi(Build.VERSION_CODES.N_MR1)
+ private fun addShortcut(item: PasswordItem, intent: Intent) {
+ val shortcutManager: ShortcutManager = getSystemService() ?: return
+ val shortcut = Builder(this, item.fullPathToParent)
+ .setShortLabel(item.toString())
+ .setLongLabel(item.fullPathToParent + item.toString())
+ .setIcon(Icon.createWithResource(this, R.drawable.ic_lock_open_24px))
+ .setIntent(intent)
+ .build()
+ val shortcuts = shortcutManager.dynamicShortcuts
+ // If we're above or equal to the maximum shortcuts allowed, drop the last item.
+ if (shortcuts.size >= MAX_SHORTCUT_COUNT) {
+ shortcuts.removeLast()
}
+ // Reverse the list so we can append our new shortcut at the 'end'.
+ shortcuts.reverse()
+ shortcuts.add(shortcut)
+ // Reverse it again, so the previous items are now in the correct order and our new item
+ // is at the front like it's supposed to.
+ shortcuts.reverse()
+ // Write back the new shortcuts.
+ shortcutManager.dynamicShortcuts = shortcuts
}
private fun validateState(): Boolean {
@@ -677,6 +688,10 @@ class PasswordStore : BaseGitActivity() {
companion object {
+ // The max shortcut count from the system is set to 15 for some godforsaken reason, which
+ // makes zero sense and is why our update logic just never worked. Capping it at 4 which is
+ // what most launchers seem to have agreed upon is the only reasonable solution.
+ private const val MAX_SHORTCUT_COUNT = 4
const val REQUEST_ARG_PATH = "PATH"
private fun isPrintable(c: Char): Boolean {
val block = UnicodeBlock.of(c)