From f2c50b500ed1804d467de5cfcca92f5d3301f503 Mon Sep 17 00:00:00 2001 From: Shannon Stork Date: Tue, 7 Aug 2018 10:38:57 -0400 Subject: [PATCH] Acquire wakelock when alarm goes off --- .../briar/android/SleepMonitor.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 briar-android/src/main/java/org/briarproject/briar/android/SleepMonitor.java diff --git a/briar-android/src/main/java/org/briarproject/briar/android/SleepMonitor.java b/briar-android/src/main/java/org/briarproject/briar/android/SleepMonitor.java new file mode 100644 index 000000000..df9e5cc75 --- /dev/null +++ b/briar-android/src/main/java/org/briarproject/briar/android/SleepMonitor.java @@ -0,0 +1,67 @@ +package org.briarproject.briar.android; + +import android.os.SystemClock; +import android.util.Log; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +import static java.util.Locale.US; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +public class SleepMonitor implements Runnable { + + /** + * How often to check the uptime and real time. + */ + private static final int INTERVAL_MS = 5000; + + /** + * If the difference between uptime and real time changes by more than this amount, assume deep + * sleep has occurred. + */ + private static final int MIN_SLEEP_DURATION_MS = 1000; + + private final ScheduledExecutorService executorService; + + private volatile long uptime, realtime, diff; + + SleepMonitor() { + uptime = SystemClock.uptimeMillis(); + realtime = SystemClock.elapsedRealtime(); + diff = realtime - uptime; + executorService = Executors.newSingleThreadScheduledExecutor(); + } + + void start() { + executorService.scheduleAtFixedRate(this, 0, INTERVAL_MS, MILLISECONDS); + } + + @Override + public void run() { + long sleepDuration = getSleepDuration(); + if (sleepDuration > MIN_SLEEP_DURATION_MS) { + String start = getTime(System.currentTimeMillis() - sleepDuration); + Log.i("SLEEP_INFO", "System slept for " + sleepDuration + " ms (since " + start + ")"); + } + } + + /** + * Returns the amount of time spent in deep sleep since the last check. + */ + private long getSleepDuration() { + uptime = SystemClock.uptimeMillis(); + realtime = SystemClock.elapsedRealtime(); + long lastDiff = diff; + diff = realtime - uptime; + return diff - lastDiff; + } + + private String getTime(long time) { + DateFormat sdf = new SimpleDateFormat("HH:mm:ss", US); + return sdf.format(new Date(time)); + } +}