Add media-browser to Music app

This commit is contained in:
James Babcock committed 2026-07-24 10:02:03 -07:00
1 parent d42e11e2a5
commit d2c987d48a
5 files changed
+856 -5

No files matched your search

@@ -0,0 +1,321 @@
package com.faceclaw.app;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.media.browse.MediaBrowser;
import android.media.session.MediaController;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Client for other apps' MediaBrowserService implementations (the mechanism
* Android Auto uses). Binding starts the target player's process even when it
* is not running, so this can list a player's library (playlists, albums, ...)
* and start playback without the phone being unlocked.
*
* Holds at most one connection at a time. All MediaBrowser interaction happens
* on the main thread; results are reported through
* FaceclawMediaBrowserListener, also on the main thread.
*/
public class FaceclawMediaBrowser {
private static final String TAG = "FaceclawMediaBrowser";
private static final String BROWSER_SERVICE_ACTION = "android.media.browse.MediaBrowserService";
// Generous: connecting may cold-start the player's process.
private static final long CONNECT_TIMEOUT_MS = 15_000;
private static final long BROWSE_TIMEOUT_MS = 15_000;
// Unbinding immediately after a play command can race the player promoting
// itself to a foreground service, letting the system kill its process
// before playback starts; keep the binding alive briefly instead.
private static final long PLAY_DISCONNECT_GRACE_MS = 5_000;
private final Context appContext;
private final Handler mainHandler = new Handler(Looper.getMainLooper());
private volatile FaceclawMediaBrowserListener listener;
// Main-thread only.
private MediaBrowser browser;
private int generation;
private long lastPlayCommandAtMs;
public FaceclawMediaBrowser(Context context) {
this.appContext = context.getApplicationContext();
}
public void setListener(FaceclawMediaBrowserListener listener) {
this.listener = listener;
}
/**
* Installed apps exposing a media browser service, as JSON sorted by app
* name: [{"packageName": ..., "serviceClass": ..., "appName": ...}, ...].
* Synchronous (PackageManager lookup only).
*/
public String listBrowsableAppsJson() {
PackageManager packageManager = appContext.getPackageManager();
List<ResolveInfo> services;
try {
services = packageManager.queryIntentServices(new Intent(BROWSER_SERVICE_ACTION), 0);
} catch (Exception e) {
Log.w(TAG, "media browser service query failed", e);
return "[]";
}
List<JSONObject> apps = new ArrayList<>();
for (ResolveInfo service : services) {
if (service.serviceInfo == null) {
continue;
}
try {
CharSequence label = service.serviceInfo.applicationInfo.loadLabel(packageManager);
JSONObject app = new JSONObject();
app.put("packageName", service.serviceInfo.packageName);
app.put("serviceClass", service.serviceInfo.name);
app.put("appName", label == null ? service.serviceInfo.packageName : label.toString());
apps.add(app);
} catch (Exception e) {
Log.w(TAG, "media browser service entry failed", e);
}
}
Collections.sort(apps, (a, b) -> a.optString("appName").compareToIgnoreCase(b.optString("appName")));
return new JSONArray(apps).toString();
}
/**
* Connect to an app's media browser service, starting its process if
* needed; replaces any existing connection. Async; reports through
* onConnectResult with the browse-tree root id on success.
*/
public void connect(int requestId, String packageName, String serviceClass) {
mainHandler.post(() -> {
closeBrowser();
final int myGeneration = generation;
final boolean[] settled = {false};
final MediaBrowser[] browserRef = new MediaBrowser[1];
MediaBrowser newBrowser = new MediaBrowser(
appContext,
new ComponentName(packageName, serviceClass),
new MediaBrowser.ConnectionCallback() {
@Override
public void onConnected() {
if (generation != myGeneration || settled[0]) {
return;
}
settled[0] = true;
String rootId = "";
try {
rootId = browserRef[0].getRoot();
} catch (Exception e) {
Log.w(TAG, "getRoot failed", e);
}
emitConnectResult(requestId, true, rootId, "");
}
@Override
public void onConnectionFailed() {
if (generation != myGeneration || settled[0]) {
return;
}
settled[0] = true;
closeBrowser();
emitConnectResult(requestId, false, "", "Connection refused by " + packageName);
}
@Override
public void onConnectionSuspended() {
if (generation != myGeneration) {
return;
}
FaceclawMediaBrowserListener currentListener = listener;
if (currentListener != null) {
currentListener.onDisconnected();
}
}
},
null
);
browserRef[0] = newBrowser;
browser = newBrowser;
mainHandler.postDelayed(() -> {
if (generation != myGeneration || settled[0]) {
return;
}
settled[0] = true;
closeBrowser();
emitConnectResult(requestId, false, "", "Connection to " + packageName + " timed out");
}, CONNECT_TIMEOUT_MS);
try {
newBrowser.connect();
} catch (Exception e) {
if (!settled[0]) {
settled[0] = true;
closeBrowser();
emitConnectResult(requestId, false, "", "Connection failed: " + e.getMessage());
}
}
});
}
/**
* Fetch one level of the connected service's content tree. Async; reports
* through onBrowseResult with children as JSON:
* [{"mediaId": ..., "title": ..., "subtitle": ..., "browsable": bool, "playable": bool}, ...].
*/
public void browse(int requestId, String parentId) {
mainHandler.post(() -> {
final MediaBrowser currentBrowser = browser;
if (currentBrowser == null || !currentBrowser.isConnected()) {
emitBrowseResult(requestId, "", "Not connected");
return;
}
final int myGeneration = generation;
final boolean[] settled = {false};
mainHandler.postDelayed(() -> {
if (generation != myGeneration || settled[0]) {
return;
}
settled[0] = true;
try {
currentBrowser.unsubscribe(parentId);
} catch (Exception ignored) {
}
emitBrowseResult(requestId, "", "Browse timed out");
}, BROWSE_TIMEOUT_MS);
try {
currentBrowser.subscribe(parentId, new MediaBrowser.SubscriptionCallback() {
@Override
public void onChildrenLoaded(String loadedParentId, List<MediaBrowser.MediaItem> children) {
if (generation != myGeneration || settled[0]) {
return;
}
settled[0] = true;
// One snapshot is all we want; unsubscribe outside the callback.
mainHandler.post(() -> {
try {
currentBrowser.unsubscribe(loadedParentId);
} catch (Exception ignored) {
}
});
emitBrowseResult(requestId, serializeChildren(children), "");
}
@Override
public void onError(String erroredParentId) {
if (generation != myGeneration || settled[0]) {
return;
}
settled[0] = true;
mainHandler.post(() -> {
try {
currentBrowser.unsubscribe(erroredParentId);
} catch (Exception ignored) {
}
});
emitBrowseResult(requestId, "", "Browse failed");
}
});
} catch (Exception e) {
if (!settled[0]) {
settled[0] = true;
emitBrowseResult(requestId, "", "Browse failed: " + e.getMessage());
}
}
});
}
/** Start playback of a browsed item through the connected service's session. */
public void playFromMediaId(String mediaId) {
mainHandler.post(() -> {
if (browser == null || !browser.isConnected()) {
return;
}
try {
MediaController controller = new MediaController(appContext, browser.getSessionToken());
controller.getTransportControls().playFromMediaId(mediaId, null);
lastPlayCommandAtMs = SystemClock.elapsedRealtime();
} catch (Exception e) {
Log.w(TAG, "playFromMediaId failed", e);
}
});
}
public void disconnect() {
mainHandler.post(() -> {
long sincePlayMs = SystemClock.elapsedRealtime() - lastPlayCommandAtMs;
long graceMs = lastPlayCommandAtMs == 0 ? 0 : PLAY_DISCONNECT_GRACE_MS - sincePlayMs;
if (graceMs <= 0) {
closeBrowser();
return;
}
final MediaBrowser lingering = browser;
browser = null;
generation++;
if (lingering != null) {
mainHandler.postDelayed(() -> {
try {
lingering.disconnect();
} catch (Exception ignored) {
}
}, graceMs);
}
});
}
private void closeBrowser() {
generation++;
if (browser != null) {
try {
browser.disconnect();
} catch (Exception ignored) {
}
browser = null;
}
}
private String serializeChildren(List<MediaBrowser.MediaItem> children) {
JSONArray out = new JSONArray();
if (children == null) {
return out.toString();
}
for (MediaBrowser.MediaItem item : children) {
try {
CharSequence title = item.getDescription() == null ? null : item.getDescription().getTitle();
CharSequence subtitle = item.getDescription() == null ? null : item.getDescription().getSubtitle();
JSONObject entry = new JSONObject();
entry.put("mediaId", item.getMediaId() == null ? "" : item.getMediaId());
entry.put("title", title == null ? "" : title.toString());
entry.put("subtitle", subtitle == null ? "" : subtitle.toString());
entry.put("browsable", item.isBrowsable());
entry.put("playable", item.isPlayable());
out.put(entry);
} catch (Exception e) {
Log.w(TAG, "media item serialization failed", e);
}
}
return out.toString();
}
private void emitConnectResult(int requestId, boolean connected, String rootId, String error) {
FaceclawMediaBrowserListener currentListener = listener;
if (currentListener != null) {
currentListener.onConnectResult(requestId, connected, rootId, error);
}
}
private void emitBrowseResult(int requestId, String childrenJson, String error) {
FaceclawMediaBrowserListener currentListener = listener;
if (currentListener != null) {
currentListener.onBrowseResult(requestId, childrenJson, error);
}
}
}
@@ -0,0 +1,10 @@
package com.faceclaw.app;
public interface FaceclawMediaBrowserListener {
void onConnectResult(int requestId, boolean connected, String rootId, String error);
void onBrowseResult(int requestId, String childrenJson, String error);
/** The service connection dropped after connecting (player crashed or was killed). */
void onDisconnected();
}
+164
View File
@@ -0,0 +1,164 @@
import { Utils } from "@nativescript/core";
declare const com: any;
export type MediaBrowserApp = {
packageName: string;
serviceClass: string;
appName: string;
};
export type MediaBrowseItem = {
mediaId: string;
title: string;
subtitle: string;
browsable: boolean;
playable: boolean;
};
type PendingRequest<T> = {
resolve: (value: T) => void;
reject: (error: Error) => void;
};
/**
* Bridge to FaceclawMediaBrowser: connects to other apps' media browser
* services (the mechanism Android Auto uses) to browse their libraries and
* start playback, including starting a player that is not currently running.
* Holds at most one connection at a time.
*/
export class FaceclawMediaBrowserBridge {
private browser: any | null = null;
private listenerProxy: any | null = null;
private nextRequestId = 1;
private readonly pendingConnects = new Map<number, PendingRequest<string>>();
private readonly pendingBrowses = new Map<number, PendingRequest<MediaBrowseItem[]>>();
private appsCache: MediaBrowserApp[] | null = null;
/** Installed apps exposing a media browser service; cached after the first call. */
listBrowsableApps(refresh = false): MediaBrowserApp[] {
if (!global.isAndroid) return [];
if (this.appsCache && !refresh) return this.appsCache;
this.ensureBrowser();
if (!this.browser) return [];
try {
const raw = JSON.parse(String(this.browser.listBrowsableAppsJson())) as Array<{
packageName?: string;
serviceClass?: string;
appName?: string;
}>;
this.appsCache = raw.map((app) => ({
packageName: String(app.packageName ?? ""),
serviceClass: String(app.serviceClass ?? ""),
appName: String(app.appName ?? ""),
}));
} catch (error) {
console.warn(`media browser app list parse failed: ${error}`);
this.appsCache = [];
}
return this.appsCache;
}
/**
* Connect to an app's browser service, starting its process if needed;
* replaces any existing connection. Resolves to the browse-tree root id.
*/
connect(app: MediaBrowserApp): Promise<string> {
if (!global.isAndroid) return Promise.reject(new Error("Android only"));
this.ensureBrowser();
if (!this.browser) return Promise.reject(new Error("Media browser unavailable"));
const requestId = this.nextRequestId++;
return new Promise<string>((resolve, reject) => {
this.pendingConnects.set(requestId, { resolve, reject });
this.browser.connect(requestId, app.packageName, app.serviceClass);
});
}
/** Fetch one level of the connected service's content tree. */
browse(parentId: string): Promise<MediaBrowseItem[]> {
if (!global.isAndroid) return Promise.reject(new Error("Android only"));
this.ensureBrowser();
if (!this.browser) return Promise.reject(new Error("Media browser unavailable"));
const requestId = this.nextRequestId++;
return new Promise<MediaBrowseItem[]>((resolve, reject) => {
this.pendingBrowses.set(requestId, { resolve, reject });
this.browser.browse(requestId, parentId);
});
}
/** Start playback of a browsed item through the connected player's session. */
playFromMediaId(mediaId: string): void {
if (!global.isAndroid) return;
this.browser?.playFromMediaId(mediaId);
}
/** Drop the connection; in-flight requests are rejected. */
disconnect(): void {
if (!global.isAndroid) return;
this.rejectAllPending("Disconnected");
this.browser?.disconnect();
}
private rejectAllPending(reason: string): void {
const pending = [...this.pendingConnects.values(), ...this.pendingBrowses.values()];
this.pendingConnects.clear();
this.pendingBrowses.clear();
for (const request of pending) {
request.reject(new Error(reason));
}
}
private ensureBrowser(): void {
if (!global.isAndroid || this.browser) return;
const context = Utils.android.getApplicationContext();
if (!context) {
throw new Error("Android application context unavailable");
}
this.browser = new com.faceclaw.app.FaceclawMediaBrowser(context);
this.listenerProxy = new com.faceclaw.app.FaceclawMediaBrowserListener({
onConnectResult: (requestId: number, connected: boolean, rootId: string, error: string) => {
const pending = this.pendingConnects.get(Number(requestId));
if (!pending) return;
this.pendingConnects.delete(Number(requestId));
if (connected) pending.resolve(String(rootId));
else pending.reject(new Error(String(error) || "Connection failed"));
},
onBrowseResult: (requestId: number, childrenJson: string, error: string) => {
const pending = this.pendingBrowses.get(Number(requestId));
if (!pending) return;
this.pendingBrowses.delete(Number(requestId));
const json = String(childrenJson ?? "");
if (!json) {
pending.reject(new Error(String(error) || "Browse failed"));
return;
}
try {
const raw = JSON.parse(json) as Array<{
mediaId?: string;
title?: string;
subtitle?: string;
browsable?: boolean;
playable?: boolean;
}>;
pending.resolve(
raw.map((item) => ({
mediaId: String(item.mediaId ?? ""),
title: String(item.title ?? ""),
subtitle: String(item.subtitle ?? ""),
browsable: Boolean(item.browsable),
playable: Boolean(item.playable),
})),
);
} catch (parseError) {
pending.reject(new Error(`Browse result parse failed: ${parseError}`));
}
},
onDisconnected: () => {
this.rejectAllPending("Connection lost");
},
});
this.browser.setListener(this.listenerProxy);
}
}
export const mediaBrowserBridge = new FaceclawMediaBrowserBridge();
+304
View File
@@ -0,0 +1,304 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures";
import {
MenuLayer,
drawListScrollbar,
drawSelectionHighlight,
drawSubmenuIndicator,
scrollToKeepSelectionVisible,
} from "../menu";
import {
mediaBrowserBridge,
type MediaBrowseItem,
type MediaBrowserApp,
} from "../../native/media-browser";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
const HEADER_HEIGHT = 34;
const ROW_HEIGHT = 16;
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
export type MediaBrowseOptions = {
app: MediaBrowserApp;
/** Called after playback of a picked item has been requested. */
onPlayed: (ctx: LayerContext) => void;
/** Double-click at the library root (leave the browser). */
onLeave: (ctx: LayerContext) => void;
};
type BrowseLevel = {
parentId: string;
title: string;
/** null while the listing is loading. */
items: MediaBrowseItem[] | null;
error: string;
selectedIndex: number;
scrollRow: number;
};
type BrowsePhase = "idle" | "connecting" | "ready" | "failed";
/**
* Library browser over another app's MediaBrowserService (the Android Auto
* mechanism): connecting starts the player's process if needed, so this works
* with no media session active and the phone locked. Scroll to select, click
* to open a folder or play an item, double-click to go up (leaving the browser
* when already at the root).
*/
export class MediaBrowseLayer implements Layer {
private phase: BrowsePhase = "idle";
private connectError = "";
private levels: BrowseLevel[] = [];
private removed = false;
constructor(private readonly options: MediaBrowseOptions) {}
paint(ctx: LayerContext): GrayImage {
if (this.phase === "idle") {
this.startConnect(ctx);
}
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(font, 20, 8, `Library: ${this.options.app.appName}`, 220);
if (this.phase === "connecting" || this.phase === "idle") {
image.drawText(font, 24, HEADER_HEIGHT + 8, `Starting ${this.options.app.appName}...`, 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
if (this.phase === "failed") {
image.drawText(font, 24, HEADER_HEIGHT + 8, "Could not connect:", 180);
image.drawText(font, 24, HEADER_HEIGHT + 24, truncateRight(font, this.connectError, width - 48), 140);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const level = this.currentLevel();
const path = this.levels.map((entry) => entry.title).join(" / ");
image.drawText(font, 20, 22, truncateLeft(font, path, width - 40), 130);
if (!level || level.items === null) {
image.drawText(font, 24, HEADER_HEIGHT + 8, level?.error ? level.error : "Loading...", level?.error ? 140 : 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
const items = level.items;
if (!items.length) {
image.drawText(font, 24, HEADER_HEIGHT + 8, "(empty)", 120);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
level.selectedIndex = clamp(level.selectedIndex, 0, items.length - 1);
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
level.scrollRow = scrollToKeepSelectionVisible(level.scrollRow, level.selectedIndex, visibleRows, items.length);
const rowWidth = width - 2 * LIST_X;
const lastVisible = Math.min(items.length, level.scrollRow + visibleRows);
for (let index = level.scrollRow; index < lastVisible; index++) {
const item = items[index]!;
const y = HEADER_HEIGHT + (index - level.scrollRow) * ROW_HEIGHT;
const selected = index === level.selectedIndex;
const highlightX = LIST_X - 6;
const highlightY = y - 1;
const highlightWidth = rowWidth + 12;
const highlightHeight = ROW_HEIGHT - 1;
if (selected) {
drawSelectionHighlight(image, highlightX, highlightY, highlightWidth, highlightHeight, ctx.stack.isFocused(), 4);
}
const value = selected ? 255 : 200;
const title = truncateRight(font, item.title || "(untitled)", rowWidth - 16);
image.drawText(font, LIST_X, y + 1, title, value);
if (item.subtitle) {
const titleWidth = font.measureText(title);
const subtitleWidth = font.measureText(item.subtitle);
if (titleWidth + subtitleWidth + 24 <= rowWidth - 16) {
image.drawText(font, LIST_X + rowWidth - 16 - subtitleWidth, y + 1, item.subtitle, 110);
}
}
if (item.browsable) {
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, selected ? value : 110);
}
}
if (items.length > visibleRows) {
drawListScrollbar(
image,
width - 12,
HEADER_HEIGHT,
visibleRows * ROW_HEIGHT - 4,
level.scrollRow,
visibleRows,
items.length,
);
}
image.drawText(
font,
20,
height - 16,
`${GESTURE_SCROLL} select ${GESTURE_CLICK} open/play ${GESTURE_DOUBLE_CLICK} up / back`,
110,
);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (this.phase === "failed") {
if (event.type === "click") {
this.phase = "idle";
this.connectError = "";
} else if (event.type === "double-click") {
this.leave(ctx);
}
return;
}
if (event.type === "double-click") {
if (this.levels.length > 1) {
this.levels.pop();
} else {
this.leave(ctx);
}
return;
}
const level = this.currentLevel();
const items = level?.items;
if (!level || !items || !items.length) return;
switch (event.type) {
case "scroll-up":
level.selectedIndex = Math.max(0, level.selectedIndex - 1);
return;
case "scroll-down":
level.selectedIndex = Math.min(items.length - 1, level.selectedIndex + 1);
return;
case "click": {
const item = items[clamp(level.selectedIndex, 0, items.length - 1)];
if (!item) return;
if (item.browsable && item.playable) {
ctx.stack.push(
new MenuLayer(truncateRight(getDefaultSmallFont(), item.title || "(untitled)", 230), [
{
label: "Play",
onSelect: (menuCtx) => {
menuCtx.stack.pop();
this.play(menuCtx, item);
},
},
{
label: "Open",
onSelect: (menuCtx) => {
menuCtx.stack.pop();
this.descendInto(menuCtx, item);
},
},
]),
);
} else if (item.browsable) {
this.descendInto(ctx, item);
} else if (item.playable) {
this.play(ctx, item);
}
return;
}
default:
return;
}
}
onRemoved(): void {
this.removed = true;
mediaBrowserBridge.disconnect();
}
private currentLevel(): BrowseLevel | undefined {
return this.levels[this.levels.length - 1];
}
private leave(ctx: LayerContext): void {
this.options.onLeave(ctx);
}
private play(ctx: LayerContext, item: MediaBrowseItem): void {
mediaBrowserBridge.playFromMediaId(item.mediaId);
this.options.onPlayed(ctx);
}
private descendInto(ctx: LayerContext, item: MediaBrowseItem): void {
const level: BrowseLevel = {
parentId: item.mediaId,
title: item.title || "(untitled)",
items: null,
error: "",
selectedIndex: 0,
scrollRow: 0,
};
this.levels.push(level);
this.loadLevel(ctx, level);
}
private startConnect(ctx: LayerContext): void {
this.phase = "connecting";
mediaBrowserBridge
.connect(this.options.app)
.then((rootId) => {
if (this.removed) return;
this.phase = "ready";
const root: BrowseLevel = {
parentId: rootId,
title: "Library",
items: null,
error: "",
selectedIndex: 0,
scrollRow: 0,
};
this.levels = [root];
this.loadLevel(ctx, root);
ctx.actions.requestRender();
})
.catch((error) => {
if (this.removed) return;
this.phase = "failed";
this.connectError = error instanceof Error ? error.message : String(error);
ctx.actions.requestRender();
});
}
private loadLevel(ctx: LayerContext, level: BrowseLevel): void {
mediaBrowserBridge
.browse(level.parentId)
.then((items) => {
if (this.removed) return;
level.items = items;
ctx.actions.requestRender();
})
.catch((error) => {
if (this.removed) return;
level.error = error instanceof Error ? error.message : String(error);
ctx.actions.requestRender();
});
}
}
function truncateRight(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}
function truncateLeft(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`...${out}`) > maxWidth) {
out = out.slice(1);
}
return `...${out}`;
}
+57 -5
View File
@@ -9,12 +9,14 @@ import {
GESTURE_SCROLL_DOWN,
GESTURE_SCROLL_UP,
} from "../gestures";
import { drawSelectionHighlight, drawSubmenuIndicator } from "../menu";
import { MenuLayer, drawSelectionHighlight, drawSubmenuIndicator } from "../menu";
import {
mediaControllerBridge,
type MediaControllerState,
type MediaQueueItem,
} from "../../native/media-controller";
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
import { MediaBrowseLayer } from "../apps/media-browse";
import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../layers";
import {
createInProcessWindow,
@@ -44,7 +46,8 @@ type MusicAction =
| { kind: "previous"; label: string; enabled: boolean }
| { kind: "next"; label: string; enabled: boolean }
| { kind: "volume"; label: string; enabled: boolean }
| { kind: "playlist"; label: string; enabled: boolean };
| { kind: "playlist"; label: string; enabled: boolean }
| { kind: "browse"; label: string; enabled: boolean };
type FocusColumn = "actions" | "playlist";
@@ -86,8 +89,14 @@ class MusicAppLayer implements Layer {
if (!media.available) {
image.drawText(font, 24, 16, "No active media session.", 180);
image.drawText(font, 24, 34, "Start playback in another app on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
if (mediaBrowserBridge.listBrowsableApps().length) {
image.drawText(font, 24, 34, "Click to browse a music app's library,", 150);
image.drawText(font, 24, 48, "or start playback on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} browse ${GESTURE_DOUBLE_CLICK} back`, 110);
} else {
image.drawText(font, 24, 34, "Start playback in another app on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
}
return image;
}
@@ -138,7 +147,7 @@ class MusicAppLayer implements Layer {
}
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
image.drawText(font, ACTION_X, y + 1, truncate(font, action.label, ACTION_WIDTH - 16), value);
if (action.kind === "playlist" && action.enabled) {
if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) {
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, value);
}
}
@@ -204,6 +213,12 @@ class MusicAppLayer implements Layer {
}
return;
}
if (!media.available) {
if (event.type === "click") {
this.openBrowse(ctx);
}
return;
}
const queue = mediaControllerBridge.getQueue();
const actions = this.buildActions(media, queue);
this.reconcileSelection(actions, queue);
@@ -238,6 +253,7 @@ class MusicAppLayer implements Layer {
if (action.kind === "play-pause") await mediaControllerBridge.playPause();
else if (action.kind === "previous") await mediaControllerBridge.skipPrevious();
else if (action.kind === "next") await mediaControllerBridge.skipNext();
else if (action.kind === "browse") this.openBrowse(ctx);
else if (action.kind === "volume") {
ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent()));
} else if (action.kind === "playlist") {
@@ -252,6 +268,41 @@ class MusicAppLayer implements Layer {
}
}
/**
* Open the media-library browser (the Android Auto MediaBrowserService
* path): with one browsable player installed go straight to its library,
* otherwise offer a picker first.
*/
private openBrowse(ctx: LayerContext): void {
const apps = mediaBrowserBridge.listBrowsableApps(true);
if (!apps.length) return;
const pushBrowser = (target: LayerContext, app: MediaBrowserApp) => {
target.stack.push(
new MediaBrowseLayer({
app,
onPlayed: (browseCtx) => browseCtx.stack.pop(),
onLeave: (browseCtx) => browseCtx.stack.pop(),
}),
);
};
if (apps.length === 1) {
pushBrowser(ctx, apps[0]!);
return;
}
ctx.stack.push(
new MenuLayer(
"Browse library",
apps.map((app) => ({
label: app.appName,
onSelect: (menuCtx: LayerContext) => {
menuCtx.stack.pop();
pushBrowser(menuCtx, app);
},
})),
),
);
}
private buildActions(media: MediaControllerState, queue: MediaQueueItem[]): MusicAction[] {
const volume = mediaControllerBridge.getMediaVolumePercent();
return [
@@ -261,6 +312,7 @@ class MusicAppLayer implements Layer {
enabled: media.canPlayPause,
},
{ kind: "playlist", label: "Playlist", enabled: queue.length > 0 },
{ kind: "browse", label: "Browse library", enabled: mediaBrowserBridge.listBrowsableApps().length > 0 },
{ kind: "volume", label: volume >= 0 ? `Volume (${volume})` : "Volume", enabled: volume >= 0 },
{ kind: "next", label: "Next track", enabled: media.canSkipNext },
{ kind: "previous", label: "Previous track", enabled: media.canSkipPrevious },