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;
|
|
|
|
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
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class NotificationState {
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
private final LinkedList<NotificationItem> notifications = new LinkedList<>();
|
|
|
|
private final Set<Long> threads = new HashSet<>();
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
private int notificationCount = 0;
|
|
|
|
|
|
|
|
public void addNotification(NotificationItem item) {
|
|
|
|
notifications.addFirst(item);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
public PendingIntent getMarkAsReadIntent(Context context) {
|
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);
|
|
|
|
intent.putExtra(MarkReadReceiver.THREAD_IDS_EXTRA, threadArray);
|
2013-05-30 12:39:56 -07:00
|
|
|
intent.setPackage(context.getPackageName());
|
|
|
|
|
2014-01-18 18:25:51 -08:00
|
|
|
// 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: " +
|
2015-06-13 20:23:30 -07:00
|
|
|
intent.getLongArrayExtra("thread_ids").length);
|
|
|
|
|
|
|
|
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PendingIntent getWearableReplyIntent(Context context, Recipients recipients) {
|
|
|
|
if (threads.size() != 1) throw new AssertionError("We only support replies to single thread notifications!");
|
|
|
|
|
|
|
|
Intent intent = new Intent(WearReplyReceiver.REPLY_ACTION);
|
|
|
|
intent.putExtra(WearReplyReceiver.RECIPIENT_IDS_EXTRA, recipients.getIds());
|
|
|
|
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
|
|
|
|
|
|
|
public PendingIntent getQuickReplyIntent(Context context, Recipients recipients) {
|
|
|
|
if (threads.size() != 1) throw new AssertionError("We only support replies to single thread notifications!");
|
|
|
|
|
|
|
|
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
|
|
|
}
|