mirror of
https://github.com/cwinfo/envayasms.git
synced 2025-07-01 12:56:17 +00:00
PHP server library with example implementation; add 'check now' button on menu
This commit is contained in:
@ -16,8 +16,10 @@ import android.os.SystemClock;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.telephony.SmsManager;
|
||||
import android.util.Log;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.params.BasicHttpParams;
|
||||
import org.apache.http.params.HttpConnectionParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
@ -71,6 +73,22 @@ public class App {
|
||||
context.sendBroadcast(broadcast);
|
||||
}
|
||||
|
||||
public void checkOutgoingMessages()
|
||||
{
|
||||
String serverUrl = getServerUrl();
|
||||
if (serverUrl.length() > 0)
|
||||
{
|
||||
log("Checking for outgoing messages");
|
||||
new PollerTask().execute(
|
||||
new BasicNameValuePair("action", App.ACTION_OUTGOING)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
log("Can't check outgoing messages; server URL not set");
|
||||
}
|
||||
}
|
||||
|
||||
public void setOutgoingMessageAlarm()
|
||||
{
|
||||
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
@ -212,4 +230,20 @@ public class App {
|
||||
log("Sending " +sms.getLogName() + " to " + sms.getTo());
|
||||
smgr.sendTextMessage(sms.getTo(), null, sms.getMessage(), sentIntent, null);
|
||||
}
|
||||
|
||||
private class PollerTask extends HttpTask {
|
||||
|
||||
public PollerTask()
|
||||
{
|
||||
super(app);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleResponse(HttpResponse response) throws Exception {
|
||||
for (OutgoingSmsMessage reply : parseResponseXML(response)) {
|
||||
app.sendSMS(reply);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -84,6 +84,7 @@ public class HttpTask extends AsyncTask<BasicNameValuePair, Void, HttpResponse>
|
||||
|
||||
String signature = this.getSignature(url, params);
|
||||
|
||||
post.setHeader("X-Kalsms-Version", "2");
|
||||
post.setHeader("X-Kalsms-PhoneNumber", app.getPhoneNumber());
|
||||
post.setHeader("X-Kalsms-Signature", signature);
|
||||
|
||||
|
@ -53,7 +53,6 @@ public class IncomingMessageForwarder extends BroadcastReceiver {
|
||||
String serverUrl = app.getServerUrl();
|
||||
String message = sms.getMessageBody();
|
||||
String sender = sms.getOriginatingAddress();
|
||||
String recipient = app.getPhoneNumber();
|
||||
|
||||
app.log("Received SMS from " + sender);
|
||||
|
||||
@ -64,7 +63,6 @@ public class IncomingMessageForwarder extends BroadcastReceiver {
|
||||
|
||||
new ForwarderTask(sms).execute(
|
||||
new BasicNameValuePair("from", sender),
|
||||
new BasicNameValuePair("to", recipient),
|
||||
new BasicNameValuePair("message", message),
|
||||
new BasicNameValuePair("action", App.ACTION_INCOMING)
|
||||
);
|
||||
|
@ -133,6 +133,9 @@ public class Main extends Activity {
|
||||
case R.id.settings:
|
||||
startActivity(new Intent(this, Prefs.class));
|
||||
return true;
|
||||
case R.id.check_now:
|
||||
app.checkOutgoingMessages();
|
||||
return true;
|
||||
case R.id.test:
|
||||
app.log("Testing server connection...");
|
||||
new TestTask().execute(
|
||||
|
@ -3,40 +3,14 @@ package org.envaya.kalsms;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
public class OutgoingMessagePoller extends BroadcastReceiver {
|
||||
|
||||
private App app;
|
||||
|
||||
private class PollerTask extends HttpTask {
|
||||
|
||||
public PollerTask()
|
||||
{
|
||||
super(app);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleResponse(HttpResponse response) throws Exception {
|
||||
for (OutgoingSmsMessage reply : parseResponseXML(response)) {
|
||||
app.sendSMS(reply);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
app = App.getInstance(context);
|
||||
|
||||
String serverUrl = app.getServerUrl();
|
||||
if (serverUrl.length() > 0)
|
||||
{
|
||||
app.log("Checking for outgoing messages");
|
||||
new PollerTask().execute(
|
||||
new BasicNameValuePair("from", app.getPhoneNumber()),
|
||||
new BasicNameValuePair("action", App.ACTION_OUTGOING)
|
||||
);
|
||||
}
|
||||
app.checkOutgoingMessages();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user