2015-07-31 15:05:24 -07: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-07-31 16:46:17 -07:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
2015-07-31 15:05:24 -07: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-07-31 16:46:17 -07:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.bumptech.glide.Glide;
|
2015-07-31 15:05:24 -07:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-07-31 16:46:17 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader;
|
2015-08-10 09:33:57 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
2015-07-31 16:46:17 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck;
|
2015-07-31 15:05:24 -07:00
|
|
|
import org.thoughtcrime.securesms.preferences.NotificationPrivacyPreference;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
2015-07-31 16:46:17 -07:00
|
|
|
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
2015-07-31 15:05:24 -07:00
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2015-07-31 16:46:17 -07:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2015-07-31 15:05:24 -07:00
|
|
|
|
|
|
|
public class SingleRecipientNotificationBuilder extends AbstractNotificationBuilder {
|
|
|
|
|
2015-07-31 16:46:17 -07:00
|
|
|
private static final String TAG = SingleRecipientNotificationBuilder.class.getSimpleName();
|
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
private final List<CharSequence> messageBodies = new LinkedList<>();
|
|
|
|
|
2015-07-31 16:46:17 -07:00
|
|
|
private ListenableFutureTask<SlideDeck> slideDeck;
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
|
|
|
|
public SingleRecipientNotificationBuilder(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
@NonNull NotificationPrivacyPreference privacy)
|
|
|
|
{
|
2015-07-31 15:05:24 -07:00
|
|
|
super(context, privacy);
|
2015-07-31 16:46:17 -07:00
|
|
|
this.masterSecret = masterSecret;
|
2015-07-31 15:05:24 -07:00
|
|
|
|
|
|
|
setSmallIcon(R.drawable.icon_notification);
|
|
|
|
setColor(context.getResources().getColor(R.color.textsecure_primary));
|
|
|
|
setPriority(NotificationCompat.PRIORITY_HIGH);
|
|
|
|
setCategory(NotificationCompat.CATEGORY_MESSAGE);
|
|
|
|
setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(MessageNotifier.DeleteReceiver.DELETE_REMINDER_ACTION), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSender(@NonNull Recipient recipient) {
|
|
|
|
if (privacy.isDisplayContact()) {
|
|
|
|
setContentTitle(recipient.toShortString());
|
|
|
|
|
|
|
|
if (recipient.getContactUri() != null) {
|
|
|
|
addPerson(recipient.getContactUri().toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
setLargeIcon(recipient.getContactPhoto()
|
|
|
|
.asDrawable(context, recipient.getColor()
|
|
|
|
.toConversationColor(context)));
|
|
|
|
} else {
|
2015-09-23 13:35:18 -07:00
|
|
|
setContentTitle(context.getString(R.string.SingleRecipientNotificationBuilder_new_signal_message));
|
2015-07-31 15:05:24 -07:00
|
|
|
setLargeIcon(Recipient.getUnknownRecipient()
|
|
|
|
.getContactPhoto()
|
|
|
|
.asDrawable(context, Recipient.getUnknownRecipient()
|
|
|
|
.getColor()
|
|
|
|
.toConversationColor(context)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMessageCount(int messageCount) {
|
|
|
|
setContentInfo(String.valueOf(messageCount));
|
|
|
|
setNumber(messageCount);
|
|
|
|
}
|
|
|
|
|
2015-07-31 16:46:17 -07:00
|
|
|
public void setPrimaryMessageBody(CharSequence message, @Nullable ListenableFutureTask<SlideDeck> slideDeck) {
|
2015-07-31 15:05:24 -07:00
|
|
|
if (privacy.isDisplayMessage()) {
|
|
|
|
setContentText(message);
|
2015-07-31 16:46:17 -07:00
|
|
|
this.slideDeck = slideDeck;
|
2015-07-31 15:05:24 -07:00
|
|
|
} else {
|
|
|
|
setContentText(context.getString(R.string.SingleRecipientNotificationBuilder_contents_hidden));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply,
|
|
|
|
context.getString(R.string.MessageNotifier_reply),
|
|
|
|
wearableReplyIntent)
|
|
|
|
.addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_VOICE_REPLY)
|
|
|
|
.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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addMessageBody(@Nullable CharSequence messageBody) {
|
|
|
|
if (privacy.isDisplayMessage()) {
|
|
|
|
messageBodies.add(messageBody == null ? "" : messageBody);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTicker(@NonNull Recipient recipient, @Nullable CharSequence message) {
|
|
|
|
if (privacy.isDisplayMessage()) {
|
|
|
|
setTicker(getStyledMessage(recipient, message));
|
|
|
|
} else if (privacy.isDisplayContact()) {
|
2015-09-23 13:35:18 -07:00
|
|
|
setTicker(getStyledMessage(recipient, context.getString(R.string.SingleRecipientNotificationBuilder_new_signal_message)));
|
2015-07-31 15:05:24 -07:00
|
|
|
} else {
|
2015-09-23 13:35:18 -07:00
|
|
|
setTicker(context.getString(R.string.SingleRecipientNotificationBuilder_new_signal_message));
|
2015-07-31 15:05:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Notification build() {
|
|
|
|
if (privacy.isDisplayMessage()) {
|
2015-07-31 16:46:17 -07: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-07-31 15:05:24 -07: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-07-31 16:46:17 -07:00
|
|
|
private boolean hasBigPictureSlide(@Nullable ListenableFutureTask<SlideDeck> slideDeck) {
|
|
|
|
try {
|
2015-08-10 09:33:57 -07:00
|
|
|
if (masterSecret == null || slideDeck == null || Build.VERSION.SDK_INT < 16) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slide thumbnailSlide = slideDeck.get().getThumbnailSlide();
|
|
|
|
|
|
|
|
return thumbnailSlide != null &&
|
|
|
|
thumbnailSlide.hasImage() &&
|
|
|
|
!thumbnailSlide.isInProgress() &&
|
|
|
|
thumbnailSlide.getThumbnailUri() != null;
|
|
|
|
|
2015-07-31 16:46:17 -07:00
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bitmap getBigPicture(@NonNull MasterSecret masterSecret,
|
|
|
|
@NonNull ListenableFutureTask<SlideDeck> slideDeck)
|
|
|
|
{
|
|
|
|
try {
|
2015-08-10 09:33:57 -07:00
|
|
|
Uri uri = slideDeck.get().getThumbnailSlide().getThumbnailUri();
|
2015-07-31 16:46:17 -07:00
|
|
|
|
|
|
|
return Glide.with(context)
|
|
|
|
.load(new DecryptableStreamUriLoader.DecryptableUri(masterSecret, uri))
|
|
|
|
.asBitmap()
|
|
|
|
.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-07-31 15:05:24 -07:00
|
|
|
}
|