also show some text in new image notification

This commit is contained in:
iNPUTmice 2014-10-24 18:27:53 +02:00
parent 864f3610b3
commit e1f11a5735

View file

@ -17,6 +17,8 @@ import android.net.Uri;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.BigPictureStyle;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.TaskStackBuilder; import android.support.v4.app.TaskStackBuilder;
import android.text.Html; import android.text.Html;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
@ -99,51 +101,33 @@ public class NotificationService {
if (notify) { if (notify) {
this.markLastNotification(); this.markLastNotification();
} }
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( Builder mBuilder;
mXmppConnectionService);
mBuilder.setSmallIcon(R.drawable.ic_notification);
if (notifications.size() == 1) { if (notifications.size() == 1) {
ArrayList<Message> messages = notifications.values().iterator() mBuilder = buildSingleConversations(notify);
.next(); } else {
if (messages.size() >= 1) { mBuilder = buildMultipleConversation();
Conversation conversation = messages.get(0) }
.getConversation(); if (notify) {
mBuilder.setLargeIcon(mXmppConnectionService if (vibrate) {
.getAvatarService().get(conversation, getPixel(64))); int dat = 70;
mBuilder.setContentTitle(conversation.getName()); long[] pattern = { 0, 3 * dat, dat, dat };
if (messages.size() == 1 && messages.get(0).getType() == Message.TYPE_IMAGE) { mBuilder.setVibrate(pattern);
try { }
Bitmap bitmap = mXmppConnectionService.getFileBackend().getThumbnail(messages.get(0),getPixel(288),false); if (ringtone != null) {
mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap)); mBuilder.setSound(Uri.parse(ringtone));
mBuilder.setContentText(mXmppConnectionService.getString(R.string.image_file)); }
} catch (FileNotFoundException e) { }
// TODO Auto-generated catch block mBuilder.setSmallIcon(R.drawable.ic_notification);
e.printStackTrace(); mBuilder.setDeleteIntent(createDeleteIntent());
mBuilder.setLights(0xffffffff, 2000, 4000);
Notification notification = mBuilder.build();
notificationManager.notify(NOTIFICATION_ID, notification);
}
} }
} else { private Builder buildMultipleConversation() {
StringBuilder text = new StringBuilder(); Builder mBuilder = new NotificationCompat.Builder(
for (int i = 0; i < messages.size(); ++i) { mXmppConnectionService);
text.append(getReadableBody(messages.get(i)));
if (i != messages.size() - 1) {
text.append("\n");
}
}
mBuilder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(text.toString()));
mBuilder.setContentText(getReadableBody(messages.get(0)));
if (notify) {
mBuilder.setTicker(getReadableBody(messages
.get(messages.size() - 1)));
}
}
mBuilder.setContentIntent(createContentIntent(conversation
.getUuid()));
} else {
notificationManager.cancel(NOTIFICATION_ID);
return;
}
} else {
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle(notifications.size() style.setBigContentTitle(notifications.size()
+ " " + " "
@ -174,23 +158,88 @@ public class NotificationService {
mBuilder.setContentIntent(createContentIntent(conversation mBuilder.setContentIntent(createContentIntent(conversation
.getUuid())); .getUuid()));
} }
return mBuilder;
} }
private Builder buildSingleConversations(boolean notify) {
Builder mBuilder = new NotificationCompat.Builder(
mXmppConnectionService);
ArrayList<Message> messages = notifications.values().iterator().next();
if (messages.size() >= 1) {
Conversation conversation = messages.get(0).getConversation();
mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService()
.get(conversation, getPixel(64)));
mBuilder.setContentTitle(conversation.getName());
Message message;
if ((message = getImage(messages)) != null) {
modifyForImage(mBuilder, message, messages, notify);
} else {
modifyForTextOnly(mBuilder, messages, notify);
}
mBuilder.setContentIntent(createContentIntent(conversation
.getUuid()));
}
return mBuilder;
}
private void modifyForImage(Builder builder, Message message,
ArrayList<Message> messages, boolean notify) {
try {
Bitmap bitmap = mXmppConnectionService.getFileBackend()
.getThumbnail(message, getPixel(288), false);
ArrayList<Message> tmp = new ArrayList<Message>();
for (Message msg : messages) {
if (msg.getType() == Message.TYPE_TEXT
&& msg.getDownloadable() == null) {
tmp.add(msg);
}
}
BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.bigPicture(bitmap);
if (tmp.size() > 0) {
bigPictureStyle.setSummaryText(getMergedBodies(tmp));
builder.setContentText(getReadableBody(tmp.get(0)));
} else {
builder.setContentText(mXmppConnectionService.getString(R.string.image_file));
}
builder.setStyle(bigPictureStyle);
} catch (FileNotFoundException e) {
modifyForTextOnly(builder, messages, notify);
}
}
private void modifyForTextOnly(Builder builder,
ArrayList<Message> messages, boolean notify) {
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(getMergedBodies(messages)));
builder.setContentText(getReadableBody(messages.get(0)));
if (notify) { if (notify) {
if (vibrate) { builder.setTicker(getReadableBody(messages.get(messages.size() - 1)));
int dat = 70;
long[] pattern = { 0, 3 * dat, dat, dat };
mBuilder.setVibrate(pattern);
}
if (ringtone != null) {
mBuilder.setSound(Uri.parse(ringtone));
} }
} }
mBuilder.setDeleteIntent(createDeleteIntent());
mBuilder.setLights(0xffffffff, 2000, 4000); private Message getImage(ArrayList<Message> messages) {
Notification notification = mBuilder.build(); for (Message message : messages) {
notificationManager.notify(NOTIFICATION_ID, notification); if (message.getType() == Message.TYPE_IMAGE
&& message.getDownloadable() == null
&& message.getEncryption() != Message.ENCRYPTION_PGP) {
return message;
} }
} }
return null;
}
private String getMergedBodies(ArrayList<Message> messages) {
StringBuilder text = new StringBuilder();
for (int i = 0; i < messages.size(); ++i) {
text.append(getReadableBody(messages.get(i)));
if (i != messages.size() - 1) {
text.append("\n");
}
}
return text.toString();
}
private String getReadableBody(Message message) { private String getReadableBody(Message message) {
if (message.getDownloadable() != null if (message.getDownloadable() != null