Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apps/desktop/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion apps/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ windows-sys = { version = "0.59", features = ["Win32_Storage_FileSystem", "Win32
rustls-platform-verifier = "0.7"
jni = "0.22"
android-native-keyring-store = "1.0.0"
ndk-context = "0.1.1"

# The updater is desktop-only upstream, and this crate also builds for Android.
# Gating it here rather than in `[dependencies]` keeps the mobile build from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,11 @@ class MainActivity : TauriActivity() {
*/
private external fun initRustlsPlatformVerifier(context: Context)

/**
* Publishes the JavaVM and app Context through `ndk-context`.
*
* Crates that expect to find them there get nothing under Tauri, which
* initialises `ndk-context` nowhere. The credential store is one such crate,
* so without this call saving a sign-in fails on Android.
*
* Implemented in `src-tauri/src/android_context.rs`.
*/
private external fun initNdkContext(context: Context)

override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()

// Order matters here, and it is not the obvious one.
//
// `super.onCreate` is what starts the Rust side, and `run()` registers the
// credential store immediately — whose constructor reads the app Context
// out of `ndk-context`. So the Context has to be published *before* the
// call to super, or the app aborts on a panic from deep inside a crate
// that has no idea Tauri exists.
//
// loadLibrary has to come first in turn, so the symbols resolve. It is
// idempotent, so calling it ahead of the Tauri/Wry setup costs nothing.
System.loadLibrary("thinkbrain_notes_desktop_lib")
initNdkContext(applicationContext)

// Tauri/tao publishes the app Context through `ndk-context` itself before
// starting Rust inside `super.onCreate`, so no bootstrap duplication here.
super.onCreate(savedInstanceState)

// TLS can wait until after: nothing opens an HTTPS connection during
Expand Down
71 changes: 0 additions & 71 deletions apps/desktop/src-tauri/src/android_context.rs

This file was deleted.

8 changes: 4 additions & 4 deletions apps/desktop/src-tauri/src/android_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
//! "Expect rustls-platform-verifier to be initialized" — which is exactly what
//! cloning a repository on a device did before this module existed.
//!
//! Nothing hands it those pointers for us. The usual route, `ndk-context`, is
//! not an option here: neither `tao` nor `wry` initialise it, and the crate is
//! not in the dependency tree at all. So the handles come from Java —
//! `MainActivity.onCreate` calls the native method exported below.
//! `tao` publishes the app `Context` through `ndk-context` before Rust
//! startup, but `rustls-platform-verifier` additionally needs its own
//! `init_with_env` hook — so the handles still come from Java, via the
//! `MainActivity.onCreate` call to the native method exported below.
//!
//! Two details worth keeping in mind if this ever needs touching:
//!
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop/src-tauri/src/credential_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ fn platform_store() -> Option<keyring_core::Result<std::sync::Arc<keyring_core::

// Secrets go into SharedPreferences encrypted under a dedicated Android
// Keystore entry, so this app never writes or owns the encryption. The store
// reads the app `Context` from `ndk-context`, which Tauri populates nowhere —
// `android_context.rs` must have run first, and does, from `MainActivity`.
// reads the app `Context` from `ndk-context`, which Tauri/tao populates
// before Rust startup inside `MainActivity.onCreate`.
#[cfg(target_os = "android")]
fn platform_store() -> Option<keyring_core::Result<std::sync::Arc<keyring_core::CredentialStore>>> {
Some(
Expand Down
2 changes: 0 additions & 2 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//! Main entry point for the Tauri desktop application, registering application state,
//! plugins, and command handlers.

#[cfg(target_os = "android")]
mod android_context;
#[cfg(target_os = "android")]
mod android_tls;
mod commands;
Expand Down
2 changes: 1 addition & 1 deletion knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"ignore": [".agents/**", "examples/**"],
"workspaces": {
".": {
"ignoreBinaries": ["rustfmt", "where.exe"]
"ignoreBinaries": ["adb", "rustfmt", "where.exe"]
},
"apps/desktop": {
"entry": ["index.html", "demo/main.tsx", "e2e/**/*.spec.ts"],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"android:dev": "pnpm desktop:tauri android dev",
"android:build": "pnpm desktop:tauri android build --debug",
"android:build:release": "pnpm desktop:tauri android build",
"android:smoke": "node scripts/android-startup-smoke.mjs",
"prepare": "husky",
"knip": "knip"
},
Expand Down
112 changes: 112 additions & 0 deletions scripts/android-startup-smoke.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env node
/**
* android-startup-smoke — cold-launch smoke test for the installed ThinkBrain
* Notes Android app.
*
* Tests the INSTALLED package only: never installs, uninstalls, clears app
* data, or touches log buffers.
*
* Usage:
* node scripts/android-startup-smoke.mjs (or `pnpm android:smoke`)
*
* Environment:
* ANDROID_SERIAL device/emulator serial (default: adb picks the single
* connected device; set when more than one is attached)
*
* Exits 0 when two consecutive cold launches each yield a stable process for
* 10s; exits 1 immediately with the failing launch's evidence otherwise.
*/
import { execFileSync } from "node:child_process";
import { setTimeout as delay } from "node:timers/promises";

const PKG = "com.thinkbrain.notes";
const ACTIVITY = `${PKG}/.MainActivity`;
const LAUNCHES = 2;
const STABLE_SECONDS = 10;
const LOG_TAIL = 300;
const ADB_TIMEOUT_MS = 30_000;

const deviceArgs = process.env.ANDROID_SERIAL
? ["-s", process.env.ANDROID_SERIAL]
: [];

function adb(...args) {
try {
return execFileSync("adb", [...deviceArgs, ...args], {
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
timeout: ADB_TIMEOUT_MS,
}).trim();
} catch (err) {
// `pidof` exits 1 when the process is absent — that is data, not failure.
// Anything else (device offline, signal, timeout, adb itself missing) is
// an execution error and must not be masked as "no process".
if (
args[0] === "shell" &&
args[1] === "pidof" &&
err.status === 1 &&
!err.signal &&
!err.stderr?.trim()
) {
return "";
}
throw new Error(`adb ${args.join(" ")} failed: ${err.message}`, {
cause: err,
});
}
}

function logcatTail() {
try {
return adb("logcat", "-d", "-t", String(LOG_TAIL));
} catch (err) {
return `(logcat retrieval failed: ${err.message})`;
}
}

function fail(launch, reason, amOutput, pidHistory) {
console.error(
`FAIL launch ${launch}/${LAUNCHES}: ${reason}\n` +
`am output: ${amOutput}\n` +
`pid history: ${pidHistory.join(", ") || "(none)"}\n` +
`--- logcat tail ---\n${logcatTail()}`,
);
process.exit(1);
}

for (let launch = 1; launch <= LAUNCHES; launch++) {
adb("shell", "am", "force-stop", PKG);
const amOutput = adb("shell", "am", "start", "-W", "-n", ACTIVITY);
if (/^Error\b/m.test(amOutput)) {
fail(launch, "am start reported an error", amOutput, []);
}

// Sample the PID now, then once per second for STABLE_SECONDS — including
// the final interval — so a crash at any point is caught.
const pidHistory = [adb("shell", "pidof", PKG) || "DEAD"];
const firstPid = pidHistory[0];
let dead = firstPid === "DEAD";
for (let s = 0; s < STABLE_SECONDS && !dead; s++) {
await delay(1000);
const pid = adb("shell", "pidof", PKG);
pidHistory.push(pid || "DEAD");
if (!pid || pid !== firstPid) {
dead = true;
}
}
if (dead) {
fail(
launch,
firstPid === "DEAD"
? "process never started"
: "process died or restarted",
amOutput,
pidHistory,
);
}
console.log(
`launch ${launch}/${LAUNCHES}: pid ${firstPid} stable ${STABLE_SECONDS}s`,
);
}

console.log("PASS");
Loading