5
0
mirror of https://github.com/cwinfo/envayasms.git synced 2024-12-04 20:45:32 +00:00

instead of deleting messages from inbox, prevent them from getting there in the first place (unless user wants to keep them)

This commit is contained in:
Jesse Young 2011-09-13 12:46:37 -07:00
parent bead45360f
commit e535266d9a
5 changed files with 273 additions and 366 deletions

View File

@ -24,7 +24,7 @@
</activity>
<receiver android:name=".IncomingMessageForwarder">
<intent-filter>
<intent-filter android:priority="101">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

View File

@ -30,5 +30,14 @@
<CheckBoxPreference
android:key="launch_on_boot"
android:title="Launch automatically?"
android:summaryOff="KalSMS will not open when phone boots (but will still forward SMS)"
android:summaryOn="KalSMS will open when phone boots"
></CheckBoxPreference>
<CheckBoxPreference
android:key="keep_in_inbox"
android:title="Keep new messages?"
android:summaryOff="Incoming SMS will not be stored in Messaging inbox"
android:summaryOn="Incoming SMS will be stored in Messaging inbox"
></CheckBoxPreference>
</PreferenceScreen>

View File

@ -7,9 +7,11 @@ package org.envaya.kalsms;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.SystemClock;
import android.preference.PreferenceManager;
@ -26,40 +28,32 @@ public class App {
public static final String ACTION_OUTGOING = "outgoing";
public static final String ACTION_INCOMING = "incoming";
public static final String ACTION_SEND_STATUS = "send_status";
public static final String STATUS_QUEUED = "queued";
public static final String STATUS_FAILED = "failed";
public static final String STATUS_SENT = "sent";
public static final String LOG_NAME = "KALSMS";
public static final String LOG_INTENT = "org.envaya.kalsms.LOG";
private static App app;
private Map<String, QueuedIncomingSms> incomingSmsMap = new HashMap<String, QueuedIncomingSms>();
private Map<String, QueuedOutgoingSms> outgoingSmsMap = new HashMap<String, QueuedOutgoingSms>();
private Map<String, QueuedOutgoingSms> outgoingSmsMap = new HashMap<String, QueuedOutgoingSms>();
public Context context;
public SharedPreferences settings;
private abstract class QueuedMessage<T>
{
private abstract class QueuedMessage<T> {
public T sms;
public long nextAttemptTime = 0;
public int numAttempts = 0;
public boolean canAttemptNow()
{
public boolean canAttemptNow() {
return (nextAttemptTime > 0 && nextAttemptTime < SystemClock.elapsedRealtime());
}
public boolean scheduleNextAttempt()
{
public boolean scheduleNextAttempt() {
long now = SystemClock.elapsedRealtime();
numAttempts++;
if (numAttempts > 4)
{
if (numAttempts > 4) {
log("5th failure: giving up");
return false;
}
@ -67,23 +61,16 @@ public class App {
int second = 1000;
int minute = second * 60;
if (numAttempts == 1)
{
if (numAttempts == 1) {
log("1st failure; retry in 1 minute");
nextAttemptTime = now + 1 * minute;
}
else if (numAttempts == 2)
{
} else if (numAttempts == 2) {
log("2nd failure; retry in 10 minutes");
nextAttemptTime = now + 10 * minute;
}
else if (numAttempts == 3)
{
} else if (numAttempts == 3) {
log("3rd failure; retry in 1 hour");
nextAttemptTime = now + 60 * minute;
}
else
{
} else {
log("4th failure: retry in 1 day");
nextAttemptTime = now + 24 * 60 * minute;
}
@ -91,36 +78,35 @@ public class App {
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0,
getAttemptIntent(),
0);
0,
getAttemptIntent(),
0);
alarm.set(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
nextAttemptTime,
pendingIntent);
AlarmManager.ELAPSED_REALTIME_WAKEUP,
nextAttemptTime,
pendingIntent);
return true;
}
public abstract void attemptNow();
protected abstract Intent getAttemptIntent();
}
private class QueuedIncomingSms extends QueuedMessage<SmsMessage> {
public QueuedIncomingSms(SmsMessage sms)
{
public QueuedIncomingSms(SmsMessage sms) {
this.sms = sms;
}
public void attemptNow()
{
public void attemptNow() {
log("Retrying forwarding SMS from " + sms.getOriginatingAddress());
trySendMessageToServer(sms);
}
protected Intent getAttemptIntent()
{
protected Intent getAttemptIntent() {
Intent intent = new Intent(context, IncomingMessageRetry.class);
intent.setData(Uri.parse("kalsms://incoming/" + getSmsId(sms)));
return intent;
@ -128,19 +114,17 @@ public class App {
}
private class QueuedOutgoingSms extends QueuedMessage<OutgoingSmsMessage> {
public QueuedOutgoingSms(OutgoingSmsMessage sms)
{
public QueuedOutgoingSms(OutgoingSmsMessage sms) {
this.sms = sms;
}
public void attemptNow()
{
log("Retrying sending " +sms.getLogName() + " to " + sms.getTo());
public void attemptNow() {
log("Retrying sending " + sms.getLogName() + " to " + sms.getTo());
trySendSMS(sms);
}
protected Intent getAttemptIntent()
{
protected Intent getAttemptIntent() {
Intent intent = new Intent(context, OutgoingMessageRetry.class);
intent.setData(Uri.parse("kalsms://outgoing/" + sms.getId()));
log("id=" + sms.getId());
@ -148,28 +132,23 @@ public class App {
}
}
protected App(Context context)
{
protected App(Context context) {
this.context = context;
this.settings = PreferenceManager.getDefaultSharedPreferences(context);
}
public static App getInstance(Context context)
{
if (app == null)
{
public static App getInstance(Context context) {
if (app == null) {
app = new App(context);
}
return app;
}
public void debug(String msg)
{
public void debug(String msg) {
Log.d(LOG_NAME, msg);
}
public void log(String msg)
{
public void log(String msg) {
Log.d(LOG_NAME, msg);
Intent broadcast = new Intent(App.LOG_INTENT);
@ -177,24 +156,18 @@ public class App {
context.sendBroadcast(broadcast);
}
public void checkOutgoingMessages()
{
public void checkOutgoingMessages() {
String serverUrl = getServerUrl();
if (serverUrl.length() > 0)
{
if (serverUrl.length() > 0) {
log("Checking for outgoing messages");
new PollerTask().execute(
new BasicNameValuePair("action", App.ACTION_OUTGOING)
);
}
else
{
new BasicNameValuePair("action", App.ACTION_OUTGOING));
} else {
log("Can't check outgoing messages; server URL not set");
}
}
public void setOutgoingMessageAlarm()
{
public void setOutgoingMessageAlarm() {
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
@ -206,155 +179,124 @@ public class App {
int pollSeconds = getOutgoingPollSeconds();
if (pollSeconds > 0)
{
if (pollSeconds > 0) {
alarm.setRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
pollSeconds * 1000,
pendingIntent);
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
pollSeconds * 1000,
pendingIntent);
log("Checking for outgoing messages every " + pollSeconds + " sec");
}
else
{
} else {
log("Not checking for outgoing messages.");
}
}
public void logError(Throwable ex)
{
public void logError(Throwable ex) {
logError("ERROR", ex);
}
public void logError(String msg, Throwable ex)
{
public void logError(String msg, Throwable ex) {
logError(msg, ex, false);
}
public void logError(String msg, Throwable ex, boolean detail)
{
public void logError(String msg, Throwable ex, boolean detail) {
log(msg + ": " + ex.getClass().getName() + ": " + ex.getMessage());
if (detail)
{
for (StackTraceElement elem : ex.getStackTrace())
{
if (detail) {
for (StackTraceElement elem : ex.getStackTrace()) {
log(elem.getClassName() + ":" + elem.getMethodName() + ":" + elem.getLineNumber());
}
Throwable innerEx = ex.getCause();
if (innerEx != null)
{
if (innerEx != null) {
logError("Inner exception:", innerEx, true);
}
}
}
public String getDisplayString(String str)
{
if (str.length() == 0)
{
public String getDisplayString(String str) {
if (str.length() == 0) {
return "(not set)";
}
else
{
} else {
return str;
}
}
public String getServerUrl()
{
public String getServerUrl() {
return settings.getString("server_url", "");
}
public String getPhoneNumber()
{
public String getPhoneNumber() {
return settings.getString("phone_number", "");
}
public int getOutgoingPollSeconds()
{
public int getOutgoingPollSeconds() {
return Integer.parseInt(settings.getString("outgoing_interval", "0"));
}
public boolean getLaunchOnBoot()
{
return settings.getBoolean("launch_on_boot", true);
public boolean getLaunchOnBoot() {
return settings.getBoolean("launch_on_boot", false);
}
public String getPassword()
public boolean getKeepInInbox()
{
return settings.getBoolean("keep_in_inbox", false);
}
public String getPassword() {
return settings.getString("password", "");
}
private void notifyStatus(OutgoingSmsMessage sms, String status, String errorMessage)
{
private void notifyStatus(OutgoingSmsMessage sms, String status, String errorMessage) {
String serverId = sms.getServerId();
String logMessage;
if (status.equals(App.STATUS_SENT))
{
if (status.equals(App.STATUS_SENT)) {
logMessage = "sent successfully";
}
else if (status.equals(App.STATUS_FAILED))
{
} else if (status.equals(App.STATUS_FAILED)) {
logMessage = "could not be sent (" + errorMessage + ")";
}
else
{
} else {
logMessage = "queued";
}
String smsDesc = sms.getLogName();
if (serverId != null)
{
if (serverId != null) {
app.log("Notifying server " + smsDesc + " " + logMessage);
new HttpTask(app).execute(
new BasicNameValuePair("id", serverId),
new BasicNameValuePair("status", status),
new BasicNameValuePair("error", errorMessage),
new BasicNameValuePair("action", App.ACTION_SEND_STATUS)
);
}
else
{
new BasicNameValuePair("id", serverId),
new BasicNameValuePair("status", status),
new BasicNameValuePair("error", errorMessage),
new BasicNameValuePair("action", App.ACTION_SEND_STATUS));
} else {
app.log(smsDesc + " " + logMessage);
}
}
public synchronized void retryStuckMessages()
{
public synchronized void retryStuckMessages() {
retryStuckOutgoingMessages();
retryStuckIncomingMessages();
}
public synchronized int getStuckMessageCount()
{
public synchronized int getStuckMessageCount() {
return outgoingSmsMap.size() + incomingSmsMap.size();
}
public synchronized void retryStuckOutgoingMessages()
{
for (QueuedOutgoingSms queuedSms : outgoingSmsMap.values())
{
public synchronized void retryStuckOutgoingMessages() {
for (QueuedOutgoingSms queuedSms : outgoingSmsMap.values()) {
queuedSms.attemptNow();
}
}
public synchronized void retryStuckIncomingMessages()
{
for (QueuedIncomingSms queuedSms : incomingSmsMap.values())
{
public synchronized void retryStuckIncomingMessages() {
for (QueuedIncomingSms queuedSms : incomingSmsMap.values()) {
queuedSms.attemptNow();
}
}
public synchronized void notifyOutgoingMessageStatus(String id, int resultCode)
{
public synchronized void notifyOutgoingMessageStatus(String id, int resultCode) {
QueuedOutgoingSms queuedSms = outgoingSmsMap.get(id);
if (queuedSms == null)
{
if (queuedSms == null) {
return;
}
@ -385,8 +327,7 @@ public class App {
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_RADIO_OFF:
case SmsManager.RESULT_ERROR_NO_SERVICE:
if (!queuedSms.scheduleNextAttempt())
{
if (!queuedSms.scheduleNextAttempt()) {
outgoingSmsMap.remove(id);
}
break;
@ -397,11 +338,9 @@ public class App {
}
public synchronized void sendSMS(OutgoingSmsMessage sms)
{
public synchronized void sendSMS(OutgoingSmsMessage sms) {
String id = sms.getId();
if (outgoingSmsMap.containsKey(id))
{
if (outgoingSmsMap.containsKey(id)) {
log(sms.getLogName() + " already sent, skipping");
return;
}
@ -409,12 +348,11 @@ public class App {
QueuedOutgoingSms queueEntry = new QueuedOutgoingSms(sms);
outgoingSmsMap.put(id, queueEntry);
log("Sending " +sms.getLogName() + " to " + sms.getTo());
log("Sending " + sms.getLogName() + " to " + sms.getTo());
trySendSMS(sms);
}
private void trySendSMS(OutgoingSmsMessage sms)
{
private void trySendSMS(OutgoingSmsMessage sms) {
SmsManager smgr = SmsManager.getDefault();
Intent intent = new Intent(context, MessageStatusNotifier.class);
@ -431,8 +369,7 @@ public class App {
private class PollerTask extends HttpTask {
public PollerTask()
{
public PollerTask() {
super(app);
}
@ -444,7 +381,6 @@ public class App {
}
}
private class ForwarderTask extends HttpTask {
private SmsMessage originalSms;
@ -455,8 +391,7 @@ public class App {
}
@Override
protected String getDefaultToAddress()
{
protected String getDefaultToAddress() {
return originalSms.getOriginatingAddress();
}
@ -471,22 +406,18 @@ public class App {
}
@Override
protected void handleFailure()
{
protected void handleFailure() {
app.notifyIncomingMessageStatus(originalSms, false);
}
}
private String getSmsId(SmsMessage sms)
{
private String getSmsId(SmsMessage sms) {
return sms.getOriginatingAddress() + ":" + sms.getMessageBody() + ":" + sms.getTimestampMillis();
}
public synchronized void sendMessageToServer(SmsMessage sms)
{
public synchronized void sendMessageToServer(SmsMessage sms) {
String id = getSmsId(sms);
if (incomingSmsMap.containsKey(id))
{
if (incomingSmsMap.containsKey(id)) {
log("Duplicate incoming SMS, skipping");
return;
}
@ -499,50 +430,40 @@ public class App {
trySendMessageToServer(sms);
}
public void trySendMessageToServer(SmsMessage sms)
{
public void trySendMessageToServer(SmsMessage sms) {
String message = sms.getMessageBody();
String sender = sms.getOriginatingAddress();
new ForwarderTask(sms).execute(
new BasicNameValuePair("from", sender),
new BasicNameValuePair("message", message),
new BasicNameValuePair("action", App.ACTION_INCOMING)
);
new BasicNameValuePair("from", sender),
new BasicNameValuePair("message", message),
new BasicNameValuePair("action", App.ACTION_INCOMING));
}
private synchronized void notifyIncomingMessageStatus(SmsMessage sms, boolean success)
{
private synchronized void notifyIncomingMessageStatus(SmsMessage sms, boolean success) {
String id = getSmsId(sms);
QueuedIncomingSms queuedSms = incomingSmsMap.get(id);
if (queuedSms != null)
{
if (success || !queuedSms.scheduleNextAttempt())
{
if (queuedSms != null) {
if (success || !queuedSms.scheduleNextAttempt()) {
incomingSmsMap.remove(id);
}
}
}
public synchronized void retryIncomingMessage(String id)
{
public synchronized void retryIncomingMessage(String id) {
QueuedIncomingSms queuedSms = incomingSmsMap.get(id);
if (queuedSms != null)
{
if (queuedSms != null) {
queuedSms.attemptNow();
}
}
public synchronized void retryOutgoingMessage(String id)
{
public synchronized void retryOutgoingMessage(String id) {
QueuedOutgoingSms queuedSms = outgoingSmsMap.get(id);
if (queuedSms != null)
{
if (queuedSms != null) {
queuedSms.attemptNow();
}
}
}

View File

@ -7,7 +7,9 @@ package org.envaya.kalsms;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -50,41 +52,35 @@ public class HttpTask extends AsyncTask<BasicNameValuePair, Void, HttpResponse>
}
private String getSignature(String url, List<BasicNameValuePair> params)
throws NoSuchAlgorithmException, UnsupportedEncodingException
{
try {
Collections.sort(params, new Comparator() {
public int compare(Object o1, Object o2)
{
return ((BasicNameValuePair)o1).getName().compareTo(((BasicNameValuePair)o2).getName());
}
});
StringBuilder builder = new StringBuilder();
builder.append(url);
for (BasicNameValuePair param : params)
Collections.sort(params, new Comparator() {
public int compare(Object o1, Object o2)
{
builder.append(",");
builder.append(param.getName());
builder.append("=");
builder.append(param.getValue());
return ((BasicNameValuePair)o1).getName().compareTo(((BasicNameValuePair)o2).getName());
}
});
StringBuilder builder = new StringBuilder();
builder.append(url);
for (BasicNameValuePair param : params)
{
builder.append(",");
builder.append(app.getPassword());
String value = builder.toString();
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(value.getBytes("utf-8"));
byte[] digest = md.digest();
return new String(Base64Coder.encode(digest));
} catch (Exception ex) {
app.logError("Error computing signature", ex);
builder.append(param.getName());
builder.append("=");
builder.append(param.getValue());
}
return "";
builder.append(",");
builder.append(app.getPassword());
String value = builder.toString();
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(value.getBytes("utf-8"));
byte[] digest = md.digest();
return new String(Base64Coder.encode(digest));
}
protected HttpResponse doInBackground(BasicNameValuePair... params) {

View File

@ -14,17 +14,20 @@ public class IncomingMessageForwarder extends BroadcastReceiver {
@Override
// source: http://www.devx.com/wireless/Article/39495/1954
public void onReceive(Context context, Intent intent) {
try {
this.app = App.getInstance(context.getApplicationContext());
app = App.getInstance(context.getApplicationContext());
try {
String action = intent.getAction();
if (action.equals("android.provider.Telephony.SMS_RECEIVED")) {
for (SmsMessage sms : getMessagesFromIntent(intent)) {
app.sendMessageToServer(sms);
}
//DeleteSMSFromInbox(context, mesg);
if (!app.getKeepInInbox())
{
this.abortBroadcast();
}
}
} catch (Throwable ex) {
@ -32,28 +35,6 @@ public class IncomingMessageForwarder extends BroadcastReceiver {
}
}
/*
private void DeleteSMSFromInbox(Context context, SmsMessage mesg) {
Log.d("KALSMS", "try to delete SMS");
try {
Uri uriSms = Uri.parse("content://sms/inbox");
StringBuilder sb = new StringBuilder();
sb.append("address='" + mesg.getOriginatingAddress() + "' AND ");
sb.append("body='" + mesg.getMessageBody() + "'");
Cursor c = context.getContentResolver().query(uriSms, null, sb.toString(), null, null);
c.moveToFirst();
int thread_id = c.getInt(1);
context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id), null, null);
c.close();
} catch (Exception ex) {
// deletions don't work most of the time since the timing of the
// receipt and saving to the inbox
// makes it difficult to match up perfectly. the SMS might not be in
// the inbox yet when this receiver triggers!
Log.d("SmsReceiver", "Error deleting sms from inbox: " + ex.getMessage());
}
}
*/
// from http://github.com/dimagi/rapidandroid
// source: http://www.devx.com/wireless/Article/39495/1954
private SmsMessage[] getMessagesFromIntent(Intent intent) {