forked from Blendman974/faceclaw
Add Timer, Weather apps; build out UI of music app; add wakeword action setting
This commit is contained in:
1 parent
008f2277ca
commit
5c73ba2a4d
27 files changed
+2181
-312
No files matched your search
@@ -19,9 +19,12 @@
|
|||||||
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
|
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
|
||||||
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
|
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30"/>
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30"/>
|
||||||
|
<!-- Approximate foreground location is sufficient for the Weather app. -->
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||||
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
|
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
|
||||||
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"/>
|
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"/>
|
||||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
||||||
|
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
|
||||||
<uses-permission android:name="android.permission.READ_CALENDAR"/>
|
<uses-permission android:name="android.permission.READ_CALENDAR"/>
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
|
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
|
||||||
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
|
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
|
||||||
@@ -87,5 +90,9 @@
|
|||||||
<action android:name="android.service.notification.NotificationListenerService" />
|
<action android:name="android.service.notification.NotificationListenerService" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</service>
|
</service>
|
||||||
|
<receiver
|
||||||
|
android:name="com.faceclaw.app.FaceclawTimerReceiver"
|
||||||
|
android:enabled="true"
|
||||||
|
android:exported="false" />
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.faceclaw.app;
|
||||||
|
|
||||||
|
/** One-shot Android location callbacks for the TypeScript Weather bridge. */
|
||||||
|
public interface FaceclawLocationListener {
|
||||||
|
void onLocation(double latitude, double longitude, float accuracyMeters, long timestampMs);
|
||||||
|
|
||||||
|
void onError(String message);
|
||||||
|
}
|
||||||
@@ -0,0 +1,196 @@
|
|||||||
|
package com.faceclaw.app;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.location.Criteria;
|
||||||
|
import android.location.Location;
|
||||||
|
import android.location.LocationListener;
|
||||||
|
import android.location.LocationManager;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One-shot, foreground-only location lookup for Weather. It prefers a fresh
|
||||||
|
* coarse network fix, but can fall back to the newest cached provider fix if
|
||||||
|
* Android cannot produce a new location before the timeout.
|
||||||
|
*/
|
||||||
|
public final class FaceclawLocationProvider implements LocationListener {
|
||||||
|
private static final long FRESH_CACHE_MS = 10L * 60L * 1000L;
|
||||||
|
private static final long MAX_CACHE_MS = 24L * 60L * 60L * 1000L;
|
||||||
|
private static final long TIMEOUT_MS = 15L * 1000L;
|
||||||
|
|
||||||
|
private final Context context;
|
||||||
|
private final LocationManager locationManager;
|
||||||
|
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
|
private final Runnable timeout = this::onTimeout;
|
||||||
|
|
||||||
|
private FaceclawLocationListener listener;
|
||||||
|
private Location cachedLocation;
|
||||||
|
private boolean running;
|
||||||
|
|
||||||
|
public FaceclawLocationProvider(Context context) {
|
||||||
|
this.context = context.getApplicationContext();
|
||||||
|
this.locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListener(FaceclawLocationListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
mainHandler.post(this::startOnMainThread);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancel() {
|
||||||
|
mainHandler.post(() -> finish(null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startOnMainThread() {
|
||||||
|
if (running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (locationManager == null) {
|
||||||
|
deliverError("Android location service is unavailable.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED
|
||||||
|
&& ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
|
deliverError("Location permission is required.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cachedLocation = newestCachedLocation();
|
||||||
|
if (isRecent(cachedLocation, FRESH_CACHE_MS)) {
|
||||||
|
deliverLocation(cachedLocation);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String provider = chooseProvider();
|
||||||
|
if (provider == null) {
|
||||||
|
if (isRecent(cachedLocation, MAX_CACHE_MS)) {
|
||||||
|
deliverLocation(cachedLocation);
|
||||||
|
} else {
|
||||||
|
deliverError("Turn on Location on your phone, then retry.");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
running = true;
|
||||||
|
locationManager.requestSingleUpdate(provider, this, Looper.getMainLooper());
|
||||||
|
mainHandler.postDelayed(timeout, TIMEOUT_MS);
|
||||||
|
} catch (SecurityException error) {
|
||||||
|
finish(null, "Location permission is required.");
|
||||||
|
} catch (Throwable error) {
|
||||||
|
finish(null, "Unable to request the current location.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String chooseProvider() {
|
||||||
|
try {
|
||||||
|
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
||||||
|
return LocationManager.NETWORK_PROVIDER;
|
||||||
|
}
|
||||||
|
Criteria criteria = new Criteria();
|
||||||
|
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
|
||||||
|
criteria.setPowerRequirement(Criteria.POWER_LOW);
|
||||||
|
return locationManager.getBestProvider(criteria, true);
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Location newestCachedLocation() {
|
||||||
|
Location newest = null;
|
||||||
|
try {
|
||||||
|
List<String> providers = locationManager.getProviders(true);
|
||||||
|
for (String provider : providers) {
|
||||||
|
Location candidate = locationManager.getLastKnownLocation(provider);
|
||||||
|
if (candidate != null && (newest == null || candidate.getTime() > newest.getTime())) {
|
||||||
|
newest = candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SecurityException ignored) {
|
||||||
|
// The explicit permission check above owns the user-facing error.
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
// A fresh request may still succeed even if cached providers fail.
|
||||||
|
}
|
||||||
|
return newest;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isRecent(Location location, long maximumAgeMs) {
|
||||||
|
return location != null
|
||||||
|
&& location.getTime() > 0L
|
||||||
|
&& System.currentTimeMillis() - location.getTime() <= maximumAgeMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onTimeout() {
|
||||||
|
if (isRecent(cachedLocation, MAX_CACHE_MS)) {
|
||||||
|
finish(cachedLocation, null);
|
||||||
|
} else {
|
||||||
|
finish(null, "Couldn't get your current location. Tap to retry.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLocationChanged(Location location) {
|
||||||
|
finish(location, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onProviderDisabled(String provider) {
|
||||||
|
// Keep waiting: Android may still deliver a queued fix or the timeout
|
||||||
|
// can fall back to another provider's cached location.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onProviderEnabled(String provider) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||||
|
}
|
||||||
|
|
||||||
|
private void finish(Location location, String error) {
|
||||||
|
if (running) {
|
||||||
|
try {
|
||||||
|
locationManager.removeUpdates(this);
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
running = false;
|
||||||
|
mainHandler.removeCallbacks(timeout);
|
||||||
|
if (location != null) {
|
||||||
|
deliverLocation(location);
|
||||||
|
} else if (error != null) {
|
||||||
|
deliverError(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deliverLocation(Location location) {
|
||||||
|
FaceclawLocationListener current = listener;
|
||||||
|
if (current != null && location != null) {
|
||||||
|
current.onLocation(
|
||||||
|
location.getLatitude(),
|
||||||
|
location.getLongitude(),
|
||||||
|
location.hasAccuracy() ? location.getAccuracy() : -1f,
|
||||||
|
location.getTime());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deliverError(String message) {
|
||||||
|
FaceclawLocationListener current = listener;
|
||||||
|
if (current != null) {
|
||||||
|
current.onError(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,10 @@ package com.faceclaw.app;
|
|||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
import android.media.AudioManager;
|
||||||
import android.media.MediaDescription;
|
import android.media.MediaDescription;
|
||||||
import android.media.MediaMetadata;
|
import android.media.MediaMetadata;
|
||||||
import android.media.session.MediaController;
|
import android.media.session.MediaController;
|
||||||
@@ -12,6 +15,7 @@ import android.media.session.MediaSessionManager;
|
|||||||
import android.media.session.PlaybackState;
|
import android.media.session.PlaybackState;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
import android.os.SystemClock;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -26,6 +30,7 @@ public class FaceclawMediaController {
|
|||||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
private final ComponentName listenerComponent;
|
private final ComponentName listenerComponent;
|
||||||
private final MediaSessionManager sessionManager;
|
private final MediaSessionManager sessionManager;
|
||||||
|
private final AudioManager audioManager;
|
||||||
|
|
||||||
private final MediaController.Callback controllerCallback = new MediaController.Callback() {
|
private final MediaController.Callback controllerCallback = new MediaController.Callback() {
|
||||||
@Override
|
@Override
|
||||||
@@ -71,6 +76,7 @@ public class FaceclawMediaController {
|
|||||||
this.appContext = context.getApplicationContext();
|
this.appContext = context.getApplicationContext();
|
||||||
this.listenerComponent = new ComponentName(appContext, FaceclawMediaNotificationListenerService.class);
|
this.listenerComponent = new ComponentName(appContext, FaceclawMediaNotificationListenerService.class);
|
||||||
this.sessionManager = (MediaSessionManager) appContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
|
this.sessionManager = (MediaSessionManager) appContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
|
||||||
|
this.audioManager = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setListener(FaceclawMediaControllerListener listener) {
|
public void setListener(FaceclawMediaControllerListener listener) {
|
||||||
@@ -171,6 +177,30 @@ public class FaceclawMediaController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Current phone media-stream volume normalized to the range 0..100. */
|
||||||
|
public int getMediaVolumePercent() {
|
||||||
|
if (audioManager == null) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
|
||||||
|
if (maxVolume <= 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
|
||||||
|
return Math.max(0, Math.min(100, Math.round(currentVolume * 100f / maxVolume)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set the phone media-stream volume from a normalized 0..100 value. */
|
||||||
|
public void setMediaVolumePercent(int volumePercent) {
|
||||||
|
if (audioManager == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int clampedPercent = Math.max(0, Math.min(100, volumePercent));
|
||||||
|
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
|
||||||
|
int streamVolume = Math.round(clampedPercent * maxVolume / 100f);
|
||||||
|
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Album art for the active session's current item, grayscale, scaled to
|
* Album art for the active session's current item, grayscale, scaled to
|
||||||
* fit within maxSize x maxSize preserving aspect. Returns
|
* fit within maxSize x maxSize preserving aspect. Returns
|
||||||
@@ -352,9 +382,13 @@ public class FaceclawMediaController {
|
|||||||
|
|
||||||
String playbackState = started ? "idle" : "stopped";
|
String playbackState = started ? "idle" : "stopped";
|
||||||
String packageName = "";
|
String packageName = "";
|
||||||
|
String appName = "";
|
||||||
String title = "";
|
String title = "";
|
||||||
String artist = "";
|
String artist = "";
|
||||||
String album = "";
|
String album = "";
|
||||||
|
long positionMs = -1L;
|
||||||
|
long durationMs = -1L;
|
||||||
|
float playbackSpeed = 0f;
|
||||||
boolean canPlayPause = false;
|
boolean canPlayPause = false;
|
||||||
boolean canSkipNext = false;
|
boolean canSkipNext = false;
|
||||||
boolean canSkipPrevious = false;
|
boolean canSkipPrevious = false;
|
||||||
@@ -368,6 +402,7 @@ public class FaceclawMediaController {
|
|||||||
status = "No active media session.";
|
status = "No active media session.";
|
||||||
} else {
|
} else {
|
||||||
packageName = safe(activeController.getPackageName());
|
packageName = safe(activeController.getPackageName());
|
||||||
|
appName = getApplicationLabel(packageName);
|
||||||
PlaybackState state = activeController.getPlaybackState();
|
PlaybackState state = activeController.getPlaybackState();
|
||||||
MediaMetadata metadata = activeController.getMetadata();
|
MediaMetadata metadata = activeController.getMetadata();
|
||||||
playbackState = playbackStateName(state);
|
playbackState = playbackStateName(state);
|
||||||
@@ -375,6 +410,14 @@ public class FaceclawMediaController {
|
|||||||
title = safe(metadata.getString(MediaMetadata.METADATA_KEY_TITLE));
|
title = safe(metadata.getString(MediaMetadata.METADATA_KEY_TITLE));
|
||||||
artist = safe(metadata.getString(MediaMetadata.METADATA_KEY_ARTIST));
|
artist = safe(metadata.getString(MediaMetadata.METADATA_KEY_ARTIST));
|
||||||
album = safe(metadata.getString(MediaMetadata.METADATA_KEY_ALBUM));
|
album = safe(metadata.getString(MediaMetadata.METADATA_KEY_ALBUM));
|
||||||
|
durationMs = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION);
|
||||||
|
if (durationMs <= 0L) {
|
||||||
|
durationMs = -1L;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (state != null) {
|
||||||
|
positionMs = estimatedPositionMs(state, durationMs);
|
||||||
|
playbackSpeed = state.getPlaybackSpeed();
|
||||||
}
|
}
|
||||||
long actions = state == null ? 0L : state.getActions();
|
long actions = state == null ? 0L : state.getActions();
|
||||||
canPlayPause = (actions & PlaybackState.ACTION_PLAY_PAUSE) != 0
|
canPlayPause = (actions & PlaybackState.ACTION_PLAY_PAUSE) != 0
|
||||||
@@ -387,9 +430,13 @@ public class FaceclawMediaController {
|
|||||||
|
|
||||||
final String finalPlaybackState = playbackState;
|
final String finalPlaybackState = playbackState;
|
||||||
final String finalPackageName = packageName;
|
final String finalPackageName = packageName;
|
||||||
|
final String finalAppName = appName;
|
||||||
final String finalTitle = title;
|
final String finalTitle = title;
|
||||||
final String finalArtist = artist;
|
final String finalArtist = artist;
|
||||||
final String finalAlbum = album;
|
final String finalAlbum = album;
|
||||||
|
final long finalPositionMs = positionMs;
|
||||||
|
final long finalDurationMs = durationMs;
|
||||||
|
final float finalPlaybackSpeed = playbackSpeed;
|
||||||
final boolean finalCanPlayPause = canPlayPause;
|
final boolean finalCanPlayPause = canPlayPause;
|
||||||
final boolean finalCanSkipNext = canSkipNext;
|
final boolean finalCanSkipNext = canSkipNext;
|
||||||
final boolean finalCanSkipPrevious = canSkipPrevious;
|
final boolean finalCanSkipPrevious = canSkipPrevious;
|
||||||
@@ -399,9 +446,13 @@ public class FaceclawMediaController {
|
|||||||
mainHandler.post(() -> currentListener.onStateChange(
|
mainHandler.post(() -> currentListener.onStateChange(
|
||||||
finalPlaybackState,
|
finalPlaybackState,
|
||||||
finalPackageName,
|
finalPackageName,
|
||||||
|
finalAppName,
|
||||||
finalTitle,
|
finalTitle,
|
||||||
finalArtist,
|
finalArtist,
|
||||||
finalAlbum,
|
finalAlbum,
|
||||||
|
finalPositionMs,
|
||||||
|
finalDurationMs,
|
||||||
|
finalPlaybackSpeed,
|
||||||
finalCanPlayPause,
|
finalCanPlayPause,
|
||||||
finalCanSkipNext,
|
finalCanSkipNext,
|
||||||
finalCanSkipPrevious,
|
finalCanSkipPrevious,
|
||||||
@@ -429,6 +480,39 @@ public class FaceclawMediaController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** PlaybackState positions are snapshots; advance a playing snapshot to now. */
|
||||||
|
private long estimatedPositionMs(PlaybackState state, long durationMs) {
|
||||||
|
long positionMs = state.getPosition();
|
||||||
|
if (positionMs == PlaybackState.PLAYBACK_POSITION_UNKNOWN) {
|
||||||
|
return -1L;
|
||||||
|
}
|
||||||
|
if (state.getState() == PlaybackState.STATE_PLAYING) {
|
||||||
|
long updatedAtMs = state.getLastPositionUpdateTime();
|
||||||
|
if (updatedAtMs > 0L) {
|
||||||
|
positionMs += (long) ((SystemClock.elapsedRealtime() - updatedAtMs) * state.getPlaybackSpeed());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
positionMs = Math.max(0L, positionMs);
|
||||||
|
return durationMs > 0L ? Math.min(positionMs, durationMs) : positionMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Resolve a media session's package id to the user-facing installed app name. */
|
||||||
|
private String getApplicationLabel(String packageName) {
|
||||||
|
if (packageName == null || packageName.isEmpty()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
PackageManager packageManager = appContext.getPackageManager();
|
||||||
|
try {
|
||||||
|
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
|
||||||
|
CharSequence label = packageManager.getApplicationLabel(applicationInfo);
|
||||||
|
if (label != null && label.length() > 0) {
|
||||||
|
return label.toString();
|
||||||
|
}
|
||||||
|
} catch (PackageManager.NameNotFoundException ignored) {
|
||||||
|
}
|
||||||
|
return packageName;
|
||||||
|
}
|
||||||
|
|
||||||
private String safe(String value) {
|
private String safe(String value) {
|
||||||
return value == null ? "" : value;
|
return value == null ? "" : value;
|
||||||
}
|
}
|
||||||
|
|||||||
+4
@@ -4,9 +4,13 @@ public interface FaceclawMediaControllerListener {
|
|||||||
void onStateChange(
|
void onStateChange(
|
||||||
String playbackState,
|
String playbackState,
|
||||||
String packageName,
|
String packageName,
|
||||||
|
String appName,
|
||||||
String title,
|
String title,
|
||||||
String artist,
|
String artist,
|
||||||
String album,
|
String album,
|
||||||
|
long positionMs,
|
||||||
|
long durationMs,
|
||||||
|
float playbackSpeed,
|
||||||
boolean canPlayPause,
|
boolean canPlayPause,
|
||||||
boolean canSkipNext,
|
boolean canSkipNext,
|
||||||
boolean canSkipPrevious,
|
boolean canSkipPrevious,
|
||||||
|
|||||||
+5
-1
@@ -388,7 +388,11 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
|||||||
if (statusBarNotification == null || statusBarNotification.getNotification() == null) {
|
if (statusBarNotification == null || statusBarNotification.getNotification() == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (service.getPackageName().equals(statusBarNotification.getPackageName())) {
|
// The dashboard's own persistent foreground notification is noise, but
|
||||||
|
// Timer expiry notifications deliberately flow through the same mirror
|
||||||
|
// and modal path as notifications from other Android apps.
|
||||||
|
if (service.getPackageName().equals(statusBarNotification.getPackageName())
|
||||||
|
&& !FaceclawTimerNotifications.isTimerNotification(statusBarNotification)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Notification notification = statusBarNotification.getNotification();
|
Notification notification = statusBarNotification.getNotification();
|
||||||
|
|||||||
@@ -0,0 +1,187 @@
|
|||||||
|
package com.faceclaw.app;
|
||||||
|
|
||||||
|
import android.app.AlarmManager;
|
||||||
|
import android.app.Notification;
|
||||||
|
import android.app.NotificationChannel;
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
|
import com.tns.NativeScriptActivity;
|
||||||
|
|
||||||
|
/** Android alarm + notification bridge used by the on-glasses Timer app. */
|
||||||
|
public final class FaceclawTimerNotifications {
|
||||||
|
public static final String CHANNEL_ID = "faceclaw-timers";
|
||||||
|
static final String ACTION_EXPIRE = "com.faceclaw.app.action.TIMER_EXPIRE";
|
||||||
|
static final String EXTRA_TIMER_ID = "timerId";
|
||||||
|
static final String EXTRA_DURATION_LABEL = "durationLabel";
|
||||||
|
|
||||||
|
private static final String TIMER_TAG_PREFIX = "faceclaw-timer:";
|
||||||
|
private static final String PREFS_NAME = "faceclaw-timer-notifications";
|
||||||
|
private static final String FIRED_KEY_PREFIX = "fired:";
|
||||||
|
|
||||||
|
private FaceclawTimerNotifications() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule an Android alarm as the durable path. The worker also keeps a
|
||||||
|
* normal JS timeout for prompt delivery while the process is awake.
|
||||||
|
*/
|
||||||
|
public static void schedule(Context context, long timerId, long triggerAtMs, String durationLabel) {
|
||||||
|
Context appContext = context.getApplicationContext();
|
||||||
|
preferences(appContext).edit().putBoolean(firedKey(timerId), false).apply();
|
||||||
|
|
||||||
|
AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
if (manager == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
PendingIntent pendingIntent = expiryPendingIntent(appContext, timerId, durationLabel);
|
||||||
|
long triggerAt = Math.max(System.currentTimeMillis(), triggerAtMs);
|
||||||
|
try {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !manager.canScheduleExactAlarms()) {
|
||||||
|
manager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt, pendingIntent);
|
||||||
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt, pendingIntent);
|
||||||
|
} else {
|
||||||
|
manager.setExact(AlarmManager.RTC_WAKEUP, triggerAt, pendingIntent);
|
||||||
|
}
|
||||||
|
} catch (SecurityException ignored) {
|
||||||
|
// Exact-alarm access can be disabled by the user on recent Android.
|
||||||
|
// The inexact alarm remains a durable backup to the worker timeout.
|
||||||
|
manager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt, pendingIntent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void cancel(Context context, long timerId) {
|
||||||
|
Context appContext = context.getApplicationContext();
|
||||||
|
AlarmManager alarmManager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
if (alarmManager != null) {
|
||||||
|
alarmManager.cancel(expiryPendingIntent(appContext, timerId, ""));
|
||||||
|
}
|
||||||
|
NotificationManager notificationManager =
|
||||||
|
(NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
if (notificationManager != null) {
|
||||||
|
notificationManager.cancel(notificationTag(timerId), notificationId(timerId));
|
||||||
|
}
|
||||||
|
preferences(appContext).edit().remove(firedKey(timerId)).apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Worker-timeout path. Cancels the backup alarm and posts at most once. */
|
||||||
|
public static void fireNow(Context context, long timerId, String durationLabel) {
|
||||||
|
Context appContext = context.getApplicationContext();
|
||||||
|
AlarmManager alarmManager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
if (alarmManager != null) {
|
||||||
|
alarmManager.cancel(expiryPendingIntent(appContext, timerId, durationLabel));
|
||||||
|
}
|
||||||
|
showExpiredOnce(appContext, timerId, durationLabel);
|
||||||
|
}
|
||||||
|
|
||||||
|
static synchronized void showExpiredOnce(Context context, long timerId, String durationLabel) {
|
||||||
|
SharedPreferences preferences = preferences(context);
|
||||||
|
String firedKey = firedKey(timerId);
|
||||||
|
if (preferences.getBoolean(firedKey, false)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
preferences.edit().putBoolean(firedKey, true).apply();
|
||||||
|
ensureChannel(context);
|
||||||
|
|
||||||
|
Intent launchIntent = new Intent(context, NativeScriptActivity.class);
|
||||||
|
launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
|
int contentFlags = PendingIntent.FLAG_UPDATE_CURRENT;
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
contentFlags |= PendingIntent.FLAG_IMMUTABLE;
|
||||||
|
}
|
||||||
|
PendingIntent contentIntent = PendingIntent.getActivity(
|
||||||
|
context,
|
||||||
|
notificationId(timerId),
|
||||||
|
launchIntent,
|
||||||
|
contentFlags
|
||||||
|
);
|
||||||
|
|
||||||
|
Notification.Builder builder = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
|
||||||
|
? new Notification.Builder(context, CHANNEL_ID)
|
||||||
|
: new Notification.Builder(context);
|
||||||
|
String label = durationLabel == null || durationLabel.trim().isEmpty() ? "Timer" : durationLabel + " timer";
|
||||||
|
Notification notification = builder
|
||||||
|
.setContentTitle("Timer finished")
|
||||||
|
.setContentText(label + " is up")
|
||||||
|
.setSmallIcon(context.getApplicationInfo().icon)
|
||||||
|
.setContentIntent(contentIntent)
|
||||||
|
.setCategory(Notification.CATEGORY_ALARM)
|
||||||
|
.setAutoCancel(true)
|
||||||
|
.setWhen(System.currentTimeMillis())
|
||||||
|
.setShowWhen(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
NotificationManager manager =
|
||||||
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
if (manager != null) {
|
||||||
|
manager.notify(notificationTag(timerId), notificationId(timerId), notification);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Only Timer notifications from our own package should enter the mirror. */
|
||||||
|
public static boolean isTimerNotification(android.service.notification.StatusBarNotification item) {
|
||||||
|
if (item == null || item.getNotification() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String tag = item.getTag();
|
||||||
|
if (tag != null && tag.startsWith(TIMER_TAG_PREFIX)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
|
||||||
|
&& CHANNEL_ID.equals(item.getNotification().getChannelId());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ensureChannel(Context context) {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
NotificationChannel channel = new NotificationChannel(
|
||||||
|
CHANNEL_ID,
|
||||||
|
"Timers",
|
||||||
|
NotificationManager.IMPORTANCE_HIGH
|
||||||
|
);
|
||||||
|
channel.setDescription("Alerts when a Faceclaw timer finishes.");
|
||||||
|
channel.enableVibration(true);
|
||||||
|
NotificationManager manager =
|
||||||
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
if (manager != null) {
|
||||||
|
manager.createNotificationChannel(channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PendingIntent expiryPendingIntent(
|
||||||
|
Context context,
|
||||||
|
long timerId,
|
||||||
|
String durationLabel
|
||||||
|
) {
|
||||||
|
Intent intent = new Intent(context, FaceclawTimerReceiver.class);
|
||||||
|
intent.setAction(ACTION_EXPIRE);
|
||||||
|
intent.putExtra(EXTRA_TIMER_ID, timerId);
|
||||||
|
intent.putExtra(EXTRA_DURATION_LABEL, durationLabel == null ? "" : durationLabel);
|
||||||
|
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
flags |= PendingIntent.FLAG_IMMUTABLE;
|
||||||
|
}
|
||||||
|
return PendingIntent.getBroadcast(context, notificationId(timerId), intent, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SharedPreferences preferences(Context context) {
|
||||||
|
return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String firedKey(long timerId) {
|
||||||
|
return FIRED_KEY_PREFIX + timerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String notificationTag(long timerId) {
|
||||||
|
return TIMER_TAG_PREFIX + timerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int notificationId(long timerId) {
|
||||||
|
return (int) (timerId ^ (timerId >>> 32)) & 0x7fffffff;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.faceclaw.app;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
/** Receives durable timer alarms even if the NativeScript worker is asleep. */
|
||||||
|
public class FaceclawTimerReceiver extends BroadcastReceiver {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
if (intent == null || !FaceclawTimerNotifications.ACTION_EXPIRE.equals(intent.getAction())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
long timerId = intent.getLongExtra(FaceclawTimerNotifications.EXTRA_TIMER_ID, 0);
|
||||||
|
if (timerId == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String durationLabel = intent.getStringExtra(FaceclawTimerNotifications.EXTRA_DURATION_LABEL);
|
||||||
|
FaceclawTimerNotifications.showExpiredOnce(context, timerId, durationLabel);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,9 +3,11 @@ import { Application, Utils } from "@nativescript/core";
|
|||||||
const PERMISSION_REQUEST_CODE = 4242;
|
const PERMISSION_REQUEST_CODE = 4242;
|
||||||
const VOICE_PERMISSION_REQUEST_CODE = 4243;
|
const VOICE_PERMISSION_REQUEST_CODE = 4243;
|
||||||
const CALENDAR_PERMISSION_REQUEST_CODE = 4244;
|
const CALENDAR_PERMISSION_REQUEST_CODE = 4244;
|
||||||
|
const LOCATION_PERMISSION_REQUEST_CODE = 4245;
|
||||||
const POST_NOTIFICATIONS_PERMISSION = "android.permission.POST_NOTIFICATIONS";
|
const POST_NOTIFICATIONS_PERMISSION = "android.permission.POST_NOTIFICATIONS";
|
||||||
const RECORD_AUDIO_PERMISSION = "android.permission.RECORD_AUDIO";
|
const RECORD_AUDIO_PERMISSION = "android.permission.RECORD_AUDIO";
|
||||||
const READ_CALENDAR_PERMISSION = "android.permission.READ_CALENDAR";
|
const READ_CALENDAR_PERMISSION = "android.permission.READ_CALENDAR";
|
||||||
|
const ACCESS_COARSE_LOCATION_PERMISSION = "android.permission.ACCESS_COARSE_LOCATION";
|
||||||
|
|
||||||
function getActivity(): androidx.appcompat.app.AppCompatActivity {
|
function getActivity(): androidx.appcompat.app.AppCompatActivity {
|
||||||
const activity = Application.android.foregroundActivity ?? Application.android.startActivity;
|
const activity = Application.android.foregroundActivity ?? Application.android.startActivity;
|
||||||
@@ -124,3 +126,26 @@ export async function ensureCalendarPermission(): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Whether approximate foreground location access has been granted. */
|
||||||
|
export function hasLocationPermission(): boolean {
|
||||||
|
if (!global.isAndroid) return false;
|
||||||
|
return isPermissionGranted(ACCESS_COARSE_LOCATION_PERMISSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prompt for approximate foreground location access. Weather only needs a
|
||||||
|
* coarse coordinate, which avoids asking for more precise access than needed.
|
||||||
|
*/
|
||||||
|
export async function ensureLocationPermission(): Promise<boolean> {
|
||||||
|
if (!global.isAndroid) return false;
|
||||||
|
try {
|
||||||
|
await ensurePermissions(
|
||||||
|
[ACCESS_COARSE_LOCATION_PERMISSION],
|
||||||
|
LOCATION_PERMISSION_REQUEST_CODE,
|
||||||
|
"Location",
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,6 +36,11 @@ import {
|
|||||||
CALENDAR_SURFACE_ID,
|
CALENDAR_SURFACE_ID,
|
||||||
CALENDAR_WINDOW_ID,
|
CALENDAR_WINDOW_ID,
|
||||||
} from "../ui/shell/calendar-app";
|
} from "../ui/shell/calendar-app";
|
||||||
|
import {
|
||||||
|
createWeatherAppWindow,
|
||||||
|
WEATHER_SURFACE_ID,
|
||||||
|
WEATHER_WINDOW_ID,
|
||||||
|
} from "../ui/shell/weather-app";
|
||||||
import {
|
import {
|
||||||
createDebugTestsAppWindow,
|
createDebugTestsAppWindow,
|
||||||
DEBUG_TESTS_SURFACE_ID,
|
DEBUG_TESTS_SURFACE_ID,
|
||||||
@@ -229,7 +234,7 @@ class DashboardController {
|
|||||||
requestRender: () => shell.foregroundWindow()?.requestRender(),
|
requestRender: () => shell.foregroundWindow()?.requestRender(),
|
||||||
},
|
},
|
||||||
apps: [
|
apps: [
|
||||||
{ appId: "stopwatch", label: "Stopwatch", icon: "timer" },
|
{ appId: "timer", label: "Timer", icon: "timer" },
|
||||||
{ appId: "terminal", label: "Terminal", icon: "terminal" },
|
{ appId: "terminal", label: "Terminal", icon: "terminal" },
|
||||||
{ appId: "teleprompt", label: "Teleprompt", icon: "file-text" },
|
{ appId: "teleprompt", label: "Teleprompt", icon: "file-text" },
|
||||||
{ appId: "music", label: "Music", icon: "music" },
|
{ appId: "music", label: "Music", icon: "music" },
|
||||||
@@ -237,6 +242,7 @@ class DashboardController {
|
|||||||
{ appId: "transcribe", label: "Transcribe", icon: "mic" },
|
{ appId: "transcribe", label: "Transcribe", icon: "mic" },
|
||||||
{ appId: "notifications", label: "Notifications", icon: "bell" },
|
{ appId: "notifications", label: "Notifications", icon: "bell" },
|
||||||
{ appId: "calendar", label: "Calendar", icon: "calendar" },
|
{ appId: "calendar", label: "Calendar", icon: "calendar" },
|
||||||
|
{ appId: "weather", label: "Weather", icon: "cloud-sun" },
|
||||||
{ appId: "debug-tests", label: "Debug tests", icon: "flask-conical" },
|
{ appId: "debug-tests", label: "Debug tests", icon: "flask-conical" },
|
||||||
{ appId: "settings", label: "Settings", icon: "settings" },
|
{ appId: "settings", label: "Settings", icon: "settings" },
|
||||||
],
|
],
|
||||||
@@ -854,8 +860,8 @@ class DashboardController {
|
|||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
// Worker paths must be string literals for the webpack worker loader.
|
// Worker paths must be string literals for the webpack worker loader.
|
||||||
let worker: Worker;
|
let worker: Worker;
|
||||||
if (appId === "stopwatch") {
|
if (appId === "timer") {
|
||||||
worker = new Worker("../workers/stopwatch-app.worker");
|
worker = new Worker("../workers/timer-app.worker");
|
||||||
} else if (appId === "terminal") {
|
} else if (appId === "terminal") {
|
||||||
worker = new Worker("../workers/terminal-app.worker");
|
worker = new Worker("../workers/terminal-app.worker");
|
||||||
} else {
|
} else {
|
||||||
@@ -898,6 +904,10 @@ class DashboardController {
|
|||||||
await this.launchInProcessApp(CALENDAR_WINDOW_ID, CALENDAR_SURFACE_ID, createCalendarAppWindow);
|
await this.launchInProcessApp(CALENDAR_WINDOW_ID, CALENDAR_SURFACE_ID, createCalendarAppWindow);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (appId === "weather") {
|
||||||
|
await this.launchInProcessApp(WEATHER_WINDOW_ID, WEATHER_SURFACE_ID, createWeatherAppWindow);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (appId === "debug-tests") {
|
if (appId === "debug-tests") {
|
||||||
await this.launchInProcessApp(DEBUG_TESTS_WINDOW_ID, DEBUG_TESTS_SURFACE_ID, createDebugTestsAppWindow);
|
await this.launchInProcessApp(DEBUG_TESTS_WINDOW_ID, DEBUG_TESTS_SURFACE_ID, createDebugTestsAppWindow);
|
||||||
return;
|
return;
|
||||||
@@ -945,10 +955,17 @@ class DashboardController {
|
|||||||
this.appendLog("launched terminal:hub");
|
this.appendLog("launched terminal:hub");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const serial = this.nextWindowSerial++;
|
if (appId === "timer") {
|
||||||
const windowId = `${appId}:${serial}`;
|
const existing = shell.getWindows().find((window) => window.appId === "timer");
|
||||||
host.openWindow({ windowId, title: `Stopwatch ${serial}`, iconLetter: "S", icon: "timer", focus: true });
|
if (existing) {
|
||||||
this.appendLog(`launched ${windowId}`);
|
shell.focusWindow(existing.windowId);
|
||||||
|
this.requestShellRender();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
host.openWindow({ windowId: "timer:main", title: "Timer", iconLetter: "T", icon: "timer", focus: true });
|
||||||
|
this.appendLog("launched timer:main");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create/refresh a window surface on the compositor, if connected. */
|
/** Create/refresh a window surface on the compositor, if connected. */
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ export const ICON_SVGS = {
|
|||||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M10.268 21a2 2 0 0 0 3.464 0"/><path d="M3.262 15.326A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326"/></svg>',
|
'<svg viewBox="0 0 24 24" fill="none"><path d="M10.268 21a2 2 0 0 0 3.464 0"/><path d="M3.262 15.326A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326"/></svg>',
|
||||||
calendar:
|
calendar:
|
||||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M8 2v4"/><path d="M16 2v4"/><rect width="18" height="18" x="3" y="4" rx="2"/><path d="M3 10h18"/></svg>',
|
'<svg viewBox="0 0 24 24" fill="none"><path d="M8 2v4"/><path d="M16 2v4"/><rect width="18" height="18" x="3" y="4" rx="2"/><path d="M3 10h18"/></svg>',
|
||||||
|
"cloud-sun":
|
||||||
|
'<svg viewBox="0 0 24 24" fill="none"><path d="M12 2v2"/><path d="m4.93 4.93 1.42 1.42"/><path d="M20 12h2"/><path d="m19.07 4.93-1.42 1.42"/><path d="M15.947 12.65a4 4 0 0 0-5.925-4.128"/><path d="M13 22H7a5 5 0 1 1 4.9-6H13a3 3 0 0 1 0 6Z"/></svg>',
|
||||||
"flask-conical":
|
"flask-conical":
|
||||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M14 2v6a2 2 0 0 0 .245.96l5.51 10.08A2 2 0 0 1 18 22H6a2 2 0 0 1-1.755-2.96l5.51-10.08A2 2 0 0 0 10 8V2"/><path d="M6.453 15h11.094"/><path d="M8.5 2h7"/></svg>',
|
'<svg viewBox="0 0 24 24" fill="none"><path d="M14 2v6a2 2 0 0 0 .245.96l5.51 10.08A2 2 0 0 1 18 22H6a2 2 0 0 1-1.755-2.96l5.51-10.08A2 2 0 0 0 10 8V2"/><path d="M6.453 15h11.094"/><path d="M8.5 2h7"/></svg>',
|
||||||
settings:
|
settings:
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { Utils } from "@nativescript/core";
|
||||||
|
|
||||||
|
declare const com: any;
|
||||||
|
|
||||||
|
export type CurrentLocation = {
|
||||||
|
latitude: number;
|
||||||
|
longitude: number;
|
||||||
|
accuracyMeters: number | null;
|
||||||
|
timestampMs: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Resolve one current foreground location using Android's LocationManager. */
|
||||||
|
export function getCurrentLocation(): Promise<CurrentLocation> {
|
||||||
|
if (!global.isAndroid) {
|
||||||
|
return Promise.reject(new Error("Weather location is only available on Android."));
|
||||||
|
}
|
||||||
|
const context = Utils.android.getApplicationContext();
|
||||||
|
if (!context) {
|
||||||
|
return Promise.reject(new Error("Android application context unavailable."));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise<CurrentLocation>((resolve, reject) => {
|
||||||
|
const provider = new com.faceclaw.app.FaceclawLocationProvider(context);
|
||||||
|
let settled = false;
|
||||||
|
const listener = new com.faceclaw.app.FaceclawLocationListener({
|
||||||
|
onLocation: (
|
||||||
|
latitude: number,
|
||||||
|
longitude: number,
|
||||||
|
accuracyMeters: number,
|
||||||
|
timestampMs: number,
|
||||||
|
) => {
|
||||||
|
if (settled) return;
|
||||||
|
settled = true;
|
||||||
|
provider.setListener(null);
|
||||||
|
resolve({
|
||||||
|
latitude: Number(latitude),
|
||||||
|
longitude: Number(longitude),
|
||||||
|
accuracyMeters: Number(accuracyMeters) >= 0 ? Number(accuracyMeters) : null,
|
||||||
|
timestampMs: Number(timestampMs),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onError: (message: string) => {
|
||||||
|
if (settled) return;
|
||||||
|
settled = true;
|
||||||
|
provider.setListener(null);
|
||||||
|
reject(new Error(String(message)));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// Retain the proxy through the Java provider until one terminal callback.
|
||||||
|
provider.setListener(listener);
|
||||||
|
provider.start();
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -23,10 +23,14 @@ export type MediaControllerState = {
|
|||||||
available: boolean;
|
available: boolean;
|
||||||
accessEnabled: boolean;
|
accessEnabled: boolean;
|
||||||
packageName: string;
|
packageName: string;
|
||||||
|
appName: string;
|
||||||
playbackState: MediaPlaybackState;
|
playbackState: MediaPlaybackState;
|
||||||
title: string;
|
title: string;
|
||||||
artist: string;
|
artist: string;
|
||||||
album: string;
|
album: string;
|
||||||
|
positionMs: number;
|
||||||
|
durationMs: number;
|
||||||
|
playbackSpeed: number;
|
||||||
status: string;
|
status: string;
|
||||||
canPlayPause: boolean;
|
canPlayPause: boolean;
|
||||||
canSkipNext: boolean;
|
canSkipNext: boolean;
|
||||||
@@ -37,10 +41,14 @@ const DEFAULT_MEDIA_STATE: MediaControllerState = {
|
|||||||
available: false,
|
available: false,
|
||||||
accessEnabled: false,
|
accessEnabled: false,
|
||||||
packageName: "",
|
packageName: "",
|
||||||
|
appName: "",
|
||||||
playbackState: "idle",
|
playbackState: "idle",
|
||||||
title: "",
|
title: "",
|
||||||
artist: "",
|
artist: "",
|
||||||
album: "",
|
album: "",
|
||||||
|
positionMs: -1,
|
||||||
|
durationMs: -1,
|
||||||
|
playbackSpeed: 0,
|
||||||
status: "Media controller unavailable.",
|
status: "Media controller unavailable.",
|
||||||
canPlayPause: false,
|
canPlayPause: false,
|
||||||
canSkipNext: false,
|
canSkipNext: false,
|
||||||
@@ -52,6 +60,7 @@ export class FaceclawMediaControllerBridge {
|
|||||||
private controller: any | null = null;
|
private controller: any | null = null;
|
||||||
private listenerProxy: any | null = null;
|
private listenerProxy: any | null = null;
|
||||||
private state: MediaControllerState = { ...DEFAULT_MEDIA_STATE };
|
private state: MediaControllerState = { ...DEFAULT_MEDIA_STATE };
|
||||||
|
private stateUpdatedAtMs = Date.now();
|
||||||
private started = false;
|
private started = false;
|
||||||
|
|
||||||
onStateChange(listener: (state: MediaControllerState) => void): () => void {
|
onStateChange(listener: (state: MediaControllerState) => void): () => void {
|
||||||
@@ -61,7 +70,12 @@ export class FaceclawMediaControllerBridge {
|
|||||||
}
|
}
|
||||||
|
|
||||||
snapshot(): MediaControllerState {
|
snapshot(): MediaControllerState {
|
||||||
return { ...this.state };
|
const state = { ...this.state };
|
||||||
|
if (state.playbackState === "playing" && state.positionMs >= 0 && state.playbackSpeed !== 0) {
|
||||||
|
state.positionMs += (Date.now() - this.stateUpdatedAtMs) * state.playbackSpeed;
|
||||||
|
state.positionMs = Math.max(0, state.durationMs > 0 ? Math.min(state.positionMs, state.durationMs) : state.positionMs);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start(): Promise<void> {
|
async start(): Promise<void> {
|
||||||
@@ -103,6 +117,21 @@ export class FaceclawMediaControllerBridge {
|
|||||||
this.controller?.skipToQueueItem(id);
|
this.controller?.skipToQueueItem(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Current Android music-stream volume normalized to 0..100. */
|
||||||
|
getMediaVolumePercent(): number {
|
||||||
|
if (!global.isAndroid) return -1;
|
||||||
|
this.ensureController();
|
||||||
|
if (!this.controller) return -1;
|
||||||
|
return Math.max(-1, Math.min(100, Math.round(Number(this.controller.getMediaVolumePercent()))));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set Android's music-stream volume from a normalized 0..100 value. */
|
||||||
|
setMediaVolumePercent(volumePercent: number): void {
|
||||||
|
if (!global.isAndroid) return;
|
||||||
|
this.ensureController();
|
||||||
|
this.controller?.setMediaVolumePercent(Math.max(0, Math.min(100, Math.round(volumePercent))));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Album art for the current item, grayscale, scaled to fit maxSize; null
|
* Album art for the current item, grayscale, scaled to fit maxSize; null
|
||||||
* when the player provides none.
|
* when the player provides none.
|
||||||
@@ -152,9 +181,13 @@ export class FaceclawMediaControllerBridge {
|
|||||||
onStateChange: (
|
onStateChange: (
|
||||||
playbackState: string,
|
playbackState: string,
|
||||||
packageName: string,
|
packageName: string,
|
||||||
|
appName: string,
|
||||||
title: string,
|
title: string,
|
||||||
artist: string,
|
artist: string,
|
||||||
album: string,
|
album: string,
|
||||||
|
positionMs: number,
|
||||||
|
durationMs: number,
|
||||||
|
playbackSpeed: number,
|
||||||
canPlayPause: boolean,
|
canPlayPause: boolean,
|
||||||
canSkipNext: boolean,
|
canSkipNext: boolean,
|
||||||
canSkipPrevious: boolean,
|
canSkipPrevious: boolean,
|
||||||
@@ -165,15 +198,20 @@ export class FaceclawMediaControllerBridge {
|
|||||||
available: Boolean(title || artist || album || packageName) || String(playbackState) !== "idle",
|
available: Boolean(title || artist || album || packageName) || String(playbackState) !== "idle",
|
||||||
accessEnabled: Boolean(accessEnabled),
|
accessEnabled: Boolean(accessEnabled),
|
||||||
packageName: String(packageName),
|
packageName: String(packageName),
|
||||||
|
appName: String(appName),
|
||||||
playbackState: normalizePlaybackState(String(playbackState)),
|
playbackState: normalizePlaybackState(String(playbackState)),
|
||||||
title: String(title),
|
title: String(title),
|
||||||
artist: String(artist),
|
artist: String(artist),
|
||||||
album: String(album),
|
album: String(album),
|
||||||
|
positionMs: Number(positionMs),
|
||||||
|
durationMs: Number(durationMs),
|
||||||
|
playbackSpeed: Number(playbackSpeed),
|
||||||
status: String(status),
|
status: String(status),
|
||||||
canPlayPause: Boolean(canPlayPause),
|
canPlayPause: Boolean(canPlayPause),
|
||||||
canSkipNext: Boolean(canSkipNext),
|
canSkipNext: Boolean(canSkipNext),
|
||||||
canSkipPrevious: Boolean(canSkipPrevious),
|
canSkipPrevious: Boolean(canSkipPrevious),
|
||||||
};
|
};
|
||||||
|
this.stateUpdatedAtMs = Date.now();
|
||||||
for (const listener of this.stateListeners) {
|
for (const listener of this.stateListeners) {
|
||||||
listener(this.snapshot());
|
listener(this.snapshot());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { Utils } from "@nativescript/core";
|
||||||
|
|
||||||
|
declare const com: any;
|
||||||
|
|
||||||
|
function getContext(): android.content.Context | null {
|
||||||
|
if (!global.isAndroid) return null;
|
||||||
|
return Utils.android.getApplicationContext() ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Schedule the durable Android alarm backing one on-glasses timer. */
|
||||||
|
export function scheduleTimerNotification(timerId: number, triggerAtMs: number, durationLabel: string): void {
|
||||||
|
const context = getContext();
|
||||||
|
if (!context) return;
|
||||||
|
com.faceclaw.app.FaceclawTimerNotifications.schedule(context, timerId, triggerAtMs, durationLabel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Cancel both a pending alarm and any already-posted notification. */
|
||||||
|
export function cancelTimerNotification(timerId: number): void {
|
||||||
|
const context = getContext();
|
||||||
|
if (!context) return;
|
||||||
|
com.faceclaw.app.FaceclawTimerNotifications.cancel(context, timerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Prompt worker-side expiry path; Java deduplicates it against the alarm. */
|
||||||
|
export function fireTimerNotification(timerId: number, durationLabel: string): void {
|
||||||
|
const context = getContext();
|
||||||
|
if (!context) return;
|
||||||
|
com.faceclaw.app.FaceclawTimerNotifications.fireNow(context, timerId, durationLabel);
|
||||||
|
}
|
||||||
@@ -0,0 +1,369 @@
|
|||||||
|
import { hasLocationPermission } from "../g2/android-permissions";
|
||||||
|
import { getCurrentLocation, type CurrentLocation } from "./location";
|
||||||
|
|
||||||
|
export type WeatherPhase = "permission-required" | "locating" | "loading" | "ready" | "error";
|
||||||
|
|
||||||
|
export type CurrentWeather = {
|
||||||
|
temperatureF: number | null;
|
||||||
|
description: string;
|
||||||
|
humidityPercent: number | null;
|
||||||
|
windSpeedMph: number | null;
|
||||||
|
windDirection: string;
|
||||||
|
timestampMs: number | null;
|
||||||
|
observed: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ForecastPeriod = {
|
||||||
|
name: string;
|
||||||
|
startTimeMs: number;
|
||||||
|
temperatureF: number | null;
|
||||||
|
shortForecast: string;
|
||||||
|
detailedForecast: string;
|
||||||
|
precipitationPercent: number | null;
|
||||||
|
windSpeed: string;
|
||||||
|
windDirection: string;
|
||||||
|
isDaytime: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type WeatherState = {
|
||||||
|
phase: WeatherPhase;
|
||||||
|
status: string;
|
||||||
|
locationName: string;
|
||||||
|
current: CurrentWeather | null;
|
||||||
|
forecast: ForecastPeriod[];
|
||||||
|
lastUpdatedMs: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
type NwsPointResponse = {
|
||||||
|
properties?: {
|
||||||
|
forecast?: unknown;
|
||||||
|
forecastHourly?: unknown;
|
||||||
|
observationStations?: unknown;
|
||||||
|
relativeLocation?: {
|
||||||
|
properties?: { city?: unknown; state?: unknown };
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
type NwsForecastResponse = {
|
||||||
|
properties?: {
|
||||||
|
periods?: NwsForecastPeriodResponse[];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
type NwsForecastPeriodResponse = {
|
||||||
|
name?: unknown;
|
||||||
|
startTime?: unknown;
|
||||||
|
temperature?: unknown;
|
||||||
|
temperatureUnit?: unknown;
|
||||||
|
shortForecast?: unknown;
|
||||||
|
detailedForecast?: unknown;
|
||||||
|
probabilityOfPrecipitation?: { value?: unknown };
|
||||||
|
windSpeed?: unknown;
|
||||||
|
windDirection?: unknown;
|
||||||
|
isDaytime?: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
type NwsStationsResponse = {
|
||||||
|
features?: Array<{ id?: unknown }>;
|
||||||
|
};
|
||||||
|
|
||||||
|
type NwsObservationResponse = {
|
||||||
|
properties?: {
|
||||||
|
timestamp?: unknown;
|
||||||
|
textDescription?: unknown;
|
||||||
|
temperature?: NwsMeasure;
|
||||||
|
relativeHumidity?: NwsMeasure;
|
||||||
|
windSpeed?: NwsMeasure;
|
||||||
|
windDirection?: NwsMeasure;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
type NwsMeasure = {
|
||||||
|
value?: unknown;
|
||||||
|
unitCode?: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
const NWS_API_ROOT = "https://api.weather.gov";
|
||||||
|
const NWS_HEADERS = {
|
||||||
|
Accept: "application/geo+json",
|
||||||
|
"User-Agent": "Faceclaw/1.0 (https://github.com/jimrandomh/faceclaw)",
|
||||||
|
};
|
||||||
|
const WEATHER_REFRESH_MS = 30 * 60 * 1000;
|
||||||
|
const FETCH_TIMEOUT_MS = 20_000;
|
||||||
|
const MAX_FORECAST_PERIODS = 14;
|
||||||
|
|
||||||
|
const DEFAULT_STATE: WeatherState = {
|
||||||
|
phase: "permission-required",
|
||||||
|
status: "Location permission is required for local weather.",
|
||||||
|
locationName: "",
|
||||||
|
current: null,
|
||||||
|
forecast: [],
|
||||||
|
lastUpdatedMs: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Shared weather state and periodic refresh, active only while its app is open. */
|
||||||
|
export class WeatherBridge {
|
||||||
|
private readonly listeners = new Set<(state: WeatherState) => void>();
|
||||||
|
private state: WeatherState = cloneState(DEFAULT_STATE);
|
||||||
|
private refreshHandle: ReturnType<typeof setInterval> | null = null;
|
||||||
|
private refreshInFlight: Promise<void> | null = null;
|
||||||
|
|
||||||
|
onStateChange(listener: (state: WeatherState) => void): () => void {
|
||||||
|
this.listeners.add(listener);
|
||||||
|
listener(this.snapshot());
|
||||||
|
return () => this.listeners.delete(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshot(): WeatherState {
|
||||||
|
return cloneState(this.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
start(): void {
|
||||||
|
if (!this.refreshHandle) {
|
||||||
|
this.refreshHandle = setInterval(() => void this.refreshNow(), WEATHER_REFRESH_MS);
|
||||||
|
}
|
||||||
|
void this.refreshNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
stop(): void {
|
||||||
|
if (this.refreshHandle) {
|
||||||
|
clearInterval(this.refreshHandle);
|
||||||
|
this.refreshHandle = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async refreshNow(): Promise<void> {
|
||||||
|
if (this.refreshInFlight) return this.refreshInFlight;
|
||||||
|
if (!hasLocationPermission()) {
|
||||||
|
this.state = cloneState(DEFAULT_STATE);
|
||||||
|
this.emit();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.refreshInFlight = this.refresh();
|
||||||
|
try {
|
||||||
|
await this.refreshInFlight;
|
||||||
|
} finally {
|
||||||
|
this.refreshInFlight = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async refresh(): Promise<void> {
|
||||||
|
try {
|
||||||
|
this.state = { ...this.state, phase: "locating", status: "Getting current location..." };
|
||||||
|
this.emit();
|
||||||
|
const location = await getCurrentLocation();
|
||||||
|
|
||||||
|
this.state = { ...this.state, phase: "loading", status: "Loading National Weather Service data..." };
|
||||||
|
this.emit();
|
||||||
|
const weather = await loadNwsWeather(location);
|
||||||
|
this.state = {
|
||||||
|
phase: "ready",
|
||||||
|
status: "Weather updated.",
|
||||||
|
locationName: weather.locationName,
|
||||||
|
current: weather.current,
|
||||||
|
forecast: weather.forecast,
|
||||||
|
lastUpdatedMs: Date.now(),
|
||||||
|
};
|
||||||
|
this.emit();
|
||||||
|
} catch (error) {
|
||||||
|
const message = friendlyWeatherError(error);
|
||||||
|
console.warn(`weather refresh failed: ${message}`);
|
||||||
|
this.state = {
|
||||||
|
...this.state,
|
||||||
|
phase: "error",
|
||||||
|
status: message,
|
||||||
|
};
|
||||||
|
this.emit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private emit(): void {
|
||||||
|
const snapshot = this.snapshot();
|
||||||
|
for (const listener of this.listeners) listener(snapshot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadNwsWeather(location: CurrentLocation): Promise<{
|
||||||
|
locationName: string;
|
||||||
|
current: CurrentWeather;
|
||||||
|
forecast: ForecastPeriod[];
|
||||||
|
}> {
|
||||||
|
// NWS recommends no more than four decimals for its point lookup.
|
||||||
|
const latitude = location.latitude.toFixed(4);
|
||||||
|
const longitude = location.longitude.toFixed(4);
|
||||||
|
const point = await fetchNwsJson<NwsPointResponse>(`${NWS_API_ROOT}/points/${latitude},${longitude}`);
|
||||||
|
const properties = point.properties;
|
||||||
|
const forecastUrl = stringValue(properties?.forecast);
|
||||||
|
const hourlyUrl = stringValue(properties?.forecastHourly);
|
||||||
|
const stationsUrl = stringValue(properties?.observationStations);
|
||||||
|
if (!forecastUrl) throw new Error("NWS did not provide a forecast for this location.");
|
||||||
|
|
||||||
|
const [forecastResponse, hourlyResponse, observation] = await Promise.all([
|
||||||
|
fetchNwsJson<NwsForecastResponse>(forecastUrl),
|
||||||
|
hourlyUrl ? fetchNwsJson<NwsForecastResponse>(hourlyUrl).catch(() => null) : Promise.resolve(null),
|
||||||
|
stationsUrl ? loadLatestObservation(stationsUrl).catch(() => null) : Promise.resolve(null),
|
||||||
|
]);
|
||||||
|
const forecast = (forecastResponse.properties?.periods ?? [])
|
||||||
|
.map(normalizeForecastPeriod)
|
||||||
|
.filter((period): period is ForecastPeriod => period !== null)
|
||||||
|
.slice(0, MAX_FORECAST_PERIODS);
|
||||||
|
if (!forecast.length) throw new Error("NWS returned an empty forecast.");
|
||||||
|
|
||||||
|
const hourly = (hourlyResponse?.properties?.periods ?? [])
|
||||||
|
.map(normalizeForecastPeriod)
|
||||||
|
.find((period): period is ForecastPeriod => period !== null);
|
||||||
|
|
||||||
|
return {
|
||||||
|
locationName: pointLocationName(point, latitude, longitude),
|
||||||
|
current: observation ? normalizeObservation(observation, hourly) : currentFromHourly(hourly, forecast[0]!),
|
||||||
|
forecast,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadLatestObservation(stationsUrl: string): Promise<NwsObservationResponse | null> {
|
||||||
|
const stations = await fetchNwsJson<NwsStationsResponse>(stationsUrl);
|
||||||
|
const stationUrl = stringValue(stations.features?.[0]?.id);
|
||||||
|
if (!stationUrl) return null;
|
||||||
|
return fetchNwsJson<NwsObservationResponse>(`${stationUrl}/observations/latest`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchNwsJson<T>(url: string): Promise<T> {
|
||||||
|
const request = fetch(url, { headers: NWS_HEADERS });
|
||||||
|
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
const timeout = new Promise<never>((_resolve, reject) => {
|
||||||
|
timeoutHandle = setTimeout(() => reject(new Error("Weather request timed out.")), FETCH_TIMEOUT_MS);
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const response = await Promise.race([request, timeout]);
|
||||||
|
if (!response.ok) {
|
||||||
|
if (response.status === 404 && url.includes("/points/")) {
|
||||||
|
throw new Error("This location is outside National Weather Service coverage.");
|
||||||
|
}
|
||||||
|
throw new Error(`National Weather Service request failed (HTTP ${response.status}).`);
|
||||||
|
}
|
||||||
|
return (await response.json()) as T;
|
||||||
|
} finally {
|
||||||
|
if (timeoutHandle) clearTimeout(timeoutHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeObservation(
|
||||||
|
response: NwsObservationResponse,
|
||||||
|
hourly: ForecastPeriod | undefined,
|
||||||
|
): CurrentWeather {
|
||||||
|
const properties = response.properties;
|
||||||
|
const temperatureF = convertTemperatureToF(properties?.temperature);
|
||||||
|
const windSpeedMph = convertSpeedToMph(properties?.windSpeed);
|
||||||
|
const windDegrees = finiteNumber(properties?.windDirection?.value);
|
||||||
|
return {
|
||||||
|
temperatureF: temperatureF ?? hourly?.temperatureF ?? null,
|
||||||
|
description: stringValue(properties?.textDescription) || hourly?.shortForecast || "Current conditions",
|
||||||
|
humidityPercent: finiteNumber(properties?.relativeHumidity?.value),
|
||||||
|
windSpeedMph,
|
||||||
|
windDirection: windDegrees === null ? hourly?.windDirection ?? "" : degreesToCompass(windDegrees),
|
||||||
|
timestampMs: timestampValue(properties?.timestamp),
|
||||||
|
observed: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function currentFromHourly(hourly: ForecastPeriod | undefined, fallback: ForecastPeriod): CurrentWeather {
|
||||||
|
const source = hourly ?? fallback;
|
||||||
|
return {
|
||||||
|
temperatureF: source.temperatureF,
|
||||||
|
description: source.shortForecast || "Current forecast",
|
||||||
|
humidityPercent: null,
|
||||||
|
windSpeedMph: firstNumberInText(source.windSpeed),
|
||||||
|
windDirection: source.windDirection,
|
||||||
|
timestampMs: source.startTimeMs || null,
|
||||||
|
observed: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeForecastPeriod(value: NwsForecastPeriodResponse): ForecastPeriod | null {
|
||||||
|
if (!value || typeof value !== "object") return null;
|
||||||
|
const name = stringValue(value.name);
|
||||||
|
const temperature = finiteNumber(value.temperature);
|
||||||
|
const unit = stringValue(value.temperatureUnit).toUpperCase();
|
||||||
|
return {
|
||||||
|
name: name || "Forecast",
|
||||||
|
startTimeMs: timestampValue(value.startTime) ?? 0,
|
||||||
|
temperatureF: temperature === null ? null : unit === "C" ? temperature * 9 / 5 + 32 : temperature,
|
||||||
|
shortForecast: stringValue(value.shortForecast),
|
||||||
|
detailedForecast: stringValue(value.detailedForecast),
|
||||||
|
precipitationPercent: finiteNumber(value.probabilityOfPrecipitation?.value),
|
||||||
|
windSpeed: stringValue(value.windSpeed),
|
||||||
|
windDirection: stringValue(value.windDirection),
|
||||||
|
isDaytime: Boolean(value.isDaytime),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertTemperatureToF(measure: NwsMeasure | undefined): number | null {
|
||||||
|
const value = finiteNumber(measure?.value);
|
||||||
|
if (value === null) return null;
|
||||||
|
const unit = stringValue(measure?.unitCode).toLowerCase();
|
||||||
|
if (unit.includes("degc")) return value * 9 / 5 + 32;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertSpeedToMph(measure: NwsMeasure | undefined): number | null {
|
||||||
|
const value = finiteNumber(measure?.value);
|
||||||
|
if (value === null) return null;
|
||||||
|
const unit = stringValue(measure?.unitCode).toLowerCase();
|
||||||
|
if (unit.includes("km_h")) return value * 0.621371;
|
||||||
|
if (unit.includes("m_s")) return value * 2.23694;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function pointLocationName(point: NwsPointResponse, latitude: string, longitude: string): string {
|
||||||
|
const relative = point.properties?.relativeLocation?.properties;
|
||||||
|
const city = stringValue(relative?.city);
|
||||||
|
const state = stringValue(relative?.state);
|
||||||
|
return [city, state].filter(Boolean).join(", ") || `${latitude}, ${longitude}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function degreesToCompass(value: number): string {
|
||||||
|
const directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
||||||
|
return directions[Math.round((((value % 360) + 360) % 360) / 45) % directions.length]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
function firstNumberInText(value: string): number | null {
|
||||||
|
const match = value.match(/\d+(?:\.\d+)?/);
|
||||||
|
return match ? Number(match[0]) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function finiteNumber(value: unknown): number | null {
|
||||||
|
if (value === null || value === undefined || typeof value === "boolean") return null;
|
||||||
|
if (typeof value === "string" && !value.trim()) return null;
|
||||||
|
const number = typeof value === "number" ? value : Number(value);
|
||||||
|
return Number.isFinite(number) ? number : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function timestampValue(value: unknown): number | null {
|
||||||
|
if (typeof value !== "string") return null;
|
||||||
|
const timestamp = Date.parse(value);
|
||||||
|
return Number.isFinite(timestamp) ? timestamp : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function stringValue(value: unknown): string {
|
||||||
|
return typeof value === "string" ? value.trim() : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function friendlyWeatherError(error: unknown): string {
|
||||||
|
const message = (error as Error)?.message || String(error);
|
||||||
|
if (/network request failed|failed to fetch|unable to resolve host/i.test(message)) {
|
||||||
|
return "Couldn't reach the National Weather Service. Check the phone's connection and retry.";
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cloneState(state: WeatherState): WeatherState {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
current: state.current ? { ...state.current } : null,
|
||||||
|
forecast: state.forecast.map((period) => ({ ...period })),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const weatherBridge = new WeatherBridge();
|
||||||
@@ -20,14 +20,14 @@ export class TranscribeLayer implements Layer {
|
|||||||
private unsubscribeTranscript: (() => void) | null = null;
|
private unsubscribeTranscript: (() => void) | null = null;
|
||||||
private unsubscribeStatus: (() => void) | null = null;
|
private unsubscribeStatus: (() => void) | null = null;
|
||||||
|
|
||||||
start(ctx: LayerContext): void {
|
start(requestRender: () => void): void {
|
||||||
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => {
|
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => {
|
||||||
this.onTranscript(event);
|
this.onTranscript(event);
|
||||||
ctx.actions.requestRender();
|
requestRender();
|
||||||
});
|
});
|
||||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||||
this.status = state.status;
|
this.status = state.status;
|
||||||
ctx.actions.requestRender();
|
requestRender();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,202 @@
|
|||||||
|
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
|
||||||
|
import { GrayImage } from "../../graphics/image";
|
||||||
|
import { wrapText } from "../../graphics/textwrap";
|
||||||
|
import { clamp } from "../../util/numeric-util";
|
||||||
|
import { type ForecastPeriod, type WeatherState } from "../../native/weather";
|
||||||
|
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures";
|
||||||
|
import { type DashboardInputEvent, type Layer, type LayerContext } from "../layers";
|
||||||
|
import { drawSelectionHighlight } from "../menu";
|
||||||
|
|
||||||
|
const PAGE_X = 18;
|
||||||
|
const HEADER_Y = 8;
|
||||||
|
const CURRENT_TOP = 34;
|
||||||
|
const CURRENT_TEXT_X = 112;
|
||||||
|
const FORECAST_HEADER_Y = 105;
|
||||||
|
const FORECAST_TOP = 124;
|
||||||
|
const FORECAST_ROW_HEIGHT = 23;
|
||||||
|
const FOOTER_HEIGHT = 18;
|
||||||
|
const FORECAST_NAME_WIDTH = 92;
|
||||||
|
const FORECAST_TEMP_X = 126;
|
||||||
|
const FORECAST_PRECIP_X = 184;
|
||||||
|
const FORECAST_SUMMARY_X = 244;
|
||||||
|
|
||||||
|
/** Weather's current-conditions summary and scrollable 12-hour forecast. */
|
||||||
|
export class WeatherLayer implements Layer {
|
||||||
|
private selectedIndex = 0;
|
||||||
|
private scrollRow = 0;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly state: () => WeatherState,
|
||||||
|
private readonly requestUpdate: () => void,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
paint(ctx: LayerContext): GrayImage {
|
||||||
|
const font = getDefaultSmallFont();
|
||||||
|
const { width, height } = ctx.stack.getBaseSize();
|
||||||
|
const image = new GrayImage(width, height, 0);
|
||||||
|
const weather = this.state();
|
||||||
|
image.drawText(font, PAGE_X, HEADER_Y, "Weather", 225);
|
||||||
|
if (weather.locationName) {
|
||||||
|
image.drawText(
|
||||||
|
font,
|
||||||
|
width - PAGE_X - font.measureText(truncate(font, weather.locationName, 220)),
|
||||||
|
HEADER_Y,
|
||||||
|
truncate(font, weather.locationName, 220),
|
||||||
|
150,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (weather.phase === "permission-required") {
|
||||||
|
this.drawMessage(
|
||||||
|
image,
|
||||||
|
font,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
"Allow approximate location on your phone to get local weather.",
|
||||||
|
`${GESTURE_CLICK} request ${GESTURE_DOUBLE_CLICK} back`,
|
||||||
|
);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
if (weather.phase === "locating" || weather.phase === "loading") {
|
||||||
|
this.drawMessage(image, font, width, height, weather.status, `${GESTURE_DOUBLE_CLICK} back`);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
if (weather.phase === "error") {
|
||||||
|
this.drawMessage(
|
||||||
|
image,
|
||||||
|
font,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
weather.status,
|
||||||
|
`${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`,
|
||||||
|
);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.drawCurrent(image, weather, width);
|
||||||
|
this.drawForecast(image, weather.forecast, ctx, width, height);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleInput(event: DashboardInputEvent): void {
|
||||||
|
const weather = this.state();
|
||||||
|
if (event.type === "click") {
|
||||||
|
this.requestUpdate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!weather.forecast.length || weather.phase !== "ready") return;
|
||||||
|
if (event.type === "scroll-up") {
|
||||||
|
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||||
|
} else if (event.type === "scroll-down") {
|
||||||
|
this.selectedIndex = Math.min(weather.forecast.length - 1, this.selectedIndex + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private drawCurrent(image: GrayImage, weather: WeatherState, width: number): void {
|
||||||
|
const current = weather.current;
|
||||||
|
if (!current) return;
|
||||||
|
const large = getDefaultLargeFont();
|
||||||
|
const medium = getDefaultMediumFont();
|
||||||
|
const small = getDefaultSmallFont();
|
||||||
|
const temperature = current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°F`;
|
||||||
|
image.drawText(large, PAGE_X, CURRENT_TOP + 8, temperature, 245);
|
||||||
|
image.drawText(
|
||||||
|
medium,
|
||||||
|
CURRENT_TEXT_X,
|
||||||
|
CURRENT_TOP + 5,
|
||||||
|
truncate(medium, current.description || "Current conditions", width - CURRENT_TEXT_X - PAGE_X),
|
||||||
|
220,
|
||||||
|
);
|
||||||
|
|
||||||
|
const details: string[] = [];
|
||||||
|
if (current.humidityPercent !== null) details.push(`Humidity ${Math.round(current.humidityPercent)}%`);
|
||||||
|
if (current.windSpeedMph !== null) {
|
||||||
|
details.push(`Wind ${current.windDirection ? `${current.windDirection} ` : ""}${Math.round(current.windSpeedMph)} mph`);
|
||||||
|
}
|
||||||
|
image.drawText(small, CURRENT_TEXT_X, CURRENT_TOP + 35, details.join(" ") || "Current forecast", 170);
|
||||||
|
const source = current.observed ? "Observed" : "Forecast";
|
||||||
|
const age = current.timestampMs ? formatAge(Date.now() - current.timestampMs) : "";
|
||||||
|
image.drawText(small, CURRENT_TEXT_X, CURRENT_TOP + 54, `${source}${age ? ` ${age}` : ""}`, 105);
|
||||||
|
image.drawLine(PAGE_X, FORECAST_HEADER_Y - 7, width - PAGE_X, FORECAST_HEADER_Y - 7, 40);
|
||||||
|
}
|
||||||
|
|
||||||
|
private drawForecast(
|
||||||
|
image: GrayImage,
|
||||||
|
forecast: ForecastPeriod[],
|
||||||
|
ctx: LayerContext,
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
): void {
|
||||||
|
const font = getDefaultSmallFont();
|
||||||
|
if (!forecast.length) {
|
||||||
|
image.drawText(font, PAGE_X, FORECAST_TOP, "No upcoming forecast periods.", 160);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.selectedIndex = clamp(this.selectedIndex, 0, forecast.length - 1);
|
||||||
|
const visibleRows = Math.max(1, Math.floor((height - FOOTER_HEIGHT - FORECAST_TOP) / FORECAST_ROW_HEIGHT));
|
||||||
|
if (this.selectedIndex < this.scrollRow) {
|
||||||
|
this.scrollRow = this.selectedIndex;
|
||||||
|
} else if (this.selectedIndex >= this.scrollRow + visibleRows) {
|
||||||
|
this.scrollRow = this.selectedIndex - visibleRows + 1;
|
||||||
|
}
|
||||||
|
this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, forecast.length - visibleRows));
|
||||||
|
|
||||||
|
image.drawText(font, PAGE_X, FORECAST_HEADER_Y, "Upcoming", 150);
|
||||||
|
image.drawText(font, FORECAST_TEMP_X, FORECAST_HEADER_Y, "Temp", 105);
|
||||||
|
image.drawText(font, FORECAST_PRECIP_X, FORECAST_HEADER_Y, "Rain", 105);
|
||||||
|
image.drawText(font, width - PAGE_X - font.measureText(`${this.selectedIndex + 1}/${forecast.length}`), FORECAST_HEADER_Y, `${this.selectedIndex + 1}/${forecast.length}`, 105);
|
||||||
|
|
||||||
|
const last = Math.min(forecast.length, this.scrollRow + visibleRows);
|
||||||
|
for (let index = this.scrollRow; index < last; index++) {
|
||||||
|
const period = forecast[index]!;
|
||||||
|
const y = FORECAST_TOP + (index - this.scrollRow) * FORECAST_ROW_HEIGHT;
|
||||||
|
const selected = index === this.selectedIndex;
|
||||||
|
if (selected) {
|
||||||
|
drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), FORECAST_ROW_HEIGHT - 2, ctx.stack.isFocused(), 5);
|
||||||
|
}
|
||||||
|
const shade = selected ? 245 : 190;
|
||||||
|
image.drawText(font, PAGE_X, y + 2, truncate(font, period.name, FORECAST_NAME_WIDTH), shade);
|
||||||
|
image.drawText(font, FORECAST_TEMP_X, y + 2, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade);
|
||||||
|
image.drawText(font, FORECAST_PRECIP_X, y + 2, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155);
|
||||||
|
image.drawText(font, FORECAST_SUMMARY_X, y + 2, truncate(font, period.shortForecast, width - FORECAST_SUMMARY_X - PAGE_X), selected ? 220 : 165);
|
||||||
|
}
|
||||||
|
|
||||||
|
image.drawText(
|
||||||
|
font,
|
||||||
|
PAGE_X,
|
||||||
|
height - 15,
|
||||||
|
`${GESTURE_SCROLL} forecast ${GESTURE_CLICK} refresh ${GESTURE_DOUBLE_CLICK} back`,
|
||||||
|
105,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private drawMessage(
|
||||||
|
image: GrayImage,
|
||||||
|
font: BdfFont,
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
message: string,
|
||||||
|
footer: string,
|
||||||
|
): void {
|
||||||
|
const lines = wrapText(font, message, width - 2 * (PAGE_X + 6));
|
||||||
|
const top = Math.max(52, Math.round((height - lines.length * 15) / 2) - 8);
|
||||||
|
for (let index = 0; index < lines.length; index++) {
|
||||||
|
image.drawText(font, PAGE_X + 6, top + index * 15, lines[index]!, 185);
|
||||||
|
}
|
||||||
|
image.drawText(font, PAGE_X, height - 20, footer, 110);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatAge(ageMs: number): string {
|
||||||
|
const minutes = Math.max(0, Math.round(ageMs / 60_000));
|
||||||
|
if (minutes < 1) return "just now";
|
||||||
|
if (minutes < 60) return `${minutes}m ago`;
|
||||||
|
return `${Math.round(minutes / 60)}h ago`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function truncate(font: BdfFont, text: string, maxWidth: number): string {
|
||||||
|
if (font.measureText(text) <= maxWidth) return text;
|
||||||
|
let out = text;
|
||||||
|
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) out = out.slice(0, -1);
|
||||||
|
return `${out}...`;
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ export type NightscoutSettings = {
|
|||||||
export type BatteryDisplayMode = "icon" | "percentage";
|
export type BatteryDisplayMode = "icon" | "percentage";
|
||||||
export type TimeFormat = "24h" | "12h";
|
export type TimeFormat = "24h" | "12h";
|
||||||
export type ScreenTimeoutSetting = "15s" | "30s" | "1m" | "never";
|
export type ScreenTimeoutSetting = "15s" | "30s" | "1m" | "never";
|
||||||
|
export type WakeWordAction = "voice-input" | "off" | "turn-screen-on";
|
||||||
|
|
||||||
type ConfigSettingOptions<TValue, TId extends string> = {
|
type ConfigSettingOptions<TValue, TId extends string> = {
|
||||||
id: TId;
|
id: TId;
|
||||||
@@ -243,13 +244,28 @@ const voiceProviderLabels: Record<VoiceProvider, string> = {
|
|||||||
|
|
||||||
export const voiceProviderSetting = new ConfigSettingEnum<VoiceProvider>({
|
export const voiceProviderSetting = new ConfigSettingEnum<VoiceProvider>({
|
||||||
id: "voice-provider",
|
id: "voice-provider",
|
||||||
label: "Provider",
|
label: "Transcription Provider",
|
||||||
storageKey: "voice.provider",
|
storageKey: "voice.provider",
|
||||||
defaultValue: "onboard",
|
defaultValue: "onboard",
|
||||||
values: ["onboard", "elevenlabs", "whisper"],
|
values: ["onboard", "elevenlabs", "whisper"],
|
||||||
formatValue: (value) => voiceProviderLabels[value] ?? value,
|
formatValue: (value) => voiceProviderLabels[value] ?? value,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const wakeWordActionLabels: Record<WakeWordAction, string> = {
|
||||||
|
"voice-input": "Voice Input",
|
||||||
|
off: "Off",
|
||||||
|
"turn-screen-on": "Turn Screen On",
|
||||||
|
};
|
||||||
|
|
||||||
|
export const wakeWordActionSetting = new ConfigSettingEnum<WakeWordAction>({
|
||||||
|
id: "wake-word-action",
|
||||||
|
label: "Wakeword Action (\"Hey Even\")",
|
||||||
|
storageKey: "voice.wakeWordAction",
|
||||||
|
defaultValue: "voice-input",
|
||||||
|
values: ["voice-input", "off", "turn-screen-on"],
|
||||||
|
formatValue: (value) => wakeWordActionLabels[value] ?? value,
|
||||||
|
});
|
||||||
|
|
||||||
export const saveVoiceRecordingsSetting = new ConfigSettingBoolean({
|
export const saveVoiceRecordingsSetting = new ConfigSettingBoolean({
|
||||||
id: "save-voice-recordings",
|
id: "save-voice-recordings",
|
||||||
label: "Save voice recordings",
|
label: "Save voice recordings",
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
nightscoutSiteUrlSetting,
|
nightscoutSiteUrlSetting,
|
||||||
nightscoutApiTokenSetting,
|
nightscoutApiTokenSetting,
|
||||||
screenTimeoutSetting,
|
screenTimeoutSetting,
|
||||||
|
wakeWordActionSetting,
|
||||||
} from "../dashboard-settings";
|
} from "../dashboard-settings";
|
||||||
import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from "./settings-panel";
|
import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from "./settings-panel";
|
||||||
|
|
||||||
@@ -49,7 +50,10 @@ function settingsSections(): SettingsSection[] {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Voice",
|
label: "Voice",
|
||||||
items: [enumSettingMenuItem(voiceProviderSetting)],
|
items: [
|
||||||
|
enumSettingMenuItem(wakeWordActionSetting),
|
||||||
|
enumSettingMenuItem(voiceProviderSetting),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "API Keys",
|
label: "API Keys",
|
||||||
@@ -118,13 +122,13 @@ function renderAbout(args: { image: import("../../graphics/image").GrayImage; x:
|
|||||||
}
|
}
|
||||||
const textX = logo ? x + logo.width + 12 : x;
|
const textX = logo ? x + logo.width + 12 : x;
|
||||||
image.drawText(font, textX, y + 8, "Faceclaw", 220);
|
image.drawText(font, textX, y + 8, "Faceclaw", 220);
|
||||||
image.drawText(font, textX, y + 24, "Dashboard prototype", 170);
|
image.drawText(font, textX, y + 24, "v0.2.0", 170);
|
||||||
image.drawTextWrapped({
|
image.drawTextWrapped({
|
||||||
font,
|
font,
|
||||||
x,
|
x,
|
||||||
y: y + 64,
|
y: y + 64,
|
||||||
width,
|
width,
|
||||||
text: "By James Babcock. Distributed under the GNU General Public License, version 3. Version 0.1.0. Too much of an early janky development prototype to have proper numbered releases.",
|
text: "By James Babcock. Distributed under the GNU General Public License, version 3.",
|
||||||
value: 170,
|
value: 170,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
+2
-2
@@ -13,8 +13,8 @@ export type DashboardInputEvent =
|
|||||||
/**
|
/**
|
||||||
* The on-glasses "Hey Even" wakeword fired. Delivered on sid 0x07 by the
|
* The on-glasses "Hey Even" wakeword fired. Delivered on sid 0x07 by the
|
||||||
* stock firmware regardless of CFW; the CFW additionally suppresses the stock
|
* stock firmware regardless of CFW; the CFW additionally suppresses the stock
|
||||||
* Even AI app so this is ours to handle. Unlike every other input event this
|
* Even AI app so this is ours to handle. Its configured action may be applied
|
||||||
* one is honoured while the screen is off.
|
* while the screen is off, unlike ordinary input events.
|
||||||
*/
|
*/
|
||||||
| { type: "wakeword" }
|
| { type: "wakeword" }
|
||||||
| { type: "unknown"; kind: string; eventSource: number; eventType: number };
|
| { type: "unknown"; kind: string; eventSource: number; eventType: number };
|
||||||
|
|||||||
@@ -181,6 +181,11 @@ class LauncherGridLayer implements Layer {
|
|||||||
* new window.
|
* new window.
|
||||||
*/
|
*/
|
||||||
export function createLauncherWindow(options: LauncherOptions): ShellWindow {
|
export function createLauncherWindow(options: LauncherOptions): ShellWindow {
|
||||||
|
const sortedOptions: LauncherOptions = {
|
||||||
|
...options,
|
||||||
|
apps: [...options.apps].sort((a, b) => a.label.localeCompare(b.label)),
|
||||||
|
};
|
||||||
|
|
||||||
return createInProcessWindow({
|
return createInProcessWindow({
|
||||||
appId: "launcher",
|
appId: "launcher",
|
||||||
windowId: LAUNCHER_WINDOW_ID,
|
windowId: LAUNCHER_WINDOW_ID,
|
||||||
@@ -191,7 +196,7 @@ export function createLauncherWindow(options: LauncherOptions): ShellWindow {
|
|||||||
actions: options.actions,
|
actions: options.actions,
|
||||||
// Not wrapped in YieldAtRootLayer: the grid handles double-click itself to
|
// Not wrapped in YieldAtRootLayer: the grid handles double-click itself to
|
||||||
// back out of item selection before yielding to the sidebar.
|
// back out of item selection before yielding to the sidebar.
|
||||||
baseLayer: new LauncherGridLayer(options),
|
baseLayer: new LauncherGridLayer(sortedOptions),
|
||||||
submitFrame: options.submitFrame,
|
submitFrame: options.submitFrame,
|
||||||
setSurfaceVisible: options.setSurfaceVisible,
|
setSurfaceVisible: options.setSurfaceVisible,
|
||||||
}).window;
|
}).window;
|
||||||
|
|||||||
+270
-73
@@ -1,15 +1,21 @@
|
|||||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
|
||||||
import { GrayImage } from "../../graphics/image";
|
import { GrayImage } from "../../graphics/image";
|
||||||
import { wrapText } from "../../graphics/textwrap";
|
import { wrapText } from "../../graphics/textwrap";
|
||||||
import { clamp } from "../../util/numeric-util";
|
import { clamp } from "../../util/numeric-util";
|
||||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures";
|
import {
|
||||||
|
GESTURE_CLICK,
|
||||||
|
GESTURE_DOUBLE_CLICK,
|
||||||
|
GESTURE_SCROLL,
|
||||||
|
GESTURE_SCROLL_DOWN,
|
||||||
|
GESTURE_SCROLL_UP,
|
||||||
|
} from "../gestures";
|
||||||
import { drawSelectionHighlight } from "../menu";
|
import { drawSelectionHighlight } from "../menu";
|
||||||
import {
|
import {
|
||||||
mediaControllerBridge,
|
mediaControllerBridge,
|
||||||
type MediaControllerState,
|
type MediaControllerState,
|
||||||
type MediaQueueItem,
|
type MediaQueueItem,
|
||||||
} from "../../native/media-controller";
|
} from "../../native/media-controller";
|
||||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
|
import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../layers";
|
||||||
import {
|
import {
|
||||||
createInProcessWindow,
|
createInProcessWindow,
|
||||||
YieldAtRootLayer,
|
YieldAtRootLayer,
|
||||||
@@ -22,18 +28,25 @@ export const MUSIC_SURFACE_ID = "window:music";
|
|||||||
|
|
||||||
const ART_SIZE = 96;
|
const ART_SIZE = 96;
|
||||||
const ART_X = 22;
|
const ART_X = 22;
|
||||||
const ART_Y = 40;
|
const ART_Y = 8;
|
||||||
const META_X = ART_X + ART_SIZE + 14;
|
const META_X = ART_X + ART_SIZE + 14;
|
||||||
const LIST_TOP = 150;
|
const LIST_TOP = 120;
|
||||||
const ROW_HEIGHT = 16;
|
const ROW_HEIGHT = 16;
|
||||||
const LIST_X = 22;
|
const ACTION_X = 22;
|
||||||
|
const ACTION_WIDTH = 154;
|
||||||
|
const COLUMN_DIVIDER_X = 190;
|
||||||
|
const QUEUE_X = 204;
|
||||||
const FOOTER_HEIGHT = 20;
|
const FOOTER_HEIGHT = 20;
|
||||||
|
const PLAYLIST_ACTION_INDEX = 4;
|
||||||
|
|
||||||
type MusicRow =
|
type MusicAction =
|
||||||
| { kind: "play-pause"; label: string; enabled: boolean }
|
| { kind: "play-pause"; label: string; enabled: boolean }
|
||||||
| { kind: "previous"; label: string; enabled: boolean }
|
| { kind: "previous"; label: string; enabled: boolean }
|
||||||
| { kind: "next"; label: string; enabled: boolean }
|
| { kind: "next"; label: string; enabled: boolean }
|
||||||
| { kind: "queue-item"; label: string; enabled: true; item: MediaQueueItem };
|
| { kind: "volume"; label: string; enabled: boolean }
|
||||||
|
| { kind: "playlist"; label: string; enabled: boolean };
|
||||||
|
|
||||||
|
type FocusColumn = "actions" | "playlist";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Music controller app: metadata + album art + transport controls for the
|
* Music controller app: metadata + album art + transport controls for the
|
||||||
@@ -41,21 +54,23 @@ type MusicRow =
|
|||||||
* player's queue when it exposes one (scroll to a track, click to jump).
|
* player's queue when it exposes one (scroll to a track, click to jump).
|
||||||
*/
|
*/
|
||||||
class MusicAppLayer implements Layer {
|
class MusicAppLayer implements Layer {
|
||||||
private selectedIndex = 0;
|
private focusColumn: FocusColumn = "actions";
|
||||||
private scrollRow = 0;
|
private selectedActionIndex = 0;
|
||||||
|
private selectedQueueIndex = 0;
|
||||||
|
private queueScrollRow = 0;
|
||||||
private art: GrayImage | null = null;
|
private art: GrayImage | null = null;
|
||||||
private artKey = "";
|
private artKey = "";
|
||||||
|
|
||||||
|
isPlaylistFocused(): boolean {
|
||||||
|
return this.focusColumn === "playlist";
|
||||||
|
}
|
||||||
|
|
||||||
paint(ctx: LayerContext): GrayImage {
|
paint(ctx: LayerContext): GrayImage {
|
||||||
const font = getDefaultSmallFont();
|
const font = getDefaultSmallFont();
|
||||||
const { width, height } = ctx.stack.getBaseSize();
|
const { width, height } = ctx.stack.getBaseSize();
|
||||||
const image = new GrayImage(width, height, 0);
|
const image = new GrayImage(width, height, 0);
|
||||||
const media = mediaControllerBridge.snapshot();
|
const media = mediaControllerBridge.snapshot();
|
||||||
|
|
||||||
image.drawText(font, 20, 8, "Music", 220);
|
|
||||||
const status = playbackLabel(media);
|
|
||||||
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
|
|
||||||
|
|
||||||
if (!media.accessEnabled) {
|
if (!media.accessEnabled) {
|
||||||
const lines = wrapText(
|
const lines = wrapText(
|
||||||
font,
|
font,
|
||||||
@@ -63,15 +78,15 @@ class MusicAppLayer implements Layer {
|
|||||||
width - 48,
|
width - 48,
|
||||||
);
|
);
|
||||||
for (let index = 0; index < lines.length; index++) {
|
for (let index = 0; index < lines.length; index++) {
|
||||||
image.drawText(font, 24, 44 + index * 14, lines[index]!, 180);
|
image.drawText(font, 24, 16 + index * 14, lines[index]!, 180);
|
||||||
}
|
}
|
||||||
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open settings ${GESTURE_DOUBLE_CLICK} back`, 110);
|
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open settings ${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!media.available) {
|
if (!media.available) {
|
||||||
image.drawText(font, 24, 44, "No active media session.", 180);
|
image.drawText(font, 24, 16, "No active media session.", 180);
|
||||||
image.drawText(font, 24, 62, "Start playback in another app on the phone.", 150);
|
image.drawText(font, 24, 34, "Start playback in another app on the phone.", 150);
|
||||||
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
@@ -87,36 +102,95 @@ class MusicAppLayer implements Layer {
|
|||||||
if (media.album) {
|
if (media.album) {
|
||||||
image.drawText(font, META_X, ART_Y + 52, truncate(font, media.album, metaWidth), 150);
|
image.drawText(font, META_X, ART_Y + 52, truncate(font, media.album, metaWidth), 150);
|
||||||
}
|
}
|
||||||
image.drawText(font, META_X, ART_Y + 70, media.packageName, 110);
|
image.drawText(font, META_X, ART_Y + 68, truncate(font, media.appName || media.packageName, metaWidth), 110);
|
||||||
|
this.drawProgress(image, media, metaWidth);
|
||||||
|
|
||||||
const rows = this.buildRows(media);
|
const queue = mediaControllerBridge.getQueue();
|
||||||
this.selectedIndex = clamp(this.selectedIndex, 0, Math.max(0, rows.length - 1));
|
const actions = this.buildActions(media, queue);
|
||||||
|
this.reconcileSelection(actions, queue);
|
||||||
const listHeight = height - LIST_TOP - FOOTER_HEIGHT;
|
const listHeight = height - LIST_TOP - FOOTER_HEIGHT;
|
||||||
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
|
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
|
||||||
if (this.selectedIndex < this.scrollRow) {
|
if (this.selectedQueueIndex < this.queueScrollRow) {
|
||||||
this.scrollRow = this.selectedIndex;
|
this.queueScrollRow = this.selectedQueueIndex;
|
||||||
} else if (this.selectedIndex >= this.scrollRow + visibleRows) {
|
} else if (this.selectedQueueIndex >= this.queueScrollRow + visibleRows) {
|
||||||
this.scrollRow = this.selectedIndex - visibleRows + 1;
|
this.queueScrollRow = this.selectedQueueIndex - visibleRows + 1;
|
||||||
}
|
}
|
||||||
this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, rows.length - visibleRows));
|
this.queueScrollRow = clamp(this.queueScrollRow, 0, Math.max(0, queue.length - visibleRows));
|
||||||
|
|
||||||
const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows);
|
for (let index = 0; index < actions.length; index++) {
|
||||||
for (let index = this.scrollRow; index < lastVisible; index++) {
|
const action = actions[index]!;
|
||||||
const row = rows[index]!;
|
const y = LIST_TOP + index * ROW_HEIGHT;
|
||||||
const y = LIST_TOP + (index - this.scrollRow) * ROW_HEIGHT;
|
const selected = index === this.selectedActionIndex;
|
||||||
const selected = index === this.selectedIndex;
|
|
||||||
if (selected) {
|
if (selected) {
|
||||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, ctx.stack.isFocused(), 4);
|
drawSelectionHighlight(
|
||||||
|
image,
|
||||||
|
ACTION_X - 6,
|
||||||
|
y - 1,
|
||||||
|
ACTION_WIDTH + 8,
|
||||||
|
ROW_HEIGHT - 1,
|
||||||
|
ctx.stack.isFocused() && this.focusColumn === "actions",
|
||||||
|
4,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
|
||||||
|
image.drawText(font, ACTION_X, y + 1, truncate(font, action.label, ACTION_WIDTH - 16), value);
|
||||||
|
if (action.kind === "playlist" && action.enabled) {
|
||||||
|
image.drawText(font, ACTION_X + ACTION_WIDTH - 16, y + 1, ">", value);
|
||||||
}
|
}
|
||||||
const value = !row.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
|
|
||||||
image.drawText(font, LIST_X, y + 1, truncate(font, row.label, width - 2 * LIST_X), value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
image.drawText(font, 20, height - 16, `${GESTURE_SCROLL} select ${GESTURE_CLICK} activate ${GESTURE_DOUBLE_CLICK} back`, 110);
|
image.drawLine(COLUMN_DIVIDER_X, LIST_TOP - 3, COLUMN_DIVIDER_X, height - FOOTER_HEIGHT - 3, 45);
|
||||||
|
if (!queue.length) {
|
||||||
|
image.drawText(font, QUEUE_X, LIST_TOP + 1, "Playlist unavailable", 90);
|
||||||
|
} else {
|
||||||
|
const queueWidth = width - QUEUE_X - 20;
|
||||||
|
const lastVisible = Math.min(queue.length, this.queueScrollRow + visibleRows);
|
||||||
|
for (let index = this.queueScrollRow; index < lastVisible; index++) {
|
||||||
|
const item = queue[index]!;
|
||||||
|
const y = LIST_TOP + (index - this.queueScrollRow) * ROW_HEIGHT;
|
||||||
|
const selected = index === this.selectedQueueIndex;
|
||||||
|
if (selected) {
|
||||||
|
drawSelectionHighlight(
|
||||||
|
image,
|
||||||
|
QUEUE_X - 6,
|
||||||
|
y - 1,
|
||||||
|
queueWidth + 8,
|
||||||
|
ROW_HEIGHT - 1,
|
||||||
|
ctx.stack.isFocused() && this.focusColumn === "playlist",
|
||||||
|
4,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
|
||||||
|
image.drawText(font, QUEUE_X, y + 1, truncate(font, label, queueWidth - 4), selected ? 255 : 200);
|
||||||
|
}
|
||||||
|
if (queue.length > visibleRows) {
|
||||||
|
const trackHeight = visibleRows * ROW_HEIGHT - 4;
|
||||||
|
const trackX = width - 12;
|
||||||
|
image.fillRect(trackX, LIST_TOP, 3, trackHeight, 30);
|
||||||
|
const thumbHeight = Math.max(8, (trackHeight * visibleRows / queue.length) | 0);
|
||||||
|
const maxScrollRow = queue.length - visibleRows;
|
||||||
|
const thumbY = LIST_TOP + (((trackHeight - thumbHeight) * this.queueScrollRow / maxScrollRow) | 0);
|
||||||
|
image.fillRect(trackX, thumbY, 3, thumbHeight, 120);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const backTarget = this.focusColumn === "playlist" ? "actions" : "back";
|
||||||
|
image.drawText(
|
||||||
|
font,
|
||||||
|
20,
|
||||||
|
height - 16,
|
||||||
|
`${GESTURE_SCROLL} select ${GESTURE_CLICK} activate ${GESTURE_DOUBLE_CLICK} ${backTarget}`,
|
||||||
|
110,
|
||||||
|
);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||||
|
if (event.type === "double-click" && this.focusColumn === "playlist") {
|
||||||
|
this.focusColumn = "actions";
|
||||||
|
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
|
||||||
|
return;
|
||||||
|
}
|
||||||
const media = mediaControllerBridge.snapshot();
|
const media = mediaControllerBridge.snapshot();
|
||||||
if (!media.accessEnabled) {
|
if (!media.accessEnabled) {
|
||||||
if (event.type === "click") {
|
if (event.type === "click") {
|
||||||
@@ -126,33 +200,52 @@ class MusicAppLayer implements Layer {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const rows = this.buildRows(media);
|
const queue = mediaControllerBridge.getQueue();
|
||||||
|
const actions = this.buildActions(media, queue);
|
||||||
|
this.reconcileSelection(actions, queue);
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case "scroll-up":
|
case "scroll-up":
|
||||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
if (this.focusColumn === "playlist") {
|
||||||
|
this.selectedQueueIndex = Math.max(0, this.selectedQueueIndex - 1);
|
||||||
|
} else {
|
||||||
|
this.selectedActionIndex = Math.max(0, this.selectedActionIndex - 1);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
case "scroll-down":
|
case "scroll-down":
|
||||||
this.selectedIndex = Math.min(Math.max(0, rows.length - 1), this.selectedIndex + 1);
|
if (this.focusColumn === "playlist") {
|
||||||
|
this.selectedQueueIndex = Math.min(Math.max(0, queue.length - 1), this.selectedQueueIndex + 1);
|
||||||
|
} else {
|
||||||
|
this.selectedActionIndex = Math.min(actions.length - 1, this.selectedActionIndex + 1);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
case "click": {
|
case "click": {
|
||||||
const row = rows[clamp(this.selectedIndex, 0, Math.max(0, rows.length - 1))];
|
if (this.focusColumn === "playlist") {
|
||||||
if (!row || !row.enabled) return;
|
const item = queue[this.selectedQueueIndex];
|
||||||
if (row.kind === "play-pause") await mediaControllerBridge.playPause();
|
if (item) await mediaControllerBridge.skipToQueueItem(item.id);
|
||||||
else if (row.kind === "previous") await mediaControllerBridge.skipPrevious();
|
return;
|
||||||
else if (row.kind === "next") await mediaControllerBridge.skipNext();
|
}
|
||||||
else if (row.kind === "queue-item") await mediaControllerBridge.skipToQueueItem(row.item.id);
|
const action = actions[this.selectedActionIndex];
|
||||||
|
if (!action || !action.enabled) return;
|
||||||
|
if (action.kind === "play-pause") await mediaControllerBridge.playPause();
|
||||||
|
else if (action.kind === "previous") await mediaControllerBridge.skipPrevious();
|
||||||
|
else if (action.kind === "next") await mediaControllerBridge.skipNext();
|
||||||
|
else if (action.kind === "volume") {
|
||||||
|
ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent()));
|
||||||
|
} else if (action.kind === "playlist") {
|
||||||
|
const activeIndex = queue.findIndex((item) => item.active);
|
||||||
|
if (activeIndex >= 0) this.selectedQueueIndex = activeIndex;
|
||||||
|
this.focusColumn = "playlist";
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
case "double-click":
|
|
||||||
ctx.stack.pop();
|
|
||||||
return;
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildRows(media: MediaControllerState): MusicRow[] {
|
private buildActions(media: MediaControllerState, queue: MediaQueueItem[]): MusicAction[] {
|
||||||
const rows: MusicRow[] = [
|
const volume = mediaControllerBridge.getMediaVolumePercent();
|
||||||
|
return [
|
||||||
{
|
{
|
||||||
kind: "play-pause",
|
kind: "play-pause",
|
||||||
label: media.playbackState === "playing" ? "Pause" : "Play",
|
label: media.playbackState === "playing" ? "Pause" : "Play",
|
||||||
@@ -160,16 +253,33 @@ class MusicAppLayer implements Layer {
|
|||||||
},
|
},
|
||||||
{ kind: "previous", label: "Previous track", enabled: media.canSkipPrevious },
|
{ kind: "previous", label: "Previous track", enabled: media.canSkipPrevious },
|
||||||
{ kind: "next", label: "Next track", enabled: media.canSkipNext },
|
{ kind: "next", label: "Next track", enabled: media.canSkipNext },
|
||||||
|
{ kind: "volume", label: volume >= 0 ? `Volume (${volume})` : "Volume", enabled: volume >= 0 },
|
||||||
|
{ kind: "playlist", label: "Playlist", enabled: queue.length > 0 },
|
||||||
];
|
];
|
||||||
for (const item of mediaControllerBridge.getQueue()) {
|
}
|
||||||
rows.push({
|
|
||||||
kind: "queue-item",
|
private reconcileSelection(actions: MusicAction[], queue: MediaQueueItem[]): void {
|
||||||
label: `${item.active ? "> " : " "}${item.title || "(untitled)"}`,
|
this.selectedActionIndex = clamp(this.selectedActionIndex, 0, actions.length - 1);
|
||||||
enabled: true,
|
this.selectedQueueIndex = clamp(this.selectedQueueIndex, 0, Math.max(0, queue.length - 1));
|
||||||
item,
|
if (!queue.length && this.focusColumn === "playlist") {
|
||||||
});
|
this.focusColumn = "actions";
|
||||||
|
this.selectedActionIndex = actions.length - 1;
|
||||||
}
|
}
|
||||||
return rows;
|
}
|
||||||
|
|
||||||
|
private drawProgress(image: GrayImage, media: MediaControllerState, width: number): void {
|
||||||
|
if (media.durationMs <= 0 || media.positionMs < 0) return;
|
||||||
|
const font = getDefaultSmallFont();
|
||||||
|
const y = ART_Y + 84;
|
||||||
|
const elapsed = formatMediaTime(media.positionMs);
|
||||||
|
const duration = formatMediaTime(media.durationMs);
|
||||||
|
image.drawText(font, META_X, y, elapsed, 140);
|
||||||
|
image.drawText(font, META_X + width - font.measureText(duration), y, duration, 140);
|
||||||
|
|
||||||
|
const barY = ART_Y + 100;
|
||||||
|
image.drawRect(META_X, barY, width, 5, 55);
|
||||||
|
const progress = clamp(media.positionMs / media.durationMs, 0, 1);
|
||||||
|
image.fillRect(META_X + 1, barY + 1, Math.round((width - 2) * progress), 3, 170);
|
||||||
}
|
}
|
||||||
|
|
||||||
private drawArt(image: GrayImage, media: MediaControllerState): void {
|
private drawArt(image: GrayImage, media: MediaControllerState): void {
|
||||||
@@ -192,8 +302,95 @@ class MusicAppLayer implements Layer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Centered volume control overlay; scroll gestures adjust a 0..100 target by two. */
|
||||||
|
class VolumeModalLayer implements Layer {
|
||||||
|
private volume: number;
|
||||||
|
|
||||||
|
constructor(initialVolume: number) {
|
||||||
|
this.volume = clamp(initialVolume, 0, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||||
|
const image = paintBelow();
|
||||||
|
const small = getDefaultSmallFont();
|
||||||
|
const medium = getDefaultMediumFont();
|
||||||
|
const { width, height } = ctx.stack.getBaseSize();
|
||||||
|
const boxWidth = 340;
|
||||||
|
const boxHeight = 150;
|
||||||
|
const x = ((width - boxWidth) / 2) | 0;
|
||||||
|
const y = ((height - boxHeight) / 2) | 0;
|
||||||
|
|
||||||
|
image.fillRoundedRect(x, y, boxWidth, boxHeight, 1, 8);
|
||||||
|
image.drawRoundedRect(x, y, boxWidth, boxHeight, 95, 8);
|
||||||
|
image.drawText(small, x + 18, y + 14, "Media volume", 180);
|
||||||
|
|
||||||
|
const value = `${this.volume} / 100`;
|
||||||
|
const valueX = x + (((boxWidth - medium.measureText(value)) / 2) | 0);
|
||||||
|
image.drawText(medium, valueX, y + 45, value, 245);
|
||||||
|
|
||||||
|
const barX = x + 28;
|
||||||
|
const barY = y + 83;
|
||||||
|
const barWidth = boxWidth - 56;
|
||||||
|
image.drawRect(barX, barY, barWidth, 9, 65);
|
||||||
|
image.fillRect(barX + 1, barY + 1, Math.round((barWidth - 2) * this.volume / 100), 7, 175);
|
||||||
|
|
||||||
|
const hint = `${GESTURE_SCROLL_UP} +2 ${GESTURE_SCROLL_DOWN} -2 ${GESTURE_DOUBLE_CLICK} done`;
|
||||||
|
const hintX = x + (((boxWidth - small.measureText(hint)) / 2) | 0);
|
||||||
|
image.drawText(small, hintX, y + 116, hint, 120);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
|
||||||
|
if (event.type === "scroll-up") {
|
||||||
|
this.adjust(2);
|
||||||
|
} else if (event.type === "scroll-down") {
|
||||||
|
this.adjust(-2);
|
||||||
|
} else if (event.type === "double-click") {
|
||||||
|
ctx.stack.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private adjust(delta: number): void {
|
||||||
|
this.volume = clamp(this.volume + delta, 0, 100);
|
||||||
|
mediaControllerBridge.setMediaVolumePercent(this.volume);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Let back leave the playlist column before the root wrapper yields to the shell. */
|
||||||
|
class MusicRootLayer implements Layer {
|
||||||
|
private readonly yieldAtRoot: YieldAtRootLayer;
|
||||||
|
|
||||||
|
constructor(private readonly music: MusicAppLayer) {
|
||||||
|
this.yieldAtRoot = new YieldAtRootLayer(music);
|
||||||
|
}
|
||||||
|
|
||||||
|
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||||
|
return this.yieldAtRoot.paint(ctx, paintBelow);
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||||
|
if (event.type === "double-click" && this.music.isPlaylistFocused()) {
|
||||||
|
await this.music.handleInput(event, ctx);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await this.yieldAtRoot.handleInput(event, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
onRemoved(): void {
|
||||||
|
this.yieldAtRoot.onRemoved();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createMusicAppWindow(options: InProcessAppOptions): InProcessWindow {
|
export function createMusicAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||||
let unsubscribe: (() => void) | null = null;
|
let unsubscribe: (() => void) | null = null;
|
||||||
|
let progressTimer: ReturnType<typeof setInterval> | null = null;
|
||||||
|
const musicLayer = new MusicAppLayer();
|
||||||
|
const stopProgressTimer = () => {
|
||||||
|
if (progressTimer !== null) {
|
||||||
|
clearInterval(progressTimer);
|
||||||
|
progressTimer = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
const app = createInProcessWindow({
|
const app = createInProcessWindow({
|
||||||
appId: "music",
|
appId: "music",
|
||||||
windowId: MUSIC_WINDOW_ID,
|
windowId: MUSIC_WINDOW_ID,
|
||||||
@@ -202,37 +399,37 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
|
|||||||
icon: "music",
|
icon: "music",
|
||||||
closeable: true,
|
closeable: true,
|
||||||
actions: options.actions,
|
actions: options.actions,
|
||||||
baseLayer: new YieldAtRootLayer(new MusicAppLayer()),
|
baseLayer: new MusicRootLayer(musicLayer),
|
||||||
submitFrame: options.submitFrame,
|
submitFrame: options.submitFrame,
|
||||||
setSurfaceVisible: options.setSurfaceVisible,
|
setSurfaceVisible: options.setSurfaceVisible,
|
||||||
removeSurface: options.removeSurface,
|
removeSurface: options.removeSurface,
|
||||||
onClosed: () => {
|
onClosed: () => {
|
||||||
|
stopProgressTimer();
|
||||||
unsubscribe?.();
|
unsubscribe?.();
|
||||||
unsubscribe = null;
|
unsubscribe = null;
|
||||||
options.onClosed();
|
options.onClosed();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
unsubscribe = mediaControllerBridge.onStateChange(() => {
|
unsubscribe = mediaControllerBridge.onStateChange((state) => {
|
||||||
|
if (state.playbackState === "playing" && state.durationMs > 0 && state.positionMs >= 0) {
|
||||||
|
progressTimer ??= setInterval(() => app.requestRender(), 1_000);
|
||||||
|
} else {
|
||||||
|
stopProgressTimer();
|
||||||
|
}
|
||||||
app.requestRender();
|
app.requestRender();
|
||||||
});
|
});
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
function playbackLabel(media: MediaControllerState): string {
|
function formatMediaTime(milliseconds: number): string {
|
||||||
switch (media.playbackState) {
|
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
|
||||||
case "playing":
|
const seconds = totalSeconds % 60;
|
||||||
return "Playing";
|
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||||
case "paused":
|
const minutes = totalMinutes % 60;
|
||||||
return "Paused";
|
const hours = Math.floor(totalMinutes / 60);
|
||||||
case "buffering":
|
return hours > 0
|
||||||
return "Buffering";
|
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
|
||||||
case "stopped":
|
: `${minutes}:${String(seconds).padStart(2, "0")}`;
|
||||||
return "Stopped";
|
|
||||||
case "notification-access-required":
|
|
||||||
return "Access required";
|
|
||||||
default:
|
|
||||||
return media.status || "Idle";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function truncate(font: import("../../graphics/bdffont").BdfFont, text: string, maxWidth: number): string {
|
function truncate(font: import("../../graphics/bdffont").BdfFont, text: string, maxWidth: number): string {
|
||||||
|
|||||||
+20
-11
@@ -5,7 +5,12 @@ import { DashboardInputEvent, LayerActions, LayerStack } from "../layers";
|
|||||||
import { MenuLayer, type MenuItem } from "../menu";
|
import { MenuLayer, type MenuItem } from "../menu";
|
||||||
import { VoiceInputLayer } from "../apps/voice-input";
|
import { VoiceInputLayer } from "../apps/voice-input";
|
||||||
import { SingleNotificationLayer } from "../notifications";
|
import { SingleNotificationLayer } from "../notifications";
|
||||||
import { batteryDisplayModeSetting, onAnySettingChanged, timeFormatSetting } from "../dashboard-settings";
|
import {
|
||||||
|
batteryDisplayModeSetting,
|
||||||
|
onAnySettingChanged,
|
||||||
|
timeFormatSetting,
|
||||||
|
wakeWordActionSetting,
|
||||||
|
} from "../dashboard-settings";
|
||||||
import { ShellChromeLayer, type ShellChromeState, type ShellChromeWindow } from "./chrome-layer";
|
import { ShellChromeLayer, type ShellChromeState, type ShellChromeWindow } from "./chrome-layer";
|
||||||
import { ShellModalLayer } from "./modal-layer";
|
import { ShellModalLayer } from "./modal-layer";
|
||||||
import { SIDEBAR_WIDTH, TOP_BAR_HEIGHT } from "./geometry";
|
import { SIDEBAR_WIDTH, TOP_BAR_HEIGHT } from "./geometry";
|
||||||
@@ -318,22 +323,26 @@ class Shell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async receiveInput(event: DashboardInputEvent, frameId = 0): Promise<ShellInputOutcome> {
|
async receiveInput(event: DashboardInputEvent, frameId = 0): Promise<ShellInputOutcome> {
|
||||||
this.lastInputAtMs = Date.now();
|
// The wakeword is handled before the screen-off short-circuit so its
|
||||||
|
// configured action can work from a dark screen. With the CFW the stock
|
||||||
// The wakeword is handled before the screen-off short-circuit: "Hey Even"
|
// Even AI app never launches, so the firmware does not power the display
|
||||||
// is meant to work from a dark screen, which is the whole point of it.
|
// for us either -- actions that need it wake the screen themselves.
|
||||||
// With the CFW the stock Even AI app never launches, so the firmware does
|
|
||||||
// not power the display for us either -- we wake it ourselves.
|
|
||||||
if (event.type === "wakeword") {
|
if (event.type === "wakeword") {
|
||||||
if (!this.screenOn) {
|
const action = wakeWordActionSetting.get();
|
||||||
this.wake("sidebar");
|
if (action === "off") {
|
||||||
|
return { shell: false, window: false };
|
||||||
}
|
}
|
||||||
if (!this.activeVoiceLayer) {
|
|
||||||
|
this.lastInputAtMs = Date.now();
|
||||||
|
const wokeScreen = !this.screenOn && this.wake("sidebar");
|
||||||
|
if (action === "voice-input" && !this.activeVoiceLayer) {
|
||||||
this.openVoiceDialog(false, true);
|
this.openVoiceDialog(false, true);
|
||||||
}
|
}
|
||||||
return { shell: true, window: false };
|
return { shell: wokeScreen || action === "voice-input", window: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.lastInputAtMs = Date.now();
|
||||||
|
|
||||||
if (!this.screenOn) {
|
if (!this.screenOn) {
|
||||||
if (event.type === "double-click") {
|
if (event.type === "double-click") {
|
||||||
this.wake("sidebar");
|
this.wake("sidebar");
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { imageFromAsciiArt } from "../../graphics/image";
|
import { imageFromAsciiArt } from "../../graphics/image";
|
||||||
import { TranscribeLayer } from "../apps/transcribe";
|
import { TranscribeLayer } from "../apps/transcribe";
|
||||||
import type { LayerActions } from "../layers";
|
|
||||||
import {
|
import {
|
||||||
createInProcessWindow,
|
createInProcessWindow,
|
||||||
YieldAtRootLayer,
|
YieldAtRootLayer,
|
||||||
@@ -49,7 +48,6 @@ export type TranscribeAppOptions = InProcessAppOptions & {
|
|||||||
*/
|
*/
|
||||||
export function createTranscribeAppWindow(options: TranscribeAppOptions): InProcessWindow {
|
export function createTranscribeAppWindow(options: TranscribeAppOptions): InProcessWindow {
|
||||||
const layer = new TranscribeLayer();
|
const layer = new TranscribeLayer();
|
||||||
const actions: LayerActions = { ...options.actions };
|
|
||||||
const app = createInProcessWindow({
|
const app = createInProcessWindow({
|
||||||
appId: "transcribe",
|
appId: "transcribe",
|
||||||
windowId: TRANSCRIBE_WINDOW_ID,
|
windowId: TRANSCRIBE_WINDOW_ID,
|
||||||
@@ -57,7 +55,7 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc
|
|||||||
iconLetter: "Tr",
|
iconLetter: "Tr",
|
||||||
icon: "mic",
|
icon: "mic",
|
||||||
closeable: true,
|
closeable: true,
|
||||||
actions,
|
actions: options.actions,
|
||||||
baseLayer: new YieldAtRootLayer(layer),
|
baseLayer: new YieldAtRootLayer(layer),
|
||||||
submitFrame: options.submitFrame,
|
submitFrame: options.submitFrame,
|
||||||
setSurfaceVisible: options.setSurfaceVisible,
|
setSurfaceVisible: options.setSurfaceVisible,
|
||||||
@@ -68,10 +66,9 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc
|
|||||||
options.onClosed();
|
options.onClosed();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
// start() wires the layer's transcript/status subscriptions; the LayerStack
|
// Wire async state changes to this window's renderer, rather than the
|
||||||
// exposes its context through a paint, but the layer needs it up front, so
|
// controller action from which the window-specific actions were derived.
|
||||||
// build a minimal context here.
|
layer.start(app.requestRender);
|
||||||
layer.start({ stack: app.stack, actions });
|
|
||||||
options.startContinuousVoiceCapture();
|
options.startContinuousVoiceCapture();
|
||||||
shell.setTrayIcon(TRAY_ICON_ID, MIC_ICON);
|
shell.setTrayIcon(TRAY_ICON_ID, MIC_ICON);
|
||||||
return app;
|
return app;
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
|
||||||
|
import { weatherBridge } from "../../native/weather";
|
||||||
|
import { WeatherLayer } from "../apps/weather";
|
||||||
|
import {
|
||||||
|
createInProcessWindow,
|
||||||
|
YieldAtRootLayer,
|
||||||
|
type InProcessAppOptions,
|
||||||
|
type InProcessWindow,
|
||||||
|
} from "./in-process-window";
|
||||||
|
|
||||||
|
export const WEATHER_WINDOW_ID = "weather";
|
||||||
|
export const WEATHER_SURFACE_ID = "window:weather";
|
||||||
|
|
||||||
|
/** Local current conditions and forecast from the National Weather Service. */
|
||||||
|
export function createWeatherAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||||
|
let requestingPermission = false;
|
||||||
|
let unsubscribe: (() => void) | null = null;
|
||||||
|
let app: InProcessWindow;
|
||||||
|
|
||||||
|
const requestUpdate = () => {
|
||||||
|
if (requestingPermission) return;
|
||||||
|
if (hasLocationPermission()) {
|
||||||
|
weatherBridge.start();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
requestingPermission = true;
|
||||||
|
void ensureLocationPermission().then((granted) => {
|
||||||
|
requestingPermission = false;
|
||||||
|
if (granted) weatherBridge.start();
|
||||||
|
app.requestRender();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
app = createInProcessWindow({
|
||||||
|
appId: "weather",
|
||||||
|
windowId: WEATHER_WINDOW_ID,
|
||||||
|
title: "Weather",
|
||||||
|
iconLetter: "W",
|
||||||
|
icon: "cloud-sun",
|
||||||
|
closeable: true,
|
||||||
|
actions: options.actions,
|
||||||
|
baseLayer: new YieldAtRootLayer(new WeatherLayer(() => weatherBridge.snapshot(), requestUpdate)),
|
||||||
|
submitFrame: options.submitFrame,
|
||||||
|
setSurfaceVisible: options.setSurfaceVisible,
|
||||||
|
removeSurface: options.removeSurface,
|
||||||
|
onClosed: () => {
|
||||||
|
unsubscribe?.();
|
||||||
|
unsubscribe = null;
|
||||||
|
weatherBridge.stop();
|
||||||
|
options.onClosed();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
unsubscribe = weatherBridge.onStateChange(() => app.requestRender());
|
||||||
|
requestUpdate();
|
||||||
|
return app;
|
||||||
|
}
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
/**
|
|
||||||
* Stopwatch app, hosted in its own worker thread. One worker serves every
|
|
||||||
* stopwatch window; per-window state is keyed by windowId. The shell (main
|
|
||||||
* thread) sends open/close/input/lifecycle messages over postMessage; frames
|
|
||||||
* are painted here and submitted directly to the Java compositor from this
|
|
||||||
* worker's own thread — no pixels cross the isolate boundary.
|
|
||||||
*/
|
|
||||||
import "@nativescript/core/globals";
|
|
||||||
import { GrayImage } from "../graphics/image";
|
|
||||||
import { getDefaultSmallFont, getFont } from "../graphics/bdffont";
|
|
||||||
import * as frameTimings from "../native/frame-timings";
|
|
||||||
import type { DashboardInputEvent } from "../ui/layers";
|
|
||||||
import type { WorkerAppMessage } from "../ui/shell/worker-window";
|
|
||||||
|
|
||||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../ui/gestures";
|
|
||||||
declare const global: any;
|
|
||||||
declare const com: any;
|
|
||||||
|
|
||||||
const RENDER_INTERVAL_MS = 100;
|
|
||||||
|
|
||||||
const stopwatchFont = getFont("terminus32");
|
|
||||||
const smallFont = getDefaultSmallFont();
|
|
||||||
|
|
||||||
type StopwatchWindow = {
|
|
||||||
windowId: string;
|
|
||||||
surfaceId: string;
|
|
||||||
viewportWidth: number;
|
|
||||||
viewportHeight: number;
|
|
||||||
foreground: boolean;
|
|
||||||
// Accumulated time survives pausing, backgrounding, and screen-off; only
|
|
||||||
// rendering pauses.
|
|
||||||
accumulatedMs: number;
|
|
||||||
runningSinceMs: number | null;
|
|
||||||
renderTimer: ReturnType<typeof setInterval> | null;
|
|
||||||
lastSubmittedFingerprint: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const windows = new Map<string, StopwatchWindow>();
|
|
||||||
let screenOn = true;
|
|
||||||
|
|
||||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
|
||||||
const message = event.data;
|
|
||||||
switch (message.type) {
|
|
||||||
case "open-window":
|
|
||||||
windows.set(message.windowId, {
|
|
||||||
windowId: message.windowId,
|
|
||||||
surfaceId: message.surfaceId,
|
|
||||||
viewportWidth: message.viewport.width,
|
|
||||||
viewportHeight: message.viewport.height,
|
|
||||||
foreground: false,
|
|
||||||
accumulatedMs: 0,
|
|
||||||
runningSinceMs: null,
|
|
||||||
renderTimer: null,
|
|
||||||
lastSubmittedFingerprint: "",
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "close-window": {
|
|
||||||
const window = windows.get(message.windowId);
|
|
||||||
if (window?.renderTimer) clearInterval(window.renderTimer);
|
|
||||||
windows.delete(message.windowId);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "input": {
|
|
||||||
const window = windows.get(message.windowId);
|
|
||||||
if (!window) {
|
|
||||||
frameTimings.finishFrame(message.frameId, "discarded: unknown stopwatch window");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
handleInput(window, message.event as DashboardInputEvent, message.frameId);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "render": {
|
|
||||||
const window = windows.get(message.windowId);
|
|
||||||
if (window) renderAndSubmit(window, 0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "foreground": {
|
|
||||||
const window = windows.get(message.windowId);
|
|
||||||
if (!window) break;
|
|
||||||
window.foreground = message.foreground;
|
|
||||||
updateRenderTimer(window);
|
|
||||||
if (window.foreground) renderAndSubmit(window, 0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "screen":
|
|
||||||
screenOn = message.on;
|
|
||||||
for (const window of windows.values()) {
|
|
||||||
updateRenderTimer(window);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function handleInput(window: StopwatchWindow, event: DashboardInputEvent, frameId: number): void {
|
|
||||||
if (event.type === "double-click") {
|
|
||||||
// Back out of the app root: return focus to the shell sidebar. State keeps
|
|
||||||
// running; the frame is finished here since no repaint happens.
|
|
||||||
frameTimings.finishFrame(frameId, "discarded: stopwatch yielded focus");
|
|
||||||
global.postMessage({ type: "yield-focus", windowId: window.windowId });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (event.type === "click") {
|
|
||||||
if (window.runningSinceMs === null) {
|
|
||||||
window.runningSinceMs = Date.now();
|
|
||||||
} else {
|
|
||||||
window.accumulatedMs += Date.now() - window.runningSinceMs;
|
|
||||||
window.runningSinceMs = null;
|
|
||||||
}
|
|
||||||
updateRenderTimer(window);
|
|
||||||
renderAndSubmit(window, frameId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
frameTimings.finishFrame(frameId, "discarded: stopwatch ignored input");
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateRenderTimer(window: StopwatchWindow): void {
|
|
||||||
const shouldRun = window.runningSinceMs !== null && window.foreground && screenOn;
|
|
||||||
if (shouldRun && window.renderTimer === null) {
|
|
||||||
window.renderTimer = setInterval(() => renderAndSubmit(window, 0), RENDER_INTERVAL_MS);
|
|
||||||
} else if (!shouldRun && window.renderTimer !== null) {
|
|
||||||
clearInterval(window.renderTimer);
|
|
||||||
window.renderTimer = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function elapsedMs(window: StopwatchWindow): number {
|
|
||||||
const runningMs = window.runningSinceMs === null ? 0 : Date.now() - window.runningSinceMs;
|
|
||||||
return window.accumulatedMs + runningMs;
|
|
||||||
}
|
|
||||||
|
|
||||||
function paint(window: StopwatchWindow): GrayImage {
|
|
||||||
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
|
||||||
const elapsed = elapsedMs(window);
|
|
||||||
const timeLabel = formatStopwatchElapsed(elapsed);
|
|
||||||
const stateLabel = window.runningSinceMs === null ? (elapsed > 0 ? "Paused" : "Ready") : "Running";
|
|
||||||
|
|
||||||
image.drawText(smallFont, 24, 16, "Stopwatch", 180);
|
|
||||||
const timeX = Math.max(0, Math.round((window.viewportWidth - stopwatchFont.measureText(timeLabel)) / 2));
|
|
||||||
const timeY = Math.max(0, Math.round((window.viewportHeight / 2 - stopwatchFont.lineHeight) / 2) + 20);
|
|
||||||
image.drawText(stopwatchFont, timeX, timeY, timeLabel, 245);
|
|
||||||
|
|
||||||
const hintsY = window.viewportHeight - 66;
|
|
||||||
image.drawText(smallFont, 90, hintsY, stateLabel, 150);
|
|
||||||
image.drawText(smallFont, 90, hintsY + 18, `${GESTURE_CLICK} start / pause`, 150);
|
|
||||||
image.drawText(smallFont, 90, hintsY + 36, `${GESTURE_DOUBLE_CLICK} back`, 150);
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderAndSubmit(window: StopwatchWindow, inputFrameId: number): void {
|
|
||||||
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
|
|
||||||
try {
|
|
||||||
const paintStartedAtMs = Date.now();
|
|
||||||
const image = frameTimings.span(frameId, "paint", () =>
|
|
||||||
frameTimings.runWithFrame(frameId, () => paint(window)),
|
|
||||||
);
|
|
||||||
const paintMs = Date.now() - paintStartedAtMs;
|
|
||||||
const fingerprint = image.fingerprint();
|
|
||||||
if (fingerprint === window.lastSubmittedFingerprint) {
|
|
||||||
frameTimings.finishFrame(frameId, "discarded: stopwatch content unchanged");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
|
||||||
if (!communicator) {
|
|
||||||
frameTimings.finishFrame(frameId, "discarded: no active communicator");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const buffer = image.to8bppBuffer();
|
|
||||||
communicator.submitSurfaceFrame(
|
|
||||||
buffer.buffer,
|
|
||||||
window.surfaceId,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
image.width,
|
|
||||||
image.height,
|
|
||||||
fingerprint,
|
|
||||||
paintMs,
|
|
||||||
frameId,
|
|
||||||
);
|
|
||||||
window.lastSubmittedFingerprint = fingerprint;
|
|
||||||
} catch (error) {
|
|
||||||
frameTimings.finishFrame(frameId, `discarded: stopwatch render failed`);
|
|
||||||
console.error(`stopwatch worker render failed: ${error}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function pad2(value: number): string {
|
|
||||||
return String(value).padStart(2, "0");
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatStopwatchElapsed(totalMs: number): string {
|
|
||||||
const totalTenths = Math.floor(totalMs / 100);
|
|
||||||
const tenths = totalTenths % 10;
|
|
||||||
const totalSeconds = Math.floor(totalTenths / 10);
|
|
||||||
const seconds = totalSeconds % 60;
|
|
||||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
|
||||||
const minutes = totalMinutes % 60;
|
|
||||||
const hours = Math.floor(totalMinutes / 60);
|
|
||||||
if (hours > 0) {
|
|
||||||
return `${hours}:${pad2(minutes)}:${pad2(seconds)}.${tenths}`;
|
|
||||||
}
|
|
||||||
return `${pad2(minutes)}:${pad2(seconds)}.${tenths}`;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,541 @@
|
|||||||
|
/**
|
||||||
|
* Timer app worker. The singleton window contains a stopwatch plus a list of
|
||||||
|
* independently scheduled countdown timers. Android AlarmManager owns the
|
||||||
|
* durable expiry path; worker timeouts keep the live UI prompt.
|
||||||
|
*/
|
||||||
|
import "@nativescript/core/globals";
|
||||||
|
import { GrayImage } from "../graphics/image";
|
||||||
|
import { getDefaultSmallFont, getFont } from "../graphics/bdffont";
|
||||||
|
import * as frameTimings from "../native/frame-timings";
|
||||||
|
import {
|
||||||
|
cancelTimerNotification,
|
||||||
|
fireTimerNotification,
|
||||||
|
scheduleTimerNotification,
|
||||||
|
} from "../native/timer-notifications";
|
||||||
|
import type { DashboardInputEvent } from "../ui/layers";
|
||||||
|
import type { WorkerAppMessage, WorkerAppReply } from "../ui/shell/worker-window";
|
||||||
|
import {
|
||||||
|
GESTURE_CLICK,
|
||||||
|
GESTURE_DOUBLE_CLICK,
|
||||||
|
GESTURE_SCROLL,
|
||||||
|
} from "../ui/gestures";
|
||||||
|
|
||||||
|
declare const global: any;
|
||||||
|
declare const com: any;
|
||||||
|
|
||||||
|
const RENDER_INTERVAL_MS = 100;
|
||||||
|
const TIMER_ROW_HEIGHT = 34;
|
||||||
|
const TIMER_LIST_TOP = 52;
|
||||||
|
const FOOTER_HEIGHT = 34;
|
||||||
|
|
||||||
|
const largeFont = getFont("terminus32");
|
||||||
|
const mediumFont = getFont("terminus24");
|
||||||
|
const smallFont = getDefaultSmallFont();
|
||||||
|
|
||||||
|
type TimerView = "stopwatch" | "timers" | "editor";
|
||||||
|
|
||||||
|
type TimerWindow = {
|
||||||
|
windowId: string;
|
||||||
|
surfaceId: string;
|
||||||
|
viewportWidth: number;
|
||||||
|
viewportHeight: number;
|
||||||
|
foreground: boolean;
|
||||||
|
view: TimerView;
|
||||||
|
stopwatchAction: number;
|
||||||
|
timerSelection: number;
|
||||||
|
editorField: number;
|
||||||
|
editorHours: number;
|
||||||
|
editorMinutes: number;
|
||||||
|
editorSeconds: number;
|
||||||
|
editorMessage: string;
|
||||||
|
renderTimer: ReturnType<typeof setInterval> | null;
|
||||||
|
lastSubmittedFingerprint: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type CountdownTimer = {
|
||||||
|
id: number;
|
||||||
|
durationMs: number;
|
||||||
|
endAtMs: number;
|
||||||
|
expired: boolean;
|
||||||
|
expiryTimeout: ReturnType<typeof setTimeout> | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const windows = new Map<string, TimerWindow>();
|
||||||
|
const timers: CountdownTimer[] = [];
|
||||||
|
let screenOn = true;
|
||||||
|
let stopwatchAccumulatedMs = 0;
|
||||||
|
let stopwatchRunningSinceMs: number | null = null;
|
||||||
|
let nextTimerSerial = 1;
|
||||||
|
|
||||||
|
function post(message: WorkerAppReply): void {
|
||||||
|
global.postMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||||
|
const message = event.data;
|
||||||
|
switch (message.type) {
|
||||||
|
case "open-window":
|
||||||
|
windows.set(message.windowId, {
|
||||||
|
windowId: message.windowId,
|
||||||
|
surfaceId: message.surfaceId,
|
||||||
|
viewportWidth: message.viewport.width,
|
||||||
|
viewportHeight: message.viewport.height,
|
||||||
|
foreground: false,
|
||||||
|
view: "stopwatch",
|
||||||
|
stopwatchAction: 0,
|
||||||
|
timerSelection: 0,
|
||||||
|
editorField: 0,
|
||||||
|
editorHours: 0,
|
||||||
|
editorMinutes: 5,
|
||||||
|
editorSeconds: 0,
|
||||||
|
editorMessage: "",
|
||||||
|
renderTimer: null,
|
||||||
|
lastSubmittedFingerprint: "",
|
||||||
|
});
|
||||||
|
syncExpiredTimers();
|
||||||
|
break;
|
||||||
|
case "close-window": {
|
||||||
|
const window = windows.get(message.windowId);
|
||||||
|
if (window?.renderTimer) clearInterval(window.renderTimer);
|
||||||
|
windows.delete(message.windowId);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "input": {
|
||||||
|
const window = windows.get(message.windowId);
|
||||||
|
if (!window) {
|
||||||
|
frameTimings.finishFrame(message.frameId, "discarded: unknown timer window");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
handleInput(window, message.event as DashboardInputEvent, message.frameId);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "render": {
|
||||||
|
const window = windows.get(message.windowId);
|
||||||
|
if (window) renderAndSubmit(window, 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "foreground": {
|
||||||
|
const window = windows.get(message.windowId);
|
||||||
|
if (!window) break;
|
||||||
|
window.foreground = message.foreground;
|
||||||
|
updateRenderTimer(window);
|
||||||
|
if (window.foreground) renderAndSubmit(window, 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "screen":
|
||||||
|
screenOn = message.on;
|
||||||
|
for (const window of windows.values()) updateRenderTimer(window);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function handleInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
|
||||||
|
if (event.type === "double-click") {
|
||||||
|
if (window.view === "editor") {
|
||||||
|
window.view = "timers";
|
||||||
|
window.editorMessage = "";
|
||||||
|
updateRenderTimer(window);
|
||||||
|
renderAndSubmit(window, frameId);
|
||||||
|
} else {
|
||||||
|
frameTimings.finishFrame(frameId, "discarded: timer yielded focus");
|
||||||
|
post({ type: "yield-focus", windowId: window.windowId });
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (window.view === "stopwatch") {
|
||||||
|
handleStopwatchInput(window, event, frameId);
|
||||||
|
} else if (window.view === "timers") {
|
||||||
|
handleTimersInput(window, event, frameId);
|
||||||
|
} else {
|
||||||
|
handleEditorInput(window, event, frameId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleStopwatchInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
|
||||||
|
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||||
|
const delta = event.type === "scroll-down" ? 1 : -1;
|
||||||
|
window.stopwatchAction = clamp(window.stopwatchAction + delta, 0, 2);
|
||||||
|
renderAndSubmit(window, frameId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.type !== "click") {
|
||||||
|
frameTimings.finishFrame(frameId, "discarded: timer stopwatch ignored input");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (window.stopwatchAction === 0) {
|
||||||
|
if (stopwatchRunningSinceMs === null) {
|
||||||
|
stopwatchRunningSinceMs = Date.now();
|
||||||
|
} else {
|
||||||
|
stopwatchAccumulatedMs += Date.now() - stopwatchRunningSinceMs;
|
||||||
|
stopwatchRunningSinceMs = null;
|
||||||
|
}
|
||||||
|
} else if (window.stopwatchAction === 1) {
|
||||||
|
stopwatchAccumulatedMs = 0;
|
||||||
|
stopwatchRunningSinceMs = null;
|
||||||
|
} else {
|
||||||
|
window.view = "timers";
|
||||||
|
window.timerSelection = 0;
|
||||||
|
}
|
||||||
|
updateRenderTimer(window);
|
||||||
|
renderAndSubmit(window, frameId);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleTimersInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
|
||||||
|
syncExpiredTimers();
|
||||||
|
const itemCount = timers.length + 2; // Stopwatch navigation + New timer + timers.
|
||||||
|
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||||
|
const delta = event.type === "scroll-down" ? 1 : -1;
|
||||||
|
window.timerSelection = clamp(window.timerSelection + delta, 0, itemCount - 1);
|
||||||
|
renderAndSubmit(window, frameId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.type !== "click") {
|
||||||
|
frameTimings.finishFrame(frameId, "discarded: timer list ignored input");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (window.timerSelection === 0) {
|
||||||
|
window.view = "stopwatch";
|
||||||
|
} else if (window.timerSelection === 1) {
|
||||||
|
openEditor(window);
|
||||||
|
} else {
|
||||||
|
const timer = timers[window.timerSelection - 2];
|
||||||
|
if (timer) removeTimer(timer);
|
||||||
|
window.timerSelection = clamp(window.timerSelection, 0, Math.max(0, timers.length + 1));
|
||||||
|
}
|
||||||
|
updateRenderTimer(window);
|
||||||
|
renderAndSubmit(window, frameId);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleEditorInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
|
||||||
|
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||||
|
const delta = event.type === "scroll-up" ? 1 : -1;
|
||||||
|
if (window.editorField === 0) {
|
||||||
|
window.editorHours = clamp(window.editorHours + delta, 0, 23);
|
||||||
|
} else if (window.editorField === 1) {
|
||||||
|
window.editorMinutes = clamp(window.editorMinutes + delta, 0, 59);
|
||||||
|
} else if (window.editorField === 2) {
|
||||||
|
window.editorSeconds = clamp(window.editorSeconds + delta, 0, 59);
|
||||||
|
} else {
|
||||||
|
window.editorField = delta > 0 ? 2 : 3;
|
||||||
|
}
|
||||||
|
window.editorMessage = "";
|
||||||
|
renderAndSubmit(window, frameId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.type !== "click") {
|
||||||
|
frameTimings.finishFrame(frameId, "discarded: timer editor ignored input");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (window.editorField < 3) {
|
||||||
|
window.editorField += 1;
|
||||||
|
} else {
|
||||||
|
const durationMs =
|
||||||
|
((window.editorHours * 60 + window.editorMinutes) * 60 + window.editorSeconds) * 1000;
|
||||||
|
if (durationMs <= 0) {
|
||||||
|
window.editorMessage = "Choose a duration";
|
||||||
|
window.editorField = 0;
|
||||||
|
} else {
|
||||||
|
addTimer(durationMs);
|
||||||
|
window.view = "timers";
|
||||||
|
window.timerSelection = timers.length + 1;
|
||||||
|
window.editorMessage = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateRenderTimer(window);
|
||||||
|
renderAndSubmit(window, frameId);
|
||||||
|
}
|
||||||
|
|
||||||
|
function openEditor(window: TimerWindow): void {
|
||||||
|
window.view = "editor";
|
||||||
|
window.editorField = 0;
|
||||||
|
window.editorHours = 0;
|
||||||
|
window.editorMinutes = 5;
|
||||||
|
window.editorSeconds = 0;
|
||||||
|
window.editorMessage = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function addTimer(durationMs: number): void {
|
||||||
|
// Millisecond epoch plus a small serial is unique across worker restarts and
|
||||||
|
// still safely representable as a JavaScript/Java long bridge value.
|
||||||
|
const id = Date.now() * 100 + (nextTimerSerial++ % 100);
|
||||||
|
const endAtMs = Date.now() + durationMs;
|
||||||
|
const timer: CountdownTimer = {
|
||||||
|
id,
|
||||||
|
durationMs,
|
||||||
|
endAtMs,
|
||||||
|
expired: false,
|
||||||
|
expiryTimeout: null,
|
||||||
|
};
|
||||||
|
timers.push(timer);
|
||||||
|
const durationLabel = formatDuration(durationMs);
|
||||||
|
scheduleTimerNotification(id, endAtMs, durationLabel);
|
||||||
|
timer.expiryTimeout = setTimeout(() => expireTimer(timer), Math.max(0, endAtMs - Date.now()));
|
||||||
|
}
|
||||||
|
|
||||||
|
function expireTimer(timer: CountdownTimer, renderWindows = true): void {
|
||||||
|
if (timer.expired) return;
|
||||||
|
timer.expired = true;
|
||||||
|
timer.expiryTimeout = null;
|
||||||
|
fireTimerNotification(timer.id, formatDuration(timer.durationMs));
|
||||||
|
for (const window of windows.values()) {
|
||||||
|
post({ type: "set-attention", windowId: window.windowId, attention: true });
|
||||||
|
updateRenderTimer(window);
|
||||||
|
if (renderWindows && window.foreground && screenOn) renderAndSubmit(window, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeTimer(timer: CountdownTimer): void {
|
||||||
|
if (timer.expiryTimeout) clearTimeout(timer.expiryTimeout);
|
||||||
|
cancelTimerNotification(timer.id);
|
||||||
|
const index = timers.indexOf(timer);
|
||||||
|
if (index >= 0) timers.splice(index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncExpiredTimers(): void {
|
||||||
|
const now = Date.now();
|
||||||
|
for (const timer of timers) {
|
||||||
|
// The caller is already about to paint, so avoid a nested render when a
|
||||||
|
// sleeping/backgrounded timer is first observed as finished.
|
||||||
|
if (!timer.expired && timer.endAtMs <= now) expireTimer(timer, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateRenderTimer(window: TimerWindow): void {
|
||||||
|
const activeCountdowns = timers.some((timer) => !timer.expired);
|
||||||
|
const dynamic =
|
||||||
|
(window.view === "stopwatch" && stopwatchRunningSinceMs !== null) ||
|
||||||
|
(window.view === "timers" && activeCountdowns);
|
||||||
|
const shouldRun = dynamic && window.foreground && screenOn;
|
||||||
|
if (shouldRun && window.renderTimer === null) {
|
||||||
|
window.renderTimer = setInterval(() => {
|
||||||
|
syncExpiredTimers();
|
||||||
|
renderAndSubmit(window, 0);
|
||||||
|
}, RENDER_INTERVAL_MS);
|
||||||
|
} else if (!shouldRun && window.renderTimer !== null) {
|
||||||
|
clearInterval(window.renderTimer);
|
||||||
|
window.renderTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function paint(window: TimerWindow): GrayImage {
|
||||||
|
syncExpiredTimers();
|
||||||
|
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
||||||
|
if (window.view === "stopwatch") {
|
||||||
|
paintStopwatch(image, window);
|
||||||
|
} else if (window.view === "timers") {
|
||||||
|
paintTimers(image, window);
|
||||||
|
} else {
|
||||||
|
paintEditor(image, window);
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
function paintStopwatch(image: GrayImage, window: TimerWindow): void {
|
||||||
|
drawTabs(image, "stopwatch");
|
||||||
|
const elapsed = stopwatchElapsedMs();
|
||||||
|
const timeLabel = formatStopwatchElapsed(elapsed);
|
||||||
|
const timeX = Math.max(0, Math.round((window.viewportWidth - largeFont.measureText(timeLabel)) / 2));
|
||||||
|
image.drawText(largeFont, timeX, 66, timeLabel, 245);
|
||||||
|
|
||||||
|
const state = stopwatchRunningSinceMs === null ? (elapsed > 0 ? "Paused" : "Ready") : "Running";
|
||||||
|
drawCenteredText(image, smallFont, 112, state, 145);
|
||||||
|
|
||||||
|
const labels = [stopwatchRunningSinceMs === null ? "Start" : "Pause", "Reset", `Timers (${timers.length})`];
|
||||||
|
drawButtonRow(image, labels, window.stopwatchAction, 154);
|
||||||
|
drawFooter(image, `${GESTURE_SCROLL} action ${GESTURE_CLICK} select ${GESTURE_DOUBLE_CLICK} back`);
|
||||||
|
}
|
||||||
|
|
||||||
|
function paintTimers(image: GrayImage, window: TimerWindow): void {
|
||||||
|
drawTabs(image, "timers");
|
||||||
|
const items = ["< Stopwatch", "+ New timer", ...timers.map(timerRowLabel)];
|
||||||
|
const listBottom = image.height - FOOTER_HEIGHT;
|
||||||
|
const visibleRows = Math.max(1, Math.floor((listBottom - TIMER_LIST_TOP) / TIMER_ROW_HEIGHT));
|
||||||
|
const first = clamp(window.timerSelection - visibleRows + 1, 0, Math.max(0, items.length - visibleRows));
|
||||||
|
|
||||||
|
for (let row = 0; row < visibleRows; row++) {
|
||||||
|
const index = first + row;
|
||||||
|
if (index >= items.length) break;
|
||||||
|
const y = TIMER_LIST_TOP + row * TIMER_ROW_HEIGHT;
|
||||||
|
const selected = index === window.timerSelection;
|
||||||
|
if (selected) {
|
||||||
|
image.fillRoundedRect(18, y, image.width - 36, TIMER_ROW_HEIGHT - 4, 34, 5);
|
||||||
|
image.drawRect(18, y, image.width - 36, TIMER_ROW_HEIGHT - 4, 105);
|
||||||
|
}
|
||||||
|
image.drawText(index >= 2 ? mediumFont : smallFont, 32, y + (index >= 2 ? 2 : 7), items[index]!, selected ? 245 : 180);
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectedTimer = window.timerSelection >= 2 ? timers[window.timerSelection - 2] : null;
|
||||||
|
const action = selectedTimer ? (selectedTimer.expired ? "dismiss" : "cancel") : "select";
|
||||||
|
drawFooter(image, `${GESTURE_SCROLL} timer ${GESTURE_CLICK} ${action} ${GESTURE_DOUBLE_CLICK} back`);
|
||||||
|
}
|
||||||
|
|
||||||
|
function paintEditor(image: GrayImage, window: TimerWindow): void {
|
||||||
|
image.drawText(smallFont, 24, 14, "New timer", 190);
|
||||||
|
const fields = [pad2(window.editorHours), pad2(window.editorMinutes), pad2(window.editorSeconds)];
|
||||||
|
const labels = ["hours", "minutes", "seconds"];
|
||||||
|
const fieldWidth = 118;
|
||||||
|
const gap = 22;
|
||||||
|
const totalWidth = fieldWidth * 3 + gap * 2;
|
||||||
|
const startX = Math.round((image.width - totalWidth) / 2);
|
||||||
|
|
||||||
|
for (let index = 0; index < 3; index++) {
|
||||||
|
const x = startX + index * (fieldWidth + gap);
|
||||||
|
if (window.editorField === index) {
|
||||||
|
image.fillRoundedRect(x, 57, fieldWidth, 68, 35, 6);
|
||||||
|
image.drawRect(x, 57, fieldWidth, 68, 120);
|
||||||
|
}
|
||||||
|
drawCenteredIn(image, largeFont, x, fieldWidth, 65, fields[index]!, window.editorField === index ? 250 : 180);
|
||||||
|
drawCenteredIn(image, smallFont, x, fieldWidth, 105, labels[index]!, 125);
|
||||||
|
if (index < 2) image.drawText(mediumFont, x + fieldWidth + 5, 71, ":", 130);
|
||||||
|
}
|
||||||
|
|
||||||
|
const startSelected = window.editorField === 3;
|
||||||
|
const buttonWidth = 160;
|
||||||
|
const buttonX = Math.round((image.width - buttonWidth) / 2);
|
||||||
|
if (startSelected) {
|
||||||
|
image.fillRoundedRect(buttonX, 151, buttonWidth, 38, 40, 6);
|
||||||
|
image.drawRect(buttonX, 151, buttonWidth, 38, 120);
|
||||||
|
}
|
||||||
|
drawCenteredIn(image, smallFont, buttonX, buttonWidth, 162, "Start timer", startSelected ? 250 : 175);
|
||||||
|
if (window.editorMessage) drawCenteredText(image, smallFont, 202, window.editorMessage, 190);
|
||||||
|
drawFooter(image, `${GESTURE_SCROLL} adjust ${GESTURE_CLICK} next ${GESTURE_DOUBLE_CLICK} cancel`);
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawTabs(image: GrayImage, active: "stopwatch" | "timers"): void {
|
||||||
|
const stopwatch = "Stopwatch";
|
||||||
|
const timerLabel = `Timers (${timers.length})`;
|
||||||
|
const left = 24;
|
||||||
|
const dividerX = left + smallFont.measureText(stopwatch) + 18;
|
||||||
|
image.drawText(smallFont, left, 15, stopwatch, active === "stopwatch" ? 245 : 105);
|
||||||
|
image.drawLine(left, 35, dividerX - 12, 35, active === "stopwatch" ? 200 : 30);
|
||||||
|
image.drawText(smallFont, dividerX, 15, timerLabel, active === "timers" ? 245 : 105);
|
||||||
|
image.drawLine(dividerX, 35, dividerX + smallFont.measureText(timerLabel), 35, active === "timers" ? 200 : 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawButtonRow(image: GrayImage, labels: string[], selected: number, y: number): void {
|
||||||
|
const gap = 12;
|
||||||
|
const margin = 24;
|
||||||
|
const width = Math.floor((image.width - margin * 2 - gap * (labels.length - 1)) / labels.length);
|
||||||
|
for (let index = 0; index < labels.length; index++) {
|
||||||
|
const x = margin + index * (width + gap);
|
||||||
|
if (index === selected) {
|
||||||
|
image.fillRoundedRect(x, y, width, 40, 38, 6);
|
||||||
|
image.drawRect(x, y, width, 40, 115);
|
||||||
|
}
|
||||||
|
drawCenteredIn(image, smallFont, x, width, y + 12, labels[index]!, index === selected ? 245 : 155);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawFooter(image: GrayImage, text: string): void {
|
||||||
|
image.drawLine(16, image.height - FOOTER_HEIGHT, image.width - 16, image.height - FOOTER_HEIGHT, 35);
|
||||||
|
drawCenteredText(image, smallFont, image.height - 22, text, 115);
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawCenteredText(image: GrayImage, font: typeof smallFont, y: number, text: string, value: number): void {
|
||||||
|
image.drawText(font, Math.round((image.width - font.measureText(text)) / 2), y, text, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawCenteredIn(
|
||||||
|
image: GrayImage,
|
||||||
|
font: typeof smallFont,
|
||||||
|
x: number,
|
||||||
|
width: number,
|
||||||
|
y: number,
|
||||||
|
text: string,
|
||||||
|
value: number,
|
||||||
|
): void {
|
||||||
|
image.drawText(font, Math.round(x + (width - font.measureText(text)) / 2), y, text, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function timerRowLabel(timer: CountdownTimer): string {
|
||||||
|
const number = timers.indexOf(timer) + 1;
|
||||||
|
if (timer.expired) return `${number}. 00:00 Finished`;
|
||||||
|
return `${number}. ${formatCountdown(timer.endAtMs - Date.now())} Running`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
|
||||||
|
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
|
||||||
|
try {
|
||||||
|
const paintStartedAtMs = Date.now();
|
||||||
|
const image = frameTimings.span(frameId, "paint", () =>
|
||||||
|
frameTimings.runWithFrame(frameId, () => paint(window)),
|
||||||
|
);
|
||||||
|
const paintMs = Date.now() - paintStartedAtMs;
|
||||||
|
const fingerprint = image.fingerprint();
|
||||||
|
if (fingerprint === window.lastSubmittedFingerprint) {
|
||||||
|
frameTimings.finishFrame(frameId, "discarded: timer content unchanged");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||||
|
if (!communicator) {
|
||||||
|
frameTimings.finishFrame(frameId, "discarded: no active communicator");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const buffer = image.to8bppBuffer();
|
||||||
|
communicator.submitSurfaceFrame(
|
||||||
|
buffer.buffer,
|
||||||
|
window.surfaceId,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
image.width,
|
||||||
|
image.height,
|
||||||
|
fingerprint,
|
||||||
|
paintMs,
|
||||||
|
frameId,
|
||||||
|
);
|
||||||
|
window.lastSubmittedFingerprint = fingerprint;
|
||||||
|
} catch (error) {
|
||||||
|
frameTimings.finishFrame(frameId, "discarded: timer render failed");
|
||||||
|
console.error(`timer worker render failed: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopwatchElapsedMs(): number {
|
||||||
|
const runningMs = stopwatchRunningSinceMs === null ? 0 : Date.now() - stopwatchRunningSinceMs;
|
||||||
|
return stopwatchAccumulatedMs + runningMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
function pad2(value: number): string {
|
||||||
|
return String(value).padStart(2, "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatStopwatchElapsed(totalMs: number): string {
|
||||||
|
const totalTenths = Math.floor(totalMs / 100);
|
||||||
|
const tenths = totalTenths % 10;
|
||||||
|
const totalSeconds = Math.floor(totalTenths / 10);
|
||||||
|
const seconds = totalSeconds % 60;
|
||||||
|
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||||
|
const minutes = totalMinutes % 60;
|
||||||
|
const hours = Math.floor(totalMinutes / 60);
|
||||||
|
return hours > 0
|
||||||
|
? `${hours}:${pad2(minutes)}:${pad2(seconds)}.${tenths}`
|
||||||
|
: `${pad2(minutes)}:${pad2(seconds)}.${tenths}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatCountdown(remainingMs: number): string {
|
||||||
|
const totalSeconds = Math.max(0, Math.ceil(remainingMs / 1000));
|
||||||
|
const seconds = totalSeconds % 60;
|
||||||
|
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||||
|
const minutes = totalMinutes % 60;
|
||||||
|
const hours = Math.floor(totalMinutes / 60);
|
||||||
|
return hours > 0 ? `${hours}:${pad2(minutes)}:${pad2(seconds)}` : `${pad2(minutes)}:${pad2(seconds)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatDuration(durationMs: number): string {
|
||||||
|
const totalSeconds = Math.round(durationMs / 1000);
|
||||||
|
const seconds = totalSeconds % 60;
|
||||||
|
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||||
|
const minutes = totalMinutes % 60;
|
||||||
|
const hours = Math.floor(totalMinutes / 60);
|
||||||
|
if (hours > 0) return `${hours}h ${minutes}m ${seconds}s`;
|
||||||
|
if (minutes > 0) return `${minutes}m ${seconds}s`;
|
||||||
|
return `${seconds}s`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function clamp(value: number, min: number, max: number): number {
|
||||||
|
return Math.max(min, Math.min(max, value));
|
||||||
|
}
|
||||||
Reference in new issue
Block a user