session-android/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java

328 lines
12 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.notifications;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Bitmap;
2019-11-27 12:02:30 +11:00
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Action;
import android.support.v4.app.RemoteInput;
import android.text.SpannableStringBuilder;
2016-10-16 19:05:07 -07:00
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
import org.thoughtcrime.securesms.contacts.avatars.FallbackContactPhoto;
import org.thoughtcrime.securesms.contacts.avatars.GeneratedContactPhoto;
import org.thoughtcrime.securesms.logging.Log;
2020-05-11 16:54:31 +10:00
import org.thoughtcrime.securesms.loki.utilities.NotificationUtilities;
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader;
2017-10-11 17:12:46 -07:00
import org.thoughtcrime.securesms.mms.GlideApp;
import org.thoughtcrime.securesms.mms.Slide;
import org.thoughtcrime.securesms.mms.SlideDeck;
import org.thoughtcrime.securesms.preferences.widgets.NotificationPrivacyPreference;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.BitmapUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
2019-07-25 10:40:57 +10:00
import network.loki.messenger.R;
public class SingleRecipientNotificationBuilder extends AbstractNotificationBuilder {
private static final String TAG = SingleRecipientNotificationBuilder.class.getSimpleName();
private final List<CharSequence> messageBodies = new LinkedList<>();
private SlideDeck slideDeck;
2017-11-26 16:32:18 -08:00
private CharSequence contentTitle;
private CharSequence contentText;
public SingleRecipientNotificationBuilder(@NonNull Context context, @NonNull NotificationPrivacyPreference privacy)
{
super(context, privacy);
setSmallIcon(R.drawable.ic_notification);
setColor(context.getResources().getColor(R.color.textsecure_primary));
setCategory(NotificationCompat.CATEGORY_MESSAGE);
if (!NotificationChannels.supported()) {
setPriority(TextSecurePreferences.getNotificationPriority(context));
}
}
public void setThread(@NonNull Recipient recipient) {
String channelId = recipient.getNotificationChannel();
setChannelId(channelId != null ? channelId : NotificationChannels.getMessagesChannel(context));
if (privacy.isDisplayContact()) {
setContentTitle(recipient.toShortString());
if (recipient.getContactUri() != null) {
addPerson(recipient.getContactUri().toString());
}
ContactPhoto contactPhoto = recipient.getContactPhoto();
FallbackContactPhoto fallbackContactPhoto = recipient.getFallbackContactPhoto();
if (contactPhoto != null) {
try {
setLargeIcon(GlideApp.with(context.getApplicationContext())
.load(contactPhoto)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.circleCrop()
.submit(context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height))
.get());
} catch (InterruptedException | ExecutionException e) {
Log.w(TAG, e);
setLargeIcon(fallbackContactPhoto.asDrawable(context, recipient.getColor().toConversationColor(context)));
}
} else {
setLargeIcon(fallbackContactPhoto.asDrawable(context, recipient.getColor().toConversationColor(context)));
}
} else {
setContentTitle(context.getString(R.string.SingleRecipientNotificationBuilder_signal));
2018-09-24 23:41:29 -07:00
setLargeIcon(new GeneratedContactPhoto("Unknown", R.drawable.ic_profile_default).asDrawable(context, ContactColors.UNKNOWN_COLOR.toConversationColor(context)));
}
}
public void setMessageCount(int messageCount) {
setContentInfo(String.valueOf(messageCount));
setNumber(messageCount);
}
public void setPrimaryMessageBody(@NonNull Recipient threadRecipients,
@NonNull Recipient individualRecipient,
@NonNull CharSequence message,
@Nullable SlideDeck slideDeck)
{
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
if (privacy.isDisplayContact() && threadRecipients.isGroupRecipient()) {
String displayName = NotificationUtilities.getOpenGroupDisplayName(individualRecipient, threadRecipients, context);
2020-01-22 15:11:14 +11:00
if (displayName != null) {
stringBuilder.append(Util.getBoldedString(displayName + ": "));
}
}
if (privacy.isDisplayMessage()) {
setContentText(stringBuilder.append(message));
this.slideDeck = slideDeck;
} else {
setContentText(stringBuilder.append(context.getString(R.string.SingleRecipientNotificationBuilder_new_message)));
}
}
public void addAndroidAutoAction(@NonNull PendingIntent androidAutoReplyIntent,
@NonNull PendingIntent androidAutoHeardIntent, long timestamp)
{
2017-11-26 16:32:18 -08:00
if (contentTitle == null || contentText == null)
return;
RemoteInput remoteInput = new RemoteInput.Builder(AndroidAutoReplyReceiver.VOICE_REPLY_KEY)
.setLabel(context.getString(R.string.MessageNotifier_reply))
.build();
NotificationCompat.CarExtender.UnreadConversation.Builder unreadConversationBuilder =
2017-11-26 16:32:18 -08:00
new NotificationCompat.CarExtender.UnreadConversation.Builder(contentTitle.toString())
.addMessage(contentText.toString())
.setLatestTimestamp(timestamp)
.setReadPendingIntent(androidAutoHeardIntent)
.setReplyAction(androidAutoReplyIntent, remoteInput);
extend(new NotificationCompat.CarExtender().setUnreadConversation(unreadConversationBuilder.build()));
}
public void addActions(@NonNull PendingIntent markReadIntent,
2020-03-12 10:42:08 +11:00
@Nullable PendingIntent quickReplyIntent,
@Nullable PendingIntent wearableReplyIntent,
@NonNull ReplyMethod replyMethod)
{
Action markAsReadAction = new Action(R.drawable.check,
context.getString(R.string.MessageNotifier_mark_read),
markReadIntent);
2020-03-12 10:42:08 +11:00
addAction(markAsReadAction);
2020-03-12 10:42:08 +11:00
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender().addAction(markAsReadAction);
2020-03-12 10:42:08 +11:00
if (quickReplyIntent != null) {
String actionName = context.getString(R.string.MessageNotifier_reply);
String label = context.getString(replyMethodLongDescription(replyMethod));
2020-05-11 16:54:31 +10:00
Action replyAction = new Action(R.drawable.ic_reply_white_36dp, actionName, quickReplyIntent);
2020-03-12 10:42:08 +11:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
replyAction = new Action.Builder(R.drawable.ic_reply_white_36dp,
actionName,
wearableReplyIntent)
2020-05-11 16:54:31 +10:00
.addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_REMOTE_REPLY).setLabel(label).build())
2020-03-12 10:42:08 +11:00
.build();
}
2020-03-12 10:42:08 +11:00
Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply,
actionName,
wearableReplyIntent)
2020-05-11 16:54:31 +10:00
.addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_REMOTE_REPLY).setLabel(label).build())
2020-03-12 10:42:08 +11:00
.build();
addAction(replyAction);
wearableExtender.addAction(wearableReplyAction);
}
extend(wearableExtender);
}
@StringRes
private static int replyMethodLongDescription(@NonNull ReplyMethod replyMethod) {
2020-03-12 10:42:08 +11:00
return R.string.MessageNotifier_reply;
}
public void addMessageBody(@NonNull Recipient threadRecipient,
@NonNull Recipient individualRecipient,
@Nullable CharSequence messageBody)
{
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
if (privacy.isDisplayContact() && threadRecipient.isGroupRecipient()) {
String displayName = NotificationUtilities.getOpenGroupDisplayName(individualRecipient, threadRecipient, context);
if (displayName != null) {
stringBuilder.append(Util.getBoldedString(displayName + ": "));
}
}
if (privacy.isDisplayMessage()) {
messageBodies.add(stringBuilder.append(messageBody == null ? "" : messageBody));
} else {
messageBodies.add(stringBuilder.append(context.getString(R.string.SingleRecipientNotificationBuilder_new_message)));
}
}
@Override
public Notification build() {
if (privacy.isDisplayMessage()) {
if (messageBodies.size() == 1 && hasBigPictureSlide(slideDeck)) {
setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(getBigPicture(slideDeck))
.setSummaryText(getBigText(messageBodies)));
} else {
setStyle(new NotificationCompat.BigTextStyle().bigText(getBigText(messageBodies)));
}
}
return super.build();
}
private void setLargeIcon(@Nullable Drawable drawable) {
if (drawable != null) {
int largeIconTargetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
Bitmap recipientPhotoBitmap = BitmapUtil.createFromDrawable(drawable, largeIconTargetSize, largeIconTargetSize);
if (recipientPhotoBitmap != null) {
2020-05-11 16:54:31 +10:00
setLargeIcon(getCircularBitmap(recipientPhotoBitmap));
}
}
}
2020-05-11 16:54:31 +10:00
private Bitmap getCircularBitmap(Bitmap bitmap) {
2019-11-27 12:02:30 +11:00
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
private boolean hasBigPictureSlide(@Nullable SlideDeck slideDeck) {
if (slideDeck == null) {
return false;
}
Slide thumbnailSlide = slideDeck.getThumbnailSlide();
return thumbnailSlide != null &&
thumbnailSlide.hasImage() &&
!thumbnailSlide.isInProgress() &&
thumbnailSlide.getThumbnailUri() != null;
}
private Bitmap getBigPicture(@NonNull SlideDeck slideDeck)
{
try {
@SuppressWarnings("ConstantConditions")
Uri uri = slideDeck.getThumbnailSlide().getThumbnailUri();
return GlideApp.with(context.getApplicationContext())
2017-10-11 17:12:46 -07:00
.asBitmap()
.load(new DecryptableStreamUriLoader.DecryptableUri(uri))
2017-10-11 17:12:46 -07:00
.diskCacheStrategy(DiskCacheStrategy.NONE)
.submit(500, 500)
.get();
} catch (InterruptedException | ExecutionException e) {
Log.w(TAG, e);
return Bitmap.createBitmap(500, 500, Bitmap.Config.RGB_565);
}
}
2017-11-26 16:32:18 -08:00
@Override
public NotificationCompat.Builder setContentTitle(CharSequence contentTitle) {
this.contentTitle = contentTitle;
return super.setContentTitle(contentTitle);
}
public NotificationCompat.Builder setContentText(CharSequence contentText) {
this.contentText = trimToDisplayLength(contentText);
return super.setContentText(this.contentText);
2017-11-26 16:32:18 -08:00
}
private CharSequence getBigText(List<CharSequence> messageBodies) {
SpannableStringBuilder content = new SpannableStringBuilder();
for (int i = 0; i < messageBodies.size(); i++) {
content.append(trimToDisplayLength(messageBodies.get(i)));
if (i < messageBodies.size() - 1) {
content.append('\n');
}
}
return content;
}
}