]> hydra-www.ietfng.org Git - acmetensortoys-ctfws-android/commitdiff
Improve settings panel
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Mon, 20 Feb 2017 08:09:48 +0000 (03:09 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Mon, 20 Feb 2017 08:09:48 +0000 (03:09 -0500)
Remove custom Dialog in favor of custom EditTextPreference
Fix some subscription issues in MainAcitivty

mobile/src/main/java/com/acmetensortoys/ctfwstimer/DefaultableEditTextPreference.java [new file with mode: 0644]
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainActivity.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/StringSettingDialogFragment.java [deleted file]
mobile/src/main/res/layout/activity_main.xml
mobile/src/main/res/layout/server_dialog.xml [deleted file]
mobile/src/main/res/menu/mainmenu.xml
mobile/src/main/res/values/strings.xml
mobile/src/main/res/xml/preferences.xml

diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/DefaultableEditTextPreference.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/DefaultableEditTextPreference.java
new file mode 100644 (file)
index 0000000..33656e1
--- /dev/null
@@ -0,0 +1,55 @@
+package com.acmetensortoys.ctfwstimer;
+
+import android.app.AlertDialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.res.TypedArray;
+import android.os.Bundle;
+import android.preference.EditTextPreference;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.Button;
+
+public class DefaultableEditTextPreference extends EditTextPreference {
+    // GAH!  Our parent Preference has this as a private field.  Grrr Android!
+    private String defText;
+
+    public DefaultableEditTextPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    @Override
+    protected Object onGetDefaultValue(TypedArray a, int index) {
+        defText = a.getString(index);
+        return super.onGetDefaultValue(a, index);
+    }
+
+    @Override
+    public void setDefaultValue(Object defaultValue) {
+        defText = (String) defaultValue;
+        super.setDefaultValue(defaultValue);
+    }
+
+    @Override
+    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
+        super.onPrepareDialogBuilder(builder);
+
+        // null callback is OK here because we're about to nuke the would-be caller below!
+        builder.setNeutralButton(R.string.dialog_reset, null);
+    }
+
+    @Override
+    protected void showDialog(Bundle state) {
+        super.showDialog(state);
+
+        ((AlertDialog)getDialog())
+                .getButton(AlertDialog.BUTTON_NEUTRAL)
+                .setOnClickListener(new View.OnClickListener() {
+                    @Override
+                    public void onClick(View v) {
+                        getEditText().setText(defText);
+                    }
+                });
+
+    }
+}
index 3d81b1a847eb8f26a0987b2181a39360f573e20a..78a1e1f98cf64d1720ec7f1a523362609ee5ceb9 100644 (file)
@@ -103,8 +103,13 @@ public class MainActivity extends AppCompatActivity {
         Log.d("CtFwS", "onStart");
         super.onStart();
 
-        Intent si = new Intent(this, MainService.class);
-        bindService(si, ctfwssc, Context.BIND_AUTO_CREATE);
+        if (mSrvBinder == null) {
+            Intent si = new Intent(this, MainService.class);
+            bindService(si, ctfwssc, Context.BIND_AUTO_CREATE);
+        } else {
+            mSrvBinder.getGameState().registerObserver(mCdl);
+            mSrvBinder.registerObserver(mSrvObs);
+        }
     }
 
     @Override
@@ -166,12 +171,6 @@ public class MainActivity extends AppCompatActivity {
                 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;
             default:
                 return super.onOptionsItemSelected(mi);
         }
diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/StringSettingDialogFragment.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/StringSettingDialogFragment.java
deleted file mode 100644 (file)
index bf17eb5..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-package com.acmetensortoys.ctfwstimer;
-
-import android.app.AlertDialog;
-import android.app.Dialog;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.content.SharedPreferences;
-import android.os.Bundle;
-import android.preference.PreferenceManager;
-import android.support.annotation.NonNull;
-import android.support.v4.app.DialogFragment;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.widget.TextView;
-
-public class StringSettingDialogFragment extends DialogFragment
-    implements SharedPreferences.OnSharedPreferenceChangeListener
-{
-    private final static String ARG_LRES_IX = "lres"; // layout id
-    private final static String ARG_VRES_IX = "vres"; // text view id
-    private final static String ARG_PREF_IX = "pref"; // preference name
-    private final static String ARG_DEFL_IX = "def";   // optional default
-
-    private TextView mTv;
-
-    public static StringSettingDialogFragment newInstance(int lres, int vres, String pref, String def) {
-        StringSettingDialogFragment ssdf = new StringSettingDialogFragment();
-        Bundle args = new Bundle();
-        args.putInt   (ARG_LRES_IX, lres);
-        args.putInt   (ARG_VRES_IX, vres);
-        args.putString(ARG_PREF_IX, pref);
-        if (def != null) { args.putString(ARG_DEFL_IX, def); }
-        ssdf.setArguments(args);
-        return ssdf;
-    }
-
-    @NonNull @Override
-    public Dialog onCreateDialog(Bundle savedInstanceState) {
-        Bundle a = getArguments();
-        AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
-        LayoutInflater li = getActivity().getLayoutInflater();
-        View v = li.inflate(a.getInt(ARG_LRES_IX), null);
-
-        mTv = (TextView)v.findViewById(a.getInt(ARG_VRES_IX));
-        final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
-        sp.registerOnSharedPreferenceChangeListener(this);
-        onSharedPreferenceChanged(sp,a.getString(ARG_PREF_IX));
-
-        adb.setView(v)
-                .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
-                    @Override
-                    public void onClick(DialogInterface dialog, int which) {
-                        sp.edit().putString("server", mTv.getText().toString()).apply();
-                    }
-                })
-                .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
-                    @Override
-                    public void onClick(DialogInterface dialog, int which) {
-                        // NOP
-                    }
-                });
-
-        final String def = a.getString(ARG_DEFL_IX);
-        if (def != null) {
-            adb.setNeutralButton(R.string.dialog_reset, new DialogInterface.OnClickListener() {
-                @Override
-                public void onClick(DialogInterface dialog, int which) {
-                    mTv.post(new Runnable() {
-                        @Override
-                        public void run() {
-                            mTv.setText(def);
-                        }
-                    });
-                }
-            });
-        }
-
-        return adb.create();
-    }
-
-    @Override
-    public void onSharedPreferenceChanged(final SharedPreferences sp, final String p) {
-        if (p != null && getArguments().getString(ARG_PREF_IX).equals(p)) {
-            mTv.post(new Runnable() {
-                @Override
-                public void run() {
-                    mTv.setText(sp.getString(p, ""));
-                }
-            });
-        }
-    }
-}
index cd57489d64824a1fd6de36b64b3c33904c152a7f..ef3f4887eb80cca6edab751f9279d300729c90e2 100644 (file)
                 <TextView
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
-                    android:text="@string/menutext_mqtt_label"
+                    android:text="@string/mqtt_uri_label"
                     android:gravity="right" />
 
                 <TextView
diff --git a/mobile/src/main/res/layout/server_dialog.xml b/mobile/src/main/res/layout/server_dialog.xml
deleted file mode 100644 (file)
index 096a0be..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical" android:layout_width="match_parent"
-    android:layout_height="match_parent">
-
-    <TextView
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:text="@string/menutext_mqtt_label"
-        android:labelFor="@+id/server_text"/>
-
-    <EditText
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:ems="10"
-        android:id="@+id/server_text"
-        android:inputType="text" />
-</LinearLayout>
\ No newline at end of file
index e4686b23812ad1cf3709c67db54611aa9c2bc894..eb5c15ea1ad5d28b033297fc021ebcf0f838b836 100644 (file)
@@ -8,13 +8,6 @@
         android:id="@+id/menu_prf"
         android:icon="@android:drawable/ic_menu_manage"
         android:checkable="false"/>
-    <item
-        android:visible="true"
-        android:enabled="true"
-        android:title="@string/menutext_mqtt"
-        android:id="@+id/menu_mqtt"
-        android:icon="@android:drawable/ic_menu_manage"
-        android:checkable="false" />
     <item android:title="@string/menutext_about"
         android:id="@+id/menu_about"
         android:icon="@android:drawable/ic_menu_compass"
index 2321331ae9984a2ce5509d817ae4302b0647763f..eac3e141491cf85d7b5bba41d70b6ac9d95172b3 100644 (file)
 
     <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_uri_label">Server URI:</string>
     <string name="mqtt_state_label">Server State:</string>
     <string name="mqtt_conn">Connected but not subscribed</string>
     <string name="mqtt_disconn">Disconnected</string>
index 8f56170f69726788a6d33a3d5da9709c90ba6b61..d8e14ae7ff6a0894b6070a676bfe0f8093002ee4 100644 (file)
@@ -1,12 +1,12 @@
 <?xml version="1.0" encoding="utf-8"?>\r
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">\r
 \r
-    <EditTextPreference\r
+    <com.acmetensortoys.ctfwstimer.DefaultableEditTextPreference\r
         android:selectAllOnFocus="true"\r
         android:singleLine="true"\r
-        android:key="prf_mqtt_uri"\r
+        android:key="server"\r
         android:defaultValue="tcp://ctfws-mqtt.ietfng.org:1883"\r
-        android:title="Change MQTT Server" />\r
+        android:title="@string/menutext_mqtt" />\r
     <CheckBoxPreference\r
         android:defaultValue="true"\r
         android:title="Vibrate?"\r