]> hydra-www.ietfng.org Git - acmetensortoys-ctfws-android/commitdiff
Add preferences menu
authorCameron Wong <camtech075@gmail.com>
Fri, 17 Feb 2017 02:03:37 +0000 (21:03 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Mon, 20 Feb 2017 06:47:38 +0000 (01:47 -0500)
mobile/src/main/AndroidManifest.xml
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainActivity.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/SettingsActivity.java [new file with mode: 0644]
mobile/src/main/res/menu/mainmenu.xml
mobile/src/main/res/values/strings.xml
mobile/src/main/res/xml/preferences.xml [new file with mode: 0644]

index 50aa44732aa0d398cd049bffeca106491737286f..f61d74d61f765cb302f3b95586f6b50b04843ddf 100644 (file)
@@ -27,6 +27,7 @@
         <service android:name="org.eclipse.paho.android.service.MqttService" />
 
         <activity android:name=".AboutActivity" />
+        <activity android:name=".SettingsActivity" />
 
         <service
             android:name=".MainService"
index 1b6f84bceab88d4f59dccfa1c30b035e8f436c80..3d81b1a847eb8f26a0987b2181a39360f573e20a 100644 (file)
@@ -154,18 +154,24 @@ public class MainActivity extends AppCompatActivity {
     }
 
     // TODO should we be using onClick instead for routing?
+    // Cam: According to official documentation, this is the preferred way to into menus, so
+    //      we're (overall) fine.
     @Override
     public boolean onOptionsItemSelected(MenuItem mi) {
         switch(mi.getItemId()) {
+            case R.id.menu_prf :
+                startActivity(new Intent(this, SettingsActivity.class));
+                return true;
+            case R.id.menu_about :
+                startActivity(new Intent(this, AboutActivity.class));
+                return true;
+            // Cam: Changing this doesn't appear to do anything? Leaving just in case.
             case R.id.menu_mqtt :
                 DialogFragment d =
                         StringSettingDialogFragment.newInstance(
                                 R.layout.server_dialog, R.id.server_text, "server", defserver);
                 d.show(getSupportFragmentManager(),"serverdialog");
                 return true;
-            case R.id.menu_about :
-                startActivity(new Intent(this, AboutActivity.class));
-                return true;
             default:
                 return super.onOptionsItemSelected(mi);
         }
index 096146dd9b991281492f20851dd286eed7cb8888..507d8401c551086ad1b7695c72ea0885100dbecf 100644 (file)
@@ -10,6 +10,7 @@ import android.content.SharedPreferences;
 import android.os.Binder;
 import android.os.Handler;
 import android.os.IBinder;
+import android.os.Vibrator;
 import android.preference.PreferenceManager;
 import android.support.annotation.Nullable;
 import android.support.v4.app.NotificationCompat;
@@ -136,7 +137,6 @@ public class MainService extends Service {
         @Override
         public void onSuccess(IMqttToken asyncActionToken) {
             Log.d("CtFwS", "Conn OK 1");
-
             IMqttAsyncClient c = asyncActionToken.getClient();
             if (c.equals(mMqc)) {
                 setMSE(MqttServerEvent.MSE_CONN);
@@ -264,6 +264,24 @@ public class MainService extends Service {
 
     // User-facing notification
     // TODO Move to its own display module?
+
+    // The pattern for notification vibration patterns. Maybe we could have multiple for different
+    // events, like flags/jailbreaks?
+    private long[] VIBRATE_PATTERN = {0, 300, 200, 300};
+
+    private void vibrate(long[] pattern) {
+        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
+        // 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)) {
+            Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
+            v.vibrate(VIBRATE_PATTERN,-1);
+        }
+        else {
+            Log.d("vibrate", "off");
+        }
+    }
+
     private ServiceConnection userNoteSC;
     private void ensureNotification() {
         synchronized(this) {
@@ -278,6 +296,7 @@ public class MainService extends Service {
                     }
                 };
             }
+            // Cam: Do we need this?
             bindService(new Intent(MainService.this, MainService.class), userNoteSC,
                     Context.BIND_AUTO_CREATE);
             startForeground(NOTE_ID_USER, userNoteBuilder.build());
@@ -294,6 +313,8 @@ public class MainService extends Service {
     }
 
     private NotificationCompat.Builder userNoteBuilder;
+    // TODO (Cam): It'd be cool if we could make the notification say "you got a flag" or something
+    //             for a few seconds after we get messages
     private CtFwSGameState.Observer mCgsObserver = new CtFwSGameState.Observer() {
         @Override
         public void onCtFwSConfigure(CtFwSGameState game) { }
@@ -303,6 +324,7 @@ public class MainService extends Service {
             userNoteBuilder.setWhen((now.roundEnd+1)*1000);
             userNoteBuilder.setUsesChronometer(true);
             if (now.rationale == null || !now.stop) {
+                vibrate(VIBRATE_PATTERN);
                 // game is afoot!
                 userNoteBuilder.setContentTitle(
                         now.rationale == null ? "Game is afoot!" : now.rationale);
@@ -318,6 +340,7 @@ public class MainService extends Service {
         @Override
         public void onCtFwSFlags(CtFwSGameState game) { }
 
+        // Cam: Are we just explicitly no-op'ing this, or should we actually display messages?
         @Override
         public void onCtFwSMessage(CtFwSGameState game, List<CtFwSGameState.Msg> msgs) { }
     };
diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/SettingsActivity.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/SettingsActivity.java
new file mode 100644 (file)
index 0000000..d007765
--- /dev/null
@@ -0,0 +1,22 @@
+package com.acmetensortoys.ctfwstimer;\r
+\r
+import android.preference.PreferenceActivity;\r
+import android.preference.PreferenceFragment;\r
+import android.os.Bundle;\r
+\r
+// TODO (Cam): changing the server doesn't actually work yet\r
+public class SettingsActivity extends PreferenceActivity {\r
+    @Override\r
+    protected void onCreate(Bundle savedInstanceState) {\r
+        super.onCreate(savedInstanceState);\r
+        getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();\r
+    }\r
+\r
+    public static class SettingsFragment extends PreferenceFragment {\r
+        @Override\r
+        public void onCreate(final Bundle savedInstanceBundle) {\r
+            super.onCreate(savedInstanceBundle);\r
+            addPreferencesFromResource(R.xml.preferences);\r
+        }\r
+    }\r
+}\r
index 0c6a9f0e93278685fe0cac786a6e000c4bb91d70..e4686b23812ad1cf3709c67db54611aa9c2bc894 100644 (file)
@@ -1,6 +1,13 @@
 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 
+    <item
+        android:visible="true"
+        android:enabled="true"
+        android:title="@string/menutext_prf"
+        android:id="@+id/menu_prf"
+        android:icon="@android:drawable/ic_menu_manage"
+        android:checkable="false"/>
     <item
         android:visible="true"
         android:enabled="true"
index 42ca20b5a9182a7576be1e4872c313fa8827819a..2321331ae9984a2ce5509d817ae4302b0647763f 100644 (file)
@@ -18,6 +18,7 @@
     <string name="menutext_about">About</string>
     <string name="menutext_mqtt">Set MQTT Server</string>
     <string name="menutext_mqtt_label">Server URI:</string>
+    <string name="menutext_prf">Settings</string>
 
     <string name="mqtt_header">Connection Metadata:</string>
     <string name="mqtt_state_label">Server State:</string>
diff --git a/mobile/src/main/res/xml/preferences.xml b/mobile/src/main/res/xml/preferences.xml
new file mode 100644 (file)
index 0000000..8f56170
--- /dev/null
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>\r
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">\r
+\r
+    <EditTextPreference\r
+        android:selectAllOnFocus="true"\r
+        android:singleLine="true"\r
+        android:key="prf_mqtt_uri"\r
+        android:defaultValue="tcp://ctfws-mqtt.ietfng.org:1883"\r
+        android:title="Change MQTT Server" />\r
+    <CheckBoxPreference\r
+        android:defaultValue="true"\r
+        android:title="Vibrate?"\r
+        android:key="prf_vibr" />\r
+</PreferenceScreen>
\ No newline at end of file