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()
|
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) {
|
private fun handle(intent: Intent) {
|
||||||
when (intent.action) {
|
when (intent.action) {
|
||||||
Intent.ACTION_VIEW -> ServerLink.parse(intent.dataString)?.let { sessions.open(it) }
|
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)
|
else -> openRequestedChat(intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ class SessionController(private val context: Context) {
|
|||||||
|
|
||||||
val settings: Settings = Settings.load()
|
val settings: Settings = Settings.load()
|
||||||
val icons = IconTheme(context, settings)
|
val icons = IconTheme(context, settings)
|
||||||
|
private val shortcuts = BookmarkShortcuts(context, icons)
|
||||||
val pictures = WebPictures(context.cacheDir, settings)
|
val pictures = WebPictures(context.cacheDir, settings)
|
||||||
internal val cacheDir: File get() = context.cacheDir
|
internal val cacheDir: File get() = context.cacheDir
|
||||||
private val bookmarkStore = Bookmarks.load()
|
private val bookmarkStore = Bookmarks.load()
|
||||||
@@ -228,6 +229,12 @@ class SessionController(private val context: Context) {
|
|||||||
_events.tryEmit(SessionEvent.ServerOpened)
|
_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) {
|
fun select(server: ServerController) {
|
||||||
if (server in _servers.value) _current.value = server
|
if (server in _servers.value) _current.value = server
|
||||||
}
|
}
|
||||||
@@ -340,6 +347,16 @@ class SessionController(private val context: Context) {
|
|||||||
private fun saveBookmarks() {
|
private fun saveBookmarks() {
|
||||||
bookmarkStore.save()
|
bookmarkStore.save()
|
||||||
_bookmarks.value = bookmarkStore.all().toList()
|
_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 ----
|
// ---- contacts ----
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ val BookmarkEditSaver = listSaver<BookmarkEdit?, Any>(
|
|||||||
save = { edit ->
|
save = { edit ->
|
||||||
val b = edit?.bookmark ?: return@listSaver emptyList()
|
val b = edit?.bookmark ?: return@listSaver emptyList()
|
||||||
listOf(edit.index, b.label.orEmpty(), b.address.orEmpty(), b.port, b.nickname.orEmpty(), b.password,
|
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 ->
|
restore = { saved ->
|
||||||
if (saved.isEmpty()) null
|
if (saved.isEmpty()) null
|
||||||
@@ -49,6 +49,7 @@ val BookmarkEditSaver = listSaver<BookmarkEdit?, Any>(
|
|||||||
channelPassword = saved[8] as String
|
channelPassword = saved[8] as String
|
||||||
connectOnStartup = saved[9] as Boolean
|
connectOnStartup = saved[9] as Boolean
|
||||||
iconId = saved[10] as Long
|
iconId = saved[10] as Long
|
||||||
|
id = saved[11] as String
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -131,7 +131,9 @@ fun ConnectScreen(
|
|||||||
}
|
}
|
||||||
itemsIndexed(bookmarks) { index, bookmark ->
|
itemsIndexed(bookmarks) { index, bookmark ->
|
||||||
BookmarkRow(bookmark, identities, { onConnect { vm.connect(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>,
|
identities: List<IdentityInfo>,
|
||||||
onConnect: () -> Unit,
|
onConnect: () -> Unit,
|
||||||
onEdit: () -> Unit,
|
onEdit: () -> Unit,
|
||||||
|
/** Null when the launcher takes no shortcuts. */
|
||||||
|
onPin: (() -> Unit)?,
|
||||||
onRemove: () -> Unit,
|
onRemove: () -> Unit,
|
||||||
) {
|
) {
|
||||||
val identity = identities.firstOrNull { it.id == bookmark.identityId }
|
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") }
|
IconButton(onClick = { menu = true }) { Icon(Icons.Default.MoreVert, "Bookmark options") }
|
||||||
DropdownMenu(menu, { menu = false }) {
|
DropdownMenu(menu, { menu = false }) {
|
||||||
DropdownMenuItem({ Text("Edit") }, onClick = { menu = false; onEdit() })
|
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() })
|
DropdownMenuItem({ Text("Remove") }, onClick = { menu = false; onRemove() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
package com.ts3client.config;
|
package com.ts3client.config;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/** A saved server for quick connect. */
|
/** A saved server for quick connect. */
|
||||||
public final class Bookmark {
|
public final class Bookmark {
|
||||||
|
/** Stays the same through edits, for whatever refers to the bookmark from outside, like a launcher shortcut. */
|
||||||
|
public String id = UUID.randomUUID().toString();
|
||||||
public String label;
|
public String label;
|
||||||
public String address;
|
public String address;
|
||||||
public int port = 9987;
|
public int port = 9987;
|
||||||
@@ -35,6 +39,7 @@ public final class Bookmark {
|
|||||||
/** A bookmark of its own with the same fields, to change without touching this one. */
|
/** A bookmark of its own with the same fields, to change without touching this one. */
|
||||||
public Bookmark copy() {
|
public Bookmark copy() {
|
||||||
Bookmark b = new Bookmark(label, address, port, nickname, password);
|
Bookmark b = new Bookmark(label, address, port, nickname, password);
|
||||||
|
b.id = id;
|
||||||
b.identityId = identityId;
|
b.identityId = identityId;
|
||||||
b.channel = channel;
|
b.channel = channel;
|
||||||
b.channelPassword = channelPassword;
|
b.channelPassword = channelPassword;
|
||||||
|
|||||||
@@ -47,6 +47,14 @@ public final class Bookmarks {
|
|||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The bookmark with this {@link Bookmark#id}, or null. */
|
||||||
|
public Bookmark find(String id) {
|
||||||
|
for (Bookmark b : entries) {
|
||||||
|
if (b.id.equals(id)) return b;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/** Bookmarks flagged to connect at startup, each in its own tab. */
|
/** Bookmarks flagged to connect at startup, each in its own tab. */
|
||||||
public List<Bookmark> startupBookmarks() {
|
public List<Bookmark> startupBookmarks() {
|
||||||
List<Bookmark> out = new ArrayList<>();
|
List<Bookmark> out = new ArrayList<>();
|
||||||
@@ -69,6 +77,8 @@ public final class Bookmarks {
|
|||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
String prefix = "bookmark." + i + ".";
|
String prefix = "bookmark." + i + ".";
|
||||||
Bookmark bm = new Bookmark();
|
Bookmark bm = new Bookmark();
|
||||||
|
String id = p.getProperty(prefix + "id", "");
|
||||||
|
if (!id.isBlank()) bm.id = id;
|
||||||
bm.label = p.getProperty(prefix + "label", "");
|
bm.label = p.getProperty(prefix + "label", "");
|
||||||
bm.address = p.getProperty(prefix + "address", "");
|
bm.address = p.getProperty(prefix + "address", "");
|
||||||
bm.port = parseInt(p.getProperty(prefix + "port"), 9987);
|
bm.port = parseInt(p.getProperty(prefix + "port"), 9987);
|
||||||
@@ -90,6 +100,7 @@ public final class Bookmarks {
|
|||||||
for (int i = 0; i < entries.size(); i++) {
|
for (int i = 0; i < entries.size(); i++) {
|
||||||
Bookmark bm = entries.get(i);
|
Bookmark bm = entries.get(i);
|
||||||
String prefix = "bookmark." + i + ".";
|
String prefix = "bookmark." + i + ".";
|
||||||
|
p.setProperty(prefix + "id", bm.id);
|
||||||
p.setProperty(prefix + "label", nullToEmpty(bm.label));
|
p.setProperty(prefix + "label", nullToEmpty(bm.label));
|
||||||
p.setProperty(prefix + "address", nullToEmpty(bm.address));
|
p.setProperty(prefix + "address", nullToEmpty(bm.address));
|
||||||
p.setProperty(prefix + "port", Integer.toString(bm.port));
|
p.setProperty(prefix + "port", Integer.toString(bm.port));
|
||||||
|
|||||||
Reference in New Issue
Block a user