2015-08-01 00:05:24 +02:00
|
|
|
package org.thoughtcrime.securesms.notifications;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2015-08-01 01:46:17 +02:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
2015-08-01 00:05:24 +02:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
|
import android.support.v4.app.NotificationCompat.Action;
|
|
|
|
import android.support.v4.app.RemoteInput;
|
|
|
|
import android.text.SpannableStringBuilder;
|
2015-08-01 01:46:17 +02:00
|
|
|
|
|
|
|
import com.bumptech.glide.Glide;
|
2016-10-17 04:05:07 +02:00
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
2015-08-01 00:05:24 +02:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-08-01 01:46:17 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader;
|
2015-08-10 18:33:57 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
2015-08-01 01:46:17 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck;
|
2015-08-01 00:05:24 +02:00
|
|
|
import org.thoughtcrime.securesms.preferences.NotificationPrivacyPreference;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-12-05 00:00:13 +01:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2015-08-01 00:05:24 +02:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
2017-04-22 08:04:54 +02:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2015-12-05 00:00:13 +01:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2015-08-01 00:05:24 +02:00
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2015-08-01 01:46:17 +02:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2015-08-01 00:05:24 +02:00
|
|
|
|
|
|
|
public class SingleRecipientNotificationBuilder extends AbstractNotificationBuilder {
|
|
|
|
|
2015-08-01 01:46:17 +02:00
|
|
|
private static final String TAG = SingleRecipientNotificationBuilder.class.getSimpleName();
|
|
|
|
|
2015-08-01 00:05:24 +02:00
|
|
|
private final List<CharSequence> messageBodies = new LinkedList<>();
|
|
|
|
|
2015-10-22 00:32:19 +02:00
|
|
|
private SlideDeck slideDeck;
|
|
|
|
private final MasterSecret masterSecret;
|
2015-08-01 01:46:17 +02:00
|
|
|
|
|
|
|
public SingleRecipientNotificationBuilder(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
@NonNull NotificationPrivacyPreference privacy)
|
|
|
|
{
|
2015-08-01 00:05:24 +02:00
|
|
|
super(context, privacy);
|
2015-08-01 01:46:17 +02:00
|
|
|
this.masterSecret = masterSecret;
|
2015-08-01 00:05:24 +02:00
|
|
|
|
|
|
|
setSmallIcon(R.drawable.icon_notification);
|
|
|
|
setColor(context.getResources().getColor(R.color.textsecure_primary));
|
2017-04-22 08:04:54 +02:00
|
|
|
setPriority(TextSecurePreferences.getNotificationPriority(context));
|
2015-08-01 00:05:24 +02:00
|
|
|
setCategory(NotificationCompat.CATEGORY_MESSAGE);
|
|
|
|
}
|
|
|
|
|
2015-12-05 00:00:13 +01:00
|
|
|
public void setThread(@NonNull Recipients recipients) {
|
2015-08-01 00:05:24 +02:00
|
|
|
if (privacy.isDisplayContact()) {
|
2015-12-05 00:00:13 +01:00
|
|
|
setContentTitle(recipients.toShortString());
|
2015-08-01 00:05:24 +02:00
|
|
|
|
2015-12-05 00:00:13 +01:00
|
|
|
if (recipients.isSingleRecipient() && recipients.getPrimaryRecipient().getContactUri() != null) {
|
|
|
|
addPerson(recipients.getPrimaryRecipient().getContactUri().toString());
|
2015-08-01 00:05:24 +02:00
|
|
|
}
|
|
|
|
|
2015-12-05 00:00:13 +01:00
|
|
|
setLargeIcon(recipients.getContactPhoto()
|
|
|
|
.asDrawable(context, recipients.getColor()
|
2015-08-01 00:05:24 +02:00
|
|
|
.toConversationColor(context)));
|
|
|
|
} else {
|
2015-11-17 00:55:22 +01:00
|
|
|
setContentTitle(context.getString(R.string.SingleRecipientNotificationBuilder_signal));
|
2015-08-01 00:05:24 +02:00
|
|
|
setLargeIcon(Recipient.getUnknownRecipient()
|
|
|
|
.getContactPhoto()
|
|
|
|
.asDrawable(context, Recipient.getUnknownRecipient()
|
|
|
|
.getColor()
|
|
|
|
.toConversationColor(context)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMessageCount(int messageCount) {
|
|
|
|
setContentInfo(String.valueOf(messageCount));
|
|
|
|
setNumber(messageCount);
|
|
|
|
}
|
|
|
|
|
2015-12-05 00:00:13 +01:00
|
|
|
public void setPrimaryMessageBody(@NonNull Recipients threadRecipients,
|
|
|
|
@NonNull Recipient individualRecipient,
|
|
|
|
@NonNull CharSequence message,
|
|
|
|
@Nullable SlideDeck slideDeck)
|
|
|
|
{
|
|
|
|
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
|
|
|
|
|
|
|
|
if (privacy.isDisplayContact() && (threadRecipients.isGroupRecipient() || !threadRecipients.isSingleRecipient())) {
|
|
|
|
stringBuilder.append(Util.getBoldedString(individualRecipient.toShortString() + ": "));
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:05:24 +02:00
|
|
|
if (privacy.isDisplayMessage()) {
|
2015-12-05 00:00:13 +01:00
|
|
|
setContentText(stringBuilder.append(message));
|
2015-08-01 01:46:17 +02:00
|
|
|
this.slideDeck = slideDeck;
|
2015-08-01 00:05:24 +02:00
|
|
|
} else {
|
2015-12-05 00:00:13 +01:00
|
|
|
setContentText(stringBuilder.append(context.getString(R.string.SingleRecipientNotificationBuilder_new_message)));
|
2015-08-01 00:05:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 23:58:24 +02:00
|
|
|
public void addAndroidAutoAction(@NonNull PendingIntent androidAutoReplyIntent,
|
|
|
|
@NonNull PendingIntent androidAutoHeardIntent, long timestamp)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (mContentTitle == null || mContentText == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RemoteInput remoteInput = new RemoteInput.Builder(AndroidAutoReplyReceiver.VOICE_REPLY_KEY)
|
|
|
|
.setLabel(context.getString(R.string.MessageNotifier_reply))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
NotificationCompat.CarExtender.UnreadConversation.Builder unreadConversationBuilder =
|
|
|
|
new NotificationCompat.CarExtender.UnreadConversation.Builder(mContentTitle.toString())
|
|
|
|
.addMessage(mContentText.toString())
|
|
|
|
.setLatestTimestamp(timestamp)
|
|
|
|
.setReadPendingIntent(androidAutoHeardIntent)
|
|
|
|
.setReplyAction(androidAutoReplyIntent, remoteInput);
|
|
|
|
|
|
|
|
extend(new NotificationCompat.CarExtender().setUnreadConversation(unreadConversationBuilder.build()));
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:05:24 +02:00
|
|
|
public void addActions(@Nullable MasterSecret masterSecret,
|
|
|
|
@NonNull PendingIntent markReadIntent,
|
|
|
|
@NonNull PendingIntent quickReplyIntent,
|
|
|
|
@NonNull PendingIntent wearableReplyIntent)
|
|
|
|
{
|
|
|
|
Action markAsReadAction = new Action(R.drawable.check,
|
|
|
|
context.getString(R.string.MessageNotifier_mark_read),
|
|
|
|
markReadIntent);
|
|
|
|
|
|
|
|
if (masterSecret != null) {
|
|
|
|
Action replyAction = new Action(R.drawable.ic_reply_white_36dp,
|
|
|
|
context.getString(R.string.MessageNotifier_reply),
|
|
|
|
quickReplyIntent);
|
|
|
|
|
2016-12-26 04:00:33 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
replyAction = new Action.Builder(R.drawable.ic_reply_white_36dp,
|
|
|
|
context.getString(R.string.MessageNotifier_reply),
|
|
|
|
wearableReplyIntent)
|
|
|
|
.addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_REMOTE_REPLY)
|
|
|
|
.setLabel(context.getString(R.string.MessageNotifier_reply)).build())
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:05:24 +02:00
|
|
|
Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply,
|
|
|
|
context.getString(R.string.MessageNotifier_reply),
|
|
|
|
wearableReplyIntent)
|
2016-12-26 04:00:33 +01:00
|
|
|
.addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_REMOTE_REPLY)
|
2015-08-01 00:05:24 +02:00
|
|
|
.setLabel(context.getString(R.string.MessageNotifier_reply)).build())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
addAction(markAsReadAction);
|
|
|
|
addAction(replyAction);
|
|
|
|
|
|
|
|
extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction)
|
|
|
|
.addAction(wearableReplyAction));
|
|
|
|
} else {
|
|
|
|
addAction(markAsReadAction);
|
|
|
|
|
|
|
|
extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 00:00:13 +01:00
|
|
|
public void addMessageBody(@NonNull Recipients threadRecipients,
|
|
|
|
@NonNull Recipient individualRecipient,
|
|
|
|
@Nullable CharSequence messageBody)
|
|
|
|
{
|
|
|
|
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
|
|
|
|
|
|
|
|
if (privacy.isDisplayContact() && (threadRecipients.isGroupRecipient() || !threadRecipients.isSingleRecipient())) {
|
|
|
|
stringBuilder.append(Util.getBoldedString(individualRecipient.toShortString() + ": "));
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:05:24 +02:00
|
|
|
if (privacy.isDisplayMessage()) {
|
2015-12-05 00:00:13 +01:00
|
|
|
messageBodies.add(stringBuilder.append(messageBody == null ? "" : messageBody));
|
|
|
|
} else {
|
|
|
|
messageBodies.add(stringBuilder.append(context.getString(R.string.SingleRecipientNotificationBuilder_new_message)));
|
2015-08-01 00:05:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Notification build() {
|
|
|
|
if (privacy.isDisplayMessage()) {
|
2015-08-01 01:46:17 +02:00
|
|
|
if (messageBodies.size() == 1 && hasBigPictureSlide(slideDeck)) {
|
|
|
|
assert masterSecret != null;
|
|
|
|
setStyle(new NotificationCompat.BigPictureStyle()
|
|
|
|
.bigPicture(getBigPicture(masterSecret, slideDeck))
|
|
|
|
.setSummaryText(getBigText(messageBodies)));
|
|
|
|
} else {
|
|
|
|
setStyle(new NotificationCompat.BigTextStyle().bigText(getBigText(messageBodies)));
|
2015-08-01 00:05:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
setLargeIcon(recipientPhotoBitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:32:19 +02:00
|
|
|
private boolean hasBigPictureSlide(@Nullable SlideDeck slideDeck) {
|
|
|
|
if (masterSecret == null || slideDeck == null || Build.VERSION.SDK_INT < 16) {
|
2015-08-01 01:46:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-22 00:32:19 +02:00
|
|
|
|
|
|
|
Slide thumbnailSlide = slideDeck.getThumbnailSlide();
|
|
|
|
|
|
|
|
return thumbnailSlide != null &&
|
|
|
|
thumbnailSlide.hasImage() &&
|
|
|
|
!thumbnailSlide.isInProgress() &&
|
|
|
|
thumbnailSlide.getThumbnailUri() != null;
|
2015-08-01 01:46:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private Bitmap getBigPicture(@NonNull MasterSecret masterSecret,
|
2015-10-22 00:32:19 +02:00
|
|
|
@NonNull SlideDeck slideDeck)
|
2015-08-01 01:46:17 +02:00
|
|
|
{
|
|
|
|
try {
|
2015-10-22 00:32:19 +02:00
|
|
|
@SuppressWarnings("ConstantConditions")
|
|
|
|
Uri uri = slideDeck.getThumbnailSlide().getThumbnailUri();
|
2015-08-01 01:46:17 +02:00
|
|
|
|
|
|
|
return Glide.with(context)
|
|
|
|
.load(new DecryptableStreamUriLoader.DecryptableUri(masterSecret, uri))
|
|
|
|
.asBitmap()
|
2016-10-17 04:05:07 +02:00
|
|
|
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
2015-08-01 01:46:17 +02:00
|
|
|
.into(500, 500)
|
|
|
|
.get();
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private CharSequence getBigText(List<CharSequence> messageBodies) {
|
|
|
|
SpannableStringBuilder content = new SpannableStringBuilder();
|
|
|
|
|
|
|
|
for (CharSequence message : messageBodies) {
|
|
|
|
content.append(message);
|
|
|
|
content.append('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:05:24 +02:00
|
|
|
}
|