Skip to content

Commit

Permalink
for #213 re-implemented time windows for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Jan 17, 2018
1 parent 47395d9 commit e54a6ee
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 59 deletions.
15 changes: 13 additions & 2 deletions src/main/java/org/havenapp/main/PreferenceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class PreferenceManager {

private static final String FIRST_LAUNCH = "first_launch";

public static final String NOTIFICATION_TIME = "notification_time";


private Context context;

Expand Down Expand Up @@ -304,12 +306,21 @@ public int getMaxImages ()

public String getAudioPath ()
{
return "/phoneypot";

return "/phoneypot"; //phoneypot is the old code name for Haven
}

public int getAudioLength ()
{
return 15000; //30 seconds
}

public int getNotificationTimeMs () {
return appSharedPrefs.getInt(NOTIFICATION_TIME,-1); //time in minutes times by seconds
}

public void setNotificationTimeMs (int notificationTimeMs) {
prefsEditor.putInt(NOTIFICATION_TIME,notificationTimeMs);
prefsEditor.commit();
}

}
20 changes: 20 additions & 0 deletions src/main/java/org/havenapp/main/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public void onCreatePreferencesFix(Bundle bundle, String s) {
findPreference(PreferenceManager.REGISTER_SIGNAL).setSummary(R.string.register_signal_desc);
}

if (preferences.getNotificationTimeMs()>0)
{
findPreference(PreferenceManager.NOTIFICATION_TIME).setSummary(preferences.getNotificationTimeMs()/60000 + " " + getString(R.string.minutes));
}

Preference prefCameraSensitivity = findPreference(PreferenceManager.CAMERA_SENSITIVITY);
prefCameraSensitivity.setOnPreferenceClickListener(preference -> {
startActivity(new Intent(mActivity, CameraConfigureActivity.class));
Expand Down Expand Up @@ -286,6 +291,21 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
askForPermission(Manifest.permission.READ_PHONE_STATE, 6);
}
setPhoneNumber();
break;
case PreferenceManager.NOTIFICATION_TIME:
try
{
String text = ((EditTextPreference)findPreference(PreferenceManager.NOTIFICATION_TIME)).getText();
int notificationTimeMs = Integer.parseInt(text)*60000;
preferences.setNotificationTimeMs(notificationTimeMs);
findPreference(PreferenceManager.NOTIFICATION_TIME).setSummary(preferences.getNotificationTimeMs()/60000 + " " + getString(R.string.minutes));

}
catch (NumberFormatException ne)
{
//error parsing user value
}

break;
case PreferenceManager.REMOTE_ACCESS_ONION: {
String text = ((EditTextPreference) findPreference(PreferenceManager.REMOTE_ACCESS_ONION)).getText();
Expand Down
13 changes: 1 addition & 12 deletions src/main/java/org/havenapp/main/model/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class Event extends SugarRecord {
@Ignore
private ArrayList<EventTrigger> mEventTriggers;

public final static long EVENT_WINDOW_TIME = 1000 * 60 * 5; //1 minutes

public Event ()
{
mStartTime = new Date();
Expand Down Expand Up @@ -48,14 +46,5 @@ public ArrayList<EventTrigger> getEventTriggers ()

return mEventTriggers;
}
/**
* Are we within the time window of this event, or should we start a new event?
*/
public boolean insideEventWindow (Date now)
{
if (mEventTriggers.size() == 0)
return now.getTime() - mStartTime.getTime() <= EVENT_WINDOW_TIME;
else
return now.getTime() - mEventTriggers.get(mEventTriggers.size()-1).getTriggerTime().getTime() <= EVENT_WINDOW_TIME;
}

}
96 changes: 51 additions & 45 deletions src/main/java/org/havenapp/main/service/MonitorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ public class MonitorService extends Service {
private AmbientLightMonitor mLightMonitor = null;

private boolean mIsRunning = false;

/**
* Last Event instances
*/
private Event mLastEvent;

/**
* Last sent notification time
*/
private Date mLastNotification;

/**
* Handler for incoming messages
*/
Expand Down Expand Up @@ -257,14 +263,17 @@ private void stopSensors ()
public synchronized void alert(int alertType, String path) {

Date now = new Date();
boolean isNewEvent = false;
boolean doNotification = false;

if (mLastEvent == null || (!mLastEvent.insideEventWindow(now)))
{
if (mLastEvent == null) {
mLastEvent = new Event();
mLastEvent.save();

isNewEvent = true;
doNotification = true;
}
else
{
//check if time window is within configured notification time window
doNotification = !((now.getTime()-mLastNotification.getTime())<mPrefs.getNotificationTimeMs());
}

EventTrigger eventTrigger = new EventTrigger();
Expand All @@ -276,48 +285,45 @@ public synchronized void alert(int alertType, String path) {
//we don't need to resave the event, only the trigger
eventTrigger.save();

/*
* If SMS mode is on we send an SMS or Signal alert to the specified
* number
*/
StringBuilder alertMessage = new StringBuilder();
alertMessage.append(getString(R.string.intrusion_detected,eventTrigger.getStringType(this)));

if (mPrefs.getSignalUsername() != null)
{
//since this is a secure channel, we can add the Onion address
if (mPrefs.getRemoteAccessActive() && (!TextUtils.isEmpty(mPrefs.getRemoteAccessOnion())))
{
alertMessage.append(" http://").append(mPrefs.getRemoteAccessOnion())
.append(':').append(WebServer.LOCAL_PORT);
}

SignalSender sender = SignalSender.getInstance(this,mPrefs.getSignalUsername());
ArrayList<String> recips = new ArrayList<>();
StringTokenizer st = new StringTokenizer(mPrefs.getSmsNumber(),",");
while (st.hasMoreTokens())
recips.add(st.nextToken());
if (doNotification) {

mLastNotification = new Date();
/*
* If SMS mode is on we send an SMS or Signal alert to the specified
* number
*/
StringBuilder alertMessage = new StringBuilder();
alertMessage.append(getString(R.string.intrusion_detected, eventTrigger.getStringType(this)));

if (mPrefs.getSignalUsername() != null) {
//since this is a secure channel, we can add the Onion address
if (mPrefs.getRemoteAccessActive() && (!TextUtils.isEmpty(mPrefs.getRemoteAccessOnion()))) {
alertMessage.append(" http://").append(mPrefs.getRemoteAccessOnion())
.append(':').append(WebServer.LOCAL_PORT);
}

SignalSender sender = SignalSender.getInstance(this, mPrefs.getSignalUsername());
ArrayList<String> recips = new ArrayList<>();
StringTokenizer st = new StringTokenizer(mPrefs.getSmsNumber(), ",");
while (st.hasMoreTokens())
recips.add(st.nextToken());

String attachment = null;
if (eventTrigger.getType() == EventTrigger.CAMERA) {
attachment = eventTrigger.getPath();
} else if (eventTrigger.getType() == EventTrigger.MICROPHONE) {
attachment = eventTrigger.getPath();
}

sender.sendMessage(recips, alertMessage.toString(), attachment);
} else if (mPrefs.getSmsActivation()) {
SmsManager manager = SmsManager.getDefault();

StringTokenizer st = new StringTokenizer(mPrefs.getSmsNumber(), ",");
while (st.hasMoreTokens())
manager.sendTextMessage(st.nextToken(), null, alertMessage.toString(), null, null);

String attachment = null;
if (eventTrigger.getType() == EventTrigger.CAMERA)
{
attachment = eventTrigger.getPath();
}
else if (eventTrigger.getType() == EventTrigger.MICROPHONE)
{
attachment = eventTrigger.getPath();
}

sender.sendMessage(recips,alertMessage.toString(), attachment);
}
else if (mPrefs.getSmsActivation() && isNewEvent)
{
SmsManager manager = SmsManager.getDefault();

StringTokenizer st = new StringTokenizer(mPrefs.getSmsNumber(),",");
while (st.hasMoreTokens())
manager.sendTextMessage(st.nextToken(), null, alertMessage.toString(), null, null);

}

}
Expand Down
7 changes: 7 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,11 @@
<string name="label_notifications">Notifications</string>

<string name="camera_sensitivity">Camera Sensitivity</string>

<string name="notification_time">Notification Time Interval</string>
<string name="notification_time_summary">Only send notifications at configured interval</string>
<string name="notification_time_dialog">Enter time (minutes) to limit notifications. \'0\' to send every notification.</string>
<string name="minutes">minutes(s)</string>


</resources>
12 changes: 12 additions & 0 deletions src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@
android:key="verify_signal"
android:summary="@string/verification_dialog_summary"
android:title="@string/verify_signal" />

<EditTextPreference
style="@style/AppPreference.DialogPreferenceSave"
android:dialogLayout="@layout/pref_dialog_edit_text"
android:dialogMessage="@string/notification_time_dialog"
android:inputType="number"
android:key="notification_time"
android:summary="@string/notification_time_summary"
android:title="@string/notification_time"
/>


</PreferenceCategory>
<PreferenceCategory android:title="@string/remote_access">

Expand Down

0 comments on commit e54a6ee

Please sign in to comment.