From e54a6eecc5cc2f64b0adc5318e643294d7195a38 Mon Sep 17 00:00:00 2001 From: n8fr8 Date: Wed, 17 Jan 2018 09:28:31 -0500 Subject: [PATCH] for #213 re-implemented time windows for notifications --- .../org/havenapp/main/PreferenceManager.java | 15 ++- .../org/havenapp/main/SettingsFragment.java | 20 ++++ .../java/org/havenapp/main/model/Event.java | 13 +-- .../havenapp/main/service/MonitorService.java | 96 ++++++++++--------- src/main/res/values/strings.xml | 7 ++ src/main/res/xml/settings.xml | 12 +++ 6 files changed, 104 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/havenapp/main/PreferenceManager.java b/src/main/java/org/havenapp/main/PreferenceManager.java index 5f4ab7a0..dbd16d65 100644 --- a/src/main/java/org/havenapp/main/PreferenceManager.java +++ b/src/main/java/org/havenapp/main/PreferenceManager.java @@ -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; @@ -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(); + } + } diff --git a/src/main/java/org/havenapp/main/SettingsFragment.java b/src/main/java/org/havenapp/main/SettingsFragment.java index aad98d6a..149b94af 100644 --- a/src/main/java/org/havenapp/main/SettingsFragment.java +++ b/src/main/java/org/havenapp/main/SettingsFragment.java @@ -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)); @@ -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(); diff --git a/src/main/java/org/havenapp/main/model/Event.java b/src/main/java/org/havenapp/main/model/Event.java index 7b158b53..c7e36459 100644 --- a/src/main/java/org/havenapp/main/model/Event.java +++ b/src/main/java/org/havenapp/main/model/Event.java @@ -18,8 +18,6 @@ public class Event extends SugarRecord { @Ignore private ArrayList mEventTriggers; - public final static long EVENT_WINDOW_TIME = 1000 * 60 * 5; //1 minutes - public Event () { mStartTime = new Date(); @@ -48,14 +46,5 @@ public ArrayList 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; - } + } diff --git a/src/main/java/org/havenapp/main/service/MonitorService.java b/src/main/java/org/havenapp/main/service/MonitorService.java index c6aab0ed..3b4f4a2d 100644 --- a/src/main/java/org/havenapp/main/service/MonitorService.java +++ b/src/main/java/org/havenapp/main/service/MonitorService.java @@ -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 */ @@ -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()) 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 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); - } } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 123d7dcd..fff37b62 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -129,4 +129,11 @@ Notifications Camera Sensitivity + + Notification Time Interval + Only send notifications at configured interval + Enter time (minutes) to limit notifications. \'0\' to send every notification. + minutes(s) + + diff --git a/src/main/res/xml/settings.xml b/src/main/res/xml/settings.xml index f671a885..14a672e5 100644 --- a/src/main/res/xml/settings.xml +++ b/src/main/res/xml/settings.xml @@ -61,6 +61,18 @@ android:key="verify_signal" android:summary="@string/verification_dialog_summary" android:title="@string/verify_signal" /> + + + +