2014-09-28 13:21:56 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2017-08-22 10:19:18 +00:00
|
|
|
import android.content.res.Resources;
|
2014-10-24 12:34:46 +00:00
|
|
|
import android.graphics.Bitmap;
|
2017-03-06 02:31:15 +00:00
|
|
|
import android.graphics.Typeface;
|
2014-09-28 13:21:56 +00:00
|
|
|
import android.net.Uri;
|
2015-01-14 03:46:06 +00:00
|
|
|
import android.os.Build;
|
2014-10-24 11:29:18 +00:00
|
|
|
import android.os.SystemClock;
|
2017-07-01 11:41:24 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2014-09-28 13:21:56 +00:00
|
|
|
import android.support.v4.app.NotificationCompat;
|
2014-10-24 16:27:53 +00:00
|
|
|
import android.support.v4.app.NotificationCompat.BigPictureStyle;
|
|
|
|
import android.support.v4.app.NotificationCompat.Builder;
|
2016-08-31 15:02:42 +00:00
|
|
|
import android.support.v4.app.NotificationManagerCompat;
|
2017-04-14 11:23:01 +00:00
|
|
|
import android.support.v4.app.NotificationCompat.CarExtender.UnreadConversation;
|
2016-08-25 15:30:44 +00:00
|
|
|
import android.support.v4.app.RemoteInput;
|
2017-03-29 16:30:28 +00:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-03-06 02:31:15 +00:00
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.style.StyleSpan;
|
2014-10-21 12:57:16 +00:00
|
|
|
import android.util.DisplayMetrics;
|
2016-06-19 09:04:59 +00:00
|
|
|
import android.util.Log;
|
2017-04-29 17:25:57 +00:00
|
|
|
import android.util.Pair;
|
2014-09-28 13:21:56 +00:00
|
|
|
|
2017-05-13 06:10:47 +00:00
|
|
|
import java.io.File;
|
2014-11-05 20:37:40 +00:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.util.ArrayList;
|
2014-12-14 07:02:17 +00:00
|
|
|
import java.util.Calendar;
|
2017-03-23 14:34:52 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2014-11-05 20:37:40 +00:00
|
|
|
import java.util.LinkedHashMap;
|
2014-11-18 14:26:28 +00:00
|
|
|
import java.util.List;
|
2016-08-25 13:20:06 +00:00
|
|
|
import java.util.Map;
|
2017-03-23 14:34:52 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2014-11-05 20:37:40 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2014-10-24 11:29:18 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-09-28 13:21:56 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2014-10-17 09:01:38 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2016-08-26 11:35:01 +00:00
|
|
|
import eu.siacs.conversations.entities.Contact;
|
2014-09-28 13:21:56 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2018-04-26 11:22:31 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversational;
|
2014-09-28 13:21:56 +00:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
2017-05-13 06:10:47 +00:00
|
|
|
import eu.siacs.conversations.persistance.FileBackend;
|
2018-03-07 18:05:28 +00:00
|
|
|
import eu.siacs.conversations.ui.ConversationsActivity;
|
2014-11-18 14:26:28 +00:00
|
|
|
import eu.siacs.conversations.ui.ManageAccountActivity;
|
2014-12-14 07:02:17 +00:00
|
|
|
import eu.siacs.conversations.ui.TimePreference;
|
2015-03-10 20:46:13 +00:00
|
|
|
import eu.siacs.conversations.utils.GeoHelper;
|
2015-01-12 15:09:39 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
2017-04-30 14:19:39 +00:00
|
|
|
import eu.siacs.conversations.xmpp.XmppConnection;
|
2014-09-28 13:21:56 +00:00
|
|
|
|
|
|
|
public class NotificationService {
|
|
|
|
|
2017-05-19 11:30:57 +00:00
|
|
|
public static final Object CATCHUP_LOCK = new Object();
|
|
|
|
|
2016-08-25 13:20:06 +00:00
|
|
|
private static final String CONVERSATIONS_GROUP = "eu.siacs.conversations";
|
2015-01-16 05:36:11 +00:00
|
|
|
private final XmppConnectionService mXmppConnectionService;
|
2014-09-28 13:21:56 +00:00
|
|
|
|
2014-12-14 07:02:17 +00:00
|
|
|
private final LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<>();
|
2014-09-28 13:21:56 +00:00
|
|
|
|
2017-04-21 13:13:08 +00:00
|
|
|
private static final int NOTIFICATION_ID_MULTIPLIER = 1024 * 1024;
|
|
|
|
|
|
|
|
public static final int NOTIFICATION_ID = 2 * NOTIFICATION_ID_MULTIPLIER;
|
|
|
|
public static final int FOREGROUND_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 4;
|
|
|
|
public static final int ERROR_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 6;
|
2014-11-18 14:26:28 +00:00
|
|
|
|
2014-09-29 16:28:13 +00:00
|
|
|
private Conversation mOpenConversation;
|
|
|
|
private boolean mIsInForeground;
|
2014-10-24 11:29:18 +00:00
|
|
|
private long mLastNotification;
|
2014-10-20 19:08:33 +00:00
|
|
|
|
2018-04-15 15:47:51 +00:00
|
|
|
private final HashMap<Conversation, AtomicInteger> mBacklogMessageCounter = new HashMap<>();
|
2017-03-23 14:34:52 +00:00
|
|
|
|
2015-01-16 05:36:11 +00:00
|
|
|
public NotificationService(final XmppConnectionService service) {
|
2014-09-28 13:21:56 +00:00
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
public boolean notify(final Message message) {
|
2018-04-26 11:22:31 +00:00
|
|
|
final Conversation conversation = (Conversation) message.getConversation();
|
2017-03-07 09:50:46 +00:00
|
|
|
return message.getStatus() == Message.STATUS_RECEIVED
|
2015-12-01 16:15:36 +00:00
|
|
|
&& notificationsEnabled()
|
2018-04-26 11:22:31 +00:00
|
|
|
&& !conversation.isMuted()
|
|
|
|
&& (conversation.alwaysNotify() || wasHighlightedOrPrivate(message))
|
|
|
|
&& (!conversation.isWithStranger() || notificationsFromStrangers())
|
2018-04-15 15:47:51 +00:00
|
|
|
;
|
2014-11-05 20:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean notificationsEnabled() {
|
2018-04-15 15:47:51 +00:00
|
|
|
return mXmppConnectionService.getBooleanPreference("show_notification", R.bool.show_notification);
|
2014-11-05 20:37:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 09:50:46 +00:00
|
|
|
private boolean notificationsFromStrangers() {
|
2018-04-15 15:47:51 +00:00
|
|
|
return mXmppConnectionService.getBooleanPreference("notifications_from_strangers", R.bool.notifications_from_strangers);
|
2017-03-07 09:50:46 +00:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:02:17 +00:00
|
|
|
public boolean isQuietHours() {
|
2017-07-01 11:41:24 +00:00
|
|
|
if (!mXmppConnectionService.getBooleanPreference("enable_quiet_hours", R.bool.enable_quiet_hours)) {
|
2014-12-14 07:02:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-01 11:41:24 +00:00
|
|
|
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
|
|
|
|
final long startTime = preferences.getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY;
|
|
|
|
final long endTime = preferences.getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY;
|
2014-12-16 23:03:16 +00:00
|
|
|
final long nowTime = Calendar.getInstance().getTimeInMillis() % Config.MILLISECONDS_IN_DAY;
|
|
|
|
|
|
|
|
if (endTime < startTime) {
|
|
|
|
return nowTime > startTime || nowTime < endTime;
|
|
|
|
} else {
|
|
|
|
return nowTime > startTime && nowTime < endTime;
|
2014-12-14 07:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 19:18:34 +00:00
|
|
|
public void pushFromBacklog(final Message message) {
|
|
|
|
if (notify(message)) {
|
2016-01-04 19:14:29 +00:00
|
|
|
synchronized (notifications) {
|
2018-04-26 11:22:31 +00:00
|
|
|
getBacklogMessageCounter((Conversation) message.getConversation()).incrementAndGet();
|
2016-01-04 19:14:29 +00:00
|
|
|
pushToStack(message);
|
|
|
|
}
|
2015-10-14 19:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 14:34:52 +00:00
|
|
|
private AtomicInteger getBacklogMessageCounter(Conversation conversation) {
|
|
|
|
synchronized (mBacklogMessageCounter) {
|
|
|
|
if (!mBacklogMessageCounter.containsKey(conversation)) {
|
2018-04-15 15:47:51 +00:00
|
|
|
mBacklogMessageCounter.put(conversation, new AtomicInteger(0));
|
2017-03-23 14:34:52 +00:00
|
|
|
}
|
|
|
|
return mBacklogMessageCounter.get(conversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 15:30:44 +00:00
|
|
|
public void pushFromDirectReply(final Message message) {
|
|
|
|
synchronized (notifications) {
|
|
|
|
pushToStack(message);
|
|
|
|
updateNotification(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 19:56:27 +00:00
|
|
|
public void finishBacklog(boolean notify, Account account) {
|
2015-10-14 19:18:34 +00:00
|
|
|
synchronized (notifications) {
|
|
|
|
mXmppConnectionService.updateUnreadCountBadge();
|
2017-01-12 19:56:27 +00:00
|
|
|
if (account == null || !notify) {
|
|
|
|
updateNotification(notify);
|
|
|
|
} else {
|
2017-03-23 14:34:52 +00:00
|
|
|
updateNotification(getBacklogMessageCount(account) > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getBacklogMessageCount(Account account) {
|
|
|
|
int count = 0;
|
|
|
|
synchronized (this.mBacklogMessageCounter) {
|
2018-04-15 15:47:51 +00:00
|
|
|
for (Iterator<Map.Entry<Conversation, AtomicInteger>> it = mBacklogMessageCounter.entrySet().iterator(); it.hasNext(); ) {
|
2017-03-23 14:34:52 +00:00
|
|
|
Map.Entry<Conversation, AtomicInteger> entry = it.next();
|
|
|
|
if (entry.getKey().getAccount() == account) {
|
|
|
|
count += entry.getValue().get();
|
|
|
|
it.remove();
|
2017-01-12 19:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-14 19:18:34 +00:00
|
|
|
}
|
2018-04-15 15:47:51 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": backlog message count=" + count);
|
2017-03-23 14:34:52 +00:00
|
|
|
return count;
|
2015-10-14 19:18:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 19:56:27 +00:00
|
|
|
public void finishBacklog(boolean notify) {
|
2018-04-15 15:47:51 +00:00
|
|
|
finishBacklog(notify, null);
|
2017-01-12 19:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 19:18:34 +00:00
|
|
|
private void pushToStack(final Message message) {
|
|
|
|
final String conversationUuid = message.getConversationUuid();
|
|
|
|
if (notifications.containsKey(conversationUuid)) {
|
|
|
|
notifications.get(conversationUuid).add(message);
|
|
|
|
} else {
|
|
|
|
final ArrayList<Message> mList = new ArrayList<>();
|
|
|
|
mList.add(message);
|
|
|
|
notifications.put(conversationUuid, mList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
public void push(final Message message) {
|
2017-05-19 11:30:57 +00:00
|
|
|
synchronized (CATCHUP_LOCK) {
|
2017-04-30 14:19:39 +00:00
|
|
|
final XmppConnection connection = message.getConversation().getAccount().getXmppConnection();
|
|
|
|
if (connection.isWaitingForSmCatchup()) {
|
|
|
|
connection.incrementSmCatchupMessageCounter();
|
|
|
|
pushFromBacklog(message);
|
|
|
|
} else {
|
|
|
|
pushNow(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void pushNow(final Message message) {
|
2015-05-20 10:47:04 +00:00
|
|
|
mXmppConnectionService.updateUnreadCountBadge();
|
2014-11-05 20:37:40 +00:00
|
|
|
if (!notify(message)) {
|
2018-04-15 15:47:51 +00:00
|
|
|
Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because turned off");
|
2014-11-05 20:37:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-10-07 22:35:04 +00:00
|
|
|
final boolean isScreenOn = mXmppConnectionService.isInteractive();
|
2015-01-20 23:24:48 +00:00
|
|
|
if (this.mIsInForeground && isScreenOn && this.mOpenConversation == message.getConversation()) {
|
2018-04-15 15:47:51 +00:00
|
|
|
Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because conversation is open");
|
2014-10-07 11:37:50 +00:00
|
|
|
return;
|
2015-01-20 23:24:48 +00:00
|
|
|
}
|
2014-10-15 12:33:13 +00:00
|
|
|
synchronized (notifications) {
|
2015-10-14 19:18:34 +00:00
|
|
|
pushToStack(message);
|
2014-12-15 16:20:22 +00:00
|
|
|
final Account account = message.getConversation().getAccount();
|
2014-11-10 12:40:16 +00:00
|
|
|
final boolean doNotify = (!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
|
2015-10-14 19:18:34 +00:00
|
|
|
&& !account.inGracePeriod()
|
|
|
|
&& !this.inMiniGracePeriod(account);
|
2014-11-10 12:40:16 +00:00
|
|
|
updateNotification(doNotify);
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
2014-10-15 12:33:13 +00:00
|
|
|
synchronized (notifications) {
|
2018-04-15 15:47:51 +00:00
|
|
|
for (ArrayList<Message> messages : notifications.values()) {
|
2016-08-27 13:25:37 +00:00
|
|
|
markAsReadIfHasDirectReply(messages);
|
|
|
|
}
|
2014-10-15 12:33:13 +00:00
|
|
|
notifications.clear();
|
2014-10-21 22:25:28 +00:00
|
|
|
updateNotification(false);
|
2014-10-15 12:33:13 +00:00
|
|
|
}
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
public void clear(final Conversation conversation) {
|
2017-03-23 14:34:52 +00:00
|
|
|
synchronized (this.mBacklogMessageCounter) {
|
|
|
|
this.mBacklogMessageCounter.remove(conversation);
|
|
|
|
}
|
2014-10-15 12:33:13 +00:00
|
|
|
synchronized (notifications) {
|
2016-08-27 13:25:37 +00:00
|
|
|
markAsReadIfHasDirectReply(conversation);
|
2018-01-22 11:24:10 +00:00
|
|
|
if (notifications.remove(conversation.getUuid()) != null) {
|
2018-04-15 15:47:51 +00:00
|
|
|
cancel(conversation.getUuid(), NOTIFICATION_ID);
|
2018-01-22 11:24:10 +00:00
|
|
|
updateNotification(false, true);
|
|
|
|
}
|
2014-10-15 12:33:13 +00:00
|
|
|
}
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 13:25:37 +00:00
|
|
|
private void markAsReadIfHasDirectReply(final Conversation conversation) {
|
|
|
|
markAsReadIfHasDirectReply(notifications.get(conversation.getUuid()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void markAsReadIfHasDirectReply(final ArrayList<Message> messages) {
|
|
|
|
if (messages != null && messages.size() > 0) {
|
|
|
|
Message last = messages.get(messages.size() - 1);
|
|
|
|
if (last.getStatus() != Message.STATUS_RECEIVED) {
|
2018-04-26 11:22:31 +00:00
|
|
|
if (mXmppConnectionService.markRead((Conversation) last.getConversation(), false)) {
|
2017-04-12 18:54:32 +00:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
}
|
2016-08-27 13:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:24:48 +00:00
|
|
|
private void setNotificationColor(final Builder mBuilder) {
|
2018-04-27 12:50:27 +00:00
|
|
|
mBuilder.setColor(ContextCompat.getColor(mXmppConnectionService, R.color.green600));
|
2015-01-20 23:24:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 22:21:47 +00:00
|
|
|
public void updateNotification(final boolean notify) {
|
2018-01-22 11:24:10 +00:00
|
|
|
updateNotification(notify, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateNotification(final boolean notify, boolean summaryOnly) {
|
2017-07-01 11:41:24 +00:00
|
|
|
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
|
2014-09-28 13:21:56 +00:00
|
|
|
|
|
|
|
if (notifications.size() == 0) {
|
2018-04-15 15:47:51 +00:00
|
|
|
cancel(NOTIFICATION_ID);
|
2014-09-28 13:21:56 +00:00
|
|
|
} else {
|
2014-10-24 11:29:18 +00:00
|
|
|
if (notify) {
|
|
|
|
this.markLastNotification();
|
|
|
|
}
|
2014-12-15 16:20:22 +00:00
|
|
|
final Builder mBuilder;
|
2016-08-25 15:30:44 +00:00
|
|
|
if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
2016-08-25 13:20:06 +00:00
|
|
|
mBuilder = buildSingleConversations(notifications.values().iterator().next());
|
|
|
|
modifyForSoundVibrationAndLight(mBuilder, notify, preferences);
|
2018-04-15 15:47:51 +00:00
|
|
|
notify(NOTIFICATION_ID, mBuilder.build());
|
2014-09-28 13:21:56 +00:00
|
|
|
} else {
|
2014-10-24 16:27:53 +00:00
|
|
|
mBuilder = buildMultipleConversation();
|
2016-08-25 13:20:06 +00:00
|
|
|
modifyForSoundVibrationAndLight(mBuilder, notify, preferences);
|
2018-01-22 11:24:10 +00:00
|
|
|
if (!summaryOnly) {
|
|
|
|
for (Map.Entry<String, ArrayList<Message>> entry : notifications.entrySet()) {
|
|
|
|
Builder singleBuilder = buildSingleConversations(entry.getValue());
|
|
|
|
singleBuilder.setGroup(CONVERSATIONS_GROUP);
|
|
|
|
setNotificationColor(singleBuilder);
|
2018-04-15 15:47:51 +00:00
|
|
|
notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build());
|
2018-01-22 11:24:10 +00:00
|
|
|
}
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
2018-04-15 15:47:51 +00:00
|
|
|
notify(NOTIFICATION_ID, mBuilder.build());
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
2016-08-25 13:20:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void modifyForSoundVibrationAndLight(Builder mBuilder, boolean notify, SharedPreferences preferences) {
|
2017-08-22 10:19:18 +00:00
|
|
|
final Resources resources = mXmppConnectionService.getResources();
|
|
|
|
final String ringtone = preferences.getString("notification_ringtone", resources.getString(R.string.notification_ringtone));
|
|
|
|
final boolean vibrate = preferences.getBoolean("vibrate_on_notification", resources.getBoolean(R.bool.vibrate_on_notification));
|
|
|
|
final boolean led = preferences.getBoolean("led", resources.getBoolean(R.bool.led));
|
|
|
|
final boolean headsup = preferences.getBoolean("notification_headsup", resources.getBoolean(R.bool.headsup_notifications));
|
2016-08-25 13:20:06 +00:00
|
|
|
if (notify && !isQuietHours()) {
|
|
|
|
if (vibrate) {
|
|
|
|
final int dat = 70;
|
|
|
|
final long[] pattern = {0, 3 * dat, dat, dat};
|
|
|
|
mBuilder.setVibrate(pattern);
|
2017-02-16 12:31:26 +00:00
|
|
|
} else {
|
|
|
|
mBuilder.setVibrate(new long[]{0});
|
2015-01-18 04:41:57 +00:00
|
|
|
}
|
2017-08-22 10:19:18 +00:00
|
|
|
Uri uri = Uri.parse(ringtone);
|
|
|
|
try {
|
|
|
|
mBuilder.setSound(fixRingtoneUri(uri));
|
|
|
|
} catch (SecurityException e) {
|
2018-04-15 15:47:51 +00:00
|
|
|
Log.d(Config.LOGTAG, "unable to use custom notification sound " + uri.toString());
|
2016-04-09 19:48:06 +00:00
|
|
|
}
|
2016-08-25 13:20:06 +00:00
|
|
|
}
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
mBuilder.setCategory(Notification.CATEGORY_MESSAGE);
|
|
|
|
}
|
2017-06-24 13:38:16 +00:00
|
|
|
mBuilder.setPriority(notify ? (headsup ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_DEFAULT) : NotificationCompat.PRIORITY_LOW);
|
2016-08-25 13:20:06 +00:00
|
|
|
setNotificationColor(mBuilder);
|
|
|
|
mBuilder.setDefaults(0);
|
|
|
|
if (led) {
|
|
|
|
mBuilder.setLights(0xff00FF00, 2000, 3000);
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 06:10:47 +00:00
|
|
|
private Uri fixRingtoneUri(Uri uri) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && "file".equals(uri.getScheme())) {
|
2018-04-15 15:47:51 +00:00
|
|
|
return FileBackend.getUriForFile(mXmppConnectionService, new File(uri.getPath()));
|
2017-05-13 06:10:47 +00:00
|
|
|
} else {
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 16:27:53 +00:00
|
|
|
private Builder buildMultipleConversation() {
|
2014-12-15 16:20:22 +00:00
|
|
|
final Builder mBuilder = new NotificationCompat.Builder(
|
2014-10-24 16:27:53 +00:00
|
|
|
mXmppConnectionService);
|
2015-01-16 05:36:11 +00:00
|
|
|
final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
|
2014-10-24 16:27:53 +00:00
|
|
|
style.setBigContentTitle(notifications.size()
|
|
|
|
+ " "
|
|
|
|
+ mXmppConnectionService
|
2014-11-05 20:37:40 +00:00
|
|
|
.getString(R.string.unread_conversations));
|
2014-12-15 16:20:22 +00:00
|
|
|
final StringBuilder names = new StringBuilder();
|
2014-10-24 16:27:53 +00:00
|
|
|
Conversation conversation = null;
|
2015-01-16 05:36:11 +00:00
|
|
|
for (final ArrayList<Message> messages : notifications.values()) {
|
2014-10-24 16:27:53 +00:00
|
|
|
if (messages.size() > 0) {
|
2018-04-26 11:22:31 +00:00
|
|
|
conversation = (Conversation) messages.get(0).getConversation();
|
2018-03-11 11:13:56 +00:00
|
|
|
final String name = conversation.getName().toString();
|
2017-03-06 02:31:15 +00:00
|
|
|
SpannableString styledString;
|
2015-12-14 09:54:55 +00:00
|
|
|
if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
|
2015-12-02 11:54:55 +00:00
|
|
|
int count = messages.size();
|
2018-04-15 15:47:51 +00:00
|
|
|
styledString = new SpannableString(name + ": " + mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
|
2017-03-06 02:31:15 +00:00
|
|
|
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
|
|
|
|
style.addLine(styledString);
|
2015-12-02 11:54:55 +00:00
|
|
|
} else {
|
2017-03-06 02:31:15 +00:00
|
|
|
styledString = new SpannableString(name + ": " + UIHelper.getMessagePreview(mXmppConnectionService, messages.get(0)).first);
|
|
|
|
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
|
|
|
|
style.addLine(styledString);
|
2015-12-02 11:54:55 +00:00
|
|
|
}
|
2014-10-24 16:27:53 +00:00
|
|
|
names.append(name);
|
|
|
|
names.append(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (names.length() >= 2) {
|
|
|
|
names.delete(names.length() - 2, names.length());
|
|
|
|
}
|
|
|
|
mBuilder.setContentTitle(notifications.size()
|
|
|
|
+ " "
|
|
|
|
+ mXmppConnectionService
|
2014-11-05 20:37:40 +00:00
|
|
|
.getString(R.string.unread_conversations));
|
2014-10-24 16:27:53 +00:00
|
|
|
mBuilder.setContentText(names.toString());
|
|
|
|
mBuilder.setStyle(style);
|
|
|
|
if (conversation != null) {
|
2015-01-16 05:36:11 +00:00
|
|
|
mBuilder.setContentIntent(createContentIntent(conversation));
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
2016-08-25 13:20:06 +00:00
|
|
|
mBuilder.setGroupSummary(true);
|
|
|
|
mBuilder.setGroup(CONVERSATIONS_GROUP);
|
|
|
|
mBuilder.setDeleteIntent(createDeleteIntent(null));
|
|
|
|
mBuilder.setSmallIcon(R.drawable.ic_notification);
|
2014-10-24 16:27:53 +00:00
|
|
|
return mBuilder;
|
|
|
|
}
|
|
|
|
|
2016-08-25 13:20:06 +00:00
|
|
|
private Builder buildSingleConversations(final ArrayList<Message> messages) {
|
|
|
|
final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
|
2014-10-24 16:27:53 +00:00
|
|
|
if (messages.size() >= 1) {
|
2018-04-26 11:22:31 +00:00
|
|
|
final Conversation conversation = (Conversation) messages.get(0).getConversation();
|
2018-03-11 11:13:56 +00:00
|
|
|
final UnreadConversation.Builder mUnreadBuilder = new UnreadConversation.Builder(conversation.getName().toString());
|
2014-10-24 16:27:53 +00:00
|
|
|
mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService()
|
|
|
|
.get(conversation, getPixel(64)));
|
|
|
|
mBuilder.setContentTitle(conversation.getName());
|
2015-12-14 09:54:55 +00:00
|
|
|
if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
|
2015-12-02 11:54:55 +00:00
|
|
|
int count = messages.size();
|
2018-04-15 15:47:51 +00:00
|
|
|
mBuilder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
|
2014-10-24 16:27:53 +00:00
|
|
|
} else {
|
2015-12-02 11:54:55 +00:00
|
|
|
Message message;
|
|
|
|
if ((message = getImage(messages)) != null) {
|
2017-04-14 11:23:01 +00:00
|
|
|
modifyForImage(mBuilder, mUnreadBuilder, message, messages);
|
2015-12-02 11:54:55 +00:00
|
|
|
} else {
|
2017-04-14 11:23:01 +00:00
|
|
|
modifyForTextOnly(mBuilder, mUnreadBuilder, messages);
|
2015-12-02 11:54:55 +00:00
|
|
|
}
|
2016-09-06 10:13:50 +00:00
|
|
|
RemoteInput remoteInput = new RemoteInput.Builder("text_reply").setLabel(UIHelper.getMessageHint(mXmppConnectionService, conversation)).build();
|
2018-01-28 12:14:30 +00:00
|
|
|
PendingIntent markAsReadPendingIntent = createReadPendingIntent(conversation);
|
|
|
|
NotificationCompat.Action markReadAction = new NotificationCompat.Action.Builder(
|
2018-03-04 12:51:27 +00:00
|
|
|
R.drawable.ic_drafts_white_24dp,
|
2018-01-28 12:14:30 +00:00
|
|
|
mXmppConnectionService.getString(R.string.mark_as_read),
|
|
|
|
markAsReadPendingIntent).build();
|
|
|
|
String replyLabel = mXmppConnectionService.getString(R.string.reply);
|
|
|
|
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.ic_send_text_offline,
|
|
|
|
replyLabel,
|
|
|
|
createReplyIntent(conversation, false)).addRemoteInput(remoteInput).build();
|
|
|
|
NotificationCompat.Action wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_wear_reply,
|
|
|
|
replyLabel,
|
|
|
|
createReplyIntent(conversation, true)).addRemoteInput(remoteInput).build();
|
2016-09-19 19:35:54 +00:00
|
|
|
mBuilder.extend(new NotificationCompat.WearableExtender().addAction(wearReplyAction));
|
2017-04-14 11:23:01 +00:00
|
|
|
mUnreadBuilder.setReplyAction(createReplyIntent(conversation, true), remoteInput);
|
2018-01-28 12:14:30 +00:00
|
|
|
mUnreadBuilder.setReadPendingIntent(markAsReadPendingIntent);
|
2017-04-14 11:23:01 +00:00
|
|
|
mBuilder.extend(new NotificationCompat.CarExtender().setUnreadConversation(mUnreadBuilder.build()));
|
2018-01-28 12:14:30 +00:00
|
|
|
int addedActionsCount = 1;
|
|
|
|
mBuilder.addAction(markReadAction);
|
2016-08-25 15:30:44 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
2016-09-19 19:35:54 +00:00
|
|
|
mBuilder.addAction(replyAction);
|
2018-01-28 12:14:30 +00:00
|
|
|
++addedActionsCount;
|
2016-08-31 15:02:42 +00:00
|
|
|
}
|
2018-01-28 12:14:30 +00:00
|
|
|
|
|
|
|
if (displaySnoozeAction(messages)) {
|
|
|
|
String label = mXmppConnectionService.getString(R.string.snooze);
|
|
|
|
PendingIntent pendingSnoozeIntent = createSnoozeIntent(conversation);
|
|
|
|
NotificationCompat.Action snoozeAction = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.ic_notifications_paused_white_24dp,
|
|
|
|
label,
|
|
|
|
pendingSnoozeIntent).build();
|
|
|
|
mBuilder.addAction(snoozeAction);
|
|
|
|
++addedActionsCount;
|
2015-12-02 11:54:55 +00:00
|
|
|
}
|
2018-01-28 12:14:30 +00:00
|
|
|
if (addedActionsCount < 3) {
|
|
|
|
final Message firstLocationMessage = getFirstLocationMessage(messages);
|
|
|
|
if (firstLocationMessage != null) {
|
|
|
|
String label = mXmppConnectionService.getResources().getString(R.string.show_location);
|
|
|
|
PendingIntent pendingShowLocationIntent = createShowLocationIntent(firstLocationMessage);
|
|
|
|
NotificationCompat.Action locationAction = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.ic_room_white_24dp,
|
|
|
|
label,
|
|
|
|
pendingShowLocationIntent).build();
|
|
|
|
mBuilder.addAction(locationAction);
|
|
|
|
++addedActionsCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (addedActionsCount < 3) {
|
|
|
|
Message firstDownloadableMessage = getFirstDownloadableMessage(messages);
|
|
|
|
if (firstDownloadableMessage != null) {
|
2018-02-01 18:47:51 +00:00
|
|
|
String label = mXmppConnectionService.getResources().getString(R.string.download_x_file, UIHelper.getFileDescriptionString(mXmppConnectionService, firstDownloadableMessage));
|
|
|
|
PendingIntent pendingDownloadIntent = createDownloadIntent(firstDownloadableMessage);
|
2018-01-28 12:14:30 +00:00
|
|
|
NotificationCompat.Action downloadAction = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.ic_file_download_white_24dp,
|
|
|
|
label,
|
|
|
|
pendingDownloadIntent).build();
|
|
|
|
mBuilder.addAction(downloadAction);
|
|
|
|
++addedActionsCount;
|
|
|
|
}
|
2015-12-02 11:54:55 +00:00
|
|
|
}
|
2015-03-10 20:46:13 +00:00
|
|
|
}
|
2016-08-26 11:35:01 +00:00
|
|
|
if (conversation.getMode() == Conversation.MODE_SINGLE) {
|
|
|
|
Contact contact = conversation.getContact();
|
|
|
|
Uri systemAccount = contact.getSystemAccount();
|
|
|
|
if (systemAccount != null) {
|
|
|
|
mBuilder.addPerson(systemAccount.toString());
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 13:20:06 +00:00
|
|
|
mBuilder.setWhen(conversation.getLatestMessage().getTimeSent());
|
|
|
|
mBuilder.setSmallIcon(R.drawable.ic_notification);
|
|
|
|
mBuilder.setDeleteIntent(createDeleteIntent(conversation));
|
2015-01-16 05:36:11 +00:00
|
|
|
mBuilder.setContentIntent(createContentIntent(conversation));
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
|
|
|
return mBuilder;
|
|
|
|
}
|
|
|
|
|
2018-01-28 12:14:30 +00:00
|
|
|
private static boolean displaySnoozeAction(List<Message> messages) {
|
|
|
|
int numberOfMessagesWithoutReply = 0;
|
2018-04-15 15:47:51 +00:00
|
|
|
for (Message message : messages) {
|
2018-01-28 12:14:30 +00:00
|
|
|
if (message.getStatus() == Message.STATUS_RECEIVED) {
|
|
|
|
++numberOfMessagesWithoutReply;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numberOfMessagesWithoutReply >= 3;
|
|
|
|
}
|
|
|
|
|
2017-04-14 11:23:01 +00:00
|
|
|
private void modifyForImage(final Builder builder, final UnreadConversation.Builder uBuilder,
|
2018-04-15 15:47:51 +00:00
|
|
|
final Message message, final ArrayList<Message> messages) {
|
2014-10-24 16:27:53 +00:00
|
|
|
try {
|
2014-12-15 16:20:22 +00:00
|
|
|
final Bitmap bitmap = mXmppConnectionService.getFileBackend()
|
2015-12-01 16:15:36 +00:00
|
|
|
.getThumbnail(message, getPixel(288), false);
|
2014-12-15 16:20:22 +00:00
|
|
|
final ArrayList<Message> tmp = new ArrayList<>();
|
|
|
|
for (final Message msg : messages) {
|
2014-10-24 16:27:53 +00:00
|
|
|
if (msg.getType() == Message.TYPE_TEXT
|
2015-07-10 13:11:03 +00:00
|
|
|
&& msg.getTransferable() == null) {
|
2014-10-24 16:27:53 +00:00
|
|
|
tmp.add(msg);
|
2015-12-01 16:15:36 +00:00
|
|
|
}
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
2014-12-15 16:20:22 +00:00
|
|
|
final BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
|
2014-10-24 16:27:53 +00:00
|
|
|
bigPictureStyle.bigPicture(bitmap);
|
|
|
|
if (tmp.size() > 0) {
|
2016-08-25 15:30:44 +00:00
|
|
|
CharSequence text = getMergedBodies(tmp);
|
|
|
|
bigPictureStyle.setSummaryText(text);
|
|
|
|
builder.setContentText(text);
|
2014-10-24 16:27:53 +00:00
|
|
|
} else {
|
2018-04-16 07:55:16 +00:00
|
|
|
builder.setContentText(UIHelper.getFileDescriptionString(mXmppConnectionService, message));
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
|
|
|
builder.setStyle(bigPictureStyle);
|
2014-12-15 16:20:22 +00:00
|
|
|
} catch (final FileNotFoundException e) {
|
2017-04-14 11:23:01 +00:00
|
|
|
modifyForTextOnly(builder, uBuilder, messages);
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 11:23:01 +00:00
|
|
|
private void modifyForTextOnly(final Builder builder, final UnreadConversation.Builder uBuilder, final ArrayList<Message> messages) {
|
2016-08-25 13:20:06 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(mXmppConnectionService.getString(R.string.me));
|
2018-04-26 11:22:31 +00:00
|
|
|
final Conversation conversation = (Conversation) messages.get(0).getConversation();
|
2016-08-25 13:20:06 +00:00
|
|
|
if (conversation.getMode() == Conversation.MODE_MULTI) {
|
|
|
|
messagingStyle.setConversationTitle(conversation.getName());
|
2016-01-13 16:35:59 +00:00
|
|
|
}
|
2016-08-25 13:20:06 +00:00
|
|
|
for (Message message : messages) {
|
|
|
|
String sender = message.getStatus() == Message.STATUS_RECEIVED ? UIHelper.getMessageDisplayName(message) : null;
|
2018-04-15 15:47:51 +00:00
|
|
|
messagingStyle.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender);
|
2016-08-25 13:20:06 +00:00
|
|
|
}
|
|
|
|
builder.setStyle(messagingStyle);
|
|
|
|
} else {
|
2018-04-15 15:47:51 +00:00
|
|
|
if (messages.get(0).getConversation().getMode() == Conversation.MODE_SINGLE) {
|
2017-03-06 02:31:15 +00:00
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getMergedBodies(messages)));
|
2017-04-12 18:54:32 +00:00
|
|
|
builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService, messages.get(0)).first);
|
2017-03-07 17:12:33 +00:00
|
|
|
} else {
|
2017-03-06 02:31:15 +00:00
|
|
|
final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
|
|
|
|
SpannableString styledString;
|
|
|
|
for (Message message : messages) {
|
|
|
|
final String name = UIHelper.getMessageDisplayName(message);
|
|
|
|
styledString = new SpannableString(name + ": " + message.getBody());
|
|
|
|
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
|
|
|
|
style.addLine(styledString);
|
|
|
|
}
|
|
|
|
builder.setStyle(style);
|
2017-03-07 17:12:33 +00:00
|
|
|
int count = messages.size();
|
2018-04-15 15:47:51 +00:00
|
|
|
if (count == 1) {
|
2017-03-06 02:31:15 +00:00
|
|
|
final String name = UIHelper.getMessageDisplayName(messages.get(0));
|
|
|
|
styledString = new SpannableString(name + ": " + messages.get(0).getBody());
|
|
|
|
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
|
|
|
|
builder.setContentText(styledString);
|
2017-03-07 17:12:33 +00:00
|
|
|
} else {
|
2018-04-15 15:47:51 +00:00
|
|
|
builder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
|
2017-03-06 02:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-09 09:26:30 +00:00
|
|
|
}
|
2017-04-29 17:25:57 +00:00
|
|
|
/** message preview for Android Auto **/
|
|
|
|
for (Message message : messages) {
|
2018-05-06 09:22:20 +00:00
|
|
|
Pair<CharSequence, Boolean> preview = UIHelper.getMessagePreview(mXmppConnectionService, message);
|
2017-04-29 17:25:57 +00:00
|
|
|
// only show user written text
|
2017-11-23 08:36:51 +00:00
|
|
|
if (!preview.second) {
|
2018-05-06 09:22:20 +00:00
|
|
|
uBuilder.addMessage(preview.first.toString());
|
2017-04-29 17:25:57 +00:00
|
|
|
uBuilder.setLatestTimestamp(message.getTimeSent());
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 09:26:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 05:36:11 +00:00
|
|
|
private Message getImage(final Iterable<Message> messages) {
|
2016-08-25 15:30:44 +00:00
|
|
|
Message image = null;
|
2014-12-15 16:20:22 +00:00
|
|
|
for (final Message message : messages) {
|
2016-08-25 15:30:44 +00:00
|
|
|
if (message.getStatus() != Message.STATUS_RECEIVED) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-07-28 23:57:08 +00:00
|
|
|
if (message.getType() != Message.TYPE_TEXT
|
2015-07-10 13:11:03 +00:00
|
|
|
&& message.getTransferable() == null
|
2015-07-28 23:57:08 +00:00
|
|
|
&& message.getEncryption() != Message.ENCRYPTION_PGP
|
|
|
|
&& message.getFileParams().height > 0) {
|
2016-08-25 15:30:44 +00:00
|
|
|
image = message;
|
2015-12-01 16:15:36 +00:00
|
|
|
}
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
2016-08-25 15:30:44 +00:00
|
|
|
return image;
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 05:36:11 +00:00
|
|
|
private Message getFirstDownloadableMessage(final Iterable<Message> messages) {
|
|
|
|
for (final Message message : messages) {
|
2017-04-05 20:32:02 +00:00
|
|
|
if (message.getTransferable() != null || (message.getType() == Message.TYPE_TEXT && message.treatAsDownloadable())) {
|
2015-01-16 05:36:11 +00:00
|
|
|
return message;
|
2015-01-20 23:24:48 +00:00
|
|
|
}
|
2015-01-16 05:36:11 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-03-10 20:46:13 +00:00
|
|
|
private Message getFirstLocationMessage(final Iterable<Message> messages) {
|
2015-12-01 16:15:36 +00:00
|
|
|
for (final Message message : messages) {
|
2017-08-03 11:24:41 +00:00
|
|
|
if (message.isGeoUri()) {
|
2015-03-10 20:46:13 +00:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-16 05:36:11 +00:00
|
|
|
private CharSequence getMergedBodies(final ArrayList<Message> messages) {
|
2014-12-15 16:20:22 +00:00
|
|
|
final StringBuilder text = new StringBuilder();
|
2018-04-15 15:47:51 +00:00
|
|
|
for (Message message : messages) {
|
2017-04-12 18:54:32 +00:00
|
|
|
if (text.length() != 0) {
|
2014-10-24 16:27:53 +00:00
|
|
|
text.append("\n");
|
|
|
|
}
|
2017-04-12 18:54:32 +00:00
|
|
|
text.append(UIHelper.getMessagePreview(mXmppConnectionService, message).first);
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
|
|
|
return text.toString();
|
|
|
|
}
|
|
|
|
|
2015-03-10 20:46:13 +00:00
|
|
|
private PendingIntent createShowLocationIntent(final Message message) {
|
2018-05-01 10:55:11 +00:00
|
|
|
Iterable<Intent> intents = GeoHelper.createGeoIntentsFromMessage(mXmppConnectionService, message);
|
2015-12-01 16:15:36 +00:00
|
|
|
for (Intent intent : intents) {
|
2015-03-10 20:46:13 +00:00
|
|
|
if (intent.resolveActivity(mXmppConnectionService.getPackageManager()) != null) {
|
2018-01-28 12:14:30 +00:00
|
|
|
return PendingIntent.getActivity(mXmppConnectionService, generateRequestCode(message.getConversation(), 18), intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2015-03-10 20:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return createOpenConversationsIntent();
|
|
|
|
}
|
|
|
|
|
2015-01-16 05:36:11 +00:00
|
|
|
private PendingIntent createContentIntent(final String conversationUuid, final String downloadMessageUuid) {
|
2018-04-15 15:47:51 +00:00
|
|
|
final Intent viewConversationIntent = new Intent(mXmppConnectionService, ConversationsActivity.class);
|
2018-03-07 18:05:28 +00:00
|
|
|
viewConversationIntent.setAction(ConversationsActivity.ACTION_VIEW_CONVERSATION);
|
|
|
|
viewConversationIntent.putExtra(ConversationsActivity.EXTRA_CONVERSATION, conversationUuid);
|
2015-01-16 05:36:11 +00:00
|
|
|
if (downloadMessageUuid != null) {
|
2018-03-07 18:05:28 +00:00
|
|
|
viewConversationIntent.putExtra(ConversationsActivity.EXTRA_DOWNLOAD_UUID, downloadMessageUuid);
|
2016-07-25 12:15:47 +00:00
|
|
|
return PendingIntent.getActivity(mXmppConnectionService,
|
2018-01-28 12:14:30 +00:00
|
|
|
generateRequestCode(conversationUuid, 8),
|
2016-07-25 12:15:47 +00:00
|
|
|
viewConversationIntent,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
} else {
|
|
|
|
return PendingIntent.getActivity(mXmppConnectionService,
|
2018-01-28 12:14:30 +00:00
|
|
|
generateRequestCode(conversationUuid, 10),
|
2016-07-25 12:15:47 +00:00
|
|
|
viewConversationIntent,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
2015-01-16 05:36:11 +00:00
|
|
|
}
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
2014-10-02 16:31:19 +00:00
|
|
|
|
2018-01-28 12:14:30 +00:00
|
|
|
private int generateRequestCode(String uuid, int actionId) {
|
|
|
|
return (actionId * NOTIFICATION_ID_MULTIPLIER) + (uuid.hashCode() % NOTIFICATION_ID_MULTIPLIER);
|
|
|
|
}
|
|
|
|
|
2018-04-26 11:22:31 +00:00
|
|
|
private int generateRequestCode(Conversational conversation, int actionId) {
|
2018-01-28 12:14:30 +00:00
|
|
|
return generateRequestCode(conversation.getUuid(), actionId);
|
|
|
|
}
|
|
|
|
|
2015-01-16 05:36:11 +00:00
|
|
|
private PendingIntent createDownloadIntent(final Message message) {
|
|
|
|
return createContentIntent(message.getConversationUuid(), message.getUuid());
|
|
|
|
}
|
|
|
|
|
2018-04-26 11:22:31 +00:00
|
|
|
private PendingIntent createContentIntent(final Conversational conversation) {
|
2015-01-16 05:36:11 +00:00
|
|
|
return createContentIntent(conversation.getUuid(), null);
|
|
|
|
}
|
|
|
|
|
2016-08-25 13:20:06 +00:00
|
|
|
private PendingIntent createDeleteIntent(Conversation conversation) {
|
|
|
|
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
|
2014-11-12 13:41:43 +00:00
|
|
|
intent.setAction(XmppConnectionService.ACTION_CLEAR_NOTIFICATION);
|
2016-08-25 13:20:06 +00:00
|
|
|
if (conversation != null) {
|
|
|
|
intent.putExtra("uuid", conversation.getUuid());
|
2018-01-28 12:14:30 +00:00
|
|
|
return PendingIntent.getService(mXmppConnectionService, generateRequestCode(conversation, 20), intent, 0);
|
2016-08-25 13:20:06 +00:00
|
|
|
}
|
2014-11-12 13:41:43 +00:00
|
|
|
return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
|
|
|
|
}
|
|
|
|
|
2016-09-19 19:35:54 +00:00
|
|
|
private PendingIntent createReplyIntent(Conversation conversation, boolean dismissAfterReply) {
|
2016-08-25 15:30:44 +00:00
|
|
|
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
|
|
|
|
intent.setAction(XmppConnectionService.ACTION_REPLY_TO_CONVERSATION);
|
2018-04-15 15:47:51 +00:00
|
|
|
intent.putExtra("uuid", conversation.getUuid());
|
|
|
|
intent.putExtra("dismiss_notification", dismissAfterReply);
|
|
|
|
final int id = generateRequestCode(conversation, dismissAfterReply ? 12 : 14);
|
2016-09-19 19:35:54 +00:00
|
|
|
return PendingIntent.getService(mXmppConnectionService, id, intent, 0);
|
2016-08-25 15:30:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 11:23:01 +00:00
|
|
|
private PendingIntent createReadPendingIntent(Conversation conversation) {
|
|
|
|
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
|
|
|
|
intent.setAction(XmppConnectionService.ACTION_MARK_AS_READ);
|
|
|
|
intent.putExtra("uuid", conversation.getUuid());
|
|
|
|
intent.setPackage(mXmppConnectionService.getPackageName());
|
2018-04-15 15:47:51 +00:00
|
|
|
return PendingIntent.getService(mXmppConnectionService, generateRequestCode(conversation, 16), intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2018-01-28 12:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public PendingIntent createSnoozeIntent(Conversation conversation) {
|
|
|
|
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
|
|
|
|
intent.setAction(XmppConnectionService.ACTION_SNOOZE);
|
|
|
|
intent.putExtra("uuid", conversation.getUuid());
|
|
|
|
intent.setPackage(mXmppConnectionService.getPackageName());
|
2018-04-15 15:47:51 +00:00
|
|
|
return PendingIntent.getService(mXmppConnectionService, generateRequestCode(conversation, 22), intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2017-04-14 11:23:01 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 16:13:34 +00:00
|
|
|
private PendingIntent createTryAgainIntent() {
|
|
|
|
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
|
|
|
|
intent.setAction(XmppConnectionService.ACTION_TRY_AGAIN);
|
|
|
|
return PendingIntent.getService(mXmppConnectionService, 45, intent, 0);
|
|
|
|
}
|
|
|
|
|
2016-10-04 09:16:59 +00:00
|
|
|
private PendingIntent createDismissErrorIntent() {
|
2015-12-01 16:15:36 +00:00
|
|
|
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
|
2016-10-04 09:16:59 +00:00
|
|
|
intent.setAction(XmppConnectionService.ACTION_DISMISS_ERROR_NOTIFICATIONS);
|
|
|
|
return PendingIntent.getService(mXmppConnectionService, 69, intent, 0);
|
2014-10-02 15:36:02 +00:00
|
|
|
}
|
2014-09-28 14:33:25 +00:00
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private boolean wasHighlightedOrPrivate(final Message message) {
|
2018-04-26 11:22:31 +00:00
|
|
|
if (message.getConversation() instanceof Conversation) {
|
|
|
|
Conversation conversation = (Conversation) message.getConversation();
|
|
|
|
final String nick = conversation.getMucOptions().getActualNick();
|
|
|
|
final Pattern highlight = generateNickHighlightPattern(nick);
|
|
|
|
if (message.getBody() == null || nick == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final Matcher m = highlight.matcher(message.getBody());
|
|
|
|
return (m.find() || message.getType() == Message.TYPE_PRIVATE);
|
|
|
|
} else {
|
2014-10-03 09:55:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-09-28 14:33:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 13:54:59 +00:00
|
|
|
public static Pattern generateNickHighlightPattern(final String nick) {
|
2018-04-24 10:31:37 +00:00
|
|
|
return Pattern.compile("(?<=(^|\\s))" + Pattern.quote(nick) + "\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
|
2014-09-28 14:33:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
public void setOpenConversation(final Conversation conversation) {
|
2014-09-29 16:28:13 +00:00
|
|
|
this.mOpenConversation = conversation;
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
public void setIsInForeground(final boolean foreground) {
|
2014-09-29 16:28:13 +00:00
|
|
|
this.mIsInForeground = foreground;
|
|
|
|
}
|
2014-10-21 12:57:16 +00:00
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private int getPixel(final int dp) {
|
|
|
|
final DisplayMetrics metrics = mXmppConnectionService.getResources()
|
2015-12-01 16:15:36 +00:00
|
|
|
.getDisplayMetrics();
|
2014-10-21 12:57:16 +00:00
|
|
|
return ((int) (dp * metrics.density));
|
|
|
|
}
|
2014-10-24 11:29:18 +00:00
|
|
|
|
|
|
|
private void markLastNotification() {
|
|
|
|
this.mLastNotification = SystemClock.elapsedRealtime();
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private boolean inMiniGracePeriod(final Account account) {
|
|
|
|
final int miniGrace = account.getStatus() == Account.State.ONLINE ? Config.MINI_GRACE_PERIOD
|
2015-12-01 16:15:36 +00:00
|
|
|
: Config.MINI_GRACE_PERIOD * 2;
|
2014-10-24 11:29:18 +00:00
|
|
|
return SystemClock.elapsedRealtime() < (this.mLastNotification + miniGrace);
|
|
|
|
}
|
2014-11-12 13:41:43 +00:00
|
|
|
|
|
|
|
public Notification createForegroundNotification() {
|
2014-12-15 16:20:22 +00:00
|
|
|
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
|
2015-01-22 04:00:35 +00:00
|
|
|
|
2014-11-12 13:41:43 +00:00
|
|
|
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service));
|
2015-12-01 16:15:36 +00:00
|
|
|
if (Config.SHOW_CONNECTED_ACCOUNTS) {
|
|
|
|
List<Account> accounts = mXmppConnectionService.getAccounts();
|
|
|
|
int enabled = 0;
|
|
|
|
int connected = 0;
|
|
|
|
for (Account account : accounts) {
|
|
|
|
if (account.isOnlineAndConnected()) {
|
|
|
|
connected++;
|
|
|
|
enabled++;
|
2017-11-06 12:57:25 +00:00
|
|
|
} else if (account.isEnabled()) {
|
2015-12-01 16:15:36 +00:00
|
|
|
enabled++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mBuilder.setContentText(mXmppConnectionService.getString(R.string.connected_accounts, connected, enabled));
|
|
|
|
} else {
|
|
|
|
mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_open_conversations));
|
|
|
|
}
|
2015-01-15 15:17:55 +00:00
|
|
|
mBuilder.setContentIntent(createOpenConversationsIntent());
|
2014-11-12 16:33:24 +00:00
|
|
|
mBuilder.setWhen(0);
|
2015-12-01 16:15:36 +00:00
|
|
|
mBuilder.setPriority(Config.SHOW_CONNECTED_ACCOUNTS ? NotificationCompat.PRIORITY_DEFAULT : NotificationCompat.PRIORITY_MIN);
|
2015-09-22 06:50:54 +00:00
|
|
|
mBuilder.setSmallIcon(R.drawable.ic_link_white_24dp);
|
2014-11-12 13:41:43 +00:00
|
|
|
return mBuilder.build();
|
|
|
|
}
|
2014-11-18 14:26:28 +00:00
|
|
|
|
2015-01-15 15:17:55 +00:00
|
|
|
private PendingIntent createOpenConversationsIntent() {
|
2018-03-07 18:05:28 +00:00
|
|
|
return PendingIntent.getActivity(mXmppConnectionService, 0, new Intent(mXmppConnectionService, ConversationsActivity.class), 0);
|
2015-01-15 15:17:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 14:26:28 +00:00
|
|
|
public void updateErrorNotification() {
|
2014-12-15 16:20:22 +00:00
|
|
|
final List<Account> errors = new ArrayList<>();
|
|
|
|
for (final Account account : mXmppConnectionService.getAccounts()) {
|
2016-10-04 09:16:59 +00:00
|
|
|
if (account.hasErrorStatus() && account.showErrorNotification()) {
|
2014-11-18 14:26:28 +00:00
|
|
|
errors.add(account);
|
|
|
|
}
|
|
|
|
}
|
2017-07-01 11:41:24 +00:00
|
|
|
if (mXmppConnectionService.keepForegroundService()) {
|
2018-04-15 15:47:51 +00:00
|
|
|
notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
|
2015-12-01 16:15:36 +00:00
|
|
|
}
|
2014-12-15 16:20:22 +00:00
|
|
|
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
|
2014-11-18 14:26:28 +00:00
|
|
|
if (errors.size() == 0) {
|
2018-04-15 15:47:51 +00:00
|
|
|
cancel(ERROR_NOTIFICATION_ID);
|
2014-11-18 14:26:28 +00:00
|
|
|
return;
|
|
|
|
} else if (errors.size() == 1) {
|
|
|
|
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_account));
|
2018-03-05 17:30:40 +00:00
|
|
|
mBuilder.setContentText(errors.get(0).getJid().asBareJid().toString());
|
2014-11-18 14:26:28 +00:00
|
|
|
} else {
|
|
|
|
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_accounts));
|
|
|
|
mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_fix));
|
|
|
|
}
|
2015-02-10 16:13:34 +00:00
|
|
|
mBuilder.addAction(R.drawable.ic_autorenew_white_24dp,
|
|
|
|
mXmppConnectionService.getString(R.string.try_again),
|
|
|
|
createTryAgainIntent());
|
2016-10-04 09:16:59 +00:00
|
|
|
mBuilder.setDeleteIntent(createDismissErrorIntent());
|
2018-02-20 07:22:08 +00:00
|
|
|
mBuilder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
|
2015-01-22 04:00:35 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
2015-04-28 12:28:51 +00:00
|
|
|
mBuilder.setSmallIcon(R.drawable.ic_warning_white_24dp);
|
2015-01-22 04:00:35 +00:00
|
|
|
} else {
|
|
|
|
mBuilder.setSmallIcon(R.drawable.ic_stat_alert_warning);
|
|
|
|
}
|
2018-02-20 07:22:08 +00:00
|
|
|
mBuilder.setLocalOnly(true);
|
2017-11-08 19:33:05 +00:00
|
|
|
mBuilder.setPriority(NotificationCompat.PRIORITY_LOW);
|
2016-10-04 09:16:59 +00:00
|
|
|
mBuilder.setContentIntent(PendingIntent.getActivity(mXmppConnectionService,
|
|
|
|
145,
|
2018-04-15 15:47:51 +00:00
|
|
|
new Intent(mXmppConnectionService, ManageAccountActivity.class),
|
2016-10-04 09:16:59 +00:00
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT));
|
2018-04-15 15:47:51 +00:00
|
|
|
notify(ERROR_NOTIFICATION_ID, mBuilder.build());
|
2014-11-18 14:26:28 +00:00
|
|
|
}
|
2017-09-18 15:56:25 +00:00
|
|
|
|
|
|
|
public Notification updateFileAddingNotification(int current, Message message) {
|
|
|
|
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
|
|
|
|
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.transcoding_video));
|
|
|
|
mBuilder.setProgress(100, current, false);
|
|
|
|
mBuilder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp);
|
|
|
|
mBuilder.setContentIntent(createContentIntent(message.getConversation()));
|
|
|
|
Notification notification = mBuilder.build();
|
2018-04-15 15:47:51 +00:00
|
|
|
notify(FOREGROUND_NOTIFICATION_ID, notification);
|
2017-09-18 15:56:25 +00:00
|
|
|
return notification;
|
|
|
|
}
|
2018-04-15 15:47:51 +00:00
|
|
|
|
|
|
|
private void notify(String tag, int id, Notification notification) {
|
|
|
|
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
|
|
|
|
try {
|
|
|
|
notificationManager.notify(tag, id, notification);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to make notification", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notify(int id, Notification notification) {
|
|
|
|
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
|
|
|
|
try {
|
|
|
|
notificationManager.notify(id, notification);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to make notification", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cancel(int id) {
|
|
|
|
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
|
|
|
|
try {
|
|
|
|
notificationManager.cancel(id);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to cancel notification", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cancel(String tag, int id) {
|
|
|
|
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
|
|
|
|
try {
|
|
|
|
notificationManager.cancel(tag, id);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to cancel notification", e);
|
|
|
|
}
|
|
|
|
}
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|