2013-02-08 11:57:54 -08:00
|
|
|
package org.thoughtcrime.securesms.notifications;
|
|
|
|
|
2013-05-30 12:39:56 -07:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2015-06-09 07:37:20 -07:00
|
|
|
import android.net.Uri;
|
2016-12-25 15:27:59 -08:00
|
|
|
import android.support.annotation.NonNull;
|
2015-06-09 07:37:20 -07:00
|
|
|
import android.support.annotation.Nullable;
|
2013-05-30 12:39:56 -07:00
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
import org.thoughtcrime.securesms.ConversationActivity;
|
|
|
|
import org.thoughtcrime.securesms.ConversationPopupActivity;
|
2017-08-21 18:37:39 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase.VibrateState;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
import java.util.LinkedHashSet;
|
2013-02-08 11:57:54 -08:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class NotificationState {
|
|
|
|
|
2018-08-02 09:25:33 -04:00
|
|
|
private static final String TAG = NotificationState.class.getSimpleName();
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
private final LinkedList<NotificationItem> notifications = new LinkedList<>();
|
2016-12-25 15:27:59 -08:00
|
|
|
private final LinkedHashSet<Long> threads = new LinkedHashSet<>();
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
private int notificationCount = 0;
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
public NotificationState() {}
|
|
|
|
|
|
|
|
public NotificationState(@NonNull List<NotificationItem> items) {
|
|
|
|
for (NotificationItem item : items) {
|
|
|
|
addNotification(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
public void addNotification(NotificationItem item) {
|
|
|
|
notifications.addFirst(item);
|
2016-12-25 15:27:59 -08:00
|
|
|
|
|
|
|
if (threads.contains(item.getThreadId())) {
|
|
|
|
threads.remove(item.getThreadId());
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
threads.add(item.getThreadId());
|
|
|
|
notificationCount++;
|
|
|
|
}
|
|
|
|
|
2018-08-22 11:58:41 -07:00
|
|
|
public @Nullable Uri getRingtone(@NonNull Context context) {
|
2015-06-09 07:37:20 -07:00
|
|
|
if (!notifications.isEmpty()) {
|
2017-08-01 08:56:00 -07:00
|
|
|
Recipient recipient = notifications.getFirst().getRecipient();
|
2015-06-09 07:37:20 -07:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
if (recipient != null) {
|
2018-08-22 11:58:41 -07:00
|
|
|
return recipient.resolve().getMessageRingtone(context);
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public VibrateState getVibrate() {
|
|
|
|
if (!notifications.isEmpty()) {
|
2017-08-01 08:56:00 -07:00
|
|
|
Recipient recipient = notifications.getFirst().getRecipient();
|
2015-06-09 07:37:20 -07:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
if (recipient != null) {
|
2018-02-16 11:10:35 -08:00
|
|
|
return recipient.resolve().getMessageVibrate();
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VibrateState.DEFAULT;
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
public boolean hasMultipleThreads() {
|
|
|
|
return threads.size() > 1;
|
|
|
|
}
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
public LinkedHashSet<Long> getThreads() {
|
|
|
|
return threads;
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:10:19 -07:00
|
|
|
public int getThreadCount() {
|
|
|
|
return threads.size();
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
public int getMessageCount() {
|
|
|
|
return notificationCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<NotificationItem> getNotifications() {
|
|
|
|
return notifications;
|
|
|
|
}
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
public List<NotificationItem> getNotificationsForThread(long threadId) {
|
|
|
|
LinkedList<NotificationItem> list = new LinkedList<>();
|
|
|
|
|
|
|
|
for (NotificationItem item : notifications) {
|
|
|
|
if (item.getThreadId() == threadId) list.addFirst(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PendingIntent getMarkAsReadIntent(Context context, int notificationId) {
|
2014-01-18 18:25:51 -08:00
|
|
|
long[] threadArray = new long[threads.size()];
|
2015-06-13 20:23:30 -07:00
|
|
|
int index = 0;
|
2014-01-18 18:25:51 -08:00
|
|
|
|
|
|
|
for (long thread : threads) {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Added thread: " + thread);
|
2014-01-18 18:25:51 -08:00
|
|
|
threadArray[index++] = thread;
|
|
|
|
}
|
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
Intent intent = new Intent(MarkReadReceiver.CLEAR_ACTION);
|
2016-12-25 15:27:59 -08:00
|
|
|
intent.setClass(context, MarkReadReceiver.class);
|
|
|
|
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
|
2015-06-13 20:23:30 -07:00
|
|
|
intent.putExtra(MarkReadReceiver.THREAD_IDS_EXTRA, threadArray);
|
2016-12-25 15:27:59 -08:00
|
|
|
intent.putExtra(MarkReadReceiver.NOTIFICATION_ID_EXTRA, notificationId);
|
2015-06-13 20:23:30 -07:00
|
|
|
|
|
|
|
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
}
|
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
public PendingIntent getRemoteReplyIntent(Context context, Recipient recipient) {
|
2015-06-13 20:23:30 -07:00
|
|
|
if (threads.size() != 1) throw new AssertionError("We only support replies to single thread notifications!");
|
|
|
|
|
2016-12-25 19:00:33 -08:00
|
|
|
Intent intent = new Intent(RemoteReplyReceiver.REPLY_ACTION);
|
|
|
|
intent.setClass(context, RemoteReplyReceiver.class);
|
2016-12-25 15:27:59 -08:00
|
|
|
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
|
2017-08-01 08:56:00 -07:00
|
|
|
intent.putExtra(RemoteReplyReceiver.ADDRESS_EXTRA, recipient.getAddress());
|
2015-06-13 20:23:30 -07:00
|
|
|
intent.setPackage(context.getPackageName());
|
2014-01-18 18:25:51 -08:00
|
|
|
|
2013-12-03 21:48:16 +00:00
|
|
|
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2013-05-30 12:39:56 -07:00
|
|
|
}
|
2015-06-13 20:23:30 -07:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
public PendingIntent getAndroidAutoReplyIntent(Context context, Recipient recipient) {
|
2016-09-14 23:58:24 +02:00
|
|
|
if (threads.size() != 1) throw new AssertionError("We only support replies to single thread notifications!");
|
|
|
|
|
|
|
|
Intent intent = new Intent(AndroidAutoReplyReceiver.REPLY_ACTION);
|
2017-01-31 17:28:44 +01:00
|
|
|
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
2016-12-25 15:27:59 -08:00
|
|
|
intent.setClass(context, AndroidAutoReplyReceiver.class);
|
|
|
|
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
|
2017-08-01 08:56:00 -07:00
|
|
|
intent.putExtra(AndroidAutoReplyReceiver.ADDRESS_EXTRA, recipient.getAddress());
|
2016-09-14 23:58:24 +02:00
|
|
|
intent.putExtra(AndroidAutoReplyReceiver.THREAD_ID_EXTRA, (long)threads.toArray()[0]);
|
|
|
|
intent.setPackage(context.getPackageName());
|
|
|
|
|
|
|
|
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
}
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
public PendingIntent getAndroidAutoHeardIntent(Context context, int notificationId) {
|
2016-09-14 23:58:24 +02:00
|
|
|
long[] threadArray = new long[threads.size()];
|
|
|
|
int index = 0;
|
|
|
|
for (long thread : threads) {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "getAndroidAutoHeardIntent Added thread: " + thread);
|
2016-09-14 23:58:24 +02:00
|
|
|
threadArray[index++] = thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent intent = new Intent(AndroidAutoHeardReceiver.HEARD_ACTION);
|
2017-01-31 17:28:44 +01:00
|
|
|
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
2016-12-25 15:27:59 -08:00
|
|
|
intent.setClass(context, AndroidAutoHeardReceiver.class);
|
|
|
|
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
|
2016-09-14 23:58:24 +02:00
|
|
|
intent.putExtra(AndroidAutoHeardReceiver.THREAD_IDS_EXTRA, threadArray);
|
2016-12-25 15:27:59 -08:00
|
|
|
intent.putExtra(AndroidAutoHeardReceiver.NOTIFICATION_ID_EXTRA, notificationId);
|
2016-09-14 23:58:24 +02:00
|
|
|
intent.setPackage(context.getPackageName());
|
|
|
|
|
|
|
|
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
}
|
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
public PendingIntent getQuickReplyIntent(Context context, Recipient recipient) {
|
2016-12-25 15:27:59 -08:00
|
|
|
if (threads.size() != 1) throw new AssertionError("We only support replies to single thread notifications! " + threads.size());
|
2015-06-13 20:23:30 -07:00
|
|
|
|
|
|
|
Intent intent = new Intent(context, ConversationPopupActivity.class);
|
2017-08-01 08:56:00 -07:00
|
|
|
intent.putExtra(ConversationActivity.ADDRESS_EXTRA, recipient.getAddress());
|
2015-06-13 20:23:30 -07:00
|
|
|
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, (long)threads.toArray()[0]);
|
|
|
|
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
|
|
|
|
|
|
|
|
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:38:55 -08:00
|
|
|
public PendingIntent getDeleteIntent(Context context) {
|
|
|
|
int index = 0;
|
|
|
|
long[] ids = new long[notifications.size()];
|
|
|
|
boolean[] mms = new boolean[ids.length];
|
|
|
|
|
|
|
|
for (NotificationItem notificationItem : notifications) {
|
|
|
|
ids[index] = notificationItem.getId();
|
|
|
|
mms[index++] = notificationItem.isMms();
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, DeleteNotificationReceiver.class);
|
|
|
|
intent.setAction(DeleteNotificationReceiver.DELETE_NOTIFICATION_ACTION);
|
|
|
|
intent.putExtra(DeleteNotificationReceiver.EXTRA_IDS, ids);
|
|
|
|
intent.putExtra(DeleteNotificationReceiver.EXTRA_MMS, mms);
|
|
|
|
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
|
|
|
|
|
|
|
|
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
}
|
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|