Skip to content
Snippets Groups Projects
Commit dcfec131 authored by Shannon Stork's avatar Shannon Stork Committed by akwizgran
Browse files

Add SleepMonitor to detect deep sleep.

parent 0ce3e850
No related branches found
No related tags found
1 merge request!3Add SleepMonitor to detect deep sleep
package org.briarproject.snooze;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
......@@ -23,11 +26,11 @@ import static org.briarproject.snooze.Constants.ACTION_START;
import static org.briarproject.snooze.Constants.ACTION_STOP;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_INTERVAL_MS;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_TYPE;
import static org.briarproject.snooze.Constants.EXTRA_IP_ADDRESS;
import static org.briarproject.snooze.Constants.EXTRA_PORT_NUMBER;
import static org.briarproject.snooze.Constants.EXTRA_FOREGROUND_SERVICE;
import static org.briarproject.snooze.Constants.EXTRA_IP_ADDRESS;
import static org.briarproject.snooze.Constants.EXTRA_KEEPALIVE_BYTES;
import static org.briarproject.snooze.Constants.EXTRA_KEEPALIVE_INTERVAL_MS;
import static org.briarproject.snooze.Constants.EXTRA_PORT_NUMBER;
import static org.briarproject.snooze.Constants.EXTRA_WAKE_LOCK;
import static org.briarproject.snooze.Constants.EXTRA_WIFI_LOCK;
import static org.briarproject.snooze.alarm.AlarmType.TIMER;
......@@ -120,13 +123,30 @@ public class SettingsFragment extends Fragment {
onClickStopServiceButton();
}
});
layout.findViewById(R.id.ipEntry).setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
layout.findViewById(R.id.portEntry).setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
return layout;
}
private void onClickApplySettingsButton() {
View layout = getView();
String ipAddress = ((EditText) layout.findViewById(R.id.ipEntry)).getText().toString();
int portNumber = Integer.parseInt(((EditText) layout.findViewById(R.id.portEntry)).getText().toString());
String portString = ((EditText) layout.findViewById(R.id.portEntry)).getText().toString();
int portNumber = Integer.parseInt(portString);
boolean foreground =
((Switch) layout.findViewById(R.id.foregroundServiceSwitch)).isChecked();
boolean wakeLock = ((Switch) layout.findViewById(R.id.wakeLockSwitch)).isChecked();
......@@ -156,4 +176,10 @@ public class SettingsFragment extends Fragment {
i.setAction(ACTION_STOP);
getContext().startService(i);
}
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager)
getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
package org.briarproject.snooze;
import android.os.SystemClock;
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;
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 Logger log;
private final ScheduledExecutorService executorService;
private volatile long uptime, realtime, diff;
SleepMonitor(Logger log) {
this.log = log;
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.log("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));
}
}
......@@ -11,6 +11,7 @@ public class SnoozeApplication extends Application {
super.onCreate();
log = new Logger(this, getString(R.string.app_name));
log.log("Application created");
new SleepMonitor(log).start();
}
Logger getLogger() {
......
......@@ -25,11 +25,11 @@ import static org.briarproject.snooze.Constants.ACTION_START;
import static org.briarproject.snooze.Constants.ACTION_STOP;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_INTERVAL_MS;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_TYPE;
import static org.briarproject.snooze.Constants.EXTRA_IP_ADDRESS;
import static org.briarproject.snooze.Constants.EXTRA_PORT_NUMBER;
import static org.briarproject.snooze.Constants.EXTRA_FOREGROUND_SERVICE;
import static org.briarproject.snooze.Constants.EXTRA_IP_ADDRESS;
import static org.briarproject.snooze.Constants.EXTRA_KEEPALIVE_BYTES;
import static org.briarproject.snooze.Constants.EXTRA_KEEPALIVE_INTERVAL_MS;
import static org.briarproject.snooze.Constants.EXTRA_PORT_NUMBER;
import static org.briarproject.snooze.Constants.EXTRA_WAKE_LOCK;
import static org.briarproject.snooze.Constants.EXTRA_WIFI_LOCK;
import static org.briarproject.snooze.alarm.AlarmType.STOP;
......@@ -46,9 +46,8 @@ public class SnoozeService extends Service {
private WifiManager.WifiLock wifiLock = null;
private NetworkClient networkClient = null;
private Alarm alarm = null;
private int alarmIntervalMs, keepaliveIntervalMs, keepaliveBytes;
private String host;
private int port;
private int alarmIntervalMs, keepaliveIntervalMs, keepaliveBytes, port;
private String host = null;
private volatile Logger log = null;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment