Skip to content

Commit

Permalink
Remote snooze fuzzy network name matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Sep 26, 2016
1 parent bc6e665 commit 42b3e2c
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 57 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -30,6 +30,7 @@
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
Expand Down
Expand Up @@ -179,7 +179,9 @@ public static void sendBridgeBattery(final int battery) {

private static void sendRealSnoozeToRemote() {
if (JoH.pratelimit("gcm-sra", 60)) {
sendMessage("sra", Long.toString(JoH.tsl()));
String wifi_ssid = JoH.getWifiSSID();
if (wifi_ssid == null) wifi_ssid = "";
sendMessage("sra", Long.toString(JoH.tsl()) + "^" + JoH.base64encode(wifi_ssid));
}
}

Expand Down
34 changes: 25 additions & 9 deletions app/src/main/java/com/eveningoutpost/dexdrip/GcmListenerSvc.java
Expand Up @@ -209,17 +209,33 @@ public void onMessageReceived(RemoteMessage rmessage) {
} 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!");
try {
long snoozed_time = 0;
String sender_ssid = "";
try {
snoozed_time = Long.parseLong(payload);
} catch (NumberFormatException e) {
String ii[] = payload.split("\\^");
snoozed_time = Long.parseLong(ii[0]);
if (ii.length > 1) sender_ssid = JoH.base64decode(ii[1]);
}
if (!Home.getPreferencesBooleanDefaultFalse("remote_snoozes_wifi_match") || JoH.getWifiFuzzyMatch(sender_ssid,JoH.getWifiSSID())) {
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 {
Log.e(TAG, "Rate limited remote snooze");
UserError.Log.uel(TAG,"Ignoring snooze as wifi network names do not match closely enough");
}
} else {
UserError.Log.uel(TAG, "Ignoring snooze as outside 5 minute window, sync lag or clock difference");
} catch (Exception e) {
UserError.Log.e(TAG, "Exception processing remote snooze: " + e);
}
} else {
UserError.Log.uel(TAG, "Rejecting remote snooze");
Expand Down
56 changes: 56 additions & 0 deletions app/src/main/java/com/eveningoutpost/dexdrip/Models/JoH.java
Expand Up @@ -17,6 +17,8 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
Expand Down Expand Up @@ -229,6 +231,15 @@ public static String base64encode(String input) {
}
}

public static String base64decode(String input) {
try {
return new String(Base64.decode(input.getBytes("UTF-8"), Base64.NO_WRAP), "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Got unsupported encoding: " + e);
return "decode-error";
}
}

public static String ucFirst(String input) {
return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
}
Expand Down Expand Up @@ -483,6 +494,51 @@ public static boolean isOngoingCall() {
}
}

public static String getWifiSSID() {
try {
final WifiManager wifi_manager = (WifiManager) xdrip.getAppContext().getSystemService(Context.WIFI_SERVICE);
if (wifi_manager.isWifiEnabled()) {
final WifiInfo wifiInfo = wifi_manager.getConnectionInfo();
if (wifiInfo != null) {
final NetworkInfo.DetailedState wifi_state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
if (wifi_state == NetworkInfo.DetailedState.CONNECTED
|| wifi_state == NetworkInfo.DetailedState.OBTAINING_IPADDR
|| wifi_state == NetworkInfo.DetailedState.CAPTIVE_PORTAL_CHECK) {
String ssid = wifiInfo.getSSID();
if (ssid.equals("<unknown ssid>")) return null; // WifiSsid.NONE;
if (ssid.charAt(0)=='"') ssid=ssid.substring(1);
if (ssid.charAt(ssid.length()-1)=='"') ssid=ssid.substring(0,ssid.length()-1);
return ssid;
}
}
}
} catch (Exception e) {
Log.e(TAG, "Got exception in getWifiSSID: " + e);
}
return null;
}

public static boolean getWifiFuzzyMatch(String local, String remote) {
if ((local == null) || (remote == null) || (local.length() == 0) || (remote.length() == 0))
return false;
final int slen = Math.min(local.length(), remote.length());
final int llen = Math.max(local.length(), remote.length());
int matched = 0;
for (int i = 0; i < slen; i++) {
if (local.charAt(i) == (remote.charAt(i))) matched++;
}
boolean result = false;
if (matched == slen) result = true; // shorter string is substring
final double quota = (double) matched / (double) llen;
final int dmatch = llen - matched;
if (slen > 2) {
if (dmatch < 3) result = true;
if (quota > 0.80) result = true;
}
//Log.d(TAG, "l:" + local + " r:" + remote + " slen:" + slen + " llen:" + llen + " matched:" + matched + " q:" + JoH.qs(quota, 2) + " dm:" + dmatch + " RESULT: " + result);
return result;
}

public static boolean runOnUiThread(Runnable theRunnable) {
final Handler mainHandler = new Handler(xdrip.getAppContext().getMainLooper());
return mainHandler.post(theRunnable);
Expand Down
99 changes: 52 additions & 47 deletions app/src/main/res/xml/xdrip_plus_prefs.xml
Expand Up @@ -3,7 +3,7 @@

<PreferenceCategory
android:key="xdrip_plus_category"
android:title="@string/xdrip_plus_extra_settings"/>
android:title="@string/xdrip_plus_extra_settings" />
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:icon="@drawable/ic_chart_areaspline_grey600_48dp"
android:key="xdrip_plus_display_settings"
Expand Down Expand Up @@ -191,28 +191,27 @@
</PreferenceScreen>

<SwitchPreference
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:defaultValue="false"
android:title="Force English Text"
android:key="force_english"
android:summary="Currently using your device's local language"
android:summaryOn="Currently forcing use of alternate language"
android:key="force_english">
</SwitchPreference>
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:title="Force English Text"></SwitchPreference>
<ListPreference
android:key="forced_language"
android:title="Choose a specific language"
android:summary="If you need an alternate language translation within xDrip+"
android:defaultValue="en"
android:entries="@array/LocaleChoices"
android:entryValues="@array/LocaleChoicesValues"
android:defaultValue="en"/>
android:key="forced_language"
android:summary="If you need an alternate language translation within xDrip+"
android:title="Choose a specific language" />

<SwitchPreference
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:defaultValue="true"
android:key="bg_compensate_noise"
android:summary="@string/try_to_work_around_noisy_readings"
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:title="\u26A0 Smooth sensor noise" />
<CheckBoxPreference
android:defaultValue="true"
Expand All @@ -225,10 +224,10 @@
android:summary="@string/after_calibration_rewrite_history"
android:title="@string/rewrite_history" />
<CheckBoxPreference
android:defaultValue="false"
android:key="widget_range_lines"
android:title="Widget Range Lines"
android:summary="Show a high and low line on the widget."
android:defaultValue="false" />
android:title="Widget Range Lines" />
<CheckBoxPreference
android:defaultValue="true"
android:key="show_graph_grid_time"
Expand Down Expand Up @@ -285,8 +284,8 @@
android:summary="Display Insulin/Carb calculations"
android:title="Show Bolus Wizard Preview" />
<CheckBoxPreference
android:dependency="show_bwp"
android:defaultValue="false"
android:dependency="show_bwp"
android:key="always_show_bwp"
android:summary="Display Insulin/Carb calculations at all times"
android:title="Always show Bolus Wizard Preview" />
Expand Down Expand Up @@ -318,7 +317,7 @@
<intent
android:action="android.intent.action.MAIN"
android:targetClass="com.eveningoutpost.dexdrip.profileeditor.ProfileEditor"
android:targetPackage="@string/local_target_package"/>
android:targetPackage="@string/local_target_package" />
</Preference>
<Preference
android:key="profile_insulin_sensitivity_default"
Expand All @@ -327,7 +326,7 @@
<intent
android:action="android.intent.action.MAIN"
android:targetClass="com.eveningoutpost.dexdrip.profileeditor.ProfileEditor"
android:targetPackage="@string/local_target_package"/>
android:targetPackage="@string/local_target_package" />
</Preference>
<EditTextPreference
android:defaultValue="35"
Expand Down Expand Up @@ -430,22 +429,28 @@
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" />
<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" />
<CheckBoxPreference
android:defaultValue="true"
android:dependency="accept_remote_snoozes"
android:key="remote_snoozes_wifi_match"
android:summary="Only accept if the remote wifi network name starts with the same letters as ours and matches very closely"
android:title="Wifi name must match" />
</PreferenceScreen>
<CheckBoxPreference
android:defaultValue="false"
Expand All @@ -461,53 +466,53 @@
android:summary="Movement detection and vehicle mode"
android:title="xDrip+ Motion Tracking">
<SwitchPreference
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:defaultValue="false"
android:key="motion_tracking_enabled"
android:summary="Use sensors to detect types of motion"
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:title="Enable Motion Tracking" />
<CheckBoxPreference
android:dependency="motion_tracking_enabled"
<CheckBoxPreference
android:defaultValue="true"
android:dependency="motion_tracking_enabled"
android:key="plot_motion"
android:summary="Store recent motion types and plot these on the main graph"
android:title="Log and Plot motion" />
<CheckBoxPreference
android:dependency="motion_tracking_enabled"
android:defaultValue="false"
android:dependency="motion_tracking_enabled"
android:key="use_remote_motion"
android:summary="Motion data taken from remote rather than local handset source"
android:title="Use remote motion" />
<CheckBoxPreference
android:dependency="motion_tracking_enabled"
android:defaultValue="false"
android:dependency="motion_tracking_enabled"
android:key="act_as_motion_master"
android:summary="Be the master for motion data even if we are a follower. Set this on only one handset!"
android:title="Act as motion master" />
<SwitchPreference
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:defaultValue="false"
android:dependency="motion_tracking_enabled"
android:key="vehicle_mode_enabled"
android:summary="When vehicle motion is detected enable extra features"
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:title="Enable Vehicle Mode" />
<CheckBoxPreference
android:dependency="vehicle_mode_enabled"
android:defaultValue="false"
android:dependency="vehicle_mode_enabled"
android:key="raise_low_limit_in_vehicle_mode"
android:summary="Increase the level at which Low alarms will be triggered when in vehicle mode"
android:title="Raise 'Low' threshold" />
<CheckBoxPreference
android:dependency="vehicle_mode_enabled"
android:defaultValue="true"
android:dependency="vehicle_mode_enabled"
android:key="play_sound_in_vehicle_mode"
android:summary="Notification sound when vehicle mode detected"
android:title="Play sound" />
<CheckBoxPreference
android:dependency="vehicle_mode_enabled"
android:defaultValue="true"
android:dependency="vehicle_mode_enabled"
android:key="repeat_sound_in_vehicle_mode"
android:summary="Repeat notification sound every 90 minutes if still in vehicle mode"
android:title="Repeat sound" />
Expand All @@ -520,12 +525,12 @@
android:summary="@string/automatic_updates_crash_reports_and_feedback"
android:title="@string/xdrip_plus_update_settings">
<SwitchPreference
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:defaultValue="true"
android:key="auto_update_download"
android:summaryOn="@string/get_notified_of_new_apk_releases"
android:summaryOff="New releases often have bugs fixed. It is recommended to switch on update notifications!"
android:summaryOn="@string/get_notified_of_new_apk_releases"
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:title="@string/automatic_update_check" />
<ListPreference
android:defaultValue="beta"
Expand Down

0 comments on commit 42b3e2c

Please sign in to comment.