Merge branch 'main' into ble-bandwidth-test
This commit is contained in:
commit
50cbc3a756
198 files changed
+15988
-2899
No files matched your search
@@ -25,7 +25,17 @@
|
||||
<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.POST_NOTIFICATIONS"/>
|
||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
|
||||
<!-- Alarm clock reliability (Timers app): USE_EXACT_ALARM is granted
|
||||
outright on Android 13+ for alarm-clock apps and cannot be revoked
|
||||
by accident; SCHEDULE_EXACT_ALARM covers Android 12. The receiver
|
||||
re-arms alarms after reboots; the ringing service plays on the alarm
|
||||
stream and shows a full-screen alarm over the lock screen. -->
|
||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" android:maxSdkVersion="32"/>
|
||||
<uses-permission android:name="android.permission.USE_EXACT_ALARM"/>
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
|
||||
<uses-permission android:name="android.permission.READ_CALENDAR"/>
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
|
||||
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
|
||||
@@ -127,10 +137,40 @@
|
||||
<data android:scheme="wear" android:host="*" android:pathPrefix="/" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
<!-- Timers app alarm clock: the AlarmManager target, the reboot /
|
||||
time-change rescheduler, the ringing foreground service, and the
|
||||
lock-screen ringing activity. All Java, so alarms ring with the
|
||||
JavaScript side asleep or gone. -->
|
||||
<receiver
|
||||
android:name="com.faceclaw.app.FaceclawTimerReceiver"
|
||||
android:name="com.faceclaw.app.FaceclawAlarmReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false" />
|
||||
<receiver
|
||||
android:name="com.faceclaw.app.FaceclawAlarmRescheduleReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
|
||||
<action android:name="android.intent.action.TIME_SET" />
|
||||
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
|
||||
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
|
||||
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<service
|
||||
android:name="com.faceclaw.app.FaceclawAlarmService"
|
||||
android:enabled="true"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="mediaPlayback" />
|
||||
<activity
|
||||
android:name="com.faceclaw.app.FaceclawAlarmActivity"
|
||||
android:exported="false"
|
||||
android:excludeFromRecents="true"
|
||||
android:launchMode="singleInstance"
|
||||
android:showWhenLocked="true"
|
||||
android:turnScreenOn="true"
|
||||
android:theme="@android:style/Theme.Material.NoActionBar" />
|
||||
<!-- adb-only settings export/import (see FaceclawSettingsPortReceiver).
|
||||
DUMP is a development permission: the adb shell holds it, other
|
||||
apps cannot get it granted without adb, so this stays reachable
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.KeyguardManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.TypedValue;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The phone's ringing screen: shown over the lock screen by the alarm
|
||||
* notification's full-screen intent (or by tapping the notification). Pure
|
||||
* Java, so it works with the JavaScript side asleep or gone. Snooze and
|
||||
* Dismiss go to the service, which journals them for the engine.
|
||||
*/
|
||||
public class FaceclawAlarmActivity extends Activity {
|
||||
private final Handler handler = new Handler(Looper.getMainLooper());
|
||||
private TextView titleView;
|
||||
private TextView textView;
|
||||
private TextView timeView;
|
||||
private Button snoozeButton;
|
||||
private long itemId;
|
||||
|
||||
private final Runnable poll = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<FaceclawAlarmService.Ringing> items = FaceclawAlarmService.snapshot();
|
||||
if (items.isEmpty()) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
FaceclawAlarmService.Ringing shown = null;
|
||||
for (FaceclawAlarmService.Ringing item : items) {
|
||||
if (item.id == itemId) {
|
||||
shown = item;
|
||||
}
|
||||
}
|
||||
if (shown == null) {
|
||||
shown = items.get(0);
|
||||
itemId = shown.id;
|
||||
}
|
||||
render(shown, items.size());
|
||||
handler.postDelayed(this, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
showOverLockScreen();
|
||||
itemId = getIntent() == null ? 0 : getIntent().getLongExtra(FaceclawAlarms.EXTRA_ID, 0);
|
||||
setContentView(buildLayout());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
setIntent(intent);
|
||||
itemId = intent == null ? 0 : intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
handler.post(poll);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
handler.removeCallbacks(poll);
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
private void showOverLockScreen() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
|
||||
setShowWhenLocked(true);
|
||||
setTurnScreenOn(true);
|
||||
KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
|
||||
if (keyguard != null) {
|
||||
keyguard.requestDismissKeyguard(this, null);
|
||||
}
|
||||
} else {
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
|
||||
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
|
||||
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
|
||||
}
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
private View buildLayout() {
|
||||
LinearLayout root = new LinearLayout(this);
|
||||
root.setOrientation(LinearLayout.VERTICAL);
|
||||
root.setGravity(Gravity.CENTER);
|
||||
root.setBackgroundColor(Color.BLACK);
|
||||
int pad = dp(32);
|
||||
root.setPadding(pad, pad, pad, pad);
|
||||
|
||||
timeView = new TextView(this);
|
||||
timeView.setTextColor(Color.WHITE);
|
||||
timeView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 64);
|
||||
timeView.setTypeface(Typeface.DEFAULT_BOLD);
|
||||
timeView.setGravity(Gravity.CENTER);
|
||||
root.addView(timeView, wrap());
|
||||
|
||||
titleView = new TextView(this);
|
||||
titleView.setTextColor(Color.WHITE);
|
||||
titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28);
|
||||
titleView.setGravity(Gravity.CENTER);
|
||||
titleView.setPadding(0, dp(16), 0, 0);
|
||||
root.addView(titleView, wrap());
|
||||
|
||||
textView = new TextView(this);
|
||||
textView.setTextColor(Color.LTGRAY);
|
||||
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
|
||||
textView.setGravity(Gravity.CENTER);
|
||||
textView.setPadding(0, dp(8), 0, dp(40));
|
||||
root.addView(textView, wrap());
|
||||
|
||||
snoozeButton = new Button(this);
|
||||
snoozeButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
|
||||
snoozeButton.setOnClickListener(view -> act(FaceclawAlarmService.ACTION_SNOOZE));
|
||||
root.addView(snoozeButton, button());
|
||||
|
||||
Button dismissButton = new Button(this);
|
||||
dismissButton.setText("Dismiss");
|
||||
dismissButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
|
||||
dismissButton.setOnClickListener(view -> act(FaceclawAlarmService.ACTION_DISMISS));
|
||||
root.addView(dismissButton, button());
|
||||
return root;
|
||||
}
|
||||
|
||||
private void render(FaceclawAlarmService.Ringing item, int count) {
|
||||
timeView.setText(DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date()));
|
||||
titleView.setText(count > 1 ? item.title + " (+" + (count - 1) + " more)" : item.title);
|
||||
String status = item.silenced ? "Not answered" : item.escalated ? "" : "Ringing on the glasses";
|
||||
textView.setText(item.text.isEmpty() ? status : status.isEmpty() ? item.text : item.text + "\n" + status);
|
||||
snoozeButton.setText(FaceclawAlarms.KIND_TIMER.equals(item.kind)
|
||||
? "+" + item.snoozeMinutes + " min"
|
||||
: "Snooze " + item.snoozeMinutes + " min");
|
||||
}
|
||||
|
||||
/** Apply the action to every ringing item (one tap silences the phone). */
|
||||
private void act(String action) {
|
||||
for (FaceclawAlarmService.Ringing item : FaceclawAlarmService.snapshot()) {
|
||||
Intent intent = new Intent(this, FaceclawAlarmService.class)
|
||||
.setAction(action)
|
||||
.putExtra(FaceclawAlarms.EXTRA_ID, item.id);
|
||||
startService(intent);
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
private LinearLayout.LayoutParams wrap() {
|
||||
return new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
||||
}
|
||||
|
||||
private LinearLayout.LayoutParams button() {
|
||||
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp(64));
|
||||
params.topMargin = dp(12);
|
||||
return params;
|
||||
}
|
||||
|
||||
private int dp(int value) {
|
||||
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Phone-side alarm actions (dismiss / snooze), delivered to the JS engine; implemented in JS. */
|
||||
public interface FaceclawAlarmListener {
|
||||
void onPhoneAction(long id, String action, int minutes);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
/** The AlarmManager target: an item came due, start the ringing service. */
|
||||
public class FaceclawAlarmReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent == null || !FaceclawAlarms.ACTION_EXPIRE.equals(intent.getAction())) {
|
||||
return;
|
||||
}
|
||||
long id = intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0);
|
||||
if (id == 0) {
|
||||
return;
|
||||
}
|
||||
FaceclawAlarmService.ring(
|
||||
context.getApplicationContext(),
|
||||
id,
|
||||
intent.getStringExtra(FaceclawAlarms.EXTRA_TITLE),
|
||||
intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT),
|
||||
intent.getStringExtra(FaceclawAlarms.EXTRA_KIND),
|
||||
intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10)
|
||||
);
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
/**
|
||||
* Re-arms the persisted alarm schedule when the system would otherwise have
|
||||
* dropped it: after a reboot (AlarmManager alarms do not survive one), a
|
||||
* package update, or a wall-clock / time-zone change. Runs without the
|
||||
* JavaScript side; only the device-protected schedule file is needed.
|
||||
*/
|
||||
public class FaceclawAlarmRescheduleReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent == null ? null : intent.getAction();
|
||||
if (action == null) {
|
||||
return;
|
||||
}
|
||||
switch (action) {
|
||||
case Intent.ACTION_BOOT_COMPLETED:
|
||||
case Intent.ACTION_LOCKED_BOOT_COMPLETED:
|
||||
case Intent.ACTION_MY_PACKAGE_REPLACED:
|
||||
case Intent.ACTION_TIME_CHANGED:
|
||||
case Intent.ACTION_TIMEZONE_CHANGED:
|
||||
case "android.intent.action.QUICKBOOT_POWERON":
|
||||
case "com.htc.intent.action.QUICKBOOT_POWERON":
|
||||
FaceclawAlarms.rescheduleAll(context, action);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,649 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioFocusRequest;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Looper;
|
||||
import android.os.PowerManager;
|
||||
import android.os.VibrationEffect;
|
||||
import android.os.Vibrator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The ringing foreground service. One instance carries every item currently
|
||||
* ringing. Each item starts silent on the phone (a heads-up / lock-screen
|
||||
* notification only, the glasses do the ringing) and escalates to phone
|
||||
* sound and vibration when the glasses cannot carry it: not connected, not
|
||||
* on a head, charging, no delivery confirmation from the JS side within a
|
||||
* few seconds, or no acknowledgement within 30 seconds of delivery. Sound
|
||||
* uses the alarm stream, so media volume, the ringer switch and (unless set
|
||||
* to total silence) Do Not Disturb do not silence it.
|
||||
*/
|
||||
public class FaceclawAlarmService extends Service {
|
||||
public static final String CHANNEL_ID = "faceclaw-alarms";
|
||||
static final String ACTION_RING = "com.faceclaw.app.action.ALARM_RING";
|
||||
static final String ACTION_DELIVERED = "com.faceclaw.app.action.ALARM_DELIVERED";
|
||||
static final String ACTION_DISMISS = "com.faceclaw.app.action.ALARM_DISMISS";
|
||||
static final String ACTION_SNOOZE = "com.faceclaw.app.action.ALARM_SNOOZE";
|
||||
/** Stop an item quietly (acknowledged on the glasses / cancelled by the engine). */
|
||||
static final String ACTION_STOP_ITEM = "com.faceclaw.app.action.ALARM_STOP_ITEM";
|
||||
|
||||
/** How long to wait for the JS side to confirm the glasses are showing the item. */
|
||||
static final long DELIVERY_WAIT_MS = 5_000L;
|
||||
/** How long after delivery to the glasses before the phone joins in. */
|
||||
static final long ACK_WAIT_MS = 30_000L;
|
||||
/** Sound and vibration stop after this; the notification stays. */
|
||||
static final long AUTO_SILENCE_MS = 10 * 60_000L;
|
||||
/** Alarm-stream level (fraction of max) used when the wearer has it muted. */
|
||||
static final float MUTED_VOLUME_FRACTION = 0.6f;
|
||||
|
||||
static final class Ringing {
|
||||
final long id;
|
||||
final String title;
|
||||
final String text;
|
||||
final String kind;
|
||||
final int snoozeMinutes;
|
||||
final long ringAtMs;
|
||||
long deliveredAtMs;
|
||||
boolean escalated;
|
||||
boolean silenced;
|
||||
|
||||
Ringing(long id, String title, String text, String kind, int snoozeMinutes) {
|
||||
this.id = id;
|
||||
this.title = title == null || title.trim().isEmpty() ? "Alarm" : title;
|
||||
this.text = text == null ? "" : text;
|
||||
this.kind = kind == null ? FaceclawAlarms.KIND_ALARM : kind;
|
||||
this.snoozeMinutes = Math.max(1, snoozeMinutes);
|
||||
this.ringAtMs = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
/** Ringing items, oldest first; static so the activity can read them. */
|
||||
private static final Map<Long, Ringing> ringing = Collections.synchronizedMap(new LinkedHashMap<>());
|
||||
/**
|
||||
* Signals that arrived before the ring intent was processed (the JS
|
||||
* engine fires, launches its window and reports delivery on the same
|
||||
* main thread the service's onStartCommand queues behind), keyed by id
|
||||
* with their arrival time. Consumed by startRinging.
|
||||
*/
|
||||
private static final Map<Long, Long> deliveredEarly = Collections.synchronizedMap(new LinkedHashMap<>());
|
||||
private static final Map<Long, Long> stoppedEarly = Collections.synchronizedMap(new LinkedHashMap<>());
|
||||
private static final long EARLY_SIGNAL_MAX_AGE_MS = 15_000L;
|
||||
|
||||
private final Handler handler = new Handler(Looper.getMainLooper());
|
||||
private MediaPlayer player;
|
||||
private Vibrator vibrator;
|
||||
private PowerManager.WakeLock wakeLock;
|
||||
private AudioManager audioManager;
|
||||
private AudioFocusRequest focusRequest;
|
||||
private int restoreAlarmVolume = -1;
|
||||
private long foregroundId;
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Static entry points
|
||||
|
||||
public static void ring(Context context, long id, String title, String text, String kind, int snoozeMinutes) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
Intent intent = new Intent(appContext, FaceclawAlarmService.class)
|
||||
.setAction(ACTION_RING)
|
||||
.putExtra(FaceclawAlarms.EXTRA_ID, id)
|
||||
.putExtra(FaceclawAlarms.EXTRA_TITLE, title)
|
||||
.putExtra(FaceclawAlarms.EXTRA_TEXT, text)
|
||||
.putExtra(FaceclawAlarms.EXTRA_KIND, kind)
|
||||
.putExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, snoozeMinutes);
|
||||
FaceclawAlarms.startServiceCompat(appContext, intent);
|
||||
}
|
||||
|
||||
public static void delivered(Context context, long id) {
|
||||
if (!ringing.containsKey(id)) {
|
||||
deliveredEarly.put(id, System.currentTimeMillis());
|
||||
return;
|
||||
}
|
||||
Context appContext = context.getApplicationContext();
|
||||
appContext.startService(new Intent(appContext, FaceclawAlarmService.class)
|
||||
.setAction(ACTION_DELIVERED)
|
||||
.putExtra(FaceclawAlarms.EXTRA_ID, id));
|
||||
}
|
||||
|
||||
public static void stopItem(Context context, long id) {
|
||||
if (!ringing.containsKey(id)) {
|
||||
stoppedEarly.put(id, System.currentTimeMillis());
|
||||
return;
|
||||
}
|
||||
Context appContext = context.getApplicationContext();
|
||||
appContext.startService(new Intent(appContext, FaceclawAlarmService.class)
|
||||
.setAction(ACTION_STOP_ITEM)
|
||||
.putExtra(FaceclawAlarms.EXTRA_ID, id));
|
||||
}
|
||||
|
||||
public static boolean isRinging(long id) {
|
||||
return ringing.containsKey(id);
|
||||
}
|
||||
|
||||
public static boolean isAnythingRinging() {
|
||||
return !ringing.isEmpty();
|
||||
}
|
||||
|
||||
static List<Ringing> snapshot() {
|
||||
synchronized (ringing) {
|
||||
return new ArrayList<>(ringing.values());
|
||||
}
|
||||
}
|
||||
|
||||
static PendingIntent actionIntent(Context appContext, String action, long id) {
|
||||
Intent intent = new Intent(appContext, FaceclawAlarmService.class)
|
||||
.setAction(action)
|
||||
.putExtra(FaceclawAlarms.EXTRA_ID, id);
|
||||
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
flags |= PendingIntent.FLAG_IMMUTABLE;
|
||||
}
|
||||
int code = FaceclawAlarms.requestCode(id) ^ action.hashCode();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
return PendingIntent.getForegroundService(appContext, code, intent, flags);
|
||||
}
|
||||
return PendingIntent.getService(appContext, code, intent, flags);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Service lifecycle
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
String action = intent == null ? null : intent.getAction();
|
||||
long id = intent == null ? 0 : intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0);
|
||||
if (action == null) {
|
||||
// Restarted by the system with nothing to do.
|
||||
finishIfIdle();
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
switch (action) {
|
||||
case ACTION_RING:
|
||||
startRinging(new Ringing(
|
||||
id,
|
||||
intent.getStringExtra(FaceclawAlarms.EXTRA_TITLE),
|
||||
intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT),
|
||||
intent.getStringExtra(FaceclawAlarms.EXTRA_KIND),
|
||||
intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10)
|
||||
));
|
||||
break;
|
||||
case ACTION_DELIVERED:
|
||||
markDelivered(id);
|
||||
break;
|
||||
case ACTION_DISMISS:
|
||||
dismiss(id);
|
||||
break;
|
||||
case ACTION_SNOOZE:
|
||||
snooze(id);
|
||||
break;
|
||||
case ACTION_STOP_ITEM:
|
||||
stopItemQuietly(id, "acknowledged");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
finishIfIdle();
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
stopSound();
|
||||
releaseWakeLock();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Ringing
|
||||
|
||||
private void startRinging(Ringing item) {
|
||||
if (item.id == 0) {
|
||||
return;
|
||||
}
|
||||
ensureChannel();
|
||||
Ringing existing = ringing.get(item.id);
|
||||
if (existing != null) {
|
||||
// The engine's JS timeout and the AlarmManager alarm both fired: one ring.
|
||||
showNotification(existing);
|
||||
return;
|
||||
}
|
||||
if (takeEarlySignal(stoppedEarly, item.id)) {
|
||||
// Acknowledged or cancelled before this intent was processed.
|
||||
FaceclawAlarms.log(this, "ring " + item.id + " already acknowledged");
|
||||
return;
|
||||
}
|
||||
ringing.put(item.id, item);
|
||||
FaceclawAlarms.log(this, "ring " + item.kind + " " + item.id + " (" + FaceclawAlarms.glassesStatusDescription() + ")");
|
||||
acquireWakeLock();
|
||||
showNotification(item);
|
||||
if (!FaceclawAlarms.glassesCanCarryAlarm()) {
|
||||
escalate(item, FaceclawAlarms.glassesStatusDescription());
|
||||
return;
|
||||
}
|
||||
if (takeEarlySignal(deliveredEarly, item.id)) {
|
||||
markDelivered(item.id);
|
||||
return;
|
||||
}
|
||||
// The glasses could carry it: give the JS side a moment to confirm
|
||||
// they are actually showing it, then hold for the acknowledgement.
|
||||
handler.postDelayed(() -> {
|
||||
Ringing current = ringing.get(item.id);
|
||||
if (current != null && !current.escalated && current.deliveredAtMs == 0) {
|
||||
escalate(current, "not delivered to the glasses");
|
||||
}
|
||||
}, DELIVERY_WAIT_MS);
|
||||
}
|
||||
|
||||
/** Consume an early signal for the id; stale entries are dropped. */
|
||||
private static boolean takeEarlySignal(Map<Long, Long> signals, long id) {
|
||||
Long at = signals.remove(id);
|
||||
long now = System.currentTimeMillis();
|
||||
synchronized (signals) {
|
||||
signals.values().removeIf(stamp -> now - stamp > EARLY_SIGNAL_MAX_AGE_MS);
|
||||
}
|
||||
return at != null && now - at <= EARLY_SIGNAL_MAX_AGE_MS;
|
||||
}
|
||||
|
||||
private void markDelivered(long id) {
|
||||
Ringing item = ringing.get(id);
|
||||
if (item == null || item.deliveredAtMs != 0) {
|
||||
return;
|
||||
}
|
||||
item.deliveredAtMs = System.currentTimeMillis();
|
||||
FaceclawAlarms.log(this, "delivered " + id + " to glasses");
|
||||
handler.postDelayed(() -> {
|
||||
Ringing current = ringing.get(id);
|
||||
if (current != null && !current.escalated) {
|
||||
escalate(current, "not acknowledged on the glasses");
|
||||
}
|
||||
}, ACK_WAIT_MS);
|
||||
}
|
||||
|
||||
private void escalate(Ringing item, String reason) {
|
||||
if (item.escalated) {
|
||||
return;
|
||||
}
|
||||
item.escalated = true;
|
||||
FaceclawAlarms.log(this, "phone sound for " + item.id + ": " + reason);
|
||||
showNotification(item);
|
||||
startSound();
|
||||
handler.postDelayed(() -> {
|
||||
Ringing current = ringing.get(item.id);
|
||||
if (current != null && current.escalated && !current.silenced) {
|
||||
current.silenced = true;
|
||||
FaceclawAlarms.log(this, "auto-silenced " + current.id);
|
||||
showNotification(current);
|
||||
syncSound();
|
||||
}
|
||||
}, AUTO_SILENCE_MS);
|
||||
}
|
||||
|
||||
private void dismiss(long id) {
|
||||
Ringing item = ringing.get(id);
|
||||
if (item == null) {
|
||||
return;
|
||||
}
|
||||
FaceclawAlarms.recordPhoneAction(this, id, "dismiss", 0);
|
||||
removeItem(item);
|
||||
// Also drop the schedule entry (a one-off is over; the engine re-arms repeats).
|
||||
FaceclawAlarms.cancel(this, id);
|
||||
}
|
||||
|
||||
private void snooze(long id) {
|
||||
Ringing item = ringing.get(id);
|
||||
if (item == null) {
|
||||
return;
|
||||
}
|
||||
int minutes = item.snoozeMinutes;
|
||||
FaceclawAlarms.recordPhoneAction(this, id, "snooze", minutes);
|
||||
removeItem(item);
|
||||
// Re-arm on the phone directly so the snooze holds even with the JS
|
||||
// side gone; the engine replays the journal and lands on the same id.
|
||||
FaceclawAlarms.schedule(
|
||||
this,
|
||||
id,
|
||||
System.currentTimeMillis() + minutes * 60_000L,
|
||||
item.title,
|
||||
item.text,
|
||||
item.kind,
|
||||
minutes
|
||||
);
|
||||
}
|
||||
|
||||
private void stopItemQuietly(long id, String reason) {
|
||||
Ringing item = ringing.get(id);
|
||||
if (item == null) {
|
||||
return;
|
||||
}
|
||||
FaceclawAlarms.log(this, reason + " " + id);
|
||||
removeItem(item);
|
||||
}
|
||||
|
||||
private void removeItem(Ringing item) {
|
||||
ringing.remove(item.id);
|
||||
NotificationManager manager = getSystemService(NotificationManager.class);
|
||||
if (manager != null) {
|
||||
manager.cancel(notificationId(item.id));
|
||||
}
|
||||
syncSound();
|
||||
if (item.id == foregroundId) {
|
||||
promoteAnotherToForeground();
|
||||
}
|
||||
}
|
||||
|
||||
private void finishIfIdle() {
|
||||
if (!ringing.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
stopSound();
|
||||
releaseWakeLock();
|
||||
stopForeground(STOP_FOREGROUND_REMOVE);
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Notification (the phone's silent display of the alarm)
|
||||
|
||||
private void showNotification(Ringing item) {
|
||||
Notification notification = buildNotification(item);
|
||||
if (foregroundId == 0 || foregroundId == item.id || !ringing.containsKey(foregroundId)) {
|
||||
foregroundId = item.id;
|
||||
startForegroundCompat(notificationId(item.id), notification);
|
||||
} else {
|
||||
NotificationManager manager = getSystemService(NotificationManager.class);
|
||||
if (manager != null) {
|
||||
manager.notify(notificationId(item.id), notification);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void promoteAnotherToForeground() {
|
||||
List<Ringing> items = snapshot();
|
||||
if (items.isEmpty()) {
|
||||
foregroundId = 0;
|
||||
return;
|
||||
}
|
||||
Ringing next = items.get(0);
|
||||
foregroundId = next.id;
|
||||
startForegroundCompat(notificationId(next.id), buildNotification(next));
|
||||
}
|
||||
|
||||
private void startForegroundCompat(int id, Notification notification) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
startForeground(id, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
|
||||
} else {
|
||||
startForeground(id, notification);
|
||||
}
|
||||
}
|
||||
|
||||
private Notification buildNotification(Ringing item) {
|
||||
Intent activity = new Intent(this, FaceclawAlarmActivity.class)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
.putExtra(FaceclawAlarms.EXTRA_ID, item.id);
|
||||
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
flags |= PendingIntent.FLAG_IMMUTABLE;
|
||||
}
|
||||
PendingIntent open = PendingIntent.getActivity(this, FaceclawAlarms.requestCode(item.id), activity, flags);
|
||||
|
||||
String status;
|
||||
if (item.silenced) {
|
||||
status = "Not answered";
|
||||
} else if (item.escalated) {
|
||||
status = "Ringing";
|
||||
} else {
|
||||
status = "Ringing on the glasses";
|
||||
}
|
||||
String text = item.text.isEmpty() ? status : item.text + " · " + status;
|
||||
|
||||
Notification.Builder builder = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
|
||||
? new Notification.Builder(this, CHANNEL_ID)
|
||||
: new Notification.Builder(this).setPriority(Notification.PRIORITY_MAX);
|
||||
builder.setContentTitle(item.title)
|
||||
.setContentText(text)
|
||||
.setSmallIcon(getApplicationInfo().icon)
|
||||
.setContentIntent(open)
|
||||
.setFullScreenIntent(open, true)
|
||||
.setCategory(Notification.CATEGORY_ALARM)
|
||||
.setVisibility(Notification.VISIBILITY_PUBLIC)
|
||||
.setOngoing(true)
|
||||
.setAutoCancel(false)
|
||||
.setOnlyAlertOnce(true)
|
||||
.setWhen(item.ringAtMs)
|
||||
.setShowWhen(true)
|
||||
.addAction(new Notification.Action.Builder(
|
||||
null,
|
||||
FaceclawAlarms.KIND_TIMER.equals(item.kind) ? "+" + item.snoozeMinutes + " min" : "Snooze " + item.snoozeMinutes + " min",
|
||||
actionIntent(this, ACTION_SNOOZE, item.id)).build())
|
||||
.addAction(new Notification.Action.Builder(
|
||||
null,
|
||||
"Dismiss",
|
||||
actionIntent(this, ACTION_DISMISS, item.id)).build());
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
// Pre-channel: the builder controls sound, and we want none.
|
||||
builder.setSound(null).setVibrate(null);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private void ensureChannel() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
return;
|
||||
}
|
||||
NotificationManager manager = getSystemService(NotificationManager.class);
|
||||
if (manager == null) {
|
||||
return;
|
||||
}
|
||||
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Alarms", NotificationManager.IMPORTANCE_HIGH);
|
||||
channel.setDescription("Timers and alarms going off. Silent here: the sound is played by the alarm itself.");
|
||||
// The service plays the sound itself (on the alarm stream) so the
|
||||
// notification stays silent and vibration-free on purpose.
|
||||
channel.setSound(null, null);
|
||||
channel.enableVibration(false);
|
||||
channel.setBypassDnd(true);
|
||||
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
|
||||
manager.createNotificationChannel(channel);
|
||||
}
|
||||
|
||||
private static int notificationId(long id) {
|
||||
return 0x41000000 | (FaceclawAlarms.requestCode(id) & 0x00ffffff);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Sound and vibration (only while some item is escalated and not yet silenced)
|
||||
|
||||
private void syncSound() {
|
||||
boolean wanted = false;
|
||||
for (Ringing item : snapshot()) {
|
||||
if (item.escalated && !item.silenced) {
|
||||
wanted = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (wanted) {
|
||||
startSound();
|
||||
} else {
|
||||
stopSound();
|
||||
}
|
||||
}
|
||||
|
||||
private void startSound() {
|
||||
if (player != null) {
|
||||
return;
|
||||
}
|
||||
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
|
||||
AudioAttributes attributes = new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_ALARM)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.build();
|
||||
raiseMutedAlarmVolume();
|
||||
requestAudioFocus(attributes);
|
||||
Uri tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
|
||||
if (tone == null) {
|
||||
tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
|
||||
}
|
||||
try {
|
||||
MediaPlayer created = new MediaPlayer();
|
||||
created.setAudioAttributes(attributes);
|
||||
created.setDataSource(this, tone);
|
||||
created.setLooping(true);
|
||||
created.prepare();
|
||||
created.start();
|
||||
player = created;
|
||||
} catch (Exception error) {
|
||||
FaceclawAlarms.log(this, "alarm sound failed: " + error.getMessage());
|
||||
player = null;
|
||||
}
|
||||
startVibration();
|
||||
}
|
||||
|
||||
private void stopSound() {
|
||||
if (player != null) {
|
||||
try {
|
||||
player.stop();
|
||||
} catch (RuntimeException ignored) {
|
||||
// Already stopped.
|
||||
}
|
||||
player.release();
|
||||
player = null;
|
||||
}
|
||||
if (vibrator != null) {
|
||||
vibrator.cancel();
|
||||
vibrator = null;
|
||||
}
|
||||
abandonAudioFocus();
|
||||
restoreAlarmVolume();
|
||||
}
|
||||
|
||||
/**
|
||||
* A muted alarm stream is the one thing that would keep an alarm clock
|
||||
* silent; raise it for the duration of the ring and put it back after.
|
||||
*/
|
||||
private void raiseMutedAlarmVolume() {
|
||||
if (audioManager == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int current = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
|
||||
int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
|
||||
if (current == 0 && max > 0) {
|
||||
restoreAlarmVolume = current;
|
||||
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, Math.max(1, Math.round(max * MUTED_VOLUME_FRACTION)), 0);
|
||||
}
|
||||
} catch (RuntimeException error) {
|
||||
FaceclawAlarms.log(this, "alarm volume change failed: " + error.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void restoreAlarmVolume() {
|
||||
if (audioManager == null || restoreAlarmVolume < 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, restoreAlarmVolume, 0);
|
||||
} catch (RuntimeException ignored) {
|
||||
// Best effort.
|
||||
}
|
||||
restoreAlarmVolume = -1;
|
||||
}
|
||||
|
||||
private void requestAudioFocus(AudioAttributes attributes) {
|
||||
if (audioManager == null) {
|
||||
return;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
|
||||
.setAudioAttributes(attributes)
|
||||
.build();
|
||||
audioManager.requestAudioFocus(focusRequest);
|
||||
} else {
|
||||
audioManager.requestAudioFocus(null, AudioManager.STREAM_ALARM, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
|
||||
}
|
||||
}
|
||||
|
||||
private void abandonAudioFocus() {
|
||||
if (audioManager == null) {
|
||||
return;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
if (focusRequest != null) {
|
||||
audioManager.abandonAudioFocusRequest(focusRequest);
|
||||
focusRequest = null;
|
||||
}
|
||||
} else {
|
||||
audioManager.abandonAudioFocus(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void startVibration() {
|
||||
Vibrator device = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
|
||||
if (device == null || !device.hasVibrator()) {
|
||||
return;
|
||||
}
|
||||
vibrator = device;
|
||||
long[] pattern = {0, 600, 400, 600, 1200};
|
||||
AudioAttributes attributes = new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_ALARM)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.build();
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
device.vibrate(VibrationEffect.createWaveform(pattern, 0), attributes);
|
||||
} else {
|
||||
device.vibrate(pattern, 0, attributes);
|
||||
}
|
||||
} catch (RuntimeException error) {
|
||||
FaceclawAlarms.log(this, "vibration failed: " + error.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Wake lock
|
||||
|
||||
private void acquireWakeLock() {
|
||||
if (wakeLock != null && wakeLock.isHeld()) {
|
||||
return;
|
||||
}
|
||||
PowerManager power = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||
if (power == null) {
|
||||
return;
|
||||
}
|
||||
wakeLock = power.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "faceclaw:alarm");
|
||||
// Bounded: sound auto-silences at AUTO_SILENCE_MS, and a stuck lock
|
||||
// past that would only drain the battery.
|
||||
wakeLock.acquire(AUTO_SILENCE_MS + ACK_WAIT_MS + DELIVERY_WAIT_MS + 10_000L);
|
||||
}
|
||||
|
||||
private void releaseWakeLock() {
|
||||
if (wakeLock != null && wakeLock.isHeld()) {
|
||||
wakeLock.release();
|
||||
}
|
||||
wakeLock = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,620 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.app.AlarmManager;
|
||||
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.media.AudioManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.tns.NativeScriptActivity;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* The phone side of the Timers app's alarms and countdown timers, built to
|
||||
* ring without the JavaScript side: scheduling through AlarmManager's alarm
|
||||
* clock API, a persisted schedule (in device-protected storage) that the
|
||||
* reschedule receiver replays after reboots and time changes, the hand-off
|
||||
* to the ringing foreground service, and a journal of what the wearer did
|
||||
* on the phone (dismiss / snooze) for the JS engine to replay when it is
|
||||
* next awake. Also the reliability self-check the UI surfaces.
|
||||
*
|
||||
* Ids are the JS engine's item ids (a millisecond epoch times 100 plus a
|
||||
* serial), shared between timers and alarms; `kind` tells the two apart for
|
||||
* wording only.
|
||||
*/
|
||||
public final class FaceclawAlarms {
|
||||
public static final String ACTION_EXPIRE = "com.faceclaw.app.action.ALARM_EXPIRE";
|
||||
static final String EXTRA_ID = "id";
|
||||
static final String EXTRA_TITLE = "title";
|
||||
static final String EXTRA_TEXT = "text";
|
||||
static final String EXTRA_KIND = "kind";
|
||||
static final String EXTRA_SNOOZE_MINUTES = "snoozeMinutes";
|
||||
|
||||
public static final String KIND_TIMER = "timer";
|
||||
public static final String KIND_ALARM = "alarm";
|
||||
|
||||
private static final String PREFS_NAME = "faceclaw-alarms";
|
||||
private static final String KEY_SCHEDULED = "scheduled";
|
||||
private static final String KEY_JOURNAL = "journal";
|
||||
private static final String KEY_LOG = "log";
|
||||
private static final int LOG_LINES = 60;
|
||||
|
||||
/**
|
||||
* A schedule entry found already past due (after a reboot, or when the
|
||||
* process was asleep) rings if it is at most this late; older ones are
|
||||
* dropped as missed. Matches the JS engine's grace window.
|
||||
*/
|
||||
static final long LATE_GRACE_MS = 5 * 60_000L;
|
||||
/** A glasses status report older than this is not trusted (the JS side is probably gone). */
|
||||
static final long GLASSES_STATUS_MAX_AGE_MS = 3 * 60_000L;
|
||||
|
||||
private static volatile boolean glassesConnected;
|
||||
private static volatile boolean glassesWorn;
|
||||
private static volatile boolean glassesCharging;
|
||||
private static volatile long glassesStatusAtMs;
|
||||
|
||||
private static volatile FaceclawAlarmListener listener;
|
||||
private static final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
private FaceclawAlarms() {}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Storage
|
||||
|
||||
/**
|
||||
* Device-protected storage, so the schedule is readable before the first
|
||||
* unlock after a reboot (the reschedule receiver may run then on some
|
||||
* devices) and survives credential-encrypted storage being unavailable.
|
||||
*/
|
||||
static SharedPreferences prefs(Context context) {
|
||||
Context base = context.getApplicationContext();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
base = base.createDeviceProtectedStorageContext();
|
||||
}
|
||||
return base.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
private static synchronized JSONArray readArray(Context context, String key) {
|
||||
String raw = prefs(context).getString(key, "[]");
|
||||
try {
|
||||
return new JSONArray(raw);
|
||||
} catch (JSONException error) {
|
||||
return new JSONArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static synchronized void writeArray(Context context, String key, JSONArray array) {
|
||||
prefs(context).edit().putString(key, array.toString()).apply();
|
||||
}
|
||||
|
||||
static JSONObject findScheduled(Context context, long id) {
|
||||
JSONArray scheduled = readArray(context, KEY_SCHEDULED);
|
||||
for (int index = 0; index < scheduled.length(); index++) {
|
||||
JSONObject entry = scheduled.optJSONObject(index);
|
||||
if (entry != null && entry.optLong("id") == id) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static synchronized void putScheduled(Context context, JSONObject entry) {
|
||||
JSONArray scheduled = readArray(context, KEY_SCHEDULED);
|
||||
JSONArray next = new JSONArray();
|
||||
long id = entry.optLong("id");
|
||||
for (int index = 0; index < scheduled.length(); index++) {
|
||||
JSONObject existing = scheduled.optJSONObject(index);
|
||||
if (existing != null && existing.optLong("id") != id) {
|
||||
next.put(existing);
|
||||
}
|
||||
}
|
||||
next.put(entry);
|
||||
writeArray(context, KEY_SCHEDULED, next);
|
||||
}
|
||||
|
||||
private static synchronized void removeScheduled(Context context, long id) {
|
||||
JSONArray scheduled = readArray(context, KEY_SCHEDULED);
|
||||
JSONArray next = new JSONArray();
|
||||
for (int index = 0; index < scheduled.length(); index++) {
|
||||
JSONObject existing = scheduled.optJSONObject(index);
|
||||
if (existing != null && existing.optLong("id") != id) {
|
||||
next.put(existing);
|
||||
}
|
||||
}
|
||||
writeArray(context, KEY_SCHEDULED, next);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Scheduling
|
||||
|
||||
/**
|
||||
* Schedule (or move) the phone alarm for an item. Idempotent: the same id
|
||||
* always maps to the same PendingIntent, so a reschedule replaces rather
|
||||
* than duplicates. setAlarmClock is exact, fires through Doze and battery
|
||||
* savers, and shows the system's alarm indicator; the fallbacks only run
|
||||
* where it is unavailable.
|
||||
*/
|
||||
public static void schedule(
|
||||
Context context,
|
||||
long id,
|
||||
long triggerAtMs,
|
||||
String title,
|
||||
String text,
|
||||
String kind,
|
||||
int snoozeMinutes
|
||||
) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
JSONObject entry = new JSONObject();
|
||||
try {
|
||||
entry.put("id", id);
|
||||
entry.put("at", triggerAtMs);
|
||||
entry.put("title", title == null ? "" : title);
|
||||
entry.put("text", text == null ? "" : text);
|
||||
entry.put("kind", kind == null ? KIND_ALARM : kind);
|
||||
entry.put("snoozeMinutes", snoozeMinutes);
|
||||
} catch (JSONException ignored) {
|
||||
return;
|
||||
}
|
||||
putScheduled(appContext, entry);
|
||||
armAlarm(appContext, entry);
|
||||
}
|
||||
|
||||
private static void armAlarm(Context appContext, JSONObject entry) {
|
||||
AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
|
||||
if (manager == null) {
|
||||
return;
|
||||
}
|
||||
long id = entry.optLong("id");
|
||||
long triggerAt = Math.max(System.currentTimeMillis(), entry.optLong("at"));
|
||||
PendingIntent fire = expiryPendingIntent(appContext, entry);
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(triggerAt, showIntent(appContext, id));
|
||||
manager.setAlarmClock(info, fire);
|
||||
} else {
|
||||
manager.setExact(AlarmManager.RTC_WAKEUP, triggerAt, fire);
|
||||
}
|
||||
} catch (SecurityException error) {
|
||||
// Exact-alarm access revoked (Android 12/13 without USE_EXACT_ALARM
|
||||
// honoured): the inexact alarm is still a wake-up, just a late one.
|
||||
log(appContext, "exact alarm refused for " + id + ": " + error.getMessage());
|
||||
manager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt, fire);
|
||||
}
|
||||
}
|
||||
|
||||
/** Cancel the phone alarm for an item, stop it if ringing, and forget it. */
|
||||
public static void cancel(Context context, long id) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
|
||||
if (manager != null) {
|
||||
JSONObject entry = new JSONObject();
|
||||
try {
|
||||
entry.put("id", id);
|
||||
} catch (JSONException ignored) {
|
||||
// unreachable for a numeric put
|
||||
}
|
||||
manager.cancel(expiryPendingIntent(appContext, entry));
|
||||
}
|
||||
removeScheduled(appContext, id);
|
||||
FaceclawAlarmService.stopItem(appContext, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replay the persisted schedule into AlarmManager: after a reboot (alarms
|
||||
* are wiped), a package update, or a wall-clock / time-zone change. Entries
|
||||
* already due ring now if inside the grace window, else are dropped.
|
||||
*/
|
||||
public static void rescheduleAll(Context context, String reason) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
JSONArray scheduled = readArray(appContext, KEY_SCHEDULED);
|
||||
long now = System.currentTimeMillis();
|
||||
int armed = 0;
|
||||
for (int index = 0; index < scheduled.length(); index++) {
|
||||
JSONObject entry = scheduled.optJSONObject(index);
|
||||
if (entry == null) {
|
||||
continue;
|
||||
}
|
||||
long at = entry.optLong("at");
|
||||
if (at <= now) {
|
||||
if (now - at <= LATE_GRACE_MS) {
|
||||
ringEntry(appContext, entry);
|
||||
} else {
|
||||
log(appContext, "missed " + entry.optString("kind") + " " + entry.optLong("id") + " (" + reason + ")");
|
||||
removeScheduled(appContext, entry.optLong("id"));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
armAlarm(appContext, entry);
|
||||
armed++;
|
||||
}
|
||||
log(appContext, "rescheduled " + armed + " (" + reason + ")");
|
||||
}
|
||||
|
||||
/**
|
||||
* The schedule and AlarmManager can disagree (a force-stop clears alarms
|
||||
* but not the file, an OEM killer drops them). True when the soonest
|
||||
* stored entry is not what the system reports as the next alarm clock.
|
||||
*/
|
||||
public static boolean scheduleLooksStale(Context context) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
||||
return false;
|
||||
}
|
||||
Context appContext = context.getApplicationContext();
|
||||
JSONArray scheduled = readArray(appContext, KEY_SCHEDULED);
|
||||
long now = System.currentTimeMillis();
|
||||
long soonest = Long.MAX_VALUE;
|
||||
for (int index = 0; index < scheduled.length(); index++) {
|
||||
JSONObject entry = scheduled.optJSONObject(index);
|
||||
if (entry != null && entry.optLong("at") > now) {
|
||||
soonest = Math.min(soonest, entry.optLong("at"));
|
||||
}
|
||||
}
|
||||
if (soonest == Long.MAX_VALUE) {
|
||||
return false;
|
||||
}
|
||||
AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
|
||||
if (manager == null) {
|
||||
return false;
|
||||
}
|
||||
AlarmManager.AlarmClockInfo next = manager.getNextAlarmClock();
|
||||
// Another app's alarm may legitimately be sooner; only a later (or
|
||||
// absent) system alarm proves ours is missing.
|
||||
return next == null || next.getTriggerTime() > soonest + 1000;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Ringing
|
||||
|
||||
/** An item came due: hand it to the ringing service (idempotent per id). */
|
||||
static void ringEntry(Context appContext, JSONObject entry) {
|
||||
FaceclawAlarmService.ring(
|
||||
appContext,
|
||||
entry.optLong("id"),
|
||||
entry.optString("title"),
|
||||
entry.optString("text"),
|
||||
entry.optString("kind", KIND_ALARM),
|
||||
entry.optInt("snoozeMinutes", 10)
|
||||
);
|
||||
}
|
||||
|
||||
/** The JS engine's own expiry path; the service deduplicates against the alarm's. */
|
||||
public static void ring(Context context, long id, String title, String text, String kind, int snoozeMinutes) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
|
||||
if (manager != null) {
|
||||
JSONObject probe = new JSONObject();
|
||||
try {
|
||||
probe.put("id", id);
|
||||
} catch (JSONException ignored) {
|
||||
// unreachable for a numeric put
|
||||
}
|
||||
manager.cancel(expiryPendingIntent(appContext, probe));
|
||||
}
|
||||
FaceclawAlarmService.ring(appContext, id, title, text, kind, snoozeMinutes);
|
||||
}
|
||||
|
||||
/** The glasses are showing the ringing item; the acknowledgement clock starts now. */
|
||||
public static void deliveredToGlasses(Context context, long id) {
|
||||
FaceclawAlarmService.delivered(context.getApplicationContext(), id);
|
||||
}
|
||||
|
||||
/** The wearer dismissed or snoozed it on the glasses: the phone side goes quiet. */
|
||||
public static void acknowledge(Context context, long id) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
removeScheduled(appContext, id);
|
||||
FaceclawAlarmService.stopItem(appContext, id);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Glasses status (pushed by the JS side; in-memory, so a dead process reads as "no glasses")
|
||||
|
||||
public static void setGlassesStatus(boolean connected, boolean worn, boolean charging) {
|
||||
glassesConnected = connected;
|
||||
glassesWorn = worn;
|
||||
glassesCharging = charging;
|
||||
glassesStatusAtMs = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/** True only when a fresh report says the glasses are connected, on a head, and not charging. */
|
||||
static boolean glassesCanCarryAlarm() {
|
||||
long age = System.currentTimeMillis() - glassesStatusAtMs;
|
||||
if (glassesStatusAtMs == 0 || age > GLASSES_STATUS_MAX_AGE_MS) {
|
||||
return false;
|
||||
}
|
||||
return glassesConnected && glassesWorn && !glassesCharging;
|
||||
}
|
||||
|
||||
static String glassesStatusDescription() {
|
||||
if (glassesStatusAtMs == 0) {
|
||||
return "no glasses status";
|
||||
}
|
||||
long age = System.currentTimeMillis() - glassesStatusAtMs;
|
||||
if (age > GLASSES_STATUS_MAX_AGE_MS) {
|
||||
return "glasses status stale";
|
||||
}
|
||||
if (!glassesConnected) {
|
||||
return "glasses not connected";
|
||||
}
|
||||
if (glassesCharging) {
|
||||
return "glasses charging";
|
||||
}
|
||||
if (!glassesWorn) {
|
||||
return "glasses not worn";
|
||||
}
|
||||
return "glasses worn";
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Phone actions -> JS
|
||||
|
||||
public static void setListener(FaceclawAlarmListener newListener) {
|
||||
listener = newListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a dismiss / snooze done on the phone. Delivered live to the JS
|
||||
* listener when one is registered, and journaled regardless so a JS side
|
||||
* that was asleep or gone can catch up on its next boot.
|
||||
*/
|
||||
static void recordPhoneAction(Context context, long id, String action, int minutes) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
JSONObject event = new JSONObject();
|
||||
try {
|
||||
event.put("id", id);
|
||||
event.put("action", action);
|
||||
event.put("minutes", minutes);
|
||||
event.put("at", System.currentTimeMillis());
|
||||
} catch (JSONException ignored) {
|
||||
return;
|
||||
}
|
||||
synchronized (FaceclawAlarms.class) {
|
||||
JSONArray journal = readArray(appContext, KEY_JOURNAL);
|
||||
journal.put(event);
|
||||
writeArray(appContext, KEY_JOURNAL, journal);
|
||||
}
|
||||
log(appContext, "phone " + action + " " + id + (minutes > 0 ? " (" + minutes + " min)" : ""));
|
||||
final FaceclawAlarmListener current = listener;
|
||||
if (current != null) {
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
current.onPhoneAction(id, action, minutes);
|
||||
} catch (RuntimeException error) {
|
||||
log(appContext, "listener failed: " + error.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Hand the journal to the JS side (as a JSON array) and clear it. */
|
||||
public static synchronized String drainJournal(Context context) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
JSONArray journal = readArray(appContext, KEY_JOURNAL);
|
||||
writeArray(appContext, KEY_JOURNAL, new JSONArray());
|
||||
return journal.toString();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Reliability self-check
|
||||
|
||||
/**
|
||||
* Conditions under which the phone may fail to ring, as a JSON array of
|
||||
* {code, message, fixable}, most serious first. Empty when everything is
|
||||
* in order. The UI shows these before the wearer relies on an alarm.
|
||||
*/
|
||||
public static String checkReliability(Context context) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
JSONArray issues = new JSONArray();
|
||||
NotificationManager notifications = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
AlarmManager alarms = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && alarms != null && !alarms.canScheduleExactAlarms()) {
|
||||
addIssue(issues, "exact-alarm", "Exact alarms are not allowed for Faceclaw, so alarms may ring late.", true);
|
||||
}
|
||||
if (notifications != null && !notifications.areNotificationsEnabled()) {
|
||||
addIssue(issues, "notifications", "Notifications are turned off for Faceclaw, so the phone cannot show or sound an alarm.", true);
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notifications != null) {
|
||||
NotificationChannel channel = notifications.getNotificationChannel(FaceclawAlarmService.CHANNEL_ID);
|
||||
if (channel != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
|
||||
addIssue(issues, "alarm-channel", "The Alarms notification category is blocked, so the phone cannot show an alarm.", true);
|
||||
}
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && notifications != null && !notifications.canUseFullScreenIntent()) {
|
||||
addIssue(issues, "full-screen", "Full-screen alarms are not allowed, so a ringing alarm will not take over the lock screen.", true);
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && notifications != null) {
|
||||
int filter = notifications.getCurrentInterruptionFilter();
|
||||
if (filter == NotificationManager.INTERRUPTION_FILTER_NONE) {
|
||||
addIssue(issues, "dnd-total", "Do Not Disturb is set to total silence, which silences alarms too.", true);
|
||||
} else if (filter == NotificationManager.INTERRUPTION_FILTER_PRIORITY) {
|
||||
try {
|
||||
NotificationManager.Policy policy = notifications.getNotificationPolicy();
|
||||
if (policy != null && (policy.priorityCategories & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0) {
|
||||
addIssue(issues, "dnd-alarms", "Do Not Disturb does not allow alarms, so the phone will stay silent.", true);
|
||||
}
|
||||
} catch (SecurityException ignored) {
|
||||
// No notification-policy access: cannot inspect the exceptions.
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
if (activityManager != null && activityManager.isBackgroundRestricted()) {
|
||||
addIssue(issues, "background-restricted", "Background usage is restricted for Faceclaw, which blocks alarms while the app is not open.", true);
|
||||
}
|
||||
}
|
||||
PowerManager power = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE);
|
||||
if (power != null && !power.isIgnoringBatteryOptimizations(appContext.getPackageName())) {
|
||||
addIssue(issues, "battery-optimized", "Battery optimization is on for Faceclaw; some phones delay alarms because of it.", true);
|
||||
}
|
||||
AudioManager audio = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE);
|
||||
if (audio != null && audio.getStreamVolume(AudioManager.STREAM_ALARM) == 0) {
|
||||
addIssue(issues, "alarm-volume", "The phone's alarm volume is muted; Faceclaw raises it while ringing, but check it.", true);
|
||||
}
|
||||
String maker = Build.MANUFACTURER == null ? "" : Build.MANUFACTURER.toLowerCase(Locale.ROOT);
|
||||
if (maker.contains("xiaomi") || maker.contains("huawei") || maker.contains("oppo") || maker.contains("vivo")
|
||||
|| maker.contains("oneplus") || maker.contains("realme") || maker.contains("meizu") || maker.contains("asus")) {
|
||||
addIssue(issues, "oem-killer", "This phone maker is known to stop background apps; allow Faceclaw to auto-start and run unrestricted (see dontkillmyapp.com).", true);
|
||||
}
|
||||
if (scheduleLooksStale(appContext)) {
|
||||
addIssue(issues, "schedule-stale", "The system lost Faceclaw's next alarm (the app may have been force-stopped); it was re-armed.", false);
|
||||
rescheduleAll(appContext, "self-check");
|
||||
}
|
||||
return issues.toString();
|
||||
}
|
||||
|
||||
private static void addIssue(JSONArray issues, String code, String message, boolean fixable) {
|
||||
JSONObject issue = new JSONObject();
|
||||
try {
|
||||
issue.put("code", code);
|
||||
issue.put("message", message);
|
||||
issue.put("fixable", fixable);
|
||||
} catch (JSONException ignored) {
|
||||
return;
|
||||
}
|
||||
issues.put(issue);
|
||||
}
|
||||
|
||||
/** Open the system screen where the issue can be fixed. */
|
||||
public static void openReliabilityFix(Context context, String code) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
String pkg = appContext.getPackageName();
|
||||
Intent intent;
|
||||
switch (code == null ? "" : code) {
|
||||
case "exact-alarm":
|
||||
intent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
|
||||
? new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, Uri.parse("package:" + pkg))
|
||||
: appDetails(pkg);
|
||||
break;
|
||||
case "notifications":
|
||||
case "alarm-channel":
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
|
||||
.putExtra(Settings.EXTRA_APP_PACKAGE, pkg);
|
||||
if ("alarm-channel".equals(code)) {
|
||||
intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
|
||||
.putExtra(Settings.EXTRA_APP_PACKAGE, pkg)
|
||||
.putExtra(Settings.EXTRA_CHANNEL_ID, FaceclawAlarmService.CHANNEL_ID);
|
||||
}
|
||||
} else {
|
||||
intent = appDetails(pkg);
|
||||
}
|
||||
break;
|
||||
case "full-screen":
|
||||
intent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE
|
||||
? new Intent(Settings.ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT, Uri.parse("package:" + pkg))
|
||||
: appDetails(pkg);
|
||||
break;
|
||||
case "dnd-total":
|
||||
case "dnd-alarms":
|
||||
intent = new Intent(Settings.ACTION_SOUND_SETTINGS);
|
||||
break;
|
||||
case "battery-optimized":
|
||||
intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:" + pkg));
|
||||
break;
|
||||
case "alarm-volume":
|
||||
intent = new Intent(Settings.ACTION_SOUND_SETTINGS);
|
||||
break;
|
||||
default:
|
||||
intent = appDetails(pkg);
|
||||
break;
|
||||
}
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
try {
|
||||
appContext.startActivity(intent);
|
||||
} catch (RuntimeException error) {
|
||||
Intent fallback = appDetails(pkg).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
try {
|
||||
appContext.startActivity(fallback);
|
||||
} catch (RuntimeException ignored) {
|
||||
// No settings activity at all; nothing more to do.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Intent appDetails(String pkg) {
|
||||
return new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + pkg));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Diagnostics log
|
||||
|
||||
static synchronized void log(Context context, String line) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
JSONArray log = readArray(appContext, KEY_LOG);
|
||||
JSONArray next = new JSONArray();
|
||||
int start = Math.max(0, log.length() - (LOG_LINES - 1));
|
||||
for (int index = start; index < log.length(); index++) {
|
||||
next.put(log.optString(index));
|
||||
}
|
||||
next.put(System.currentTimeMillis() + " " + line);
|
||||
writeArray(appContext, KEY_LOG, next);
|
||||
}
|
||||
|
||||
/** The ring / miss / action log, newest last, one line per entry. */
|
||||
public static String readLog(Context context) {
|
||||
JSONArray log = readArray(context.getApplicationContext(), KEY_LOG);
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (int index = 0; index < log.length(); index++) {
|
||||
if (index > 0) {
|
||||
out.append('\n');
|
||||
}
|
||||
out.append(log.optString(index));
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Intents
|
||||
|
||||
private static PendingIntent expiryPendingIntent(Context appContext, JSONObject entry) {
|
||||
Intent intent = new Intent(appContext, FaceclawAlarmReceiver.class);
|
||||
intent.setAction(ACTION_EXPIRE);
|
||||
long id = entry.optLong("id");
|
||||
intent.putExtra(EXTRA_ID, id);
|
||||
intent.putExtra(EXTRA_TITLE, entry.optString("title"));
|
||||
intent.putExtra(EXTRA_TEXT, entry.optString("text"));
|
||||
intent.putExtra(EXTRA_KIND, entry.optString("kind", KIND_ALARM));
|
||||
intent.putExtra(EXTRA_SNOOZE_MINUTES, entry.optInt("snoozeMinutes", 10));
|
||||
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
flags |= PendingIntent.FLAG_IMMUTABLE;
|
||||
}
|
||||
return PendingIntent.getBroadcast(appContext, requestCode(id), intent, flags);
|
||||
}
|
||||
|
||||
/** What the system's alarm indicator opens: the app. */
|
||||
private static PendingIntent showIntent(Context appContext, long id) {
|
||||
Intent intent = new Intent(appContext, NativeScriptActivity.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
flags |= PendingIntent.FLAG_IMMUTABLE;
|
||||
}
|
||||
return PendingIntent.getActivity(appContext, requestCode(id), intent, flags);
|
||||
}
|
||||
|
||||
static int requestCode(long id) {
|
||||
return (int) (id ^ (id >>> 32)) & 0x7fffffff;
|
||||
}
|
||||
|
||||
static void startServiceCompat(Context appContext, Intent intent) {
|
||||
ContextCompat.startForegroundService(appContext, intent);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawAmbientLightListener {
|
||||
/**
|
||||
* A CFW ambient-light report (settings-channel field 105) from the master
|
||||
* temple. body is the raw 24-byte ['A','L',ver,reason,...] record; it is
|
||||
* decoded on the TS side (app/native/ambient-light.ts).
|
||||
*/
|
||||
void onAmbientLight(byte[] body);
|
||||
}
|
||||
+230
-23
@@ -43,6 +43,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private static final long FACECLAW_WAKE_LEASE_RENEW_MS = 45_000;
|
||||
private static final long FACECLAW_WAKE_CONTROL_WAIT_MS = 1_500;
|
||||
private static final long CFW_CLEANUP_WAIT_MS = 4_000;
|
||||
private static final int COMPASS_REPORT_INTERVAL_MS = 100;
|
||||
private static final int COMPASS_MIN_CHANGE_DEGREES = 0;
|
||||
|
||||
private final Context appContext;
|
||||
private final PowerManager powerManager;
|
||||
@@ -58,7 +60,20 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private volatile FaceclawBleCommunicatorListener listener;
|
||||
private final java.util.List<FaceclawImuListener> imuListeners =
|
||||
new java.util.concurrent.CopyOnWriteArrayList<>();
|
||||
private final java.util.List<FaceclawCompassListener> compassListeners =
|
||||
/** A compass subscriber plus the Looper it registered from (see addCompassListener). */
|
||||
private static final class CompassSubscription {
|
||||
final FaceclawCompassListener listener;
|
||||
final Handler handler;
|
||||
|
||||
CompassSubscription(FaceclawCompassListener listener, Handler handler) {
|
||||
this.listener = listener;
|
||||
this.handler = handler;
|
||||
}
|
||||
}
|
||||
|
||||
private final java.util.List<CompassSubscription> compassSubscriptions =
|
||||
new java.util.concurrent.CopyOnWriteArrayList<>();
|
||||
private final java.util.List<FaceclawAmbientLightListener> ambientLightListeners =
|
||||
new java.util.concurrent.CopyOnWriteArrayList<>();
|
||||
private final java.util.List<FaceclawMicStatusListener> micStatusListeners =
|
||||
new java.util.concurrent.CopyOnWriteArrayList<>();
|
||||
@@ -88,7 +103,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private int firmwareDebugFlagsLastSent = -1;
|
||||
// Desired CFW mode-10 compass state. It survives reconnects; lastSent is
|
||||
// reset with each session so an open Compass window is re-asserted.
|
||||
private boolean compassEnabled;
|
||||
/**
|
||||
* Who currently wants the stock compass running (the Compass window, the
|
||||
* Navigate worker, ...). The magnetometer is one shared resource, so it
|
||||
* stays on while any owner holds it and is released when the last lets go;
|
||||
* this keeps one app's release from silently switching off another's feed.
|
||||
*/
|
||||
private final java.util.Set<String> compassOwners = new java.util.HashSet<>();
|
||||
private int compassControlLastSent = -1;
|
||||
// Whether the glasses-side compass may still be running: set when an enable
|
||||
// is enqueued, cleared only when a disable is acked. Drives the forced
|
||||
@@ -141,6 +162,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private long lastShutdownExitAtMs = 0;
|
||||
private int headsetBattery = -1;
|
||||
private int headsetCharging = -1;
|
||||
private int ringBattery = -1;
|
||||
private int ringCharging = -1;
|
||||
// Silent mode: 1 = on, 0 = off, -1 = not yet known. See updateSilentModeLocked.
|
||||
private int silentMode = -1;
|
||||
private int wearState = -1;
|
||||
@@ -508,12 +531,32 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
* stock sid-0x08 navigation notifications and FaceclawCompassListeners.
|
||||
*/
|
||||
public void setCompassEnabled(boolean enable) {
|
||||
setCompassEnabled("compass", enable);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, on behalf of a named owner. The compass runs while at least
|
||||
* one owner has enabled it; an owner disabling it only takes effect once
|
||||
* no other owner still wants it.
|
||||
*/
|
||||
public void setCompassEnabled(String owner, boolean enable) {
|
||||
synchronized (lock) {
|
||||
compassEnabled = enable;
|
||||
boolean before = !compassOwners.isEmpty();
|
||||
if (enable) {
|
||||
compassOwners.add(owner);
|
||||
} else {
|
||||
compassOwners.remove(owner);
|
||||
}
|
||||
boolean wanted = !compassOwners.isEmpty();
|
||||
if (wanted == before && compassControlLastSent == (wanted ? 1 : 0)) {
|
||||
logLine("compass " + (enable ? "enable" : "disable") + " by " + owner
|
||||
+ "; state unchanged (owners=" + compassOwners + ")");
|
||||
return;
|
||||
}
|
||||
compassControlLastSent = -1;
|
||||
clearMessagesOfKindLocked("compass-control");
|
||||
if (running && sessionReady && !shutdownRequested && fixedLayoutCreated) {
|
||||
enqueueCompassControlLocked(true, compassEnabled);
|
||||
enqueueCompassControlLocked(true, wanted);
|
||||
} else {
|
||||
logLine("defer compass " + (enable ? "enable" : "disable") + "; display path not ready");
|
||||
}
|
||||
@@ -764,6 +807,91 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return lastFirmwareCapabilities;
|
||||
}
|
||||
|
||||
public void addAmbientLightListener(FaceclawAmbientLightListener listener) {
|
||||
if (listener != null) {
|
||||
ambientLightListeners.add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeAmbientLightListener(FaceclawAmbientLightListener listener) {
|
||||
if (listener != null) {
|
||||
ambientLightListeners.remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
private void emitAmbientLight(byte[] body) {
|
||||
if (ambientLightListeners.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
byte[] copy = java.util.Arrays.copyOf(body, body.length);
|
||||
mainHandler.post(() -> {
|
||||
for (FaceclawAmbientLightListener alsListener : ambientLightListeners) {
|
||||
try {
|
||||
alsListener.onAmbientLight(copy);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "ambient light listener failed", t);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Request one CFW ambient-light report (image-handler mode 16 op 0). */
|
||||
public void queryAmbientLight() {
|
||||
synchronized (lock) {
|
||||
enqueueAmbientLightControlLocked(new byte[] { (byte) 16, (byte) 0 }, "als query", false);
|
||||
}
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start or stop CFW passive light-sensor polling (image-handler mode 16
|
||||
* ops 1/2). While polling, the firmware reads its OPT3001 every intervalMs
|
||||
* (clamped 100..5000 by the firmware), never steps the panel brightness
|
||||
* itself, and pushes a field-105 report when the reading moved by at least
|
||||
* minDelta or heartbeatMs elapsed. bindToLease stops polling automatically
|
||||
* when the Faceclaw framebuffer lease is released or lapses. Starting is
|
||||
* idempotent and re-opens the sensor if the stock firmware closed it.
|
||||
*/
|
||||
public void setAmbientLightPolling(boolean enable, int intervalMs, int minDelta,
|
||||
int heartbeatMs, boolean bindToLease) {
|
||||
byte[] payload = enable
|
||||
? new byte[] {
|
||||
(byte) 16,
|
||||
(byte) 1,
|
||||
(byte) (bindToLease ? 1 : 0),
|
||||
(byte) (intervalMs & 0xff),
|
||||
(byte) ((intervalMs >> 8) & 0xff),
|
||||
(byte) (minDelta & 0xff),
|
||||
(byte) ((minDelta >> 8) & 0xff),
|
||||
(byte) (heartbeatMs & 0xff),
|
||||
(byte) ((heartbeatMs >> 8) & 0xff),
|
||||
}
|
||||
: new byte[] { (byte) 16, (byte) 2 };
|
||||
synchronized (lock) {
|
||||
clearMessagesOfKindLocked("als-control");
|
||||
enqueueAmbientLightControlLocked(payload, "als polling " + (enable ? "start" : "stop"), true);
|
||||
}
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
private void enqueueAmbientLightControlLocked(byte[] payload, String label, boolean priority) {
|
||||
if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) {
|
||||
logLine("skip " + label + "; display path not ready");
|
||||
return;
|
||||
}
|
||||
OutboundMessage message = messageBuilder.imagePayload(
|
||||
"als-control",
|
||||
DASHBOARD_TILE,
|
||||
nextMapSessionId(),
|
||||
payload,
|
||||
label,
|
||||
connectionOptions.sendImagesToLeft);
|
||||
message.onTimeout = () -> logLine(label + " ack timeout");
|
||||
if (priority) pendingMessages.addFirst(message);
|
||||
else pendingMessages.addLast(message);
|
||||
logLine("queue " + label);
|
||||
}
|
||||
|
||||
public void addMicStatusListener(FaceclawMicStatusListener listener) {
|
||||
if (listener != null) {
|
||||
micStatusListeners.add(listener);
|
||||
@@ -851,15 +979,28 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to compass events. Callbacks are delivered on the Looper of
|
||||
* the thread that registered (falling back to the main thread), so app
|
||||
* worker isolates can listen without a cross-thread hop into their JS.
|
||||
*/
|
||||
public void addCompassListener(FaceclawCompassListener listener) {
|
||||
if (listener != null) {
|
||||
compassListeners.add(listener);
|
||||
if (listener == null) {
|
||||
return;
|
||||
}
|
||||
Looper looper = Looper.myLooper();
|
||||
Handler handler = looper != null ? new Handler(looper) : mainHandler;
|
||||
compassSubscriptions.add(new CompassSubscription(listener, handler));
|
||||
}
|
||||
|
||||
public void removeCompassListener(FaceclawCompassListener listener) {
|
||||
if (listener != null) {
|
||||
compassListeners.remove(listener);
|
||||
if (listener == null) {
|
||||
return;
|
||||
}
|
||||
for (CompassSubscription subscription : compassSubscriptions) {
|
||||
if (subscription.listener == listener) {
|
||||
compassSubscriptions.remove(subscription);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1014,6 +1155,16 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
compositor.removeSurface(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dim every surface below zOrder belowZOrder to factor256/256 (see
|
||||
* SurfaceCompositor.setUnderlayDim). Takes effect with the next submitted
|
||||
* frame: the shell always submits its own surface right after changing
|
||||
* this, so no recomposite happens here.
|
||||
*/
|
||||
public void setUnderlayDim(int belowZOrder, int factor256) {
|
||||
compositor.setUnderlayDim(belowZOrder, factor256);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply an update to one compositor surface and submit the recomposited
|
||||
* screen as the desired frame. The update covers the rect (rectX, rectY,
|
||||
@@ -1549,6 +1700,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (!faceclawWakeNotification
|
||||
&& frame.ok
|
||||
&& frame.sid == BleProtocol.SID_UI_SETTING) {
|
||||
// Mode-17 query notifications; settings READs are handled by
|
||||
// createBatteryQueryMessageLocked so headset/ring update together.
|
||||
if (address.equalsIgnoreCase(rightAddress)
|
||||
&& (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT)) {
|
||||
BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(frame.pb);
|
||||
if (ring != null) {
|
||||
ringBattery = ring.battery;
|
||||
ringCharging = ring.charging;
|
||||
emitBatteryState(headsetBattery, headsetCharging);
|
||||
}
|
||||
}
|
||||
// CFW mic status (field 104) rides both standalone pushes and
|
||||
// settings read acks, from each temple on its own link.
|
||||
byte[] micStatus = BleProtocol.parseFaceclawMicStatus(frame.pb);
|
||||
@@ -1556,6 +1718,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
emitMicStatus(micStatus, address);
|
||||
}
|
||||
}
|
||||
if (!faceclawWakeNotification
|
||||
&& frame.ok
|
||||
&& frame.sid == BleProtocol.SID_UI_SETTING) {
|
||||
// CFW ambient-light report (field 105) from the master temple.
|
||||
byte[] alsReport = BleProtocol.parseFaceclawAlsReport(frame.pb);
|
||||
if (alsReport != null) {
|
||||
emitAmbientLight(alsReport);
|
||||
}
|
||||
}
|
||||
if (!faceclawWakeNotification
|
||||
&& decodedWearState < 0
|
||||
&& frame.ok
|
||||
@@ -1617,7 +1788,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
emitWearState(decodedWearState > 0);
|
||||
}
|
||||
if (compassEvent != null) {
|
||||
emitCompassEvent(compassEvent.command, compassEvent.headingDegrees);
|
||||
emitCompassEvent(compassEvent);
|
||||
}
|
||||
if (event != null) {
|
||||
if (event.hasImu) {
|
||||
@@ -2093,10 +2264,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated
|
||||
&& (compassEnabled ? 1 : 0) != compassControlLastSent
|
||||
&& (compassOwners.isEmpty() ? 0 : 1) != compassControlLastSent
|
||||
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
Log.i(TAG, "enqueueing compass " + (compassEnabled ? "enable" : "disable"));
|
||||
enqueueCompassControlLocked(false, compassEnabled);
|
||||
boolean wanted = !compassOwners.isEmpty();
|
||||
Log.i(TAG, "enqueueing compass " + (wanted ? "enable" : "disable"));
|
||||
enqueueCompassControlLocked(false, wanted);
|
||||
}
|
||||
|
||||
if (benchmarkActive) {
|
||||
@@ -2435,6 +2607,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
message.onAck.run();
|
||||
}
|
||||
consecutiveAckTimeouts = 0;
|
||||
// onAck may have just satisfied a waiter blocked on lock.wait() (e.g.
|
||||
// awaitEvenHubSessionReady polling fixedLayoutCreated/displayedFingerprint
|
||||
// after a create-layout or image ack). Without this, that waiter only
|
||||
// notices on its own up-to-100ms poll tick, adding avoidable latency to
|
||||
// every EvenHub wake. Always called with lock held (see call site).
|
||||
lock.notifyAll();
|
||||
}
|
||||
|
||||
private void logImageUpdateSendLandmarkLocked(OutboundMessage message) {
|
||||
@@ -2462,6 +2640,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return;
|
||||
}
|
||||
long ackedAtMs = SystemClock.elapsedRealtime();
|
||||
FaceclawBleManager.recordDisplayFrameSent();
|
||||
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.remove(message.imageUpdateId);
|
||||
if (stats != null && stats.firstWriteStartedAtMs > 0) {
|
||||
emitFrameMetrics(stats.paintMs, (int) Math.max(0, ackedAtMs - stats.firstWriteStartedAtMs), stats.tileCount);
|
||||
@@ -2557,10 +2736,22 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
logLine("queue firmware debug flags " + (show ? "show" : "hide"));
|
||||
}
|
||||
|
||||
/** Send CFW image-handler mode 10: [10][1] start, [10][0] stop. */
|
||||
/**
|
||||
* Send CFW image-handler mode 10. Enable uses the configurable form
|
||||
* [10][2][interval-ms LE16][minimum-change-degrees LE16]; disable remains [10][0].
|
||||
*/
|
||||
private void enqueueCompassControlLocked(boolean priority, boolean enable) {
|
||||
int sentState = enable ? 1 : 0;
|
||||
byte[] payload = new byte[] { (byte) 10, (byte) sentState };
|
||||
byte[] payload = enable
|
||||
? new byte[] {
|
||||
(byte) 10,
|
||||
(byte) 2,
|
||||
(byte) (COMPASS_REPORT_INTERVAL_MS & 0xff),
|
||||
(byte) ((COMPASS_REPORT_INTERVAL_MS >> 8) & 0xff),
|
||||
(byte) (COMPASS_MIN_CHANGE_DEGREES & 0xff),
|
||||
(byte) ((COMPASS_MIN_CHANGE_DEGREES >> 8) & 0xff),
|
||||
}
|
||||
: new byte[] { (byte) 10, (byte) 0 };
|
||||
OutboundMessage message = messageBuilder.imagePayload(
|
||||
"compass-control",
|
||||
DASHBOARD_TILE,
|
||||
@@ -2824,6 +3015,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private OutboundMessage createBatteryQueryMessageLocked() {
|
||||
OutboundMessage message = messageBuilder.batteryQuery();
|
||||
message.onAck = () -> {
|
||||
BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(message.ackPayload);
|
||||
// Old firmware and missing/malformed extensions must clear any prior reading.
|
||||
ringBattery = ring == null ? -1 : ring.battery;
|
||||
ringCharging = ring == null ? -1 : ring.charging;
|
||||
BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(message.ackPayload);
|
||||
if (snapshot != null) {
|
||||
headsetBattery = snapshot.battery;
|
||||
@@ -3409,6 +3604,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
private void resetSessionStateLocked() {
|
||||
ringBattery = -1;
|
||||
ringCharging = -1;
|
||||
sessionReady = false;
|
||||
shutdownRequested = false;
|
||||
fixedLayoutCreated = false;
|
||||
@@ -3504,19 +3701,27 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
});
|
||||
}
|
||||
|
||||
private void emitCompassEvent(int command, int headingDegrees) {
|
||||
if (compassListeners.isEmpty()) {
|
||||
return;
|
||||
private void emitCompassEvent(BleProtocol.CompassEvent event) {
|
||||
if (event.diagnosticFlags >= 0) {
|
||||
String[] sources = { "unknown", "GRV", "GMRV", "RV" };
|
||||
Log.i("FaceclawCompass", "heading=" + event.headingDegrees
|
||||
+ " magneticAccuracy=" + event.magneticAccuracy
|
||||
+ " magneticAnomalies=" + event.magneticAnomalies
|
||||
+ " orientationSource=" + sources[event.orientationSource]
|
||||
+ " flags=0x" + Integer.toHexString(event.diagnosticFlags)
|
||||
+ " sampleTimeMs=" + event.sampleTimeMs);
|
||||
}
|
||||
mainHandler.post(() -> {
|
||||
for (FaceclawCompassListener compassListener : compassListeners) {
|
||||
for (CompassSubscription subscription : compassSubscriptions) {
|
||||
subscription.handler.post(() -> {
|
||||
try {
|
||||
compassListener.onCompassEvent(command, headingDegrees);
|
||||
subscription.listener.onCompassEvent(event.command, event.headingDegrees,
|
||||
event.magneticAccuracy, event.magneticAnomalies, event.orientationSource,
|
||||
event.diagnosticFlags, event.sampleTimeMs);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onCompassEvent failed", t);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void emitSilentMode(boolean silent) {
|
||||
@@ -3568,13 +3773,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
private void emitBatteryState(int headsetBattery, int headsetCharging) {
|
||||
final int reportedRingBattery = ringBattery;
|
||||
final int reportedRingCharging = ringCharging;
|
||||
final FaceclawBleCommunicatorListener current = listener;
|
||||
if (current == null) {
|
||||
return;
|
||||
}
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
current.onBatteryState(headsetBattery, headsetCharging);
|
||||
current.onBatteryState(headsetBattery, headsetCharging, reportedRingBattery, reportedRingCharging);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onBatteryState failed", t);
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ public interface FaceclawBleCommunicatorListener {
|
||||
void onLog(String line);
|
||||
void onStateChange(String phase, String status);
|
||||
void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId);
|
||||
void onBatteryState(int headsetBattery, int headsetCharging);
|
||||
void onBatteryState(int headsetBattery, int headsetCharging, int ringBattery, int ringCharging);
|
||||
void onSilentMode(boolean silent);
|
||||
void onWearState(boolean wearing);
|
||||
void onPhoneLockState(boolean locked);
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
public class FaceclawBleManager {
|
||||
@@ -51,6 +52,23 @@ public class FaceclawBleManager {
|
||||
|
||||
private volatile FaceclawBleListener listener;
|
||||
|
||||
// Process-wide outbound-traffic totals, sampled by the phone UI's BLE
|
||||
// bandwidth indicator. Static so counts from every manager instance and
|
||||
// isolate land in the same totals.
|
||||
private static final AtomicLong outboundMessages = new AtomicLong();
|
||||
private static final AtomicLong outboundBytes = new AtomicLong();
|
||||
private static final AtomicLong displayFramesSent = new AtomicLong();
|
||||
|
||||
/** Called by the communicator when a display frame's last message is acked. */
|
||||
public static void recordDisplayFrameSent() {
|
||||
displayFramesSent.incrementAndGet();
|
||||
}
|
||||
|
||||
/** Running totals of outbound BLE traffic since process start: [messages, bytes, display frames]. */
|
||||
public static long[] sampleOutboundTraffic() {
|
||||
return new long[] { outboundMessages.get(), outboundBytes.get(), displayFramesSent.get() };
|
||||
}
|
||||
|
||||
public FaceclawBleManager(Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
BluetoothManager bluetoothManager = (BluetoothManager) this.context.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||
@@ -250,8 +268,10 @@ public class FaceclawBleManager {
|
||||
if (!startWrite(gatt, characteristic, address, frame, writeType, timeoutMs)) {
|
||||
return false;
|
||||
}
|
||||
outboundBytes.addAndGet(frame != null ? frame.length : 0);
|
||||
}
|
||||
}
|
||||
outboundMessages.incrementAndGet();
|
||||
int totalSize = frames.stream().mapToInt(frame -> frame != null ? frame.length : 0).sum();
|
||||
Log.i(TAG, "writeFrames wrote " + frames.size() + " frames totaling " + totalSize + " bytes in " + (System.currentTimeMillis() - startMs) + "ms");
|
||||
return true;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Receives stock compass heading and calibration notifications on the main thread. */
|
||||
/** Receives stock compass heading and calibration notifications on the thread that subscribed. */
|
||||
public interface FaceclawCompassListener {
|
||||
void onCompassEvent(int command, int headingDegrees);
|
||||
void onCompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies,
|
||||
int orientationSource, int diagnosticFlags, long sampleTimeMs);
|
||||
}
|
||||
+159
-39
@@ -27,9 +27,18 @@ import java.util.concurrent.TimeUnit;
|
||||
* whole lifecycle on a single worker thread. It expects the main app to be
|
||||
* disconnected while it runs (onboarding, before flashing).
|
||||
*
|
||||
* Only the right arm is used: the lenses relay messages to each other, so
|
||||
* anything sent to the right arm reaches both, and acks/events only ever come
|
||||
* from the right arm anyway.
|
||||
* Both arms are connected and security-authenticated up front, even though the
|
||||
* prompt itself only needs the right arm (the lenses relay messages to each
|
||||
* other, and acks/events only ever come from the right arm): on an unbonded
|
||||
* phone the auth exchange is what raises the OS pairing prompt, and doing both
|
||||
* here means both prompts appear at the start rather than one popping up half
|
||||
* way through flashing the second lens. While connected, each arm's battery
|
||||
* level is read (they are independent batteries) so the caller can refuse to
|
||||
* flash on a low charge.
|
||||
*
|
||||
* With `skipPrompt` the on-glasses confirmation is not shown: the flow is just
|
||||
* connect + auth + battery read + result(approved). Used to re-check the
|
||||
* battery after the user has already confirmed once.
|
||||
*/
|
||||
public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
private static final String TAG = "FaceclawFlashPrompt";
|
||||
@@ -44,10 +53,13 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
private static final int HEARTBEAT_INTERVAL_MS = 4_000;
|
||||
private static final int SELECTION_TIMEOUT_MS = 120_000;
|
||||
private static final int CREATE_ACK_TIMEOUT_MS = 3_000;
|
||||
private static final int BATTERY_ACK_TIMEOUT_MS = 3_000;
|
||||
|
||||
private final Context context;
|
||||
private final String rightAddress;
|
||||
private final String leftAddress;
|
||||
private final String warningText;
|
||||
private final boolean skipPrompt;
|
||||
private final FaceclawBleManager bleManager;
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
@@ -61,6 +73,7 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
private volatile boolean cancelled = false;
|
||||
private volatile boolean finished = false;
|
||||
private volatile boolean rightConnected = false;
|
||||
private volatile boolean leftConnected = false;
|
||||
private volatile boolean rightLost = false;
|
||||
private volatile Boolean approved = null;
|
||||
private volatile ScheduledExecutorService heartbeatExecutor;
|
||||
@@ -68,10 +81,13 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
private int nextMagic = 100;
|
||||
private int nextSeq = 0x40;
|
||||
|
||||
public FaceclawFlashPromptCommunicator(Context context, String rightAddress, String warningText) {
|
||||
public FaceclawFlashPromptCommunicator(
|
||||
Context context, String rightAddress, String leftAddress, String warningText, boolean skipPrompt) {
|
||||
this.context = context.getApplicationContext();
|
||||
this.rightAddress = rightAddress == null ? "" : rightAddress;
|
||||
this.leftAddress = leftAddress == null ? "" : leftAddress;
|
||||
this.warningText = warningText == null ? "" : warningText;
|
||||
this.skipPrompt = skipPrompt;
|
||||
this.bleManager = new FaceclawBleManager(this.context);
|
||||
this.bleManager.setListener(this);
|
||||
}
|
||||
@@ -117,43 +133,85 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
}
|
||||
|
||||
emitState("connecting", "");
|
||||
connectArm(rightAddress);
|
||||
connectArm(rightAddress, "right");
|
||||
rightConnected = true;
|
||||
if (cancelled) {
|
||||
teardown();
|
||||
return;
|
||||
}
|
||||
if (!leftAddress.trim().isEmpty()) {
|
||||
connectArm(leftAddress, "left");
|
||||
leftConnected = true;
|
||||
if (cancelled) {
|
||||
teardown();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
emitState("connected", "");
|
||||
bringUpArm(rightAddress);
|
||||
// Auth both arms now (pairing prompts, if any, happen here) before
|
||||
// anything else, so both bonds exist by the time flashing starts.
|
||||
authenticateArm(rightAddress, "right");
|
||||
if (leftConnected) {
|
||||
authenticateArm(leftAddress, "left");
|
||||
}
|
||||
if (cancelled) {
|
||||
teardown();
|
||||
return;
|
||||
}
|
||||
sendPrelude(rightAddress);
|
||||
|
||||
startHeartbeat();
|
||||
emitState("prompting", "");
|
||||
if (!skipPrompt) {
|
||||
showPrompt(rightAddress);
|
||||
startHeartbeat();
|
||||
emitState("prompting", "");
|
||||
|
||||
boolean signalled = selectionLatch.await(SELECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
|
||||
stopHeartbeat();
|
||||
selectionLatch.await(SELECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
|
||||
stopHeartbeat();
|
||||
|
||||
if (cancelled) {
|
||||
emitState("cancelled", "");
|
||||
teardown();
|
||||
return;
|
||||
}
|
||||
if (approved == null) {
|
||||
if (rightLost) {
|
||||
emitState("disconnected", "Lost connection to the glasses.");
|
||||
} else {
|
||||
emitState("timeout", "No response from the glasses.");
|
||||
}
|
||||
teardown();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
sendShutdown();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
if (!Boolean.TRUE.equals(approved)) {
|
||||
emitResult(false);
|
||||
emitState("result", "declined");
|
||||
teardown();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Approved (or prompt skipped): read each arm's battery while the
|
||||
// links are still up, then report the result.
|
||||
emitState("battery", "");
|
||||
readBatteries();
|
||||
if (cancelled) {
|
||||
emitState("cancelled", "");
|
||||
teardown();
|
||||
return;
|
||||
}
|
||||
if (approved != null) {
|
||||
boolean result = Boolean.TRUE.equals(approved);
|
||||
try {
|
||||
sendShutdown();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
emitResult(result);
|
||||
emitState("result", result ? "approved" : "declined");
|
||||
if (rightLost) {
|
||||
emitState("disconnected", "Lost connection to the glasses.");
|
||||
teardown();
|
||||
return;
|
||||
}
|
||||
if (rightLost) {
|
||||
emitState("disconnected", "Lost connection to the glasses.");
|
||||
} else {
|
||||
emitState("timeout", "No response from the glasses.");
|
||||
}
|
||||
finished = true;
|
||||
emitResult(true);
|
||||
emitState("result", "approved");
|
||||
teardown();
|
||||
} catch (Exception e) {
|
||||
stopHeartbeat();
|
||||
@@ -165,26 +223,28 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
}
|
||||
}
|
||||
|
||||
private void connectArm(String address) {
|
||||
private void connectArm(String address, String arm) {
|
||||
if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) {
|
||||
throw new IllegalStateException("connect failed: " + address);
|
||||
throw new IllegalStateException("could not connect to the " + arm + " arm (" + address + ")");
|
||||
}
|
||||
bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH);
|
||||
bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS);
|
||||
if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) {
|
||||
throw new IllegalStateException("discoverServices failed: " + address);
|
||||
throw new IllegalStateException("discoverServices failed: " + arm + " arm (" + address + ")");
|
||||
}
|
||||
if (!bleManager.enableNotifications(address, BleProtocol.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) {
|
||||
throw new IllegalStateException("enableNotifications failed: " + address);
|
||||
throw new IllegalStateException("enableNotifications failed: " + arm + " arm (" + address + ")");
|
||||
}
|
||||
emitLog("connected right arm");
|
||||
emitLog("connected " + arm + " arm");
|
||||
}
|
||||
|
||||
private void bringUpArm(String address) throws InterruptedException {
|
||||
// 1. Security-auth exchange on sid=0x80: firmware 2.2.9 will not run a
|
||||
// session (and closes the link after ~30 s) without it, and on an
|
||||
// unbonded phone this is what triggers SMP pairing, so it may sit
|
||||
// waiting on an OS pairing prompt.
|
||||
/**
|
||||
* Security-auth exchange on sid=0x80: firmware 2.2.9 will not run a
|
||||
* session (and closes the link after ~30 s) without it, and on an
|
||||
* unbonded phone this is what triggers SMP pairing, so it may sit
|
||||
* waiting on an OS pairing prompt.
|
||||
*/
|
||||
private void authenticateArm(String address, String arm) throws InterruptedException {
|
||||
int authMagic = allocMagic();
|
||||
byte[] authAck = writeAndAwaitAck(
|
||||
address,
|
||||
@@ -195,10 +255,14 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS);
|
||||
if (authAck == null || !BleProtocol.isAuthenticationSuccess(authAck, authMagic)) {
|
||||
throw new IllegalStateException(
|
||||
"could not authenticate with the glasses (" + address
|
||||
"could not authenticate with the " + arm + " arm (" + address
|
||||
+ ") — if Android shows a Bluetooth pairing request, accept it and try again");
|
||||
}
|
||||
// 2. Mandatory session prelude on sid=0x01 (app-launch).
|
||||
emitLog("security auth complete: " + arm + " arm");
|
||||
}
|
||||
|
||||
/** Mandatory session prelude on sid=0x01 (app-launch). Right arm only; it relays. */
|
||||
private void sendPrelude(String address) throws InterruptedException {
|
||||
if (writeAndAwaitAck(
|
||||
address,
|
||||
BleProtocol.PRELUDE_ACK_SID,
|
||||
@@ -208,10 +272,13 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
ConnectionOptions.PRELUDE_TIMEOUT_MS) == null) {
|
||||
throw new IllegalStateException("session prelude not acked: " + address);
|
||||
}
|
||||
// 3. Create the prompt page (warning text + No/Yes list) on sid=0xe0 Cmd=0.
|
||||
// Two attempts: on 2.2.9 the first request sent right after the
|
||||
// prelude can be dropped while the lens emits its own sid-0x80
|
||||
// notifications (observed with the device-info settings read).
|
||||
}
|
||||
|
||||
private void showPrompt(String address) throws InterruptedException {
|
||||
// Create the prompt page (warning text + No/Yes list) on sid=0xe0 Cmd=0.
|
||||
// Two attempts: on 2.2.9 the first request sent right after the
|
||||
// prelude can be dropped while the lens emits its own sid-0x80
|
||||
// notifications (observed with the device-info settings read).
|
||||
byte[] pageAck = null;
|
||||
for (int attempt = 0; attempt < 2 && pageAck == null && !cancelled; attempt++) {
|
||||
int magic = allocMagic();
|
||||
@@ -228,6 +295,42 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
emitLog("prompt page shown on " + address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read each arm's battery via a sid-0x09 settings read on that arm's own
|
||||
* link (the arms have independent batteries and each answers with its
|
||||
* own). An arm that does not answer reports -1; the caller decides what
|
||||
* to do with a partial reading.
|
||||
*/
|
||||
private void readBatteries() throws InterruptedException {
|
||||
int right = rightConnected ? readBattery(rightAddress, "right") : -1;
|
||||
int left = leftConnected ? readBattery(leftAddress, "left") : -1;
|
||||
emitLog("battery R=" + (right < 0 ? "?" : right + "%") + " L=" + (left < 0 ? "?" : left + "%"));
|
||||
emitBattery(right, left);
|
||||
}
|
||||
|
||||
private int readBattery(String address, String arm) throws InterruptedException {
|
||||
for (int attempt = 0; attempt < 2 && !cancelled; attempt++) {
|
||||
int magic = allocMagic();
|
||||
byte[] ack = writeAndAwaitAck(
|
||||
address,
|
||||
BleProtocol.SID_UI_SETTING,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildSettingsQuery(magic),
|
||||
BATTERY_ACK_TIMEOUT_MS);
|
||||
if (ack == null) {
|
||||
emitLog("battery read attempt " + (attempt + 1) + " unacked: " + arm + " arm");
|
||||
continue;
|
||||
}
|
||||
BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(ack);
|
||||
if (snapshot != null) {
|
||||
return snapshot.battery;
|
||||
}
|
||||
emitLog("battery read ack had no battery field: " + arm + " arm");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** Write and wait for the matching ack; returns the ack's protobuf (may be empty), or null on timeout/write failure. */
|
||||
private byte[] writeAndAwaitAck(String address, int sid, int flag, int magic, byte[] payload, int timeoutMs)
|
||||
throws InterruptedException {
|
||||
@@ -312,6 +415,7 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
rightConnected = false;
|
||||
leftConnected = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -371,6 +475,13 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
if (connected) {
|
||||
return;
|
||||
}
|
||||
if (address.equalsIgnoreCase(leftAddress)) {
|
||||
// The prompt itself only needs the right arm; a dropped left link
|
||||
// just means no left battery reading.
|
||||
leftConnected = false;
|
||||
emitLog("left arm disconnected");
|
||||
return;
|
||||
}
|
||||
if (address.equalsIgnoreCase(rightAddress)) {
|
||||
rightConnected = false;
|
||||
synchronized (lock) {
|
||||
@@ -415,6 +526,15 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
});
|
||||
}
|
||||
|
||||
private void emitBattery(int rightPercent, int leftPercent) {
|
||||
mainHandler.post(() -> {
|
||||
FaceclawFlashPromptListener current = listener;
|
||||
if (current != null) {
|
||||
current.onBattery(rightPercent, leftPercent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void emitResult(boolean approvedResult) {
|
||||
mainHandler.post(() -> {
|
||||
FaceclawFlashPromptListener current = listener;
|
||||
|
||||
+9
-2
@@ -6,11 +6,18 @@ public interface FaceclawFlashPromptListener {
|
||||
|
||||
/**
|
||||
* Lifecycle updates. `state` is one of: connecting, connected, prompting,
|
||||
* result, cancelled, timeout, disconnected, error. `detail` carries the
|
||||
* battery, result, cancelled, timeout, disconnected, error. `detail` carries the
|
||||
* "approved"/"declined" text for result, or an error/explanation message.
|
||||
*/
|
||||
void onState(String state, String detail);
|
||||
|
||||
/** Fires once the user picks a menu row: true = flash, false = cancel. */
|
||||
/**
|
||||
* Battery percent of each arm, read after the user approved (or right
|
||||
* after auth when the prompt is skipped); -1 when an arm didn't answer.
|
||||
* Always fires before onResult(true).
|
||||
*/
|
||||
void onBattery(int rightPercent, int leftPercent);
|
||||
|
||||
/** Fires once the user picks a menu row (or, with skipPrompt, once the battery is read): true = flash, false = cancel. */
|
||||
void onResult(boolean approved);
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
@@ -26,6 +27,14 @@ public final class FaceclawLocationTracker implements LocationListener {
|
||||
private final LocationManager locationManager;
|
||||
private final Handler callbackHandler;
|
||||
|
||||
/**
|
||||
* Only seed from the platform's cached fix when it is this fresh. An older
|
||||
* one is likely wherever the phone was last used (for example the previous
|
||||
* navigation's destination), and guidance built on it is wrong from the
|
||||
* first frame: the route starts there and arrival fires immediately.
|
||||
*/
|
||||
private static final long MAX_SEED_AGE_MS = 60L * 1000L;
|
||||
|
||||
private FaceclawLocationTrackerListener listener;
|
||||
private boolean running;
|
||||
|
||||
@@ -83,11 +92,9 @@ public final class FaceclawLocationTracker implements LocationListener {
|
||||
locationManager.requestLocationUpdates(provider, Math.max(500L, intervalMs), 0f, this,
|
||||
callbackHandler.getLooper());
|
||||
// Seed with the freshest cached fix so the UI has a position
|
||||
// before the first live fix (GPS cold starts can take a while).
|
||||
Location cached = locationManager.getLastKnownLocation(provider);
|
||||
if (cached == null) {
|
||||
cached = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||
}
|
||||
// before the first live fix (GPS cold starts can take a while),
|
||||
// but only if it's recent enough to still be where the user is.
|
||||
Location cached = freshestCachedLocation(provider);
|
||||
if (cached != null) {
|
||||
deliverLocation(cached);
|
||||
}
|
||||
@@ -100,6 +107,32 @@ public final class FaceclawLocationTracker implements LocationListener {
|
||||
}
|
||||
}
|
||||
|
||||
private Location freshestCachedLocation(String primaryProvider) {
|
||||
Location best = null;
|
||||
for (String provider : new String[] {primaryProvider, LocationManager.NETWORK_PROVIDER,
|
||||
LocationManager.GPS_PROVIDER}) {
|
||||
Location candidate;
|
||||
try {
|
||||
candidate = locationManager.getLastKnownLocation(provider);
|
||||
} catch (Throwable ignored) {
|
||||
continue;
|
||||
}
|
||||
if (candidate == null || ageMs(candidate) > MAX_SEED_AGE_MS) {
|
||||
continue;
|
||||
}
|
||||
if (best == null || candidate.getElapsedRealtimeNanos() > best.getElapsedRealtimeNanos()) {
|
||||
best = candidate;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/** Age via the monotonic clock, so a wall-clock change can't make an old fix look fresh. */
|
||||
private static long ageMs(Location location) {
|
||||
long ageNanos = SystemClock.elapsedRealtimeNanos() - location.getElapsedRealtimeNanos();
|
||||
return ageNanos / 1_000_000L;
|
||||
}
|
||||
|
||||
private String chooseProvider() {
|
||||
try {
|
||||
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||
|
||||
@@ -23,6 +23,8 @@ import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class FaceclawMediaController {
|
||||
private final Context appContext;
|
||||
@@ -71,6 +73,7 @@ public class FaceclawMediaController {
|
||||
private volatile FaceclawMediaControllerListener listener;
|
||||
private MediaController activeController;
|
||||
private boolean started;
|
||||
private Set<String> ignoredPackages = new HashSet<>();
|
||||
|
||||
public FaceclawMediaController(Context context) {
|
||||
this.appContext = context.getApplicationContext();
|
||||
@@ -113,6 +116,23 @@ public class FaceclawMediaController {
|
||||
}
|
||||
}
|
||||
|
||||
/** Apply the complete preference set and immediately choose an allowed session. */
|
||||
public void setIgnoredPackagesJson(String packagesJson) {
|
||||
Set<String> packages = new HashSet<>();
|
||||
try {
|
||||
JSONArray array = new JSONArray(packagesJson);
|
||||
for (int i = 0; i < array.length(); i++) packages.add(array.getString(i));
|
||||
} catch (Exception e) {
|
||||
Log.w("FaceclawMedia", "invalid ignored packages", e);
|
||||
return;
|
||||
}
|
||||
synchronized (lock) {
|
||||
if (ignoredPackages.equals(packages)) return;
|
||||
ignoredPackages = packages;
|
||||
refreshActiveControllerLocked(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
synchronized (lock) {
|
||||
if (!started) {
|
||||
@@ -205,11 +225,11 @@ public class FaceclawMediaController {
|
||||
* Album art for the active session's current item, grayscale, scaled to
|
||||
* fit within maxSize x maxSize preserving aspect. Returns a gray packet
|
||||
* (see ImageFileLoader.bitmapToGrayPacket) or an empty array when no art
|
||||
* is available. Cover art is continuous-tone, so it is tone-mapped by
|
||||
* gamma and dithered onto the display's 16 levels rather than rounded
|
||||
* per pixel.
|
||||
* is available. gamma and dither are the photographic tone handling
|
||||
* described on ImageFileLoader.bitmapToGrayPacket; the TS side passes
|
||||
* its shared photo preset.
|
||||
*/
|
||||
public byte[] getAlbumArtGray(int maxSize, float gamma) {
|
||||
public byte[] getAlbumArtGray(int maxSize, float gamma, boolean dither) {
|
||||
Bitmap art;
|
||||
synchronized (lock) {
|
||||
if (activeController == null) {
|
||||
@@ -227,7 +247,7 @@ public class FaceclawMediaController {
|
||||
art = metadata.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON);
|
||||
}
|
||||
}
|
||||
return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize, gamma, true);
|
||||
return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize, gamma, dither);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -289,6 +309,7 @@ public class FaceclawMediaController {
|
||||
controllers = null;
|
||||
}
|
||||
}
|
||||
emitSessionAppsLocked(controllers);
|
||||
setActiveControllerLocked(chooseController(controllers));
|
||||
emitStateLocked();
|
||||
}
|
||||
@@ -297,8 +318,10 @@ public class FaceclawMediaController {
|
||||
if (controllers == null || controllers.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
MediaController first = controllers.get(0);
|
||||
MediaController first = null;
|
||||
for (MediaController controller : controllers) {
|
||||
if (ignoredPackages.contains(controller.getPackageName())) continue;
|
||||
if (first == null) first = controller;
|
||||
PlaybackState playbackState = controller.getPlaybackState();
|
||||
if (playbackState != null && playbackState.getState() == PlaybackState.STATE_PLAYING) {
|
||||
return controller;
|
||||
@@ -307,6 +330,27 @@ public class FaceclawMediaController {
|
||||
return first;
|
||||
}
|
||||
|
||||
private void emitSessionAppsLocked(List<MediaController> controllers) {
|
||||
FaceclawMediaControllerListener currentListener = listener;
|
||||
if (currentListener == null || controllers == null) return;
|
||||
JSONArray apps = new JSONArray();
|
||||
Set<String> seen = new HashSet<>();
|
||||
for (MediaController controller : controllers) {
|
||||
String packageName = safe(controller.getPackageName());
|
||||
if (packageName.isEmpty() || !seen.add(packageName)) continue;
|
||||
try {
|
||||
JSONObject app = new JSONObject();
|
||||
app.put("packageName", packageName);
|
||||
app.put("appName", getApplicationLabel(packageName));
|
||||
apps.put(app);
|
||||
} catch (Exception e) {
|
||||
Log.w("FaceclawMedia", "session app serialization failed", e);
|
||||
}
|
||||
}
|
||||
String json = apps.toString();
|
||||
mainHandler.post(() -> currentListener.onSessionAppsChanged(json));
|
||||
}
|
||||
|
||||
private void setActiveControllerLocked(MediaController controller) {
|
||||
if (sameController(activeController, controller)) {
|
||||
return;
|
||||
|
||||
+3
@@ -1,6 +1,9 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawMediaControllerListener {
|
||||
/** All observed session owners, before filtering or choosing the active player. */
|
||||
void onSessionAppsChanged(String appsJson);
|
||||
|
||||
void onStateChange(
|
||||
String playbackState,
|
||||
String packageName,
|
||||
|
||||
+6
-6
@@ -210,7 +210,7 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
|
||||
public static String getActiveNotificationsJson(int maxNotifications) {
|
||||
FaceclawMediaNotificationListenerService service = activeService;
|
||||
int limit = Math.max(0, Math.min(100, maxNotifications));
|
||||
int limit = Math.max(0, maxNotifications);
|
||||
if (service == null || limit == 0) {
|
||||
return "[]";
|
||||
}
|
||||
@@ -388,11 +388,11 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
if (statusBarNotification == null || statusBarNotification.getNotification() == null) {
|
||||
return false;
|
||||
}
|
||||
// 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)) {
|
||||
// Our own notifications stay out of the mirror: the foreground-service
|
||||
// one is noise, and the Timers app rings on the glasses itself (its
|
||||
// phone notification is for the phone), so mirroring it would stack a
|
||||
// notification modal over the ringing screen.
|
||||
if (service.getPackageName().equals(statusBarNotification.getPackageName())) {
|
||||
return false;
|
||||
}
|
||||
Notification notification = statusBarNotification.getNotification();
|
||||
|
||||
@@ -72,6 +72,11 @@ public final class FaceclawPreviewCompositor {
|
||||
compositor.removeSurface(id);
|
||||
}
|
||||
|
||||
/** Dim every surface below zOrder belowZOrder to factor256/256 (see SurfaceCompositor). */
|
||||
public void setUnderlayDim(int belowZOrder, int factor256) {
|
||||
compositor.setUnderlayDim(belowZOrder, factor256);
|
||||
}
|
||||
|
||||
public void setSurfaceVisible(String id, boolean visible) {
|
||||
compositor.setSurfaceVisible(id, visible);
|
||||
}
|
||||
|
||||
@@ -1,187 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,9 @@ import android.util.Log;
|
||||
* Decodes image files (and bitmaps generally) to the grayscale packet format
|
||||
* used across the TS bridge: [widthLo, widthHi, heightLo, heightHi,
|
||||
* pixels...] with one byte per pixel, row-major, or an empty array on
|
||||
* failure. FaceclawMediaController's album art shares bitmapToGrayPacket.
|
||||
* failure. FaceclawMediaController's album art shares bitmapToGrayPacket;
|
||||
* the TS side (app/native/image-files.ts) owns the shared photo-tone preset
|
||||
* passed as (gamma, dither).
|
||||
*/
|
||||
public final class ImageFileLoader {
|
||||
private static final String TAG = "ImageFileLoader";
|
||||
@@ -17,9 +19,21 @@ public final class ImageFileLoader {
|
||||
|
||||
/**
|
||||
* Decode an image file and downscale it to fit within maxWidth x
|
||||
* maxHeight, preserving aspect ratio and never upscaling.
|
||||
* maxHeight, preserving aspect ratio and never upscaling. Flat
|
||||
* conversion (no tone curve, per-pixel rounding): right for UI raster
|
||||
* such as icons.
|
||||
*/
|
||||
public static byte[] loadGray(String path, int maxWidth, int maxHeight) {
|
||||
return loadGray(path, maxWidth, maxHeight, 1f, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, with the photographic tone handling described on
|
||||
* bitmapToGrayPacket(Bitmap, int, int, float, boolean): use for
|
||||
* continuous-tone images (photos) viewed on the glasses.
|
||||
*/
|
||||
public static byte[] loadGray(
|
||||
String path, int maxWidth, int maxHeight, float gamma, boolean dither) {
|
||||
if (path == null || maxWidth <= 0 || maxHeight <= 0) {
|
||||
return new byte[0];
|
||||
}
|
||||
@@ -43,7 +57,7 @@ public final class ImageFileLoader {
|
||||
if (bitmap == null) {
|
||||
return new byte[0];
|
||||
}
|
||||
return bitmapToGrayPacket(bitmap, maxWidth, maxHeight);
|
||||
return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither);
|
||||
} catch (Exception | OutOfMemoryError e) {
|
||||
Log.w(TAG, "image decode failed: " + path, e);
|
||||
return new byte[0];
|
||||
@@ -56,6 +70,12 @@ public final class ImageFileLoader {
|
||||
* ByteBuffer because NativeScript marshals a JS ArrayBuffer to one.
|
||||
*/
|
||||
public static byte[] loadGrayFromBytes(java.nio.ByteBuffer buffer, int maxWidth, int maxHeight) {
|
||||
return loadGrayFromBytes(buffer, maxWidth, maxHeight, 1f, false);
|
||||
}
|
||||
|
||||
/** As above, with photographic tone handling (see bitmapToGrayPacket). */
|
||||
public static byte[] loadGrayFromBytes(
|
||||
java.nio.ByteBuffer buffer, int maxWidth, int maxHeight, float gamma, boolean dither) {
|
||||
if (buffer == null || maxWidth <= 0 || maxHeight <= 0) {
|
||||
return new byte[0];
|
||||
}
|
||||
@@ -71,7 +91,7 @@ public final class ImageFileLoader {
|
||||
if (bitmap == null) {
|
||||
return new byte[0];
|
||||
}
|
||||
return bitmapToGrayPacket(bitmap, maxWidth, maxHeight);
|
||||
return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither);
|
||||
} catch (Exception | OutOfMemoryError e) {
|
||||
Log.w(TAG, "byte-array image decode failed", e);
|
||||
return new byte[0];
|
||||
|
||||
@@ -161,6 +161,9 @@ public final class SurfaceCompositor {
|
||||
private int screenWidth;
|
||||
private int screenHeight;
|
||||
private boolean blanked;
|
||||
/** See setUnderlayDim: surfaces with zOrder below this are dimmed by underlayDim/256. */
|
||||
private int underlayDimBelowZOrder = Integer.MIN_VALUE;
|
||||
private int underlayDim = 256;
|
||||
private final Map<String, Surface> surfaces = new HashMap<>();
|
||||
private long nextCompositeSeq = 1;
|
||||
|
||||
@@ -216,6 +219,33 @@ public final class SurfaceCompositor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dim every surface whose zOrder is below belowZOrder to factor256/256 of
|
||||
* its brightness (256 = no dimming): how a shell overlay that dims what it
|
||||
* covers (the TS Layer.dimUnderneath) reaches the window surfaces beneath
|
||||
* the shell surface, which the shell's own layer stack cannot paint.
|
||||
* Visible pixels stay at least 1 (the color-key black); glyph and
|
||||
* firmware-text draws keep their cached-draw form with a dimmed value;
|
||||
* image draws leave the composite's draw list (their pixels, already
|
||||
* baked into the surface, dim as raster). Takes effect when the next
|
||||
* frame composites.
|
||||
*/
|
||||
public void setUnderlayDim(int belowZOrder, int factor256) {
|
||||
synchronized (lock) {
|
||||
underlayDimBelowZOrder = belowZOrder;
|
||||
underlayDim = Math.max(0, Math.min(256, factor256));
|
||||
}
|
||||
}
|
||||
|
||||
/** The dim factor (256 = none) that applies to a surface. */
|
||||
private int dimForLocked(Surface surface) {
|
||||
return surface.zOrder < underlayDimBelowZOrder ? underlayDim : 256;
|
||||
}
|
||||
|
||||
private static int dimValue(int value, int dim) {
|
||||
return Math.max(1, (value * dim + 128) >> 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hidden surfaces keep their retained pixels and accept updates, but are
|
||||
* excluded from the composite and its fingerprint (used for background
|
||||
@@ -410,9 +440,12 @@ public final class SurfaceCompositor {
|
||||
for (Surface surface : ordered) {
|
||||
if (!surface.visible) continue;
|
||||
blendLocked(gray, surface);
|
||||
int dim = dimForLocked(surface);
|
||||
for (ScreenDraw draw : surface.draws) {
|
||||
if (dim < 256 && draw.kind == ScreenDraw.KIND_IMAGE) continue;
|
||||
int value = dim < 256 ? dimValue(draw.value, dim) : draw.value;
|
||||
draws.add(new ScreenDraw(draw.kind, draw.fontId, draw.encoding, draw.imageId,
|
||||
draw.x + surface.x, draw.y + surface.y, draw.value,
|
||||
draw.x + surface.x, draw.y + surface.y, value,
|
||||
draw.fwCps, draw.fwDx, draw.fwInk));
|
||||
}
|
||||
fingerprint.append('|').append(surface.id)
|
||||
@@ -421,6 +454,9 @@ public final class SurfaceCompositor {
|
||||
.append('#').append(surface.zOrder)
|
||||
.append(':').append(surface.transparency)
|
||||
.append(':').append(surface.fingerprint);
|
||||
if (dim < 256) {
|
||||
fingerprint.append(":dim").append(dim);
|
||||
}
|
||||
}
|
||||
return new Composite(gray, screenWidth, screenHeight, fingerprint.toString(), nextCompositeSeq++,
|
||||
draws.toArray(new ScreenDraw[0]));
|
||||
@@ -437,10 +473,22 @@ public final class SurfaceCompositor {
|
||||
if (copyWidth <= 0 || copyHeight <= 0) {
|
||||
return;
|
||||
}
|
||||
int dim = dimForLocked(surface);
|
||||
for (int row = 0; row < copyHeight; row++) {
|
||||
int srcOffset = (srcY + row) * surface.width + srcX;
|
||||
int dstOffset = (dstY + row) * screenWidth + dstX;
|
||||
if (surface.transparency == TRANSPARENCY_OPAQUE) {
|
||||
if (dim < 256) {
|
||||
// Dimmed: opaque or color-keyed, 0 stays 0 (black / transparent)
|
||||
// and everything visible scales, never below 1.
|
||||
for (int col = 0; col < copyWidth; col++) {
|
||||
int value = surface.pixels[srcOffset + col] & 0xff;
|
||||
if (value != 0) {
|
||||
gray[dstOffset + col] = (byte) dimValue(value, dim);
|
||||
} else if (surface.transparency == TRANSPARENCY_OPAQUE) {
|
||||
gray[dstOffset + col] = 0;
|
||||
}
|
||||
}
|
||||
} else if (surface.transparency == TRANSPARENCY_OPAQUE) {
|
||||
System.arraycopy(surface.pixels, srcOffset, gray, dstOffset, copyWidth);
|
||||
} else {
|
||||
for (int col = 0; col < copyWidth; col++) {
|
||||
|
||||
@@ -57,6 +57,12 @@ public class BleProtocol {
|
||||
*/
|
||||
public static final int FACECLAW_MIC_CONTROL_FIELD = 103;
|
||||
public static final int FACECLAW_MIC_STATUS_FIELD = 104;
|
||||
/**
|
||||
* CFW als_sensor (EVENCFW/18, caps token als16): ambient-light reports on
|
||||
* settings-channel field 105, 24-byte ['A','L',ver,reason,...] records from
|
||||
* the master temple (decoded in app/native/ambient-light.ts).
|
||||
*/
|
||||
public static final int FACECLAW_ALS_REPORT_FIELD = 105;
|
||||
public static final int FACECLAW_WAKE_OP_ACQUIRE = 1;
|
||||
public static final int FACECLAW_WAKE_OP_RELEASE = 2;
|
||||
public static final int FACECLAW_WAKE_OP_CLAIM = 3;
|
||||
@@ -104,6 +110,8 @@ public class BleProtocol {
|
||||
public static final int EVENT_IMU_DATA_REPORT = 8;
|
||||
public static final int EVENT_RING_LONG_PRESS = 9;
|
||||
public static final int EVENT_RING_LONG_PRESS_RELEASE = 10;
|
||||
// CFW extension: G2 2.2.9 tap-then-hold gesture forwarded to the phone.
|
||||
public static final int EVENT_SHORT_THEN_LONG_PRESS = 11;
|
||||
|
||||
public static final int EVENT_SOURCE_GLASSES_R = 1;
|
||||
public static final int EVENT_SOURCE_RING = 2;
|
||||
@@ -447,6 +455,52 @@ public class BleProtocol {
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw CFW ambient-light report from a sid-0x09 frame, or null
|
||||
* when this is an ordinary settings frame. Only the 'A','L',version prelude
|
||||
* is validated here; the fields are decoded on the TS side.
|
||||
*/
|
||||
public static byte[] parseFaceclawAlsReport(byte[] pb) {
|
||||
if (pb == null) {
|
||||
return null;
|
||||
}
|
||||
byte[] body = readFieldBytes(stripTrailingCrc(pb), FACECLAW_ALS_REPORT_FIELD);
|
||||
if (body == null
|
||||
|| body.length < 24
|
||||
|| body[0] != 'A'
|
||||
|| body[1] != 'L'
|
||||
|| (body[2] & 0xff) != 1) {
|
||||
return null;
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
/** Field 106: RB/version 1/flags/percentage, from CFW ringbat17. */
|
||||
public static RingBatterySnapshot parseRingBattery(byte[] pb) {
|
||||
if (pb == null) return null;
|
||||
byte[] body = readFieldBytes(stripTrailingCrc(pb), 106);
|
||||
if (body == null || body.length != 5 || body[0] != 'R' || body[1] != 'B'
|
||||
|| body[2] != 1) return null;
|
||||
int flags = body[3] & 0xff;
|
||||
int level = body[4] & 0xff;
|
||||
if ((flags & ~7) != 0) return null;
|
||||
if ((flags & 2) != 0) {
|
||||
if ((flags & 1) == 0 || level > 100) return null;
|
||||
return new RingBatterySnapshot(level, (flags & 4) != 0 ? 1 : 0);
|
||||
}
|
||||
if (level != 255 || (flags & 4) != 0) return null;
|
||||
return new RingBatterySnapshot(-1, -1);
|
||||
}
|
||||
|
||||
public static final class RingBatterySnapshot {
|
||||
final int battery;
|
||||
final int charging;
|
||||
RingBatterySnapshot(int battery, int charging) {
|
||||
this.battery = battery;
|
||||
this.charging = charging;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the uint16 wake nonce from a CFW field-102 notification, or -1
|
||||
* when this is an ordinary settings frame.
|
||||
@@ -744,7 +798,23 @@ public class BleProtocol {
|
||||
byte[] compass = readFieldBytes(root, 10);
|
||||
if (compass == null) return null;
|
||||
int heading = readVarintFieldValue(compass, 1, -1);
|
||||
return heading >= 0 ? new CompassEvent(command, heading) : null;
|
||||
if (heading < 0 || heading >= 360) return null;
|
||||
byte[] diagnostic = readFieldBytes(root, 100);
|
||||
if (diagnostic != null && diagnostic.length == 12
|
||||
&& diagnostic[0] == 'C' && diagnostic[1] == 'M' && diagnostic[2] == 1
|
||||
&& diagnostic[7] == 0
|
||||
&& ((diagnostic[3] & 0xff) <= 3 || diagnostic[3] == (byte) 255)
|
||||
&& ((diagnostic[4] & 0xff) <= 2 || diagnostic[4] == (byte) 255)
|
||||
&& (diagnostic[5] & 0xff) <= 3) {
|
||||
long sampleTimeMs = 0;
|
||||
for (int i = 0; i < 4; i++) sampleTimeMs |= (long) (diagnostic[8 + i] & 0xff) << (8 * i);
|
||||
return new CompassEvent(command, heading,
|
||||
diagnostic[3] == (byte) 255 ? -1 : diagnostic[3] & 0xff,
|
||||
diagnostic[4] == (byte) 255 ? -1 : diagnostic[4] & 0xff,
|
||||
diagnostic[5] & 0xff, diagnostic[6] & 0xff, sampleTimeMs);
|
||||
}
|
||||
// Stock/older CFW and unknown extensions still supply usable headings.
|
||||
return new CompassEvent(command, heading);
|
||||
}
|
||||
if (command == NAV_CMD_COMPASS_CALIBRATION_STARTED
|
||||
|| command == NAV_CMD_COMPASS_CALIBRATION_COMPLETE) {
|
||||
@@ -986,10 +1056,23 @@ public class BleProtocol {
|
||||
public static final class CompassEvent {
|
||||
public final int command;
|
||||
public final int headingDegrees;
|
||||
/** -1 means unavailable; source: 0 unknown, 1 GRV, 2 GMRV, 3 RV. */
|
||||
public final int magneticAccuracy, magneticAnomalies, orientationSource, diagnosticFlags;
|
||||
public final long sampleTimeMs;
|
||||
|
||||
CompassEvent(int command, int headingDegrees) {
|
||||
this(command, headingDegrees, -1, -1, -1, -1, -1);
|
||||
}
|
||||
|
||||
CompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies,
|
||||
int orientationSource, int diagnosticFlags, long sampleTimeMs) {
|
||||
this.command = command;
|
||||
this.headingDegrees = headingDegrees;
|
||||
this.magneticAccuracy = magneticAccuracy;
|
||||
this.magneticAnomalies = magneticAnomalies;
|
||||
this.orientationSource = orientationSource;
|
||||
this.diagnosticFlags = diagnosticFlags;
|
||||
this.sampleTimeMs = sampleTimeMs;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Keyboard icon for the main page's tab-bar keyboard-dialog button (beside
|
||||
the mic button). Material Design "keyboard" glyph (Apache 2.0); base fill
|
||||
is overridden by tint-color in app.css. -->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="#FF333333"
|
||||
android:pathData="M20 5H4c-1.1 0-1.99.9-1.99 2L2 17c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm-9 3h2v2h-2V8zm0 3h2v2h-2v-2zM8 8h2v2H8V8zm0 3h2v2H8v-2zm-1 2H5v-2h2v2zm0-3H5V8h2v2zm9 7H8v-2h8v2zm0-4h-2v-2h2v2zm0-3h-2V8h2v2zm3 3h-2v-2h2v2zm0-3h-2V8h2v2z" />
|
||||
</vector>
|
||||
@@ -1,5 +1,55 @@
|
||||
# Changelog
|
||||
|
||||
## 0.6.6 - Future
|
||||
* Navigate and terminal app: App-specific screen size/position setting
|
||||
* Transcribe app: Scrolling; automatic reconnection; insert newlines
|
||||
* Notifications: Add notification filters
|
||||
* Phone screen preview: Add rotation option; improve landscape layout
|
||||
* Add "AI Chat" app (conversational-style use of the voice assistant with hold-to-talk)
|
||||
* Add rightness control to the system context menu
|
||||
* Messaging in the flasher distinguishes between downgrading from stock firmwares where that's been validated and veresions where it hasn't, and considers 2.2.10 validated
|
||||
* Add R1 battery indicator to the top bar
|
||||
* Change the styling and add more customization options for battery indicators
|
||||
|
||||
## 0.6.5 - 2026-09-04
|
||||
* Changed context menu gestures: — for system, ●— for app context menu.
|
||||
* System and app context menus show gesture help for switching to the other context menu
|
||||
* Move some items from app context menus to the system context menu
|
||||
* Long-press now opens the system context menu and tap-then-hold the app context menu (games keep long-press as a move while playing)
|
||||
* Content beneath an open context menu is dimmed
|
||||
* Add a keyboard-input button to the phone home screen
|
||||
* Add a light sensor demo to the Developer app
|
||||
* Move Calculator, Microphones, Nightscout, and Roam apps to Prototypes folder by default
|
||||
* Navigate app: Location indicator rotates based on the compass
|
||||
* Navigate app: Add saved home/work and recent address settings
|
||||
* Navigate app: Darken the map background color
|
||||
* EvenHub app: Add New, Search, and Updates tabs
|
||||
* Timer app: Fully rewritten, with new better UI
|
||||
* Add a teleprompter app
|
||||
|
||||
## 0.6.4 - 2026-09-02
|
||||
* Context menus are centered and include window title
|
||||
* Long-press and short-then-long with a context menu open switches between context menus
|
||||
* Developer speed metrics include bytes/frame
|
||||
* The custom firmware uninstaller flashes 2.2.9.22, not 2.2.6.10
|
||||
* Compass app: Increase sample rate from 1/s to ~3/s
|
||||
* Compass app: Handle the distinction between true north and magnetic north
|
||||
* EvenHub compass readings now include calibrated magnetic, declination and true headings
|
||||
* Files app: Viewing image files gamma-corrects and dithers them
|
||||
* Watch app: Uses ambient mode so the watch no longer blurs when idle
|
||||
* Watch app: Fix flicker on waking the watch
|
||||
* Watch app: Tapping the idle watch to wake it counts as a tap on the pad
|
||||
* Watch app: Remove a number of unfinished prototype features
|
||||
* Firmware flasher checks for glasses battery>30% before starting
|
||||
* Fix a problem in 0.6.3 where it would abort before starting a custom firmware install with a checksum error
|
||||
|
||||
## 0.6.3 - 2026-09-01
|
||||
* Update custom firmware base version from 2.2.6 to 2.2.9
|
||||
* Add support for the short-then-long-press gesture
|
||||
* Update EvenHub compatibility layer to include new functions from 0.0.14
|
||||
* EvenHub permissions dialog scrolls when it overflows
|
||||
* Add "Show BLE bandwidth usage" option to developer settings
|
||||
|
||||
## 0.6.2 - 2026-08-30
|
||||
* Preview Only mode works
|
||||
* Voice input uses phone mic in preview mode
|
||||
|
||||
@@ -18,7 +18,7 @@ User-facing documentation lives at https://faceclaw.org/.
|
||||

|
||||

|
||||

|
||||
|
||||

|
||||
|
||||
## Installation
|
||||
|
||||
@@ -81,23 +81,14 @@ import it into the new version.
|
||||
* **Power management**: the glasses go to sleep properly when the screen is
|
||||
off, and wake when you double-tap the ring or speak the wakeword, allowing
|
||||
battery life similar to the stock Even app.
|
||||
* **Connection management** with auto-reconnect, and autodetection of
|
||||
conflict with the official Even Realities app.
|
||||
* **A Wear OS watch app** that replaces (and outdoes) the R1 ring: tap,
|
||||
* **A Wear OS watch app** that complements (or replaces) the R1 ring: tap,
|
||||
swipe, hold and crown gestures, side buttons, app launching and window
|
||||
switching, voice or keyboard queries to the assistant with the reply on
|
||||
your wrist, typing into apps, and glasses status/lock/display control.
|
||||
* **On-phone screen mirroring with touch control**: tap what you see on the
|
||||
mirror (sidebar icons, launcher cells), or use the phone's own touchpad,
|
||||
d-pad and Back/Menu buttons — the same spatial scheme as the watch — plus a
|
||||
compact ring simulator. A display-mode picker (576×288 band, 576×480 tall,
|
||||
or the full 640×480 panel with an auto-hiding sidebar) and a brightness
|
||||
slider with an Auto toggle sit beside the mirror.
|
||||
* **Bluetooth pairing** that scans for nearby glasses and identifies each
|
||||
pair before connecting: model, frame shape, and colour decoded from the
|
||||
advertised serial (with product photos), left and right arms matched to
|
||||
each other by that serial, an estimated distance so the pair in your hand
|
||||
sorts first, and the optional R1 ring.
|
||||
* **On-phone screen mirroring**: Shows the glasses screen contents and
|
||||
provides a phone-screen touch area equivalent to the R1 ring or the Wear OS
|
||||
watch app. This can also be used to try out the app without pairing or
|
||||
flashing a real pair of glasses.
|
||||
* **Dual-language NativeScript architecture**, with Java for the
|
||||
multithreaded Android API and bluetooth stack bits, Typescript for the bits
|
||||
you want to hack on.
|
||||
|
||||
+36
-7
@@ -98,6 +98,13 @@ Button.-primary {
|
||||
font-size: 13;
|
||||
}
|
||||
|
||||
/* Developer BLE-bandwidth totals overlaid on the main page's bottom edge. */
|
||||
.ble-bandwidth-indicator {
|
||||
color: #777777;
|
||||
font-size: 12;
|
||||
padding: 2 8;
|
||||
}
|
||||
|
||||
/*
|
||||
* Stands in for the lens preview when showing it would be misleading (silent
|
||||
* mode, or a disconnect explained by a flat battery). Sized by the view model
|
||||
@@ -175,11 +182,12 @@ Button.-primary {
|
||||
background-color: #424242;
|
||||
color: #ffffff;
|
||||
}
|
||||
/* The wakeword button at the tab bar's right edge: icon-only, narrower than
|
||||
* the tabs, and never in the selected state (it isn't a tab). It is a
|
||||
* button-styled GridLayout (Buttons can't hold images), so it borrows the
|
||||
* .tab-button look and adds the elevation MaterialButtons get for free. */
|
||||
.tab-mic-button {
|
||||
/* The wakeword and keyboard buttons at the tab bar's right edge: icon-only,
|
||||
* narrower than the tabs, and never in the selected state (they aren't tabs).
|
||||
* Each is a button-styled GridLayout (Buttons can't hold images), so it
|
||||
* borrows the .tab-button look and adds the elevation MaterialButtons get for
|
||||
* free. */
|
||||
.tab-icon-button {
|
||||
width: 56;
|
||||
/* The real MaterialButtons render 48dp tall inside a 60dp layout box (6dp
|
||||
* insets top and bottom); the margins and explicit height reproduce that.
|
||||
@@ -189,11 +197,32 @@ Button.-primary {
|
||||
margin: 6 2;
|
||||
android-elevation: 2;
|
||||
}
|
||||
.tab-mic-icon {
|
||||
.tab-icon {
|
||||
height: 20;
|
||||
tint-color: #333333;
|
||||
}
|
||||
.ns-dark .tab-mic-icon {
|
||||
/* Landscape uses the full page height and a shared left edge for tabs/pad. */
|
||||
.landscape-layout .remote-controls {
|
||||
margin: 0;
|
||||
}
|
||||
.landscape-layout .controls-tab-bar {
|
||||
margin-bottom: 2;
|
||||
}
|
||||
.landscape-layout .tab-button {
|
||||
margin-left: 1;
|
||||
margin-right: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
.landscape-layout .tab-button[col="0"] {
|
||||
margin-left: 0;
|
||||
}
|
||||
.landscape-layout .tab-icon-button {
|
||||
width: 48;
|
||||
}
|
||||
.landscape-layout .tab-icon-button[col="4"] {
|
||||
margin-right: 0;
|
||||
}
|
||||
.ns-dark .tab-icon {
|
||||
tint-color: #cccccc;
|
||||
}
|
||||
.ns-dark .tab-button {
|
||||
|
||||
@@ -7,9 +7,11 @@ purpose of the file is to pass control to the app’s first module.
|
||||
import { Application } from '@nativescript/core'
|
||||
import { registerShareIntentHandler } from './native/share-intents'
|
||||
import { installNativeUserAgent } from './util/http'
|
||||
import { registerPhoneRotation } from './native/phone-rotation'
|
||||
|
||||
installNativeUserAgent()
|
||||
registerShareIntentHandler()
|
||||
registerPhoneRotation()
|
||||
|
||||
Application.run({ moduleName: 'app-root' })
|
||||
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { ASSISTANT_MODEL_VALUES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models";
|
||||
import type { AssistantTranscriptEntry } from "../../assistant/session";
|
||||
import type { AssistantConversations, ReasoningLevel } from "../../assistant/conversations";
|
||||
import { anthropicApiKeySetting, assistantBackendSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, gestureHints, type InputEvent } from "../../ui/gestures";
|
||||
import type { Layer, LayerContext } from "../../ui/layers";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import { WindowMenuLayer } from "../../ui/window-menu";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { VoiceDraft } from "./voice-draft";
|
||||
|
||||
class AiChatLayer implements Layer {
|
||||
readonly acceptsDirectional = true;
|
||||
private firstLine: number | null = null; // null follows the streaming tail
|
||||
private maxFirstLine = 0;
|
||||
private selectedId = "";
|
||||
private requestRender = () => {};
|
||||
private readonly wrappedEntries = new WeakMap<AssistantTranscriptEntry, {
|
||||
font: UiFont; width: number; text: string; lines: string[];
|
||||
}>();
|
||||
readonly draft: VoiceDraft;
|
||||
|
||||
constructor(private readonly conversations: AssistantConversations, options: InProcessAppOptions) {
|
||||
this.draft = new VoiceDraft(options.actions, () => shell.prepareChatVoiceCapture(),
|
||||
() => this.requestRender(), (text) => this.send(text));
|
||||
}
|
||||
start(render: () => void): () => void {
|
||||
this.requestRender = render;
|
||||
return this.conversations.onChanged(() => render());
|
||||
}
|
||||
send(text: string): void {
|
||||
this.firstLine = null;
|
||||
shell.sendToAssistant(text, false);
|
||||
}
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const size = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(size.width, size.height);
|
||||
const font = getDefaultSmallFont();
|
||||
const step = font.lineHeight + 3;
|
||||
const left = 12;
|
||||
const width = size.width - 28;
|
||||
const record = this.conversations.current();
|
||||
if (this.selectedId !== record.id) { this.selectedId = record.id; this.firstLine = null; }
|
||||
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
|
||||
const title = this.conversations.title();
|
||||
image.drawText(font, left, 6, truncateText(font, title, width), 180);
|
||||
const label = assistantBackendSetting.get() === "external" ? "External agent" :
|
||||
`${assistantModelLabel(record.model)} · ${record.reasoning === "default" ? "default reasoning" : record.reasoning}`;
|
||||
const footer = wrapText(font, `${label} ${gestureHints([
|
||||
[GESTURE_CLICK, "menu"], [GESTURE_DOUBLE_CLICK, "back"], [GESTURE_LONG_PRESS, "hold to talk"],
|
||||
])}`, width);
|
||||
const footerY = size.height - step * footer.length;
|
||||
const footerTop = footerY - step * 3 - 12;
|
||||
const historyTop = step + 16;
|
||||
const visible = Math.max(1, Math.floor((footerTop - historyTop - 8) / step));
|
||||
const lines: { text: string; color: number }[] = [];
|
||||
for (const entry of transcript) {
|
||||
if (!entry.text) continue;
|
||||
let wrapped = this.wrappedEntries.get(entry);
|
||||
if (!wrapped || wrapped.font !== font || wrapped.width !== width || wrapped.text !== entry.text) {
|
||||
wrapped = { font, width, text: entry.text,
|
||||
lines: wrapText(font, `${entry.role === "user" ? "You" : "AI"}: ${entry.text}`, width - 8) };
|
||||
this.wrappedEntries.set(entry, wrapped);
|
||||
}
|
||||
for (const text of wrapped.lines) {
|
||||
lines.push({ text, color: entry.role === "user" ? 175 : 245 });
|
||||
}
|
||||
lines.push({ text: "", color: 0 });
|
||||
}
|
||||
if (!lines.length) lines.push({ text: "Hold to talk to your assistant.", color: 175 },
|
||||
{ text: "Voice and chat share this conversation.", color: 150 });
|
||||
this.maxFirstLine = Math.max(0, lines.length - visible);
|
||||
const first = this.firstLine === null ? this.maxFirstLine : Math.min(this.firstLine, this.maxFirstLine);
|
||||
for (let row = 0; row < visible; row++) {
|
||||
const line = lines[first + row];
|
||||
if (line) image.drawText(font, left, historyTop + row * step, line.text, line.color);
|
||||
}
|
||||
if (this.maxFirstLine > 0) {
|
||||
const track = visible * step;
|
||||
const thumb = Math.max(8, Math.floor(track * visible / lines.length));
|
||||
image.fillRect(size.width - 5, historyTop + Math.floor((track - thumb) * first / this.maxFirstLine), 2, thumb, 110);
|
||||
}
|
||||
image.fillRect(left, footerTop, width, 1, 65);
|
||||
const state = this.draft.active ? this.draft.status : record.session?.status || this.draft.status;
|
||||
if (state) image.drawText(font, left, footerTop + 5, truncateText(font, state, width), 160);
|
||||
const pending = wrapText(font, this.draft.text || (this.draft.active ? "Listening..." : "Hold to speak; release to send"), width);
|
||||
pending.slice(-2).forEach((text, i) => image.drawText(font, left, footerTop + step * (i + 1) + 5, text, 220));
|
||||
footer.forEach((text, i) => image.drawText(font, left + width - font.measureText(text), footerY + i * step, text, 120));
|
||||
return image;
|
||||
}
|
||||
handleInput(event: InputEvent): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up": case "swipe-up":
|
||||
this.firstLine = Math.max(0, (this.firstLine ?? this.maxFirstLine) - 3); return;
|
||||
case "scroll-down": case "swipe-down": {
|
||||
const next = (this.firstLine ?? this.maxFirstLine) + 3;
|
||||
this.firstLine = next >= this.maxFirstLine ? null : next; return;
|
||||
}
|
||||
case "long-press":
|
||||
if (!this.conversations.current().session?.isTurnActive()) {
|
||||
if (!shell.isAssistantAvailable()) { shell.showAlert("Set an API key or download the on-phone model in Settings."); return; }
|
||||
void this.draft.start();
|
||||
}
|
||||
return;
|
||||
case "long-press-release": this.draft.release(); return;
|
||||
case "double-click": this.draft.cancel(); shell.yieldFocusToSidebar(); return;
|
||||
}
|
||||
}
|
||||
menuItems(): MenuItem[] {
|
||||
const record = this.conversations.current();
|
||||
const busy = () => this.draft.active || Boolean(record.session?.isTurnActive());
|
||||
const external = assistantBackendSetting.get() === "external";
|
||||
const submenu = (ctx: LayerContext, title: string, items: MenuItem[]) => ctx.stack.push(new WindowMenuLayer(title, items, true));
|
||||
return [
|
||||
...(external ? [{ label: "Sessions and model managed by bridge", disabled: true, onSelect: () => {} }] : []),
|
||||
{ label: "New session", disabled: () => external || busy(), onSelect: (ctx) => {
|
||||
if (this.conversations.create()) ctx.stack.clearToBase();
|
||||
} },
|
||||
{ label: "Switch session", disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Sessions",
|
||||
[...this.conversations.list()].reverse().map((item) => ({
|
||||
label: `${item.id === record.id ? "✓ " : ""}${this.conversations.title(item)}`,
|
||||
onSelect: (ctx) => { if (this.conversations.select(item.id)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Model: ${assistantModelLabel(record.model)}`, disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Model",
|
||||
ASSISTANT_MODEL_VALUES.map((model) => ({
|
||||
label: `${model === record.model ? "✓ " : ""}${assistantModelLabel(model)}`,
|
||||
disabled: () => !this.conversations.available(model, record.reasoning),
|
||||
onSelect: (ctx) => { if (this.conversations.configure(model, record.reasoning)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Reasoning: ${record.reasoning}`, disabled: () => {
|
||||
const llm = resolveAssistantModel(record.model, { anthropic: anthropicApiKeySetting.get(), openai: openAiApiKeySetting.get() });
|
||||
return external || busy() || !llm?.effort;
|
||||
}, onSelect: (ctx) => submenu(ctx, "Reasoning", (["default", "low", "medium", "high"] as ReasoningLevel[]).map((reasoning) => ({
|
||||
label: `${reasoning === record.reasoning ? "✓ " : ""}${reasoning === "default" ? "Model default" : reasoning}`,
|
||||
onSelect: (ctx) => { if (this.conversations.configure(record.model, reasoning)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
...(record.session?.isTurnActive() ? [{ label: "Cancel response", onSelect: (ctx: LayerContext) => {
|
||||
record.session?.cancel(); ctx.stack.clearToBase();
|
||||
} }] : []),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export function createAiChatWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
const layer = new AiChatLayer(shell.getAssistantConversations(), options);
|
||||
let unsubscribe = () => {};
|
||||
const app = createInProcessWindow({
|
||||
...options,
|
||||
appId: "ai-chat", windowId: "ai-chat", title: "AI Chat", iconLetter: "AI", icon: "message-circle", closeable: true,
|
||||
baseLayer: layer, holdToTalk: true,
|
||||
isVoiceCapturing: () => layer.draft.active,
|
||||
onSystemMenuOpened: () => layer.draft.cancel(),
|
||||
onAppMenuOpened: () => layer.draft.cancel(),
|
||||
setScreenOn: (on) => { if (!on) layer.draft.cancel(); },
|
||||
setSurfaceVisible: (visible) => { if (!visible) layer.draft.cancel(); options.setSurfaceVisible(visible); },
|
||||
menuItems: () => layer.menuItems(),
|
||||
receiveTextInput: (text) => { if (!layer.draft.active) layer.send(text); },
|
||||
onClosed: () => { unsubscribe(); layer.draft.cancel(); options.onClosed(); },
|
||||
});
|
||||
unsubscribe = layer.start(app.requestRender);
|
||||
return app;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import type { AppDefinition } from "../app-definition";
|
||||
import { createAiChatWindow } from "./ai-chat-app";
|
||||
|
||||
const aiChatApp: AppDefinition = {
|
||||
appId: "ai-chat",
|
||||
title: "AI Chat",
|
||||
icon: "message-circle",
|
||||
launch: (ctx) => ctx.launchInProcessApp("ai-chat", "window:ai-chat", createAiChatWindow),
|
||||
};
|
||||
export default aiChatApp;
|
||||
@@ -0,0 +1,125 @@
|
||||
import { voiceControlBridge } from "../../native/voice-control";
|
||||
import { voiceActivity } from "../../ui/shell/voice-activity";
|
||||
import type { LayerActions } from "../../ui/layers";
|
||||
|
||||
/** One hold/release cycle. Async startup and trailing cloud finals belong to this cycle only. */
|
||||
export class VoiceDraft {
|
||||
phase: "idle" | "preparing" | "listening" | "finishing" = "idle";
|
||||
text = "";
|
||||
status = "";
|
||||
private committed = "";
|
||||
private live = "";
|
||||
private generation = 0;
|
||||
private started = false;
|
||||
private ownsVoiceActivity = false;
|
||||
private starting = false;
|
||||
private unsubscribe: (() => void)[] = [];
|
||||
private timer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
constructor(
|
||||
private readonly actions: Pick<LayerActions, "startVoiceCapture" | "stopVoiceCapture">,
|
||||
private readonly prepare: () => Promise<boolean>,
|
||||
private readonly changed: () => void,
|
||||
private readonly send: (text: string) => void,
|
||||
) {}
|
||||
|
||||
get active(): boolean { return this.phase !== "idle" || this.starting; }
|
||||
|
||||
async start(): Promise<void> {
|
||||
if (this.active) return;
|
||||
const generation = ++this.generation;
|
||||
this.text = this.committed = this.live = "";
|
||||
this.phase = "preparing";
|
||||
this.status = "Preparing microphone...";
|
||||
this.changed();
|
||||
try {
|
||||
const ready = await this.prepare();
|
||||
if (generation !== this.generation) return;
|
||||
if (!ready) { this.cancel("Microphone unavailable"); return; }
|
||||
this.phase = "listening";
|
||||
this.status = "Listening... release to send";
|
||||
this.ownsVoiceActivity = true;
|
||||
voiceActivity.setActive(true);
|
||||
this.unsubscribe.push(voiceControlBridge.onTranscript((event) => {
|
||||
if (!this.active || generation !== this.generation) return;
|
||||
if (event.isFinal) {
|
||||
const text = event.text.trim();
|
||||
if (text) this.committed = [this.committed, text].filter(Boolean).join(" ");
|
||||
this.live = "";
|
||||
} else this.live = event.text.trim();
|
||||
this.text = [this.committed, this.live].filter(Boolean).join(" ");
|
||||
if (event.isFinal && this.phase === "finishing") this.finish();
|
||||
else this.changed();
|
||||
}));
|
||||
// onStatus immediately replays the previous capture's status.
|
||||
let subscribed = false;
|
||||
this.unsubscribe.push(voiceControlBridge.onStatus(({ status }) => {
|
||||
if (!subscribed) return;
|
||||
if (generation !== this.generation || !this.active) return;
|
||||
if (/ignored|failed|error|denied/i.test(status)) this.cancel(status);
|
||||
}));
|
||||
subscribed = true;
|
||||
if (generation !== this.generation) return;
|
||||
this.starting = true;
|
||||
this.changed();
|
||||
await this.actions.startVoiceCapture();
|
||||
this.starting = false;
|
||||
if (generation !== this.generation) {
|
||||
await this.actions.stopVoiceCapture();
|
||||
this.changed();
|
||||
return;
|
||||
}
|
||||
this.started = true;
|
||||
if (this.isFinishing()) this.stopAndFinalize();
|
||||
} catch (error) {
|
||||
this.starting = false;
|
||||
if (generation === this.generation) this.cancel(`Microphone: ${String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
private isFinishing(): boolean { return this.phase === "finishing"; }
|
||||
|
||||
release(): void {
|
||||
if (this.phase === "preparing") { this.cancel(); return; }
|
||||
if (this.phase !== "listening") return;
|
||||
this.phase = "finishing";
|
||||
this.status = "Finishing transcription...";
|
||||
this.changed();
|
||||
if (!this.starting) this.stopAndFinalize();
|
||||
}
|
||||
|
||||
private stopAndFinalize(): void {
|
||||
// Install fallback first: stop may synchronously deliver the final.
|
||||
this.timer = setTimeout(() => this.finish(), 1500);
|
||||
if (this.started) {
|
||||
this.started = false;
|
||||
void Promise.resolve(this.actions.stopVoiceCapture()).catch((error) => this.cancel(String(error)));
|
||||
}
|
||||
}
|
||||
|
||||
private finish(): void {
|
||||
if (this.phase !== "finishing") return;
|
||||
const text = this.text.trim();
|
||||
this.cancel(text ? "" : "No speech detected");
|
||||
if (text) this.send(text);
|
||||
}
|
||||
|
||||
cancel(status = ""): void {
|
||||
++this.generation;
|
||||
this.phase = "idle";
|
||||
this.status = status;
|
||||
this.text = this.live = this.committed = "";
|
||||
if (this.timer !== null) clearTimeout(this.timer);
|
||||
this.timer = null;
|
||||
for (const unsubscribe of this.unsubscribe.splice(0)) unsubscribe();
|
||||
if (this.started) {
|
||||
this.started = false;
|
||||
void Promise.resolve(this.actions.stopVoiceCapture()).catch(() => {});
|
||||
}
|
||||
if (this.ownsVoiceActivity) {
|
||||
this.ownsVoiceActivity = false;
|
||||
voiceActivity.setActive(false);
|
||||
}
|
||||
this.changed();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { type AppDefinition } from "./app-definition";
|
||||
import aiChatApp from "./ai-chat";
|
||||
import launcherApp from "./launcher";
|
||||
import timerApp from "./timer";
|
||||
import calculatorApp from "./calculator";
|
||||
@@ -7,6 +8,7 @@ import filesApp from "./files";
|
||||
import musicApp from "./music";
|
||||
import nightscoutApp from "./nightscout";
|
||||
import transcribeApp from "./transcribe";
|
||||
import teleprompterApp from "./teleprompter";
|
||||
import microphonesApp from "./microphones";
|
||||
import notificationsApp from "./notifications";
|
||||
import calendarApp from "./calendar";
|
||||
@@ -29,6 +31,7 @@ import settingsApp from "./settings";
|
||||
*/
|
||||
export const ALL_APPS: readonly AppDefinition[] = [
|
||||
launcherApp,
|
||||
aiChatApp,
|
||||
timerApp,
|
||||
calculatorApp,
|
||||
terminalApp,
|
||||
@@ -36,6 +39,7 @@ export const ALL_APPS: readonly AppDefinition[] = [
|
||||
musicApp,
|
||||
nightscoutApp,
|
||||
transcribeApp,
|
||||
teleprompterApp,
|
||||
microphonesApp,
|
||||
notificationsApp,
|
||||
calendarApp,
|
||||
|
||||
@@ -6,18 +6,19 @@
|
||||
* Controls (in play): scroll moves the piece, click rotates, long-press hard
|
||||
* drops, double-click pauses. Watch swipes are spatial: left/right move,
|
||||
* up rotates, down hard-drops. Paused: click resumes, double-click yields
|
||||
* focus, long-press opens the window menu.
|
||||
* focus, tap-then-hold opens the window menu.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import {
|
||||
directionalFallback,
|
||||
@@ -136,12 +137,13 @@ type GamePhase = "playing" | "paused" | "game-over";
|
||||
type BlocksWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
/** Whether this window is the shell's input target (pushed with each message). */
|
||||
focused: boolean;
|
||||
/** Long-press window menu; created on first open. */
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: GamePhase;
|
||||
/** Locked cells; 0 = empty, otherwise the piece's shade byte. */
|
||||
@@ -181,6 +183,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const window: BlocksWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -285,9 +288,9 @@ function playSfx(window: BlocksWindow, steps: Step[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's long-press menu (sound toggle + default entries). */
|
||||
function openWindowMenu(window: BlocksWindow): void {
|
||||
windowMenu(window).open([
|
||||
/** The window's context menu (sound toggle), offered while paused or over; playing keeps long-press for hard drop. */
|
||||
function windowMenuItems(window: BlocksWindow): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: window.soundOn ? "Sound: on" : "Sound: off",
|
||||
onSelect: (ctx) => {
|
||||
@@ -296,13 +299,17 @@ function openWindowMenu(window: BlocksWindow): void {
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
...defaultWindowMenuItems(window.windowId, post),
|
||||
]);
|
||||
];
|
||||
}
|
||||
|
||||
function windowMenu(window: BlocksWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
|
||||
claimsLongPress: () => window.phase === "playing",
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -353,6 +360,10 @@ function handlePlayingInput(window: BlocksWindow, event: InputEvent, frameId: nu
|
||||
case "long-press":
|
||||
hardDrop(window);
|
||||
break;
|
||||
case "short-then-long-press":
|
||||
// No context menu mid-game, so this opens the system menu instead.
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
case "double-click":
|
||||
window.phase = "paused";
|
||||
updateTickTimer(window);
|
||||
@@ -378,8 +389,8 @@ function handleIdleInput(window: BlocksWindow, event: InputEvent, frameId: numbe
|
||||
frameTimings.finishFrame(frameId, "discarded: blocks yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: blocks ignored input");
|
||||
@@ -554,10 +565,7 @@ function updateTickTimer(window: BlocksWindow): void {
|
||||
}
|
||||
|
||||
function paint(window: BlocksWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(paintContent(window));
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: BlocksWindow): GrayImage {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Calculator window wiring: the in-process window, its long-press menu (mode
|
||||
* Calculator window wiring: the in-process window, its context menu (mode
|
||||
* follow-ups, clear, settings), the assistant tools, and the voice/text
|
||||
* plumbing into the layer.
|
||||
*
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
/**
|
||||
* The compass calibration screen: a crosshair the wearer aims at a landmark
|
||||
* of known heading, plus raw/offset/calibrated readouts. Swipe adjusts the
|
||||
* offset by 1°, double-tap returns to the compass.
|
||||
* of known heading, plus raw/offset/declination/calibrated readouts. Swipe
|
||||
* adjusts the offset by 1°, double-tap returns to the compass.
|
||||
*
|
||||
* The landmark's heading must be in the frame the compass is showing: aiming
|
||||
* at a true heading in true mode leaves the wearer-fit offset free of
|
||||
* declination, which is what lets the calibration survive travel.
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
@@ -10,18 +14,16 @@ import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import { screenCenterInViewportX } from "../../ui/shell/geometry";
|
||||
import {
|
||||
formatOffset,
|
||||
getCompassOffset,
|
||||
markCompassCalibrated,
|
||||
normalizeHeading,
|
||||
setCompassOffset,
|
||||
} from "./calibration";
|
||||
import { formatOffset, getCompassOffset, markCompassCalibrated, setCompassOffset } from "./calibration";
|
||||
import { resolveHeading, type NorthReference } from "./heading";
|
||||
|
||||
const INSTRUCTIONS =
|
||||
const INSTRUCTIONS_PREAMBLE =
|
||||
"This compass uses the magnetometer in the right arm of the glasses; depending how it rests on your head, "
|
||||
+ "it may be slightly off. To calibrate, look at a distant object or down a street where you know the true "
|
||||
+ "compass heading, then adjust the calibration offset below";
|
||||
+ "it may be slightly off. To calibrate, look at a distant object or down a street where you know the ";
|
||||
const INSTRUCTIONS_BY_FRAME: Record<NorthReference, string> = {
|
||||
true: "true compass heading (as shown on a map), then adjust the calibration offset below",
|
||||
magnetic: "magnetic compass heading (as read by another compass), then adjust the calibration offset below",
|
||||
};
|
||||
|
||||
/** Margins around the instruction paragraph. */
|
||||
const TEXT_PADDING_X = 16;
|
||||
@@ -40,7 +42,12 @@ export class CompassCalibrationLayer implements Layer {
|
||||
const medium = getDefaultMediumFont();
|
||||
const smallStep = lineStep(small);
|
||||
|
||||
const lines = wrapText(small, INSTRUCTIONS, width - TEXT_PADDING_X * 2);
|
||||
const raw = this.getRawHeading();
|
||||
const resolved = raw === null ? null : resolveHeading(raw);
|
||||
// Before the first reading there is no frame to name yet; the true-mode
|
||||
// wording is the default and the readout will correct it once data arrives.
|
||||
const frame = resolved?.frame ?? "true";
|
||||
const lines = wrapText(small, INSTRUCTIONS_PREAMBLE + INSTRUCTIONS_BY_FRAME[frame], width - TEXT_PADDING_X * 2);
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
image.drawText(small, TEXT_PADDING_X, TEXT_PADDING_TOP + i * smallStep, lines[i]!, 190);
|
||||
}
|
||||
@@ -49,13 +56,20 @@ export class CompassCalibrationLayer implements Layer {
|
||||
// Readouts along the bottom: dim label over a bright value, in thirds.
|
||||
const valueTop = height - medium.lineHeight;
|
||||
const labelTop = valueTop - small.lineHeight;
|
||||
const raw = this.getRawHeading();
|
||||
const offset = getCompassOffset();
|
||||
const columns: Array<[string, string]> = [
|
||||
["Raw", raw === null ? "--°" : `${Math.round(raw)}°`],
|
||||
["Offset", formatOffset(offset)],
|
||||
["Calibrated", raw === null ? "--°" : `${Math.round(normalizeHeading(raw + offset))}°`],
|
||||
["Offset", formatOffset(getCompassOffset())],
|
||||
];
|
||||
// Declination is only part of the sum in true mode, so only show it then;
|
||||
// in magnetic mode it would be a number that changes nothing on screen.
|
||||
if (frame === "true") {
|
||||
const declination = resolved?.declinationDegrees ?? null;
|
||||
columns.push(["Declination", declination === null ? "--°" : formatOffset(Math.round(declination))]);
|
||||
}
|
||||
columns.push([
|
||||
frame === "true" ? "True" : "Magnetic",
|
||||
resolved === null ? "--°" : `${Math.round(resolved.displayDegrees)}°`,
|
||||
]);
|
||||
const columnWidth = width / columns.length;
|
||||
for (let i = 0; i < columns.length; i++) {
|
||||
const [label, value] = columns[i]!;
|
||||
|
||||
+180
-17
@@ -7,6 +7,7 @@ import {
|
||||
addCompassListener,
|
||||
setCompassEnabled,
|
||||
type CompassEvent,
|
||||
type CompassDiagnostics,
|
||||
} from "../../native/compass";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
@@ -20,8 +21,12 @@ import {
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { calibrateHeading, isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
|
||||
import { isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||
import { CompassCalibrationLayer } from "./calibration-layer";
|
||||
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination";
|
||||
import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading";
|
||||
import { compassDebugLines, isCompassDebugEnabled, setCompassDebugEnabled } from "./debug";
|
||||
|
||||
export const COMPASS_WINDOW_ID = "compass";
|
||||
export const COMPASS_SURFACE_ID = "window:compass";
|
||||
@@ -92,19 +97,56 @@ const WALL_LIT = 62;
|
||||
|
||||
class CompassLayer implements Layer {
|
||||
private rawHeading: number | null = null;
|
||||
private diagnostics: CompassDiagnostics | null = null;
|
||||
/** A firmware calibration message, shown until the next heading arrives. */
|
||||
private firmwareStatus: string | null = null;
|
||||
private enabled = false;
|
||||
private removed = false;
|
||||
private timer: ReturnType<typeof setInterval> | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private unsubscribeDeclination: (() => void) | null = null;
|
||||
private requestingPermission = false;
|
||||
private background: { key: string; image: GrayImage } | null = null;
|
||||
|
||||
constructor(private readonly requestRender: () => void) {}
|
||||
|
||||
start(): void {
|
||||
this.unsubscribe = addCompassListener((event) => this.onCompassEvent(event));
|
||||
this.unsubscribeDeclination = onDeclinationChanged(() => this.requestRender());
|
||||
this.timer = setInterval(() => this.reconcile(), RECONCILE_INTERVAL_MS);
|
||||
this.reconcile();
|
||||
this.ensureDeclination();
|
||||
}
|
||||
|
||||
/**
|
||||
* True north needs the phone's location. Ask for it the way Weather does,
|
||||
* on opening, but only while the wearer actually wants true north: a
|
||||
* magnetic-mode compass has no business raising a permission prompt.
|
||||
*/
|
||||
private ensureDeclination(): void {
|
||||
if (getNorthReference() !== "true") return;
|
||||
if (hasLocationPermission()) {
|
||||
refreshDeclination();
|
||||
return;
|
||||
}
|
||||
if (this.requestingPermission) return;
|
||||
this.requestingPermission = true;
|
||||
void ensureLocationPermission().then((granted) => {
|
||||
this.requestingPermission = false;
|
||||
if (granted) refreshDeclination();
|
||||
this.requestRender();
|
||||
});
|
||||
}
|
||||
|
||||
toggleNorthReference(): void {
|
||||
setNorthReference(getNorthReference() === "true" ? "magnetic" : "true");
|
||||
this.ensureDeclination();
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
toggleDebugInfo(): void {
|
||||
setCompassDebugEnabled(!isCompassDebugEnabled());
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
@@ -114,6 +156,8 @@ class CompassLayer implements Layer {
|
||||
this.timer = null;
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.unsubscribeDeclination?.();
|
||||
this.unsubscribeDeclination = null;
|
||||
if (this.enabled) setCompassEnabled(false);
|
||||
this.enabled = false;
|
||||
}
|
||||
@@ -132,8 +176,11 @@ class CompassLayer implements Layer {
|
||||
const visible = shell.isWindowVisible(COMPASS_WINDOW_ID);
|
||||
if (visible === this.enabled) return;
|
||||
this.enabled = visible;
|
||||
this.diagnostics = null;
|
||||
setCompassEnabled(visible);
|
||||
this.firmwareStatus = null;
|
||||
// The stored fix may have aged out while the compass was hidden.
|
||||
if (visible && hasLocationPermission()) refreshDeclination();
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
@@ -141,6 +188,7 @@ class CompassLayer implements Layer {
|
||||
if (this.removed) return;
|
||||
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
|
||||
this.rawHeading = normalizeHeading(event.headingDegrees);
|
||||
this.diagnostics = event.diagnostics ?? null;
|
||||
this.firmwareStatus = null;
|
||||
} else if (event.command === COMPASS_CALIBRATION_STARTED) {
|
||||
this.firmwareStatus = "Calibrating — move the glasses";
|
||||
@@ -154,18 +202,29 @@ class CompassLayer implements Layer {
|
||||
if (this.firmwareStatus !== null) return this.firmwareStatus;
|
||||
if (!this.enabled) return "Compass paused";
|
||||
if (this.rawHeading === null) return "Waiting for compass data…";
|
||||
return isCompassCalibrated() ? "Magnetic heading" : "Uncalibrated - Tap to calibrate";
|
||||
if (!isCompassCalibrated()) return "Uncalibrated - Tap to calibrate";
|
||||
return frameStatusText();
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const small = getDefaultSmallFont();
|
||||
const large = getDefaultLargeFont();
|
||||
const heading = this.rawHeading === null ? null : calibrateHeading(this.rawHeading);
|
||||
const heading = this.rawHeading === null ? null : resolveHeading(this.rawHeading).displayDegrees;
|
||||
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
|
||||
const statusLines = wrapText(small, this.statusText(), width - STACK_GAP * 2);
|
||||
const smallStep = lineStep(small);
|
||||
const debugLines = isCompassDebugEnabled() ? compassDebugLines(this.diagnostics) : [];
|
||||
const debugWidth = Math.max(0, ...debugLines.map((line) => small.measureText(line)));
|
||||
const debugGap = debugLines.length ? 12 : 0;
|
||||
// Keep diagnostics beside the heading even in narrow windows. The rose
|
||||
// stays centred on the optical axis; the combined readout fits the viewport.
|
||||
const headingFont = large.measureText(headingText) + debugGap + debugWidth <= width - EDGE_PAD * 2
|
||||
? large : small;
|
||||
const headingWidth = headingFont.measureText(headingText);
|
||||
const readoutWidth = headingWidth + debugGap + debugWidth;
|
||||
const debugHeight = debugLines.length ? (debugLines.length - 1) * smallStep + small.lineHeight : 0;
|
||||
const readoutHeight = Math.max(headingFont.lineHeight, debugHeight);
|
||||
|
||||
// One column centred on the display's true centre, so the rose sits where
|
||||
// the wearer is looking rather than 32px right of it.
|
||||
@@ -173,7 +232,7 @@ class CompassLayer implements Layer {
|
||||
|
||||
// The rose is sized and placed first — it hangs off the bottom edge — and
|
||||
// the readout then floats in whatever room is left above it.
|
||||
const textHeight = large.lineHeight + 4 + statusLines.length * smallStep;
|
||||
const textHeight = readoutHeight + 4 + statusLines.length * smallStep;
|
||||
const radius = Math.max(
|
||||
24,
|
||||
Math.min(
|
||||
@@ -191,15 +250,30 @@ class CompassLayer implements Layer {
|
||||
const ringTop = cy - (radius * TILT_SQUASH) / (1 + TILT_PERSPECTIVE);
|
||||
|
||||
let y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
||||
image.drawText(large, Math.round(cx - large.measureText(headingText) / 2), y, headingText, heading === null ? 150 : 255);
|
||||
y += large.lineHeight + 4;
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
const clipY = y + textHeight + 6;
|
||||
const backgroundKey = [width, height, cx, cy, radius, clipY, fade].join(",");
|
||||
if (this.background?.key !== backgroundKey) {
|
||||
this.background = {
|
||||
key: backgroundKey,
|
||||
image: createCompassBackground(width, height, cx, cy, radius, clipY, fade),
|
||||
};
|
||||
}
|
||||
const image = this.background.image.clone();
|
||||
drawCompassRose(image, cx, cy, radius, heading);
|
||||
const readoutX = Math.round(Math.max(EDGE_PAD, Math.min(width - EDGE_PAD - readoutWidth, cx - readoutWidth / 2)));
|
||||
image.drawText(headingFont, readoutX, y + Math.floor((readoutHeight - headingFont.lineHeight) / 2),
|
||||
headingText, heading === null ? 150 : 255);
|
||||
for (let i = 0; i < debugLines.length; i++) {
|
||||
image.drawText(small, readoutX + headingWidth + debugGap,
|
||||
y + Math.floor((readoutHeight - debugHeight) / 2) + i * smallStep, debugLines[i]!, 175);
|
||||
}
|
||||
y += readoutHeight + 4;
|
||||
for (const line of statusLines) {
|
||||
image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125);
|
||||
y += smallStep;
|
||||
}
|
||||
|
||||
drawPlaneGrid(image, cx, cy, radius, y + 6);
|
||||
drawCompassRose(image, cx, cy, radius, heading);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -231,6 +305,22 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
|
||||
layer.openCalibration(ctx);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: `North: ${northReferenceName(getNorthReference())}`,
|
||||
description: "True north is what maps use. Magnetic north matches a handheld compass; it needs no location.",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
layer.toggleNorthReference();
|
||||
},
|
||||
},
|
||||
{
|
||||
label: `Debug information: ${isCompassDebugEnabled() ? "On" : "Off"}`,
|
||||
description: "Show magnetic accuracy (0–3), anomaly flags (0–2), and orientation source beside the heading.",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
layer.toggleDebugInfo();
|
||||
},
|
||||
},
|
||||
],
|
||||
baseLayer: new YieldAtRootLayer(layer),
|
||||
submitFrame: options.submitFrame,
|
||||
@@ -246,6 +336,27 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
|
||||
return app;
|
||||
}
|
||||
|
||||
export function northReferenceName(reference: NorthReference): string {
|
||||
return reference === "true" ? "True" : "Magnetic";
|
||||
}
|
||||
|
||||
/**
|
||||
* Name the frame the readout is in. When the wearer wants true north but the
|
||||
* phone can't supply declination, say that the reading is magnetic and why,
|
||||
* rather than quietly showing a magnetic heading under a "true" label.
|
||||
*/
|
||||
function frameStatusText(): string {
|
||||
if (getNorthReference() === "magnetic") return "Magnetic heading";
|
||||
switch (getDeclinationAvailability()) {
|
||||
case "available":
|
||||
return "True heading";
|
||||
case "no-permission":
|
||||
return "Magnetic heading - location permission needed for true north";
|
||||
case "no-fix":
|
||||
return "Magnetic heading - waiting for location";
|
||||
}
|
||||
}
|
||||
|
||||
function cardinalDirection(heading: number): string {
|
||||
const names = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
||||
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
|
||||
@@ -272,15 +383,66 @@ function projectRose(cx: number, cy: number, scale: number, radius: number, angl
|
||||
};
|
||||
}
|
||||
|
||||
/** Draw the tilted rose, with its brightest point aimed at magnetic north. */
|
||||
/**
|
||||
* Bounds of every orientation of the rotating points. The longest tip sweeps
|
||||
* a circle in the disc's plane; perspective projects it to an offset ellipse.
|
||||
* Round outward so all rasterized triangle pixels fit inside the rectangle.
|
||||
*/
|
||||
function rotatingRoseBounds(cx: number, cy: number, radius: number): {
|
||||
x: number; y: number; width: number; height: number;
|
||||
} {
|
||||
const perspective = TILT_PERSPECTIVE * CARDINAL_TIP;
|
||||
const halfWidth = radius * CARDINAL_TIP / Math.sqrt(1 - perspective * perspective);
|
||||
const depth = radius * CARDINAL_TIP * TILT_SQUASH;
|
||||
const x = Math.floor(cx - halfWidth);
|
||||
const y = Math.floor(cy - depth / (1 + perspective));
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
width: Math.ceil(cx + halfWidth) - x,
|
||||
height: Math.ceil(cy + depth / (1 - perspective)) - y,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the fixed pixels inside the rotating rose's dirty rectangle as one
|
||||
* immutable texture. The wire planner can omit them from each raster delta
|
||||
* and restore them with a cached image draw. Outside that rectangle the
|
||||
* background stays raster, since heading updates do not change it.
|
||||
*
|
||||
* The texture uses transparent zero for the empty disc interior, so it can
|
||||
* overlay the rotating points. Its only ink over the points is the fixed
|
||||
* heading tick, which belongs on top of them.
|
||||
*/
|
||||
function createCompassBackground(
|
||||
width: number, height: number, cx: number, cy: number, radius: number, clipY: number, fade: number,
|
||||
): GrayImage {
|
||||
const image = new GrayImage(width, height, 0);
|
||||
drawPlaneGrid(image, cx, cy, radius, clipY);
|
||||
drawDiscWall(image, cx, cy, radius, fade);
|
||||
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
||||
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
||||
|
||||
const bounds = rotatingRoseBounds(cx, cy, radius);
|
||||
const left = Math.max(0, bounds.x);
|
||||
const top = Math.max(0, bounds.y);
|
||||
const right = Math.min(width, bounds.x + bounds.width);
|
||||
const bottom = Math.min(height, bounds.y + bounds.height);
|
||||
if (right > left && bottom > top) {
|
||||
const texture = new GrayImage(right - left, bottom - top, 0);
|
||||
texture.bitBlt(image, 0, 0, { sx: left, sy: top, width: texture.width, height: texture.height });
|
||||
image.fillRect(left, top, texture.width, texture.height, 0);
|
||||
image.drawImage(texture, left, top);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Draw only the rotating points, with the brightest one aimed at north. */
|
||||
function drawCompassRose(image: GrayImage, cx: number, cy: number, radius: number, heading: number | null): void {
|
||||
// Without a reading there is nothing to aim, so show the rose in its resting
|
||||
// orientation, dimmed, rather than an empty ring.
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
const bearing = heading ?? 0;
|
||||
drawDiscWall(image, cx, cy, radius, fade);
|
||||
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
||||
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
||||
// Short points first, so the long ones sit on top of them at the waist. Only
|
||||
// north is drawn at full brightness: the other seven are there to make the
|
||||
// rose read as a rose, and competing with north would defeat the point.
|
||||
@@ -411,14 +573,15 @@ function drawTiltedRing(image: GrayImage, cx: number, cy: number, radius: number
|
||||
}
|
||||
}
|
||||
|
||||
/** A fixed mark at the top of the ring: the heading the wearer is facing. */
|
||||
/** A fixed mark just inside the top rim: the heading the wearer is facing. */
|
||||
function drawHeadingTick(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const top = projectRose(cx, cy, radius, 1, 0);
|
||||
const tickTop = top.y + 1;
|
||||
fillTriangle(
|
||||
image,
|
||||
{ x: cx, y: top.y - 1 },
|
||||
{ x: cx - TICK_HALF_WIDTH, y: top.y - TICK_HEIGHT },
|
||||
{ x: cx + TICK_HALF_WIDTH, y: top.y - TICK_HEIGHT },
|
||||
{ x: cx, y: tickTop + TICK_HEIGHT - 1 },
|
||||
{ x: cx - TICK_HALF_WIDTH, y: tickTop },
|
||||
{ x: cx + TICK_HALF_WIDTH, y: tickTop },
|
||||
value,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { CompassDiagnostics } from "../../native/compass";
|
||||
import { getBooleanSetting, setBooleanSetting } from "../../native/settings-store";
|
||||
|
||||
const DEBUG_KEY = "compass.debugInfo";
|
||||
|
||||
export function isCompassDebugEnabled(): boolean {
|
||||
return getBooleanSetting(DEBUG_KEY, false);
|
||||
}
|
||||
|
||||
export function setCompassDebugEnabled(enabled: boolean): void {
|
||||
setBooleanSetting(DEBUG_KEY, enabled);
|
||||
}
|
||||
|
||||
export function compassDebugLines(diagnostics: CompassDiagnostics | null): string[] {
|
||||
if (!diagnostics) return ["Diagnostics", "unavailable"];
|
||||
if (!(diagnostics.flags & 0x80)) return ["Sample data", "unavailable"];
|
||||
const accuracy = diagnostics.magneticAccuracy < 0 ? "--" : `${diagnostics.magneticAccuracy}/3`;
|
||||
const anomalies = diagnostics.magneticAnomalies < 0 ? "--" : String(diagnostics.magneticAnomalies);
|
||||
const source = ["--", "GRV", "GMRV", "RV"][diagnostics.orientationSource] ?? "--";
|
||||
return [`Mag: ${accuracy}`, `Anom: ${anomalies}`, `Src: ${source}`];
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Local magnetic declination for the compass, so the app can show true north.
|
||||
*
|
||||
* Declination only varies over tens of kilometres, so the location it needs is
|
||||
* whatever the phone can cheaply produce: the same one-shot coarse lookup the
|
||||
* Weather app uses, refreshed no more than daily. The last fix is persisted so
|
||||
* a fresh start has a usable declination before location comes back, and the
|
||||
* model is re-evaluated at the current time when it does.
|
||||
*/
|
||||
import { hasLocationPermission } from "../../g2/android-permissions";
|
||||
import { magneticDeclinationDegrees } from "../../native/geomagnetic";
|
||||
import { getCurrentLocation } from "../../native/location";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
|
||||
const LATITUDE_KEY = "compass.declination.latitude";
|
||||
const LONGITUDE_KEY = "compass.declination.longitude";
|
||||
const LOCATED_AT_KEY = "compass.declination.locatedAtMs";
|
||||
|
||||
/** A stored fix older than this is refreshed when the compass next runs. */
|
||||
const FIX_MAX_AGE_MS = 24 * 60 * 60 * 1000;
|
||||
/** Back-off between failed location attempts, so a denied or dark phone isn't polled. */
|
||||
const RETRY_INTERVAL_MS = 10 * 60 * 1000;
|
||||
|
||||
export type DeclinationAvailability = "available" | "no-permission" | "no-fix";
|
||||
|
||||
type StoredFix = { latitude: number; longitude: number; locatedAtMs: number };
|
||||
|
||||
let declination: number | null = null;
|
||||
let fix: StoredFix | null = null;
|
||||
let loaded = false;
|
||||
let refreshing = false;
|
||||
let lastAttemptMs = 0;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function loadStoredFix(): void {
|
||||
if (loaded) return;
|
||||
loaded = true;
|
||||
const latitude = parseFloat(getStringSetting(LATITUDE_KEY, ""));
|
||||
const longitude = parseFloat(getStringSetting(LONGITUDE_KEY, ""));
|
||||
const locatedAtMs = parseInt(getStringSetting(LOCATED_AT_KEY, ""), 10);
|
||||
if (Number.isFinite(latitude) && Number.isFinite(longitude) && Number.isFinite(locatedAtMs)) {
|
||||
fix = { latitude, longitude, locatedAtMs };
|
||||
declination = magneticDeclinationDegrees(latitude, longitude);
|
||||
}
|
||||
}
|
||||
|
||||
/** Declination in degrees (east positive), or null when no location is known. */
|
||||
export function getDeclinationDegrees(): number | null {
|
||||
loadStoredFix();
|
||||
return declination;
|
||||
}
|
||||
|
||||
/** Why declination is or isn't available, for the compass status line. */
|
||||
export function getDeclinationAvailability(): DeclinationAvailability {
|
||||
if (getDeclinationDegrees() !== null) return "available";
|
||||
return hasLocationPermission() ? "no-fix" : "no-permission";
|
||||
}
|
||||
|
||||
export function onDeclinationChanged(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a location and recompute declination if the stored fix is stale or
|
||||
* missing. Safe to call on every compass start; it is a no-op while a lookup
|
||||
* is in flight, while the fix is fresh, or shortly after a failed attempt.
|
||||
* Never prompts for permission: callers that want to ask do that themselves.
|
||||
*/
|
||||
export function refreshDeclination(): void {
|
||||
loadStoredFix();
|
||||
if (refreshing || !hasLocationPermission()) return;
|
||||
const now = Date.now();
|
||||
if (fix !== null && now - fix.locatedAtMs < FIX_MAX_AGE_MS) return;
|
||||
if (now - lastAttemptMs < RETRY_INTERVAL_MS) return;
|
||||
lastAttemptMs = now;
|
||||
refreshing = true;
|
||||
getCurrentLocation()
|
||||
.then((location) => {
|
||||
const value = magneticDeclinationDegrees(location.latitude, location.longitude);
|
||||
if (value === null) return;
|
||||
fix = { latitude: location.latitude, longitude: location.longitude, locatedAtMs: now };
|
||||
declination = value;
|
||||
setStringSetting(LATITUDE_KEY, String(location.latitude));
|
||||
setStringSetting(LONGITUDE_KEY, String(location.longitude));
|
||||
setStringSetting(LOCATED_AT_KEY, String(now));
|
||||
for (const listener of listeners) listener();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn(`Compass declination location lookup failed: ${error}`);
|
||||
})
|
||||
.finally(() => {
|
||||
refreshing = false;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* The compass heading model: raw magnetometer reading → wearer-calibrated
|
||||
* magnetic heading → true heading, and which of the last two the wearer has
|
||||
* asked to see.
|
||||
*
|
||||
* The wearer-fit offset and declination are kept as separate terms on purpose.
|
||||
* The offset changes when the glasses are re-seated but not when the wearer
|
||||
* travels; declination is the reverse. One combined hand-tuned number would go
|
||||
* quietly wrong after a long trip.
|
||||
*/
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { calibrateHeading, normalizeHeading } from "./calibration";
|
||||
import { getDeclinationDegrees } from "./declination";
|
||||
|
||||
const NORTH_REFERENCE_KEY = "compass.northReference";
|
||||
|
||||
/** Which north the compass reads relative to. True is the default: it is what maps use. */
|
||||
export type NorthReference = "true" | "magnetic";
|
||||
|
||||
export type ResolvedHeading = {
|
||||
/** Raw reading corrected by the wearer-fit offset. */
|
||||
magneticDegrees: number;
|
||||
/** Local declination (east positive), or null when no location is known. */
|
||||
declinationDegrees: number | null;
|
||||
/** Magnetic heading plus declination, or null when declination is unknown. */
|
||||
trueDegrees: number | null;
|
||||
/** The heading to show, in whichever frame `frame` names. */
|
||||
displayDegrees: number;
|
||||
/** The frame actually shown: true if requested and available, else magnetic. */
|
||||
frame: NorthReference;
|
||||
};
|
||||
|
||||
export function getNorthReference(): NorthReference {
|
||||
return getStringSetting(NORTH_REFERENCE_KEY, "true") === "magnetic" ? "magnetic" : "true";
|
||||
}
|
||||
|
||||
export function setNorthReference(reference: NorthReference): void {
|
||||
setStringSetting(NORTH_REFERENCE_KEY, reference);
|
||||
}
|
||||
|
||||
export function resolveHeading(rawDegrees: number): ResolvedHeading {
|
||||
const magneticDegrees = calibrateHeading(rawDegrees);
|
||||
const declinationDegrees = getDeclinationDegrees();
|
||||
const trueDegrees = declinationDegrees === null ? null : normalizeHeading(magneticDegrees + declinationDegrees);
|
||||
const wantTrue = getNorthReference() === "true";
|
||||
const frame: NorthReference = wantTrue && trueDegrees !== null ? "true" : "magnetic";
|
||||
return {
|
||||
magneticDegrees,
|
||||
declinationDegrees,
|
||||
trueDegrees,
|
||||
displayDegrees: frame === "true" ? trueDegrees! : magneticDegrees,
|
||||
frame,
|
||||
};
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { ScreenTestLayer } from "./screen-test";
|
||||
import { BuzzerDemoLayer } from "./buzzer-demo";
|
||||
import { AccelerometerDemoLayer } from "./accelerometer-demo";
|
||||
import { BandwidthBenchmarkLayer } from "./bandwidth-benchmark";
|
||||
import { LightSensorDemoLayer } from "./light-sensor-demo";
|
||||
import { ResourceUsageLayer } from "./resource-usage";
|
||||
import { LoadAppFromQrLayer, LoadAppFromUrlLayer } from "./load-app";
|
||||
import { type AppContext } from "../app-definition";
|
||||
@@ -68,6 +69,12 @@ function debugTestsMenu(): MenuLayer {
|
||||
ctx.stack.push(new AccelerometerDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Light sensor",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new LightSensorDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "BLE bandwidth",
|
||||
onSelect: (ctx) => {
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import {
|
||||
addAmbientLightListener,
|
||||
queryAmbientLight,
|
||||
setAmbientLightPolling,
|
||||
type AmbientLightReport,
|
||||
} from "../../native/ambient-light";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
|
||||
// Passive poll period requested from the firmware (it clamps to 100..5000 ms).
|
||||
const POLL_INTERVAL_MS = 250;
|
||||
// Report every poll (min delta 0) so the trace shows the raw reading; the
|
||||
// heartbeat only matters if the firmware ever starts filtering.
|
||||
const POLL_MIN_DELTA = 0;
|
||||
const POLL_HEARTBEAT_MS = 1_000;
|
||||
// How often to reconcile the polling on/off state with window visibility.
|
||||
const RECONCILE_INTERVAL_MS = 400;
|
||||
// Length of the on-screen trace.
|
||||
const HISTORY_MS = 30_000;
|
||||
const MAX_SAMPLES = Math.ceil(HISTORY_MS / 100) + 2;
|
||||
|
||||
type Sample = { atMs: number; value: number; peak: number };
|
||||
|
||||
/**
|
||||
* Light sensor demo: turns CFW passive light-sensor polling (mode 16) on while
|
||||
* this page is visible and off otherwise, and shows the latest OPT3001
|
||||
* reading, the stock driver's peak/target, and a 30 s trace.
|
||||
*
|
||||
* While passive polling is on the stock auto-brightness adjuster does not step
|
||||
* the panel, so the level shown here stays wherever it was set. Leaving the
|
||||
* page hands the sensor back to the stock firmware.
|
||||
*/
|
||||
export class LightSensorDemoLayer implements Layer {
|
||||
private report: AmbientLightReport | null = null;
|
||||
private readonly samples: Sample[] = [];
|
||||
private reportCount = 0;
|
||||
private enabled = false;
|
||||
private removed = false;
|
||||
private reconcileTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
|
||||
constructor(
|
||||
private readonly windowId: string,
|
||||
private readonly requestRender: () => void,
|
||||
) {
|
||||
this.start();
|
||||
}
|
||||
|
||||
private start(): void {
|
||||
this.unsubscribe = addAmbientLightListener((report) => {
|
||||
if (this.removed) return;
|
||||
this.report = report;
|
||||
this.reportCount++;
|
||||
const atMs = Date.now();
|
||||
if (report.readOk || !report.passive) {
|
||||
this.samples.push({ atMs, value: report.alsValue, peak: report.peak });
|
||||
const cutoff = atMs - HISTORY_MS;
|
||||
while (this.samples.length > 1 && this.samples[0]!.atMs < cutoff) this.samples.shift();
|
||||
while (this.samples.length > MAX_SAMPLES) this.samples.shift();
|
||||
}
|
||||
this.requestRender();
|
||||
});
|
||||
// Show whatever the driver currently holds before polling has started.
|
||||
queryAmbientLight();
|
||||
this.reconcileTimer = setInterval(() => this.reconcile(), RECONCILE_INTERVAL_MS);
|
||||
this.reconcile();
|
||||
}
|
||||
|
||||
/** Match the polling on/off state to whether this page is currently visible. */
|
||||
private reconcile(): void {
|
||||
if (this.removed) return;
|
||||
const visible = shell.isWindowVisible(this.windowId);
|
||||
if (visible === this.enabled) return;
|
||||
this.enabled = visible;
|
||||
setAmbientLightPolling(visible, {
|
||||
intervalMs: POLL_INTERVAL_MS,
|
||||
minDelta: POLL_MIN_DELTA,
|
||||
heartbeatMs: POLL_HEARTBEAT_MS,
|
||||
bindToLease: true,
|
||||
});
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.removed = true;
|
||||
if (this.reconcileTimer !== null) {
|
||||
clearInterval(this.reconcileTimer);
|
||||
this.reconcileTimer = null;
|
||||
}
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
if (this.enabled) {
|
||||
setAmbientLightPolling(false);
|
||||
this.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const small = getDefaultSmallFont();
|
||||
const medium = getDefaultMediumFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
|
||||
image.drawText(small, 20, 8, "Light sensor", 220);
|
||||
const status = this.enabled ? "polling" : "paused";
|
||||
image.drawText(small, width - 20 - small.measureText(status), 8, status, 150);
|
||||
|
||||
const report = this.report;
|
||||
if (!report) {
|
||||
const message = this.enabled ? "Waiting for a report…" : "Paused (page not visible).";
|
||||
image.drawText(small, 22, 44, message, 190);
|
||||
image.drawText(small, 22, 66, "Needs custom firmware with the als16", 120);
|
||||
image.drawText(small, 22, 82, "extension (EVENCFW/18 or newer).", 120);
|
||||
image.drawText(small, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
// Headline reading. The stock driver's unit is roughly a tenth of a lux.
|
||||
const lux = (report.alsValue / 10).toFixed(1);
|
||||
image.drawText(medium, 22, 30, `${lux} lux`, 245);
|
||||
const peakText = `peak ${(report.peak / 10).toFixed(1)}`;
|
||||
image.drawText(small, 22 + medium.measureText(`${lux} lux`) + 14, 36, peakText, 170);
|
||||
|
||||
const flags: string[] = [];
|
||||
if (!report.opened) flags.push("sensor closed");
|
||||
if (report.passive) flags.push("passive");
|
||||
if (report.autoBrightness) flags.push("auto on");
|
||||
if (report.passive && !report.readOk) flags.push("read failed");
|
||||
image.drawText(small, 22, 58, flags.join(" ") || "stock polling", 150);
|
||||
|
||||
image.drawText(
|
||||
small,
|
||||
22,
|
||||
76,
|
||||
`brightness ${report.brightness} stock target ${report.stockTarget} gear ${report.gear} scale ${(report.scaleQ10 / 1024).toFixed(2)}`,
|
||||
170,
|
||||
);
|
||||
|
||||
// 30 s trace of the reading (bright) and the driver's 5-sample peak (dim).
|
||||
// The axis label sits between the stats line and the chart, so the chart
|
||||
// starts a full text line lower than the stats.
|
||||
const chartTop = 76 + small.lineHeight * 2 + 4;
|
||||
const bounds = { x: 20, y: chartTop, width: width - 40, height: height - chartTop - 34 };
|
||||
const maxValue = Math.max(...this.samples.map((sample) => Math.max(sample.value, sample.peak)), 10);
|
||||
const scale = roundUpScale(maxValue * 1.08);
|
||||
image.drawText(small, bounds.x, bounds.y - small.lineHeight - 2, `0–${(scale / 10).toFixed(0)} lux, last 30 s`, 120);
|
||||
drawTrace(image, this.samples, Date.now(), bounds, scale);
|
||||
|
||||
image.drawText(small, 20, height - 16, `reports: ${this.reportCount}`, 110);
|
||||
const back = `${GESTURE_DOUBLE_CLICK} back`;
|
||||
image.drawText(small, width - 20 - small.measureText(back), height - 16, back, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Round a trace ceiling up to 1/2/5 × 10^n so the axis label stays tidy. */
|
||||
function roundUpScale(value: number): number {
|
||||
const magnitude = 10 ** Math.floor(Math.log10(Math.max(value, 1)));
|
||||
for (const step of [1, 2, 5, 10]) {
|
||||
if (value <= step * magnitude) return step * magnitude;
|
||||
}
|
||||
return 10 * magnitude;
|
||||
}
|
||||
|
||||
function drawTrace(
|
||||
image: GrayImage,
|
||||
samples: readonly Sample[],
|
||||
nowMs: number,
|
||||
bounds: { x: number; y: number; width: number; height: number },
|
||||
scale: number,
|
||||
): void {
|
||||
image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 45);
|
||||
const startMs = nowMs - HISTORY_MS;
|
||||
const series: Array<[(sample: Sample) => number, number]> = [
|
||||
[(sample) => sample.peak, 90],
|
||||
[(sample) => sample.value, 235],
|
||||
];
|
||||
for (const [value, shade] of series) {
|
||||
let previous: { x: number; y: number } | null = null;
|
||||
for (const sample of samples) {
|
||||
if (sample.atMs < startMs) continue;
|
||||
const x = bounds.x + Math.round(clamp((sample.atMs - startMs) / HISTORY_MS, 0, 1) * (bounds.width - 1));
|
||||
const y = bounds.y + bounds.height - 1 - Math.round(clamp(value(sample) / scale, 0, 1) * (bounds.height - 1));
|
||||
if (previous) image.drawLine(previous.x, previous.y, x, y, shade);
|
||||
else image.setPixel(x, y, shade);
|
||||
previous = { x, y };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,46 @@
|
||||
* running EvenHub apps, mirroring the IMU router. Only the foreground app
|
||||
* receives headings, so a backgrounded app can't keep the sensor running.
|
||||
*
|
||||
* The compass is NOT pre-calibrated — the magnetometer is in the right arm,
|
||||
* which rests on the head with a wearer-dependent curl (see compass-app memory);
|
||||
* headings are relative, not true north.
|
||||
* The raw reading is NOT pre-calibrated — the magnetometer is in the right
|
||||
* arm, which rests on the head with a wearer-dependent curl (see compass-app
|
||||
* memory). Each delivery also carries the Compass app's wearer-calibrated
|
||||
* magnetic heading and, when the phone has a location, declination and the
|
||||
* true heading, so apps needn't redo that work.
|
||||
*/
|
||||
import { addCompassListener, setCompassEnabled, COMPASS_CHANGED, type CompassEvent } from "../../native/compass";
|
||||
import { isCompassCalibrated } from "../compass/calibration";
|
||||
import { refreshDeclination } from "../compass/declination";
|
||||
import { resolveHeading } from "../compass/heading";
|
||||
|
||||
/** Wire shape of the `compass` extension event (faceclaw-extensions `CompassReading`). */
|
||||
export type EvenHubCompassReading = {
|
||||
headingDegrees: number;
|
||||
magneticHeadingDegrees: number;
|
||||
wearerCalibrated: boolean;
|
||||
declinationDegrees?: number;
|
||||
trueHeadingDegrees?: number;
|
||||
};
|
||||
|
||||
export type EvenHubCompassClient = {
|
||||
readonly windowId: string;
|
||||
isForeground(): boolean;
|
||||
deliverCompass(headingDegrees: number): void;
|
||||
deliverCompass(reading: EvenHubCompassReading): void;
|
||||
};
|
||||
|
||||
export function buildCompassReading(rawDegrees: number): EvenHubCompassReading {
|
||||
const resolved = resolveHeading(rawDegrees);
|
||||
const reading: EvenHubCompassReading = {
|
||||
headingDegrees: rawDegrees,
|
||||
magneticHeadingDegrees: resolved.magneticDegrees,
|
||||
wearerCalibrated: isCompassCalibrated(),
|
||||
};
|
||||
if (resolved.declinationDegrees !== null && resolved.trueDegrees !== null) {
|
||||
reading.declinationDegrees = resolved.declinationDegrees;
|
||||
reading.trueHeadingDegrees = resolved.trueDegrees;
|
||||
}
|
||||
return reading;
|
||||
}
|
||||
|
||||
class EvenHubCompassRouter {
|
||||
private readonly requesting = new Set<EvenHubCompassClient>();
|
||||
private active: EvenHubCompassClient | null = null;
|
||||
@@ -47,13 +75,16 @@ class EvenHubCompassRouter {
|
||||
this.unsubscribe = addCompassListener((event: CompassEvent) => {
|
||||
// Only heading updates carry a real heading; calibration events use -1.
|
||||
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
|
||||
this.active?.deliverCompass(event.headingDegrees);
|
||||
this.active?.deliverCompass(buildCompassReading(event.headingDegrees));
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!this.enabled) {
|
||||
setCompassEnabled(true);
|
||||
this.enabled = true;
|
||||
// Uses location only if the wearer already granted it; an app turning
|
||||
// on the compass is not a reason to raise a system permission prompt.
|
||||
refreshDeclination();
|
||||
}
|
||||
} else {
|
||||
if (this.enabled) {
|
||||
|
||||
@@ -26,9 +26,23 @@ export const EVENHUB_SCREEN_HEIGHT = 288;
|
||||
const TEXT_WHITE = 255;
|
||||
const LIST_ROW_PADDING = 4;
|
||||
|
||||
/**
|
||||
* SDK 0.0.14 textColor levels 0..4 as grey values. Level 4 is the default and
|
||||
* the only one apps got before 0.0.14, so it must stay exactly TEXT_WHITE.
|
||||
* Level 0 is the firmware's "fully dim" step, which over the black page
|
||||
* background means the text is effectively not shown.
|
||||
*/
|
||||
const TEXT_BRIGHTNESS_GREYS = [0, 64, 128, 191, 255];
|
||||
|
||||
function textGrey(level: number | undefined): number {
|
||||
return level === undefined ? TEXT_WHITE : (TEXT_BRIGHTNESS_GREYS[level] ?? TEXT_WHITE);
|
||||
}
|
||||
|
||||
/** Chars the font lacks are skipped silently on stock (no tofu); EvenHubFont
|
||||
* skips unknown glyphs, matching that. */
|
||||
function paintTextContainer(image: GrayImage, container: EvenHubTextContainer): void {
|
||||
// Only the text dims: textColor is a text-brightness level, and stock draws
|
||||
// the container's border at full brightness regardless.
|
||||
paintBorder(image, container);
|
||||
const inset = container.borderWidth + container.paddingLength;
|
||||
EvenHubFont.get().drawTextWrapped(
|
||||
@@ -37,7 +51,7 @@ function paintTextContainer(image: GrayImage, container: EvenHubTextContainer):
|
||||
container.y + inset,
|
||||
Math.max(1, container.width - 2 * inset),
|
||||
container.content,
|
||||
TEXT_WHITE,
|
||||
textGrey(container.textColor),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ export type EvenHubTextContainer = {
|
||||
/** Extended-layout: inherit content from a same-named container on replace. */
|
||||
preserve: boolean;
|
||||
content: string;
|
||||
/** SDK 0.0.14 textColor: brightness level 0..4; undefined means the device default (4). */
|
||||
textColor: number | undefined;
|
||||
};
|
||||
|
||||
export type EvenHubImageContainer = {
|
||||
@@ -63,11 +65,31 @@ export type EvenHubListContainer = {
|
||||
|
||||
export type EvenHubContainer = EvenHubTextContainer | EvenHubImageContainer | EvenHubListContainer;
|
||||
|
||||
/** One entry of the OS contextual menu an app registers via `menuObject`. */
|
||||
export type EvenHubMenuItem = {
|
||||
itemName: string;
|
||||
/** Non-zero uint32, unique within the menu; echoed back in menuItemClickEvent. */
|
||||
itemID: number;
|
||||
};
|
||||
|
||||
export type EvenHubPage = {
|
||||
/** In declaration order (lists, images, texts); z-sorted at paint time. */
|
||||
containers: EvenHubContainer[];
|
||||
/**
|
||||
* The page's contextual-menu entries (SDK 0.0.14 `menuObject`). Empty when
|
||||
* the app declared none, which on rebuild is how a menu is cleared.
|
||||
*/
|
||||
menuItems: EvenHubMenuItem[];
|
||||
};
|
||||
|
||||
/** Firmware brightness levels for text; 4 is the device default. */
|
||||
const MIN_TEXT_BRIGHTNESS = 0;
|
||||
const MAX_TEXT_BRIGHTNESS = 4;
|
||||
|
||||
/** Firmware limits on a contextual menu, enforced app-side by the SDK too. */
|
||||
const MENU_MAX_ITEMS = 10;
|
||||
const MENU_NAME_MAX_BYTES = 32;
|
||||
|
||||
function normalizeKey(key: string): string {
|
||||
return key.replace(/_/g, "").toLowerCase();
|
||||
}
|
||||
@@ -123,9 +145,62 @@ function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer
|
||||
zOrderIndex: readOptionalNumber(json, "zOrderIndex"),
|
||||
preserve: readNumber(json, "preserve", 0) !== 0,
|
||||
content: readString(json, "content", ""),
|
||||
textColor: readTextBrightness(json),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A text container's brightness level, or undefined for the device default.
|
||||
* Out-of-range values are dropped rather than clamped: the SDK rejects the
|
||||
* whole call for them, so an app that sends one is already misbehaving and
|
||||
* the default level is the least surprising thing to draw.
|
||||
*/
|
||||
export function readTextBrightness(json: Record<string, unknown>): number | undefined {
|
||||
const value = readOptionalNumber(json, "textColor");
|
||||
if (value === undefined || !Number.isInteger(value)) return undefined;
|
||||
if (value < MIN_TEXT_BRIGHTNESS || value > MAX_TEXT_BRIGHTNESS) return undefined;
|
||||
return value;
|
||||
}
|
||||
|
||||
/** The raw `menuObject.menuItems` array as sent, before any rule is applied. */
|
||||
export function declaredMenuItems(data: Record<string, unknown>): unknown[] {
|
||||
const raw = pickLoose(asRecord(pickLoose(data, "menuObject")), "menuItems");
|
||||
return Array.isArray(raw) ? raw : [];
|
||||
}
|
||||
|
||||
function utf8ByteLength(value: string): number {
|
||||
let bytes = 0;
|
||||
for (const char of value) {
|
||||
const code = char.codePointAt(0) ?? 0;
|
||||
bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse `menuObject.menuItems`. The SDK validates these app-side and refuses
|
||||
* to call the host at all on a violation, so anything invalid arriving here
|
||||
* came from an app bypassing the SDK; drop the offending entries (and any
|
||||
* past the tenth) rather than rejecting the whole page, which would leave the
|
||||
* app with no UI at all.
|
||||
*/
|
||||
export function parseMenuItems(data: Record<string, unknown>): EvenHubMenuItem[] {
|
||||
const items: EvenHubMenuItem[] = [];
|
||||
const seen = new Set<number>();
|
||||
for (const entry of declaredMenuItems(data)) {
|
||||
if (items.length >= MENU_MAX_ITEMS) break;
|
||||
const json = asRecord(entry);
|
||||
const itemID = readNumber(json, "itemID", 0);
|
||||
if (!Number.isInteger(itemID) || itemID <= 0 || itemID > 0xffffffff) continue;
|
||||
if (seen.has(itemID)) continue;
|
||||
const itemName = readString(json, "itemName", "");
|
||||
if (utf8ByteLength(itemName) > MENU_NAME_MAX_BYTES) continue;
|
||||
seen.add(itemID);
|
||||
items.push({ itemName, itemID });
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
function parseImageContainer(json: Record<string, unknown>): EvenHubImageContainer {
|
||||
return {
|
||||
kind: "image",
|
||||
@@ -173,6 +248,9 @@ function parseListContainer(json: Record<string, unknown>): EvenHubListContainer
|
||||
* page. Containers keep declaration order within and across the three arrays
|
||||
* (lists, then images, then texts — texts last so info panels and capture
|
||||
* containers sit above image tiles when no zOrderIndex is given).
|
||||
*
|
||||
* The whole page is replaced on rebuild, which is also what gives an omitted
|
||||
* menuObject its documented meaning: the previous contextual menu goes away.
|
||||
*/
|
||||
export function parsePage(data: Record<string, unknown>): EvenHubPage {
|
||||
const containers: EvenHubContainer[] = [];
|
||||
@@ -188,7 +266,7 @@ export function parsePage(data: Record<string, unknown>): EvenHubPage {
|
||||
if (Array.isArray(texts)) {
|
||||
for (const item of texts) containers.push(parseTextContainer(asRecord(item)));
|
||||
}
|
||||
return { containers };
|
||||
return { containers, menuItems: parseMenuItems(data) };
|
||||
}
|
||||
|
||||
/** The container that owns gesture events (exactly one per valid page). */
|
||||
|
||||
@@ -20,6 +20,7 @@ const API_HOST = "https://api.evenrealities.com";
|
||||
const PUBLIC_CDN_HOST = "https://cdn-pub.evenhub.evenrealities.com";
|
||||
const SIGN_KEY = "a7964f42c39200cfa25c258b7a311b106e20232173667e543c34ced91d63b404";
|
||||
const REQUEST_TIMEOUT_MS = 30_000;
|
||||
const PAGE_SIZE = 40;
|
||||
|
||||
export type EvenHubStoreApp = {
|
||||
id: number;
|
||||
@@ -80,17 +81,31 @@ export class EvenHubApiClient {
|
||||
private loginPromise: Promise<string> | null = null;
|
||||
|
||||
async listApps(page = 1, category = ""): Promise<EvenHubStorePage> {
|
||||
const query: Record<string, string | number> = { page, page_size: 40 };
|
||||
const query: Record<string, string | number> = { page, page_size: PAGE_SIZE };
|
||||
if (category) query.category = category;
|
||||
const data = await this.authenticatedRequest("GET", "/v2/evenhub/leaderboard", { query });
|
||||
const root = asRecord(data);
|
||||
const list = Array.isArray(root.list) ? root.list : [];
|
||||
return {
|
||||
apps: list.map(parseStoreApp).filter((app): app is EvenHubStoreApp => app !== null),
|
||||
total: finiteNumber(root.total),
|
||||
page: finiteNumber(root.page) || page,
|
||||
pageSize: finiteNumber(root.page_size) || 40,
|
||||
};
|
||||
return parseStorePage(data, page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Public apps ordered by first publication, newest first. The official app
|
||||
* requests `/ranking?sort_by=first_published_at`; whether the endpoint pages
|
||||
* is unverified, so callers must tolerate a repeated (or complete) list and
|
||||
* a missing total.
|
||||
*/
|
||||
async listNewApps(page = 1): Promise<EvenHubStorePage> {
|
||||
const data = await this.authenticatedRequest("GET", "/v2/evenhub/ranking", {
|
||||
query: { sort_by: "first_published_at", page, page_size: PAGE_SIZE },
|
||||
});
|
||||
return parseStorePage(data, page);
|
||||
}
|
||||
|
||||
/** Full-text storefront search (`/search?q=`), with the same paging caveats as listNewApps. */
|
||||
async searchApps(queryText: string, page = 1): Promise<EvenHubStorePage> {
|
||||
const data = await this.authenticatedRequest("GET", "/v2/evenhub/search", {
|
||||
query: { q: queryText, page, page_size: PAGE_SIZE },
|
||||
});
|
||||
return parseStorePage(data, page);
|
||||
}
|
||||
|
||||
async getAppDetail(packageId: string): Promise<Record<string, unknown>> {
|
||||
@@ -254,8 +269,15 @@ function buildCommon(version: 1 | 3): string {
|
||||
const fields: Record<string, string | number> = {
|
||||
platform: "16",
|
||||
package: "com.even.sg",
|
||||
versionName: "2.2.6",
|
||||
build: "114",
|
||||
// versionName/build must be a real shipped pair of the official Android
|
||||
// app (versionName/versionCode from its manifest): 2.2.8 = versionCode 122
|
||||
// (captures/even-2.2.8-base.apk). Earlier this said 2.2.6 with build 114,
|
||||
// but 114 was actually 2.2.4's versionCode (see notes/firmware-download.md).
|
||||
// buildTime is not a literal in the app binary (computed at runtime), so
|
||||
// it still carries the value from the 2.2.4 capture; the API has accepted
|
||||
// the mismatch so far.
|
||||
versionName: "2.2.8",
|
||||
build: "122",
|
||||
brand: "Google",
|
||||
model: "Pixel 7a",
|
||||
osVersion: "16",
|
||||
@@ -357,6 +379,25 @@ export function getPhoneOpenUdid(): string {
|
||||
return openUdid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a list response. The leaderboard returns `{list, total, page,
|
||||
* page_size}`; the ranking/search endpoints were only recovered from the
|
||||
* client's request paths, so accept a bare array or the other common
|
||||
* wrapper keys too. A missing total comes back as 0 ("unknown").
|
||||
*/
|
||||
function parseStorePage(data: unknown, requestedPage: number): EvenHubStorePage {
|
||||
const root = asRecord(data);
|
||||
const list = Array.isArray(data)
|
||||
? data
|
||||
: [root.list, root.apps, root.items, root.data, root.results].find(Array.isArray) ?? [];
|
||||
return {
|
||||
apps: list.map(parseStoreApp).filter((app): app is EvenHubStoreApp => app !== null),
|
||||
total: finiteNumber(root.total),
|
||||
page: finiteNumber(root.page) || requestedPage,
|
||||
pageSize: finiteNumber(root.page_size) || PAGE_SIZE,
|
||||
};
|
||||
}
|
||||
|
||||
function parseStoreApp(value: unknown): EvenHubStoreApp | null {
|
||||
const item = asRecord(value);
|
||||
const version = asRecord(item.version);
|
||||
|
||||
@@ -35,9 +35,11 @@ class EvenHubAppLayer implements Layer {
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent): void {
|
||||
// Everything goes to the app; long-press never reaches here (the window
|
||||
// menu intercepts it), which is the guaranteed way out since EvenHub
|
||||
// apps own double-click.
|
||||
// Everything goes to the app; the menu gestures never reach here (the
|
||||
// shell keeps long-press for its system menu and the window's context
|
||||
// menu intercepts tap-then-hold), which is the guaranteed way out since
|
||||
// EvenHub apps own double-click. Tap-then-hold is still reported to the
|
||||
// app as LONG_PRESS_EVENT — see the handleInput wrapper below.
|
||||
this.session.handleGesture(event);
|
||||
}
|
||||
}
|
||||
@@ -61,9 +63,22 @@ export function createEvenHubWindow(
|
||||
fallbackIcon,
|
||||
),
|
||||
heightMode: "medium",
|
||||
// Glasses-first: the app runs without the phone showing it; this reveals
|
||||
// its phone UI (config pages, etc.) over the dashboard on demand.
|
||||
menuItems: () => [{ label: "Show phone UI", onSelect: (ctx) => { ctx.stack.pop(); onShowPhone(); } }],
|
||||
// The window's context menu doubles as the OS contextual menu EvenHub
|
||||
// apps register with `menuObject` (SDK 0.0.14): the app's own entries come
|
||||
// first, then the host entries. Evaluated at open time, so a rebuild that
|
||||
// changes or clears the menu is reflected on the next press.
|
||||
menuItems: () => [
|
||||
...session.contextMenuItems().map((item) => ({
|
||||
label: item.itemName,
|
||||
onSelect: (ctx: LayerContext) => {
|
||||
ctx.stack.pop();
|
||||
session.selectContextMenuItem(item.itemID);
|
||||
},
|
||||
})),
|
||||
// Glasses-first: the app runs without the phone showing it; this reveals
|
||||
// its phone UI (config pages, etc.) over the dashboard on demand.
|
||||
{ label: "Show phone UI", onSelect: (ctx: LayerContext) => { ctx.stack.pop(); onShowPhone(); } },
|
||||
],
|
||||
actions: options.actions,
|
||||
baseLayer: new EvenHubAppLayer(session),
|
||||
submitFrame: options.submitFrame,
|
||||
@@ -87,6 +102,20 @@ export function createEvenHubWindow(
|
||||
windowId,
|
||||
});
|
||||
|
||||
// LONG_PRESS_EVENT / LONG_PRESS_RELEASE_EVENT (SDK 0.0.14): the app hears
|
||||
// the context-menu gesture (tap-then-hold; a plain long-press is the
|
||||
// shell's and never arrives) as its long-press. Wrapped at the window
|
||||
// rather than handled in the base layer because the press opens the window
|
||||
// menu and so never reaches a layer, and because the release lands on that
|
||||
// menu once it is up.
|
||||
const baseHandleInput = created.window.handleInput;
|
||||
created.window.handleInput = async (event, frameId) => {
|
||||
if (event.type === "short-then-long-press" || event.type === "long-press-release") {
|
||||
session.handleGesture(event);
|
||||
}
|
||||
await baseHandleInput(event, frameId);
|
||||
};
|
||||
|
||||
// FOREGROUND_ENTER/EXIT for the app on shell focus changes.
|
||||
const baseSetForeground = created.window.setForeground;
|
||||
created.window.setForeground = (foreground) => {
|
||||
|
||||
+60
-33
@@ -14,6 +14,7 @@ import { launchPackage, launchUrl, normalizeAppUrl } from "./manager";
|
||||
import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { renderEvenRealitiesLogo } from "../../graphics/even-realities-logo";
|
||||
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
|
||||
import { type InstalledEvenHubApp } from "./installed-apps";
|
||||
|
||||
const EVENHUB_STORE_WINDOW_ID = "evenhub:store";
|
||||
const EVENHUB_STORE_SURFACE_ID = "window:evenhub:store";
|
||||
@@ -40,45 +41,71 @@ export function isLoadableAppUrl(url: string): boolean {
|
||||
return normalizeAppUrl(url) !== null;
|
||||
}
|
||||
|
||||
/** The open store window, so a second launch focuses it and deep links can reach its layer. */
|
||||
let storeWindow: { window: InProcessWindow; store: EvenHubStoreLayer } | null = null;
|
||||
|
||||
/** Open the store window, or focus the one already open. */
|
||||
async function ensureStoreWindow(ctx: AppContext): Promise<{ window: InProcessWindow; store: EvenHubStoreLayer }> {
|
||||
if (storeWindow && shell.getWindows().some((window) => window.windowId === EVENHUB_STORE_WINDOW_ID)) {
|
||||
shell.focusWindow(EVENHUB_STORE_WINDOW_ID);
|
||||
ctx.requestShellRender();
|
||||
return storeWindow;
|
||||
}
|
||||
let created: InProcessWindow | null = null;
|
||||
const store = new EvenHubStoreLayer({
|
||||
launchApp: (appId) => ctx.launchApp(appId),
|
||||
appendLog: ctx.appendLog,
|
||||
});
|
||||
await ctx.launchInProcessApp(EVENHUB_STORE_WINDOW_ID, EVENHUB_STORE_SURFACE_ID, (options) => {
|
||||
created = createInProcessWindow({
|
||||
appId: "evenhub",
|
||||
windowId: EVENHUB_STORE_WINDOW_ID,
|
||||
title: "EvenHub",
|
||||
iconLetter: "EH",
|
||||
icon: "package",
|
||||
drawIcon: makeImageWindowIcon(renderEvenRealitiesLogo, windowIcon("package", "EH")),
|
||||
closeable: true,
|
||||
menuItems: () => (created?.stack.isAtBase() ? store.buildMenuItems() : []),
|
||||
actions: options.actions,
|
||||
// Dictated text runs a store search (when the store page itself is on top).
|
||||
receiveTextInput: (text) => {
|
||||
if (created?.stack.receiveTextInput(text)) created.requestRender();
|
||||
},
|
||||
baseLayer: store,
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
store.onWindowClosed(options.actions);
|
||||
if (storeWindow?.store === store) storeWindow = null;
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
return created;
|
||||
});
|
||||
if (!created) throw new Error("EvenHub store window was not created.");
|
||||
storeWindow = { window: created, store };
|
||||
return storeWindow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the store on an installed app's page. The launcher uses it when the
|
||||
* registry lists a package whose EHPK is gone (a settings import after a
|
||||
* reinstall), so the tap lands on a Reinstall button instead of failing.
|
||||
*/
|
||||
export async function openEvenHubStoreForPackage(ctx: AppContext, installed: InstalledEvenHubApp): Promise<void> {
|
||||
const { window, store } = await ensureStoreWindow(ctx);
|
||||
store.showInstalledPackage(window.stack, installed);
|
||||
window.requestRender();
|
||||
}
|
||||
|
||||
const evenhubApp: AppDefinition = {
|
||||
appId: "evenhub",
|
||||
title: "EvenHub",
|
||||
icon: "package",
|
||||
renderIcon: renderEvenRealitiesLogo,
|
||||
launch: async (ctx) => {
|
||||
const existing = shell.getWindows().find((window) => window.windowId === EVENHUB_STORE_WINDOW_ID);
|
||||
if (existing) {
|
||||
shell.focusWindow(existing.windowId);
|
||||
ctx.requestShellRender();
|
||||
return;
|
||||
}
|
||||
let created: InProcessWindow | null = null;
|
||||
const store = new EvenHubStoreLayer({
|
||||
launchApp: (appId) => ctx.launchApp(appId),
|
||||
appendLog: ctx.appendLog,
|
||||
});
|
||||
await ctx.launchInProcessApp(EVENHUB_STORE_WINDOW_ID, EVENHUB_STORE_SURFACE_ID, (options) => {
|
||||
created = createInProcessWindow({
|
||||
appId: "evenhub",
|
||||
windowId: EVENHUB_STORE_WINDOW_ID,
|
||||
title: "EvenHub",
|
||||
iconLetter: "EH",
|
||||
icon: "package",
|
||||
drawIcon: makeImageWindowIcon(renderEvenRealitiesLogo, windowIcon("package", "EH")),
|
||||
closeable: true,
|
||||
menuItems: () => (created?.stack.isAtBase() ? store.buildMenuItems() : []),
|
||||
actions: options.actions,
|
||||
baseLayer: store,
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
store.onWindowClosed(options.actions);
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
return created;
|
||||
});
|
||||
await ensureStoreWindow(ctx);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { drawListScrollbar, drawSelectionHighlight } from "../../ui/menu";
|
||||
import { permissionDetail, permissionLabel, type EvenHubPermission } from "./permissions";
|
||||
import { openPrivacyPolicyOnPhone } from "./privacy-policy";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
@@ -19,10 +19,20 @@ const PERM_GAP = 4;
|
||||
* before installing it or running an uninstalled package. Allow proceeds;
|
||||
* Cancel (or double-click) backs out. A privacy-policy-only app still reaches
|
||||
* this dialog so the user can review the policy before allowing first run.
|
||||
*
|
||||
* When the list overflows the viewport the dialog scrolls: scroll events move
|
||||
* through the content row by row, and only once the bottom is fully visible do
|
||||
* further scrolls move the action selection. The actions are inert until then
|
||||
* (a click just scrolls), so the user can't Allow without having had the whole
|
||||
* list on screen.
|
||||
*/
|
||||
export class EvenHubPermissionDialogLayer implements Layer {
|
||||
/** Starts on Allow; the optional privacy-policy action is inserted after it. */
|
||||
private selectedIndex = 0;
|
||||
/** First content row drawn; rows above it are scrolled off. */
|
||||
private firstRow = 0;
|
||||
/** Scroll position at which the last row is fully visible; set by paint. */
|
||||
private maxFirstRow = 0;
|
||||
private resolved = false;
|
||||
|
||||
constructor(
|
||||
@@ -33,6 +43,11 @@ export class EvenHubPermissionDialogLayer implements Layer {
|
||||
private readonly onCancel: () => void,
|
||||
) {}
|
||||
|
||||
/** Whether the dialog is scrolled to the bottom, making the actions live. */
|
||||
private atBottom(): boolean {
|
||||
return this.firstRow >= this.maxFirstRow;
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { height: viewportHeight } = ctx.stack.getBaseSize();
|
||||
@@ -44,48 +59,88 @@ export class EvenHubPermissionDialogLayer implements Layer {
|
||||
const permLabelStep = lineStep(font) + 1;
|
||||
const permDetailStep = lineStep(font) - 1;
|
||||
const actionRowH = listRowHeight(font);
|
||||
const permsHeight = this.permissions.reduce(
|
||||
(sum, permission) => sum + permLabelStep + (permissionDetail(permission) ? permDetailStep : 0) + PERM_GAP,
|
||||
0,
|
||||
);
|
||||
const emptyPermissionsHeight = this.permissions.length === 0 ? permLabelStep + PERM_GAP : 0;
|
||||
const bodyTop = PADDING + headerStep + 6 + permsHeight + emptyPermissionsHeight + 6;
|
||||
const height = Math.min(bodyTop + this.actions().length * actionRowH + PADDING, viewportHeight - 2 * DIALOG_Y);
|
||||
const actions = this.actions();
|
||||
const focused = ctx.stack.isFocused();
|
||||
|
||||
// The dialog body as scrollable rows; each row's height includes the gap
|
||||
// below it, so scrolling and visibility checks work on whole rows.
|
||||
type Row = { height: number; draw: (top: number) => void };
|
||||
const rows: Row[] = [];
|
||||
const heading = this.permissions.length ? `${this.appName} needs:` : `${this.appName} is ready to run`;
|
||||
rows.push({
|
||||
height: headerStep + 6,
|
||||
draw: (top) => image.drawText(font, left, top, truncateText(font, heading, textWidth), 235),
|
||||
});
|
||||
if (this.permissions.length === 0) {
|
||||
rows.push({
|
||||
height: permLabelStep + PERM_GAP + 2,
|
||||
draw: (top) => image.drawText(font, left, top, "No special permissions requested.", 140),
|
||||
});
|
||||
}
|
||||
for (let index = 0; index < this.permissions.length; index++) {
|
||||
const permission = this.permissions[index]!;
|
||||
const detail = permissionDetail(permission);
|
||||
const gapBelow = PERM_GAP + (index === this.permissions.length - 1 ? 2 : 0);
|
||||
rows.push({
|
||||
height: permLabelStep + (detail ? 0 : gapBelow),
|
||||
draw: (top) => image.drawText(font, left, top, truncateText(font, permissionLabel(permission.name), textWidth), 220),
|
||||
});
|
||||
if (detail) {
|
||||
rows.push({
|
||||
height: permDetailStep + gapBelow,
|
||||
draw: (top) => image.drawText(font, left + 8, top, truncateText(font, detail, textWidth - 8), 140),
|
||||
});
|
||||
}
|
||||
}
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
rows.push({
|
||||
height: actionRowH,
|
||||
draw: (top) => {
|
||||
const selected = this.atBottom() && index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, DIALOG_X + 12, top, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8);
|
||||
}
|
||||
image.drawText(font, DIALOG_X + 22, top + 3, actions[index]!, selected ? 255 : 200);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const contentHeight = rows.reduce((sum, row) => sum + row.height, 0);
|
||||
const height = Math.min(contentHeight + 2 * PADDING, viewportHeight - 2 * DIALOG_Y);
|
||||
const visibleHeight = height - 2 * PADDING;
|
||||
|
||||
// The bottommost scroll position: the largest suffix of rows that fits.
|
||||
let maxFirstRow = rows.length;
|
||||
for (let suffixHeight = 0; maxFirstRow > 0; maxFirstRow--) {
|
||||
const rowHeight = rows[maxFirstRow - 1]!.height;
|
||||
if (suffixHeight + rowHeight > visibleHeight) break;
|
||||
suffixHeight += rowHeight;
|
||||
}
|
||||
this.maxFirstRow = maxFirstRow;
|
||||
this.firstRow = Math.max(0, Math.min(this.firstRow, maxFirstRow));
|
||||
|
||||
// Fill 1 (transparent color key is 0), outline for the dialog edge.
|
||||
image.fillRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 1);
|
||||
image.drawRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 72);
|
||||
|
||||
let y = DIALOG_Y + PADDING;
|
||||
const heading = this.permissions.length ? `${this.appName} needs:` : `${this.appName} is ready to run`;
|
||||
image.drawText(font, left, y, truncateText(font, heading, textWidth), 235);
|
||||
y += headerStep + 6;
|
||||
|
||||
if (this.permissions.length === 0) {
|
||||
image.drawText(font, left, y, "No special permissions requested.", 140);
|
||||
y += emptyPermissionsHeight;
|
||||
for (let index = this.firstRow; index < rows.length; index++) {
|
||||
const row = rows[index]!;
|
||||
if (y + row.height > DIALOG_Y + PADDING + visibleHeight) break;
|
||||
row.draw(y);
|
||||
y += row.height;
|
||||
}
|
||||
for (const permission of this.permissions) {
|
||||
image.drawText(font, left, y, truncateText(font, permissionLabel(permission.name), textWidth), 220);
|
||||
y += permLabelStep;
|
||||
const detail = permissionDetail(permission);
|
||||
if (detail) {
|
||||
image.drawText(font, left + 8, y, truncateText(font, detail, textWidth - 8), 140);
|
||||
y += permDetailStep;
|
||||
}
|
||||
y += PERM_GAP;
|
||||
}
|
||||
y += 2;
|
||||
|
||||
const focused = ctx.stack.isFocused();
|
||||
const actions = this.actions();
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
const rowY = y + index * actionRowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8);
|
||||
}
|
||||
image.drawText(font, DIALOG_X + 22, rowY + 3, actions[index]!, selected ? 255 : 200);
|
||||
if (maxFirstRow > 0) {
|
||||
drawListScrollbar(
|
||||
image,
|
||||
DIALOG_X + DIALOG_WIDTH - 7,
|
||||
DIALOG_Y + PADDING,
|
||||
visibleHeight,
|
||||
this.firstRow,
|
||||
rows.length - maxFirstRow,
|
||||
rows.length,
|
||||
);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
@@ -94,12 +149,24 @@ export class EvenHubPermissionDialogLayer implements Layer {
|
||||
const actions = this.actions();
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = (this.selectedIndex + actions.length - 1) % actions.length;
|
||||
if (this.atBottom() && this.selectedIndex > 0) {
|
||||
this.selectedIndex -= 1;
|
||||
} else if (this.firstRow > 0) {
|
||||
this.firstRow -= 1;
|
||||
}
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = (this.selectedIndex + 1) % actions.length;
|
||||
if (!this.atBottom()) {
|
||||
this.firstRow += 1;
|
||||
} else {
|
||||
this.selectedIndex = Math.min(actions.length - 1, this.selectedIndex + 1);
|
||||
}
|
||||
return;
|
||||
case "click":
|
||||
if (!this.atBottom()) {
|
||||
this.firstRow += 1;
|
||||
return;
|
||||
}
|
||||
if (actions[this.selectedIndex] === "Privacy policy") {
|
||||
openPrivacyPolicyOnPhone(this.privacyPolicyUrl, this.appName);
|
||||
} else {
|
||||
|
||||
@@ -38,14 +38,17 @@ import {
|
||||
import { EvenHubApiKeyDialogLayer } from "./api-key-dialog";
|
||||
import {
|
||||
asRecord,
|
||||
declaredMenuItems,
|
||||
eventCaptureContainer,
|
||||
parsePage,
|
||||
readNumber,
|
||||
readOptionalNumber,
|
||||
readString,
|
||||
readTextBrightness,
|
||||
type EvenHubImageContainer,
|
||||
type EvenHubListContainer,
|
||||
type EvenHubContainer,
|
||||
type EvenHubMenuItem,
|
||||
type EvenHubPage,
|
||||
} from "./containers";
|
||||
import { compositePage } from "./compositor";
|
||||
@@ -53,7 +56,7 @@ import { type EvenHubManifest } from "./ehpk";
|
||||
import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions";
|
||||
import { evenHubMicRouter, type EvenHubMicClient } from "./mic-router";
|
||||
import { evenHubImuRouter, type EvenHubImuClient } from "./imu-router";
|
||||
import { evenHubCompassRouter, type EvenHubCompassClient } from "./compass-router";
|
||||
import { evenHubCompassRouter, type EvenHubCompassClient, type EvenHubCompassReading } from "./compass-router";
|
||||
import { type ImuReading } from "../../native/imu";
|
||||
import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry";
|
||||
import { getCurrentLocation } from "../../native/location";
|
||||
@@ -73,6 +76,9 @@ const FOREGROUND_ENTER_EVENT = 4;
|
||||
const FOREGROUND_EXIT_EVENT = 5;
|
||||
const SYSTEM_EXIT_EVENT = 7;
|
||||
const IMU_DATA_REPORT_EVENT = 8;
|
||||
/** SDK 0.0.14: the press gesture, delivered like any other list/text/sys event. */
|
||||
const LONG_PRESS_EVENT = 9;
|
||||
const LONG_PRESS_RELEASE_EVENT = 10;
|
||||
|
||||
/**
|
||||
* Injected into every served HTML document at document start (before any app
|
||||
@@ -440,6 +446,24 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
case "double-click":
|
||||
this.emitSysEvent(DOUBLE_CLICK_EVENT, gestureSource(event.source));
|
||||
break;
|
||||
case "long-press":
|
||||
case "short-then-long-press":
|
||||
case "long-press-release": {
|
||||
// SDK 0.0.14 exposes press and release as ordinary events. The app's
|
||||
// press is the context-menu gesture (tap-then-hold; the shell keeps
|
||||
// the plain long-press for its system menu): the window still opens
|
||||
// its menu on it (that menu is where the app's contextual-menu items
|
||||
// live, and it is the way out of an app that owns double-click), so
|
||||
// an app hears the press as well as seeing the menu — it cannot be
|
||||
// given exclusive use of the gesture.
|
||||
const eventType = event.type === "long-press-release" ? LONG_PRESS_RELEASE_EVENT : LONG_PRESS_EVENT;
|
||||
if (capture?.kind === "list") {
|
||||
this.emitListEvent(capture, eventType);
|
||||
} else {
|
||||
this.emitSysEvent(eventType, gestureSource(event.source));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "scroll-up":
|
||||
case "scroll-down": {
|
||||
const eventType = event.type === "scroll-up" ? SCROLL_TOP_EVENT : SCROLL_BOTTOM_EVENT;
|
||||
@@ -455,6 +479,20 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The contextual-menu entries the running page registered (SDK 0.0.14
|
||||
* `menuObject`), for the window to merge into its own menu. Empty when the
|
||||
* app declared none.
|
||||
*/
|
||||
contextMenuItems(): EvenHubMenuItem[] {
|
||||
return this.page?.menuItems ?? [];
|
||||
}
|
||||
|
||||
/** The user picked one of the app's contextual-menu entries. */
|
||||
selectContextMenuItem(itemID: number): void {
|
||||
this.pushMessage("evenHubEvent", { type: "menuItemClickEvent", jsonData: { itemID } });
|
||||
}
|
||||
|
||||
private scrollList(list: EvenHubListContainer, eventType: number): void {
|
||||
// Swipe up selects the previous item, swipe down the next (stock direction).
|
||||
const delta = eventType === SCROLL_TOP_EVENT ? -1 : 1;
|
||||
@@ -542,10 +580,21 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
return this.screenOn;
|
||||
}
|
||||
|
||||
/** A decoded mic frame from the router: hand it to the app as an audioEvent. */
|
||||
/**
|
||||
* A decoded mic frame from the router: hand it to the app as an audioEvent.
|
||||
*
|
||||
* SDK 0.0.14 added per-frame `direction` and `speakerRole`. Neither is
|
||||
* available here — the router hands over plain PCM with no beamforming tag,
|
||||
* and speakerRole is an app-level diarization result the stock host computes
|
||||
* — so both are sent as the fallbacks the SDK documents for a phone mic and
|
||||
* for older hosts.
|
||||
*/
|
||||
deliverAudioPcm(pcm: Uint8Array): void {
|
||||
const audioPcm = Array.from(pcm);
|
||||
this.pushMessage("evenHubEvent", { type: "audioEvent", jsonData: { audioPcm } });
|
||||
this.pushMessage("evenHubEvent", {
|
||||
type: "audioEvent",
|
||||
jsonData: { audioPcm, direction: null, speakerRole: "unknown" },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -757,7 +806,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
return 1;
|
||||
}
|
||||
const page = parsePage(data);
|
||||
this.warnOnInvalidPage(page);
|
||||
this.warnOnInvalidPage(page, data);
|
||||
this.page = page;
|
||||
this.pageCreated = true;
|
||||
// The launch focus arrived before the page existed; deliver the ENTER now.
|
||||
@@ -772,14 +821,21 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
private rebuildPage(data: Record<string, unknown>): boolean {
|
||||
// Also serves as create for apps that fall back to rebuild-on-error.
|
||||
const page = parsePage(data);
|
||||
this.warnOnInvalidPage(page);
|
||||
this.warnOnInvalidPage(page, data);
|
||||
this.page = page;
|
||||
this.pageCreated = true;
|
||||
this.windowHooks?.requestRender();
|
||||
return true;
|
||||
}
|
||||
|
||||
private warnOnInvalidPage(page: EvenHubPage): void {
|
||||
private warnOnInvalidPage(page: EvenHubPage, data: Record<string, unknown>): void {
|
||||
const declaredMenuCount = declaredMenuItems(data).length;
|
||||
if (declaredMenuCount > page.menuItems.length) {
|
||||
this.log(
|
||||
`evenhub: dropped ${declaredMenuCount - page.menuItems.length} of ${declaredMenuCount} contextual menu ` +
|
||||
"items (max 10, itemID must be non-zero and unique, itemName at most 32 UTF-8 bytes)",
|
||||
);
|
||||
}
|
||||
// Stock rejects these outright; we accept-and-log until rejection is
|
||||
// proven necessary for compatibility.
|
||||
const captures = page.containers.filter((c) => c.kind !== "image" && c.isEventCapture).length;
|
||||
@@ -813,6 +869,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
} else {
|
||||
container.content = content;
|
||||
}
|
||||
// SDK 0.0.14: textColor on an incremental update sets the container's
|
||||
// brightness; omitting it keeps whatever level the container has.
|
||||
const brightness = readTextBrightness(data);
|
||||
if (brightness !== undefined) container.textColor = brightness;
|
||||
this.windowHooks?.requestRender();
|
||||
return true;
|
||||
}
|
||||
@@ -953,8 +1013,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
|
||||
// ----- compass (EvenHubCompassClient) -----
|
||||
|
||||
deliverCompass(headingDegrees: number): void {
|
||||
this.pushExtEvent("compass", { headingDegrees });
|
||||
deliverCompass(reading: EvenHubCompassReading): void {
|
||||
this.pushExtEvent("compass", reading);
|
||||
}
|
||||
|
||||
private setCompass(enable: boolean): void {
|
||||
|
||||
@@ -8,13 +8,12 @@ import { TextViewerLayer } from "../files/text-viewer";
|
||||
import { evenHubApi, type EvenHubStoreApp } from "./even-api";
|
||||
import {
|
||||
getInstalledEvenHubApp,
|
||||
installEvenHubPackageBytes,
|
||||
installedEvenHubAppId,
|
||||
readEvenHubPackageManifestBytes,
|
||||
setInstalledEvenHubIcon,
|
||||
type EvenHubInstallIcon,
|
||||
type InstalledEvenHubApp,
|
||||
} from "./installed-apps";
|
||||
import { EvenHubPermissionDialogLayer } from "./permission-dialog";
|
||||
import { cleanError, installStoreApp } from "./store-install";
|
||||
import { describeUpdate } from "./updates";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
|
||||
const X = 18;
|
||||
@@ -22,6 +21,8 @@ const X = 18;
|
||||
export type EvenHubStoreDetailOptions = {
|
||||
launchApp: (appId: string) => Promise<void> | void;
|
||||
appendLog: (message: string) => void;
|
||||
/** A package was installed, updated, or reinstalled from this page. */
|
||||
onInstalled?: (app: InstalledEvenHubApp) => void;
|
||||
};
|
||||
|
||||
/** Storefront metadata and the Install/Launch action for one public app. */
|
||||
@@ -46,7 +47,7 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
const actionRowH = listRowHeight(font) + 4;
|
||||
|
||||
const installed = getInstalledEvenHubApp(this.app.packageId);
|
||||
const actions = ["About", "What's New", this.working ? "Installing..." : installed ? "Launch" : "Install"];
|
||||
const actions = ["About", "What's New", this.working ? "Installing..." : this.primaryActionLabel(installed)];
|
||||
|
||||
// Detail lines as (text, shade, extra gap below); built per candidate
|
||||
// width so the layout choice below can measure before drawing.
|
||||
@@ -70,7 +71,13 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
lines.push({ text: truncateText(font, line, textWidth), value: 125, gapAfter: 0 });
|
||||
}
|
||||
if (installed && !this.status) {
|
||||
lines.push({ text: `Installed version ${installed.version}`, value: 155, gapAfter: 0 });
|
||||
const standing = describeUpdate(installed, this.app);
|
||||
const note = standing.packageMissing
|
||||
? " · package missing, reinstall"
|
||||
: standing.updateAvailable
|
||||
? " · update available"
|
||||
: "";
|
||||
lines.push({ text: truncateText(font, `Installed version ${installed.version}${note}`, textWidth), value: 155, gapAfter: 0 });
|
||||
}
|
||||
if (this.status) {
|
||||
lines.push({ text: truncateText(font, this.status, textWidth), value: 180, gapAfter: 0 });
|
||||
@@ -138,77 +145,39 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
}
|
||||
|
||||
const installed = getInstalledEvenHubApp(this.app.packageId);
|
||||
if (installed) {
|
||||
if (installed && this.primaryActionLabel(installed) === "Launch") {
|
||||
await this.options.launchApp(installedEvenHubAppId(installed.packageId));
|
||||
return;
|
||||
}
|
||||
|
||||
await this.prepareInstall(ctx);
|
||||
await this.install(ctx);
|
||||
}
|
||||
|
||||
private async prepareInstall(ctx: LayerContext): Promise<void> {
|
||||
/** Install for a new app; Update / Reinstall when the installed copy is stale or gone. */
|
||||
private primaryActionLabel(installed: InstalledEvenHubApp | null): string {
|
||||
if (!installed) return "Install";
|
||||
const standing = describeUpdate(installed, this.app);
|
||||
if (standing.packageMissing) return "Reinstall";
|
||||
return standing.updateAvailable ? "Update" : "Launch";
|
||||
}
|
||||
|
||||
private async install(ctx: LayerContext): Promise<void> {
|
||||
this.working = true;
|
||||
this.status = `Downloading ${this.app.name}...`;
|
||||
ctx.actions.requestRender();
|
||||
try {
|
||||
const [download, icon] = await Promise.all([
|
||||
evenHubApi.downloadApp(this.app.packageId),
|
||||
this.app.iconPath
|
||||
? evenHubApi.downloadPublicAsset(this.app.iconPath).catch((error) => {
|
||||
this.options.appendLog(`evenhub store: icon unavailable for ${this.app.packageId}: ${cleanError(error)}`);
|
||||
return undefined;
|
||||
})
|
||||
: Promise.resolve(undefined),
|
||||
]);
|
||||
const manifest = readEvenHubPackageManifestBytes(download.bytes);
|
||||
if (!manifest) throw new Error("The downloaded EHPK manifest could not be read.");
|
||||
const privacyPolicyUrl = download.privacyPolicyUrl || manifest.privacyPolicyUrl;
|
||||
this.working = false;
|
||||
this.status = "";
|
||||
ctx.actions.requestRender();
|
||||
if (manifest.permissions.length > 0 || privacyPolicyUrl) {
|
||||
ctx.stack.push(
|
||||
new EvenHubPermissionDialogLayer(
|
||||
manifest.name,
|
||||
manifest.permissions,
|
||||
privacyPolicyUrl,
|
||||
() => {
|
||||
void this.installAndLaunch(ctx, download.bytes, icon);
|
||||
},
|
||||
() => {
|
||||
this.status = "Installation canceled.";
|
||||
ctx.actions.requestRender();
|
||||
},
|
||||
),
|
||||
);
|
||||
const result = await installStoreApp(ctx, this.app, {
|
||||
appendLog: this.options.appendLog,
|
||||
onStatus: (status) => {
|
||||
this.status = status;
|
||||
ctx.actions.requestRender();
|
||||
},
|
||||
});
|
||||
if (!result) {
|
||||
this.status = "Installation canceled.";
|
||||
return;
|
||||
}
|
||||
await this.installAndLaunch(ctx, download.bytes, icon);
|
||||
} catch (error) {
|
||||
this.working = false;
|
||||
this.status = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: ${this.status}`);
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
private async installAndLaunch(
|
||||
ctx: LayerContext,
|
||||
bytes: Uint8Array,
|
||||
icon: EvenHubInstallIcon | undefined,
|
||||
): Promise<void> {
|
||||
this.working = true;
|
||||
this.status = "Installing...";
|
||||
ctx.actions.requestRender();
|
||||
try {
|
||||
const result = installEvenHubPackageBytes(bytes, {
|
||||
expectedPackageId: this.app.packageId,
|
||||
icon,
|
||||
});
|
||||
this.options.appendLog(
|
||||
`evenhub store: installed ${result.packageId} ${result.version} (${bytes.length} bytes)`,
|
||||
);
|
||||
this.status = "Installed";
|
||||
this.options.onInstalled?.(result);
|
||||
ctx.actions.requestRender();
|
||||
await this.options.launchApp(installedEvenHubAppId(result.packageId));
|
||||
} catch (error) {
|
||||
@@ -258,7 +227,3 @@ function formatBytes(value: number): string {
|
||||
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} KB`;
|
||||
return `${(value / (1024 * 1024)).toFixed(1)} MB`;
|
||||
}
|
||||
|
||||
function cleanError(error: unknown): string {
|
||||
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* The storefront install pipeline shared by the app detail page and the
|
||||
* Updates tab: download the package and its icon, confirm permissions when
|
||||
* that is warranted, close running instances, and store the package.
|
||||
*/
|
||||
import { type LayerContext } from "../../ui/layers";
|
||||
import { evenHubApi, type EvenHubStoreApp } from "./even-api";
|
||||
import { type EvenHubManifest } from "./ehpk";
|
||||
import {
|
||||
installEvenHubPackageBytes,
|
||||
installedEvenHubPackagePath,
|
||||
readEvenHubPackageManifest,
|
||||
readEvenHubPackageManifestBytes,
|
||||
type InstalledEvenHubApp,
|
||||
} from "./installed-apps";
|
||||
import { closeRunningPackage } from "./manager";
|
||||
import { EvenHubPermissionDialogLayer } from "./permission-dialog";
|
||||
|
||||
export type StoreInstallOptions = {
|
||||
appendLog: (message: string) => void;
|
||||
/** Progress text for the caller's status line ("Downloading X...", "Installing..."). */
|
||||
onStatus: (status: string) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* Install (or update/reinstall) one storefront app. Resolves to the new
|
||||
* registry entry, or null when the user declined the permission dialog.
|
||||
* Failures throw with a user-presentable message.
|
||||
*
|
||||
* The permission dialog is shown for a first install, and for an update
|
||||
* whose manifest declares permissions the currently installed package did
|
||||
* not (or a privacy policy it did not); an update that asks for nothing new
|
||||
* goes straight through.
|
||||
*/
|
||||
export async function installStoreApp(
|
||||
ctx: LayerContext,
|
||||
app: EvenHubStoreApp,
|
||||
options: StoreInstallOptions,
|
||||
): Promise<InstalledEvenHubApp | null> {
|
||||
options.onStatus(`Downloading ${app.name}...`);
|
||||
const [download, icon] = await Promise.all([
|
||||
evenHubApi.downloadApp(app.packageId),
|
||||
app.iconPath
|
||||
? evenHubApi.downloadPublicAsset(app.iconPath).catch((error) => {
|
||||
options.appendLog(`evenhub store: icon unavailable for ${app.packageId}: ${cleanError(error)}`);
|
||||
return undefined;
|
||||
})
|
||||
: Promise.resolve(undefined),
|
||||
]);
|
||||
const manifest = readEvenHubPackageManifestBytes(download.bytes);
|
||||
if (!manifest) throw new Error("The downloaded EHPK manifest could not be read.");
|
||||
const privacyPolicyUrl = download.privacyPolicyUrl || manifest.privacyPolicyUrl;
|
||||
const needsPrompt = (manifest.permissions.length > 0 || privacyPolicyUrl)
|
||||
&& !alreadyGranted(app.packageId, manifest);
|
||||
if (needsPrompt) {
|
||||
options.onStatus("");
|
||||
ctx.actions.requestRender();
|
||||
const accepted = await new Promise<boolean>((resolve) => {
|
||||
ctx.stack.push(
|
||||
new EvenHubPermissionDialogLayer(
|
||||
manifest.name,
|
||||
manifest.permissions,
|
||||
privacyPolicyUrl,
|
||||
() => resolve(true),
|
||||
() => resolve(false),
|
||||
),
|
||||
);
|
||||
});
|
||||
if (!accepted) return null;
|
||||
}
|
||||
|
||||
options.onStatus("Installing...");
|
||||
ctx.actions.requestRender();
|
||||
// A running instance would keep serving the old unpacked runtime; every
|
||||
// launch re-unpacks from the stored EHPK, so closing it is enough.
|
||||
closeRunningPackage(app.packageId);
|
||||
const result = installEvenHubPackageBytes(download.bytes, { expectedPackageId: app.packageId, icon });
|
||||
options.appendLog(`evenhub store: installed ${result.packageId} ${result.version} (${download.bytes.length} bytes)`);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the installed package already declares every permission the new
|
||||
* manifest asks for. (The store's privacy-policy link is not compared: the
|
||||
* download metadata carries it as a CDN path while the installed app.json
|
||||
* usually has none, so comparing would re-prompt on every update.)
|
||||
*/
|
||||
function alreadyGranted(packageId: string, manifest: EvenHubManifest): boolean {
|
||||
const current = readEvenHubPackageManifest(installedEvenHubPackagePath(packageId));
|
||||
if (!current) return false;
|
||||
const granted = new Set(current.permissions);
|
||||
return manifest.permissions.every((permission) => granted.has(permission));
|
||||
}
|
||||
|
||||
export function cleanError(error: unknown): string {
|
||||
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
+594
-128
@@ -3,39 +3,119 @@ import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerActions, type LayerContext } from "../../ui/layers";
|
||||
import { type Layer, type LayerActions, type LayerContext, type LayerStack } from "../../ui/layers";
|
||||
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { evenHubApi, EvenHubAuthenticationError, isEvenHubStoreConfigured, type EvenHubStoreApp } from "./even-api";
|
||||
import { ConfigSettingString } from "../../ui/dashboard-settings";
|
||||
import { evenHubApi, EvenHubAuthenticationError, isEvenHubStoreConfigured, type EvenHubStoreApp, type EvenHubStorePage } from "./even-api";
|
||||
import { clearEvenHubLoginForm, clearEvenHubSession, clearTransientEvenHubSession, evenHubLoginEmailSetting, evenHubLoginPasswordSetting, evenHubRememberMeSetting, resetEvenHubLoginForm } from "./credentials";
|
||||
import { getInstalledEvenHubApps, uninstallEvenHubPackage, type InstalledEvenHubApp } from "./installed-apps";
|
||||
import { EvenHubStoreDetailLayer } from "./store-detail-layer";
|
||||
import { cleanError, installStoreApp } from "./store-install";
|
||||
import { checkForEvenHubUpdates, describeUpdate, type EvenHubAppUpdate } from "./updates";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
|
||||
const LIST_X = 18;
|
||||
/** Horizontal gap between tab labels. */
|
||||
const TAB_GAP = 18;
|
||||
|
||||
/** Header: title line, then the status/subtitle line, then a small gap. */
|
||||
/** Header: title + tabs line, then the status/subtitle line, then a small gap. */
|
||||
function headerHeight(font: UiFont): number {
|
||||
return 8 + lineStep(font) + font.lineHeight + 6;
|
||||
}
|
||||
|
||||
/** Last search typed on the phone; kept so reopening the store shows it again. */
|
||||
export const evenHubSearchQuerySetting = new ConfigSettingString({
|
||||
id: "evenhub-search-query",
|
||||
label: "Search",
|
||||
storageKey: "evenhub.store.searchQuery",
|
||||
defaultValue: "",
|
||||
editorTitle: "Search EvenHub",
|
||||
glassesEditTitle: "Search EvenHub",
|
||||
normalize: (value) => (value ?? "").replace(/\s+/g, " ").trim(),
|
||||
});
|
||||
|
||||
type StoreTab = "top" | "new" | "search" | "updates";
|
||||
|
||||
const TABS: { id: StoreTab; label: string }[] = [
|
||||
{ id: "top", label: "Top" },
|
||||
{ id: "new", label: "New" },
|
||||
{ id: "search", label: "Search" },
|
||||
{ id: "updates", label: "Updates" },
|
||||
];
|
||||
|
||||
type StoreRow =
|
||||
/** A storefront app; `update` is set on the Updates tab. */
|
||||
| { kind: "app"; app: EvenHubStoreApp; update?: EvenHubAppUpdate }
|
||||
/** The Search tab's first row: shows the query and opens the phone editor. */
|
||||
| { kind: "search-query" }
|
||||
/** A registry entry whose package is gone and which the store no longer lists. */
|
||||
| { kind: "orphan"; update: EvenHubAppUpdate };
|
||||
|
||||
type TabState = {
|
||||
rows: StoreRow[];
|
||||
/** Server-reported total, or 0 when unknown. */
|
||||
total: number;
|
||||
nextPage: number;
|
||||
/** No further pages: the total was reached, or a page added nothing new. */
|
||||
exhausted: boolean;
|
||||
/** A load has completed (or failed) at least once. */
|
||||
loaded: boolean;
|
||||
loading: boolean;
|
||||
/** Progress or error text for the subtitle line. */
|
||||
status: string;
|
||||
selectedIndex: number;
|
||||
scrollRow: number;
|
||||
/** Search only: the query the rows belong to. */
|
||||
query: string;
|
||||
};
|
||||
|
||||
function emptyTab(): TabState {
|
||||
return {
|
||||
rows: [],
|
||||
total: 0,
|
||||
nextPage: 1,
|
||||
exhausted: false,
|
||||
loaded: false,
|
||||
loading: false,
|
||||
status: "",
|
||||
selectedIndex: 0,
|
||||
scrollRow: 0,
|
||||
query: "",
|
||||
};
|
||||
}
|
||||
|
||||
export type EvenHubStoreLayerOptions = {
|
||||
launchApp: (appId: string) => Promise<void> | void;
|
||||
appendLog: (message: string) => void;
|
||||
};
|
||||
|
||||
/** Browse the public EvenHub leaderboard and download one package to run. */
|
||||
/**
|
||||
* The EvenHub storefront: Top (leaderboard), New (by first publication),
|
||||
* Search, and Updates (installed apps that are stale or whose package is
|
||||
* missing). The tab row is its own focus level above the list: double-click
|
||||
* in the list focuses the tabs, scrolling there switches tabs, and a click
|
||||
* returns to the list.
|
||||
*/
|
||||
export class EvenHubStoreLayer implements Layer {
|
||||
private apps: EvenHubStoreApp[] = [];
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
private total = 0;
|
||||
private nextPage = 1;
|
||||
private readonly tabs: Record<StoreTab, TabState> = {
|
||||
top: emptyTab(),
|
||||
new: emptyTab(),
|
||||
search: emptyTab(),
|
||||
updates: emptyTab(),
|
||||
};
|
||||
private activeTab: StoreTab = "top";
|
||||
private focus: "list" | "tabs" = "list";
|
||||
private started = false;
|
||||
private loading = false;
|
||||
private showingLogin = !isEvenHubStoreConfigured();
|
||||
private status = this.showingLogin ? "Sign in to browse public apps." : "";
|
||||
private credentialEditorOpen = false;
|
||||
private loginStatus = this.showingLogin ? "Sign in to browse public apps." : "";
|
||||
private loginBusy = false;
|
||||
private phoneEditor: "none" | "credentials" | "search" = "none";
|
||||
private closed = false;
|
||||
/** Serializes the Update All action; also blocks per-row installs meanwhile. */
|
||||
private updatingAll = false;
|
||||
/** An app page requested while the login form was up; opened once signed in. */
|
||||
private pendingPackage: InstalledEvenHubApp | null = null;
|
||||
|
||||
constructor(private readonly options: EvenHubStoreLayerOptions) {
|
||||
if (this.showingLogin) resetEvenHubLoginForm();
|
||||
@@ -47,7 +127,7 @@ export class EvenHubStoreLayer implements Layer {
|
||||
if (this.showingLogin) {
|
||||
this.openCredentialEditor(ctx);
|
||||
} else {
|
||||
void this.reload(ctx);
|
||||
void this.ensureTabLoaded(ctx, this.activeTab);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,11 +137,9 @@ export class EvenHubStoreLayer implements Layer {
|
||||
const headerH = headerHeight(font);
|
||||
image.drawText(font, LIST_X, 8, "EvenHub", 220);
|
||||
|
||||
const subtitle = this.status || (this.total ? `${this.apps.length} of ${this.total} public apps` : "Public apps");
|
||||
image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, subtitle, width - LIST_X * 2), 125);
|
||||
|
||||
if (this.showingLogin) {
|
||||
const message = this.loading
|
||||
image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, this.loginStatus, width - LIST_X * 2), 125);
|
||||
const message = this.loginBusy
|
||||
? "Signing in..."
|
||||
: "Enter your Even account email and password in the phone app.";
|
||||
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).entries()) {
|
||||
@@ -71,69 +149,181 @@ export class EvenHubStoreLayer implements Layer {
|
||||
return image;
|
||||
}
|
||||
|
||||
if (this.apps.length === 0) {
|
||||
const message = this.loading ? "Loading apps..." : this.status || "No apps returned.";
|
||||
image.drawText(font, LIST_X, headerH + 22, truncateText(font, message, width - LIST_X * 2), 190);
|
||||
} else {
|
||||
this.selectedIndex = clamp(this.selectedIndex, 0, this.apps.length - 1);
|
||||
const rowH = Math.max(30, 2 * lineStep(font) + 4);
|
||||
const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH));
|
||||
this.scrollRow = scrollToKeepSelectionVisible(
|
||||
this.scrollRow,
|
||||
this.selectedIndex,
|
||||
visibleRows,
|
||||
this.apps.length,
|
||||
);
|
||||
const last = Math.min(this.apps.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < last; index++) {
|
||||
const app = this.apps[index]!;
|
||||
const y = headerH + (index - this.scrollRow) * rowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y, width - LIST_X * 2 + 12, rowH - 2, ctx.stack.isFocused(), 5);
|
||||
}
|
||||
image.drawText(font, LIST_X, y + 2, truncateText(font, app.name, width - LIST_X * 2), 215);
|
||||
const detail = app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`;
|
||||
image.drawText(font, LIST_X, y + 2 + lineStep(font), truncateText(font, detail, width - LIST_X * 2), 115);
|
||||
}
|
||||
if (this.apps.length > visibleRows) {
|
||||
drawListScrollbar(image, width - 5, headerH, visibleRows * rowH - 3, this.scrollRow, visibleRows, this.apps.length);
|
||||
this.paintTabs(image, font, ctx);
|
||||
|
||||
const tab = this.tabs[this.activeTab];
|
||||
const subtitle = tab.status || this.subtitle(this.activeTab, tab);
|
||||
image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, subtitle, width - LIST_X * 2), 125);
|
||||
|
||||
if (tab.rows.length === 0) {
|
||||
const message = tab.loading ? "Loading..." : this.emptyMessage(this.activeTab, tab);
|
||||
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).slice(0, 3).entries()) {
|
||||
image.drawText(font, LIST_X, headerH + 22 + index * lineStep(font), line, 190);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
tab.selectedIndex = clamp(tab.selectedIndex, 0, tab.rows.length - 1);
|
||||
const rowH = Math.max(30, 2 * lineStep(font) + 4);
|
||||
const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH));
|
||||
tab.scrollRow = scrollToKeepSelectionVisible(tab.scrollRow, tab.selectedIndex, visibleRows, tab.rows.length);
|
||||
const last = Math.min(tab.rows.length, tab.scrollRow + visibleRows);
|
||||
const listFocused = ctx.stack.isFocused() && this.focus === "list";
|
||||
for (let index = tab.scrollRow; index < last; index++) {
|
||||
const row = tab.rows[index]!;
|
||||
const y = headerH + (index - tab.scrollRow) * rowH;
|
||||
if (index === tab.selectedIndex) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y, width - LIST_X * 2 + 12, rowH - 2, listFocused, 5);
|
||||
}
|
||||
const { title, detail, titleValue } = this.rowText(row);
|
||||
image.drawText(font, LIST_X, y + 2, truncateText(font, title, width - LIST_X * 2), titleValue);
|
||||
image.drawText(font, LIST_X, y + 2 + lineStep(font), truncateText(font, detail, width - LIST_X * 2), 115);
|
||||
}
|
||||
if (tab.rows.length > visibleRows) {
|
||||
drawListScrollbar(image, width - 5, headerH, visibleRows * rowH - 3, tab.scrollRow, visibleRows, tab.rows.length);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Tab labels to the right of the title; the active one is bright and underlined. */
|
||||
private paintTabs(image: GrayImage, font: UiFont, ctx: LayerContext): void {
|
||||
const tabsFocused = ctx.stack.isFocused() && this.focus === "tabs";
|
||||
let x = LIST_X + font.measureText("EvenHub") + 28;
|
||||
for (const tab of TABS) {
|
||||
const label = this.tabLabel(tab.id);
|
||||
const w = font.measureText(label);
|
||||
const active = tab.id === this.activeTab;
|
||||
if (active) {
|
||||
if (tabsFocused) {
|
||||
drawSelectionHighlight(image, x - 6, 5, w + 12, font.lineHeight + 6, true, 4);
|
||||
} else {
|
||||
image.fillRect(x, 8 + font.lineHeight + 1, w, 1, 150);
|
||||
}
|
||||
}
|
||||
image.drawText(font, x, 8, label, active ? 235 : 120);
|
||||
x += w + TAB_GAP;
|
||||
}
|
||||
}
|
||||
|
||||
private tabLabel(tab: StoreTab): string {
|
||||
const label = TABS.find((entry) => entry.id === tab)!.label;
|
||||
if (tab !== "updates") return label;
|
||||
const updates = this.tabs.updates;
|
||||
const count = updates.rows.filter((row) => row.kind !== "search-query").length;
|
||||
return updates.loaded && count > 0 ? `${label} (${count})` : label;
|
||||
}
|
||||
|
||||
private subtitle(tab: StoreTab, state: TabState): string {
|
||||
const count = state.rows.length;
|
||||
switch (tab) {
|
||||
case "top":
|
||||
return state.total ? `${count} of ${state.total} public apps` : count ? `${count} public apps` : "Public apps by popularity";
|
||||
case "new":
|
||||
return count ? `${count} newest apps` : "Recently published apps";
|
||||
case "search": {
|
||||
const results = count - 1;
|
||||
if (!state.query) return "Search public apps";
|
||||
return results === 1 ? `1 result for "${state.query}"` : `${results} results for "${state.query}"`;
|
||||
}
|
||||
case "updates":
|
||||
if (!state.loaded) return "Installed apps with newer versions";
|
||||
return count === 0 ? "All installed apps are up to date" : count === 1 ? "1 app needs attention" : `${count} apps need attention`;
|
||||
}
|
||||
}
|
||||
|
||||
private emptyMessage(tab: StoreTab, state: TabState): string {
|
||||
if (state.status) return state.status;
|
||||
switch (tab) {
|
||||
case "updates":
|
||||
return getInstalledEvenHubApps().length === 0
|
||||
? "No apps are installed from EvenHub."
|
||||
: "Every installed app is at its latest version.";
|
||||
default:
|
||||
return "No apps returned.";
|
||||
}
|
||||
}
|
||||
|
||||
private rowText(row: StoreRow): { title: string; detail: string; titleValue: number } {
|
||||
switch (row.kind) {
|
||||
case "search-query": {
|
||||
const query = evenHubSearchQuerySetting.get();
|
||||
return {
|
||||
title: query ? `Search: ${query}` : "Search: (enter a query)",
|
||||
detail: "Click to type on the phone, or use voice input.",
|
||||
titleValue: 200,
|
||||
};
|
||||
}
|
||||
case "orphan":
|
||||
return {
|
||||
title: row.update.installed.name,
|
||||
detail: row.update.error
|
||||
? `Package missing · store lookup failed: ${row.update.error}`
|
||||
: "Package missing · not in the store · click to forget",
|
||||
titleValue: 170,
|
||||
};
|
||||
case "app": {
|
||||
const { app, update } = row;
|
||||
if (!update) {
|
||||
return {
|
||||
title: app.name,
|
||||
detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`,
|
||||
titleValue: 215,
|
||||
};
|
||||
}
|
||||
const detail = update.packageMissing
|
||||
? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim()
|
||||
: `Version ${update.installed.version} installed · ${app.version} available`;
|
||||
return { title: app.name, detail, titleValue: 215 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.showingLogin) {
|
||||
if (event.type === "click" && !this.loading) {
|
||||
if (event.type === "click" && !this.loginBusy) {
|
||||
this.openCredentialEditor(ctx);
|
||||
} else if (event.type === "double-click") {
|
||||
shell.yieldFocusToSidebar();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (this.focus === "tabs") {
|
||||
await this.handleTabInput(event, ctx);
|
||||
return;
|
||||
}
|
||||
const tab = this.tabs[this.activeTab];
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
tab.selectedIndex = Math.max(0, tab.selectedIndex - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = Math.min(Math.max(0, this.apps.length - 1), this.selectedIndex + 1);
|
||||
if (this.selectedIndex >= this.apps.length - 4 && this.apps.length < this.total) {
|
||||
void this.loadNextPage(ctx);
|
||||
}
|
||||
tab.selectedIndex = Math.min(Math.max(0, tab.rows.length - 1), tab.selectedIndex + 1);
|
||||
if (tab.selectedIndex >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab);
|
||||
return;
|
||||
case "click":
|
||||
if (!this.loading) {
|
||||
const app = this.apps[this.selectedIndex];
|
||||
if (app) {
|
||||
ctx.stack.push(
|
||||
new EvenHubStoreDetailLayer(app, {
|
||||
launchApp: this.options.launchApp,
|
||||
appendLog: this.options.appendLog,
|
||||
}),
|
||||
);
|
||||
}
|
||||
await this.activateRow(ctx, tab.rows[tab.selectedIndex]);
|
||||
return;
|
||||
case "double-click":
|
||||
this.focus = "tabs";
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private async handleTabInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
const index = TABS.findIndex((tab) => tab.id === this.activeTab);
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.switchTab(ctx, TABS[Math.max(0, index - 1)]!.id);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.switchTab(ctx, TABS[Math.min(TABS.length - 1, index + 1)]!.id);
|
||||
return;
|
||||
case "click":
|
||||
this.focus = "list";
|
||||
if (this.activeTab === "search" && !evenHubSearchQuerySetting.get()) {
|
||||
this.openSearchEditor(ctx);
|
||||
}
|
||||
return;
|
||||
case "double-click":
|
||||
@@ -144,107 +334,366 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
buildMenuItems(): MenuItem[] {
|
||||
if (this.showingLogin) return [];
|
||||
return [
|
||||
{
|
||||
label: "Refresh",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
void this.reload(ctx);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Log Out",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
clearEvenHubSession();
|
||||
this.enterLogin(ctx, "Signed out.");
|
||||
},
|
||||
},
|
||||
];
|
||||
private switchTab(ctx: LayerContext, tab: StoreTab): void {
|
||||
if (tab === this.activeTab) return;
|
||||
if (this.activeTab === "search") this.closePhoneEditor(ctx.actions, "search");
|
||||
this.activeTab = tab;
|
||||
void this.ensureTabLoaded(ctx, tab);
|
||||
}
|
||||
|
||||
closeCredentialEditor(actions: Pick<LayerActions, "endTextSettingEdit">): void {
|
||||
if (!this.credentialEditorOpen) return;
|
||||
this.credentialEditorOpen = false;
|
||||
void actions.endTextSettingEdit();
|
||||
private async activateRow(ctx: LayerContext, row: StoreRow | undefined): Promise<void> {
|
||||
if (!row) return;
|
||||
switch (row.kind) {
|
||||
case "search-query":
|
||||
this.openSearchEditor(ctx);
|
||||
return;
|
||||
case "orphan": {
|
||||
// Nothing can be reinstalled; the only useful action is to drop the
|
||||
// dead registry entry so the launcher stops offering it.
|
||||
const { packageId, name } = row.update.installed;
|
||||
if (uninstallEvenHubPackage(packageId)) {
|
||||
this.options.appendLog(`evenhub store: forgot missing package ${packageId}`);
|
||||
this.removeUpdateRow(packageId);
|
||||
this.tabs.updates.status = `Forgot ${name}.`;
|
||||
}
|
||||
return;
|
||||
}
|
||||
case "app":
|
||||
if (this.updatingAll) return;
|
||||
ctx.stack.push(
|
||||
new EvenHubStoreDetailLayer(row.app, {
|
||||
launchApp: this.options.launchApp,
|
||||
appendLog: this.options.appendLog,
|
||||
onInstalled: (installed) => this.onInstalled(installed),
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Jump to an installed app's store page (the launcher's route for a package
|
||||
* whose file is missing). Any page already open above the list is replaced.
|
||||
* Store metadata is filled in by the detail page itself; until then a
|
||||
* placeholder built from the registry entry stands in.
|
||||
*/
|
||||
showInstalledPackage(stack: LayerStack, installed: InstalledEvenHubApp): void {
|
||||
if (this.showingLogin) {
|
||||
this.pendingPackage = installed;
|
||||
return;
|
||||
}
|
||||
this.pendingPackage = null;
|
||||
stack.clearToBase();
|
||||
stack.push(
|
||||
new EvenHubStoreDetailLayer(placeholderStoreApp(installed), {
|
||||
launchApp: this.options.launchApp,
|
||||
appendLog: this.options.appendLog,
|
||||
onInstalled: (result) => this.onInstalled(result),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/** Dictated text becomes a search (the shell's "Type Into App" / voice input). */
|
||||
receiveTextInput(text: string, ctx: LayerContext): void {
|
||||
if (this.showingLogin) return;
|
||||
const query = evenHubSearchQuerySetting.set(text);
|
||||
if (!query) return;
|
||||
this.closePhoneEditor(ctx.actions, "search");
|
||||
this.activeTab = "search";
|
||||
this.focus = "list";
|
||||
void this.runSearch(ctx, query);
|
||||
}
|
||||
|
||||
buildMenuItems(): MenuItem[] {
|
||||
if (this.showingLogin) return [];
|
||||
const items: MenuItem[] = [];
|
||||
if (this.activeTab === "search") {
|
||||
items.push({
|
||||
label: "Search...",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
this.focus = "list";
|
||||
this.openSearchEditor(ctx);
|
||||
},
|
||||
});
|
||||
}
|
||||
if (this.activeTab === "updates" && this.updatableRows().length > 0) {
|
||||
items.push({
|
||||
label: this.updatingAll ? "Updating..." : "Update All",
|
||||
disabled: () => this.updatingAll,
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
void this.updateAll(ctx);
|
||||
},
|
||||
});
|
||||
}
|
||||
items.push({
|
||||
label: this.activeTab === "updates" ? "Check Again" : "Refresh",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
void this.reload(ctx, this.activeTab);
|
||||
},
|
||||
});
|
||||
items.push({
|
||||
label: "Log Out",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
clearEvenHubSession();
|
||||
this.enterLogin(ctx, "Signed out.");
|
||||
},
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
onWindowClosed(actions: Pick<LayerActions, "endTextSettingEdit">): void {
|
||||
this.closed = true;
|
||||
this.closeCredentialEditor(actions);
|
||||
this.closePhoneEditor(actions);
|
||||
clearTransientEvenHubSession();
|
||||
}
|
||||
|
||||
private async reload(ctx: LayerContext): Promise<void> {
|
||||
if (this.loading) return;
|
||||
this.apps = [];
|
||||
this.total = 0;
|
||||
this.nextPage = 1;
|
||||
this.selectedIndex = 0;
|
||||
this.scrollRow = 0;
|
||||
this.status = "";
|
||||
// ---- loading -----------------------------------------------------------
|
||||
|
||||
private ensureTabLoaded(ctx: LayerContext, tab: StoreTab): Promise<void> {
|
||||
const state = this.tabs[tab];
|
||||
if (state.loaded || state.loading) return Promise.resolve();
|
||||
return this.reload(ctx, tab);
|
||||
}
|
||||
|
||||
private async reload(ctx: LayerContext, tab: StoreTab): Promise<void> {
|
||||
if (!isEvenHubStoreConfigured()) {
|
||||
this.enterLogin(ctx);
|
||||
return;
|
||||
}
|
||||
await this.loadNextPage(ctx);
|
||||
if (this.tabs[tab].loading) return;
|
||||
switch (tab) {
|
||||
case "top":
|
||||
case "new":
|
||||
this.tabs[tab] = emptyTab();
|
||||
await this.loadNextPage(ctx, tab);
|
||||
return;
|
||||
case "search":
|
||||
await this.runSearch(ctx, evenHubSearchQuerySetting.get());
|
||||
return;
|
||||
case "updates":
|
||||
await this.checkUpdates(ctx);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private async loadNextPage(ctx: LayerContext): Promise<void> {
|
||||
if (this.loading || (this.total > 0 && this.apps.length >= this.total)) return;
|
||||
this.loading = true;
|
||||
this.status = this.apps.length ? "Loading more apps..." : "Loading apps...";
|
||||
private async loadNextPage(ctx: LayerContext, tab: StoreTab): Promise<void> {
|
||||
const state = this.tabs[tab];
|
||||
if (state.loading || state.exhausted) return;
|
||||
if (tab === "search" && !state.query) return;
|
||||
if (tab === "updates") return;
|
||||
state.loading = true;
|
||||
state.status = state.rows.length ? "Loading more apps..." : "Loading apps...";
|
||||
ctx.actions.requestRender();
|
||||
try {
|
||||
const page = await evenHubApi.listApps(this.nextPage);
|
||||
const seen = new Set(this.apps.map((app) => app.packageId));
|
||||
this.apps.push(...page.apps.filter((app) => !seen.has(app.packageId)));
|
||||
this.total = page.total;
|
||||
this.nextPage = page.page + 1;
|
||||
this.status = "";
|
||||
} catch (error) {
|
||||
const message = cleanError(error);
|
||||
if (error instanceof EvenHubAuthenticationError) {
|
||||
this.options.appendLog(`evenhub store login failed: ${message}`);
|
||||
this.enterLogin(ctx, `Login failed: ${message}`);
|
||||
return;
|
||||
const page = await this.fetchPage(tab, state.nextPage, state.query);
|
||||
const seen = new Set(state.rows.map((row) => (row.kind === "app" ? row.app.packageId : "")));
|
||||
const fresh = page.apps.filter((app) => !seen.has(app.packageId));
|
||||
state.rows.push(...fresh.map((app): StoreRow => ({ kind: "app", app })));
|
||||
state.total = page.total;
|
||||
state.nextPage = page.page + 1;
|
||||
// A page that adds nothing (or a short page with no known total) means
|
||||
// the endpoint has no more to give — or ignores paging altogether.
|
||||
state.exhausted = (state.total > 0 && state.rows.length >= state.total)
|
||||
|| fresh.length === 0
|
||||
|| (state.total === 0 && page.apps.length < page.pageSize);
|
||||
state.status = "";
|
||||
if (tab === "top" && state.nextPage === 2 && !this.tabs.updates.loaded && !this.tabs.updates.loading) {
|
||||
// Quietly find out whether the Updates tab deserves a count.
|
||||
void this.checkUpdates(ctx, true);
|
||||
}
|
||||
this.status = message;
|
||||
this.options.appendLog(`evenhub store: ${this.status}`);
|
||||
} catch (error) {
|
||||
if (this.handleAuthError(ctx, error)) return;
|
||||
state.status = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: ${state.status}`);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
state.loaded = true;
|
||||
state.loading = false;
|
||||
if (!this.closed) ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
private fetchPage(tab: StoreTab, page: number, query: string): Promise<EvenHubStorePage> {
|
||||
switch (tab) {
|
||||
case "new":
|
||||
return evenHubApi.listNewApps(page);
|
||||
case "search":
|
||||
return evenHubApi.searchApps(query, page);
|
||||
default:
|
||||
return evenHubApi.listApps(page);
|
||||
}
|
||||
}
|
||||
|
||||
private async runSearch(ctx: LayerContext, query: string): Promise<void> {
|
||||
const state = emptyTab();
|
||||
state.query = query;
|
||||
state.rows = [{ kind: "search-query" }];
|
||||
state.loaded = !query;
|
||||
// Keep the cursor on the first result once there are some.
|
||||
state.selectedIndex = query ? 1 : 0;
|
||||
this.tabs.search = state;
|
||||
ctx.actions.requestRender();
|
||||
if (query) await this.loadNextPage(ctx, "search");
|
||||
}
|
||||
|
||||
private async checkUpdates(ctx: LayerContext, quiet = false): Promise<void> {
|
||||
const state = this.tabs.updates;
|
||||
if (state.loading) return;
|
||||
state.loading = true;
|
||||
state.status = "";
|
||||
const installedCount = getInstalledEvenHubApps().length;
|
||||
if (!quiet) {
|
||||
state.status = installedCount ? `Checking ${installedCount} installed apps...` : "";
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
try {
|
||||
const updates = await checkForEvenHubUpdates((done, total) => {
|
||||
if (quiet) return;
|
||||
state.status = `Checking installed apps (${done}/${total})...`;
|
||||
ctx.actions.requestRender();
|
||||
});
|
||||
const rows: StoreRow[] = updates.map((update) =>
|
||||
update.latest ? { kind: "app", app: update.latest, update } : { kind: "orphan", update },
|
||||
);
|
||||
state.rows = rows;
|
||||
state.selectedIndex = 0;
|
||||
state.scrollRow = 0;
|
||||
state.status = "";
|
||||
state.exhausted = true;
|
||||
} catch (error) {
|
||||
if (this.handleAuthError(ctx, error)) return;
|
||||
state.status = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: update check failed: ${state.status}`);
|
||||
} finally {
|
||||
state.loaded = true;
|
||||
state.loading = false;
|
||||
if (!this.closed) ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
private updatableRows(): Extract<StoreRow, { kind: "app" }>[] {
|
||||
return this.tabs.updates.rows.filter(
|
||||
(row): row is Extract<StoreRow, { kind: "app" }> => row.kind === "app" && row.update !== undefined,
|
||||
);
|
||||
}
|
||||
|
||||
/** Install every pending update in turn; a declined permission dialog skips that app. */
|
||||
private async updateAll(ctx: LayerContext): Promise<void> {
|
||||
if (this.updatingAll) return;
|
||||
this.updatingAll = true;
|
||||
const state = this.tabs.updates;
|
||||
const pending = this.updatableRows();
|
||||
let installed = 0;
|
||||
const failures: string[] = [];
|
||||
try {
|
||||
for (const [index, row] of pending.entries()) {
|
||||
if (this.closed) return;
|
||||
const prefix = `${index + 1}/${pending.length} ${row.app.name}: `;
|
||||
try {
|
||||
const result = await installStoreApp(ctx, row.app, {
|
||||
appendLog: this.options.appendLog,
|
||||
onStatus: (status) => {
|
||||
state.status = prefix + (status || "Confirm permissions on the glasses...");
|
||||
ctx.actions.requestRender();
|
||||
},
|
||||
});
|
||||
if (result) {
|
||||
installed++;
|
||||
this.onInstalled(result);
|
||||
} else {
|
||||
failures.push(`${row.app.name} (declined)`);
|
||||
}
|
||||
} catch (error) {
|
||||
const message = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: update of ${row.app.packageId} failed: ${message}`);
|
||||
failures.push(row.app.name);
|
||||
}
|
||||
}
|
||||
state.status = failures.length
|
||||
? `Updated ${installed}; skipped ${failures.join(", ")}`
|
||||
: installed === 1 ? "Updated 1 app." : `Updated ${installed} apps.`;
|
||||
} finally {
|
||||
this.updatingAll = false;
|
||||
if (!this.closed) ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
/** After any install, drop the Updates row it satisfied (from the detail page or Update All). */
|
||||
private onInstalled(installed: InstalledEvenHubApp): void {
|
||||
const row = this.tabs.updates.rows.find(
|
||||
(entry) => entry.kind === "app" && entry.app.packageId === installed.packageId,
|
||||
);
|
||||
if (row?.kind !== "app" || !row.update) return;
|
||||
const standing = describeUpdate(installed, row.app);
|
||||
if (!standing.updateAvailable && !standing.packageMissing) this.removeUpdateRow(installed.packageId);
|
||||
}
|
||||
|
||||
private removeUpdateRow(packageId: string): void {
|
||||
const state = this.tabs.updates;
|
||||
state.rows = state.rows.filter((row) =>
|
||||
row.kind === "search-query" ? true : row.kind === "app" ? row.app.packageId !== packageId : row.update.installed.packageId !== packageId,
|
||||
);
|
||||
state.selectedIndex = clamp(state.selectedIndex, 0, Math.max(0, state.rows.length - 1));
|
||||
}
|
||||
|
||||
// ---- login and phone editors --------------------------------------------
|
||||
|
||||
/** Route a rejected session back to the login form; true if it was one. */
|
||||
private handleAuthError(ctx: LayerContext, error: unknown): boolean {
|
||||
if (!(error instanceof EvenHubAuthenticationError)) return false;
|
||||
const message = cleanError(error);
|
||||
this.options.appendLog(`evenhub store login failed: ${message}`);
|
||||
this.enterLogin(ctx, `Login failed: ${message}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
private enterLogin(ctx: LayerContext, status = "Sign in to browse public apps."): void {
|
||||
const wasShowingLogin = this.showingLogin;
|
||||
this.closePhoneEditor(ctx.actions, "search");
|
||||
this.showingLogin = true;
|
||||
this.loading = false;
|
||||
this.apps = [];
|
||||
this.total = 0;
|
||||
this.status = status;
|
||||
this.loginBusy = false;
|
||||
this.loginStatus = status;
|
||||
for (const tab of TABS) this.tabs[tab.id] = emptyTab();
|
||||
this.focus = "list";
|
||||
if (!wasShowingLogin) resetEvenHubLoginForm();
|
||||
this.openCredentialEditor(ctx);
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
|
||||
private openCredentialEditor(ctx: LayerContext): void {
|
||||
if (this.credentialEditorOpen) return;
|
||||
this.credentialEditorOpen = true;
|
||||
if (this.phoneEditor !== "none") return;
|
||||
this.phoneEditor = "credentials";
|
||||
void ctx.actions.startTextSettingsEdit(
|
||||
[evenHubLoginEmailSetting, evenHubLoginPasswordSetting],
|
||||
"Sign in to EvenHub",
|
||||
() => {
|
||||
this.credentialEditorOpen = false;
|
||||
this.phoneEditor = "none";
|
||||
void this.submitCredentials(ctx);
|
||||
},
|
||||
{ setting: evenHubRememberMeSetting, label: "Remember me" },
|
||||
);
|
||||
}
|
||||
|
||||
private openSearchEditor(ctx: LayerContext): void {
|
||||
if (this.phoneEditor !== "none") return;
|
||||
this.phoneEditor = "search";
|
||||
void ctx.actions.startTextSettingsEdit([evenHubSearchQuerySetting], "Search EvenHub", () => {
|
||||
this.phoneEditor = "none";
|
||||
if (this.closed) return;
|
||||
void this.runSearch(ctx, evenHubSearchQuerySetting.get());
|
||||
});
|
||||
}
|
||||
|
||||
/** Close the phone editor if one is open (optionally only a given kind). */
|
||||
private closePhoneEditor(actions: Pick<LayerActions, "endTextSettingEdit">, kind?: "credentials" | "search"): void {
|
||||
if (this.phoneEditor === "none" || (kind && this.phoneEditor !== kind)) return;
|
||||
this.phoneEditor = "none";
|
||||
void actions.endTextSettingEdit();
|
||||
}
|
||||
|
||||
private async submitCredentials(ctx: LayerContext): Promise<void> {
|
||||
const email = evenHubLoginEmailSetting.get();
|
||||
const password = evenHubLoginPasswordSetting.get();
|
||||
@@ -252,8 +701,8 @@ export class EvenHubStoreLayer implements Layer {
|
||||
this.enterLogin(ctx, "Email and password are required.");
|
||||
return;
|
||||
}
|
||||
this.loading = true;
|
||||
this.status = "Signing in...";
|
||||
this.loginBusy = true;
|
||||
this.loginStatus = "Signing in...";
|
||||
ctx.actions.requestRender();
|
||||
try {
|
||||
await evenHubApi.signIn(email, password, evenHubRememberMeSetting.get());
|
||||
@@ -263,26 +712,43 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
this.showingLogin = false;
|
||||
clearEvenHubLoginForm();
|
||||
this.loading = false;
|
||||
await this.reload(ctx);
|
||||
this.loginBusy = false;
|
||||
const pending = this.pendingPackage;
|
||||
if (pending) this.showInstalledPackage(ctx.stack, pending);
|
||||
await this.reload(ctx, this.activeTab);
|
||||
} catch (error) {
|
||||
if (this.closed) return;
|
||||
const message = cleanError(error);
|
||||
this.options.appendLog(`evenhub store login failed: ${message}`);
|
||||
this.status = `Login failed: ${message}`;
|
||||
this.loginStatus = `Login failed: ${message}`;
|
||||
evenHubLoginPasswordSetting.set("");
|
||||
this.openCredentialEditor(ctx);
|
||||
} finally {
|
||||
evenHubLoginPasswordSetting.set("");
|
||||
this.loading = false;
|
||||
this.loginBusy = false;
|
||||
if (!this.closed) ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function cleanError(error: unknown): string {
|
||||
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
|
||||
/** A registry entry dressed as a store record, for pages opened before the store record arrives. */
|
||||
function placeholderStoreApp(installed: InstalledEvenHubApp): EvenHubStoreApp {
|
||||
return {
|
||||
id: 0,
|
||||
packageId: installed.packageId,
|
||||
name: installed.name,
|
||||
creatorName: "",
|
||||
tagline: "",
|
||||
description: "",
|
||||
categories: [],
|
||||
installCount: 0,
|
||||
likeCount: 0,
|
||||
firstPublishedAt: "",
|
||||
iconPath: "",
|
||||
version: "",
|
||||
changelog: "",
|
||||
fileSize: 0,
|
||||
};
|
||||
}
|
||||
|
||||
function formatCount(value: number): string {
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Update detection for installed EvenHub apps.
|
||||
*
|
||||
* Two situations need the user's attention: the store has a newer version
|
||||
* than the one installed, and the registry lists a package whose EHPK is no
|
||||
* longer on disk (a settings export imported after a reinstall carries the
|
||||
* registry but not the files).
|
||||
*/
|
||||
import { statPath } from "../../native/file-access";
|
||||
import { evenHubApi, EvenHubAuthenticationError, type EvenHubStoreApp } from "./even-api";
|
||||
import {
|
||||
getInstalledEvenHubApps,
|
||||
installedEvenHubPackagePath,
|
||||
type InstalledEvenHubApp,
|
||||
} from "./installed-apps";
|
||||
|
||||
export type EvenHubAppUpdate = {
|
||||
installed: InstalledEvenHubApp;
|
||||
/** Storefront record, or null when the store no longer lists the package. */
|
||||
latest: EvenHubStoreApp | null;
|
||||
/** The registry entry has no package file behind it. */
|
||||
packageMissing: boolean;
|
||||
/** The store's version is newer than the installed one. */
|
||||
updateAvailable: boolean;
|
||||
/** Why the store lookup failed, when it did. */
|
||||
error?: string;
|
||||
};
|
||||
|
||||
/** Whether an installed package's EHPK is actually present on disk. */
|
||||
export function isInstalledPackagePresent(packageId: string): boolean {
|
||||
const entry = statPath(installedEvenHubPackagePath(packageId));
|
||||
return entry !== null && !entry.isDirectory && entry.sizeBytes > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether `candidate` is a newer version than `current`. Dotted numeric
|
||||
* components compare numerically; non-numeric versions (or ones that agree
|
||||
* numerically but differ textually, e.g. "1.0.0-beta" vs "1.0.0") are not
|
||||
* treated as newer, so a stray tag never nags forever.
|
||||
*/
|
||||
export function isNewerVersion(candidate: string, current: string): boolean {
|
||||
const a = versionComponents(candidate);
|
||||
const b = versionComponents(current);
|
||||
if (a.length === 0 || b.length === 0) return false;
|
||||
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
||||
const delta = (a[i] ?? 0) - (b[i] ?? 0);
|
||||
if (delta !== 0) return delta > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function versionComponents(version: string): number[] {
|
||||
const match = version.trim().match(/^v?(\d+(?:\.\d+)*)/i);
|
||||
return match ? match[1]!.split(".").map(Number) : [];
|
||||
}
|
||||
|
||||
/** Describe an installed app's standing against the store record it was fetched with. */
|
||||
export function describeUpdate(installed: InstalledEvenHubApp, latest: EvenHubStoreApp | null): EvenHubAppUpdate {
|
||||
return {
|
||||
installed,
|
||||
latest,
|
||||
packageMissing: !isInstalledPackagePresent(installed.packageId),
|
||||
updateAvailable: latest !== null && isNewerVersion(latest.version, installed.version),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up every installed app in the store and return the ones needing
|
||||
* action (a newer version, or a missing package). Lookups run a few at a
|
||||
* time; `onProgress` reports how many have completed so far.
|
||||
*/
|
||||
export async function checkForEvenHubUpdates(
|
||||
onProgress?: (done: number, total: number) => void,
|
||||
): Promise<EvenHubAppUpdate[]> {
|
||||
const installed = getInstalledEvenHubApps();
|
||||
const results: EvenHubAppUpdate[] = new Array(installed.length);
|
||||
let next = 0;
|
||||
let done = 0;
|
||||
const worker = async () => {
|
||||
while (next < installed.length) {
|
||||
const index = next++;
|
||||
const app = installed[index]!;
|
||||
try {
|
||||
results[index] = describeUpdate(app, await evenHubApi.getStoreAppDetail(app.packageId));
|
||||
} catch (error) {
|
||||
// A rejected session affects every lookup: let the caller re-login.
|
||||
if (error instanceof EvenHubAuthenticationError) throw error;
|
||||
results[index] = { ...describeUpdate(app, null), error: cleanError(error) };
|
||||
}
|
||||
done++;
|
||||
onProgress?.(done, installed.length);
|
||||
}
|
||||
};
|
||||
await Promise.all(Array.from({ length: Math.min(4, installed.length) }, worker));
|
||||
return results.filter((update) => update.updateAvailable || update.packageMissing);
|
||||
}
|
||||
|
||||
function cleanError(error: unknown): string {
|
||||
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { type BdfFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { loadImageFileAsGray } from "../../native/image-files";
|
||||
import { loadImageFileAsGray, PHOTO_TONE } from "../../native/image-files";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
|
||||
@@ -13,8 +13,9 @@ const BODY_MARGIN = 2;
|
||||
|
||||
/**
|
||||
* Image viewer for a file on disk: title line, then the image decoded to
|
||||
* grayscale and downscaled to fit the remaining area (never upscaled).
|
||||
* Double-click closes it. Sized to its hosting stack (a Files document
|
||||
* grayscale with the shared photo tone (sRGB gamma, dithered onto the 16
|
||||
* display levels) and downscaled to fit the remaining area (never
|
||||
* upscaled). Double-click closes it. Sized to its hosting stack (a Files document
|
||||
* window, or pushed over the file browser for view-in-place).
|
||||
*/
|
||||
export class ImageViewerLayer implements Layer {
|
||||
@@ -35,7 +36,7 @@ export class ImageViewerLayer implements Layer {
|
||||
const bodyHeight = height - BODY_TOP - BODY_MARGIN;
|
||||
if (!this.loadAttempted) {
|
||||
this.loadAttempted = true;
|
||||
this.image = loadImageFileAsGray(this.path, width - 2 * BODY_MARGIN, bodyHeight);
|
||||
this.image = loadImageFileAsGray(this.path, width - 2 * BODY_MARGIN, bodyHeight, PHOTO_TONE);
|
||||
}
|
||||
if (!this.image) {
|
||||
out.drawText(font, MARGIN_X, BODY_TOP + 16, "(could not load image)", 160);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* on a destination moves there; cascade-to-cascade moves take the longest
|
||||
* legal run that fits (supermoves via empty cells/columns). Double-click
|
||||
* sends the card at the cursor to its foundation, or cancels a pending
|
||||
* selection. Long-press opens the window menu (undo, new game, restart).
|
||||
* selection. Tap-then-hold opens the window menu (undo, new game, restart).
|
||||
* Watch swipes move the cursor spatially: left/right within the row, up/down
|
||||
* between the top row (cells + foundations) and the cascades.
|
||||
* Safe cards auto-play to the foundations after every move.
|
||||
@@ -22,7 +22,7 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, type InputEvent } from "../../ui/gestures";
|
||||
@@ -100,12 +100,13 @@ type Snapshot = {
|
||||
type FreecellWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
/** Whether this window is the shell's input target (pushed with each message). */
|
||||
focused: boolean;
|
||||
/** Long-press window menu; created on first open. */
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: "playing" | "won";
|
||||
cascades: number[][];
|
||||
@@ -142,6 +143,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const window: FreecellWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -212,8 +214,8 @@ function playSfx(window: FreecellWindow, steps: Step[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's long-press menu (game actions + default entries). */
|
||||
function openWindowMenu(window: FreecellWindow): void {
|
||||
/** The window's context menu (game actions). */
|
||||
function windowMenuItems(window: FreecellWindow): MenuItem[] {
|
||||
const items: MenuItem[] = [];
|
||||
if (window.undoStack.length > 0 && window.phase === "playing") {
|
||||
items.push({
|
||||
@@ -250,12 +252,16 @@ function openWindowMenu(window: FreecellWindow): void {
|
||||
},
|
||||
},
|
||||
);
|
||||
windowMenu(window).open([...items, ...defaultWindowMenuItems(window.windowId, post)]);
|
||||
return items;
|
||||
}
|
||||
|
||||
function windowMenu(window: FreecellWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => windowMenuItems(window),
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -326,8 +332,8 @@ function handlePlayingInput(window: FreecellWindow, event: InputEvent, frameId:
|
||||
sendToFoundation(window, window.cursor);
|
||||
}
|
||||
break;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: freecell ignored input");
|
||||
@@ -346,8 +352,8 @@ function handleWonInput(window: FreecellWindow, event: InputEvent, frameId: numb
|
||||
frameTimings.finishFrame(frameId, "discarded: freecell yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: freecell ignored input");
|
||||
@@ -629,19 +635,18 @@ function layoutFor(window: FreecellWindow): Layout {
|
||||
}
|
||||
|
||||
function paint(window: FreecellWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
const planes = singlePlane(paintContent(window));
|
||||
if (window.phase === "won") {
|
||||
// The win box goes on its own plane: card labels are deferred glyphs that
|
||||
// render above their own image's raster, so an overlay drawn into the
|
||||
// board image could not cover them.
|
||||
const overlay = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
||||
paintWinOverlay(overlay, window);
|
||||
planes.push({ image: overlay, x: 0, y: 0 });
|
||||
}
|
||||
return planes;
|
||||
return windowMenu(window).paint(() => {
|
||||
const planes = singlePlane(paintContent(window));
|
||||
if (window.phase === "won") {
|
||||
// The win box goes on its own plane: card labels are deferred glyphs
|
||||
// that render above their own image's raster, so an overlay drawn into
|
||||
// the board image could not cover them.
|
||||
const overlay = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
||||
paintWinOverlay(overlay, window);
|
||||
planes.push({ image: overlay, x: 0, y: 0 });
|
||||
}
|
||||
return planes;
|
||||
});
|
||||
}
|
||||
|
||||
function paintContent(window: FreecellWindow): GrayImage {
|
||||
|
||||
@@ -169,7 +169,7 @@ class LauncherGridLayer implements Layer {
|
||||
}
|
||||
|
||||
/**
|
||||
* App-specific entries for the window's long-press menu. Only item mode
|
||||
* App-specific entries for the window's context menu. Only item mode
|
||||
* pins the selection to a single cell, and only apps can move to folders,
|
||||
* so anywhere else the menu falls back to just the defaults.
|
||||
*/
|
||||
|
||||
@@ -16,6 +16,10 @@ const DEFAULT_ASSIGNMENTS: Record<string, string> = {
|
||||
freecell: "Games",
|
||||
minesweeper: "Games",
|
||||
pinball: "Games",
|
||||
calculator: "Prototypes",
|
||||
microphones: "Prototypes",
|
||||
nightscout: "Prototypes",
|
||||
roam: "Prototypes",
|
||||
};
|
||||
|
||||
export const FOLDER_NAME_MAX_LENGTH = 24;
|
||||
|
||||
@@ -15,14 +15,15 @@
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import {
|
||||
directionalFallback,
|
||||
@@ -94,12 +95,13 @@ type GamePhase = "playing" | "paused" | "won" | "lost";
|
||||
type MinesweeperWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
/** Whether this window is the shell's input target (pushed with each message). */
|
||||
focused: boolean;
|
||||
/** Long-press window menu; created on first open. */
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: GamePhase;
|
||||
difficultyIndex: number;
|
||||
@@ -146,6 +148,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const window: MinesweeperWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -249,10 +252,10 @@ function playSfx(window: MinesweeperWindow, steps: Step[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's long-press menu (game actions + default entries). */
|
||||
function openWindowMenu(window: MinesweeperWindow): void {
|
||||
/** The window's context menu (game actions), offered while paused or over; playing keeps long-press for flagging. */
|
||||
function windowMenuItems(window: MinesweeperWindow): MenuItem[] {
|
||||
const nextDifficulty = DIFFICULTIES[(window.difficultyIndex + 1) % DIFFICULTIES.length]!;
|
||||
windowMenu(window).open([
|
||||
return [
|
||||
{
|
||||
label: "New game",
|
||||
onSelect: (ctx) => {
|
||||
@@ -278,13 +281,17 @@ function openWindowMenu(window: MinesweeperWindow): void {
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
...defaultWindowMenuItems(window.windowId, post),
|
||||
]);
|
||||
];
|
||||
}
|
||||
|
||||
function windowMenu(window: MinesweeperWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
|
||||
claimsLongPress: () => window.phase === "playing",
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -359,6 +366,10 @@ function handlePlayingInput(window: MinesweeperWindow, event: InputEvent, frameI
|
||||
}
|
||||
toggleFlag(window);
|
||||
break;
|
||||
case "short-then-long-press":
|
||||
// No context menu mid-game, so this opens the system menu instead.
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
case "double-click":
|
||||
if (rowMode || watch) {
|
||||
window.phase = "paused";
|
||||
@@ -391,8 +402,8 @@ function handleIdleInput(window: MinesweeperWindow, event: InputEvent, frameId:
|
||||
frameTimings.finishFrame(frameId, "discarded: minesweeper yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: minesweeper ignored input");
|
||||
@@ -588,10 +599,7 @@ function elapsedMs(window: MinesweeperWindow): number {
|
||||
}
|
||||
|
||||
function paint(window: MinesweeperWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(paintContent(window));
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: MinesweeperWindow): GrayImage {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller";
|
||||
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
|
||||
import { MediaBrowseLayer } from "./media-browse";
|
||||
import { MusicSettingsLayer } from "./music-settings";
|
||||
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
|
||||
@@ -480,6 +481,13 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
|
||||
iconLetter: "M",
|
||||
icon: "music",
|
||||
closeable: true,
|
||||
menuItems: () => [{
|
||||
label: "Music settings",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new MusicSettingsLayer());
|
||||
},
|
||||
}],
|
||||
actions: options.actions,
|
||||
baseLayer: new MusicRootLayer(musicLayer),
|
||||
submitFrame: options.submitFrame,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { mediaBrowserBridge } from "../../native/media-browser";
|
||||
import { isMediaAppEnabled, readMediaApps, setMediaAppEnabled } from "../../native/media-apps";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawToggleMenuItem, MenuLayer, type MenuItem } from "../../ui/menu";
|
||||
|
||||
/** Discovered apps stay visible even when ignored or no longer publishing a session. */
|
||||
export class MusicSettingsLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
private signature = "";
|
||||
private selectedPackage = "";
|
||||
|
||||
constructor() {
|
||||
// Refresh installed browser services, remembering ignored services too.
|
||||
mediaBrowserBridge.listBrowsableApps(true);
|
||||
}
|
||||
|
||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||
const apps = readMediaApps().sort((a, b) =>
|
||||
a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName));
|
||||
const width = ctx.stack.getBaseSize().width;
|
||||
const signature = JSON.stringify([width, apps.map((app) => [app.packageName, app.appName])]);
|
||||
if (!this.menu || signature !== this.signature) {
|
||||
this.signature = signature;
|
||||
const items: MenuItem[] = apps.map((app) => ({
|
||||
label: app.appName,
|
||||
onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)),
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
if (selected) this.selectedPackage = app.packageName;
|
||||
const font = getDefaultSmallFont();
|
||||
const duplicateName = apps.some((other) => other !== app && other.appName === app.appName);
|
||||
const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName;
|
||||
drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48),
|
||||
isMediaAppEnabled(app.packageName), selected);
|
||||
},
|
||||
}));
|
||||
if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} });
|
||||
this.menu = new MenuLayer("Music settings", items, {
|
||||
x: 8, y: 8, width: width - 16,
|
||||
opaque: true, showBorder: false,
|
||||
footer: "On = enabled Off = ignored",
|
||||
}).selectItem(Math.max(0, apps.findIndex((app) => app.packageName === this.selectedPackage)));
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
return this.currentMenu(ctx).paint(ctx, paintBelow);
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
await this.currentMenu(ctx).handleInput(event, ctx);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* Saved and recent destinations for the Navigate app: Home and Work (plain
|
||||
* address settings, also editable in the Settings app), custom named
|
||||
* destinations, and a recency list of resolved places. Pure settings-store
|
||||
* accessors, usable from the worker and the main thread alike.
|
||||
*/
|
||||
import {
|
||||
navigateHomeAddressSetting,
|
||||
navigateRecentDestinationsSetting,
|
||||
navigateRememberRecentSetting,
|
||||
navigateSavedDestinationsSetting,
|
||||
navigateWorkAddressSetting,
|
||||
} from "../../ui/dashboard-settings";
|
||||
|
||||
export const HOME_DESTINATION_ID = "home";
|
||||
export const WORK_DESTINATION_ID = "work";
|
||||
|
||||
/** A named place the user set up; the address is resolved when navigating. */
|
||||
export type SavedDestination = {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
/** Home and Work are fixed slots (renaming/removal disabled); others are custom. */
|
||||
builtin: boolean;
|
||||
};
|
||||
|
||||
/** A place the user actually navigated to, already geocoded. */
|
||||
export type RecentDestination = {
|
||||
name: string;
|
||||
place: string;
|
||||
longitude: number;
|
||||
latitude: number;
|
||||
atMs: number;
|
||||
};
|
||||
|
||||
const MAX_RECENT = 8;
|
||||
const MAX_CUSTOM = 20;
|
||||
|
||||
/** Every saved destination in display order: Home, Work (when set), then custom ones. */
|
||||
export function loadSavedDestinations(): SavedDestination[] {
|
||||
const out: SavedDestination[] = [];
|
||||
const home = navigateHomeAddressSetting.get();
|
||||
if (home) out.push({ id: HOME_DESTINATION_ID, name: "Home", address: home, builtin: true });
|
||||
const work = navigateWorkAddressSetting.get();
|
||||
if (work) out.push({ id: WORK_DESTINATION_ID, name: "Work", address: work, builtin: true });
|
||||
out.push(...loadCustomDestinations());
|
||||
return out;
|
||||
}
|
||||
|
||||
export function loadCustomDestinations(): SavedDestination[] {
|
||||
const parsed = parseJsonArray(navigateSavedDestinationsSetting.get());
|
||||
const out: SavedDestination[] = [];
|
||||
for (const entry of parsed) {
|
||||
if (!entry || typeof entry !== "object") continue;
|
||||
const record = entry as Record<string, unknown>;
|
||||
const id = typeof record.id === "string" ? record.id : "";
|
||||
const name = typeof record.name === "string" ? record.name.trim() : "";
|
||||
const address = typeof record.address === "string" ? record.address.trim() : "";
|
||||
if (!id || !name) continue;
|
||||
out.push({ id, name, address, builtin: false });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function saveCustomDestinations(destinations: SavedDestination[]): void {
|
||||
navigateSavedDestinationsSetting.set(
|
||||
JSON.stringify(destinations.slice(0, MAX_CUSTOM).map(({ id, name, address }) => ({ id, name, address }))),
|
||||
);
|
||||
}
|
||||
|
||||
/** Add a custom destination; returns its id. */
|
||||
export function addCustomDestination(name: string, address: string): string {
|
||||
const id = `d${Date.now().toString(36)}`;
|
||||
saveCustomDestinations([...loadCustomDestinations(), { id, name, address, builtin: false }]);
|
||||
return id;
|
||||
}
|
||||
|
||||
export function updateCustomDestination(id: string, patch: { name?: string; address?: string }): void {
|
||||
saveCustomDestinations(
|
||||
loadCustomDestinations().map((destination) =>
|
||||
destination.id === id
|
||||
? { ...destination, name: patch.name ?? destination.name, address: patch.address ?? destination.address }
|
||||
: destination,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export function removeCustomDestination(id: string): void {
|
||||
saveCustomDestinations(loadCustomDestinations().filter((destination) => destination.id !== id));
|
||||
}
|
||||
|
||||
/** Set a destination's address; Home/Work go to their own settings. */
|
||||
export function setDestinationAddress(id: string, address: string): void {
|
||||
if (id === HOME_DESTINATION_ID) navigateHomeAddressSetting.set(address);
|
||||
else if (id === WORK_DESTINATION_ID) navigateWorkAddressSetting.set(address);
|
||||
else updateCustomDestination(id, { address });
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a saved destination by name ("home", "work", or a custom name),
|
||||
* case-insensitively, for voice/assistant queries such as "navigate home".
|
||||
*/
|
||||
export function findSavedDestinationByName(query: string): SavedDestination | null {
|
||||
const wanted = query.trim().toLowerCase();
|
||||
if (!wanted) return null;
|
||||
return loadSavedDestinations().find((destination) => destination.name.toLowerCase() === wanted) ?? null;
|
||||
}
|
||||
|
||||
export function loadRecentDestinations(): RecentDestination[] {
|
||||
if (!navigateRememberRecentSetting.get()) return [];
|
||||
const parsed = parseJsonArray(navigateRecentDestinationsSetting.get());
|
||||
const out: RecentDestination[] = [];
|
||||
for (const entry of parsed) {
|
||||
if (!entry || typeof entry !== "object") continue;
|
||||
const record = entry as Record<string, unknown>;
|
||||
const name = typeof record.name === "string" ? record.name : "";
|
||||
const longitude = Number(record.longitude);
|
||||
const latitude = Number(record.latitude);
|
||||
if (!name || !Number.isFinite(longitude) || !Number.isFinite(latitude)) continue;
|
||||
out.push({
|
||||
name,
|
||||
place: typeof record.place === "string" ? record.place : "",
|
||||
longitude,
|
||||
latitude,
|
||||
atMs: Number(record.atMs) || 0,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Record a navigated-to place at the head of the recent list (no-op when the setting is off). */
|
||||
export function rememberRecentDestination(destination: Omit<RecentDestination, "atMs">): void {
|
||||
if (!navigateRememberRecentSetting.get()) return;
|
||||
const others = loadRecentDestinations().filter((recent) => !isSamePlace(recent, destination));
|
||||
const next = [{ ...destination, atMs: Date.now() }, ...others].slice(0, MAX_RECENT);
|
||||
navigateRecentDestinationsSetting.set(JSON.stringify(next));
|
||||
}
|
||||
|
||||
export function removeRecentDestination(destination: RecentDestination): void {
|
||||
const remaining = loadRecentDestinations().filter((recent) => !isSamePlace(recent, destination));
|
||||
navigateRecentDestinationsSetting.set(JSON.stringify(remaining));
|
||||
}
|
||||
|
||||
export function clearRecentDestinations(): void {
|
||||
navigateRecentDestinationsSetting.set("[]");
|
||||
}
|
||||
|
||||
function isSamePlace(
|
||||
a: { name: string; place: string; longitude: number; latitude: number },
|
||||
b: { name: string; place: string; longitude: number; latitude: number },
|
||||
): boolean {
|
||||
// Within ~10 m, or the same name at the same formatted place: a repeat
|
||||
// visit, not a new entry. (Two branches of one chain stay distinct.)
|
||||
return (
|
||||
(a.name === b.name && a.place === b.place) ||
|
||||
(Math.abs(a.longitude - b.longitude) < 0.0001 && Math.abs(a.latitude - b.latitude) < 0.0001)
|
||||
);
|
||||
}
|
||||
|
||||
function parseJsonArray(text: string): unknown[] {
|
||||
try {
|
||||
const parsed = JSON.parse(text);
|
||||
return Array.isArray(parsed) ? parsed : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -8,16 +8,60 @@
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import {
|
||||
drawListScrollbar,
|
||||
drawRightValueMenuItem,
|
||||
drawSelectionHighlight,
|
||||
drawSubmenuIndicator,
|
||||
drawToggleMenuItem,
|
||||
scrollToKeepSelectionVisible,
|
||||
type MenuItem,
|
||||
} from "../../ui/menu";
|
||||
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
|
||||
import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu";
|
||||
import type { LayerContext } from "../../ui/layers";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { onSettingsStoreChanged } from "../../native/settings-store";
|
||||
import {
|
||||
navigateDestinationAddressDraftSetting,
|
||||
navigateDestinationNameDraftSetting,
|
||||
navigateRememberRecentSetting,
|
||||
navigateDisplayModeSetting,
|
||||
navigateVerticalPositionSetting,
|
||||
enumSettingMenuItem,
|
||||
type ConfigSettingString,
|
||||
} from "../../ui/dashboard-settings";
|
||||
import {
|
||||
addCustomDestination,
|
||||
clearRecentDestinations,
|
||||
findSavedDestinationByName,
|
||||
HOME_DESTINATION_ID,
|
||||
loadRecentDestinations,
|
||||
loadSavedDestinations,
|
||||
removeCustomDestination,
|
||||
rememberRecentDestination,
|
||||
removeRecentDestination,
|
||||
setDestinationAddress,
|
||||
updateCustomDestination,
|
||||
WORK_DESTINATION_ID,
|
||||
type RecentDestination,
|
||||
type SavedDestination,
|
||||
} from "./destinations";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import type { ToolSpec, ToolResult } from "../../assistant/tool-registry";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures";
|
||||
import {
|
||||
GESTURE_CLICK,
|
||||
GESTURE_DOUBLE_CLICK,
|
||||
GESTURE_SCROLL,
|
||||
GESTURE_SHORT_THEN_LONG_PRESS,
|
||||
type InputEvent,
|
||||
} from "../../ui/gestures";
|
||||
import { LocationTracker, type TrackedLocation } from "../../native/location-tracker";
|
||||
import {
|
||||
fetchRoute,
|
||||
@@ -29,6 +73,9 @@ import {
|
||||
type RouteProfile,
|
||||
type StaticMapCamera,
|
||||
} from "../../native/mapbox";
|
||||
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled, type CompassEvent } from "../../native/compass";
|
||||
import { magneticDeclinationDegrees } from "../../native/geomagnetic";
|
||||
import { calibrateHeading, normalizeHeading } from "../compass/calibration";
|
||||
import { bearingDegrees, haversineMeters, RouteFollower, type RouteProgress } from "./route-follower";
|
||||
import { drawManeuverGlyph } from "./maneuver-icons";
|
||||
|
||||
@@ -46,8 +93,19 @@ const MAP_RETRY_BACKOFF_MS = 5_000;
|
||||
const REROUTE_MIN_INTERVAL_MS = 20_000;
|
||||
const FIRST_FIX_TIMEOUT_MS = 10_000;
|
||||
const LOCATION_INTERVAL_MS = 1_000;
|
||||
/**
|
||||
* A fix older than this is not where the user is now. The tracker seeds each
|
||||
* start with the platform's cached fix, and after a previous trip that can be
|
||||
* the old destination: routing from it, or feeding it to the follower, ends a
|
||||
* fresh navigation with "Arrived" before a live fix ever comes in.
|
||||
*/
|
||||
const FIX_MAX_AGE_MS = 60_000;
|
||||
/** Above this speed a GPS bearing is trustworthy for heading-up. */
|
||||
const BEARING_MIN_SPEED_MPS = 2.5;
|
||||
/** The head-direction chevron only redraws once the wearer has turned this far. */
|
||||
const HEADING_STEP_DEG = 4;
|
||||
/** Our claim on the shared glasses magnetometer (see setCompassEnabled). */
|
||||
const COMPASS_OWNER = "navigate";
|
||||
|
||||
const largeFont = getFont("terminus32");
|
||||
const mediumFont = getFont("terminus24");
|
||||
@@ -59,6 +117,7 @@ type MapMode = "follow" | "overview";
|
||||
type NavWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
@@ -71,7 +130,7 @@ const NAVIGATE_TOOLS: ToolSpec[] = [
|
||||
{
|
||||
name: "start_route",
|
||||
description:
|
||||
"Start turn-by-turn navigation to a destination. Give the destination as free text (a place name, business, or address); it is resolved near the user's current location. Returns the resolved destination, distance, and estimated time. If the wrong place was picked, call again with a more specific query.",
|
||||
"Start turn-by-turn navigation to a destination. Give the destination as free text (a place name, business, or address); it is resolved near the user's current location. The name of one of the user's saved destinations (e.g. 'home', 'work') is used directly. Returns the resolved destination, distance, and estimated time. If the wrong place was picked, call again with a more specific query.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
@@ -123,6 +182,35 @@ let rerouteInFlight = false;
|
||||
/** Bumped on every new route/mode so stale map fetches can be discarded. */
|
||||
let routeGeneration = 0;
|
||||
|
||||
/**
|
||||
* Where to go: free text to geocode (optionally shown under a saved
|
||||
* destination's label), or an already-resolved place (a recent destination).
|
||||
*/
|
||||
type NavTarget =
|
||||
| { kind: "query"; query: string; label?: string }
|
||||
| { kind: "place"; name: string; place: string; longitude: number; latitude: number };
|
||||
|
||||
/** An entry on the idle page's destination list. */
|
||||
type IdleEntry =
|
||||
| { kind: "saved"; destination: SavedDestination }
|
||||
| { kind: "recent"; destination: RecentDestination };
|
||||
|
||||
/** Idle-page list selection (index into idleEntries()). */
|
||||
let idleSelection = 0;
|
||||
let idleScrollRow = 0;
|
||||
|
||||
/**
|
||||
* In-progress destination edit: the phone text editor is open on `setting`
|
||||
* (a staging draft); click confirms with the typed value, double-click
|
||||
* cancels. `onDone` may start another edit (name, then address).
|
||||
*/
|
||||
type DestinationEdit = {
|
||||
setting: ConfigSettingString;
|
||||
title: string;
|
||||
onDone: (value: string) => void;
|
||||
};
|
||||
let editing: DestinationEdit | null = null;
|
||||
|
||||
let mapMode: MapMode = "follow";
|
||||
let zoomOffset = 0;
|
||||
let mapImage: GrayImage | null = null;
|
||||
@@ -132,6 +220,17 @@ let mapLastFetchAtMs = 0;
|
||||
let mapLastFetchCenter: [number, number] | null = null;
|
||||
let mapNextRetryAtMs = 0;
|
||||
let mapLastError = "";
|
||||
/** Bearing the current map image was rendered with (its "up"), for follow mode. */
|
||||
let mapFetchedBearingDeg = 0;
|
||||
|
||||
// Glasses compass: the same firmware feed and wearer calibration as the
|
||||
// Compass app, so the position chevron shows where the wearer is looking
|
||||
// relative to the map rather than always pointing up the direction of travel.
|
||||
let compassActive = false;
|
||||
let unsubscribeCompass: (() => void) | null = null;
|
||||
/** Wearer's true heading (degrees clockwise from north) as last drawn, or null before compass data arrives. */
|
||||
let headHeadingDeg: number | null = null;
|
||||
let declinationCache: { latitude: number; longitude: number; degrees: number | null } | null = null;
|
||||
|
||||
let tickTimer: ReturnType<typeof setInterval> | null = null;
|
||||
let firstFixWaiters: Array<(fix: TrackedLocation) => void> = [];
|
||||
@@ -149,6 +248,12 @@ function post(message: WorkerAppReply): void {
|
||||
global.postMessage(message);
|
||||
}
|
||||
|
||||
// Destinations edited on the phone (the text editor, or the Settings app's
|
||||
// Home/Work rows) repaint the idle list / the edit screen live.
|
||||
onSettingsStoreChanged((key) => {
|
||||
if (key.startsWith("navigate.")) render();
|
||||
});
|
||||
|
||||
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||
// evaluation is synchronous, so the handler below is installed before any
|
||||
@@ -162,6 +267,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
window = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -171,9 +277,24 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
};
|
||||
post({ type: "set-tools", windowId: message.windowId, tools: NAVIGATE_TOOLS });
|
||||
break;
|
||||
case "resize-window":
|
||||
if (!window || window.windowId !== message.windowId) break;
|
||||
if (window.viewportWidth === message.viewport.width && window.viewportHeight === message.viewport.height) break;
|
||||
window.viewportWidth = message.viewport.width;
|
||||
window.viewportHeight = message.viewport.height;
|
||||
window.menu?.resize(message.viewport);
|
||||
window.lastSubmittedFingerprint = "";
|
||||
render();
|
||||
break;
|
||||
case "close-window":
|
||||
if (editing) {
|
||||
// Window closed mid-edit: shut the phone editor down.
|
||||
editing = null;
|
||||
post({ type: "end-text-setting-edit" });
|
||||
}
|
||||
stopNavigation("");
|
||||
window = null;
|
||||
reconcileCompass();
|
||||
break;
|
||||
case "input":
|
||||
if (!window || window.windowId !== message.windowId) {
|
||||
@@ -187,8 +308,12 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
handleInput(window, message.event as InputEvent, message.frameId);
|
||||
break;
|
||||
case "text-input":
|
||||
// Voice input / typed text sets a destination directly.
|
||||
if (message.text.trim()) {
|
||||
if (editing) {
|
||||
// Voice input as an alternative to the phone keyboard.
|
||||
editing.setting.set(message.text.trim());
|
||||
render();
|
||||
} else if (message.text.trim()) {
|
||||
// Voice input / typed text sets a destination directly.
|
||||
void startNavigation(message.text.trim(), profile).catch(() => {});
|
||||
}
|
||||
break;
|
||||
@@ -201,6 +326,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (!window) break;
|
||||
window.foreground = message.foreground;
|
||||
window.focused = message.focused;
|
||||
reconcileCompass();
|
||||
if (window.foreground) {
|
||||
maybeRefreshMap();
|
||||
render();
|
||||
@@ -208,6 +334,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
break;
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
reconcileCompass();
|
||||
if (screenOn && window?.foreground) {
|
||||
maybeRefreshMap();
|
||||
render();
|
||||
@@ -232,7 +359,20 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Navigation state machine
|
||||
|
||||
async function startNavigation(query: string, requestedProfile: RouteProfile): Promise<string> {
|
||||
/**
|
||||
* Navigate to free text: a saved destination's name ("home") routes to its
|
||||
* address under that label; anything else is geocoded as typed.
|
||||
*/
|
||||
function startNavigation(query: string, requestedProfile: RouteProfile): Promise<string> {
|
||||
const saved = findSavedDestinationByName(query);
|
||||
return startNavigationTo(
|
||||
saved ? { kind: "query", query: saved.address, label: saved.name } : { kind: "query", query },
|
||||
requestedProfile,
|
||||
);
|
||||
}
|
||||
|
||||
async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfile): Promise<string> {
|
||||
const query = target.kind === "query" ? target.query : target.name;
|
||||
if (!isMapboxConfigured()) {
|
||||
statusMessage = "Set a Mapbox token in Settings > API Keys.";
|
||||
phase = "idle";
|
||||
@@ -255,21 +395,44 @@ async function startNavigation(query: string, requestedProfile: RouteProfile): P
|
||||
const fix = await waitForFix();
|
||||
|
||||
phase = "routing";
|
||||
statusMessage = `Finding ${query}...`;
|
||||
render();
|
||||
const candidates = await geocodeForward(query, fix, 5);
|
||||
if (!candidates.length) {
|
||||
throw new Error(`No places found matching "${query}".`);
|
||||
let picked: GeocodeCandidate;
|
||||
let candidates: GeocodeCandidate[] = [];
|
||||
if (target.kind === "place") {
|
||||
picked = {
|
||||
name: target.name,
|
||||
placeFormatted: target.place,
|
||||
longitude: target.longitude,
|
||||
latitude: target.latitude,
|
||||
distanceMeters: null,
|
||||
};
|
||||
} else {
|
||||
statusMessage = `Finding ${query}...`;
|
||||
render();
|
||||
candidates = await geocodeForward(query, fix, 5);
|
||||
if (!candidates.length) {
|
||||
throw new Error(`No places found matching "${query}".`);
|
||||
}
|
||||
picked = candidates[0]!;
|
||||
}
|
||||
const picked = candidates[0]!;
|
||||
destination = { longitude: picked.longitude, latitude: picked.latitude };
|
||||
destinationName = picked.name;
|
||||
destinationName = target.kind === "query" && target.label ? target.label : picked.name;
|
||||
destinationPlace = picked.placeFormatted;
|
||||
|
||||
statusMessage = `Routing to ${picked.name}...`;
|
||||
statusMessage = `Routing to ${destinationName}...`;
|
||||
render();
|
||||
const route = await fetchRoute(fix, destination, profile);
|
||||
adoptRoute(route);
|
||||
// Saved destinations already have a row of their own on the idle page;
|
||||
// everything else (voice, assistant, a re-picked recent) goes on the
|
||||
// recent list, most recent first.
|
||||
if (!(target.kind === "query" && target.label)) {
|
||||
rememberRecentDestination({
|
||||
name: picked.name,
|
||||
place: picked.placeFormatted,
|
||||
longitude: picked.longitude,
|
||||
latitude: picked.latitude,
|
||||
});
|
||||
}
|
||||
if (window) post({ type: "focus-window", windowId: window.windowId });
|
||||
return startSummary(picked, candidates, route);
|
||||
} catch (error) {
|
||||
@@ -323,8 +486,16 @@ function stopNavigation(finalStatus: string): void {
|
||||
ensureTickTimer();
|
||||
}
|
||||
|
||||
function isFreshFix(fix: TrackedLocation): boolean {
|
||||
return Date.now() - fix.timestampMs < FIX_MAX_AGE_MS;
|
||||
}
|
||||
|
||||
function handleFix(fix: TrackedLocation): void {
|
||||
lastFix = fix;
|
||||
if (!lastFix || fix.timestampMs >= lastFix.timestampMs) lastFix = fix;
|
||||
// A stale fix (a cached seed from a previous outing) may position the map
|
||||
// until something better arrives, but it must not stand in for the user's
|
||||
// current location: it neither starts a route nor advances guidance.
|
||||
if (!isFreshFix(fix)) return;
|
||||
if (fix.bearingDeg !== null && (fix.speedMps ?? 0) >= BEARING_MIN_SPEED_MPS) {
|
||||
lastBearingDeg = fix.bearingDeg;
|
||||
}
|
||||
@@ -378,7 +549,7 @@ function stopTrackingIfIdle(): void {
|
||||
}
|
||||
|
||||
function waitForFix(): Promise<TrackedLocation> {
|
||||
if (lastFix && Date.now() - lastFix.timestampMs < 60_000) {
|
||||
if (lastFix && isFreshFix(lastFix)) {
|
||||
return Promise.resolve(lastFix);
|
||||
}
|
||||
return new Promise<TrackedLocation>((resolve, reject) => {
|
||||
@@ -394,6 +565,59 @@ function waitForFix(): Promise<TrackedLocation> {
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Head heading (glasses compass)
|
||||
|
||||
/** Hold the magnetometer only while its reading is actually on screen. */
|
||||
function reconcileCompass(): void {
|
||||
const wanted =
|
||||
window !== null && window.foreground && screenOn && phase === "navigating" && mapMode === "follow";
|
||||
if (wanted === compassActive) return;
|
||||
compassActive = wanted;
|
||||
if (wanted) {
|
||||
unsubscribeCompass = addCompassListener(handleCompassEvent);
|
||||
setCompassEnabled(true, COMPASS_OWNER);
|
||||
} else {
|
||||
setCompassEnabled(false, COMPASS_OWNER);
|
||||
unsubscribeCompass?.();
|
||||
unsubscribeCompass = null;
|
||||
headHeadingDeg = null;
|
||||
}
|
||||
}
|
||||
|
||||
function handleCompassEvent(event: CompassEvent): void {
|
||||
if (!compassActive || event.command !== COMPASS_CHANGED || event.headingDegrees < 0) return;
|
||||
// Wearer-fit offset from the Compass app's calibration, then declination
|
||||
// from our own GPS fix: the map is always true-north referenced.
|
||||
const magnetic = calibrateHeading(event.headingDegrees);
|
||||
const heading = normalizeHeading(magnetic + (currentDeclination() ?? 0));
|
||||
if (headHeadingDeg !== null && angularDistance(heading, headHeadingDeg) < HEADING_STEP_DEG) return;
|
||||
headHeadingDeg = heading;
|
||||
render();
|
||||
}
|
||||
|
||||
/** Declination at the last fix; it only varies over tens of kilometres, so cache per coarse position. */
|
||||
function currentDeclination(): number | null {
|
||||
if (!lastFix) return null;
|
||||
if (
|
||||
!declinationCache ||
|
||||
Math.abs(declinationCache.latitude - lastFix.latitude) > 0.5 ||
|
||||
Math.abs(declinationCache.longitude - lastFix.longitude) > 0.5
|
||||
) {
|
||||
declinationCache = {
|
||||
latitude: lastFix.latitude,
|
||||
longitude: lastFix.longitude,
|
||||
degrees: magneticDeclinationDegrees(lastFix.latitude, lastFix.longitude),
|
||||
};
|
||||
}
|
||||
return declinationCache.degrees;
|
||||
}
|
||||
|
||||
function angularDistance(a: number, b: number): number {
|
||||
const delta = Math.abs(normalizeHeading(a) - normalizeHeading(b));
|
||||
return Math.min(delta, 360 - delta);
|
||||
}
|
||||
|
||||
function ensureTickTimer(): void {
|
||||
const shouldRun = phase === "navigating";
|
||||
if (shouldRun && tickTimer === null) {
|
||||
@@ -512,9 +736,10 @@ function maybeRefreshMap(): void {
|
||||
.then((image) => {
|
||||
mapInFlight = false;
|
||||
if (generation !== routeGeneration) return; // Route/mode changed mid-fetch.
|
||||
boostContrast(image);
|
||||
levelMap(image);
|
||||
mapImage = image;
|
||||
mapFetchedKey = view.key;
|
||||
mapFetchedBearingDeg = view.camera.kind === "center" ? view.camera.bearingDeg : 0;
|
||||
mapLastFetchAtMs = Date.now();
|
||||
mapLastFetchCenter = view.camera.kind === "center" ? [view.camera.longitude, view.camera.latitude] : null;
|
||||
mapLastError = "";
|
||||
@@ -539,11 +764,25 @@ function simplifyPath(path: Array<[number, number]>, maxPoints: number): Array<[
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Stretch the dark-style render so roads survive 4bpp quantization. */
|
||||
function boostContrast(image: GrayImage): void {
|
||||
/**
|
||||
* Level the render for the lens. The dominant tone is the land fill: black
|
||||
* when the style override took, dark gray otherwise. Either way it becomes
|
||||
* the black point, and everything above it is stretched so roads survive
|
||||
* 4bpp quantization.
|
||||
*/
|
||||
function levelMap(image: GrayImage): void {
|
||||
const histogram = new Uint32Array(256);
|
||||
for (let i = 0; i < image.pixels.length; i++) histogram[image.pixels[i]!]++;
|
||||
let black = 0;
|
||||
for (let value = 1; value < 256; value++) {
|
||||
if (histogram[value]! > histogram[black]!) black = value;
|
||||
}
|
||||
// A bright dominant tone isn't a floor (a large plaza, a fetch that came
|
||||
// back mostly label); flattening it would eat the roads.
|
||||
if (black > 64) black = 0;
|
||||
for (let i = 0; i < image.pixels.length; i++) {
|
||||
const value = image.pixels[i]!;
|
||||
image.pixels[i] = Math.max(0, Math.min(255, Math.round((value - 12) * 1.7)));
|
||||
image.pixels[i] = value <= black ? 0 : Math.min(255, Math.round((value - black) * 1.7));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -609,9 +848,272 @@ function describeRouteStatus(): string {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Input
|
||||
|
||||
/**
|
||||
* The window's context menu: Stop navigation while a route is up, then the
|
||||
* display and saved-destination settings (Home, Work, custom named places), and the
|
||||
* recent-destinations preference.
|
||||
*/
|
||||
function windowMenuItems(win: NavWindow): MenuItem[] {
|
||||
const items: MenuItem[] = [];
|
||||
if (phase === "navigating" || phase === "arrived") {
|
||||
items.push({
|
||||
label: "Stop navigation",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
stopNavigation("Navigation stopped.");
|
||||
render();
|
||||
},
|
||||
});
|
||||
}
|
||||
items.push(
|
||||
enumSettingMenuItem(navigateDisplayModeSetting),
|
||||
enumSettingMenuItem(navigateVerticalPositionSetting),
|
||||
);
|
||||
items.push({
|
||||
label: "Saved destinations",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new WindowMenuLayer("Saved destinations", savedDestinationMenuItems()));
|
||||
},
|
||||
render: ({ image, x, y, width, height, text }) => {
|
||||
image.drawText(smallFont, x, y + LIST_ROW_TEXT_INSET, text, 200);
|
||||
drawSubmenuIndicator(image, smallFont, x - 10, y, width + 20, height, 150);
|
||||
},
|
||||
});
|
||||
items.push({
|
||||
label: navigateRememberRecentSetting.label,
|
||||
onSelect: () => {
|
||||
const enabled = navigateRememberRecentSetting.toggle();
|
||||
if (!enabled) clearRecentDestinations();
|
||||
clampIdleSelection();
|
||||
},
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
drawToggleMenuItem(
|
||||
image,
|
||||
smallFont,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
navigateRememberRecentSetting.label,
|
||||
navigateRememberRecentSetting.get(),
|
||||
selected,
|
||||
);
|
||||
},
|
||||
});
|
||||
const selectedEntry = phase === "idle" ? idleEntries()[idleSelection] : undefined;
|
||||
if (selectedEntry?.kind === "recent") {
|
||||
const recent = selectedEntry.destination;
|
||||
items.push({
|
||||
label: `Forget ${truncateText(smallFont, recent.name, 160)}`,
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
removeRecentDestination(recent);
|
||||
clampIdleSelection();
|
||||
},
|
||||
});
|
||||
}
|
||||
if (loadRecentDestinations().length) {
|
||||
items.push({
|
||||
label: "Clear recent destinations",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
clearRecentDestinations();
|
||||
clampIdleSelection();
|
||||
},
|
||||
});
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/** Home, Work, each custom destination (its own action submenu), and Add. */
|
||||
function savedDestinationMenuItems(): MenuItem[] {
|
||||
const items: MenuItem[] = [];
|
||||
const saved = loadSavedDestinations();
|
||||
const home = saved.find((d) => d.id === HOME_DESTINATION_ID);
|
||||
const work = saved.find((d) => d.id === WORK_DESTINATION_ID);
|
||||
items.push(addressMenuItem("Home", HOME_DESTINATION_ID, home?.address ?? ""));
|
||||
items.push(addressMenuItem("Work", WORK_DESTINATION_ID, work?.address ?? ""));
|
||||
for (const destination of saved) {
|
||||
if (destination.builtin) continue;
|
||||
items.push({
|
||||
label: destination.name,
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new WindowMenuLayer(destination.name, customDestinationMenuItems(destination)));
|
||||
},
|
||||
render: ({ image, x, y, width, height }) => {
|
||||
drawAddressRow(image, x, y, width, destination.name, destination.address, true);
|
||||
drawSubmenuIndicator(image, smallFont, x - 10, y, width + 20, height, 150);
|
||||
},
|
||||
});
|
||||
}
|
||||
items.push({
|
||||
label: "Add destination...",
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => beginAddDestination());
|
||||
},
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
/** A Home/Work row: shows the address, opens the address editor. */
|
||||
function addressMenuItem(label: string, id: string, address: string): MenuItem {
|
||||
return {
|
||||
label,
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => beginAddressEdit(id, label, address));
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
drawAddressRow(image, x, y, width, label, address, false);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function customDestinationMenuItems(destination: SavedDestination): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: "Navigate there",
|
||||
disabled: !destination.address,
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => {
|
||||
void startNavigationTo({ kind: "query", query: destination.address, label: destination.name }, profile).catch(
|
||||
() => {},
|
||||
);
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Set address",
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => beginAddressEdit(destination.id, destination.name, destination.address));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Rename",
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => {
|
||||
navigateDestinationNameDraftSetting.set(destination.name);
|
||||
beginEdit(navigateDestinationNameDraftSetting, `Rename ${destination.name}`, (name) => {
|
||||
if (name) updateCustomDestination(destination.id, { name });
|
||||
});
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Remove",
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => {
|
||||
removeCustomDestination(destination.id);
|
||||
clampIdleSelection();
|
||||
});
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function drawAddressRow(
|
||||
image: GrayImage,
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
label: string,
|
||||
address: string,
|
||||
hasSubmenu: boolean,
|
||||
): void {
|
||||
const valueWidth = Math.max(40, width - smallFont.measureText(label) - (hasSubmenu ? 28 : 12));
|
||||
const value = truncateText(smallFont, address || "(not set)", valueWidth);
|
||||
drawRightValueMenuItem(image, smallFont, x, y, width - (hasSubmenu ? 16 : 0), label, value);
|
||||
}
|
||||
|
||||
/** Close the whole menu stack, then run an action that takes over the window. */
|
||||
function closeMenusAnd(ctx: LayerContext, action: () => void): void {
|
||||
ctx.stack.clearToBase();
|
||||
action();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Destination editing (phone text editor + voice input)
|
||||
|
||||
/**
|
||||
* Open the phone text editor on a staging setting. The editor writes the
|
||||
* draft live (repainted via the settings listener); click here confirms.
|
||||
*/
|
||||
function beginEdit(setting: ConfigSettingString, title: string, onDone: (value: string) => void): void {
|
||||
editing = { setting, title, onDone };
|
||||
post({ type: "start-text-setting-edit", settingId: setting.id });
|
||||
render();
|
||||
}
|
||||
|
||||
function finishEdit(confirmed: boolean): void {
|
||||
const current = editing;
|
||||
editing = null;
|
||||
post({ type: "end-text-setting-edit" });
|
||||
if (current && confirmed) current.onDone(current.setting.get());
|
||||
// onDone may have started the next step; only fully leave when it didn't.
|
||||
if (!editing) {
|
||||
navigateDestinationNameDraftSetting.set("");
|
||||
navigateDestinationAddressDraftSetting.set("");
|
||||
clampIdleSelection();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
function beginAddressEdit(id: string, name: string, current: string): void {
|
||||
navigateDestinationAddressDraftSetting.set(current);
|
||||
beginEdit(navigateDestinationAddressDraftSetting, `${name} address`, (address) => {
|
||||
if (address) setDestinationAddress(id, address);
|
||||
});
|
||||
}
|
||||
|
||||
/** Two-step add: a name, then its address. An empty answer cancels. */
|
||||
function beginAddDestination(): void {
|
||||
navigateDestinationNameDraftSetting.set("");
|
||||
beginEdit(navigateDestinationNameDraftSetting, "New destination name", (name) => {
|
||||
if (!name) return;
|
||||
navigateDestinationAddressDraftSetting.set("");
|
||||
beginEdit(navigateDestinationAddressDraftSetting, `${name} address`, (address) => {
|
||||
if (address) addCustomDestination(name, address);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Idle page destination list
|
||||
|
||||
function idleEntries(): IdleEntry[] {
|
||||
const entries: IdleEntry[] = loadSavedDestinations()
|
||||
.filter((destination) => destination.address)
|
||||
.map((destination): IdleEntry => ({ kind: "saved", destination }));
|
||||
for (const destination of loadRecentDestinations()) {
|
||||
entries.push({ kind: "recent", destination });
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
function clampIdleSelection(): void {
|
||||
const count = idleEntries().length;
|
||||
idleSelection = Math.max(0, Math.min(idleSelection, count - 1));
|
||||
}
|
||||
|
||||
function navigateToIdleEntry(entry: IdleEntry): void {
|
||||
const target: NavTarget =
|
||||
entry.kind === "saved"
|
||||
? { kind: "query", query: entry.destination.address, label: entry.destination.name }
|
||||
: {
|
||||
kind: "place",
|
||||
name: entry.destination.name,
|
||||
place: entry.destination.place,
|
||||
longitude: entry.destination.longitude,
|
||||
latitude: entry.destination.latitude,
|
||||
};
|
||||
void startNavigationTo(target, profile).catch(() => {});
|
||||
}
|
||||
|
||||
function windowMenu(win: NavWindow): WindowMenu {
|
||||
if (!win.menu) {
|
||||
win.menu = new WindowMenu({
|
||||
windowId: win.windowId,
|
||||
post,
|
||||
title: () => win.title,
|
||||
items: () => windowMenuItems(win),
|
||||
size: { width: win.viewportWidth, height: win.viewportHeight },
|
||||
paintBase: () => paintContent(win),
|
||||
isFocused: () => win.focused,
|
||||
@@ -628,19 +1130,20 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
.then(() => renderAndSubmit(win, frameId));
|
||||
return;
|
||||
}
|
||||
if (event.type === "long-press") {
|
||||
const items = [...defaultWindowMenuItems(win.windowId, post)];
|
||||
if (phase === "navigating" || phase === "arrived") {
|
||||
items.unshift({
|
||||
label: "Stop navigation",
|
||||
onSelect: (ctx: any) => {
|
||||
ctx.stack.pop();
|
||||
stopNavigation("Navigation stopped.");
|
||||
render();
|
||||
},
|
||||
});
|
||||
if (editing) {
|
||||
if (event.type === "click") {
|
||||
finishEdit(true);
|
||||
} else if (event.type === "double-click") {
|
||||
finishEdit(false);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate edit ignored input");
|
||||
return;
|
||||
}
|
||||
windowMenu(win).open(items);
|
||||
renderAndSubmit(win, frameId);
|
||||
return;
|
||||
}
|
||||
if (event.type === "short-then-long-press") {
|
||||
windowMenu(win).open();
|
||||
renderAndSubmit(win, frameId);
|
||||
return;
|
||||
}
|
||||
@@ -656,6 +1159,13 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
maybeRefreshMap();
|
||||
} else if (phase === "arrived") {
|
||||
stopNavigation("");
|
||||
} else if (phase === "idle") {
|
||||
const entry = idleEntries()[idleSelection];
|
||||
if (!entry) {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate ignored click");
|
||||
return;
|
||||
}
|
||||
navigateToIdleEntry(entry);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate ignored click");
|
||||
return;
|
||||
@@ -668,6 +1178,10 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
zoomOffset = Math.max(-3, Math.min(2, zoomOffset + (event.type === "scroll-up" ? 0.5 : -0.5)));
|
||||
maybeRefreshMap();
|
||||
renderAndSubmit(win, frameId);
|
||||
} else if (phase === "idle" && idleEntries().length > 1) {
|
||||
const count = idleEntries().length;
|
||||
idleSelection = Math.max(0, Math.min(count - 1, idleSelection + (event.type === "scroll-down" ? 1 : -1)));
|
||||
renderAndSubmit(win, frameId);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate ignored scroll");
|
||||
}
|
||||
@@ -680,45 +1194,133 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
// Painting
|
||||
|
||||
function paint(win: NavWindow): Plane[] {
|
||||
if (win.menu?.isOpen()) return win.menu.paint();
|
||||
return singlePlane(paintContent(win));
|
||||
return windowMenu(win).paint();
|
||||
}
|
||||
|
||||
function paintContent(win: NavWindow): GrayImage {
|
||||
const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0);
|
||||
if (phase === "idle" || phase === "acquiring" || phase === "routing") {
|
||||
paintIdle(image);
|
||||
if (editing) {
|
||||
paintEdit(image, editing);
|
||||
} else if (phase === "idle" || phase === "acquiring" || phase === "routing") {
|
||||
paintIdle(image, win);
|
||||
} else {
|
||||
paintNavigating(image, win);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
function paintIdle(image: GrayImage): void {
|
||||
/** The "type on the phone" screen for a destination name/address edit. */
|
||||
function paintEdit(image: GrayImage, edit: DestinationEdit): void {
|
||||
const step = smallFont.lineHeight + 2;
|
||||
image.drawText(mediumFont, 24, 16, edit.title, 245);
|
||||
const messageY = 64;
|
||||
const lines = wrapText(smallFont, "Type the value in the phone app, or use voice input.", image.width - 48);
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
image.drawText(smallFont, 24, messageY + index * step, lines[index]!, 190);
|
||||
}
|
||||
const value = edit.setting.get();
|
||||
image.drawText(
|
||||
smallFont,
|
||||
24,
|
||||
messageY + (lines.length + 1) * step,
|
||||
truncateText(smallFont, value || "(empty)", image.width - 48),
|
||||
value ? 225 : 130,
|
||||
);
|
||||
drawFooter(image, `${GESTURE_CLICK} done ${GESTURE_DOUBLE_CLICK} cancel`);
|
||||
}
|
||||
|
||||
function paintIdle(image: GrayImage, win: NavWindow): void {
|
||||
image.drawText(mediumFont, 24, 16, "Navigate", 245);
|
||||
const busy = phase !== "idle";
|
||||
const hint = isMapboxConfigured()
|
||||
? "Ask the voice assistant to navigate somewhere, or use Voice input from the long-press menu to say a destination."
|
||||
: "Set a Mapbox token in Settings > API Keys to enable navigation.";
|
||||
image.drawTextWrapped({ font: smallFont, x: 24, y: 64, width: image.width - 48, text: hint, value: 170 });
|
||||
const entries = busy ? [] : idleEntries();
|
||||
const hint = !isMapboxConfigured()
|
||||
? "Set a Mapbox token in Settings > API Keys to enable navigation."
|
||||
: entries.length
|
||||
? "Pick a destination below, or say one via Voice input (system menu, long-press)."
|
||||
: "Ask the voice assistant to navigate somewhere, or pick Voice input from the system menu (long-press) to say a destination. Save Home, Work and other places from the app menu.";
|
||||
const hintLines = wrapText(smallFont, hint, image.width - 48);
|
||||
const hintStep = smallFont.lineHeight + 2;
|
||||
for (let index = 0; index < hintLines.length; index++) {
|
||||
image.drawText(smallFont, 24, 52 + index * hintStep, hintLines[index]!, 170);
|
||||
}
|
||||
let y = 52 + hintLines.length * hintStep + 6;
|
||||
if (statusMessage) {
|
||||
image.drawTextWrapped({
|
||||
font: smallFont,
|
||||
x: 24,
|
||||
y: 130,
|
||||
width: image.width - 48,
|
||||
text: statusMessage,
|
||||
value: busy ? 220 : 190,
|
||||
});
|
||||
const statusLines = wrapText(smallFont, statusMessage, image.width - 48);
|
||||
for (let index = 0; index < statusLines.length; index++) {
|
||||
image.drawText(smallFont, 24, y + index * hintStep, statusLines[index]!, busy ? 220 : 190);
|
||||
}
|
||||
y += statusLines.length * hintStep + 6;
|
||||
}
|
||||
if (entries.length) {
|
||||
paintIdleList(image, win, entries, y);
|
||||
drawFooter(
|
||||
image,
|
||||
`${GESTURE_SCROLL} select ${GESTURE_CLICK} go ${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`,
|
||||
);
|
||||
} else {
|
||||
drawFooter(image, `${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Saved destinations (name + address) then recent places, one selectable row each. */
|
||||
function paintIdleList(image: GrayImage, win: NavWindow, entries: IdleEntry[], top: number): void {
|
||||
const rowHeight = listRowHeight(smallFont);
|
||||
const listBottom = image.height - 30;
|
||||
const visibleRows = Math.max(1, Math.floor((listBottom - top) / rowHeight));
|
||||
idleSelection = Math.max(0, Math.min(idleSelection, entries.length - 1));
|
||||
idleScrollRow = scrollToKeepSelectionVisible(idleScrollRow, idleSelection, visibleRows, entries.length);
|
||||
const rowX = 20;
|
||||
const rowWidth = image.width - 40 - (entries.length > visibleRows ? 10 : 0);
|
||||
const labelWidth = Math.floor(rowWidth * 0.4);
|
||||
for (let row = 0; row < visibleRows; row++) {
|
||||
const index = idleScrollRow + row;
|
||||
const entry = entries[index];
|
||||
if (!entry) break;
|
||||
const y = top + row * rowHeight;
|
||||
const selected = index === idleSelection;
|
||||
if (selected) drawSelectionHighlight(image, rowX, y, rowWidth, rowHeight - 2, win.focused);
|
||||
const textX = rowX + 6;
|
||||
const textY = y + LIST_ROW_TEXT_INSET;
|
||||
if (entry.kind === "saved") {
|
||||
const label = truncateText(smallFont, entry.destination.name, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
image.drawText(
|
||||
smallFont,
|
||||
detailX,
|
||||
textY,
|
||||
truncateText(smallFont, entry.destination.address, rowX + rowWidth - 6 - detailX),
|
||||
selected ? 170 : 130,
|
||||
);
|
||||
} else {
|
||||
const label = truncateText(smallFont, entry.destination.name, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
const detail = entry.destination.place ? `${entry.destination.place} (recent)` : "(recent)";
|
||||
image.drawText(
|
||||
smallFont,
|
||||
detailX,
|
||||
textY,
|
||||
truncateText(smallFont, detail, rowX + rowWidth - 6 - detailX),
|
||||
selected ? 170 : 130,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (entries.length > visibleRows) {
|
||||
drawListScrollbar(image, rowX + rowWidth + 4, top, visibleRows * rowHeight - 2, idleScrollRow, visibleRows, entries.length);
|
||||
}
|
||||
drawFooter(image, `${GESTURE_DOUBLE_CLICK} back`);
|
||||
}
|
||||
|
||||
function paintNavigating(image: GrayImage, win: NavWindow): void {
|
||||
// Map pane (left).
|
||||
if (mapImage) {
|
||||
image.bitBlt(mapImage, 0, 0);
|
||||
if (mapMode === "follow") drawChevron(image, MAP_SIZE / 2, MAP_SIZE / 2);
|
||||
if (mapMode === "follow") {
|
||||
// Head heading relative to the map's up (its render bearing); no
|
||||
// compass reading yet means "up", i.e. the direction of travel.
|
||||
const angle = headHeadingDeg === null ? 0 : headHeadingDeg - mapFetchedBearingDeg;
|
||||
drawChevron(image, MAP_SIZE / 2, MAP_SIZE / 2, angle);
|
||||
}
|
||||
} else {
|
||||
image.drawRect(0, 0, MAP_SIZE, MAP_SIZE, 60);
|
||||
const text = mapLastError ? "Map unavailable" : "Loading map...";
|
||||
@@ -778,11 +1380,21 @@ function paintNavigating(image: GrayImage, win: NavWindow): void {
|
||||
drawFooter(image, `${GESTURE_SCROLL} zoom ${GESTURE_CLICK} ${modeHint} ${GESTURE_DOUBLE_CLICK} back`, PANEL_X + 8);
|
||||
}
|
||||
|
||||
/** Position chevron, fixed at the map center (heading-up: it always points up). */
|
||||
function drawChevron(image: GrayImage, cx: number, cy: number): void {
|
||||
/**
|
||||
* Position chevron, fixed at the map center and rotated `angleDeg` clockwise
|
||||
* from straight up (the map's render bearing) to the wearer's head heading.
|
||||
*/
|
||||
function drawChevron(image: GrayImage, cx: number, cy: number, angleDeg: number): void {
|
||||
const radians = (angleDeg * Math.PI) / 180;
|
||||
const cos = Math.cos(radians);
|
||||
const sin = Math.sin(radians);
|
||||
const triangle = (points: Array<[number, number]>, value: number): void => {
|
||||
const [a, b, c] = points.map(([x, y]): [number, number] => [cx + x * cos - y * sin, cy + x * sin + y * cos]);
|
||||
fillTriangle(image, a![0], a![1], b![0], b![1], c![0], c![1], value);
|
||||
};
|
||||
// Black halo first so it reads over bright roads.
|
||||
fillTriangle(image, cx, cy - 11, cx - 9, cy + 9, cx + 9, cy + 9, 0);
|
||||
fillTriangle(image, cx, cy - 8, cx - 6, cy + 6, cx + 6, cy + 6, 255);
|
||||
triangle([[0, -11], [-9, 9], [9, 9]], 0);
|
||||
triangle([[0, -8], [-6, 6], [6, 6]], 255);
|
||||
}
|
||||
|
||||
function fillTriangle(image: GrayImage, x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, value: number): void {
|
||||
@@ -866,6 +1478,9 @@ function render(): void {
|
||||
}
|
||||
|
||||
function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
|
||||
// Every state change funnels through here, so it doubles as the point where
|
||||
// the compass claim tracks foreground/phase/mode.
|
||||
reconcileCompass();
|
||||
if (!win.foreground && inputFrameId === 0) return;
|
||||
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${win.windowId}`);
|
||||
try {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createNightscoutAppWindow, NIGHTSCOUT_SURFACE_ID, NIGHTSCOUT_WINDOW_ID } from "./nightscout-app";
|
||||
import { createNightscoutAppWindow, startNightscoutTrayIcon, NIGHTSCOUT_SURFACE_ID, NIGHTSCOUT_WINDOW_ID } from "./nightscout-app";
|
||||
|
||||
const nightscoutApp: AppDefinition = {
|
||||
appId: "nightscout",
|
||||
title: "Nightscout",
|
||||
icon: "nightscout",
|
||||
boot: () => startNightscoutTrayIcon(),
|
||||
launch: (ctx) => ctx.launchInProcessApp(NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow),
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
export type NightscoutAlertValues = {
|
||||
cageTimestampMs: number | null;
|
||||
reservoirUnits: number | null;
|
||||
batteryVoltage: number | null;
|
||||
loopTimestampMs: number | null;
|
||||
};
|
||||
|
||||
export type NightscoutThresholds = {
|
||||
maxCannulaAgeHours: number;
|
||||
cartridgeLowUnits: number;
|
||||
batteryLowVoltage: number;
|
||||
maxLoopAgeMinutes: number;
|
||||
};
|
||||
|
||||
/** Zero disables a threshold. Missing measurements do not imply a breach. */
|
||||
export function evaluateNightscoutAlerts(values: NightscoutAlertValues, limits: NightscoutThresholds, nowMs: number) {
|
||||
const known = (value: number | null): value is number => value !== null && Number.isFinite(value) && value >= 0;
|
||||
const enabled = (value: number): boolean => Number.isFinite(value) && value > 0;
|
||||
const tooOld = (timestamp: number | null, limit: number, unitMs: number): boolean =>
|
||||
enabled(limit) && known(timestamp) && timestamp > 0 && nowMs - timestamp > limit * unitMs;
|
||||
const tooLow = (value: number | null, limit: number): boolean => enabled(limit) && known(value) && value < limit;
|
||||
const cannula = tooOld(values.cageTimestampMs, limits.maxCannulaAgeHours, 3_600_000);
|
||||
const cartridge = tooLow(values.reservoirUnits, limits.cartridgeLowUnits);
|
||||
const battery = tooLow(values.batteryVoltage, limits.batteryLowVoltage);
|
||||
const loop = tooOld(values.loopTimestampMs, limits.maxLoopAgeMinutes, 60_000);
|
||||
return { cannula, cartridge, battery, loop, any: cannula || cartridge || battery || loop };
|
||||
}
|
||||
|
||||
export function normalizeNightscoutThreshold(value: string | null | undefined): string {
|
||||
const text = (value ?? "").trim();
|
||||
const number = Number(text);
|
||||
return /^(\d+(\.\d*)?|\.\d+)$/.test(text) && Number.isFinite(number) && number >= 0 ? String(number) : "0";
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import {
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
import { evaluateNightscoutAlerts } from "./nightscout-alerts";
|
||||
import { loadNightscoutThresholds, nightscoutAlwaysShowInTopBarSetting, onAnySettingChanged } from "../../ui/dashboard-settings";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
|
||||
export const NIGHTSCOUT_WINDOW_ID = "nightscout";
|
||||
@@ -19,14 +21,34 @@ const TRAY_GRAPH_WIDTH = 48;
|
||||
const TRAY_STALE_MS = 15 * 60 * 1000;
|
||||
const TRAY_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* The Nightscout app: the full-screen glucose view (previously reached via
|
||||
* the dashboard card) in its own in-process window. While the app is running
|
||||
* it also publishes a top-bar tray icon: the latest blood-glucose value
|
||||
* (struck through when stale) plus a minimal 48px history graph.
|
||||
*/
|
||||
const windowRenders = new Set<() => void>();
|
||||
let trayStarted = false;
|
||||
let trayTick: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
function syncNightscout(): void {
|
||||
const visible = windowRenders.size > 0 || nightscoutAlwaysShowInTopBarSetting.get();
|
||||
shell.setTrayIcon(TRAY_ICON_ID, visible ? buildNightscoutTrayIcon(nightscoutBridge.snapshot()) : null);
|
||||
for (const render of windowRenders) render();
|
||||
// Age warnings and stale glucose must advance even during a stalled fetch.
|
||||
if (visible && trayTick === null) trayTick = setInterval(syncNightscout, 60_000);
|
||||
if (!visible && trayTick !== null) {
|
||||
clearInterval(trayTick);
|
||||
trayTick = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Shell-lifetime subscription so the tray can be enabled before any window opens. */
|
||||
export function startNightscoutTrayIcon(): void {
|
||||
if (trayStarted) return;
|
||||
trayStarted = true;
|
||||
nightscoutBridge.onStateChange(syncNightscout);
|
||||
onAnySettingChanged(syncNightscout);
|
||||
}
|
||||
|
||||
/** Full-screen glucose view with a shared, optionally persistent tray readout. */
|
||||
export function createNightscoutAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
let unsubscribe: (() => void) | null = null;
|
||||
startNightscoutTrayIcon();
|
||||
const render = () => app.requestRender();
|
||||
const app = createInProcessWindow({
|
||||
appId: "nightscout",
|
||||
windowId: NIGHTSCOUT_WINDOW_ID,
|
||||
@@ -41,28 +63,25 @@ export function createNightscoutAppWindow(options: InProcessAppOptions): InProce
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
unsubscribe?.();
|
||||
unsubscribe = null;
|
||||
shell.setTrayIcon(TRAY_ICON_ID, null);
|
||||
windowRenders.delete(render);
|
||||
syncNightscout();
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
// onStateChange fires immediately with the current snapshot, so this also
|
||||
// publishes the initial tray icon.
|
||||
unsubscribe = nightscoutBridge.onStateChange((state) => {
|
||||
shell.setTrayIcon(TRAY_ICON_ID, buildNightscoutTrayIcon(state));
|
||||
app.requestRender();
|
||||
});
|
||||
windowRenders.add(render);
|
||||
syncNightscout();
|
||||
return app;
|
||||
}
|
||||
|
||||
/** BG value (struck through when stale) + minimal 2-hour line graph. */
|
||||
/** BG value, 2-hour graph, then a warning triangle when any threshold is breached. */
|
||||
function buildNightscoutTrayIcon(state: NightscoutState): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const nowMs = Date.now();
|
||||
const label = state.latest ? `${state.latest.sgv}` : "--";
|
||||
const labelWidth = font.measureText(label);
|
||||
const image = new GrayImage(labelWidth + 4 + TRAY_GRAPH_WIDTH, TRAY_HEIGHT, 0);
|
||||
const warning = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs).any;
|
||||
const graphEnd = labelWidth + 4 + TRAY_GRAPH_WIDTH;
|
||||
const image = new GrayImage(graphEnd + (warning ? 24 : 0), TRAY_HEIGHT, 0);
|
||||
|
||||
const textY = Math.max(0, ((TRAY_HEIGHT - font.lineHeight) / 2) | 0);
|
||||
image.drawText(font, 0, textY, label, 220);
|
||||
@@ -72,6 +91,14 @@ function buildNightscoutTrayIcon(state: NightscoutState): GrayImage {
|
||||
}
|
||||
|
||||
drawTrayGraph(image, labelWidth + 4, state, nowMs);
|
||||
if (warning) {
|
||||
const x = graphEnd + 4;
|
||||
image.drawLine(x + 9, 3, x, 20, 255);
|
||||
image.drawLine(x, 20, x + 18, 20, 255);
|
||||
image.drawLine(x + 18, 20, x + 9, 3, 255);
|
||||
image.fillRect(x + 8, 9, 2, 6, 255);
|
||||
image.fillRect(x + 8, 17, 2, 2, 255);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { evaluateNightscoutAlerts } from "./nightscout-alerts";
|
||||
import { getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
@@ -6,6 +7,13 @@ import { Layer, LayerContext } from "../../ui/layers";
|
||||
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
|
||||
import {
|
||||
isNightscoutSettingsConfigured,
|
||||
loadNightscoutThresholds,
|
||||
nightscoutMaxCannulaAgeSetting,
|
||||
nightscoutCartridgeLowSetting,
|
||||
nightscoutBatteryLowSetting,
|
||||
nightscoutMaxLoopAgeSetting,
|
||||
nightscoutAlwaysShowInTopBarSetting,
|
||||
toggleSettingMenuItem,
|
||||
nightscoutApiTokenSetting,
|
||||
nightscoutSiteUrlSetting,
|
||||
textSettingMenuItem,
|
||||
@@ -332,21 +340,43 @@ export class NightscoutLayer implements Layer {
|
||||
|
||||
const statusX = 170;
|
||||
const statusWidth = width - 22 - statusX;
|
||||
const statusLines: { text: string; shade: number; direction?: string }[] = [
|
||||
const alerts = evaluateNightscoutAlerts(nightscout, loadNightscoutThresholds(), nowMs);
|
||||
const statusLines: { text: string; shade: number; direction?: string; fields?: { text: string; warning?: boolean }[] }[] = [
|
||||
{ text: `Delta ${formatDelta(nightscout.delta)} Trend `, shade: 180, direction: nightscout.direction },
|
||||
{
|
||||
text: `IOB ${nightscout.iob === null ? "--" : nightscout.iob.toFixed(2)} COB ${nightscout.cob === null ? "--" : formatWholeNumber(nightscout.cob)} Updated ${formatTimestamp(latest.timestampMs)}`,
|
||||
shade: 160,
|
||||
},
|
||||
{
|
||||
text: `CAGE ${formatAgeShortFromTimestamp(nightscout.cageTimestampMs, nowMs)} Loop ${nightscout.openapsStatusShort}`,
|
||||
shade: 160,
|
||||
text: "", shade: 160,
|
||||
fields: [
|
||||
{ text: `CAGE ${formatAgeShortFromTimestamp(nightscout.cageTimestampMs, nowMs)}`, warning: alerts.cannula },
|
||||
{ text: `Loop ${formatAgeShortFromTimestamp(nightscout.loopTimestampMs, nowMs)}`, warning: alerts.loop },
|
||||
],
|
||||
},
|
||||
{
|
||||
text: "", shade: 150,
|
||||
fields: [
|
||||
{ text: "Pump" },
|
||||
{ text: nightscout.reservoirUnits === null ? "--U" : `${Math.round(nightscout.reservoirUnits)}U`, warning: alerts.cartridge },
|
||||
{ text: nightscout.batteryVoltage === null ? "--V" : `${nightscout.batteryVoltage.toFixed(2)}V`, warning: alerts.battery },
|
||||
{ text: nightscout.pumpStatus || "--" },
|
||||
],
|
||||
},
|
||||
{ text: `Pump ${nightscout.pumpStatus || "--"}`, shade: 150 },
|
||||
];
|
||||
let statusY = graphTop - 8 - statusLines.length * step;
|
||||
for (const line of statusLines) {
|
||||
if (line.direction !== undefined) {
|
||||
if (line.fields) {
|
||||
let x = statusX;
|
||||
for (const field of line.fields) {
|
||||
const text = truncateText(font, field.text, Math.max(0, statusX + statusWidth - x));
|
||||
const textWidth = font.measureText(text);
|
||||
if (field.warning && textWidth > 0) image.fillRect(x - 2, statusY - 1, textWidth + 4, font.lineHeight + 2, 230);
|
||||
image.drawText(font, x, statusY, text, field.warning ? 0 : line.shade);
|
||||
x += textWidth + font.measureText(" ");
|
||||
if (x >= statusX + statusWidth) break;
|
||||
}
|
||||
} else if (line.direction !== undefined) {
|
||||
image.drawText(font, statusX, statusY, line.text, line.shade);
|
||||
drawDirectionIndicator(image, font, statusX + font.measureText(line.text), statusY, line.direction, line.shade);
|
||||
} else {
|
||||
@@ -390,6 +420,11 @@ export function nightscoutMenuItems(): MenuItem[] {
|
||||
openSettingsSubMenu(ctx, "Nightscout settings", [
|
||||
textSettingMenuItem(nightscoutSiteUrlSetting),
|
||||
textSettingMenuItem(nightscoutApiTokenSetting),
|
||||
textSettingMenuItem(nightscoutMaxCannulaAgeSetting),
|
||||
textSettingMenuItem(nightscoutCartridgeLowSetting),
|
||||
textSettingMenuItem(nightscoutBatteryLowSetting),
|
||||
textSettingMenuItem(nightscoutMaxLoopAgeSetting),
|
||||
toggleSettingMenuItem(nightscoutAlwaysShowInTopBarSetting),
|
||||
]);
|
||||
},
|
||||
},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { NotificationsListLayer } from "../../ui/notifications";
|
||||
import { NotificationFilterLayer } from "../../ui/notification-filter";
|
||||
import { onAndroidNotificationPosted } from "../../native/notification-icons";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
@@ -25,6 +26,13 @@ export function createNotificationsAppWindow(options: InProcessAppOptions): InPr
|
||||
iconLetter: "N",
|
||||
icon: "bell",
|
||||
closeable: true,
|
||||
menuItems: () => [{
|
||||
label: "Notification filter",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new NotificationFilterLayer());
|
||||
},
|
||||
}],
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(new NotificationsListLayer()),
|
||||
submitFrame: options.submitFrame,
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
@@ -29,7 +29,8 @@ import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import {
|
||||
directionalFallback,
|
||||
@@ -71,7 +72,7 @@ const GRAVITY = 240;
|
||||
const DRAG_PER_S = 0.12;
|
||||
const MAX_SPEED = 600;
|
||||
const PHYSICS_DT = 1 / 120;
|
||||
const RENDER_TICK_MS = 110;
|
||||
const RENDER_TICK_MS = 50;
|
||||
/** Launch speed by power setting (index = power - 1). */
|
||||
const LAUNCH_SPEEDS = [320, 380, 440, 500, 560] as const;
|
||||
const BUMPER_KICK = 340;
|
||||
@@ -242,12 +243,13 @@ type Flipper = {
|
||||
type PinballWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
/** Whether this window is the shell's input target (pushed with each message). */
|
||||
focused: boolean;
|
||||
/** Long-press window menu; created on first open. */
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: GamePhase;
|
||||
/** "ready" = parked on the plunger awaiting launch; "live" = in play. */
|
||||
@@ -301,6 +303,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const window: PinballWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -443,9 +446,9 @@ function playSfx(window: PinballWindow, steps: Step[], minor = false): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's long-press menu (game actions + default entries). */
|
||||
function openWindowMenu(window: PinballWindow): void {
|
||||
windowMenu(window).open([
|
||||
/** The window's context menu (game actions), offered while paused or over; playing keeps long-press for the nudge. */
|
||||
function windowMenuItems(window: PinballWindow): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: "New game",
|
||||
onSelect: (ctx) => {
|
||||
@@ -462,13 +465,17 @@ function openWindowMenu(window: PinballWindow): void {
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
...defaultWindowMenuItems(window.windowId, post),
|
||||
]);
|
||||
];
|
||||
}
|
||||
|
||||
function windowMenu(window: PinballWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
|
||||
claimsLongPress: () => window.phase === "playing",
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -547,6 +554,10 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n
|
||||
}
|
||||
nudge(window);
|
||||
break;
|
||||
case "short-then-long-press":
|
||||
// No context menu mid-game, so this opens the system menu instead.
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
case "double-click":
|
||||
window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
@@ -573,8 +584,8 @@ function handleIdleInput(window: PinballWindow, event: InputEvent, frameId: numb
|
||||
frameTimings.finishFrame(frameId, "discarded: pinball yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: pinball ignored input");
|
||||
@@ -945,10 +956,7 @@ function drawFlipper(image: GrayImage, flipper: Flipper): void {
|
||||
}
|
||||
|
||||
function paint(window: PinballWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(paintContent(window));
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: PinballWindow): GrayImage {
|
||||
|
||||
@@ -104,7 +104,7 @@ async function roamPost(endpoint: "q" | "pull" | "write", body: object): Promise
|
||||
const graph = roamGraphNameSetting.get();
|
||||
const token = roamApiTokenSetting.get();
|
||||
if (!graph || !token) {
|
||||
throw new Error("Set the Roam graph name and API token from the Roam app's long-press menu.");
|
||||
throw new Error("Set the Roam graph name and API token from the Roam app's context menu (tap, then hold).");
|
||||
}
|
||||
const path = `/api/graph/${encodeURIComponent(graph)}/${endpoint}`;
|
||||
const payload = JSON.stringify(body);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* todo or follows a [[page]] link, double-click walks back through visited
|
||||
* pages then yields focus. Edits (check/uncheck, add todo, edit block) go
|
||||
* through the Roam backend API and are also exposed as assistant tools.
|
||||
* The graph name and API token are configured here too, from the long-press
|
||||
* The graph name and API token are configured here too, from the context
|
||||
* menu, via the phone app's text editor.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
@@ -17,7 +17,7 @@ import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { onSettingsStoreChanged } from "../../native/settings-store";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
|
||||
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
@@ -64,6 +64,7 @@ const smallFont = getDefaultSmallFont();
|
||||
type RoamWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
@@ -168,6 +169,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
window = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -327,6 +329,10 @@ function maybeRefreshStalePage(): void {
|
||||
function windowMenu(win: RoamWindow): WindowMenu {
|
||||
if (!win.menu) {
|
||||
win.menu = new WindowMenu({
|
||||
windowId: win.windowId,
|
||||
post,
|
||||
title: () => win.title,
|
||||
items: () => menuItems(win),
|
||||
size: { width: win.viewportWidth, height: win.viewportHeight },
|
||||
paintBase: () => paintContent(win),
|
||||
isFocused: () => win.focused,
|
||||
@@ -377,7 +383,7 @@ function menuItems(win: RoamWindow): MenuItem[] {
|
||||
beginSettingEdit(roamApiTokenSetting);
|
||||
},
|
||||
});
|
||||
return [...items, ...defaultWindowMenuItems(win.windowId, post)];
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -417,8 +423,8 @@ function handleInput(win: RoamWindow, event: InputEvent, frameId: number): void
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (event.type === "long-press") {
|
||||
windowMenu(win).open(menuItems(win));
|
||||
if (event.type === "short-then-long-press") {
|
||||
windowMenu(win).open();
|
||||
renderAndSubmit(win, frameId);
|
||||
return;
|
||||
}
|
||||
@@ -519,7 +525,7 @@ async function addTodoFromText(text: string): Promise<void> {
|
||||
|
||||
async function handleRoamTool(name: string, args: any): Promise<ToolResult> {
|
||||
if (!isRoamConfigured()) {
|
||||
return { ok: false, error: "Roam is not configured; set the graph name and API token from the Roam app's long-press menu." };
|
||||
return { ok: false, error: "Roam is not configured; set the graph name and API token from the Roam app's context menu (tap, then hold)." };
|
||||
}
|
||||
switch (name) {
|
||||
case "read_page": {
|
||||
@@ -580,9 +586,9 @@ async function handleRoamTool(name: string, args: any): Promise<ToolResult> {
|
||||
// Painting
|
||||
|
||||
function paint(win: RoamWindow): Plane[] {
|
||||
if (win.menu?.isOpen()) return win.menu.paint();
|
||||
if (editingSetting) return singlePlane(paintSettingEdit(win, editingSetting));
|
||||
return singlePlane(paintContent(win));
|
||||
return windowMenu(win).paint(() =>
|
||||
singlePlane(editingSetting ? paintSettingEdit(win, editingSetting) : paintContent(win)),
|
||||
);
|
||||
}
|
||||
|
||||
/** The "type on the phone" screen shown while a Roam setting is being edited. */
|
||||
@@ -628,7 +634,7 @@ function paintContent(win: RoamWindow): GrayImage {
|
||||
image.drawLine(DOC_MARGIN, HEADER_HEIGHT - 4, win.viewportWidth - DOC_MARGIN, HEADER_HEIGHT - 4, 40);
|
||||
|
||||
if (!isRoamConfigured()) {
|
||||
drawBodyMessage(image, win, "Set the Roam graph name and API token: long-press for the menu, then pick Set graph name and Set API token.");
|
||||
drawBodyMessage(image, win, "Set the Roam graph name and API token: tap, then hold, for the menu; pick Set graph name and Set API token.");
|
||||
} else if (errorMessage) {
|
||||
drawBodyMessage(image, win, `${errorMessage}\n\n${GESTURE_CLICK} retry`);
|
||||
} else if (currentPage && currentPage.children.length === 0 && !loading) {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createTeleprompterAppWindow, TELEPROMPTER_SURFACE_ID, TELEPROMPTER_WINDOW_ID } from "./teleprompter-app";
|
||||
|
||||
const teleprompterApp: AppDefinition = {
|
||||
appId: "teleprompter",
|
||||
title: "Teleprompter",
|
||||
icon: "scroll-text",
|
||||
launch: (ctx) =>
|
||||
ctx.launchInProcessApp(TELEPROMPTER_WINDOW_ID, TELEPROMPTER_SURFACE_ID, (options) =>
|
||||
createTeleprompterAppWindow({
|
||||
...options,
|
||||
startContinuousVoiceCapture: () => void options.actions.startContinuousVoiceCapture(),
|
||||
stopContinuousVoiceCapture: () => void options.actions.stopContinuousVoiceCapture(),
|
||||
appendLog: (message) => ctx.appendLog(message),
|
||||
}),
|
||||
),
|
||||
};
|
||||
|
||||
export default teleprompterApp;
|
||||
@@ -0,0 +1,300 @@
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { ScriptTracker, tokenizeScript, type TokenizedScript } from "./script-tracker";
|
||||
|
||||
const MARGIN_X = 18;
|
||||
const BODY_TOP = 6;
|
||||
/** Gap between the last body line and the footer. */
|
||||
const FOOTER_GAP = 4;
|
||||
/** Lines per ring scroll tick. */
|
||||
const SCROLL_STEP_LINES = 2;
|
||||
|
||||
/** Brightness of words already spoken, still to come, and the next one. */
|
||||
const PAST_VALUE = 90;
|
||||
const UPCOMING_VALUE = 200;
|
||||
const NEXT_VALUE = 255;
|
||||
|
||||
type LayoutSpan = { tokenIndex: number; x: number; width: number; text: string };
|
||||
type LayoutLine = { spans: LayoutSpan[] };
|
||||
|
||||
export type ReaderHooks = {
|
||||
requestRender: () => void;
|
||||
/** Hold continuous mic capture while tracking is on. */
|
||||
startCapture: () => void;
|
||||
stopCapture: () => void;
|
||||
/** The reader closed (or paused) at this word position. */
|
||||
savePosition: (position: number) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* The teleprompter's reading view. Lays the script out word by word in the
|
||||
* medium UI font, follows the speaker through it from the live transcript
|
||||
* (see ScriptTracker), auto-scrolls to keep the next word about a third of
|
||||
* the way down, and highlights that word. Ring scrolls move the view by hand
|
||||
* and re-anchor the tracker to what is now at the anchor row, so scrolling
|
||||
* doubles as "I'm here now". Click pauses/resumes tracking; double-click
|
||||
* goes back to the file list.
|
||||
*/
|
||||
export class TeleprompterReaderLayer implements Layer {
|
||||
private readonly script: TokenizedScript;
|
||||
private readonly tracker: ScriptTracker;
|
||||
private lines: LayoutLine[] | null = null;
|
||||
/** Line index of each word (parallel to script.words). */
|
||||
private wordLines: number[] = [];
|
||||
private layoutKey = "";
|
||||
private firstLine = 0;
|
||||
/** True after the tracker moved: the next paint re-centers on the position. */
|
||||
private autoScrollPending = true;
|
||||
private tracking = true;
|
||||
private status = "";
|
||||
private unsubscribeTranscript: (() => void) | null = null;
|
||||
private unsubscribeStatus: (() => void) | null = null;
|
||||
// Geometry from the last paint, for the input handlers.
|
||||
private visibleRows = 10;
|
||||
private anchorRow = 3;
|
||||
|
||||
constructor(
|
||||
text: string,
|
||||
private readonly title: string,
|
||||
private readonly hooks: ReaderHooks,
|
||||
initialPosition = 0,
|
||||
) {
|
||||
this.script = tokenizeScript(text);
|
||||
this.tracker = new ScriptTracker(this.script.words);
|
||||
this.tracker.anchor(initialPosition);
|
||||
}
|
||||
|
||||
/** Start listening. Called once the layer is on its stack. */
|
||||
attach(): void {
|
||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||
this.status = state.status;
|
||||
this.hooks.requestRender();
|
||||
});
|
||||
this.startTracking();
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.hooks.savePosition(this.resumePosition());
|
||||
this.stopTracking();
|
||||
this.unsubscribeStatus?.();
|
||||
this.unsubscribeStatus = null;
|
||||
}
|
||||
|
||||
get position(): number {
|
||||
return this.tracker.position;
|
||||
}
|
||||
|
||||
buildMenuItems(): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: this.tracking ? "Pause voice tracking" : "Resume voice tracking",
|
||||
onSelect: (ctx) => {
|
||||
this.toggleTracking();
|
||||
ctx.stack.pop();
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Restart from the top",
|
||||
onSelect: (ctx) => {
|
||||
this.tracker.anchor(0);
|
||||
this.firstLine = 0;
|
||||
this.autoScrollPending = true;
|
||||
ctx.stack.pop();
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultMediumFont();
|
||||
const smallFont = getDefaultSmallFont();
|
||||
const step = lineStep(font);
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
|
||||
const footerY = height - smallFont.lineHeight - 4;
|
||||
this.visibleRows = Math.max(1, Math.floor((footerY - FOOTER_GAP - BODY_TOP) / step));
|
||||
this.anchorRow = Math.floor(this.visibleRows / 3);
|
||||
const lines = this.layout(font, width - 2 * MARGIN_X);
|
||||
|
||||
if (this.autoScrollPending) {
|
||||
this.autoScrollPending = false;
|
||||
this.firstLine = this.clampFirstLine(this.lineOfPosition() - this.anchorRow);
|
||||
} else {
|
||||
this.firstLine = this.clampFirstLine(this.firstLine);
|
||||
}
|
||||
|
||||
const position = this.tracker.position;
|
||||
const nextToken = position < this.script.words.length ? this.script.wordTokens[position]! : this.script.tokens.length;
|
||||
const lastLine = Math.min(lines.length, this.firstLine + this.visibleRows);
|
||||
for (let lineIndex = this.firstLine; lineIndex < lastLine; lineIndex++) {
|
||||
const y = BODY_TOP + (lineIndex - this.firstLine) * step;
|
||||
for (const span of lines[lineIndex]!.spans) {
|
||||
const isNext = span.tokenIndex === nextToken;
|
||||
const value = isNext ? NEXT_VALUE : span.tokenIndex < nextToken ? PAST_VALUE : UPCOMING_VALUE;
|
||||
image.drawText(font, MARGIN_X + span.x, y, span.text, value);
|
||||
if (isNext) {
|
||||
image.fillRect(MARGIN_X + span.x, y + font.lineHeight - 2, span.width, 2, NEXT_VALUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.paintFooter(image, smallFont, width, footerY);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||
switch (event.type) {
|
||||
case "scroll-down":
|
||||
this.scrollBy(SCROLL_STEP_LINES);
|
||||
return;
|
||||
case "scroll-up":
|
||||
this.scrollBy(-SCROLL_STEP_LINES);
|
||||
return;
|
||||
case "click":
|
||||
this.toggleTracking();
|
||||
return;
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private paintFooter(image: GrayImage, font: UiFont, width: number, y: number): void {
|
||||
const total = this.script.words.length;
|
||||
const percent = total === 0 ? 0 : Math.round((100 * this.tracker.position) / total);
|
||||
const right = `${this.tracking ? this.status || "Listening" : "Paused"} ${percent}%`;
|
||||
const rightWidth = font.measureText(right);
|
||||
image.drawText(font, width - MARGIN_X - rightWidth, y, right, 110);
|
||||
const titleMax = width - 2 * MARGIN_X - rightWidth - 12;
|
||||
if (titleMax > 20) {
|
||||
image.drawText(font, MARGIN_X, y, truncateText(font, this.title, titleMax), 110);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hand-scroll by whole lines. The tracker is re-anchored to the first word
|
||||
* at (or after) the anchor row, so the highlight follows the view and
|
||||
* speech matching resumes from there.
|
||||
*/
|
||||
private scrollBy(deltaLines: number): void {
|
||||
const lines = this.lines;
|
||||
if (!lines) return;
|
||||
this.autoScrollPending = false;
|
||||
this.firstLine = this.clampFirstLine(this.firstLine + deltaLines);
|
||||
const anchorLine = this.firstLine + this.anchorRow;
|
||||
const word = this.firstWordAtOrAfterLine(anchorLine);
|
||||
this.tracker.anchor(word);
|
||||
}
|
||||
|
||||
private clampFirstLine(value: number): number {
|
||||
const lineCount = this.lines?.length ?? 0;
|
||||
// Scroll far enough that the last line can reach the anchor row.
|
||||
return clamp(value, 0, Math.max(0, lineCount - this.anchorRow - 1));
|
||||
}
|
||||
|
||||
private lineOfPosition(): number {
|
||||
const position = this.tracker.position;
|
||||
if (this.wordLines.length === 0) return 0;
|
||||
if (position >= this.wordLines.length) return this.wordLines[this.wordLines.length - 1]!;
|
||||
return this.wordLines[position]!;
|
||||
}
|
||||
|
||||
private firstWordAtOrAfterLine(line: number): number {
|
||||
for (let word = 0; word < this.wordLines.length; word++) {
|
||||
if (this.wordLines[word]! >= line) return word;
|
||||
}
|
||||
return this.wordLines.length;
|
||||
}
|
||||
|
||||
/** Where to resume next time: a script read to the end restarts from the top. */
|
||||
private resumePosition(): number {
|
||||
return this.tracker.position >= this.script.words.length ? 0 : this.tracker.position;
|
||||
}
|
||||
|
||||
private toggleTracking(): void {
|
||||
if (this.tracking) {
|
||||
this.stopTracking();
|
||||
this.hooks.savePosition(this.resumePosition());
|
||||
} else {
|
||||
this.startTracking();
|
||||
}
|
||||
}
|
||||
|
||||
private startTracking(): void {
|
||||
this.tracking = true;
|
||||
if (!this.unsubscribeTranscript) {
|
||||
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => this.onTranscript(event));
|
||||
}
|
||||
this.hooks.startCapture();
|
||||
}
|
||||
|
||||
private stopTracking(): void {
|
||||
this.tracking = false;
|
||||
this.unsubscribeTranscript?.();
|
||||
this.unsubscribeTranscript = null;
|
||||
this.hooks.stopCapture();
|
||||
}
|
||||
|
||||
private onTranscript(event: VoiceTranscriptEvent): void {
|
||||
if (!this.tracking) return;
|
||||
if (this.tracker.feed(event.text)) {
|
||||
this.autoScrollPending = true;
|
||||
this.hooks.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
/** Word-by-word greedy line layout; cached per font and width. */
|
||||
private layout(font: UiFont, maxWidth: number): LayoutLine[] {
|
||||
const key = `${font.fingerprintId}:${maxWidth}`;
|
||||
if (this.lines && this.layoutKey === key) return this.lines;
|
||||
const spaceWidth = font.measureText(" ");
|
||||
const lines: LayoutLine[] = [];
|
||||
const wordLines: number[] = [];
|
||||
let current: LayoutLine = { spans: [] };
|
||||
let currentWidth = 0;
|
||||
let paragraph = -1;
|
||||
const flush = () => {
|
||||
lines.push(current);
|
||||
current = { spans: [] };
|
||||
currentWidth = 0;
|
||||
};
|
||||
for (let tokenIndex = 0; tokenIndex < this.script.tokens.length; tokenIndex++) {
|
||||
const token = this.script.tokens[tokenIndex]!;
|
||||
if (token.paragraph !== paragraph) {
|
||||
// New paragraph: end the current line, and leave one empty line per
|
||||
// blank source line (paragraph numbers count every newline).
|
||||
if (paragraph >= 0) {
|
||||
flush();
|
||||
for (let blank = paragraph + 1; blank < token.paragraph; blank++) flush();
|
||||
}
|
||||
paragraph = token.paragraph;
|
||||
}
|
||||
const wordWidth = font.measureText(token.text);
|
||||
const x = current.spans.length === 0 ? 0 : currentWidth + spaceWidth;
|
||||
if (current.spans.length > 0 && x + wordWidth > maxWidth) {
|
||||
flush();
|
||||
}
|
||||
const spanX = current.spans.length === 0 ? 0 : currentWidth + spaceWidth;
|
||||
current.spans.push({ tokenIndex, x: spanX, width: wordWidth, text: token.text });
|
||||
currentWidth = spanX + wordWidth;
|
||||
if (token.wordIndex >= 0) {
|
||||
wordLines[token.wordIndex] = lines.length;
|
||||
}
|
||||
}
|
||||
if (current.spans.length > 0 || lines.length === 0) flush();
|
||||
this.lines = lines;
|
||||
this.wordLines = wordLines;
|
||||
this.layoutKey = key;
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
|
||||
/**
|
||||
* The Teleprompter's recently opened scripts, most recent first, with the
|
||||
* word position the reader was at when it closed (so reopening resumes
|
||||
* there). Stored as a JSON array in the settings store.
|
||||
*/
|
||||
export type RecentScript = {
|
||||
path: string;
|
||||
name: string;
|
||||
/** Word index the reader was at when last closed; 0 = start. */
|
||||
position: number;
|
||||
openedMs: number;
|
||||
};
|
||||
|
||||
const RECENTS_KEY = "teleprompter.recents";
|
||||
const MAX_RECENTS = 12;
|
||||
|
||||
export function getRecentScripts(): RecentScript[] {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(getStringSetting(RECENTS_KEY, "[]"));
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed
|
||||
.filter((entry): entry is RecentScript => !!entry && typeof entry === "object" && typeof entry.path === "string")
|
||||
.map((entry) => ({
|
||||
path: entry.path,
|
||||
name: typeof entry.name === "string" && entry.name ? entry.name : baseName(entry.path),
|
||||
position: typeof entry.position === "number" && entry.position >= 0 ? entry.position : 0,
|
||||
openedMs: typeof entry.openedMs === "number" ? entry.openedMs : 0,
|
||||
}));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function saveRecentScripts(recents: RecentScript[]): void {
|
||||
setStringSetting(RECENTS_KEY, JSON.stringify(recents.slice(0, MAX_RECENTS)));
|
||||
}
|
||||
|
||||
/** Move (or add) a script to the top of the list, keeping its saved position. */
|
||||
export function touchRecentScript(path: string, name: string): RecentScript {
|
||||
const recents = getRecentScripts();
|
||||
const existing = recents.find((entry) => entry.path === path);
|
||||
const entry: RecentScript = {
|
||||
path,
|
||||
name,
|
||||
position: existing?.position ?? 0,
|
||||
openedMs: Date.now(),
|
||||
};
|
||||
saveRecentScripts([entry, ...recents.filter((other) => other.path !== path)]);
|
||||
return entry;
|
||||
}
|
||||
|
||||
export function saveRecentScriptPosition(path: string, position: number): void {
|
||||
const recents = getRecentScripts();
|
||||
const entry = recents.find((other) => other.path === path);
|
||||
if (!entry) return;
|
||||
entry.position = Math.max(0, position);
|
||||
saveRecentScripts(recents);
|
||||
}
|
||||
|
||||
export function removeRecentScript(path: string): void {
|
||||
saveRecentScripts(getRecentScripts().filter((entry) => entry.path !== path));
|
||||
}
|
||||
|
||||
export function clearRecentScripts(): void {
|
||||
saveRecentScripts([]);
|
||||
}
|
||||
|
||||
export function baseName(path: string): string {
|
||||
const slash = path.lastIndexOf("/");
|
||||
return slash >= 0 ? path.slice(slash + 1) : path;
|
||||
}
|
||||
|
||||
/** The last directory segment of a file path, for a dim "where it lives" hint. */
|
||||
export function parentDirName(path: string): string {
|
||||
const slash = path.lastIndexOf("/");
|
||||
if (slash <= 0) return "";
|
||||
return baseName(path.slice(0, slash));
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
/**
|
||||
* Follows a speaker through a script from live speech-to-text. Pure logic
|
||||
* (no NativeScript imports) so it runs under plain node for the tests.
|
||||
*
|
||||
* The script is tokenized once into display tokens; the subset with a
|
||||
* non-empty normalized key are the "words" the tracker aligns against. The
|
||||
* tracker keeps `position`: the index (in word space) of the next word the
|
||||
* speaker is expected to say. Each transcript update (replace semantics: the
|
||||
* whole current utterance, partial or final) re-aligns the tail of the
|
||||
* transcript against a window of script words around the current position,
|
||||
* with a local alignment that tolerates misrecognized, skipped and ad-libbed
|
||||
* words, and moves the position to just after the last aligned script word.
|
||||
*/
|
||||
|
||||
export type ScriptToken = {
|
||||
/** The token as written, for display. */
|
||||
text: string;
|
||||
/** Paragraph number (newline-separated), for layout. */
|
||||
paragraph: number;
|
||||
/** Lowercased alphanumeric key, or "" for punctuation-only tokens. */
|
||||
key: string;
|
||||
/** Index into the tracker's word list, or -1 when key is empty. */
|
||||
wordIndex: number;
|
||||
};
|
||||
|
||||
export type TokenizedScript = {
|
||||
tokens: ScriptToken[];
|
||||
/** Normalized keys of the trackable tokens, in order. */
|
||||
words: string[];
|
||||
/** Token index of each word. */
|
||||
wordTokens: number[];
|
||||
};
|
||||
|
||||
/** Lowercase, strip everything but letters/digits, fold common apostrophes. */
|
||||
export function normalizeWord(raw: string): string {
|
||||
return raw
|
||||
.normalize("NFKD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "");
|
||||
}
|
||||
|
||||
export function tokenizeScript(text: string): TokenizedScript {
|
||||
const tokens: ScriptToken[] = [];
|
||||
const words: string[] = [];
|
||||
const wordTokens: number[] = [];
|
||||
const paragraphs = text.replace(/\r/g, "").split("\n");
|
||||
for (let paragraph = 0; paragraph < paragraphs.length; paragraph++) {
|
||||
for (const raw of paragraphs[paragraph]!.split(/\s+/)) {
|
||||
if (!raw) continue;
|
||||
const key = normalizeWord(raw);
|
||||
const wordIndex = key ? words.length : -1;
|
||||
if (key) {
|
||||
words.push(key);
|
||||
wordTokens.push(tokens.length);
|
||||
}
|
||||
tokens.push({ text: raw, paragraph, key, wordIndex });
|
||||
}
|
||||
}
|
||||
return { tokens, words, wordTokens };
|
||||
}
|
||||
|
||||
/** Split a transcript into normalized keys, dropping empties. */
|
||||
export function transcriptKeys(text: string): string[] {
|
||||
return text
|
||||
.split(/\s+/)
|
||||
.map(normalizeWord)
|
||||
.filter((key) => key.length > 0);
|
||||
}
|
||||
|
||||
// Function words match so often that on their own they say little about
|
||||
// where the speaker is; they count for less than content words.
|
||||
const STOP_WORDS = new Set([
|
||||
"a", "an", "the", "and", "or", "but", "of", "to", "in", "on", "at", "for", "is", "it", "its",
|
||||
"i", "we", "you", "he", "she", "they", "that", "this", "as", "be", "are", "was", "were",
|
||||
"with", "by", "so", "if", "not", "no", "do", "have", "has", "had", "will", "can",
|
||||
]);
|
||||
|
||||
const MATCH_SCORE = 3;
|
||||
const STOP_WORD_MATCH_SCORE = 1.5;
|
||||
/** A fuzzy (edit-distance 1) or prefix match: real evidence, but weaker. */
|
||||
const FUZZY_MATCH_SCORE = 2;
|
||||
const MISMATCH_SCORE = -2;
|
||||
const GAP_SCORE = -1.5;
|
||||
/** The alignment must be worth at least this to move the position at all. */
|
||||
const MIN_ALIGNMENT_SCORE = 4.5;
|
||||
const MIN_MATCHED_WORDS = 2;
|
||||
/** Per-word cost of ending an alignment away from the expected position (a tie-breaker). */
|
||||
const FORWARD_DISTANCE_COST = 0.02;
|
||||
const BACKWARD_DISTANCE_COST = 0.1;
|
||||
|
||||
export type TrackerOptions = {
|
||||
/** Script words before the current position that stay in play (repeats, backtracking). */
|
||||
back?: number;
|
||||
/** Script words after the current position that stay in play (skipping ahead). */
|
||||
ahead?: number;
|
||||
/** Only the last this-many transcript words are aligned. */
|
||||
tailWords?: number;
|
||||
};
|
||||
|
||||
const DEFAULT_OPTIONS: Required<TrackerOptions> = { back: 30, ahead: 160, tailWords: 20 };
|
||||
|
||||
/**
|
||||
* Edit distance capped at 1 (true when the strings are within one edit),
|
||||
* which covers the typical STT slip: a dropped/extra letter or a substitution.
|
||||
*/
|
||||
export function withinOneEdit(a: string, b: string): boolean {
|
||||
if (a === b) return true;
|
||||
const lengthDelta = a.length - b.length;
|
||||
if (lengthDelta > 1 || lengthDelta < -1) return false;
|
||||
const [short, long] = a.length <= b.length ? [a, b] : [b, a];
|
||||
let i = 0;
|
||||
let j = 0;
|
||||
let edits = 0;
|
||||
while (i < short.length && j < long.length) {
|
||||
if (short[i] === long[j]) {
|
||||
i++;
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
if (++edits > 1) return false;
|
||||
if (short.length === long.length) {
|
||||
i++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return edits + (long.length - j) <= 1;
|
||||
}
|
||||
|
||||
/** Score of aligning transcript key `spoken` with script key `expected`. */
|
||||
function matchScore(spoken: string, expected: string, isLastSpoken: boolean): number {
|
||||
if (spoken === expected) {
|
||||
return STOP_WORDS.has(expected) ? STOP_WORD_MATCH_SCORE : MATCH_SCORE;
|
||||
}
|
||||
// The final transcript word is often still being recognized: a prefix of
|
||||
// the script word counts, once it is long enough not to be noise.
|
||||
if (isLastSpoken && spoken.length >= 2 && expected.length > spoken.length && expected.startsWith(spoken)) {
|
||||
return FUZZY_MATCH_SCORE;
|
||||
}
|
||||
if (spoken.length >= 5 && expected.length >= 5 && withinOneEdit(spoken, expected)) {
|
||||
return FUZZY_MATCH_SCORE;
|
||||
}
|
||||
return MISMATCH_SCORE;
|
||||
}
|
||||
|
||||
export type AlignmentResult = {
|
||||
/** Index (in the script word list) of the word after the last aligned one; the new position. */
|
||||
position: number;
|
||||
score: number;
|
||||
matchedWords: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Align the transcript tail against script words [windowStart, windowEnd).
|
||||
* Local alignment (free start on both sides) that must consume the last
|
||||
* transcript word and may end anywhere in the window. Returns null when the
|
||||
* best alignment is too weak to trust.
|
||||
*/
|
||||
export function alignTail(
|
||||
words: readonly string[],
|
||||
spoken: readonly string[],
|
||||
windowStart: number,
|
||||
windowEnd: number,
|
||||
expectedPosition: number,
|
||||
): AlignmentResult | null {
|
||||
const m = spoken.length;
|
||||
const n = windowEnd - windowStart;
|
||||
if (m === 0 || n <= 0) return null;
|
||||
|
||||
// H[i][j]: best score of an alignment ending with spoken[i-1] / window[j-1]
|
||||
// (or a gap), clamped at 0 (free start). Matches counts how many spoken
|
||||
// words matched along the path that produced H[i][j].
|
||||
const width = n + 1;
|
||||
const score = new Float64Array((m + 1) * width);
|
||||
const matches = new Uint16Array((m + 1) * width);
|
||||
for (let i = 1; i <= m; i++) {
|
||||
const spokenWord = spoken[i - 1]!;
|
||||
const isLast = i === m;
|
||||
for (let j = 1; j <= n; j++) {
|
||||
const here = i * width + j;
|
||||
const s = matchScore(spokenWord, words[windowStart + j - 1]!, isLast);
|
||||
const diagonal = score[here - width - 1]! + s;
|
||||
const skipScript = score[here - 1]! + GAP_SCORE;
|
||||
const skipSpoken = score[here - width]! + GAP_SCORE;
|
||||
let best = 0;
|
||||
let bestMatches = 0;
|
||||
if (diagonal > best) {
|
||||
best = diagonal;
|
||||
bestMatches = matches[here - width - 1]! + (s > 0 ? 1 : 0);
|
||||
}
|
||||
if (skipScript > best) {
|
||||
best = skipScript;
|
||||
bestMatches = matches[here - 1]!;
|
||||
}
|
||||
if (skipSpoken > best) {
|
||||
best = skipSpoken;
|
||||
bestMatches = matches[here - width]!;
|
||||
}
|
||||
score[here] = best;
|
||||
matches[here] = bestMatches;
|
||||
}
|
||||
}
|
||||
|
||||
let bestColumn = -1;
|
||||
let bestAdjusted = -Infinity;
|
||||
for (let j = 1; j <= n; j++) {
|
||||
const raw = score[m * width + j]!;
|
||||
if (raw <= 0) continue;
|
||||
const position = windowStart + j;
|
||||
const distance = position - expectedPosition;
|
||||
const adjusted = raw - (distance >= 0 ? distance * FORWARD_DISTANCE_COST : -distance * BACKWARD_DISTANCE_COST);
|
||||
if (adjusted > bestAdjusted) {
|
||||
bestAdjusted = adjusted;
|
||||
bestColumn = j;
|
||||
}
|
||||
}
|
||||
if (bestColumn < 0) return null;
|
||||
const rawScore = score[m * width + bestColumn]!;
|
||||
const matchedWords = matches[m * width + bestColumn]!;
|
||||
if (rawScore < MIN_ALIGNMENT_SCORE || matchedWords < MIN_MATCHED_WORDS) return null;
|
||||
return { position: windowStart + bestColumn, score: rawScore, matchedWords };
|
||||
}
|
||||
|
||||
export class ScriptTracker {
|
||||
private readonly options: Required<TrackerOptions>;
|
||||
/** Index of the next word the speaker is expected to say (0..words.length). */
|
||||
position = 0;
|
||||
|
||||
constructor(
|
||||
private readonly words: readonly string[],
|
||||
options: TrackerOptions = {},
|
||||
) {
|
||||
this.options = { ...DEFAULT_OPTIONS, ...options };
|
||||
}
|
||||
|
||||
get wordCount(): number {
|
||||
return this.words.length;
|
||||
}
|
||||
|
||||
/** Jump the expected position (manual scroll, restart). */
|
||||
anchor(position: number): void {
|
||||
this.position = Math.max(0, Math.min(this.words.length, position));
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a transcript update (the whole current utterance, partial or
|
||||
* final). Returns true when the position moved.
|
||||
*/
|
||||
feed(text: string): boolean {
|
||||
const keys = transcriptKeys(text);
|
||||
if (keys.length === 0) return false;
|
||||
const tail = keys.length > this.options.tailWords ? keys.slice(-this.options.tailWords) : keys;
|
||||
const windowStart = Math.max(0, this.position - this.options.back);
|
||||
const windowEnd = Math.min(this.words.length, this.position + this.options.ahead);
|
||||
const result = alignTail(this.words, tail, windowStart, windowEnd, this.position);
|
||||
if (!result || result.position === this.position) return false;
|
||||
this.position = result.position;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { readTextFile, type DirectoryEntry } from "../../native/file-access";
|
||||
import { LIST_ROW_TEXT_INSET } from "../../ui/metrics";
|
||||
import { MenuLayer, type MenuItem } from "../../ui/menu";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { appViewportSize } from "../../ui/shell/geometry";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
import { FileBrowserLayer } from "../files/file-browser";
|
||||
import { TeleprompterReaderLayer } from "./reader";
|
||||
import {
|
||||
baseName,
|
||||
clearRecentScripts,
|
||||
getRecentScripts,
|
||||
parentDirName,
|
||||
removeRecentScript,
|
||||
saveRecentScriptPosition,
|
||||
touchRecentScript,
|
||||
} from "./recent-files";
|
||||
|
||||
export const TELEPROMPTER_WINDOW_ID = "teleprompter";
|
||||
export const TELEPROMPTER_SURFACE_ID = "window:teleprompter";
|
||||
|
||||
const SCRIPT_FILE = /\.(txt|md|markdown|text|log)$/i;
|
||||
|
||||
const HOME_LAYOUT = {
|
||||
x: 8,
|
||||
y: 8,
|
||||
width: 272,
|
||||
showBorder: false,
|
||||
minHeight: 0,
|
||||
maxHeight: appViewportSize("min").height - 16,
|
||||
opaque: true,
|
||||
};
|
||||
|
||||
export type TeleprompterAppOptions = InProcessAppOptions & {
|
||||
/** Hold continuous mic capture while a script is being tracked. */
|
||||
startContinuousVoiceCapture: () => void;
|
||||
stopContinuousVoiceCapture: () => void;
|
||||
appendLog: (message: string) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* The Teleprompter app window. The home page is a Browse entry plus the
|
||||
* recently opened scripts; Browse pushes the Files app's browser to pick a
|
||||
* text file, and either path pushes the reader (see TeleprompterReaderLayer)
|
||||
* over the home page.
|
||||
*/
|
||||
export function createTeleprompterAppWindow(options: TeleprompterAppOptions): InProcessWindow {
|
||||
// The home menu's items are rebuilt in place whenever the recents change
|
||||
// (MenuLayer reads the array each paint).
|
||||
const homeItems: MenuItem[] = [];
|
||||
const home = new MenuLayer("Teleprompter", homeItems, HOME_LAYOUT);
|
||||
let created: InProcessWindow | null = null;
|
||||
let notice = "";
|
||||
|
||||
const requestRender = () => created?.requestRender();
|
||||
|
||||
/** The layer currently on top of the window's stack (the home menu at base). */
|
||||
const topLayer = (): Layer => {
|
||||
let top: Layer = home;
|
||||
created?.stack.topMatches((layer) => {
|
||||
top = layer;
|
||||
return true;
|
||||
});
|
||||
return top;
|
||||
};
|
||||
|
||||
const openScript = (ctx: LayerContext, path: string, name: string) => {
|
||||
const text = readTextFile(path);
|
||||
if (text === null) {
|
||||
removeRecentScript(path);
|
||||
notice = `Could not read ${name}`;
|
||||
options.appendLog(`teleprompter: could not read ${path}`);
|
||||
refreshHome();
|
||||
return;
|
||||
}
|
||||
const recent = touchRecentScript(path, name);
|
||||
notice = "";
|
||||
refreshHome();
|
||||
const layer = new TeleprompterReaderLayer(
|
||||
text,
|
||||
name,
|
||||
{
|
||||
requestRender,
|
||||
startCapture: () => options.startContinuousVoiceCapture(),
|
||||
stopCapture: () => options.stopContinuousVoiceCapture(),
|
||||
savePosition: (position) => saveRecentScriptPosition(path, position),
|
||||
},
|
||||
recent.position,
|
||||
);
|
||||
ctx.stack.push(layer);
|
||||
layer.attach();
|
||||
};
|
||||
|
||||
const openBrowser = (ctx: LayerContext) => {
|
||||
const stack = ctx.stack;
|
||||
const picker = new FileBrowserLayer({
|
||||
isSupportedFile: (name) => SCRIPT_FILE.test(name),
|
||||
onLeave: () => stack.pop(),
|
||||
onFilePicked: (entry: DirectoryEntry, pickCtx) => {
|
||||
if (!SCRIPT_FILE.test(entry.name)) {
|
||||
notice = "Pick a text file (.txt, .md)";
|
||||
return;
|
||||
}
|
||||
pickCtx.stack.pop();
|
||||
openScript(pickCtx, entry.path, entry.name);
|
||||
},
|
||||
});
|
||||
stack.push(picker);
|
||||
};
|
||||
|
||||
function refreshHome(): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const items: MenuItem[] = [
|
||||
{
|
||||
label: "Browse for a script...",
|
||||
onSelect: (ctx) => openBrowser(ctx),
|
||||
},
|
||||
];
|
||||
for (const recent of getRecentScripts()) {
|
||||
const folder = parentDirName(recent.path);
|
||||
items.push({
|
||||
label: recent.name || baseName(recent.path),
|
||||
onSelect: (ctx) => openScript(ctx, recent.path, recent.name),
|
||||
render: ({ image, x, y, width, selected, text }) => {
|
||||
const value = selected ? 255 : 200;
|
||||
const folderWidth = folder ? font.measureText(folder) : 0;
|
||||
const nameMax = folder ? width - folderWidth - 12 : width;
|
||||
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, truncateText(font, text, Math.max(40, nameMax)), value);
|
||||
if (folder && nameMax >= 40) {
|
||||
image.drawText(font, x + width - folderWidth, y + LIST_ROW_TEXT_INSET, folder, 90);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
if (notice) {
|
||||
items.push({ label: notice, disabled: true, onSelect: () => {} });
|
||||
}
|
||||
homeItems.length = 0;
|
||||
homeItems.push(...items);
|
||||
// Land on the most recent script (one click resumes it), else on Browse.
|
||||
home.selectItem(getRecentScripts().length > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
refreshHome();
|
||||
|
||||
created = createInProcessWindow({
|
||||
appId: "teleprompter",
|
||||
windowId: TELEPROMPTER_WINDOW_ID,
|
||||
title: "Teleprompter",
|
||||
iconLetter: "Tp",
|
||||
icon: "scroll-text",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
menuItems: () => {
|
||||
const top = topLayer();
|
||||
if (top instanceof TeleprompterReaderLayer) return top.buildMenuItems();
|
||||
if (top instanceof FileBrowserLayer) return top.buildMenuItems();
|
||||
if (top !== home) return [];
|
||||
return [
|
||||
{
|
||||
label: "Clear recent scripts",
|
||||
onSelect: (ctx) => {
|
||||
clearRecentScripts();
|
||||
refreshHome();
|
||||
ctx.stack.pop();
|
||||
},
|
||||
},
|
||||
];
|
||||
},
|
||||
onFocus: (lastInput) => {
|
||||
const top = topLayer();
|
||||
if (top instanceof FileBrowserLayer) top.onFocus(lastInput);
|
||||
},
|
||||
baseLayer: new YieldAtRootLayer(home),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: options.onClosed,
|
||||
});
|
||||
return created;
|
||||
}
|
||||
@@ -32,11 +32,11 @@
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getDefaultSmallFont, getTerminalFontConfig } from "../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { TERMINAL_ICON_GLYPHS } from "../../graphics/icons";
|
||||
import { TERMINAL_ICON_GLYPHS, type IconActivity } from "../../graphics/icons";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
@@ -48,7 +48,7 @@ import { connectionDisplayName, loadConnections, parseConnectionString, saveConn
|
||||
import { TerminalEmulator } from "./terminal-emulator";
|
||||
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import { appViewportSize } from "../../ui/shell/geometry";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
|
||||
@@ -88,6 +88,8 @@ const NEW_CONNECTION_DRAFT_KEY = "terminal.newConnectionDraft";
|
||||
type BaseWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
/** Shell-side window title, echoed back in the open-window message. */
|
||||
title: string;
|
||||
/** Content viewport from the shell's open-window message; grid = viewport / cell. */
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
@@ -283,7 +285,7 @@ const sessionRecency = new Map<string, number>();
|
||||
// continues, so 5s of slack keeps the indicator lit through the gaps.
|
||||
const sessionActivity = new Map<string, number>();
|
||||
const ACTIVITY_ACTIVE_MS = 5_000;
|
||||
// Alternation period of the hub's activity indicator.
|
||||
// Alternation period shared by hub rows and sidebar cursors.
|
||||
const HUB_ANIMATION_STEP_MS = 800;
|
||||
|
||||
function recencyKey(connectionId: string, socket: string): string {
|
||||
@@ -304,8 +306,32 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "open-window":
|
||||
openWindow(message.windowId, message.surfaceId, message.viewport);
|
||||
openWindow(message.windowId, message.surfaceId, message.title, message.viewport);
|
||||
break;
|
||||
case "resize-window": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window || (window.viewportWidth === message.viewport.width && window.viewportHeight === message.viewport.height)) break;
|
||||
window.viewportWidth = message.viewport.width;
|
||||
window.viewportHeight = message.viewport.height;
|
||||
window.menu?.resize(message.viewport);
|
||||
window.lastSubmittedFingerprint = "";
|
||||
if (window.kind === "view") {
|
||||
window.gridCols = Math.floor(window.viewportWidth / window.cellWidth);
|
||||
window.gridRows = Math.floor(window.viewportHeight / window.cellHeight);
|
||||
if (window.reconnectTimer) clearTimeout(window.reconnectTimer);
|
||||
window.reconnectTimer = null;
|
||||
window.client.stop();
|
||||
window.client.setViewport(window.gridCols, window.gridRows);
|
||||
window.emulator.dispose();
|
||||
window.emulator = new TerminalEmulator(window.gridCols, window.gridRows);
|
||||
window.receivedData = false;
|
||||
window.archive = [];
|
||||
window.scrollTop = null;
|
||||
reconnectView(window);
|
||||
}
|
||||
scheduleRender(window);
|
||||
break;
|
||||
}
|
||||
case "close-window":
|
||||
closeWindow(message.windowId);
|
||||
break;
|
||||
@@ -430,11 +456,12 @@ function cancelPendingReconnects(): void {
|
||||
}
|
||||
}
|
||||
|
||||
function openWindow(windowId: string, surfaceId: string, viewport: { width: number; height: number }): void {
|
||||
function openWindow(windowId: string, surfaceId: string, title: string, viewport: { width: number; height: number }): void {
|
||||
const pendingView = pendingViews.get(windowId);
|
||||
if (pendingView) {
|
||||
pendingViews.delete(windowId);
|
||||
windows.set(windowId, createViewWindow(windowId, surfaceId, viewport, pendingView));
|
||||
windows.set(windowId, createViewWindow(windowId, surfaceId, title, viewport, pendingView));
|
||||
updateHubAnimation();
|
||||
renderHubWindows();
|
||||
return;
|
||||
}
|
||||
@@ -443,6 +470,7 @@ function openWindow(windowId: string, surfaceId: string, viewport: { width: numb
|
||||
kind: "hub",
|
||||
windowId,
|
||||
surfaceId,
|
||||
title,
|
||||
viewportWidth: viewport.width,
|
||||
viewportHeight: viewport.height,
|
||||
gridCols: Math.floor(viewport.width / hubCell.cellWidth),
|
||||
@@ -463,6 +491,7 @@ function openWindow(windowId: string, surfaceId: string, viewport: { width: numb
|
||||
if (!controlsInitialized) {
|
||||
syncControlsFromSettings();
|
||||
}
|
||||
updateHubAnimation();
|
||||
}
|
||||
|
||||
function closeWindow(windowId: string): void {
|
||||
@@ -483,6 +512,8 @@ function closeWindow(windowId: string): void {
|
||||
endAddConnection(window);
|
||||
}
|
||||
windows.delete(windowId);
|
||||
windowIconActivity.delete(windowId);
|
||||
updateHubAnimation();
|
||||
// Auto-reconnect only runs while at least one terminal window is open.
|
||||
if (windows.size === 0) {
|
||||
for (const control of controls.values()) {
|
||||
@@ -693,42 +724,47 @@ function renderHubWindows(): void {
|
||||
}
|
||||
}
|
||||
|
||||
// Hub activity animation: while a foregrounded hub lists at least one active
|
||||
// session, a timer re-renders it so the per-row indicator alternates. The
|
||||
// timer stops itself once every session's activity ages out (its final tick
|
||||
// renders the rows indicator-free) or the hub leaves the foreground.
|
||||
// One animation clock for foreground hub rows and every terminal sidebar icon.
|
||||
// Keep ticking while the sidebar can show activity, even with the hub closed.
|
||||
let hubAnimationPhase = 0;
|
||||
let hubAnimationTimer: ReturnType<typeof setInterval> | null = null;
|
||||
const windowIconActivity = new Map<string, IconActivity>();
|
||||
|
||||
function isSessionActive(connectionId: string, socket: string): boolean {
|
||||
const at = sessionActivity.get(recencyKey(connectionId, socket));
|
||||
return at !== undefined && Date.now() - at < ACTIVITY_ACTIVE_MS;
|
||||
}
|
||||
|
||||
function hubAnimationShouldRun(): boolean {
|
||||
if (!screenOn) return false;
|
||||
let hubVisible = false;
|
||||
for (const window of windows.values()) {
|
||||
if (window.kind === "hub" && window.foreground) hubVisible = true;
|
||||
}
|
||||
if (!hubVisible) return false;
|
||||
function isWindowSessionActive(window: TerminalWindow): boolean {
|
||||
if (window.kind === "view") return isSessionActive(window.connectionId, window.socket);
|
||||
const now = Date.now();
|
||||
for (const at of sessionActivity.values()) {
|
||||
if (now - at < ACTIVITY_ACTIVE_MS) return true;
|
||||
}
|
||||
return false;
|
||||
return [...sessionActivity.values()].some((at) => now - at < ACTIVITY_ACTIVE_MS);
|
||||
}
|
||||
|
||||
/** Start or stop the animation timer to match the current state. */
|
||||
function hubAnimationShouldRun(): boolean {
|
||||
return screenOn && [...windows.values()].some(isWindowSessionActive);
|
||||
}
|
||||
|
||||
function syncWindowIconActivity(): void {
|
||||
for (const window of windows.values()) {
|
||||
const activity: IconActivity = screenOn && isWindowSessionActive(window)
|
||||
? (hubAnimationPhase === 0 ? "on" : "off") : "idle";
|
||||
if ((windowIconActivity.get(window.windowId) ?? "idle") === activity) continue;
|
||||
windowIconActivity.set(window.windowId, activity);
|
||||
post({ type: "set-icon-activity", windowId: window.windowId, activity });
|
||||
}
|
||||
}
|
||||
|
||||
/** Start or stop animation and publish changed icons, including expiry/wake. */
|
||||
function updateHubAnimation(): void {
|
||||
if (hubAnimationShouldRun()) {
|
||||
const shouldRun = hubAnimationShouldRun();
|
||||
if (shouldRun && !hubAnimationTimer) hubAnimationPhase = 0;
|
||||
syncWindowIconActivity();
|
||||
if (shouldRun) {
|
||||
if (hubAnimationTimer) return;
|
||||
hubAnimationTimer = setInterval(() => {
|
||||
hubAnimationPhase = (hubAnimationPhase + 1) % 2;
|
||||
if (!hubAnimationShouldRun() && hubAnimationTimer) {
|
||||
clearInterval(hubAnimationTimer);
|
||||
hubAnimationTimer = null;
|
||||
}
|
||||
updateHubAnimation();
|
||||
// Render even on the stopping tick, to clear expired indicators.
|
||||
renderHubWindows();
|
||||
}, HUB_ANIMATION_STEP_MS);
|
||||
@@ -753,6 +789,7 @@ function noteSessionActivity(connectionId: string, socket: string): void {
|
||||
function createViewWindow(
|
||||
windowId: string,
|
||||
surfaceId: string,
|
||||
title: string,
|
||||
viewport: { width: number; height: number },
|
||||
view: PendingView,
|
||||
): ViewWindow {
|
||||
@@ -768,6 +805,7 @@ function createViewWindow(
|
||||
kind: "view",
|
||||
windowId,
|
||||
surfaceId,
|
||||
title,
|
||||
viewportWidth: viewport.width,
|
||||
viewportHeight: viewport.height,
|
||||
gridCols,
|
||||
@@ -896,10 +934,14 @@ function reconnectView(window: ViewWindow): void {
|
||||
scheduleRender(window);
|
||||
}
|
||||
|
||||
/** The window's long-press menu, created lazily so window literals stay simple. */
|
||||
/** The window's context menu, created lazily so window literals stay simple. */
|
||||
function windowMenu(window: TerminalWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => windowMenuItems(window),
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -978,7 +1020,6 @@ function windowMenuItems(window: TerminalWindow): MenuItem[] {
|
||||
},
|
||||
);
|
||||
}
|
||||
items.push(...defaultWindowMenuItems(window.windowId, post));
|
||||
return items;
|
||||
}
|
||||
|
||||
@@ -992,8 +1033,8 @@ function handleInput(window: TerminalWindow, event: InputEvent, frameId: number)
|
||||
.then(() => renderAndSubmit(window, frameId));
|
||||
return;
|
||||
}
|
||||
if (event.type === "long-press") {
|
||||
windowMenu(window).open(windowMenuItems(window));
|
||||
if (event.type === "short-then-long-press") {
|
||||
windowMenu(window).open();
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
@@ -1426,8 +1467,7 @@ function openViewWindow(control: ControlConnection, socket: string, label: strin
|
||||
icon: "terminal",
|
||||
iconGlyph: glyph || undefined,
|
||||
focus: true,
|
||||
// Terminal views are the one full-height window kind: more rows matter
|
||||
// more than a small on-screen footprint.
|
||||
// Default to tall sessions; the app display setting can override this.
|
||||
heightMode: "max",
|
||||
});
|
||||
}
|
||||
@@ -1451,10 +1491,7 @@ async function launchAndOpenView(control: ControlConnection, preset: string): Pr
|
||||
}
|
||||
|
||||
function paint(window: TerminalWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(paintContent(window));
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: TerminalWindow): GrayImage {
|
||||
|
||||
@@ -53,6 +53,10 @@ export class TerminalEmulator {
|
||||
this.term.write(data, onProcessed);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.term.dispose();
|
||||
}
|
||||
|
||||
/** Full reset; snapshots are defined as repainting from a cleared screen. */
|
||||
reset(): void {
|
||||
this.term.reset();
|
||||
|
||||
+35
-10
@@ -1,17 +1,42 @@
|
||||
import { launchWorkerAppWindow, type AppDefinition } from "../app-definition";
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { registerTimerTools } from "../../assistant/timer-tools";
|
||||
import { timerEngine, timersWakeSetting } from "./timer-engine";
|
||||
import { createTimerAppWindow, startTimersTrayIcon, TIMER_SURFACE_ID, TIMER_WINDOW_ID } from "./timer-app";
|
||||
|
||||
/**
|
||||
* Timers, stopwatch and alarms. The clock engine boots with the shell so
|
||||
* timers and alarms fire (and the assistant's timer.* / alarm.* tools work)
|
||||
* with no window open; launching only opens the view on it.
|
||||
*/
|
||||
const timerApp: AppDefinition = {
|
||||
appId: "timer",
|
||||
title: "Timer",
|
||||
title: "Timers",
|
||||
icon: "timer",
|
||||
launch: (ctx) =>
|
||||
launchWorkerAppWindow(ctx, {
|
||||
createWorker: () => new Worker("./timer-app.worker"),
|
||||
windowId: "timer:main",
|
||||
title: "Timer",
|
||||
iconLetter: "T",
|
||||
icon: "timer",
|
||||
}),
|
||||
boot: (ctx) => {
|
||||
timerEngine.boot({
|
||||
playBuzzer: (payload) => ctx.actions.playBuzzerSequence(payload),
|
||||
onRing: (item) => {
|
||||
// Bring the ringing item on screen: focus the Timers window, then
|
||||
// wake the display if it is off (a setting, since a wearer may
|
||||
// prefer the phone alone while the glasses sleep). Only once it is
|
||||
// actually showing does the phone start its acknowledgement clock;
|
||||
// otherwise the phone adds its own sound within seconds.
|
||||
void ctx
|
||||
.launchApp("timer")
|
||||
.then(() => {
|
||||
if (timersWakeSetting.get() && !shell.isScreenOn()) shell.wake("window");
|
||||
if (shell.isScreenOn() && shell.foregroundWindow()?.windowId === TIMER_WINDOW_ID) {
|
||||
timerEngine.markDeliveredToGlasses(item);
|
||||
}
|
||||
})
|
||||
.catch((error) => console.error(`timers ring launch failed: ${error}`));
|
||||
},
|
||||
});
|
||||
startTimersTrayIcon();
|
||||
registerTimerTools((appId) => ctx.launchApp(appId));
|
||||
},
|
||||
launch: (ctx) => ctx.launchInProcessApp(TIMER_WINDOW_ID, TIMER_SURFACE_ID, createTimerAppWindow),
|
||||
};
|
||||
|
||||
export default timerApp;
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,745 +0,0 @@
|
||||
/**
|
||||
* 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 { type UiFont, GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import {
|
||||
cancelTimerNotification,
|
||||
fireTimerNotification,
|
||||
scheduleTimerNotification,
|
||||
} from "../../native/timer-notifications";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
|
||||
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");
|
||||
// Resolved per use so a font-setting change applies without a worker restart.
|
||||
function smallFont(): UiFont {
|
||||
return getDefaultSmallFont();
|
||||
}
|
||||
|
||||
type TimerView = "stopwatch" | "timers" | "editor";
|
||||
|
||||
type TimerWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
/** Whether this window is the shell's input target (pushed with each message). */
|
||||
focused: boolean;
|
||||
/** Long-press window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
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 TIMER_TOOLS: ToolSpec[] = [
|
||||
{
|
||||
name: "set_timer",
|
||||
description:
|
||||
"Start a new countdown timer. Give the duration as hours/minutes/seconds; at least one must be nonzero. Returns the timer's number and when it will finish.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
hours: { type: "number", description: "Hours component of the duration (default 0)." },
|
||||
minutes: { type: "number", description: "Minutes component of the duration (default 0)." },
|
||||
seconds: { type: "number", description: "Seconds component of the duration (default 0)." },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
availability: "open",
|
||||
},
|
||||
{
|
||||
name: "list_timers",
|
||||
description:
|
||||
"List the countdown timers: each timer's number, its original duration, and the time remaining (or that it has finished).",
|
||||
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
||||
availability: "open",
|
||||
},
|
||||
{
|
||||
name: "cancel_timer",
|
||||
description:
|
||||
"Cancel a running countdown timer, or dismiss a finished one. Give the timer number from list_timers; it may be omitted when only one timer exists. Pass all=true to clear every timer.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
timer: { type: "number", description: "1-based timer number, as shown in the timer list." },
|
||||
all: { type: "boolean", description: "Clear all timers instead of a single one." },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
availability: "open",
|
||||
},
|
||||
];
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||
// evaluation is synchronous, so the handler below is installed before any
|
||||
// queued message can be delivered.
|
||||
post({ type: "worker-ready" });
|
||||
|
||||
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,
|
||||
focused: false,
|
||||
menu: null,
|
||||
view: "stopwatch",
|
||||
stopwatchAction: 0,
|
||||
timerSelection: 0,
|
||||
editorField: 0,
|
||||
editorHours: 0,
|
||||
editorMinutes: 5,
|
||||
editorSeconds: 0,
|
||||
editorMessage: "",
|
||||
renderTimer: null,
|
||||
lastSubmittedFingerprint: "",
|
||||
});
|
||||
post({ type: "set-tools", windowId: message.windowId, tools: TIMER_TOOLS });
|
||||
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;
|
||||
}
|
||||
window.focused = message.focused;
|
||||
// Marks the main-thread -> worker hop, which is otherwise an
|
||||
// unexplained gap inside the shell's handle-input span.
|
||||
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
|
||||
handleInput(window, message.event as InputEvent, message.frameId);
|
||||
break;
|
||||
}
|
||||
case "render": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.focused = message.focused;
|
||||
renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "foreground": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.foreground = message.foreground;
|
||||
window.focused = message.focused;
|
||||
updateRenderTimer(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) updateRenderTimer(window);
|
||||
break;
|
||||
case "tool-call": {
|
||||
let result: ToolResult;
|
||||
try {
|
||||
result = handleTimerTool(message.name, message.args);
|
||||
} catch (error) {
|
||||
result = { ok: false, error: String((error as Error)?.message ?? error) };
|
||||
}
|
||||
post({ type: "tool-result", callId: message.callId, result });
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tools
|
||||
|
||||
function handleTimerTool(name: string, args: any): ToolResult {
|
||||
syncExpiredTimers();
|
||||
switch (name) {
|
||||
case "set_timer": {
|
||||
const hours = toolDurationField(args?.hours, "hours");
|
||||
const minutes = toolDurationField(args?.minutes, "minutes");
|
||||
const seconds = toolDurationField(args?.seconds, "seconds");
|
||||
const durationMs = Math.round(((hours * 60 + minutes) * 60 + seconds) * 1000);
|
||||
if (durationMs <= 0) return { ok: false, error: "set_timer requires a duration longer than zero" };
|
||||
addTimer(durationMs);
|
||||
const timer = timers[timers.length - 1]!;
|
||||
refreshWindowsAfterToolChange();
|
||||
return {
|
||||
ok: true,
|
||||
content:
|
||||
`Started a ${formatDuration(durationMs)} timer (timer ${timers.length}), ` +
|
||||
`finishing at ${formatClock(timer.endAtMs)}.`,
|
||||
};
|
||||
}
|
||||
case "list_timers": {
|
||||
if (timers.length === 0) return { ok: true, content: "No timers are set." };
|
||||
return { ok: true, content: timers.map(describeTimer).join("\n") };
|
||||
}
|
||||
case "cancel_timer": {
|
||||
if (args?.all === true) {
|
||||
if (timers.length === 0) return { ok: true, content: "No timers are set." };
|
||||
const count = timers.length;
|
||||
for (const timer of [...timers]) removeTimer(timer);
|
||||
refreshWindowsAfterToolChange();
|
||||
return { ok: true, content: count === 1 ? "Cleared the timer." : `Cleared ${count} timers.` };
|
||||
}
|
||||
if (timers.length === 0) return { ok: true, content: "No timers are set." };
|
||||
let timer: CountdownTimer;
|
||||
if (args?.timer === undefined || args?.timer === null) {
|
||||
if (timers.length > 1) {
|
||||
return {
|
||||
ok: false,
|
||||
error: `There are ${timers.length} timers; give the timer number.\n${timers.map(describeTimer).join("\n")}`,
|
||||
};
|
||||
}
|
||||
timer = timers[0]!;
|
||||
} else {
|
||||
const index = Math.floor(Number(args.timer));
|
||||
if (!Number.isFinite(index) || index < 1 || index > timers.length) {
|
||||
return { ok: false, error: `No timer ${args.timer}; there are ${timers.length} timers.` };
|
||||
}
|
||||
timer = timers[index - 1]!;
|
||||
}
|
||||
const description = describeTimer(timer);
|
||||
removeTimer(timer);
|
||||
refreshWindowsAfterToolChange();
|
||||
return { ok: true, content: `${timer.expired ? "Dismissed" : "Canceled"}: ${description}` };
|
||||
}
|
||||
default:
|
||||
return { ok: false, error: `Unknown timer tool: ${name}` };
|
||||
}
|
||||
}
|
||||
|
||||
function toolDurationField(value: unknown, label: string): number {
|
||||
if (value === undefined || value === null) return 0;
|
||||
const parsed = Number(value);
|
||||
if (!Number.isFinite(parsed) || parsed < 0) {
|
||||
throw new Error(`Invalid ${label} value: ${value}`);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function describeTimer(timer: CountdownTimer): string {
|
||||
const number = timers.indexOf(timer) + 1;
|
||||
const duration = formatDuration(timer.durationMs);
|
||||
if (timer.expired) return `Timer ${number}: ${duration} timer, finished.`;
|
||||
return (
|
||||
`Timer ${number}: ${duration} timer, ${formatCountdown(timer.endAtMs - Date.now())} remaining ` +
|
||||
`(finishes at ${formatClock(timer.endAtMs)}).`
|
||||
);
|
||||
}
|
||||
|
||||
/** Repaint and re-gate every window after a tool call changed the timer list. */
|
||||
function refreshWindowsAfterToolChange(): void {
|
||||
for (const window of windows.values()) {
|
||||
window.timerSelection = clamp(window.timerSelection, 0, timers.length + 1);
|
||||
updateRenderTimer(window);
|
||||
if (window.foreground && screenOn) renderAndSubmit(window, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's long-press menu (default entries only), created lazily. */
|
||||
function windowMenu(window: TimerWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
});
|
||||
}
|
||||
return window.menu;
|
||||
}
|
||||
|
||||
function handleInput(window: TimerWindow, event: InputEvent, frameId: number): void {
|
||||
// An open window menu owns all input (it closes itself via pop).
|
||||
if (window.menu?.isOpen()) {
|
||||
window.menu
|
||||
.handleInput(event)
|
||||
.catch((error) => console.error(`timer menu input failed: ${error}`))
|
||||
.then(() => renderAndSubmit(window, frameId));
|
||||
return;
|
||||
}
|
||||
if (event.type === "long-press") {
|
||||
windowMenu(window).open(defaultWindowMenuItems(window.windowId, post));
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
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: InputEvent, 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: InputEvent, 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: InputEvent, 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): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(paintContent(window));
|
||||
}
|
||||
|
||||
function paintContent(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: UiFont, 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: UiFont,
|
||||
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 planes = frameTimings.span(frameId, "paint", () =>
|
||||
frameTimings.runWithFrame(frameId, () => paint(window)),
|
||||
);
|
||||
const paintMs = Date.now() - paintStartedAtMs;
|
||||
const fingerprint = planesFingerprint(planes);
|
||||
if (fingerprint === window.lastSubmittedFingerprint) {
|
||||
frameTimings.finishFrame(frameId, "discarded: timer content unchanged");
|
||||
return;
|
||||
}
|
||||
const communicator = getActiveDisplay();
|
||||
if (!communicator) {
|
||||
frameTimings.finishFrame(frameId, "discarded: no active display");
|
||||
return;
|
||||
}
|
||||
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||
communicator.submitSurfaceFrame(
|
||||
buffer.buffer,
|
||||
window.surfaceId,
|
||||
0,
|
||||
0,
|
||||
image.width,
|
||||
image.height,
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
|
||||
);
|
||||
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 formatClock(timestampMs: number): string {
|
||||
const date = new Date(timestampMs);
|
||||
let hours = date.getHours();
|
||||
const minutes = String(date.getMinutes()).padStart(2, "0");
|
||||
const meridiem = hours >= 12 ? "PM" : "AM";
|
||||
hours = hours % 12;
|
||||
if (hours === 0) hours = 12;
|
||||
return `${hours}:${minutes} ${meridiem}`;
|
||||
}
|
||||
|
||||
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`;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,692 @@
|
||||
/**
|
||||
* The Timers app's clock engine: a main-thread singleton that owns the
|
||||
* timers / stopwatch / alarms state, persists it, schedules the durable
|
||||
* phone alarms behind every expiry, rings the glasses buzzer, and tells
|
||||
* listeners (the window, the tray icon, the assistant tools) when anything
|
||||
* changes. It is booted at shell start, so everything here works with the
|
||||
* window closed; the window is only a view on it.
|
||||
*
|
||||
* Two expiry paths run side by side: a JS deadline for prompt on-glasses
|
||||
* ringing while the process is awake, and an AlarmManager alarm-clock alarm
|
||||
* (FaceclawAlarms.java) that rings on the phone at the right moment even if
|
||||
* the process is asleep or dead. The phone shows the item silently first
|
||||
* and adds its own sound when the glasses cannot carry it (not connected,
|
||||
* not worn, charging, not shown within seconds, or not acknowledged within
|
||||
* 30 s); this engine keeps the phone told about the glasses, reports
|
||||
* delivery and acknowledgement, and replays what the wearer did on the
|
||||
* phone (dismiss / snooze) into its own state.
|
||||
*/
|
||||
import { ConfigSettingBoolean, ConfigSettingEnum, timeFormatSetting } from "../../ui/dashboard-settings";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import {
|
||||
acknowledgePhoneAlarm,
|
||||
cancelPhoneAlarm,
|
||||
checkPhoneAlarmReliability,
|
||||
drainPhoneAlarmJournal,
|
||||
onPhoneAlarmAction,
|
||||
openPhoneAlarmReliabilityFix,
|
||||
phoneAlarmDeliveredToGlasses,
|
||||
ringPhoneAlarm,
|
||||
schedulePhoneAlarm,
|
||||
setPhoneAlarmGlassesStatus,
|
||||
type AlarmReliabilityIssue,
|
||||
type PhoneAlarmAction,
|
||||
} from "../../native/alarms";
|
||||
import { getGlassesPresence, glassesCanCarryAlert, onGlassesPresenceChanged } from "../../g2/glasses-presence";
|
||||
import { buildSoundSequencePayload, findSoundEffect, effectPhrases } from "../../ui/sound-effects";
|
||||
import {
|
||||
alarmNextRingMs,
|
||||
createAlarm,
|
||||
createTimer,
|
||||
emptyTimersState,
|
||||
formatDurationWords,
|
||||
formatTimeOfDay,
|
||||
parseTimersState,
|
||||
pushRecentDuration,
|
||||
serializeTimersState,
|
||||
stopwatchElapsedMs,
|
||||
timerRemainingMs,
|
||||
type Alarm,
|
||||
type CountdownTimer,
|
||||
type TimersState,
|
||||
} from "./timer-model";
|
||||
|
||||
const STATE_KEY = "timers.state";
|
||||
|
||||
/**
|
||||
* An expiry that passed longer ago than this while the process was down
|
||||
* (phone off, app killed) is marked rung silently rather than blaring on
|
||||
* the glasses hours late; the phone notification already fired on time.
|
||||
*/
|
||||
const RING_GRACE_MS = 5 * 60_000;
|
||||
/** How often the buzzer repeats while something is ringing. */
|
||||
const RING_REPEAT_MS = 3_000;
|
||||
/** Stop buzzing an item this long after it started ringing (it stays "rung"). */
|
||||
const RING_MAX_MS = 2 * 60_000;
|
||||
/**
|
||||
* Upper bound between deadline checks even with nothing due, so a JS timer
|
||||
* delayed by Doze or a clock jump can't leave a due item unnoticed for long.
|
||||
*/
|
||||
const MAX_ARM_MS = 30_000;
|
||||
/** How often the phone is reminded of the glasses' status (it distrusts stale reports). */
|
||||
const GLASSES_STATUS_HEARTBEAT_MS = 60_000;
|
||||
/** How often the phone-side reliability self-check reruns while something is armed. */
|
||||
const RELIABILITY_CHECK_MS = 5 * 60_000;
|
||||
|
||||
export const timersSoundSetting = new ConfigSettingBoolean({
|
||||
id: "timersSound",
|
||||
label: "Sound on glasses",
|
||||
storageKey: "timers.soundOnGlasses",
|
||||
defaultValue: true,
|
||||
description: "Play the glasses buzzer while a timer or alarm is ringing.",
|
||||
});
|
||||
|
||||
export const timersWakeSetting = new ConfigSettingBoolean({
|
||||
id: "timersWake",
|
||||
label: "Wake glasses when ringing",
|
||||
storageKey: "timers.wakeGlasses",
|
||||
defaultValue: true,
|
||||
description: "Turn the display on and show the Timers app when a timer or alarm goes off.",
|
||||
});
|
||||
|
||||
export type SnoozeMinutes = "5" | "10" | "15";
|
||||
export const timersSnoozeSetting = new ConfigSettingEnum<SnoozeMinutes>({
|
||||
id: "timersSnooze",
|
||||
label: "Snooze length",
|
||||
storageKey: "timers.snoozeMinutes",
|
||||
defaultValue: "10",
|
||||
values: ["5", "10", "15"],
|
||||
formatValue: (value) => `${value} min`,
|
||||
});
|
||||
|
||||
export type RingingItem = { kind: "timer"; timer: CountdownTimer } | { kind: "alarm"; alarm: Alarm };
|
||||
|
||||
export type TimerEngineHooks = {
|
||||
/** Play one buzzer sequencer payload on the glasses (no-op when disconnected). */
|
||||
playBuzzer: (payload: Uint8Array) => Promise<void> | void;
|
||||
/** Something started ringing audibly: bring the app forward, wake the glasses. */
|
||||
onRing: (item: RingingItem) => void;
|
||||
};
|
||||
|
||||
const noopHooks: TimerEngineHooks = { playBuzzer: () => {}, onRing: () => {} };
|
||||
|
||||
export class TimerEngine {
|
||||
state: TimersState;
|
||||
private hooks: TimerEngineHooks = noopHooks;
|
||||
private readonly listeners = new Set<() => void>();
|
||||
private deadlineTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private ringTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private booted = false;
|
||||
private reliabilityIssues: AlarmReliabilityIssue[] = [];
|
||||
private reliabilityCheckedAtMs = 0;
|
||||
|
||||
constructor(private readonly now: () => number = () => Date.now()) {
|
||||
this.state = emptyTimersState();
|
||||
}
|
||||
|
||||
/**
|
||||
* Install the side-effect hooks and reconcile persisted state against the
|
||||
* clock: anything that came due while the process was down rings (or is
|
||||
* marked rung silently past the grace window), and the Android alarms are
|
||||
* re-armed for what is still pending.
|
||||
*/
|
||||
boot(hooks: TimerEngineHooks): void {
|
||||
this.hooks = hooks;
|
||||
if (this.booted) return;
|
||||
this.booted = true;
|
||||
// Loaded here rather than at module evaluation, so the settings store is
|
||||
// certainly up before the first read.
|
||||
this.state = parseTimersState(getStringSetting(STATE_KEY, ""));
|
||||
// What the wearer did on the phone while this side was down comes first,
|
||||
// so a dismissed alarm is not rung again by the check below.
|
||||
for (const action of drainPhoneAlarmJournal()) this.applyPhoneAction(action);
|
||||
onPhoneAlarmAction((action) => {
|
||||
this.applyPhoneAction(action);
|
||||
this.commit();
|
||||
});
|
||||
this.pushGlassesStatus();
|
||||
onGlassesPresenceChanged(() => this.pushGlassesStatus());
|
||||
setInterval(() => this.pushGlassesStatus(), GLASSES_STATUS_HEARTBEAT_MS);
|
||||
this.check();
|
||||
for (const timer of this.state.timers) {
|
||||
if (timer.endsAtMs !== null && timer.rungAtMs === null) this.schedulePhoneNotification(timer);
|
||||
}
|
||||
for (const alarm of this.state.alarms) this.schedulePhoneAlarm(alarm);
|
||||
this.save();
|
||||
this.refreshReliability(true);
|
||||
setInterval(() => this.refreshReliability(false), RELIABILITY_CHECK_MS);
|
||||
}
|
||||
|
||||
/**
|
||||
* The glasses are showing a ringing item (the window is up and the screen
|
||||
* on). The phone's 30-second acknowledgement clock starts from here; without
|
||||
* this, the phone adds its sound within seconds.
|
||||
*/
|
||||
markDeliveredToGlasses(item: RingingItem): void {
|
||||
if (!glassesCanCarryAlert()) return;
|
||||
phoneAlarmDeliveredToGlasses(item.kind === "timer" ? item.timer.id : item.alarm.id);
|
||||
}
|
||||
|
||||
private pushGlassesStatus(): void {
|
||||
const presence = getGlassesPresence();
|
||||
setPhoneAlarmGlassesStatus(presence.connected, presence.worn === true, presence.charging);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replay a phone-side dismiss or snooze. The item may be ringing here too,
|
||||
* may not have been noticed yet (the phone rang it while this side slept),
|
||||
* or may already be gone; each case lands on the same end state.
|
||||
*/
|
||||
private applyPhoneAction(action: PhoneAlarmAction): void {
|
||||
const at = action.atMs || this.now();
|
||||
const timer = this.findTimer(action.id);
|
||||
if (timer) {
|
||||
if (action.action === "dismiss") {
|
||||
this.state.timers = this.state.timers.filter((candidate) => candidate.id !== timer.id);
|
||||
} else {
|
||||
const extra = Math.max(1, action.minutes) * 60_000;
|
||||
timer.rungAtMs = null;
|
||||
timer.pausedRemainingMs = null;
|
||||
timer.durationMs = extra;
|
||||
timer.endsAtMs = at + extra;
|
||||
}
|
||||
return;
|
||||
}
|
||||
const alarm = this.findAlarm(action.id);
|
||||
if (!alarm) return;
|
||||
alarm.ringingSinceMs = null;
|
||||
alarm.lastFiredAtMs = Math.max(alarm.lastFiredAtMs ?? 0, at);
|
||||
if (action.action === "dismiss") {
|
||||
alarm.snoozedUntilMs = null;
|
||||
if (alarm.days === 0) alarm.enabled = false;
|
||||
} else {
|
||||
alarm.snoozedUntilMs = at + Math.max(1, action.minutes) * 60_000;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Phone reliability
|
||||
|
||||
/** Conditions under which the phone may fail to ring, most serious first (cached). */
|
||||
phoneReliabilityIssues(): AlarmReliabilityIssue[] {
|
||||
return this.reliabilityIssues;
|
||||
}
|
||||
|
||||
/**
|
||||
* One-line warning for the UI, or "" when nothing is armed that could
|
||||
* suffer (no enabled alarm, no running timer) or nothing is wrong.
|
||||
*/
|
||||
phoneReliabilityMessage(): string {
|
||||
if (this.reliabilityIssues.length === 0) return "";
|
||||
const armed =
|
||||
this.state.alarms.some((alarm) => alarm.enabled) ||
|
||||
this.state.timers.some((timer) => timer.endsAtMs !== null && timer.rungAtMs === null);
|
||||
if (!armed) return "";
|
||||
return `The phone may not ring for alarms: ${this.reliabilityIssues[0]!.message}`;
|
||||
}
|
||||
|
||||
/** Open the system screen for the most serious fixable issue, if any. */
|
||||
openPhoneReliabilityFix(): void {
|
||||
const issue = this.reliabilityIssues.find((candidate) => candidate.fixable);
|
||||
if (issue) openPhoneAlarmReliabilityFix(issue.code);
|
||||
}
|
||||
|
||||
/** Re-run the phone self-check (rate-limited unless forced) and notify on change. */
|
||||
refreshReliability(force: boolean): void {
|
||||
const now = this.now();
|
||||
if (!force && now - this.reliabilityCheckedAtMs < 30_000) return;
|
||||
this.reliabilityCheckedAtMs = now;
|
||||
let issues: AlarmReliabilityIssue[];
|
||||
try {
|
||||
issues = checkPhoneAlarmReliability();
|
||||
} catch (error) {
|
||||
console.warn(`alarm reliability check failed: ${error}`);
|
||||
return;
|
||||
}
|
||||
const changed =
|
||||
issues.length !== this.reliabilityIssues.length ||
|
||||
issues.some((issue, index) => issue.code !== this.reliabilityIssues[index]?.code);
|
||||
this.reliabilityIssues = issues;
|
||||
if (changed) this.notify();
|
||||
}
|
||||
|
||||
onChange(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => {
|
||||
this.listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
/** True while anything on screen changes by itself once a second. */
|
||||
isDynamic(): boolean {
|
||||
return (
|
||||
this.state.stopwatch.runningSinceMs !== null ||
|
||||
this.state.timers.some((timer) => timer.endsAtMs !== null && timer.rungAtMs === null) ||
|
||||
this.ringingItems().length > 0
|
||||
);
|
||||
}
|
||||
|
||||
ringingItems(): RingingItem[] {
|
||||
const items: RingingItem[] = [];
|
||||
for (const timer of this.state.timers) {
|
||||
if (timer.rungAtMs !== null) items.push({ kind: "timer", timer });
|
||||
}
|
||||
for (const alarm of this.state.alarms) {
|
||||
if (alarm.ringingSinceMs !== null) items.push({ kind: "alarm", alarm });
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Timers
|
||||
|
||||
findTimer(id: number): CountdownTimer | undefined {
|
||||
return this.state.timers.find((timer) => timer.id === id);
|
||||
}
|
||||
|
||||
startTimer(durationMs: number, label = ""): CountdownTimer {
|
||||
const timer = createTimer(durationMs, label, this.now());
|
||||
this.state.timers.push(timer);
|
||||
this.state.recentDurationsMs = pushRecentDuration(this.state.recentDurationsMs, durationMs);
|
||||
this.schedulePhoneNotification(timer);
|
||||
this.commit();
|
||||
return timer;
|
||||
}
|
||||
|
||||
pauseTimer(id: number): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer || timer.endsAtMs === null || timer.rungAtMs !== null) return;
|
||||
timer.pausedRemainingMs = Math.max(0, timer.endsAtMs - this.now());
|
||||
timer.endsAtMs = null;
|
||||
cancelPhoneAlarm(timer.id);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
resumeTimer(id: number): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer || timer.pausedRemainingMs === null) return;
|
||||
timer.endsAtMs = this.now() + timer.pausedRemainingMs;
|
||||
timer.pausedRemainingMs = null;
|
||||
this.schedulePhoneNotification(timer);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Add time to a timer; a rung timer becomes a running one with just the added time. */
|
||||
addTimerTime(id: number, deltaMs: number): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer) return;
|
||||
if (timer.rungAtMs !== null) {
|
||||
timer.rungAtMs = null;
|
||||
timer.durationMs = deltaMs;
|
||||
timer.endsAtMs = this.now() + deltaMs;
|
||||
cancelPhoneAlarm(timer.id);
|
||||
this.schedulePhoneNotification(timer);
|
||||
} else if (timer.pausedRemainingMs !== null) {
|
||||
timer.pausedRemainingMs += deltaMs;
|
||||
timer.durationMs += deltaMs;
|
||||
} else if (timer.endsAtMs !== null) {
|
||||
timer.endsAtMs += deltaMs;
|
||||
timer.durationMs += deltaMs;
|
||||
this.schedulePhoneNotification(timer);
|
||||
}
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Start the timer over at its set duration ("Again" on a rung timer). */
|
||||
restartTimer(id: number): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer) return;
|
||||
timer.rungAtMs = null;
|
||||
timer.pausedRemainingMs = null;
|
||||
timer.endsAtMs = this.now() + timer.durationMs;
|
||||
cancelPhoneAlarm(timer.id);
|
||||
this.schedulePhoneNotification(timer);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
setTimerLabel(id: number, label: string): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer) return;
|
||||
timer.label = label.trim();
|
||||
if (timer.endsAtMs !== null && timer.rungAtMs === null) this.schedulePhoneNotification(timer);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Cancel a timer, or dismiss a rung one. */
|
||||
removeTimer(id: number): void {
|
||||
const index = this.state.timers.findIndex((timer) => timer.id === id);
|
||||
if (index < 0) return;
|
||||
acknowledgePhoneAlarm(id);
|
||||
this.state.timers.splice(index, 1);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Stopwatch
|
||||
|
||||
stopwatchToggle(): void {
|
||||
const stopwatch = this.state.stopwatch;
|
||||
const now = this.now();
|
||||
if (stopwatch.runningSinceMs === null) {
|
||||
stopwatch.runningSinceMs = now;
|
||||
} else {
|
||||
stopwatch.accumulatedMs += Math.max(0, now - stopwatch.runningSinceMs);
|
||||
stopwatch.runningSinceMs = null;
|
||||
}
|
||||
this.commit();
|
||||
}
|
||||
|
||||
stopwatchStart(): void {
|
||||
if (this.state.stopwatch.runningSinceMs === null) this.stopwatchToggle();
|
||||
}
|
||||
|
||||
stopwatchStop(): void {
|
||||
if (this.state.stopwatch.runningSinceMs !== null) this.stopwatchToggle();
|
||||
}
|
||||
|
||||
stopwatchLap(): void {
|
||||
const stopwatch = this.state.stopwatch;
|
||||
if (stopwatch.runningSinceMs === null) return;
|
||||
stopwatch.lapsMs.push(stopwatchElapsedMs(stopwatch, this.now()));
|
||||
this.commit();
|
||||
}
|
||||
|
||||
stopwatchReset(): void {
|
||||
this.state.stopwatch = { accumulatedMs: 0, runningSinceMs: null, lapsMs: [] };
|
||||
this.commit();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Alarms
|
||||
|
||||
findAlarm(id: number): Alarm | undefined {
|
||||
return this.state.alarms.find((alarm) => alarm.id === id);
|
||||
}
|
||||
|
||||
addAlarm(hour: number, minute: number, days: number, label = ""): Alarm {
|
||||
const alarm = createAlarm(hour, minute, days, label, this.now());
|
||||
this.state.alarms.push(alarm);
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
return alarm;
|
||||
}
|
||||
|
||||
setAlarmTime(id: number, hour: number, minute: number): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm) return;
|
||||
alarm.hour = hour;
|
||||
alarm.minute = minute;
|
||||
alarm.enabled = true;
|
||||
alarm.snoozedUntilMs = null;
|
||||
alarm.ringingSinceMs = null;
|
||||
alarm.lastFiredAtMs = null;
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
setAlarmDays(id: number, days: number): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm) return;
|
||||
alarm.days = days;
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
setAlarmEnabled(id: number, enabled: boolean): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm) return;
|
||||
alarm.enabled = enabled;
|
||||
alarm.snoozedUntilMs = null;
|
||||
alarm.ringingSinceMs = null;
|
||||
if (enabled) alarm.lastFiredAtMs = null;
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
setAlarmLabel(id: number, label: string): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm) return;
|
||||
alarm.label = label.trim();
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
deleteAlarm(id: number): void {
|
||||
const index = this.state.alarms.findIndex((alarm) => alarm.id === id);
|
||||
if (index < 0) return;
|
||||
cancelPhoneAlarm(id);
|
||||
this.state.alarms.splice(index, 1);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
snoozeAlarm(id: number, minutes = Number(timersSnoozeSetting.get())): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm || alarm.ringingSinceMs === null) return;
|
||||
alarm.ringingSinceMs = null;
|
||||
alarm.snoozedUntilMs = this.now() + minutes * 60_000;
|
||||
acknowledgePhoneAlarm(alarm.id);
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Stop a ringing alarm; a one-off alarm switches itself off. */
|
||||
dismissAlarm(id: number): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm || alarm.ringingSinceMs === null) return;
|
||||
alarm.ringingSinceMs = null;
|
||||
alarm.snoozedUntilMs = null;
|
||||
if (alarm.days === 0) alarm.enabled = false;
|
||||
acknowledgePhoneAlarm(alarm.id);
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Dismiss whatever is ringing (timers and alarms alike). Returns how many. */
|
||||
dismissAllRinging(): number {
|
||||
const items = this.ringingItems();
|
||||
for (const item of items) {
|
||||
if (item.kind === "timer") this.removeTimer(item.timer.id);
|
||||
else this.dismissAlarm(item.alarm.id);
|
||||
}
|
||||
return items.length;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Deadlines and ringing
|
||||
|
||||
/**
|
||||
* Fire anything due and re-arm for the next deadline. Called on every
|
||||
* change, on the deadline timer, and by the window before it paints, so a
|
||||
* timer never shows 0:00 without also being rung.
|
||||
*/
|
||||
check(): void {
|
||||
const now = this.now();
|
||||
let changed = false;
|
||||
for (const timer of this.state.timers) {
|
||||
if (timer.endsAtMs === null || timer.rungAtMs !== null || timer.endsAtMs > now) continue;
|
||||
timer.rungAtMs = timer.endsAtMs;
|
||||
timer.endsAtMs = null;
|
||||
changed = true;
|
||||
// Late (found due after a restart): the phone rang or missed it on
|
||||
// its own schedule; only a fresh expiry starts the phone flow here.
|
||||
const late = now - timer.rungAtMs > RING_GRACE_MS;
|
||||
if (!late) {
|
||||
ringPhoneAlarm(timer.id, "Timer finished", timerNotificationText(timer), "timer", snoozeMinutes());
|
||||
this.startRinging({ kind: "timer", timer });
|
||||
}
|
||||
}
|
||||
for (const alarm of this.state.alarms) {
|
||||
if (alarm.ringingSinceMs !== null) continue;
|
||||
const due = this.alarmDueMs(alarm, now);
|
||||
if (due === null || due > now) continue;
|
||||
alarm.ringingSinceMs = now;
|
||||
alarm.snoozedUntilMs = null;
|
||||
alarm.lastFiredAtMs = due;
|
||||
changed = true;
|
||||
const late = now - due > RING_GRACE_MS;
|
||||
if (late) {
|
||||
// Missed by too much to ring now: fall through to the next occurrence.
|
||||
alarm.ringingSinceMs = null;
|
||||
if (alarm.days === 0) alarm.enabled = false;
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
} else {
|
||||
ringPhoneAlarm(alarm.id, "Alarm", alarmNotificationText(alarm), "alarm", snoozeMinutes());
|
||||
this.startRinging({ kind: "alarm", alarm });
|
||||
}
|
||||
}
|
||||
this.arm(now);
|
||||
this.syncRingLoop(now);
|
||||
if (changed) this.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* The occurrence an alarm should ring for next: the pending snooze, or the
|
||||
* first occurrence after (now - grace) that it has not already rung for.
|
||||
* Inside the grace window this returns a time in the past, which check()
|
||||
* fires; beyond it the missed occurrence is skipped.
|
||||
*/
|
||||
private alarmDueMs(alarm: Alarm, now: number): number | null {
|
||||
return alarmNextRingMs(alarm, now - RING_GRACE_MS);
|
||||
}
|
||||
|
||||
private startRinging(item: RingingItem): void {
|
||||
try {
|
||||
this.hooks.onRing(item);
|
||||
} catch (error) {
|
||||
console.warn(`timers onRing hook failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Re-arm the single deadline timer for the soonest pending expiry. */
|
||||
private arm(now: number): void {
|
||||
if (this.deadlineTimer !== null) {
|
||||
clearTimeout(this.deadlineTimer);
|
||||
this.deadlineTimer = null;
|
||||
}
|
||||
const deadlines: number[] = [];
|
||||
for (const timer of this.state.timers) {
|
||||
if (timer.rungAtMs === null && timer.endsAtMs !== null) deadlines.push(timer.endsAtMs);
|
||||
}
|
||||
for (const alarm of this.state.alarms) {
|
||||
if (alarm.ringingSinceMs !== null) continue;
|
||||
const due = this.alarmDueMs(alarm, now);
|
||||
if (due !== null) deadlines.push(due);
|
||||
}
|
||||
if (deadlines.length === 0) return;
|
||||
const delay = Math.min(MAX_ARM_MS, Math.max(0, Math.min(...deadlines) - now));
|
||||
this.deadlineTimer = setTimeout(() => {
|
||||
this.deadlineTimer = null;
|
||||
this.check();
|
||||
}, delay);
|
||||
}
|
||||
|
||||
/** Run the buzzer repeat loop while anything is (still audibly) ringing. */
|
||||
private syncRingLoop(now: number): void {
|
||||
const audible = this.ringingItems().some((item) => this.ringAgeMs(item, now) < RING_MAX_MS);
|
||||
if (audible && this.ringTimer === null) {
|
||||
this.playRingSound();
|
||||
this.ringTimer = setInterval(() => {
|
||||
const at = this.now();
|
||||
if (!this.ringingItems().some((item) => this.ringAgeMs(item, at) < RING_MAX_MS)) {
|
||||
this.syncRingLoop(at);
|
||||
return;
|
||||
}
|
||||
this.playRingSound();
|
||||
}, RING_REPEAT_MS);
|
||||
} else if (!audible && this.ringTimer !== null) {
|
||||
clearInterval(this.ringTimer);
|
||||
this.ringTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private ringAgeMs(item: RingingItem, now: number): number {
|
||||
const since = item.kind === "timer" ? item.timer.rungAtMs : item.alarm.ringingSinceMs;
|
||||
return since === null ? Infinity : Math.max(0, now - since);
|
||||
}
|
||||
|
||||
private playRingSound(): void {
|
||||
if (!timersSoundSetting.get()) return;
|
||||
const effect = findSoundEffect("alarm");
|
||||
if (!effect) return;
|
||||
const phrase = effectPhrases(effect.make())[0];
|
||||
if (!phrase) return;
|
||||
try {
|
||||
void this.hooks.playBuzzer(buildSoundSequencePayload(phrase));
|
||||
} catch (error) {
|
||||
console.warn(`timers buzzer failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Phone notifications
|
||||
|
||||
private schedulePhoneNotification(timer: CountdownTimer): void {
|
||||
if (timer.endsAtMs === null) return;
|
||||
schedulePhoneAlarm(timer.id, timer.endsAtMs, "Timer finished", timerNotificationText(timer), "timer", snoozeMinutes());
|
||||
}
|
||||
|
||||
/** (Re)arm or cancel the phone alarm for an alarm's next occurrence. */
|
||||
private schedulePhoneAlarm(alarm: Alarm): void {
|
||||
const due = alarm.ringingSinceMs === null ? this.alarmDueMs(alarm, this.now()) : null;
|
||||
if (due === null) {
|
||||
cancelPhoneAlarm(alarm.id);
|
||||
return;
|
||||
}
|
||||
schedulePhoneAlarm(alarm.id, due, "Alarm", alarmNotificationText(alarm), "alarm", snoozeMinutes());
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Change plumbing
|
||||
|
||||
private commit(): void {
|
||||
this.save();
|
||||
this.check();
|
||||
this.notify();
|
||||
if (this.booted) this.refreshReliability(false);
|
||||
}
|
||||
|
||||
private save(): void {
|
||||
try {
|
||||
setStringSetting(STATE_KEY, serializeTimersState(this.state));
|
||||
} catch (error) {
|
||||
console.warn(`timers state save failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
private notify(): void {
|
||||
for (const listener of Array.from(this.listeners)) {
|
||||
try {
|
||||
listener();
|
||||
} catch (error) {
|
||||
console.warn(`timers listener failed: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function snoozeMinutes(): number {
|
||||
return Number(timersSnoozeSetting.get()) || 10;
|
||||
}
|
||||
|
||||
function timerNotificationText(timer: CountdownTimer): string {
|
||||
const duration = formatDurationWords(timer.durationMs);
|
||||
return timer.label ? `${timer.label} (${duration}) is up` : `${duration} timer is up`;
|
||||
}
|
||||
|
||||
function alarmNotificationText(alarm: Alarm): string {
|
||||
const time = formatTimeOfDay(alarm.hour, alarm.minute, timeFormatSetting.get() === "12h");
|
||||
return alarm.label ? `${time} · ${alarm.label}` : time;
|
||||
}
|
||||
|
||||
/** Remaining time of the timer that ends soonest, for the tray icon; null when none runs. */
|
||||
export function soonestRunningTimerRemainingMs(engine: TimerEngine, now = Date.now()): number | null {
|
||||
let soonest: number | null = null;
|
||||
for (const timer of engine.state.timers) {
|
||||
if (timer.endsAtMs === null || timer.rungAtMs !== null) continue;
|
||||
const remaining = timerRemainingMs(timer, now);
|
||||
if (soonest === null || remaining < soonest) soonest = remaining;
|
||||
}
|
||||
return soonest;
|
||||
}
|
||||
|
||||
export const timerEngine = new TimerEngine();
|
||||
@@ -0,0 +1,460 @@
|
||||
/**
|
||||
* Pure data model for the Timers app: countdown timers, the stopwatch, and
|
||||
* alarms, plus the formatting and scheduling arithmetic they need. No
|
||||
* NativeScript imports, so tests/timer-model.test.cjs can run it under plain
|
||||
* node. Side effects (persistence, Android alarms, the buzzer, waking the
|
||||
* glasses) live in timer-engine.ts; drawing lives in timer-app.ts.
|
||||
*/
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
|
||||
/**
|
||||
* A countdown timer. Exactly one of the three phases holds at a time:
|
||||
* running (endsAtMs set), paused (pausedRemainingMs set), or rung
|
||||
* (rungAtMs set). A rung timer stays until dismissed so a missed one still
|
||||
* reads "Time's up".
|
||||
*/
|
||||
export type CountdownTimer = {
|
||||
/** Unique across restarts and safe as a Java long (see newItemId). */
|
||||
id: number;
|
||||
/** Wearer-given name; "" for an unlabelled timer (shown by duration). */
|
||||
label: string;
|
||||
durationMs: number;
|
||||
endsAtMs: number | null;
|
||||
pausedRemainingMs: number | null;
|
||||
rungAtMs: number | null;
|
||||
createdAtMs: number;
|
||||
};
|
||||
|
||||
export type StopwatchState = {
|
||||
/** Time accumulated over previous running stretches. */
|
||||
accumulatedMs: number;
|
||||
/** Start of the current running stretch, or null while stopped. */
|
||||
runningSinceMs: number | null;
|
||||
/** Cumulative elapsed time at each lap mark, oldest first. */
|
||||
lapsMs: number[];
|
||||
};
|
||||
|
||||
/** Day-of-week bits for Alarm.days, Monday first (Mon = bit 0 ... Sun = bit 6). */
|
||||
export const DAY_BITS = [1, 2, 4, 8, 16, 32, 64] as const;
|
||||
export const DAY_LETTERS = ["M", "T", "W", "T", "F", "S", "S"] as const;
|
||||
export const DAY_NAMES = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] as const;
|
||||
export const DAY_FULL_NAMES = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] as const;
|
||||
export const WEEKDAYS_MASK = 1 | 2 | 4 | 8 | 16;
|
||||
export const WEEKEND_MASK = 32 | 64;
|
||||
export const EVERY_DAY_MASK = 127;
|
||||
|
||||
export type Alarm = {
|
||||
id: number;
|
||||
hour: number;
|
||||
minute: number;
|
||||
/** Repeat days as DAY_BITS; 0 means a one-off alarm (disabled after it rings). */
|
||||
days: number;
|
||||
label: string;
|
||||
enabled: boolean;
|
||||
/** Set while a snooze is pending; the alarm rings again at this time. */
|
||||
snoozedUntilMs: number | null;
|
||||
/** Set while ringing; cleared by dismiss or snooze. */
|
||||
ringingSinceMs: number | null;
|
||||
/**
|
||||
* For a repeating alarm: the occurrence most recently rung (or dismissed
|
||||
* from ringing), so the same occurrence does not ring twice after a
|
||||
* dismiss that lands before the minute is over.
|
||||
*/
|
||||
lastFiredAtMs: number | null;
|
||||
};
|
||||
|
||||
export type TimersState = {
|
||||
timers: CountdownTimer[];
|
||||
stopwatch: StopwatchState;
|
||||
alarms: Alarm[];
|
||||
/** Durations the wearer started most recently, newest first. */
|
||||
recentDurationsMs: number[];
|
||||
};
|
||||
|
||||
export function emptyTimersState(): TimersState {
|
||||
return {
|
||||
timers: [],
|
||||
stopwatch: { accumulatedMs: 0, runningSinceMs: null, lapsMs: [] },
|
||||
alarms: [],
|
||||
recentDurationsMs: [],
|
||||
};
|
||||
}
|
||||
|
||||
/** How many recent durations to keep for the "New timer" menu. */
|
||||
export const RECENT_DURATION_COUNT = 3;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Ids
|
||||
|
||||
let idSerial = 1;
|
||||
|
||||
/**
|
||||
* Millisecond epoch plus a small serial: unique across restarts, ordered by
|
||||
* creation, and still exactly representable through the JS -> Java long
|
||||
* bridge.
|
||||
*/
|
||||
export function newItemId(nowMs: number): number {
|
||||
return nowMs * 100 + (idSerial++ % 100);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Duration ladder (the "New timer" dial)
|
||||
|
||||
const SECOND = 1000;
|
||||
const MINUTE = 60 * SECOND;
|
||||
const HOUR = 60 * MINUTE;
|
||||
|
||||
/**
|
||||
* The durations the dial scrolls through: fine steps where timers are
|
||||
* usually short, coarser steps as they get long, so anything from ten
|
||||
* seconds to a day is a few dozen scroll clicks away at most.
|
||||
*/
|
||||
export const DURATION_LADDER_MS: readonly number[] = (() => {
|
||||
const ladder: number[] = [10 * SECOND, 15 * SECOND, 20 * SECOND, 30 * SECOND, 45 * SECOND, MINUTE, 90 * SECOND];
|
||||
for (let minutes = 2; minutes <= 20; minutes++) ladder.push(minutes * MINUTE);
|
||||
for (let minutes = 25; minutes <= 60; minutes += 5) ladder.push(minutes * MINUTE);
|
||||
for (let minutes = 75; minutes <= 180; minutes += 15) ladder.push(minutes * MINUTE);
|
||||
for (let minutes = 210; minutes <= 360; minutes += 30) ladder.push(minutes * MINUTE);
|
||||
for (let hours = 7; hours <= 12; hours++) ladder.push(hours * HOUR);
|
||||
ladder.push(18 * HOUR, 24 * HOUR);
|
||||
return ladder;
|
||||
})();
|
||||
|
||||
/** Index of the ladder rung closest to a duration (ties go to the shorter). */
|
||||
export function nearestLadderIndex(durationMs: number): number {
|
||||
let best = 0;
|
||||
let bestDistance = Infinity;
|
||||
for (let index = 0; index < DURATION_LADDER_MS.length; index++) {
|
||||
const distance = Math.abs(DURATION_LADDER_MS[index]! - durationMs);
|
||||
if (distance < bestDistance) {
|
||||
best = index;
|
||||
bestDistance = distance;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
export const DEFAULT_TIMER_DURATION_MS = 10 * MINUTE;
|
||||
export const MAX_TIMER_DURATION_MS = 24 * HOUR;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Timers
|
||||
|
||||
export function timerRemainingMs(timer: CountdownTimer, nowMs: number): number {
|
||||
if (timer.pausedRemainingMs !== null) return timer.pausedRemainingMs;
|
||||
if (timer.endsAtMs !== null) return Math.max(0, timer.endsAtMs - nowMs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
export type TimerPhase = "running" | "paused" | "rung";
|
||||
|
||||
export function timerPhase(timer: CountdownTimer): TimerPhase {
|
||||
if (timer.rungAtMs !== null) return "rung";
|
||||
if (timer.pausedRemainingMs !== null) return "paused";
|
||||
return "running";
|
||||
}
|
||||
|
||||
/** 0..1 progress toward the end (1 once rung). */
|
||||
export function timerProgress(timer: CountdownTimer, nowMs: number): number {
|
||||
if (timer.rungAtMs !== null || timer.durationMs <= 0) return 1;
|
||||
return Math.min(1, Math.max(0, 1 - timerRemainingMs(timer, nowMs) / timer.durationMs));
|
||||
}
|
||||
|
||||
export function createTimer(durationMs: number, label: string, nowMs: number): CountdownTimer {
|
||||
return {
|
||||
id: newItemId(nowMs),
|
||||
label,
|
||||
durationMs,
|
||||
endsAtMs: nowMs + durationMs,
|
||||
pausedRemainingMs: null,
|
||||
rungAtMs: null,
|
||||
createdAtMs: nowMs,
|
||||
};
|
||||
}
|
||||
|
||||
/** Running timers soonest first, then paused, then rung (oldest ring first). */
|
||||
export function sortTimers(timers: readonly CountdownTimer[], nowMs: number): CountdownTimer[] {
|
||||
const rank = (timer: CountdownTimer) => (timer.rungAtMs !== null ? 0 : timer.pausedRemainingMs !== null ? 2 : 1);
|
||||
return [...timers].sort((a, b) => {
|
||||
const rankDelta = rank(a) - rank(b);
|
||||
if (rankDelta !== 0) return rankDelta;
|
||||
if (a.rungAtMs !== null && b.rungAtMs !== null) return a.rungAtMs - b.rungAtMs;
|
||||
return timerRemainingMs(a, nowMs) - timerRemainingMs(b, nowMs) || a.createdAtMs - b.createdAtMs;
|
||||
});
|
||||
}
|
||||
|
||||
export function pushRecentDuration(recent: readonly number[], durationMs: number): number[] {
|
||||
return [durationMs, ...recent.filter((value) => value !== durationMs)].slice(0, RECENT_DURATION_COUNT);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Stopwatch
|
||||
|
||||
export function stopwatchElapsedMs(stopwatch: StopwatchState, nowMs: number): number {
|
||||
const running = stopwatch.runningSinceMs === null ? 0 : Math.max(0, nowMs - stopwatch.runningSinceMs);
|
||||
return stopwatch.accumulatedMs + running;
|
||||
}
|
||||
|
||||
export function stopwatchIsRunning(stopwatch: StopwatchState): boolean {
|
||||
return stopwatch.runningSinceMs !== null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Alarms
|
||||
|
||||
export function createAlarm(hour: number, minute: number, days: number, label: string, nowMs: number): Alarm {
|
||||
return {
|
||||
id: newItemId(nowMs),
|
||||
hour,
|
||||
minute,
|
||||
days,
|
||||
label,
|
||||
enabled: true,
|
||||
snoozedUntilMs: null,
|
||||
ringingSinceMs: null,
|
||||
lastFiredAtMs: null,
|
||||
};
|
||||
}
|
||||
|
||||
/** Monday-first weekday index (0..6) of a Date, from JS's Sunday-first getDay. */
|
||||
export function mondayFirstDay(date: Date): number {
|
||||
return (date.getDay() + 6) % 7;
|
||||
}
|
||||
|
||||
/**
|
||||
* When the alarm next goes off, given the current time: the pending snooze
|
||||
* if there is one, otherwise the next wall-clock occurrence of hour:minute
|
||||
* on an enabled day (any day for a one-off), strictly after `nowMs` and
|
||||
* after the occurrence it last rang for. Null when the alarm is disabled.
|
||||
* Wall-clock arithmetic runs in the local zone via Date, so DST changes
|
||||
* land on the right local time.
|
||||
*/
|
||||
export function alarmNextRingMs(alarm: Alarm, nowMs: number): number | null {
|
||||
if (!alarm.enabled) return null;
|
||||
if (alarm.snoozedUntilMs !== null) return alarm.snoozedUntilMs;
|
||||
const floor = Math.max(nowMs, alarm.lastFiredAtMs ?? 0);
|
||||
const start = new Date(nowMs);
|
||||
for (let dayOffset = 0; dayOffset <= 7; dayOffset++) {
|
||||
const candidate = new Date(start.getFullYear(), start.getMonth(), start.getDate() + dayOffset, alarm.hour, alarm.minute, 0, 0);
|
||||
if (candidate.getTime() <= floor) continue;
|
||||
if (alarm.days !== 0 && !(alarm.days & DAY_BITS[mondayFirstDay(candidate)]!)) continue;
|
||||
return candidate.getTime();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Alarms by time of day, then by creation. */
|
||||
export function sortAlarms(alarms: readonly Alarm[]): Alarm[] {
|
||||
return [...alarms].sort((a, b) => a.hour * 60 + a.minute - (b.hour * 60 + b.minute) || a.id - b.id);
|
||||
}
|
||||
|
||||
export function formatDays(days: number): string {
|
||||
if (days === 0) return "Once";
|
||||
if (days === EVERY_DAY_MASK) return "Every day";
|
||||
if (days === WEEKDAYS_MASK) return "Weekdays";
|
||||
if (days === WEEKEND_MASK) return "Weekends";
|
||||
return DAY_NAMES.filter((_, index) => days & DAY_BITS[index]!).join(" ");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Formatting
|
||||
|
||||
function pad2(value: number): string {
|
||||
return String(value).padStart(2, "0");
|
||||
}
|
||||
|
||||
/** Countdown / elapsed digits: m:ss below an hour, h:mm:ss above. */
|
||||
export function formatClockDigits(totalSeconds: number): string {
|
||||
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)}` : `${minutes}:${pad2(seconds)}`;
|
||||
}
|
||||
|
||||
/** Remaining time as a countdown reads it: rounds up, so 0:00 means done. */
|
||||
export function formatCountdown(remainingMs: number): string {
|
||||
return formatClockDigits(Math.max(0, Math.ceil(remainingMs / 1000)));
|
||||
}
|
||||
|
||||
/** Elapsed time as a stopwatch reads it: rounds down, plus optional tenths. */
|
||||
export function formatElapsed(elapsedMs: number, tenths: boolean): string {
|
||||
const clamped = Math.max(0, elapsedMs);
|
||||
const digits = formatClockDigits(Math.floor(clamped / 1000));
|
||||
return tenths ? `${digits}.${Math.floor(clamped / 100) % 10}` : digits;
|
||||
}
|
||||
|
||||
/** A set duration in words: "20 min", "1h 30m", "45s", "2m 30s". */
|
||||
export function formatDurationWords(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 minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
|
||||
if (minutes > 0) return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes} min`;
|
||||
return `${seconds}s`;
|
||||
}
|
||||
|
||||
/** A span until something: "9h 12m", "12m", "45s". Coarse on purpose. */
|
||||
export function formatSpanWords(spanMs: number): string {
|
||||
const totalSeconds = Math.max(0, Math.round(spanMs / 1000));
|
||||
if (totalSeconds < 60) return `${totalSeconds}s`;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
if (hours === 0) return `${minutes}m`;
|
||||
if (hours < 24) return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
|
||||
const days = Math.floor(hours / 24);
|
||||
const restHours = hours % 24;
|
||||
return restHours > 0 ? `${days}d ${restHours}h` : `${days}d`;
|
||||
}
|
||||
|
||||
/** Wall-clock time of day in the wearer's chosen format. */
|
||||
export function formatTimeOfDay(hour24: number, minute: number, twelveHour: boolean): string {
|
||||
const minutes = pad2(minute);
|
||||
if (!twelveHour) return `${hour24}:${minutes}`;
|
||||
const hour12 = ((hour24 + 11) % 12) + 1;
|
||||
return `${hour12}:${minutes} ${hour24 < 12 ? "AM" : "PM"}`;
|
||||
}
|
||||
|
||||
export function formatClockAt(timestampMs: number, twelveHour: boolean): string {
|
||||
const date = new Date(timestampMs);
|
||||
return formatTimeOfDay(date.getHours(), date.getMinutes(), twelveHour);
|
||||
}
|
||||
|
||||
/** Row/stage label for a timer: its label, else its set duration. */
|
||||
export function timerDisplayName(timer: CountdownTimer): string {
|
||||
return timer.label || formatDurationWords(timer.durationMs);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Persistence shape
|
||||
|
||||
/**
|
||||
* Parse a persisted state blob defensively: anything malformed falls back to
|
||||
* a field default rather than failing the whole load, so a bad record loses
|
||||
* itself and nothing else.
|
||||
*/
|
||||
export function parseTimersState(raw: string | null | undefined): TimersState {
|
||||
const state = emptyTimersState();
|
||||
if (!raw) return state;
|
||||
let parsed: any;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
return state;
|
||||
}
|
||||
if (!parsed || typeof parsed !== "object") return state;
|
||||
if (Array.isArray(parsed.timers)) {
|
||||
for (const item of parsed.timers) {
|
||||
if (!item || typeof item !== "object" || !isFiniteNumber(item.id) || !isFiniteNumber(item.durationMs)) continue;
|
||||
state.timers.push({
|
||||
id: item.id,
|
||||
label: typeof item.label === "string" ? item.label : "",
|
||||
durationMs: item.durationMs,
|
||||
endsAtMs: isFiniteNumber(item.endsAtMs) ? item.endsAtMs : null,
|
||||
pausedRemainingMs: isFiniteNumber(item.pausedRemainingMs) ? item.pausedRemainingMs : null,
|
||||
rungAtMs: isFiniteNumber(item.rungAtMs) ? item.rungAtMs : null,
|
||||
createdAtMs: isFiniteNumber(item.createdAtMs) ? item.createdAtMs : 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
const stopwatch = parsed.stopwatch;
|
||||
if (stopwatch && typeof stopwatch === "object") {
|
||||
state.stopwatch = {
|
||||
accumulatedMs: isFiniteNumber(stopwatch.accumulatedMs) ? Math.max(0, stopwatch.accumulatedMs) : 0,
|
||||
runningSinceMs: isFiniteNumber(stopwatch.runningSinceMs) ? stopwatch.runningSinceMs : null,
|
||||
lapsMs: Array.isArray(stopwatch.lapsMs) ? stopwatch.lapsMs.filter(isFiniteNumber) : [],
|
||||
};
|
||||
}
|
||||
if (Array.isArray(parsed.alarms)) {
|
||||
for (const item of parsed.alarms) {
|
||||
if (!item || typeof item !== "object" || !isFiniteNumber(item.id)) continue;
|
||||
if (!isFiniteNumber(item.hour) || !isFiniteNumber(item.minute)) continue;
|
||||
state.alarms.push({
|
||||
id: item.id,
|
||||
hour: Math.min(23, Math.max(0, Math.floor(item.hour))),
|
||||
minute: Math.min(59, Math.max(0, Math.floor(item.minute))),
|
||||
days: isFiniteNumber(item.days) ? item.days & EVERY_DAY_MASK : 0,
|
||||
label: typeof item.label === "string" ? item.label : "",
|
||||
enabled: item.enabled !== false,
|
||||
snoozedUntilMs: isFiniteNumber(item.snoozedUntilMs) ? item.snoozedUntilMs : null,
|
||||
ringingSinceMs: isFiniteNumber(item.ringingSinceMs) ? item.ringingSinceMs : null,
|
||||
lastFiredAtMs: isFiniteNumber(item.lastFiredAtMs) ? item.lastFiredAtMs : null,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (Array.isArray(parsed.recentDurationsMs)) {
|
||||
state.recentDurationsMs = parsed.recentDurationsMs.filter(isFiniteNumber).slice(0, RECENT_DURATION_COUNT);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
export function serializeTimersState(state: TimersState): string {
|
||||
return JSON.stringify(state);
|
||||
}
|
||||
|
||||
function isFiniteNumber(value: unknown): value is number {
|
||||
return typeof value === "number" && Number.isFinite(value);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tool-argument parsing (shared by the assistant tools)
|
||||
|
||||
/**
|
||||
* Parse a time of day from the forms an assistant is likely to pass:
|
||||
* "7:30", "07:30", "7:30 am", "7pm", "19:30", "noon", "midnight".
|
||||
* Returns null when unparseable.
|
||||
*/
|
||||
export function parseTimeOfDay(text: string): { hour: number; minute: number } | null {
|
||||
const trimmed = text.trim().toLowerCase();
|
||||
if (trimmed === "noon") return { hour: 12, minute: 0 };
|
||||
if (trimmed === "midnight") return { hour: 0, minute: 0 };
|
||||
const match = /^(\d{1,2})(?::(\d{2}))?\s*(a\.?m\.?|p\.?m\.?)?$/.exec(trimmed);
|
||||
if (!match) return null;
|
||||
let hour = parseInt(match[1]!, 10);
|
||||
const minute = match[2] ? parseInt(match[2], 10) : 0;
|
||||
const meridiem = match[3]?.replace(/\./g, "");
|
||||
if (minute > 59) return null;
|
||||
if (meridiem) {
|
||||
if (hour < 1 || hour > 12) return null;
|
||||
hour = hour % 12;
|
||||
if (meridiem === "pm") hour += 12;
|
||||
} else if (hour > 23) {
|
||||
return null;
|
||||
}
|
||||
return { hour, minute };
|
||||
}
|
||||
|
||||
/** Parse repeat days from names ("mon", "tuesday"), "weekdays", "weekends", "daily"/"every day", "once". */
|
||||
export function parseRepeatDays(values: readonly string[] | string | null | undefined): number | null {
|
||||
if (values === null || values === undefined) return 0;
|
||||
const list =
|
||||
typeof values === "string"
|
||||
? values.toLowerCase().replace(/every\s*day/g, "daily").split(/[,\s]+/)
|
||||
: values.map((value) => String(value).toLowerCase().replace(/every\s*day/g, "daily"));
|
||||
let mask = 0;
|
||||
for (const raw of list) {
|
||||
const value = String(raw).trim().toLowerCase();
|
||||
if (!value || value === "once" || value === "none") continue;
|
||||
if (value === "weekdays") {
|
||||
mask |= WEEKDAYS_MASK;
|
||||
continue;
|
||||
}
|
||||
if (value === "weekends" || value === "weekend") {
|
||||
mask |= WEEKEND_MASK;
|
||||
continue;
|
||||
}
|
||||
if (value === "daily" || value === "every day" || value === "everyday" || value === "all") {
|
||||
mask |= EVERY_DAY_MASK;
|
||||
continue;
|
||||
}
|
||||
const index = DAY_FULL_NAMES.findIndex((name) => name.toLowerCase().startsWith(value.slice(0, 3)));
|
||||
if (index < 0 || value.length < 2) return null;
|
||||
mask |= DAY_BITS[index]!;
|
||||
}
|
||||
return mask;
|
||||
}
|
||||
@@ -57,10 +57,18 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(layer),
|
||||
menuItems: () => [{
|
||||
label: "Save",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
layer.saveTranscript();
|
||||
},
|
||||
}],
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
layer.onRemoved();
|
||||
options.stopContinuousVoiceCapture();
|
||||
shell.setTrayIcon(TRAY_ICON_ID, null);
|
||||
options.onClosed();
|
||||
|
||||
@@ -1,33 +1,35 @@
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import { writeTextToDownloads } from "../../native/file-access";
|
||||
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
|
||||
import { voiceControlBridge } from "../../native/voice-control";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { TranscriptModel, TranscriptScroll, wrapTranscribeText } from "./transcript-model";
|
||||
|
||||
/**
|
||||
* Live transcription view. Continuous mic capture is owned by the app wrapper
|
||||
* (createTranscribeAppWindow); this layer accumulates the transcript, shows
|
||||
* it, and saves it to Downloads on click.
|
||||
* it, and saves it to Downloads from the app menu.
|
||||
*/
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
export class TranscribeLayer implements Layer {
|
||||
private status = "Listening...";
|
||||
// Finalized utterances, plus the live (replace-semantics) partial appended
|
||||
// when painting.
|
||||
private finalizedText = "";
|
||||
private liveText = "";
|
||||
private readonly transcript = new TranscriptModel();
|
||||
private readonly scroll = new TranscriptScroll();
|
||||
private saveNotice = "";
|
||||
private unsubscribeTranscript: (() => void) | null = null;
|
||||
private unsubscribePause: (() => void) | null = null;
|
||||
private unsubscribeStatus: (() => void) | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => {
|
||||
this.onTranscript(event);
|
||||
this.transcript.accept(event);
|
||||
requestRender();
|
||||
});
|
||||
this.unsubscribePause = voiceControlBridge.onSpeechPause(() => this.transcript.pause());
|
||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||
this.status = state.status;
|
||||
this.saveNotice = "";
|
||||
requestRender();
|
||||
});
|
||||
}
|
||||
@@ -36,29 +38,34 @@ export class TranscribeLayer implements Layer {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const text = this.displayText() || "Listening...";
|
||||
const wrapped = wrapTranscribeText(font, text, width - 64);
|
||||
const text = this.transcript.text || "Listening...";
|
||||
const wrapped = wrapTranscribeText((text) => font.measureText(text), text, width - 64);
|
||||
|
||||
image.drawText(font, 24, 20, "Transcribe", 200);
|
||||
image.drawText(font, 24, 40, this.saveNotice || this.status, 110);
|
||||
|
||||
const step = lineStep(font) + 2;
|
||||
const bodyTop = 62;
|
||||
const footerY = height - font.lineHeight - 6;
|
||||
const bodyLines = Math.max(1, Math.floor((footerY - bodyTop) / step));
|
||||
const firstLine = Math.max(0, wrapped.length - bodyLines);
|
||||
for (let index = firstLine; index < wrapped.length; index++) {
|
||||
const bodyLines = Math.max(1, Math.floor((height - 6 - bodyTop - font.lineHeight) / step) + 1);
|
||||
const firstLine = this.scroll.layout(wrapped.length, bodyLines);
|
||||
for (let index = firstLine; index < Math.min(wrapped.length, firstLine + bodyLines); index++) {
|
||||
const y = bodyTop + (index - firstLine) * step;
|
||||
image.drawText(font, 32, y, wrapped[index]!, 230);
|
||||
}
|
||||
|
||||
image.drawText(font, 24, footerY, `${GESTURE_CLICK} save ${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
if (wrapped.length > bodyLines) {
|
||||
const trackHeight = height - bodyTop - 6;
|
||||
const thumbHeight = Math.max(8, Math.round(trackHeight * bodyLines / wrapped.length));
|
||||
const thumbY = bodyTop + Math.round((trackHeight - thumbHeight) * firstLine / (wrapped.length - bodyLines));
|
||||
image.fillRect(width - 14, bodyTop, 2, trackHeight, 45);
|
||||
image.fillRect(width - 14, thumbY, 2, thumbHeight, 140);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "click") {
|
||||
this.saveTranscript();
|
||||
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
this.scroll.scroll(event.type === "scroll-up" ? -3 : 3);
|
||||
ctx.actions.requestRender();
|
||||
return;
|
||||
}
|
||||
@@ -68,14 +75,16 @@ export class TranscribeLayer implements Layer {
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.unsubscribePause?.();
|
||||
this.unsubscribePause = null;
|
||||
this.unsubscribeTranscript?.();
|
||||
this.unsubscribeTranscript = null;
|
||||
this.unsubscribeStatus?.();
|
||||
this.unsubscribeStatus = null;
|
||||
}
|
||||
|
||||
private saveTranscript(): void {
|
||||
const text = this.displayText().trim();
|
||||
saveTranscript(): void {
|
||||
const text = this.transcript.text.trim();
|
||||
if (!text) {
|
||||
this.saveNotice = "Nothing to save yet.";
|
||||
return;
|
||||
@@ -84,25 +93,6 @@ export class TranscribeLayer implements Layer {
|
||||
const path = writeTextToDownloads(filename, `${text}\n`);
|
||||
this.saveNotice = path ? `Saved ${filename}` : "Save failed (check file access).";
|
||||
}
|
||||
|
||||
private displayText(): string {
|
||||
if (!this.finalizedText) return this.liveText;
|
||||
if (!this.liveText) return this.finalizedText;
|
||||
return `${this.finalizedText} ${this.liveText}`;
|
||||
}
|
||||
|
||||
private onTranscript(event: VoiceTranscriptEvent): void {
|
||||
if (event.isFinal) {
|
||||
const finalText = event.text.trim() || this.liveText.trim();
|
||||
if (finalText) {
|
||||
this.finalizedText = this.finalizedText ? `${this.finalizedText} ${finalText}` : finalText;
|
||||
}
|
||||
this.liveText = "";
|
||||
} else {
|
||||
// Replace semantics: the live partial is the whole current best transcript.
|
||||
this.liveText = event.text.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function transcriptTimestamp(): string {
|
||||
@@ -113,22 +103,3 @@ function transcriptTimestamp(): string {
|
||||
`-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`
|
||||
);
|
||||
}
|
||||
|
||||
function wrapTranscribeText(font: UiFont, text: string, maxWidth: number): string[] {
|
||||
const words = text.split(/\s+/).filter(Boolean);
|
||||
const lines: string[] = [];
|
||||
let line = "";
|
||||
for (const word of words) {
|
||||
const candidate = line ? `${line} ${word}` : word;
|
||||
if (line && font.measureText(candidate) > maxWidth) {
|
||||
lines.push(line);
|
||||
line = word;
|
||||
} else {
|
||||
line = candidate;
|
||||
}
|
||||
}
|
||||
if (line) {
|
||||
lines.push(line);
|
||||
}
|
||||
return lines.length ? lines : [""];
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
type TranscriptEvent = {
|
||||
text: string;
|
||||
isFinal: boolean;
|
||||
transcribeText?: string;
|
||||
paragraphBreakAfter?: boolean;
|
||||
};
|
||||
|
||||
/** Text accumulation belongs to this app, not the shared dictation stream. */
|
||||
export class TranscriptModel {
|
||||
private finalized = "";
|
||||
private live = "";
|
||||
private separator = " ";
|
||||
private breakAfterLive = false;
|
||||
|
||||
get text(): string {
|
||||
return this.finalized && this.live ? this.finalized + this.separator + this.live : this.finalized || this.live;
|
||||
}
|
||||
|
||||
accept(event: TranscriptEvent): void {
|
||||
const text = (event.transcribeText ?? event.text).trim();
|
||||
if (!event.isFinal) {
|
||||
this.live = text;
|
||||
return;
|
||||
}
|
||||
const finalText = text || this.live;
|
||||
if (finalText) this.finalized = this.finalized ? this.finalized + this.separator + finalText : finalText;
|
||||
this.live = "";
|
||||
if (finalText) this.separator = event.paragraphBreakAfter || this.breakAfterLive ? "\n" : " ";
|
||||
this.breakAfterLive = false;
|
||||
}
|
||||
|
||||
/** The onboard recognizer may finalize just before or just after the pause. */
|
||||
pause(): void {
|
||||
if (this.live) this.breakAfterLive = true;
|
||||
else if (this.finalized) this.separator = "\n";
|
||||
}
|
||||
}
|
||||
|
||||
/** An absolute history position, with an explicit follow-tail mode. */
|
||||
export class TranscriptScroll {
|
||||
firstLine = 0;
|
||||
private maximum = 0;
|
||||
private following = true;
|
||||
|
||||
layout(totalLines: number, visibleLines: number): number {
|
||||
this.maximum = Math.max(0, totalLines - visibleLines);
|
||||
this.firstLine = this.following ? this.maximum : Math.min(this.firstLine, this.maximum);
|
||||
return this.firstLine;
|
||||
}
|
||||
|
||||
scroll(delta: number): void {
|
||||
this.firstLine = Math.max(0, Math.min(this.maximum, this.firstLine + delta));
|
||||
this.following = this.firstLine === this.maximum;
|
||||
}
|
||||
}
|
||||
|
||||
export function wrapTranscribeText(measure: (text: string) => number, text: string, maxWidth: number): string[] {
|
||||
const lines: string[] = [];
|
||||
for (const paragraph of text.split("\n")) {
|
||||
let line = "";
|
||||
for (const word of paragraph.split(/\s+/).filter(Boolean)) {
|
||||
const candidate = line ? `${line} ${word}` : word;
|
||||
if (line && measure(candidate) > maxWidth) {
|
||||
lines.push(line);
|
||||
line = "";
|
||||
}
|
||||
if (measure(word) <= maxWidth) {
|
||||
line = line ? `${line} ${word}` : word;
|
||||
continue;
|
||||
}
|
||||
// Long URLs and unspaced languages must not run off the display.
|
||||
for (const char of (line ? " " : "") + word) {
|
||||
if (line && measure(line + char) > maxWidth) {
|
||||
lines.push(line);
|
||||
line = "";
|
||||
}
|
||||
line += char;
|
||||
}
|
||||
}
|
||||
lines.push(line);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import { AssistantSession, type AssistantBackendConfig, type AssistantSessionHistory } from "./session";
|
||||
import { ASSISTANT_MODEL_VALUES, type AssistantModel } from "./models";
|
||||
|
||||
export type ReasoningLevel = "default" | "low" | "medium" | "high";
|
||||
export type Conversation = {
|
||||
id: string;
|
||||
model: AssistantModel;
|
||||
reasoning: ReasoningLevel;
|
||||
session: AssistantSession | null;
|
||||
history?: AssistantSessionHistory;
|
||||
};
|
||||
type SavedConversation = Omit<Conversation, "session">;
|
||||
type SavedConversations = { selectedId: string; conversations: SavedConversation[] };
|
||||
|
||||
/** Shared selection and history for every assistant entry point. No credentials are saved. */
|
||||
export class AssistantConversations {
|
||||
private records: Conversation[] = [];
|
||||
private selectedId = "";
|
||||
private readonly listeners = new Set<() => void>();
|
||||
|
||||
constructor(
|
||||
private readonly resolve: (model: AssistantModel, reasoning: ReasoningLevel) => AssistantBackendConfig | null,
|
||||
private readonly defaultModel: () => AssistantModel,
|
||||
private readonly persist: (value: string) => void,
|
||||
saved = "",
|
||||
) {
|
||||
try {
|
||||
const data: SavedConversations = JSON.parse(saved);
|
||||
if (Array.isArray(data.conversations)) {
|
||||
this.records = data.conversations.filter((record) =>
|
||||
typeof record.id === "string" && ASSISTANT_MODEL_VALUES.includes(record.model) &&
|
||||
["default", "low", "medium", "high"].includes(record.reasoning) &&
|
||||
Array.isArray(record.history?.messages) && Array.isArray(record.history?.transcript),
|
||||
).map((record) => ({ ...record, session: null }));
|
||||
this.selectedId = data.selectedId;
|
||||
}
|
||||
} catch { /* First launch or invalid saved state. */ }
|
||||
if (!this.records.length) this.records.push(this.makeConversation());
|
||||
if (!this.records.some((record) => record.id === this.selectedId)) this.selectedId = this.records[0]!.id;
|
||||
}
|
||||
|
||||
current(): Conversation { return this.records.find((record) => record.id === this.selectedId)!; }
|
||||
list(): readonly Conversation[] { return this.records; }
|
||||
title(record = this.current()): string {
|
||||
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
|
||||
return transcript.find((entry) => entry.role === "user")?.text.replace(/\s+/g, " ").slice(0, 60) || "New session";
|
||||
}
|
||||
onChanged(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
private changed(save = true): void {
|
||||
if (save) this.persist(JSON.stringify({ selectedId: this.selectedId, conversations: this.records.map(
|
||||
({ session, ...record }) => ({ ...record, history: session?.history() ?? record.history ?? { messages: [], transcript: [] } }),
|
||||
) }));
|
||||
for (const listener of this.listeners) listener();
|
||||
}
|
||||
private makeConversation(): Conversation {
|
||||
return { id: `${Date.now()}-${Math.random().toString(36).slice(2)}`, model: this.defaultModel(), reasoning: "default", session: null };
|
||||
}
|
||||
create(): boolean {
|
||||
if (this.current().session?.isTurnActive()) return false;
|
||||
const record = this.makeConversation();
|
||||
this.records.push(record);
|
||||
this.selectedId = record.id;
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
select(id: string): boolean {
|
||||
if (this.current().session?.isTurnActive() || !this.records.some((record) => record.id === id)) return false;
|
||||
this.selectedId = id;
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
configure(model: AssistantModel, reasoning: ReasoningLevel): boolean {
|
||||
const record = this.current();
|
||||
if (record.session?.isTurnActive() || !this.resolve(model, reasoning)) return false;
|
||||
record.model = model;
|
||||
record.reasoning = reasoning;
|
||||
this.ensureSession();
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
available(model: AssistantModel, reasoning: ReasoningLevel): boolean { return this.resolve(model, reasoning) !== null; }
|
||||
ensureSession(): AssistantSession | null {
|
||||
const record = this.current();
|
||||
const config = this.resolve(record.model, record.reasoning);
|
||||
if (!config) return null;
|
||||
if (!record.session) {
|
||||
record.session = new AssistantSession(config, undefined, record.history);
|
||||
record.session.onChanged(() => this.changed(!record.session!.isTurnActive()));
|
||||
} else record.session.configure(config);
|
||||
return record.session;
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ export class DirectAssistantBackend {
|
||||
const startMs = Date.now();
|
||||
let cancelled = false;
|
||||
let streamHandle: LlmStreamHandle | null = null;
|
||||
let priorText = "";
|
||||
|
||||
const finishError = (message: string) => {
|
||||
if (cancelled) return;
|
||||
@@ -78,7 +79,7 @@ export class DirectAssistantBackend {
|
||||
tools: options.buildTools(),
|
||||
onTextDelta: (delta, textSoFar) => {
|
||||
if (cancelled) return;
|
||||
options.callbacks.onTextDelta(delta, textSoFar);
|
||||
options.callbacks.onTextDelta(delta, priorText + textSoFar);
|
||||
},
|
||||
onDone: (result) => {
|
||||
streamHandle = null;
|
||||
@@ -87,6 +88,9 @@ export class DirectAssistantBackend {
|
||||
finishError("The assistant declined this request");
|
||||
return;
|
||||
}
|
||||
const fullText = priorText + result.text;
|
||||
options.callbacks.onTextDelta("", fullText);
|
||||
priorText = fullText ? `${fullText}\n\n` : "";
|
||||
// Record what the model produced this iteration.
|
||||
const assistantContent: LlmContentBlock[] = result.content.length
|
||||
? result.content
|
||||
|
||||
@@ -23,7 +23,7 @@ export function registerNavigateTools(
|
||||
{
|
||||
name: "nav.start_navigation",
|
||||
description:
|
||||
"Start turn-by-turn navigation on the glasses to a destination given as free text (place name, business, or address near the user). Opens the Navigate app. Returns the resolved destination, distance, and ETA; if the wrong place was resolved, call again with a more specific query.",
|
||||
"Start turn-by-turn navigation on the glasses to a destination given as free text (place name, business, or address near the user), or the name of one of the user's saved destinations such as 'home' or 'work'. Opens the Navigate app. Returns the resolved destination, distance, and ETA; if the wrong place was resolved, call again with a more specific query.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
|
||||
@@ -57,7 +57,7 @@ async function callAppTool(
|
||||
args: unknown,
|
||||
): Promise<ToolResult> {
|
||||
if (roamGraphNameSetting.get().length === 0 || roamApiTokenSetting.get().length === 0) {
|
||||
return err("Roam is not configured; the user must set the graph name and API token from the Roam app's long-press menu.");
|
||||
return err("Roam is not configured; the user must set the graph name and API token from the Roam app's context menu (tap, then hold).");
|
||||
}
|
||||
return callAppToolWithLaunch(registry, launchApp, "roam", unprefixedName, args);
|
||||
}
|
||||
|
||||
+113
-47
@@ -15,26 +15,20 @@ import type {
|
||||
AssistantTurnHandle,
|
||||
} from "./types";
|
||||
|
||||
/**
|
||||
* One assistant conversation. Owns the message history and turn state and
|
||||
* drives either the on-phone provider loop (direct) or the external agent
|
||||
* bridge. The UI talks only to this surface, so AssistantLayer is
|
||||
* backend-agnostic.
|
||||
*
|
||||
* Sessions are cheap and idle-expire (see isExpired): the shell starts a fresh
|
||||
* one for a new wakeword and reuses the current one for a follow-up. In
|
||||
* external mode the conversation history lives on the agent's machine (one
|
||||
* long-lived session there), so expiry here only affects the overlay UI.
|
||||
*/
|
||||
/** One shared conversation, independent of the voice overlay or chat window. */
|
||||
export type AssistantTranscriptEntry = { role: "user" | "assistant"; text: string };
|
||||
export type AssistantSessionHistory = {
|
||||
messages: LlmMessage[];
|
||||
transcript: AssistantTranscriptEntry[];
|
||||
/** Model identity for safe replay after Auto resolves differently on restart. */
|
||||
engine?: string;
|
||||
};
|
||||
|
||||
/** Which engine answers utterances, plus what it needs to do so. */
|
||||
export type AssistantBackendConfig =
|
||||
| { kind: "direct"; llm: ResolvedAssistantModel }
|
||||
| { kind: "external"; bridge: AssistantBridgeConfig };
|
||||
|
||||
/** A new PTT after this much idle starts a fresh conversation. */
|
||||
const SESSION_IDLE_MS = 10 * 60 * 1000;
|
||||
|
||||
/** Trim history from the head once it grows past this many messages. */
|
||||
const MAX_HISTORY_MESSAGES = 40;
|
||||
|
||||
@@ -42,38 +36,61 @@ export class AssistantSession {
|
||||
private readonly directBackend = new DirectAssistantBackend();
|
||||
private readonly messages: LlmMessage[] = [];
|
||||
private turnHandle: AssistantTurnHandle | null = null;
|
||||
private lastActivityMs = Date.now();
|
||||
readonly transcript: AssistantTranscriptEntry[] = [];
|
||||
status = "";
|
||||
private readonly listeners = new Set<() => void>();
|
||||
private turnGeneration = 0;
|
||||
// API-safe tool name -> canonical registry name. Provider APIs restrict tool
|
||||
// names, so dotted registry names are sanitized and mapped back on calls.
|
||||
private readonly toolNameMap = new Map<string, string>();
|
||||
|
||||
constructor(
|
||||
private readonly config: AssistantBackendConfig,
|
||||
private config: AssistantBackendConfig,
|
||||
private readonly registry: ToolRegistry = toolRegistry,
|
||||
) {}
|
||||
|
||||
matchesConfiguration(config: AssistantBackendConfig): boolean {
|
||||
if (this.config.kind !== config.kind) return false;
|
||||
if (this.config.kind === "direct" && config.kind === "direct") {
|
||||
return (
|
||||
this.config.llm.provider === config.llm.provider &&
|
||||
this.config.llm.model === config.llm.model &&
|
||||
this.config.llm.apiKey === config.llm.apiKey
|
||||
);
|
||||
history?: AssistantSessionHistory,
|
||||
) {
|
||||
if (history) {
|
||||
this.messages.push(...history.messages);
|
||||
this.transcript.push(...history.transcript);
|
||||
if (history.engine !== this.engine()) this.rebuildTextHistory();
|
||||
}
|
||||
if (this.config.kind === "external" && config.kind === "external") {
|
||||
return (
|
||||
this.config.bridge.host === config.bridge.host &&
|
||||
this.config.bridge.port === config.bridge.port &&
|
||||
this.config.bridge.token === config.bridge.token
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Whether this session has been idle long enough to retire. */
|
||||
isExpired(nowMs = Date.now()): boolean {
|
||||
return nowMs - this.lastActivityMs > SESSION_IDLE_MS;
|
||||
onChanged(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
private changed(): void {
|
||||
for (const listener of this.listeners) {
|
||||
try { listener(); } catch (error) { console.warn("assistant history listener failed", error); }
|
||||
}
|
||||
}
|
||||
|
||||
history(): AssistantSessionHistory {
|
||||
return { messages: this.messages, transcript: this.transcript, engine: this.engine() };
|
||||
}
|
||||
|
||||
configure(config: AssistantBackendConfig): void {
|
||||
if (this.isTurnActive()) return;
|
||||
// Opaque reasoning/tool provider items are only replayable on their
|
||||
// original model. Preserve visible conversation when switching engines.
|
||||
if (this.config.kind !== config.kind ||
|
||||
(this.config.kind === "direct" && config.kind === "direct" &&
|
||||
(this.config.llm.model !== config.llm.model || this.config.llm.provider !== config.llm.provider))) {
|
||||
this.rebuildTextHistory();
|
||||
}
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
private engine(): string {
|
||||
return this.config.kind === "direct" ? `${this.config.llm.provider}:${this.config.llm.model}` : "external";
|
||||
}
|
||||
|
||||
private rebuildTextHistory(): void {
|
||||
this.messages.splice(0, this.messages.length, ...this.transcript
|
||||
.filter((entry) => entry.text.trim())
|
||||
.map((entry) => ({ role: entry.role, content: entry.text })));
|
||||
}
|
||||
|
||||
isTurnActive(): boolean {
|
||||
@@ -86,20 +103,44 @@ export class AssistantSession {
|
||||
callbacks.onError("The assistant is still working on the previous request");
|
||||
return;
|
||||
}
|
||||
this.lastActivityMs = Date.now();
|
||||
text = text.trim();
|
||||
if (!text) return;
|
||||
const generation = ++this.turnGeneration;
|
||||
const reply: AssistantTranscriptEntry = { role: "assistant", text: "" };
|
||||
this.transcript.push({ role: "user", text }, reply);
|
||||
this.status = "Thinking...";
|
||||
// Install a sentinel before calling providers: some failures are synchronous.
|
||||
this.turnHandle = { cancel: () => {} };
|
||||
this.changed();
|
||||
|
||||
const finish = () => {
|
||||
++this.turnGeneration;
|
||||
this.turnHandle = null;
|
||||
this.lastActivityMs = Date.now();
|
||||
this.changed();
|
||||
};
|
||||
const wrappedCallbacks: AssistantTurnCallbacks = {
|
||||
onTextDelta: callbacks.onTextDelta,
|
||||
onToolActivity: callbacks.onToolActivity,
|
||||
onTextDelta: (delta, full) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
reply.text = full;
|
||||
this.status = "Thinking...";
|
||||
this.changed();
|
||||
callbacks.onTextDelta(delta, full);
|
||||
},
|
||||
onToolActivity: (label) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.status = `→ ${label}`;
|
||||
this.changed();
|
||||
callbacks.onToolActivity(label);
|
||||
},
|
||||
onTurnDone: (result) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.status = "";
|
||||
finish();
|
||||
callbacks.onTurnDone(result);
|
||||
},
|
||||
onError: (message) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.status = message;
|
||||
finish();
|
||||
callbacks.onError(message);
|
||||
},
|
||||
@@ -108,7 +149,8 @@ export class AssistantSession {
|
||||
if (this.config.kind === "external") {
|
||||
// History and the agent loop live on the agent's machine; the phone just
|
||||
// streams this turn. The overlay keeps its own display state.
|
||||
this.turnHandle = assistantBridge.sendUtterance(text, ctx, wrappedCallbacks);
|
||||
const handle = assistantBridge.sendUtterance(text, ctx, wrappedCallbacks);
|
||||
if (generation === this.turnGeneration && this.turnHandle) this.turnHandle = handle;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -123,24 +165,48 @@ export class AssistantSession {
|
||||
content: isLocal ? `${text}\n\n(${describeAssistantContext(ctx)})` : text,
|
||||
});
|
||||
this.trimHistory();
|
||||
this.turnHandle = this.directBackend.runTurn({
|
||||
// Work on a turn-local array: cancelled tools may finish later, and must
|
||||
// never append orphan results into a subsequent turn's context.
|
||||
const turnMessages = this.messages.slice();
|
||||
const handle = this.directBackend.runTurn({
|
||||
provider: llm.provider,
|
||||
apiKey: llm.apiKey,
|
||||
model: llm.model,
|
||||
effort: llm.effort,
|
||||
system: isLocal ? ASSISTANT_SYSTEM_PROMPT_BASE : buildAssistantSystemPrompt(ctx),
|
||||
messages: this.messages,
|
||||
messages: turnMessages,
|
||||
buildTools: () => this.buildToolDefinitions(),
|
||||
registry: this.registry,
|
||||
resolveToolName: (apiName) => this.toolNameMap.get(apiName) ?? apiName,
|
||||
callbacks: wrappedCallbacks,
|
||||
callbacks: {
|
||||
...wrappedCallbacks,
|
||||
onTurnDone: (result) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.messages.splice(0, this.messages.length, ...turnMessages);
|
||||
wrappedCallbacks.onTurnDone(result);
|
||||
},
|
||||
onError: (message) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
// Retain the visible partial answer, without incomplete tool pairs.
|
||||
if (reply.text) this.messages.push({ role: "assistant", content: reply.text });
|
||||
wrappedCallbacks.onError(message);
|
||||
},
|
||||
},
|
||||
});
|
||||
if (generation === this.turnGeneration && this.turnHandle) this.turnHandle = handle;
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.turnHandle?.cancel();
|
||||
if (!this.turnHandle) return;
|
||||
++this.turnGeneration;
|
||||
this.turnHandle.cancel();
|
||||
this.turnHandle = null;
|
||||
this.lastActivityMs = Date.now();
|
||||
const reply = this.transcript[this.transcript.length - 1];
|
||||
if (reply?.role === "assistant" && reply.text) {
|
||||
this.messages.push({ role: "assistant", content: reply.text });
|
||||
}
|
||||
this.status = "Cancelled";
|
||||
this.changed();
|
||||
}
|
||||
|
||||
private buildToolDefinitions(): LlmToolDefinition[] {
|
||||
|
||||
+383
-26
@@ -1,76 +1,433 @@
|
||||
/**
|
||||
* Shell-side timer.* assistant tools. The Timer app's own tools are
|
||||
* window-scoped ("open" tier), but "start a 5 minute timer" must work with no
|
||||
* Timer window open — so these always-available wrappers launch the app,
|
||||
* wait for its tools to register, and forward the call. Timer state lives in
|
||||
* the app's worker, which outlives its window, so list/cancel also route
|
||||
* through the app rather than answering "no timers" from the shell.
|
||||
* The assistant's timer.* / stopwatch.* / alarm.* tools. They talk to the
|
||||
* Timers app's engine directly (it boots with the shell), so every one works
|
||||
* with no Timers window open; timer.set and alarm.set also bring the window
|
||||
* forward so the wearer sees what was set.
|
||||
*/
|
||||
import { toolRegistry, type ToolRegistry } from "./tool-registry";
|
||||
import { callAppToolWithLaunch } from "./launch-on-call";
|
||||
import {
|
||||
alarmNextRingMs,
|
||||
formatClockAt,
|
||||
formatCountdown,
|
||||
formatDays,
|
||||
formatDurationWords,
|
||||
formatElapsed,
|
||||
formatSpanWords,
|
||||
formatTimeOfDay,
|
||||
MAX_TIMER_DURATION_MS,
|
||||
parseRepeatDays,
|
||||
parseTimeOfDay,
|
||||
sortAlarms,
|
||||
sortTimers,
|
||||
stopwatchElapsedMs,
|
||||
stopwatchIsRunning,
|
||||
timerDisplayName,
|
||||
timerPhase,
|
||||
timerRemainingMs,
|
||||
type Alarm,
|
||||
type CountdownTimer,
|
||||
} from "../apps/timer/timer-model";
|
||||
import { timerEngine } from "../apps/timer/timer-engine";
|
||||
import { timeFormatSetting } from "../ui/dashboard-settings";
|
||||
import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry";
|
||||
|
||||
let registered = false;
|
||||
|
||||
// Wrapper timeout: covers a first-launch worker spawn (up to the 5 s
|
||||
// tool-appear wait in callAppToolWithLaunch) plus the forwarded call's own
|
||||
// 10 s default budget.
|
||||
const TIMER_WRAPPER_TIMEOUT_MS = 20_000;
|
||||
function twelveHour(): boolean {
|
||||
return timeFormatSetting.get() === "12h";
|
||||
}
|
||||
|
||||
export function registerTimerTools(
|
||||
launchApp: (appId: string) => Promise<void>,
|
||||
registry: ToolRegistry = toolRegistry,
|
||||
): void {
|
||||
/** Timers in the order the app lists them, so "timer 2" means the same thing in both places. */
|
||||
function listedTimers(): CountdownTimer[] {
|
||||
return sortTimers(timerEngine.state.timers, Date.now());
|
||||
}
|
||||
|
||||
function listedAlarms(): Alarm[] {
|
||||
return sortAlarms(timerEngine.state.alarms);
|
||||
}
|
||||
|
||||
function describeTimer(timer: CountdownTimer, number: number, now: number): string {
|
||||
const name = timerDisplayName(timer);
|
||||
switch (timerPhase(timer)) {
|
||||
case "rung":
|
||||
return `Timer ${number} (${name}): finished ${formatSpanWords(now - timer.rungAtMs!)} ago, still ringing until dismissed.`;
|
||||
case "paused":
|
||||
return `Timer ${number} (${name}): paused with ${formatCountdown(timerRemainingMs(timer, now))} left.`;
|
||||
default:
|
||||
return `Timer ${number} (${name}): ${formatCountdown(timerRemainingMs(timer, now))} left, ends at ${formatClockAt(timer.endsAtMs!, twelveHour())}.`;
|
||||
}
|
||||
}
|
||||
|
||||
function describeAlarm(alarm: Alarm, number: number, now: number): string {
|
||||
const time = formatTimeOfDay(alarm.hour, alarm.minute, twelveHour());
|
||||
const name = alarm.label ? ` (${alarm.label})` : "";
|
||||
if (alarm.ringingSinceMs !== null) return `Alarm ${number}: ${time}${name}, ringing now.`;
|
||||
if (!alarm.enabled) return `Alarm ${number}: ${time}${name}, ${formatDays(alarm.days).toLowerCase()}, off.`;
|
||||
const next = alarmNextRingMs(alarm, now);
|
||||
const when = next === null ? "" : `, rings in ${formatSpanWords(next - now)}`;
|
||||
const snoozed = alarm.snoozedUntilMs !== null ? " (snoozed)" : "";
|
||||
return `Alarm ${number}: ${time}${name}, ${formatDays(alarm.days).toLowerCase()}${when}${snoozed}.`;
|
||||
}
|
||||
|
||||
/** Resolve a 1-based "timer"/"alarm" argument, or the only item when omitted. */
|
||||
function pickItem<T>(items: T[], raw: unknown, noun: string, describe: (item: T, number: number) => string): T | ToolResult {
|
||||
if (items.length === 0) return { ok: false, error: `No ${noun}s are set.` };
|
||||
if (raw === undefined || raw === null) {
|
||||
if (items.length === 1) return items[0]!;
|
||||
return {
|
||||
ok: false,
|
||||
error: `There are ${items.length} ${noun}s; give the ${noun} number.\n${items.map(describe).join("\n")}`,
|
||||
};
|
||||
}
|
||||
const index = Math.floor(Number(raw));
|
||||
if (!Number.isFinite(index) || index < 1 || index > items.length) {
|
||||
return { ok: false, error: `No ${noun} ${raw}; there are ${items.length} ${noun}s.` };
|
||||
}
|
||||
return items[index - 1]!;
|
||||
}
|
||||
|
||||
function isResult(value: unknown): value is ToolResult {
|
||||
return !!value && typeof value === "object" && "ok" in (value as object) && !("id" in (value as object));
|
||||
}
|
||||
|
||||
function durationField(value: unknown, label: string): number {
|
||||
if (value === undefined || value === null || value === "") return 0;
|
||||
const parsed = Number(value);
|
||||
if (!Number.isFinite(parsed) || parsed < 0) throw new Error(`Invalid ${label} value: ${value}`);
|
||||
return parsed;
|
||||
}
|
||||
|
||||
const NUMBER_ARG = (noun: string, listTool: string) => ({
|
||||
type: "number",
|
||||
description: `1-based ${noun} number, as shown by ${listTool}. May be omitted when only one ${noun} exists.`,
|
||||
});
|
||||
|
||||
export function registerTimerTools(launchApp: (appId: string) => Promise<void>, registry: ToolRegistry = toolRegistry): void {
|
||||
if (registered) return;
|
||||
registered = true;
|
||||
|
||||
const showApp = () => {
|
||||
void launchApp("timer").catch((error) => console.warn(`timer tool launch failed: ${error}`));
|
||||
};
|
||||
|
||||
// --- timers ---
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "timer.set",
|
||||
description:
|
||||
"Start a countdown timer on the glasses. Give the duration as hours/minutes/seconds; at least one must be nonzero. Opens the Timer app if it isn't already open. Returns the timer's number and when it will finish.",
|
||||
"Start a countdown timer on the glasses. Give the duration as hours/minutes/seconds (at least one nonzero) and optionally a label such as \"pasta\". Returns the timer's number and when it will finish.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
hours: { type: "number", description: "Hours component of the duration (default 0)." },
|
||||
minutes: { type: "number", description: "Minutes component of the duration (default 0)." },
|
||||
seconds: { type: "number", description: "Seconds component of the duration (default 0)." },
|
||||
label: { type: "string", description: "Optional short name for the timer." },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
timeoutMs: TIMER_WRAPPER_TIMEOUT_MS,
|
||||
proactive: true,
|
||||
},
|
||||
(args) => callAppToolWithLaunch(registry, launchApp, "timer", "set_timer", args ?? {}),
|
||||
(args) => {
|
||||
const durationMs = Math.round(
|
||||
((durationField(args?.hours, "hours") * 60 + durationField(args?.minutes, "minutes")) * 60 +
|
||||
durationField(args?.seconds, "seconds")) *
|
||||
1000,
|
||||
);
|
||||
if (durationMs <= 0) return { ok: false, error: "timer.set requires a duration longer than zero." };
|
||||
if (durationMs > MAX_TIMER_DURATION_MS) return { ok: false, error: "Timers can run for at most 24 hours." };
|
||||
const label = typeof args?.label === "string" ? args.label.trim() : "";
|
||||
const timer = timerEngine.startTimer(durationMs, label);
|
||||
showApp();
|
||||
const number = listedTimers().indexOf(timer) + 1;
|
||||
return {
|
||||
ok: true,
|
||||
content: `Started a ${formatDurationWords(durationMs)} timer${label ? ` "${label}"` : ""} (timer ${number}), finishing at ${formatClockAt(timer.endsAtMs!, twelveHour())}.`,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "timer.list",
|
||||
description:
|
||||
"List the countdown timers on the glasses: each timer's number, its original duration, and the time remaining (or that it has finished). Opens the Timer app if it isn't already open — timers keep running while its window is closed.",
|
||||
"List the countdown timers on the glasses: each timer's number, name, time remaining (or paused / finished), and end time.",
|
||||
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
||||
timeoutMs: TIMER_WRAPPER_TIMEOUT_MS,
|
||||
proactive: true,
|
||||
},
|
||||
() => callAppToolWithLaunch(registry, launchApp, "timer", "list_timers", {}),
|
||||
() => {
|
||||
const timers = listedTimers();
|
||||
if (timers.length === 0) return { ok: true, content: "No timers are set." };
|
||||
const now = Date.now();
|
||||
return { ok: true, content: timers.map((timer, index) => describeTimer(timer, index + 1, now)).join("\n") };
|
||||
},
|
||||
);
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "timer.cancel",
|
||||
description:
|
||||
"Cancel a running countdown timer on the glasses, or dismiss a finished one. Give the timer number from timer.list; it may be omitted when only one timer exists. Pass all=true to clear every timer. Opens the Timer app if it isn't already open.",
|
||||
"Cancel a countdown timer, or dismiss a finished one. Give the timer number from timer.list (omit it when only one exists), or all=true to clear every timer.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
timer: { type: "number", description: "1-based timer number, as shown by timer.list." },
|
||||
timer: NUMBER_ARG("timer", "timer.list"),
|
||||
all: { type: "boolean", description: "Clear all timers instead of a single one." },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
timeoutMs: TIMER_WRAPPER_TIMEOUT_MS,
|
||||
proactive: true,
|
||||
},
|
||||
(args) => callAppToolWithLaunch(registry, launchApp, "timer", "cancel_timer", args ?? {}),
|
||||
(args) => {
|
||||
const timers = listedTimers();
|
||||
if (args?.all === true) {
|
||||
if (timers.length === 0) return { ok: true, content: "No timers are set." };
|
||||
for (const timer of timers) timerEngine.removeTimer(timer.id);
|
||||
return { ok: true, content: timers.length === 1 ? "Cleared the timer." : `Cleared ${timers.length} timers.` };
|
||||
}
|
||||
const now = Date.now();
|
||||
const picked = pickItem(timers, args?.timer, "timer", (timer, number) => describeTimer(timer, number, now));
|
||||
if (isResult(picked)) return picked;
|
||||
const description = describeTimer(picked, timers.indexOf(picked) + 1, now);
|
||||
timerEngine.removeTimer(picked.id);
|
||||
return { ok: true, content: `${timerPhase(picked) === "rung" ? "Dismissed" : "Canceled"}: ${description}` };
|
||||
},
|
||||
);
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "timer.pause",
|
||||
description: "Pause a running countdown timer, or resume a paused one. Give the timer number from timer.list (omit it when only one exists).",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
timer: NUMBER_ARG("timer", "timer.list"),
|
||||
resume: { type: "boolean", description: "true to resume a paused timer instead of pausing." },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
proactive: true,
|
||||
},
|
||||
(args) => {
|
||||
const timers = listedTimers();
|
||||
const now = Date.now();
|
||||
const picked = pickItem(timers, args?.timer, "timer", (timer, number) => describeTimer(timer, number, now));
|
||||
if (isResult(picked)) return picked;
|
||||
const phase = timerPhase(picked);
|
||||
if (phase === "rung") return { ok: false, error: "That timer has already finished; dismiss it with timer.cancel." };
|
||||
const resume = args?.resume === true || phase === "paused";
|
||||
if (resume) timerEngine.resumeTimer(picked.id);
|
||||
else timerEngine.pauseTimer(picked.id);
|
||||
const updated = timerEngine.findTimer(picked.id)!;
|
||||
return { ok: true, content: `${resume ? "Resumed" : "Paused"} ${describeTimer(updated, timers.indexOf(picked) + 1, Date.now())}` };
|
||||
},
|
||||
);
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "timer.add_time",
|
||||
description: "Add minutes to a countdown timer (a finished one restarts with just the added time). Give the timer number from timer.list (omit it when only one exists).",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
timer: NUMBER_ARG("timer", "timer.list"),
|
||||
minutes: { type: "number", description: "Minutes to add (default 1)." },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
proactive: true,
|
||||
},
|
||||
(args) => {
|
||||
const timers = listedTimers();
|
||||
const now = Date.now();
|
||||
const picked = pickItem(timers, args?.timer, "timer", (timer, number) => describeTimer(timer, number, now));
|
||||
if (isResult(picked)) return picked;
|
||||
const minutes = args?.minutes === undefined ? 1 : Number(args.minutes);
|
||||
if (!Number.isFinite(minutes) || minutes <= 0) return { ok: false, error: `Invalid minutes value: ${args?.minutes}` };
|
||||
timerEngine.addTimerTime(picked.id, Math.round(minutes * 60_000));
|
||||
const updated = timerEngine.findTimer(picked.id)!;
|
||||
return { ok: true, content: `Added ${formatDurationWords(minutes * 60_000)}. ${describeTimer(updated, listedTimers().indexOf(updated) + 1, Date.now())}` };
|
||||
},
|
||||
);
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "timer.dismiss",
|
||||
description: "Stop whatever is ringing on the glasses right now: finished timers are cleared and ringing alarms dismissed.",
|
||||
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
||||
proactive: true,
|
||||
},
|
||||
() => {
|
||||
const count = timerEngine.dismissAllRinging();
|
||||
return { ok: true, content: count === 0 ? "Nothing is ringing." : `Dismissed ${count === 1 ? "it" : `${count} items`}.` };
|
||||
},
|
||||
);
|
||||
|
||||
// --- stopwatch ---
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "stopwatch.control",
|
||||
description:
|
||||
"Control the stopwatch on the glasses: action start, stop, lap, reset, or read (report the elapsed time and laps without changing anything).",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
action: { type: "string", enum: ["start", "stop", "lap", "reset", "read"], description: "What to do." },
|
||||
},
|
||||
required: ["action"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
proactive: true,
|
||||
},
|
||||
(args) => {
|
||||
const action = String(args?.action ?? "read");
|
||||
switch (action) {
|
||||
case "start":
|
||||
timerEngine.stopwatchStart();
|
||||
showApp();
|
||||
break;
|
||||
case "stop":
|
||||
timerEngine.stopwatchStop();
|
||||
break;
|
||||
case "lap":
|
||||
if (!stopwatchIsRunning(timerEngine.state.stopwatch)) return { ok: false, error: "The stopwatch is not running." };
|
||||
timerEngine.stopwatchLap();
|
||||
break;
|
||||
case "reset":
|
||||
timerEngine.stopwatchReset();
|
||||
break;
|
||||
case "read":
|
||||
break;
|
||||
default:
|
||||
return { ok: false, error: `Unknown stopwatch action: ${action}` };
|
||||
}
|
||||
const stopwatch = timerEngine.state.stopwatch;
|
||||
const elapsed = formatElapsed(stopwatchElapsedMs(stopwatch, Date.now()), true);
|
||||
const state = stopwatchIsRunning(stopwatch) ? "running" : "stopped";
|
||||
const laps = stopwatch.lapsMs.length > 0 ? ` Laps: ${stopwatch.lapsMs.map((lap) => formatElapsed(lap, true)).join(", ")}.` : "";
|
||||
return { ok: true, content: `Stopwatch ${state} at ${elapsed}.${laps}` };
|
||||
},
|
||||
);
|
||||
|
||||
// --- alarms ---
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "alarm.set",
|
||||
description:
|
||||
"Set an alarm on the glasses. Give the time of day (e.g. \"7:30\", \"7:30 am\", \"19:30\"), optional repeat days (day names, \"weekdays\", \"weekends\", \"every day\"; default once), and an optional label.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
time: { type: "string", description: "Time of day, 12- or 24-hour." },
|
||||
days: {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
description: "Repeat days: day names or weekdays / weekends / every day. Omit for a one-off alarm.",
|
||||
},
|
||||
label: { type: "string", description: "Optional short name for the alarm." },
|
||||
},
|
||||
required: ["time"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
proactive: true,
|
||||
},
|
||||
(args) => {
|
||||
const time = parseTimeOfDay(String(args?.time ?? ""));
|
||||
if (!time) return { ok: false, error: `Could not understand the time "${args?.time}". Use a form like 7:30 am or 19:30.` };
|
||||
const days = parseRepeatDays(args?.days);
|
||||
if (days === null) return { ok: false, error: `Could not understand the repeat days ${JSON.stringify(args?.days)}.` };
|
||||
const label = typeof args?.label === "string" ? args.label.trim() : "";
|
||||
const alarm = timerEngine.addAlarm(time.hour, time.minute, days, label);
|
||||
showApp();
|
||||
const now = Date.now();
|
||||
return { ok: true, content: `Set. ${describeAlarm(alarm, listedAlarms().indexOf(alarm) + 1, now)}` };
|
||||
},
|
||||
);
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "alarm.list",
|
||||
description: "List the alarms on the glasses: each alarm's number, time, repeat days, label, and whether it is on.",
|
||||
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
||||
proactive: true,
|
||||
},
|
||||
() => {
|
||||
const alarms = listedAlarms();
|
||||
if (alarms.length === 0) return { ok: true, content: "No alarms are set." };
|
||||
const now = Date.now();
|
||||
return { ok: true, content: alarms.map((alarm, index) => describeAlarm(alarm, index + 1, now)).join("\n") };
|
||||
},
|
||||
);
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "alarm.enable",
|
||||
description: "Turn an alarm on or off without deleting it. Give the alarm number from alarm.list (omit it when only one exists).",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
alarm: NUMBER_ARG("alarm", "alarm.list"),
|
||||
enabled: { type: "boolean", description: "true to turn the alarm on, false to turn it off." },
|
||||
},
|
||||
required: ["enabled"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
proactive: true,
|
||||
},
|
||||
(args) => {
|
||||
const alarms = listedAlarms();
|
||||
const now = Date.now();
|
||||
const picked = pickItem(alarms, args?.alarm, "alarm", (alarm, number) => describeAlarm(alarm, number, now));
|
||||
if (isResult(picked)) return picked;
|
||||
timerEngine.setAlarmEnabled(picked.id, args?.enabled !== false);
|
||||
const updated = timerEngine.findAlarm(picked.id)!;
|
||||
return { ok: true, content: describeAlarm(updated, alarms.indexOf(picked) + 1, Date.now()) };
|
||||
},
|
||||
);
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "alarm.delete",
|
||||
description: "Delete an alarm. Give the alarm number from alarm.list (omit it when only one exists).",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: { alarm: NUMBER_ARG("alarm", "alarm.list") },
|
||||
additionalProperties: false,
|
||||
},
|
||||
proactive: true,
|
||||
},
|
||||
(args) => {
|
||||
const alarms = listedAlarms();
|
||||
const now = Date.now();
|
||||
const picked = pickItem(alarms, args?.alarm, "alarm", (alarm, number) => describeAlarm(alarm, number, now));
|
||||
if (isResult(picked)) return picked;
|
||||
const description = describeAlarm(picked, alarms.indexOf(picked) + 1, now);
|
||||
timerEngine.deleteAlarm(picked.id);
|
||||
return { ok: true, content: `Deleted: ${description}` };
|
||||
},
|
||||
);
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "alarm.snooze",
|
||||
description: "Snooze the alarm that is ringing now. Optionally give the snooze length in minutes (default: the Timers app's snooze setting).",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: { minutes: { type: "number", description: "Snooze length in minutes." } },
|
||||
additionalProperties: false,
|
||||
},
|
||||
proactive: true,
|
||||
},
|
||||
(args) => {
|
||||
const ringing = timerEngine.state.alarms.filter((alarm) => alarm.ringingSinceMs !== null);
|
||||
if (ringing.length === 0) return { ok: false, error: "No alarm is ringing." };
|
||||
const minutes = args?.minutes === undefined ? undefined : Number(args.minutes);
|
||||
if (minutes !== undefined && (!Number.isFinite(minutes) || minutes <= 0)) return { ok: false, error: `Invalid minutes value: ${args?.minutes}` };
|
||||
for (const alarm of ringing) timerEngine.snoozeAlarm(alarm.id, minutes);
|
||||
const updated = timerEngine.findAlarm(ringing[0]!.id)!;
|
||||
return { ok: true, content: `Snoozed until ${formatClockAt(updated.snoozedUntilMs!, twelveHour())}.` };
|
||||
},
|
||||
);
|
||||
}
|
||||
+189
-18
@@ -12,7 +12,8 @@ import * as frameTimings from "../native/frame-timings";
|
||||
import { startForegroundNotification, stopForegroundNotification, updateForegroundNotification } from "../native/foreground-service";
|
||||
import { mediaControllerBridge } from "../native/media-controller";
|
||||
import { nightscoutBridge } from "../native/nightscout-bridge";
|
||||
import { onAndroidNotificationPosted } from "../native/notification-icons";
|
||||
import { ALL_NOTIFICATIONS, onAndroidNotificationPosted, readActiveNotifications } from "../native/notification-icons";
|
||||
import { shouldShowNotificationOnGlasses } from "../native/notification-sources";
|
||||
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
|
||||
import { grayImageToPreviewSource } from "../native/gray-image-preview";
|
||||
import { firmwareIncompatibilityMessage } from "./firmware-compat";
|
||||
@@ -52,11 +53,12 @@ import { getDefaultMediumFont } from "../graphics/ui-fonts";
|
||||
import { wrapText } from "../graphics/textwrap";
|
||||
import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell";
|
||||
import { registerSystemTools } from "../assistant/system-tools";
|
||||
import { updateGlassesPresence } from "./glasses-presence";
|
||||
import { timerEngine } from "../apps/timer/timer-engine";
|
||||
import { registerNavigateTools } from "../assistant/navigate-tools";
|
||||
import { registerRoamTools } from "../assistant/roam-tools";
|
||||
import { assistantBridge } from "../assistant/bridge-client";
|
||||
import { registerWindowTools } from "../assistant/window-tools";
|
||||
import { registerTimerTools } from "../assistant/timer-tools";
|
||||
import { WorkerAppHost } from "../ui/shell/worker-window";
|
||||
import { ALL_APPS } from "../apps/all-apps";
|
||||
import { type AppContext, type AppDefinition, type AppLaunchParams, type TextEditorHost } from "../apps/app-definition";
|
||||
@@ -64,7 +66,8 @@ import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-p
|
||||
import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence";
|
||||
import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible, type WindowHeightMode } from "../ui/shell/geometry";
|
||||
import { type LayerActions, type TextSettingsEditToggle } from "../ui/layers";
|
||||
import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, displayModeSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
|
||||
import { type KeyboardInputSession } from "../ui/shell/keyboard-input";
|
||||
import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
|
||||
import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization";
|
||||
import {
|
||||
getInstalledEvenHubAppById,
|
||||
@@ -72,6 +75,8 @@ import {
|
||||
uninstallEvenHubPackage,
|
||||
} from "../apps/evenhub/installed-apps";
|
||||
import { closeRunningPackage, launchInstalledPackage } from "../apps/evenhub/manager";
|
||||
import { openEvenHubStoreForPackage } from "../apps/evenhub";
|
||||
import { isInstalledPackagePresent } from "../apps/evenhub/updates";
|
||||
import { wearerVerificationOptions } from "../apps/microphones/speakers";
|
||||
import { micSession } from "../apps/microphones/mic-session";
|
||||
import { glassesDisplayLabel } from "./glasses-display-state";
|
||||
@@ -102,6 +107,13 @@ export type DashboardSnapshot = {
|
||||
activeTextEditorToggleLabel: string;
|
||||
activeTextEditorToggleValue: boolean;
|
||||
activeTextEditorToggleVisible: boolean;
|
||||
/**
|
||||
* The keyboard dialog is up on the glasses (opened by the phone's keyboard
|
||||
* button): the phone shows its typing panel, with a send button per
|
||||
* destination in the glasses menu's row order.
|
||||
*/
|
||||
keyboardInputActive: boolean;
|
||||
keyboardInputTargets: ReadonlyArray<{ id: string; label: string }>;
|
||||
evenAppConflictMessage: string;
|
||||
evenAppConflictWarningVisible: boolean;
|
||||
firmwareWarningMessage: string;
|
||||
@@ -109,6 +121,8 @@ export type DashboardSnapshot = {
|
||||
screenRecordingActive: boolean;
|
||||
batteryOptimizationWarningVisible: boolean;
|
||||
fontsMissingWarningVisible: boolean;
|
||||
/** Why the phone may fail to ring for a set alarm; "" when nothing is wrong or armed. */
|
||||
alarmReliabilityMessage: string;
|
||||
/**
|
||||
* True while the headless preview display is standing in for a glasses
|
||||
* connection (preview-only mode): the mirror is live and interactive, but
|
||||
@@ -122,6 +136,8 @@ type DashboardListener = (snapshot: DashboardSnapshot) => void;
|
||||
// The shell chrome (sidebar + top bar + overlays) composites above all app
|
||||
// window surfaces with color-key transparency.
|
||||
const SHELL_SURFACE_ID = "shell";
|
||||
/** The shell chrome composites above every window surface (zOrder 0). */
|
||||
const SHELL_SURFACE_Z_ORDER = 1;
|
||||
const LOCK_SCREEN_SURFACE_ID = "lock-screen";
|
||||
const LOCK_SCREEN_MESSAGE = "Glasses locked; unlock the phone to unlock the glasses.";
|
||||
// Top-bar clock refresh; the phone-side preview polls the Java composite so
|
||||
@@ -210,6 +226,7 @@ class DashboardController {
|
||||
private firmwareWarningMessage = "";
|
||||
private batteryOptimizationWarningVisible = false;
|
||||
private fontsMissingWarningVisible = false;
|
||||
private alarmReliabilityMessage = "";
|
||||
// Sticky positive: the extracted-font file doesn't vanish mid-session, so a
|
||||
// successful check spares later refreshes the file read and JSON parse.
|
||||
private extractedFontsConfirmed = false;
|
||||
@@ -265,11 +282,16 @@ class DashboardController {
|
||||
/** Input frame that requested the next shell render; see requestShellRender. */
|
||||
private pendingShellRenderCauseFrameId = 0;
|
||||
private nextShellRenderWantsFreshData = false;
|
||||
/** The under-shell dim last sent to the display (see Shell.underlayDim); 1 = none. */
|
||||
private appliedUnderlayDim = 1;
|
||||
// One shared worker per app hosts all its windows; spawned on first launch.
|
||||
private readonly appHosts = new Map<string, WorkerAppHost>();
|
||||
// The window hosting the on-glasses text-setting editor (the Settings app
|
||||
// registers itself); edit flows from the phone UI reach it through this.
|
||||
private textEditorHost: TextEditorHost | null = null;
|
||||
// The open keyboard dialog's session (see Shell.startKeyboardInput); the
|
||||
// phone's typing panel feeds it.
|
||||
private keyboardInput: KeyboardInputSession | null = null;
|
||||
// Other in-process singleton apps, keyed by windowId.
|
||||
private readonly inProcessApps = new Map<string, InProcessWindow>();
|
||||
private sharedActions!: Omit<LayerActions, "requestRender">;
|
||||
@@ -297,7 +319,7 @@ class DashboardController {
|
||||
toggle?: TextSettingsEditToggle,
|
||||
) => this.startTextSettingsEdit(settings, title, onFinish, toggle),
|
||||
endTextSettingEdit: () => this.endTextSettingEdit(),
|
||||
startVoiceCapture: () => this.startVoiceCapture(),
|
||||
startVoiceCapture: (endpointing = false) => this.startVoiceCapture(endpointing),
|
||||
stopVoiceCapture: () => this.stopVoiceCapture(),
|
||||
startContinuousVoiceCapture: () => this.startContinuousVoiceCapture(),
|
||||
stopContinuousVoiceCapture: () => this.stopContinuousVoiceCapture(),
|
||||
@@ -311,8 +333,6 @@ class DashboardController {
|
||||
registerNavigateTools((appId) => this.launchApp(appId));
|
||||
// roam.* tools launch the Roam app on demand likewise.
|
||||
registerRoamTools((appId) => this.launchApp(appId));
|
||||
// timer.* tools launch the Timer app on demand likewise.
|
||||
registerTimerTools((appId) => this.launchApp(appId));
|
||||
// apps.* tools mirror the launcher grid and sidebar (launch, focus, close).
|
||||
registerWindowTools({
|
||||
apps: LAUNCHABLE_APPS,
|
||||
@@ -329,6 +349,10 @@ class DashboardController {
|
||||
getScreenTimeoutMs: () => screenTimeoutSettingToMs(screenTimeoutSetting.get()),
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
prepareVoiceCapture: () => this.prepareVoiceCapture(),
|
||||
onKeyboardInputChanged: (session) => {
|
||||
this.keyboardInput = session;
|
||||
this.emit();
|
||||
},
|
||||
onWindowsChanged: () => {
|
||||
this.persistOpenApps();
|
||||
// The foreground title is mirrored on both remote-control faces.
|
||||
@@ -345,11 +369,13 @@ class DashboardController {
|
||||
for (const app of ALL_APPS) {
|
||||
app.boot?.(this.buildAppContext(app));
|
||||
}
|
||||
timerEngine.onChange(() => this.refreshAlarmReliabilityWarning());
|
||||
this.offAndroidNotification = onAndroidNotificationPosted((notificationKey) => {
|
||||
void this.handleAndroidNotificationPosted(notificationKey).catch((error) => {
|
||||
this.appendLog(`notification wake failed: ${this.formatError(error)}`);
|
||||
});
|
||||
});
|
||||
readActiveNotifications(ALL_NOTIFICATIONS);
|
||||
// Settings toggled from the glasses can change what the phone UI shows
|
||||
// (e.g. the text-setting editor), so re-emit the snapshot on any change.
|
||||
onAnySettingChanged(() => {
|
||||
@@ -360,6 +386,7 @@ class DashboardController {
|
||||
this.syncEvenHubScreenOffSetting();
|
||||
this.applyVerticalPositionIfChanged();
|
||||
this.applyDisplayModeIfChanged();
|
||||
this.applyAppLayoutsIfChanged();
|
||||
this.syncAssistantBridgeIfChanged();
|
||||
this.syncLockScreenSettingIfChanged();
|
||||
// A Preview color change should show on the mirror at once, not at the
|
||||
@@ -437,8 +464,8 @@ class DashboardController {
|
||||
/**
|
||||
* Display mode changed (Settings > Display, or the phone page's picker):
|
||||
* every window's viewport size changes. In-process windows re-measure in
|
||||
* place; worker windows get their canvas once at open, so they are closed
|
||||
* and launched again at the new size.
|
||||
* place; workers that support resizing receive the new viewport. Other
|
||||
* worker windows are closed and launched again at the new size.
|
||||
*/
|
||||
private applyDisplayModeIfChanged(): void {
|
||||
const mode = displayModeSetting.get();
|
||||
@@ -474,6 +501,31 @@ class DashboardController {
|
||||
});
|
||||
}
|
||||
|
||||
private lastAppLayouts = this.appLayouts();
|
||||
|
||||
private appLayouts(): Record<string, string> {
|
||||
return {
|
||||
navigate: `${navigateDisplayModeSetting.get()}:${navigateVerticalPositionSetting.get()}`,
|
||||
terminal: `${terminalDisplayModeSetting.get()}:${terminalVerticalPositionSetting.get()}`,
|
||||
};
|
||||
}
|
||||
|
||||
private applyAppLayoutsIfChanged(): void {
|
||||
const layouts = this.appLayouts();
|
||||
const changed = Object.keys(layouts).filter((appId) => layouts[appId] !== this.lastAppLayouts[appId]);
|
||||
if (!changed.length) return;
|
||||
this.lastAppLayouts = layouts;
|
||||
void (async () => {
|
||||
for (const window of Array.from(shell.getWindows())) {
|
||||
if (!changed.includes(window.appId)) continue;
|
||||
await this.configureWindowSurface(window.surfaceId, window.windowId === shell.foregroundWindow()?.windowId, window.heightMode);
|
||||
window.relayout?.();
|
||||
}
|
||||
shell.foregroundWindow()?.requestRender();
|
||||
this.requestShellRender();
|
||||
})().catch((error) => this.appendLog(`app layout change failed: ${this.formatError(error)}`));
|
||||
}
|
||||
|
||||
private applyVerticalPositionIfChanged(): void {
|
||||
const position = verticalPositionSetting.get();
|
||||
if (position === this.lastVerticalPosition) return;
|
||||
@@ -546,6 +598,7 @@ class DashboardController {
|
||||
|
||||
private handleWearState(wearing: boolean): void {
|
||||
this.glassesWorn = wearing;
|
||||
updateGlassesPresence({ worn: wearing });
|
||||
this.appendLog(wearing ? "glasses wear state: ON_HEAD" : "glasses wear state: OFF_HEAD");
|
||||
this.wearRemote?.schedulePublish();
|
||||
if (!wearing && this.phoneLocked && lockScreenEnabledSetting.get()) {
|
||||
@@ -905,6 +958,8 @@ class DashboardController {
|
||||
activeTextEditorToggleLabel: this.activeTextEditorToggle?.label ?? "",
|
||||
activeTextEditorToggleValue: this.activeTextEditorToggle?.setting.get() ?? false,
|
||||
activeTextEditorToggleVisible: this.activeTextEditorToggle !== null,
|
||||
keyboardInputActive: this.keyboardInput !== null,
|
||||
keyboardInputTargets: this.keyboardInput?.targets ?? [],
|
||||
evenAppConflictMessage: this.evenAppConflictMessage,
|
||||
evenAppConflictWarningVisible: this.evenAppConflictMessage.length > 0,
|
||||
firmwareWarningMessage: this.firmwareWarningMessage,
|
||||
@@ -912,6 +967,7 @@ class DashboardController {
|
||||
screenRecordingActive: this.screenRecordingActive,
|
||||
batteryOptimizationWarningVisible: this.batteryOptimizationWarningVisible,
|
||||
fontsMissingWarningVisible: this.fontsMissingWarningVisible,
|
||||
alarmReliabilityMessage: this.alarmReliabilityMessage,
|
||||
previewMode: this.isPreviewDisplayActive(),
|
||||
};
|
||||
}
|
||||
@@ -1003,9 +1059,35 @@ class DashboardController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mirror the Timers engine's phone self-check into the warnings modal. The
|
||||
* engine re-checks on its own schedule and on every timer/alarm change;
|
||||
* a page load forces a fresh check so a just-fixed setting clears at once.
|
||||
*/
|
||||
refreshAlarmReliabilityWarning(force = false): void {
|
||||
if (force) timerEngine.refreshReliability(true);
|
||||
const message = timerEngine.phoneReliabilityMessage();
|
||||
if (message !== this.alarmReliabilityMessage) {
|
||||
this.alarmReliabilityMessage = message;
|
||||
this.emit();
|
||||
}
|
||||
}
|
||||
|
||||
openAlarmReliabilityFix(): void {
|
||||
timerEngine.openPhoneReliabilityFix();
|
||||
// The system screen is asynchronous with no result; poll briefly so the
|
||||
// warning clears once the setting is changed.
|
||||
let checksLeft = 12;
|
||||
const poll = setInterval(() => {
|
||||
this.refreshAlarmReliabilityWarning(true);
|
||||
if (!this.alarmReliabilityMessage || --checksLeft <= 0) clearInterval(poll);
|
||||
}, 5_000);
|
||||
}
|
||||
|
||||
refreshEvenAppStatus(): void {
|
||||
this.refreshBatteryOptimizationStatus();
|
||||
this.refreshEvenHubFontStatus();
|
||||
this.refreshAlarmReliabilityWarning(true);
|
||||
const state = readEvenAppNotificationState();
|
||||
const wasActive = this.evenNotificationActive;
|
||||
this.evenNotificationActive = state.evenNotificationActive;
|
||||
@@ -1041,6 +1123,39 @@ class DashboardController {
|
||||
this.previewOrRenderAfterTextSettingChange();
|
||||
}
|
||||
|
||||
/**
|
||||
* The phone's keyboard button: open the keyboard dialog on the glasses
|
||||
* (the typed twin of the mic button's wakeword). Needs a display to draw
|
||||
* on and, like ring input, does nothing while the glasses are locked.
|
||||
*/
|
||||
startKeyboardInput(): void {
|
||||
if (this.phase !== "connected" && this.phase !== "charging" && !this.isPreviewDisplayActive()) return;
|
||||
if (this.glassesLocked) {
|
||||
this.appendLog("keyboard input ignored while the glasses are locked");
|
||||
return;
|
||||
}
|
||||
shell.startKeyboardInput();
|
||||
}
|
||||
|
||||
/** The phone's typing panel changed: mirror the text on the glasses. */
|
||||
setKeyboardInputText(text: string): void {
|
||||
shell.noteUserActivity();
|
||||
this.keyboardInput?.setText(text);
|
||||
}
|
||||
|
||||
/** Send to one destination, or (no id: the IME's send key) the one highlighted on the glasses. */
|
||||
sendKeyboardInput(targetId?: string): void {
|
||||
if (targetId) {
|
||||
this.keyboardInput?.sendTo(targetId);
|
||||
} else {
|
||||
this.keyboardInput?.send();
|
||||
}
|
||||
}
|
||||
|
||||
discardKeyboardInput(): void {
|
||||
this.keyboardInput?.discard();
|
||||
}
|
||||
|
||||
setActiveTextEditorToggleValue(value: boolean): void {
|
||||
if (!this.activeTextEditorToggle || this.activeTextEditorToggle.setting.get() === value) return;
|
||||
this.activeTextEditorToggle.setting.set(value);
|
||||
@@ -1084,9 +1199,13 @@ class DashboardController {
|
||||
y: 0,
|
||||
width: G2_LENS_WIDTH,
|
||||
height: G2_LENS_HEIGHT,
|
||||
zOrder: 1,
|
||||
zOrder: SHELL_SURFACE_Z_ORDER,
|
||||
transparency: "color-key",
|
||||
});
|
||||
// A fresh (or reused) compositor starts undimmed; the shell render
|
||||
// loop re-applies the current dim from here.
|
||||
await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1);
|
||||
this.appliedUnderlayDim = 1;
|
||||
const foregroundWindowId = shell.foregroundWindow()?.windowId;
|
||||
for (const window of shell.getWindows()) {
|
||||
await this.configureWindowSurface(
|
||||
@@ -1233,6 +1352,7 @@ class DashboardController {
|
||||
// the transport comes back, so do not make lock decisions from a
|
||||
// stale pre-disconnect value in the meantime.
|
||||
this.glassesWorn = null;
|
||||
updateGlassesPresence({ worn: null });
|
||||
// The mic enable was session-scoped too: park any live capture so
|
||||
// the next session restarts it, instead of leaving a holder that
|
||||
// makes every later request think the mic is already running.
|
||||
@@ -1270,10 +1390,16 @@ class DashboardController {
|
||||
// especially while the glasses remain reachable in their case.
|
||||
const hasBatteryLevel = Number.isInteger(state.battery) && state.battery >= 0 && state.battery <= 100;
|
||||
if (hasBatteryLevel) this.lastHeadsetBattery = state.battery;
|
||||
const ringBattery = state.ringBattery;
|
||||
const hasRingBattery = typeof ringBattery === "number" && Number.isInteger(ringBattery)
|
||||
&& ringBattery >= 0 && ringBattery <= 100;
|
||||
shell.setBatteryLevels({
|
||||
headset: hasBatteryLevel ? state.battery : this.lastHeadsetBattery,
|
||||
headsetCharging: state.chargingStatus > 0,
|
||||
ring: hasRingBattery ? ringBattery : null,
|
||||
ringCharging: hasRingBattery ? state.ringChargingStatus === 1 : null,
|
||||
});
|
||||
updateGlassesPresence({ charging: state.chargingStatus > 0 || this.phase === "charging" });
|
||||
if ((this.phase === "connected" || this.phase === "charging") && this.communicator) {
|
||||
// Repaint the top bar (battery indicators live in the shell chrome).
|
||||
this.requestShellRender();
|
||||
@@ -1364,9 +1490,13 @@ class DashboardController {
|
||||
y: 0,
|
||||
width: G2_LENS_WIDTH,
|
||||
height: G2_LENS_HEIGHT,
|
||||
zOrder: 1,
|
||||
zOrder: SHELL_SURFACE_Z_ORDER,
|
||||
transparency: "color-key",
|
||||
});
|
||||
// A fresh (or reused) compositor starts undimmed; the shell render
|
||||
// loop re-applies the current dim from here.
|
||||
await communicator.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1);
|
||||
this.appliedUnderlayDim = 1;
|
||||
await this.configureLockSurface(communicator);
|
||||
const foregroundWindowId = shell.foregroundWindow()?.windowId;
|
||||
for (const window of shell.getWindows()) {
|
||||
@@ -1684,7 +1814,7 @@ class DashboardController {
|
||||
}
|
||||
const x = Math.round(Math.min(1, Math.max(0, nx)) * G2_LENS_WIDTH);
|
||||
const y = Math.round(Math.min(1, Math.max(0, ny)) * G2_LENS_HEIGHT);
|
||||
const stripShown = sidebarStripVisible(shell.getFocus());
|
||||
const stripShown = sidebarStripVisible(shell.getFocus(), shell.foregroundWindow()?.appId);
|
||||
this.appendLog(`mirror tap at ${x},${y}`);
|
||||
if (!shell.hasOverlay() && stripShown && x < SIDEBAR_WIDTH) {
|
||||
const target = shell.windowAtSidebarPoint(x, y);
|
||||
@@ -1698,7 +1828,7 @@ class DashboardController {
|
||||
}
|
||||
const window = shell.foregroundWindow();
|
||||
if (window && !shell.hasOverlay()) {
|
||||
const rect = appViewportRect(window.heightMode);
|
||||
const rect = appViewportRect(window.heightMode, window.appId);
|
||||
const inside = x >= rect.x && x < rect.x + rect.width && y >= rect.y && y < rect.y + rect.height;
|
||||
if (shell.getFocus() !== "window") {
|
||||
shell.focusWindow(window.windowId);
|
||||
@@ -1749,11 +1879,14 @@ class DashboardController {
|
||||
* push-to-talk and the Transcribe app. Android mic permission is the consent
|
||||
* gate even though the audio source is the G2 mic over BLE.
|
||||
*/
|
||||
private startVoiceCapture(endpointing = false): void {
|
||||
this.beginVoiceCapture("ptt", endpointing);
|
||||
private pttCaptureGeneration = 0;
|
||||
|
||||
private startVoiceCapture(endpointing = false): Promise<void> {
|
||||
return this.beginVoiceCapture("ptt", endpointing);
|
||||
}
|
||||
|
||||
private stopVoiceCapture(): void {
|
||||
++this.pttCaptureGeneration;
|
||||
voiceControlBridge.stopPushToTalk();
|
||||
}
|
||||
|
||||
@@ -1819,7 +1952,8 @@ class DashboardController {
|
||||
return granted;
|
||||
}
|
||||
|
||||
private beginVoiceCapture(kind: "ptt" | "continuous", endpointing = false): void {
|
||||
private async beginVoiceCapture(kind: "ptt" | "continuous", endpointing = false): Promise<void> {
|
||||
const pttGeneration = kind === "ptt" ? ++this.pttCaptureGeneration : 0;
|
||||
// Preview mode captures from the phone mic (voiceCaptureOptions with a
|
||||
// null communicator); otherwise a live glasses session must be the source.
|
||||
const previewCapture = this.isPreviewDisplayActive();
|
||||
@@ -1827,8 +1961,9 @@ class DashboardController {
|
||||
return;
|
||||
}
|
||||
const communicator = this.communicator;
|
||||
void ensureVoicePermissions()
|
||||
await ensureVoicePermissions()
|
||||
.then(() => {
|
||||
if (kind === "ptt" && pttGeneration !== this.pttCaptureGeneration) return;
|
||||
if (previewCapture) {
|
||||
if (!this.isPreviewDisplayActive()) return;
|
||||
} else if (this.phase !== "connected" || this.communicator !== communicator) {
|
||||
@@ -2156,6 +2291,13 @@ class DashboardController {
|
||||
const installed = getInstalledEvenHubAppById(appId);
|
||||
if (installed) {
|
||||
const host = ALL_APPS.find((entry) => entry.appId === "evenhub")!;
|
||||
if (!isInstalledPackagePresent(installed.packageId)) {
|
||||
// The registry knows the app but its package is gone (a settings
|
||||
// import after a reinstall): offer the store's Reinstall page.
|
||||
this.appendLog(`evenhub: package missing for ${installed.packageId}; opening store page`);
|
||||
await openEvenHubStoreForPackage(this.buildAppContext(host), installed);
|
||||
return;
|
||||
}
|
||||
await launchInstalledPackage(this.buildAppContext({ ...host, appId }), installed);
|
||||
return;
|
||||
}
|
||||
@@ -2184,7 +2326,7 @@ class DashboardController {
|
||||
): Promise<void> {
|
||||
if (!target) return;
|
||||
await target.configureSurface(surfaceId, {
|
||||
...appViewportRect(heightMode),
|
||||
...appViewportRect(heightMode, shell.getWindows().find((window) => window.surfaceId === surfaceId)?.appId),
|
||||
zOrder: 0,
|
||||
transparency: "opaque",
|
||||
});
|
||||
@@ -2292,6 +2434,14 @@ class DashboardController {
|
||||
frameTimings.finishFrame(frameId, "discarded: shell render with no display target");
|
||||
return;
|
||||
}
|
||||
// A shell overlay that dims what it covers (a context menu) must dim the
|
||||
// window surfaces too, which live below the shell surface in the
|
||||
// compositor: forward the factor before this frame composites.
|
||||
const underlayDim = shell.underlayDim();
|
||||
if (underlayDim !== this.appliedUnderlayDim) {
|
||||
this.appliedUnderlayDim = underlayDim;
|
||||
await display.setUnderlayDim(SHELL_SURFACE_Z_ORDER, underlayDim);
|
||||
}
|
||||
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
|
||||
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||
@@ -2326,7 +2476,8 @@ class DashboardController {
|
||||
}
|
||||
|
||||
private async handleAndroidNotificationPosted(notificationKey: string): Promise<void> {
|
||||
if (!notificationKey) {
|
||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
|
||||
if (!notification || !shouldShowNotificationOnGlasses(notification.packageName)) {
|
||||
this.requestShellRender();
|
||||
return;
|
||||
}
|
||||
@@ -2386,6 +2537,14 @@ class DashboardController {
|
||||
private setPhase(phase: ConnectionPhase): void {
|
||||
if (this.phase === phase) return;
|
||||
this.phase = phase;
|
||||
if (phase !== "connected" && phase !== "charging") {
|
||||
shell.setBatteryLevels({ ring: null, ringCharging: null });
|
||||
}
|
||||
// "charging" is a live BLE link with the glasses in their case.
|
||||
updateGlassesPresence({
|
||||
connected: phase === "connected" || phase === "charging",
|
||||
charging: phase === "charging" || (shell.getBatteryLevels().headsetCharging ?? false),
|
||||
});
|
||||
if (phase === "disconnected") {
|
||||
// Kept across "connecting": silent mode blocks app launches, so it can
|
||||
// itself cause the reconnect churn, and Java re-reports it either way.
|
||||
@@ -2494,6 +2653,7 @@ class DashboardController {
|
||||
| "scroll-down"
|
||||
| "long-press"
|
||||
| "long-press-release"
|
||||
| "short-then-long-press"
|
||||
| "wakeword"
|
||||
| "swipe-left"
|
||||
| "swipe-right"
|
||||
@@ -2553,6 +2713,17 @@ class DashboardController {
|
||||
systemExitReasonCode: 0,
|
||||
frameId,
|
||||
};
|
||||
case "short-then-long-press":
|
||||
// Discrete, unlike a hold: the firmware's later generic release is a
|
||||
// hardware artifact the shell ignores, so synthetic sources send none.
|
||||
return {
|
||||
kind: "sys-event",
|
||||
containerName: "",
|
||||
eventType: OsEventTypeList.SHORT_THEN_LONG_PRESS_EVENT,
|
||||
eventSource,
|
||||
systemExitReasonCode: 0,
|
||||
frameId,
|
||||
};
|
||||
case "click":
|
||||
return {
|
||||
kind: "sys-event",
|
||||
|
||||
@@ -69,6 +69,8 @@ export const OsEventTypeList = {
|
||||
IMU_DATA_REPORT: 8,
|
||||
RING_LONG_PRESS_EVENT: 9,
|
||||
RING_LONG_PRESS_RELEASE_EVENT: 10,
|
||||
/** CFW extension: the 2.2.9 tap-then-hold Menu gesture. */
|
||||
SHORT_THEN_LONG_PRESS_EVENT: 11,
|
||||
} as const;
|
||||
|
||||
export const OsEventTypeName: Record<number, string> = {
|
||||
@@ -83,4 +85,5 @@ export const OsEventTypeName: Record<number, string> = {
|
||||
8: "IMU_DATA_REPORT",
|
||||
9: "RING_LONG_PRESS_EVENT",
|
||||
10: "RING_LONG_PRESS_RELEASE_EVENT",
|
||||
11: "SHORT_THEN_LONG_PRESS_EVENT",
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Builds the Faceclaw custom firmware on-device: downloads the stock Even
|
||||
* Realities G2 2.2.6.10 image from Even's CDN, verifies its SHA-256, applies
|
||||
* Realities G2 image the patch set was built against (CFW_PATCH_SET.base,
|
||||
* currently 2.2.9.22) from Even's CDN, verifies its SHA-256, applies
|
||||
* the committed byte-patch set (cfw-patches.ts), extracts the stock EvenHub
|
||||
* fonts for phone-side rendering, verifies the patched SHA-256, and writes the
|
||||
* result to app storage.
|
||||
@@ -24,9 +25,15 @@ import { EvenHubFont } from "../graphics/evenhub-font";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
const FIRMWARE_URL = "https://cdn.evenreal.co/firmware/e28738432d7b612d625331b00383149b.bin";
|
||||
const CFW_OUTPUT_FILENAME = "g2_2.2.6.10_cfw.bin";
|
||||
const STOCK_OUTPUT_FILENAME = "g2_2.2.6.10.bin";
|
||||
// The CDN names firmware files by MD5. This must be the image whose SHA-256 is
|
||||
// CFW_PATCH_SET.baseSha256 — keep it in sync with FW_URL in g2flash/build_cfw.sh
|
||||
// whenever cfw-patches.ts is regenerated against a new stock base.
|
||||
// (2.2.9.22 = fc250b05…; the older 2.2.6.10 base was e2873843….)
|
||||
const FIRMWARE_URL = "https://cdn.evenreal.co/firmware/fc250b05e98a9ff998b4b68f5f99f994.bin";
|
||||
// Output names follow the patch set's base name (e.g. g2_2.2.9.22.bin →
|
||||
// g2_2.2.9.22_cfw.bin) so a rebase can't leave stale version numbers here.
|
||||
const STOCK_OUTPUT_FILENAME = CFW_PATCH_SET.base;
|
||||
const CFW_OUTPUT_FILENAME = CFW_PATCH_SET.base.replace(/\.bin$/, "") + "_cfw.bin";
|
||||
|
||||
export type FirmwareProgress =
|
||||
| { phase: "downloading" }
|
||||
|
||||
@@ -13,8 +13,10 @@ const REQUIRED_FIRMWARE_EXTENSIONS = ["img640", "fbguard", "wearnotify"] as cons
|
||||
// The stock firmware release Faceclaw's custom image is built from. Stock at or
|
||||
// below this can be flashed with our patched image; a newer stock version is
|
||||
// unrecognized (its layout may differ from what our patch set targets).
|
||||
export const FLASHABLE_STOCK_VERSION = [2, 2, 9, 22];
|
||||
export const FLASHABLE_STOCK_VERSION_TEXT = FLASHABLE_STOCK_VERSION.join(".");
|
||||
export const BASE_STOCK_VERSION = [2, 2, 9, 22];
|
||||
export const BASE_STOCK_VERSION_TEXT = BASE_STOCK_VERSION.join(".");
|
||||
export const VALIDATED_STOCK_VERSION = [2, 2, 10, 10];
|
||||
export const VALIDATED_STOCK_VERSION_TEXT = VALIDATED_STOCK_VERSION.join(".");
|
||||
|
||||
function parseDottedVersion(version: string): number[] {
|
||||
return version
|
||||
@@ -89,6 +91,15 @@ export function hasMicControl(info: FirmwareInfo): boolean {
|
||||
return info.capabilities.trim().split(/\s+/).includes("micctl");
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the firmware advertises the optional ambient-light extension:
|
||||
* image-handler mode 16 (query / passive polling of the OPT3001 light sensor)
|
||||
* with field-105 reports, used to drive a phone-side brightness policy.
|
||||
*/
|
||||
export function hasAmbientLightSensor(info: FirmwareInfo): boolean {
|
||||
return info.capabilities.trim().split(/\s+/).includes("als16");
|
||||
}
|
||||
|
||||
/** The higher of the two arms' reported versions, or "" if none reported. */
|
||||
export function reportedFirmwareVersion(info: FirmwareInfo): string {
|
||||
const versions = [info.leftVersion, info.rightVersion].map((v) => v.trim()).filter(Boolean);
|
||||
@@ -102,10 +113,12 @@ export function reportedFirmwareVersion(info: FirmwareInfo): string {
|
||||
* How the pre-flash firmware check should treat the connected glasses:
|
||||
* - "custom": Faceclaw's firmware is already installed — nothing to flash.
|
||||
* - "flashable-stock": stock firmware at or below the version we build from.
|
||||
* - "newer-stock": stock firmware newer than we recognize — flash only on override.
|
||||
* - "newer-stock-validated": stock firmware newer than the version we build
|
||||
* from, on which the downgrade has been tested successfully
|
||||
* - "newer-stock-unvalidated": stock firmware newer than we recognize — flash only on override.
|
||||
* - "unknown": no version could be read (treated as a probe/connection failure).
|
||||
*/
|
||||
export type OnboardingFirmwareKind = "custom" | "flashable-stock" | "newer-stock" | "unknown";
|
||||
export type OnboardingFirmwareKind = "custom" | "flashable-stock" | "newer-stock-validated" | "newer-stock-unvalidated" | "unknown";
|
||||
|
||||
export function classifyOnboardingFirmware(info: FirmwareInfo): {
|
||||
kind: OnboardingFirmwareKind;
|
||||
@@ -118,6 +131,16 @@ export function classifyOnboardingFirmware(info: FirmwareInfo): {
|
||||
if (!version) {
|
||||
return { kind: "unknown", version: "" };
|
||||
}
|
||||
const comparison = compareVersions(parseDottedVersion(version), FLASHABLE_STOCK_VERSION);
|
||||
return { kind: comparison <= 0 ? "flashable-stock" : "newer-stock", version };
|
||||
if (compareVersions(parseDottedVersion(version), BASE_STOCK_VERSION) <= 0) {
|
||||
return { kind: "flashable-stock", version };
|
||||
} else if (compareVersions(parseDottedVersion(version), VALIDATED_STOCK_VERSION) <= 0) {
|
||||
return { kind: "newer-stock-validated", version };
|
||||
} else {
|
||||
return { kind: "newer-stock-unvalidated", version };
|
||||
}
|
||||
}
|
||||
|
||||
/** Cached R1 battery reports in settings field 106 and query mode 17. */
|
||||
export function hasRingBattery(info: FirmwareInfo): boolean {
|
||||
return info.capabilities.trim().split(/\s+/).includes("ringbat17");
|
||||
}
|
||||
@@ -21,10 +21,16 @@ type FontSpec = {
|
||||
};
|
||||
|
||||
// Priority order is the stock EvenHub fallback chain (before the CJK font).
|
||||
// These are lv_font_fmt_txt_dsc_t addresses in the stock image the patch set
|
||||
// is built against (CFW_PATCH_SET.base), so they move on every firmware
|
||||
// rebase: run scripts/find-firmware-fonts.py against the new stock .bin and
|
||||
// match rows by cmap count / code-point count (12/8081, 9/9566, 4/11590).
|
||||
// 2.2.9.22: 0x007a8954, 0x007a8968, 0x007a8878
|
||||
// 2.2.6.10: 0x0077f414, 0x0077f428, 0x0077f374
|
||||
const FONT_SPECS: FontSpec[] = [
|
||||
{ descriptor: 0x0077f414, lineHeight: 27, baseLine: 5 },
|
||||
{ descriptor: 0x0077f428, lineHeight: 26, baseLine: 5 },
|
||||
{ descriptor: 0x0077f374, lineHeight: 20, baseLine: 2 },
|
||||
{ descriptor: 0x007a8954, lineHeight: 27, baseLine: 5 },
|
||||
{ descriptor: 0x007a8968, lineHeight: 26, baseLine: 5 },
|
||||
{ descriptor: 0x007a8878, lineHeight: 20, baseLine: 2 },
|
||||
];
|
||||
|
||||
type ExtractedGlyph = {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// AUTO-GENERATED from g2flash/patches/cfw_patches.json — do not edit by hand.
|
||||
// Regenerate with scripts/update_deltas.sh.
|
||||
// Turns the stock G2 2.2.6.10 image into the Faceclaw custom firmware.
|
||||
// Turns the stock G2 2.2.9.22 image into the Faceclaw custom firmware.
|
||||
|
||||
export type FirmwarePatchOp = {
|
||||
offset: number;
|
||||
@@ -17,164 +17,176 @@ export type FirmwarePatchSet = {
|
||||
};
|
||||
|
||||
export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
"base": "g2_2.2.6.10.bin",
|
||||
"baseSha256": "f4dfb0b49ad3de3c2daf17f8a27a157c3dc98411d6a0d3ab2cfd0918f41b9afa",
|
||||
"outputSha256": "d4054ab1519c7184fcc4450104de18f9ff714180c80b3a579f91106536a274cd",
|
||||
"base": "g2_2.2.9.22.bin",
|
||||
"baseSha256": "a03fbea9f68a9de6bc271daabb9f3a41c59053d1086622c76a4e990f829cc561",
|
||||
"outputSha256": "12e1669fe091a94016ecc31b3940eaffb9ac1fbcb1e7807003c1f68e7f85ead3",
|
||||
"patches": [
|
||||
{
|
||||
"offset": 1089903,
|
||||
"offset": 1124178,
|
||||
"old": "5ff43432",
|
||||
"new": "5ff43332",
|
||||
"desc": "reserve final 1 KiB of primary TLSF arena for CFW context anchor"
|
||||
},
|
||||
{
|
||||
"offset": 1449549,
|
||||
"offset": 1524180,
|
||||
"old": "bdf82c10",
|
||||
"new": "40f24120",
|
||||
"desc": "container width <= 576"
|
||||
},
|
||||
{
|
||||
"offset": 1449749,
|
||||
"offset": 1524380,
|
||||
"old": "bdf82e00",
|
||||
"new": "40f22111",
|
||||
"desc": "container height movw #0x121"
|
||||
},
|
||||
{
|
||||
"offset": 1449753,
|
||||
"offset": 1524384,
|
||||
"old": "9128",
|
||||
"new": "8842",
|
||||
"desc": "container height cmp r0,r1"
|
||||
},
|
||||
{
|
||||
"offset": 1447919,
|
||||
"old": "7ef7fefd",
|
||||
"new": "bcf23afb",
|
||||
"desc": "bl snapshot_side @ 0x4db968"
|
||||
"offset": 1516784,
|
||||
"old": "70f775ff",
|
||||
"new": "d6f2fffa",
|
||||
"desc": "bl snapshot_side @ 0x4ec0ee (shared image-complete helper)"
|
||||
},
|
||||
{
|
||||
"offset": 1448931,
|
||||
"old": "7ef704fc",
|
||||
"new": "bcf240f9",
|
||||
"desc": "bl snapshot_side @ 0x4dbd5c"
|
||||
"offset": 1516816,
|
||||
"old": "30d0",
|
||||
"new": "1cd0",
|
||||
"desc": "image-complete success -> immediate ACK (pipelining-safe)"
|
||||
},
|
||||
{
|
||||
"offset": 1165461,
|
||||
"old": "45f0cefd",
|
||||
"new": "01f3ebfa",
|
||||
"offset": 1222660,
|
||||
"old": "49f0daff",
|
||||
"new": "1ef379f9",
|
||||
"desc": "bl image_deferred (deferred consumer -> FIFO restore + worker, both lenses)"
|
||||
},
|
||||
{
|
||||
"offset": 1186287,
|
||||
"old": "d9f7d4ff",
|
||||
"new": "fdf21afa",
|
||||
"offset": 1242342,
|
||||
"old": "d4f790fb",
|
||||
"new": "1af320fd",
|
||||
"desc": "bl settings_send_wrapper (append caps field 100)"
|
||||
},
|
||||
{
|
||||
"offset": 1183983,
|
||||
"old": "f4f75aff",
|
||||
"new": "fdf2bafb",
|
||||
"offset": 1240038,
|
||||
"old": "f5f710f9",
|
||||
"new": "1af3b6fe",
|
||||
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
|
||||
},
|
||||
{
|
||||
"offset": 926945,
|
||||
"old": "08f068fa",
|
||||
"new": "3cf3ddf8",
|
||||
"desc": "bl faceclaw_display_start @ 0x45c65a (fail-open double-tap takeover)"
|
||||
"offset": 939336,
|
||||
"old": "0bf02af9",
|
||||
"new": "64f321f9",
|
||||
"desc": "bl faceclaw_display_start @ 0x45f146 (fail-open double-tap takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 927137,
|
||||
"old": "08f008fa",
|
||||
"new": "3cf37df8",
|
||||
"desc": "bl faceclaw_display_start @ 0x45c71a (fail-open double-tap takeover)"
|
||||
"offset": 939528,
|
||||
"old": "0bf0caf8",
|
||||
"new": "64f3c1f8",
|
||||
"desc": "bl faceclaw_display_start @ 0x45f206 (fail-open double-tap takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 822553,
|
||||
"old": "28f003f8",
|
||||
"new": "56f3e5fd",
|
||||
"desc": "bl evenhub_longpress (Faceclaw lease gates custom event vs stock dialog)"
|
||||
"offset": 831036,
|
||||
"old": "1df057fc",
|
||||
"new": "80f33df9",
|
||||
"desc": "bl gesture_press (Faceclaw lease gates event 9 vs stock UI event 8)"
|
||||
},
|
||||
{
|
||||
"offset": 823369,
|
||||
"old": "1cf09bfb",
|
||||
"new": "56f371fc",
|
||||
"desc": "bl ring_release (forward source-qualified release-long-press)"
|
||||
"offset": 831042,
|
||||
"old": "24f023f9",
|
||||
"new": "80f3def9",
|
||||
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
|
||||
},
|
||||
{
|
||||
"offset": 1474137,
|
||||
"offset": 831800,
|
||||
"old": "1df0d9fa",
|
||||
"new": "7ff3c3ff",
|
||||
"desc": "bl gesture_release (source-qualified event 10 vs stock UI event 0x4a)"
|
||||
},
|
||||
{
|
||||
"offset": 1553752,
|
||||
"old": "7fb50600",
|
||||
"new": "b6f2d3bf",
|
||||
"new": "cef2d5bc",
|
||||
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
|
||||
},
|
||||
{
|
||||
"offset": 1022741,
|
||||
"old": "f8f7c1fe",
|
||||
"new": "20f30ffc",
|
||||
"desc": "bl display_copy_hook @ 0x473c8e (640x480 direct framebuffer)"
|
||||
"offset": 1047796,
|
||||
"old": "f6f7edff",
|
||||
"new": "45f33df9",
|
||||
"desc": "bl display_copy_hook @ 0x4798f2 (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1022959,
|
||||
"old": "f8f754fe",
|
||||
"new": "20f3a2fb",
|
||||
"desc": "bl display_copy_hook @ 0x473d68 (640x480 direct framebuffer)"
|
||||
"offset": 1048112,
|
||||
"old": "f6f74fff",
|
||||
"new": "45f39ff8",
|
||||
"desc": "bl display_copy_hook @ 0x479a2e (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1198787,
|
||||
"old": "dff770fb",
|
||||
"new": "f9f2f4fc",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x49ec3c (outside onboarding)"
|
||||
"offset": 1255404,
|
||||
"old": "d9f758ff",
|
||||
"new": "16f3cdfe",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4ac3ea (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 1198881,
|
||||
"old": "dff741fb",
|
||||
"new": "f9f2c5fc",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x49ec9a (outside onboarding)"
|
||||
"offset": 1255504,
|
||||
"old": "d9f726ff",
|
||||
"new": "16f39bfe",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4ac44e (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 823567,
|
||||
"old": "1cf038fb",
|
||||
"new": "51f3d2f8",
|
||||
"desc": "bl compass_event_forward (global IMU heading -> stock nav BLE notifier)"
|
||||
"offset": 1297700,
|
||||
"old": "65f0affd",
|
||||
"new": "0cf3a1f9",
|
||||
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
|
||||
},
|
||||
{
|
||||
"offset": 4301227,
|
||||
"offset": 1296176,
|
||||
"old": "fff759fc",
|
||||
"new": "0cf315fd",
|
||||
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
|
||||
},
|
||||
{
|
||||
"offset": 4476518,
|
||||
"old": "",
|
||||
"new": "02fb01f000e000bfb0b546f27424c2f22a040146206846f23f52c5f28802b2eb500f40f2543044f281124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f2543044f29f22c2f20000c0f24802104770b500284ed001684ef66862ccf2ff02914218bf70bd90f8081190f8095142f6c52c8d42c0f2500c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b149f29942c0f244022146bde87040104770bd00bf70b54ff6fd03c0f2450314460d46984746f27021c2f22a010e6831466ff39601b1f1005f0ad131684ef66862ccf2ff02914218bf0026412d03d070bd0026412dfbd1002cf9d04af26951c0f2450105468847002e01462846efd00129edd196f807120029e9d02168002948bf70bd47f25d02c0f2580208469047284670bd00bf2de9f04f9db046f2702ac2f22a0adaf800b058466ff39600b0f1005f40f07d81dbf800004ef66867ccf2ff07b84240f074819bf82802002800f05d81dbf82442002c00f0588140f2b870c2f20000d0f8008004a802f0caffb8f1000f00f01982032045f24075cff68070a4f1600108f160026ff0bf03c0f2020500bf0e6ed41826664e6ec03366668e6eab42a666ce6ee6660e6f26674e6f66678e6fa667ce6fe667d1f88060c4f88060d1f88460c4f88460d1f88860c4f88860d1f88c60c4f88c60d1f89060c4f89060d1f89460c4f89460d1f89860c4f89860d1f89c60c4f89c60d1f8a060c4f8a060d1f8a460c4f8a460d1f8a860c4f8a860d1f8ac60c4f8ac60d1f8b060c4f8b060d1f8b460c4f8b460d1f8b860c4f8b860d1f8bc60c4f8bc6051f8c06fc4f8c0604e68c4f8c4608e68c4f8c860ce68c4f8cc600e69c4f8d0604e69c4f8d4608e69c4f8d860ce69c4f8dc600e6ac4f8e0604e6ac4f8e4608e6ac4f8e860ce6ac4f8ec600e6bc4f8f0604e6bc4f8f4608e6bc4f8f860ce6bc4f8fc600e6cc4f800614e6cc4f804618e6cc4f80861ce6cc4f80c610e6dc4f810614e6dc4f814618e6dc4f81861ce6dc4f81c617ff462afdaf800603040b0f1005f40f0b2803068b84240f0ae8096f8ce00002840f0708196f8dc10002000298df8140000f0e88105a90a5c002a00f0bb810a185378002b00f079819378002b00f07781d378002b00f075811379002b00f073815379002b00f071819379002b00f06f81d379002b00f06d81137a002b00f06b81537a002b00f06981937a002b00f06781d37a002b00f06581137b002b00f06381537b002b00f06181937b002b00f05f81d37b002b00f05d81137c002b00f05b81537c002b00f05981937c002b00f05781d37c002b00f05581137d002b00f05381537d002b00f05181937d002b00f04f81d37d002b00f04d81137e002b00f04b81537e002b00f04981937e002b00f04781d37e002b00f04581137f002b00f04381537f002b00f04181937f002b00f03f81d27f002a00f03d81203060287ff47baf5ce19bf82a0270b1dbf82c0240b144f63421c2f207010968401a002800f3ea8000208bf82a024cf61520c0f246001db0bde8f04f00474ff41b7004f063fe002800f0bd804ff69451cff6ff71002200bf43183e3183f86c2283f86d2283f86e2283f86f2283f8702283f8712283f8722283f8732283f8742283f8752283f8762283f8772283f8782283f8792283f87a2283f87b2283f87c2283f87d2283f87e2283f87f2283f8802283f8812283f8822283f8832283f8842283f8852283f8862283f8872283f8882283f8892283f88a2283f88b2283f88c2283f88d2283f88e2283f88f2283f8902283f8912283f8922283f8932283f8942283f8952283f8962283f8972283f8982283f8992283f89a2283f89b2283f89c2283f89d2283f89e2283f89f2283f8a02283f8a12283f8a22283f8a32283f8a42283f8a52283f8a62283f8a72283f8a82283f8a9227ff480af012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e000208bf82802cbf824028bf82a0201208bf829024cf61520c0f24600804715e00020caf8000000208bf82802cbf824024ff4163045f20f11069005a8c0f24701cdf81480884701208bf82a0204a802f0aafecbf8d4001db0bde8f08f01303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f2821d84ff052020a541dd0014445225e284a7018d04f225d288a7014d052225c28ca7010d044225b280a710cd045225a284a7108d0522259288a7104d020225828ca7142f0168500208df87300012096f8dd10002900f0c280002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf54e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2915d84ff05303535411d00a444b235e2953700cd049235d29937008d050235c29d37004d020235b29137140f0c28200218df87310013096f8de10002900f0be80002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e004313Line truncated
|
||||
"new": "02fb01f000e000bfb0b54ff2ac44c2f229040146206846f23f52c5f28802b2eb500f40f258304cf2e9124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f258304cf20732c2f20000c0f24802104770b500284ed001684ef66a62ccf2ff02914218bf70bd90f8081190f8095146f6756c8d42c0f2510c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b142f64d42c0f244022146bde87040104770bd00bf2de9f04f9db04ff2a84ac2f2290adaf800b058466ff39600b0f1005f40f07d81dbf800004ef66a67ccf2ff07b84240f074819bf82802002800f05d81dbf82442002c00f0588140f6b400c2f20000d0f8008004a802f0d4ffb8f1000f00f02382032045f24075cff68070a4f1600108f160026ff0bf03c0f2020500bf0e6ed41826664e6ec03366668e6eab42a666ce6ee6660e6f26674e6f66678e6fa667ce6fe667d1f88060c4f88060d1f88460c4f88460d1f88860c4f88860d1f88c60c4f88c60d1f89060c4f89060d1f89460c4f89460d1f89860c4f89860d1f89c60c4f89c60d1f8a060c4f8a060d1f8a460c4f8a460d1f8a860c4f8a860d1f8ac60c4f8ac60d1f8b060c4f8b060d1f8b460c4f8b460d1f8b860c4f8b860d1f8bc60c4f8bc6051f8c06fc4f8c0604e68c4f8c4608e68c4f8c860ce68c4f8cc600e69c4f8d0604e69c4f8d4608e69c4f8d860ce69c4f8dc600e6ac4f8e0604e6ac4f8e4608e6ac4f8e860ce6ac4f8ec600e6bc4f8f0604e6bc4f8f4608e6bc4f8f860ce6bc4f8fc600e6cc4f800614e6cc4f804618e6cc4f80861ce6cc4f80c610e6dc4f810614e6dc4f814618e6dc4f81861ce6dc4f81c617ff462afdaf800603040b0f1005f40f0b2803068b84240f0ae8096f8ce00002840f07a8196f8dc10002000298df8140000f0f28105a90a5c002a00f0c5810a185378002b00f083819378002b00f08181d378002b00f07f811379002b00f07d815379002b00f07b819379002b00f07981d379002b00f07781137a002b00f07581537a002b00f07381937a002b00f07181d37a002b00f06f81137b002b00f06d81537b002b00f06b81937b002b00f06981d37b002b00f06781137c002b00f06581537c002b00f06381937c002b00f06181d37c002b00f05f81137d002b00f05d81537d002b00f05b81937d002b00f05981d37d002b00f05781137e002b00f05581537e002b00f05381937e002b00f05181d37e002b00f04f81137f002b00f04d81537f002b00f04b81937f002b00f04981d27f002a00f04781203060287ff47baf66e19bf82a0270b1dbf82c0240b146f68051c2f207010968401a002800f3f48000208bf82a0240f6d100c0f247001db0bde8f04f00474ff4497005f0e5f8002800f0c7804ff6dc41cff6ff71002200bf4318433183f8242383f8252383f8262383f8272383f8282383f8292383f82a2383f82b2383f82c2383f82d2383f82e2383f82f2383f8302383f8312383f8322383f8332383f8342383f8352383f8362383f8372383f8382383f8392383f83a2383f83b2383f83c2383f83d2383f83e2383f83f2383f8402383f8412383f8422383f8432383f8442383f8452383f8462383f8472383f8482383f8492383f84a2383f84b2383f84c2383f84d2383f84e2383f84f2383f8502383f8512383f8522383f8532383f8542383f8552383f8562383f8572383f8582383f8592383f85a2383f85b2383f85c2383f85d2383f85e2383f85f2383f8602383f8612383f8622383f8632383f8642383f8652383f866237ff476af012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e000208bf82802cbf824028bf82a0201208bf8290240f6d100c0f24700804715e00020caf8000000208bf82802cbf824024ff416304cf60361069005a8c0f24701cdf81480884701208bf82a0204a802f0b4fecbf8d4001db0bde8f08f01303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f2821d84ff052020a541dd0014445225e284a7018d04f225d288a7014d052225c28ca7010d044225b280a710cd045225a284a7108d0522259288a7104d020225828ca7142f0168500208df87300012096f8dd10002900f0c280002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf54e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2915d84ff05303535411d00a444b235e2953700cd049235d29937008d050235c29d37004d020235b29137140f0c28200218df87310013096f8de10002900f0be80002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315Line truncated
|
||||
"desc": "append injected blobs to main-app payload"
|
||||
},
|
||||
{
|
||||
"offset": 777711,
|
||||
"old": "44c33500",
|
||||
"new": "4a5c3600",
|
||||
"offset": 779114,
|
||||
"old": "846a3800",
|
||||
"new": "86113900",
|
||||
"desc": "main-app subheader payload size (ps)"
|
||||
},
|
||||
{
|
||||
"offset": 152,
|
||||
"old": "c4c33500",
|
||||
"new": "ca5c3600",
|
||||
"old": "046b3800",
|
||||
"new": "06123900",
|
||||
"desc": "main-app TOC entry size (ps + 128)"
|
||||
},
|
||||
{
|
||||
"offset": 777831,
|
||||
"old": "44c33504",
|
||||
"new": "4a5c3604",
|
||||
"offset": 779234,
|
||||
"old": "846a3804",
|
||||
"new": "86113904",
|
||||
"desc": "main-app preamble length (low 24 bits)"
|
||||
},
|
||||
{
|
||||
"offset": 777835,
|
||||
"old": "df8110c9",
|
||||
"new": "0aa0bcf3",
|
||||
"offset": 779238,
|
||||
"old": "85c355a2",
|
||||
"new": "1a7d445b",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
|
||||
},
|
||||
{
|
||||
"offset": 156,
|
||||
"old": "c1be0fb1",
|
||||
"new": "01fcf86d",
|
||||
"old": "ed292ee4",
|
||||
"new": "a7b82e22",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
|
||||
},
|
||||
{
|
||||
"offset": 777715,
|
||||
"old": "c1be0fb1",
|
||||
"new": "01fcf86d",
|
||||
"offset": 779118,
|
||||
"old": "ed292ee4",
|
||||
"new": "a7b82e22",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Whether the glasses could carry an alert right now: connected over BLE, on
|
||||
* a head, and not charging. Fed by the dashboard controller from the
|
||||
* transport's phase, wear-state and battery reports; consumed by the Timers
|
||||
* engine, which mirrors it to the phone's alarm clock so the phone knows
|
||||
* when to add its own sound.
|
||||
*/
|
||||
|
||||
export type GlassesPresence = {
|
||||
connected: boolean;
|
||||
/** null until the firmware has reported a wear state this session. */
|
||||
worn: boolean | null;
|
||||
charging: boolean;
|
||||
};
|
||||
|
||||
let presence: GlassesPresence = { connected: false, worn: null, charging: false };
|
||||
const listeners = new Set<(presence: GlassesPresence) => void>();
|
||||
|
||||
export function getGlassesPresence(): GlassesPresence {
|
||||
return presence;
|
||||
}
|
||||
|
||||
/** True when a fresh, positive report says the glasses are worn and usable. */
|
||||
export function glassesCanCarryAlert(current: GlassesPresence = presence): boolean {
|
||||
return current.connected && current.worn === true && !current.charging;
|
||||
}
|
||||
|
||||
export function updateGlassesPresence(patch: Partial<GlassesPresence>): void {
|
||||
const next = { ...presence, ...patch };
|
||||
if (next.connected === presence.connected && next.worn === presence.worn && next.charging === presence.charging) return;
|
||||
presence = next;
|
||||
for (const listener of Array.from(listeners)) {
|
||||
try {
|
||||
listener(presence);
|
||||
} catch (error) {
|
||||
console.warn(`glasses presence listener failed: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function onGlassesPresenceChanged(listener: (presence: GlassesPresence) => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
@@ -44,6 +44,7 @@ export type WearRemoteInputKind =
|
||||
| "long-press"
|
||||
| "long-press-start"
|
||||
| "long-press-release"
|
||||
| "short-then-long-press"
|
||||
| "wakeword"
|
||||
| "swipe-left"
|
||||
| "swipe-right"
|
||||
@@ -58,6 +59,7 @@ const INPUT_KINDS: readonly WearRemoteInputKind[] = [
|
||||
"long-press",
|
||||
"long-press-start",
|
||||
"long-press-release",
|
||||
"short-then-long-press",
|
||||
"wakeword",
|
||||
"swipe-left",
|
||||
"swipe-right",
|
||||
|
||||
Loaded 100 of 198 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user