last part. update notification if user reads them

This commit is contained in:
Daniel Gultsch 2014-03-08 06:25:35 +01:00
parent 83c5fdaf0e
commit aa31732ea3
4 changed files with 81 additions and 65 deletions

View File

@ -164,10 +164,7 @@ public class XmppConnectionService extends Service {
if (convChangedListener != null) { if (convChangedListener != null) {
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();
} else { } else {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); UIHelper.updateNotification(getApplicationContext(), getConversations(), notify);
mNotificationManager.notify(2342, UIHelper
.getNotification(
getApplicationContext(),getConversations(),notify));
} }
} }
}; };

View File

@ -232,6 +232,7 @@ public class ConversationActivity extends XmppActivity {
invalidateOptionsMenu(); invalidateOptionsMenu();
if (!getSelectedConversation().isRead()) { if (!getSelectedConversation().isRead()) {
getSelectedConversation().markRead(); getSelectedConversation().markRead();
UIHelper.updateNotification(getApplicationContext(), getConversationList(), false);
updateConversationList(); updateConversationList();
} }
} }
@ -400,8 +401,6 @@ public class ConversationActivity extends XmppActivity {
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancelAll();
if (conversationList.size()>=1) { if (conversationList.size()>=1) {
onConvChanged.onConversationListChanged(); onConvChanged.onConversationListChanged();
} }

View File

@ -24,6 +24,8 @@ import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Fragment; import android.app.Fragment;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentSender; import android.content.IntentSender;
@ -346,10 +348,6 @@ public class ConversationFragment extends Fragment {
} }
} }
if (!conversation.isRead()) {
conversation.markRead();
activity.updateConversationList();
}
if (queuedPqpMessage != null) { if (queuedPqpMessage != null) {
this.conversation.nextMessageEncryption = Message.ENCRYPTION_PGP; this.conversation.nextMessageEncryption = Message.ENCRYPTION_PGP;
Message message = new Message(conversation, queuedPqpMessage, Message message = new Message(conversation, queuedPqpMessage,
@ -421,6 +419,8 @@ public class ConversationFragment extends Fragment {
messagesView.setSelection(size - 1); messagesView.setSelection(size - 1);
if (!activity.shouldPaneBeOpen()) { if (!activity.shouldPaneBeOpen()) {
conversation.markRead(); conversation.markRead();
//TODO update notifications
UIHelper.updateNotification(getActivity(), activity.getConversationList(), false);
activity.updateConversationList(); activity.updateConversationList();
} }
} }

View File

@ -15,6 +15,7 @@ import eu.siacs.conversations.ui.ConversationActivity;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -101,15 +102,18 @@ public class UIHelper {
Rect rect = new Rect(); Rect rect = new Rect();
paint.getTextBounds("!", 0, 1, rect); paint.getTextBounds("!", 0, 1, rect);
float width = paint.measureText("!"); float width = paint.measureText("!");
canvas.drawText("!", (size / 2) - (width / 2), (size / 2) canvas.drawText("!", (size / 2) - (width / 2),
+ (rect.height() / 2), paint); (size / 2) + (rect.height() / 2), paint);
return bitmap; return bitmap;
} }
public static Notification getNotification(Context context, public static void updateNotification(Context context,
List<Conversation> conversations, boolean notify) { List<Conversation> conversations, boolean notify) {
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
String targetUuid = ""; String targetUuid = "";
List<Conversation> unread = new ArrayList<Conversation>(); List<Conversation> unread = new ArrayList<Conversation>();
for (Conversation conversation : conversations) { for (Conversation conversation : conversations) {
@ -125,14 +129,19 @@ public class UIHelper {
Resources res = context.getResources(); Resources res = context.getResources();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context); context);
if (unread.size() == 1) { if (unread.size() == 0) {
mNotificationManager.cancelAll();
} else if (unread.size() == 1) {
Conversation conversation = unread.get(0); Conversation conversation = unread.get(0);
targetUuid = conversation.getUuid(); targetUuid = conversation.getUuid();
mBuilder.setLargeIcon(UIHelper.getUnknownContactPicture(conversation mBuilder.setLargeIcon(UIHelper.getUnknownContactPicture(
.getName(), (int) res conversation.getName(),
(int) res
.getDimension(android.R.dimen.notification_large_icon_width))); .getDimension(android.R.dimen.notification_large_icon_width)));
mBuilder.setContentTitle(conversation.getName()); mBuilder.setContentTitle(conversation.getName());
if (notify) {
mBuilder.setTicker(conversation.getLatestMessage().getBody().trim()); mBuilder.setTicker(conversation.getLatestMessage().getBody().trim());
}
StringBuilder bigText = new StringBuilder(); StringBuilder bigText = new StringBuilder();
List<Message> messages = conversation.getMessages(); List<Message> messages = conversation.getMessages();
String firstLine = ""; String firstLine = "";
@ -150,7 +159,8 @@ public class UIHelper {
} }
} }
mBuilder.setContentText(firstLine); mBuilder.setContentText(firstLine);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText.toString())); mBuilder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(bigText.toString()));
} else { } else {
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle(unread.size() + " unread Conversations"); style.setBigContentTitle(unread.size() + " unread Conversations");
@ -162,12 +172,14 @@ public class UIHelper {
} else { } else {
names.append(unread.get(i).getName()); names.append(unread.get(i).getName());
} }
style.addLine(Html.fromHtml("<b>"+unread.get(i).getName()+"</b> "+unread.get(i).getLatestMessage().getBody())); style.addLine(Html.fromHtml("<b>" + unread.get(i).getName()
+ "</b> " + unread.get(i).getLatestMessage().getBody().trim()));
} }
mBuilder.setContentTitle(unread.size() + " unread Conversations"); mBuilder.setContentTitle(unread.size() + " unread Conversations");
mBuilder.setContentText(names.toString()); mBuilder.setContentText(names.toString());
mBuilder.setStyle(style); mBuilder.setStyle(style);
} }
if (unread.size() != 0) {
mBuilder.setSmallIcon(R.drawable.notification); mBuilder.setSmallIcon(R.drawable.notification);
if (notify) { if (notify) {
mBuilder.setLights(0xffffffff, 2000, 4000); mBuilder.setLights(0xffffffff, 2000, 4000);
@ -184,15 +196,17 @@ public class UIHelper {
viewConversationIntent.setAction(Intent.ACTION_VIEW); viewConversationIntent.setAction(Intent.ACTION_VIEW);
viewConversationIntent.putExtra(ConversationActivity.CONVERSATION, viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
targetUuid); targetUuid);
viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION); viewConversationIntent
.setType(ConversationActivity.VIEW_CONVERSATION);
stackBuilder.addNextIntent(viewConversationIntent); stackBuilder.addNextIntent(viewConversationIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
PendingIntent.FLAG_UPDATE_CURRENT); 0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent); mBuilder.setContentIntent(resultPendingIntent);
return mBuilder.build(); mNotificationManager.notify(2342, mBuilder.build());
}
} }
public static void prepareContactBadge(final Activity activity, public static void prepareContactBadge(final Activity activity,
@ -205,15 +219,19 @@ public class UIHelper {
if (contact.getProfilePhoto() != null) { if (contact.getProfilePhoto() != null) {
badge.setImageURI(Uri.parse(contact.getProfilePhoto())); badge.setImageURI(Uri.parse(contact.getProfilePhoto()));
} else { } else {
badge.setImageBitmap(UIHelper.getUnknownContactPicture(contact.getDisplayName(), 400)); badge.setImageBitmap(UIHelper.getUnknownContactPicture(
contact.getDisplayName(), 400));
} }
} else { } else {
badge.setImageBitmap(UIHelper.getUnknownContactPicture(contact.getDisplayName(), 400)); badge.setImageBitmap(UIHelper.getUnknownContactPicture(
contact.getDisplayName(), 400));
} }
} }
public static AlertDialog getVerifyFingerprintDialog(final ConversationActivity activity,final Conversation conversation, final LinearLayout msg) { public static AlertDialog getVerifyFingerprintDialog(
final ConversationActivity activity,
final Conversation conversation, final LinearLayout msg) {
final Contact contact = conversation.getContact(); final Contact contact = conversation.getContact();
final Account account = conversation.getAccount(); final Account account = conversation.getAccount();
@ -222,8 +240,10 @@ public class UIHelper {
LayoutInflater inflater = activity.getLayoutInflater(); LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_verify_otr, null); View view = inflater.inflate(R.layout.dialog_verify_otr, null);
TextView jid = (TextView) view.findViewById(R.id.verify_otr_jid); TextView jid = (TextView) view.findViewById(R.id.verify_otr_jid);
TextView fingerprint = (TextView) view.findViewById(R.id.verify_otr_fingerprint); TextView fingerprint = (TextView) view
TextView yourprint = (TextView) view.findViewById(R.id.verify_otr_yourprint); .findViewById(R.id.verify_otr_fingerprint);
TextView yourprint = (TextView) view
.findViewById(R.id.verify_otr_yourprint);
jid.setText(contact.getJid()); jid.setText(contact.getJid());
fingerprint.setText(conversation.getOtrFingerprint()); fingerprint.setText(conversation.getOtrFingerprint());