diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 271a611777..710c8f50f2 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -204,6 +204,14 @@ + + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index bbd4ffec13..abc9bea179 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -259,8 +259,10 @@ (No Subject) Message delivery failed. Failed to deliver message. - Error delivering message. - + Error delivering message. + Mark all as read + Mark as read + Currently unable to send your SMS message. It will be sent once service becomes available. diff --git a/src/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java b/src/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java new file mode 100644 index 0000000000..4338a7a42c --- /dev/null +++ b/src/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java @@ -0,0 +1,45 @@ +package org.thoughtcrime.securesms.notifications; + +import android.app.NotificationManager; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; +import android.util.Log; + +import org.thoughtcrime.securesms.crypto.MasterSecret; +import org.thoughtcrime.securesms.database.DatabaseFactory; + +public class MarkReadReceiver extends BroadcastReceiver { + + public static final String CLEAR_ACTION = "org.thoughtcrime.securesms.notifications.CLEAR"; + + @Override + public void onReceive(final Context context, Intent intent) { + if (!intent.getAction().equals(CLEAR_ACTION)) + return; + + final long[] threadIds = intent.getLongArrayExtra("thread_ids"); + final MasterSecret masterSecret = intent.getParcelableExtra("master_secret"); + + if (threadIds != null && masterSecret != null) { + Log.w("MarkReadReceiver", "threadIds length: " + threadIds.length); + + ((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)) + .cancel(MessageNotifier.NOTIFICATION_ID); + + new AsyncTask() { + @Override + protected Void doInBackground(Void... params) { + for (long threadId : threadIds) { + Log.w("MarkReadReceiver", "Marking as read: " + threadId); + DatabaseFactory.getThreadDatabase(context).setRead(threadId); + } + + MessageNotifier.updateNotification(context, masterSecret); + return null; + } + }.execute(); + } + } +} diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index c0d1f8d3d0..e13bbd74f0 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -136,9 +136,9 @@ public class MessageNotifier { NotificationState notificationState = constructNotificationState(context, masterSecret, cursor); if (notificationState.hasMultipleThreads()) { - sendMultipleThreadNotification(context, notificationState, signal); + sendMultipleThreadNotification(context, masterSecret, notificationState, signal); } else { - sendSingleThreadNotification(context, notificationState, signal); + sendSingleThreadNotification(context, masterSecret, notificationState, signal); } } finally { if (cursor != null) @@ -147,6 +147,7 @@ public class MessageNotifier { } private static void sendSingleThreadNotification(Context context, + MasterSecret masterSecret, NotificationState notificationState, boolean signal) { @@ -160,6 +161,11 @@ public class MessageNotifier { builder.setContentText(notifications.get(0).getText()); builder.setContentIntent(notifications.get(0).getPendingIntent(context)); + if (masterSecret != null) { + builder.addAction(R.drawable.check, context.getString(R.string.MessageNotifier_mark_as_read), + notificationState.getMarkAsReadIntent(context, masterSecret)); + } + SpannableStringBuilder content = new SpannableStringBuilder(); for (NotificationItem item : notifications) { @@ -180,6 +186,7 @@ public class MessageNotifier { } private static void sendMultipleThreadNotification(Context context, + MasterSecret masterSecret, NotificationState notificationState, boolean signal) { @@ -195,6 +202,11 @@ public class MessageNotifier { notifications.get(0).getIndividualRecipientName())); builder.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, RoutingActivity.class), 0)); + if (masterSecret != null) { + builder.addAction(R.drawable.check, context.getString(R.string.MessageNotifier_mark_all_as_read), + notificationState.getMarkAsReadIntent(context, masterSecret)); + } + InboxStyle style = new InboxStyle(); for (NotificationItem item : notifications) { diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationState.java b/src/org/thoughtcrime/securesms/notifications/NotificationState.java index 717c4a64ef..0b4c9ecfc5 100644 --- a/src/org/thoughtcrime/securesms/notifications/NotificationState.java +++ b/src/org/thoughtcrime/securesms/notifications/NotificationState.java @@ -1,6 +1,12 @@ package org.thoughtcrime.securesms.notifications; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; import android.graphics.Bitmap; +import android.util.Log; + +import org.thoughtcrime.securesms.crypto.MasterSecret; import java.util.HashSet; import java.util.LinkedList; @@ -36,4 +42,26 @@ public class NotificationState { return notifications.get(0).getIndividualRecipient().getContactPhoto(); } + public PendingIntent getMarkAsReadIntent(Context context, MasterSecret masterSecret) { + long[] threadArray = new long[threads.size()]; + int index = 0; + + for (long thread : threads) { + Log.w("NotificationState", "Added thread: " + thread); + threadArray[index++] = thread; + } + + Intent intent = new Intent(MarkReadReceiver.CLEAR_ACTION); + intent.putExtra("thread_ids", threadArray); + intent.putExtra("master_secret", masterSecret); + intent.setPackage(context.getPackageName()); + + // XXX : This is an Android bug. If we don't pull off the extra + // once before handing off the PendingIntent, the array will be + // truncated to one element when the PendingIntent fires. Thanks guys! + Log.w("NotificationState", "Pending array off intent length: " + + intent.getLongArrayExtra("thread_ids").length); + + return PendingIntent.getBroadcast(context, 0, intent, 0); + } }