Add calendar app and accelerometer demo

This commit is contained in:
James Babcock committed 2026-07-11 14:15:10 -07:00
1 parent 3cdc5ba262
commit ff447d5161
18 files changed
+911 -5

No files matched your search

@@ -47,6 +47,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private final String ringAddress;
private volatile FaceclawBleCommunicatorListener listener;
private final java.util.List<FaceclawImuListener> imuListeners =
new java.util.concurrent.CopyOnWriteArrayList<>();
private volatile Thread workerThread;
private volatile boolean running;
private volatile boolean userDisconnectRequested;
@@ -253,6 +255,39 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
}
/**
* Enable or disable the IMU (accelerometer) report stream. Fire-and-forget:
* the control message is queued ahead of other traffic; readings arrive via
* registered FaceclawImuListeners. reportFrq is the requested sample rate
* (ignored on disable).
*/
public void setImuReportEnabled(boolean enable, int reportFrq) {
synchronized (lock) {
if (!running || !sessionReady) {
logLine("skip IMU " + (enable ? "enable" : "disable") + "; session not ready");
return;
}
clearMessagesOfKindLocked("imu-control");
OutboundMessage message = messageBuilder.enableOrDisableImu(enable, reportFrq);
message.onTimeout = () -> logLine("IMU control ack timeout");
pendingMessages.addFirst(message);
logLine("queue IMU " + (enable ? "enable freq=" + reportFrq : "disable"));
}
interruptibleSleep.interrupt();
}
public void addImuListener(FaceclawImuListener listener) {
if (listener != null) {
imuListeners.add(listener);
}
}
public void removeImuListener(FaceclawImuListener listener) {
if (listener != null) {
imuListeners.remove(listener);
}
}
// The most recently started communicator; lets app worker threads submit
// surface frames without holding a cross-isolate reference to the bridge
@@ -579,7 +614,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
if (frame.ok && address.equalsIgnoreCase(rightAddress) && (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT)) {
event = G2Event.decode(frame);
if (event != null) {
lastConnectionOrInputAtMs = lastIncomingAtMs;
// Pure IMU samples arrive continuously; don't let them count
// as user input (which would starve battery polling).
boolean pureImuSample = "sys-event".equals(event.kind)
&& event.eventType == BleProtocol.EVENT_IMU_DATA_REPORT;
if (!pureImuSample) {
lastConnectionOrInputAtMs = lastIncomingAtMs;
}
if ("sys-event".equals(event.kind)) {
if (event.eventType == BleProtocol.EVENT_FOREGROUND_EXIT || event.eventType == BleProtocol.EVENT_ABNORMAL_EXIT || event.eventType == BleProtocol.EVENT_SYSTEM_EXIT) {
if (shutdownRequested) {
@@ -597,10 +638,20 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
interruptibleSleep.interrupt();
if (event != null) {
int frameId = FrameTimings.getInstance().startFrame(
"input:" + event.kind + " type=" + event.eventType + " src=" + event.eventSource);
FrameTimings.getInstance().log(frameId, "input event decoded from BLE notification");
emitRingEvent(event.kind, event.containerName, event.eventType, event.eventSource, event.systemExitReasonCode, frameId);
if (event.hasImu) {
emitImuData(event.imuX, event.imuY, event.imuZ, event.eventSource);
}
// A standalone IMU_DATA_REPORT is a sensor sample, not a gesture:
// deliver it only to IMU listeners, skipping the input pipeline (and
// its per-frame latency bookkeeping) to avoid flooding it.
boolean pureImuSample = "sys-event".equals(event.kind)
&& event.eventType == BleProtocol.EVENT_IMU_DATA_REPORT;
if (!pureImuSample) {
int frameId = FrameTimings.getInstance().startFrame(
"input:" + event.kind + " type=" + event.eventType + " src=" + event.eventSource);
FrameTimings.getInstance().log(frameId, "input event decoded from BLE notification");
emitRingEvent(event.kind, event.containerName, event.eventType, event.eventSource, event.systemExitReasonCode, frameId);
}
}
}
@@ -1833,6 +1884,21 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
});
}
private void emitImuData(double x, double y, double z, int eventSource) {
if (imuListeners.isEmpty()) {
return;
}
mainHandler.post(() -> {
for (FaceclawImuListener imuListener : imuListeners) {
try {
imuListener.onImuData(x, y, z, eventSource);
} catch (Throwable t) {
Log.w(TAG, "listener onImuData failed", t);
}
}
});
}
private void emitBatteryState(int headsetBattery, int headsetCharging) {
final FaceclawBleCommunicatorListener current = listener;
if (current == null) {
@@ -0,0 +1,98 @@
package com.faceclaw.app;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Reads upcoming events from the Android Calendar provider for the Calendar
* app. Queries the Instances table (rather than Events) so that recurring
* events are expanded into concrete occurrences within the requested window.
* Requires the READ_CALENDAR runtime permission; without it the content
* resolver throws SecurityException and this returns an empty array.
*/
public final class FaceclawCalendarProvider {
private static final String TAG = "FaceclawCalendar";
private static final String[] PROJECTION = {
CalendarContract.Instances.EVENT_ID,
CalendarContract.Instances.TITLE,
CalendarContract.Instances.BEGIN,
CalendarContract.Instances.END,
CalendarContract.Instances.ALL_DAY,
CalendarContract.Instances.EVENT_LOCATION,
CalendarContract.Instances.CALENDAR_DISPLAY_NAME,
};
private FaceclawCalendarProvider() {
}
/**
* JSON array of upcoming events starting from now through now+windowMs,
* ordered by start time, capped at maxEvents. Each element carries id,
* title, startMs, endMs, allDay, location, and calendarName.
*/
public static String getUpcomingEventsJson(Context context, int maxEvents, long windowMs) {
if (context == null || maxEvents <= 0) {
return "[]";
}
long now = System.currentTimeMillis();
long end = now + Math.max(0L, windowMs);
int limit = Math.min(200, maxEvents);
Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, now);
ContentUris.appendId(builder, end);
Uri uri = builder.build();
JSONArray out = new JSONArray();
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(
uri,
PROJECTION,
null,
null,
CalendarContract.Instances.BEGIN + " ASC");
if (cursor != null) {
while (cursor.moveToNext() && out.length() < limit) {
try {
out.put(buildEventJson(cursor));
} catch (JSONException e) {
Log.w(TAG, "failed to serialize calendar event", e);
}
}
}
} catch (SecurityException e) {
Log.w(TAG, "calendar access denied while reading events", e);
return "[]";
} catch (Throwable t) {
Log.w(TAG, "failed to read calendar events", t);
return "[]";
} finally {
if (cursor != null) {
cursor.close();
}
}
return out.toString();
}
private static JSONObject buildEventJson(Cursor cursor) throws JSONException {
JSONObject event = new JSONObject();
event.put("id", cursor.getLong(0));
event.put("title", cursor.isNull(1) ? "" : cursor.getString(1));
event.put("startMs", cursor.getLong(2));
event.put("endMs", cursor.getLong(3));
event.put("allDay", cursor.getInt(4) != 0);
event.put("location", cursor.isNull(5) ? "" : cursor.getString(5));
event.put("calendarName", cursor.isNull(6) ? "" : cursor.getString(6));
return event;
}
}
@@ -0,0 +1,10 @@
package com.faceclaw.app;
/**
* Receives IMU (accelerometer) readings decoded from the glasses' sys-event
* stream. Registered on FaceclawBleCommunicator via addImuListener; callbacks
* are delivered on the Android main thread.
*/
public interface FaceclawImuListener {
void onImuData(double x, double y, double z, int eventSource);
}
@@ -35,6 +35,9 @@ public class BleProtocol {
public static final int FLAG_NOTIFY = 0x01;
public static final int FLAG_NOTIFY_ALT = 0x06;
public static final int CMD_AUDIO_CONTROL = 15;
// EvenHub_Cmd_List.APP_REQUEST_OPEN_IMU_PACKET; the accelerometer stream is
// gated behind this control message (ImuCtrl, field 22 of the main ctx).
public static final int CMD_OPEN_IMU = 19;
public static final int EVENT_CLICK = 0;
public static final int EVENT_SCROLL_TOP = 1;
@@ -44,6 +47,7 @@ public class BleProtocol {
public static final int EVENT_FOREGROUND_EXIT = 5;
public static final int EVENT_ABNORMAL_EXIT = 6;
public static final int EVENT_SYSTEM_EXIT = 7;
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;
@@ -237,6 +241,20 @@ public class BleProtocol {
return wrapEvenHub(9, magic, 11, encodeVarintField(1, exitMode));
}
/**
* IMU control (ImuCtrl = field 22 of the main ctx). IMU_CtrlCmd carries
* IMUReportEn (field 1) and reportFrq (field 2). reportFrq is only
* meaningful when enabling; on disable we send just the enable flag.
*/
public static byte[] buildImuControl(int magic, boolean enable, int reportFrq) {
List<byte[]> inner = new ArrayList<>();
inner.add(encodeVarintField(1, enable ? 1 : 0));
if (enable && reportFrq > 0) {
inner.add(encodeVarintField(2, reportFrq));
}
return wrapEvenHub(CMD_OPEN_IMU, magic, 22, concat(inner));
}
public static byte[] buildSettingsQuery(int magic) {
byte[] request = encodeVarintField(1, 1);
return concat(CollectionUtils.listOf(
@@ -519,6 +537,55 @@ public class BleProtocol {
return defaultValue;
}
/**
* Read a `float` (protobuf wire type 5, little-endian fixed32) field. The
* g2-kit proto declares IMU_Report_Data's x/y/z as `double`, but the
* firmware actually sends them as fixed32 floats (confirmed from captures).
* Returns defaultValue if absent.
*/
public static float readFloatFieldValue(byte[] pb, int fieldNumber, float defaultValue) {
int offset = 0;
while (offset < pb.length) {
VarintResult key = readVarint(pb, offset);
if (key == null) {
return defaultValue;
}
offset = key.next;
int field = key.value >> 3;
int wire = key.value & 0x07;
if (wire == 0) {
VarintResult value = readVarint(pb, offset);
if (value == null) {
return defaultValue;
}
offset = value.next;
} else if (wire == 1) {
offset += 8;
} else if (wire == 5) {
if (offset + 4 > pb.length) {
return defaultValue;
}
if (field == fieldNumber) {
int bits = 0;
for (int i = 0; i < 4; i++) {
bits |= (pb[offset + i] & 0xff) << (8 * i);
}
return Float.intBitsToFloat(bits);
}
offset += 4;
} else if (wire == 2) {
VarintResult length = readVarint(pb, offset);
if (length == null) {
return defaultValue;
}
offset = length.next + length.value;
} else {
return defaultValue;
}
}
return defaultValue;
}
public static String readStringFieldValue(byte[] pb, int fieldNumber) {
byte[] bytes = readFieldBytes(pb, fieldNumber);
if (bytes == null) {
@@ -6,13 +6,28 @@ public final class G2Event {
final int eventType;
final int eventSource;
final int systemExitReasonCode;
// IMU_Report_Data (Sys_ItemEvent.IMUData), populated when the firmware
// attaches an accelerometer reading to a sys-event.
final boolean hasImu;
final double imuX;
final double imuY;
final double imuZ;
public G2Event(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode) {
this(kind, containerName, eventType, eventSource, systemExitReasonCode, false, 0, 0, 0);
}
public G2Event(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode,
boolean hasImu, double imuX, double imuY, double imuZ) {
this.kind = kind;
this.containerName = containerName;
this.eventType = eventType;
this.eventSource = eventSource;
this.systemExitReasonCode = systemExitReasonCode;
this.hasImu = hasImu;
this.imuX = imuX;
this.imuY = imuY;
this.imuZ = imuZ;
}
public static G2Event decode(BleProtocol.ParsedFrame frame) {
@@ -49,9 +64,30 @@ public final class G2Event {
int eventType = BleProtocol.readVarintFieldValue(sysEvent, 1, BleProtocol.EVENT_CLICK);
int eventSource = BleProtocol.readVarintFieldValue(sysEvent, 2, 0);
int exitReason = BleProtocol.readVarintFieldValue(sysEvent, 4, 0);
// IMUData (field 3) is IMU_Report_Data { double x=1, y=2, z=3 }. It
// may accompany any sys-event, or arrive as a standalone
// IMU_DATA_REPORT sample.
byte[] imu = BleProtocol.readFieldBytes(sysEvent, 3);
if (imu != null) {
double x = BleProtocol.readFloatFieldValue(imu, 1, 0);
double y = BleProtocol.readFloatFieldValue(imu, 2, 0);
double z = BleProtocol.readFloatFieldValue(imu, 3, 0);
return new G2Event("sys-event", "", eventType, eventSource, exitReason, true, x, y, z);
}
return new G2Event("sys-event", "", eventType, eventSource, exitReason);
}
return null;
}
private static String hex(byte[] data) {
if (data == null) {
return "null";
}
StringBuilder sb = new StringBuilder(data.length * 2);
for (byte b : data) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
}
@@ -107,6 +107,21 @@ public class MessageBuilder {
);
}
public OutboundMessage enableOrDisableImu(boolean enable, int reportFrq) {
int magic = magicPool.allocate();
return new OutboundMessage(
"imu-control",
enable ? "IMU enable" : "IMU disable",
BleProtocol.SID_EVENHUB,
BleProtocol.FLAG_REQUEST,
magic,
BleProtocol.buildImuControl(magic, enable, reportFrq),
ACK_TIMEOUT_MS,
-1,
false
);
}
public OutboundMessage createLayout(BleProtocol.ImageTileOptions... tiles) {
int magic = magicPool.allocate();
return new OutboundMessage(