Let user change server hostname/port
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 5 Mar 2013 22:32:22 +0000 (00:32 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Tue, 5 Mar 2013 22:32:22 +0000 (00:32 +0200)
res/xml/prefs.xml
src/ro/ieval/fonbot/FonBotPreferenceActivity.java
src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java

index b289fc94bb6c8bd59b3354382d6e136af37a3be7..a962a760e2d25d448d01a782f2dbee479bd3a708 100644 (file)
@@ -6,4 +6,6 @@
     <CheckBoxPreference android:title="Device administration" android:summaryOn="Device administration is enabled." android:key="admin" android:summaryOff="Device administration is disabled. Some commands require device administration."/><CheckBoxPreference android:key="foreground" android:title="Run service in foreground" android:summaryOn="The FonBot service is kept in foreground. " android:summaryOff="The FonBot service is not kept in foreground."/><CheckBoxPreference android:key="poll_on_screen_on" android:summaryOff="The server is not polled when the screen is turned on" android:summaryOn="The server is polled when the screen is turned on" android:title="Poll server on screen on"/>
     <CheckBoxPreference android:key="system" android:summaryOff="FonBot is not a system app. Changing this requires root and busybox" android:title="System App" android:summaryOn="FonBot is a system app. Changing this requires root and busybox"/>
     
+    <EditTextPreference android:dialogTitle="Server hostname" android:title="Server hostname" android:dialogMessage="Server hostname. Do not change unless you know what you are doing!" android:key="hostname" android:defaultValue="ieval.ro" android:summary="ieval.ro"/>
+    <EditTextPreference android:dialogMessage="Server port. Do not change unless you know what you are doing!" android:dialogTitle="Server port" android:title="Server port" android:summary="7777" android:defaultValue="7777" android:key="port"/>
 </PreferenceScreen>
\ No newline at end of file
index 848a4369aa8a530e688e0fe643724326733a702e..8754825fb430e1f892296b3a009f53f97f7da8e8 100644 (file)
@@ -10,6 +10,7 @@ import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.os.Bundle;
 import android.preference.CheckBoxPreference;
+import android.preference.EditTextPreference;
 import android.preference.Preference;
 import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.PreferenceActivity;
@@ -137,6 +138,37 @@ public final class FonBotPreferenceActivity extends PreferenceActivity {
                                return true;
                        }
                });
+
+               final EditTextPreference hostnamePreference=(EditTextPreference) findPreference("hostname");
+               hostnamePreference.setSummary(hostnamePreference.getText());
+               hostnamePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
+                       @Override
+                       public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) {
+                               if(newValue==null)
+                                       return false;
+
+                               hostnamePreference.setSummary(newValue.toString());
+                               return true;
+                       }
+               });
+
+               final EditTextPreference portPreference=(EditTextPreference) findPreference("port");
+               portPreference.setSummary(portPreference.getText());
+               portPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
+                       @Override
+                       public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) {
+                               if(newValue==null)
+                                       return false;
+
+                               try{
+                                       Integer.parseInt(newValue.toString());
+                               } catch (NumberFormatException e){
+                                       return false;
+                               }
+                               portPreference.setSummary(newValue.toString());
+                               return true;
+                       }
+               });
        }
 
        @Override
index a40f7eda89c3f472e5e025ae049eef077b1d5e0b..e27848159817262d3d9462947b6c7b01fa80170f 100644 (file)
@@ -14,6 +14,7 @@ import org.eclipse.jdt.annotation.Nullable;
 import android.content.Context;
 import android.content.Intent;
 import android.os.AsyncTask;
+import android.preference.PreferenceManager;
 import android.util.Log;
 
 /*
@@ -127,7 +128,9 @@ public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, Stri
                        msg=data;
 
                try {
-                       final URL url=new URL("http://ieval.ro:7777/");
+                       final String hostname=PreferenceManager.getDefaultSharedPreferences(context).getString("hostname", "ieval.ro");
+                       final int port=Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString("port", "7777"));
+                       final URL url=new URL("http", hostname, port, "/");
                        final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                        conn.setDoOutput(true);
                        conn.setFixedLengthStreamingMode(msg.length);
This page took 0.012229 seconds and 4 git commands to generate.