Background battery permission; incremental images

This commit is contained in:
James Babcock committed 2026-07-04 19:57:41 -07:00
1 parent fab6f6689e
commit 875956e140
9 files changed
+233 -2

No files matched your search

@@ -29,6 +29,10 @@
package visibility filtering otherwise makes Icon.loadDrawable fail with
NameNotFoundException for any package we cannot see. -->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
<!-- Deep Doze ignores our G2-screen wakelock unless the user exempts the app
from battery optimization; this lets us show the system exemption dialog
instead of sending users hunting through Settings. -->
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<!-- usesCleartextTraffic: the g2mirror terminal-mirroring protocol runs
plain ws:// over tailscale (transport security is the network's job),
@@ -1246,10 +1246,23 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
FrameTimings.getInstance().spanStart(frameId, "compress-and-plan");
BleImageOptimizer.TileImagePlan plan =
new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId());
// Incremental (mode 3 bounding box) update when the previous frame is
// known-displayed; displayedBmp is cleared on connect/reset/timeout
// paths, so a non-empty value means display state is trusted.
byte[] incrementalPayload = null;
if (connectionOptions.INCREMENTAL_FRAMES && displayedBmp.length > 0) {
incrementalPayload = BleImageOptimizer.buildIncrementalImagePayload(displayedBmp, bmp);
}
BleImageOptimizer.TileImagePlan plan = incrementalPayload != null
? new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId(), incrementalPayload)
: new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId());
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan");
if (incrementalPayload != null) {
FrameTimings.getInstance().log(frameId, "incremental update bbox="
+ ((incrementalPayload[3] & 0xff) * 4) + "x" + ((incrementalPayload[4] & 0xff) * 2)
+ "+" + ((incrementalPayload[1] & 0xff) * 4) + "+" + ((incrementalPayload[2] & 0xff) * 2));
}
int updateId = nextImageUpdateId++;
int messageCount = plan.fragments.size();
@@ -85,6 +85,102 @@ public final class BleImageOptimizer {
this.payload = maybeCompress(this.bmp);
this.sessionId = sessionId;
}
/**
* Plan with an explicit wire payload (e.g. a mode-3 incremental update).
* bmp must still be the FULL new frame: it feeds the displayed-frame
* dedup cache, which after an applied incremental equals the full frame.
*/
TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] bmp, int sessionId, byte[] payloadOverride) {
this.tileIndex = tileIndex;
this.tile = tile;
this.bmp = BmpUtil.copyTileBmp(bmp);
this.payload = payloadOverride;
this.sessionId = sessionId;
}
}
/**
* Build a mode-3 bounding-box incremental payload, or null when a full
* update should be sent instead (frames not comparable, or the changed
* region spans the whole screen). Wire format:
* [3][left/4][top/2][width/4][height/2][zlib of headerless 4bpp region]
* The region is top-down rows of the NEW frame, stride width/2 bytes. The
* box is aligned so left/width are multiples of 4 pixels and top/height
* multiples of 2 rows, letting each coordinate fit one byte and avoiding
* 4bpp byte-boundary corner cases. Identical frames yield null too; the
* caller's dedup check should normally catch that first.
*/
public static byte[] buildIncrementalImagePayload(byte[] previousBmp, byte[] newBmp) {
int width = BmpUtil.readBmpWidth(newBmp);
int height = BmpUtil.readBmpHeight(newBmp);
if (width <= 0 || height <= 0
|| width != BmpUtil.readBmpWidth(previousBmp)
|| height != BmpUtil.readBmpHeight(previousBmp)) {
return null;
}
byte[] previous = BmpUtil.pack4bppFromBmp(previousBmp);
byte[] next = BmpUtil.pack4bppFromBmp(newBmp);
int stride = (width + 1) >> 1;
if (previous.length != stride * height || next.length != stride * height) {
return null;
}
// Changed bounding box in packed-byte coordinates (1 byte = 2 pixels).
int minByteX = stride;
int maxByteX = -1;
int minY = height;
int maxY = -1;
for (int y = 0; y < height; y++) {
int rowOffset = y * stride;
for (int x = 0; x < stride; x++) {
if (previous[rowOffset + x] != next[rowOffset + x]) {
if (x < minByteX) minByteX = x;
if (x > maxByteX) maxByteX = x;
if (y < minY) minY = y;
maxY = y;
// The rest of the row only matters for maxByteX; scan from
// the right end to finish this row quickly.
for (int rx = stride - 1; rx > maxByteX; rx--) {
if (previous[rowOffset + rx] != next[rowOffset + rx]) {
maxByteX = rx;
break;
}
}
break;
}
}
}
if (maxY < 0) {
return null; // identical
}
// Pixel-space box, aligned: left/width to multiples of 4, top/height to
// multiples of 2, clamped to the frame.
int left = (minByteX * 2) & ~3;
int rightExclusive = Math.min(width, (((maxByteX + 1) * 2) + 3) & ~3);
int top = minY & ~1;
int bottomExclusive = Math.min(height, (maxY + 2) & ~1);
int boxWidth = rightExclusive - left;
int boxHeight = bottomExclusive - top;
if (boxWidth >= width && boxHeight >= height) {
return null; // whole screen changed; full update is strictly better
}
int regionStride = boxWidth >> 1;
byte[] region = new byte[regionStride * boxHeight];
for (int y = 0; y < boxHeight; y++) {
System.arraycopy(next, (top + y) * stride + (left >> 1), region, y * regionStride, regionStride);
}
byte[] compressed = deflate(region);
byte[] out = new byte[5 + compressed.length];
out[0] = 3;
out[1] = (byte) (left / 4);
out[2] = (byte) (top / 2);
out[3] = (byte) (boxWidth / 4);
out[4] = (byte) (boxHeight / 2);
System.arraycopy(compressed, 0, out, 5, compressed.length);
return out;
}
/**
@@ -28,4 +28,12 @@ public class ConnectionOptions {
final boolean sendImagesToLeft = true;
final boolean skipSessionIds = true;
final int WINDOW_SIZE = 1;
// Send changed-region (mode 3 bounding box) updates instead of full frames
// when the previous frame is known to be displayed. Requires firmware with
// the experimental bbox-incremental mode. Disabled: the firmware-side
// display buffer is not always the previous frame (occasionally two frames
// back, apparently display-driver buffer swapping), so partial updates
// composite onto stale content per-lens. Re-enable once the firmware
// guarantees the compositing base.
final boolean INCREMENTAL_FRAMES = false;
}
@@ -107,6 +107,21 @@ public class BmpUtil {
putUint32Le(buf, offset, value);
}
public static int readBmpWidth(byte[] bmp) {
if (bmp == null || bmp.length < 0x36 || bmp[0] != 0x42 || bmp[1] != 0x4d) {
return 0;
}
return readUint32Le(bmp, 18);
}
public static int readBmpHeight(byte[] bmp) {
if (bmp == null || bmp.length < 0x36 || bmp[0] != 0x42 || bmp[1] != 0x4d) {
return 0;
}
int heightSigned = readInt32Le(bmp, 22);
return heightSigned < 0 ? -heightSigned : heightSigned;
}
public static int readBmpPixelOffset(byte[] bmp) {
if (bmp == null || bmp.length < 14 || bmp[0] != 0x42 || bmp[1] != 0x4d) {
return -1;
+32
View File
@@ -37,6 +37,7 @@ import {
type ConfigSettingString,
} from "../ui/dashboard-settings";
import { saveRawScreenshot } from "../native/raw-screenshot";
import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization";
type ConnectionPhase = "disconnected" | "connecting" | "connected" | "disconnecting";
@@ -53,6 +54,7 @@ export type DashboardSnapshot = {
firmwareWarningMessage: string;
firmwareWarningVisible: boolean;
rawScreenshotsEnabled: boolean;
batteryOptimizationWarningVisible: boolean;
};
type DashboardListener = (snapshot: DashboardSnapshot) => void;
@@ -128,6 +130,7 @@ class DashboardController {
private evenNotificationActive = false;
private evenAppConflictMessage = "";
private firmwareWarningMessage = "";
private batteryOptimizationWarningVisible = false;
private displayPreview: ImageSource | null = createInitialDisplayPreview();
private readonly listeners = new Set<DashboardListener>();
@@ -219,10 +222,38 @@ class DashboardController {
firmwareWarningMessage: this.firmwareWarningMessage,
firmwareWarningVisible: this.firmwareWarningMessage.length > 0,
rawScreenshotsEnabled: rawScreenshotsEnabledSetting.get(),
batteryOptimizationWarningVisible: this.batteryOptimizationWarningVisible,
};
}
/**
* Re-check the Doze exemption (cheap system call, but cached so snapshot()
* stays trivial). Called on connect, page load, and after the user answers
* the system exemption dialog.
*/
refreshBatteryOptimizationStatus(): void {
const warningVisible = !isIgnoringBatteryOptimizations();
if (warningVisible !== this.batteryOptimizationWarningVisible) {
this.batteryOptimizationWarningVisible = warningVisible;
this.emit();
}
}
requestBatteryOptimizationExemption(): void {
requestIgnoreBatteryOptimizations();
// The system dialog is asynchronous and there is no result callback from
// this context; poll briefly so the banner clears once granted.
let checksLeft = 12;
const poll = setInterval(() => {
this.refreshBatteryOptimizationStatus();
if (!this.batteryOptimizationWarningVisible || --checksLeft <= 0) {
clearInterval(poll);
}
}, 5_000);
}
refreshEvenAppStatus(): void {
this.refreshBatteryOptimizationStatus();
const state = readEvenAppNotificationState();
const wasActive = this.evenNotificationActive;
this.evenNotificationActive = state.evenNotificationActive;
@@ -268,6 +299,7 @@ class DashboardController {
this.lastInput = "waiting...";
this.lastSys = "none yet";
this.firmwareWarningMessage = "";
this.refreshBatteryOptimizationStatus();
resetDashboardSleepTimerAndWake();
this.refreshEvenAppStatus();
this.setPhase("connecting");
+33
View File
@@ -0,0 +1,33 @@
import { Utils } from "@nativescript/core";
declare const android: any;
/**
* Battery-optimization ("Doze") exemption checks. Faceclaw holds a partial
* wakelock while the G2 screen is on, but Android ignores wakelocks of
* non-exempt apps in deep Doze (phone unplugged + still + screen off), which
* suspends the CPU between BLE packets and stalls the render timers to a few
* frames per second. The exemption ("Allow background usage: Unrestricted")
* makes the system honor the wakelock.
*/
export function isIgnoringBatteryOptimizations(): boolean {
if (!global.isAndroid) return true;
const context = Utils.android.getApplicationContext();
if (!context) return true;
const powerManager = context.getSystemService(android.content.Context.POWER_SERVICE);
return Boolean(powerManager?.isIgnoringBatteryOptimizations(context.getPackageName()));
}
/** Open the system dialog asking to exempt Faceclaw from battery optimization. */
export function requestIgnoreBatteryOptimizations(): void {
if (!global.isAndroid) return;
const context = Utils.android.getApplicationContext();
if (!context) return;
const intent = new android.content.Intent(
android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
android.net.Uri.parse(`package:${context.getPackageName()}`),
);
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
+8
View File
@@ -17,6 +17,10 @@
<Button text="Open Even app settings" tap="{{ onOpenEvenAppSettingsTap }}" class="-primary" />
</StackLayout>
<Label text="{{ firmwareWarningMessage }}" textWrap="true" visibility="{{ firmwareWarningVisibility }}" />
<StackLayout visibility="{{ batteryOptimizationWarningVisibility }}">
<Label text="Android will slow Faceclaw to a crawl when the phone screen is off unless it is exempt from battery optimization." textWrap="true" />
<Button text="Allow unrestricted background usage" tap="{{ onAllowBackgroundUsageTap }}" class="-primary" />
</StackLayout>
</StackLayout>
<StackLayout row="1">
<GridLayout rows="auto,auto,auto" columns="*,*,*" class="m-t-8">
@@ -41,6 +45,10 @@
<Button text="Open Even app settings" tap="{{ onOpenEvenAppSettingsTap }}" class="-primary" />
</StackLayout>
<Label text="{{ firmwareWarningMessage }}" textWrap="true" visibility="{{ firmwareWarningVisibility }}" />
<StackLayout visibility="{{ batteryOptimizationWarningVisibility }}">
<Label text="Android will slow Faceclaw to a crawl when the phone screen is off unless it is exempt from battery optimization." textWrap="true" />
<Button text="Allow unrestricted background usage" tap="{{ onAllowBackgroundUsageTap }}" class="-primary" />
</StackLayout>
</StackLayout>
<GridLayout row="1" rows="auto,auto,*" columns="*,260">
<StackLayout row="0" col="0" visibility="{{ displayPreviewVisibility }}" horizontalAlignment="left">
+22
View File
@@ -16,6 +16,7 @@ export class MainViewModel extends Observable {
private _firmwareWarningMessage = "";
private _firmwareWarningVisible = false;
private _rawScreenshotsEnabled = false;
private _batteryOptimizationWarningVisible = false;
private _showLog = false;
private _phase: "disconnected" | "connecting" | "connected" | "disconnecting" = "disconnected";
@@ -34,6 +35,7 @@ export class MainViewModel extends Observable {
this.firmwareWarningMessage = snapshot.firmwareWarningMessage;
this.firmwareWarningVisible = snapshot.firmwareWarningVisible;
this.rawScreenshotsEnabled = snapshot.rawScreenshotsEnabled;
this.batteryOptimizationWarningVisible = snapshot.batteryOptimizationWarningVisible;
});
}
@@ -256,6 +258,26 @@ export class MainViewModel extends Observable {
}
}
get batteryOptimizationWarningVisible(): boolean {
return this._batteryOptimizationWarningVisible;
}
set batteryOptimizationWarningVisible(value: boolean) {
if (this._batteryOptimizationWarningVisible !== value) {
this._batteryOptimizationWarningVisible = value;
this.notifyPropertyChange("batteryOptimizationWarningVisible", value);
this.notifyPropertyChange("batteryOptimizationWarningVisibility", this.batteryOptimizationWarningVisibility);
}
}
get batteryOptimizationWarningVisibility(): "visible" | "collapse" {
return this._batteryOptimizationWarningVisible ? "visible" : "collapse";
}
onAllowBackgroundUsageTap(): void {
dashboardController.requestBatteryOptimizationExemption();
}
get phase(): "disconnected" | "connecting" | "connected" | "disconnecting" {
return this._phase;
}