--- /dev/null
+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);
+ }
+ });
+
+ }
+}
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
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);
}
+++ /dev/null
-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, ""));
- }
- });
- }
- }
-}
<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
+++ /dev/null
-<?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
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"
<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>
<?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