aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java
diff options
context:
space:
mode:
authorlookshe <github@lookshe.org>2016-03-06 19:42:55 +0100
committerlookshe <github@lookshe.org>2016-03-06 19:42:55 +0100
commit3c400703e082a1b180b35d891b8fb3460c7d5b87 (patch)
tree28738dd90fc41b4ab71897f38d324828778ad2e3 /src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java
parent72114d732427266024cdd6e27cd8d1aa60afae2f (diff)
parentf28d77dc42f6bac5a026e0b1c78562dee8de45ac (diff)
Merge branch 'trz/rebase' into trz/rename
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java234
1 files changed, 131 insertions, 103 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java b/src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java
index b02b4d82..92b7cfa1 100644
--- a/src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java
+++ b/src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java
@@ -1,6 +1,5 @@
package de.thedevstack.conversationsplus.services;
-import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
@@ -9,7 +8,6 @@ import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
-import android.os.PowerManager;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.BigPictureStyle;
@@ -27,12 +25,11 @@ import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.ConversationsPlusPreferences;
import de.thedevstack.conversationsplus.utils.ImageUtil;
+import de.thedevstack.conversationsplus.utils.MessageUtil;
import de.tzur.conversations.Settings;
import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.R;
@@ -67,9 +64,7 @@ public class NotificationService {
&& !message.isRead()
&& ConversationsPlusPreferences.showNotification()
&& !message.getConversation().isMuted()
- && (message.getConversation().getMode() == Conversation.MODE_SINGLE
- || ConversationsPlusPreferences.alwaysNotifyInConference()
- || wasHighlightedOrPrivate(message)
+ && (message.getConversation().alwaysNotify() || wasHighlightedOrPrivate(message)
);
}
@@ -109,47 +104,47 @@ public class NotificationService {
}
}
- @SuppressLint("NewApi")
- @SuppressWarnings("deprecation")
- private boolean isInteractive() {
- final PowerManager pm = (PowerManager) mXmppConnectionService
- .getSystemService(Context.POWER_SERVICE);
+ public void pushFromBacklog(final Message message) {
+ if (notify(message)) {
+ synchronized (notifications) {
+ pushToStack(message);
+ }
+ }
+ }
- final boolean isScreenOn;
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
- isScreenOn = pm.isScreenOn();
- } else {
- isScreenOn = pm.isInteractive();
+ public void finishBacklog(boolean notify) {
+ synchronized (notifications) {
+ mXmppConnectionService.updateUnreadCountBadge();
+ updateNotification(notify);
}
+ }
- return isScreenOn;
+ 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);
+ }
}
public void push(final Message message) {
if (!notify(message)) {
return;
}
- mXmppConnectionService.updateUnreadCountBadge();
-
- final boolean isScreenOn = isInteractive();
-
+ mXmppConnectionService.updateUnreadCountBadge();
+ final boolean isScreenOn = mXmppConnectionService.isInteractive();
if (this.mIsInForeground && isScreenOn && this.mOpenConversation == message.getConversation()) {
return;
}
-
synchronized (notifications) {
- 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);
- }
+ pushToStack(message);
final Account account = message.getConversation().getAccount();
final boolean doNotify = (!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
- && !account.inGracePeriod()
- && !this.inMiniGracePeriod(account);
+ && !account.inGracePeriod()
+ && !this.inMiniGracePeriod(account);
updateNotification(doNotify);
if (doNotify) {
notifyPebble(message);
@@ -172,10 +167,10 @@ public class NotificationService {
}
private void setNotificationColor(final Builder mBuilder) {
- mBuilder.setColor(mXmppConnectionService.getResources().getColor(R.color.notification));
+ mBuilder.setColor(mXmppConnectionService.getResources().getColor(R.color.primary));
}
- private void updateNotification(final boolean notify) {
+ public void updateNotification(final boolean notify) {
final NotificationManager notificationManager = (NotificationManager) ConversationsPlusApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
final String ringtone = ConversationsPlusPreferences.notificationRingtone();
@@ -230,8 +225,13 @@ public class NotificationService {
if (messages.size() > 0) {
conversation = messages.get(0).getConversation();
final String name = conversation.getName();
- style.addLine(Html.fromHtml("<b>" + name + "</b> "
- + UIHelper.getMessagePreview(mXmppConnectionService,messages.get(0)).first));
+ if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
+ int count = messages.size();
+ style.addLine(Html.fromHtml("<b>"+name+"</b>: "+mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages,count,count)));
+ } else {
+ style.addLine(Html.fromHtml("<b>" + name + "</b>: "
+ + UIHelper.getMessagePreview(mXmppConnectionService, messages.get(0)).first));
+ }
names.append(name);
names.append(", ");
}
@@ -259,25 +259,32 @@ public class NotificationService {
final Conversation conversation = messages.get(0).getConversation();
mBuilder.setLargeIcon(AvatarService.getInstance().get(conversation, getPixel(64)));
mBuilder.setContentTitle(conversation.getName());
- Message message;
- if ((message = getImage(messages)) != null) {
- modifyForImage(mBuilder, message, messages, notify);
+ if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
+ int count = messages.size();
+ mBuilder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages,count,count));
} else {
- modifyForTextOnly(mBuilder, messages, notify);
- }
- if ((message = getFirstDownloadableMessage(messages)) != null) {
- mBuilder.addAction(
- Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ?
- R.drawable.ic_file_download_white_24dp : R.drawable.ic_action_download,
- mXmppConnectionService.getResources().getString(R.string.download_x_file,
- UIHelper.getFileDescriptionString(mXmppConnectionService, message)),
- createDownloadIntent(message)
- );
- }
- if ((message = getFirstLocationMessage(messages)) != null) {
- mBuilder.addAction(R.drawable.ic_room_white_24dp,
- mXmppConnectionService.getString(R.string.show_location),
- createShowLocationIntent(message));
+ Message message;
+ if ((message = getImage(messages)) != null) {
+ modifyForImage(mBuilder, message, messages, notify);
+ } else if (conversation.getMode() == Conversation.MODE_MULTI) {
+ modifyForConference(mBuilder, conversation, messages, notify);
+ } else {
+ modifyForTextOnly(mBuilder, messages, notify);
+ }
+ if ((message = getFirstDownloadableMessage(messages)) != null) {
+ mBuilder.addAction(
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ?
+ R.drawable.ic_file_download_white_24dp : R.drawable.ic_action_download,
+ mXmppConnectionService.getResources().getString(R.string.download_x_file,
+ UIHelper.getFileDescriptionString(mXmppConnectionService, message)),
+ createDownloadIntent(message)
+ );
+ }
+ if ((message = getFirstLocationMessage(messages)) != null) {
+ mBuilder.addAction(R.drawable.ic_room_white_24dp,
+ mXmppConnectionService.getString(R.string.show_location),
+ createShowLocationIntent(message));
+ }
}
mBuilder.setContentIntent(createContentIntent(conversation));
}
@@ -285,7 +292,7 @@ public class NotificationService {
}
private void modifyForImage(final Builder builder, final Message message,
- final ArrayList<Message> messages, final boolean notify) {
+ final ArrayList<Message> messages, final boolean notify) {
try {
final Bitmap bitmap = ImageUtil.getThumbnail(message, getPixel(288), false);
final ArrayList<Message> tmp = new ArrayList<>();
@@ -293,17 +300,17 @@ public class NotificationService {
if (msg.getType() == Message.TYPE_TEXT
&& msg.getTransferable() == null) {
tmp.add(msg);
- }
+ }
}
final BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.bigPicture(bitmap);
if (tmp.size() > 0) {
bigPictureStyle.setSummaryText(getMergedBodies(tmp));
- builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService,tmp.get(0)).first);
+ builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService, tmp.get(0)).first);
} else {
builder.setContentText(mXmppConnectionService.getString(
R.string.received_x_file,
- UIHelper.getFileDescriptionString(mXmppConnectionService,message)));
+ UIHelper.getFileDescriptionString(mXmppConnectionService, message)));
}
builder.setStyle(bigPictureStyle);
} catch (final FileNotFoundException e) {
@@ -312,19 +319,40 @@ public class NotificationService {
}
private void modifyForTextOnly(final Builder builder,
- final ArrayList<Message> messages, final boolean notify) {
+ final ArrayList<Message> messages, final boolean notify) {
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getMergedBodies(messages)));
- builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService,messages.get(0)).first);
+ builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService, messages.get(0)).first);
if (notify) {
- builder.setTicker(UIHelper.getMessagePreview(mXmppConnectionService,messages.get(messages.size() - 1)).first);
+ builder.setTicker(UIHelper.getMessagePreview(mXmppConnectionService, messages.get(messages.size() - 1)).first);
+ }
+ }
+
+ private void modifyForConference(Builder builder, Conversation conversation, List<Message> messages, boolean notify) {
+ final Message first = messages.get(0);
+ final Message last = messages.get(messages.size() - 1);
+ final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
+ style.setBigContentTitle(conversation.getName());
+
+ for(Message message : messages) {
+ if (message.hasMeCommand()) {
+ style.addLine(UIHelper.getMessagePreview(mXmppConnectionService,message).first);
+ } else {
+ style.addLine(Html.fromHtml("<b>" + UIHelper.getMessageDisplayName(message) + "</b>: " + UIHelper.getMessagePreview(mXmppConnectionService, message).first));
+ }
+ }
+ builder.setContentText((first.hasMeCommand() ? "" :UIHelper.getMessageDisplayName(first)+ ": ") +UIHelper.getMessagePreview(mXmppConnectionService, first).first);
+ builder.setStyle(style);
+ if (notify) {
+ builder.setTicker((last.hasMeCommand() ? "" : UIHelper.getMessageDisplayName(last) + ": ") + UIHelper.getMessagePreview(mXmppConnectionService,last).first);
}
}
private Message getImage(final Iterable<Message> messages) {
for (final Message message : messages) {
- if (message.getType() == Message.TYPE_IMAGE
+ if (message.getType() != Message.TYPE_TEXT
&& message.getTransferable() == null
- && message.getEncryption() != Message.ENCRYPTION_PGP) {
+ && message.getEncryption() != Message.ENCRYPTION_PGP
+ && message.getFileParams().height > 0) {
return message;
}
}
@@ -342,7 +370,7 @@ public class NotificationService {
}
private Message getFirstLocationMessage(final Iterable<Message> messages) {
- for(final Message message : messages) {
+ for (final Message message : messages) {
if (GeoHelper.isGeoUri(message.getBody())) {
return message;
}
@@ -353,7 +381,7 @@ public class NotificationService {
private CharSequence getMergedBodies(final ArrayList<Message> messages) {
final StringBuilder text = new StringBuilder();
for (int i = 0; i < messages.size(); ++i) {
- text.append(UIHelper.getMessagePreview(mXmppConnectionService,messages.get(i)).first);
+ text.append(UIHelper.getMessagePreview(mXmppConnectionService, messages.get(i)).first);
if (i != messages.size() - 1) {
text.append("\n");
}
@@ -363,9 +391,9 @@ public class NotificationService {
private PendingIntent createShowLocationIntent(final Message message) {
Iterable<Intent> intents = GeoHelper.createGeoIntentsFromMessage(message);
- for(Intent intent : intents) {
+ for (Intent intent : intents) {
if (intent.resolveActivity(mXmppConnectionService.getPackageManager()) != null) {
- return PendingIntent.getActivity(mXmppConnectionService,18,intent,PendingIntent.FLAG_UPDATE_CURRENT);
+ return PendingIntent.getActivity(mXmppConnectionService, 18, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
return createOpenConversationsIntent();
@@ -373,7 +401,7 @@ public class NotificationService {
private PendingIntent createContentIntent(final String conversationUuid, final String downloadMessageUuid) {
final TaskStackBuilder stackBuilder = TaskStackBuilder
- .create(mXmppConnectionService);
+ .create(mXmppConnectionService);
stackBuilder.addParentStack(ConversationActivity.class);
final Intent viewConversationIntent = new Intent(mXmppConnectionService,
@@ -425,30 +453,14 @@ public class NotificationService {
}
private PendingIntent createDisableAccountIntent(final Account account) {
- final Intent intent = new Intent(mXmppConnectionService,XmppConnectionService.class);
+ final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_DISABLE_ACCOUNT);
- intent.putExtra("account",account.getJid().toBareJid().toString());
- return PendingIntent.getService(mXmppConnectionService,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
- }
-
- public boolean wasHighlightedOrPrivate(final Message message) {
- final String nick = message.getConversation().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);
+ intent.putExtra("account", account.getJid().toBareJid().toString());
+ return PendingIntent.getService(mXmppConnectionService, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
- private static Pattern generateNickHighlightPattern(final String nick) {
- // 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" + Pattern.quote(nick) + "\\p{Punct}?\\b",
- Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
+ private boolean wasHighlightedOrPrivate(final Message message) {
+ return MessageUtil.wasHighlightedOrPrivate(message);
}
public void setOpenConversation(final Conversation conversation) {
@@ -461,7 +473,7 @@ public class NotificationService {
private int getPixel(final int dp) {
final DisplayMetrics metrics = mXmppConnectionService.getResources()
- .getDisplayMetrics();
+ .getDisplayMetrics();
return ((int) (dp * metrics.density));
}
@@ -471,7 +483,7 @@ public class NotificationService {
private boolean inMiniGracePeriod(final Account account) {
final int miniGrace = account.getStatus() == Account.State.ONLINE ? Config.MINI_GRACE_PERIOD
- : Config.MINI_GRACE_PERIOD * 2;
+ : Config.MINI_GRACE_PERIOD * 2;
return SystemClock.elapsedRealtime() < (this.mLastNotification + miniGrace);
}
@@ -479,41 +491,57 @@ public class NotificationService {
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service));
- mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_open_conversations));
+ 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++;
+ } else if (!account.isOptionSet(Account.OPTION_DISABLED)) {
+ enabled++;
+ }
+ }
+ mBuilder.setContentText(mXmppConnectionService.getString(R.string.connected_accounts, connected, enabled));
+ } else {
+ mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_open_conversations));
+ }
mBuilder.setContentIntent(createOpenConversationsIntent());
mBuilder.setWhen(0);
- mBuilder.setPriority(NotificationCompat.PRIORITY_MIN);
+ mBuilder.setPriority(Config.SHOW_CONNECTED_ACCOUNTS ? NotificationCompat.PRIORITY_DEFAULT : NotificationCompat.PRIORITY_MIN);
final int cancelIcon;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setCategory(Notification.CATEGORY_SERVICE);
- mBuilder.setSmallIcon(R.drawable.ic_import_export_white_24dp);
cancelIcon = R.drawable.ic_cancel_white_24dp;
} else {
- mBuilder.setSmallIcon(R.drawable.ic_stat_communication_import_export);
cancelIcon = R.drawable.ic_action_cancel;
}
+ mBuilder.setSmallIcon(R.drawable.ic_link_white_24dp);
mBuilder.addAction(cancelIcon,
mXmppConnectionService.getString(R.string.disable_foreground_service),
createDisableForeground());
- setNotificationColor(mBuilder);
return mBuilder.build();
}
private PendingIntent createOpenConversationsIntent() {
- return PendingIntent.getActivity(mXmppConnectionService, 0, new Intent(mXmppConnectionService,ConversationActivity.class),0);
+ return PendingIntent.getActivity(mXmppConnectionService, 0, new Intent(mXmppConnectionService, ConversationActivity.class), 0);
}
public void updateErrorNotification() {
- final NotificationManager mNotificationManager = (NotificationManager) mXmppConnectionService.getSystemService(Context.NOTIFICATION_SERVICE);
+ final NotificationManager notificationManager = (NotificationManager) mXmppConnectionService.getSystemService(Context.NOTIFICATION_SERVICE);
final List<Account> errors = new ArrayList<>();
for (final Account account : mXmppConnectionService.getAccounts()) {
if (account.hasErrorStatus()) {
errors.add(account);
}
}
+ if (mXmppConnectionService.getPreferences().getBoolean("keep_foreground_service", false)) {
+ notificationManager.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
+ }
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
if (errors.size() == 0) {
- mNotificationManager.cancel(ERROR_NOTIFICATION_ID);
+ notificationManager.cancel(ERROR_NOTIFICATION_ID);
return;
} else if (errors.size() == 1) {
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_account));
@@ -540,12 +568,12 @@ public class NotificationService {
final TaskStackBuilder stackBuilder = TaskStackBuilder.create(mXmppConnectionService);
stackBuilder.addParentStack(ConversationActivity.class);
- final Intent manageAccountsIntent = new Intent(mXmppConnectionService,ManageAccountActivity.class);
+ final Intent manageAccountsIntent = new Intent(mXmppConnectionService, ManageAccountActivity.class);
stackBuilder.addNextIntent(manageAccountsIntent);
- final PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
+ final PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
- mNotificationManager.notify(ERROR_NOTIFICATION_ID, mBuilder.build());
+ notificationManager.notify(ERROR_NOTIFICATION_ID, mBuilder.build());
}
}