Skip to content

Instantly share code, notes, and snippets.

View LethalMaus's full-sized avatar

James Cullimore LethalMaus

View GitHub Profile
@LethalMaus
LethalMaus / article-bleftp-python-client-list-push-pull.py
Created March 19, 2026 19:47
BLE FTP Python client: list, push, and pull
from pathlib import Path, PurePosixPath
def normalize_remote_path(path: str | None) -> str:
if not path:
return ""
normalized = path.replace("\\", "/").strip("/")
if normalized in {"", "."}:
return ""
parts = [part for part in PurePosixPath(normalized).parts if part not in {"", "."}]
@LethalMaus
LethalMaus / article-bleftp-android-gatt-manager.kt
Created March 19, 2026 19:47
Android BLE file transfer manager with folder-based protocol
private val advertiseCallback = object : AdvertiseCallback() {
override fun onStartSuccess(settingsInEffect: AdvertiseSettings) {
pendingStart = false
log("Advertising started for service $SERVICE_UUID")
}
override fun onStartFailure(errorCode: Int) {
pendingStart = false
log("Advertising failed: ${advertiseFailureMessage(errorCode)}")
stopServer()
@LethalMaus
LethalMaus / article-bleftp-foreground-service.kt
Created March 19, 2026 19:47
Foreground BLE transfer service for Android
class BleTransferService : Service(), BleFileTransferManager.Listener {
inner class LocalBinder : Binder() {
fun getService(): BleTransferService = this@BleTransferService
}
private val binder = LocalBinder()
private lateinit var bleManager: BleFileTransferManager
private lateinit var prefs: SharedPreferences
private var statusText: String = ""
@LethalMaus
LethalMaus / strictmode-initializer.kt
Created March 13, 2026 20:11
StrictMode initializer snippet for Android app startup article
internal object StrictModeInitializer {
fun enableIfDebuggable(application: Application) {
val isDebuggable =
(application.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
if (!isDebuggable) return
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
@LethalMaus
LethalMaus / recomposition-overlay-test.kt
Created March 13, 2026 20:11
Recomposition overlay test snippet for Android app startup article
@RunWith(AndroidJUnit4::class)
class MainScreenRecompositionOverlayTest {
@get:Rule
val composeRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun overlay_expandShowsCounters_and_clearResetsRows() {
assumeTrue(BuildConfig.DEBUG)
@LethalMaus
LethalMaus / async-startup-display-settings.kt
Created March 13, 2026 20:11
Async display settings startup snippet for Android app startup article
LaunchedEffect(Unit) {
withFrameNanos { }
displaySettings = withContext(Dispatchers.IO) { readDisplaySettingsState() }
observeDisplaySettings = true
}
@LethalMaus
LethalMaus / bootreceiver-service-only.kt
Created March 13, 2026 20:11
Service-only boot receiver snippet for Android app startup article
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_BOOT_COMPLETED,
Intent.ACTION_LOCKED_BOOT_COMPLETED,
Intent.ACTION_MY_PACKAGE_REPLACED -> {
SystemEventService.start(context)
}
}
}
@LethalMaus
LethalMaus / manual-baseline-profile-fallback.sh
Created March 13, 2026 20:10
Manual baseline profile fallback snippet for Android app startup article
#!/usr/bin/env bash
set -euo pipefail
PKG="dev.jamescullimore.app"
ACTIVITY="dev.jamescullimore.app/.MainActivity"
RECEIVER="dev.jamescullimore.app/androidx.profileinstaller.ProfileInstallReceiver"
REMOTE_PROFILE="/data/misc/profman/${PKG}-primary.prof.txt"
run_scenario() {
local onboarding="$1"
@LethalMaus
LethalMaus / startup-macrobenchmark.kt
Created March 13, 2026 20:10
Startup macrobenchmark snippet for Android app startup article
package dev.jamescullimore.app.benchmark
import androidx.benchmark.macro.CompilationMode
import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.StartupTimingMetric
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import org.junit.Rule
import org.junit.Test
class StartupBenchmarks {
@LethalMaus
LethalMaus / recomposition-overlay.kt
Created March 13, 2026 20:10
Recomposition overlay snippet for Android app startup article
@Composable
private fun RecomposeDebugOverlay(modifier: Modifier = Modifier) {
if (!BuildConfig.DEBUG) return
val counts by RecomposeDebugTracker.counts.collectAsStateWithLifecycle()
var expanded by rememberSaveable { mutableStateOf(false) }
Surface(modifier = modifier.clickable { expanded = !expanded }) {
Column(modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp)) {
Text(text = "Recompose ${if (expanded) "hide" else "show"}")