Android battery

A buddy that kills your battery gets turned off. Every app change must keep the phone asleep as much as it can.

What the app does today

Code is in android/app/src/main/java/app/assistant/.

PartHow it runsBattery care
Sync (Alarms.ensurePeriodic)One alarm about every 15 min, setAndAllowWhileIdle, re-armed each timeOne tiny request. Inexact, so Android can group it with other wakes
Reminders and alarms (Alarms.kt)setAlarmClock for ringing alarms, setExactAndAllowWhileIdle for the restExact only when the user set a time. Falls back to inexact without the permission
Location (LocationService)Foreground service, a point about every 5 minBalanced accuracy (Wi-Fi and cell, not GPS). Slows to every 15 min after 3 points within 150 m (SLOW_MS). System may hold points up to 15 min. Points sent together at most every 15 min
Geofences (Fences.kt)System geofences for named placesThe system watches; the app sleeps. The 5 min points are the backup
Live notices (NoticeStream)An open event stream while location is onRides on the location service, so no extra service. Backs off on errors
Listening places (PlaceWatch)Runs after each location uploadOne small request; no own timer. Shows a tap-to-start notice at a chosen place
Notifications (NotifyListener)System-bound listener; the system wakes itSends only watched apps. Asks the server for the watch list on connect
Detected items (Detect)Runs inside NotifyListener on the phoneSends one small item to /v1/detected, skips repeats (last 50)
Health (HealthSync)Sync when asked, at most every 10 minSkips if it ran recently
Listening (CaptureService)Foreground service, mic onSends text in batches every 15 s or 5 lines. Waits 60 s when the server says no
Wake word (WakeService)Foreground service, speech recognizer onCostly: no low-power wake chip is open to apps. Off by default. Pauses while another app has the mic, then retries after 45 s or 3 min
Ringing (RingService)Foreground service with a wake lockWake lock has a timeout (ring time + 5 s)

The app asks to skip battery optimisation (REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) so alarms and location arrive on time.

Rules for new app code

  1. Do not add a service. Ride on one that runs, or use a scheduled wake.
  2. Batch network calls. Queue, then send together. Never one request per point or event.
  3. Inexact first. Use exact alarms only for a time the user chose.
  4. Every wake lock has a timeout. Always acquire(ms), never acquire().
  5. Balanced location. No GPS-only or high-accuracy requests unless the user is moving and asked for it.
  6. Back off on errors. Wait longer after each failure. Stop on 401 or 409.
  7. Do nothing when off. A feature switched off must stop its timers and services.
  8. Say the cost. A feature that costs battery says so on its switch, as Location does.

Gaps

How to measure

  1. Charge the phone full. Turn on only the feature you test.
  2. Leave it a set time, like 8 hours, screen off.
  3. Read the app's use in Settings > Battery > App battery use.
  4. For detail, take a bug report and open it in Battery Historian:
adb shell dumpsys batterystats --reset
# use the phone for a while
adb bugreport bugreport.zip
  1. Check wakes and alarms: adb shell dumpsys alarm | grep app.assistant.
  2. Write the before and after numbers in the change notes.

Planned features that send more data, like the Full activity log, must follow these rules and batch their uploads.