Skip to content

Commit

Permalink
move alarm handler to app service
Browse files Browse the repository at this point in the history
start service at boot time
  • Loading branch information
wuan committed Apr 25, 2014
1 parent ab47088 commit 6fa962b
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 254 deletions.
11 changes: 11 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<uses-sdk
android:minSdkVersion="14"
Expand All @@ -32,6 +33,16 @@
<activity
android:name=".Preferences"
android:label="@string/preferences"/>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="org.blitzortung.android.app" />
</intent-filter>
</receiver>
<!--<receiver android:name=".WidgetProvider">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
Expand Down
2 changes: 1 addition & 1 deletion doc/README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
upcoming features:

* animation
* background task and close activity alarm
* background task and close context alarm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.util.Log;
import org.blitzortung.android.alarm.factory.AlarmObjectFactory;
import org.blitzortung.android.alarm.handler.AlarmStatusHandler;
import org.blitzortung.android.alarm.listener.AlertListener;
import org.blitzortung.android.alarm.object.AlarmSector;
import org.blitzortung.android.alarm.object.AlarmStatus;
import org.blitzortung.android.app.Main;
Expand All @@ -25,7 +26,7 @@
import java.util.HashSet;
import java.util.Set;

public class AlarmManager implements OnSharedPreferenceChangeListener, LocationHandler.Listener {
public class AlertManager implements OnSharedPreferenceChangeListener, LocationHandler.Listener {

private final Vibrator vibrator;
private final NotificationHandler notificationHandler;
Expand All @@ -39,12 +40,6 @@ public void updateContext(Context context) {
alarmParameters.updateSectorLabels(context);
}

public interface AlarmListener {
void onAlarmResult(AlarmResult alarmResult);

void onAlarmClear();
}

private final AlarmParameters alarmParameters;

private Location location;
Expand All @@ -53,8 +48,7 @@ public interface AlarmListener {

private boolean alarmValid;

// VisibleForTesting
protected final Set<AlarmListener> alarmListeners;
private AlertListener alertListener;

private final AlarmStatus alarmStatus;

Expand All @@ -70,7 +64,7 @@ public interface AlarmListener {

private long signalingLastTimestamp;

public AlarmManager(LocationHandler locationHandler, SharedPreferences preferences, Context context, Vibrator vibrator, NotificationHandler notificationHandler, AlarmObjectFactory alarmObjectFactory, AlarmParameters alarmParameters) {
public AlertManager(LocationHandler locationHandler, SharedPreferences preferences, Context context, Vibrator vibrator, NotificationHandler notificationHandler, AlarmObjectFactory alarmObjectFactory, AlarmParameters alarmParameters) {
this.locationHandler = locationHandler;
this.context = context;
this.vibrator = vibrator;
Expand All @@ -80,8 +74,6 @@ public AlarmManager(LocationHandler locationHandler, SharedPreferences preferenc
this.alarmParameters = alarmParameters;


alarmListeners = new HashSet<AlarmListener>();

preferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(preferences, PreferenceKey.ALARM_ENABLED);
onSharedPreferenceChanged(preferences, PreferenceKey.MEASUREMENT_UNIT);
Expand Down Expand Up @@ -140,16 +132,16 @@ private void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Pref
public void onLocationChanged(Location location) {
this.location = location;

checkStrokes(lastStrokes, true);
checkStrokes(lastStrokes);
}

public boolean isAlarmEnabled() {
return alarmEnabled;
}

public void checkStrokes(Collection<? extends Stroke> strokes, boolean strokesAreInRealtime) {
boolean currentAlarmIsValid = isAlarmEnabled() && location != null && strokes != null && strokesAreInRealtime;
lastStrokes = strokesAreInRealtime ? strokes : null;
public void checkStrokes(Collection<? extends Stroke> strokes) {
boolean currentAlarmIsValid = isAlarmEnabled() && location != null && strokes != null;
lastStrokes = strokes;

if (currentAlarmIsValid) {
alarmValid = true;
Expand All @@ -168,20 +160,8 @@ public String getTextMessage(float notificationDistanceLimit) {
return alarmStatusHandler.getTextMessage(alarmStatus, notificationDistanceLimit);
}

public void addAlarmListener(AlarmListener alarmListener) {
alarmListeners.add(alarmListener);
}

public Set<AlarmListener> getAlarmListeners() {
return alarmListeners;
}

public void clearAlarmListeners() {
alarmListeners.clear();
}

public void removeAlarmListener(AlarmListener alarmView) {
alarmListeners.remove(alarmView);
public void setAlertListener(AlertListener alertListener) {
this.alertListener = alertListener;
}

public Collection<AlarmSector> getAlarmSectors() {
Expand Down Expand Up @@ -212,25 +192,25 @@ private void invalidateAlarm() {
}

private void broadcastClear() {
for (AlarmListener alarmListener : alarmListeners) {
alarmListener.onAlarmClear();
if (alertListener != null) {
alertListener.onAlertCancel();
}
}

private void broadcastResult(AlarmResult alarmResult) {
for (AlarmListener alarmListener : alarmListeners) {
alarmListener.onAlarmResult(alarmResult);
if (alertListener != null) {
alertListener.onAlert(alarmStatus, alarmResult);
}
}

private void processResult(AlarmResult alarmResult) {
if (alarmResult != null) {
Log.v(Main.LOG_TAG, "AlarmManager.processResult()");
Log.v(Main.LOG_TAG, "AlertManager.processResult()");

if (alarmResult.getClosestStrokeDistance() <= signalingDistanceLimit) {
long signalingLatestTimestamp = alarmStatusHandler.getLatestTimstampWithin(signalingDistanceLimit, alarmStatus);
if (signalingLatestTimestamp > signalingLastTimestamp) {
Log.v(Main.LOG_TAG, "AlarmManager.processResult() perform alarm");
Log.v(Main.LOG_TAG, "AlertManager.processResult() perform alarm");
vibrateIfEnabled();
playSoundIfEnabled();
signalingLastTimestamp = signalingLatestTimestamp;
Expand All @@ -242,7 +222,7 @@ private void processResult(AlarmResult alarmResult) {
if (alarmResult.getClosestStrokeDistance() <= notificationDistanceLimit) {
long notificationLatestTimestamp = alarmStatusHandler.getLatestTimstampWithin(notificationDistanceLimit, alarmStatus);
if (notificationLatestTimestamp > notificationLastTimestamp) {
Log.v(Main.LOG_TAG, "AlarmManager.processResult() perform notification");
Log.v(Main.LOG_TAG, "AlertManager.processResult() perform notification");
notificationHandler.sendNotification(context.getResources().getString(R.string.activity) + ": " + getTextMessage(notificationDistanceLimit));
notificationLastTimestamp = notificationLatestTimestamp;
} else {
Expand All @@ -255,7 +235,7 @@ private void processResult(AlarmResult alarmResult) {
notificationHandler.clearNotification();
}

Log.v(Main.LOG_TAG, String.format("AlarmManager.processResult() broadcast result %s", alarmResult));
Log.v(Main.LOG_TAG, String.format("AlertManager.processResult() broadcast result %s", alarmResult));

broadcastResult(alarmResult);
}
Expand All @@ -277,4 +257,7 @@ public Location getCurrentLocation() {
return location;
}

public void cancelAlert() {
broadcastClear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.blitzortung.android.alarm.listener;

import org.blitzortung.android.alarm.AlarmParameters;
import org.blitzortung.android.alarm.AlarmResult;
import org.blitzortung.android.alarm.object.AlarmStatus;

public interface AlertListener {
void onAlert(AlarmStatus alarmStatus, AlarmResult alarmResult);

void onAlertCancel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
public class AlarmStatus {

private final Collection<AlarmSector> sectors;
private final AlarmParameters alarmParameters;

public AlarmStatus(AlarmObjectFactory alarmObjectFactory, AlarmParameters alarmParameters) {
this.alarmParameters = alarmParameters;
final String[] sectorLabels = alarmParameters.getSectorLabels();
float sectorWidth = 360f / sectorLabels.length;

Expand All @@ -36,4 +38,8 @@ public void clearResults() {
public Collection<AlarmSector> getSectors() {
return sectors;
}

public AlarmParameters getAlarmParameters() {
return alarmParameters;
}
}
Loading

0 comments on commit 6fa962b

Please sign in to comment.