first step towards nicer notifications

This commit is contained in:
Daniel Gultsch 2014-03-08 05:46:20 +01:00
parent fbd25c8129
commit 02883a7ac4
2 changed files with 54 additions and 22 deletions

View file

@ -164,7 +164,7 @@ public class XmppConnectionService extends Service {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2342, UIHelper mNotificationManager.notify(2342, UIHelper
.getUnreadMessageNotification( .getUnreadMessageNotification(
getApplicationContext(), conversation)); getApplicationContext(),getConversations()));
} }
} }
} }

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.utils; package eu.siacs.conversations.utils;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -29,7 +30,10 @@ import android.net.Uri;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Contacts;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.InboxStyle;
import android.support.v4.app.TaskStackBuilder; import android.support.v4.app.TaskStackBuilder;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -104,8 +108,16 @@ public class UIHelper {
} }
public static Notification getUnreadMessageNotification(Context context, public static Notification getUnreadMessageNotification(Context context,
Conversation conversation) { List<Conversation> conversations) {
String targetUuid = "";
List<Conversation> unread = new ArrayList<Conversation>();
for(Conversation conversation : conversations) {
if (!conversation.isRead()) {
unread.add(conversation);
}
}
SharedPreferences sharedPref = PreferenceManager SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(context); .getDefaultSharedPreferences(context);
String ringtone = sharedPref.getString("notification_ringtone", null); String ringtone = sharedPref.getString("notification_ringtone", null);
@ -113,29 +125,49 @@ 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);
mBuilder.setLargeIcon(UIHelper.getUnknownContactPicture(conversation if (unread.size() == 1) {
.getName(), (int) res Conversation conversation = unread.get(0);
.getDimension(android.R.dimen.notification_large_icon_width))); targetUuid = conversation.getUuid();
mBuilder.setContentTitle(conversation.getName()); mBuilder.setLargeIcon(UIHelper.getUnknownContactPicture(conversation
mBuilder.setTicker(conversation.getLatestMessage().getBody().trim()); .getName(), (int) res
StringBuilder bigText = new StringBuilder(); .getDimension(android.R.dimen.notification_large_icon_width)));
List<Message> messages = conversation.getMessages(); mBuilder.setContentTitle(conversation.getName());
String firstLine = ""; mBuilder.setTicker(conversation.getLatestMessage().getBody().trim());
for(int i = messages.size() -1; i >= 0; --i) { StringBuilder bigText = new StringBuilder();
if (!messages.get(i).isRead()) { List<Message> messages = conversation.getMessages();
if (i == messages.size() -1 ) { String firstLine = "";
firstLine = messages.get(i).getBody().trim(); for(int i = messages.size() -1; i >= 0; --i) {
bigText.append(firstLine); if (!messages.get(i).isRead()) {
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");
}
} else { } else {
firstLine = messages.get(i).getBody().trim(); break;
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()+" unread Conversations");
StringBuilder names = new StringBuilder();
for(int i = 0; i < unread.size(); ++i) {
targetUuid = conversations.get(0).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().getBody()));
}
mBuilder.setContentTitle(unread.size()+" unread Conversations");
mBuilder.setContentText(names.toString());
mBuilder.setStyle(style);
} }
mBuilder.setContentText(firstLine);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText.toString()));
mBuilder.setSmallIcon(R.drawable.notification); mBuilder.setSmallIcon(R.drawable.notification);
mBuilder.setLights(0xffffffff, 2000, 4000); mBuilder.setLights(0xffffffff, 2000, 4000);
if (ringtone != null) { if (ringtone != null) {
@ -149,7 +181,7 @@ public class UIHelper {
ConversationActivity.class); ConversationActivity.class);
viewConversationIntent.setAction(Intent.ACTION_VIEW); viewConversationIntent.setAction(Intent.ACTION_VIEW);
viewConversationIntent.putExtra(ConversationActivity.CONVERSATION, viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
conversation.getUuid()); targetUuid);
viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION); viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
stackBuilder.addNextIntent(viewConversationIntent); stackBuilder.addNextIntent(viewConversationIntent);