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

+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;
}