Add an iOS Spotify integration

This commit is contained in:
James Babcock committed 2026-10-24 00:18:04 -07:00
1 parent e36fb14a34
commit c9cf29e5c0
32 files changed
+3176 -50

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@ TARGETED_DEVICE_FAMILY = 1;
CODE_SIGN_STYLE = Automatic;
// Set IOS_TEAM_ID in build_paths.sh (or pass --teamId) to choose the signing team.
OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework SafariServices -framework UniformTypeIdentifiers -framework ImageIO -framework EventKit -framework MediaPlayer
OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework SafariServices -framework UniformTypeIdentifiers -framework ImageIO -framework EventKit -framework MediaPlayer -framework AuthenticationServices -framework Security
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
+2 -24
View File
@@ -1,4 +1,5 @@
#import "FaceclawAppleMusic.h"
#import "FaceclawGraphics.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <UIKit/UIKit.h>
@@ -219,30 +220,7 @@ static NSDictionary *BrowseRow(NSString *mediaId, NSString *title, NSString *sub
- (NSData *)albumArt:(NSInteger)maxSize {
if (maxSize <= 0 || maxSize > 1024 || ![self hasPermission]) return nil;
MPMediaItemArtwork *artwork = MPMusicPlayerController.systemMusicPlayer.nowPlayingItem.artwork;
UIImage *image = [artwork imageWithSize:CGSizeMake(maxSize, maxSize)];
CGImageRef source = image.CGImage;
if (!source) return nil;
double sourceWidth = CGImageGetWidth(source), sourceHeight = CGImageGetHeight(source);
if (sourceWidth <= 0 || sourceHeight <= 0) return nil;
double scale = MIN(1.0, MIN(maxSize / sourceWidth, maxSize / sourceHeight));
NSInteger width = MAX(1, lround(sourceWidth * scale)), height = MAX(1, lround(sourceHeight * scale));
NSMutableData *packet = [NSMutableData dataWithLength:4 + width * height * 4];
uint8_t *bytes = packet.mutableBytes;
bytes[0] = width & 255; bytes[1] = (width >> 8) & 255;
bytes[2] = height & 255; bytes[3] = (height >> 8) & 255;
uint8_t *pixels = bytes + 4;
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
// Memory order B, G, R, A: one little-endian ARGB word per pixel.
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * 4, space,
kCGBitmapByteOrder32Little | (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(space);
if (!context) return nil;
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), source);
CGContextRelease(context);
// Composited over black (the display's see-through background); now opaque.
for (NSInteger i = 0; i < width * height; i++) pixels[i * 4 + 3] = 255;
return packet;
return [FaceclawGraphics bgraPacket:[artwork imageWithSize:CGSizeMake(maxSize, maxSize)].CGImage maxSize:maxSize];
}
#pragma mark - Queue
+4
View File
@@ -2,4 +2,8 @@
@interface FaceclawCrypto : NSObject
+ (NSString *)hmacSha256:(NSString *)secret message:(NSString *)message;
+ (NSString *)sha256:(NSData *)data;
/** byteCount bytes from the system CSPRNG, base64url without padding (an OAuth state or PKCE verifier). */
+ (NSString *)randomBase64Url:(NSInteger)byteCount;
/** SHA-256 of the text's UTF-8, base64url without padding (a PKCE S256 code challenge). */
+ (NSString *)sha256Base64Url:(NSString *)text;
@end
+21
View File
@@ -1,6 +1,15 @@
#import "FaceclawCrypto.h"
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonDigest.h>
#import <Security/Security.h>
static NSString *Base64Url(NSData *data) {
NSString *base64 = [data base64EncodedStringWithOptions:0];
base64 = [base64 stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
base64 = [base64 stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
return [base64 stringByReplacingOccurrencesOfString:@"=" withString:@""];
}
@implementation FaceclawCrypto
+ (NSString *)hmacSha256:(NSString *)secret message:(NSString *)message {
NSData *key = [secret dataUsingEncoding:NSUTF8StringEncoding];
@@ -16,4 +25,16 @@
for (NSUInteger i = 0; i < sizeof(digest); i++) [hex appendFormat:@"%02x", digest[i]];
return hex;
}
+ (NSString *)randomBase64Url:(NSInteger)byteCount {
if (byteCount <= 0 || byteCount > 1024) return nil;
NSMutableData *bytes = [NSMutableData dataWithLength:(NSUInteger)byteCount];
if (SecRandomCopyBytes(kSecRandomDefault, bytes.length, bytes.mutableBytes) != errSecSuccess) return nil;
return Base64Url(bytes);
}
+ (NSString *)sha256Base64Url:(NSString *)text {
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
unsigned char digest[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(data.bytes, (CC_LONG)data.length, digest);
return Base64Url([NSData dataWithBytes:digest length:sizeof(digest)]);
}
@end
+7
View File
@@ -7,4 +7,11 @@
+ (NSData *)renderSVG:(NSString *)svg size:(NSInteger)size;
+ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height;
+ (NSData *)decodeImageFile:(NSString *)path maxWidth:(NSInteger)maxWidth maxHeight:(NSInteger)maxHeight;
/**
* [widthLo, widthHi, heightLo, heightHi, BGRA...] fitted within maxSize (never upscaled),
* composited over black; the input of the shared Kotlin gray tone curve and dither.
*/
+ (NSData *)bgraPacket:(CGImageRef)image maxSize:(NSInteger)maxSize;
/** bgraPacket of encoded image bytes (JPEG, PNG, ...); nil when undecodable. */
+ (NSData *)bgraPacketFromImageData:(NSData *)data maxSize:(NSInteger)maxSize;
@end
+38
View File
@@ -120,4 +120,42 @@
CGColorSpaceRelease(space);
return gray;
}
+ (NSData *)bgraPacket:(CGImageRef)image maxSize:(NSInteger)maxSize {
if (!image || maxSize <= 0 || maxSize > 1024) return nil;
double sourceWidth = CGImageGetWidth(image), sourceHeight = CGImageGetHeight(image);
if (sourceWidth <= 0 || sourceHeight <= 0) return nil;
double scale = MIN(1.0, MIN(maxSize / sourceWidth, maxSize / sourceHeight));
NSInteger width = MAX(1, lround(sourceWidth * scale)), height = MAX(1, lround(sourceHeight * scale));
NSMutableData *packet = [NSMutableData dataWithLength:4 + width * height * 4];
uint8_t *bytes = packet.mutableBytes;
bytes[0] = width & 255; bytes[1] = (width >> 8) & 255;
bytes[2] = height & 255; bytes[3] = (height >> 8) & 255;
uint8_t *pixels = bytes + 4;
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
// Memory order B, G, R, A: one little-endian ARGB word per pixel.
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * 4, space,
kCGBitmapByteOrder32Little | (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(space);
if (!context) return nil;
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGContextRelease(context);
// Composited over black (the display's see-through background); now opaque.
for (NSInteger i = 0; i < width * height; i++) pixels[i * 4 + 3] = 255;
return packet;
}
+ (NSData *)bgraPacketFromImageData:(NSData *)data maxSize:(NSInteger)maxSize {
if (!data.length || maxSize <= 0 || maxSize > 1024) return nil;
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
if (!source) return nil;
NSDictionary *options = @{(id)kCGImageSourceCreateThumbnailFromImageAlways: @YES,
(id)kCGImageSourceCreateThumbnailWithTransform: @YES,
(id)kCGImageSourceThumbnailMaxPixelSize: @(maxSize)};
CGImageRef image = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef)options);
CFRelease(source);
if (!image) return nil;
NSData *packet = [self bgraPacket:image maxSize:maxSize];
CGImageRelease(image);
return packet;
}
@end
@@ -0,0 +1,16 @@
#import <Foundation/Foundation.h>
/**
* Catches an OAuth redirect to http://127.0.0.1:<port><path> (the loopback redirect that
* providers such as Spotify accept from native apps): listens on the loopback interface
* only, answers the browser's request for the path with a short "return to Faceclaw"
* page and hands back the full redirect URL, query included. Any other path (a favicon)
* gets a 404. Foundation-only, so the macOS test builds it as is.
*/
@interface FaceclawLoopbackRedirect : NSObject
/** Called on the main queue with each redirect to the path. */
@property(nonatomic, copy) void (^redirectHandler)(NSString *url);
/** Start listening; nil on success, otherwise why not (e.g. another app holds the port). */
- (NSString *)startOnPort:(NSInteger)port path:(NSString *)path;
- (void)stop;
@end
@@ -0,0 +1,128 @@
#import "FaceclawLoopbackRedirect.h"
#import <arpa/inet.h>
#import <errno.h>
#import <fcntl.h>
#import <netinet/in.h>
#import <sys/socket.h>
#import <unistd.h>
/** Request heads beyond this are not a browser's redirect; stop reading. */
static const NSUInteger kMaxRequestBytes = 16384;
static NSString *const kDonePage =
@"<!doctype html><html><head><meta charset=\"utf-8\">"
@"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>Faceclaw</title></head>"
@"<body style=\"font-family: -apple-system, sans-serif; text-align: center; padding: 64px 24px\">"
@"<h2>Signed in</h2><p>You can return to Faceclaw.</p></body></html>";
@interface FaceclawLoopbackRedirect ()
@property(nonatomic, strong) dispatch_queue_t queue;
@property(nonatomic, strong) dispatch_source_t acceptSource;
@property(nonatomic) NSInteger port;
@property(nonatomic, copy) NSString *path;
@end
@implementation FaceclawLoopbackRedirect
- (instancetype)init {
if ((self = [super init])) {
_queue = dispatch_queue_create("com.faceclaw.loopback-redirect", DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (void)dealloc {
[self stop];
}
- (NSString *)startOnPort:(NSInteger)port path:(NSString *)path {
[self stop];
if (port <= 0 || port > 65535 || ![path hasPrefix:@"/"]) return @"Invalid redirect address";
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) return [NSString stringWithFormat:@"Could not open a socket (errno %d)", errno];
int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
struct sockaddr_in address = {0};
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons((uint16_t)port);
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (bind(fd, (struct sockaddr *)&address, sizeof(address)) != 0 || listen(fd, 8) != 0) {
int error = errno;
close(fd);
return error == EADDRINUSE
? [NSString stringWithFormat:@"Another app is using port %ld; close it and try again", (long)port]
: [NSString stringWithFormat:@"Could not listen on port %ld (errno %d)", (long)port, error];
}
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
self.port = port;
self.path = path;
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)fd, 0, self.queue);
__weak typeof(self) weakSelf = self;
dispatch_source_set_event_handler(source, ^{
int client = accept(fd, NULL, NULL);
if (client < 0) return;
FaceclawLoopbackRedirect *strongSelf = weakSelf;
if (strongSelf) [strongSelf serve:client];
else close(client);
});
dispatch_source_set_cancel_handler(source, ^{ close(fd); });
self.acceptSource = source;
dispatch_resume(source);
return nil;
}
- (void)stop {
if (self.acceptSource) dispatch_source_cancel(self.acceptSource);
self.acceptSource = nil;
}
/** Read one request head and answer it; runs on the receiver's queue (blocking, with timeouts). */
- (void)serve:(int)client {
// BSD sockets inherit O_NONBLOCK from the listener.
fcntl(client, F_SETFL, fcntl(client, F_GETFL) & ~O_NONBLOCK);
int yes = 1;
setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes));
struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(client, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
NSMutableData *head = [NSMutableData data];
NSData *terminator = [@"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding];
char buffer[2048];
while (head.length < kMaxRequestBytes) {
ssize_t count = recv(client, buffer, sizeof(buffer), 0);
if (count <= 0) break;
[head appendBytes:buffer length:(NSUInteger)count];
if ([head rangeOfData:terminator options:0 range:NSMakeRange(0, head.length)].location != NSNotFound) break;
}
NSString *text = [[NSString alloc] initWithData:head encoding:NSUTF8StringEncoding] ?: @"";
NSArray<NSString *> *requestLine = [[text componentsSeparatedByString:@"\r\n"].firstObject componentsSeparatedByString:@" "];
NSString *target = requestLine.count >= 3 && [requestLine[0] isEqualToString:@"GET"] ? requestLine[1] : nil;
BOOL matched = target && [[target componentsSeparatedByString:@"?"].firstObject isEqualToString:self.path];
[self respond:client status:matched ? @"200 OK" : @"404 Not Found" body:matched ? kDonePage : @"Not found"];
close(client);
if (!matched) return;
NSString *url = [NSString stringWithFormat:@"http://127.0.0.1:%ld%@", (long)self.port, target];
void (^handler)(NSString *) = self.redirectHandler;
dispatch_async(dispatch_get_main_queue(), ^{
if (handler) handler(url);
});
}
- (void)respond:(int)client status:(NSString *)status body:(NSString *)body {
NSData *bodyData = [body dataUsingEncoding:NSUTF8StringEncoding];
NSString *headers = [NSString stringWithFormat:
@"HTTP/1.1 %@\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: %lu\r\n"
@"Cache-Control: no-store\r\nConnection: close\r\n\r\n", status, (unsigned long)bodyData.length];
NSMutableData *response = [[headers dataUsingEncoding:NSASCIIStringEncoding] mutableCopy];
[response appendData:bodyData];
const uint8_t *bytes = response.bytes;
NSUInteger sent = 0;
while (sent < response.length) {
ssize_t count = send(client, bytes + sent, response.length - sent, 0);
if (count <= 0) break;
sent += (NSUInteger)count;
}
}
@end
+21
View File
@@ -0,0 +1,21 @@
#import <Foundation/Foundation.h>
/**
* OAuth sign-in in the system browser sheet (ASWebAuthenticationSession, which shares
* Safari's cookies, so a provider the user is already signed in to needs only a consent
* tap) for providers that redirect to a loopback address: a FaceclawLoopbackRedirect
* catches the redirect, then the sheet is closed. Presenting needs Faceclaw in the
* foreground, so a sign-in started from the glasses waits until the user opens the app.
* One sign-in at a time; starting another cancels the first. Call from the main thread.
*/
@interface FaceclawWebSignIn : NSObject
+ (instancetype)shared;
/**
* completion(redirectUrl, nil) once the provider redirects to http://127.0.0.1:<port><path>;
* (nil, "cancelled") when the user closes the sheet or cancel is called; (nil, message)
* when the sheet or the receiver cannot start. Called once, on the main queue.
*/
- (void)signIn:(NSString *)authorizeUrl port:(NSInteger)port path:(NSString *)path
completion:(void (^)(NSString *redirectUrl, NSString *error))completion;
- (void)cancel;
@end
+110
View File
@@ -0,0 +1,110 @@
#import "FaceclawWebSignIn.h"
#import "FaceclawLoopbackRedirect.h"
#import <AuthenticationServices/AuthenticationServices.h>
#import <UIKit/UIKit.h>
/** Never reached: the loopback receiver ends the session before any custom-scheme redirect. */
static NSString *const kUnusedCallbackScheme = @"faceclaw-signin";
@interface FaceclawWebSignIn () <ASWebAuthenticationPresentationContextProviding>
@property(nonatomic, strong) FaceclawLoopbackRedirect *receiver;
@property(nonatomic, strong) ASWebAuthenticationSession *session;
@property(nonatomic, copy) void (^completion)(NSString *redirectUrl, NSString *error);
/** The sign-in page waiting for Faceclaw to come to the foreground. */
@property(nonatomic, copy) NSURL *pendingUrl;
@property(nonatomic, strong) id activeObserver;
@end
@implementation FaceclawWebSignIn
+ (instancetype)shared {
static FaceclawWebSignIn *instance;
static dispatch_once_t once;
dispatch_once(&once, ^{ instance = [FaceclawWebSignIn new]; });
return instance;
}
- (void)signIn:(NSString *)authorizeUrl port:(NSInteger)port path:(NSString *)path
completion:(void (^)(NSString *, NSString *))completion {
[self finishWithUrl:nil error:@"cancelled"];
NSURL *url = [NSURL URLWithString:authorizeUrl];
if (!url || ![url.scheme isEqualToString:@"https"]) {
if (completion) completion(nil, @"Invalid sign-in address");
return;
}
FaceclawLoopbackRedirect *receiver = [FaceclawLoopbackRedirect new];
NSString *error = [receiver startOnPort:port path:path];
if (error) {
if (completion) completion(nil, error);
return;
}
__weak typeof(self) weakSelf = self;
receiver.redirectHandler = ^(NSString *redirectUrl) {
[weakSelf finishWithUrl:redirectUrl error:nil];
};
self.receiver = receiver;
self.completion = completion;
if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) {
[self present:url];
return;
}
self.pendingUrl = url;
self.activeObserver = [NSNotificationCenter.defaultCenter addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {
FaceclawWebSignIn *strongSelf = weakSelf;
NSURL *pending = strongSelf.pendingUrl;
if (!pending) return;
[strongSelf removeActiveObserver];
[strongSelf present:pending];
}];
}
- (void)cancel {
[self finishWithUrl:nil error:@"cancelled"];
}
- (void)present:(NSURL *)url {
__weak typeof(self) weakSelf = self;
ASWebAuthenticationSession *session = [[ASWebAuthenticationSession alloc] initWithURL:url
callbackURLScheme:kUnusedCallbackScheme completionHandler:^(NSURL *callbackURL, NSError *error) {
// The sheet closed: the user dismissed it, or finishing already cancelled it.
BOOL dismissed = !error || ([error.domain isEqualToString:ASWebAuthenticationSessionErrorDomain]
&& error.code == ASWebAuthenticationSessionErrorCodeCanceledLogin);
[weakSelf finishWithUrl:callbackURL.absoluteString
error:callbackURL ? nil : dismissed ? @"cancelled" : error.localizedDescription];
}];
session.presentationContextProvider = self;
session.prefersEphemeralWebBrowserSession = NO;
self.session = session;
if (![session start]) [self finishWithUrl:nil error:@"Could not open the sign-in page"];
}
/** Report the outcome once; tear down the receiver and close the sheet. */
- (void)finishWithUrl:(NSString *)url error:(NSString *)error {
void (^completion)(NSString *, NSString *) = self.completion;
ASWebAuthenticationSession *session = self.session;
self.completion = nil;
self.session = nil;
self.pendingUrl = nil;
[self removeActiveObserver];
[self.receiver stop];
self.receiver = nil;
// Re-enters through the session's completion handler, which then finds nothing to report.
[session cancel];
if (completion) completion(url, url ? nil : error);
}
- (void)removeActiveObserver {
if (self.activeObserver) [NSNotificationCenter.defaultCenter removeObserver:self.activeObserver];
self.activeObserver = nil;
}
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session {
for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
if (![scene isKindOfClass:UIWindowScene.class]) continue;
NSArray<UIWindow *> *windows = ((UIWindowScene *)scene).windows;
for (UIWindow *window in windows) if (window.isKeyWindow) return window;
if (windows.firstObject) return windows.firstObject;
}
return [UIWindow new];
}
@end
+2
View File
@@ -11,5 +11,7 @@ module FaceclawGraphics {
header "FaceclawEvenHubWebView.h"
header "FaceclawCrypto.h"
header "FaceclawQrScanner.h"
header "FaceclawLoopbackRedirect.h"
header "FaceclawWebSignIn.h"
export *
}
+1
View File
@@ -7,6 +7,7 @@
* Use Open-Meteo for weather in regions that the US National Weather Service doesn't cover
* Android: Show pictures attached to notifications (such as Dexcom's glucose graph) in the notification view
* Add an Apple Music integration on iOS
* Add a Spotify integration on iOS by voice (needs your own Spotify developer client ID and Spotify Premium)
### Bug fixes
+14
View File
@@ -153,6 +153,20 @@ Apple's on iOS) for the name of the place.
Open-Meteo: https://open-meteo.com/en/terms
## Spotify
On iOS, the Music app, Glanceboard and the voice assistant can show and
control Spotify through Spotify's Web API, once the user has created their own
Spotify developer app and signed in. Signing in happens on Spotify's website in
the system's sign-in sheet; Faceclaw never sees the password, and stores the
resulting sign-in tokens on the phone. While Spotify is shown or used,
Faceclaw requests the user's playback state, queue, library and search results
from Spotify and sends it playback commands, directly from the phone. This is
subject to Spotify's privacy policy.
Spotify: https://www.spotify.com/legal/privacy-policy/
## EvenHub
The EvenHub app store is subject to Even Realities' privacy policy:
+49 -14
View File
@@ -8,8 +8,10 @@ import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller";
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
import { spotifyService } from "../../native/spotify";
import { MediaBrowseLayer } from "./media-browse";
import { MusicSettingsLayer } from "./music-settings";
import { spotifyMenuItem } from "./spotify-menu";
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
@@ -33,6 +35,8 @@ const QUEUE_X = 204;
/** Horizontal inset of row text from its selection box. */
const LIST_TEXT_INSET = 6;
const PLAYLIST_ACTION_INDEX = 1;
/** Minimum room for the dim artist after a queue row's title; less and it is left off. */
const QUEUE_SUBTITLE_MIN_WIDTH = 40;
type MusicAction =
| { kind: "play-pause"; label: string; enabled: boolean }
@@ -40,14 +44,17 @@ type MusicAction =
| { kind: "next"; label: string; enabled: boolean }
| { kind: "volume"; label: string; enabled: boolean }
| { kind: "playlist"; label: string; enabled: boolean }
| { kind: "browse"; label: string; enabled: boolean };
| { kind: "browse"; label: string; enabled: boolean }
/** A player-specific action from the state's customActions (Spotify's shuffle / repeat / like). */
| { kind: "custom"; label: string; enabled: boolean; action: string };
type FocusColumn = "actions" | "playlist";
/**
* Music controller app: metadata + album art + transport controls for the
* active Android media session (any player that publishes one) or, on iOS,
* the Apple Music system player, plus the player's queue when it exposes one
* the Apple Music system player or Spotify (signed in from the window menu),
* plus the player's queue when it exposes one
* (scroll to a track, click to jump). With no session, the front page lists
* the apps whose library can be browsed (click one to open its
* MediaBrowseLayer).
@@ -110,7 +117,7 @@ class MusicAppLayer implements Layer {
const apps = mediaBrowserBridge.listBrowsableApps(!this.libraryAppsFresh);
this.libraryAppsFresh = true;
this.libraryMenu.setItems(apps);
image.drawText(font, 24, 16, global.isIOS ? "Nothing is playing in Apple Music." : "No active media session.", 180);
image.drawText(font, 24, 16, global.isIOS ? media.status || "Nothing is playing." : "No active media session.", 180);
const prompt = global.isIOS
? "Browse your library, or start playback on the phone."
: apps.length
@@ -121,6 +128,11 @@ class MusicAppLayer implements Layer {
image.drawText(font, 24, y, line, 150);
y += lineStep(font);
}
// Why the last command failed (e.g. playing from the Spotify library with no device open).
for (const line of media.notice ? wrapText(font, media.notice, width - 48) : []) {
image.drawText(font, 24, y, line, 200);
y += lineStep(font);
}
if (apps.length) {
const listTop = y + 6;
const listHeight = height - listTop - LIST_BOTTOM_MARGIN;
@@ -156,12 +168,13 @@ class MusicAppLayer implements Layer {
if (media.album) {
image.drawText(font, META_X, metaTop + 4 + (titleSlots + 1) * metaStep, truncateText(font, media.album, metaWidth), 150);
}
// The player line, or for a few seconds why a command failed.
image.drawText(
font,
META_X,
metaTop + 4 + (titleSlots + 2) * metaStep,
truncateText(font, media.appName || media.packageName, metaWidth),
110,
truncateText(font, media.notice || media.appName || media.packageName, metaWidth),
media.notice ? 200 : 110,
);
this.drawProgress(image, media, metaWidth);
@@ -212,7 +225,16 @@ class MusicAppLayer implements Layer {
private drawQueueItem({ image, item, x, y, width, height, selected }: MenuDrawArgs<MediaQueueItem>): void {
const font = getDefaultSmallFont();
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, label, width - 2 * LIST_TEXT_INSET), selected ? 255 : 200);
const textWidth = width - 2 * LIST_TEXT_INSET;
const textY = centeredTextY(font, y, height);
const title = truncateText(font, label, textWidth);
image.drawText(font, x + LIST_TEXT_INSET, textY, title, selected ? 255 : 200);
// The artist, dimmer, in whatever room the title leaves.
const subtitleX = font.measureText(`${label} `);
if (item.subtitle && title === label && textWidth - subtitleX >= QUEUE_SUBTITLE_MIN_WIDTH) {
image.drawText(font, x + LIST_TEXT_INSET + subtitleX, textY,
truncateText(font, item.subtitle, textWidth - subtitleX), selected ? 150 : 110);
}
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
@@ -278,6 +300,7 @@ class MusicAppLayer implements Layer {
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 === "custom") await mediaControllerBridge.sendCustomAction?.(action.action);
else if (action.kind === "volume") {
ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent()));
} else if (action.kind === "playlist") {
@@ -342,6 +365,12 @@ class MusicAppLayer implements Layer {
{ 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 },
...(mediaControllerBridge.sendCustomAction ? media.customActions ?? [] : []).map((custom): MusicAction => ({
kind: "custom",
label: custom.name,
enabled: true,
action: custom.action,
})),
];
}
@@ -496,14 +525,20 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
iconLetter: "M",
icon: "music",
closeable: true,
// Music settings choose among Android media apps; iOS has only Apple Music.
menuItems: () => global.isIOS ? [] : [{
label: "Music settings",
onSelect: (ctx) => {
ctx.stack.pop();
ctx.stack.push(new MusicSettingsLayer());
},
}],
// Music settings choose among Android media apps; iOS has Apple Music, plus Spotify's sign-in here.
menuItems: () => {
if (!global.isIOS) {
return [{
label: "Music settings",
onSelect: (ctx) => {
ctx.stack.pop();
ctx.stack.push(new MusicSettingsLayer());
},
}];
}
const spotify = spotifyService();
return spotify ? [spotifyMenuItem(spotify)] : [];
},
actions: options.actions,
baseLayer: new MusicRootLayer(musicLayer),
submitFrame: options.submitFrame,
+107
View File
@@ -0,0 +1,107 @@
import type { GrayImage } from "../../graphics/image";
import { isValidSpotifyClientId, SPOTIFY_DASHBOARD_URL, SPOTIFY_REDIRECT_URI } from "../../native/spotify-auth";
import type { SpotifyService } from "../../native/spotify-service";
import { spotifyClientIdSetting, textSettingMenuItem } from "../../ui/dashboard-settings";
import { type InputEvent } from "../../ui/gestures";
import { type LayerContext, type PaintBelow } from "../../ui/layers";
import { submenuItem, TextPageLayer, type MenuItem } from "../../ui/menu";
import { WindowMenuLayer } from "../../ui/window-menu";
import { TextViewerLayer } from "../files/text-viewer";
const SETUP_HELP = [
"Faceclaw controls Spotify through Spotify's Web API, which needs a Spotify developer app of your own. Creating one is free; Spotify requires its owner to have Premium, and controlling playback needs Premium too.",
"",
`1. On a computer, open ${SPOTIFY_DASHBOARD_URL.replace("https://", "")} and sign in with your Spotify account.`,
"2. Create an app. Any name and description will do.",
`3. Add this Redirect URI exactly: ${SPOTIFY_REDIRECT_URI}`,
"4. Under \"Which API/SDKs are you planning to use?\", tick Web API, then save.",
"5. Open the app's settings, copy its Client ID, and enter it as Client ID here.",
"6. Choose Sign in, then finish on your phone: Faceclaw opens Spotify's sign-in page there.",
"",
"Spotify plays on whichever of your devices is open (this phone, a computer, a speaker). To start music when nothing is playing, Spotify must be open on one of them.",
"",
"Others can sign in with your client ID once you add their Spotify email under User Management in the dashboard (up to five people).",
].join("\n");
/** The Music app menu's Spotify row (iOS): sign in, sign out, client ID, setup help. */
export function spotifyMenuItem(service: SpotifyService): MenuItem {
return submenuItem("Spotify", (ctx) => ctx.stack.push(new SpotifyMenuLayer(service)), {
description: "Sign in to Spotify to show and control it from the glasses.",
});
}
/**
* Rows rebuilt every paint and input from the account and sign-in progress,
* which change asynchronously. A WindowMenuLayer, so the shell's system menu
* closes it like the window menu it opens from.
*/
class SpotifyMenuLayer extends WindowMenuLayer {
private unsubscribe: (() => void) | null = null;
constructor(private readonly service: SpotifyService) {
super("Spotify", []);
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
this.unsubscribe ??= this.service.onChange(() => ctx.actions.requestRender());
this.setItems(this.rows());
return super.paint(ctx, paintBelow);
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
this.setItems(this.rows());
await super.handleInput(event, ctx);
}
onRemoved(): void {
this.unsubscribe?.();
this.unsubscribe = null;
}
private rows(): MenuItem[] {
const service = this.service;
const account = service.account();
const status = service.signInStatus();
const help: MenuItem = {
label: "Setup help",
onSelect: (ctx) => ctx.stack.push(new TextViewerLayer(SETUP_HELP, "Spotify setup")),
};
if (account) {
return [
info(`Signed in as ${account.displayName || account.userId}`),
...(account.product && account.product !== "premium" ? [info("Playback control needs Premium")] : []),
{ label: "Sign out", onSelect: () => service.signOut() },
textSettingMenuItem(spotifyClientIdSetting),
help,
];
}
if (status.phase === "waiting") {
return [
info("Finish signing in on the phone"),
{ label: "Cancel sign-in", onSelect: () => service.cancelSignIn() },
help,
];
}
const failure: MenuItem[] = status.error
? [{
label: "Sign-in failed (details)",
onSelect: (ctx) => ctx.stack.push(new TextPageLayer("Spotify sign-in failed", status.error)),
}]
: [];
return [
{
label: "Sign in",
description: "Opens Spotify's sign-in page on the phone.",
disabled: () => !isValidSpotifyClientId(spotifyClientIdSetting.get()),
onSelect: () => void service.signIn(),
},
textSettingMenuItem(spotifyClientIdSetting),
help,
...failure,
];
}
}
function info(label: string): MenuItem {
return { label, disabled: true, onSelect: () => {} };
}
+47 -1
View File
@@ -1,6 +1,7 @@
import { readUpcomingEventsAsync, type CalendarEvent } from "../native/calendar";
import { hasCalendarPermission } from "../native/calendar-permissions";
import { mediaControllerBridge } from "../native/media-controller";
import { spotifyService } from "../native/spotify";
import { dismissNotification, readActiveNotifications } from "../native/notification-icons";
import { shell } from "../ui/shell/shell";
import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry";
@@ -93,7 +94,11 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void
inputSchema: { type: "object", properties: {}, additionalProperties: false },
proactive: true,
},
() => ok(describeNowPlaying()),
async () => {
// Spotify's state is polled only while something shows it.
await mediaControllerBridge.refresh?.();
return ok(describeNowPlaying());
},
);
registry.registerSystemTool(
@@ -103,6 +108,7 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void
inputSchema: { type: "object", properties: {}, additionalProperties: false },
},
async () => {
await mediaControllerBridge.refresh?.();
const state = mediaControllerBridge.snapshot();
if (!state.canPlayPause) return err("No media session can be controlled right now.");
await mediaControllerBridge.playPause();
@@ -117,6 +123,7 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void
inputSchema: { type: "object", properties: {}, additionalProperties: false },
},
async () => {
await mediaControllerBridge.refresh?.();
const state = mediaControllerBridge.snapshot();
if (!state.canSkipNext) return err("The current media session can't skip to the next track.");
await mediaControllerBridge.skipNext();
@@ -124,6 +131,45 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void
},
);
const spotify = spotifyService();
if (spotify) {
registry.register({
spec: {
name: "media.spotify_play",
description:
"Search Spotify and start playing the best match on the user's Spotify (this phone or another of their Spotify devices). Use for requests like \"play Daft Punk\" (artist), \"play the album Discovery\" (album) or \"play Harder Better Faster Stronger\" (track).",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "What to search for, e.g. an artist, album, song or playlist name." },
kind: {
type: "string",
enum: ["track", "album", "artist", "playlist"],
description: "What kind of thing to play (default track).",
},
},
required: ["query"],
additionalProperties: false,
},
availability: "always",
// Search, then play (with a device lookup when none is active).
timeoutMs: 20_000,
},
handler: async (args) => {
const query = String(args?.query ?? "").trim();
if (!query) return err("media.spotify_play requires a query");
const kind = ["track", "album", "artist", "playlist"].includes(args?.kind) ? args.kind : "track";
try {
return ok(`Playing ${await spotify.playSearch(query, kind)} on Spotify.`);
} catch (error) {
return err(String((error as Error)?.message ?? error));
}
},
// Offered once signed in from the Music app menu.
isAvailable: () => spotify.account() !== null,
});
}
// Android notification tools are still platform-specific.
if (global.isIOS) return;
+43 -2
View File
@@ -6,6 +6,9 @@ import {
type MediaBrowserApp,
type MediaBrowserBridge,
} from "./media-types";
import { spotifyService } from "./spotify.ios";
import type { SpotifyLibrary } from "./spotify-library";
import { SPOTIFY_PACKAGE } from "./spotify-player";
export type { MediaBrowseItem, MediaBrowserApp } from "./media-types";
@@ -30,7 +33,7 @@ export class AppleMusicBrowserBridge implements MediaBrowserBridge {
connect(app: MediaBrowserApp): Promise<string> {
this.disconnect();
if (app.packageName !== APPLE_MUSIC_PACKAGE) return Promise.reject(new Error("Only Apple Music can be browsed on iOS"));
if (app.packageName !== APPLE_MUSIC_PACKAGE) return Promise.reject(new Error("Not an Apple Music library"));
if (!FaceclawAppleMusic.shared().hasPermission()) return Promise.reject(new Error("Apple Music access is not allowed"));
this.connected = true;
return Promise.resolve("root");
@@ -67,4 +70,42 @@ export class AppleMusicBrowserBridge implements MediaBrowserBridge {
}
}
export const mediaBrowserBridge: MediaBrowserBridge = new AppleMusicBrowserBridge();
/** Apple Music's library, plus the Spotify library once signed in. */
export class IosMediaBrowserBridge implements MediaBrowserBridge {
private current: MediaBrowserBridge | null = null;
constructor(
private readonly appleMusic: AppleMusicBrowserBridge,
private readonly spotify: () => SpotifyLibrary | null,
) {}
listBrowsableApps(): MediaBrowserApp[] {
return [...this.appleMusic.listBrowsableApps(), ...(this.spotify()?.listBrowsableApps() ?? [])];
}
connect(app: MediaBrowserApp): Promise<string> {
this.disconnect();
const library = app.packageName === SPOTIFY_PACKAGE ? this.spotify() : this.appleMusic;
if (!library) return Promise.reject(new Error(`${app.appName} can't be browsed`));
this.current = library;
return library.connect(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 IosMediaBrowserBridge(
new AppleMusicBrowserBridge(),
() => spotifyService()?.library ?? null,
);
+195 -5
View File
@@ -11,6 +11,8 @@ import {
type MediaPlaybackState,
type MediaQueueItem,
} from "./media-types";
import { spotifyService } from "./spotify.ios";
import type { SpotifyPlayer } from "./spotify-player";
export type { MediaControllerState, MediaPlaybackState, MediaQueueItem } from "./media-types";
@@ -88,7 +90,7 @@ export class AppleMusicControllerBridge implements MediaControllerBridge {
onStateChange(listener: (state: MediaControllerState) => void): () => void {
void this.start();
this.stateListeners.add(listener);
this.pollTimer ??= setInterval(() => this.refresh(), POLL_MS);
this.pollTimer ??= setInterval(() => this.publish(), POLL_MS);
listener(this.snapshot());
return () => {
this.stateListeners.delete(listener);
@@ -142,7 +144,7 @@ export class AppleMusicControllerBridge implements MediaControllerBridge {
if (this.hasAccess()) return Promise.resolve(true);
this.pendingAccess ??= this.askForAccess().finally(() => {
this.pendingAccess = null;
this.refresh();
this.publish();
});
return this.pendingAccess;
}
@@ -187,12 +189,12 @@ export class AppleMusicControllerBridge implements MediaControllerBridge {
const music = FaceclawAppleMusic.shared();
if (!this.bound) {
this.bound = true;
music.changeHandler = () => this.refresh();
music.changeHandler = () => this.publish();
}
return music;
}
private refresh(): void {
private publish(): void {
if (!this.stateListeners.size) return;
const state = this.snapshot();
const signature = stateSignature(state);
@@ -228,4 +230,192 @@ export class AppleMusicControllerBridge implements MediaControllerBridge {
}
}
export const mediaControllerBridge: MediaControllerBridge = new AppleMusicControllerBridge();
export type IosMediaSource = "apple-music" | "spotify";
/**
* Which player to follow: the one playing; with both playing, the current
* choice (no flapping); with neither, the one that played last, and before
* either has played this run, Spotify when it reports a session (its player
* reports only a recently active one, while the system Music player keeps
* its last item for days).
*/
export function chooseMediaSource(
current: IosMediaSource,
appleMusic: MediaControllerState,
spotify: MediaControllerState | null,
appleMusicLastPlayingAtMs: number,
spotifyLastPlayingAtMs: number,
): IosMediaSource {
if (!spotify?.accessEnabled) return "apple-music";
if (!appleMusic.accessEnabled) return "spotify";
const applePlaying = appleMusic.playbackState === "playing";
const spotifyPlaying = spotify.playbackState === "playing";
if (applePlaying && spotifyPlaying) return current;
if (applePlaying) return "apple-music";
if (spotifyPlaying) return "spotify";
if (appleMusicLastPlayingAtMs !== spotifyLastPlayingAtMs) {
return spotifyLastPlayingAtMs > appleMusicLastPlayingAtMs ? "spotify" : "apple-music";
}
return spotify.available ? "spotify" : "apple-music";
}
/**
* The iOS Music bridge: Apple Music (the system player), plus Spotify once
* signed in (its Web API: playback on this phone or any Spotify Connect
* device). The state and every command follow one of them, by
* chooseMediaSource.
*/
export class IosMediaControllerBridge implements MediaControllerBridge {
readonly accessPrompt =
"Faceclaw needs access to Apple Music to show and control what's playing. Tap, then allow access on your phone. For Spotify, sign in from the app menu instead.";
readonly accessLabel: string;
readonly accessAction: string;
private source: IosMediaSource = "apple-music";
private appleMusicLastPlayingAtMs = 0;
private readonly listeners = new Set<(state: MediaControllerState) => void>();
private unsubscribeSources: Array<() => void> = [];
/** The source and its artVersion the composite artVersion was bumped for. */
private artKey = "";
private artVersion = 0;
constructor(
private readonly appleMusic: AppleMusicControllerBridge,
private readonly spotify: () => SpotifyPlayer | null,
private readonly now: () => number = Date.now,
) {
this.accessLabel = appleMusic.accessLabel;
this.accessAction = appleMusic.accessAction;
}
get skipRebuildsQueue(): boolean {
return this.active().skipRebuildsQueue;
}
onStateChange(listener: (state: MediaControllerState) => void): () => void {
if (!this.listeners.size) {
const update = () => this.emit();
const spotify = this.spotify();
this.unsubscribeSources = [this.appleMusic.onStateChange(update), ...(spotify ? [spotify.onStateChange(update)] : [])];
}
this.listeners.add(listener);
listener(this.snapshot());
return () => {
this.listeners.delete(listener);
if (this.listeners.size) return;
for (const unsubscribe of this.unsubscribeSources) unsubscribe();
this.unsubscribeSources = [];
};
}
snapshot(): MediaControllerState {
const appleMusic = this.appleMusic.snapshot();
const spotify = this.spotify()?.snapshot() ?? null;
this.select(appleMusic, spotify);
const state = this.source === "spotify" && spotify ? spotify : appleMusic;
// Changes with the source as well as with that source's art.
const artKey = `${this.source}:${state.artVersion}`;
if (artKey !== this.artKey) {
this.artKey = artKey;
this.artVersion++;
}
return {
...state,
status: state.available ? state.status : idleStatus(appleMusic, spotify, state),
artVersion: this.artVersion,
// A Spotify command can fail while Apple Music is the one shown (playing from the Spotify library).
notice: spotify?.notice || state.notice || "",
};
}
async start(): Promise<void> {
await this.appleMusic.start();
}
async stop(): Promise<void> {
await this.appleMusic.stop();
}
playPause(): Promise<void> {
return this.active().playPause();
}
skipNext(): Promise<void> {
return this.active().skipNext();
}
skipPrevious(): Promise<void> {
return this.active().skipPrevious();
}
/** Apple Music's media-library permission (what requestAccess asks for); Spotify signs in from the Music menu. */
hasAccess(): boolean {
return this.appleMusic.hasAccess();
}
requestAccess(): Promise<boolean> {
return this.appleMusic.requestAccess();
}
skipToQueueItem(id: number): Promise<void> {
return this.active().skipToQueueItem(id);
}
getMediaVolumePercent(): number {
return this.active().getMediaVolumePercent();
}
setMediaVolumePercent(volumePercent: number): void {
this.active().setMediaVolumePercent(volumePercent);
}
getAlbumArt(maxSize: number): GrayImage | null {
return this.active().getAlbumArt(maxSize);
}
getQueue(): MediaQueueItem[] {
return this.active().getQueue();
}
async sendCustomAction(action: string): Promise<void> {
await this.active().sendCustomAction?.(action);
}
async refresh(): Promise<void> {
await this.spotify()?.refresh();
}
private active(): MediaControllerBridge {
const spotify = this.spotify();
this.select(this.appleMusic.snapshot(), spotify?.snapshot() ?? null);
return this.source === "spotify" && spotify ? spotify : this.appleMusic;
}
private select(appleMusic: MediaControllerState, spotify: MediaControllerState | null): void {
if (appleMusic.playbackState === "playing") this.appleMusicLastPlayingAtMs = this.now();
this.source = chooseMediaSource(this.source, appleMusic, spotify, this.appleMusicLastPlayingAtMs,
this.spotify()?.lastPlayingAtMs ?? 0);
}
/**
* Forward every source update: each source already drops repeats, and
* Spotify also signals changes that are not in the state (its queue).
*/
private emit(): void {
if (!this.listeners.size) return;
const state = this.snapshot();
for (const listener of this.listeners) listener(state);
}
}
/** The no-session line: which players were checked. */
function idleStatus(appleMusic: MediaControllerState, spotify: MediaControllerState | null, shown: MediaControllerState): string {
if (appleMusic.accessEnabled && spotify?.accessEnabled) return "Nothing is playing.";
return shown.status;
}
export const appleMusicControllerBridge = new AppleMusicControllerBridge();
export const mediaControllerBridge: MediaControllerBridge = new IosMediaControllerBridge(
appleMusicControllerBridge,
() => spotifyService()?.player ?? null,
);
+23 -1
View File
@@ -3,7 +3,8 @@ import type { GrayImage } from "../graphics/image";
/**
* Shapes shared by the media bridges: media-controller.ts / media-browser.ts
* drive Android media sessions and MediaBrowserServices; their .ios.ts
* counterparts drive Apple Music through the MediaPlayer framework.
* counterparts drive Apple Music through the MediaPlayer framework and
* Spotify through its Web API.
*/
/** The player behind the iOS bridges: the system Music app. */
@@ -13,9 +14,19 @@ export const APPLE_MUSIC_PACKAGE = "com.apple.Music";
export type MediaQueueItem = {
id: number;
title: string;
/** Usually the artist; absent when the player gives none. */
subtitle?: string;
active: boolean;
};
/** A player-specific action beyond the transport controls (Spotify's shuffle, repeat, like). */
export type MediaCustomAction = {
/** Stable id to send back with sendCustomAction. */
action: string;
/** The row label, naming the current state where there is one ("Shuffle: on"). */
name: string;
};
export type MediaPlaybackState =
| "notification-access-required"
| "idle"
@@ -47,6 +58,10 @@ export type MediaControllerState = {
* players re-publish the same strings with a different bitmap.
*/
artVersion: number;
/** Player-specific actions (see sendCustomAction); absent when the player offers none. */
customActions?: MediaCustomAction[];
/** A short-lived message about a command that failed (Spotify needs Premium, ...); absent or "" otherwise. */
notice?: string;
};
export const DEFAULT_MEDIA_STATE: MediaControllerState = {
@@ -113,6 +128,13 @@ export interface MediaControllerBridge {
getAlbumArt(maxSize: number): GrayImage | null;
/** The player's queue (playlist), empty when the player exposes none. */
getQueue(): MediaQueueItem[];
/** Run one of the state's customActions. */
sendCustomAction?(action: string): Promise<void>;
/**
* Re-read the player now, for one-off readers (the assistant) when no
* listener keeps the state current; absent when snapshots are always live.
*/
refresh?(): Promise<void>;
}
export type MediaBrowserApp = {
+273
View File
@@ -0,0 +1,273 @@
import { getStringSetting, setStringSetting } from "./settings-store";
import { buildQuery, SpotifyApiError, SpotifyClient, type SpotifyFetch, type SpotifyTokenSource } from "./spotify-web-api";
/**
* Spotify sign-in: OAuth authorization code with PKCE against the user's own
* client ID (no client secret), redirected to a loopback address that the
* phone catches (FaceclawWebSignIn on iOS). Spotify accepts http only for
* loopback IP literals, and custom-scheme redirects fail with "insecure
* redirect URI" for apps created since 2025, so the redirect is
* http://127.0.0.1 on a fixed port the user registers in the dashboard.
*
* The tokens are stored with the client ID they were issued to; changing the
* client ID setting signs out.
*/
export const SPOTIFY_ACCOUNTS_BASE = "https://accounts.spotify.com";
export const SPOTIFY_REDIRECT_PORT = 43823;
export const SPOTIFY_REDIRECT_PATH = "/spotify-callback";
export const SPOTIFY_REDIRECT_URI = `http://127.0.0.1:${SPOTIFY_REDIRECT_PORT}${SPOTIFY_REDIRECT_PATH}`;
export const SPOTIFY_DASHBOARD_URL = "https://developer.spotify.com/dashboard";
export const SPOTIFY_CLIENT_ID_KEY = "music.spotify.clientId";
export const SPOTIFY_ACCOUNT_KEY = "music.spotify.account";
export const SPOTIFY_SCOPES = [
"user-read-playback-state",
"user-modify-playback-state",
"user-read-currently-playing",
"user-read-recently-played",
"playlist-read-private",
"playlist-read-collaborative",
"user-library-read",
"user-library-modify",
"user-follow-read",
];
/** Refresh this long before Spotify's stated expiry (an hour), so a token never lapses mid-request. */
const EXPIRY_MARGIN_MS = 60_000;
export type SpotifyAccount = {
clientId: string;
accessToken: string;
refreshToken: string;
/** Epoch ms. */
expiresAtMs: number;
userId: string;
displayName: string;
/** "premium", "free", ...; playback control needs premium. */
product: string;
};
export type SpotifyTokens = { accessToken: string; refreshToken: string; expiresAtMs: number };
/** The phone side of a sign-in: PKCE material and the browser sheet that catches the loopback redirect. */
export type WebSignIn = {
/** A fresh PKCE verifier and its S256 challenge. */
createPkce(): { verifier: string; challenge: string };
randomState(): string;
/** Show the provider's page; resolves to the redirect URL, rejects with "cancelled" or why it failed. */
open(authorizeUrl: string, port: number, path: string): Promise<string>;
cancel(): void;
};
export function spotifyClientId(): string {
return getStringSetting(SPOTIFY_CLIENT_ID_KEY, "").trim();
}
export function isValidSpotifyClientId(value: string): boolean {
return /^[0-9a-f]{32}$/i.test(value.trim());
}
/** The stored sign-in, or null when signed out or signed in with another client ID. */
export function readSpotifyAccount(): SpotifyAccount | null {
let raw: any;
try {
raw = JSON.parse(getStringSetting(SPOTIFY_ACCOUNT_KEY, "") || "null");
} catch {
return null;
}
if (!raw || typeof raw.refreshToken !== "string" || !raw.refreshToken) return null;
const clientId = spotifyClientId();
if (!clientId || raw.clientId !== clientId) return null;
return {
clientId,
accessToken: String(raw.accessToken ?? ""),
refreshToken: raw.refreshToken,
expiresAtMs: Number(raw.expiresAtMs) || 0,
userId: String(raw.userId ?? ""),
displayName: String(raw.displayName ?? ""),
product: String(raw.product ?? ""),
};
}
export function saveSpotifyAccount(account: SpotifyAccount | null): void {
setStringSetting(SPOTIFY_ACCOUNT_KEY, account ? JSON.stringify(account) : "");
}
export function spotifyAuthorizeUrl(clientId: string, codeChallenge: string, state: string): string {
return `${SPOTIFY_ACCOUNTS_BASE}/authorize${buildQuery({
response_type: "code",
client_id: clientId,
scope: SPOTIFY_SCOPES.join(" "),
redirect_uri: SPOTIFY_REDIRECT_URI,
state,
code_challenge_method: "S256",
code_challenge: codeChallenge,
})}`;
}
/** The authorization code from Spotify's redirect; throws with a message for the glasses otherwise. */
export function parseSpotifyRedirect(url: string, expectedState: string): string {
const params = parseQueryString(url);
if (params.error) {
throw new Error(params.error === "access_denied" ? "Spotify sign-in was declined." : `Spotify sign-in failed: ${params.error}`);
}
if (params.state !== expectedState) throw new Error("Spotify sign-in failed: the reply didn't match this sign-in. Try again.");
if (!params.code) throw new Error("Spotify sign-in failed: no authorization code in the reply.");
return params.code;
}
/** Query parameters of a URL (not URLSearchParams, which the NativeScript runtime mishandles). */
export function parseQueryString(url: string): Record<string, string> {
const query = url.split("#")[0]!.split("?").slice(1).join("?");
const params: Record<string, string> = {};
for (const part of query.split("&")) {
if (!part) continue;
const separator = part.indexOf("=");
const key = separator < 0 ? part : part.slice(0, separator);
const value = separator < 0 ? "" : part.slice(separator + 1);
try {
params[decodeURIComponent(key.replace(/\+/g, " "))] = decodeURIComponent(value.replace(/\+/g, " "));
} catch {
// Malformed escapes: skip the parameter.
}
}
return params;
}
/** POST to Spotify's token endpoint (authorization_code or refresh_token grant). */
export async function requestSpotifyTokens(
fetchImpl: SpotifyFetch,
params: Record<string, string>,
nowMs: number,
): Promise<SpotifyTokens> {
let status = 0;
let body = "";
try {
const response = await fetchImpl(`${SPOTIFY_ACCOUNTS_BASE}/api/token`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" },
body: buildQuery(params).slice(1),
});
status = response.status;
body = await response.text();
} catch (error) {
throw new SpotifyApiError(`Can't reach Spotify: ${(error as Error)?.message ?? error}`, 0);
}
let parsed: any = null;
try {
parsed = JSON.parse(body);
} catch {
// Reported below.
}
if (status >= 200 && status < 300 && typeof parsed?.access_token === "string") {
const expiresIn = Number(parsed.expires_in);
return {
accessToken: parsed.access_token,
// Spotify may rotate the refresh token; "" keeps the current one.
refreshToken: typeof parsed.refresh_token === "string" ? parsed.refresh_token : "",
expiresAtMs: nowMs + (Number.isFinite(expiresIn) && expiresIn > 0 ? expiresIn : 3600) * 1000,
};
}
const error = typeof parsed?.error === "string" ? parsed.error : "";
const description = typeof parsed?.error_description === "string" ? parsed.error_description : "";
if (error === "invalid_grant" && params.grant_type === "refresh_token") {
throw new SpotifyApiError("Spotify no longer accepts this sign-in. Sign in again from the Music app menu.", status, error);
}
if (error === "invalid_client") {
throw new SpotifyApiError("Spotify doesn't recognize the client ID. Check it under Spotify in the Music app menu.", status, error);
}
throw new SpotifyApiError(`Spotify sign-in failed: ${description || error || `HTTP ${status}`}`, status, error);
}
/**
* Access tokens for the stored account: the current one until it nears
* expiry, then one refresh shared by every caller waiting on it. A refresh
* Spotify rejects (revoked access) signs out.
*/
export class SpotifySession implements SpotifyTokenSource {
private refreshing: Promise<string> | null = null;
constructor(
private readonly fetchImpl: SpotifyFetch,
private readonly now: () => number = Date.now,
) {}
accessToken(forceRefresh: boolean): Promise<string> {
const account = readSpotifyAccount();
if (!account) return Promise.reject(new SpotifyApiError("Not signed in to Spotify.", 401, "signed_out"));
if (!forceRefresh && account.accessToken && account.expiresAtMs - EXPIRY_MARGIN_MS > this.now()) {
return Promise.resolve(account.accessToken);
}
this.refreshing ??= this.refresh(account).finally(() => {
this.refreshing = null;
});
return this.refreshing;
}
private async refresh(account: SpotifyAccount): Promise<string> {
let tokens: SpotifyTokens;
try {
tokens = await requestSpotifyTokens(this.fetchImpl, {
grant_type: "refresh_token",
refresh_token: account.refreshToken,
client_id: account.clientId,
}, this.now());
} catch (error) {
if (error instanceof SpotifyApiError && error.reason === "invalid_grant" && readSpotifyAccount()?.refreshToken === account.refreshToken) {
saveSpotifyAccount(null);
}
throw error;
}
// A sign-out or another sign-in while this was in flight wins.
if (readSpotifyAccount()?.refreshToken !== account.refreshToken) {
throw new SpotifyApiError("Spotify sign-in changed; try again.", 401, "signed_out");
}
saveSpotifyAccount({
...account,
accessToken: tokens.accessToken,
refreshToken: tokens.refreshToken || account.refreshToken,
expiresAtMs: tokens.expiresAtMs,
});
return tokens.accessToken;
}
}
/**
* The whole sign-in: the browser sheet, the code exchange and the profile
* lookup; stores and returns the account. Rejects with "cancelled" when the
* user closes the sheet.
*/
export async function signInToSpotify(
fetchImpl: SpotifyFetch,
webSignIn: WebSignIn,
now: () => number = Date.now,
): Promise<SpotifyAccount> {
const clientId = spotifyClientId();
if (!isValidSpotifyClientId(clientId)) throw new Error("Enter your Spotify client ID first.");
const { verifier, challenge } = webSignIn.createPkce();
const state = webSignIn.randomState();
const redirect = await webSignIn.open(spotifyAuthorizeUrl(clientId, challenge, state), SPOTIFY_REDIRECT_PORT, SPOTIFY_REDIRECT_PATH);
const code = parseSpotifyRedirect(redirect, state);
const tokens = await requestSpotifyTokens(fetchImpl, {
grant_type: "authorization_code",
code,
redirect_uri: SPOTIFY_REDIRECT_URI,
client_id: clientId,
code_verifier: verifier,
}, now());
const user = await new SpotifyClient(fetchImpl, { accessToken: async () => tokens.accessToken }).me();
const account: SpotifyAccount = {
clientId,
accessToken: tokens.accessToken,
refreshToken: tokens.refreshToken,
expiresAtMs: tokens.expiresAtMs,
userId: user.id,
displayName: user.displayName,
product: user.product,
};
if (!account.refreshToken) throw new Error("Spotify sign-in failed: no refresh token in the reply.");
saveSpotifyAccount(account);
return account;
}
+157
View File
@@ -0,0 +1,157 @@
import type { MediaBrowseItem, MediaBrowserApp, MediaBrowserBridge } from "./media-types";
import { SPOTIFY_APP_NAME, SPOTIFY_PACKAGE, type SpotifyPlayer } from "./spotify-player";
import type { SpotifyClient, SpotifyItem, SpotifyPlayRequest } from "./spotify-web-api";
export const SPOTIFY_BROWSER_APP: MediaBrowserApp = { packageName: SPOTIFY_PACKAGE, serviceClass: "", appName: SPOTIFY_APP_NAME };
const ROOT_ROWS: MediaBrowseItem[] = [
{ mediaId: "recent", title: "Recently played", subtitle: "", browsable: true, playable: false },
{ mediaId: "playlists", title: "Playlists", subtitle: "", browsable: true, playable: false },
{ mediaId: "liked", title: "Liked Songs", subtitle: "", browsable: true, playable: true },
{ mediaId: "albums", title: "Albums", subtitle: "", browsable: true, playable: false },
{ mediaId: "artists", title: "Artists", subtitle: "", browsable: true, playable: false },
];
/** Separates a track's URI from the context it plays in ("" for none, "liked" for Liked Songs). */
const TRACK_CONTEXT_SEPARATOR = "|";
export type SpotifyLibraryDeps = {
client: SpotifyClient;
player: SpotifyPlayer;
isSignedIn(): boolean;
/** The signed-in user's id (Liked Songs' context URI names it). */
userId(): string;
};
/**
* The signed-in user's Spotify library as a browse tree (Web API):
*
* root -> recent | playlists | liked | albums | artists
* playlist:<id>, album:<id> -> tracks (browsable and playable)
* artist:<id> -> albums
* track:<uri>|<context> -> plays the track within its context
*
* Only playlists the user owns or collaborates on can list their tracks
* (development-mode client IDs, since February 2026); others are play-only.
*/
export class SpotifyLibrary implements MediaBrowserBridge {
private connected = false;
/** Bumped on every connect/disconnect; answers to older requests are dropped. */
private generation = 0;
constructor(private readonly deps: SpotifyLibraryDeps) {}
listBrowsableApps(): MediaBrowserApp[] {
return this.deps.isSignedIn() ? [SPOTIFY_BROWSER_APP] : [];
}
connect(app: MediaBrowserApp): Promise<string> {
this.disconnect();
if (app.packageName !== SPOTIFY_PACKAGE) return Promise.reject(new Error("Not a Spotify library"));
if (!this.deps.isSignedIn()) return Promise.reject(new Error("Sign in under Spotify in the Music app menu first."));
this.connected = true;
return Promise.resolve("root");
}
async browse(parentId: string): Promise<MediaBrowseItem[]> {
if (!this.connected) throw new Error("Not connected");
const generation = this.generation;
const rows = await this.list(parentId);
if (generation !== this.generation) throw new Error("Disconnected");
return rows;
}
playFromMediaId(mediaId: string): void {
if (!this.connected) return;
const request = this.playRequest(mediaId);
if (request) void this.deps.player.startPlayback(request);
}
disconnect(): void {
this.connected = false;
this.generation++;
}
/** What playing a browse row starts; null for rows that don't play. */
playRequest(mediaId: string): SpotifyPlayRequest | null {
if (mediaId === "liked") return { contextUri: this.likedContextUri() };
const [kind, ...rest] = mediaId.split(":");
const id = rest.join(":");
switch (kind) {
case "playlist":
case "album":
case "artist":
return id ? { contextUri: `spotify:${kind}:${id}` } : null;
case "track": {
const separator = id.lastIndexOf(TRACK_CONTEXT_SEPARATOR);
const uri = separator < 0 ? id : id.slice(0, separator);
const context = separator < 0 ? "" : id.slice(separator + 1);
if (!uri) return null;
if (context === "liked") return { contextUri: this.likedContextUri(), offsetUri: uri };
return context ? { contextUri: context, offsetUri: uri } : { uris: [uri] };
}
default:
return null;
}
}
private async list(parentId: string): Promise<MediaBrowseItem[]> {
const client = this.deps.client;
if (parentId === "root") return ROOT_ROWS;
if (parentId === "recent") {
// The history repeats tracks; keep each one's latest play.
const seen = new Set<string>();
return (await client.recentlyPlayed())
.filter((played) => !seen.has(played.item.uri) && seen.add(played.item.uri))
.map((played) => trackRow(played.item, played.contextUri));
}
if (parentId === "playlists") {
const userId = this.deps.userId();
return (await client.playlists()).map((playlist) => {
const listable = playlist.collaborative || playlist.ownerId === userId;
return {
mediaId: `playlist:${playlist.id}`,
title: playlist.name,
subtitle: listable ? "" : playlist.ownerName,
browsable: listable,
playable: true,
};
});
}
if (parentId === "liked") return (await client.savedTracks()).map((item) => trackRow(item, "liked"));
if (parentId === "albums") return (await client.savedAlbums()).map(albumRow);
if (parentId === "artists") {
return (await client.followedArtists()).map((artist) => ({
mediaId: `artist:${artist.id}`,
title: artist.name,
subtitle: "",
browsable: true,
playable: true,
}));
}
const [kind, ...rest] = parentId.split(":");
const id = rest.join(":");
if (kind === "playlist") return (await client.playlistItems(id)).map((item) => trackRow(item, `spotify:playlist:${id}`));
if (kind === "album") return (await client.albumTracks(id)).map((item) => trackRow(item, `spotify:album:${id}`));
if (kind === "artist") return (await client.artistAlbums(id)).map(albumRow);
throw new Error(`Unknown Spotify library item: ${parentId}`);
}
private likedContextUri(): string {
return `spotify:user:${this.deps.userId()}:collection`;
}
}
function trackRow(item: SpotifyItem, contextUri: string): MediaBrowseItem {
return {
mediaId: `track:${item.uri}${contextUri ? `${TRACK_CONTEXT_SEPARATOR}${contextUri}` : ""}`,
title: item.name,
subtitle: item.artists,
browsable: false,
playable: true,
};
}
function albumRow(album: { id: string; name: string; artists: string }): MediaBrowseItem {
return { mediaId: `album:${album.id}`, title: album.name, subtitle: album.artists, browsable: true, playable: true };
}
+570
View File
@@ -0,0 +1,570 @@
import type { GrayImage } from "../graphics/image";
import {
DEFAULT_MEDIA_STATE,
interpolateMediaState,
type MediaControllerBridge,
type MediaControllerState,
type MediaCustomAction,
type MediaQueueItem,
} from "./media-types";
import {
SpotifyApiError,
type SpotifyClient,
type SpotifyDevice,
type SpotifyItem,
type SpotifyPlayback,
type SpotifyPlayRequest,
type SpotifyRepeat,
} from "./spotify-web-api";
export const SPOTIFY_APP_NAME = "Spotify";
/** The iOS app's bundle id; Spotify's Android package is com.spotify.music. */
export const SPOTIFY_PACKAGE = "com.spotify.client";
/**
* Poll intervals while something listens. The Web API has no push channel, so
* a track change shows up within one interval (sooner at a track's end, which
* is polled for directly); requests count against the client ID's quota.
*/
const POLL_PLAYING_MS = 5_000;
const POLL_PAUSED_MS = 10_000;
const POLL_IDLE_MS = 20_000;
const POLL_ERROR_MS = 30_000;
/** Re-read soon after a command: Spotify takes a moment to report its effect. */
const SETTLE_POLL_MS = 600;
const SETTLE_POLLS = 2;
const NOTICE_MS = 8_000;
/** Re-read the queue this often while the Music app shows it, besides on every track change. */
const QUEUE_MAX_AGE_MS = 30_000;
/** At most this often, even on track changes (and after a failed read). */
const QUEUE_MIN_INTERVAL_MS = 5_000;
/** Album art: the smallest of Spotify's sizes (640/300/64) at least this wide. */
const ART_MIN_WIDTH = 128;
const REPEAT_NEXT: Record<SpotifyRepeat, SpotifyRepeat> = { off: "context", context: "track", track: "off" };
const REPEAT_LABEL: Record<SpotifyRepeat, string> = { off: "Repeat: off", context: "Repeat: all", track: "Repeat: one" };
export type SpotifyPlayerDeps = {
client: SpotifyClient;
isSignedIn(): boolean;
/** Fetch an album-art image's encoded bytes; null on failure. */
download(url: string): Promise<Uint8Array | null>;
/** Encoded image bytes to the shared photo tone, fitted within maxSize; null when undecodable. */
decodeArt(bytes: Uint8Array, maxSize: number): GrayImage | null;
/** The phone's output volume, for playback on this phone (Spotify won't set a phone's volume). */
systemVolume: { get(): number; set(percent: number): void };
now?: () => number;
};
/**
* Spotify Connect playback (on this phone or any of the user's devices)
* through the Web API: state from polling GET /me/player while something
* listens, commands as API calls whose failures (no device, no Premium)
* become a short notice in the state.
*/
export class SpotifyPlayer implements MediaControllerBridge {
readonly accessPrompt = "Sign in to Spotify from the Music app menu to show and control Spotify.";
readonly accessLabel = "Spotify sign-in needed";
readonly accessAction = "open the menu";
/** Jumping skips forward through the queue, so the chosen track ends up first. */
readonly skipRebuildsQueue = true;
/** When Spotify was last seen playing (epoch ms); 0 = not this run. */
lastPlayingAtMs = 0;
private readonly now: () => number;
private readonly listeners = new Set<(state: MediaControllerState) => void>();
private playback: SpotifyPlayback | null = null;
private fetchedAtMs = 0;
private pollTimer: ReturnType<typeof setTimeout> | null = null;
private polling: Promise<void> | null = null;
private pollAgain = false;
private settlePolls = 0;
private lastSignature = "";
private notice = "";
private noticeUntilMs = 0;
private noticeTimer: ReturnType<typeof setTimeout> | null = null;
private upcoming: SpotifyItem[] = [];
/** The item the queue was read with; "" = never read. */
private queueCurrentUri = "";
/** When the queue was last read, or a read failed. */
private queueFetchedAtMs = 0;
private queueLoading = false;
private art: { url: string; bytes: Uint8Array | null; decoded: Map<number, GrayImage | null> } | null = null;
private artVersion = 0;
private liked: { uri: string; value: boolean | null } | null = null;
private volumeTarget: number | null = null;
private volumeSending = false;
constructor(private readonly deps: SpotifyPlayerDeps) {
this.now = deps.now ?? Date.now;
}
onStateChange(listener: (state: MediaControllerState) => void): () => void {
this.listeners.add(listener);
listener(this.snapshot());
if (this.listeners.size === 1) this.schedulePoll(0);
return () => {
this.listeners.delete(listener);
if (!this.listeners.size && !this.settlePolls) this.clearPollTimer();
};
}
snapshot(): MediaControllerState {
const notice = this.currentNotice();
if (!this.deps.isSignedIn()) {
return { ...DEFAULT_MEDIA_STATE, status: "Not signed in to Spotify.", notice };
}
const playback = this.playback;
if (!playback) {
return {
...DEFAULT_MEDIA_STATE,
accessEnabled: true,
packageName: SPOTIFY_PACKAGE,
appName: SPOTIFY_APP_NAME,
status: "Nothing is playing on Spotify.",
notice,
};
}
const item = playback.item;
const restricted = playback.device?.restricted === true;
const allowed = (action: string) => !restricted && !playback.disallows.includes(action);
const state: MediaControllerState = {
available: true,
accessEnabled: true,
packageName: SPOTIFY_PACKAGE,
appName: spotifyAppName(playback.device),
playbackState: playback.playing ? "playing" : "paused",
title: item?.name ?? (playback.playingType === "ad" ? "Advertisement" : ""),
artist: item?.artists ?? "",
album: item?.album ?? "",
positionMs: item ? playback.progressMs : -1,
durationMs: item?.durationMs ?? -1,
playbackSpeed: playback.playing ? 1 : 0,
status: "",
canPlayPause: allowed(playback.playing ? "pausing" : "resuming"),
canSkipNext: allowed("skipping_next"),
canSkipPrevious: allowed("skipping_prev"),
artVersion: this.artVersion,
customActions: restricted ? [] : this.customActions(playback),
notice,
};
return interpolateMediaState(state, this.fetchedAtMs, this.now());
}
async start(): Promise<void> {}
async stop(): Promise<void> {}
async playPause(): Promise<void> {
const playback = this.playback;
if (!playback?.playing) {
await this.startPlayback({});
return;
}
await this.command(() => this.deps.client.pause(), () => {
playback.progressMs = this.snapshot().positionMs;
playback.playing = false;
this.fetchedAtMs = this.now();
});
}
async skipNext(): Promise<void> {
await this.command(() => this.deps.client.next());
}
async skipPrevious(): Promise<void> {
await this.command(() => this.deps.client.previous());
}
/** Signed in; whether anything is playing is in the state. */
hasAccess(): boolean {
return this.deps.isSignedIn();
}
/** Sign-in happens from the Music app menu, which shows its progress. */
async requestAccess(): Promise<boolean> {
return this.hasAccess();
}
/**
* Queue ids are positions in getQueue (0 = the current item). The Web API
* has no jump, so this skips forward that many times.
*/
async skipToQueueItem(id: number): Promise<void> {
const steps = Math.round(id);
if (steps <= 0 || steps > this.upcoming.length) return;
await this.command(async () => {
for (let step = 0; step < steps; step++) await this.deps.client.next();
}, () => {
const target = this.upcoming[steps - 1];
if (this.playback && target) {
this.playback.item = target;
this.playback.progressMs = 0;
this.fetchedAtMs = this.now();
this.upcoming = this.upcoming.slice(steps);
this.queueCurrentUri = target.uri;
this.itemChanged(target);
}
});
}
getMediaVolumePercent(): number {
const device = this.playback?.device ?? null;
if (!usesDeviceVolume(device)) return this.deps.systemVolume.get();
return this.volumeTarget ?? device!.volumePercent;
}
setMediaVolumePercent(volumePercent: number): void {
const device = this.playback?.device ?? null;
const volume = Math.max(0, Math.min(100, Math.round(volumePercent)));
if (!usesDeviceVolume(device)) {
this.deps.systemVolume.set(volume);
return;
}
// Scrolling sends a value per step: keep one request in flight and send the latest after it.
device!.volumePercent = volume;
this.volumeTarget = volume;
if (!this.volumeSending) void this.sendVolume(device!.id);
}
getAlbumArt(maxSize: number): GrayImage | null {
const art = this.art;
if (!art?.bytes) return null;
const size = Math.round(maxSize);
if (!art.decoded.has(size)) art.decoded.set(size, this.deps.decodeArt(art.bytes, size));
return art.decoded.get(size) ?? null;
}
/**
* The current item, then what Spotify will play next. Read on demand (the
* Music app asks while it paints), so nothing else pays for the request; a
* stale list stays up, minus the items already played, until the new one
* arrives.
*/
getQueue(): MediaQueueItem[] {
const current = this.playback?.item;
if (!current) return [];
const age = this.now() - this.queueFetchedAtMs;
if (!this.queueLoading && (age > QUEUE_MAX_AGE_MS || (this.queueCurrentUri !== current.uri && age > QUEUE_MIN_INTERVAL_MS))) {
void this.loadQueue();
}
if (!this.queueCurrentUri) return [];
if (this.queueCurrentUri !== current.uri) {
const index = this.upcoming.findIndex((item) => item.uri === current.uri);
if (index >= 0) {
this.upcoming = this.upcoming.slice(index + 1);
this.queueCurrentUri = current.uri;
}
}
return [current, ...this.upcoming].map((item, index) => ({
id: index,
title: item.name,
subtitle: item.artists,
active: index === 0,
}));
}
async sendCustomAction(action: string): Promise<void> {
const playback = this.playback;
if (!playback) return;
const client = this.deps.client;
if (action === "shuffle") {
const shuffle = !playback.shuffle;
await this.command(() => client.setShuffle(shuffle), () => {
playback.shuffle = shuffle;
// Shuffling reorders what comes next.
this.queueFetchedAtMs = 0;
});
} else if (action === "repeat") {
const repeat = REPEAT_NEXT[playback.repeat];
await this.command(() => client.setRepeat(repeat), () => {
playback.repeat = repeat;
});
} else if (action === "like") {
const liked = this.liked;
if (!liked || liked.value === null) return;
const value = !liked.value;
await this.command(() => (value ? client.saveToLibrary([liked.uri]) : client.removeFromLibrary([liked.uri])), () => {
if (this.liked === liked) liked.value = value;
});
}
}
async refresh(): Promise<void> {
await this.poll();
}
/**
* Play something (a browsed item, a search result) or, with an empty
* request, resume. With no active device, starts on one the account has
* open (preferring a phone). Resolves to whether it worked; a failure is
* left as the state's notice.
*/
async startPlayback(request: SpotifyPlayRequest): Promise<boolean> {
const client = this.deps.client;
const playback = this.playback;
const ok = await this.command(async () => {
try {
await client.play(request);
} catch (error) {
if (!(error instanceof SpotifyApiError) || error.reason !== "NO_ACTIVE_DEVICE") throw error;
const device = pickDevice(await client.devices());
if (!device) throw error;
await client.play(request, device.id);
}
}, () => {
this.lastPlayingAtMs = this.now();
if (playback && this.playback === playback && !request.contextUri && !request.uris) {
playback.playing = true;
this.fetchedAtMs = this.now();
}
});
return ok;
}
/** The account changed (signed in, out, or as someone else): forget the old account's state. */
accountChanged(): void {
this.playback = null;
this.upcoming = [];
this.queueCurrentUri = "";
this.art = null;
this.artVersion++;
this.liked = null;
this.lastPlayingAtMs = 0;
this.emit(true);
if (this.listeners.size) this.schedulePoll(0);
}
/** Show a message about a failed command in the state's notice for a few seconds. */
showNotice(message: string): void {
this.notice = message;
this.noticeUntilMs = this.now() + NOTICE_MS;
if (this.noticeTimer !== null) clearTimeout(this.noticeTimer);
this.noticeTimer = setTimeout(() => {
this.noticeTimer = null;
this.emit(true);
}, NOTICE_MS + 50);
this.emit(true);
}
private currentNotice(): string {
return this.notice && this.now() < this.noticeUntilMs ? this.notice : "";
}
private customActions(playback: SpotifyPlayback): MediaCustomAction[] {
if (!playback.item) return [];
const actions: MediaCustomAction[] = [];
if (!playback.disallows.includes("toggling_shuffle")) {
actions.push({ action: "shuffle", name: playback.shuffle ? "Shuffle: on" : "Shuffle: off" });
}
if (!playback.disallows.includes("toggling_repeat_context") || !playback.disallows.includes("toggling_repeat_track")) {
actions.push({ action: "repeat", name: REPEAT_LABEL[playback.repeat] });
}
const liked = this.liked;
if (playback.item.type === "track" && liked?.uri === playback.item.uri && liked.value !== null) {
actions.push({ action: "like", name: liked.value ? "Unlike song" : "Like song" });
}
return actions;
}
/** Run a command; on success apply its expected effect at once, then re-read to confirm. */
private async command(run: () => Promise<void>, applied?: () => void): Promise<boolean> {
let ok = false;
try {
await run();
ok = true;
applied?.();
this.emit(true);
} catch (error) {
this.showNotice(errorMessage(error));
}
this.settlePolls = SETTLE_POLLS;
this.schedulePoll(SETTLE_POLL_MS);
return ok;
}
private async sendVolume(deviceId: string): Promise<void> {
this.volumeSending = true;
try {
while (this.volumeTarget !== null) {
const volume = this.volumeTarget;
try {
await this.deps.client.setVolume(volume, deviceId);
} catch (error) {
this.showNotice(errorMessage(error));
}
if (this.volumeTarget === volume) this.volumeTarget = null;
}
} finally {
this.volumeSending = false;
}
}
private poll(): Promise<void> {
if (this.polling) {
this.pollAgain = true;
return this.polling;
}
this.polling = this.pollOnce().finally(() => {
this.polling = null;
if (this.pollAgain) {
this.pollAgain = false;
void this.poll();
}
});
return this.polling;
}
private async pollOnce(): Promise<void> {
this.clearPollTimer();
if (!this.deps.isSignedIn()) {
if (this.playback) {
this.playback = null;
this.emit(true);
}
return;
}
let delay: number;
try {
const playback = await this.deps.client.playback();
this.fetchedAtMs = this.now();
this.applyPlayback(playback);
delay = this.nextPollDelay(playback);
} catch (error) {
if (!(error instanceof SpotifyApiError) || error.reason !== "signed_out") {
console.warn(`[spotify] playback read failed: ${error}`);
}
// Revoked access signs out (SpotifySession); the account listener resets the state.
delay = POLL_ERROR_MS;
}
if (this.settlePolls > 0) {
this.settlePolls--;
delay = Math.min(delay, 1_500);
}
this.emit(false);
this.schedulePoll(delay);
}
private applyPlayback(playback: SpotifyPlayback | null): void {
const previousUri = this.playback?.item?.uri ?? "";
this.playback = playback;
if (playback?.playing) this.lastPlayingAtMs = this.now();
const item = playback?.item ?? null;
if ((item?.uri ?? "") !== previousUri) this.itemChanged(item);
}
/** A new item: fetch its art and whether it is liked. */
private itemChanged(item: SpotifyItem | null): void {
const url = item ? pickArtUrl(item) : "";
if (url !== (this.art?.url ?? "")) {
this.artVersion++;
this.art = url ? { url, bytes: null, decoded: new Map() } : null;
if (url) void this.loadArt(url);
}
this.liked = item?.type === "track" ? { uri: item.uri, value: null } : null;
if (item?.type === "track") void this.loadLiked(item.uri);
}
private async loadArt(url: string): Promise<void> {
let bytes: Uint8Array | null = null;
try {
bytes = await this.deps.download(url);
} catch (error) {
console.warn(`[spotify] album art download failed: ${error}`);
}
if (!bytes?.length || this.art?.url !== url) return;
this.art.bytes = bytes;
this.artVersion++;
this.emit(true);
}
private async loadLiked(uri: string): Promise<void> {
try {
const [value] = await this.deps.client.libraryContains([uri]);
if (this.liked?.uri !== uri) return;
this.liked.value = Boolean(value);
this.emit(true);
} catch (error) {
console.warn(`[spotify] library check failed: ${error}`);
}
}
private async loadQueue(): Promise<void> {
this.queueLoading = true;
try {
const queue = await this.deps.client.queue();
this.upcoming = queue.upcoming;
this.queueCurrentUri = queue.current?.uri ?? "";
this.queueFetchedAtMs = this.now();
this.emit(true);
} catch (error) {
console.warn(`[spotify] queue read failed: ${error}`);
this.queueFetchedAtMs = this.now();
} finally {
this.queueLoading = false;
}
}
private nextPollDelay(playback: SpotifyPlayback | null): number {
if (!playback) return POLL_IDLE_MS;
if (!playback.playing) return POLL_PAUSED_MS;
const durationMs = playback.item?.durationMs ?? -1;
// Catch the change to the next track as it happens.
const remaining = durationMs > 0 ? durationMs - playback.progressMs + 700 : POLL_PLAYING_MS;
return Math.max(1_000, Math.min(POLL_PLAYING_MS, remaining));
}
private schedulePoll(delayMs: number): void {
if (!this.listeners.size && !this.settlePolls) return;
this.clearPollTimer();
this.pollTimer = setTimeout(() => {
this.pollTimer = null;
void this.poll();
}, delayMs);
}
private clearPollTimer(): void {
if (this.pollTimer !== null) clearTimeout(this.pollTimer);
this.pollTimer = null;
}
/** Tell listeners; unforced, only when something they draw (other than the advancing position) changed. */
private emit(force: boolean): void {
if (!this.listeners.size) return;
const state = this.snapshot();
const signature = JSON.stringify({ ...state, positionMs: 0 });
if (!force && signature === this.lastSignature) return;
this.lastSignature = signature;
for (const listener of this.listeners) listener(state);
}
}
/** "Spotify", or "Spotify · <device>" when it plays elsewhere than a phone. */
export function spotifyAppName(device: SpotifyDevice | null): string {
if (!device?.name || device.type === "Smartphone") return SPOTIFY_APP_NAME;
return `${SPOTIFY_APP_NAME} · ${device.name}`;
}
/** Spotify sets a speaker's or computer's volume; a phone's (this one) goes through the system volume. */
function usesDeviceVolume(device: SpotifyDevice | null): boolean {
return Boolean(device && device.type !== "Smartphone" && device.supportsVolume && device.volumePercent >= 0 && device.id);
}
/** A device to start playback on when none is active: an open one, a phone first. */
export function pickDevice(devices: SpotifyDevice[]): SpotifyDevice | null {
const usable = devices.filter((device) => device.id && !device.restricted);
return usable.find((device) => device.active)
?? usable.find((device) => device.type === "Smartphone")
?? usable[0]
?? null;
}
function pickArtUrl(item: SpotifyItem): string {
const images = [...item.images].sort((a, b) => a.width - b.width);
return (images.find((image) => image.width >= ART_MIN_WIDTH) ?? images[images.length - 1])?.url ?? "";
}
export function errorMessage(error: unknown): string {
if (error instanceof Error) return error.message;
return String(error);
}
+159
View File
@@ -0,0 +1,159 @@
import type { GrayImage } from "../graphics/image";
import { onSettingsStoreChanged } from "./settings-store";
import {
readSpotifyAccount,
saveSpotifyAccount,
signInToSpotify,
SPOTIFY_ACCOUNT_KEY,
SPOTIFY_CLIENT_ID_KEY,
SpotifySession,
type SpotifyAccount,
type WebSignIn,
} from "./spotify-auth";
import { SpotifyLibrary } from "./spotify-library";
import { errorMessage, SpotifyPlayer } from "./spotify-player";
import { SpotifyClient, type SpotifyFetch, type SpotifySearchType } from "./spotify-web-api";
/** Give up on a sign-in nobody finished on the phone. */
const SIGN_IN_TIMEOUT_MS = 10 * 60_000;
/** What the platform supplies (spotify.ios.ts). */
export type SpotifyPlatform = {
fetch: SpotifyFetch;
webSignIn: WebSignIn;
download(url: string): Promise<Uint8Array | null>;
decodeArt(bytes: Uint8Array, maxSize: number): GrayImage | null;
systemVolume: { get(): number; set(percent: number): void };
};
export type SpotifySignInState =
| { phase: "idle"; error: string }
/** The sign-in sheet is open on the phone, or waiting for Faceclaw to come to the foreground. */
| { phase: "waiting"; error: "" };
/**
* Spotify for the Music app, the media bridges and the assistant: the
* account and its sign-in, the Web API client, the player and the library.
*/
export class SpotifyService {
readonly client: SpotifyClient;
readonly player: SpotifyPlayer;
readonly library: SpotifyLibrary;
private signInState: SpotifySignInState = { phase: "idle", error: "" };
private readonly listeners = new Set<() => void>();
private accountKey: string;
constructor(private readonly platform: SpotifyPlatform) {
const isSignedIn = () => readSpotifyAccount() !== null;
this.client = new SpotifyClient(platform.fetch, new SpotifySession(platform.fetch));
this.player = new SpotifyPlayer({
client: this.client,
isSignedIn,
download: (url) => platform.download(url),
decodeArt: (bytes, maxSize) => platform.decodeArt(bytes, maxSize),
systemVolume: platform.systemVolume,
});
this.library = new SpotifyLibrary({
client: this.client,
player: this.player,
isSignedIn,
userId: () => readSpotifyAccount()?.userId ?? "",
});
this.accountKey = accountIdentity(readSpotifyAccount());
onSettingsStoreChanged((key) => {
if (key !== SPOTIFY_ACCOUNT_KEY && key !== SPOTIFY_CLIENT_ID_KEY) return;
// Token refreshes rewrite the account too; only a different sign-in resets the player.
const identity = accountIdentity(readSpotifyAccount());
if (identity !== this.accountKey) {
this.accountKey = identity;
this.player.accountChanged();
}
this.emit();
});
}
account(): SpotifyAccount | null {
return readSpotifyAccount();
}
signInStatus(): SpotifySignInState {
return this.signInState;
}
/** Repaint hook for the sign-in menu: account or sign-in progress changed. */
onChange(listener: () => void): () => void {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
/**
* Open Spotify's sign-in page on the phone (it waits for Faceclaw to be in
* the foreground); the outcome lands in signInStatus().
*/
async signIn(): Promise<void> {
if (this.signInState.phase === "waiting") return;
this.signInState = { phase: "waiting", error: "" };
this.emit();
const timer = setTimeout(() => this.platform.webSignIn.cancel(), SIGN_IN_TIMEOUT_MS);
try {
await signInToSpotify(this.platform.fetch, this.platform.webSignIn);
this.signInState = { phase: "idle", error: "" };
} catch (error) {
const message = errorMessage(error);
this.signInState = { phase: "idle", error: message === "cancelled" ? "" : message };
} finally {
clearTimeout(timer);
this.emit();
}
}
cancelSignIn(): void {
this.platform.webSignIn.cancel();
}
signOut(): void {
saveSpotifyAccount(null);
this.signInState = { phase: "idle", error: "" };
this.emit();
}
/**
* Search Spotify and play the best match: a track, or the album, artist or
* playlist when the request names that kind. Resolves to what started, or
* rejects with why not.
*/
async playSearch(query: string, kind: SpotifySearchType = "track"): Promise<string> {
if (!readSpotifyAccount()) throw new Error("Not signed in to Spotify. Sign in under Spotify in the Music app menu on the glasses.");
const results = await this.client.search(query, [kind], 3);
let request: { contextUri?: string; uris?: string[] } | null = null;
let description = "";
if (kind === "track" && results.tracks[0]) {
const track = results.tracks[0];
request = { uris: [track.uri] };
description = `${track.name}${track.artists ? ` by ${track.artists}` : ""}`;
} else if (kind === "album" && results.albums[0]) {
const album = results.albums[0];
request = { contextUri: album.uri };
description = `the album ${album.name}${album.artists ? ` by ${album.artists}` : ""}`;
} else if (kind === "artist" && results.artists[0]) {
request = { contextUri: results.artists[0].uri };
description = results.artists[0].name;
} else if (kind === "playlist" && results.playlists[0]) {
request = { contextUri: results.playlists[0].uri };
description = `the playlist ${results.playlists[0].name}`;
}
if (!request) throw new Error(`Spotify found no ${kind} matching "${query}".`);
if (!(await this.player.startPlayback(request))) {
throw new Error(this.player.snapshot().notice || "Spotify didn't start playback.");
}
return description;
}
private emit(): void {
for (const listener of this.listeners) listener();
}
}
function accountIdentity(account: SpotifyAccount | null): string {
return account ? `${account.clientId}/${account.userId}` : "";
}
+474
View File
@@ -0,0 +1,474 @@
/**
* Spotify Web API over HTTPS with the signed-in user's token: the player
* (what is playing on any of the user's Spotify Connect devices, and control
* of it), the user's library, and search, normalized into the small shapes
* the Music app uses. No NativeScript imports, so the Node tests drive it with
* a fake fetch.
*
* Every personal client ID is a development-mode app, which since February
* 2026 means: the client ID's owner needs Premium, playback control needs
* Premium, at most five allowlisted users, only owned or collaborative
* playlists list their items, no batch lookups, and search and artist albums
* return at most 10 results per page.
*/
export const SPOTIFY_API_BASE = "https://api.spotify.com/v1";
const HTTP_TIMEOUT_MS = 12_000;
/** The most a library listing gathers (a page is 50); enough to scroll through on the glasses. */
const MAX_LIST_ITEMS = 500;
/** The subset of fetch this client needs (fetchTextWithUserAgent in the app). */
export type SpotifyFetch = (
url: string,
init: { method: string; headers: Record<string, string>; body?: string },
) => Promise<{ status: number; text(): Promise<string> }>;
/** Hands out a current access token, refreshing it when expired or when forced (after a 401). */
export type SpotifyTokenSource = {
accessToken(forceRefresh: boolean): Promise<string>;
};
/** A failed request; `reason` is Spotify's player error code (NO_ACTIVE_DEVICE, PREMIUM_REQUIRED, ...). */
export class SpotifyApiError extends Error {
constructor(
message: string,
readonly status: number,
readonly reason = "",
) {
super(message);
this.name = "SpotifyApiError";
}
}
export type SpotifyImage = { url: string; width: number; height: number };
export type SpotifyItem = {
uri: string;
type: "track" | "episode";
name: string;
/** Track artists, comma-joined; for an episode, its show. */
artists: string;
/** The track's album; for an episode, its show. "" when the API leaves it out (album track listings). */
album: string;
durationMs: number;
images: SpotifyImage[];
};
export type SpotifyDevice = {
id: string;
name: string;
/** "Smartphone", "Computer", "Speaker", ... */
type: string;
active: boolean;
/** Restricted devices accept no Web API commands. */
restricted: boolean;
/** -1 when the device reports none. */
volumePercent: number;
supportsVolume: boolean;
};
export type SpotifyRepeat = "off" | "context" | "track";
export type SpotifyPlayback = {
device: SpotifyDevice | null;
playing: boolean;
progressMs: number;
shuffle: boolean;
repeat: SpotifyRepeat;
/** The playlist / album / artist playing from; "" for none (a queue of single tracks). */
contextUri: string;
/** null during an ad, between items, or for content the API does not describe. */
item: SpotifyItem | null;
/** "track", "episode", "ad" or "unknown". */
playingType: string;
/** The player actions the context forbids, as Spotify names them: pausing, resuming, skipping_next, ... */
disallows: string[];
};
export type SpotifyQueue = { current: SpotifyItem | null; upcoming: SpotifyItem[] };
export type SpotifyUser = { id: string; displayName: string; product: string };
export type SpotifyPlaylist = {
id: string;
uri: string;
name: string;
ownerId: string;
ownerName: string;
collaborative: boolean;
total: number;
};
export type SpotifyAlbum = { id: string; uri: string; name: string; artists: string };
export type SpotifyArtist = { id: string; uri: string; name: string };
/** A played track and the context it played from ("" when none). */
export type SpotifyPlayed = { item: SpotifyItem; contextUri: string };
export type SpotifySearchResults = {
tracks: SpotifyItem[];
albums: SpotifyAlbum[];
artists: SpotifyArtist[];
playlists: SpotifyPlaylist[];
};
export type SpotifySearchType = "track" | "album" | "artist" | "playlist";
/**
* What to play: a context (playlist, album, artist, show) optionally starting
* at one of its items, or a list of track URIs. Empty resumes the current
* playback.
*/
export type SpotifyPlayRequest = {
contextUri?: string;
uris?: string[];
offsetUri?: string;
offsetPosition?: number;
};
type Query = Record<string, string | number | boolean | undefined>;
export class SpotifyClient {
constructor(
private readonly fetchImpl: SpotifyFetch,
private readonly tokens: SpotifyTokenSource,
) {}
/** The current playback on whichever device is active; null when nothing is. */
async playback(): Promise<SpotifyPlayback | null> {
const raw = await this.request("GET", "/me/player", { query: { additional_types: "episode" } });
return raw ? parsePlayback(raw) : null;
}
async queue(): Promise<SpotifyQueue> {
const raw = await this.request("GET", "/me/player/queue");
return {
current: parseItem(raw?.currently_playing),
upcoming: asArray(raw?.queue).map(parseItem).filter(isPresent),
};
}
async devices(): Promise<SpotifyDevice[]> {
const raw = await this.request("GET", "/me/player/devices");
return asArray(raw?.devices).map(parseDevice).filter(isPresent);
}
async play(request: SpotifyPlayRequest = {}, deviceId = ""): Promise<void> {
const body: Record<string, unknown> = {};
if (request.contextUri) body.context_uri = request.contextUri;
if (request.uris?.length) body.uris = request.uris;
if (request.offsetUri) body.offset = { uri: request.offsetUri };
else if (request.offsetPosition !== undefined) body.offset = { position: request.offsetPosition };
await this.request("PUT", "/me/player/play", { query: { device_id: deviceId || undefined }, body });
}
async pause(): Promise<void> {
await this.request("PUT", "/me/player/pause");
}
async next(): Promise<void> {
await this.request("POST", "/me/player/next");
}
async previous(): Promise<void> {
await this.request("POST", "/me/player/previous");
}
async setShuffle(on: boolean): Promise<void> {
await this.request("PUT", "/me/player/shuffle", { query: { state: on } });
}
async setRepeat(mode: SpotifyRepeat): Promise<void> {
await this.request("PUT", "/me/player/repeat", { query: { state: mode } });
}
async setVolume(percent: number, deviceId = ""): Promise<void> {
const volume = Math.max(0, Math.min(100, Math.round(percent)));
await this.request("PUT", "/me/player/volume", { query: { volume_percent: volume, device_id: deviceId || undefined } });
}
/** Move playback to a device (one that shows in devices() but is not active), optionally playing. */
async transfer(deviceId: string, play: boolean): Promise<void> {
await this.request("PUT", "/me/player", { body: { device_ids: [deviceId], play } });
}
async me(): Promise<SpotifyUser> {
const raw = await this.request("GET", "/me");
return { id: str(raw?.id), displayName: str(raw?.display_name) || str(raw?.id), product: str(raw?.product) };
}
/** Whether each URI is in the user's library (Liked Songs for tracks). */
async libraryContains(uris: string[]): Promise<boolean[]> {
const raw = await this.request("GET", "/me/library/contains", { query: { uris: uris.join(",") } });
return asArray(raw).map(Boolean);
}
async saveToLibrary(uris: string[]): Promise<void> {
await this.request("PUT", "/me/library", { query: { uris: uris.join(",") } });
}
async removeFromLibrary(uris: string[]): Promise<void> {
await this.request("DELETE", "/me/library", { query: { uris: uris.join(",") } });
}
playlists(): Promise<SpotifyPlaylist[]> {
return this.collect("/me/playlists", { limit: 50 }, (raw) => asArray(raw?.items).map(parsePlaylist));
}
/** Only playlists the user owns or collaborates on list their items (403 otherwise). */
playlistItems(playlistId: string): Promise<SpotifyItem[]> {
return this.collect(`/playlists/${encodeURIComponent(playlistId)}/items`, { limit: 50, additional_types: "track,episode" },
(raw) => asArray(raw?.items).map((entry: any) => parseItem(entry?.item ?? entry?.track)));
}
savedTracks(): Promise<SpotifyItem[]> {
return this.collect("/me/tracks", { limit: 50 }, (raw) => asArray(raw?.items).map((entry: any) => parseItem(entry?.track)));
}
savedAlbums(): Promise<SpotifyAlbum[]> {
return this.collect("/me/albums", { limit: 50 }, (raw) => asArray(raw?.items).map((entry: any) => parseAlbum(entry?.album)));
}
albumTracks(albumId: string): Promise<SpotifyItem[]> {
return this.collect(`/albums/${encodeURIComponent(albumId)}/tracks`, { limit: 50 }, (raw) => asArray(raw?.items).map(parseItem));
}
followedArtists(): Promise<SpotifyArtist[]> {
// Cursor-paged, with the page wrapped in an "artists" object.
return this.collect("/me/following", { type: "artist", limit: 50 }, (raw) => asArray(raw?.artists?.items).map(parseArtist),
(raw) => str(raw?.artists?.next));
}
artistAlbums(artistId: string): Promise<SpotifyAlbum[]> {
return this.collect(`/artists/${encodeURIComponent(artistId)}/albums`, { include_groups: "album,single,compilation", limit: 10 },
(raw) => asArray(raw?.items).map(parseAlbum), undefined, 100);
}
/** Recently played tracks, newest first (one page: the API keeps only the last 50). */
async recentlyPlayed(): Promise<SpotifyPlayed[]> {
const raw = await this.request("GET", "/me/player/recently-played", { query: { limit: 50 } });
return asArray(raw?.items)
.map((entry: any) => {
const item = parseItem(entry?.track);
return item ? { item, contextUri: str(entry?.context?.uri) } : null;
})
.filter(isPresent);
}
async search(query: string, types: SpotifySearchType[], limit = 5): Promise<SpotifySearchResults> {
const raw = await this.request("GET", "/search", {
query: { q: query, type: types.join(","), limit: Math.max(1, Math.min(10, Math.round(limit))) },
});
return {
tracks: asArray(raw?.tracks?.items).map(parseItem).filter(isPresent),
albums: asArray(raw?.albums?.items).map(parseAlbum).filter(isPresent),
artists: asArray(raw?.artists?.items).map(parseArtist).filter(isPresent),
// Search results include nulls for playlists the API can no longer return.
playlists: asArray(raw?.playlists?.items).map(parsePlaylist).filter(isPresent),
};
}
/** Gather a paged listing by following its `next` links, up to maxItems. */
private async collect<T>(
path: string,
query: Query,
parse: (raw: any) => Array<T | null>,
nextOf: (raw: any) => string = (raw) => str(raw?.next),
maxItems = MAX_LIST_ITEMS,
): Promise<T[]> {
const items: T[] = [];
let raw = await this.request("GET", path, { query });
for (;;) {
for (const item of parse(raw)) if (item) items.push(item);
const next = nextOf(raw);
if (!next || items.length >= maxItems) return items.slice(0, maxItems);
raw = await this.request("GET", next);
}
}
/**
* One API call. `path` is relative to the API base, or an absolute `next`
* link from a page. Resolves to the parsed JSON body (null when empty);
* a 401 refreshes the token and retries once.
*/
private async request(method: string, path: string, options: { query?: Query; body?: unknown } = {}): Promise<any> {
const url = path.startsWith("https://") ? path : `${SPOTIFY_API_BASE}${path}${buildQuery(options.query)}`;
const headers: Record<string, string> = { Accept: "application/json" };
if (options.body !== undefined) headers["Content-Type"] = "application/json";
// A command without a body still sends an empty one: Spotify answers a
// PUT or POST with no Content-Length with 411.
const body = options.body !== undefined ? JSON.stringify(options.body) : method === "GET" ? undefined : "";
for (let attempt = 0; ; attempt++) {
const authorization = `Bearer ${await this.tokens.accessToken(attempt > 0)}`;
const response = await withTimeout(this.fetchImpl(url, { method, headers: { ...headers, Authorization: authorization }, body }));
const text = await withTimeout(response.text());
if (response.status === 401 && attempt === 0) continue;
if (response.status >= 200 && response.status < 300) {
if (!text.trim()) return null;
try {
return JSON.parse(text);
} catch {
// Commands answer 200 with a snapshot id or plain text; nothing to read.
return null;
}
}
throw apiError(response.status, text);
}
}
}
/** Form / query encoding (not URLSearchParams, which the NativeScript runtime mishandles). */
export function buildQuery(query: Query | undefined): string {
if (!query) return "";
const parts = Object.keys(query)
.filter((key) => query[key] !== undefined && query[key] !== "")
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(String(query[key]))}`);
return parts.length ? `?${parts.join("&")}` : "";
}
async function withTimeout<T>(promise: Promise<T>): Promise<T> {
let timer: ReturnType<typeof setTimeout> | null = null;
const timeout = new Promise<never>((_resolve, reject) => {
timer = setTimeout(() => reject(new SpotifyApiError("Spotify didn't answer. Check the phone's connection.", 0)), HTTP_TIMEOUT_MS);
});
try {
return await Promise.race([promise, timeout]);
} catch (error) {
if (error instanceof SpotifyApiError) throw error;
throw new SpotifyApiError(`Can't reach Spotify: ${(error as Error)?.message ?? error}`, 0);
} finally {
if (timer) clearTimeout(timer);
}
}
/** A message for the glasses from Spotify's error body ({error: {status, message, reason}}). */
function apiError(status: number, body: string): SpotifyApiError {
let message = "";
let reason = "";
try {
const parsed = JSON.parse(body);
message = str(parsed?.error?.message ?? parsed?.error_description);
reason = str(parsed?.error?.reason);
} catch {
// Not JSON (a gateway error page).
}
switch (reason) {
case "NO_ACTIVE_DEVICE":
return new SpotifyApiError("Spotify isn't open on any device. Open Spotify on your phone, then try again.", status, reason);
case "PREMIUM_REQUIRED":
return new SpotifyApiError("Controlling Spotify playback needs Spotify Premium.", status, reason);
}
if (status === 401) return new SpotifyApiError("Spotify no longer accepts this sign-in. Sign in again from the Music app menu.", status, reason);
if (status === 403 && /registered|developer dashboard/i.test(message)) {
return new SpotifyApiError("This Spotify account isn't on the client ID's user list (Developer Dashboard > User Management).", status, reason);
}
if (status === 429) return new SpotifyApiError("Spotify is limiting requests; try again in a minute.", status, reason);
return new SpotifyApiError(message ? `Spotify: ${message}` : `Spotify error (HTTP ${status}).`, status, reason);
}
export function parsePlayback(raw: any): SpotifyPlayback {
const repeat = str(raw?.repeat_state);
const disallows = raw?.actions?.disallows;
return {
device: parseDevice(raw?.device),
playing: raw?.is_playing === true,
progressMs: num(raw?.progress_ms, 0),
shuffle: raw?.shuffle_state === true,
repeat: repeat === "context" || repeat === "track" ? repeat : "off",
contextUri: str(raw?.context?.uri),
item: parseItem(raw?.item),
playingType: str(raw?.currently_playing_type) || "unknown",
disallows: disallows && typeof disallows === "object" ? Object.keys(disallows).filter((key) => disallows[key] === true) : [],
};
}
export function parseItem(raw: any): SpotifyItem | null {
if (!raw || typeof raw !== "object" || typeof raw.uri !== "string" || !raw.uri) return null;
if (raw.type === "episode") {
const show = str(raw.show?.name);
return {
uri: raw.uri,
type: "episode",
name: str(raw.name),
artists: show || str(raw.show?.publisher),
album: show,
durationMs: num(raw.duration_ms, -1),
images: parseImages(raw.images?.length ? raw.images : raw.show?.images),
};
}
return {
uri: raw.uri,
type: "track",
name: str(raw.name),
artists: joinNames(raw.artists),
album: str(raw.album?.name),
durationMs: num(raw.duration_ms, -1),
images: parseImages(raw.album?.images),
};
}
export function parseDevice(raw: any): SpotifyDevice | null {
if (!raw || typeof raw !== "object") return null;
const volume = raw.volume_percent;
return {
id: str(raw.id),
name: str(raw.name),
type: str(raw.type),
active: raw.is_active === true,
restricted: raw.is_restricted === true,
volumePercent: typeof volume === "number" && Number.isFinite(volume) ? volume : -1,
supportsVolume: raw.supports_volume === true,
};
}
function parsePlaylist(raw: any): SpotifyPlaylist | null {
if (!raw || typeof raw !== "object" || !raw.id) return null;
return {
id: str(raw.id),
uri: str(raw.uri) || `spotify:playlist:${raw.id}`,
name: str(raw.name),
ownerId: str(raw.owner?.id),
ownerName: str(raw.owner?.display_name) || str(raw.owner?.id),
collaborative: raw.collaborative === true,
// `items` replaced the deprecated `tracks` summary in 2026.
total: num(raw.items?.total ?? raw.tracks?.total, -1),
};
}
function parseAlbum(raw: any): SpotifyAlbum | null {
if (!raw || typeof raw !== "object" || !raw.id) return null;
return { id: str(raw.id), uri: str(raw.uri) || `spotify:album:${raw.id}`, name: str(raw.name), artists: joinNames(raw.artists) };
}
function parseArtist(raw: any): SpotifyArtist | null {
if (!raw || typeof raw !== "object" || !raw.id) return null;
return { id: str(raw.id), uri: str(raw.uri) || `spotify:artist:${raw.id}`, name: str(raw.name) };
}
function parseImages(raw: any): SpotifyImage[] {
return asArray(raw)
.filter((image: any) => image && typeof image.url === "string" && image.url)
.map((image: any) => ({ url: image.url, width: num(image.width, 0), height: num(image.height, 0) }));
}
function joinNames(raw: any): string {
return asArray(raw).map((entry: any) => str(entry?.name)).filter(Boolean).join(", ");
}
function asArray(value: unknown): any[] {
return Array.isArray(value) ? value : [];
}
function str(value: unknown): string {
return typeof value === "string" ? value : "";
}
function num(value: unknown, fallback: number): number {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
}
function isPresent<T>(value: T | null | undefined): value is T {
return value !== null && value !== undefined;
}
+63
View File
@@ -0,0 +1,63 @@
import { fetchTextWithUserAgent } from "../util/http";
import { downloadBinary } from "./binary-http.ios";
import { grayImageFromPacket, PHOTO_TONE } from "./image-files";
import { fromData, toData } from "./kotlin-data";
import type { WebSignIn } from "./spotify-auth";
import { SpotifyService } from "./spotify-service";
declare const FaceclawAppleMusic: any;
declare const FaceclawCrypto: any;
declare const FaceclawGraphics: any;
declare const FaceclawKitIosProtocol: any;
declare const FaceclawWebSignIn: any;
const ART_DOWNLOAD_TIMEOUT_MS = 10_000;
/** Sign-in in the system browser sheet, which FaceclawWebSignIn closes once the loopback redirect arrives. */
const webSignIn: WebSignIn = {
createPkce: () => {
// 64 URL-safe characters (RFC 7636 allows 43..128).
const verifier = String(FaceclawCrypto.randomBase64Url(48));
return { verifier, challenge: String(FaceclawCrypto.sha256Base64Url(verifier)) };
},
randomState: () => String(FaceclawCrypto.randomBase64Url(16)),
open: (authorizeUrl, port, path) =>
new Promise<string>((resolve, reject) => {
FaceclawWebSignIn.shared().signInPortPathCompletion(authorizeUrl, port, path, (redirectUrl: string | null, error: string | null) => {
if (redirectUrl) resolve(String(redirectUrl));
else reject(new Error(String(error || "Sign-in failed")));
});
}),
cancel: () => FaceclawWebSignIn.shared().cancel(),
};
let protocol: any = null;
let service: SpotifyService | null = null;
/** The Spotify service, created on first use (main thread only: it owns timers and the sign-in sheet). */
export function spotifyService(): SpotifyService | null {
service ??= new SpotifyService({
fetch: (url, init) => fetchTextWithUserAgent(url, init),
webSignIn,
download: async (url) => {
const response = await downloadBinary(url, ART_DOWNLOAD_TIMEOUT_MS);
return response.status === 200 ? response.bytes : null;
},
decodeArt: (bytes, maxSize) => {
const packet = FaceclawGraphics.bgraPacketFromImageDataMaxSize(toData(bytes), Math.round(maxSize));
if (!packet) return null;
// The shared Kotlin tone curve and dither, as for Apple Music's art.
protocol ??= FaceclawKitIosProtocol.new();
return grayImageFromPacket(fromData(protocol.grayPacketBgraGammaDither(packet, PHOTO_TONE.gamma, PHOTO_TONE.dither)));
},
// FaceclawAppleMusic's system output volume needs no media-library access.
systemVolume: {
get: () => {
const volume = Number(FaceclawAppleMusic.shared().volumePercent());
return Number.isFinite(volume) ? Math.max(-1, Math.min(100, Math.round(volume))) : -1;
},
set: (percent) => FaceclawAppleMusic.shared().setVolumePercent(Math.max(0, Math.min(100, Math.round(percent)))),
},
});
return service;
}
+9
View File
@@ -0,0 +1,9 @@
import type { SpotifyService } from "./spotify-service";
/**
* Spotify through its Web API is wired up on iOS only (spotify.ios.ts): on
* Android, Spotify publishes a media session the Music app already controls.
*/
export function spotifyService(): SpotifyService | null {
return null;
}
+16
View File
@@ -18,6 +18,7 @@ import {
} from "~/assistant/models";
import { isLocalModelReady } from "../native/llama";
import { normalizeWhisperServerUrl } from "../native/whisper-server-stt";
import { isValidSpotifyClientId, SPOTIFY_CLIENT_ID_KEY } from "../native/spotify-auth";
import { drawRightValueMenuItem, drawToggleMenuItem, MenuItem, openModalMenu } from "./menu";
import { ANIMATION_SPEEDS, animationSpeedLabel, scaleAnimationDuration, type AnimationSpeed } from "./animation-speed";
import { MENU_ANIMATION_KEY } from "./menu-animation-pref";
@@ -1101,6 +1102,21 @@ export const mapboxApiKeySetting = new ConfigSettingString({
description: "Mapbox public token (pk. prefix), used by the Navigate app for maps, geocoding, and directions.",
});
/** Edited from the Music app's Spotify menu (iOS); a sign-in is tied to the client ID it was made with. */
export const spotifyClientIdSetting = new ConfigSettingString({
id: "spotify-client-id",
label: "Client ID",
storageKey: SPOTIFY_CLIENT_ID_KEY,
defaultValue: "",
editorTitle: "Spotify client ID (from your app in the Spotify Developer Dashboard)",
glassesEditTitle: "Edit Spotify client ID",
normalize: (value) => (value ?? "").trim(),
validate: (value) => (!value || isValidSpotifyClientId(value) ? null
: "A Spotify client ID is 32 letters and digits, shown on your app's page in the Spotify Developer Dashboard."),
formatValue: (value) => (value ? `${value.slice(0, 6)}...` : "(not set)"),
description: "The client ID of your own Spotify developer app, which Faceclaw signs in with.",
});
/**
* Staging buffer for the Terminal app's "Add connection" flow: the worker
* asks the shell to open the phone text editor on this setting, the user
+8 -2
View File
@@ -56,6 +56,8 @@ function controller(music = fakeMusic()) {
'./image-files': { grayImageFromPacket: bytes => (bytes && bytes.length ? { bytes } : null), PHOTO_TONE: { gamma: 2.2, dither: true } },
'./kotlin-data': { fromData: data => data },
'./media-types': types,
// Signed out of Spotify: the composite bridge follows Apple Music alone.
'./spotify.ios': { spotifyService: () => null },
}, {
FaceclawAppleMusic: { shared: () => music },
FaceclawKitIosProtocol: { new: () => ({ grayPacketBgraGammaDither: (packet, gamma, dither) => {
@@ -155,7 +157,11 @@ test('access requests share one prompt, and denied or restricted access points t
});
function browser(music = fakeMusic()) {
const api = load('app/native/media-browser.ios.ts', { './media-types': types }, {
const api = load('app/native/media-browser.ios.ts', {
'./media-types': types,
'./spotify.ios': { spotifyService: () => null },
'./spotify-player': { SPOTIFY_PACKAGE: 'com.spotify.client' },
}, {
FaceclawAppleMusic: { shared: () => music },
});
return { bridge: api.mediaBrowserBridge, music };
@@ -165,7 +171,7 @@ test('the Apple Music library browses only with access and drops answers after d
const f = browser();
const [app] = f.bridge.listBrowsableApps();
assert.equal(app.appName, 'Apple Music');
await assert.rejects(f.bridge.connect({ ...app, packageName: 'other' }), /Only Apple Music/);
await assert.rejects(f.bridge.connect({ ...app, packageName: 'other' }), /Not an Apple Music library/);
await assert.rejects(f.bridge.browse('root'), /Not connected/);
assert.equal(await f.bridge.connect(app), 'root');
+1
View File
@@ -229,6 +229,7 @@ test('iOS assistant calendar tool awaits events, reports denied access and failu
const { registerSystemTools } = load('app/assistant/system-tools.ts', {
'../native/calendar': f.api, '../native/calendar-permissions': f.permissions,
'../native/media-controller': { mediaControllerBridge: { start: async () => {} } },
'../native/spotify': { spotifyService: () => null },
'../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {},
}, { global: { isIOS: true } });
registerSystemTools({ registerSystemTool: (definition, handler) => registered.set(definition.name, handler) });
+18
View File
@@ -0,0 +1,18 @@
// macOS integration test for the loopback OAuth redirect receiver compiled into iOS
// (Spotify sign-in): builds FaceclawLoopbackRedirect.m with a probe and talks HTTP to it.
const { execFileSync } = require('node:child_process');
const { mkdtempSync, rmSync } = require('node:fs');
const { tmpdir } = require('node:os');
const { join, resolve } = require('node:path');
const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-loopback-'));
try {
execFileSync('xcrun', ['clang', '-fobjc-arc', '-framework', 'Foundation', '-I', 'App_Resources/iOS/src',
'tests/native/ios-loopback-redirect-test.m', 'App_Resources/iOS/src/FaceclawLoopbackRedirect.m',
'-o', join(dir, 'loopback-test')], { cwd: root, stdio: 'inherit' });
// A port unlikely to be taken on a dev machine; the app's own port is in spotify-auth.ts.
execFileSync(join(dir, 'loopback-test'), ['43899'], { stdio: 'inherit' });
} catch {
process.exitCode = 1;
} finally {
rmSync(dir, { recursive: true, force: true });
}
+530
View File
@@ -0,0 +1,530 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
function load(file, modules = {}, globals = {}) {
const sandbox = { exports: {}, console, JSON, Promise, Date, Math, setTimeout, clearTimeout, require: id => {
if (!(id in modules)) throw new Error(`Unstubbed import: ${id}`);
return modules[id];
}, ...globals };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, sandbox);
return sandbox.exports;
}
const CLIENT_ID = '0123456789abcdef0123456789abcdef';
/** Values from the sandbox carry its own Array/Object prototypes; compare their plain JSON form. */
const plain = value => JSON.parse(JSON.stringify(value));
/** settings-store stand-in: synchronous change notifications. */
function fakeSettings(initial = {}) {
const values = new Map(Object.entries(initial));
const listeners = new Set();
return {
values,
getStringSetting: (key, fallback) => (values.has(key) ? values.get(key) : fallback),
setStringSetting: (key, value) => { values.set(key, value); for (const listener of listeners) listener(key); },
onSettingsStoreChanged: listener => { listeners.add(listener); return () => listeners.delete(listener); },
};
}
/** Manual timers: run() fires everything due, in order. */
function fakeTimers() {
let now = 1_000_000; let nextId = 1;
const pending = new Map();
return {
now: () => now,
setTimeout: (fn, delay) => { const id = nextId++; pending.set(id, { fn, at: now + (delay || 0) }); return id; },
clearTimeout: id => pending.delete(id),
advance(ms) {
now += ms;
for (const [id, timer] of [...pending].sort((a, b) => a[1].at - b[1].at)) {
if (timer.at <= now && pending.has(id)) { pending.delete(id); timer.fn(); }
}
},
delays: () => [...pending.values()].map(timer => timer.at - now),
};
}
/**
* Spotify's endpoints as a route table: "METHOD /path" -> response or function(call).
* Records every call with its parsed URL, headers and body.
*/
function fakeFetch(routes) {
const calls = [];
const fetch = async (url, init) => {
const parsed = new URL(url);
const call = { url, method: init.method, path: parsed.pathname.replace('/v1', ''), query: Object.fromEntries(parsed.searchParams),
headers: init.headers, body: init.body };
calls.push(call);
let route = routes[`${init.method} ${call.path}`];
if (typeof route === 'function') route = route(call);
if (Array.isArray(route)) route = route.length > 1 ? route.shift() : route[0];
const { status = 200, body = '' } = route ?? { status: 404, body: '{"error":{"status":404,"message":"no route"}}' };
return { status, text: async () => (typeof body === 'string' ? body : JSON.stringify(body)) };
};
return { fetch, calls };
}
function loadSpotify({ settings = fakeSettings(), timers = null } = {}) {
const globals = timers ? { setTimeout: timers.setTimeout, clearTimeout: timers.clearTimeout } : {};
const types = load('app/native/media-types.ts');
const api = load('app/native/spotify-web-api.ts', {}, globals);
const auth = load('app/native/spotify-auth.ts', { './settings-store': settings, './spotify-web-api': api }, globals);
const player = load('app/native/spotify-player.ts', { './media-types': types, './spotify-web-api': api }, globals);
const library = load('app/native/spotify-library.ts', { './spotify-player': player }, globals);
const service = load('app/native/spotify-service.ts', {
'./settings-store': settings, './spotify-auth': auth, './spotify-library': library, './spotify-player': player, './spotify-web-api': api,
}, globals);
return { types, api, auth, player, library, service, settings };
}
function signedIn(extra = {}) {
return fakeSettings({
'music.spotify.clientId': CLIENT_ID,
'music.spotify.account': JSON.stringify({ clientId: CLIENT_ID, accessToken: 'access-1', refreshToken: 'refresh-1',
expiresAtMs: 9e15, userId: 'jim', displayName: 'Jim', product: 'premium', ...extra }),
});
}
const tokens = { accessToken: async force => (force ? 'fresh' : 'stale') };
function track(id, extra = {}) {
return { type: 'track', uri: `spotify:track:${id}`, name: `Song ${id}`, duration_ms: 200_000,
artists: [{ name: 'Band' }, { name: 'Guest' }], album: { name: 'Record', images: [
{ url: 'https://i.scdn.co/640', width: 640, height: 640 },
{ url: 'https://i.scdn.co/300', width: 300, height: 300 },
{ url: 'https://i.scdn.co/64', width: 64, height: 64 }] }, ...extra };
}
function playbackBody(extra = {}) {
return { device: { id: 'phone', name: "Jim's iPhone", type: 'Smartphone', is_active: true, volume_percent: 70, supports_volume: false },
is_playing: true, progress_ms: 30_000, shuffle_state: false, repeat_state: 'off', context: { uri: 'spotify:playlist:p1' },
item: track('a'), currently_playing_type: 'track', actions: { disallows: { resuming: true } }, ...extra };
}
test('the Web API client parses playback, retries a 401 with a fresh token and names player errors', async () => {
const { api } = loadSpotify();
const f = fakeFetch({
'GET /me/player': [{ status: 401, body: '' }, { body: playbackBody({
item: { type: 'episode', uri: 'spotify:episode:e', name: 'Ep', duration_ms: 1000, images: [], show: { name: 'Show', images: [{ url: 'u', width: 300 }] } },
}) }],
'PUT /me/player/play': { status: 404, body: { error: { status: 404, message: 'Player command failed: No active device found', reason: 'NO_ACTIVE_DEVICE' } } },
'POST /me/player/next': { status: 403, body: { error: { status: 403, message: 'Premium required', reason: 'PREMIUM_REQUIRED' } } },
'PUT /me/player/pause': { status: 204 },
});
const client = new api.SpotifyClient(f.fetch, tokens);
const playback = await client.playback();
assert.deepEqual(f.calls.map(call => call.headers.Authorization), ['Bearer stale', 'Bearer fresh']);
assert.equal(f.calls[0].query.additional_types, 'episode');
assert.equal(playback.playing, true); assert.equal(playback.device.type, 'Smartphone'); assert.equal(playback.device.volumePercent, 70);
assert.deepEqual(plain(playback.disallows), ['resuming']); assert.equal(playback.contextUri, 'spotify:playlist:p1');
assert.equal(playback.item.type, 'episode'); assert.equal(playback.item.artists, 'Show'); assert.equal(playback.item.images[0].url, 'u');
await assert.rejects(client.play({ contextUri: 'spotify:album:x', offsetUri: 'spotify:track:y' }),
error => error instanceof api.SpotifyApiError && error.reason === 'NO_ACTIVE_DEVICE' && /Open Spotify/.test(error.message));
const play = f.calls.find(call => call.path === '/me/player/play');
assert.deepEqual(JSON.parse(play.body), { context_uri: 'spotify:album:x', offset: { uri: 'spotify:track:y' } });
assert.equal(play.headers['Content-Type'], 'application/json');
await assert.rejects(client.next(), /needs Spotify Premium/);
// Body-less commands still send an (empty) body, or Spotify answers 411.
await client.pause();
assert.equal(f.calls.at(-1).body, '');
assert.equal(f.calls.find(call => call.method === 'GET').body, undefined);
});
test('the Web API client follows next links and reads the renamed playlist item field', async () => {
const { api } = loadSpotify();
const f = fakeFetch({
'GET /playlists/p1/items': call => (call.query.offset === '2'
? { body: { items: [{ track: track('c') }], next: null } }
: { body: { items: [{ item: track('a') }, { item: null }, { item: track('b') }], next: 'https://api.spotify.com/v1/playlists/p1/items?offset=2' } }),
'GET /me/following': { body: { artists: { items: [{ id: 'ar', name: 'Artist', uri: 'spotify:artist:ar' }], next: null } } },
'GET /search': { body: { tracks: { items: [track('s')] }, playlists: { items: [null, { id: 'pl', name: 'Mix', owner: { id: 'o' } }] } } },
});
const client = new api.SpotifyClient(f.fetch, tokens);
assert.deepEqual(plain((await client.playlistItems('p1')).map(item => item.name)), ['Song a', 'Song b', 'Song c']);
assert.equal(f.calls[0].query.limit, '50');
assert.equal((await client.followedArtists())[0].name, 'Artist');
const results = await client.search('daft punk', ['track', 'playlist'], 50);
assert.equal(f.calls.at(-1).query.limit, '10'); // development-mode maximum
assert.equal(f.calls.at(-1).query.q, 'daft punk');
assert.equal(results.tracks[0].artists, 'Band, Guest');
assert.equal(results.playlists.length, 1); assert.equal(results.playlists[0].uri, 'spotify:playlist:pl');
});
test('sign-in builds a PKCE authorize URL, exchanges the code and stores the account', async () => {
const settings = fakeSettings({ 'music.spotify.clientId': CLIENT_ID });
const { auth } = loadSpotify({ settings });
const f = fakeFetch({
'POST /api/token': { body: { access_token: 'access-1', refresh_token: 'refresh-1', expires_in: 3600 } },
'GET /me': { body: { id: 'jim', display_name: 'Jim', product: 'premium' } },
});
let opened = null;
const webSignIn = {
createPkce: () => ({ verifier: 'verifier-1', challenge: 'challenge-1' }),
randomState: () => 'state-1',
open: async (url, port, redirectPath) => {
opened = { url, port, redirectPath };
return 'http://127.0.0.1:43823/spotify-callback?code=AQB%2Fcode&state=state-1';
},
cancel: () => {},
};
const account = await auth.signInToSpotify(f.fetch, webSignIn, () => 5_000);
const authorize = new URL(opened.url);
assert.equal(authorize.origin + authorize.pathname, 'https://accounts.spotify.com/authorize');
assert.equal(authorize.searchParams.get('client_id'), CLIENT_ID);
assert.equal(authorize.searchParams.get('redirect_uri'), 'http://127.0.0.1:43823/spotify-callback');
assert.equal(authorize.searchParams.get('code_challenge_method'), 'S256');
assert.equal(authorize.searchParams.get('code_challenge'), 'challenge-1');
assert.match(authorize.searchParams.get('scope'), /user-modify-playback-state .*user-library-modify/);
assert.deepEqual([opened.port, opened.redirectPath], [43823, '/spotify-callback']);
const exchange = Object.fromEntries(new URLSearchParams(f.calls[0].body));
assert.deepEqual(exchange, { grant_type: 'authorization_code', code: 'AQB/code', redirect_uri: 'http://127.0.0.1:43823/spotify-callback',
client_id: CLIENT_ID, code_verifier: 'verifier-1' });
assert.equal(f.calls[1].headers.Authorization, 'Bearer access-1');
assert.equal(account.displayName, 'Jim'); assert.equal(account.expiresAtMs, 5_000 + 3_600_000);
assert.equal(auth.readSpotifyAccount().refreshToken, 'refresh-1');
// Another client ID: the stored sign-in no longer counts.
settings.setStringSetting('music.spotify.clientId', 'f'.repeat(32));
assert.equal(auth.readSpotifyAccount(), null);
});
test('redirect parsing rejects mismatched state and reports a declined sign-in', () => {
const { auth } = loadSpotify();
assert.equal(auth.parseSpotifyRedirect('http://127.0.0.1:1/cb?state=s&code=a%20b', 's'), 'a b');
assert.throws(() => auth.parseSpotifyRedirect('http://127.0.0.1:1/cb?state=x&code=a', 's'), /didn't match/);
assert.throws(() => auth.parseSpotifyRedirect('http://127.0.0.1:1/cb?error=access_denied&state=s', 's'), /declined/);
assert.throws(() => auth.parseSpotifyRedirect('http://127.0.0.1:1/cb?state=s', 's'), /no authorization code/);
});
test('the session refreshes an expired token once for concurrent callers and signs out when revoked', async () => {
const settings = signedIn({ expiresAtMs: 0 });
const { auth } = loadSpotify({ settings });
let refreshes = 0;
const f = fakeFetch({ 'POST /api/token': () => {
refreshes++;
return refreshes === 1 ? { body: { access_token: 'access-2', refresh_token: 'refresh-2', expires_in: 3600 } }
: { status: 400, body: { error: 'invalid_grant', error_description: 'Refresh token revoked' } };
} });
const session = new auth.SpotifySession(f.fetch, () => 10_000);
assert.deepEqual(await Promise.all([session.accessToken(false), session.accessToken(false)]), ['access-2', 'access-2']);
assert.equal(refreshes, 1);
assert.equal(Object.fromEntries(new URLSearchParams(f.calls[0].body)).refresh_token, 'refresh-1');
// The rotated refresh token is kept; the new access token is used until it nears expiry.
assert.equal(auth.readSpotifyAccount().refreshToken, 'refresh-2');
assert.equal(await session.accessToken(false), 'access-2'); assert.equal(refreshes, 1);
await assert.rejects(session.accessToken(true), /Sign in again/);
assert.equal(auth.readSpotifyAccount(), null);
await assert.rejects(session.accessToken(false), /Not signed in/);
});
function playerFixture({ routes = {}, settings = signedIn() } = {}) {
const timers = fakeTimers();
const s = loadSpotify({ settings, timers });
const f = fakeFetch({ 'GET /me/player': { body: playbackBody() }, 'GET /me/library/contains': { body: [false] },
'GET /me/player/queue': { body: { currently_playing: track('a'), queue: [track('b'), track('c', { artists: [{ name: 'Other' }] })] } },
...routes });
const downloads = []; const decodes = []; const system = { volume: 40, set: [] };
const service = new s.service.SpotifyService({
fetch: f.fetch,
webSignIn: { createPkce: () => ({}), randomState: () => '', open: async () => '', cancel: () => {} },
download: async url => { downloads.push(url); return new Uint8Array([1, 2, 3]); },
decodeArt: (_bytes, size) => { decodes.push(size); return { size }; },
systemVolume: { get: () => system.volume, set: value => system.set.push(value) },
});
// The player reads time from Date.now by default; give it the fake clock.
const player = new s.player.SpotifyPlayer({ client: service.client, isSignedIn: () => s.auth.readSpotifyAccount() !== null,
download: async url => { downloads.push(url); return new Uint8Array([1, 2, 3]); },
decodeArt: (_bytes, size) => { decodes.push(size); return { size }; },
systemVolume: { get: () => system.volume, set: value => system.set.push(value) }, now: timers.now });
const flush = () => new Promise(resolve => setImmediate(resolve));
const settle = async () => { for (let i = 0; i < 6; i++) await flush(); };
return { ...s, f, timers, service, player, downloads, decodes, system, settle };
}
test('the player polls while listened to and maps playback, art and actions into the media state', async () => {
const p = playerFixture();
const states = [];
const unsubscribe = p.player.onStateChange(state => states.push(state));
assert.equal(states[0].available, false); assert.equal(states[0].accessEnabled, true);
p.timers.advance(0); await p.settle();
const state = p.player.snapshot();
assert.equal(state.available, true); assert.equal(state.playbackState, 'playing');
assert.equal(state.title, 'Song a'); assert.equal(state.artist, 'Band, Guest'); assert.equal(state.album, 'Record');
assert.equal(state.appName, 'Spotify'); assert.equal(state.packageName, 'com.spotify.client');
assert.equal(state.canPlayPause, true); assert.equal(state.canSkipNext, true);
assert.deepEqual(plain(state.customActions.map(action => action.name)), ['Shuffle: off', 'Repeat: off', 'Like song']);
// The 300px image (the smallest at least 128 wide), downloaded once and decoded per size.
assert.deepEqual(p.downloads, ['https://i.scdn.co/300']);
assert.deepEqual(plain(p.player.getAlbumArt(112)), { size: 112 }); p.player.getAlbumArt(112);
assert.deepEqual(p.decodes, [112]);
// Position advances between polls.
p.timers.advance(1_000);
assert.equal(p.player.snapshot().positionMs, 31_000);
// Polling continues on a playing interval, and stops with the last listener.
assert.ok(p.timers.delays().some(delay => delay > 0 && delay <= 5_000));
unsubscribe();
assert.deepEqual(p.timers.delays(), []);
});
test('pausing applies at once, resuming with no active device starts on an open phone, and failures become a notice', async () => {
let active = true;
const p = playerFixture({ routes: {
'PUT /me/player/pause': { status: 204 },
'PUT /me/player/play': call => (call.query.device_id || active ? { status: 204 }
: { status: 404, body: { error: { status: 404, reason: 'NO_ACTIVE_DEVICE', message: 'none' } } }),
'GET /me/player/devices': { body: { devices: [
{ id: 'mac', name: 'MacBook', type: 'Computer', is_active: false },
{ id: 'iphone', name: 'iPhone', type: 'Smartphone', is_active: false },
{ id: 'tv', name: 'TV', type: 'TV', is_restricted: true }] } },
'POST /me/player/next': { status: 403, body: { error: { status: 403, reason: 'PREMIUM_REQUIRED', message: 'x' } } },
} });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
await p.player.playPause();
assert.equal(p.player.snapshot().playbackState, 'paused');
assert.equal(p.f.calls.filter(call => call.path === '/me/player/pause').length, 1);
active = false;
await p.player.playPause();
const plays = p.f.calls.filter(call => call.path === '/me/player/play');
assert.deepEqual(plays.map(call => call.query.device_id ?? ''), ['', 'iphone']);
assert.deepEqual(JSON.parse(plays[1].body), {});
await p.player.skipNext();
assert.match(p.player.snapshot().notice, /Premium/);
// A settle poll re-reads soon after each command.
assert.ok(p.timers.delays().some(delay => delay <= 600));
p.timers.advance(8_100);
assert.equal(p.player.snapshot().notice, '');
});
test('the queue lists the current track first and jumping skips forward to the chosen item', async () => {
const p = playerFixture({ routes: { 'POST /me/player/next': { status: 204 } } });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
assert.deepEqual(plain(p.player.getQueue()), []); // first read in flight
await p.settle();
assert.deepEqual(plain(p.player.getQueue().map(item => [item.id, item.title, item.subtitle, item.active])),
[[0, 'Song a', 'Band, Guest', true], [1, 'Song b', 'Band, Guest', false], [2, 'Song c', 'Other', false]]);
await p.player.skipToQueueItem(2);
assert.equal(p.f.calls.filter(call => call.path === '/me/player/next').length, 2);
assert.equal(p.player.snapshot().title, 'Song c');
assert.deepEqual(plain(p.player.getQueue().map(item => item.title)), ['Song c']);
});
test('volume goes to a speaker through Spotify and to this phone through the system volume', async () => {
const p = playerFixture({ routes: {
'GET /me/player': { body: playbackBody({ device: { id: 'spk', name: 'Kitchen', type: 'Speaker', is_active: true, volume_percent: 30, supports_volume: true } }) },
'PUT /me/player/volume': { status: 204 },
} });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
assert.equal(p.player.snapshot().appName, 'Spotify · Kitchen');
assert.equal(p.player.getMediaVolumePercent(), 30);
p.player.setMediaVolumePercent(32); p.player.setMediaVolumePercent(34);
assert.equal(p.player.getMediaVolumePercent(), 34);
await p.settle();
const volumes = p.f.calls.filter(call => call.path === '/me/player/volume');
// One request in flight; the latest value follows it.
assert.deepEqual(volumes.map(call => [call.query.volume_percent, call.query.device_id]), [['32', 'spk'], ['34', 'spk']]);
const phone = playerFixture();
phone.player.onStateChange(() => {});
phone.timers.advance(0); await phone.settle();
assert.equal(phone.player.getMediaVolumePercent(), 40);
phone.player.setMediaVolumePercent(55);
assert.deepEqual(phone.system.set, [55]);
assert.equal(phone.f.calls.some(call => call.path === '/me/player/volume'), false);
});
test('shuffle, repeat and like run their Web API calls and relabel at once', async () => {
const p = playerFixture({ routes: {
'PUT /me/player/shuffle': { status: 204 }, 'PUT /me/player/repeat': { status: 204 }, 'PUT /me/library': { status: 200 },
} });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
await p.player.sendCustomAction('shuffle');
await p.player.sendCustomAction('repeat');
await p.player.sendCustomAction('like');
assert.deepEqual(plain(p.player.snapshot().customActions.map(action => action.name)), ['Shuffle: on', 'Repeat: all', 'Unlike song']);
const calls = p.f.calls.filter(call => call.method === 'PUT');
assert.deepEqual(calls.map(call => [call.path, call.query.state ?? call.query.uris]),
[['/me/player/shuffle', 'true'], ['/me/player/repeat', 'context'], ['/me/library', 'spotify:track:a']]);
});
test('signed out, the player reports no access and never calls the API', async () => {
const p = playerFixture({ settings: fakeSettings({ 'music.spotify.clientId': CLIENT_ID }) });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
assert.equal(p.player.snapshot().accessEnabled, false);
assert.equal(p.f.calls.length, 0);
});
test('the library tree lists owned playlists as browsable and plays tracks within their context', async () => {
const s = loadSpotify({ settings: signedIn() });
const f = fakeFetch({
'GET /me/playlists': { body: { items: [
{ id: 'mine', name: 'Mine', owner: { id: 'jim' } },
{ id: 'theirs', name: 'Theirs', owner: { id: 'spotify', display_name: 'Spotify' } },
{ id: 'collab', name: 'Collab', collaborative: true, owner: { id: 'friend' } }], next: null } },
'GET /me/player/recently-played': { body: { items: [
{ track: track('a'), context: { uri: 'spotify:album:al' } }, { track: track('a'), context: null }, { track: track('b'), context: null }] } },
});
const plays = [];
const library = new s.library.SpotifyLibrary({ client: new s.api.SpotifyClient(f.fetch, tokens),
player: { startPlayback: async request => { plays.push(request); return true; } },
isSignedIn: () => true, userId: () => 'jim' });
const [app] = library.listBrowsableApps();
assert.equal(await library.connect(app), 'root');
assert.deepEqual(plain((await library.browse('root')).map(row => row.title)), ['Recently played', 'Playlists', 'Liked Songs', 'Albums', 'Artists']);
const playlists = await library.browse('playlists');
assert.deepEqual(plain(playlists.map(row => [row.title, row.browsable, row.subtitle])),
[['Mine', true, ''], ['Theirs', false, 'Spotify'], ['Collab', true, '']]);
const recent = await library.browse('recent');
assert.deepEqual(plain(recent.map(row => row.mediaId)), ['track:spotify:track:a|spotify:album:al', 'track:spotify:track:b']);
library.playFromMediaId(recent[0].mediaId);
library.playFromMediaId(recent[1].mediaId);
library.playFromMediaId('track:spotify:track:z|liked');
library.playFromMediaId('liked');
library.playFromMediaId('playlist:mine');
assert.deepEqual(plain(plays), [
{ contextUri: 'spotify:album:al', offsetUri: 'spotify:track:a' },
{ uris: ['spotify:track:b'] },
{ contextUri: 'spotify:user:jim:collection', offsetUri: 'spotify:track:z' },
{ contextUri: 'spotify:user:jim:collection' },
{ contextUri: 'spotify:playlist:mine' },
]);
library.disconnect();
await assert.rejects(library.browse('root'), /Not connected/);
});
test('search-and-play starts the best match and explains failures', async () => {
const p = playerFixture({ routes: {
'GET /search': call => ({ body: call.query.type === 'album'
? { albums: { items: [{ id: 'al', name: 'Discovery', uri: 'spotify:album:al', artists: [{ name: 'Daft Punk' }] }] } }
: { tracks: { items: [] } } }),
'PUT /me/player/play': { status: 204 },
} });
assert.equal(await p.service.playSearch('discovery', 'album'), 'the album Discovery by Daft Punk');
const play = p.f.calls.find(call => call.path === '/me/player/play');
assert.deepEqual(JSON.parse(play.body), { context_uri: 'spotify:album:al' });
await assert.rejects(p.service.playSearch('nothing'), /found no track/);
});
test('the service resets the player when the signed-in account changes, not when tokens refresh', async () => {
const p = playerFixture();
const reset = [];
p.service.player.accountChanged = () => reset.push('reset');
const changes = [];
p.service.onChange(() => changes.push('change'));
const account = JSON.parse(p.settings.values.get('music.spotify.account'));
p.settings.setStringSetting('music.spotify.account', JSON.stringify({ ...account, accessToken: 'access-9' }));
assert.deepEqual(reset, []);
p.service.signOut();
assert.deepEqual(reset, ['reset']);
assert.equal(p.service.account(), null);
assert.ok(changes.length >= 2);
});
function composite(appleState, spotifyState, spotifyLastPlayingAtMs = 0) {
const types = load('app/native/media-types.ts');
const calls = [];
const fakeBridge = (name, state) => ({
accessLabel: `${name} label`, accessAction: `${name} action`, skipRebuildsQueue: name === 'spotify', state, lastPlayingAtMs: 0,
onStateChange(listener) { listener(this.state); return () => {}; },
snapshot() { return this.state; }, start: async () => calls.push(`${name}:start`), stop: async () => {},
playPause: async () => calls.push(`${name}:playPause`), skipNext: async () => calls.push(`${name}:next`),
getAlbumArt: () => name, getQueue: () => [], refresh: async () => calls.push(`${name}:refresh`),
hasAccess: () => name === 'apple', requestAccess: async () => calls.push(`${name}:access`),
});
const apple = fakeBridge('apple', appleState);
const spotify = fakeBridge('spotify', spotifyState);
spotify.lastPlayingAtMs = spotifyLastPlayingAtMs;
const api = load('app/native/media-controller.ios.ts', {
'@nativescript/core': { Dialogs: {}, Utils: {} }, './image-files': {}, './kotlin-data': {}, './media-types': types,
'./spotify.ios': { spotifyService: () => null },
});
const bridge = new api.IosMediaControllerBridge(apple, () => (spotifyState ? spotify : null), () => 50);
return { api, bridge, calls, apple, spotify, types };
}
test('the iOS bridge follows whichever player is playing and routes commands to it', async () => {
const types = load('app/native/media-types.ts');
const base = { ...types.DEFAULT_MEDIA_STATE, accessEnabled: true };
const playing = { ...base, available: true, playbackState: 'playing', title: 'Playing' };
const paused = { ...base, available: true, playbackState: 'paused', title: 'Paused' };
const { chooseMediaSource } = composite(base, null).api;
assert.equal(chooseMediaSource('apple-music', base, null, 0, 0), 'apple-music');
assert.equal(chooseMediaSource('apple-music', { ...base, accessEnabled: false }, base, 0, 0), 'spotify');
assert.equal(chooseMediaSource('apple-music', paused, playing, 0, 0), 'spotify');
assert.equal(chooseMediaSource('spotify', playing, paused, 0, 0), 'apple-music');
assert.equal(chooseMediaSource('spotify', playing, playing, 0, 0), 'spotify'); // no flapping
assert.equal(chooseMediaSource('spotify', paused, paused, 20, 10), 'apple-music'); // played last
assert.equal(chooseMediaSource('apple-music', paused, paused, 0, 0), 'spotify');
assert.equal(chooseMediaSource('apple-music', paused, base, 0, 0), 'apple-music');
const c = composite(paused, { ...playing, notice: '' });
assert.equal(c.bridge.snapshot().title, 'Playing');
assert.equal(c.bridge.skipRebuildsQueue, true);
await c.bridge.playPause(); await c.bridge.refresh();
assert.deepEqual(c.calls, ['spotify:playPause', 'spotify:refresh']);
assert.equal(c.bridge.getAlbumArt(10), 'spotify');
const firstArt = c.bridge.snapshot().artVersion;
// Switching players changes the art version even when the sources' versions match.
c.spotify.state = { ...paused, notice: 'Spotify needs Premium' };
c.apple.state = { ...playing, title: 'Apple' };
const state = c.bridge.snapshot();
assert.equal(state.title, 'Apple'); assert.notEqual(state.artVersion, firstArt);
// A Spotify failure still shows while Apple Music is the player shown.
assert.equal(state.notice, 'Spotify needs Premium');
assert.equal(c.bridge.hasAccess(), true); // Apple Music's permission
await c.bridge.requestAccess();
assert.equal(c.calls.at(-1), 'apple:access');
const idle = composite(base, base);
assert.equal(idle.bridge.snapshot().status, 'Nothing is playing.');
});
test('the assistant offers Spotify search-and-play once signed in and refreshes before reading media state', async () => {
let account = null; const searches = []; const refreshes = [];
const spotify = {
account: () => account,
playSearch: async (query, kind) => { searches.push([query, kind]); if (query === 'fail') throw new Error('No device'); return 'Song by Band'; },
};
const registered = new Map(); const systemTools = new Map();
const { registerSystemTools } = load('app/assistant/system-tools.ts', {
'../native/calendar': {}, '../native/calendar-permissions': {},
'../native/media-controller': { mediaControllerBridge: { start: async () => {}, refresh: async () => refreshes.push('refresh'),
snapshot: () => ({ accessEnabled: true, playbackState: 'playing', title: 'Song', artist: 'Band', appName: 'Spotify' }) } },
'../native/spotify': { spotifyService: () => spotify },
'../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {},
}, { global: { isIOS: true } });
registerSystemTools({
registerSystemTool: (definition, handler) => systemTools.set(definition.name, handler),
register: registration => registered.set(registration.spec.name, registration),
});
assert.match((await systemTools.get('media.now_playing')()).content, /Playing: Song by Band in Spotify/);
assert.deepEqual(refreshes, ['refresh']);
const tool = registered.get('media.spotify_play');
assert.equal(tool.spec.availability, 'always');
assert.equal(tool.isAvailable(), false);
account = { userId: 'jim' };
assert.equal(tool.isAvailable(), true);
assert.equal((await tool.handler({ query: ' discovery ', kind: 'album' })).content, 'Playing Song by Band on Spotify.');
await tool.handler({ query: 'x', kind: 'podcast' });
assert.deepEqual(plain(searches), [['discovery', 'album'], ['x', 'track']]);
assert.equal((await tool.handler({ query: 'fail' })).error, 'No device');
assert.equal((await tool.handler({ query: ' ' })).ok, false);
});
+69
View File
@@ -0,0 +1,69 @@
#import <Foundation/Foundation.h>
#import "FaceclawLoopbackRedirect.h"
/** GET a URL, pumping the main run loop (where the receiver delivers redirects) until it answers. */
static NSInteger get(NSString *url, NSString **body) {
__block NSInteger status = -1;
__block NSString *text = nil;
__block BOOL done = NO;
[[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:url]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
status = error ? -1 : ((NSHTTPURLResponse *)response).statusCode;
text = data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : nil;
done = YES;
}] resume];
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:10];
while (!done && deadline.timeIntervalSinceNow > 0) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
if (body) *body = text;
return status;
}
static void pumpBriefly(void) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
}
int main(int argc, const char **argv) {
@autoreleasepool {
NSInteger port = argc > 1 ? atoi(argv[1]) : 43899;
NSString *base = [NSString stringWithFormat:@"http://127.0.0.1:%ld", (long)port];
FaceclawLoopbackRedirect *receiver = [FaceclawLoopbackRedirect new];
NSMutableArray<NSString *> *redirects = [NSMutableArray array];
receiver.redirectHandler = ^(NSString *url) { [redirects addObject:url]; };
NSString *error = [receiver startOnPort:port path:@"/spotify-callback"];
if (error) { NSLog(@"FAIL start: %@", error); return 1; }
// A second listener on the same port reports the conflict instead of failing silently.
FaceclawLoopbackRedirect *second = [FaceclawLoopbackRedirect new];
NSString *conflict = [second startOnPort:port path:@"/spotify-callback"];
if (!conflict || [conflict rangeOfString:@"port"].location == NSNotFound) { NSLog(@"FAIL conflict: %@", conflict); return 1; }
// Other paths (a browser's favicon request) are refused and not reported.
NSString *body = nil;
if (get([base stringByAppendingString:@"/favicon.ico"], &body) != 404) { NSLog(@"FAIL favicon"); return 1; }
pumpBriefly();
if (redirects.count) { NSLog(@"FAIL favicon reported %@", redirects); return 1; }
// The redirect itself: a page for the browser, the full URL (query intact) for the app.
NSString *redirect = [base stringByAppendingString:@"/spotify-callback?code=AQB%2Fx-y_z&state=s1"];
if (get(redirect, &body) != 200 || [body rangeOfString:@"return to Faceclaw"].location == NSNotFound) {
NSLog(@"FAIL redirect page: %@", body); return 1;
}
pumpBriefly();
if (redirects.count != 1 || ![redirects[0] isEqualToString:redirect]) { NSLog(@"FAIL redirect url %@", redirects); return 1; }
// A query-less or prefixed path does not count as the redirect.
if (get([base stringByAppendingString:@"/spotify-callback-extra?code=x"], NULL) != 404) { NSLog(@"FAIL prefix"); return 1; }
// Once stopped, the port is free again.
[receiver stop];
pumpBriefly();
if (get(redirect, NULL) != -1) { NSLog(@"FAIL still listening after stop"); return 1; }
NSString *again = [second startOnPort:port path:@"/spotify-callback"];
if (again) { NSLog(@"FAIL restart: %@", again); return 1; }
[second stop];
NSLog(@"PASS loopback redirect");
return 0;
}
}