diff --git a/App_Resources/iOS/Info.plist b/App_Resources/iOS/Info.plist
index b63a4b01..b79da82a 100644
--- a/App_Resources/iOS/Info.plist
+++ b/App_Resources/iOS/Info.plist
@@ -23,10 +23,13 @@
NSSpeechRecognitionUsageDescription
Faceclaw turns audio from your glasses microphone into text for the app you choose. Dictation uses on-device recognition.
NSLocationWhenInUseUsageDescription
- Faceclaw uses your location to correct the glasses compass from magnetic north to true north.
+ Faceclaw uses your location for local weather, turn-by-turn navigation, and true north on the glasses compass.
+ NSLocationTemporaryUsageDescriptionDictionary
+ NavigationPrecise location lets Faceclaw follow your route and show upcoming turns on your glasses.
UIBackgroundModes
bluetooth-central
+ location
NSAppTransportSecurity
diff --git a/App_Resources/iOS/build.xcconfig b/App_Resources/iOS/build.xcconfig
index fdc60547..d6de5450 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;
// Select the local signing team in Xcode or pass --teamId to the iOS scripts.
-OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework UniformTypeIdentifiers
+OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework UniformTypeIdentifiers -framework ImageIO
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
diff --git a/App_Resources/iOS/src/FaceclawGraphics.h b/App_Resources/iOS/src/FaceclawGraphics.h
index 3dc2e7d5..333f6bc2 100644
--- a/App_Resources/iOS/src/FaceclawGraphics.h
+++ b/App_Resources/iOS/src/FaceclawGraphics.h
@@ -3,4 +3,5 @@
@interface FaceclawGraphics : NSObject
+ (UIImage *)preview:(NSData *)gray width:(NSInteger)width height:(NSInteger)height green:(BOOL)green;
+ (NSData *)renderSVG:(NSString *)svg size:(NSInteger)size;
++ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height;
@end
diff --git a/App_Resources/iOS/src/FaceclawGraphics.m b/App_Resources/iOS/src/FaceclawGraphics.m
index 96a5e25e..361e7353 100644
--- a/App_Resources/iOS/src/FaceclawGraphics.m
+++ b/App_Resources/iOS/src/FaceclawGraphics.m
@@ -1,7 +1,34 @@
#import "FaceclawGraphics.h"
#import
+#import
@implementation FaceclawGraphics
++ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height {
+ if (!data.length || width <= 0 || height <= 0 || width > 2048 || height > 2048) return nil;
+ CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
+ if (!source) return nil;
+ NSDictionary *options = @{(id)kCGImageSourceCreateThumbnailFromImageAlways: @YES,
+ (id)kCGImageSourceCreateThumbnailWithTransform: @YES,
+ (id)kCGImageSourceThumbnailMaxPixelSize: @(MAX(width, height))};
+ CGImageRef image = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef)options);
+ CFRelease(source);
+ if (!image) return nil;
+ NSMutableData *rgba = [NSMutableData dataWithLength:width * height * 4];
+ CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
+ CGContextRef context = CGBitmapContextCreate(rgba.mutableBytes, width, height, 8, width * 4, space,
+ kCGBitmapByteOrder32Big | (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
+ CGColorSpaceRelease(space);
+ if (!context) { CGImageRelease(image); return nil; }
+ // ImageIO applies image orientation; the bitmap's row order is top-to-bottom.
+ CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
+ NSMutableData *gray = [NSMutableData dataWithLength:width * height];
+ const uint8_t *src = rgba.bytes;
+ uint8_t *dst = gray.mutableBytes;
+ for (NSInteger i = 0; i < width * height; i++)
+ dst[i] = (uint8_t)round(0.2126 * src[i * 4] + 0.7152 * src[i * 4 + 1] + 0.0722 * src[i * 4 + 2]);
+ CGContextRelease(context); CGImageRelease(image);
+ return gray;
+}
+ (UIImage *)preview:(NSData *)gray width:(NSInteger)width height:(NSInteger)height green:(BOOL)green {
if (width <= 0 || height <= 0 || gray.length != width * height) return nil;
NSMutableData *rgba = [NSMutableData dataWithLength:width * height * 4];
diff --git a/App_Resources/iOS/src/FaceclawLocation.h b/App_Resources/iOS/src/FaceclawLocation.h
index 53aaa101..9045fbf3 100644
--- a/App_Resources/iOS/src/FaceclawLocation.h
+++ b/App_Resources/iOS/src/FaceclawLocation.h
@@ -5,5 +5,14 @@
+ (instancetype)shared;
- (BOOL)hasPermission;
- (void)requestPermission:(void (^)(BOOL granted))completion;
+- (void)requestPrecisePermission:(void (^)(BOOL granted))completion;
- (void)requestDeclination:(void (^)(double degrees, double latitude, double longitude, NSString *error))completion;
@end
+
+/** Each lookup/stream owns a separate manager, so stopping one never stops another. Main thread only. */
+@interface FaceclawLocationUpdates : NSObject
+@property(nonatomic, copy) void (^eventHandler)(NSString *json);
+- (void)startOnce;
+- (void)startTracking:(NSInteger)intervalMs;
+- (void)stop;
+@end
diff --git a/App_Resources/iOS/src/FaceclawLocation.m b/App_Resources/iOS/src/FaceclawLocation.m
index 1f8872ef..0e9b175f 100644
--- a/App_Resources/iOS/src/FaceclawLocation.m
+++ b/App_Resources/iOS/src/FaceclawLocation.m
@@ -47,6 +47,17 @@
for (void (^callback)(BOOL) in callbacks) callback([self hasPermission]);
if (![self hasPermission]) [self finishWithError:@"Location permission is required for true north"];
}
+- (void)requestPrecisePermission:(void (^)(BOOL))completion {
+ [self requestPermission:^(BOOL granted) {
+ if (!granted || self.manager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy) {
+ completion(granted);
+ return;
+ }
+ [self.manager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"Navigation" completion:^(NSError *error) {
+ completion([self hasPermission] && self.manager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy);
+ }];
+ }];
+}
- (void)requestDeclination:(void (^)(double, double, double, NSString *))completion {
if (![self hasPermission] || ![CLLocationManager headingAvailable]) {
completion(0, 0, 0, @"Location or heading unavailable for true north");
@@ -99,3 +110,88 @@
callback(degrees, coordinate.latitude, coordinate.longitude, error);
}
@end
+
+@interface FaceclawLocationUpdates ()
+@property(nonatomic, strong) CLLocationManager *manager;
+@property(nonatomic, strong) NSTimer *timeout;
+@property(nonatomic) BOOL running;
+@property(nonatomic) BOOL once;
+@property(nonatomic) NSTimeInterval interval;
+@property(nonatomic) NSTimeInterval lastTimestamp;
+@end
+
+@implementation FaceclawLocationUpdates
+- (void)startOnce { [self start:YES interval:0]; }
+- (void)startTracking:(NSInteger)intervalMs { [self start:NO interval:MAX(500, intervalMs) / 1000.0]; }
+- (void)start:(BOOL)once interval:(NSTimeInterval)interval {
+ if (self.running) return;
+ self.once = once; self.interval = interval; self.lastTimestamp = 0;
+ self.manager = [CLLocationManager new];
+ self.manager.delegate = self;
+ CLAuthorizationStatus status = self.manager.authorizationStatus;
+ if (status != kCLAuthorizationStatusAuthorizedAlways && status != kCLAuthorizationStatusAuthorizedWhenInUse) {
+ [self fail:@"Location permission is required. Allow location in the phone's Settings, then retry."]; return;
+ }
+ if (!once && self.manager.accuracyAuthorization != CLAccuracyAuthorizationFullAccuracy) {
+ [self fail:@"Precise location is required for navigation. Enable Precise Location in the phone's Settings."]; return;
+ }
+ self.running = YES;
+ self.manager.desiredAccuracy = once ? kCLLocationAccuracyKilometer : kCLLocationAccuracyBestForNavigation;
+ self.manager.distanceFilter = kCLDistanceFilterNone;
+ if (!once) {
+ self.manager.activityType = CLActivityTypeOtherNavigation;
+ self.manager.pausesLocationUpdatesAutomatically = NO;
+ self.manager.allowsBackgroundLocationUpdates = YES;
+ self.manager.showsBackgroundLocationIndicator = YES;
+ }
+ [self.manager startUpdatingLocation];
+ if (once) self.timeout = [NSTimer scheduledTimerWithTimeInterval:20 repeats:NO block:^(NSTimer *timer) {
+ [self fail:@"Couldn't get a current location. Check Location Services and retry."];
+ }];
+}
+- (void)stop {
+ self.running = NO;
+ [self.timeout invalidate]; self.timeout = nil;
+ [self.manager stopUpdatingLocation];
+ self.manager.delegate = nil; self.manager = nil;
+ self.eventHandler = nil;
+}
+- (void)emit:(NSDictionary *)event {
+ NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil];
+ if (data && self.eventHandler) self.eventHandler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
+}
+- (void)fail:(NSString *)message {
+ void (^handler)(NSString *) = self.eventHandler;
+ [self stop];
+ NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"error": message} options:0 error:nil];
+ if (handler) handler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
+}
+- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
+ if (!self.running) return;
+ CLAuthorizationStatus status = manager.authorizationStatus;
+ if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted ||
+ (!self.once && manager.accuracyAuthorization != CLAccuracyAuthorizationFullAccuracy))
+ [self fail:@"Location permission changed. Allow precise location in the phone's Settings, then retry."];
+}
+- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
+ if (!self.running) return;
+ CLLocation *fix = locations.lastObject;
+ if (!fix || fix.horizontalAccuracy < 0 || fabs(fix.timestamp.timeIntervalSinceNow) > 60) return;
+ NSTimeInterval timestamp = fix.timestamp.timeIntervalSince1970;
+ if (self.lastTimestamp && timestamp - self.lastTimestamp < self.interval) return;
+ self.lastTimestamp = timestamp;
+ NSDictionary *event = @{@"latitude": @(fix.coordinate.latitude), @"longitude": @(fix.coordinate.longitude),
+ @"accuracyMeters": @(fix.horizontalAccuracy), @"timestampMs": @(timestamp * 1000),
+ @"bearingDeg": fix.course >= 0 ? @(fix.course) : NSNull.null,
+ @"speedMps": fix.speed >= 0 ? @(fix.speed) : NSNull.null};
+ if (self.once) {
+ void (^handler)(NSString *) = self.eventHandler;
+ [self stop];
+ NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil];
+ if (handler) handler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
+ } else [self emit:event];
+}
+- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
+ if (self.running && error.code != kCLErrorLocationUnknown) [self fail:error.localizedDescription];
+}
+@end
diff --git a/app/apps/ios-availability.ts b/app/apps/ios-availability.ts
index bae12f44..52de7cfd 100644
--- a/app/apps/ios-availability.ts
+++ b/app/apps/ios-availability.ts
@@ -4,8 +4,6 @@ const unavailable: Record = {
notifications: 'Phone notification integration is not available on iOS yet.',
transcribe: 'Voice capture and transcription are not available on iOS yet.',
microphones: 'Glasses microphone capture is not available on iOS yet.',
- navigate: 'Navigation location tracking is not available on iOS yet.',
- weather: 'Weather location access is not available on iOS yet.',
calendar: 'Calendar access is not available on iOS yet.',
evenhub: 'Open a local .ehpk file in Files to run an EvenHub app. The EvenHub store is not available on iOS yet.',
}
diff --git a/app/apps/navigate/index.ts b/app/apps/navigate/index.ts
index a6f2c0cf..6f02096d 100644
--- a/app/apps/navigate/index.ts
+++ b/app/apps/navigate/index.ts
@@ -1,4 +1,4 @@
-import { ensureFineLocationPermission } from "../../g2/android-permissions";
+import { ensureFineLocationPermission } from "../../native/location-permissions";
import { launchWorkerAppWindow, type AppDefinition } from "../app-definition";
const navigateApp: AppDefinition = {
diff --git a/app/apps/navigate/navigate-app.worker.ts b/app/apps/navigate/navigate-app.worker.ts
index 3eff4595..1334515b 100644
--- a/app/apps/navigate/navigate-app.worker.ts
+++ b/app/apps/navigate/navigate-app.worker.ts
@@ -64,7 +64,7 @@ import {
GESTURE_SHORT_THEN_LONG_PRESS,
type InputEvent,
} from "../../ui/gestures";
-import { LocationTracker, type TrackedLocation } from "../../native/location-tracker";
+import { LocationTracker, type TrackedLocation } from "./navigation-sensors";
import {
fetchRoute,
fetchStaticMapGray,
@@ -76,8 +76,8 @@ import {
type RouteProfile,
type StaticMapCamera,
} from "../../native/mapbox";
-import { addCompassListener, COMPASS_CHANGED, setCompassEnabled, type CompassEvent } from "../../native/compass";
-import { magneticDeclinationDegrees } from "../../native/geomagnetic";
+import { addCompassListener, COMPASS_CHANGED, setCompassEnabled, type CompassEvent } from "./navigation-sensors";
+import { magneticDeclinationDegrees, handleNavigationSensorEvent } from "./navigation-sensors";
import { calibrateHeading, normalizeHeading } from "../compass/calibration";
import { bearingDegrees, haversineMeters, RouteFollower, type RouteProgress } from "./route-follower";
import { drawManeuverGlyph } from "./maneuver-icons";
@@ -184,6 +184,8 @@ let lastRerouteAtMs = 0;
let rerouteInFlight = false;
/** Bumped on every new route/mode so stale map fetches can be discarded. */
let routeGeneration = 0;
+/** Invalidates GPS/geocoding/routing work when the route is replaced or closed. */
+let navigationGeneration = 0;
/**
* Where to go: free text to geocode (optionally shown under a saved
@@ -239,7 +241,6 @@ let compassActive = false;
let unsubscribeCompass: (() => void) | null = null;
/** Wearer's true heading (degrees clockwise from north) as last drawn, or null before compass data arrives. */
let headHeadingDeg: number | null = null;
-let declinationCache: { latitude: number; longitude: number; degrees: number | null } | null = null;
let tickTimer: ReturnType | null = null;
let firstFixWaiters: Array<(fix: TrackedLocation) => void> = [];
@@ -274,6 +275,9 @@ post({ type: "worker-ready" });
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
+ case "navigation-sensors":
+ handleNavigationSensorEvent(message.event);
+ break;
case "open-window":
window = {
windowId: message.windowId,
@@ -390,6 +394,8 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi
render();
throw new Error(statusMessage);
}
+ const generation = ++navigationGeneration;
+ const checkCurrent = () => { if (generation !== navigationGeneration) throw new Error("Navigation cancelled."); };
const hadActiveRoute = phase === "navigating" && follower !== null;
const previous = {
destination,
@@ -404,6 +410,7 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi
render();
ensureTracking();
const fix = await waitForFix();
+ checkCurrent();
phase = "routing";
let picked: GeocodeCandidate;
@@ -420,6 +427,7 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi
statusMessage = `Finding ${query}...`;
render();
candidates = await geocodeForward(query, fix, 5);
+ checkCurrent();
if (!candidates.length) {
throw new Error(`No places found matching "${query}".`);
}
@@ -432,6 +440,7 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi
statusMessage = `Routing to ${destinationName}...`;
render();
const route = await fetchRoute(fix, destination, profile);
+ checkCurrent();
adoptRoute(route);
// Saved destinations already have a row of their own on the idle page;
// everything else (voice, assistant, a re-picked recent) goes on the
@@ -447,6 +456,7 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi
if (window) post({ type: "focus-window", windowId: window.windowId });
return startSummary(picked, candidates, route);
} catch (error) {
+ if (generation !== navigationGeneration) throw error;
const message = String((error as Error)?.message ?? error);
statusMessage = message;
// A failed new destination shouldn't kill guidance that was already
@@ -484,6 +494,7 @@ function adoptRoute(route: Route): void {
}
function stopNavigation(finalStatus: string): void {
+ ++navigationGeneration;
phase = "idle";
statusMessage = finalStatus;
follower = null;
@@ -533,14 +544,16 @@ function handleFix(fix: TrackedLocation): void {
async function maybeReroute(fix: TrackedLocation): Promise {
if (rerouteInFlight || !destination) return;
if (Date.now() - lastRerouteAtMs < REROUTE_MIN_INTERVAL_MS) return;
+ const generation = navigationGeneration;
rerouteInFlight = true;
lastRerouteAtMs = Date.now();
statusMessage = "Rerouting...";
render();
try {
const route = await fetchRoute(fix, destination, profile);
- if (phase === "navigating") adoptRoute(route);
+ if (generation === navigationGeneration && phase === "navigating") adoptRoute(route);
} catch (error) {
+ if (generation !== navigationGeneration) return;
statusMessage = `Reroute failed: ${String((error as Error)?.message ?? error)}`;
render();
} finally {
@@ -601,7 +614,9 @@ function handleCompassEvent(event: CompassEvent): void {
// Wearer-fit offset from the Compass app's calibration, then declination
// from our own GPS fix: the map is always true-north referenced.
const magnetic = calibrateHeading(event.headingDegrees);
- const heading = normalizeHeading(magnetic + (currentDeclination() ?? 0));
+ const correction = currentDeclination();
+ if (correction === null) return; // Keep the travel-direction arrow until true north is known.
+ const heading = normalizeHeading(magnetic + correction);
if (headHeadingDeg !== null && angularDistance(heading, headHeadingDeg) < HEADING_STEP_DEG) return;
headHeadingDeg = heading;
render();
@@ -609,19 +624,7 @@ function handleCompassEvent(event: CompassEvent): void {
/** Declination at the last fix; it only varies over tens of kilometres, so cache per coarse position. */
function currentDeclination(): number | null {
- if (!lastFix) return null;
- if (
- !declinationCache ||
- Math.abs(declinationCache.latitude - lastFix.latitude) > 0.5 ||
- Math.abs(declinationCache.longitude - lastFix.longitude) > 0.5
- ) {
- declinationCache = {
- latitude: lastFix.latitude,
- longitude: lastFix.longitude,
- degrees: magneticDeclinationDegrees(lastFix.latitude, lastFix.longitude),
- };
- }
- return declinationCache.degrees;
+ return lastFix ? magneticDeclinationDegrees(lastFix.latitude, lastFix.longitude) : null;
}
function angularDistance(a: number, b: number): number {
@@ -1123,7 +1126,7 @@ function tokenSetupEntries(): IdleEntry[] {
detail: "Get a free token in the phone browser",
run: () => {
statusMessage = openUrlOnPhone(MAPBOX_TOKENS_URL)
- ? "Opened mapbox.com on your phone. Copy your public token (pk...) and pick Edit token."
+ ? "Opening mapbox.com on your phone. Copy your public token (pk...) and pick Edit token."
: "Could not open a browser on the phone. Visit account.mapbox.com/access-tokens to get a token.";
},
},
diff --git a/app/apps/navigate/navigation-sensors-messages.ts b/app/apps/navigate/navigation-sensors-messages.ts
new file mode 100644
index 00000000..964571a7
--- /dev/null
+++ b/app/apps/navigate/navigation-sensors-messages.ts
@@ -0,0 +1,11 @@
+import { type TrackedLocation } from '../../native/location-types'
+import { type CompassEvent } from '../../native/compass-types'
+
+export type NavigationSensorRequest =
+ | { action: 'start'; id: number; intervalMs: number }
+ | { action: 'stop'; id: number }
+ | { action: 'compass'; enabled: boolean }
+export type NavigationSensorEvent =
+ | { kind: 'location'; id: number; location: TrackedLocation }
+ | { kind: 'error'; id: number; message: string }
+ | { kind: 'compass'; event: CompassEvent; declination: number | null }
diff --git a/app/apps/navigate/navigation-sensors.ios.ts b/app/apps/navigate/navigation-sensors.ios.ts
new file mode 100644
index 00000000..2a8739e4
--- /dev/null
+++ b/app/apps/navigate/navigation-sensors.ios.ts
@@ -0,0 +1,45 @@
+import { type LocationTrackerCallbacks } from '../../native/location-types'
+import { type CompassEvent } from '../../native/compass-types'
+import { type NavigationSensorEvent, type NavigationSensorRequest } from './navigation-sensors-messages'
+export { type TrackedLocation } from '../../native/location-types'
+export { COMPASS_CHANGED, type CompassEvent } from '../../native/compass-types'
+declare const global: any
+
+const listeners = new Set<(event: CompassEvent) => void>()
+const trackers = new Map()
+let nextId = 0
+let declination: number | null = null
+function send(request: NavigationSensorRequest): void {
+ global.postMessage({ type: 'navigation-sensors', request })
+}
+export class LocationTracker {
+ private id = 0
+ constructor(private readonly callbacks: LocationTrackerCallbacks) {}
+ isRunning(): boolean { return this.id !== 0 }
+ start(intervalMs = 1000): void {
+ if (this.id) return
+ this.id = ++nextId; trackers.set(this.id, this)
+ send({ action: 'start', id: this.id, intervalMs })
+ }
+ stop(): void {
+ if (!this.id) return
+ const id = this.id; this.id = 0; trackers.delete(id)
+ send({ action: 'stop', id })
+ }
+ receive(event: NavigationSensorEvent): void {
+ if (event.kind === 'location') this.callbacks.onLocation(event.location)
+ else if (event.kind === 'error') { this.stop(); this.callbacks.onError(event.message) }
+ }
+}
+export function handleNavigationSensorEvent(event: NavigationSensorEvent): void {
+ if (event.kind === 'compass') {
+ declination = event.declination
+ for (const listener of [...listeners]) listener(event.event)
+ } else trackers.get(event.id)?.receive(event)
+}
+export function addCompassListener(listener: (event: CompassEvent) => void): () => void {
+ listeners.add(listener)
+ return () => { listeners.delete(listener) }
+}
+export function setCompassEnabled(enabled: boolean, _owner?: string): void { send({ action: 'compass', enabled }) }
+export function magneticDeclinationDegrees(_latitude: number, _longitude: number): number | null { return declination }
diff --git a/app/apps/navigate/navigation-sensors.ts b/app/apps/navigate/navigation-sensors.ts
new file mode 100644
index 00000000..4bf4793d
--- /dev/null
+++ b/app/apps/navigate/navigation-sensors.ts
@@ -0,0 +1,13 @@
+import { type NavigationSensorEvent } from './navigation-sensors-messages'
+export { LocationTracker, type TrackedLocation } from '../../native/location-tracker'
+export { addCompassListener, setCompassEnabled, COMPASS_CHANGED, type CompassEvent } from '../../native/compass'
+import { magneticDeclinationDegrees as calculate } from '../../native/geomagnetic'
+let cached: { latitude: number; longitude: number; degrees: number | null } | null = null
+export function magneticDeclinationDegrees(latitude: number, longitude: number): number | null {
+ if (!cached || Math.abs(latitude - cached.latitude) > 0.5 || Math.abs(longitude - cached.longitude) > 0.5)
+ cached = { latitude, longitude, degrees: calculate(latitude, longitude) }
+ return cached.degrees
+}
+
+/** Android delivers native callbacks directly on the worker's Looper. */
+export function handleNavigationSensorEvent(_event: NavigationSensorEvent): void {}
diff --git a/app/apps/weather/weather-app.ts b/app/apps/weather/weather-app.ts
index 5636af1a..a0efec28 100644
--- a/app/apps/weather/weather-app.ts
+++ b/app/apps/weather/weather-app.ts
@@ -1,4 +1,4 @@
-import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
+import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions";
import { weatherBridge } from "../../native/weather";
import { WeatherLayer } from "./weather";
import {
@@ -13,12 +13,13 @@ export const WEATHER_SURFACE_ID = "window:weather";
/** Local current conditions and forecast from the National Weather Service. */
export function createWeatherAppWindow(options: InProcessAppOptions): InProcessWindow {
+ let closed = false;
let requestingPermission = false;
let unsubscribe: (() => void) | null = null;
let app: InProcessWindow;
const requestUpdate = () => {
- if (requestingPermission) return;
+ if (closed || requestingPermission) return;
if (hasLocationPermission()) {
weatherBridge.start();
return;
@@ -26,9 +27,10 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW
requestingPermission = true;
void ensureLocationPermission().then((granted) => {
requestingPermission = false;
+ if (closed) return;
if (granted) weatherBridge.start();
app.requestRender();
- });
+ }).catch((error) => { requestingPermission = false; console.warn(`Weather permission: ${error}`); });
};
app = createInProcessWindow({
@@ -52,7 +54,9 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
+ reconfigureSurface: options.reconfigureSurface,
onClosed: () => {
+ closed = true;
unsubscribe?.();
unsubscribe = null;
weatherBridge.stop();
diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts
index 1f0b0a51..dd4f927e 100644
--- a/app/g2/ios-preview-controller.ts
+++ b/app/g2/ios-preview-controller.ts
@@ -1,3 +1,5 @@
+import { IosNavigationSensors } from '../native/ios-navigation-sensors'
+import { Utils } from '@nativescript/core'
import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios'
import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core'
import { iosBluetooth } from '../native/ios-bluetooth'
@@ -331,8 +333,12 @@ export class IosPreviewController {
private ensureWorkerHost(appId: string, create: () => Worker): WorkerAppHost {
const existing = this.appHosts.get(appId)
if (existing) return existing
+ const worker = create()
+ const navigationSensors = appId === 'navigate'
+ ? new IosNavigationSensors(event => worker.postMessage({ type: 'navigation-sensors', event })) : undefined
const host = new WorkerAppHost({
- appId, worker: create(),
+ appId, worker, navigationSensors,
+ openUrl: url => { void Utils.openUrl(url) },
configureSurface: async (id, visible, heightMode) => {
this.compositor.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' })
this.compositor.setSurfaceVisible(id, visible)
diff --git a/app/native/image-bytes.ios.ts b/app/native/image-bytes.ios.ts
new file mode 100644
index 00000000..d5cac1c7
--- /dev/null
+++ b/app/native/image-bytes.ios.ts
@@ -0,0 +1,12 @@
+import { GrayImage } from '../graphics/image'
+import { toData, fromData } from './kotlin-data'
+declare const FaceclawGraphics: any
+
+/** Native image decoding and grayscale conversion, safe on a rendering worker. */
+export function decodeImageBytes(bytes: ArrayBuffer, width: number, height: number): GrayImage | null {
+ const data = FaceclawGraphics.decodeImageWidthHeight(toData(new Uint8Array(bytes)), width, height)
+ if (!data) return null
+ const image = new GrayImage(width, height, 0)
+ image.pixels.set(fromData(data))
+ return image
+}
diff --git a/app/native/image-bytes.ts b/app/native/image-bytes.ts
new file mode 100644
index 00000000..12c7fd5e
--- /dev/null
+++ b/app/native/image-bytes.ts
@@ -0,0 +1,7 @@
+import { grayImageFromPacket } from './image-files'
+import { type GrayImage } from '../graphics/image'
+declare const com: any
+
+export function decodeImageBytes(bytes: ArrayBuffer, width: number, height: number): GrayImage | null {
+ return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(bytes, width, height))
+}
diff --git a/app/native/ios-navigation-sensors.ts b/app/native/ios-navigation-sensors.ts
new file mode 100644
index 00000000..36580bd1
--- /dev/null
+++ b/app/native/ios-navigation-sensors.ts
@@ -0,0 +1,56 @@
+import { LocationTracker } from './location-tracker.ios'
+import { ensureFineLocationPermission } from './location-permissions.ios'
+import { addCompassListener, setCompassEnabled } from './compass.ios'
+import { getDeclinationDegrees, refreshDeclination } from '../apps/compass/declination'
+import { type NavigationSensorEvent, type NavigationSensorRequest } from '../apps/navigate/navigation-sensors-messages'
+
+/** Core Location stays on main; the shared Navigate worker receives only JSON. */
+export class IosNavigationSensors {
+ private generation = 0
+ private id = 0
+ private tracker: LocationTracker | null = null
+ private offCompass: (() => void) | null = null
+ private readonly owner = 'worker:navigate'
+ constructor(private readonly reply: (event: NavigationSensorEvent) => void) {}
+ handle(request: NavigationSensorRequest): void {
+ if (request.action === 'compass') {
+ if (request.enabled && !this.offCompass) {
+ refreshDeclination()
+ this.offCompass = addCompassListener(event => this.reply({ kind: 'compass', event, declination: getDeclinationDegrees() }))
+ } else if (!request.enabled) { this.offCompass?.(); this.offCompass = null }
+ setCompassEnabled(request.enabled, this.owner)
+ } else if (request.action === 'stop') {
+ if (request.id === this.id) this.stopLocation()
+ } else {
+ this.stopLocation(); this.id = request.id
+ const generation = this.generation
+ void ensureFineLocationPermission().then(granted => {
+ if (generation !== this.generation) return
+ if (!granted) throw new Error('Precise location is required for navigation. Enable it in the phone’s Settings, then retry.')
+ refreshDeclination()
+ this.tracker = new LocationTracker({
+ onLocation: location => {
+ if (generation === this.generation) this.reply({ kind: 'location', id: request.id, location })
+ },
+ onError: message => {
+ if (generation !== this.generation) return
+ this.stopLocation(); this.reply({ kind: 'error', id: request.id, message })
+ },
+ })
+ this.tracker.start(request.intervalMs)
+ }).catch(error => {
+ if (generation !== this.generation) return
+ this.stopLocation(); this.reply({ kind: 'error', id: request.id, message: String(error.message ?? error) })
+ })
+ }
+ }
+ private stopLocation(): void {
+ ++this.generation; this.id = 0
+ this.tracker?.stop(); this.tracker = null
+ }
+ stop(): void {
+ this.stopLocation()
+ this.offCompass?.(); this.offCompass = null
+ setCompassEnabled(false, this.owner)
+ }
+}
diff --git a/app/native/kotlin-protocol-smoke.ios.ts b/app/native/kotlin-protocol-smoke.ios.ts
index b7c964f6..390f662f 100644
--- a/app/native/kotlin-protocol-smoke.ios.ts
+++ b/app/native/kotlin-protocol-smoke.ios.ts
@@ -1,3 +1,4 @@
+import { decodeImageBytes } from './image-bytes.ios'
import * as protocol from '../g2/ble-protocol.ios'
import { CfwTransport, parseCfwAcks } from '../g2/cfw-transport.ios'
import { SurfaceCompositor } from '../graphics/surface-compositor.ios'
@@ -36,5 +37,9 @@ export function runKotlinProtocolSmokeTest(): void {
payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) })
if (compass?.headingDegrees !== 359 || compass.diagnostics?.sampleTimeMs !== 0xfedcba98)
throw new Error('Kotlin compass notification mismatch')
+ const png = NSData.alloc().initWithBase64EncodedStringOptions('iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAEklEQVR4nGP4z8DAAMIM/4EAAB/uBfsL2WiLAAAAAElFTkSuQmCC', 0 as NSDataBase64DecodingOptions)
+ const decoded = decodeImageBytes(interop.bufferFromData(png), 2, 2)
+ assert(!!decoded && Array.from(decoded.pixels).join(',') === '54,182,18,255', 'native map image orientation and grayscale')
+ assert(decodeImageBytes(new ArrayBuffer(0), 2, 2) === null, 'invalid map image')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS iOS')
}
diff --git a/app/native/location-permissions.ios.ts b/app/native/location-permissions.ios.ts
index aa668f93..4a8fc468 100644
--- a/app/native/location-permissions.ios.ts
+++ b/app/native/location-permissions.ios.ts
@@ -3,3 +3,7 @@ export const hasLocationPermission = (): boolean => FaceclawLocation.shared().ha
export function ensureLocationPermission(): Promise {
return new Promise(resolve => FaceclawLocation.shared().requestPermission(resolve))
}
+
+export function ensureFineLocationPermission(): Promise {
+ return new Promise(resolve => FaceclawLocation.shared().requestPrecisePermission(resolve))
+}
diff --git a/app/native/location-permissions.ts b/app/native/location-permissions.ts
index c4776a51..7d05f8b3 100644
--- a/app/native/location-permissions.ts
+++ b/app/native/location-permissions.ts
@@ -1 +1 @@
-export { ensureLocationPermission, hasLocationPermission } from '../g2/android-permissions'
+export { ensureLocationPermission, ensureFineLocationPermission, hasLocationPermission } from '../g2/android-permissions'
diff --git a/app/native/location-tracker.ios.ts b/app/native/location-tracker.ios.ts
new file mode 100644
index 00000000..da4b8a4d
--- /dev/null
+++ b/app/native/location-tracker.ios.ts
@@ -0,0 +1,25 @@
+import { type LocationTrackerCallbacks } from './location-types'
+export { type TrackedLocation, type LocationTrackerCallbacks } from './location-types'
+declare const FaceclawLocationUpdates: any
+
+/** Owned by the main-thread iOS sensor host; JSON fixes are relayed to app workers. */
+export class LocationTracker {
+ private native: any = null
+ constructor(private readonly callbacks: LocationTrackerCallbacks) {}
+ isRunning(): boolean { return this.native !== null }
+ start(intervalMs = 1000): void {
+ if (this.native) return
+ const native = this.native = FaceclawLocationUpdates.new()
+ native.eventHandler = (json: string) => {
+ if (this.native !== native) return
+ const event = JSON.parse(json)
+ if (event.error) { this.stop(); this.callbacks.onError(event.error) }
+ else this.callbacks.onLocation(event)
+ }
+ native.startTracking(intervalMs)
+ }
+ stop(): void {
+ const native = this.native; this.native = null
+ native?.stop()
+ }
+}
diff --git a/app/native/location-tracker.ts b/app/native/location-tracker.ts
index 6487ddb6..f087a650 100644
--- a/app/native/location-tracker.ts
+++ b/app/native/location-tracker.ts
@@ -9,22 +9,8 @@ import { Utils } from "@nativescript/core";
declare const com: any;
declare const global: any;
-export type TrackedLocation = {
- latitude: number;
- longitude: number;
- /** Meters, or null when the fix doesn't report accuracy. */
- accuracyMeters: number | null;
- /** Degrees clockwise from true north, or null (common when stationary). */
- bearingDeg: number | null;
- /** Meters per second, or null. */
- speedMps: number | null;
- timestampMs: number;
-};
-
-export type LocationTrackerCallbacks = {
- onLocation: (location: TrackedLocation) => void;
- onError: (message: string) => void;
-};
+import { type LocationTrackerCallbacks } from "./location-types";
+export { type LocationTrackerCallbacks, type TrackedLocation } from "./location-types";
export class LocationTracker {
private tracker: any = null;
diff --git a/app/native/location-types.ts b/app/native/location-types.ts
new file mode 100644
index 00000000..bd29a813
--- /dev/null
+++ b/app/native/location-types.ts
@@ -0,0 +1,23 @@
+export type CurrentLocation = {
+ latitude: number;
+ longitude: number;
+ accuracyMeters: number | null;
+ timestampMs: number;
+};
+
+export type TrackedLocation = {
+ latitude: number;
+ longitude: number;
+ /** Meters, or null when the fix doesn't report accuracy. */
+ accuracyMeters: number | null;
+ /** Degrees clockwise from true north, or null (common when stationary). */
+ bearingDeg: number | null;
+ /** Meters per second, or null. */
+ speedMps: number | null;
+ timestampMs: number;
+};
+
+export type LocationTrackerCallbacks = {
+ onLocation: (location: TrackedLocation) => void;
+ onError: (message: string) => void;
+};
diff --git a/app/native/location.ios.ts b/app/native/location.ios.ts
new file mode 100644
index 00000000..886b6e77
--- /dev/null
+++ b/app/native/location.ios.ts
@@ -0,0 +1,19 @@
+import { type CurrentLocation } from './location-types'
+export { type CurrentLocation } from './location-types'
+declare const FaceclawLocationUpdates: any
+
+/** Main-thread one-shot lookup, independent of navigation and compass managers. */
+export function getCurrentLocation(): Promise {
+ return new Promise((resolve, reject) => {
+ const provider = FaceclawLocationUpdates.new()
+ provider.eventHandler = (json: string) => {
+ try {
+ const event = JSON.parse(json)
+ if (event.error) reject(new Error(event.error))
+ else resolve(event)
+ } catch (error) { reject(error) }
+ finally { provider.stop() }
+ }
+ provider.startOnce()
+ })
+}
diff --git a/app/native/location.ts b/app/native/location.ts
index e2b23abd..1dbafb0e 100644
--- a/app/native/location.ts
+++ b/app/native/location.ts
@@ -2,12 +2,8 @@ import { Utils } from "@nativescript/core";
declare const com: any;
-export type CurrentLocation = {
- latitude: number;
- longitude: number;
- accuracyMeters: number | null;
- timestampMs: number;
-};
+import { type CurrentLocation } from "./location-types";
+export { type CurrentLocation } from "./location-types";
/** Resolve one current foreground location using Android's LocationManager. */
export function getCurrentLocation(): Promise {
diff --git a/app/native/mapbox.ts b/app/native/mapbox.ts
index 364d9252..9f5ad8da 100644
--- a/app/native/mapbox.ts
+++ b/app/native/mapbox.ts
@@ -6,11 +6,9 @@
*/
import { GrayImage } from "../graphics/image";
import { getStringSetting } from "./settings-store";
-import { grayImageFromPacket } from "./image-files";
+import { decodeImageBytes } from "./image-bytes";
import { fetchWithUserAgent } from "../util/http";
-declare const com: any;
-declare const global: any;
export const MAPBOX_TOKEN_SETTING_KEY = "maps.mapboxApiKey";
const SEARCH_ROOT = "https://api.mapbox.com/search/searchbox/v1";
@@ -231,10 +229,7 @@ export async function fetchStaticMapGray(options: {
}
throw new Error(`Mapbox static map failed (HTTP ${response.status}).`);
}
- if (!global.isAndroid) throw new Error("Map decoding is only available on Android.");
- const image = grayImageFromPacket(
- com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(bytes, Math.round(options.width), Math.round(options.height)),
- );
+ const image = decodeImageBytes(bytes, Math.round(options.width), Math.round(options.height));
if (!image) throw new Error("Failed to decode the Mapbox map image.");
return image;
}
diff --git a/app/native/open-url.ios.ts b/app/native/open-url.ios.ts
new file mode 100644
index 00000000..6c9eac63
--- /dev/null
+++ b/app/native/open-url.ios.ts
@@ -0,0 +1,9 @@
+import { Utils } from '@nativescript/core'
+declare const global: any
+
+export function openUrlOnPhone(url: string): boolean {
+ if (!/^https?:\/\//i.test(url)) return false
+ if (NSThread.isMainThread) void Utils.openUrl(url)
+ else global.postMessage({ type: 'open-url', url })
+ return true
+}
diff --git a/app/native/weather.ts b/app/native/weather.ts
index 8cc073e9..f1e98a5f 100644
--- a/app/native/weather.ts
+++ b/app/native/weather.ts
@@ -1,4 +1,4 @@
-import { hasLocationPermission } from "../g2/android-permissions";
+import { hasLocationPermission } from "./location-permissions";
import { getCurrentLocation, type CurrentLocation } from "./location";
import { fetchWithUserAgent } from "../util/http";
import { USER_AGENT } from "../version";
diff --git a/app/ui/shell/worker-window.ts b/app/ui/shell/worker-window.ts
index 77966429..6f5f3251 100644
--- a/app/ui/shell/worker-window.ts
+++ b/app/ui/shell/worker-window.ts
@@ -1,3 +1,4 @@
+import { type NavigationSensorRequest, type NavigationSensorEvent } from "../../apps/navigate/navigation-sensors-messages";
import { GrayImage } from "../../graphics/image";
import { windowIcon } from "./chrome-layer";
import { type IconActivity, type IconName } from "../../graphics/icons";
@@ -14,6 +15,7 @@ import { publishWorkerState } from "./worker-state";
* worker→Java directly on Android; iOS posts baked frames to the main host.
*/
export type WorkerAppMessage =
+ | { type: "navigation-sensors"; event: NavigationSensorEvent }
| { type: "open-window"; windowId: string; surfaceId: string; title: string; viewport: { width: number; height: number } }
| { type: "resize-window"; windowId: string; viewport: { width: number; height: number } }
| { type: "close-window"; windowId: string }
@@ -32,6 +34,8 @@ export type WorkerAppMessage =
| { type: "tool-call"; callId: string; windowId: string; name: string; args: unknown };
export type WorkerAppReply =
+ | { type: "navigation-sensors"; request: NavigationSensorRequest }
+ | { type: "open-url"; url: string }
| { type: "surface-frame"; surfaceId: string; width: number; height: number; pixels: string }
| {
/**
@@ -177,6 +181,8 @@ export type WorkerWindowSpec = {
export type WorkerAppHostOptions = {
appId: string;
worker: Worker;
+ navigationSensors?: { handle(request: NavigationSensorRequest): void; stop(): void };
+ openUrl?: (url: string) => void;
/** Create/refresh a window surface on the compositor (no-op when disconnected). */
configureSurface: (surfaceId: string, visible: boolean, heightMode: WindowHeightMode) => Promise;
setSurfaceVisible: (surfaceId: string, visible: boolean) => void;
@@ -234,6 +240,12 @@ export class WorkerAppHost {
const message = event.data as WorkerAppReply | undefined;
if (!message) return;
switch (message.type) {
+ case "navigation-sensors":
+ if (this.openWindows.size) this.options.navigationSensors?.handle(message.request);
+ break;
+ case "open-url":
+ if (this.openWindows.size && /^https?:\/\//i.test(message.url)) this.options.openUrl?.(message.url);
+ break;
case "surface-frame": {
if (!global.isIOS || !this.options.submitPixels || !this.openWindows.has(message.surfaceId.replace(/^window:/, ""))) break;
const { width, height } = message;
@@ -361,6 +373,7 @@ export class WorkerAppHost {
}
};
options.worker.onerror = (error) => {
+ options.navigationSensors?.stop();
console.error(`worker app ${options.appId} error: ${JSON.stringify(error)}`);
};
}
@@ -397,6 +410,7 @@ export class WorkerAppHost {
claimsLongPress: () => this.windowGestures.get(spec.windowId)?.claimsLongPress ?? false,
close: () => {
this.openWindows.delete(spec.windowId);
+ if (!this.openWindows.size) this.options.navigationSensors?.stop();
this.windowGestures.delete(spec.windowId);
this.windowIconActivity.delete(spec.windowId);
// Withdraw this window's tools and fail any in-flight calls to it.
diff --git a/tests/ios-app-host.test.cjs b/tests/ios-app-host.test.cjs
index e81d37a2..e7374bad 100644
--- a/tests/ios-app-host.test.cjs
+++ b/tests/ios-app-host.test.cjs
@@ -89,6 +89,7 @@ test('background glasses input still composites frames; phone resume preserves t
}
const settings = { onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } };
const modules = {
+ "../native/ios-navigation-sensors": {},
'../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} },
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) }, knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
'../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {} } },
@@ -157,6 +158,7 @@ test('iOS bandwidth footer toggles live, polls only in foreground and resets its
on: (key, fn) => appEvents.set(key, fn), off: key => appEvents.delete(key) },
Button: View, Color: class {}, Dialogs: {}, GridLayout: View, Image: View, Label: View, Page: View, StackLayout: View };
const modules = {
+ "../native/ios-navigation-sensors": {},
'../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} },
'@nativescript/core': core,
'../apps/all-apps': { ALL_APPS: [] },
diff --git a/tests/ios-glanceboard.test.cjs b/tests/ios-glanceboard.test.cjs
index eb518f36..86f8cadd 100644
--- a/tests/ios-glanceboard.test.cjs
+++ b/tests/ios-glanceboard.test.cjs
@@ -68,6 +68,7 @@ function fixture() {
paint: () => { boardStats.paints++; return new images.GrayImage(100, 80, 200); } }),
};
const modules = {
+ "../native/ios-navigation-sensors": {},
"../native/compass.ios": { bindCompassSession() {}, receiveCompassEvent() {} },
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) },
knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
diff --git a/tests/ios-location.test.cjs b/tests/ios-location.test.cjs
new file mode 100644
index 00000000..01f384e7
--- /dev/null
+++ b/tests/ios-location.test.cjs
@@ -0,0 +1,122 @@
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const { loader } = require('./helpers/load-typescript.cjs');
+const flush = () => new Promise(resolve => setImmediate(resolve));
+
+function nativeLocation() {
+ const instances = [];
+ const FaceclawLocationUpdates = { new() {
+ const value = { stopped: 0, startOnce() { this.once = true; }, startTracking(ms) { this.interval = ms; },
+ stop() { this.stopped++; this.eventHandler = null; } };
+ instances.push(value); return value;
+ } };
+ return { instances, load: loader({ FaceclawLocationUpdates }) };
+}
+const fix = { latitude: 37.7, longitude: -122.4, accuracyMeters: 8, bearingDeg: null, speedMps: null, timestampMs: 1234 };
+
+test('iOS one-shot location resolves a fix, rejects native errors, and releases each provider', async () => {
+ const h = nativeLocation(), api = h.load('app/native/location.ios.ts');
+ const result = api.getCurrentLocation(), native = h.instances[0];
+ assert.equal(native.once, true);
+ native.eventHandler(JSON.stringify(fix));
+ assert.deepEqual({ ...await result }, fix); assert.ok(native.stopped);
+ const denied = api.getCurrentLocation();
+ h.instances[1].eventHandler(JSON.stringify({ error: 'Permission denied' }));
+ await assert.rejects(denied, /Permission denied/);
+ assert.ok(h.instances[1].stopped);
+});
+
+test('iOS tracking preserves unavailable course/speed and ignores callbacks after stop or restart', () => {
+ const h = nativeLocation(), api = h.load('app/native/location-tracker.ios.ts'), fixes = [], errors = [];
+ const tracker = new api.LocationTracker({ onLocation: fix => fixes.push(fix), onError: error => errors.push(error) });
+ tracker.start(1000); tracker.start(1000);
+ assert.equal(h.instances.length, 1); assert.equal(h.instances[0].interval, 1000);
+ const old = h.instances[0].eventHandler;
+ old(JSON.stringify(fix)); tracker.stop(); old(JSON.stringify(fix)); tracker.start(); old(JSON.stringify(fix));
+ assert.equal(fixes.length, 1); assert.equal(fixes[0].bearingDeg, null);
+ h.instances[1].eventHandler(JSON.stringify({ error: 'Location disabled' }));
+ assert.equal(tracker.isRunning(), false); assert.deepEqual(errors, ['Location disabled']);
+});
+
+test('Navigate worker routes sensor callbacks by stream id and receives true-north correction', () => {
+ const messages = [], fixes = [], headings = [];
+ const api = loader({ global: { postMessage: message => messages.push(message) } })('app/apps/navigate/navigation-sensors.ios.ts');
+ const tracker = new api.LocationTracker({ onLocation: fix => fixes.push(fix), onError() {} });
+ tracker.start(); const first = messages.at(-1).request.id;
+ tracker.stop(); tracker.start(); const second = messages.at(-1).request.id;
+ api.handleNavigationSensorEvent({ kind: 'location', id: first, location: fix });
+ api.handleNavigationSensorEvent({ kind: 'location', id: second, location: fix });
+ assert.equal(fixes.length, 1);
+ const off = api.addCompassListener(event => headings.push(event));
+ api.handleNavigationSensorEvent({ kind: 'compass', event: { command: 15, headingDegrees: 123 }, declination: 12 });
+ assert.equal(api.magneticDeclinationDegrees(37.7, -122.4), 12);
+ off(); api.handleNavigationSensorEvent({ kind: 'compass', event: {}, declination: null });
+ assert.equal(headings.length, 1); tracker.stop();
+});
+
+function sensorHost() {
+ const pending = [], trackers = [], holds = [], replies = [];
+ let compass;
+ class Tracker {
+ constructor(callbacks) { this.callbacks = callbacks; trackers.push(this); }
+ start() { this.started = true; }
+ stop() { this.stopped = true; }
+ }
+ const api = loader({}, {
+ './location-tracker.ios': { LocationTracker: Tracker },
+ './location-permissions.ios': { ensureFineLocationPermission: () => new Promise(resolve => pending.push(resolve)) },
+ './compass.ios': { setCompassEnabled: (on, owner) => holds.push([on, owner]),
+ addCompassListener: fn => { compass = fn; return () => { compass = null; }; } },
+ '../apps/compass/declination': { refreshDeclination() {}, getDeclinationDegrees: () => 12.5 },
+ })('app/native/ios-navigation-sensors.ts');
+ return { host: new api.IosNavigationSensors(event => replies.push(event)), pending, trackers, holds, replies,
+ heading: () => compass?.({ command: 15, headingDegrees: 90 }) };
+}
+test('closing Navigate while precise permission is pending cannot start GPS later', async () => {
+ const h = sensorHost(); h.host.handle({ action: 'start', id: 1, intervalMs: 1000 });
+ h.host.stop(); h.pending.shift()(true); await flush();
+ assert.equal(h.trackers.length, 0); assert.equal(h.replies.length, 0);
+ h.host.handle({ action: 'start', id: 2, intervalMs: 1000 }); h.pending.shift()(false); await flush();
+ assert.equal(h.replies[0].kind, 'error'); assert.equal(h.replies[0].id, 2);
+});
+test('Navigate host releases GPS and compass together and ignores an old stream error', async () => {
+ const h = sensorHost(); h.host.handle({ action: 'start', id: 1, intervalMs: 1000 });
+ h.pending.shift()(true); await flush();
+ h.host.handle({ action: 'compass', enabled: true }); h.heading();
+ assert.equal(h.replies[0].declination, 12.5);
+ h.trackers[0].callbacks.onLocation(fix); assert.equal(h.replies[1].kind, 'location');
+ h.host.stop(); h.trackers[0].callbacks.onError('late error'); h.heading();
+ assert.equal(h.replies.length, 2); assert.equal(h.trackers[0].stopped, true);
+ assert.deepEqual(h.holds.at(-1), [false, 'worker:navigate']);
+});
+
+test('Weather uses the portable location provider and preserves NWS parsing', async () => {
+ const requests = [], timers = new Set(); let permission = true;
+ const period = { name: 'Today', temperature: 70, temperatureUnit: 'F', shortForecast: 'Sunny', windSpeed: '5 mph', windDirection: 'W' };
+ const api = loader({ setTimeout, clearTimeout, setInterval: fn => { timers.add(fn); return fn; }, clearInterval: fn => timers.delete(fn) }, {
+ './location-permissions': { hasLocationPermission: () => permission },
+ './location': { getCurrentLocation: async () => fix },
+ '../version': { USER_AGENT: 'test' },
+ '../util/http': { fetchWithUserAgent: async url => { requests.push(url); return { ok: true, json: async () =>
+ url.includes('/points/') ? { properties: { forecast: 'https://fixture/forecast', relativeLocation: { properties: { city: 'San Francisco', state: 'CA' } } } }
+ : { properties: { periods: [period] } } }; } },
+ })('app/native/weather.ts');
+ const bridge = new api.WeatherBridge(); bridge.start(); await bridge.refreshNow();
+ assert.equal(bridge.snapshot().phase, 'ready'); assert.equal(bridge.snapshot().current.temperatureF, 70);
+ assert.equal(bridge.snapshot().locationName, 'San Francisco, CA');
+ assert.match(requests[0], /37\.7000,-122\.4000/);
+ bridge.stop(); assert.equal(timers.size, 0);
+ permission = false; await bridge.refreshNow(); assert.equal(bridge.snapshot().phase, 'permission-required');
+});
+
+test('closing Weather during permission prompt cannot restart polling', async () => {
+ let granted, options, starts = 0, stops = 0;
+ const api = loader({}, {
+ '../../native/location-permissions': { hasLocationPermission: () => false, ensureLocationPermission: () => new Promise(resolve => { granted = resolve; }) },
+ '../../native/weather': { weatherBridge: { start() { starts++; }, stop() { stops++; }, onStateChange: () => () => {}, snapshot() {} } },
+ './weather': { WeatherLayer: class {} },
+ '../../ui/shell/in-process-window': { YieldAtRootLayer: class {}, createInProcessWindow: opts => { options = opts; return { requestRender() {} }; } },
+ })('app/apps/weather/weather-app.ts');
+ api.createWeatherAppWindow({ onClosed() {} }); options.onClosed(); granted(true); await flush();
+ assert.equal(starts, 0); assert.equal(stops, 1);
+});
diff --git a/tests/navigate-worker.test.cjs b/tests/navigate-worker.test.cjs
new file mode 100644
index 00000000..c4706615
--- /dev/null
+++ b/tests/navigate-worker.test.cjs
@@ -0,0 +1,78 @@
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const vm = require('node:vm');
+const ts = require('typescript');
+const path = require('node:path');
+const { loader } = require('./helpers/load-typescript.cjs');
+const flush = () => new Promise(resolve => setImmediate(resolve));
+
+const route = { coordinates: [[-122.4, 37.7], [-122.4, 37.71]], distanceMeters: 1112, durationSec: 600,
+ steps: [{ distanceMeters: 1112, durationSec: 600, instruction: 'Head north', maneuverType: 'depart' },
+ { distanceMeters: 0, durationSec: 0, instruction: 'Arrive', maneuverType: 'arrive' }] };
+const fix = latitude => ({ latitude, longitude: -122.4, timestampMs: Date.now(), bearingDeg: 0, speedMps: 1.5, accuracyMeters: 5 });
+function fixture() {
+ const messages = [], timers = new Set(), holds = [], maps = [], errors = [];
+ let callbacks, compass, delayRoute = null;
+ const routeMath = loader()('app/apps/navigate/route-follower.ts');
+ const tracker = { running: false, isRunning() { return this.running; }, start() { this.running = true; }, stop() { this.running = false; } };
+ const sensor = { LocationTracker: function (value) { callbacks = value; return tracker; }, COMPASS_CHANGED: 15,
+ addCompassListener: fn => { compass = fn; return () => { compass = null; }; },
+ setCompassEnabled: value => holds.push(value), magneticDeclinationDegrees: () => 12,
+ handleNavigationSensorEvent: event => { if (event.kind === 'location') callbacks.onLocation(event.location); } };
+ const modules = {
+ '@nativescript/core/globals': {},
+ '../../graphics/bdffont': { getFont: () => ({}) }, '../../graphics/ui-fonts': { getDefaultSmallFont: () => ({}) },
+ '../../native/settings-store': { onSettingsStoreChanged() {} },
+ './navigation-sensors': sensor,
+ './route-follower': routeMath,
+ './destinations': { findSavedDestinationByName: () => null, rememberRecentDestination() {} },
+ '../compass/calibration': { calibrateHeading: value => value + 5, normalizeHeading: value => (value + 360) % 360 },
+ '../../native/frame-timings': { startFrame: () => 1, finishFrame() {}, span: (_id, _name, fn) => fn(), runWithFrame: (_id, fn) => fn() },
+ '../../native/active-display': { getActiveDisplay: () => null },
+ '../../ui/window-menu': { WindowMenu: class { paint() { return []; } } },
+ '../../graphics/plane': { planesFingerprint: () => 'frame' },
+ '../../native/mapbox': {
+ isMapboxConfigured: () => true,
+ geocodeForward: async () => [{ name: 'Test park', placeFormatted: 'San Francisco', latitude: 37.71, longitude: -122.4 }],
+ fetchRoute: async () => delayRoute ? await delayRoute : route,
+ fetchStaticMapGray: async options => { maps.push(options); return { pixels: new Uint8Array([0, 128, 255]) }; },
+ },
+ };
+ const global = { postMessage: message => messages.push(message) };
+ const source = fs.readFileSync(path.join(__dirname, '../app/apps/navigate/navigate-app.worker.ts'), 'utf8') +
+ '\nexports.probe = { startNavigation, stopNavigation, state: () => ({ phase, headHeadingDeg, progress }) };';
+ const exports = {};
+ vm.runInNewContext(ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, {
+ exports, global, Uint8Array, setTimeout, clearTimeout,
+ setInterval: fn => { timers.add(fn); return fn; }, clearInterval: fn => timers.delete(fn),
+ console: { log() {}, warn() {}, error: error => errors.push(error) }, require: id => modules[id] ?? {},
+ });
+ const send = data => global.onmessage({ data });
+ send({ type: 'open-window', windowId: 'navigate:main', surfaceId: 'window:navigate:main', title: 'Navigate', viewport: { width: 576, height: 480 } });
+ send({ type: 'foreground', windowId: 'navigate:main', foreground: true, focused: true });
+ return { ...exports.probe, send, timers, holds, maps, errors, tracker,
+ fix: latitude => send({ type: 'navigation-sensors', event: { kind: 'location', location: fix(latitude), id: 1 } }),
+ heading: value => compass?.({ command: 15, headingDegrees: value }),
+ holdRoute: () => { let resolve; delayRoute = new Promise(done => { resolve = done; }); return () => resolve(route); } };
+}
+
+test('shared Navigate worker follows streamed GPS, uses calibrated true heading and stops at arrival', async () => {
+ const h = fixture(); const starting = h.startNavigation('Test park', 'walking');
+ h.fix(37.7); assert.match(await starting, /Navigating to Test park/); await flush();
+ assert.equal(h.state().phase, 'navigating'); assert.ok(h.maps.length > 0);
+ h.heading(90); assert.equal(h.state().headHeadingDeg, 107);
+ h.send({ type: 'screen', on: false }); assert.equal(h.holds.at(-1), false); assert.equal(h.tracker.running, true);
+ h.fix(37.705); assert.ok(h.state().progress.remainingMeters < 600);
+ h.fix(37.71); assert.equal(h.state().phase, 'arrived'); assert.equal(h.tracker.running, false);
+ assert.equal(h.timers.size, 0); assert.deepEqual(h.errors, []);
+});
+
+test('closing Navigate while a route request is pending cannot restart guidance or GPS', async () => {
+ const h = fixture(), release = h.holdRoute();
+ const starting = h.startNavigation('Test park', 'walking');
+ h.fix(37.7); await flush(); assert.equal(h.state().phase, 'routing');
+ h.send({ type: 'close-window', windowId: 'navigate:main' }); release();
+ await assert.rejects(starting, /Navigation cancelled/);
+ assert.equal(h.state().phase, 'idle'); assert.equal(h.tracker.running, false); assert.equal(h.timers.size, 0);
+});