From d13f1533daefa5353d59d79f804931777a3aace6 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Mon, 11 Mar 2013 17:27:54 +0200 Subject: [PATCH] Upgrade to the latest server protocol. Server protocol changes: * All server requests use Basic access authentication * There is no more X-Action. Server paths are used instead: - /send for sending messages, with the destination address in the X-Destination header. - /ok for doing nothing. Used for the main activity login status. Other minor changes: * Centered the resultTextView in main.xml. * Utils#parseHttpMessage can parse messages like "(text)". --- res/layout/main.xml | 1 + res/values/strings.xml | 1 + src/ro/ieval/fonbot/FonBotMainActivity.java | 18 ++----- src/ro/ieval/fonbot/PollServerAsyncTask.java | 9 ++++ .../ieval/fonbot/RemoteCrashdumpHandler.java | 7 ++- .../fonbot/SendHttpMessageAsyncTask.java | 49 ++++++++++++++----- src/ro/ieval/fonbot/Utils.java | 6 ++- 7 files changed, 60 insertions(+), 31 deletions(-) diff --git a/res/layout/main.xml b/res/layout/main.xml index 1fc54d2..90b6d4a 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -9,6 +9,7 @@ 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" /> diff --git a/res/values/strings.xml b/res/values/strings.xml index 6e75444..bdc90a6 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -444,4 +444,5 @@ The help command can be used to get a list of commands and help for them. Exampl Notification canceled Notification shown Notify help + User or password not set \ No newline at end of file diff --git a/src/ro/ieval/fonbot/FonBotMainActivity.java b/src/ro/ieval/fonbot/FonBotMainActivity.java index 92a81af..7ef45b8 100644 --- a/src/ro/ieval/fonbot/FonBotMainActivity.java +++ b/src/ro/ieval/fonbot/FonBotMainActivity.java @@ -1,25 +1,22 @@ 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; @@ -204,14 +201,9 @@ public final class FonBotMainActivity extends ListActivity { @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.
emptySet()), + LOGIN_BROADCAST,this).execute(); connection.refreshAdapter(); } diff --git a/src/ro/ieval/fonbot/PollServerAsyncTask.java b/src/ro/ieval/fonbot/PollServerAsyncTask.java index 2a4a21d..15bf9cc 100644 --- a/src/ro/ieval/fonbot/PollServerAsyncTask.java +++ b/src/ro/ieval/fonbot/PollServerAsyncTask.java @@ -17,6 +17,8 @@ import org.json.JSONObject; 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; /* @@ -97,6 +99,13 @@ public final class PollServerAsyncTask extends AsyncTaskemptyList()), + toNonNull(context), toNonNull(baos.toByteArray())).execute(); try { baos.close(); diff --git a/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java b/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java index 5087b0b..c05dd05 100644 --- a/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java +++ b/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java @@ -13,6 +13,8 @@ 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.Base64; import android.util.Log; /* @@ -65,19 +67,25 @@ public final class SendHttpMessageAsyncTask extends AsyncTask headers, final Context context){ + public SendHttpMessageAsyncTask(final String path, final Collection
headers, final Context context){ super(); + this.path=path; this.headers=headers; this.broadcast=null;//NOPMD final field this.context=context; @@ -87,12 +95,14 @@ public final class SendHttpMessageAsyncTask extends AsyncTask headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable. + public SendHttpMessageAsyncTask(final String path, final Collection
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; @@ -102,12 +112,14 @@ public final class SendHttpMessageAsyncTask extends AsyncTask headers, final String broadcast, final Context context){ + public SendHttpMessageAsyncTask(final String path, final Collection
headers, final String broadcast, final Context context){ super(); + this.path=path; this.headers=headers; this.broadcast=broadcast; this.context=context; @@ -120,23 +132,36 @@ public final class SendHttpMessageAsyncTask extends AsyncTask 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(); diff --git a/src/ro/ieval/fonbot/Utils.java b/src/ro/ieval/fonbot/Utils.java index 117c528..d9060eb 100644 --- a/src/ro/ieval/fonbot/Utils.java +++ b/src/ro/ieval/fonbot/Utils.java @@ -316,8 +316,8 @@ public final class Utils { 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: @@ -345,6 +345,8 @@ public final class Utils { 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)); } -- 2.39.2