From a97d31fb5c1bc1725079b5329168b22db3cf9cf3 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu <marius@ieval.ro> Date: Thu, 7 Mar 2013 00:08:38 +0200 Subject: [PATCH] Make custom server work when polling 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 | 2 +- src/ro/ieval/fonbot/FonBotApplication.java | 2 +- src/ro/ieval/fonbot/GCMIntentService.java | 5 ++++- src/ro/ieval/fonbot/Heavy.java | 2 +- src/ro/ieval/fonbot/LocalBroadcastReceiver.java | 4 ++-- src/ro/ieval/fonbot/PollServerAsyncTask.java | 15 ++++++++++++++- .../ieval/fonbot/SendHttpMessageAsyncTask.java | 7 ++----- src/ro/ieval/fonbot/Utils.java | 17 +++++++++++++++++ 8 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/ro/ieval/fonbot/DynamicEventReceiver.java b/src/ro/ieval/fonbot/DynamicEventReceiver.java index 926280b..d7acf54 100644 --- a/src/ro/ieval/fonbot/DynamicEventReceiver.java +++ b/src/ro/ieval/fonbot/DynamicEventReceiver.java @@ -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)){ diff --git a/src/ro/ieval/fonbot/FonBotApplication.java b/src/ro/ieval/fonbot/FonBotApplication.java index 46a0328..38b5b40 100644 --- a/src/ro/ieval/fonbot/FonBotApplication.java +++ b/src/ro/ieval/fonbot/FonBotApplication.java @@ -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)); diff --git a/src/ro/ieval/fonbot/GCMIntentService.java b/src/ro/ieval/fonbot/GCMIntentService.java index de38208..aea7202 100644 --- a/src/ro/ieval/fonbot/GCMIntentService.java +++ b/src/ro/ieval/fonbot/GCMIntentService.java @@ -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(); } }); } diff --git a/src/ro/ieval/fonbot/Heavy.java b/src/ro/ieval/fonbot/Heavy.java index df48722..905b577 100644 --- a/src/ro/ieval/fonbot/Heavy.java +++ b/src/ro/ieval/fonbot/Heavy.java @@ -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(); } /** diff --git a/src/ro/ieval/fonbot/LocalBroadcastReceiver.java b/src/ro/ieval/fonbot/LocalBroadcastReceiver.java index 6605e3d..88cf462 100644 --- a/src/ro/ieval/fonbot/LocalBroadcastReceiver.java +++ b/src/ro/ieval/fonbot/LocalBroadcastReceiver.java @@ -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(); } } diff --git a/src/ro/ieval/fonbot/PollServerAsyncTask.java b/src/ro/ieval/fonbot/PollServerAsyncTask.java index 7e95303..2a4a21d 100644 --- a/src/ro/ieval/fonbot/PollServerAsyncTask.java +++ b/src/ro/ieval/fonbot/PollServerAsyncTask.java @@ -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(); diff --git a/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java b/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java index e278481..5087b0b 100644 --- a/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java +++ b/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java @@ -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)); } } diff --git a/src/ro/ieval/fonbot/Utils.java b/src/ro/ieval/fonbot/Utils.java index d5e0f5b..117c528 100644 --- a/src/ro/ieval/fonbot/Utils.java +++ b/src/ro/ieval/fonbot/Utils.java @@ -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 * -- 2.39.5