Commit a942d4f4 authored by akwizgran's avatar akwizgran

Log power management events.

parent c26038ba
......@@ -21,7 +21,7 @@ class AlarmFactory {
case ALARM_CLOCK:
return new AndroidClockAlarm(appContext);
default:
throw new UnsupportedOperationException();
throw new IllegalArgumentException();
}
}
}
......@@ -2,6 +2,7 @@ package org.briarproject.snooze;
enum AlarmType {
STOP,
TIMER,
SCHEDULED_EXECUTOR_SERVICE,
RTC,
......
......@@ -10,6 +10,8 @@ interface Constants {
String EXTRA_WAKE_LOCK = "org.briarproject.snooze.EXTRA_WAKE_LOCK";
String EXTRA_WIFI_LOCK = "org.briarproject.snooze.EXTRA_WIFI_LOCK";
String EXTRA_ALARM_TYPE = "org.briarproject.snooze.EXTRA_ALARM_TYPE";
int ALARM_INTERVAL_MS = 5 * 1000;
......
package org.briarproject.snooze;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.PowerManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import static android.content.Context.POWER_SERVICE;
import static android.content.Intent.ACTION_BATTERY_CHANGED;
import static android.content.Intent.ACTION_SCREEN_OFF;
import static android.content.Intent.ACTION_SCREEN_ON;
import static android.os.BatteryManager.EXTRA_LEVEL;
import static android.os.BatteryManager.EXTRA_PLUGGED;
import static android.os.BatteryManager.EXTRA_SCALE;
class PowerManagementEventReceiver extends WakefulBroadcastReceiver {
IntentFilter getIntentFilter() {
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_SCREEN_ON);
filter.addAction(ACTION_SCREEN_OFF);
filter.addAction(ACTION_BATTERY_CHANGED);
if (Build.VERSION.SDK_INT >= 21)
filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
if (Build.VERSION.SDK_INT >= 23)
filter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
return filter;
}
@Override
public void onReceive(Context ctx, Intent i) {
Logger log = ((SnoozeApplication) ctx.getApplicationContext()).getLogger();
String action = i.getAction();
if (ACTION_SCREEN_ON.equals(action)) {
log.log("Screen on");
} else if (ACTION_SCREEN_OFF.equals(action)) {
log.log("Screen off");
} else if (ACTION_BATTERY_CHANGED.equals(action)) {
int level = i.getIntExtra(EXTRA_LEVEL, -1);
int scale = i.getIntExtra(EXTRA_SCALE, -1);
int plugged = i.getIntExtra(EXTRA_PLUGGED, -1);
log.log("Battery level: " + (level / (float) scale) + ", plugged: " + (plugged != 0));
} else if (Build.VERSION.SDK_INT >= 21
&& PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(action)) {
PowerManager pm = (PowerManager) ctx.getSystemService(POWER_SERVICE);
log.log("Power save mode: " + pm.isPowerSaveMode());
} else if (Build.VERSION.SDK_INT >= 23
&& PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(action)) {
PowerManager pm = (PowerManager) ctx.getSystemService(POWER_SERVICE);
log.log("Idle mode: " + pm.isDeviceIdleMode());
}
}
}
......@@ -20,6 +20,7 @@ import static org.briarproject.snooze.Constants.ACTION_START;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_TYPE;
import static org.briarproject.snooze.Constants.EXTRA_FOREGROUND_SERVICE;
import static org.briarproject.snooze.Constants.EXTRA_WAKE_LOCK;
import static org.briarproject.snooze.Constants.EXTRA_WIFI_LOCK;
public class SnoozeActivity extends AppCompatActivity implements LogListener,
OnItemSelectedListener {
......@@ -68,10 +69,12 @@ public class SnoozeActivity extends AppCompatActivity implements LogListener,
public void onClickStartServiceButton(View v) {
boolean foreground = ((Switch) findViewById(R.id.foregroundServiceSwitch)).isChecked();
boolean wakeLock = ((Switch) findViewById(R.id.permanentWakeLockSwitch)).isChecked();
boolean wifiLock = ((Switch) findViewById(R.id.wifiLockSwitch)).isChecked();
Intent i = new Intent(this, SnoozeService.class);
i.setAction(ACTION_START);
i.putExtra(EXTRA_FOREGROUND_SERVICE, foreground);
i.putExtra(EXTRA_WAKE_LOCK, wakeLock);
i.putExtra(EXTRA_WIFI_LOCK, wifiLock);
i.putExtra(EXTRA_ALARM_TYPE, alarmType.name());
startService(i);
}
......
package org.briarproject.snooze;
import android.app.Application;
import android.os.Build;
import android.os.PowerManager;
public class SnoozeApplication extends Application {
......@@ -11,6 +13,16 @@ public class SnoozeApplication extends Application {
super.onCreate();
log = new Logger(this, SnoozeApplication.class.getSimpleName());
log.log("Application created");
log.log("Manufacturer: " + Build.MANUFACTURER);
log.log("Model: " + Build.MODEL);
log.log("Device: " + Build.DEVICE);
log.log("Product: " + Build.PRODUCT);
log.log("API version: " + Build.VERSION.SDK_INT);
if (Build.VERSION.SDK_INT >= 23) {
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
boolean ignoring = pm.isIgnoringBatteryOptimizations(getPackageName());
log.log("Ignoring battery optimizations: " + ignoring);
}
}
Logger getLogger() {
......
......@@ -3,6 +3,7 @@ package org.briarproject.snooze;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.IBinder;
import android.os.PowerManager;
......@@ -10,21 +11,28 @@ import android.support.v7.app.NotificationCompat;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.net.wifi.WifiManager.WIFI_MODE_FULL;
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.briarproject.snooze.AlarmType.STOP;
import static org.briarproject.snooze.Constants.ACTION_ALARM;
import static org.briarproject.snooze.Constants.ACTION_START;
import static org.briarproject.snooze.Constants.ALARM_INTERVAL_MS;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_TYPE;
import static org.briarproject.snooze.Constants.EXTRA_FOREGROUND_SERVICE;
import static org.briarproject.snooze.Constants.EXTRA_WAKE_LOCK;
import static org.briarproject.snooze.Constants.EXTRA_WIFI_LOCK;
public class SnoozeService extends Service {
private static final String TAG = SnoozeService.class.getName();
private final Binder binder = new Binder();
private final PowerManagementEventReceiver receiver = new PowerManagementEventReceiver();
private boolean foreground = false;
private PowerManager.WakeLock wakeLock = null;
private WifiManager.WifiLock wifiLock = null;
private Alarm alarm = null;
private volatile Logger log = null;
......@@ -39,6 +47,7 @@ public class SnoozeService extends Service {
super.onCreate();
log = ((SnoozeApplication) getApplication()).getLogger();
log.log("Service created");
registerReceiver(receiver, receiver.getIntentFilter());
}
@Override
......@@ -46,12 +55,15 @@ public class SnoozeService extends Service {
if (ACTION_START.equals(i.getAction())) {
boolean newForeground = i.getBooleanExtra(EXTRA_FOREGROUND_SERVICE, false);
boolean newWakeLock = i.getBooleanExtra(EXTRA_WAKE_LOCK, false);
boolean newWifiLock = i.getBooleanExtra(EXTRA_WIFI_LOCK, false);
AlarmType newAlarmType = AlarmType.valueOf(i.getStringExtra(EXTRA_ALARM_TYPE));
log.log("Setting alarm " + newAlarmType);
if (newForeground && !foreground) startForegroundService();
else if (!newForeground && foreground) stopForegroundService();
if (newWakeLock && wakeLock == null) acquireWakeLock();
else if (!newWakeLock && wakeLock != null) releaseWakeLock();
if (newWifiLock && wifiLock == null) acquireWifiLock();
else if (!newWifiLock && wifiLock != null) releaseWifiLock();
setAlarm(newAlarmType);
} else if (ACTION_ALARM.equals(i.getAction())) {
AlarmType alarmType = AlarmType.valueOf(i.getStringExtra(EXTRA_ALARM_TYPE));
......@@ -71,6 +83,7 @@ public class SnoozeService extends Service {
super.onDestroy();
log.log("Service destroyed");
if (foreground) stopForegroundService();
unregisterReceiver(receiver);
if (wakeLock != null) releaseWakeLock();
}
......@@ -106,9 +119,8 @@ public class SnoozeService extends Service {
private void acquireWakeLock() {
if (wakeLock != null) throw new IllegalStateException();
log.log("Acquiring wake lock");
Object o = getSystemService(POWER_SERVICE);
PowerManager pm = (PowerManager) o;
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, SnoozeService.class.getName());
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, TAG);
wakeLock.setReferenceCounted(false);
wakeLock.acquire();
}
......@@ -120,10 +132,28 @@ public class SnoozeService extends Service {
wakeLock = null;
}
private void setAlarm(AlarmType newAlarmType) {
private void acquireWifiLock() {
if (wifiLock != null) throw new IllegalStateException();
log.log("Acquiring wifi lock");
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
wifiLock = wm.createWifiLock(WIFI_MODE_FULL, TAG);
wifiLock.setReferenceCounted(false);
wifiLock.acquire();
}
private void releaseWifiLock() {
if (wifiLock == null) throw new IllegalStateException();
log.log("Releasing wifi lock");
wifiLock.release();
wifiLock = null;
}
private void setAlarm(AlarmType alarmType) {
if (alarm != null) alarm.cancelAlarm();
alarm = AlarmFactory.createAlarm(newAlarmType, getApplicationContext());
setAlarm();
if (alarmType != STOP) {
alarm = AlarmFactory.createAlarm(alarmType, getApplicationContext());
setAlarm();
}
}
private void setAlarm() {
......
......@@ -20,6 +20,12 @@
android:layout_height="wrap_content"
android:text="@string/use_permanent_wake_lock"/>
<Switch
android:id="@+id/wifiLockSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/use_wifi_lock"/>
<Spinner
android:id="@+id/alarmTypeSpinner"
android:layout_width="match_parent"
......
<resources>
<string-array name="alarm_types">
<item>Timer</item>
<item>ScheduledExecutorService</item>
<item>RTC</item>
<item>RTC wakeup</item>
<item>Alarm clock</item>
<item>No alarm</item>
<item>Java Timer</item>
<item>Java ScheduledExecutorService</item>
<item>Android alarm</item>
<item>Android wakeup alarm</item>
<item>Android alarm clock</item>
</string-array>
</resources>
<resources>
<string-array name="alarm_types">
<item>Timer</item>
<item>ScheduledExecutorService</item>
<item>RTC</item>
<item>RTC wakeup</item>
<item>Alarm clock</item>
<item>RTC allow when idle</item>
<item>RTC wakeup allow when idle</item>
<item>No alarm</item>
<item>Java Timer</item>
<item>Java ScheduledExecutorService</item>
<item>Android alarm</item>
<item>Android wakeup alarm</item>
<item>Android alarm clock</item>
<item>Android alarm, allow when idle</item>
<item>Android wakeup alarm, allow when idle</item>
</string-array>
</resources>
......@@ -5,10 +5,12 @@
<string name="use_permanent_wake_lock">Permanent wake lock</string>
<string name="use_foreground_service">Foreground service</string>
<string name="apply_settings">Apply settings</string>
<string name="use_wifi_lock">Wi-Fi lock</string>
<string-array name="alarm_types">
<item>Timer</item>
<item>ScheduledExecutorService</item>
<item>RTC</item>
<item>RTC wakeup</item>
<item>No alarm</item>
<item>Java Timer</item>
<item>Java ScheduledExecutorService</item>
<item>Android alarm</item>
<item>Android wakeup alarm</item>
</string-array>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment