Android: home-screen shortcuts to bookmarks
"Add to home screen" in a bookmark's menu pins a shortcut that connects to the bookmark, or goes to its channel when already on that server. The shortcut names the bookmark by a new stable id, so it follows edits and stops working once the bookmark is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package com.ts3client.android
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.ShortcutInfo
|
||||
import android.content.pm.ShortcutManager
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Rect
|
||||
import android.graphics.drawable.Icon
|
||||
import com.ts3client.config.Bookmark
|
||||
import com.ts3client.net.IconRepository
|
||||
|
||||
/**
|
||||
* Home-screen shortcuts that connect to a bookmark in one tap. A shortcut names its bookmark by
|
||||
* [Bookmark.id], so editing the bookmark carries over and removing it disables the shortcut.
|
||||
*/
|
||||
internal class BookmarkShortcuts(private val context: Context, private val icons: IconTheme) {
|
||||
|
||||
private val manager = context.getSystemService(ShortcutManager::class.java)
|
||||
|
||||
val supported: Boolean get() = manager?.isRequestPinShortcutSupported == true
|
||||
|
||||
/** Asks the launcher to pin a shortcut; it confirms with the user itself. */
|
||||
fun pin(bookmark: Bookmark) {
|
||||
manager?.requestPinShortcut(info(bookmark), null)
|
||||
}
|
||||
|
||||
/** Pinned shortcuts take on their bookmarks' current names and icons; those of removed ones stop working. */
|
||||
fun sync(bookmarks: List<Bookmark>) {
|
||||
val manager = manager ?: return
|
||||
val byId = bookmarks.associateBy { shortcutId(it.id) }
|
||||
val pinned = manager.pinnedShortcuts
|
||||
val gone = pinned.map { it.id }.filter { it !in byId }
|
||||
if (gone.isNotEmpty()) manager.disableShortcuts(gone, "Bookmark removed")
|
||||
val kept = pinned.mapNotNull { byId[it.id] }
|
||||
if (kept.isNotEmpty()) {
|
||||
manager.enableShortcuts(kept.map { shortcutId(it.id) })
|
||||
manager.updateShortcuts(kept.map(::info))
|
||||
}
|
||||
}
|
||||
|
||||
private fun info(bookmark: Bookmark): ShortcutInfo =
|
||||
ShortcutInfo.Builder(context, shortcutId(bookmark.id))
|
||||
.setShortLabel(bookmark.displayName())
|
||||
.setLongLabel(listOfNotNull(bookmark.displayName(), bookmark.channel.ifBlank { null }).joinToString(" · "))
|
||||
.setIcon(icon(bookmark.iconId))
|
||||
.setIntent(Intent(context, MainActivity::class.java)
|
||||
.setAction(ACTION_CONNECT)
|
||||
.putExtra(EXTRA_BOOKMARK, bookmark.id))
|
||||
.build()
|
||||
|
||||
/** The server's own icon as last seen there, else the pack's server icon, inside the adaptive icon's safe zone. */
|
||||
private fun icon(iconId: Long): Icon {
|
||||
val art = IconRepository.cached(iconId)?.let { BitmapFactory.decodeByteArray(it, 0, it.size) }
|
||||
?: icons.render(listOf("SERVER_GREEN", "BOOKMARK_MANAGER"), ART_PX, 0x202020)
|
||||
val canvas = Bitmap.createBitmap(ADAPTIVE_PX, ADAPTIVE_PX, Bitmap.Config.ARGB_8888)
|
||||
Canvas(canvas).apply {
|
||||
drawColor(BACKGROUND)
|
||||
if (art != null) {
|
||||
val inset = (ADAPTIVE_PX - ART_PX) / 2
|
||||
drawBitmap(art, null, Rect(inset, inset, inset + ART_PX, inset + ART_PX), null)
|
||||
}
|
||||
}
|
||||
return Icon.createWithAdaptiveBitmap(canvas)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val ACTION_CONNECT = "com.ts3client.android.CONNECT_BOOKMARK"
|
||||
const val EXTRA_BOOKMARK = "bookmark"
|
||||
|
||||
/** 108 dp at xxxhdpi; the inner 72 dp always shows, the art sits well within. */
|
||||
private const val ADAPTIVE_PX = 432
|
||||
private const val ART_PX = 192
|
||||
private const val BACKGROUND = 0xFFE8EEF4.toInt()
|
||||
|
||||
private fun shortcutId(bookmarkId: String) = "bookmark:$bookmarkId"
|
||||
}
|
||||
}
|
||||
@@ -37,10 +37,12 @@ class MainActivity : ComponentActivity() {
|
||||
super.onStop()
|
||||
}
|
||||
|
||||
/** A notification opens its conversation; a `ts3server://` link connects. */
|
||||
/** A notification opens its conversation; a `ts3server://` link or a bookmark shortcut connects. */
|
||||
private fun handle(intent: Intent) {
|
||||
when (intent.action) {
|
||||
Intent.ACTION_VIEW -> ServerLink.parse(intent.dataString)?.let { sessions.open(it) }
|
||||
BookmarkShortcuts.ACTION_CONNECT -> intent.getStringExtra(BookmarkShortcuts.EXTRA_BOOKMARK)
|
||||
?.let(sessions::openBookmark)
|
||||
else -> openRequestedChat(intent)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +85,7 @@ class SessionController(private val context: Context) {
|
||||
|
||||
val settings: Settings = Settings.load()
|
||||
val icons = IconTheme(context, settings)
|
||||
private val shortcuts = BookmarkShortcuts(context, icons)
|
||||
val pictures = WebPictures(context.cacheDir, settings)
|
||||
internal val cacheDir: File get() = context.cacheDir
|
||||
private val bookmarkStore = Bookmarks.load()
|
||||
@@ -228,6 +229,12 @@ class SessionController(private val context: Context) {
|
||||
_events.tryEmit(SessionEvent.ServerOpened)
|
||||
}
|
||||
|
||||
/** What a home-screen shortcut does: connects to the bookmark, or goes to its channel when already on its server. */
|
||||
fun openBookmark(id: String) {
|
||||
val bookmark = bookmarkStore.find(id) ?: return error("That bookmark no longer exists")
|
||||
open(ServerLink.of(bookmark), bookmark.identityId)
|
||||
}
|
||||
|
||||
fun select(server: ServerController) {
|
||||
if (server in _servers.value) _current.value = server
|
||||
}
|
||||
@@ -340,6 +347,16 @@ class SessionController(private val context: Context) {
|
||||
private fun saveBookmarks() {
|
||||
bookmarkStore.save()
|
||||
_bookmarks.value = bookmarkStore.all().toList()
|
||||
shortcuts.sync(_bookmarks.value)
|
||||
}
|
||||
|
||||
val canPinShortcuts: Boolean get() = shortcuts.supported
|
||||
|
||||
/** Asks the launcher for a home-screen shortcut connecting to [bookmark]. */
|
||||
fun pinShortcut(bookmark: Bookmark) {
|
||||
// A bookmark not saved since ids came in has a new one on every load until it is.
|
||||
bookmarkStore.save()
|
||||
shortcuts.pin(bookmark)
|
||||
}
|
||||
|
||||
// ---- contacts ----
|
||||
|
||||
@@ -38,7 +38,7 @@ val BookmarkEditSaver = listSaver<BookmarkEdit?, Any>(
|
||||
save = { edit ->
|
||||
val b = edit?.bookmark ?: return@listSaver emptyList()
|
||||
listOf(edit.index, b.label.orEmpty(), b.address.orEmpty(), b.port, b.nickname.orEmpty(), b.password,
|
||||
b.identityId, b.channel, b.channelPassword, b.connectOnStartup, b.iconId)
|
||||
b.identityId, b.channel, b.channelPassword, b.connectOnStartup, b.iconId, b.id)
|
||||
},
|
||||
restore = { saved ->
|
||||
if (saved.isEmpty()) null
|
||||
@@ -49,6 +49,7 @@ val BookmarkEditSaver = listSaver<BookmarkEdit?, Any>(
|
||||
channelPassword = saved[8] as String
|
||||
connectOnStartup = saved[9] as Boolean
|
||||
iconId = saved[10] as Long
|
||||
id = saved[11] as String
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
@@ -131,7 +131,9 @@ fun ConnectScreen(
|
||||
}
|
||||
itemsIndexed(bookmarks) { index, bookmark ->
|
||||
BookmarkRow(bookmark, identities, { onConnect { vm.connect(bookmark) } },
|
||||
onEdit = { onEditBookmark(BookmarkEdit(index, bookmark)) }, onRemove = { vm.removeBookmark(bookmark) })
|
||||
onEdit = { onEditBookmark(BookmarkEdit(index, bookmark)) },
|
||||
onPin = if (vm.canPinShortcuts) ({ vm.pinShortcut(bookmark) }) else null,
|
||||
onRemove = { vm.removeBookmark(bookmark) })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -186,6 +188,8 @@ private fun BookmarkRow(
|
||||
identities: List<IdentityInfo>,
|
||||
onConnect: () -> Unit,
|
||||
onEdit: () -> Unit,
|
||||
/** Null when the launcher takes no shortcuts. */
|
||||
onPin: (() -> Unit)?,
|
||||
onRemove: () -> Unit,
|
||||
) {
|
||||
val identity = identities.firstOrNull { it.id == bookmark.identityId }
|
||||
@@ -202,6 +206,7 @@ private fun BookmarkRow(
|
||||
IconButton(onClick = { menu = true }) { Icon(Icons.Default.MoreVert, "Bookmark options") }
|
||||
DropdownMenu(menu, { menu = false }) {
|
||||
DropdownMenuItem({ Text("Edit") }, onClick = { menu = false; onEdit() })
|
||||
onPin?.let { DropdownMenuItem({ Text("Add to home screen") }, onClick = { menu = false; it() }) }
|
||||
DropdownMenuItem({ Text("Remove") }, onClick = { menu = false; onRemove() })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user