2015-07-31 15:05:24 -07:00
|
|
|
package org.thoughtcrime.securesms.notifications;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
2016-08-01 00:15:49 +02:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2017-08-21 18:37:39 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
2017-09-21 10:03:05 -07:00
|
|
|
import org.thoughtcrime.securesms.preferences.widgets.NotificationPrivacyPreference;
|
2015-07-31 15:05:24 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
|
|
|
|
public abstract class AbstractNotificationBuilder extends NotificationCompat.Builder {
|
|
|
|
|
2017-12-01 12:55:24 -08:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private static final String TAG = AbstractNotificationBuilder.class.getSimpleName();
|
|
|
|
|
2019-04-12 17:49:22 -04:00
|
|
|
private static final int MAX_DISPLAY_LENGTH = 500;
|
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
protected Context context;
|
|
|
|
protected NotificationPrivacyPreference privacy;
|
|
|
|
|
|
|
|
public AbstractNotificationBuilder(Context context, NotificationPrivacyPreference privacy) {
|
|
|
|
super(context);
|
|
|
|
|
|
|
|
this.context = context;
|
|
|
|
this.privacy = privacy;
|
2016-09-01 21:46:20 +02:00
|
|
|
|
2018-08-16 09:47:43 -07:00
|
|
|
setChannelId(NotificationChannels.getMessagesChannel(context));
|
2016-09-01 21:46:20 +02:00
|
|
|
setLed();
|
2015-07-31 15:05:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
protected CharSequence getStyledMessage(@NonNull Recipient recipient, @Nullable CharSequence message) {
|
|
|
|
SpannableStringBuilder builder = new SpannableStringBuilder();
|
|
|
|
builder.append(Util.getBoldedString(recipient.toShortString()));
|
|
|
|
builder.append(": ");
|
|
|
|
builder.append(message == null ? "" : message);
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2017-08-21 18:37:39 -07:00
|
|
|
public void setAlarms(@Nullable Uri ringtone, RecipientDatabase.VibrateState vibrate) {
|
2018-08-30 17:59:15 -07:00
|
|
|
Uri defaultRingtone = NotificationChannels.supported() ? NotificationChannels.getMessageRingtone(context) : TextSecurePreferences.getNotificationRingtone(context);
|
|
|
|
boolean defaultVibrate = NotificationChannels.supported() ? NotificationChannels.getMessageVibrate(context) : TextSecurePreferences.isNotificationVibrateEnabled(context);
|
2015-07-31 15:05:24 -07:00
|
|
|
|
2017-12-19 14:42:00 -08:00
|
|
|
if (ringtone == null && !TextUtils.isEmpty(defaultRingtone.toString())) setSound(defaultRingtone);
|
|
|
|
else if (ringtone != null && !ringtone.toString().isEmpty()) setSound(ringtone);
|
2015-07-31 15:05:24 -07:00
|
|
|
|
2017-08-21 18:37:39 -07:00
|
|
|
if (vibrate == RecipientDatabase.VibrateState.ENABLED ||
|
|
|
|
(vibrate == RecipientDatabase.VibrateState.DEFAULT && defaultVibrate))
|
2015-07-31 15:05:24 -07:00
|
|
|
{
|
|
|
|
setDefaults(Notification.DEFAULT_VIBRATE);
|
|
|
|
}
|
2016-09-01 21:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setLed() {
|
|
|
|
String ledColor = TextSecurePreferences.getNotificationLedColor(context);
|
|
|
|
String ledBlinkPattern = TextSecurePreferences.getNotificationLedPattern(context);
|
|
|
|
String ledBlinkPatternCustom = TextSecurePreferences.getNotificationLedPatternCustom(context);
|
2015-07-31 15:05:24 -07:00
|
|
|
|
|
|
|
if (!ledColor.equals("none")) {
|
2016-09-01 21:46:20 +02:00
|
|
|
String[] blinkPatternArray = parseBlinkPattern(ledBlinkPattern, ledBlinkPatternCustom);
|
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
setLights(Color.parseColor(ledColor),
|
|
|
|
Integer.parseInt(blinkPatternArray[0]),
|
|
|
|
Integer.parseInt(blinkPatternArray[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 00:15:49 +02:00
|
|
|
public void setTicker(@NonNull Recipient recipient, @Nullable CharSequence message) {
|
|
|
|
if (privacy.isDisplayMessage()) {
|
2019-04-12 17:49:22 -04:00
|
|
|
setTicker(getStyledMessage(recipient, trimToDisplayLength(message)));
|
2016-08-01 00:15:49 +02:00
|
|
|
} else if (privacy.isDisplayContact()) {
|
|
|
|
setTicker(getStyledMessage(recipient, context.getString(R.string.AbstractNotificationBuilder_new_message)));
|
|
|
|
} else {
|
|
|
|
setTicker(context.getString(R.string.AbstractNotificationBuilder_new_message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
private String[] parseBlinkPattern(String blinkPattern, String blinkPatternCustom) {
|
|
|
|
if (blinkPattern.equals("custom"))
|
|
|
|
blinkPattern = blinkPatternCustom;
|
|
|
|
|
|
|
|
return blinkPattern.split(",");
|
|
|
|
}
|
2019-04-12 17:49:22 -04:00
|
|
|
|
|
|
|
protected @NonNull CharSequence trimToDisplayLength(@Nullable CharSequence text) {
|
|
|
|
text = text == null ? "" : text;
|
|
|
|
|
|
|
|
return text.length() <= MAX_DISPLAY_LENGTH ? text
|
|
|
|
: text.subSequence(0, MAX_DISPLAY_LENGTH);
|
|
|
|
}
|
2015-07-31 15:05:24 -07:00
|
|
|
}
|