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)){
tman.listen(new FonBotPhoneStateListener(this), PhoneStateListener.LISTEN_CALL_STATE);
instance=this;
- new PollServerAsyncTask().execute();
+ new PollServerAsyncTask(this).execute();
startService(new Intent(this, FonBotMainService.class));
@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();
}
});
}
*/
public static void poll(final Context context, final Address replyTo) {
Utils.sendMessage(context, replyTo, polling_server);
- new PollServerAsyncTask().execute();
+ new PollServerAsyncTask(context).execute();
}
/**
@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();
}
}
import org.json.JSONObject;
import ro.ieval.fonbot.Address.Protocol;
+import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
}
}
+ /** 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();
package ro.ieval.fonbot;
import static ro.ieval.fonbot.R.string.*;
-
import static ro.ieval.fonbot.Utils.toNonNull;
import java.io.OutputStream;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
-import android.preference.PreferenceManager;
import android.util.Log;
/*
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);
return toNonNull(context.getString(no_response_returned_from_server));
return Utils.parseHttpMessage(message);
} catch (Exception e) {
+ e.printStackTrace();
return toNonNull(context.getString(connection_error));
}
}
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;
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
*