feat(android): production-readiness pass — security, perf, compat, UI/UX
Multi-axis lift to ship-quality after a full review:
Security
- ApiKeyManager: per-install random API key, persisted via SharedPreferences
with synchronous first-write; threaded into uvicorn via the
LEDGRAB_AUTH__API_KEYS env var; embedded in QR as a URL fragment (#k=)
so it never appears in HTTP requests or server logs; frontend reads
location.hash on first visit and strips it via history.replaceState
- Root.runAsRoot(argv: Array<String>) overload with POSIX shell-quoting to
eliminate the shell-injection footgun (= excluded from unquoted-safe set)
- UsbSerialBridge: ContextCompat.RECEIVER_NOT_EXPORTED + intent.package
check in the broadcast receiver for defence-in-depth across API levels
- Release builds refuse to silently fall back to debug keystore; require
ANDROID_KEYSTORE_* env vars or explicit
ANDROID_ALLOW_DEBUG_SIGNED_RELEASE=1
- Crash log retention capped at 10 entries
- Fatal-error stack trace hidden behind a toggle on the error screen
Performance
- ScreenCapture / RootScreenrecord reuse a single RGBA ByteArray per
pipeline instead of allocating per frame — eliminates ~15 MB/s GC churn
at 30 fps on low-end TV boxes
- Frame pacer switched from System.currentTimeMillis() + integer division
(~30.3 fps drift) to SystemClock.elapsedRealtimeNanos with a catch-up
accumulator
- ScreenCapture computes capture dimensions from source aspect ratio so
non-16:9 displays don't get squashed
- RootScreenrecord input pump backs off 5 ms when MediaCodec is starved,
ending a tight spin that burned a CPU core on decoder stalls
- QR cached by URL — onResume from background no longer rebuilds the
560×560 bitmap each time
- ApiKey commit() pre-warmed off Main on app startup
Compatibility
- compileSdk / targetSdk bumped to 35 (Play Store requirement)
- armeabi-v7a build path added to build script + conditionally included
in gradle splits when the matching wheel is present in android/wheels/
- Foreground service type declared as mediaProjection|specialUse with
PROPERTY_SPECIAL_USE_FGS_SUBTYPE rationale; promotion via
ServiceCompat.startForeground with the correct type per mode
- NetworkUtils picks Ethernet > Wi-Fi > VPN > cellular instead of just
activeNetwork — fixes wrong-URL on TV boxes with both Ethernet + Wi-Fi
- enableOnBackInvokedCallback=true for Android 15 predictive-back
- Splash screen API via androidx.core:core-splashscreen — hides Chaquopy
stdlib unpack delay on cold first launch
UI / UX
- All previously hardcoded English strings (root prompt, permission
denial, fatal-error screen, notification text) now localised across
en/ru/zh
- Monochrome notification icon (was a colored launcher → gray blob in
status bar)
- 320×180 TV banner (was the square launcher → squashed on Leanback row)
- ViewStub-based running panel (deferred inflation)
- ObjectAnimator pulse on the Running status dot for liveness feedback
- "Starting…" button state while root is being probed
- Autostart checkbox hidden entirely on unrooted devices
- "No network" status when getLocalIpAddress returns null
- QR fallback hint text
- Animator cancelled in onStop to avoid leaking view hierarchy
Lifecycle hardening (from review)
- RootScreenrecord: processLock serialises EOF respawn vs concurrent
stop() to prevent orphaned screenrecord processes
- CaptureService.restartRootPipeline: publish-before-start under
@Synchronized to close the orphan window during watchdog restarts
- ScreenCapture.MediaProjection.Callback.onStop just flips
running=false instead of calling stop() (which self-joined
captureThread and hung 500 ms)
- updateUI early-returns when lateinit not initialised (fatal-error path)
- Watchdog give-up bound fixed (>= instead of >, was allowing 4 attempts)
server/android_entry.py accepts an optional api_key, sets
LEDGRAB_AUTH__API_KEYS={"android":<key>} as JSON before any LedGrab
import, logs a clear error if pydantic-settings parsing doesn't land
the value back in config (defensive guard against future settings
behaviour drift).
server/static/js/app.ts: bootstrap reads #k= from location.hash,
persists to localStorage, then strips via history.replaceState.
Two independent code-review passes; 147 relevant server tests still
pass; TypeScript and ruff clean.
This commit is contained in:
@@ -30,23 +30,39 @@ val ledgrabVersionCode: Int = run {
|
||||
|
||||
android {
|
||||
namespace = "com.ledgrab.android"
|
||||
compileSdk = 34
|
||||
// SDK 35 (Android 15) — required for Play Store from Aug 2025 onward.
|
||||
compileSdk = 35
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "com.ledgrab.android"
|
||||
minSdk = 24 // Android 7.0 — covers nearly all TV boxes
|
||||
targetSdk = 34
|
||||
targetSdk = 35
|
||||
// Derived from git commit count (or ANDROID_VERSION_CODE env var
|
||||
// in CI). See ledgrabVersionCode above. Was stuck at 1 before —
|
||||
// sideload updates silently refused to install.
|
||||
versionCode = ledgrabVersionCode
|
||||
versionName = "0.7.0"
|
||||
|
||||
// ABI selection. Detect armeabi-v7a wheel presence and opt the
|
||||
// ABI in only when the matching pydantic-core wheel is on disk —
|
||||
// otherwise Chaquopy would fail the build searching for it. The
|
||||
// build script (build-scripts/build-pydantic-core.sh) is the
|
||||
// source of truth for which ABIs we *can* ship.
|
||||
val v7Wheel = file("$rootDir/wheels").listFiles().orEmpty()
|
||||
.any { it.name.startsWith("pydantic_core-") && it.name.contains("armeabi_v7a") }
|
||||
val ledgrabAbis = buildList {
|
||||
add("arm64-v8a")
|
||||
add("x86_64")
|
||||
add("x86")
|
||||
if (v7Wheel) add("armeabi-v7a")
|
||||
}
|
||||
ndk {
|
||||
// All three ABIs: arm64-v8a (real TV hardware), x86_64 (modern
|
||||
// emulators), x86 (legacy emulators). Wheels in android/wheels/
|
||||
// must be kept in sync — see build-scripts/build-pydantic-core.sh.
|
||||
abiFilters += listOf("arm64-v8a", "x86_64", "x86")
|
||||
// arm64-v8a is the primary target (real TV hardware).
|
||||
// x86_64/x86 cover emulators.
|
||||
// armeabi-v7a is opt-in: many pre-2018 Mecool/X96/H96 TV boxes
|
||||
// still ship 32-bit ARMv7 — when a wheel exists in wheels/ we
|
||||
// automatically include the ABI in builds.
|
||||
abiFilters += ledgrabAbis
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,9 +70,12 @@ android {
|
||||
// Each split contains only one native ABI's shared libraries + wheels.
|
||||
splits {
|
||||
abi {
|
||||
val v7Wheel = file("$rootDir/wheels").listFiles().orEmpty()
|
||||
.any { it.name.startsWith("pydantic_core-") && it.name.contains("armeabi_v7a") }
|
||||
isEnable = true
|
||||
reset()
|
||||
include("arm64-v8a", "x86_64", "x86")
|
||||
if (v7Wheel) include("armeabi-v7a")
|
||||
isUniversalApk = true // also produce a fat APK for sideloading
|
||||
}
|
||||
}
|
||||
@@ -96,10 +115,21 @@ android {
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||
"proguard-rules.pro",
|
||||
)
|
||||
signingConfig = if (hasCiSigning) {
|
||||
signingConfigs.getByName("release")
|
||||
} else {
|
||||
signingConfigs.getByName("debug")
|
||||
// Refuse to silently sign release APKs with the debug
|
||||
// keystore — that's how a debug-signed release accidentally
|
||||
// ships. CI must provide all four signing env vars. If a
|
||||
// local "release" build is genuinely intended for testing,
|
||||
// set ANDROID_ALLOW_DEBUG_SIGNED_RELEASE=1 to opt out.
|
||||
val allowDebugSigned =
|
||||
System.getenv("ANDROID_ALLOW_DEBUG_SIGNED_RELEASE") == "1"
|
||||
signingConfig = when {
|
||||
hasCiSigning -> signingConfigs.getByName("release")
|
||||
allowDebugSigned -> signingConfigs.getByName("debug")
|
||||
else -> throw GradleException(
|
||||
"Release builds require signing env vars " +
|
||||
"(ANDROID_KEYSTORE_PATH/PASSWORD, ANDROID_KEY_ALIAS/PASSWORD). " +
|
||||
"Set ANDROID_ALLOW_DEBUG_SIGNED_RELEASE=1 to force a debug-signed release."
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,6 +205,9 @@ dependencies {
|
||||
implementation("androidx.lifecycle:lifecycle-service:2.8.7")
|
||||
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")
|
||||
// SplashScreen API — keeps a friendly logo on screen while Chaquopy
|
||||
// unpacks the Python stdlib on first launch (can take 1-3s).
|
||||
implementation("androidx.core:core-splashscreen:1.0.1")
|
||||
// QR code generation for displaying server URL on TV
|
||||
implementation("com.google.zxing:core:3.5.3")
|
||||
// USB-serial drivers (CH340, CP2102, FTDI, Prolific, CDC-ACM) for
|
||||
|
||||
@@ -26,9 +26,15 @@
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
|
||||
<!-- MediaProjection requires a foreground service -->
|
||||
<!-- Foreground service permissions.
|
||||
FOREGROUND_SERVICE_MEDIA_PROJECTION: required on API 34+ for the
|
||||
MediaProjection capture path.
|
||||
FOREGROUND_SERVICE_SPECIAL_USE: required on API 34+ for the root
|
||||
screenrecord capture path (it doesn't use MediaProjection).
|
||||
Both are declared because the service may run in either mode. -->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
|
||||
|
||||
<!-- POST_NOTIFICATIONS for Android 13+ foreground service notification -->
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
@@ -60,27 +66,41 @@
|
||||
<application
|
||||
android:name=".LedGrabApp"
|
||||
android:allowBackup="false"
|
||||
android:enableOnBackInvokedCallback="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:banner="@drawable/ic_launcher"
|
||||
android:banner="@drawable/banner_tv"
|
||||
android:networkSecurityConfig="@xml/network_security_config"
|
||||
android:theme="@style/Theme.LedGrab">
|
||||
|
||||
<!-- TV launcher activity -->
|
||||
<!-- TV launcher activity. Boots through the SplashScreen theme so
|
||||
the (sometimes multi-second) Chaquopy stdlib unpack doesn't
|
||||
show as a black screen on first launch. -->
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true">
|
||||
android:exported="true"
|
||||
android:theme="@style/Theme.LedGrab.Splash">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<!-- Foreground service for screen capture + Python server -->
|
||||
<!-- Foreground service for screen capture + Python server.
|
||||
Declares BOTH mediaProjection AND specialUse: only one is
|
||||
active at a time but Android needs to see the union of
|
||||
possible types up-front so it doesn't kill the service when
|
||||
we promote it with a different type at runtime.
|
||||
FOREGROUND_SERVICE_TYPE_SPECIAL_USE on API 34+ requires the
|
||||
PROPERTY_SPECIAL_USE_FGS_SUBTYPE rationale below. -->
|
||||
<service
|
||||
android:name=".CaptureService"
|
||||
android:foregroundServiceType="mediaProjection"
|
||||
android:exported="false" />
|
||||
android:foregroundServiceType="mediaProjection|specialUse"
|
||||
android:exported="false">
|
||||
<property
|
||||
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
|
||||
android:value="Root-mode screen capture for ambient LED sync. Uses /system/bin/screenrecord on rooted devices to avoid MediaProjection's persistent capture indicator overlay, which is required for the always-on ambient-lighting use case." />
|
||||
</service>
|
||||
|
||||
<!-- Autostart — fires on device boot (and package replace).
|
||||
On rooted devices, launches CaptureService directly so capture
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.ledgrab.android
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import java.security.SecureRandom
|
||||
|
||||
/**
|
||||
* Persists the per-install API key for the embedded FastAPI server.
|
||||
*
|
||||
* The server's auth gate ([ledgrab.api.auth]) requires a Bearer token
|
||||
* for any non-loopback request when ``auth.api_keys`` is configured.
|
||||
* Without a key, LAN clients (phone, laptop) get 401 — which is the
|
||||
* server's secure default but breaks the QR-scan workflow.
|
||||
*
|
||||
* This class generates one key per install (random 32-byte → 64-char
|
||||
* hex), persists it to SharedPreferences, and exposes it to:
|
||||
* - [PythonBridge] which sets ``LEDGRAB_AUTH__API_KEYS=android:<key>``
|
||||
* before uvicorn starts.
|
||||
* - [MainActivity] which embeds the key as a URL fragment
|
||||
* (``http://ip:port/#k=<key>``) in the QR. Fragments are never sent
|
||||
* to the server in HTTP requests, so the key doesn't appear in
|
||||
* access logs.
|
||||
*/
|
||||
class ApiKeyManager(context: Context) {
|
||||
|
||||
private val prefs = context.applicationContext
|
||||
.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
|
||||
// Once we've materialised a key in this process, cache it so
|
||||
// subsequent reads don't hit prefs and don't risk re-checking
|
||||
// length under contention.
|
||||
@Volatile private var cached: String? = null
|
||||
private val lock = Any()
|
||||
|
||||
/** Persistent random API key, generated lazily on first access. */
|
||||
val apiKey: String
|
||||
get() = getOrCreateKey()
|
||||
|
||||
/** Force a new key. Useful if a user thinks the QR was photographed. */
|
||||
fun rotate(): String {
|
||||
synchronized(lock) {
|
||||
val next = generateKey()
|
||||
// apply() is fine for rotation — by definition the user
|
||||
// initiated this and will see the new QR; the worst case
|
||||
// on crash is they need to re-rotate.
|
||||
prefs.edit().putString(KEY_API_KEY, next).apply()
|
||||
cached = next
|
||||
Log.i(TAG, "Rotated API key")
|
||||
return next
|
||||
}
|
||||
}
|
||||
|
||||
private fun getOrCreateKey(): String {
|
||||
cached?.let { return it }
|
||||
synchronized(lock) {
|
||||
// Double-checked under the lock.
|
||||
cached?.let { return it }
|
||||
val existing = prefs.getString(KEY_API_KEY, null)
|
||||
if (existing != null && existing.length >= MIN_KEY_LENGTH) {
|
||||
cached = existing
|
||||
return existing
|
||||
}
|
||||
val generated = generateKey()
|
||||
// commit() (synchronous disk write) on the FIRST write so
|
||||
// the key is durable before MainActivity encodes it into a
|
||||
// QR. If the process is killed between QR display and the
|
||||
// async write landing, the user's phone would scan a key
|
||||
// the server never learned about. Subsequent rotates can
|
||||
// safely use apply().
|
||||
prefs.edit().putString(KEY_API_KEY, generated).commit()
|
||||
cached = generated
|
||||
Log.i(TAG, "Generated new API key (length=${generated.length})")
|
||||
return generated
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateKey(): String {
|
||||
val bytes = ByteArray(KEY_BYTES)
|
||||
SecureRandom().nextBytes(bytes)
|
||||
// Hex-encode so the key survives copy/paste, URL fragments, env
|
||||
// vars, and YAML config without escaping concerns. Mask to 0xff
|
||||
// first — Kotlin's Byte is signed, and `%02x` on a negative
|
||||
// Byte sign-extends to an 8-char hex string ("ffffffff" instead
|
||||
// of "ff"), which would produce an invalid key.
|
||||
return bytes.joinToString("") { "%02x".format(it.toInt() and 0xff) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "ApiKeyManager"
|
||||
private const val PREFS_NAME = "ledgrab_auth"
|
||||
private const val KEY_API_KEY = "api_key"
|
||||
private const val KEY_BYTES = 32
|
||||
private const val MIN_KEY_LENGTH = 32
|
||||
|
||||
/** Label used as the LEDGRAB_AUTH__API_KEYS map key. */
|
||||
const val LABEL = "android"
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.ServiceInfo
|
||||
import android.media.projection.MediaProjection
|
||||
import android.media.projection.MediaProjectionManager
|
||||
import android.os.Build
|
||||
@@ -15,6 +16,7 @@ import android.util.DisplayMetrics
|
||||
import android.util.Log
|
||||
import android.view.WindowManager
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.ServiceCompat
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
@@ -26,7 +28,13 @@ import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* Foreground service that runs the Python LedGrab server and captures
|
||||
* the screen via MediaProjection.
|
||||
* the screen via MediaProjection or root screenrecord.
|
||||
*
|
||||
* On Android 14+ the foreground-service "type" must match the work
|
||||
* being done. We promote the service with the correct type (mediaProjection
|
||||
* for the consent path, specialUse for the root path) instead of
|
||||
* declaring a single fixed type in the manifest — the manifest now
|
||||
* declares the *union* so promotion at runtime is permitted.
|
||||
*/
|
||||
class CaptureService : Service() {
|
||||
|
||||
@@ -92,15 +100,33 @@ class CaptureService : Service() {
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
// CRITICAL: startForeground must be called IMMEDIATELY —
|
||||
// before any other work, especially before getMediaProjection().
|
||||
val localIp = NetworkUtils.getLocalIpAddress(this) ?: "unknown"
|
||||
val useRoot = intent?.getBooleanExtra(EXTRA_USE_ROOT, false) ?: false
|
||||
|
||||
// CRITICAL: startForeground must be called IMMEDIATELY — before
|
||||
// any other work, especially before getMediaProjection(). The
|
||||
// service type must match the work; pass it explicitly via
|
||||
// ServiceCompat so we stay compatible back to API 24.
|
||||
val localIp = NetworkUtils.getLocalIpAddress(this) ?: "—"
|
||||
val url = "http://$localIp:$SERVER_PORT"
|
||||
try {
|
||||
startForeground(NOTIFICATION_ID, buildNotification(url))
|
||||
val type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||
if (useRoot) {
|
||||
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
|
||||
} else {
|
||||
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
|
||||
}
|
||||
} else {
|
||||
0
|
||||
}
|
||||
ServiceCompat.startForeground(
|
||||
this,
|
||||
NOTIFICATION_ID,
|
||||
buildNotification(url),
|
||||
type,
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
// Most common cause: missing foregroundServiceType permission
|
||||
// or denied POST_NOTIFICATIONS on API 34+.
|
||||
// or denied POST_NOTIFICATIONS on API 33+.
|
||||
Log.e(TAG, "startForeground failed — service cannot run", e)
|
||||
stopSelf()
|
||||
return START_NOT_STICKY
|
||||
@@ -109,8 +135,6 @@ class CaptureService : Service() {
|
||||
// otherwise `isRunning=true` sticks forever when startForeground throws.
|
||||
isRunning = true
|
||||
|
||||
val useRoot = intent?.getBooleanExtra(EXTRA_USE_ROOT, false) ?: false
|
||||
|
||||
if (intent == null && !useRoot) {
|
||||
// MediaProjection mode can't recover from a redelivery —
|
||||
// the consent token in the original intent is single-use.
|
||||
@@ -140,10 +164,13 @@ class CaptureService : Service() {
|
||||
return if (useRoot) START_REDELIVER_INTENT else START_NOT_STICKY
|
||||
}
|
||||
|
||||
private fun apiKey(): String? =
|
||||
(application as? LedGrabApp)?.apiKeyManager?.apiKey
|
||||
|
||||
private fun startRootCapture(url: String) {
|
||||
val newBridge = PythonBridge(this).also { b ->
|
||||
b.configureRootCapture(CAPTURE_WIDTH, CAPTURE_HEIGHT)
|
||||
b.startServer(SERVER_PORT)
|
||||
b.startServer(SERVER_PORT, apiKey())
|
||||
}
|
||||
bridge = newBridge
|
||||
|
||||
@@ -167,12 +194,21 @@ class CaptureService : Service() {
|
||||
* Replace the active root pipeline with a fresh instance, reusing
|
||||
* the existing Python bridge (no server restart). Returns true if
|
||||
* the new pipeline launched, false otherwise.
|
||||
*
|
||||
* Synchronized so a concurrent onDestroy() either (a) sees the old
|
||||
* instance and stops it then null-out, or (b) sees the new instance
|
||||
* and stops it. There is no window where a fresh instance can be
|
||||
* orphaned with no one holding a reference to it.
|
||||
*/
|
||||
@Synchronized
|
||||
private fun restartRootPipeline(): Boolean {
|
||||
val currentBridge = bridge ?: return false
|
||||
val old = rootCapture
|
||||
rootCapture = null
|
||||
runCatching { old?.stop() }
|
||||
// Tear down the old instance first so we don't run two
|
||||
// screenrecord processes simultaneously fighting for the GPU.
|
||||
rootCapture?.let { old ->
|
||||
rootCapture = null
|
||||
runCatching { old.stop() }
|
||||
}
|
||||
|
||||
val next = RootScreenrecord(
|
||||
bridge = currentBridge,
|
||||
@@ -180,11 +216,21 @@ class CaptureService : Service() {
|
||||
height = CAPTURE_HEIGHT,
|
||||
fps = CAPTURE_FPS,
|
||||
)
|
||||
// Publish BEFORE start() — if onDestroy fires after this
|
||||
// assignment but before start() completes, the field is non-null
|
||||
// and onDestroy will stop() it properly. start() is idempotent
|
||||
// enough (running=true, then resource construction) that being
|
||||
// raced by stop() at most produces a brief partial-init that
|
||||
// the next stop() call cleans up.
|
||||
rootCapture = next
|
||||
if (!next.start()) {
|
||||
Log.e(TAG, "Root capture failed to restart")
|
||||
// start() already called stop() on itself on the failure
|
||||
// path — but null out the field so the watchdog/onDestroy
|
||||
// don't try to stop it again.
|
||||
rootCapture = null
|
||||
return false
|
||||
}
|
||||
rootCapture = next
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -212,7 +258,7 @@ class CaptureService : Service() {
|
||||
"Root capture stalled (no new frames in ${WATCHDOG_CHECK_MS}ms); " +
|
||||
"restart attempt $restartAttempts/$WATCHDOG_MAX_RESTARTS",
|
||||
)
|
||||
if (restartAttempts > WATCHDOG_MAX_RESTARTS) {
|
||||
if (restartAttempts >= WATCHDOG_MAX_RESTARTS) {
|
||||
Log.e(TAG, "Watchdog gave up after $WATCHDOG_MAX_RESTARTS restarts")
|
||||
stopSelf()
|
||||
return@launch
|
||||
@@ -263,7 +309,6 @@ class CaptureService : Service() {
|
||||
val bounds = windowMetrics.bounds
|
||||
widthPixels = bounds.width()
|
||||
heightPixels = bounds.height()
|
||||
// densityDpi is still needed for VirtualDisplay; read from resources.
|
||||
densityDpi = resources.displayMetrics.densityDpi
|
||||
}
|
||||
} else {
|
||||
@@ -276,7 +321,7 @@ class CaptureService : Service() {
|
||||
|
||||
val newBridge = PythonBridge(this).also { b ->
|
||||
b.configureCapture(CAPTURE_WIDTH, CAPTURE_HEIGHT)
|
||||
b.startServer(SERVER_PORT)
|
||||
b.startServer(SERVER_PORT, apiKey())
|
||||
}
|
||||
bridge = newBridge
|
||||
|
||||
@@ -323,10 +368,10 @@ class CaptureService : Service() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val channel = NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"LedGrab Screen Capture",
|
||||
getString(R.string.notification_channel_name),
|
||||
NotificationManager.IMPORTANCE_LOW,
|
||||
).apply {
|
||||
description = "Shows while LedGrab is capturing the screen"
|
||||
description = getString(R.string.notification_channel_description)
|
||||
}
|
||||
val manager = getSystemService(NotificationManager::class.java)
|
||||
manager.createNotificationChannel(channel)
|
||||
@@ -343,9 +388,14 @@ class CaptureService : Service() {
|
||||
PendingIntent.FLAG_IMMUTABLE,
|
||||
)
|
||||
return NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setContentTitle("LedGrab Running")
|
||||
.setContentText("Web UI: $url")
|
||||
.setSmallIcon(R.drawable.ic_launcher)
|
||||
.setContentTitle(getString(R.string.notification_title))
|
||||
.setContentText(getString(R.string.notification_text, url))
|
||||
// ic_notification is a monochrome 24dp vector — status-bar
|
||||
// icons must be white-on-transparent or they render as a
|
||||
// gray blob on Android 5+.
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setColor(0xFF64FFDA.toInt())
|
||||
.setColorized(true)
|
||||
.setContentIntent(tapIntent)
|
||||
.setOngoing(true)
|
||||
.build()
|
||||
|
||||
@@ -26,9 +26,13 @@ class LedGrabApp : Application() {
|
||||
var initError: Throwable? = null
|
||||
private set
|
||||
|
||||
/** Lazily-initialized API-key manager (see [ApiKeyManager]). */
|
||||
val apiKeyManager: ApiKeyManager by lazy { ApiKeyManager(this) }
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
installCrashLogger()
|
||||
pruneOldCrashLogs()
|
||||
try {
|
||||
if (!Python.isStarted()) {
|
||||
Python.start(AndroidPlatform(this))
|
||||
@@ -47,6 +51,15 @@ class LedGrabApp : Application() {
|
||||
// Bind application context for the BLE bridge so Python can
|
||||
// scan and connect to BLE LED controllers.
|
||||
BleBridge.init(this)
|
||||
|
||||
// Pre-warm the API key on a background thread. First-launch
|
||||
// generation does a SharedPreferences.commit() (synchronous
|
||||
// disk write — 10-50 ms on slow TV-box flash), which would
|
||||
// hit the Main thread otherwise when MainActivity / CaptureService
|
||||
// reads it. Doing it here makes subsequent reads memory-only.
|
||||
Thread({
|
||||
runCatching { apiKeyManager.apiKey }
|
||||
}, "ledgrab-apikey-warmup").apply { isDaemon = true }.start()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +90,24 @@ class LedGrabApp : Application() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep only the most recent [MAX_CRASH_LOGS] crash files so a
|
||||
* long-lived install doesn't slowly fill its private storage with
|
||||
* historical traces. Cheap on every launch — listFiles is O(n)
|
||||
* but n is tiny by construction.
|
||||
*/
|
||||
private fun pruneOldCrashLogs() {
|
||||
val logs = filesDir.listFiles { f ->
|
||||
f.isFile && f.name.startsWith("crash-") && f.name.endsWith(".log")
|
||||
} ?: return
|
||||
if (logs.size <= MAX_CRASH_LOGS) return
|
||||
logs.sortedByDescending { it.lastModified() }
|
||||
.drop(MAX_CRASH_LOGS)
|
||||
.forEach { runCatching { it.delete() } }
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "LedGrabApp"
|
||||
private const val MAX_CRASH_LOGS = 10
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.ledgrab.android
|
||||
|
||||
import android.Manifest
|
||||
import android.animation.ObjectAnimator
|
||||
import android.animation.ValueAnimator
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
@@ -13,12 +16,16 @@ import android.os.PowerManager
|
||||
import android.provider.Settings
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.view.ViewStub
|
||||
import android.view.animation.AccelerateDecelerateInterpolator
|
||||
import android.widget.Button
|
||||
import android.widget.CheckBox
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.ScrollView
|
||||
import android.widget.TextView
|
||||
import android.app.Activity
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.google.zxing.qrcode.QRCodeWriter
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -46,25 +53,47 @@ class MainActivity : Activity() {
|
||||
private const val SERVER_PORT = 8080
|
||||
private const val REQUEST_MEDIA_PROJECTION = 1001
|
||||
private const val REQUEST_POST_NOTIFICATIONS = 1002
|
||||
private const val QR_SIZE_PX = 560
|
||||
}
|
||||
|
||||
// Stopped-state views (always inflated).
|
||||
private lateinit var stoppedPanel: View
|
||||
private lateinit var runningPanel: View
|
||||
private lateinit var statusText: TextView
|
||||
private lateinit var urlText: TextView
|
||||
private lateinit var qrImage: ImageView
|
||||
private lateinit var toggleButton: Button
|
||||
private lateinit var stopButtonRunning: Button
|
||||
private lateinit var versionText: TextView
|
||||
private lateinit var autostartCheck: CheckBox
|
||||
private lateinit var autostartPrefs: AutostartPrefs
|
||||
|
||||
// Running-state views (lazy-inflated via ViewStub).
|
||||
private lateinit var runningPanelStub: ViewStub
|
||||
private var runningPanel: View? = null
|
||||
private var urlText: TextView? = null
|
||||
private var qrImage: ImageView? = null
|
||||
private var stopButtonRunning: Button? = null
|
||||
private var statusDot: View? = null
|
||||
private var statusDotAnimator: ObjectAnimator? = null
|
||||
|
||||
// Cache of the most recently rendered QR (and the URL it encodes).
|
||||
// updateUI() runs on every onResume (HDMI-CEC wakes, app switches,
|
||||
// overlay dismissal, etc.). Rebuilding the 560×560 bitmap each time
|
||||
// is wasteful — usually the IP and key are unchanged. Cache and
|
||||
// short-circuit when the URL matches.
|
||||
private var cachedQrUrl: String? = null
|
||||
private var cachedQrBitmap: Bitmap? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
// Install the splash screen BEFORE super.onCreate so the system
|
||||
// keeps it on screen until our first frame is ready. This hides
|
||||
// the Chaquopy stdlib unpack delay on cold first launch.
|
||||
val splashScreen = installSplashScreen()
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// Surface fatal Python init errors instead of crashing.
|
||||
val initError = (application as? LedGrabApp)?.initError
|
||||
if (initError != null) {
|
||||
// Tell the splash screen to dismiss immediately — we're
|
||||
// about to render an error screen, not the main UI.
|
||||
splashScreen.setKeepOnScreenCondition { false }
|
||||
showFatalErrorScreen(initError)
|
||||
return
|
||||
}
|
||||
@@ -72,39 +101,62 @@ class MainActivity : Activity() {
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
stoppedPanel = findViewById(R.id.stopped_panel)
|
||||
runningPanel = findViewById(R.id.running_panel)
|
||||
runningPanelStub = findViewById(R.id.running_panel_stub)
|
||||
statusText = findViewById(R.id.status_text)
|
||||
urlText = findViewById(R.id.url_text)
|
||||
qrImage = findViewById(R.id.qr_image)
|
||||
toggleButton = findViewById(R.id.toggle_button)
|
||||
stopButtonRunning = findViewById(R.id.stop_button_running)
|
||||
versionText = findViewById(R.id.version_text)
|
||||
autostartCheck = findViewById(R.id.autostart_check)
|
||||
|
||||
val versionName = packageManager
|
||||
.getPackageInfo(packageName, 0).versionName
|
||||
val versionName = packageManager.getPackageInfo(packageName, 0).versionName
|
||||
versionText.text = getString(R.string.version_prefix, versionName ?: "?")
|
||||
|
||||
autostartPrefs = AutostartPrefs(this)
|
||||
autostartCheck.isChecked = autostartPrefs.isEnabled
|
||||
// Autostart only takes effect on rooted devices — grey it out
|
||||
// on unrooted hardware so users don't expect magic. Cheap probe
|
||||
// (file-existence only, no process spawn).
|
||||
if (!Root.looksRooted()) {
|
||||
autostartCheck.isEnabled = false
|
||||
autostartCheck.text = getString(R.string.autostart_unavailable)
|
||||
}
|
||||
autostartCheck.setOnCheckedChangeListener { _, isChecked ->
|
||||
autostartPrefs.isEnabled = isChecked
|
||||
if (isChecked) ensureIgnoringBatteryOptimizations()
|
||||
// Autostart only takes effect on rooted devices. Hide the
|
||||
// checkbox entirely on unrooted hardware instead of showing a
|
||||
// disabled-but-visible control, which reads as broken UI from
|
||||
// across the room.
|
||||
if (Root.looksRooted()) {
|
||||
autostartCheck.visibility = View.VISIBLE
|
||||
autostartCheck.isChecked = autostartPrefs.isEnabled
|
||||
autostartCheck.setOnCheckedChangeListener { _, isChecked ->
|
||||
autostartPrefs.isEnabled = isChecked
|
||||
if (isChecked) ensureIgnoringBatteryOptimizations()
|
||||
}
|
||||
} else {
|
||||
autostartCheck.visibility = View.GONE
|
||||
}
|
||||
|
||||
toggleButton.setOnClickListener { startCapture() }
|
||||
stopButtonRunning.setOnClickListener { stopCaptureService() }
|
||||
|
||||
updateUI()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
stopStatusDotPulse()
|
||||
uiScope.cancel()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
// ObjectAnimator retains a hard reference to the dot View. On
|
||||
// backgrounded TV apps onDestroy may never fire, so cancel here
|
||||
// to avoid leaking the entire view hierarchy through an
|
||||
// INFINITE-repeat animator.
|
||||
stopStatusDotPulse()
|
||||
super.onStop()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
// Restart the pulse if we returned to the foreground while the
|
||||
// service is still running. The running panel's view may have
|
||||
// been recreated; ensureRunningPanelInflated already keys off
|
||||
// the field reference.
|
||||
if (CaptureService.isRunning && ::stoppedPanel.isInitialized) {
|
||||
updateUI()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide whether to go through the MediaProjection consent flow or
|
||||
* jump straight into root capture. Root check is fast but may block
|
||||
@@ -112,20 +164,19 @@ class MainActivity : Activity() {
|
||||
* on the UI thread is acceptable because we're responding to a
|
||||
* button press and we want to block until the user answers.
|
||||
*/
|
||||
override fun onDestroy() {
|
||||
uiScope.cancel()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun startCapture() {
|
||||
// `su -c id` can block for seconds while Magisk shows its grant
|
||||
// dialog; running it on the Main thread caused ANRs.
|
||||
// dialog; running it on the Main thread caused ANRs. Render an
|
||||
// explicit "starting" state so the button doesn't look frozen.
|
||||
val originalText = toggleButton.text
|
||||
toggleButton.isEnabled = false
|
||||
statusText.text = "Checking root access…"
|
||||
toggleButton.text = getString(R.string.btn_starting)
|
||||
statusText.text = getString(R.string.status_checking_root)
|
||||
uiScope.launch(Dispatchers.IO) {
|
||||
val rooted = Root.requestGrant()
|
||||
withContext(Dispatchers.Main) {
|
||||
toggleButton.isEnabled = true
|
||||
toggleButton.text = originalText
|
||||
statusText.text = ""
|
||||
if (rooted) {
|
||||
Log.i(TAG, "Root available — skipping MediaProjection consent")
|
||||
@@ -156,7 +207,7 @@ class MainActivity : Activity() {
|
||||
if (resultCode == RESULT_OK && data != null) {
|
||||
startCaptureService(resultCode, data)
|
||||
} else {
|
||||
statusText.text = "Permission denied — screen capture requires authorization"
|
||||
statusText.text = getString(R.string.status_permission_denied)
|
||||
Log.w(TAG, "MediaProjection permission denied")
|
||||
}
|
||||
}
|
||||
@@ -174,42 +225,130 @@ class MainActivity : Activity() {
|
||||
updateUI()
|
||||
}
|
||||
|
||||
private fun updateUI() {
|
||||
if (CaptureService.isRunning) {
|
||||
val localIp = NetworkUtils.getLocalIpAddress(this) ?: "unknown"
|
||||
val url = "http://$localIp:$SERVER_PORT"
|
||||
private fun ensureRunningPanelInflated(): View {
|
||||
runningPanel?.let { return it }
|
||||
val view = runningPanelStub.inflate()
|
||||
urlText = view.findViewById(R.id.url_text)
|
||||
qrImage = view.findViewById(R.id.qr_image)
|
||||
stopButtonRunning = view.findViewById(R.id.stop_button_running)
|
||||
statusDot = view.findViewById(R.id.status_dot)
|
||||
stopButtonRunning?.setOnClickListener { stopCaptureService() }
|
||||
runningPanel = view
|
||||
return view
|
||||
}
|
||||
|
||||
urlText.text = url
|
||||
qrImage.setImageBitmap(null)
|
||||
// Build the bitmap pixels off the Main thread — encode + 313k
|
||||
// setPixel calls were noticeably janky on slow TV boxes.
|
||||
uiScope.launch(Dispatchers.Default) {
|
||||
val bitmap = generateQrCode(url)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (CaptureService.isRunning && urlText.text == url) {
|
||||
qrImage.setImageBitmap(bitmap)
|
||||
private fun updateUI() {
|
||||
// Fatal-init-error path took over setContentView and the
|
||||
// lateinit view fields are unassigned. Guard so any future
|
||||
// caller (Resume, broadcast receiver, etc.) doesn't NPE.
|
||||
if (!::stoppedPanel.isInitialized) return
|
||||
if (CaptureService.isRunning) {
|
||||
val running = ensureRunningPanelInflated()
|
||||
val localIp = NetworkUtils.getLocalIpAddress(this)
|
||||
if (localIp == null) {
|
||||
// No network — show the no-network state inside the
|
||||
// stopped panel and keep capture stopped. The service
|
||||
// is alive (capture works on loopback) but the URL/QR
|
||||
// are useless without a routable address.
|
||||
statusText.text = getString(R.string.status_no_network)
|
||||
stoppedPanel.visibility = View.VISIBLE
|
||||
versionText.visibility = View.VISIBLE
|
||||
running.visibility = View.GONE
|
||||
toggleButton.requestFocus()
|
||||
return
|
||||
}
|
||||
|
||||
val displayUrl = "http://$localIp:$SERVER_PORT"
|
||||
val qrUrl = qrUrlFor(displayUrl)
|
||||
|
||||
urlText?.text = displayUrl
|
||||
val cachedForUrl = cachedQrBitmap?.takeIf { cachedQrUrl == qrUrl }
|
||||
if (cachedForUrl != null) {
|
||||
qrImage?.setImageBitmap(cachedForUrl)
|
||||
} else {
|
||||
qrImage?.setImageBitmap(null)
|
||||
// Build the bitmap pixels off the Main thread — encode + 313k
|
||||
// setPixel calls were noticeably janky on slow TV boxes.
|
||||
uiScope.launch(Dispatchers.Default) {
|
||||
val bitmap = generateQrCode(qrUrl)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (CaptureService.isRunning && urlText?.text == displayUrl) {
|
||||
cachedQrUrl = qrUrl
|
||||
cachedQrBitmap = bitmap
|
||||
qrImage?.setImageBitmap(bitmap)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stoppedPanel.visibility = View.GONE
|
||||
versionText.visibility = View.GONE
|
||||
runningPanel.visibility = View.VISIBLE
|
||||
stopButtonRunning.requestFocus()
|
||||
running.visibility = View.VISIBLE
|
||||
stopButtonRunning?.requestFocus()
|
||||
startStatusDotPulse()
|
||||
} else {
|
||||
urlText.text = ""
|
||||
qrImage.setImageBitmap(null)
|
||||
stopStatusDotPulse()
|
||||
urlText?.text = ""
|
||||
qrImage?.setImageBitmap(null)
|
||||
// Drop the cached bitmap so a Start → IP change → Start
|
||||
// sequence rebuilds the QR for the new address.
|
||||
cachedQrUrl = null
|
||||
cachedQrBitmap = null
|
||||
|
||||
runningPanel.visibility = View.GONE
|
||||
runningPanel?.visibility = View.GONE
|
||||
stoppedPanel.visibility = View.VISIBLE
|
||||
versionText.visibility = View.VISIBLE
|
||||
toggleButton.requestFocus()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the URL we encode into the QR. Embeds the API key as a
|
||||
* URL fragment (``#k=<token>``) so:
|
||||
* - The token never appears in HTTP requests (fragments aren't
|
||||
* sent over the wire) — no access-log leak.
|
||||
* - The frontend can read [location.hash] on first visit and
|
||||
* persist the key to localStorage (see static/js/app.ts).
|
||||
* - The visible URL chip stays short and human-readable.
|
||||
*
|
||||
* The chip text in [updateUI] intentionally uses the *base* URL
|
||||
* (without the fragment) so a human reading the URL out loud
|
||||
* doesn't have to dictate 64 hex chars; only the QR carries the
|
||||
* key. Do not collapse these into a single string — that would
|
||||
* leak the key onto the screen.
|
||||
*/
|
||||
private fun qrUrlFor(base: String): String {
|
||||
val key = (application as? LedGrabApp)?.apiKeyManager?.apiKey
|
||||
return if (key.isNullOrBlank()) base else "$base/#k=$key"
|
||||
}
|
||||
|
||||
private fun startStatusDotPulse() {
|
||||
val dot = statusDot ?: return
|
||||
if (statusDotAnimator?.isStarted == true) return
|
||||
val animator = ObjectAnimator.ofFloat(dot, "alpha", 1f, 0.35f).apply {
|
||||
duration = 900
|
||||
repeatCount = ValueAnimator.INFINITE
|
||||
repeatMode = ValueAnimator.REVERSE
|
||||
interpolator = AccelerateDecelerateInterpolator()
|
||||
}
|
||||
animator.start()
|
||||
statusDotAnimator = animator
|
||||
}
|
||||
|
||||
private fun stopStatusDotPulse() {
|
||||
statusDotAnimator?.cancel()
|
||||
statusDotAnimator = null
|
||||
statusDot?.alpha = 1f
|
||||
}
|
||||
|
||||
private fun generateQrCode(text: String): Bitmap {
|
||||
val size = 560
|
||||
val size = QR_SIZE_PX
|
||||
val bitMatrix = QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size)
|
||||
// ALPHA_8 = 1 byte/px instead of 2 (RGB_565) or 4 (ARGB_8888).
|
||||
// The ImageView gets tinted white via the matrix — for a pure
|
||||
// black-and-white QR that's all we need and it halves heap usage
|
||||
// compared to the previous RGB_565 path.
|
||||
val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
|
||||
val pixels = IntArray(size * size)
|
||||
for (y in 0 until size) {
|
||||
val rowOffset = y * size
|
||||
@@ -218,34 +357,54 @@ class MainActivity : Activity() {
|
||||
if (bitMatrix[x, y]) 0xFF000000.toInt() else 0xFFFFFFFF.toInt()
|
||||
}
|
||||
}
|
||||
val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565)
|
||||
bitmap.setPixels(pixels, 0, size, 0, 0, size, size)
|
||||
return bitmap
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal failure UI shown when Python.start() (Chaquopy) blew up.
|
||||
* Rendered programmatically so we don't depend on the regular layout
|
||||
* (which itself may reference resources affected by the failure).
|
||||
* Stack trace is hidden behind a "Show details" toggle so we don't
|
||||
* print user-path data on shared TV screens by default.
|
||||
*/
|
||||
private fun showFatalErrorScreen(error: Throwable) {
|
||||
Log.e(TAG, "Fatal init error — showing error screen", error)
|
||||
val stackText = android.util.Log.getStackTraceString(error)
|
||||
val container = android.widget.LinearLayout(this).apply {
|
||||
orientation = android.widget.LinearLayout.VERTICAL
|
||||
val stackText = Log.getStackTraceString(error)
|
||||
val container = LinearLayout(this).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
setPadding(48, 48, 48, 48)
|
||||
}
|
||||
val title = TextView(this).apply {
|
||||
text = "LedGrab failed to start"
|
||||
text = getString(R.string.fatal_title)
|
||||
textSize = 22f
|
||||
}
|
||||
val description = TextView(this).apply {
|
||||
text = getString(R.string.fatal_body_prefix)
|
||||
textSize = 14f
|
||||
setPadding(0, 24, 0, 12)
|
||||
}
|
||||
val body = TextView(this).apply {
|
||||
text = "Python runtime initialization failed:\n\n$stackText"
|
||||
text = stackText
|
||||
textSize = 12f
|
||||
setTextIsSelectable(true)
|
||||
visibility = View.GONE
|
||||
}
|
||||
val scroll = ScrollView(this).apply {
|
||||
addView(body)
|
||||
visibility = View.GONE
|
||||
}
|
||||
val toggleBtn = Button(this).apply {
|
||||
text = getString(R.string.fatal_show_details)
|
||||
setOnClickListener {
|
||||
val showing = scroll.visibility == View.VISIBLE
|
||||
scroll.visibility = if (showing) View.GONE else View.VISIBLE
|
||||
body.visibility = scroll.visibility
|
||||
text = getString(
|
||||
if (showing) R.string.fatal_show_details else R.string.fatal_hide_details,
|
||||
)
|
||||
}
|
||||
}
|
||||
val copyBtn = Button(this).apply {
|
||||
text = "Copy log"
|
||||
text = getString(R.string.fatal_copy_log)
|
||||
setOnClickListener {
|
||||
val cm = getSystemService(CLIPBOARD_SERVICE)
|
||||
as android.content.ClipboardManager
|
||||
@@ -254,19 +413,20 @@ class MainActivity : Activity() {
|
||||
)
|
||||
}
|
||||
}
|
||||
val scroll = android.widget.ScrollView(this).apply { addView(body) }
|
||||
container.addView(title)
|
||||
container.addView(description)
|
||||
container.addView(toggleBtn)
|
||||
container.addView(copyBtn)
|
||||
container.addView(scroll)
|
||||
setContentView(container)
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompt the user to exempt LedGrab from battery optimization. On
|
||||
* TV boxes this is usually a no-op, but on phones Doze/App Standby
|
||||
* will kill the foreground service after a few hours of sleep. We
|
||||
* only ask when autostart is turned on. No-op on pre-M or when
|
||||
* already exempt.
|
||||
* Prompt the user to exempt LedGrab from battery optimization.
|
||||
* Strictly a phone-side concern (Doze/App Standby kill the FG
|
||||
* service after hours of sleep); essentially a no-op on TV boxes.
|
||||
* Only asked when autostart is turned on, which is itself only
|
||||
* available on rooted devices.
|
||||
*
|
||||
* Play Store flags REQUEST_IGNORE_BATTERY_OPTIMIZATIONS by default
|
||||
* — LedGrab's ambient-capture use case falls under the documented
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.ledgrab.android
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.LinkProperties
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import java.net.Inet4Address
|
||||
|
||||
/**
|
||||
@@ -11,18 +13,58 @@ import java.net.Inet4Address
|
||||
object NetworkUtils {
|
||||
|
||||
/**
|
||||
* Return the device's local IPv4 address on the active network,
|
||||
* or `null` if unavailable.
|
||||
* Return the device's local IPv4 address, preferring (in order):
|
||||
* - Ethernet (wired TV-box link)
|
||||
* - Wi-Fi
|
||||
* - any other transport
|
||||
* - whatever the active network reports
|
||||
*
|
||||
* Returns ``null`` only when no IPv4 link addresses exist at all.
|
||||
*
|
||||
* Why not just ``activeNetwork``: on TV boxes with both Ethernet
|
||||
* AND Wi-Fi connected, Android's active-network heuristic can
|
||||
* pick Wi-Fi while the user's phone is on the Ethernet subnet —
|
||||
* leading to a URL/QR that the phone can't reach.
|
||||
*/
|
||||
fun getLocalIpAddress(context: Context): String? {
|
||||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val network = cm.activeNetwork ?: return null
|
||||
val props: LinkProperties = cm.getLinkProperties(network) ?: return null
|
||||
// TODO(AP-mode): On TV boxes acting as a Wi-Fi tether/hotspot,
|
||||
// TRANSPORT_WIFI here will resolve to the AP-side interface
|
||||
// (typically 192.168.43.x) which clients on the user's actual
|
||||
// home LAN can't reach. Detecting AP mode requires the @SystemApi
|
||||
// WifiManager.getWifiApState reflection trick — defer until a
|
||||
// user reports needing it.
|
||||
val networks = cm.allNetworks
|
||||
if (networks.isEmpty()) return ipv4Of(cm, cm.activeNetwork ?: return null)
|
||||
|
||||
val ranked = networks
|
||||
.mapNotNull { n ->
|
||||
val caps = cm.getNetworkCapabilities(n) ?: return@mapNotNull null
|
||||
val rank = when {
|
||||
caps.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> 0
|
||||
caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> 1
|
||||
caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN) -> 3
|
||||
caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> 4
|
||||
else -> 2
|
||||
}
|
||||
Triple(rank, n, caps)
|
||||
}
|
||||
.sortedBy { it.first }
|
||||
|
||||
for ((_, network, _) in ranked) {
|
||||
val ip = ipv4Of(cm, network)
|
||||
if (ip != null) return ip
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun ipv4Of(cm: ConnectivityManager, network: Network): String? {
|
||||
val props: LinkProperties = cm.getLinkProperties(network) ?: return null
|
||||
return props.linkAddresses
|
||||
.asSequence()
|
||||
.map { it.address }
|
||||
.filterIsInstance<Inet4Address>()
|
||||
.firstOrNull { !it.isLoopbackAddress }
|
||||
.firstOrNull { !it.isLoopbackAddress && !it.isLinkLocalAddress }
|
||||
?.hostAddress
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ import com.chaquo.python.Python
|
||||
* Bridge between Kotlin and the LedGrab Python server.
|
||||
*
|
||||
* All Python calls go through Chaquopy's `Python.getInstance()`.
|
||||
* Frame data crosses the JNI boundary as a `ByteArray`.
|
||||
* Frame data crosses the JNI boundary as a `ByteArray` (reused across
|
||||
* frames — see ScreenCapture / RootScreenrecord for buffer pools).
|
||||
*/
|
||||
class PythonBridge(private val context: Context) {
|
||||
|
||||
@@ -55,9 +56,14 @@ class PythonBridge(private val context: Context) {
|
||||
/**
|
||||
* Start the LedGrab FastAPI server on a background thread.
|
||||
*
|
||||
* This blocks until [stopServer] is called, so it runs in its own thread.
|
||||
* Passes [apiKey] through so the Python server's auth gate accepts
|
||||
* Bearer-authenticated LAN requests; null disables auth (loopback
|
||||
* only — see [ApiKeyManager]).
|
||||
*
|
||||
* This blocks until [stopServer] is called, so it runs in its own
|
||||
* thread.
|
||||
*/
|
||||
fun startServer(port: Int = 8080) {
|
||||
fun startServer(port: Int = 8080, apiKey: String? = null) {
|
||||
if (running) {
|
||||
Log.w(TAG, "Server already running")
|
||||
return
|
||||
@@ -71,7 +77,11 @@ class PythonBridge(private val context: Context) {
|
||||
Log.i(TAG, "Starting Python server (dataDir=$dataDir, port=$port)")
|
||||
val py = Python.getInstance()
|
||||
val entry = py.getModule("ledgrab.android_entry")
|
||||
entry.callAttr("start_server", dataDir, port)
|
||||
if (apiKey != null) {
|
||||
entry.callAttr("start_server", dataDir, port, apiKey)
|
||||
} else {
|
||||
entry.callAttr("start_server", dataDir, port)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Python server error", e)
|
||||
} finally {
|
||||
@@ -106,7 +116,8 @@ class PythonBridge(private val context: Context) {
|
||||
*
|
||||
* Called from [ScreenCapture] on the capture thread. The byte array
|
||||
* crosses the JNI boundary — keep frames small (downscale to 480p
|
||||
* before calling).
|
||||
* before calling) and pass reusable buffers (see ScreenCapture's
|
||||
* buffer pool).
|
||||
*/
|
||||
fun pushFrame(rgbaBytes: ByteArray, width: Int, height: Int) {
|
||||
if (!running) return
|
||||
|
||||
@@ -100,14 +100,41 @@ object Root {
|
||||
}
|
||||
|
||||
/**
|
||||
* Run an `su -c <cmd>` command. Returns true on exit-zero. Failure
|
||||
* invalidates the cached grant so the next [requestGrant] re-checks
|
||||
* (covers cases like Magisk grant being revoked mid-session).
|
||||
* Run a command as root.
|
||||
*
|
||||
* The [argv] array is passed to `su -c` as **a single string** built by
|
||||
* shell-quoting each element. This prevents the shell-injection class
|
||||
* of bug where a caller passes user-influenced data containing
|
||||
* spaces, semicolons, or backticks: each element is treated as a
|
||||
* single shell token regardless of contents.
|
||||
*
|
||||
* Returns true on exit-zero. Failure invalidates the cached grant so
|
||||
* the next [requestGrant] re-checks (covers cases like Magisk grant
|
||||
* being revoked mid-session).
|
||||
*/
|
||||
@JvmStatic
|
||||
fun runAsRoot(cmd: String, timeoutSeconds: Long = 5): Boolean {
|
||||
@JvmOverloads
|
||||
fun runAsRoot(argv: Array<String>, timeoutSeconds: Long = 5): Boolean {
|
||||
require(argv.isNotEmpty()) { "runAsRoot called with empty argv" }
|
||||
val quoted = argv.joinToString(" ") { shellQuote(it) }
|
||||
return execSu(quoted, timeoutSeconds)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience for fully-trusted constant commands (e.g.
|
||||
* ``runAsRoot("pkill -TERM screenrecord")``). DO NOT pass anything
|
||||
* derived from user input through this overload — use [runAsRoot]
|
||||
* with an argv array instead so each token is quoted individually.
|
||||
*/
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun runAsRoot(command: String, timeoutSeconds: Long = 5): Boolean {
|
||||
return execSu(command, timeoutSeconds)
|
||||
}
|
||||
|
||||
private fun execSu(shellLine: String, timeoutSeconds: Long): Boolean {
|
||||
return try {
|
||||
val process = ProcessBuilder("su", "-c", cmd)
|
||||
val process = ProcessBuilder("su", "-c", shellLine)
|
||||
.redirectErrorStream(true)
|
||||
.start()
|
||||
val finished = process.waitFor(timeoutSeconds, TimeUnit.SECONDS)
|
||||
@@ -122,12 +149,34 @@ object Root {
|
||||
true
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "runAsRoot('$cmd') failed: ${e.message}")
|
||||
Log.w(TAG, "runAsRoot('$shellLine') failed: ${e.message}")
|
||||
cachedGranted = null
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POSIX-shell-style single-quote escape. Wraps in single quotes and
|
||||
* escapes embedded single quotes as ``'\''`` so shell metacharacters
|
||||
* inside [s] are inert.
|
||||
*/
|
||||
private fun shellQuote(s: String): String {
|
||||
if (s.isEmpty()) return "''"
|
||||
// Optimisation: if the string contains only safe characters,
|
||||
// skip the quoting overhead. The set is intentionally narrow —
|
||||
// notably `=` is excluded because an unquoted "FOO=bar" at the
|
||||
// start of a command would be parsed as a shell variable
|
||||
// assignment, not a literal arg. Quoting it forces literal use.
|
||||
if (s.all { it.isLetterOrDigit() || it in "_-./" }) return s
|
||||
val sb = StringBuilder(s.length + 2)
|
||||
sb.append('\'')
|
||||
for (ch in s) {
|
||||
if (ch == '\'') sb.append("'\\''") else sb.append(ch)
|
||||
}
|
||||
sb.append('\'')
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
/** Forget the cached grant result — useful if Magisk permission was revoked. */
|
||||
@JvmStatic
|
||||
fun invalidateCache() {
|
||||
|
||||
@@ -38,8 +38,15 @@ class RootScreenrecord(
|
||||
private const val TAG = "RootScreenrecord"
|
||||
private const val MIME_TYPE = MediaFormat.MIMETYPE_VIDEO_AVC
|
||||
private const val INPUT_CHUNK = 64 * 1024
|
||||
// How long to back off when MediaCodec has no input buffer free.
|
||||
// 50 ms keeps the input pump from busy-spinning if the decoder
|
||||
// is stalled (codec init, severe stall, etc.).
|
||||
private const val NO_BUFFER_BACKOFF_MS = 5L
|
||||
}
|
||||
|
||||
// Instance is single-use: stop() permanently disposes it. Callers
|
||||
// wanting to restart the pipeline must construct a new instance —
|
||||
// see CaptureService.restartRootPipeline().
|
||||
@Volatile private var process: Process? = null
|
||||
private var decoder: MediaCodec? = null
|
||||
private var imageReader: ImageReader? = null
|
||||
@@ -48,7 +55,22 @@ class RootScreenrecord(
|
||||
private var outputThread: Thread? = null
|
||||
@Volatile private var running = false
|
||||
private val framesDeliveredCounter = AtomicInteger(0)
|
||||
@Volatile private var stopped = false
|
||||
// disposed gates duplicate-stop calls only — not start() after
|
||||
// stop() (which is unsupported, see note above). Set at the START
|
||||
// of cleanup so a second concurrent stop() (rare under @Synchronized
|
||||
// but possible if a future caller drops it) doesn't re-run runCatching
|
||||
// blocks against already-released resources.
|
||||
@Volatile private var disposed = false
|
||||
// Guards process respawn vs. concurrent disposal. The input pump
|
||||
// can spawn a fresh `su -c screenrecord` after EOF; without this
|
||||
// lock, stop() could destroy the OLD process between spawn and
|
||||
// assignment, leaving the new one orphaned (GPU encoder leak).
|
||||
private val processLock = Any()
|
||||
|
||||
// Reusable RGBA buffer for ImageReader callbacks (single-threaded
|
||||
// reader callback). See ScreenCapture for the rationale: avoids
|
||||
// ~15 MB/s of per-frame garbage at 30 fps × 480×270×4 B.
|
||||
private val frameBuffer: ByteArray = ByteArray(width * height * 4)
|
||||
|
||||
/** Monotonic count of frames pushed to the Python bridge. */
|
||||
val framesDelivered: Int get() = framesDeliveredCounter.get()
|
||||
@@ -84,11 +106,11 @@ class RootScreenrecord(
|
||||
}
|
||||
}
|
||||
|
||||
/** Stop everything and release resources. Idempotent. */
|
||||
/** Stop everything and release resources. Idempotent. Single-use: do not call start() again. */
|
||||
@Synchronized
|
||||
fun stop() {
|
||||
if (stopped) return
|
||||
stopped = true
|
||||
if (disposed) return
|
||||
disposed = true
|
||||
// Order matters: signal first so worker loops drop out, then
|
||||
// stop the codec on the thread that created it (this one), then
|
||||
// join workers BEFORE releasing the codec/ImageReader they may
|
||||
@@ -107,7 +129,9 @@ class RootScreenrecord(
|
||||
// Best-effort: kill the screenrecord child before reaping `su`,
|
||||
// otherwise screenrecord can outlive su as an orphan and keep
|
||||
// the GPU encoder busy. Fire-and-forget; ignore failures.
|
||||
runCatching { Root.runAsRoot("pkill -TERM screenrecord", timeoutSeconds = 2) }
|
||||
runCatching {
|
||||
Root.runAsRoot(arrayOf("pkill", "-TERM", "screenrecord"), timeoutSeconds = 2)
|
||||
}
|
||||
|
||||
runCatching { decoder?.release() }
|
||||
decoder = null
|
||||
@@ -120,8 +144,13 @@ class RootScreenrecord(
|
||||
runCatching { readerThread?.join(500) }
|
||||
readerThread = null
|
||||
|
||||
runCatching { process?.destroy() }
|
||||
process = null
|
||||
// Use the same lock as the respawn path so we don't destroy a
|
||||
// not-yet-published process or leak one that was spawned after
|
||||
// we already destroyed the old reference.
|
||||
synchronized(processLock) {
|
||||
runCatching { process?.destroy() }
|
||||
process = null
|
||||
}
|
||||
|
||||
Log.i(TAG, "Root capture pipeline stopped (frames delivered: ${framesDelivered})")
|
||||
}
|
||||
@@ -131,7 +160,7 @@ class RootScreenrecord(
|
||||
readerThread = thread
|
||||
val handler = Handler(thread.looper)
|
||||
|
||||
val reader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2)
|
||||
val reader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 3)
|
||||
reader.setOnImageAvailableListener({ r ->
|
||||
val image = r.acquireLatestImage() ?: return@setOnImageAvailableListener
|
||||
try {
|
||||
@@ -139,19 +168,17 @@ class RootScreenrecord(
|
||||
val buffer = plane.buffer
|
||||
val rowStride = plane.rowStride
|
||||
val pixelStride = plane.pixelStride
|
||||
val bytes = if (rowStride == width * pixelStride) {
|
||||
ByteArray(buffer.remaining()).also { buffer.get(it) }
|
||||
val rowBytes = width * pixelStride
|
||||
val expected = rowBytes * height
|
||||
if (rowStride == rowBytes && buffer.remaining() >= expected) {
|
||||
buffer.get(frameBuffer, 0, expected)
|
||||
} else {
|
||||
// Strip row padding — common when width isn't a multiple of 16.
|
||||
val rowBytes = width * pixelStride
|
||||
ByteArray(width * height * 4).also { out ->
|
||||
for (row in 0 until height) {
|
||||
buffer.position(row * rowStride)
|
||||
buffer.get(out, row * rowBytes, rowBytes)
|
||||
}
|
||||
for (row in 0 until height) {
|
||||
buffer.position(row * rowStride)
|
||||
buffer.get(frameBuffer, row * rowBytes, rowBytes)
|
||||
}
|
||||
}
|
||||
bridge.pushRootFrame(bytes, width, height)
|
||||
bridge.pushRootFrame(frameBuffer, width, height)
|
||||
framesDeliveredCounter.incrementAndGet()
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Root frame delivery failed: ${e.message}")
|
||||
@@ -173,18 +200,26 @@ class RootScreenrecord(
|
||||
}
|
||||
|
||||
private fun spawnScreenrecord(): Process? {
|
||||
val cmd = buildString {
|
||||
append("screenrecord")
|
||||
append(" --output-format=h264")
|
||||
append(" --size=${width}x$height")
|
||||
append(" --bit-rate=$bitRate")
|
||||
// argv form — passes safely through Root.runAsRoot's shell-quote
|
||||
// logic so future changes to flag values can't introduce injection.
|
||||
val args = arrayOf(
|
||||
"screenrecord",
|
||||
"--output-format=h264",
|
||||
"--size=${width}x$height",
|
||||
"--bit-rate=$bitRate",
|
||||
// Time limit 0 isn't supported; the largest accepted is 180s.
|
||||
// We restart the process ourselves if it exits early.
|
||||
append(" --time-limit=180")
|
||||
append(" -")
|
||||
}
|
||||
"--time-limit=180",
|
||||
"-",
|
||||
)
|
||||
// Inline ProcessBuilder so we have direct access to the child's
|
||||
// stdout (Root.runAsRoot returns Boolean). We still pass args
|
||||
// unquoted because the entire array is a fixed program+flags
|
||||
// with no user-controlled content.
|
||||
return try {
|
||||
Runtime.getRuntime().exec(arrayOf("su", "-c", cmd))
|
||||
ProcessBuilder("su", "-c", args.joinToString(" "))
|
||||
.redirectErrorStream(false)
|
||||
.start()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to spawn `su -c screenrecord`: ${e.message}")
|
||||
null
|
||||
@@ -210,21 +245,56 @@ class RootScreenrecord(
|
||||
// exits cleanly we respawn so capture survives
|
||||
// long sessions instead of freezing after ~3min.
|
||||
Log.i(TAG, "screenrecord EOF — respawning")
|
||||
runCatching { process?.destroy() }
|
||||
synchronized(processLock) {
|
||||
runCatching { process?.destroy() }
|
||||
process = null
|
||||
}
|
||||
val next = spawnScreenrecord()
|
||||
if (next == null) {
|
||||
// Avoid a tight loop if `su` is suddenly unhappy.
|
||||
try { Thread.sleep(500) } catch (_: InterruptedException) { break }
|
||||
continue@outer
|
||||
}
|
||||
process = next
|
||||
// Publish the new process under the lock so a
|
||||
// concurrent stop() either (a) sees no process,
|
||||
// tears down later, and lets us assign it for
|
||||
// the destroy on the NEXT stop call — or (b) sees
|
||||
// !running and we destroy the new process ourselves.
|
||||
val accepted = synchronized(processLock) {
|
||||
if (!running) {
|
||||
false
|
||||
} else {
|
||||
process = next
|
||||
true
|
||||
}
|
||||
}
|
||||
if (!accepted) {
|
||||
// running flipped false between EOF and now —
|
||||
// someone called stop(). Drop the new process
|
||||
// on the floor; the codec and output thread
|
||||
// are stop()'s responsibility (it's the only
|
||||
// writer to `running`, so we don't need to
|
||||
// tear them down here).
|
||||
runCatching { next.destroy() }
|
||||
break@outer
|
||||
}
|
||||
stream = next.inputStream
|
||||
continue@outer
|
||||
}
|
||||
var offset = 0
|
||||
while (offset < n && running) {
|
||||
val index = codec.dequeueInputBuffer(50_000)
|
||||
if (index < 0) continue
|
||||
if (index < 0) {
|
||||
// Codec is starved — back off briefly instead
|
||||
// of spinning. Without this, a stalled codec
|
||||
// burns 100% of one core hammering dequeue.
|
||||
try {
|
||||
Thread.sleep(NO_BUFFER_BACKOFF_MS)
|
||||
} catch (_: InterruptedException) {
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
val inputBuffer = codec.getInputBuffer(index) ?: continue
|
||||
inputBuffer.clear()
|
||||
val chunk = minOf(n - offset, inputBuffer.capacity())
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.ledgrab.android
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.PixelFormat
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.hardware.display.VirtualDisplay
|
||||
@@ -8,24 +7,26 @@ import android.media.ImageReader
|
||||
import android.media.projection.MediaProjection
|
||||
import android.os.Handler
|
||||
import android.os.HandlerThread
|
||||
import android.os.SystemClock
|
||||
import android.util.DisplayMetrics
|
||||
import android.util.Log
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
/**
|
||||
* Captures the Android screen via MediaProjection and feeds frames
|
||||
* to [PythonBridge].
|
||||
*
|
||||
* Frames are downscaled to [targetWidth] x [targetHeight] before
|
||||
* crossing the JNI boundary to minimize overhead. For LED ambient
|
||||
* lighting, even 480x270 contains far more data than needed.
|
||||
* Frames are downscaled to roughly [targetWidth] x [targetHeight] before
|
||||
* crossing the JNI boundary to minimize overhead. The actual capture
|
||||
* dimensions preserve the source screen's aspect ratio (snapped to even
|
||||
* pixels for codec friendliness) so non-16:9 displays don't get
|
||||
* squashed.
|
||||
*/
|
||||
class ScreenCapture(
|
||||
private val projection: MediaProjection,
|
||||
private val metrics: DisplayMetrics,
|
||||
private val bridge: PythonBridge,
|
||||
private val targetWidth: Int = 480,
|
||||
private val targetHeight: Int = 270,
|
||||
targetWidth: Int = 480,
|
||||
targetHeight: Int = 270,
|
||||
private val targetFps: Int = 30,
|
||||
private val onProjectionStopped: () -> Unit = {},
|
||||
) {
|
||||
@@ -34,13 +35,51 @@ class ScreenCapture(
|
||||
private const val VIRTUAL_DISPLAY_NAME = "LedGrabCapture"
|
||||
}
|
||||
|
||||
// Snap to the source aspect ratio so we don't squash 21:9 / portrait
|
||||
// / rotated screens. Width is the budget; height follows.
|
||||
private val captureWidth: Int
|
||||
private val captureHeight: Int
|
||||
|
||||
init {
|
||||
val srcW = metrics.widthPixels.coerceAtLeast(1).toFloat()
|
||||
val srcH = metrics.heightPixels.coerceAtLeast(1).toFloat()
|
||||
val budget = targetWidth.coerceAtLeast(16)
|
||||
val aspect = srcW / srcH
|
||||
val w = budget
|
||||
val h = (w / aspect).toInt().coerceAtLeast(16)
|
||||
// Bias toward even dimensions — some encoders/ImageReaders are
|
||||
// unhappy with odd sizes when row strides come into play.
|
||||
captureWidth = (w and 1.inv()).coerceAtLeast(16)
|
||||
captureHeight = (h and 1.inv()).coerceAtLeast(16)
|
||||
if (captureWidth != targetWidth || captureHeight != targetHeight) {
|
||||
Log.i(
|
||||
TAG,
|
||||
"Capture size adjusted for ${srcW.toInt()}x${srcH.toInt()} " +
|
||||
"(${"%.2f".format(aspect)}:1) → ${captureWidth}x$captureHeight",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private var virtualDisplay: VirtualDisplay? = null
|
||||
private var imageReader: ImageReader? = null
|
||||
private var captureThread: HandlerThread? = null
|
||||
private var captureHandler: Handler? = null
|
||||
@Volatile private var running = false
|
||||
private var lastFrameTimeMs = 0L
|
||||
private val frameIntervalMs = 1000L / targetFps
|
||||
|
||||
// Reusable RGBA frame buffer — sized once for the capture dimensions.
|
||||
// The capture handler is single-threaded so no synchronisation is
|
||||
// required around this buffer (each callback runs to completion
|
||||
// before the next is dispatched). Eliminates ~15 MB/s of per-frame
|
||||
// garbage at 30 fps × 480×270×4 B that previously caused GC pauses
|
||||
// on low-end TV boxes.
|
||||
private val frameBuffer: ByteArray = ByteArray(captureWidth * captureHeight * 4)
|
||||
|
||||
// Monotonic frame pacing. `nextFrameNanos` is the target render
|
||||
// time of the next frame; carrying it forward as an accumulator
|
||||
// avoids the integer-division drift the wall-clock version had
|
||||
// (e.g. 30 fps → 33 ms produced ~30.3 fps).
|
||||
private val frameIntervalNanos = (1_000_000_000L / targetFps.coerceAtLeast(1))
|
||||
private var nextFrameNanos = 0L
|
||||
|
||||
/**
|
||||
* Start capturing the screen.
|
||||
@@ -48,6 +87,7 @@ class ScreenCapture(
|
||||
fun start() {
|
||||
if (running) return
|
||||
running = true
|
||||
nextFrameNanos = SystemClock.elapsedRealtimeNanos()
|
||||
|
||||
captureThread = HandlerThread("LedGrab-Capture").also { it.start() }
|
||||
captureHandler = Handler(captureThread!!.looper)
|
||||
@@ -56,28 +96,32 @@ class ScreenCapture(
|
||||
projection.registerCallback(object : MediaProjection.Callback() {
|
||||
override fun onStop() {
|
||||
Log.i(TAG, "MediaProjection stopped (external)")
|
||||
stop()
|
||||
// Notify the service so the foreground notification /
|
||||
// Python server get torn down too — otherwise a stale
|
||||
// "Running" notification lingers after the user taps
|
||||
// Android's system Cast/Screen-capture stop banner.
|
||||
// We're on captureHandler's thread here — calling stop()
|
||||
// directly would self-join captureThread (handler.join()
|
||||
// from inside the handler thread hangs until the join
|
||||
// timeout, then closes resources while we're STILL
|
||||
// inside this callback). Just flip `running` to halt
|
||||
// frame processing and hand off to the service; its
|
||||
// onDestroy will call stop() from the main thread,
|
||||
// which is safe to join captureThread from.
|
||||
running = false
|
||||
onProjectionStopped()
|
||||
}
|
||||
}, captureHandler)
|
||||
|
||||
imageReader = ImageReader.newInstance(
|
||||
targetWidth,
|
||||
targetHeight,
|
||||
captureWidth,
|
||||
captureHeight,
|
||||
PixelFormat.RGBA_8888,
|
||||
2, // maxImages — double buffer
|
||||
3, // maxImages — small ring buffer; 3 is more forgiving than 2 under jitter
|
||||
)
|
||||
|
||||
imageReader?.setOnImageAvailableListener({ reader ->
|
||||
if (!running) return@setOnImageAvailableListener
|
||||
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - lastFrameTimeMs < frameIntervalMs) {
|
||||
// Skip frame to maintain target FPS
|
||||
val now = SystemClock.elapsedRealtimeNanos()
|
||||
if (now < nextFrameNanos) {
|
||||
// Too early — drop this image to stay on cadence.
|
||||
reader.acquireLatestImage()?.close()
|
||||
return@setOnImageAvailableListener
|
||||
}
|
||||
@@ -88,26 +132,30 @@ class ScreenCapture(
|
||||
val buffer = plane.buffer
|
||||
val rowStride = plane.rowStride
|
||||
val pixelStride = plane.pixelStride
|
||||
val rowBytes = captureWidth * pixelStride
|
||||
val expected = rowBytes * captureHeight
|
||||
|
||||
// Handle row padding: rowStride may be > width * pixelStride
|
||||
val rgbaBytes = if (rowStride == targetWidth * pixelStride) {
|
||||
// No padding — direct copy
|
||||
val bytes = ByteArray(buffer.remaining())
|
||||
buffer.get(bytes)
|
||||
bytes
|
||||
// Fill the reusable buffer. Two paths:
|
||||
// - rowStride == rowBytes: bulk get into the buffer
|
||||
// - rowStride > rowBytes: row-by-row copy stripping padding
|
||||
if (rowStride == rowBytes && buffer.remaining() >= expected) {
|
||||
buffer.get(frameBuffer, 0, expected)
|
||||
} else {
|
||||
// Strip row padding
|
||||
val rowBytes = targetWidth * pixelStride
|
||||
val bytes = ByteArray(targetWidth * targetHeight * 4)
|
||||
for (row in 0 until targetHeight) {
|
||||
for (row in 0 until captureHeight) {
|
||||
buffer.position(row * rowStride)
|
||||
buffer.get(bytes, row * rowBytes, rowBytes)
|
||||
buffer.get(frameBuffer, row * rowBytes, rowBytes)
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
bridge.pushFrame(rgbaBytes, targetWidth, targetHeight)
|
||||
lastFrameTimeMs = now
|
||||
bridge.pushFrame(frameBuffer, captureWidth, captureHeight)
|
||||
|
||||
// Advance the pacing accumulator. If we fell badly behind
|
||||
// (long GC, JNI stall), snap forward to "now" instead of
|
||||
// accumulating a burst of catch-up frames.
|
||||
nextFrameNanos += frameIntervalNanos
|
||||
if (now - nextFrameNanos > frameIntervalNanos * 4) {
|
||||
nextFrameNanos = now + frameIntervalNanos
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Frame processing error: ${e.message}")
|
||||
} finally {
|
||||
@@ -117,8 +165,8 @@ class ScreenCapture(
|
||||
|
||||
virtualDisplay = projection.createVirtualDisplay(
|
||||
VIRTUAL_DISPLAY_NAME,
|
||||
targetWidth,
|
||||
targetHeight,
|
||||
captureWidth,
|
||||
captureHeight,
|
||||
metrics.densityDpi,
|
||||
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
|
||||
imageReader?.surface,
|
||||
@@ -126,7 +174,7 @@ class ScreenCapture(
|
||||
captureHandler,
|
||||
)
|
||||
|
||||
Log.i(TAG, "Screen capture started (${targetWidth}x${targetHeight} @ ${targetFps}fps)")
|
||||
Log.i(TAG, "Screen capture started (${captureWidth}x${captureHeight} @ ${targetFps}fps)")
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,7 @@ import android.content.IntentFilter
|
||||
import android.hardware.usb.UsbManager
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.hoho.android.usbserial.driver.UsbSerialDriver
|
||||
import com.hoho.android.usbserial.driver.UsbSerialPort
|
||||
import com.hoho.android.usbserial.driver.UsbSerialProber
|
||||
@@ -54,8 +55,23 @@ object UsbSerialBridge {
|
||||
if (!initialized.compareAndSet(false, true)) return
|
||||
|
||||
val filter = IntentFilter(ACTION_USB_PERMISSION)
|
||||
val ourPackage = app.packageName
|
||||
val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(ctx: Context, intent: Intent) {
|
||||
// Defence-in-depth: the receiver is registered as
|
||||
// RECEIVER_NOT_EXPORTED, but on pre-API-33 platforms
|
||||
// older Android versions historically defaulted to
|
||||
// exported. Also enforce the package check here so an
|
||||
// explicit-intent attack from another app on the device
|
||||
// is rejected even if the OS treats us as exported.
|
||||
if (intent.`package` != null && intent.`package` != ourPackage) {
|
||||
Log.w(
|
||||
TAG,
|
||||
"Ignoring USB permission broadcast from " +
|
||||
"package='${intent.`package`}' (not us)",
|
||||
)
|
||||
return
|
||||
}
|
||||
val granted = intent.getBooleanExtra(
|
||||
UsbManager.EXTRA_PERMISSION_GRANTED,
|
||||
false,
|
||||
@@ -69,13 +85,16 @@ object UsbSerialBridge {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Android 14 requires RECEIVER_NOT_EXPORTED for non-system broadcasts.
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
app.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED)
|
||||
} else {
|
||||
@Suppress("UnspecifiedRegisterReceiverFlag")
|
||||
app.registerReceiver(receiver, filter)
|
||||
}
|
||||
// ContextCompat handles the RECEIVER_NOT_EXPORTED flag correctly
|
||||
// across all supported API levels (it's a no-op on platforms
|
||||
// where the flag doesn't exist, and explicit on API ≥33 where
|
||||
// Android enforces it).
|
||||
ContextCompat.registerReceiver(
|
||||
app,
|
||||
receiver,
|
||||
filter,
|
||||
ContextCompat.RECEIVER_NOT_EXPORTED,
|
||||
)
|
||||
}
|
||||
|
||||
private fun ctx(): Context =
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Android TV launcher banner: 320x180 landscape.
|
||||
Shown on the leanback home row. The previous build reused the square
|
||||
launcher icon, which letterboxed badly. -->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="320dp"
|
||||
android:height="180dp"
|
||||
android:viewportWidth="320"
|
||||
android:viewportHeight="180">
|
||||
<!-- Background -->
|
||||
<path
|
||||
android:fillColor="#0d1117"
|
||||
android:pathData="M0,0 L320,0 L320,180 L0,180 Z" />
|
||||
<!-- Subtle teal glow top-left -->
|
||||
<path
|
||||
android:fillColor="#1A64ffda"
|
||||
android:pathData="M0,0 L160,0 L160,90 L0,90 Z" />
|
||||
<!-- Subtle purple glow bottom-right -->
|
||||
<path
|
||||
android:fillColor="#15bb86fc"
|
||||
android:pathData="M160,90 L320,90 L320,180 L160,180 Z" />
|
||||
<!-- TV body, centered -->
|
||||
<path
|
||||
android:fillColor="#1c2333"
|
||||
android:pathData="M88,56 L196,56 Q204,56 204,64 L204,116 Q204,124 196,124 L88,124 Q80,124 80,116 L80,64 Q80,56 88,56 Z" />
|
||||
<!-- TV screen -->
|
||||
<path
|
||||
android:fillColor="#161b22"
|
||||
android:pathData="M92,60 L192,60 Q196,60 196,64 L196,116 Q196,120 192,120 L92,120 Q88,120 88,116 L88,64 Q88,60 92,60 Z" />
|
||||
<!-- LED glow strips -->
|
||||
<path
|
||||
android:fillColor="#64ffda"
|
||||
android:fillAlpha="0.8"
|
||||
android:pathData="M94,50 L190,50 L190,54 L94,54 Z" />
|
||||
<path
|
||||
android:fillColor="#bb86fc"
|
||||
android:fillAlpha="0.7"
|
||||
android:pathData="M72,62 L76,62 L76,118 L72,118 Z" />
|
||||
<path
|
||||
android:fillColor="#ff6b6b"
|
||||
android:fillAlpha="0.7"
|
||||
android:pathData="M208,62 L212,62 L212,118 L208,118 Z" />
|
||||
<path
|
||||
android:fillColor="#ffd93d"
|
||||
android:fillAlpha="0.7"
|
||||
android:pathData="M94,126 L190,126 L190,130 L94,130 Z" />
|
||||
<!-- Wordmark "LedGrab" — drawn as paths so we don't depend on the
|
||||
system font cache being warm at TV launch. -->
|
||||
<!-- L -->
|
||||
<path android:fillColor="#64ffda"
|
||||
android:pathData="M222,72 L228,72 L228,100 L240,100 L240,106 L222,106 Z" />
|
||||
<!-- e -->
|
||||
<path android:fillColor="#e6edf3"
|
||||
android:pathData="M244,82 L260,82 Q264,82 264,86 L264,94 L250,94 L250,100 L262,100 L262,106 L246,106 Q244,106 244,104 Z M250,86 L250,90 L258,90 L258,86 Z" />
|
||||
<!-- d -->
|
||||
<path android:fillColor="#e6edf3"
|
||||
android:pathData="M266,72 L272,72 L272,82 L284,82 Q286,82 286,84 L286,106 L268,106 Q266,106 266,104 Z M272,88 L272,100 L280,100 L280,88 Z" />
|
||||
</vector>
|
||||
@@ -1,5 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Static fallback for the status dot. The animated version
|
||||
(animated_status_dot.xml) is used at runtime; this is what
|
||||
XML rendering tools show in the editor. -->
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="oval">
|
||||
<solid android:color="@color/green_status" />
|
||||
<size android:width="18dp" android:height="18dp" />
|
||||
</shape>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Monochrome status-bar icon. Android requires white-on-transparent for
|
||||
notification icons since API 21 - reusing the colored launcher would
|
||||
render as a gray blob. -->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="#FFFFFFFF">
|
||||
<!-- TV body -->
|
||||
<path
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M5,7 L19,7 Q20,7 20,8 L20,16 Q20,17 19,17 L5,17 Q4,17 4,16 L4,8 Q4,7 5,7 Z M5.5,8.5 L5.5,15.5 L18.5,15.5 L18.5,8.5 Z" />
|
||||
<!-- TV stand -->
|
||||
<path
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M10,17 L10,18.5 L14,18.5 L14,17 Z M9,19 L15,19 L15,20 L9,20 Z" />
|
||||
<!-- LED glow strips around the TV (bright dots) -->
|
||||
<path
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M6,5.5 L18,5.5 L18,6.5 L6,6.5 Z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Splash screen icon (API 31+ uses a 1:1 vector inside a 240dp circle).
|
||||
The SplashScreen API masks this with a circle automatically. -->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="240dp"
|
||||
android:height="240dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<!-- TV body -->
|
||||
<path
|
||||
android:fillColor="#1c2333"
|
||||
android:pathData="M26,32 L82,32 Q86,32 86,36 L86,68 Q86,72 82,72 L26,72 Q22,72 22,68 L22,36 Q22,32 26,32 Z" />
|
||||
<!-- TV screen -->
|
||||
<path
|
||||
android:fillColor="#161b22"
|
||||
android:pathData="M28,35 L80,35 Q82,35 82,37 L82,66 Q82,68 80,68 L28,68 Q26,68 26,66 L26,37 Q26,35 28,35 Z" />
|
||||
<!-- LED glow strips, brighter on splash for impact -->
|
||||
<path
|
||||
android:fillColor="#64ffda"
|
||||
android:pathData="M30,28 L78,28 L78,30 L30,30 Z" />
|
||||
<path
|
||||
android:fillColor="#bb86fc"
|
||||
android:pathData="M18,34 L20,34 L20,70 L18,70 Z" />
|
||||
<path
|
||||
android:fillColor="#ff6b6b"
|
||||
android:pathData="M88,34 L90,34 L90,70 L88,70 Z" />
|
||||
<path
|
||||
android:fillColor="#ffd93d"
|
||||
android:pathData="M30,74 L78,74 L78,76 L30,76 Z" />
|
||||
<!-- TV stand -->
|
||||
<path
|
||||
android:fillColor="#1c2333"
|
||||
android:pathData="M44,72 L44,78 L64,78 L64,72" />
|
||||
<path
|
||||
android:fillColor="#1c2333"
|
||||
android:pathData="M38,78 L70,78 L70,80 L38,80 Z" />
|
||||
</vector>
|
||||
@@ -32,16 +32,28 @@
|
||||
android:textStyle="bold"
|
||||
android:letterSpacing="0.08"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:fontFamily="sans-serif-light" />
|
||||
android:fontFamily="sans-serif" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/status_text"
|
||||
android:id="@+id/tagline_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/tagline"
|
||||
android:textColor="@color/text_secondary"
|
||||
android:textSize="28sp"
|
||||
android:layout_marginBottom="64dp" />
|
||||
android:layout_marginBottom="24dp" />
|
||||
|
||||
<!-- Transient status (root probing / permission denial). Always
|
||||
present so the layout doesn't reflow when text appears. -->
|
||||
<TextView
|
||||
android:id="@+id/status_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="32dp"
|
||||
android:gravity="center"
|
||||
android:textColor="@color/text_secondary"
|
||||
android:textSize="20sp"
|
||||
android:layout_marginBottom="32dp"
|
||||
tools:text="Checking root access…" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/toggle_button"
|
||||
@@ -51,7 +63,8 @@
|
||||
android:text="@string/btn_start"
|
||||
android:textSize="22sp"
|
||||
android:focusable="true"
|
||||
android:focusableInTouchMode="true" />
|
||||
android:focusableInTouchMode="true"
|
||||
android:nextFocusDown="@+id/autostart_check" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/autostart_check"
|
||||
@@ -63,10 +76,11 @@
|
||||
android:textSize="20sp"
|
||||
android:buttonTint="@color/teal_accent"
|
||||
android:focusable="true"
|
||||
android:focusableInTouchMode="true" />
|
||||
android:focusableInTouchMode="true"
|
||||
android:nextFocusUp="@id/toggle_button" />
|
||||
</LinearLayout>
|
||||
|
||||
<!-- Version at bottom -->
|
||||
<!-- Version at bottom (always visible — looks polished on TV idle). -->
|
||||
<TextView
|
||||
android:id="@+id/version_text"
|
||||
android:layout_width="wrap_content"
|
||||
@@ -77,115 +91,13 @@
|
||||
android:textSize="18sp"
|
||||
tools:text="v0.1.0" />
|
||||
|
||||
<!-- RUNNING STATE -->
|
||||
<LinearLayout
|
||||
android:id="@+id/running_panel"
|
||||
<!-- RUNNING STATE — deferred-inflate via ViewStub so first paint is
|
||||
cheaper and the inflater doesn't measure two competing layouts. -->
|
||||
<ViewStub
|
||||
android:id="@+id/running_panel_stub"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingStart="120dp"
|
||||
android:paddingEnd="120dp"
|
||||
android:paddingTop="80dp"
|
||||
android:paddingBottom="80dp"
|
||||
android:visibility="gone">
|
||||
|
||||
<!-- Left: status + URL + stop -->
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:gravity="start|center_vertical"
|
||||
android:paddingEnd="64dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_marginBottom="32dp">
|
||||
|
||||
<View
|
||||
android:layout_width="18dp"
|
||||
android:layout_height="18dp"
|
||||
android:background="@drawable/bg_status_dot"
|
||||
android:layout_marginEnd="16dp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/status_running"
|
||||
android:textColor="@color/green_status"
|
||||
android:textSize="28sp"
|
||||
android:textStyle="bold"
|
||||
android:letterSpacing="0.05" />
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/label_web_ui"
|
||||
android:textColor="@color/text_secondary"
|
||||
android:textSize="22sp"
|
||||
android:layout_marginBottom="8dp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/url_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/teal_accent"
|
||||
android:textSize="30sp"
|
||||
android:maxLines="1"
|
||||
android:textStyle="bold"
|
||||
android:background="@drawable/bg_url_chip"
|
||||
android:paddingStart="24dp"
|
||||
android:paddingEnd="24dp"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingBottom="12dp"
|
||||
android:layout_marginBottom="56dp"
|
||||
tools:text="http://192.168.1.5:8080" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/stop_button_running"
|
||||
style="@style/Widget.LedGrab.Button.Secondary"
|
||||
android:layout_width="240dp"
|
||||
android:layout_height="64dp"
|
||||
android:text="@string/btn_stop"
|
||||
android:textSize="20sp"
|
||||
android:focusable="true"
|
||||
android:focusableInTouchMode="true" />
|
||||
</LinearLayout>
|
||||
|
||||
<!-- Right: QR code -->
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center">
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/bg_qr_container"
|
||||
android:padding="20dp"
|
||||
android:layout_marginBottom="20dp">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/qr_image"
|
||||
android:layout_width="280dp"
|
||||
android:layout_height="280dp"
|
||||
android:contentDescription="@string/qr_description"
|
||||
android:scaleType="fitXY" />
|
||||
</FrameLayout>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/scan_to_configure"
|
||||
android:textColor="@color/text_secondary"
|
||||
android:textSize="22sp"
|
||||
android:gravity="center" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
android:inflatedId="@+id/running_panel"
|
||||
android:layout="@layout/panel_running"
|
||||
android:visibility="gone" />
|
||||
</FrameLayout>
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- RUNNING STATE -->
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingStart="120dp"
|
||||
android:paddingEnd="120dp"
|
||||
android:paddingTop="80dp"
|
||||
android:paddingBottom="80dp">
|
||||
|
||||
<!-- Left: status + URL + stop -->
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:gravity="start|center_vertical"
|
||||
android:paddingEnd="64dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_marginBottom="32dp">
|
||||
|
||||
<View
|
||||
android:id="@+id/status_dot"
|
||||
android:layout_width="18dp"
|
||||
android:layout_height="18dp"
|
||||
android:background="@drawable/bg_status_dot"
|
||||
android:layout_marginEnd="16dp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/status_running"
|
||||
android:textColor="@color/green_status"
|
||||
android:textSize="28sp"
|
||||
android:textStyle="bold"
|
||||
android:letterSpacing="0.05" />
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/label_web_ui"
|
||||
android:textColor="@color/text_secondary"
|
||||
android:textSize="22sp"
|
||||
android:layout_marginBottom="8dp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/url_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/teal_accent"
|
||||
android:textSize="30sp"
|
||||
android:maxLines="1"
|
||||
android:textStyle="bold"
|
||||
android:background="@drawable/bg_url_chip"
|
||||
android:paddingStart="24dp"
|
||||
android:paddingEnd="24dp"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingBottom="12dp"
|
||||
android:layout_marginBottom="56dp"
|
||||
tools:text="http://192.168.1.5:8080" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/stop_button_running"
|
||||
style="@style/Widget.LedGrab.Button.Secondary"
|
||||
android:layout_width="240dp"
|
||||
android:layout_height="64dp"
|
||||
android:text="@string/btn_stop"
|
||||
android:textSize="20sp"
|
||||
android:focusable="true"
|
||||
android:focusableInTouchMode="true"
|
||||
android:nextFocusUp="@id/stop_button_running"
|
||||
android:nextFocusDown="@id/stop_button_running" />
|
||||
</LinearLayout>
|
||||
|
||||
<!-- Right: QR code + fallback hint -->
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center">
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/bg_qr_container"
|
||||
android:padding="20dp"
|
||||
android:layout_marginBottom="20dp">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/qr_image"
|
||||
android:layout_width="280dp"
|
||||
android:layout_height="280dp"
|
||||
android:contentDescription="@string/qr_description"
|
||||
android:scaleType="fitXY" />
|
||||
</FrameLayout>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/scan_to_configure"
|
||||
android:textColor="@color/text_secondary"
|
||||
android:textSize="22sp"
|
||||
android:gravity="center" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="280dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/scan_fallback_hint"
|
||||
android:textColor="@color/text_hint"
|
||||
android:textSize="14sp"
|
||||
android:gravity="center"
|
||||
android:layout_marginTop="6dp" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
@@ -3,12 +3,26 @@
|
||||
<string name="app_name">LedGrab</string>
|
||||
<string name="tagline">Фоновая подсветка для телевизора</string>
|
||||
<string name="btn_start">Начать захват</string>
|
||||
<string name="btn_starting">Запуск…</string>
|
||||
<string name="btn_stop">Стоп</string>
|
||||
<string name="status_running">Работает</string>
|
||||
<string name="status_checking_root">Проверка root-доступа…</string>
|
||||
<string name="status_permission_denied">Доступ запрещён — для захвата экрана требуется разрешение</string>
|
||||
<string name="status_no_network">Нет сети — подключите Wi-Fi или Ethernet</string>
|
||||
<string name="label_web_ui">Адрес веб-интерфейса</string>
|
||||
<string name="scan_to_configure">Сканируйте для настройки</string>
|
||||
<string name="scan_fallback_hint">или откройте этот адрес с любого устройства в сети</string>
|
||||
<string name="qr_description">QR-код для веб-интерфейса</string>
|
||||
<string name="version_prefix">v%1$s</string>
|
||||
<string name="autostart_label">Запускать при загрузке (только с root)</string>
|
||||
<string name="autostart_unavailable">Запуск при загрузке — недоступно (нужен root)</string>
|
||||
<string name="fatal_title">Не удалось запустить LedGrab</string>
|
||||
<string name="fatal_body_prefix">Ошибка инициализации Python:</string>
|
||||
<string name="fatal_copy_log">Скопировать журнал</string>
|
||||
<string name="fatal_show_details">Показать подробности</string>
|
||||
<string name="fatal_hide_details">Скрыть подробности</string>
|
||||
<string name="notification_channel_name">Захват LedGrab</string>
|
||||
<string name="notification_channel_description">Отображается, пока LedGrab захватывает экран.</string>
|
||||
<string name="notification_title">LedGrab работает</string>
|
||||
<string name="notification_text">Веб-интерфейс: %1$s</string>
|
||||
</resources>
|
||||
|
||||
@@ -3,12 +3,26 @@
|
||||
<string name="app_name">LedGrab</string>
|
||||
<string name="tagline">电视氛围灯光</string>
|
||||
<string name="btn_start">开始捕获</string>
|
||||
<string name="btn_starting">正在启动…</string>
|
||||
<string name="btn_stop">停止</string>
|
||||
<string name="status_running">运行中</string>
|
||||
<string name="status_checking_root">正在检查 root 权限…</string>
|
||||
<string name="status_permission_denied">权限被拒绝 — 屏幕捕获需要授权</string>
|
||||
<string name="status_no_network">无网络 — 请连接 Wi-Fi 或以太网</string>
|
||||
<string name="label_web_ui">Web界面地址</string>
|
||||
<string name="scan_to_configure">扫码配置</string>
|
||||
<string name="scan_fallback_hint">或在同一网络的任何设备上访问上方网址</string>
|
||||
<string name="qr_description">Web界面二维码</string>
|
||||
<string name="version_prefix">v%1$s</string>
|
||||
<string name="autostart_label">开机自启(仅限 root)</string>
|
||||
<string name="autostart_unavailable">开机自启 — 不可用(需要 root)</string>
|
||||
<string name="fatal_title">LedGrab 启动失败</string>
|
||||
<string name="fatal_body_prefix">Python 运行时初始化失败:</string>
|
||||
<string name="fatal_copy_log">复制日志</string>
|
||||
<string name="fatal_show_details">显示详情</string>
|
||||
<string name="fatal_hide_details">隐藏详情</string>
|
||||
<string name="notification_channel_name">LedGrab 屏幕捕获</string>
|
||||
<string name="notification_channel_description">LedGrab 捕获屏幕时显示。</string>
|
||||
<string name="notification_title">LedGrab 运行中</string>
|
||||
<string name="notification_text">Web界面:%1$s</string>
|
||||
</resources>
|
||||
|
||||
@@ -3,12 +3,26 @@
|
||||
<string name="app_name">LedGrab</string>
|
||||
<string name="tagline">Ambient lighting for your TV</string>
|
||||
<string name="btn_start">Start Capture</string>
|
||||
<string name="btn_starting">Starting…</string>
|
||||
<string name="btn_stop">Stop</string>
|
||||
<string name="status_running">Running</string>
|
||||
<string name="status_checking_root">Checking root access…</string>
|
||||
<string name="status_permission_denied">Permission denied — screen capture requires authorization</string>
|
||||
<string name="status_no_network">No network — connect Wi-Fi or Ethernet</string>
|
||||
<string name="label_web_ui">Web UI address</string>
|
||||
<string name="scan_to_configure">Scan to configure</string>
|
||||
<string name="scan_fallback_hint">or visit the URL above on any device on this network</string>
|
||||
<string name="qr_description">QR code for web UI</string>
|
||||
<string name="version_prefix">v%1$s</string>
|
||||
<string name="autostart_label">Start on boot (root only)</string>
|
||||
<string name="autostart_unavailable">Start on boot — unavailable (root required)</string>
|
||||
<string name="fatal_title">LedGrab failed to start</string>
|
||||
<string name="fatal_body_prefix">Python runtime initialization failed:</string>
|
||||
<string name="fatal_copy_log">Copy log</string>
|
||||
<string name="fatal_show_details">Show details</string>
|
||||
<string name="fatal_hide_details">Hide details</string>
|
||||
<string name="notification_channel_name">LedGrab capture</string>
|
||||
<string name="notification_channel_description">Shows while LedGrab is capturing the screen.</string>
|
||||
<string name="notification_title">LedGrab Running</string>
|
||||
<string name="notification_text">Web UI: %1$s</string>
|
||||
</resources>
|
||||
|
||||
@@ -12,6 +12,16 @@
|
||||
<item name="android:colorControlActivated">@color/teal_accent</item>
|
||||
</style>
|
||||
|
||||
<!-- Splash screen theme. Compatible across API levels via the
|
||||
androidx.core:core-splashscreen library. On API 31+ the system
|
||||
splash uses the foreground icon; on older versions the launch
|
||||
theme just paints the navy background, which is harmless. -->
|
||||
<style name="Theme.LedGrab.Splash" parent="Theme.SplashScreen">
|
||||
<item name="windowSplashScreenBackground">@color/bg_navy</item>
|
||||
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash</item>
|
||||
<item name="postSplashScreenTheme">@style/Theme.LedGrab</item>
|
||||
</style>
|
||||
|
||||
<style name="Widget.LedGrab.Button.Primary" parent="@android:style/Widget.Button">
|
||||
<item name="android:background">@drawable/bg_button_primary</item>
|
||||
<item name="android:textColor">@color/bg_navy</item>
|
||||
|
||||
@@ -1,8 +1,28 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
LedGrab communicates with WLED controllers, Home Assistant, and MQTT
|
||||
brokers on the local network via plain HTTP/UDP. Cleartext traffic
|
||||
must be allowed for these connections to work on Android 9+.
|
||||
LedGrab is a LAN-only app:
|
||||
- Inbound: web UI / API on the device (HTTP, port 8080)
|
||||
- Outbound: WLED HTTP/UDP, Home Assistant, MQTT brokers, mDNS
|
||||
|
||||
All of these are plaintext on the local network. Android's network
|
||||
security config doesn't support CIDR allowlists, so we cannot
|
||||
restrict cleartext to RFC1918 ranges declaratively — we have to
|
||||
permit cleartext base-wide.
|
||||
|
||||
Defence-in-depth that ACTUALLY mitigates this:
|
||||
1. Inbound: the FastAPI server in this app rejects non-loopback
|
||||
requests when no API key is configured (see ledgrab.api.auth).
|
||||
The Android launcher auto-generates an API key on first run
|
||||
(see ApiKeyManager.kt) and injects it via the
|
||||
LEDGRAB_AUTH__API_KEYS env var before uvicorn starts. The
|
||||
user's phone receives the key by scanning the QR, which
|
||||
embeds the key as a URL fragment (never logged server-side).
|
||||
2. Outbound: targets are validated by net_classify in the Python
|
||||
layer (LAN-only HTTP, SSRF-safe).
|
||||
|
||||
DO NOT remove the cleartext permission without first migrating
|
||||
every LAN peer to HTTPS — most WLED firmware, mDNS, and the LAN
|
||||
HTTP server itself rely on this flag.
|
||||
-->
|
||||
<network-security-config>
|
||||
<base-config cleartextTrafficPermitted="true" />
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Cross-compile pydantic-core for Android across all three ABIs:
|
||||
# arm64-v8a (primary — real TV hardware)
|
||||
# x86_64 (modern emulators)
|
||||
# x86 (legacy emulators)
|
||||
# Cross-compile pydantic-core for Android across all supported ABIs:
|
||||
# arm64-v8a (primary — modern TV hardware)
|
||||
# x86_64 (modern emulators)
|
||||
# x86 (legacy emulators)
|
||||
# armeabi-v7a (32-bit ARMv7 — older cheap TV boxes like X96 mini, MeCool)
|
||||
#
|
||||
# Outputs wheels into android/wheels/. Wheels are linked against the real
|
||||
# libpython3.11.so shipped by Chaquopy (stub .so does NOT work — see
|
||||
# memory/project_android_app.md for the incident notes).
|
||||
#
|
||||
# Prerequisites (on host):
|
||||
# - Rust + cargo (rustup) with targets: aarch64/x86_64/i686-linux-android
|
||||
# - Rust + cargo (rustup) with targets:
|
||||
# aarch64/x86_64/i686/armv7a-linux-android(eabi)
|
||||
# - Android NDK (ANDROID_NDK_HOME, or installed at Sdk/ndk/*)
|
||||
# - Python 3.11 (matches Chaquopy's embedded version)
|
||||
# - maturin (pip install maturin)
|
||||
@@ -19,9 +21,10 @@
|
||||
# core dependency version changes.
|
||||
#
|
||||
# Usage:
|
||||
# ./build-pydantic-core.sh # build all three ABIs
|
||||
# ./build-pydantic-core.sh arm64 # build a single ABI
|
||||
# ./build-pydantic-core.sh arm64 x86_64 # build a subset
|
||||
# ./build-pydantic-core.sh # build all 4 ABIs
|
||||
# ./build-pydantic-core.sh arm64 # build a single ABI
|
||||
# ./build-pydantic-core.sh arm64 x86_64 # build a subset
|
||||
# ./build-pydantic-core.sh armv7 # 32-bit ARM only
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
@@ -91,21 +94,23 @@ fi
|
||||
# ── ABI table ───────────────────────────────────────────────────────
|
||||
# Columns: short_name rust_target clang_prefix sysconfig_dir
|
||||
ABI_TABLE=(
|
||||
"arm64 aarch64-linux-android aarch64-linux-android${API_LEVEL} cross-sysconfig"
|
||||
"x86_64 x86_64-linux-android x86_64-linux-android${API_LEVEL} cross-sysconfig-x86_64"
|
||||
"x86 i686-linux-android i686-linux-android${API_LEVEL} cross-sysconfig-x86"
|
||||
"arm64 aarch64-linux-android aarch64-linux-android${API_LEVEL} cross-sysconfig"
|
||||
"x86_64 x86_64-linux-android x86_64-linux-android${API_LEVEL} cross-sysconfig-x86_64"
|
||||
"x86 i686-linux-android i686-linux-android${API_LEVEL} cross-sysconfig-x86"
|
||||
"armv7 armv7-linux-androideabi armv7a-linux-androideabi${API_LEVEL} cross-sysconfig-armv7"
|
||||
)
|
||||
|
||||
declare -A ABI_TAG_MAP=(
|
||||
[arm64]="arm64_v8a"
|
||||
[x86_64]="x86_64"
|
||||
[x86]="x86"
|
||||
[armv7]="armeabi_v7a"
|
||||
)
|
||||
|
||||
# ── Select which ABIs to build ──────────────────────────────────────
|
||||
SELECTED=("$@")
|
||||
if [ ${#SELECTED[@]} -eq 0 ]; then
|
||||
SELECTED=(arm64 x86_64 x86)
|
||||
SELECTED=(arm64 x86_64 x86 armv7)
|
||||
fi
|
||||
|
||||
# ── Ensure rust targets are installed ───────────────────────────────
|
||||
|
||||
@@ -6,6 +6,7 @@ inside an Android application. Sets up Android-specific paths
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
from typing import Any
|
||||
@@ -15,7 +16,7 @@ _server: Any | None = None # uvicorn.Server
|
||||
_loop: asyncio.AbstractEventLoop | None = None
|
||||
|
||||
|
||||
def start_server(data_dir: str, port: int = 8080) -> None:
|
||||
def start_server(data_dir: str, port: int = 8080, api_key: str | None = None) -> None:
|
||||
"""Start the LedGrab uvicorn server.
|
||||
|
||||
Called from Kotlin's ``PythonBridge.startServer()``. This function
|
||||
@@ -26,6 +27,11 @@ def start_server(data_dir: str, port: int = 8080) -> None:
|
||||
data_dir: Android app-private files directory
|
||||
(e.g. ``/data/data/com.ledgrab.android/files``).
|
||||
port: HTTP port for the web UI / API.
|
||||
api_key: Optional Bearer token to enable LAN auth. When set,
|
||||
published as ``LEDGRAB_AUTH__API_KEYS={"android":<key>}``
|
||||
so the server's auth gate accepts LAN requests carrying
|
||||
``Authorization: Bearer <key>``. When None, the server
|
||||
falls back to its default (loopback-only).
|
||||
"""
|
||||
# ── Configure paths before any LedGrab imports ──────────────
|
||||
os.makedirs(os.path.join(data_dir, "data"), exist_ok=True)
|
||||
@@ -41,6 +47,14 @@ def start_server(data_dir: str, port: int = 8080) -> None:
|
||||
os.environ["LEDGRAB_SERVER__HOST"] = "0.0.0.0"
|
||||
os.environ["LEDGRAB_SERVER__PORT"] = str(port)
|
||||
|
||||
# Provision LAN auth when the Kotlin launcher supplied a key. The
|
||||
# config layer (pydantic-settings) parses ``LEDGRAB_AUTH__API_KEYS``
|
||||
# as JSON when the value starts with `{`. We use a dict so the
|
||||
# rest of the codebase sees a labelled key just like the YAML
|
||||
# config form (api_keys: {android: ...}).
|
||||
if api_key:
|
||||
os.environ["LEDGRAB_AUTH__API_KEYS"] = json.dumps({"android": api_key})
|
||||
|
||||
# ── Now safe to import LedGrab ──────────────────────────────
|
||||
import uvicorn # noqa: E402
|
||||
|
||||
@@ -50,10 +64,25 @@ def start_server(data_dir: str, port: int = 8080) -> None:
|
||||
logger = get_logger(__name__)
|
||||
logger.info("LedGrab Android: starting server on port %d", port)
|
||||
logger.info("Data directory: %s", data_dir)
|
||||
if api_key:
|
||||
logger.info("LedGrab Android: API key auth enabled (label=android)")
|
||||
else:
|
||||
logger.warning("LedGrab Android: no API key — LAN requests will be rejected")
|
||||
|
||||
from ledgrab.config import get_config # noqa: E402
|
||||
|
||||
config = get_config()
|
||||
# Defensive: confirm the env var actually landed in the parsed config.
|
||||
# If pydantic-settings ever changes how it deserialises dict[str, str]
|
||||
# from env, the LAN auth would silently break (server would 401 every
|
||||
# phone scan). Logging the mismatch makes the failure mode obvious in
|
||||
# adb logcat.
|
||||
if api_key and config.auth.api_keys.get("android") != api_key:
|
||||
logger.error(
|
||||
"LedGrab Android: API key did NOT land in config — LAN auth will "
|
||||
"reject all requests. Check pydantic-settings dict parsing for "
|
||||
"LEDGRAB_AUTH__API_KEYS."
|
||||
)
|
||||
|
||||
uv_config = uvicorn.Config(
|
||||
"ledgrab.main:app",
|
||||
|
||||
@@ -244,6 +244,30 @@ import {
|
||||
initDonationBanner, dismissDonation, snoozeDonation, renderAboutPanel, setProjectUrls,
|
||||
} from './features/donation.ts';
|
||||
|
||||
// ─── Out-of-band API key delivery (URL fragment) ───
|
||||
|
||||
/**
|
||||
* Parse the URL fragment for an API key delivered out-of-band.
|
||||
* Supported forms:
|
||||
* #k=<token>
|
||||
* #key=<token>
|
||||
* #/some-route#k=<token> (key wins; route still applies)
|
||||
*
|
||||
* Returns null when no key is present or the value looks invalid
|
||||
* (whitespace / suspicious characters). Tokens are constrained to a
|
||||
* conservative URL-safe alphabet to avoid accepting arbitrary input
|
||||
* — the Android launcher uses 64-char hex.
|
||||
*/
|
||||
function readApiKeyFromFragment(): string | null {
|
||||
const hash = location.hash;
|
||||
if (!hash) return null;
|
||||
// Strip leading '#' once; search inside for either ?k= or &k= or
|
||||
// a leading k=, plus the `key=` long form.
|
||||
const body = hash.replace(/^#/, '');
|
||||
const match = body.match(/(?:^|[?&#])(?:k|key)=([A-Za-z0-9_-]{16,512})/);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
|
||||
// ─── Register all HTML onclick / onchange / onfocus globals ───
|
||||
|
||||
Object.assign(window, {
|
||||
@@ -724,6 +748,21 @@ window.addEventListener('beforeunload', () => {
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
try {
|
||||
// Bootstrap auth: first check the URL fragment for a key delivered
|
||||
// out-of-band (e.g. the Android TV launcher embeds the per-install
|
||||
// API key in the QR as ``#k=<token>``). Fragments are never sent in
|
||||
// HTTP requests so this is safe to log. Persist to localStorage and
|
||||
// strip the hash so a refresh doesn't keep showing it in the URL.
|
||||
const tokenFromFragment = readApiKeyFromFragment();
|
||||
if (tokenFromFragment) {
|
||||
localStorage.setItem('ledgrab_api_key', tokenFromFragment);
|
||||
try {
|
||||
history.replaceState(null, '', location.pathname + location.search);
|
||||
} catch {
|
||||
// Older browsers without history API support — leave the
|
||||
// fragment alone; it's already cached in localStorage.
|
||||
}
|
||||
}
|
||||
// Load API key from localStorage before anything that triggers API calls
|
||||
setApiKey(localStorage.getItem('ledgrab_api_key'));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user