Commit 4fe0b14b authored by akwizgran's avatar akwizgran

Log network connectiviy events.

parent a62d5f31
......@@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.briarproject.snooze">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
......
package org.briarproject.snooze;
interface Constants {
public interface Constants {
String ACTION_START = "org.briarproject.snooze.ACTION_START";
......
......@@ -16,7 +16,8 @@ import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import static org.briarproject.snooze.AlarmType.TIMER;
import org.briarproject.snooze.alarm.AlarmType;
import static org.briarproject.snooze.Constants.ACTION_START;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_INTERVAL_MS;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_TYPE;
......@@ -24,6 +25,7 @@ import static org.briarproject.snooze.Constants.EXTRA_FOREGROUND_SERVICE;
import static org.briarproject.snooze.Constants.EXTRA_KEEPALIVE_INTERVAL_MS;
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;
public class SettingsFragment extends Fragment {
......
......@@ -7,7 +7,6 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class SnoozeActivity extends AppCompatActivity {
......
package org.briarproject.snooze;
import android.app.Application;
import android.os.Build;
import android.os.PowerManager;
public class SnoozeApplication extends Application {
......
......@@ -3,29 +3,41 @@ package org.briarproject.snooze;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.PowerManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.content.Context.POWER_SERVICE;
import static android.content.Intent.ACTION_AIRPLANE_MODE_CHANGED;
import static android.content.Intent.ACTION_BATTERY_CHANGED;
import static android.content.Intent.ACTION_POWER_CONNECTED;
import static android.content.Intent.ACTION_POWER_DISCONNECTED;
import static android.content.Intent.ACTION_SCREEN_OFF;
import static android.content.Intent.ACTION_SCREEN_ON;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static android.net.ConnectivityManager.TYPE_WIFI;
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 {
class SnoozeBroadcastReceiver extends WakefulBroadcastReceiver {
IntentFilter getIntentFilter() {
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_SCREEN_ON);
filter.addAction(ACTION_SCREEN_OFF);
filter.addAction(ACTION_BATTERY_CHANGED);
filter.addAction(ACTION_POWER_CONNECTED);
filter.addAction(ACTION_POWER_DISCONNECTED);
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);
filter.addAction(ACTION_AIRPLANE_MODE_CHANGED);
filter.addAction(CONNECTIVITY_ACTION);
return filter;
}
......@@ -42,6 +54,10 @@ class PowerManagementEventReceiver extends WakefulBroadcastReceiver {
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 (ACTION_POWER_CONNECTED.equals(action)) {
log.log("Power connected");
} else if (ACTION_POWER_DISCONNECTED.equals(action)) {
log.log("Power disconnected");
} else if (Build.VERSION.SDK_INT >= 21
&& PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(action)) {
PowerManager pm = (PowerManager) ctx.getSystemService(POWER_SERVICE);
......@@ -50,6 +66,15 @@ class PowerManagementEventReceiver extends WakefulBroadcastReceiver {
&& PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(action)) {
PowerManager pm = (PowerManager) ctx.getSystemService(POWER_SERVICE);
log.log("Idle mode: " + pm.isDeviceIdleMode());
} else if (ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
log.log("Airplane mode: " + i.getBooleanExtra("state", false));
} else if (CONNECTIVITY_ACTION.equals(action)) {
ConnectivityManager cm =
(ConnectivityManager) ctx.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo net = cm.getActiveNetworkInfo();
boolean online = net != null && net.isConnected();
boolean wifi = net != null && net.getType() == TYPE_WIFI;
log.log("Online: " + online + ", wifi: " + wifi);
}
}
}
......@@ -9,12 +9,15 @@ import android.os.IBinder;
import android.os.PowerManager;
import android.support.v7.app.NotificationCompat;
import org.briarproject.snooze.alarm.Alarm;
import org.briarproject.snooze.alarm.AlarmFactory;
import org.briarproject.snooze.alarm.AlarmType;
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.EXTRA_ALARM_INTERVAL_MS;
......@@ -23,13 +26,14 @@ import static org.briarproject.snooze.Constants.EXTRA_FOREGROUND_SERVICE;
import static org.briarproject.snooze.Constants.EXTRA_KEEPALIVE_INTERVAL_MS;
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;
public class SnoozeService extends Service {
private static final String LOCK_TAG = SnoozeService.class.getName();
private final Binder binder = new Binder();
private final PowerManagementEventReceiver receiver = new PowerManagementEventReceiver();
private final SnoozeBroadcastReceiver receiver = new SnoozeBroadcastReceiver();
private boolean foreground = false;
private PowerManager.WakeLock wakeLock = null;
......
package org.briarproject.snooze;
package org.briarproject.snooze.alarm;
import android.content.Context;
import android.content.Intent;
import org.briarproject.snooze.SnoozeService;
import static org.briarproject.snooze.Constants.ACTION_ALARM;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_TYPE;
......
package org.briarproject.snooze;
package org.briarproject.snooze.alarm;
import java.util.concurrent.TimeUnit;
interface Alarm {
public interface Alarm {
AlarmType getAlarmType();
......
package org.briarproject.snooze;
package org.briarproject.snooze.alarm;
import android.content.Context;
class AlarmFactory {
public class AlarmFactory {
static Alarm createAlarm(AlarmType alarmType, Context appContext) {
public static Alarm createAlarm(AlarmType alarmType, Context appContext) {
switch (alarmType) {
case TIMER:
return new TimerAlarm(appContext);
......
package org.briarproject.snooze;
package org.briarproject.snooze.alarm;
enum AlarmType {
public enum AlarmType {
STOP,
TIMER,
......
package org.briarproject.snooze;
package org.briarproject.snooze.alarm;
import android.app.AlarmManager;
import android.app.PendingIntent;
......@@ -10,8 +10,6 @@ import java.util.concurrent.TimeUnit;
import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
import static android.content.Context.ALARM_SERVICE;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.briarproject.snooze.AlarmType.RTC;
import static org.briarproject.snooze.AlarmType.RTC_WAKEUP;
class AndroidAlarm extends AbstractAlarm {
......@@ -24,7 +22,7 @@ class AndroidAlarm extends AbstractAlarm {
@Override
public AlarmType getAlarmType() {
return wakeup ? RTC_WAKEUP : RTC;
return wakeup ? AlarmType.RTC_WAKEUP : AlarmType.RTC;
}
int getAndroidAlarmType() {
......
package org.briarproject.snooze;
package org.briarproject.snooze.alarm;
import android.app.AlarmManager.AlarmClockInfo;
import android.app.PendingIntent;
......@@ -7,8 +7,6 @@ import android.os.Build;
import java.util.concurrent.TimeUnit;
import static org.briarproject.snooze.AlarmType.ALARM_CLOCK;
class AndroidClockAlarm extends AndroidAlarm {
AndroidClockAlarm(Context appContext) {
......@@ -17,7 +15,7 @@ class AndroidClockAlarm extends AndroidAlarm {
@Override
public AlarmType getAlarmType() {
return ALARM_CLOCK;
return AlarmType.ALARM_CLOCK;
}
@Override
......
package org.briarproject.snooze;
package org.briarproject.snooze.alarm;
import android.app.PendingIntent;
import android.content.Context;
......@@ -6,9 +6,6 @@ import android.os.Build;
import java.util.concurrent.TimeUnit;
import static org.briarproject.snooze.AlarmType.RTC_WAKEUP_WHEN_IDLE;
import static org.briarproject.snooze.AlarmType.RTC_WHEN_IDLE;
class AndroidIdleAlarm extends AndroidAlarm {
AndroidIdleAlarm(Context appContext, boolean wakeup) {
......@@ -17,7 +14,7 @@ class AndroidIdleAlarm extends AndroidAlarm {
@Override
public AlarmType getAlarmType() {
return wakeup ? RTC_WAKEUP_WHEN_IDLE : RTC_WHEN_IDLE;
return wakeup ? AlarmType.RTC_WAKEUP_WHEN_IDLE : AlarmType.RTC_WHEN_IDLE;
}
@Override
......
package org.briarproject.snooze;
package org.briarproject.snooze.alarm;
import android.content.Context;
import android.support.annotation.NonNull;
......@@ -8,8 +8,6 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import static org.briarproject.snooze.AlarmType.SCHEDULED_EXECUTOR_SERVICE;
class ScheduledExecutorServiceAlarm extends AbstractAlarm {
private static final ThreadFactory THREAD_FACTORY = new ThreadFactory() {
......@@ -29,7 +27,7 @@ class ScheduledExecutorServiceAlarm extends AbstractAlarm {
@Override
public AlarmType getAlarmType() {
return SCHEDULED_EXECUTOR_SERVICE;
return AlarmType.SCHEDULED_EXECUTOR_SERVICE;
}
@Override
......
package org.briarproject.snooze;
package org.briarproject.snooze.alarm;
import android.content.Context;
......@@ -7,7 +7,6 @@ import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.briarproject.snooze.AlarmType.TIMER;
class TimerAlarm extends AbstractAlarm {
......@@ -19,7 +18,7 @@ class TimerAlarm extends AbstractAlarm {
@Override
public AlarmType getAlarmType() {
return TIMER;
return AlarmType.TIMER;
}
@Override
......
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