Make custom server work when polling
authorMarius Gavrilescu <marius@ieval.ro>
Wed, 6 Mar 2013 22:08:38 +0000 (00:08 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Wed, 6 Mar 2013 22:08:38 +0000 (00:08 +0200)
Until now, the custom server hostname:port were only used in
SendHttpMessageAsyncTask, while PollServerAsyncTask used the
hardcoded http://ieval.ro:7777/get.

src/ro/ieval/fonbot/DynamicEventReceiver.java
src/ro/ieval/fonbot/FonBotApplication.java
src/ro/ieval/fonbot/GCMIntentService.java
src/ro/ieval/fonbot/Heavy.java
src/ro/ieval/fonbot/LocalBroadcastReceiver.java
src/ro/ieval/fonbot/PollServerAsyncTask.java
src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java
src/ro/ieval/fonbot/Utils.java

index 926280b6234d60e24cc791136588b89bd83b2c29..d7acf5467da3ba0b9873cffa05d55076ac16484a 100644 (file)
@@ -47,7 +47,7 @@ public final class DynamicEventReceiver extends BroadcastReceiver {
 
                if(action.equals(Intent.ACTION_SCREEN_ON)){
                        if(PreferenceManager.getDefaultSharedPreferences(context).getBoolean("poll_on_screen_on", false))
-                               new PollServerAsyncTask().execute();
+                               new PollServerAsyncTask(context).execute();
                } else if(action.equals(Intent.ACTION_BATTERY_CHANGED))
                        Heavy.describeBatteryLevel(context, null, toNonNull(MessageType.BATTERY_CHANGED));
                else if(action.equals(Intent.ACTION_HEADSET_PLUG)){
index 46a0328fb716ddd2c015cdfaf316d328665443be..38b5b40ac7ca30ab0c70a1f4230e134d476d9cf0 100644 (file)
@@ -57,7 +57,7 @@ public final class FonBotApplication extends Application {
                tman.listen(new FonBotPhoneStateListener(this), PhoneStateListener.LISTEN_CALL_STATE);
                instance=this;
 
-               new PollServerAsyncTask().execute();
+               new PollServerAsyncTask(this).execute();
 
                startService(new Intent(this, FonBotMainService.class));
 
index de3820874242db746df6a281c2a2079e550997e0..aea7202da4d1c23966685df588e3a193141404f2 100644 (file)
@@ -51,10 +51,13 @@ public class GCMIntentService extends GCMBaseIntentService {
 
        @Override
        protected void onMessage(@Nullable final Context context, @Nullable final Intent intent) {
+               if(context==null)
+                       return;
+
                handler.post(new Runnable(){
                        @Override
                        public void run() {
-                               new PollServerAsyncTask().execute();    
+                               new PollServerAsyncTask(context).execute();     
                        }
                });
        }
index df48722ea5b6dacb9cd65102165b54945481d1a2..905b577d34ac187de0bac36072e5f564a84cafdb 100644 (file)
@@ -1507,7 +1507,7 @@ final class Heavy {
         */
        public static void poll(final Context context, final Address replyTo) {
                Utils.sendMessage(context, replyTo, polling_server);
-               new PollServerAsyncTask().execute();
+               new PollServerAsyncTask(context).execute();
        }
 
        /**
index 6605e3de5bb2d58beb4a6d70170cedcc5e47e0f8..88cf462b776d93cc0bf3e6f0d45d3fbb924b7c30 100644 (file)
@@ -38,12 +38,12 @@ public final class LocalBroadcastReceiver extends BroadcastReceiver {
 
        @Override
        public void onReceive(final @Nullable Context context, final @Nullable Intent intent) {
-               if(intent==null)
+               if(context==null || intent==null)
                        return;
 
                final String action=intent.getAction();
                if(action.equals(LocalBroadcastReceiver.ACTION_POLL_ALARM))
-                       new PollServerAsyncTask().execute();
+                       new PollServerAsyncTask(context).execute();
        }
 
 }
index 7e95303090df4f2a6cfc5d58006d211cd0d222c9..2a4a21d60fe73661bdc9b652f3647eb4a7d791bd 100644 (file)
@@ -15,6 +15,7 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import ro.ieval.fonbot.Address.Protocol;
+import android.content.Context;
 import android.os.AsyncTask;
 import android.util.Log;
 
@@ -76,12 +77,24 @@ public final class PollServerAsyncTask extends AsyncTask<Void, Void, List<PollSe
                }
        }
 
+       /** Context instance */
+       private final Context context;
+
+       /**
+        * Constructs a PollServerAsyncTask.
+        *
+        * @param context Context instance
+        */
+       public PollServerAsyncTask(final Context context) {
+               this.context=context;
+       }
+
        @Override
        protected List<Command> doInBackground(final @Nullable Void... params) {
                Log.d(getClass().getName(), "Polling server");
                final List<Command> commands=new ArrayList<Command>(10);
                try {
-                       final URL url=new URL("http://ieval.ro:7777/get");
+                       final URL url=Utils.getServerURL(toNonNull(context),"/get");
                        final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                        conn.setRequestProperty("X-ID", FonBotApplication.instance.regID);
                        conn.connect();
index e27848159817262d3d9462947b6c7b01fa80170f..5087b0b9762dc46f4de5ba5b0e770159e26f9b0c 100644 (file)
@@ -1,7 +1,6 @@
 package ro.ieval.fonbot;
 
 import static ro.ieval.fonbot.R.string.*;
-
 import static ro.ieval.fonbot.Utils.toNonNull;
 
 import java.io.OutputStream;
@@ -14,7 +13,6 @@ 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;
 
 /*
@@ -128,9 +126,7 @@ public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, Stri
                        msg=data;
 
                try {
-                       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 URL url=Utils.getServerURL(toNonNull(context),"/");
                        final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                        conn.setDoOutput(true);
                        conn.setFixedLengthStreamingMode(msg.length);
@@ -149,6 +145,7 @@ public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, Stri
                                return toNonNull(context.getString(no_response_returned_from_server));
                        return Utils.parseHttpMessage(message);
                } catch (Exception e) {
+                       e.printStackTrace();
                        return toNonNull(context.getString(connection_error));
                }
        }
index d5e0f5bcf8de0d506748fedce0dbdaf143bf4e6d..117c528f6408d8cea153b5f665544009935329c0 100644 (file)
@@ -24,6 +24,8 @@ import static ro.ieval.fonbot.R.string.the_second_argument_to_wipe_must_be;
 import static ro.ieval.fonbot.R.string.unknown_command;
 import static ro.ieval.fonbot.R.string.wipetype_should_be_one_of;
 
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.Arrays;
 import java.util.Locale;
 
@@ -406,6 +408,21 @@ public final class Utils {
                context.startService(intent);
        }
 
+       /**
+        * Gets the server URL according to the user preferences.
+        *
+        * @param context Context instance
+        * @param path URL path
+        * @return the server URL
+        * @throws MalformedURLException if the user preferences create an invalid URL
+        */
+       public static URL getServerURL(final Context context, final String path) throws MalformedURLException{
+               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, path);
+               return url;
+       }
+
        /**
         * Executes a given command
         *
This page took 0.020797 seconds and 4 git commands to generate.