Skip to content

Commit

Permalink
Remote snoozing and follower chime
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Sep 25, 2016
1 parent 596a5ee commit 7077afc
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 5 deletions.
48 changes: 48 additions & 0 deletions app/src/main/java/com/eveningoutpost/dexdrip/GcmActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.eveningoutpost.dexdrip.Models.BgReading;
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.Treatments;
import com.eveningoutpost.dexdrip.Models.UserError;
import com.eveningoutpost.dexdrip.utils.CipherUtils;
import com.eveningoutpost.dexdrip.utils.DisplayQRCode;
import com.eveningoutpost.dexdrip.utils.SdcardImportExport;
Expand Down Expand Up @@ -176,6 +177,53 @@ public static void sendBridgeBattery(final int battery) {
}
}

private static void sendRealSnoozeToRemote() {
if (JoH.pratelimit("gcm-sra", 60)) {
sendMessage("sra", Long.toString(JoH.tsl()));
}
}

public static void sendSnoozeToRemote() {
if ((Home.get_master() || Home.get_follower()) && (Home.getPreferencesBooleanDefaultFalse("send_snooze_to_remote"))
&& (JoH.pratelimit("gcm-sra-maybe", 5))) {
if (Home.getPreferencesBooleanDefaultFalse("confirm_snooze_to_remote")) {
Home.startHomeWithExtra(xdrip.getAppContext(), Home.HOME_FULL_WAKEUP, "1");
Home.startHomeWithExtra(xdrip.getAppContext(), Home.SNOOZE_CONFIRM_DIALOG, "");
} else {
sendRealSnoozeToRemote();
UserError.Log.ueh(TAG, "Sent snooze to remote");
}
}
}

public static void sendSnoozeToRemoteWithConfirm(final Context context) {
final long when = JoH.tsl();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Confirm Remote Snooze");
builder.setMessage("Are you sure you wish to snooze all other devices in your sync group?");
builder.setPositiveButton("YES, send it!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if ((JoH.tsl() - when) < 120000) {
sendRealSnoozeToRemote();
UserError.Log.ueh(TAG, "Sent snooze to remote after confirmation");
} else {
JoH.static_toast_long("Took too long to confirm! Ignoring!");
UserError.Log.ueh(TAG, "Ignored snooze confirmation as took > 2 minutes to confirm!");
}
}
});

builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}

public static void sendMotionUpdate(final long timestamp, final int activity) {
if (JoH.ratelimit("gcm-amu", 5)) {
sendMessage("amu", Long.toString(timestamp) + "^" + Integer.toString(activity));
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/eveningoutpost/dexdrip/GcmListenerSvc.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.eveningoutpost.dexdrip.Models.Treatments;
import com.eveningoutpost.dexdrip.Models.UserError;
import com.eveningoutpost.dexdrip.Services.ActivityRecognizedService;
import com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer;
import com.eveningoutpost.dexdrip.utils.CipherUtils;
import com.eveningoutpost.dexdrip.utils.Preferences;
import com.eveningoutpost.dexdrip.utils.WebAppHelper;
Expand Down Expand Up @@ -205,13 +206,35 @@ public void onMessageReceived(RemoteMessage rmessage) {
Home.toaststaticnext("Receiving motion updates from a different master! Make only one the master!");
}
}
} else if (action.equals("sra")) {
if ((Home.get_follower() || Home.get_master())) {
if (Home.getPreferencesBooleanDefaultFalse("accept_remote_snoozes")) {
final long snoozed_time = Long.parseLong(payload);
if (Math.abs(JoH.tsl() - snoozed_time) < 300000) {
if (JoH.pratelimit("received-remote-snooze", 30)) {
AlertPlayer.getPlayer().Snooze(xdrip.getAppContext(), -1, false);
UserError.Log.ueh(TAG, "Accepted remote snooze");
JoH.static_toast_long("Received remote snooze!");
} else {
Log.e(TAG, "Rate limited remote snooze");
}
} else {
UserError.Log.uel(TAG, "Ignoring snooze as outside 5 minute window, sync lag or clock difference");
}
} else {
UserError.Log.uel(TAG, "Rejecting remote snooze");
}
}
} else if (action.equals("bgs")) {
Log.i(TAG, "Received BG packet(s)");
if (Home.get_follower()) {
String bgs[] = payload.split("\\^");
for (String bgr : bgs) {
BgReading.bgReadingInsertFromJson(bgr);
}
if (Home.getPreferencesBooleanDefaultFalse("follower_chime") && JoH.pratelimit("bgs-notify", 1200)) {
JoH.showNotification("New glucose data @" + JoH.hourMinuteString(), "Follower Chime: will alert whenever it has been more than 20 minutes since last", null, 60311, true, true, true);
}
} else {
Log.e(TAG, "Received remote BG packet but we are not set as a follower");
}
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/com/eveningoutpost/dexdrip/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public class Home extends ActivityWithMenu {
public final static String CREATE_TREATMENT_NOTE = "CREATE_TREATMENT_NOTE";
public final static String HOME_FULL_WAKEUP = "HOME_FULL_WAKEUP";
public final static String GCM_RESOLUTION_ACTIVITY = "GCM_RESOLUTION_ACTIVITY";
public final static String SNOOZE_CONFIRM_DIALOG = "SNOOZE_CONFIRM_DIALOG";
public static String menu_name = "Home Screen";
public static boolean activityVisible = false;
public static boolean invalidateMenu = false;
Expand Down Expand Up @@ -577,6 +578,8 @@ public void run() {
}
} else if (bundle.getString(Home.GCM_RESOLUTION_ACTIVITY) != null) {
GcmActivity.checkPlayServices(this, this);
} else if (bundle.getString(Home.SNOOZE_CONFIRM_DIALOG) != null) {
GcmActivity.sendSnoozeToRemoteWithConfirm(this);
}
}
}
Expand Down Expand Up @@ -2008,9 +2011,14 @@ private void addDisplayDelta() {
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev){
public boolean dispatchTouchEvent(MotionEvent ev) {
if (blockTouches) return true;
return super.dispatchTouchEvent(ev);
try {
return super.dispatchTouchEvent(ev);
} catch (Exception e) {
// !?
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.support.v4.app.NotificationCompat;

import com.eveningoutpost.dexdrip.EditAlertActivity;
import com.eveningoutpost.dexdrip.GcmActivity;
import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.Models.ActiveBgAlert;
import com.eveningoutpost.dexdrip.Models.AlertType;
Expand Down Expand Up @@ -148,12 +149,18 @@ public synchronized void stopAlert(Context ctx, boolean ClearData, boolean clear
}
}

// default signature for user initiated interactive snoozes only
public synchronized void Snooze(Context ctx, int repeatTime) {
Snooze(ctx, repeatTime, true);
}

public synchronized void Snooze(Context ctx, int repeatTime, boolean from_interactive) {
Log.i(TAG, "Snooze called repeatTime = " + repeatTime);
stopAlert(ctx, false, false);
ActiveBgAlert activeBgAlert = ActiveBgAlert.getOnly();
if (activeBgAlert == null) {
Log.e(TAG, "Error, snooze was called but no alert is active. alert was probably removed in ui ");
Log.e(TAG, "Error, snooze was called but no alert is active.");
if (from_interactive) GcmActivity.sendSnoozeToRemote();
return;
}
if (repeatTime == -1) {
Expand All @@ -168,6 +175,7 @@ public synchronized void Snooze(Context ctx, int repeatTime) {
}
}
activeBgAlert.snooze(repeatTime);
if (from_interactive) GcmActivity.sendSnoozeToRemote();
}

public synchronized void PreSnooze(Context ctx, String uuid, int repeatTime) {
Expand Down Expand Up @@ -389,7 +397,7 @@ private void Vibrate(Context ctx, AlertType alert, String bgValue, Boolean overr
}

String title = bgValue + " " + alert.name;
String content = "BG LEVEL ALERT: " + bgValue;
String content = "BG LEVEL ALERT: " + bgValue + " (@" + JoH.hourMinuteString() + ")";
Intent intent = new Intent(ctx, SnoozeActivity.class);

NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/pref_notifications.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@
android:key="parakeet_charge_silent"
android:summary="@string/raise_parakeet_notification_silently_when_charging"
android:title="@string/silent_alert_when_charging" />
<SwitchPreference
android:defaultValue="false"
android:key="follower_chime"
android:summary="Notify when data arrives from the master if the interval is > 20 mins."
android:title="Follower Chime New" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceCategory>
Expand Down
23 changes: 22 additions & 1 deletion app/src/main/res/xml/xdrip_plus_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@
android:summary="Delayed but more stable when noisy (not recommended)"
android:title="@string/glucose_number_from_filtered" />
<CheckBoxPreference
android:defaultValue="true"
android:defaultValue="false"
android:key="show_bwp"
android:summary="Display Insulin/Carb calculations"
android:title="Show Bolus Wizard Preview" />
Expand Down Expand Up @@ -426,6 +426,27 @@
android:key="plus_follow_geolocation"
android:summary="@string/send_parakeet_map_location_to_followers"
android:title="@string/sync_parakeet_geolocation" />
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="xdrip_plus_remote_snooze_settings"
android:summary="Sending and receiving remote snoozes"
android:title="Remote Snoozing">
<CheckBoxPreference
android:defaultValue="false"
android:key="send_snooze_to_remote"
android:summary="Snoozes will silence all handsets in group with the accept setting below enabled on them"
android:title="Send snooze to all" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="send_snooze_to_remote"
android:key="confirm_snooze_to_remote"
android:summary="Pop up a dialog to confirm sending each remote snooze"
android:title="Confirm sending snooze" />
<CheckBoxPreference
android:defaultValue="false"
android:key="accept_remote_snoozes"
android:summary="Allow remote handsets to silence alarms on this one"
android:title="Accept remote snoozes" />
</PreferenceScreen>
<CheckBoxPreference
android:defaultValue="false"
android:key="disable_all_sync"
Expand Down

0 comments on commit 7077afc

Please sign in to comment.