diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 44b647d37f..c97612217c 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -168,7 +168,6 @@ android:enabled="true" android:exported="true"> - diff --git a/src/org/thoughtcrime/securesms/service/SmsSender.java b/src/org/thoughtcrime/securesms/service/SmsSender.java index 54a7103bb8..750497ee71 100644 --- a/src/org/thoughtcrime/securesms/service/SmsSender.java +++ b/src/org/thoughtcrime/securesms/service/SmsSender.java @@ -20,6 +20,7 @@ import android.app.Activity; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.database.Cursor; import android.net.Uri; import android.preference.PreferenceManager; @@ -121,16 +122,19 @@ public class SmsSender { if (result == Activity.RESULT_OK) { DatabaseFactory.getSmsDatabase(context).markAsSent(messageId, type); + unregisterForRadioChanges(); } else if (result == SmsManager.RESULT_ERROR_NO_SERVICE || result == SmsManager.RESULT_ERROR_RADIO_OFF) { toastHandler .obtainMessage(0, context.getString(R.string.SmsReceiver_currently_unable_to_send_your_sms_message)) .sendToTarget(); + registerForRadioChanges(); } else { long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId); Recipients recipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(context, threadId); DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageId); MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId); + unregisterForRadioChanges(); } pendingMessages.remove(messageId); @@ -150,6 +154,22 @@ public class SmsSender { DatabaseFactory.getSmsDatabase(context).markStatus(messageId, message.getStatus()); } + private void registerForRadioChanges() { + unregisterForRadioChanges(); + + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(SystemStateListener.ACTION_SERVICE_STATE); + context.registerReceiver(SystemStateListener.getInstance(), intentFilter); + } + + private void unregisterForRadioChanges() { + try { + context.unregisterReceiver(SystemStateListener.getInstance()); + } catch (IllegalArgumentException iae) { + + } + } + private String getClearTextBody(MasterCipher masterCipher, String body) { if (body.startsWith(Prefix.SYMMETRIC_ENCRYPT)) { try { diff --git a/src/org/thoughtcrime/securesms/service/SystemStateListener.java b/src/org/thoughtcrime/securesms/service/SystemStateListener.java index 18f8cad908..ef8cf8bc52 100644 --- a/src/org/thoughtcrime/securesms/service/SystemStateListener.java +++ b/src/org/thoughtcrime/securesms/service/SystemStateListener.java @@ -9,9 +9,15 @@ import android.telephony.ServiceState; public class SystemStateListener extends BroadcastReceiver { - private static final String ACTION_SERVICE_STATE = "android.intent.action.SERVICE_STATE"; + public static final String ACTION_SERVICE_STATE = "android.intent.action.SERVICE_STATE"; private static final String ACTION_CONNECTIVITY_CHANGE = "android.net.conn.CONNECTIVITY_CHANGE"; + private static final SystemStateListener instance = new SystemStateListener(); + + public static SystemStateListener getInstance() { + return instance; + } + private void sendSmsOutbox(Context context) { Intent smsSenderIntent = new Intent(SendReceiveService.SEND_SMS_ACTION, null, context, SendReceiveService.class);