android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
+ android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textIsSelectable="false" />
<string name="notification_canceled">Notification canceled</string>
<string name="notification_shown">Notification shown</string>
<string name="notify_help">Notify help</string>
+ <string name="user_or_password_not_set">User or password not set</string>
</resources>
\ No newline at end of file
package ro.ieval.fonbot;
-import static ro.ieval.fonbot.R.string.*;
-
+import static ro.ieval.fonbot.R.string.logging_in;
import static ro.ieval.fonbot.Utils.toNonNull;
-import java.util.Arrays;
+import java.util.Collections;
+
import org.eclipse.jdt.annotation.Nullable;
import ro.ieval.fonbot.FonBotMainService.Binder;
import ro.ieval.fonbot.Utils.OngoingEvent;
-
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
-import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
-import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.Menu;
@Override
protected void onStart() {
super.onStart();
- final SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
resultTextView.setText(logging_in);
- new SendHttpMessageAsyncTask(toNonNull(Arrays.asList(
- new Header("X-Action","LOGIN")
- )),LOGIN_BROADCAST,this).execute(
- sp.getString("username", null),
- sp.getString("password", null)
- );
+ new SendHttpMessageAsyncTask("/ok", toNonNull(Collections.<Header>emptySet()),
+ LOGIN_BROADCAST,this).execute();
connection.refreshAdapter();
}
import ro.ieval.fonbot.Address.Protocol;
import android.content.Context;
import android.os.AsyncTask;
+import android.preference.PreferenceManager;
+import android.util.Base64;
import android.util.Log;
/*
final URL url=Utils.getServerURL(toNonNull(context),"/get");
final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestProperty("X-ID", FonBotApplication.instance.regID);
+ final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
+ final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
+ if(user == null || password == null)
+ return commands;
+
+ conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
+ (user+':'+password).getBytes(), Base64.NO_WRAP));
conn.connect();
final byte[] buf=new byte[4096*1024];
Log.d(getClass().getName(), "Server poll got response code "+conn.getResponseCode()+" and message "+conn.getResponseMessage());
import java.io.IOException;
import java.io.PrintStream;
import java.lang.Thread.UncaughtExceptionHandler;
-import java.util.Arrays;
+import java.util.Collections;
import org.eclipse.jdt.annotation.Nullable;
ex.printStackTrace(pw);
pw.close();
- new SendHttpMessageAsyncTask(toNonNull(Arrays.asList(
- new Header("X-Action", "CRASHDUMP")
- )), toNonNull(context), toNonNull(baos.toByteArray())).execute();
+ new SendHttpMessageAsyncTask("/crashdump", toNonNull(Collections.<Header>emptyList()),
+ toNonNull(context), toNonNull(baos.toByteArray())).execute();
try {
baos.close();
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
+import android.preference.PreferenceManager;
+import android.util.Base64;
import android.util.Log;
/*
* Data to send to the server
*/
private final byte[] data;
+ /**
+ * Server URL path
+ */
+ private final String path;
/**
* Response code from the server
*/
- private int responseCode;
+ private int responseCode=0;
/**
* Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
*
+ * @param path URL path
* @param headers the extra headers
* @param context the context instance
*/
- public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context){
+ public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context){
super();
+ this.path=path;
this.headers=headers;
this.broadcast=null;//NOPMD final field
this.context=context;
/**
* Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
*
+ * @param path URL path
* @param headers the extra headers
* @param context the context instance
* @param data the message to send
*/
- public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable.
+ public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable.
super();
+ this.path=path;
this.headers=headers;
this.broadcast=null;//NOPMD final field
this.context=context;
/**
* Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
*
+ * @param path URL path
* @param headers the extra headers
* @param broadcast the broadcast to send
* @param context the context instance
*/
- public SendHttpMessageAsyncTask(final Collection<Header> headers, final String broadcast, final Context context){
+ public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final String broadcast, final Context context){
super();
+ this.path=path;
this.headers=headers;
this.broadcast=broadcast;
this.context=context;
return "";
final byte[] msg;
- if(data==null)
+ if(data==null && args.length > 0)
msg=Utils.join(" ", args).getBytes();
- else
+ else if(data!=null)
msg=data;
+ else
+ msg=null;
try {
- final URL url=Utils.getServerURL(toNonNull(context),"/");
+ final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
- conn.setDoOutput(true);
- conn.setFixedLengthStreamingMode(msg.length);
+ if(msg!=null){
+ conn.setDoOutput(true);
+ conn.setFixedLengthStreamingMode(msg.length);
+ }
conn.setRequestProperty("X-ID", FonBotApplication.instance.regID);
+ final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
+ final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
+ if(user == null || password == null)
+ return toNonNull(context.getString(user_or_password_not_set));
+
+ conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
+ (user+':'+password).getBytes(), Base64.NO_WRAP));
for (Header header : headers)
conn.setRequestProperty(header.name, header.value);
conn.connect();
- final OutputStream stream=conn.getOutputStream();
- stream.write(msg);
- stream.close();
+ if(msg!=null){
+ final OutputStream stream=conn.getOutputStream();
+ stream.write(msg);
+ stream.close();
+ }
Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
conn.disconnect();
final String message=conn.getResponseMessage();
public static void sendMessage(final Context context, final Address address, final String message){
switch(address.protocol){
case HTTP:
- new SendHttpMessageAsyncTask(toNonNull(Arrays.asList(
- new Header("X-Action", "SEND "+address.data))), context).execute(message);
+ new SendHttpMessageAsyncTask("/send", toNonNull(Arrays.asList(
+ new Header("X-Destination", toNonNull(address.data)))), context).execute(message);
break;
case SMS:
final int indexOfParen=message.indexOf(')');
if(indexOfParen==-1)
return message;
+ if(indexOfParen == message.length()-1)
+ return toNonNull(message.substring(1, indexOfParen));
return toNonNull(message.substring(indexOfParen+2));
}