Use Spotify API if an API key is provided

This commit is contained in:
James Babcock committed 2026-10-31 17:03:09 -07:00
1 parent f47d82b871
commit 49ea8a7d74
18 files changed
+736 -39

No files matched your search

+54 -1
View File
@@ -2,6 +2,9 @@ import { Utils } from "@nativescript/core";
import { ignoredMediaPackages, isMediaAppEnabled, MEDIA_APPS_KEY, rememberMediaApps } from "./media-apps";
import { onSettingsStoreChanged } from "./settings-store";
import { parseMediaBrowseItems, type MediaBrowseItem, type MediaBrowserApp, type MediaBrowserBridge } from "./media-types";
import { spotifyService } from "./spotify";
import type { SpotifyLibrary } from "./spotify-library";
import { SPOTIFY_ANDROID_PACKAGE } from "./spotify-player";
declare const com: any;
@@ -157,4 +160,54 @@ export class FaceclawMediaBrowserBridge implements MediaBrowserBridge {
}
}
export const mediaBrowserBridge: MediaBrowserBridge = new FaceclawMediaBrowserBridge();
/**
* The Android libraries: apps' media browser services, with Spotify's
* library from its Web API instead. Spotify's own browser service admits
* only allowlisted callers (Android Auto, Wear, Assistant), so connecting to
* it always fails; signed in, its row opens the Web API library, and signed
* out, opening it says how to sign in. Signed in with Spotify not installed,
* the library is still offered (it plays on the user's other devices).
*/
export class AndroidMediaBrowserBridge implements MediaBrowserBridge {
private current: MediaBrowserBridge | null = null;
constructor(
private readonly services: MediaBrowserBridge,
private readonly spotify: () => SpotifyLibrary | null,
private readonly isEnabled: (packageName: string) => boolean = isMediaAppEnabled,
) {}
listBrowsableApps(refresh = false): MediaBrowserApp[] {
const apps = this.services.listBrowsableApps(refresh);
const library = this.spotify();
if (!library || apps.some((app) => app.packageName === SPOTIFY_ANDROID_PACKAGE) || !this.isEnabled(SPOTIFY_ANDROID_PACKAGE)) {
return apps;
}
return [...apps, ...library.listBrowsableApps()];
}
connect(app: MediaBrowserApp): Promise<string> {
this.disconnect();
const library = app.packageName === SPOTIFY_ANDROID_PACKAGE ? this.spotify() : null;
this.current = library ?? this.services;
return this.current.connect(library ? library.app : app);
}
browse(parentId: string): Promise<MediaBrowseItem[]> {
return this.current ? this.current.browse(parentId) : Promise.reject(new Error("Not connected"));
}
playFromMediaId(mediaId: string): void {
this.current?.playFromMediaId(mediaId);
}
disconnect(): void {
this.current?.disconnect();
this.current = null;
}
}
export const mediaBrowserBridge: MediaBrowserBridge = new AndroidMediaBrowserBridge(
new FaceclawMediaBrowserBridge(),
() => spotifyService()?.library ?? null,
);