From aa31732ea3b66201780975eae3305c9e80cc2d11 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 8 Mar 2014 06:25:35 +0100 Subject: [PATCH] last part. update notification if user reads them --- .../services/XmppConnectionService.java | 5 +- .../ui/ConversationActivity.java | 3 +- .../ui/ConversationFragment.java | 8 +- .../siacs/conversations/utils/UIHelper.java | 130 ++++++++++-------- 4 files changed, 81 insertions(+), 65 deletions(-) diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 337f928c3..64a93ea42 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -164,10 +164,7 @@ public class XmppConnectionService extends Service { if (convChangedListener != null) { convChangedListener.onConversationListChanged(); } else { - NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - mNotificationManager.notify(2342, UIHelper - .getNotification( - getApplicationContext(),getConversations(),notify)); + UIHelper.updateNotification(getApplicationContext(), getConversations(), notify); } } }; diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index f8e22dad2..07ac5518f 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -232,6 +232,7 @@ public class ConversationActivity extends XmppActivity { invalidateOptionsMenu(); if (!getSelectedConversation().isRead()) { getSelectedConversation().markRead(); + UIHelper.updateNotification(getApplicationContext(), getConversationList(), false); updateConversationList(); } } @@ -400,8 +401,6 @@ public class ConversationActivity extends XmppActivity { public void onStart() { super.onStart(); - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - nm.cancelAll(); if (conversationList.size()>=1) { onConvChanged.onConversationListChanged(); } diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index 3b8b6888f..aab712087 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -24,6 +24,8 @@ import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.UIHelper; import android.app.AlertDialog; import android.app.Fragment; +import android.app.NotificationManager; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentSender; @@ -346,10 +348,6 @@ public class ConversationFragment extends Fragment { } } - if (!conversation.isRead()) { - conversation.markRead(); - activity.updateConversationList(); - } if (queuedPqpMessage != null) { this.conversation.nextMessageEncryption = Message.ENCRYPTION_PGP; Message message = new Message(conversation, queuedPqpMessage, @@ -421,6 +419,8 @@ public class ConversationFragment extends Fragment { messagesView.setSelection(size - 1); if (!activity.shouldPaneBeOpen()) { conversation.markRead(); + //TODO update notifications + UIHelper.updateNotification(getActivity(), activity.getConversationList(), false); activity.updateConversationList(); } } diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 7ae2b745e..3571bc3a1 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -15,6 +15,7 @@ import eu.siacs.conversations.ui.ConversationActivity; import android.app.Activity; import android.app.AlertDialog; import android.app.Notification; +import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; @@ -86,7 +87,7 @@ public class UIHelper { return bitmap; } - + public static Bitmap getErrorPicture(int size) { Bitmap bitmap = Bitmap .createBitmap(size, size, Bitmap.Config.ARGB_8888); @@ -101,23 +102,26 @@ public class UIHelper { Rect rect = new Rect(); paint.getTextBounds("!", 0, 1, rect); float width = paint.measureText("!"); - canvas.drawText("!", (size / 2) - (width / 2), (size / 2) - + (rect.height() / 2), paint); + canvas.drawText("!", (size / 2) - (width / 2), + (size / 2) + (rect.height() / 2), paint); return bitmap; } - public static Notification getNotification(Context context, + public static void updateNotification(Context context, List conversations, boolean notify) { + NotificationManager mNotificationManager = (NotificationManager) context + .getSystemService(Context.NOTIFICATION_SERVICE); + String targetUuid = ""; List unread = new ArrayList(); - for(Conversation conversation : conversations) { + for (Conversation conversation : conversations) { if (!conversation.isRead()) { unread.add(conversation); } } - + SharedPreferences sharedPref = PreferenceManager .getDefaultSharedPreferences(context); String ringtone = sharedPref.getString("notification_ringtone", null); @@ -125,112 +129,128 @@ public class UIHelper { Resources res = context.getResources(); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( context); - if (unread.size() == 1) { + if (unread.size() == 0) { + mNotificationManager.cancelAll(); + } else if (unread.size() == 1) { Conversation conversation = unread.get(0); targetUuid = conversation.getUuid(); - mBuilder.setLargeIcon(UIHelper.getUnknownContactPicture(conversation - .getName(), (int) res - .getDimension(android.R.dimen.notification_large_icon_width))); + mBuilder.setLargeIcon(UIHelper.getUnknownContactPicture( + conversation.getName(), + (int) res + .getDimension(android.R.dimen.notification_large_icon_width))); mBuilder.setContentTitle(conversation.getName()); - mBuilder.setTicker(conversation.getLatestMessage().getBody().trim()); + if (notify) { + mBuilder.setTicker(conversation.getLatestMessage().getBody().trim()); + } StringBuilder bigText = new StringBuilder(); List messages = conversation.getMessages(); String firstLine = ""; - for(int i = messages.size() -1; i >= 0; --i) { + for (int i = messages.size() - 1; i >= 0; --i) { if (!messages.get(i).isRead()) { - if (i == messages.size() -1 ) { + if (i == messages.size() - 1) { firstLine = messages.get(i).getBody().trim(); bigText.append(firstLine); } else { firstLine = messages.get(i).getBody().trim(); - bigText.insert(0, firstLine+"\n"); + bigText.insert(0, firstLine + "\n"); } } else { break; } } mBuilder.setContentText(firstLine); - mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText.toString())); + mBuilder.setStyle(new NotificationCompat.BigTextStyle() + .bigText(bigText.toString())); } else { NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); - style.setBigContentTitle(unread.size()+" unread Conversations"); + style.setBigContentTitle(unread.size() + " unread Conversations"); StringBuilder names = new StringBuilder(); - for(int i = 0; i < unread.size(); ++i) { + for (int i = 0; i < unread.size(); ++i) { targetUuid = conversations.get(0).getUuid(); - if (i< unread.size()-1) { - names.append(unread.get(i).getName()+", "); + if (i < unread.size() - 1) { + names.append(unread.get(i).getName() + ", "); } else { names.append(unread.get(i).getName()); } - style.addLine(Html.fromHtml(""+unread.get(i).getName()+" "+unread.get(i).getLatestMessage().getBody())); + style.addLine(Html.fromHtml("" + unread.get(i).getName() + + " " + unread.get(i).getLatestMessage().getBody().trim())); } - mBuilder.setContentTitle(unread.size()+" unread Conversations"); + mBuilder.setContentTitle(unread.size() + " unread Conversations"); mBuilder.setContentText(names.toString()); mBuilder.setStyle(style); } - mBuilder.setSmallIcon(R.drawable.notification); - if (notify) { - mBuilder.setLights(0xffffffff, 2000, 4000); - if (ringtone != null) { - mBuilder.setSound(Uri.parse(ringtone)); + if (unread.size() != 0) { + mBuilder.setSmallIcon(R.drawable.notification); + if (notify) { + 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); + mNotificationManager.notify(2342, mBuilder.build()); } - - 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); - return mBuilder.build(); } public static void prepareContactBadge(final Activity activity, QuickContactBadge badge, final Contact contact) { - if (contact.getSystemAccount()!=null) { + if (contact.getSystemAccount() != null) { String[] systemAccount = contact.getSystemAccount().split("#"); long id = Long.parseLong(systemAccount[0]); badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1])); - + if (contact.getProfilePhoto() != null) { badge.setImageURI(Uri.parse(contact.getProfilePhoto())); } else { - badge.setImageBitmap(UIHelper.getUnknownContactPicture(contact.getDisplayName(), 400)); + badge.setImageBitmap(UIHelper.getUnknownContactPicture( + contact.getDisplayName(), 400)); } } 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 Account account = conversation.getAccount(); - + AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("Verify fingerprint"); LayoutInflater inflater = activity.getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_verify_otr, null); TextView jid = (TextView) view.findViewById(R.id.verify_otr_jid); - TextView fingerprint = (TextView) view.findViewById(R.id.verify_otr_fingerprint); - TextView yourprint = (TextView) view.findViewById(R.id.verify_otr_yourprint); - + TextView fingerprint = (TextView) view + .findViewById(R.id.verify_otr_fingerprint); + TextView yourprint = (TextView) view + .findViewById(R.id.verify_otr_yourprint); + jid.setText(contact.getJid()); fingerprint.setText(conversation.getOtrFingerprint()); yourprint.setText(account.getOtrFingerprint()); builder.setNegativeButton("Cancel", null); builder.setPositiveButton("Verify", new OnClickListener() { - + @Override public void onClick(DialogInterface dialog, int which) { contact.addOtrFingerprint(conversation.getOtrFingerprint());