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;
|
2014-01-18 18:25:51 -08:00
|
|
|
import android.util.Log;
|
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;
|
2015-06-09 07:37:20 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
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 {
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
public @Nullable Uri getRingtone() {
|
|
|
|
if (!notifications.isEmpty()) {
|
|
|
|
Recipients recipients = notifications.getFirst().getRecipients();
|
|
|
|
|
|
|
|
if (recipients != null) {
|
|
|
|
return recipients.getRingtone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public VibrateState getVibrate() {
|
|
|
|
if (!notifications.isEmpty()) {
|
|
|
|
Recipients recipients = notifications.getFirst().getRecipients();
|
|
|
|
|
|
|
|
if (recipients != null) {
|
|
|
|
return recipients.getVibrate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
Log.w("NotificationState", "Added thread: " + thread);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-25 19:00:33 -08:00
|
|
|
public PendingIntent getRemoteReplyIntent(Context context, Recipients recipients) {
|
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())));
|
2016-12-25 19:00:33 -08:00
|
|
|
intent.putExtra(RemoteReplyReceiver.RECIPIENT_IDS_EXTRA, recipients.getIds());
|
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
|
|
|
|
2016-09-14 23:58:24 +02:00
|
|
|
public PendingIntent getAndroidAutoReplyIntent(Context context, Recipients recipients) {
|
|
|
|
if (threads.size() != 1) throw new AssertionError("We only support replies to single thread notifications!");
|
|
|
|
|
|
|
|
Intent intent = new Intent(AndroidAutoReplyReceiver.REPLY_ACTION);
|
2016-12-25 15:27:59 -08:00
|
|
|
intent.setClass(context, AndroidAutoReplyReceiver.class);
|
|
|
|
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
|
2016-09-14 23:58:24 +02:00
|
|
|
intent.putExtra(AndroidAutoReplyReceiver.RECIPIENT_IDS_EXTRA, recipients.getIds());
|
|
|
|
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) {
|
|
|
|
Log.w("NotificationState", "getAndroidAutoHeardIntent Added thread: " + thread);
|
|
|
|
threadArray[index++] = thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent intent = new Intent(AndroidAutoHeardReceiver.HEARD_ACTION);
|
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);
|
|
|
|
}
|
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
public PendingIntent getQuickReplyIntent(Context context, Recipients recipients) {
|
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);
|
|
|
|
intent.putExtra(ConversationActivity.RECIPIENTS_EXTRA, recipients.getIds());
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|