]> hydra-www.ietfng.org Git - acmetensortoys-ctfws-android/commitdiff
Enable more fine-grained control of vibration
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Sat, 21 Oct 2017 05:59:25 +0000 (01:59 -0400)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Sat, 21 Oct 2017 07:11:59 +0000 (03:11 -0400)
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java
mobile/src/main/res/values/strings.xml
mobile/src/main/res/xml/preferences.xml

index c6a287221be8cf7ae07e24f0b4d0f783da5a8cff..5e025ec30af0790565bb7dc05515bbccac20f4d7 100644 (file)
@@ -19,9 +19,10 @@ class MainServiceNotification {
     final private MainService mService;
     private final NotificationCompat.Builder userNoteBuilder;
 
-    private long lastForegroundTime;
+    private long lastVibrateTime;
 
-    private enum LastContentTextSource { NONE, FLAG, MESG };
+    private enum VibrationSource { NONE, BREAK, FLAG, MESG }
+    private enum LastContentTextSource { NONE, FLAG, MESG }
     private LastContentTextSource lastContextTextSource = LastContentTextSource.NONE;
 
     MainServiceNotification(MainService ms, CtFwSGameState game){
@@ -68,7 +69,7 @@ class MainServiceNotification {
                         userNoteBuilder.setSubText(now.rationale);
                     }
 
-                    vibrate(VIBRATE_PATTERN_NOW);
+                    vibrate(VibrationSource.BREAK);
                     ensureNotification();
                 } else {
                     // game no longer afoot
@@ -84,7 +85,7 @@ class MainServiceNotification {
                 if (game.flagsVisible
                         && ((lastContextTextSource == LastContentTextSource.FLAG)
                             || (game.flagsRed + game.flagsYel > 0))) {
-                    vibrate(VIBRATE_PATTERN_FLAG);
+                    vibrate(VibrationSource.FLAG);
                     lastContextTextSource = LastContentTextSource.FLAG;
                     userNoteBuilder.setContentText(
                             String.format(mService.getResources().getString(R.string.notify_flags),
@@ -98,7 +99,7 @@ class MainServiceNotification {
                 // Only do anything if we aren't clearing the message list
                 int s = msgs.size();
                 if (s != 0) {
-                    vibrate(VIBRATE_PATTERN_MSG);
+                    vibrate(VibrationSource.MESG);
                     lastContextTextSource = LastContentTextSource.MESG;
                     userNoteBuilder.setContentText(msgs.get(s - 1).msg);
                     refreshNotification();
@@ -113,12 +114,42 @@ class MainServiceNotification {
     private final long[] VIBRATE_PATTERN_FLAG = {0, 100, 100, 100, 100, 300, 100, 100}; // 'F' = ..-.
     private final long[] VIBRATE_PATTERN_MSG  = {0, 300, 100, 300};                     // 'M' = --
 
-    private void vibrate(long[] pattern) {
-        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mService.getBaseContext());
+    private void vibrate(VibrationSource vs) {
+        long now = System.currentTimeMillis();
+
+        // Clobber the vibration request if we probably recently did such a thing
+        if ((now - lastVibrateTime < VIBRATE_SUPPRESS_THRESHOLD)) {
+            vs = VibrationSource.NONE;
+        }
+
+        String pref;
+        long[] pattern;
+
+        switch(vs) {
+            case BREAK:
+                pref = "prf_vibr_jb";
+                pattern = VIBRATE_PATTERN_NOW;
+                break;
+            case FLAG:
+                pref = "prf_vibr_flag";
+                pattern = VIBRATE_PATTERN_FLAG;
+                break;
+            case MESG:
+                pref = "prf_vibr_mesg";
+                pattern = VIBRATE_PATTERN_MSG;
+                break;
+            case NONE:
+            default:
+                userNoteBuilder.setVibrate(null);
+                return;
+        }
+
         // Cam: default value is "false" because we really don't want to be vibrating if we
         //      accidentally lose our preferences somehow
-        if (sp.getBoolean("prf_vibr", false)) {
+        if (PreferenceManager.getDefaultSharedPreferences(mService.getBaseContext())
+                .getBoolean(pref, false)) {
             userNoteBuilder.setVibrate(pattern);
+            lastVibrateTime = now;
         }
         else {
             userNoteBuilder.setVibrate(null);
@@ -128,14 +159,8 @@ class MainServiceNotification {
     private ServiceConnection userNoteSC;
     private void refreshNotification() {
         synchronized (this) {
-            long now = System.currentTimeMillis();
             if (userNoteSC != null) {
-                // Clobber the vibration request if we probably recently did such a thing
-                if (now - lastForegroundTime < VIBRATE_SUPPRESS_THRESHOLD){
-                    userNoteBuilder.setVibrate(null);
-                }
                 mService.startForeground(MainService.NOTE_ID_USER, userNoteBuilder.build());
-                lastForegroundTime = now;
             }
         }
     }
index a7b4c1af7517ae0d0f2869111fa00c2a6d76b53c..fa31c71b9fefaef729ec11a51822b8a0cb4dfe35 100644 (file)
@@ -31,7 +31,9 @@
     <string name="mqtt_subbed">Subscribed</string>
 
     <string name="preftext_mqtt">Set MQTT Server</string>
-    <string name="preftext_vibrate">Vibrate?</string>
+    <string name="preftext_vibrate_jb">Vibrate on Jailbreak?</string>
+    <string name="preftext_vibrate_flag">Vibrate on Flag Capture?</string>
+    <string name="preftext_vibrate_mesg">Vibrate on Message?</string>
 
     <string name="string_null">&lt;&lt;null&gt;&gt;</string>
 
index 67bf097565d150abc16c4c70c3368ea2eda39b04..04c91d66a9536ec0debb4813d5323af240336e47 100644 (file)
@@ -9,6 +9,18 @@
         android:title="@string/preftext_mqtt" />\r
     <CheckBoxPreference\r
         android:defaultValue="true"\r
-        android:title="@string/preftext_vibrate"\r
-        android:key="prf_vibr" />\r
+        android:key="prf_vibr_jb"\r
+        android:title="@string/preftext_vibrate_jb" />\r
+    <CheckBoxPreference\r
+        android:layout_width="wrap_content"\r
+        android:layout_height="wrap_content"\r
+        android:defaultValue="true"\r
+        android:key="prf_vibr_flag"\r
+        android:title="@string/preftext_vibrate_flag" />\r
+    <CheckBoxPreference\r
+        android:layout_width="wrap_content"\r
+        android:layout_height="wrap_content"\r
+        android:defaultValue="true"\r
+        android:key="prf_vibr_mesg"\r
+        android:title="@string/preftext_vibrate_mesg" />\r
 </PreferenceScreen>\r