forked from Blendman974/faceclaw
Port navigate and weather
This commit is contained in:
1 parent
2b3e068a23
commit
9e18745e53
33 files changed
+630
-60
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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')
|
||||
}
|
||||
@@ -3,3 +3,7 @@ export const hasLocationPermission = (): boolean => FaceclawLocation.shared().ha
|
||||
export function ensureLocationPermission(): Promise<boolean> {
|
||||
return new Promise(resolve => FaceclawLocation.shared().requestPermission(resolve))
|
||||
}
|
||||
|
||||
export function ensureFineLocationPermission(): Promise<boolean> {
|
||||
return new Promise(resolve => FaceclawLocation.shared().requestPrecisePermission(resolve))
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
export { ensureLocationPermission, hasLocationPermission } from '../g2/android-permissions'
|
||||
export { ensureLocationPermission, ensureFineLocationPermission, hasLocationPermission } from '../g2/android-permissions'
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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<CurrentLocation> {
|
||||
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()
|
||||
})
|
||||
}
|
||||
@@ -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<CurrentLocation> {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
Reference in new issue
Block a user