diff --git a/App_Resources/iOS/Info.plist b/App_Resources/iOS/Info.plist
index d5bb7e78..61957b6e 100644
--- a/App_Resources/iOS/Info.plist
+++ b/App_Resources/iOS/Info.plist
@@ -30,6 +30,8 @@
Faceclaw reads your upcoming calendar events to display them in Calendar and Glanceboard on your glasses.
NSCalendarsFullAccessUsageDescription
Faceclaw reads your upcoming calendar events to display them in Calendar and Glanceboard on your glasses.
+ NSAppleMusicUsageDescription
+ Faceclaw shows what is playing in Apple Music on your glasses, controls playback, and lets you browse and play your music library from the glasses.
NSLocationWhenInUseUsageDescription
Faceclaw uses your location for local weather, turn-by-turn navigation, and true north on the glasses compass.
NSLocationTemporaryUsageDescriptionDictionary
diff --git a/App_Resources/iOS/build.xcconfig b/App_Resources/iOS/build.xcconfig
index 356e6b89..03805280 100644
--- a/App_Resources/iOS/build.xcconfig
+++ b/App_Resources/iOS/build.xcconfig
@@ -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
+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
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
diff --git a/App_Resources/iOS/src/FaceclawAppleMusic.h b/App_Resources/iOS/src/FaceclawAppleMusic.h
new file mode 100644
index 00000000..50ca5c4d
--- /dev/null
+++ b/App_Resources/iOS/src/FaceclawAppleMusic.h
@@ -0,0 +1,41 @@
+#import
+
+/**
+ * Apple Music (the system Music app) through the MediaPlayer framework: now-playing state
+ * and transport on the system player, the media library as a browse tree, and the system
+ * output volume. Call from the main thread; callbacks are delivered on the main queue.
+ * Nothing touches the player or library until media-library access is granted, so no
+ * permission prompt appears unasked.
+ */
+@interface FaceclawAppleMusic : NSObject
++ (instancetype)shared;
+/** Playback state, the now-playing item or authorization may have changed. */
+@property(nonatomic, copy) void (^changeHandler)(void);
+- (BOOL)hasPermission;
+- (NSString *)permissionStatus;
+- (void)requestPermission:(void (^)(BOOL granted))completion;
+/** Observe the system player (and keep observing across authorization changes). */
+- (void)start;
+- (void)stop;
+/** The system player as a JSON object; {"access": false} until access is granted. */
+- (NSString *)stateJson;
+- (void)playPause;
+- (void)skipNext;
+- (void)skipPrevious;
+/**
+ * The queue Faceclaw last started, as [{"id", "title", "active"}], while the player is
+ * still playing from it; "[]" otherwise (the system player does not expose queues that
+ * the Music app started).
+ */
+- (NSString *)queueJson;
+- (void)skipToQueueIndex:(NSInteger)index;
+/** [widthLo, widthHi, heightLo, heightHi, BGRA...] fitted within maxSize (never upscaled), or nil. */
+- (NSData *)albumArt:(NSInteger)maxSize;
+/** System output volume 0..100, or -1 when unknown. */
+- (double)volumePercent;
+- (void)setVolumePercent:(double)percent;
+/** One level of the library tree ("root" first) as [{"mediaId", "title", "subtitle", "browsable", "playable"}]. */
+- (void)browse:(NSString *)parentId completion:(void (^)(NSString *json, NSString *error))completion;
+/** Start playback of a browsed collection, or of a song within its collection. */
+- (void)play:(NSString *)mediaId completion:(void (^)(NSString *error))completion;
+@end
diff --git a/App_Resources/iOS/src/FaceclawAppleMusic.m b/App_Resources/iOS/src/FaceclawAppleMusic.m
new file mode 100644
index 00000000..d6b8cef0
--- /dev/null
+++ b/App_Resources/iOS/src/FaceclawAppleMusic.m
@@ -0,0 +1,512 @@
+#import "FaceclawAppleMusic.h"
+#import
+#import
+#import
+#import
+#import
+#import
+
+/** How long a queue Faceclaw just set may take to become the player's now-playing context. */
+static const NSTimeInterval kQueueConfirmSeconds = 5.0;
+
+static NSString *JsonString(id object, NSString *fallback) {
+ NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:nil];
+ return data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : fallback;
+}
+
+/** Persistent IDs are unsigned 64-bit, beyond JavaScript's exact integers, so they cross as decimal strings. */
+static NSString *IdString(MPMediaEntityPersistentID persistentId) {
+ return [NSString stringWithFormat:@"%llu", persistentId];
+}
+
+static BOOL ParseId(NSString *text, MPMediaEntityPersistentID *out) {
+ const char *chars = text.UTF8String;
+ if (!chars || chars[0] < '0' || chars[0] > '9') return NO;
+ char *end = NULL;
+ errno = 0;
+ unsigned long long value = strtoull(chars, &end, 10);
+ if (errno || !end || *end) return NO;
+ *out = value;
+ return YES;
+}
+
+static NSDictionary *BrowseRow(NSString *mediaId, NSString *title, NSString *subtitle, BOOL browsable, BOOL playable) {
+ return @{@"mediaId": mediaId, @"title": title ?: @"", @"subtitle": subtitle ?: @"",
+ @"browsable": @(browsable), @"playable": @(playable)};
+}
+
+@interface FaceclawAppleMusic ()
+@property(nonatomic, strong) dispatch_queue_t libraryQueue;
+@property(nonatomic) BOOL started;
+@property(nonatomic) BOOL observing;
+/** Bumped whenever the now-playing item changes, so cached album art is refetched. */
+@property(nonatomic) NSInteger artVersion;
+@property(nonatomic, copy) NSString *nowPlayingKey;
+/** The items of the queue Faceclaw last started, until the player leaves them. */
+@property(nonatomic, copy) NSArray *queueItems;
+@property(nonatomic) BOOL queueConfirmed;
+@property(nonatomic, strong) NSDate *queueSetAt;
+@property(nonatomic, strong) MPVolumeView *volumeView;
+@end
+
+@implementation FaceclawAppleMusic
++ (instancetype)shared {
+ static FaceclawAppleMusic *instance;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ instance = [FaceclawAppleMusic new]; });
+ return instance;
+}
+
+- (instancetype)init {
+ if ((self = [super init])) {
+ _libraryQueue = dispatch_queue_create("com.faceclaw.applemusic", DISPATCH_QUEUE_SERIAL);
+ _nowPlayingKey = @"";
+ // Access may have been granted or revoked in Settings while away.
+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(appBecameActive:)
+ name:UIApplicationDidBecomeActiveNotification object:nil];
+ }
+ return self;
+}
+
+#pragma mark - Authorization
+
+- (BOOL)hasPermission {
+ return MPMediaLibrary.authorizationStatus == MPMediaLibraryAuthorizationStatusAuthorized;
+}
+
+- (NSString *)permissionStatus {
+ switch (MPMediaLibrary.authorizationStatus) {
+ case MPMediaLibraryAuthorizationStatusAuthorized: return @"granted";
+ case MPMediaLibraryAuthorizationStatusDenied: return @"denied";
+ case MPMediaLibraryAuthorizationStatusRestricted: return @"restricted";
+ default: return @"not-determined";
+ }
+}
+
+- (void)requestPermission:(void (^)(BOOL))completion {
+ if ([self hasPermission]) {
+ completion(YES);
+ return;
+ }
+ [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [self syncObserving];
+ [self notifyChanged];
+ completion(status == MPMediaLibraryAuthorizationStatusAuthorized);
+ });
+ }];
+}
+
+- (void)appBecameActive:(NSNotification *)notification {
+ [self syncObserving];
+ [self notifyChanged];
+}
+
+#pragma mark - Observation
+
+- (void)start {
+ self.started = YES;
+ [self syncObserving];
+}
+
+- (void)stop {
+ self.started = NO;
+ [self syncObserving];
+}
+
+- (void)syncObserving {
+ BOOL want = self.started && [self hasPermission];
+ if (want == self.observing) return;
+ self.observing = want;
+ MPMusicPlayerController *player = MPMusicPlayerController.systemMusicPlayer;
+ NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
+ if (want) {
+ [center addObserver:self selector:@selector(playbackStateChanged:)
+ name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:player];
+ [center addObserver:self selector:@selector(nowPlayingItemChanged:)
+ name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:player];
+ [player beginGeneratingPlaybackNotifications];
+ } else {
+ [center removeObserver:self name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:player];
+ [center removeObserver:self name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:player];
+ [player endGeneratingPlaybackNotifications];
+ }
+}
+
+- (void)playbackStateChanged:(NSNotification *)notification {
+ [self notifyChanged];
+}
+
+- (void)nowPlayingItemChanged:(NSNotification *)notification {
+ // Streamed catalog items can share identity fields, so every change counts.
+ self.artVersion++;
+ [self notifyChanged];
+}
+
+- (void)notifyChanged {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (self.changeHandler) self.changeHandler();
+ });
+}
+
+#pragma mark - Now playing
+
+- (NSString *)stateJson {
+ if (![self hasPermission]) return @"{\"access\":false}";
+ MPMusicPlayerController *player = MPMusicPlayerController.systemMusicPlayer;
+ MPMediaItem *item = player.nowPlayingItem;
+ NSString *key = item ? [NSString stringWithFormat:@"%llu|%@|%@|%@", item.persistentID,
+ item.playbackStoreID ?: @"", item.title ?: @"", item.artist ?: @""] : @"";
+ if (![key isEqualToString:self.nowPlayingKey]) {
+ self.nowPlayingKey = key;
+ self.artVersion++;
+ }
+ NSString *state;
+ switch (player.playbackState) {
+ case MPMusicPlaybackStatePlaying:
+ case MPMusicPlaybackStateSeekingForward:
+ case MPMusicPlaybackStateSeekingBackward:
+ state = @"playing";
+ break;
+ case MPMusicPlaybackStatePaused:
+ case MPMusicPlaybackStateInterrupted:
+ state = @"paused";
+ break;
+ default:
+ state = @"stopped";
+ break;
+ }
+ NSTimeInterval position = player.currentPlaybackTime;
+ NSTimeInterval duration = item.playbackDuration;
+ float rate = player.currentPlaybackRate;
+ return JsonString(@{
+ @"access": @YES,
+ @"hasItem": @(item != nil),
+ @"state": state,
+ @"title": item.title ?: @"",
+ @"artist": item.artist ?: @"",
+ @"album": item.albumTitle ?: @"",
+ @"positionMs": @(item && isfinite(position) && position >= 0 ? position * 1000.0 : -1),
+ @"durationMs": @(item && isfinite(duration) && duration > 0 ? duration * 1000.0 : -1),
+ @"rate": @(isfinite(rate) ? rate : 0),
+ @"artVersion": @(self.artVersion),
+ }, @"{\"access\":true}");
+}
+
+- (void)playPause {
+ if (![self hasPermission]) return;
+ MPMusicPlayerController *player = MPMusicPlayerController.systemMusicPlayer;
+ switch (player.playbackState) {
+ case MPMusicPlaybackStatePlaying:
+ case MPMusicPlaybackStateSeekingForward:
+ case MPMusicPlaybackStateSeekingBackward:
+ [player pause];
+ break;
+ default:
+ [player play];
+ break;
+ }
+}
+
+- (void)skipNext {
+ if ([self hasPermission]) [MPMusicPlayerController.systemMusicPlayer skipToNextItem];
+}
+
+- (void)skipPrevious {
+ if ([self hasPermission]) [MPMusicPlayerController.systemMusicPlayer skipToPreviousItem];
+}
+
+- (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;
+}
+
+#pragma mark - Queue
+
+/** Index of the now-playing item in Faceclaw's queue; forgets the queue once the player has left it. */
+- (NSInteger)activeQueueIndex {
+ NSArray *items = self.queueItems;
+ if (!items.count || ![self hasPermission]) return -1;
+ MPMusicPlayerController *player = MPMusicPlayerController.systemMusicPlayer;
+ MPMediaItem *now = player.nowPlayingItem;
+ NSInteger found = -1;
+ if (now) {
+ NSUInteger index = player.indexOfNowPlayingItem;
+ if (index < items.count && items[index].persistentID == now.persistentID) {
+ found = (NSInteger)index;
+ } else {
+ for (NSUInteger i = 0; i < items.count; i++) {
+ if (items[i].persistentID == now.persistentID) { found = (NSInteger)i; break; }
+ }
+ }
+ }
+ if (found >= 0) {
+ self.queueConfirmed = YES;
+ return found;
+ }
+ // Either the player hasn't switched over yet, or it has moved on to another queue.
+ if (self.queueConfirmed || -[self.queueSetAt timeIntervalSinceNow] > kQueueConfirmSeconds) {
+ self.queueItems = nil;
+ }
+ return -1;
+}
+
+- (NSString *)queueJson {
+ NSInteger active = [self activeQueueIndex];
+ if (active < 0) return @"[]";
+ NSMutableArray *rows = [NSMutableArray arrayWithCapacity:self.queueItems.count];
+ [self.queueItems enumerateObjectsUsingBlock:^(MPMediaItem *item, NSUInteger index, BOOL *stop) {
+ [rows addObject:@{@"id": @(index), @"title": item.title ?: @"", @"active": @((NSInteger)index == active)}];
+ }];
+ return JsonString(rows, @"[]");
+}
+
+- (void)skipToQueueIndex:(NSInteger)index {
+ if ([self activeQueueIndex] < 0 || index < 0 || index >= (NSInteger)self.queueItems.count) return;
+ MPMusicPlayerController *player = MPMusicPlayerController.systemMusicPlayer;
+ player.nowPlayingItem = self.queueItems[index];
+ [player play];
+}
+
+#pragma mark - Volume
+
+- (UIWindow *)hostWindow {
+ for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
+ if (![scene isKindOfClass:UIWindowScene.class]) continue;
+ NSArray *windows = ((UIWindowScene *)scene).windows;
+ for (UIWindow *window in windows) if (window.isKeyWindow) return window;
+ if (windows.firstObject) return windows.firstObject;
+ }
+ return nil;
+}
+
+/**
+ * The slider of an off-screen MPVolumeView: iOS has no public setter for the system volume,
+ * but the volume view's slider drives it while the view is in a window.
+ */
+- (UISlider *)volumeSlider:(BOOL *)attached {
+ if (!self.volumeView) {
+ self.volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-2000, -2000, 160, 40)];
+ }
+ if (!self.volumeView.window) {
+ UIWindow *window = [self hostWindow];
+ if (!window) return nil;
+ [window addSubview:self.volumeView];
+ if (attached) *attached = YES;
+ }
+ for (UIView *view in self.volumeView.subviews) {
+ if ([view isKindOfClass:UISlider.class]) return (UISlider *)view;
+ }
+ return nil;
+}
+
+- (double)volumePercent {
+ // The slider tracks the system volume once attached; until then (nothing has been set
+ // yet) the session's reading, which can lag while Faceclaw's audio session is inactive.
+ UISlider *slider = self.volumeView.window ? [self volumeSlider:NULL] : nil;
+ float volume = slider ? slider.value : AVAudioSession.sharedInstance.outputVolume;
+ return isfinite(volume) ? MAX(0.0, MIN(100.0, round(volume * 100.0))) : -1;
+}
+
+- (void)setVolumePercent:(double)percent {
+ if (!isfinite(percent)) return;
+ BOOL attached = NO;
+ UISlider *slider = [self volumeSlider:&attached];
+ if (!slider) return;
+ float value = (float)MAX(0.0, MIN(1.0, percent / 100.0));
+ void (^apply)(void) = ^{
+ [slider setValue:value animated:NO];
+ [slider sendActionsForControlEvents:UIControlEventValueChanged];
+ };
+ // A freshly attached volume view ignores changes until it has wired up its slider.
+ if (attached) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), apply);
+ else apply();
+}
+
+#pragma mark - Library
+
+/**
+ * Songs of a playable collection id: "playlist:", "album:", "artist:" (album by
+ * album, in track order) or "songs" (the whole library). Runs on the library queue.
+ */
+- (NSArray *)itemsForCollection:(NSString *)collectionId {
+ if ([collectionId isEqualToString:@"songs"]) return [MPMediaQuery songsQuery].items ?: @[];
+ NSRange colon = [collectionId rangeOfString:@":"];
+ MPMediaEntityPersistentID persistentId;
+ if (colon.location == NSNotFound || !ParseId([collectionId substringFromIndex:colon.location + 1], &persistentId)) return @[];
+ NSString *kind = [collectionId substringToIndex:colon.location];
+ MPMediaQuery *query;
+ NSString *property;
+ if ([kind isEqualToString:@"playlist"]) {
+ query = [MPMediaQuery playlistsQuery];
+ property = MPMediaPlaylistPropertyPersistentID;
+ } else if ([kind isEqualToString:@"album"]) {
+ query = [MPMediaQuery albumsQuery];
+ property = MPMediaItemPropertyAlbumPersistentID;
+ } else if ([kind isEqualToString:@"artist"]) {
+ query = [MPMediaQuery albumsQuery];
+ property = MPMediaItemPropertyArtistPersistentID;
+ } else {
+ return @[];
+ }
+ [query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@(persistentId) forProperty:property]];
+ NSMutableArray *items = [NSMutableArray new];
+ for (MPMediaItemCollection *collection in query.collections) [items addObjectsFromArray:collection.items];
+ return items;
+}
+
+- (NSArray *)songRows:(NSArray *)items context:(NSString *)collectionId {
+ NSMutableArray *rows = [NSMutableArray arrayWithCapacity:items.count];
+ for (MPMediaItem *item in items) {
+ [rows addObject:BrowseRow([NSString stringWithFormat:@"song:%@@%@", IdString(item.persistentID), collectionId],
+ item.title, item.artist, NO, YES)];
+ }
+ return rows;
+}
+
+- (NSArray *)albumRows:(MPMediaQuery *)query {
+ NSMutableArray *rows = [NSMutableArray new];
+ for (MPMediaItemCollection *album in query.collections) {
+ MPMediaItem *item = album.representativeItem;
+ if (!item) continue;
+ [rows addObject:BrowseRow([@"album:" stringByAppendingString:IdString(item.albumPersistentID)],
+ item.albumTitle, item.albumArtist.length ? item.albumArtist : item.artist, YES, YES)];
+ }
+ return rows;
+}
+
+- (NSArray *)childrenOf:(NSString *)parentId {
+ if ([parentId isEqualToString:@"root"]) {
+ return @[BrowseRow(@"playlists", @"Playlists", nil, YES, NO), BrowseRow(@"artists", @"Artists", nil, YES, NO),
+ BrowseRow(@"albums", @"Albums", nil, YES, NO), BrowseRow(@"songs", @"Songs", nil, YES, NO)];
+ }
+ if ([parentId isEqualToString:@"playlists"]) {
+ NSMutableArray *rows = [NSMutableArray new];
+ for (MPMediaPlaylist *playlist in [MPMediaQuery playlistsQuery].collections) {
+ NSUInteger count = playlist.count;
+ NSString *subtitle = count == 1 ? @"1 song" : [NSString stringWithFormat:@"%lu songs", (unsigned long)count];
+ [rows addObject:BrowseRow([@"playlist:" stringByAppendingString:IdString(playlist.persistentID)],
+ playlist.name, subtitle, YES, count > 0)];
+ }
+ return rows;
+ }
+ if ([parentId isEqualToString:@"artists"]) {
+ NSMutableArray *rows = [NSMutableArray new];
+ for (MPMediaItemCollection *artist in [MPMediaQuery artistsQuery].collections) {
+ MPMediaItem *item = artist.representativeItem;
+ if (!item) continue;
+ [rows addObject:BrowseRow([@"artist:" stringByAppendingString:IdString(item.artistPersistentID)],
+ item.artist.length ? item.artist : @"Unknown Artist", nil, YES, YES)];
+ }
+ return rows;
+ }
+ if ([parentId isEqualToString:@"albums"]) return [self albumRows:[MPMediaQuery albumsQuery]];
+ if ([parentId isEqualToString:@"songs"]) return [self songRows:[MPMediaQuery songsQuery].items ?: @[] context:@"songs"];
+ if ([parentId hasPrefix:@"artist:"]) {
+ MPMediaEntityPersistentID artistId;
+ if (!ParseId([parentId substringFromIndex:7], &artistId)) return nil;
+ MPMediaQuery *query = [MPMediaQuery albumsQuery];
+ [query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@(artistId)
+ forProperty:MPMediaItemPropertyArtistPersistentID]];
+ return [self albumRows:query];
+ }
+ if ([parentId hasPrefix:@"playlist:"] || [parentId hasPrefix:@"album:"]) {
+ return [self songRows:[self itemsForCollection:parentId] context:parentId];
+ }
+ return nil;
+}
+
+- (void)browse:(NSString *)parentId completion:(void (^)(NSString *, NSString *))completion {
+ // Library queries can take a while on large libraries; keep them off the main thread.
+ dispatch_async(self.libraryQueue, ^{
+ NSString *json = nil, *error = nil;
+ if (![self hasPermission]) {
+ error = @"Apple Music access is not allowed";
+ } else {
+ @try {
+ NSArray *rows = [self childrenOf:parentId ?: @""];
+ if (rows) json = JsonString(rows, nil);
+ if (!json) error = rows ? @"Library listing failed" : @"Unknown library item";
+ } @catch (NSException *exception) {
+ error = exception.reason ?: @"Library unavailable";
+ }
+ }
+ dispatch_async(dispatch_get_main_queue(), ^{ completion(json, error); });
+ });
+}
+
+- (void)play:(NSString *)mediaId completion:(void (^)(NSString *))completion {
+ dispatch_async(self.libraryQueue, ^{
+ NSString *collectionId = mediaId ?: @"";
+ MPMediaEntityPersistentID startId = 0;
+ BOOL hasStart = NO;
+ if ([collectionId hasPrefix:@"song:"]) {
+ NSRange at = [collectionId rangeOfString:@"@"];
+ if (at.location == NSNotFound) collectionId = @"";
+ else {
+ hasStart = ParseId([collectionId substringWithRange:NSMakeRange(5, at.location - 5)], &startId);
+ collectionId = [collectionId substringFromIndex:at.location + 1];
+ }
+ }
+ NSArray *items = @[];
+ NSString *error = nil;
+ if (![self hasPermission]) {
+ error = @"Apple Music access is not allowed";
+ } else {
+ @try {
+ items = [self itemsForCollection:collectionId];
+ } @catch (NSException *exception) {
+ error = exception.reason ?: @"Library unavailable";
+ }
+ if (!error && !items.count) error = @"Nothing to play";
+ }
+ MPMediaItem *start = nil;
+ if (hasStart) {
+ for (MPMediaItem *item in items) {
+ if (item.persistentID == startId) { start = item; break; }
+ }
+ }
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (error) {
+ completion(error);
+ return;
+ }
+ MPMusicPlayerController *player = MPMusicPlayerController.systemMusicPlayer;
+ MPMusicPlayerMediaItemQueueDescriptor *descriptor = [[MPMusicPlayerMediaItemQueueDescriptor alloc]
+ initWithItemCollection:[MPMediaItemCollection collectionWithItems:items]];
+ if (start) descriptor.startItem = start;
+ [player setQueueWithDescriptor:descriptor];
+ [player play];
+ self.queueItems = items;
+ self.queueConfirmed = NO;
+ self.queueSetAt = [NSDate date];
+ [self notifyChanged];
+ completion(nil);
+ });
+ });
+}
+@end
diff --git a/App_Resources/iOS/src/module.modulemap b/App_Resources/iOS/src/module.modulemap
index 14291369..02935284 100644
--- a/App_Resources/iOS/src/module.modulemap
+++ b/App_Resources/iOS/src/module.modulemap
@@ -6,6 +6,7 @@ module FaceclawGraphics {
header "FaceclawSpeech.h"
header "FaceclawLocation.h"
header "FaceclawCalendar.h"
+ header "FaceclawAppleMusic.h"
header "FaceclawConfigPort.h"
header "FaceclawEvenHubWebView.h"
header "FaceclawCrypto.h"
diff --git a/CHANGELOG b/CHANGELOG
index 0ba3c545..8b412589 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -6,6 +6,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
### Bug fixes
diff --git a/app/apps/glanceboard/widgets/music-widget.ts b/app/apps/glanceboard/widgets/music-widget.ts
index 4780bf59..ecb23a0c 100644
--- a/app/apps/glanceboard/widgets/music-widget.ts
+++ b/app/apps/glanceboard/widgets/music-widget.ts
@@ -57,9 +57,9 @@ export function musicSettingsMenuItems(): MenuItem[] {
/**
* Now playing: album art (unless turned off), title/artist/album and a
- * progress bar for the active Android media session, the Music app's header
- * at card size. The media bridge runs for the whole session; the widget
- * subscribes and ticks once a second only while something is playing.
+ * progress bar for the active media session (Apple Music on iOS), the Music
+ * app's header at card size. The media bridge runs for the whole session; the
+ * widget subscribes and ticks once a second only while something is playing.
*/
export class MusicWidget implements GlanceWidget {
private media: MediaControllerState = mediaControllerBridge.snapshot();
@@ -105,7 +105,7 @@ export class MusicWidget implements GlanceWidget {
if (!media.accessEnabled) {
// Actionable, so it stays as text.
image.drawText(small, PAD, PAD, "Music", 150);
- image.drawText(small, PAD, PAD + small.lineHeight + 6, "Notification access needed", 140);
+ image.drawText(small, PAD, PAD + small.lineHeight + 6, mediaControllerBridge.accessLabel, 140);
return;
}
// Nothing playing: a dim note, centred, and nothing else.
diff --git a/app/apps/ios-availability.ts b/app/apps/ios-availability.ts
index 8fd09e06..21f0dbf3 100644
--- a/app/apps/ios-availability.ts
+++ b/app/apps/ios-availability.ts
@@ -1,6 +1,5 @@
/** Native services still outside the current iOS app-screen milestone. */
const unavailable: Record = {
- music: 'Media-player integration is not available on iOS yet.',
microphones: 'Glasses microphone capture is not available on iOS yet.',
}
export function iosAppUnavailableReason(appId: string): string | null {
diff --git a/app/apps/music/music-app.ts b/app/apps/music/music-app.ts
index ed0160d2..f35a1130 100644
--- a/app/apps/music/music-app.ts
+++ b/app/apps/music/music-app.ts
@@ -46,10 +46,11 @@ type FocusColumn = "actions" | "playlist";
/**
* Music controller app: metadata + album art + transport controls for the
- * active Android media session (any player that publishes one), 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).
+ * 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
+ * (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).
*/
class MusicAppLayer implements Layer {
// Watch swipes skip tracks: right = next, left = previous.
@@ -97,15 +98,11 @@ class MusicAppLayer implements Layer {
const media = mediaControllerBridge.snapshot();
if (!media.accessEnabled) {
- const lines = wrapText(
- font,
- "Notification access is required to control Android media sessions. Tap to open settings.",
- width - 48,
- );
+ const lines = wrapText(font, mediaControllerBridge.accessPrompt, width - 48);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, 24, 16 + index * lineStep(font), lines[index]!, 180);
}
- image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open settings`, 110);
+ image.drawText(font, 20, height - 16, `${GESTURE_CLICK} ${mediaControllerBridge.accessAction}`, 110);
return image;
}
@@ -113,10 +110,12 @@ class MusicAppLayer implements Layer {
const apps = mediaBrowserBridge.listBrowsableApps(!this.libraryAppsFresh);
this.libraryAppsFresh = true;
this.libraryMenu.setItems(apps);
- image.drawText(font, 24, 16, "No active media session.", 180);
- const prompt = apps.length
- ? "Browse a music app's library, or start playback on the phone."
- : "Start playback in another app on the phone.";
+ image.drawText(font, 24, 16, global.isIOS ? "Nothing is playing in Apple Music." : "No active media session.", 180);
+ const prompt = global.isIOS
+ ? "Browse your library, or start playback on the phone."
+ : apps.length
+ ? "Browse a music app's library, or start playback on the phone."
+ : "Start playback in another app on the phone.";
let y = 22 + lineStep(font);
for (const line of wrapText(font, prompt, width - 48)) {
image.drawText(font, 24, y, line, 150);
@@ -229,7 +228,7 @@ class MusicAppLayer implements Layer {
}
if (!media.accessEnabled) {
if (event.type === "click") {
- mediaControllerBridge.openNotificationAccessSettings();
+ void mediaControllerBridge.requestAccess();
} else if (event.type === "double-click") {
ctx.stack.pop();
}
@@ -268,8 +267,8 @@ class MusicAppLayer implements Layer {
const item = this.queueMenu.selectedItem;
if (item) {
await mediaControllerBridge.skipToQueueItem(item.id);
- // The player rebuilds the queue with the chosen track at index 0.
- this.queueMenu.select(0);
+ // Android players rebuild the queue with the chosen track at index 0.
+ if (mediaControllerBridge.skipRebuildsQueue) this.queueMenu.select(0);
}
return;
}
@@ -497,7 +496,8 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
iconLetter: "M",
icon: "music",
closeable: true,
- menuItems: () => [{
+ // Music settings choose among Android media apps; iOS has only Apple Music.
+ menuItems: () => global.isIOS ? [] : [{
label: "Music settings",
onSelect: (ctx) => {
ctx.stack.pop();
diff --git a/app/assistant/system-tools.ts b/app/assistant/system-tools.ts
index 75309775..9baacf56 100644
--- a/app/assistant/system-tools.ts
+++ b/app/assistant/system-tools.ts
@@ -21,7 +21,7 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void
registered = true;
// Keep a media listener warm so "what's playing" works without opening Music.
- if (!global.isIOS) void mediaControllerBridge.start();
+ void mediaControllerBridge.start();
registry.registerSystemTool(
{
@@ -86,9 +86,6 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void
},
);
- // Phone media and Android notification tools are still platform-specific.
- if (global.isIOS) return;
-
registry.registerSystemTool(
{
name: "media.now_playing",
@@ -127,6 +124,9 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void
},
);
+ // Android notification tools are still platform-specific.
+ if (global.isIOS) return;
+
registry.registerSystemTool(
{
name: "notifications.list",
@@ -181,7 +181,7 @@ function describeState(): string {
function describeNowPlaying(): string {
const state = mediaControllerBridge.snapshot();
- if (!state.accessEnabled) return "Notification access isn't granted, so media state is unavailable.";
+ if (!state.accessEnabled) return `Media state is unavailable (${mediaControllerBridge.accessLabel.toLowerCase()}).`;
if (state.playbackState === "playing" || state.playbackState === "paused") {
const verb = state.playbackState === "playing" ? "Playing" : "Paused";
const title = state.title || "(unknown title)";
diff --git a/app/native/media-browser.ios.ts b/app/native/media-browser.ios.ts
new file mode 100644
index 00000000..ac1ded94
--- /dev/null
+++ b/app/native/media-browser.ios.ts
@@ -0,0 +1,70 @@
+import {
+ APPLE_MUSIC_APP_NAME,
+ APPLE_MUSIC_PACKAGE,
+ parseMediaBrowseItems,
+ type MediaBrowseItem,
+ type MediaBrowserApp,
+ type MediaBrowserBridge,
+} from "./media-types";
+
+export type { MediaBrowseItem, MediaBrowserApp } from "./media-types";
+
+declare const FaceclawAppleMusic: any;
+
+const APPLE_MUSIC_APP: MediaBrowserApp = { packageName: APPLE_MUSIC_PACKAGE, serviceClass: "", appName: APPLE_MUSIC_APP_NAME };
+
+/**
+ * The Apple Music library (playlists, artists, albums, songs in the
+ * on-device / iCloud music library) as a browse tree, through
+ * FaceclawAppleMusic's MediaPlayer queries. Playing an item queues its
+ * collection on the system Music player.
+ */
+export class AppleMusicBrowserBridge implements MediaBrowserBridge {
+ private connected = false;
+ /** Bumped on every connect/disconnect; answers to older requests are dropped. */
+ private generation = 0;
+
+ listBrowsableApps(): MediaBrowserApp[] {
+ return FaceclawAppleMusic.shared().hasPermission() ? [APPLE_MUSIC_APP] : [];
+ }
+
+ connect(app: MediaBrowserApp): Promise {
+ this.disconnect();
+ if (app.packageName !== APPLE_MUSIC_PACKAGE) return Promise.reject(new Error("Only Apple Music can be browsed on iOS"));
+ if (!FaceclawAppleMusic.shared().hasPermission()) return Promise.reject(new Error("Apple Music access is not allowed"));
+ this.connected = true;
+ return Promise.resolve("root");
+ }
+
+ browse(parentId: string): Promise {
+ if (!this.connected) return Promise.reject(new Error("Not connected"));
+ const generation = this.generation;
+ return new Promise((resolve, reject) => {
+ FaceclawAppleMusic.shared().browseCompletion(parentId, (json: string | null, error: string | null) => {
+ if (generation !== this.generation) reject(new Error("Disconnected"));
+ else if (error || json == null) reject(new Error(String(error || "Browse failed")));
+ else {
+ try {
+ resolve(parseMediaBrowseItems(String(json)));
+ } catch (parseError) {
+ reject(new Error(`Browse result parse failed: ${parseError}`));
+ }
+ }
+ });
+ });
+ }
+
+ playFromMediaId(mediaId: string): void {
+ if (!this.connected) return;
+ FaceclawAppleMusic.shared().playCompletion(mediaId, (error: string | null) => {
+ if (error) console.warn(`[apple-music] play ${mediaId} failed: ${error}`);
+ });
+ }
+
+ disconnect(): void {
+ this.connected = false;
+ this.generation++;
+ }
+}
+
+export const mediaBrowserBridge: MediaBrowserBridge = new AppleMusicBrowserBridge();
diff --git a/app/native/media-browser.ts b/app/native/media-browser.ts
index 7da4d72a..0061f9fe 100644
--- a/app/native/media-browser.ts
+++ b/app/native/media-browser.ts
@@ -1,22 +1,11 @@
import { Utils } from "@nativescript/core";
import { ignoredMediaPackages, isMediaAppEnabled, MEDIA_APPS_KEY, rememberMediaApps } from "./media-apps";
import { onSettingsStoreChanged } from "./settings-store";
+import { parseMediaBrowseItems, type MediaBrowseItem, type MediaBrowserApp, type MediaBrowserBridge } from "./media-types";
declare const com: any;
-export type MediaBrowserApp = {
- packageName: string;
- serviceClass: string;
- appName: string;
-};
-
-export type MediaBrowseItem = {
- mediaId: string;
- title: string;
- subtitle: string;
- browsable: boolean;
- playable: boolean;
-};
+export type { MediaBrowseItem, MediaBrowserApp } from "./media-types";
type PendingRequest = {
resolve: (value: T) => void;
@@ -29,7 +18,7 @@ type PendingRequest = {
* start playback, including starting a player that is not currently running.
* Holds at most one connection at a time.
*/
-export class FaceclawMediaBrowserBridge {
+export class FaceclawMediaBrowserBridge implements MediaBrowserBridge {
private browser: any = null;
private listenerProxy: any = null;
private nextRequestId = 1;
@@ -155,22 +144,7 @@ export class FaceclawMediaBrowserBridge {
return;
}
try {
- const raw = JSON.parse(json) as Array<{
- mediaId?: string;
- title?: string;
- subtitle?: string;
- browsable?: boolean;
- playable?: boolean;
- }>;
- pending.resolve(
- raw.map((item) => ({
- mediaId: String(item.mediaId ?? ""),
- title: String(item.title ?? ""),
- subtitle: String(item.subtitle ?? ""),
- browsable: Boolean(item.browsable),
- playable: Boolean(item.playable),
- })),
- );
+ pending.resolve(parseMediaBrowseItems(json));
} catch (parseError) {
pending.reject(new Error(`Browse result parse failed: ${parseError}`));
}
@@ -183,4 +157,4 @@ export class FaceclawMediaBrowserBridge {
}
}
-export const mediaBrowserBridge = new FaceclawMediaBrowserBridge();
+export const mediaBrowserBridge: MediaBrowserBridge = new FaceclawMediaBrowserBridge();
diff --git a/app/native/media-controller.ios.ts b/app/native/media-controller.ios.ts
new file mode 100644
index 00000000..1200c7ce
--- /dev/null
+++ b/app/native/media-controller.ios.ts
@@ -0,0 +1,231 @@
+import { Dialogs, Utils } from "@nativescript/core";
+import type { GrayImage } from "../graphics/image";
+import { grayImageFromPacket, PHOTO_TONE } from "./image-files";
+import { fromData } from "./kotlin-data";
+import {
+ APPLE_MUSIC_APP_NAME,
+ APPLE_MUSIC_PACKAGE,
+ DEFAULT_MEDIA_STATE,
+ type MediaControllerBridge,
+ type MediaControllerState,
+ type MediaPlaybackState,
+ type MediaQueueItem,
+} from "./media-types";
+
+export type { MediaControllerState, MediaPlaybackState, MediaQueueItem } from "./media-types";
+
+declare const FaceclawAppleMusic: any;
+declare const FaceclawKitIosProtocol: any;
+
+/**
+ * Re-read the system player this often while something listens: its change
+ * notifications are not delivered while Faceclaw is suspended, so a track
+ * change in the background would otherwise wait for the next one.
+ */
+const POLL_MS = 2_000;
+
+/** Normalize FaceclawAppleMusic.stateJson()'s object. */
+export function appleMusicState(raw: any): MediaControllerState {
+ if (!raw || raw.access !== true) {
+ return { ...DEFAULT_MEDIA_STATE, status: "Apple Music access is required." };
+ }
+ const hasItem = Boolean(raw.hasItem);
+ const playbackState: MediaPlaybackState = !hasItem
+ ? "idle"
+ : raw.state === "playing" || raw.state === "paused" || raw.state === "stopped" ? raw.state : "unknown";
+ const rate = Number(raw.rate);
+ return {
+ available: hasItem,
+ accessEnabled: true,
+ packageName: APPLE_MUSIC_PACKAGE,
+ appName: APPLE_MUSIC_APP_NAME,
+ playbackState,
+ title: String(raw.title ?? ""),
+ artist: String(raw.artist ?? ""),
+ album: String(raw.album ?? ""),
+ positionMs: finiteOr(raw.positionMs, -1),
+ durationMs: finiteOr(raw.durationMs, -1),
+ // The system player reports a zero rate while it is still starting up.
+ playbackSpeed: playbackState === "playing" ? (rate > 0 ? rate : 1) : 0,
+ status: hasItem ? "" : "Nothing is playing in Apple Music.",
+ canPlayPause: hasItem,
+ canSkipNext: hasItem,
+ canSkipPrevious: hasItem,
+ artVersion: finiteOr(raw.artVersion, 0),
+ };
+}
+
+function finiteOr(value: unknown, fallback: number): number {
+ const number = Number(value);
+ return typeof value === "number" && Number.isFinite(number) ? number : fallback;
+}
+
+/** Everything a listener would redraw for; the position advances on its own. */
+function stateSignature(state: MediaControllerState): string {
+ return JSON.stringify({ ...state, positionMs: 0 });
+}
+
+/**
+ * Apple Music through FaceclawAppleMusic (MediaPlayer's system player): iOS
+ * gives other apps' players no shared session API, so the system Music app is
+ * the only player Faceclaw can follow and control. Snapshots read the player
+ * directly (cheap property reads), so they are current even when a change
+ * notification was missed.
+ */
+export class AppleMusicControllerBridge implements MediaControllerBridge {
+ readonly accessPrompt = "Faceclaw needs access to Apple Music to show and control what's playing. Tap, then allow access on your phone.";
+ readonly accessLabel = "Apple Music access needed";
+ readonly accessAction = "allow access";
+ readonly skipRebuildsQueue = false;
+ private readonly stateListeners = new Set<(state: MediaControllerState) => void>();
+ private bound = false;
+ private started = false;
+ private pollTimer: ReturnType | null = null;
+ private lastSignature = "";
+ private pendingAccess: Promise | null = null;
+ private protocol: any = null;
+
+ onStateChange(listener: (state: MediaControllerState) => void): () => void {
+ void this.start();
+ this.stateListeners.add(listener);
+ this.pollTimer ??= setInterval(() => this.refresh(), POLL_MS);
+ listener(this.snapshot());
+ return () => {
+ this.stateListeners.delete(listener);
+ if (!this.stateListeners.size && this.pollTimer !== null) {
+ clearInterval(this.pollTimer);
+ this.pollTimer = null;
+ }
+ };
+ }
+
+ snapshot(): MediaControllerState {
+ let raw: unknown = null;
+ try {
+ raw = JSON.parse(String(this.native().stateJson()));
+ } catch (error) {
+ console.warn(`[apple-music] state read failed: ${error}`);
+ }
+ return appleMusicState(raw);
+ }
+
+ async start(): Promise {
+ if (this.started) return;
+ this.started = true;
+ this.native().start();
+ }
+
+ async stop(): Promise {
+ if (!this.started) return;
+ this.started = false;
+ this.native().stop();
+ }
+
+ async playPause(): Promise {
+ this.native().playPause();
+ }
+
+ async skipNext(): Promise {
+ this.native().skipNext();
+ }
+
+ async skipPrevious(): Promise {
+ this.native().skipPrevious();
+ }
+
+ hasAccess(): boolean {
+ return Boolean(this.native().hasPermission());
+ }
+
+ /** Concurrent requests (glasses tap, phone Permissions card) share one prompt. */
+ requestAccess(): Promise {
+ if (this.hasAccess()) return Promise.resolve(true);
+ this.pendingAccess ??= this.askForAccess().finally(() => {
+ this.pendingAccess = null;
+ this.refresh();
+ });
+ return this.pendingAccess;
+ }
+
+ async skipToQueueItem(id: number): Promise {
+ this.native().skipToQueueIndex(Math.round(id));
+ }
+
+ getMediaVolumePercent(): number {
+ const volume = Number(this.native().volumePercent());
+ return Number.isFinite(volume) ? Math.max(-1, Math.min(100, Math.round(volume))) : -1;
+ }
+
+ setMediaVolumePercent(volumePercent: number): void {
+ this.native().setVolumePercent(Math.max(0, Math.min(100, Math.round(volumePercent))));
+ }
+
+ getAlbumArt(maxSize: number): GrayImage | null {
+ const packet = this.native().albumArt(Math.round(maxSize));
+ if (!packet) return null;
+ // The shared Kotlin tone curve and dither, as on Android.
+ this.protocol ??= FaceclawKitIosProtocol.new();
+ return grayImageFromPacket(fromData(this.protocol.grayPacketBgraGammaDither(packet, PHOTO_TONE.gamma, PHOTO_TONE.dither)));
+ }
+
+ getQueue(): MediaQueueItem[] {
+ try {
+ const raw = JSON.parse(String(this.native().queueJson()));
+ if (!Array.isArray(raw)) return [];
+ return raw.map((item: { id?: number; title?: string; active?: boolean }) => ({
+ id: Number(item?.id ?? -1),
+ title: String(item?.title ?? ""),
+ active: Boolean(item?.active),
+ }));
+ } catch (error) {
+ console.warn(`[apple-music] queue parse failed: ${error}`);
+ return [];
+ }
+ }
+
+ private native(): any {
+ const music = FaceclawAppleMusic.shared();
+ if (!this.bound) {
+ this.bound = true;
+ music.changeHandler = () => this.refresh();
+ }
+ return music;
+ }
+
+ private refresh(): void {
+ if (!this.stateListeners.size) return;
+ const state = this.snapshot();
+ const signature = stateSignature(state);
+ if (signature === this.lastSignature) return;
+ this.lastSignature = signature;
+ for (const listener of this.stateListeners) listener(state);
+ }
+
+ private async askForAccess(): Promise {
+ const music = this.native();
+ const status = String(music.permissionStatus());
+ if (status === "denied") {
+ const open = await Dialogs.confirm({
+ title: "Apple Music Access",
+ message: "Allow Media & Apple Music access in Settings to show and control Apple Music on your glasses.",
+ okButtonText: "Open Settings",
+ cancelButtonText: "Cancel",
+ });
+ if (open) Utils.openUrl("app-settings:");
+ return false;
+ }
+ if (status === "restricted") {
+ await Dialogs.alert({
+ title: "Apple Music Access Restricted",
+ message: "Media library access is restricted on this iPhone. Check Screen Time or device management settings.",
+ okButtonText: "OK",
+ });
+ return false;
+ }
+ return new Promise((resolve) => {
+ music.requestPermission((granted: boolean) => resolve(Boolean(granted)));
+ });
+ }
+}
+
+export const mediaControllerBridge: MediaControllerBridge = new AppleMusicControllerBridge();
diff --git a/app/native/media-controller.ts b/app/native/media-controller.ts
index caee371b..c6baeb2e 100644
--- a/app/native/media-controller.ts
+++ b/app/native/media-controller.ts
@@ -3,68 +3,25 @@ import { GrayImage } from "../graphics/image";
import { grayImageFromPacket, PHOTO_TONE } from "./image-files";
import { ignoredMediaPackages, MEDIA_APPS_KEY, rememberMediaApps } from "./media-apps";
import { onSettingsStoreChanged } from "./settings-store";
+import { isNotificationListenerEnabled } from "./notification-access";
+import {
+ DEFAULT_MEDIA_STATE,
+ interpolateMediaState,
+ type MediaControllerBridge,
+ type MediaControllerState,
+ type MediaPlaybackState,
+ type MediaQueueItem,
+} from "./media-types";
declare const com: any;
-export type MediaQueueItem = {
- id: number;
- title: string;
- active: boolean;
-};
+export type { MediaControllerState, MediaPlaybackState, MediaQueueItem } from "./media-types";
-export type MediaPlaybackState =
- | "notification-access-required"
- | "idle"
- | "playing"
- | "paused"
- | "buffering"
- | "stopped"
- | "unknown";
-
-export type MediaControllerState = {
- available: boolean;
- accessEnabled: boolean;
- packageName: string;
- appName: string;
- playbackState: MediaPlaybackState;
- title: string;
- artist: string;
- album: string;
- positionMs: number;
- durationMs: number;
- playbackSpeed: number;
- status: string;
- canPlayPause: boolean;
- canSkipNext: boolean;
- canSkipPrevious: boolean;
- /**
- * Changes whenever getAlbumArt's result may have changed (any metadata
- * update or session switch). Key cached art on this, not the title:
- * players re-publish the same strings with a different bitmap.
- */
- artVersion: number;
-};
-
-const DEFAULT_MEDIA_STATE: MediaControllerState = {
- available: false,
- accessEnabled: false,
- packageName: "",
- appName: "",
- playbackState: "idle",
- title: "",
- artist: "",
- album: "",
- positionMs: -1,
- durationMs: -1,
- playbackSpeed: 0,
- status: "Media controller unavailable.",
- canPlayPause: false,
- canSkipNext: false,
- canSkipPrevious: false,
- artVersion: 0,
-};
-
-export class FaceclawMediaControllerBridge {
+export class FaceclawMediaControllerBridge implements MediaControllerBridge {
+ readonly accessPrompt = "Notification access is required to control Android media sessions. Tap to open settings.";
+ readonly accessLabel = "Notification access needed";
+ readonly accessAction = "open settings";
+ readonly skipRebuildsQueue = true;
private readonly stateListeners = new Set<(state: MediaControllerState) => void>();
private controller: any = null;
private listenerProxy: any = null;
@@ -79,12 +36,7 @@ export class FaceclawMediaControllerBridge {
}
snapshot(): MediaControllerState {
- const state = { ...this.state };
- if (state.playbackState === "playing" && state.positionMs >= 0 && state.playbackSpeed !== 0) {
- state.positionMs += (Date.now() - this.stateUpdatedAtMs) * state.playbackSpeed;
- state.positionMs = Math.max(0, state.durationMs > 0 ? Math.min(state.positionMs, state.durationMs) : state.positionMs);
- }
- return state;
+ return interpolateMediaState(this.state, this.stateUpdatedAtMs, Date.now());
}
async start(): Promise {
@@ -115,9 +67,15 @@ export class FaceclawMediaControllerBridge {
this.controller?.skipPrevious();
}
- openNotificationAccessSettings(): void {
+ hasAccess(): boolean {
+ return isNotificationListenerEnabled();
+ }
+
+ /** Opens the notification-access settings screen; the grant is only visible once the app resumes. */
+ async requestAccess(): Promise {
this.ensureController();
this.controller?.openNotificationAccessSettings();
+ return false;
}
/** Jump playback to a queue item (see getQueue). */
@@ -257,4 +215,4 @@ function normalizePlaybackState(value: string): MediaPlaybackState {
}
}
-export const mediaControllerBridge = new FaceclawMediaControllerBridge();
+export const mediaControllerBridge: MediaControllerBridge = new FaceclawMediaControllerBridge();
diff --git a/app/native/media-types.ts b/app/native/media-types.ts
new file mode 100644
index 00000000..84449b30
--- /dev/null
+++ b/app/native/media-types.ts
@@ -0,0 +1,161 @@
+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.
+ */
+
+/** The player behind the iOS bridges: the system Music app. */
+export const APPLE_MUSIC_APP_NAME = "Apple Music";
+export const APPLE_MUSIC_PACKAGE = "com.apple.Music";
+
+export type MediaQueueItem = {
+ id: number;
+ title: string;
+ active: boolean;
+};
+
+export type MediaPlaybackState =
+ | "notification-access-required"
+ | "idle"
+ | "playing"
+ | "paused"
+ | "buffering"
+ | "stopped"
+ | "unknown";
+
+export type MediaControllerState = {
+ available: boolean;
+ accessEnabled: boolean;
+ packageName: string;
+ appName: string;
+ playbackState: MediaPlaybackState;
+ title: string;
+ artist: string;
+ album: string;
+ positionMs: number;
+ durationMs: number;
+ playbackSpeed: number;
+ status: string;
+ canPlayPause: boolean;
+ canSkipNext: boolean;
+ canSkipPrevious: boolean;
+ /**
+ * Changes whenever getAlbumArt's result may have changed (any metadata
+ * update or session switch). Key cached art on this, not the title:
+ * players re-publish the same strings with a different bitmap.
+ */
+ artVersion: number;
+};
+
+export const DEFAULT_MEDIA_STATE: MediaControllerState = {
+ available: false,
+ accessEnabled: false,
+ packageName: "",
+ appName: "",
+ playbackState: "idle",
+ title: "",
+ artist: "",
+ album: "",
+ positionMs: -1,
+ durationMs: -1,
+ playbackSpeed: 0,
+ status: "Media controller unavailable.",
+ canPlayPause: false,
+ canSkipNext: false,
+ canSkipPrevious: false,
+ artVersion: 0,
+};
+
+/** Interpolate the position of a playing item from the last state update. */
+export function interpolateMediaState(state: MediaControllerState, updatedAtMs: number, nowMs: number): MediaControllerState {
+ const result = { ...state };
+ if (result.playbackState === "playing" && result.positionMs >= 0 && result.playbackSpeed !== 0) {
+ result.positionMs += (nowMs - updatedAtMs) * result.playbackSpeed;
+ result.positionMs = Math.max(0, result.durationMs > 0 ? Math.min(result.positionMs, result.durationMs) : result.positionMs);
+ }
+ return result;
+}
+
+export interface MediaControllerBridge {
+ /** Explains, on the glasses, what access is missing and that a tap asks for it. */
+ readonly accessPrompt: string;
+ /** The missing access as a short card label. */
+ readonly accessLabel: string;
+ /** What clicking the access prompt does, for its gesture hint. */
+ readonly accessAction: string;
+ /**
+ * Whether jumping to a queue item rebuilds the queue with that item first
+ * (Android players do); otherwise the queue keeps its order.
+ */
+ readonly skipRebuildsQueue: boolean;
+ onStateChange(listener: (state: MediaControllerState) => void): () => void;
+ snapshot(): MediaControllerState;
+ start(): Promise;
+ stop(): Promise;
+ playPause(): Promise;
+ skipNext(): Promise;
+ skipPrevious(): Promise;
+ hasAccess(): boolean;
+ /** Show the grant UI: a system settings screen or a permission prompt on the phone. */
+ requestAccess(): Promise;
+ /** Jump playback to a queue item (see getQueue). */
+ skipToQueueItem(id: number): Promise;
+ /** Media volume normalized to 0..100; -1 when unavailable. */
+ getMediaVolumePercent(): number;
+ setMediaVolumePercent(volumePercent: number): void;
+ /**
+ * Album art for the current item, grayscale with the shared photo tone
+ * (gamma-corrected and dithered onto the display's 16 levels), scaled to
+ * fit maxSize; null when the player provides none.
+ */
+ getAlbumArt(maxSize: number): GrayImage | null;
+ /** The player's queue (playlist), empty when the player exposes none. */
+ getQueue(): MediaQueueItem[];
+}
+
+export type MediaBrowserApp = {
+ packageName: string;
+ serviceClass: string;
+ appName: string;
+};
+
+export type MediaBrowseItem = {
+ mediaId: string;
+ title: string;
+ subtitle: string;
+ browsable: boolean;
+ playable: boolean;
+};
+
+export interface MediaBrowserBridge {
+ /** Apps whose library can be browsed; cached after the first call. */
+ listBrowsableApps(refresh?: boolean): MediaBrowserApp[];
+ /** Connect to an app's library, replacing any existing connection. Resolves to the browse-tree root id. */
+ connect(app: MediaBrowserApp): Promise;
+ /** Fetch one level of the connected library's tree. */
+ browse(parentId: string): Promise;
+ /** Start playback of a browsed item. */
+ playFromMediaId(mediaId: string): void;
+ /** Drop the connection; in-flight requests are rejected. */
+ disconnect(): void;
+}
+
+/** Parse a browse listing's JSON rows. */
+export function parseMediaBrowseItems(json: string): MediaBrowseItem[] {
+ const raw = JSON.parse(json) as Array<{
+ mediaId?: string;
+ title?: string;
+ subtitle?: string;
+ browsable?: boolean;
+ playable?: boolean;
+ }>;
+ return raw.map((item) => ({
+ mediaId: String(item.mediaId ?? ""),
+ title: String(item.title ?? ""),
+ subtitle: String(item.subtitle ?? ""),
+ browsable: Boolean(item.browsable),
+ playable: Boolean(item.playable),
+ }));
+}
diff --git a/app/phone-ui/permissions-view-model.ts b/app/phone-ui/permissions-view-model.ts
index fbc05c1d..51e40986 100644
--- a/app/phone-ui/permissions-view-model.ts
+++ b/app/phone-ui/permissions-view-model.ts
@@ -15,6 +15,7 @@ import { ensureCalendarPermission, hasCalendarPermission } from "../native/calen
import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization";
import { hasAllFilesAccess, requestAllFilesAccess } from "../native/file-access";
import { isNotificationListenerEnabled, requestNotificationListenerAccess } from "../native/notification-access";
+import { mediaControllerBridge } from "../native/media-controller";
type PermissionDefinition = {
id: string;
@@ -112,6 +113,12 @@ const PERMISSIONS: PermissionDefinition[] = global.isIOS ? [{
optional: true,
isGranted: hasCalendarPermission,
request: ensureCalendarPermission,
+}, {
+ id: "apple-music", title: "Apple Music",
+ description: "Optional: show and control what's playing in Apple Music, and play your music library, from the glasses.",
+ optional: true,
+ isGranted: () => mediaControllerBridge.hasAccess(),
+ request: () => mediaControllerBridge.requestAccess(),
}] : ANDROID_PERMISSIONS;
/** One Repeater card: the permission's copy plus the display-only fields the XML binds. */
@@ -168,7 +175,7 @@ export class PermissionsViewModel extends Observable {
}
get instructions(): string {
- if (global.isIOS) return "Faceclaw uses Bluetooth to communicate with your smart glasses. Calendar access is optional. Tap a card to allow access; a checkmark means it is ready.";
+ if (global.isIOS) return "Faceclaw uses Bluetooth to communicate with your smart glasses. Calendar and Apple Music access are optional. Tap a card to allow access; a checkmark means it is ready.";
return this.onboarding
? `Faceclaw uses these ${global.isIOS ? "iOS" : "Android"} permissions. Tap a card to grant one; a checkmark means it is already granted. ` +
"Optional permissions can also be granted later, from the Permissions item in the main-screen menu."
diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt
index ffe38e36..9f2a21bc 100644
--- a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt
+++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt
@@ -195,6 +195,28 @@ class IosProtocol {
fun png(data: NSData, width: Int, height: Int): NSData =
SharedScreenshots.encode4BitGrayPng(data.byteArray(), width, height).data()
+
+ /**
+ * A [widthLo, widthHi, heightLo, heightHi, B, G, R, A...] packet (little-endian ARGB words,
+ * as CoreGraphics draws them) to the gray packet Android's ImageFileLoader returns, with the
+ * same tone curve and dither. Malformed packets give empty data (no image) rather than an
+ * exception, which would terminate the app on its way into Objective-C.
+ */
+ fun grayPacket(bgra: NSData, gamma: Float, dither: Boolean): NSData {
+ val bytes = bgra.byteArray()
+ if (bytes.size < GrayPacket.HEADER_SIZE) return NSData()
+ val width = (bytes[0].toInt() and 0xff) or ((bytes[1].toInt() and 0xff) shl 8)
+ val height = (bytes[2].toInt() and 0xff) or ((bytes[3].toInt() and 0xff) shl 8)
+ if (width <= 0 || height <= 0 || bytes.size != GrayPacket.HEADER_SIZE + width * height * 4) return NSData()
+ val pixels = IntArray(width * height) { i ->
+ val at = GrayPacket.HEADER_SIZE + i * 4
+ (bytes[at].toInt() and 0xff) or
+ ((bytes[at + 1].toInt() and 0xff) shl 8) or
+ ((bytes[at + 2].toInt() and 0xff) shl 16) or
+ ((bytes[at + 3].toInt() and 0xff) shl 24)
+ }
+ return GrayPacket.fromArgb(pixels, width, height, gamma, dither).data()
+ }
}
/** iOS retains/coalesces surface updates in the same compositor Android uses. */
diff --git a/tests/ios-apple-music.test.cjs b/tests/ios-apple-music.test.cjs
new file mode 100644
index 00000000..35a0ebd5
--- /dev/null
+++ b/tests/ios-apple-music.test.cjs
@@ -0,0 +1,196 @@
+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, 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 types = load('app/native/media-types.ts');
+
+/** A FaceclawAppleMusic stand-in: state is plain data the tests mutate. */
+function fakeMusic() {
+ const music = {
+ permission: 'granted', state: { access: true, hasItem: true, state: 'playing', title: 'Song', artist: 'Band',
+ album: 'Record', positionMs: 1000, durationMs: 200000, rate: 1, artVersion: 3 },
+ queue: '[]', volume: 40, art: null, calls: [], permissionRequests: [], browses: [], plays: [],
+ changeHandler: null,
+ hasPermission() { return this.permission === 'granted'; },
+ permissionStatus() { return this.permission; },
+ requestPermission(done) { this.permissionRequests.push(done); },
+ start() { this.calls.push('start'); }, stop() { this.calls.push('stop'); },
+ stateJson() { return JSON.stringify(this.hasPermission() ? this.state : { access: false }); },
+ playPause() { this.calls.push('playPause'); }, skipNext() { this.calls.push('skipNext'); },
+ skipPrevious() { this.calls.push('skipPrevious'); }, skipToQueueIndex(i) { this.calls.push(`skipTo:${i}`); },
+ queueJson() { return this.queue; },
+ albumArt(size) { this.calls.push(`art:${size}`); return this.art; },
+ volumePercent() { return this.volume; }, setVolumePercent(v) { this.calls.push(`volume:${v}`); },
+ browseCompletion(parentId, done) { this.browses.push({ parentId, done }); },
+ playCompletion(mediaId, done) { this.plays.push({ mediaId, done }); },
+ };
+ return music;
+}
+
+function controller(music = fakeMusic()) {
+ const timers = new Map(); let nextTimer = 1;
+ const dialogs = { confirms: [], alerts: [], confirmResult: true, opened: [] };
+ const protocolCalls = [];
+ const api = load('app/native/media-controller.ios.ts', {
+ '@nativescript/core': {
+ Dialogs: {
+ confirm: async options => { dialogs.confirms.push(options); return dialogs.confirmResult; },
+ alert: async options => { dialogs.alerts.push(options); },
+ },
+ Utils: { openUrl: url => dialogs.opened.push(url) },
+ },
+ './image-files': { grayImageFromPacket: bytes => (bytes && bytes.length ? { bytes } : null), PHOTO_TONE: { gamma: 2.2, dither: true } },
+ './kotlin-data': { fromData: data => data },
+ './media-types': types,
+ }, {
+ FaceclawAppleMusic: { shared: () => music },
+ FaceclawKitIosProtocol: { new: () => ({ grayPacketBgraGammaDither: (packet, gamma, dither) => {
+ protocolCalls.push({ packet, gamma, dither }); return [1, 0, 1, 0, 200];
+ } }) },
+ setInterval: fn => { const id = nextTimer++; timers.set(id, fn); return id; },
+ clearInterval: id => timers.delete(id),
+ });
+ return { api, bridge: api.mediaControllerBridge, music, timers, dialogs, protocolCalls,
+ tick: () => { for (const fn of timers.values()) fn(); } };
+}
+
+test('Apple Music state normalizes the native player, access and missing items', () => {
+ const { appleMusicState } = controller().api;
+ const denied = appleMusicState({ access: false });
+ assert.equal(denied.accessEnabled, false); assert.equal(denied.available, false);
+ assert.equal(appleMusicState(null).accessEnabled, false);
+
+ const playing = appleMusicState({ access: true, hasItem: true, state: 'playing', title: 'A', artist: 'B', album: 'C',
+ positionMs: 5000, durationMs: 60000, rate: 0, artVersion: 7 });
+ assert.equal(playing.available, true); assert.equal(playing.appName, 'Apple Music');
+ assert.equal(playing.packageName, 'com.apple.Music');
+ assert.equal(playing.playbackSpeed, 1, 'a starting player reports rate 0 but is advancing');
+ assert.equal(playing.positionMs, 5000); assert.equal(playing.artVersion, 7);
+ assert.ok(playing.canPlayPause && playing.canSkipNext && playing.canSkipPrevious);
+
+ const paused = appleMusicState({ access: true, hasItem: true, state: 'paused', rate: 1, positionMs: 'x', durationMs: null });
+ assert.equal(paused.playbackSpeed, 0); assert.equal(paused.positionMs, -1); assert.equal(paused.durationMs, -1);
+ assert.equal(appleMusicState({ access: true, hasItem: true, state: 'odd' }).playbackState, 'unknown');
+
+ const empty = appleMusicState({ access: true, hasItem: false, state: 'playing' });
+ assert.equal(empty.accessEnabled, true); assert.equal(empty.available, false);
+ assert.equal(empty.playbackState, 'idle'); assert.equal(empty.canPlayPause, false);
+});
+
+test('listeners start observation, are told only of real changes, and the poll runs while anyone listens', () => {
+ const f = controller(); const seen = [];
+ const off = f.bridge.onStateChange(state => seen.push(state));
+ assert.deepEqual(f.music.calls, ['start']); assert.equal(seen.length, 1); assert.equal(f.timers.size, 1);
+ assert.equal(typeof f.music.changeHandler, 'function');
+
+ f.music.changeHandler(); // first refresh records the baseline
+ const baseline = seen.length;
+ f.music.state.positionMs = 9000; f.tick(); f.music.changeHandler();
+ assert.equal(seen.length, baseline, 'playback progress alone does not notify');
+ f.music.state.title = 'Next song'; f.music.state.artVersion = 4; f.tick();
+ assert.equal(seen.length, baseline + 1); assert.equal(seen.at(-1).title, 'Next song');
+ f.tick(); assert.equal(seen.length, baseline + 1);
+
+ off(); assert.equal(f.timers.size, 0);
+ f.music.state.title = 'Unheard'; f.music.changeHandler(); assert.equal(seen.length, baseline + 1);
+ f.bridge.onStateChange(() => {}); assert.equal(f.music.calls.filter(c => c === 'start').length, 1);
+});
+
+test('transport, queue, volume and album art go through the native player', async () => {
+ const f = controller();
+ await f.bridge.playPause(); await f.bridge.skipNext(); await f.bridge.skipPrevious(); await f.bridge.skipToQueueItem(2.4);
+ assert.deepEqual(f.music.calls, ['playPause', 'skipNext', 'skipPrevious', 'skipTo:2']);
+ assert.equal(f.bridge.skipRebuildsQueue, false);
+
+ f.music.queue = JSON.stringify([{ id: 0, title: 'One', active: false }, { id: 1, title: 'Two', active: true }, null]);
+ assert.deepEqual(JSON.parse(JSON.stringify(f.bridge.getQueue())),
+ [{ id: 0, title: 'One', active: false }, { id: 1, title: 'Two', active: true }, { id: -1, title: '', active: false }]);
+ f.music.queue = 'not json'; assert.equal(f.bridge.getQueue().length, 0);
+
+ assert.equal(f.bridge.getMediaVolumePercent(), 40);
+ f.music.volume = -1; assert.equal(f.bridge.getMediaVolumePercent(), -1);
+ f.music.volume = NaN; assert.equal(f.bridge.getMediaVolumePercent(), -1);
+ f.bridge.setMediaVolumePercent(140); assert.equal(f.music.calls.at(-1), 'volume:100');
+
+ assert.equal(f.bridge.getAlbumArt(112), null); assert.equal(f.protocolCalls.length, 0);
+ f.music.art = 'bgra-packet';
+ assert.deepEqual(Array.from(f.bridge.getAlbumArt(111.6).bytes), [1, 0, 1, 0, 200]);
+ assert.equal(f.music.calls.at(-1), 'art:112');
+ assert.deepEqual(f.protocolCalls[0], { packet: 'bgra-packet', gamma: 2.2, dither: true });
+});
+
+test('access requests share one prompt, and denied or restricted access points to Settings', async () => {
+ const f = controller(); f.music.permission = 'not-determined';
+ const seen = []; f.bridge.onStateChange(state => seen.push(state.accessEnabled));
+ assert.equal(f.bridge.hasAccess(), false); assert.equal(f.bridge.snapshot().accessEnabled, false);
+ const first = f.bridge.requestAccess(), second = f.bridge.requestAccess();
+ assert.equal(f.music.permissionRequests.length, 1);
+ f.music.permission = 'granted'; f.music.permissionRequests[0](true);
+ assert.equal(await first, true); assert.equal(await second, true);
+ assert.equal(seen.at(-1), true, 'listeners hear about the grant');
+ assert.equal(await f.bridge.requestAccess(), true); assert.equal(f.music.permissionRequests.length, 1);
+
+ f.music.permission = 'denied';
+ assert.equal(await f.bridge.requestAccess(), false);
+ assert.equal(f.dialogs.confirms.length, 1); assert.deepEqual(f.dialogs.opened, ['app-settings:']);
+ f.dialogs.confirmResult = false; await f.bridge.requestAccess(); assert.equal(f.dialogs.opened.length, 1);
+
+ f.music.permission = 'restricted';
+ assert.equal(await f.bridge.requestAccess(), false); assert.equal(f.dialogs.alerts.length, 1);
+ assert.equal(f.music.permissionRequests.length, 1);
+});
+
+function browser(music = fakeMusic()) {
+ const api = load('app/native/media-browser.ios.ts', { './media-types': types }, {
+ FaceclawAppleMusic: { shared: () => music },
+ });
+ return { bridge: api.mediaBrowserBridge, music };
+}
+
+test('the Apple Music library browses only with access and drops answers after disconnect', async () => {
+ 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.browse('root'), /Not connected/);
+
+ assert.equal(await f.bridge.connect(app), 'root');
+ const listing = f.bridge.browse('root');
+ assert.equal(f.music.browses[0].parentId, 'root');
+ f.music.browses[0].done(JSON.stringify([{ mediaId: 'playlists', title: 'Playlists', browsable: true }]), null);
+ assert.deepEqual(JSON.parse(JSON.stringify(await listing)),
+ [{ mediaId: 'playlists', title: 'Playlists', subtitle: '', browsable: true, playable: false }]);
+
+ const failed = f.bridge.browse('album:1');
+ f.music.browses[1].done(null, 'Unknown library item');
+ await assert.rejects(failed, /Unknown library item/);
+ const garbled = f.bridge.browse('songs');
+ f.music.browses[2].done('{', null);
+ await assert.rejects(garbled, /parse failed/);
+
+ f.bridge.playFromMediaId('song:5@album:1');
+ assert.equal(f.music.plays[0].mediaId, 'song:5@album:1');
+ const stale = f.bridge.browse('playlists');
+ f.bridge.disconnect();
+ f.music.browses[3].done('[]', null);
+ await assert.rejects(stale, /Disconnected/);
+ f.bridge.playFromMediaId('playlist:2'); assert.equal(f.music.plays.length, 1);
+
+ f.music.permission = 'denied';
+ assert.equal(f.bridge.listBrowsableApps().length, 0);
+ await assert.rejects(f.bridge.connect(app), /not allowed/);
+});
diff --git a/tests/ios-calendar.test.cjs b/tests/ios-calendar.test.cjs
index b07af690..07e92ee3 100644
--- a/tests/ios-calendar.test.cjs
+++ b/tests/ios-calendar.test.cjs
@@ -228,10 +228,12 @@ test('iOS assistant calendar tool awaits events, reports denied access and failu
const f = provider(), registered = new Map();
const { registerSystemTools } = load('app/assistant/system-tools.ts', {
'../native/calendar': f.api, '../native/calendar-permissions': f.permissions,
- '../native/media-controller': {}, '../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {},
+ '../native/media-controller': { mediaControllerBridge: { start: async () => {} } },
+ '../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {},
}, { global: { isIOS: true } });
registerSystemTools({ registerSystemTool: (definition, handler) => registered.set(definition.name, handler) });
- assert.deepEqual([...registered.keys()], ['glasses.get_state', 'glasses.show_alert', 'calendar.list_events']);
+ assert.deepEqual([...registered.keys()], ['glasses.get_state', 'glasses.show_alert', 'calendar.list_events',
+ 'media.now_playing', 'media.play_pause', 'media.next']);
const list = registered.get('calendar.list_events');
const result = list({ within_hours: 2, max_events: 3 });
assert.equal(f.reads[0].max, 3); assert.equal(f.reads[0].window, 7200000);
diff --git a/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt
index b81255a5..1f672f26 100644
--- a/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt
+++ b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt
@@ -22,6 +22,21 @@ class IosNativeApiTest {
assertContentEquals(auth.byteArray(), result.payload.byteArray())
}
+ @Test
+ fun bgraPacketsConvertWithTheSharedGrayTone() {
+ // 2x1: opaque orange, half-transparent white (B, G, R, A in memory).
+ val bgra = byteArrayOf(2, 0, 1, 0, 0x20, 0x80.toByte(), 0xff.toByte(), 0xff.toByte(),
+ 0xff.toByte(), 0xff.toByte(), 0xff.toByte(), 0x80.toByte())
+ val argb = intArrayOf(0xffff8020.toInt(), 0x80ffffff.toInt())
+ val protocol = IosProtocol()
+ for (dither in listOf(false, true)) {
+ assertContentEquals(GrayPacket.fromArgb(argb, 2, 1, 2.2f, dither),
+ protocol.grayPacket(bgra.data(), 2.2f, dither).byteArray())
+ }
+ assertEquals(0UL, protocol.grayPacket(bgra.copyOf(10).data(), 1f, false).length)
+ assertEquals(0UL, protocol.grayPacket(byteArrayOf(0, 0, 1, 0).data(), 1f, false).length)
+ }
+
@Test
fun compassFacadePreservesTheEndOfTheCrcFreePayload() {
val protocol = IosProtocol()
diff --git a/tests/music-settings.test.cjs b/tests/music-settings.test.cjs
index d7f4a0c3..e3d4f416 100644
--- a/tests/music-settings.test.cjs
+++ b/tests/music-settings.test.cjs
@@ -89,6 +89,8 @@ function bridges() {
'./settings-store': storage,
'../graphics/image': {},
'./image-files': {},
+ './notification-access': { isNotificationListenerEnabled: () => true },
+ './media-types': load('app/native/media-types.ts', () => { throw new Error('media-types has no imports'); }),
};
const globals = { global: { isAndroid: true }, com: { faceclaw: { app: {
FaceclawMediaBrowser: NativeBrowser,