From b6a372d72868e80fc87785bbce3423c1b4160357 Mon Sep 17 00:00:00 2001 From: Jesse Young Date: Wed, 21 Sep 2011 11:53:48 -0700 Subject: [PATCH] avoid forwarding sms from network; unregister mms observer when kalsms disabled --- AndroidManifest.xml | 4 +-- src/org/envaya/kalsms/App.java | 30 ++++++++++++++-------- src/org/envaya/kalsms/IncomingMessage.java | 27 ++++++++++++++++--- src/org/envaya/kalsms/MmsObserver.java | 10 ++++---- src/org/envaya/kalsms/ui/Main.java | 7 +---- src/org/envaya/kalsms/ui/Prefs.java | 3 +-- 6 files changed, 52 insertions(+), 29 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 926d641..59e354e 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="3" + android:versionName="2.0-beta 2"> diff --git a/src/org/envaya/kalsms/App.java b/src/org/envaya/kalsms/App.java index 6f9057d..85f7bd0 100755 --- a/src/org/envaya/kalsms/App.java +++ b/src/org/envaya/kalsms/App.java @@ -122,6 +122,8 @@ public final class App extends Application { outgoingMessagePackages.add(getPackageName()); + mmsObserver = new MmsObserver(this); + try { packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0); @@ -140,7 +142,7 @@ public final class App extends Application { log("Server URL is: " + getDisplayString(getServerUrl())); log("Your phone number is: " + getDisplayString(getPhoneNumber())); - + if (isTestMode()) { log("Test mode is ON"); @@ -150,19 +152,27 @@ public final class App extends Application { { log(" " + sender); } - } + } + + enabledChanged(); - mmsObserver = new MmsObserver(this); - mmsObserver.register(); - - setOutgoingMessageAlarm(); - updateEnabledNotification(); + log(Html.fromHtml("Press Menu to edit settings.")); } - public void updateEnabledNotification() - { + public void enabledChanged() + { + if (isEnabled()) + { + mmsObserver.register(); + } + else + { + mmsObserver.unregister(); + } + + setOutgoingMessageAlarm(); startService(new Intent(this, ForegroundService.class)); - } + } public PackageInfo getPackageInfo() { diff --git a/src/org/envaya/kalsms/IncomingMessage.java b/src/org/envaya/kalsms/IncomingMessage.java index 022a856..5c15bb2 100755 --- a/src/org/envaya/kalsms/IncomingMessage.java +++ b/src/org/envaya/kalsms/IncomingMessage.java @@ -24,10 +24,29 @@ public abstract class IncomingMessage extends QueuedMessage { } /* - * don't forward messages from shortcodes - * because they're likely to be spam or messages from network - */ - return from.length() > 5; + * Don't forward messages from shortcodes or users with + * addresses like 'Vodacom' because they're likely to be + * messages from network, or spam. At least for network + * messages we should let them go in to the Messaging inbox + * because the person managing this phone needs to know + * when they're out of credit, etc. + * + * The minimum length of normal subscriber numbers doesn't + * seem to be specified, but in practice seems to be + * at least 7 digits everywhere. + */ + int fromDigits = 0; + int fromLength = from.length(); + + for (int i = 0; i < fromLength; i++) + { + if (Character.isDigit(from.charAt(i))) + { + fromDigits++; + } + } + + return fromDigits >= 7; } public String getFrom() diff --git a/src/org/envaya/kalsms/MmsObserver.java b/src/org/envaya/kalsms/MmsObserver.java index d09a980..c0aeb4f 100755 --- a/src/org/envaya/kalsms/MmsObserver.java +++ b/src/org/envaya/kalsms/MmsObserver.java @@ -16,11 +16,6 @@ final class MmsObserver extends ContentObserver { public void register() { - /* - * Content observers can watch the MMS inbox URI for changes; - * This is the URL passed to PduPersister.persist by - * com.android.mms.transaction.RetrieveTransaction.run - */ app.getContentResolver().registerContentObserver( MmsUtils.OBSERVER_URI, true, this); @@ -32,6 +27,11 @@ final class MmsObserver extends ContentObserver { mmsUtils.markOldMms(mms); } } + + public void unregister() + { + app.getContentResolver().unregisterContentObserver(this); + } @Override public void onChange(final boolean selfChange) { diff --git a/src/org/envaya/kalsms/ui/Main.java b/src/org/envaya/kalsms/ui/Main.java index fbee98a..e7dff8b 100755 --- a/src/org/envaya/kalsms/ui/Main.java +++ b/src/org/envaya/kalsms/ui/Main.java @@ -79,12 +79,7 @@ public class Main extends Activity { IntentFilter logReceiverFilter = new IntentFilter(); logReceiverFilter.addAction(App.LOG_INTENT); - registerReceiver(logReceiver, logReceiverFilter); - - if (savedInstanceState == null) - { - app.log(Html.fromHtml("Press Menu to edit settings.")); - } + registerReceiver(logReceiver, logReceiverFilter); } @Override diff --git a/src/org/envaya/kalsms/ui/Prefs.java b/src/org/envaya/kalsms/ui/Prefs.java index d4e5136..a6a7d9d 100755 --- a/src/org/envaya/kalsms/ui/Prefs.java +++ b/src/org/envaya/kalsms/ui/Prefs.java @@ -101,8 +101,7 @@ public class Prefs extends PreferenceActivity implements OnSharedPreferenceChang else if (key.equals("enabled")) { app.log(app.isEnabled() ? "SMS Gateway started." : "SMS Gateway stopped."); - app.setOutgoingMessageAlarm(); - app.updateEnabledNotification(); + app.enabledChanged(); } updatePrefSummary(findPreference(key));