more improvments for new notification service
This commit is contained in:
parent
bff23c2e23
commit
87010e6094
|
@ -54,7 +54,7 @@
|
|||
android:text="@string/download_image"
|
||||
android:visibility="gone" />
|
||||
|
||||
<LinearLayout
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
|
@ -65,12 +65,10 @@
|
|||
android:layout_width="?attr/TextSizeInfo"
|
||||
android:layout_height="?attr/TextSizeInfo"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:gravity="center_vertical"
|
||||
android:src="@drawable/ic_secure_indicator"
|
||||
android:layout_marginRight="4sp"
|
||||
android:alpha="0.54"/>
|
||||
|
||||
|
||||
android:alpha="0.54"
|
||||
android:gravity="center_vertical"
|
||||
android:src="@drawable/ic_secure_indicator" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/message_time"
|
||||
|
|
|
@ -63,27 +63,25 @@
|
|||
android:textColor="@color/secondarytext"
|
||||
android:textSize="?attr/TextSizeInfo" />
|
||||
|
||||
<ImageView
|
||||
<ImageView
|
||||
android:id="@+id/security_indicator"
|
||||
android:layout_width="?attr/TextSizeInfo"
|
||||
android:layout_height="?attr/TextSizeInfo"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:gravity="center_vertical"
|
||||
android:src="@drawable/ic_secure_indicator"
|
||||
android:layout_marginLeft="4sp"
|
||||
android:alpha="0.54"/>
|
||||
android:alpha="0.54"
|
||||
android:gravity="center_vertical"
|
||||
android:src="@drawable/ic_secure_indicator" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/indicator_received"
|
||||
android:layout_width="?attr/TextSizeInfo"
|
||||
android:layout_height="?attr/TextSizeInfo"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:gravity="center_vertical"
|
||||
android:src="@drawable/ic_received_indicator"
|
||||
android:layout_marginLeft="4sp"
|
||||
android:alpha="0.54"/>
|
||||
|
||||
|
||||
android:alpha="0.54"
|
||||
android:gravity="center_vertical"
|
||||
android:src="@drawable/ic_received_indicator" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
|
|
@ -26,8 +26,9 @@
|
|||
<item>2 ordu</item>
|
||||
<item>8 ordu</item>
|
||||
<item>abisatu arte</item>
|
||||
</string-array>
|
||||
<integer-array name="mute_options_durations">
|
||||
</string-array>
|
||||
|
||||
<integer-array name="mute_options_durations">
|
||||
<item>1800</item>
|
||||
<item>3600</item>
|
||||
<item>7200</item>
|
||||
|
|
|
@ -52,15 +52,9 @@
|
|||
|
||||
<CheckBoxPreference
|
||||
android:dependency="show_notification"
|
||||
android:key="notify_in_conversation_when_highlighted"
|
||||
android:key="always_notify_in_conference"
|
||||
android:summary="@string/pref_conference_notifications_summary"
|
||||
android:title="@string/pref_conference_notifications" />
|
||||
<CheckBoxPreference
|
||||
android:defaultValue="true"
|
||||
android:dependency="show_notification"
|
||||
android:key="notification_grace_period_after_carbon_received"
|
||||
android:summary="@string/pref_notification_grace_period_summary"
|
||||
android:title="@string/pref_notification_grace_period" />
|
||||
</PreferenceCategory>
|
||||
<PreferenceCategory android:title="@string/pref_ui_options" >
|
||||
<CheckBoxPreference
|
||||
|
|
|
@ -8,6 +8,7 @@ import eu.siacs.conversations.entities.Account;
|
|||
import eu.siacs.conversations.entities.Contact;
|
||||
import eu.siacs.conversations.entities.Conversation;
|
||||
import eu.siacs.conversations.entities.Message;
|
||||
import eu.siacs.conversations.services.NotificationService;
|
||||
import eu.siacs.conversations.services.XmppConnectionService;
|
||||
import eu.siacs.conversations.utils.CryptoHelper;
|
||||
import eu.siacs.conversations.xml.Element;
|
||||
|
@ -386,11 +387,13 @@ public class MessageParser extends AbstractParser implements
|
|||
@Override
|
||||
public void onMessagePacketReceived(Account account, MessagePacket packet) {
|
||||
Message message = null;
|
||||
boolean notify = true;
|
||||
if (mXmppConnectionService.getPreferences().getBoolean(
|
||||
"notification_grace_period_after_carbon_received", true)) {
|
||||
notify = (SystemClock.elapsedRealtime() - lastCarbonMessageReceived) > (Config.CARBON_GRACE_PERIOD * 1000);
|
||||
}
|
||||
boolean notify = mXmppConnectionService.getPreferences().getBoolean(
|
||||
"show_notification", true);
|
||||
notify = notify
|
||||
&& (SystemClock.elapsedRealtime() - lastCarbonMessageReceived) > (Config.CARBON_GRACE_PERIOD * 1000);
|
||||
boolean alwaysNotifyInConference = notify
|
||||
&& mXmppConnectionService.getPreferences().getBoolean(
|
||||
"always_notify_in_conference", false);
|
||||
|
||||
this.parseNick(packet, account);
|
||||
|
||||
|
@ -427,8 +430,11 @@ public class MessageParser extends AbstractParser implements
|
|||
if (message != null) {
|
||||
if (message.getStatus() == Message.STATUS_RECEIVED) {
|
||||
message.markUnread();
|
||||
notify = alwaysNotifyInConference
|
||||
|| NotificationService
|
||||
.wasHighlightedOrPrivate(message);
|
||||
} else {
|
||||
message.getConversation().markRead();
|
||||
mXmppConnectionService.markRead(message.getConversation());
|
||||
lastCarbonMessageReceived = SystemClock.elapsedRealtime();
|
||||
notify = false;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,9 @@ package eu.siacs.conversations.services;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
|
@ -162,4 +165,22 @@ public class NotificationService {
|
|||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
return resultPendingIntent;
|
||||
}
|
||||
|
||||
public static boolean wasHighlightedOrPrivate(Message message) {
|
||||
String nick = message.getConversation().getMucOptions().getActualNick();
|
||||
Pattern highlight = generateNickHighlightPattern(nick);
|
||||
Matcher m = highlight.matcher(message.getBody());
|
||||
return (m.find() || message.getType() == Message.TYPE_PRIVATE);
|
||||
}
|
||||
|
||||
private static Pattern generateNickHighlightPattern(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" + nick + "\\p{Punct}?\\b",
|
||||
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,13 +7,11 @@ import java.util.Date;
|
|||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import eu.siacs.conversations.R;
|
||||
import eu.siacs.conversations.entities.Account;
|
||||
import eu.siacs.conversations.entities.Contact;
|
||||
import eu.siacs.conversations.entities.Conversation;
|
||||
import eu.siacs.conversations.entities.Message;
|
||||
import eu.siacs.conversations.entities.MucOptions.User;
|
||||
import eu.siacs.conversations.ui.ConversationActivity;
|
||||
import eu.siacs.conversations.ui.ManageAccountActivity;
|
||||
|
@ -27,7 +25,6 @@ import android.content.Context;
|
|||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
|
@ -35,13 +32,11 @@ import android.graphics.Paint;
|
|||
import android.graphics.Rect;
|
||||
import android.graphics.Typeface;
|
||||
import android.net.Uri;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.provider.ContactsContract.Contacts;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.support.v4.app.TaskStackBuilder;
|
||||
import android.text.format.DateFormat;
|
||||
import android.text.format.DateUtils;
|
||||
import android.text.Html;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
|
@ -331,177 +326,6 @@ public class UIHelper {
|
|||
mNotificationManager.notify(1111, notification);
|
||||
}
|
||||
|
||||
private static Pattern generateNickHighlightPattern(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" + nick + "\\p{Punct}?\\b",
|
||||
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
|
||||
}
|
||||
|
||||
private static void updateNotification(Context context,
|
||||
List<Conversation> conversations, Conversation currentCon,
|
||||
boolean notify) {
|
||||
NotificationManager mNotificationManager = (NotificationManager) context
|
||||
.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
||||
SharedPreferences preferences = PreferenceManager
|
||||
.getDefaultSharedPreferences(context);
|
||||
boolean showNofifications = preferences.getBoolean("show_notification",
|
||||
true);
|
||||
boolean vibrate = preferences.getBoolean("vibrate_on_notification",
|
||||
true);
|
||||
boolean alwaysNotify = preferences.getBoolean(
|
||||
"notify_in_conversation_when_highlighted", false);
|
||||
|
||||
if (!showNofifications) {
|
||||
mNotificationManager.cancel(2342);
|
||||
return;
|
||||
}
|
||||
|
||||
String targetUuid = "";
|
||||
|
||||
if ((currentCon != null)
|
||||
&& (currentCon.getMode() == Conversation.MODE_MULTI)
|
||||
&& (!alwaysNotify) && notify) {
|
||||
String nick = currentCon.getMucOptions().getActualNick();
|
||||
Pattern highlight = generateNickHighlightPattern(nick);
|
||||
Matcher m = highlight.matcher(currentCon.getLatestMessage()
|
||||
.getBody());
|
||||
notify = m.find()
|
||||
|| (currentCon.getLatestMessage().getType() == Message.TYPE_PRIVATE);
|
||||
}
|
||||
|
||||
List<Conversation> unread = new ArrayList<Conversation>();
|
||||
for (Conversation conversation : conversations) {
|
||||
if (conversation.getMode() == Conversation.MODE_MULTI) {
|
||||
if ((!conversation.isRead())
|
||||
&& ((wasHighlightedOrPrivate(conversation) || (alwaysNotify)))) {
|
||||
unread.add(conversation);
|
||||
}
|
||||
} else {
|
||||
if (!conversation.isRead()) {
|
||||
unread.add(conversation);
|
||||
}
|
||||
}
|
||||
}
|
||||
String ringtone = preferences.getString("notification_ringtone", null);
|
||||
|
||||
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
|
||||
context);
|
||||
if (unread.size() == 0) {
|
||||
mNotificationManager.cancel(2342);
|
||||
return;
|
||||
} else if (unread.size() == 1) {
|
||||
Conversation conversation = unread.get(0);
|
||||
targetUuid = conversation.getUuid();
|
||||
mBuilder.setLargeIcon(conversation.getImage(context, 64));
|
||||
mBuilder.setContentTitle(conversation.getName());
|
||||
if (notify) {
|
||||
mBuilder.setTicker(conversation.getLatestMessage()
|
||||
.getReadableBody(context));
|
||||
}
|
||||
StringBuilder bigText = new StringBuilder();
|
||||
List<Message> messages = conversation.getMessages();
|
||||
String firstLine = "";
|
||||
for (int i = messages.size() - 1; i >= 0; --i) {
|
||||
if (!messages.get(i).isRead()) {
|
||||
if (i == messages.size() - 1) {
|
||||
firstLine = messages.get(i).getReadableBody(context);
|
||||
bigText.append(firstLine);
|
||||
} else {
|
||||
firstLine = messages.get(i).getReadableBody(context);
|
||||
bigText.insert(0, firstLine + "\n");
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
mBuilder.setContentText(firstLine);
|
||||
mBuilder.setStyle(new NotificationCompat.BigTextStyle()
|
||||
.bigText(bigText.toString()));
|
||||
} else {
|
||||
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
|
||||
style.setBigContentTitle(unread.size() + " "
|
||||
+ context.getString(R.string.unread_conversations));
|
||||
StringBuilder names = new StringBuilder();
|
||||
for (int i = 0; i < unread.size(); ++i) {
|
||||
targetUuid = unread.get(i).getUuid();
|
||||
if (i < unread.size() - 1) {
|
||||
names.append(unread.get(i).getName() + ", ");
|
||||
} else {
|
||||
names.append(unread.get(i).getName());
|
||||
}
|
||||
style.addLine(Html.fromHtml("<b>"
|
||||
+ unread.get(i).getName()
|
||||
+ "</b> "
|
||||
+ unread.get(i).getLatestMessage()
|
||||
.getReadableBody(context)));
|
||||
}
|
||||
mBuilder.setContentTitle(unread.size() + " "
|
||||
+ context.getString(R.string.unread_conversations));
|
||||
mBuilder.setContentText(names.toString());
|
||||
mBuilder.setStyle(style);
|
||||
}
|
||||
if ((currentCon != null) && (notify)) {
|
||||
targetUuid = currentCon.getUuid();
|
||||
}
|
||||
if (unread.size() != 0) {
|
||||
mBuilder.setSmallIcon(R.drawable.ic_notification);
|
||||
if (notify) {
|
||||
if (vibrate) {
|
||||
int dat = 70;
|
||||
long[] pattern = { 0, 3 * dat, dat, dat };
|
||||
mBuilder.setVibrate(pattern);
|
||||
}
|
||||
mBuilder.setLights(0xffffffff, 2000, 4000);
|
||||
if (ringtone != null) {
|
||||
mBuilder.setSound(Uri.parse(ringtone));
|
||||
}
|
||||
}
|
||||
|
||||
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
|
||||
stackBuilder.addParentStack(ConversationActivity.class);
|
||||
|
||||
Intent viewConversationIntent = new Intent(context,
|
||||
ConversationActivity.class);
|
||||
viewConversationIntent.setAction(Intent.ACTION_VIEW);
|
||||
viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
|
||||
targetUuid);
|
||||
viewConversationIntent
|
||||
.setType(ConversationActivity.VIEW_CONVERSATION);
|
||||
|
||||
stackBuilder.addNextIntent(viewConversationIntent);
|
||||
|
||||
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
|
||||
0, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
mBuilder.setContentIntent(resultPendingIntent);
|
||||
Notification notification = mBuilder.build();
|
||||
mNotificationManager.notify(2342, notification);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean wasHighlightedOrPrivate(Conversation conversation) {
|
||||
List<Message> messages = conversation.getMessages();
|
||||
String nick = conversation.getMucOptions().getActualNick();
|
||||
Pattern highlight = generateNickHighlightPattern(nick);
|
||||
for (int i = messages.size() - 1; i >= 0; --i) {
|
||||
if (messages.get(i).isRead()) {
|
||||
break;
|
||||
} else {
|
||||
Matcher m = highlight.matcher(messages.get(i).getBody());
|
||||
if (m.find()
|
||||
|| messages.get(i).getType() == Message.TYPE_PRIVATE) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void prepareContactBadge(final Activity activity,
|
||||
QuickContactBadge badge, final Contact contact, Context context) {
|
||||
if (contact.getSystemAccount() != null) {
|
||||
|
|
Loading…
Reference in a new issue