forked from Blendman974/faceclaw
Merge branch 'main' of https://github.com/jimrandomh/faceclaw into HEAD
This commit is contained in:
commit
ae48313b81
48 files changed
+1946
-213
No files matched your search
@@ -46,6 +46,17 @@ def faceclawNdkHostTag = {
|
||||
return "linux-x86_64"
|
||||
}
|
||||
|
||||
// Host tools carry .exe on Windows. Call the binaries themselves: the NDK's
|
||||
// target-prefixed clang wrappers are bash scripts (or .cmd files) that only add
|
||||
// --target.
|
||||
def faceclawHostExecutable = { File dir, String name ->
|
||||
return file("$dir/$name${Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : ""}")
|
||||
}
|
||||
|
||||
def faceclawNdkTool = { File ndkRoot, String name ->
|
||||
return faceclawHostExecutable(file("$ndkRoot/toolchains/llvm/prebuilt/${faceclawNdkHostTag()}/bin"), name)
|
||||
}
|
||||
|
||||
def faceclawFindNdkRoot = {
|
||||
def explicitNdk = System.getenv("ANDROID_NDK_HOME")
|
||||
if (explicitNdk != null && file(explicitNdk).exists()) {
|
||||
@@ -111,12 +122,7 @@ tasks.register("prepareFaceclawNativeLibs") {
|
||||
if (ndkRoot == null || !ndkRoot.exists()) {
|
||||
throw new GradleException("Android NDK is required to build libfaceclaw_lc3.so")
|
||||
}
|
||||
// The NDK ships the clang driver twice: extensionless (a Unix shell script)
|
||||
// and as .cmd (the CreateProcess-launchable one Windows needs).
|
||||
def clangBin = "$ndkRoot/toolchains/llvm/prebuilt/${faceclawNdkHostTag()}/bin/aarch64-linux-android24-clang"
|
||||
def clang = Os.isFamily(Os.FAMILY_WINDOWS)
|
||||
? file("${clangBin}.cmd")
|
||||
: file(clangBin)
|
||||
def clang = faceclawNdkTool(ndkRoot, "clang")
|
||||
if (!clang.exists()) {
|
||||
throw new GradleException("Could not find Android clang at $clang")
|
||||
}
|
||||
@@ -130,6 +136,7 @@ tasks.register("prepareFaceclawNativeLibs") {
|
||||
exec {
|
||||
commandLine([
|
||||
clang.absolutePath,
|
||||
"--target=aarch64-linux-android24",
|
||||
"-shared",
|
||||
"-fPIC",
|
||||
"-O3",
|
||||
@@ -197,8 +204,8 @@ tasks.register("prepareFaceclawLlama") {
|
||||
if (cmakeDir == null) {
|
||||
throw new GradleException("Android SDK cmake package is required to build libfaceclaw_llama.so")
|
||||
}
|
||||
def cmake = file("$cmakeDir/bin/cmake")
|
||||
def ninja = file("$cmakeDir/bin/ninja")
|
||||
def cmake = faceclawHostExecutable(file("$cmakeDir/bin"), "cmake")
|
||||
def ninja = faceclawHostExecutable(file("$cmakeDir/bin"), "ninja")
|
||||
def march = "-march=armv8.2-a+dotprod+fp16"
|
||||
|
||||
// Configure once; afterwards the ninja no-op build keeps this cheap while
|
||||
@@ -232,18 +239,13 @@ tasks.register("prepareFaceclawLlama") {
|
||||
}
|
||||
// The static-linked lib carries llama.cpp's full debug info (~50MB);
|
||||
// strip it so the APK only pays for the code (~8MB).
|
||||
def ndkRootForStrip = faceclawFindNdkRoot()
|
||||
if (ndkRootForStrip != null) {
|
||||
// Windows wants llvm-strip.exe; the extensionless file is a Unix script.
|
||||
def llvmStripBin = "$ndkRootForStrip/toolchains/llvm/prebuilt/${faceclawNdkHostTag()}/bin/llvm-strip"
|
||||
def llvmStrip = Os.isFamily(Os.FAMILY_WINDOWS) && file("${llvmStripBin}.exe").exists()
|
||||
? file("${llvmStripBin}.exe")
|
||||
: file(llvmStripBin)
|
||||
if (llvmStrip.exists()) {
|
||||
exec {
|
||||
commandLine([llvmStrip.absolutePath, "--strip-unneeded", llamaSo.absolutePath])
|
||||
}
|
||||
def llvmStrip = faceclawNdkTool(ndkRoot, "llvm-strip")
|
||||
if (llvmStrip.exists()) {
|
||||
exec {
|
||||
commandLine([llvmStrip.absolutePath, "--strip-unneeded", llamaSo.absolutePath])
|
||||
}
|
||||
} else {
|
||||
logger.warn("Could not find llvm-strip at $llvmStrip; packaging an unstripped libfaceclaw_llama.so (~50MB)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+103
@@ -9,6 +9,7 @@ import android.graphics.Color
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.graphics.drawable.Icon
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
@@ -182,6 +183,33 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
/**
|
||||
* The picture attached to one active notification (BigPictureStyle: a
|
||||
* photo, a screenshot, Dexcom's glucose graph), scaled to fit
|
||||
* maxWidth x maxHeight as a gray packet with the tone handling (and
|
||||
* light-background inversion) of ImageFileLoader.bitmapToGrayPacket;
|
||||
* empty when the notification or its picture is unavailable.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getNotificationPictureGray(
|
||||
key: String?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean, invertLightBackground: Boolean,
|
||||
): ByteArray {
|
||||
val service = activeService
|
||||
if (service == null || maxWidth <= 0 || maxHeight <= 0) {
|
||||
return ByteArray(0)
|
||||
}
|
||||
val statusBarNotification = findActiveNotificationByKey(service, key)
|
||||
if (statusBarNotification == null || statusBarNotification.notification == null) {
|
||||
return ByteArray(0)
|
||||
}
|
||||
val picture = loadNotificationPicture(service, statusBarNotification.notification)
|
||||
if (picture == null) {
|
||||
return ByteArray(0)
|
||||
}
|
||||
return ImageFileLoader.bitmapToGrayPacket(
|
||||
halveToward(picture, maxWidth, maxHeight), maxWidth, maxHeight, gamma, dither, invertLightBackground)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getActiveNotificationsJson(maxNotifications: Int): String {
|
||||
val service = activeService
|
||||
@@ -541,6 +569,79 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
|
||||
return (flags and android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE) != 0
|
||||
}
|
||||
|
||||
/** Whether a notification carries a picture for loadNotificationPicture, without loading it. */
|
||||
@Suppress("DEPRECATION")
|
||||
private fun hasNotificationPicture(extras: Bundle): Boolean {
|
||||
return extras.get(Notification.EXTRA_PICTURE) != null
|
||||
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && extras.get(Notification.EXTRA_PICTURE_ICON) != null)
|
||||
}
|
||||
|
||||
/**
|
||||
* A notification's attached picture: BigPictureStyle's bitmap
|
||||
* (EXTRA_PICTURE) or, from Android 12, its Icon (EXTRA_PICTURE_ICON).
|
||||
* Newer releases swap large pictures for a content URI to a bitmap
|
||||
* the system has offloaded (Dexcom's graph arrives that way); the
|
||||
* offload provider lets enabled notification listeners open those.
|
||||
*/
|
||||
@Suppress("DEPRECATION")
|
||||
private fun loadNotificationPicture(service: FaceclawMediaNotificationListenerService, notification: Notification): Bitmap? {
|
||||
val extras: Bundle = notification.extras ?: return null
|
||||
try {
|
||||
val picture: Bitmap? = extras.getParcelable(Notification.EXTRA_PICTURE)
|
||||
if (picture != null) {
|
||||
return softwareBitmap(picture)
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
Log.w(TAG, "failed to read notification picture", t)
|
||||
}
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
val icon: Icon? = extras.getParcelable(Notification.EXTRA_PICTURE_ICON)
|
||||
val drawable: Drawable? = icon?.loadDrawable(service)
|
||||
if (drawable is BitmapDrawable && drawable.bitmap != null) {
|
||||
return softwareBitmap(drawable.bitmap)
|
||||
}
|
||||
if (drawable != null && drawable.intrinsicWidth > 0 && drawable.intrinsicHeight > 0) {
|
||||
val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
|
||||
drawable.setBounds(0, 0, bitmap.width, bitmap.height)
|
||||
drawable.draw(Canvas(bitmap))
|
||||
return bitmap
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
Log.w(TAG, "failed to load notification picture icon", t)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** Hardware bitmaps can't be read back pixel by pixel or drawn on a software canvas; copy those. */
|
||||
private fun softwareBitmap(bitmap: Bitmap): Bitmap {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && bitmap.config == Bitmap.Config.HARDWARE) {
|
||||
return bitmap.copy(Bitmap.Config.ARGB_8888, false)
|
||||
}
|
||||
return bitmap
|
||||
}
|
||||
|
||||
/**
|
||||
* Halve a picture while it is still at least twice the size it will
|
||||
* be shown at, leaving the last (under 2x) step of fitting it into
|
||||
* maxWidth x maxHeight to bitmapToGrayPacket. As with icons
|
||||
* (renderIconScaled), one filtered pass from a much larger source
|
||||
* samples too sparsely: a graph's thin lines break up into dots.
|
||||
*/
|
||||
private fun halveToward(source: Bitmap, maxWidth: Int, maxHeight: Int): Bitmap {
|
||||
var bitmap = source
|
||||
while ((bitmap.width >= maxWidth * 2 || bitmap.height >= maxHeight * 2) && bitmap.width >= 2 && bitmap.height >= 2) {
|
||||
val halved = Bitmap.createScaledBitmap(bitmap, bitmap.width / 2, bitmap.height / 2, true)
|
||||
if (bitmap !== source) {
|
||||
bitmap.recycle()
|
||||
}
|
||||
bitmap = halved
|
||||
}
|
||||
return bitmap
|
||||
}
|
||||
|
||||
/**
|
||||
* Debug aid for garbled-icon reports: saves each rendered icon to
|
||||
* <externalFilesDir>/debug-icons/ (adb-pullable) so extraction problems can
|
||||
@@ -630,8 +731,10 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
|
||||
}
|
||||
}
|
||||
out.put("lines", lines)
|
||||
out.put("hasPicture", hasNotificationPicture(extras))
|
||||
} else {
|
||||
out.put("lines", JSONArray())
|
||||
out.put("hasPicture", false)
|
||||
}
|
||||
|
||||
val actionsJson = JSONArray()
|
||||
|
||||
@@ -132,10 +132,15 @@ class ImageFileLoader private constructor() {
|
||||
* round on its own, which turns smooth gradients into visible bands.
|
||||
* Dithered output is already quantized: every byte is a level times 16,
|
||||
* which BmpUtil re-quantizes back to exactly that level.
|
||||
*
|
||||
* invertLightBackground shows a picture drawn on a light background
|
||||
* (a chart, a screenshot) as its negative; see GrayPacket.fromArgb.
|
||||
*/
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun bitmapToGrayPacket(
|
||||
source: Bitmap?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean): ByteArray {
|
||||
source: Bitmap?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean,
|
||||
invertLightBackground: Boolean = false): ByteArray {
|
||||
if (source == null || source.width <= 0 || source.height <= 0
|
||||
|| maxWidth <= 0 || maxHeight <= 0) {
|
||||
return ByteArray(0)
|
||||
@@ -152,7 +157,7 @@ class ImageFileLoader private constructor() {
|
||||
}
|
||||
val pixels = IntArray(width * height)
|
||||
scaled.getPixels(pixels, 0, width, 0, 0, width, height)
|
||||
return GrayPacket.fromArgb(pixels, width, height, gamma, dither)
|
||||
return GrayPacket.fromArgb(pixels, width, height, gamma, dither, invertLightBackground)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "bitmap conversion failed", e)
|
||||
return ByteArray(0)
|
||||
|
||||
@@ -30,6 +30,8 @@
|
||||
<string>Faceclaw reads your upcoming calendar events to display them in Calendar and Glanceboard on your glasses.</string>
|
||||
<key>NSCalendarsFullAccessUsageDescription</key>
|
||||
<string>Faceclaw reads your upcoming calendar events to display them in Calendar and Glanceboard on your glasses.</string>
|
||||
<key>NSAppleMusicUsageDescription</key>
|
||||
<string>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.</string>
|
||||
<key>NSLocationWhenInUseUsageDescription</key>
|
||||
<string>Faceclaw uses your location for local weather, turn-by-turn navigation, and true north on the glasses compass.</string>
|
||||
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -0,0 +1,512 @@
|
||||
#import "FaceclawAppleMusic.h"
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
#import <MediaPlayer/MediaPlayer.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <errno.h>
|
||||
#import <math.h>
|
||||
#import <stdlib.h>
|
||||
|
||||
/** 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<MPMediaItem *> *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<MPMediaItem *> *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<UIWindow *> *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:<id>", "album:<id>", "artist:<id>" (album by
|
||||
* album, in track order) or "songs" (the whole library). Runs on the library queue.
|
||||
*/
|
||||
- (NSArray<MPMediaItem *> *)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<MPMediaItem *> *items = [NSMutableArray new];
|
||||
for (MPMediaItemCollection *collection in query.collections) [items addObjectsFromArray:collection.items];
|
||||
return items;
|
||||
}
|
||||
|
||||
- (NSArray *)songRows:(NSArray<MPMediaItem *> *)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<MPMediaItem *> *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
|
||||
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user