From 9a574b3ab4eddbf6a221e3c4bebe7ed489530d9a Mon Sep 17 00:00:00 2001 From: Jesse Young Date: Mon, 19 Sep 2011 18:51:45 -0700 Subject: [PATCH] add foreground service to keep App in memory (otherwise rate-limiting won't work) and provide notification --- AndroidManifest.xml | 3 + LICENSE | 5 +- res/values/strings.xml | 1 + server/php/KalSMS.php | 63 +++++- src/org/envaya/kalsms/App.java | 11 +- .../envaya/kalsms/CheckMmsInboxService.java | 9 +- src/org/envaya/kalsms/ForegroundService.java | 188 ++++++++++++++++++ src/org/envaya/kalsms/MmsObserver.java | 3 - src/org/envaya/kalsms/task/HttpTask.java | 6 +- src/org/envaya/kalsms/ui/Prefs.java | 1 + 10 files changed, 268 insertions(+), 22 deletions(-) create mode 100755 src/org/envaya/kalsms/ForegroundService.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 31cc376..733f7b1 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -82,6 +82,9 @@ + + + diff --git a/LICENSE b/LICENSE index 699688d..806fed2 100644 --- a/LICENSE +++ b/LICENSE @@ -38,6 +38,7 @@ org.envaya.kalsms.ui.InertCheckBox and org.envaya.kalsms.ui.CheckableRelativeLay is (c) Cédric Caron, released presumably into the public domain at http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/ -org.envaya.kalsms.App.chooseOutgoingSmsPackage includes code from Android, - Copyright 2005-2008 The Android Open Source Project +org.envaya.kalsms.App.chooseOutgoingSmsPackage and +org.envaya.kalsms.ForegroundService include code from Android, + Copyright 2005-2009 The Android Open Source Project See NOTICE \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index b95d4d7..80e5f06 100755 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7,6 +7,7 @@ Help Retry Fwd Inbox... + New SMS will be forwarded to server When running KalSMS in Test Mode, KalSMS will only forward SMS messages from the phone numbers listed below. (Incoming SMS messages from other phone numbers will be saved diff --git a/server/php/KalSMS.php b/server/php/KalSMS.php index 38785a1..782b0a6 100755 --- a/server/php/KalSMS.php +++ b/server/php/KalSMS.php @@ -16,10 +16,16 @@ class KalSMS const STATUS_FAILED = 'failed'; const STATUS_SENT = 'sent'; + const MESSAGE_TYPE_SMS = 'sms'; + const MESSAGE_TYPE_MMS = 'mms'; + static function new_from_request() { - $version = @$_POST['version']; - + $version = @$_POST['version']; + + // If API version changes, could return different KalSMS instance + // to support multiple phone versions + return new KalSMS(); } @@ -96,9 +102,9 @@ class KalSMS class KalSMS_OutgoingMessage { - public $id = ''; - public $to; - public $message; + public $id = ''; // ID generated by server + public $to; // destination phone number + public $message; // content of SMS message } class KalSMS_Action @@ -121,10 +127,37 @@ class KalSMS_Action_Test extends KalSMS_Action } } +class KalSMS_MMS_Part +{ + public $form_name; // name of form field with MMS part content + public $cid; // MMS Content-ID + public $type; // Content type + public $filename; // Original filename of MMS part on sender phone + public $tmp_name; // Temporary file where MMS part content is stored + public $size; // Content length + public $error; // see http://www.php.net/manual/en/features.file-upload.errors.php + + function __construct($args) + { + $this->form_name = $args['name']; + $this->cid = $args['cid']; + $this->type = $args['type']; + $this->filename = $args['filename']; + + $file = $_FILES[$this->form_name]; + + $this->tmp_name = $file['tmp_name']; + $this->size = $file['size']; + $this->error = $file['error']; + } +} + class KalSMS_Action_Incoming extends KalSMS_Action { - public $from; - public $message; + public $from; // Sender phone number + public $message; // The message body of the SMS, or the content of the text/plain part of the MMS. + public $message_type; // KalSMS::MESSAGE_TYPE_MMS or KalSMS::MESSAGE_TYPE_SMS + public $mms_parts; // array of KalSMS_MMS_Part instances function __construct($kalsms) { @@ -132,6 +165,16 @@ class KalSMS_Action_Incoming extends KalSMS_Action $this->type = KalSMS::ACTION_INCOMING; $this->from = $_POST['from']; $this->message = $_POST['message']; + $this->message_type = $_POST['message_type']; + + if ($this->message_type == KalSMS::MESSAGE_TYPE_MMS) + { + $this->mms_parts = array(); + foreach (json_decode($_POST['mms_parts'], true) as $mms_part) + { + $this->mms_parts[] = new KalSMS_MMS_Part($mms_part); + } + } } function get_response_xml($messages) @@ -173,9 +216,9 @@ class KalSMS_Action_Outgoing extends KalSMS_Action class KalSMS_Action_SendStatus extends KalSMS_Action { - public $status; - public $id; - + public $status; // KalSMS::STATUS_* values + public $id; // server ID previously used in KalSMS_OutgoingMessage + function __construct($type) { $this->type = KalSMS::ACTION_SEND_STATUS; diff --git a/src/org/envaya/kalsms/App.java b/src/org/envaya/kalsms/App.java index aadd38b..3086b61 100755 --- a/src/org/envaya/kalsms/App.java +++ b/src/org/envaya/kalsms/App.java @@ -58,6 +58,9 @@ public final class App extends Application { // intent to signal to Main activity (if open) that log has changed public static final String LOG_INTENT = "org.envaya.kalsms.LOG"; + + public static final String START_INTENT = "org.envaya.kalsms.START"; + public static final String STOP_INTENT = "org.envaya.kalsms.STOP"; public static final String QUERY_EXPANSION_PACKS_INTENT = "org.envaya.kalsms.QUERY_EXPANSION_PACKS"; public static final String QUERY_EXPANSION_PACKS_EXTRA_PACKAGES = "packages"; @@ -141,9 +144,15 @@ public final class App extends Application { mmsObserver = new MmsObserver(this); mmsObserver.register(); - setOutgoingMessageAlarm(); + setOutgoingMessageAlarm(); + updateEnabledNotification(); } + public void updateEnabledNotification() + { + startService(new Intent(this, ForegroundService.class)); + } + public synchronized String chooseOutgoingSmsPackage() { outgoingMessageCount++; diff --git a/src/org/envaya/kalsms/CheckMmsInboxService.java b/src/org/envaya/kalsms/CheckMmsInboxService.java index e808bff..b430a56 100755 --- a/src/org/envaya/kalsms/CheckMmsInboxService.java +++ b/src/org/envaya/kalsms/CheckMmsInboxService.java @@ -42,7 +42,14 @@ public class CheckMmsInboxService extends IntentService // times if we don't delete them. mmsUtils.markOldMms(mms); - app.forwardToServer(mms); + if (mms.isForwardable()) + { + app.forwardToServer(mms); + } + else + { + app.log("Ignoring incoming MMS from " + mms.getFrom()); + } } } } diff --git a/src/org/envaya/kalsms/ForegroundService.java b/src/org/envaya/kalsms/ForegroundService.java new file mode 100755 index 0000000..039e97e --- /dev/null +++ b/src/org/envaya/kalsms/ForegroundService.java @@ -0,0 +1,188 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.envaya.kalsms; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; +import android.util.Log; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import org.envaya.kalsms.ui.Main; + +/* + * Service running in foreground to make sure App instance stays + * in memory (otherwise we could lose timestamps of sent messages + * which could cause us to exceed Android's SMS sending limit) + * + * Also adds notification to status bar. + */ +public class ForegroundService extends Service { + + private App app; + + private static final Class[] mSetForegroundSignature = new Class[] { + boolean.class}; + private static final Class[] mStartForegroundSignature = new Class[] { + int.class, Notification.class}; + private static final Class[] mStopForegroundSignature = new Class[] { + boolean.class}; + + private NotificationManager mNM; + private Method mSetForeground; + private Method mStartForeground; + private Method mStopForeground; + private Object[] mSetForegroundArgs = new Object[1]; + private Object[] mStartForegroundArgs = new Object[2]; + private Object[] mStopForegroundArgs = new Object[1]; + + void invokeMethod(Method method, Object[] args) { + try { + mStartForeground.invoke(this, mStartForegroundArgs); + } catch (InvocationTargetException e) { + // Should not happen. + Log.w("ApiDemos", "Unable to invoke method", e); + } catch (IllegalAccessException e) { + // Should not happen. + Log.w("ApiDemos", "Unable to invoke method", e); + } + } + + /** + * This is a wrapper around the new startForeground method, using the older + * APIs if it is not available. + */ + void startForegroundCompat(int id, Notification notification) { + // If we have the new startForeground API, then use it. + if (mStartForeground != null) { + mStartForegroundArgs[0] = Integer.valueOf(id); + mStartForegroundArgs[1] = notification; + invokeMethod(mStartForeground, mStartForegroundArgs); + return; + } + + // Fall back on the old API. + mSetForegroundArgs[0] = Boolean.TRUE; + invokeMethod(mSetForeground, mSetForegroundArgs); + mNM.notify(id, notification); + } + + /** + * This is a wrapper around the new stopForeground method, using the older + * APIs if it is not available. + */ + void stopForegroundCompat(int id) { + // If we have the new stopForeground API, then use it. + if (mStopForeground != null) { + mStopForegroundArgs[0] = Boolean.TRUE; + try { + mStopForeground.invoke(this, mStopForegroundArgs); + } catch (InvocationTargetException e) { + // Should not happen. + Log.w("ApiDemos", "Unable to invoke stopForeground", e); + } catch (IllegalAccessException e) { + // Should not happen. + Log.w("ApiDemos", "Unable to invoke stopForeground", e); + } + return; + } + + // Fall back on the old API. Note to cancel BEFORE changing the + // foreground state, since we could be killed at that point. + mNM.cancel(id); + mSetForegroundArgs[0] = Boolean.FALSE; + invokeMethod(mSetForeground, mSetForegroundArgs); + } + + @Override + public void onCreate() { + mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + app = (App)getApplication(); + try { + mStartForeground = getClass().getMethod("startForeground", + mStartForegroundSignature); + mStopForeground = getClass().getMethod("stopForeground", + mStopForegroundSignature); + } catch (NoSuchMethodException e) { + // Running on an older platform. + mStartForeground = mStopForeground = null; + return; + } + try { + mSetForeground = getClass().getMethod("setForeground", + mSetForegroundSignature); + } catch (NoSuchMethodException e) { + throw new IllegalStateException( + "OS doesn't have Service.startForeground OR Service.setForeground!"); + } + } + + @Override + public void onDestroy() { + // Make sure our notification is gone. + stopForegroundCompat(R.string.service_started); + } + + // This is the old onStart method that will be called on the pre-2.0 + // platform. On 2.0 or later we override onStartCommand() so this + // method will not be called. + @Override + public void onStart(Intent intent, int startId) { + handleCommand(intent); + } + + //@Override + public int onStartCommand(Intent intent, int flags, int startId) { + handleCommand(intent); + // We want this service to continue running until it is explicitly + // stopped, so return sticky. + return 1; //START_STICKY; + } + + void handleCommand(Intent intent) + { + if (app.isEnabled()) + { + CharSequence text = getText(R.string.service_started); + + Notification notification = new Notification(R.drawable.icon, text, + System.currentTimeMillis()); + + PendingIntent contentIntent = PendingIntent.getActivity(this, 0, + new Intent(this, Main.class), 0); + + notification.setLatestEventInfo(this, + "KalSMS running", + text, contentIntent); + + startForegroundCompat(R.string.service_started, notification); + } + else + { + this.stopForegroundCompat(R.string.service_started); + } + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } +} \ No newline at end of file diff --git a/src/org/envaya/kalsms/MmsObserver.java b/src/org/envaya/kalsms/MmsObserver.java index 5f73c05..d09a980 100755 --- a/src/org/envaya/kalsms/MmsObserver.java +++ b/src/org/envaya/kalsms/MmsObserver.java @@ -1,12 +1,9 @@ package org.envaya.kalsms; -import android.app.IntentService; import android.content.Intent; import android.database.ContentObserver; import android.os.Handler; -import java.util.HashSet; import java.util.List; -import java.util.Set; final class MmsObserver extends ContentObserver { diff --git a/src/org/envaya/kalsms/task/HttpTask.java b/src/org/envaya/kalsms/task/HttpTask.java index 3678a4c..e202c23 100755 --- a/src/org/envaya/kalsms/task/HttpTask.java +++ b/src/org/envaya/kalsms/task/HttpTask.java @@ -26,7 +26,6 @@ import org.apache.http.entity.mime.FormBodyPart; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; import org.envaya.kalsms.App; import org.envaya.kalsms.Base64Coder; import org.envaya.kalsms.OutgoingMessage; @@ -210,6 +209,7 @@ public class HttpTask extends AsyncTask { try { handleResponse(response); + response.getEntity().consumeContent(); } catch (Throwable ex) { @@ -226,10 +226,6 @@ public class HttpTask extends AsyncTask { protected void handleResponse(HttpResponse response) throws Exception { - if (response != null) - { - response.getEntity().consumeContent(); - } } protected void handleFailure() diff --git a/src/org/envaya/kalsms/ui/Prefs.java b/src/org/envaya/kalsms/ui/Prefs.java index 0753f33..d4e5136 100755 --- a/src/org/envaya/kalsms/ui/Prefs.java +++ b/src/org/envaya/kalsms/ui/Prefs.java @@ -102,6 +102,7 @@ public class Prefs extends PreferenceActivity implements OnSharedPreferenceChang { app.log(app.isEnabled() ? "SMS Gateway started." : "SMS Gateway stopped."); app.setOutgoingMessageAlarm(); + app.updateEnabledNotification(); } updatePrefSummary(findPreference(key));