2014-09-28 13:21:56 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
2015-01-14 03:46:06 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2014-09-28 13:21:56 +00:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2014-10-24 12:34:46 +00:00
|
|
|
import android.graphics.Bitmap;
|
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-02 20:17:25 +00:00
|
|
|
import android.os.PowerManager;
|
2014-10-24 11:29:18 +00:00
|
|
|
import android.os.SystemClock;
|
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;
|
2014-09-28 13:21:56 +00:00
|
|
|
import android.support.v4.app.TaskStackBuilder;
|
|
|
|
import android.text.Html;
|
2014-10-21 12:57:16 +00:00
|
|
|
import android.util.DisplayMetrics;
|
2015-01-02 11:04:33 +00:00
|
|
|
import android.util.Log;
|
2014-09-28 13:21:56 +00:00
|
|
|
|
2015-01-14 03:46:06 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
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;
|
2014-11-10 12:40:16 +00:00
|
|
|
import java.util.HashMap;
|
2014-11-05 20:37:40 +00:00
|
|
|
import java.util.LinkedHashMap;
|
2014-11-18 14:26:28 +00:00
|
|
|
import java.util.List;
|
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;
|
2014-09-28 13:21:56 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
|
|
|
import eu.siacs.conversations.ui.ConversationActivity;
|
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-01-12 15:09:39 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
2014-09-28 13:21:56 +00:00
|
|
|
|
|
|
|
public class NotificationService {
|
|
|
|
|
|
|
|
private XmppConnectionService mXmppConnectionService;
|
|
|
|
|
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
|
|
|
|
2014-12-15 16:39:18 +00:00
|
|
|
public static final int NOTIFICATION_ID = 0x2342;
|
|
|
|
public static final int FOREGROUND_NOTIFICATION_ID = 0x8899;
|
|
|
|
public static final int ERROR_NOTIFICATION_ID = 0x5678;
|
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
|
|
|
|
2014-09-28 13:21:56 +00:00
|
|
|
public NotificationService(XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
public boolean notify(final Message message) {
|
2014-11-05 20:37:40 +00:00
|
|
|
return (message.getStatus() == Message.STATUS_RECEIVED)
|
2014-12-14 07:02:17 +00:00
|
|
|
&& notificationsEnabled()
|
|
|
|
&& !message.getConversation().isMuted()
|
|
|
|
&& (message.getConversation().getMode() == Conversation.MODE_SINGLE
|
2014-11-05 20:37:40 +00:00
|
|
|
|| conferenceNotificationsEnabled()
|
|
|
|
|| wasHighlightedOrPrivate(message)
|
2014-12-14 07:02:17 +00:00
|
|
|
);
|
2014-11-05 20:37:40 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 03:46:06 +00:00
|
|
|
public void notifyPebble(final Message message) {
|
2014-11-10 12:40:16 +00:00
|
|
|
final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
|
|
|
|
|
|
|
|
final Conversation conversation = message.getConversation();
|
2015-01-14 03:46:06 +00:00
|
|
|
final JSONObject jsonData = new JSONObject(new HashMap<String, String>(2) {{
|
|
|
|
put("title", conversation.getName());
|
|
|
|
put("body", message.getBody());
|
|
|
|
}});
|
2014-11-10 12:40:16 +00:00
|
|
|
final String notificationData = new JSONArray().put(jsonData).toString();
|
|
|
|
|
|
|
|
i.putExtra("messageType", "PEBBLE_ALERT");
|
|
|
|
i.putExtra("sender", "Conversations"); /* XXX: Shouldn't be hardcoded, e.g., AbstractGenerator.APP_NAME); */
|
|
|
|
i.putExtra("notificationData", notificationData);
|
|
|
|
|
|
|
|
mXmppConnectionService.sendBroadcast(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-05 20:37:40 +00:00
|
|
|
public boolean notificationsEnabled() {
|
|
|
|
return mXmppConnectionService.getPreferences().getBoolean("show_notification", true);
|
|
|
|
}
|
|
|
|
|
2014-12-14 07:02:17 +00:00
|
|
|
public boolean isQuietHours() {
|
|
|
|
if (!mXmppConnectionService.getPreferences().getBoolean("enable_quiet_hours", false)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-16 23:03:16 +00:00
|
|
|
final long startTime = mXmppConnectionService.getPreferences().getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY;
|
|
|
|
final long endTime = mXmppConnectionService.getPreferences().getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 20:37:40 +00:00
|
|
|
public boolean conferenceNotificationsEnabled() {
|
|
|
|
return mXmppConnectionService.getPreferences().getBoolean("always_notify_in_conference", false);
|
|
|
|
}
|
|
|
|
|
2015-01-14 03:46:06 +00:00
|
|
|
@SuppressLint("NewApi")
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
private boolean isInteractive() {
|
|
|
|
final PowerManager pm = (PowerManager) mXmppConnectionService
|
|
|
|
.getSystemService(Context.POWER_SERVICE);
|
|
|
|
|
|
|
|
final boolean isScreenOn;
|
|
|
|
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
isScreenOn = pm.isScreenOn();
|
|
|
|
} else {
|
|
|
|
isScreenOn = pm.isInteractive();
|
|
|
|
}
|
|
|
|
|
|
|
|
return isScreenOn;
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
public void push(final Message message) {
|
2014-11-05 20:37:40 +00:00
|
|
|
if (!notify(message)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-14 03:46:06 +00:00
|
|
|
|
|
|
|
final boolean isScreenOn = isInteractive();
|
2014-10-15 12:33:13 +00:00
|
|
|
|
2014-10-02 20:17:25 +00:00
|
|
|
if (this.mIsInForeground && isScreenOn
|
2014-09-29 16:28:13 +00:00
|
|
|
&& this.mOpenConversation == message.getConversation()) {
|
2014-10-07 11:37:50 +00:00
|
|
|
return;
|
2014-12-14 07:02:17 +00:00
|
|
|
}
|
2014-10-15 12:33:13 +00:00
|
|
|
synchronized (notifications) {
|
2014-12-15 16:20:22 +00:00
|
|
|
final String conversationUuid = message.getConversationUuid();
|
2014-10-15 12:33:13 +00:00
|
|
|
if (notifications.containsKey(conversationUuid)) {
|
|
|
|
notifications.get(conversationUuid).add(message);
|
|
|
|
} else {
|
2014-12-15 16:20:22 +00:00
|
|
|
final ArrayList<Message> mList = new ArrayList<>();
|
2014-10-15 12:33:13 +00:00
|
|
|
mList.add(message);
|
|
|
|
notifications.put(conversationUuid, mList);
|
|
|
|
}
|
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-01-14 03:46:06 +00:00
|
|
|
&& !account.inGracePeriod()
|
|
|
|
&& !this.inMiniGracePeriod(account);
|
2014-11-10 12:40:16 +00:00
|
|
|
updateNotification(doNotify);
|
|
|
|
if (doNotify) {
|
|
|
|
notifyPebble(message);
|
|
|
|
}
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
2014-10-21 22:25:28 +00:00
|
|
|
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
2014-10-15 12:33:13 +00:00
|
|
|
synchronized (notifications) {
|
|
|
|
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) {
|
2014-10-15 12:33:13 +00:00
|
|
|
synchronized (notifications) {
|
|
|
|
notifications.remove(conversation.getUuid());
|
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
|
|
|
private void updateNotification(final boolean notify) {
|
|
|
|
final NotificationManager notificationManager = (NotificationManager) mXmppConnectionService
|
2014-12-14 07:02:17 +00:00
|
|
|
.getSystemService(Context.NOTIFICATION_SERVICE);
|
2014-12-15 16:20:22 +00:00
|
|
|
final SharedPreferences preferences = mXmppConnectionService.getPreferences();
|
2014-09-28 13:21:56 +00:00
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
final String ringtone = preferences.getString("notification_ringtone", null);
|
|
|
|
final boolean vibrate = preferences.getBoolean("vibrate_on_notification", true);
|
2014-09-28 13:21:56 +00:00
|
|
|
|
|
|
|
if (notifications.size() == 0) {
|
2014-10-21 12:57:16 +00:00
|
|
|
notificationManager.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;
|
2014-09-28 13:21:56 +00:00
|
|
|
if (notifications.size() == 1) {
|
2014-10-24 16:27:53 +00:00
|
|
|
mBuilder = buildSingleConversations(notify);
|
2014-09-28 13:21:56 +00:00
|
|
|
} else {
|
2014-10-24 16:27:53 +00:00
|
|
|
mBuilder = buildMultipleConversation();
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
2014-12-15 16:20:22 +00:00
|
|
|
if (notify && !isQuietHours()) {
|
2014-09-28 13:21:56 +00:00
|
|
|
if (vibrate) {
|
2014-12-15 16:20:22 +00:00
|
|
|
final int dat = 70;
|
|
|
|
final long[] pattern = {0, 3 * dat, dat, dat};
|
2014-09-28 13:21:56 +00:00
|
|
|
mBuilder.setVibrate(pattern);
|
|
|
|
}
|
|
|
|
if (ringtone != null) {
|
|
|
|
mBuilder.setSound(Uri.parse(ringtone));
|
|
|
|
}
|
|
|
|
}
|
2014-10-24 16:27:53 +00:00
|
|
|
mBuilder.setSmallIcon(R.drawable.ic_notification);
|
2014-10-02 15:36:02 +00:00
|
|
|
mBuilder.setDeleteIntent(createDeleteIntent());
|
2014-10-17 09:01:38 +00:00
|
|
|
mBuilder.setLights(0xffffffff, 2000, 4000);
|
2014-12-15 16:20:22 +00:00
|
|
|
final Notification notification = mBuilder.build();
|
2014-10-21 12:57:16 +00:00
|
|
|
notificationManager.notify(NOTIFICATION_ID, notification);
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
|
|
|
|
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;
|
|
|
|
for (ArrayList<Message> messages : notifications.values()) {
|
|
|
|
if (messages.size() > 0) {
|
|
|
|
conversation = messages.get(0).getConversation();
|
|
|
|
String name = conversation.getName();
|
|
|
|
style.addLine(Html.fromHtml("<b>" + name + "</b> "
|
2015-01-12 15:09:39 +00:00
|
|
|
+ UIHelper.getMessagePreview(mXmppConnectionService,messages.get(0)).first));
|
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) {
|
|
|
|
mBuilder.setContentIntent(createContentIntent(conversation
|
2014-12-14 07:02:17 +00:00
|
|
|
.getUuid()));
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
|
|
|
return mBuilder;
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private Builder buildSingleConversations(final boolean notify) {
|
|
|
|
final Builder mBuilder = new NotificationCompat.Builder(
|
2014-10-24 16:27:53 +00:00
|
|
|
mXmppConnectionService);
|
2014-12-15 16:20:22 +00:00
|
|
|
final ArrayList<Message> messages = notifications.values().iterator().next();
|
2014-10-24 16:27:53 +00:00
|
|
|
if (messages.size() >= 1) {
|
2014-12-15 16:20:22 +00:00
|
|
|
final Conversation conversation = messages.get(0).getConversation();
|
2014-10-24 16:27:53 +00:00
|
|
|
mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService()
|
|
|
|
.get(conversation, getPixel(64)));
|
|
|
|
mBuilder.setContentTitle(conversation.getName());
|
2014-12-15 16:20:22 +00:00
|
|
|
final Message message;
|
2014-10-24 16:27:53 +00:00
|
|
|
if ((message = getImage(messages)) != null) {
|
|
|
|
modifyForImage(mBuilder, message, messages, notify);
|
|
|
|
} else {
|
|
|
|
modifyForTextOnly(mBuilder, messages, notify);
|
|
|
|
}
|
|
|
|
mBuilder.setContentIntent(createContentIntent(conversation
|
2014-12-14 07:02:17 +00:00
|
|
|
.getUuid()));
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
|
|
|
return mBuilder;
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private void modifyForImage(final Builder builder, final Message message,
|
|
|
|
final ArrayList<Message> messages, final boolean notify) {
|
2014-10-24 16:27:53 +00:00
|
|
|
try {
|
2014-12-15 16:20:22 +00:00
|
|
|
final Bitmap bitmap = mXmppConnectionService.getFileBackend()
|
2014-12-14 07:02:17 +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
|
|
|
|
&& msg.getDownloadable() == null) {
|
|
|
|
tmp.add(msg);
|
2014-12-14 07:02:17 +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) {
|
|
|
|
bigPictureStyle.setSummaryText(getMergedBodies(tmp));
|
2015-01-12 15:09:39 +00:00
|
|
|
builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService,tmp.get(0)).first);
|
2014-10-24 16:27:53 +00:00
|
|
|
} else {
|
|
|
|
builder.setContentText(mXmppConnectionService.getString(R.string.image_file));
|
|
|
|
}
|
|
|
|
builder.setStyle(bigPictureStyle);
|
2014-12-15 16:20:22 +00:00
|
|
|
} catch (final FileNotFoundException e) {
|
2014-10-24 16:27:53 +00:00
|
|
|
modifyForTextOnly(builder, messages, notify);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private void modifyForTextOnly(final Builder builder,
|
|
|
|
final ArrayList<Message> messages, final boolean notify) {
|
2015-01-12 15:09:39 +00:00
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getMergedBodies(messages)));
|
|
|
|
builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService,messages.get(0)).first);
|
2014-10-24 16:27:53 +00:00
|
|
|
if (notify) {
|
2015-01-12 15:09:39 +00:00
|
|
|
builder.setTicker(UIHelper.getMessagePreview(mXmppConnectionService,messages.get(messages.size() - 1)).first);
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private Message getImage(final ArrayList<Message> messages) {
|
|
|
|
for (final Message message : messages) {
|
2014-10-24 16:27:53 +00:00
|
|
|
if (message.getType() == Message.TYPE_IMAGE
|
|
|
|
&& message.getDownloadable() == null
|
|
|
|
&& message.getEncryption() != Message.ENCRYPTION_PGP) {
|
|
|
|
return message;
|
2014-12-14 07:02:17 +00:00
|
|
|
}
|
2014-10-24 16:27:53 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private String getMergedBodies(final ArrayList<Message> messages) {
|
|
|
|
final StringBuilder text = new StringBuilder();
|
2014-10-24 16:27:53 +00:00
|
|
|
for (int i = 0; i < messages.size(); ++i) {
|
2015-01-12 15:09:39 +00:00
|
|
|
text.append(UIHelper.getMessagePreview(mXmppConnectionService,messages.get(i)).first);
|
2014-10-24 16:27:53 +00:00
|
|
|
if (i != messages.size() - 1) {
|
|
|
|
text.append("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text.toString();
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private PendingIntent createContentIntent(final String conversationUuid) {
|
|
|
|
final TaskStackBuilder stackBuilder = TaskStackBuilder
|
2014-12-14 07:02:17 +00:00
|
|
|
.create(mXmppConnectionService);
|
2014-09-28 13:21:56 +00:00
|
|
|
stackBuilder.addParentStack(ConversationActivity.class);
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
final Intent viewConversationIntent = new Intent(mXmppConnectionService,
|
2014-09-28 13:21:56 +00:00
|
|
|
ConversationActivity.class);
|
|
|
|
viewConversationIntent.setAction(Intent.ACTION_VIEW);
|
2014-12-15 16:20:22 +00:00
|
|
|
if (conversationUuid != null) {
|
2014-11-12 13:41:43 +00:00
|
|
|
viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
|
|
|
|
conversationUuid);
|
|
|
|
viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
|
|
|
|
}
|
2014-09-28 13:21:56 +00:00
|
|
|
|
|
|
|
stackBuilder.addNextIntent(viewConversationIntent);
|
|
|
|
|
2014-12-14 07:02:17 +00:00
|
|
|
return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
2014-10-02 16:31:19 +00:00
|
|
|
|
2014-10-02 15:36:02 +00:00
|
|
|
private PendingIntent createDeleteIntent() {
|
2014-12-15 16:20:22 +00:00
|
|
|
final Intent intent = new Intent(mXmppConnectionService,
|
2014-10-02 16:31:19 +00:00
|
|
|
XmppConnectionService.class);
|
2014-11-12 13:41:43 +00:00
|
|
|
intent.setAction(XmppConnectionService.ACTION_CLEAR_NOTIFICATION);
|
|
|
|
return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private PendingIntent createDisableForeground() {
|
2014-12-15 16:20:22 +00:00
|
|
|
final Intent intent = new Intent(mXmppConnectionService,
|
2014-11-12 13:41:43 +00:00
|
|
|
XmppConnectionService.class);
|
|
|
|
intent.setAction(XmppConnectionService.ACTION_DISABLE_FOREGROUND);
|
2014-10-02 15:36:02 +00:00
|
|
|
return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
|
|
|
|
}
|
2014-09-28 14:33:25 +00:00
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private boolean wasHighlightedOrPrivate(final Message message) {
|
|
|
|
final String nick = message.getConversation().getMucOptions().getActualNick();
|
|
|
|
final Pattern highlight = generateNickHighlightPattern(nick);
|
2014-10-03 09:55:38 +00:00
|
|
|
if (message.getBody() == null || nick == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-15 16:20:22 +00:00
|
|
|
final Matcher m = highlight.matcher(message.getBody());
|
2014-09-28 14:33:25 +00:00
|
|
|
return (m.find() || message.getType() == Message.TYPE_PRIVATE);
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
private static Pattern generateNickHighlightPattern(final String nick) {
|
2014-09-28 14:33:25 +00:00
|
|
|
// We expect a word boundary, i.e. space or start of string, followed by
|
|
|
|
// the
|
|
|
|
// nick (matched in case-insensitive manner), followed by optional
|
|
|
|
// punctuation (for example "bob: i disagree" or "how are you alice?"),
|
|
|
|
// followed by another word boundary.
|
|
|
|
return Pattern.compile("\\b" + nick + "\\p{Punct}?\\b",
|
|
|
|
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-01-02 11:04:33 +00:00
|
|
|
if (foreground != this.mIsInForeground) {
|
|
|
|
Log.d(Config.LOGTAG,"setIsInForeground("+Boolean.toString(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()
|
2014-12-14 07:02:17 +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
|
2014-12-14 07:02:17 +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);
|
2014-11-12 13:41:43 +00:00
|
|
|
mBuilder.setSmallIcon(R.drawable.ic_stat_communication_import_export);
|
|
|
|
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service));
|
2015-01-15 15:17:55 +00:00
|
|
|
mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_open_conversations));
|
|
|
|
mBuilder.addAction(R.drawable.ic_action_cancel,
|
|
|
|
mXmppConnectionService.getString(R.string.disable_foreground_service),
|
|
|
|
createDisableForeground());
|
|
|
|
mBuilder.setContentIntent(createOpenConversationsIntent());
|
2014-11-12 16:33:24 +00:00
|
|
|
mBuilder.setWhen(0);
|
|
|
|
mBuilder.setPriority(NotificationCompat.PRIORITY_MIN);
|
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() {
|
|
|
|
return PendingIntent.getActivity(mXmppConnectionService, 0, new Intent(mXmppConnectionService,ConversationActivity.class),0);
|
|
|
|
}
|
|
|
|
|
2014-11-18 14:26:28 +00:00
|
|
|
public void updateErrorNotification() {
|
2014-12-15 16:20:22 +00:00
|
|
|
final NotificationManager mNotificationManager = (NotificationManager) mXmppConnectionService.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
final List<Account> errors = new ArrayList<>();
|
|
|
|
for (final Account account : mXmppConnectionService.getAccounts()) {
|
2014-11-18 14:26:28 +00:00
|
|
|
if (account.hasErrorStatus()) {
|
|
|
|
errors.add(account);
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
mNotificationManager.cancel(ERROR_NOTIFICATION_ID);
|
|
|
|
return;
|
|
|
|
} else if (errors.size() == 1) {
|
|
|
|
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_account));
|
|
|
|
mBuilder.setContentText(errors.get(0).getJid().toBareJid().toString());
|
|
|
|
} else {
|
|
|
|
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_accounts));
|
|
|
|
mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_fix));
|
|
|
|
}
|
|
|
|
mBuilder.setOngoing(true);
|
2015-01-15 14:45:11 +00:00
|
|
|
//mBuilder.setLights(0xffffffff, 2000, 4000);
|
2014-11-18 14:26:28 +00:00
|
|
|
mBuilder.setSmallIcon(R.drawable.ic_stat_alert_warning);
|
|
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mXmppConnectionService);
|
|
|
|
stackBuilder.addParentStack(ConversationActivity.class);
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
final Intent manageAccountsIntent = new Intent(mXmppConnectionService,ManageAccountActivity.class);
|
2014-11-18 14:26:28 +00:00
|
|
|
stackBuilder.addNextIntent(manageAccountsIntent);
|
|
|
|
|
2014-12-15 16:20:22 +00:00
|
|
|
final PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
|
2014-11-18 14:26:28 +00:00
|
|
|
|
|
|
|
mBuilder.setContentIntent(resultPendingIntent);
|
2014-12-15 16:20:22 +00:00
|
|
|
mNotificationManager.notify(ERROR_NOTIFICATION_ID, mBuilder.build());
|
2014-11-18 14:26:28 +00:00
|
|
|
}
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|