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;
|
2015-05-19 10:24:08 +02:00
|
|
|
import android.os.Bundle;
|
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
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-30 12:39:56 -07:00
|
|
|
public PendingIntent getMarkAsReadIntent(Context context, MasterSecret masterSecret) {
|
2015-05-19 10:24:08 +02:00
|
|
|
Bundle extras = new Bundle();
|
|
|
|
extras.putParcelable("master_secret", masterSecret);
|
|
|
|
return craftIntent(context, MarkReadReceiver.CLEAR_ACTION, extras);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PendingIntent getReplyIntent(Context context, MasterSecret masterSecret, long recipientId) {
|
|
|
|
Bundle extras = new Bundle();
|
|
|
|
extras.putParcelable("master_secret", masterSecret);
|
|
|
|
extras.putLong("recipient_id", recipientId);
|
|
|
|
return craftIntent(context, WearReplyReceiver.REPLY_ACTION, extras);
|
|
|
|
}
|
|
|
|
|
|
|
|
private PendingIntent craftIntent(Context context, String intentAction, Bundle extras) {
|
2014-01-18 18:25:51 -08:00
|
|
|
long[] threadArray = new long[threads.size()];
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
for (long thread : threads) {
|
|
|
|
Log.w("NotificationState", "Added thread: " + thread);
|
|
|
|
threadArray[index++] = thread;
|
|
|
|
}
|
|
|
|
|
2015-05-19 10:24:08 +02:00
|
|
|
Intent intent = new Intent(intentAction);
|
2014-01-18 18:25:51 -08:00
|
|
|
intent.putExtra("thread_ids", threadArray);
|
2015-05-19 10:24:08 +02:00
|
|
|
intent.putExtras(extras);
|
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-05-19 10:24:08 +02:00
|
|
|
intent.getLongArrayExtra("thread_ids").length);
|
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
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|