2018-08-06 12:20:24 -04:00
|
|
|
package org.thoughtcrime.securesms.notifications;
|
|
|
|
|
2018-08-16 09:47:43 -07:00
|
|
|
import android.annotation.TargetApi;
|
2018-08-06 12:20:24 -04:00
|
|
|
import android.app.NotificationChannel;
|
2018-08-16 09:47:43 -07:00
|
|
|
import android.app.NotificationChannelGroup;
|
2018-08-06 12:20:24 -04:00
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.content.Context;
|
2018-08-16 09:47:43 -07:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.media.AudioAttributes;
|
|
|
|
import android.net.Uri;
|
2018-08-06 12:20:24 -04:00
|
|
|
import android.os.Build;
|
2018-08-16 09:47:43 -07:00
|
|
|
import android.provider.Settings;
|
2018-08-06 12:20:24 -04:00
|
|
|
import android.support.annotation.NonNull;
|
2018-08-16 09:47:43 -07:00
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.annotation.WorkerThread;
|
|
|
|
import android.text.TextUtils;
|
2018-08-06 12:20:24 -04:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.BuildConfig;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2018-08-16 09:47:43 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase.VibrateState;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.whispersystems.libsignal.logging.Log;
|
2018-08-06 12:20:24 -04:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
public class NotificationChannels {
|
|
|
|
|
2018-08-16 09:47:43 -07:00
|
|
|
private static final String TAG = NotificationChannels.class.getSimpleName();
|
|
|
|
|
|
|
|
private static final int VERSION_MESSAGES_CATEGORY = 2;
|
|
|
|
|
|
|
|
private static final int VERSION = 2;
|
|
|
|
|
|
|
|
private static final String CATEGORY_MESSAGES = "messages";
|
|
|
|
private static final String CONTACT_PREFIX = "contact_";
|
|
|
|
private static final String MESSAGES_PREFIX = "messages_";
|
|
|
|
|
|
|
|
public static final String CALLS = "calls_v2";
|
|
|
|
public static final String FAILURES = "failures";
|
|
|
|
public static final String APP_UPDATES = "app_updates";
|
|
|
|
public static final String BACKUPS = "backups_v2";
|
|
|
|
public static final String LOCKED_STATUS = "locked_status_v2";
|
|
|
|
public static final String OTHER = "other_v2";
|
2018-08-06 12:20:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures all of the notification channels are created. No harm in repeat calls. Call is safely
|
|
|
|
* ignored for API < 26.
|
|
|
|
*/
|
|
|
|
public static void create(@NonNull Context context) {
|
2018-08-15 12:11:10 -07:00
|
|
|
if (!supported()) {
|
2018-08-06 12:20:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
NotificationManager notificationManager = getNotificationManager(context);
|
2018-08-16 09:47:43 -07:00
|
|
|
|
|
|
|
int oldVersion = TextSecurePreferences.getNotificationChannelVersion(context);
|
|
|
|
if (oldVersion != VERSION) {
|
|
|
|
onUpgrade(notificationManager, oldVersion, VERSION);
|
|
|
|
TextSecurePreferences.setNotificationChannelVersion(context, VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
onCreate(context, notificationManager);
|
|
|
|
}
|
|
|
|
|
2018-09-04 10:57:33 -07:00
|
|
|
/**
|
|
|
|
* Recreates all notification channels for contacts with custom notifications enabled. Should be
|
|
|
|
* safe to call repeatedly. Needs to be executed on a background thread.
|
|
|
|
*/
|
|
|
|
@WorkerThread
|
|
|
|
public static void restoreContactNotificationChannels(@NonNull Context context) {
|
|
|
|
if (!NotificationChannels.supported()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context);
|
|
|
|
|
|
|
|
try (RecipientDatabase.RecipientReader reader = db.getRecipientsWithNotificationChannels()) {
|
|
|
|
Recipient recipient;
|
|
|
|
while ((recipient = reader.getNext()) != null) {
|
|
|
|
NotificationChannels.createChannelFor(context, recipient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 09:47:43 -07:00
|
|
|
/**
|
2018-08-30 17:59:15 -07:00
|
|
|
* @return The channel ID for the default messages channel.
|
2018-08-16 09:47:43 -07:00
|
|
|
*/
|
|
|
|
public static @NonNull String getMessagesChannel(@NonNull Context context) {
|
|
|
|
return getMessagesChannelId(TextSecurePreferences.getNotificationMessagesChannelVersion(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Whether or not notification channels are supported.
|
|
|
|
*/
|
|
|
|
public static boolean supported() {
|
|
|
|
return Build.VERSION.SDK_INT >= 26;
|
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
/**
|
|
|
|
* @return A name suitable to be displayed as the notification channel title.
|
|
|
|
*/
|
2018-08-22 13:19:59 -07:00
|
|
|
public static @NonNull String getChannelDisplayNameFor(@NonNull Context context, @Nullable String systemName, @Nullable String profileName, @NonNull Address address) {
|
|
|
|
if (!TextUtils.isEmpty(systemName)) {
|
|
|
|
return systemName;
|
|
|
|
} else if (!TextUtils.isEmpty(profileName)) {
|
|
|
|
return profileName;
|
|
|
|
} else if (!TextUtils.isEmpty(address.serialize())) {
|
|
|
|
return address.serialize();
|
|
|
|
} else {
|
|
|
|
return context.getString(R.string.NotificationChannel_missing_display_name);
|
|
|
|
}
|
2018-08-16 09:47:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a channel for the specified recipient.
|
|
|
|
* @return The channel ID for the newly-created channel.
|
|
|
|
*/
|
|
|
|
public static String createChannelFor(@NonNull Context context, @NonNull Recipient recipient) {
|
|
|
|
VibrateState vibrateState = recipient.getMessageVibrate();
|
|
|
|
boolean vibrationEnabled = vibrateState == VibrateState.DEFAULT ? TextSecurePreferences.isNotificationVibrateEnabled(context) : vibrateState == VibrateState.ENABLED;
|
2018-08-30 17:59:15 -07:00
|
|
|
Uri messageRingtone = recipient.getMessageRingtone(context) != null ? recipient.getMessageRingtone(context) : getMessageRingtone(context);
|
2018-08-22 13:19:59 -07:00
|
|
|
String displayName = getChannelDisplayNameFor(context, recipient.getName(), recipient.getProfileName(), recipient.getAddress());
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
return createChannelFor(context, recipient.getAddress(), displayName, messageRingtone, vibrationEnabled);
|
2018-08-16 09:47:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* More verbose version of {@link #createChannelFor(Context, Recipient)}.
|
|
|
|
*/
|
2018-08-30 17:59:15 -07:00
|
|
|
public static @Nullable String createChannelFor(@NonNull Context context,
|
|
|
|
@NonNull Address address,
|
|
|
|
@NonNull String displayName,
|
|
|
|
@Nullable Uri messageSound,
|
|
|
|
boolean vibrationEnabled)
|
2018-08-16 09:47:43 -07:00
|
|
|
{
|
|
|
|
if (!supported()) {
|
2018-08-30 17:59:15 -07:00
|
|
|
return null;
|
2018-08-16 09:47:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
String channelId = generateChannelIdFor(address);
|
|
|
|
NotificationChannel channel = new NotificationChannel(channelId, displayName, NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
|
|
|
|
setLedPreference(channel, TextSecurePreferences.getNotificationLedColor(context));
|
|
|
|
channel.setGroup(CATEGORY_MESSAGES);
|
|
|
|
channel.enableVibration(vibrationEnabled);
|
2018-08-22 13:19:59 -07:00
|
|
|
|
|
|
|
if (messageSound != null) {
|
|
|
|
channel.setSound(messageSound, new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
|
|
|
|
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
|
|
|
|
.build());
|
|
|
|
}
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
NotificationManager notificationManager = getNotificationManager(context);
|
2018-08-16 09:47:43 -07:00
|
|
|
notificationManager.createNotificationChannel(channel);
|
|
|
|
|
|
|
|
return channelId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the channel generated for the provided recipient. Safe to call even if there was never
|
|
|
|
* a channel made for that recipient.
|
|
|
|
*/
|
|
|
|
public static void deleteChannelFor(@NonNull Context context, @NonNull Recipient recipient) {
|
|
|
|
if (!supported()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
NotificationManager notificationManager = getNotificationManager(context);
|
|
|
|
String channel = recipient.getNotificationChannel();
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
if (channel != null) {
|
|
|
|
Log.i(TAG, "Deleting channel");
|
2018-08-16 09:47:43 -07:00
|
|
|
notificationManager.deleteNotificationChannel(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigates the user to the system settings for the desired notification channel.
|
|
|
|
*/
|
|
|
|
public static void openChannelSettings(@NonNull Context context, @NonNull String channelId) {
|
|
|
|
if (!supported()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
|
|
|
|
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
|
|
|
|
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the LED color for message notifications and all contact-specific message notification
|
|
|
|
* channels. Performs database operations and should therefore be invoked on a background thread.
|
|
|
|
*/
|
|
|
|
@WorkerThread
|
|
|
|
public static void updateMessagesLedColor(@NonNull Context context, @NonNull String color) {
|
|
|
|
if (!supported()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-30 17:59:15 -07:00
|
|
|
Log.i(TAG, "Updating LED color.");
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
NotificationManager notificationManager = getNotificationManager(context);
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
updateMessageChannel(context, channel -> setLedPreference(channel, color));
|
2018-08-16 09:47:43 -07:00
|
|
|
updateAllRecipientChannelLedColors(context, notificationManager, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-30 17:59:15 -07:00
|
|
|
* @return The message ringtone set for the default message channel.
|
2018-08-16 09:47:43 -07:00
|
|
|
*/
|
2018-08-30 17:59:15 -07:00
|
|
|
public static @NonNull Uri getMessageRingtone(@NonNull Context context) {
|
|
|
|
if (!supported()) {
|
|
|
|
return Uri.EMPTY;
|
2018-08-16 09:47:43 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
Uri sound = getNotificationManager(context).getNotificationChannel(getMessagesChannel(context)).getSound();
|
|
|
|
return sound == null ? Uri.EMPTY : sound;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static @Nullable Uri getMessageRingtone(@NonNull Context context, @NonNull Recipient recipient) {
|
|
|
|
if (!supported() || recipient.getNotificationChannel() == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationManager notificationManager = getNotificationManager(context);
|
|
|
|
NotificationChannel channel = notificationManager.getNotificationChannel(recipient.getNotificationChannel());
|
|
|
|
|
|
|
|
if (!channelExists(channel)) {
|
|
|
|
Log.w(TAG, "Recipient had no channel. Returning null.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel.getSound();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the message ringtone for the default message channel.
|
|
|
|
*/
|
|
|
|
public static void updateMessageRingtone(@NonNull Context context, @Nullable Uri uri) {
|
|
|
|
if (!supported()) {
|
2018-08-16 09:47:43 -07:00
|
|
|
return;
|
|
|
|
}
|
2018-08-30 17:59:15 -07:00
|
|
|
Log.i(TAG, "Updating default message ringtone with URI: " + String.valueOf(uri));
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
updateMessageChannel(context, channel -> {
|
|
|
|
channel.setSound(uri == null ? Settings.System.DEFAULT_NOTIFICATION_URI : uri, getRingtoneAudioAttributes());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the message ringtone for a specific recipient. If that recipient has no channel, this
|
|
|
|
* does nothing.
|
|
|
|
*
|
|
|
|
* This has to update the database, and therefore should be run on a background thread.
|
|
|
|
*/
|
|
|
|
@WorkerThread
|
|
|
|
public static void updateMessageRingtone(@NonNull Context context, @NonNull Recipient recipient, @Nullable Uri uri) {
|
|
|
|
if (!supported() || recipient.getNotificationChannel() == null) {
|
2018-08-16 09:47:43 -07:00
|
|
|
return;
|
|
|
|
}
|
2018-08-30 17:59:15 -07:00
|
|
|
Log.i(TAG, "Updating recipient message ringtone with URI: " + String.valueOf(uri));
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-09-03 17:48:55 -07:00
|
|
|
String newChannelId = generateChannelIdFor(recipient.getAddress());
|
|
|
|
boolean success = updateExistingChannel(getNotificationManager(context),
|
|
|
|
recipient.getNotificationChannel(),
|
|
|
|
generateChannelIdFor(recipient.getAddress()),
|
|
|
|
channel -> channel.setSound(uri == null ? Settings.System.DEFAULT_NOTIFICATION_URI : uri, getRingtoneAudioAttributes()));
|
2018-08-30 17:59:15 -07:00
|
|
|
|
2018-09-03 17:48:55 -07:00
|
|
|
DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient, success ? newChannelId : null);
|
2018-08-16 09:47:43 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
/**
|
|
|
|
* @return The vibrate settings for the default message channel.
|
|
|
|
*/
|
|
|
|
public static boolean getMessageVibrate(@NonNull Context context) {
|
2018-08-22 11:58:41 -07:00
|
|
|
if (!supported()) {
|
2018-08-30 17:59:15 -07:00
|
|
|
return false;
|
2018-08-22 11:58:41 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
return getNotificationManager(context).getNotificationChannel(getMessagesChannel(context)).shouldVibrate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The vibrate setting for a specific recipient. If that recipient has no channel, this
|
|
|
|
* will return the setting for the default message channel.
|
|
|
|
*/
|
|
|
|
public static boolean getMessageVibrate(@NonNull Context context, @NonNull Recipient recipient) {
|
|
|
|
if (!supported()) {
|
|
|
|
return getMessageVibrate(context);
|
2018-08-22 11:58:41 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
NotificationManager notificationManager = getNotificationManager(context);
|
|
|
|
NotificationChannel channel = notificationManager.getNotificationChannel(recipient.getNotificationChannel());
|
|
|
|
|
|
|
|
if (!channelExists(channel)) {
|
|
|
|
Log.w(TAG, "Recipient didn't have a channel. Returning message default.");
|
|
|
|
return getMessageVibrate(context);
|
2018-08-22 11:58:41 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
return channel.shouldVibrate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the vibrate property for the default message channel.
|
|
|
|
*/
|
|
|
|
public static void updateMessageVibrate(@NonNull Context context, boolean enabled) {
|
|
|
|
if (!supported()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log.i(TAG, "Updating default vibrate with value: " + enabled);
|
|
|
|
|
|
|
|
updateMessageChannel(context, channel -> channel.enableVibration(enabled));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the message ringtone for a specific recipient. If that recipient has no channel, this
|
|
|
|
* does nothing.
|
|
|
|
*
|
|
|
|
* This has to update the database and should therefore be run on a background thread.
|
|
|
|
*/
|
|
|
|
@WorkerThread
|
|
|
|
public static void updateMessageVibrate(@NonNull Context context, @NonNull Recipient recipient, VibrateState vibrateState) {
|
|
|
|
if (!supported() || recipient.getNotificationChannel() == null) {
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
Log.i(TAG, "Updating recipient vibrate with value: " + vibrateState);
|
|
|
|
|
|
|
|
boolean enabled = vibrateState == VibrateState.DEFAULT ? getMessageVibrate(context) : vibrateState == VibrateState.ENABLED;
|
|
|
|
String newChannelId = generateChannelIdFor(recipient.getAddress());
|
2018-09-03 17:48:55 -07:00
|
|
|
boolean success = updateExistingChannel(getNotificationManager(context),
|
|
|
|
recipient.getNotificationChannel(),
|
|
|
|
newChannelId,
|
|
|
|
channel -> channel.enableVibration(enabled));
|
2018-08-30 17:59:15 -07:00
|
|
|
|
2018-09-03 17:48:55 -07:00
|
|
|
DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient, success ? newChannelId : null);
|
2018-08-30 17:59:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the name of an existing channel to match the recipient's current name. Will have no
|
|
|
|
* effect if the recipient doesn't have an existing valid channel.
|
|
|
|
*/
|
|
|
|
public static void updateContactChannelName(@NonNull Context context, @NonNull Recipient recipient) {
|
|
|
|
if (!supported() || recipient.getNotificationChannel() == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log.i(TAG, "Updating contact channel name");
|
|
|
|
|
|
|
|
NotificationManager notificationManager = getNotificationManager(context);
|
|
|
|
|
|
|
|
if (notificationManager.getNotificationChannel(recipient.getNotificationChannel()) == null) {
|
|
|
|
Log.w(TAG, "Tried to update the name of a channel, but that channel doesn't exist.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationChannel channel = new NotificationChannel(recipient.getNotificationChannel(),
|
|
|
|
getChannelDisplayNameFor(context, recipient.getName(), recipient.getProfileName(), recipient.getAddress()),
|
|
|
|
NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
channel.setGroup(CATEGORY_MESSAGES);
|
|
|
|
notificationManager.createNotificationChannel(channel);
|
2018-08-22 11:58:41 -07:00
|
|
|
}
|
|
|
|
|
2018-08-16 09:47:43 -07:00
|
|
|
@TargetApi(26)
|
|
|
|
private static void onCreate(@NonNull Context context, @NonNull NotificationManager notificationManager) {
|
|
|
|
NotificationChannelGroup messagesGroup = new NotificationChannelGroup(CATEGORY_MESSAGES, context.getResources().getString(R.string.NotificationChannel_group_messages));
|
|
|
|
notificationManager.createNotificationChannelGroup(messagesGroup);
|
|
|
|
|
|
|
|
NotificationChannel messages = new NotificationChannel(getMessagesChannel(context), context.getString(R.string.NotificationChannel_messages), NotificationManager.IMPORTANCE_HIGH);
|
2018-08-06 12:20:24 -04:00
|
|
|
NotificationChannel calls = new NotificationChannel(CALLS, context.getString(R.string.NotificationChannel_calls), NotificationManager.IMPORTANCE_LOW);
|
|
|
|
NotificationChannel failures = new NotificationChannel(FAILURES, context.getString(R.string.NotificationChannel_failures), NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
NotificationChannel backups = new NotificationChannel(BACKUPS, context.getString(R.string.NotificationChannel_backups), NotificationManager.IMPORTANCE_LOW);
|
|
|
|
NotificationChannel lockedStatus = new NotificationChannel(LOCKED_STATUS, context.getString(R.string.NotificationChannel_locked_status), NotificationManager.IMPORTANCE_LOW);
|
|
|
|
NotificationChannel other = new NotificationChannel(OTHER, context.getString(R.string.NotificationChannel_other), NotificationManager.IMPORTANCE_LOW);
|
|
|
|
|
2018-08-16 09:47:43 -07:00
|
|
|
messages.setGroup(CATEGORY_MESSAGES);
|
|
|
|
messages.enableVibration(TextSecurePreferences.isNotificationVibrateEnabled(context));
|
2018-08-30 17:59:15 -07:00
|
|
|
messages.setSound(TextSecurePreferences.getNotificationRingtone(context), getRingtoneAudioAttributes());
|
2018-08-16 09:47:43 -07:00
|
|
|
setLedPreference(messages, TextSecurePreferences.getNotificationLedColor(context));
|
|
|
|
|
|
|
|
calls.setShowBadge(false);
|
|
|
|
backups.setShowBadge(false);
|
|
|
|
lockedStatus.setShowBadge(false);
|
|
|
|
other.setShowBadge(false);
|
|
|
|
|
2018-08-06 12:20:24 -04:00
|
|
|
notificationManager.createNotificationChannels(Arrays.asList(messages, calls, failures, backups, lockedStatus, other));
|
|
|
|
|
|
|
|
if (BuildConfig.PLAY_STORE_DISABLED) {
|
|
|
|
NotificationChannel appUpdates = new NotificationChannel(APP_UPDATES, context.getString(R.string.NotificationChannel_app_updates), NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
notificationManager.createNotificationChannel(appUpdates);
|
|
|
|
} else {
|
|
|
|
notificationManager.deleteNotificationChannel(APP_UPDATES);
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 12:11:10 -07:00
|
|
|
|
2018-08-16 09:47:43 -07:00
|
|
|
@TargetApi(26)
|
|
|
|
private static void onUpgrade(@NonNull NotificationManager notificationManager, int oldVersion, int newVersion) {
|
|
|
|
Log.i(TAG, "Upgrading channels from " + oldVersion + " to " + newVersion);
|
|
|
|
|
|
|
|
if (oldVersion < VERSION_MESSAGES_CATEGORY) {
|
|
|
|
notificationManager.deleteNotificationChannel("messages");
|
|
|
|
notificationManager.deleteNotificationChannel("calls");
|
|
|
|
notificationManager.deleteNotificationChannel("locked_status");
|
|
|
|
notificationManager.deleteNotificationChannel("backups");
|
|
|
|
notificationManager.deleteNotificationChannel("other");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(26)
|
|
|
|
private static void setLedPreference(@NonNull NotificationChannel channel, @NonNull String ledColor) {
|
|
|
|
if ("none".equals(ledColor)) {
|
|
|
|
channel.enableLights(false);
|
|
|
|
} else {
|
|
|
|
channel.enableLights(true);
|
|
|
|
channel.setLightColor(Color.parseColor(ledColor));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static @NonNull String generateChannelIdFor(@NonNull Address address) {
|
|
|
|
return CONTACT_PREFIX + address.serialize() + "_" + System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(26)
|
|
|
|
private static @NonNull NotificationChannel copyChannel(@NonNull NotificationChannel original, @NonNull String id) {
|
|
|
|
NotificationChannel copy = new NotificationChannel(id, original.getName(), original.getImportance());
|
|
|
|
|
|
|
|
copy.setGroup(original.getGroup());
|
|
|
|
copy.setSound(original.getSound(), original.getAudioAttributes());
|
|
|
|
copy.setBypassDnd(original.canBypassDnd());
|
|
|
|
copy.enableVibration(original.shouldVibrate());
|
|
|
|
copy.setVibrationPattern(original.getVibrationPattern());
|
|
|
|
copy.setLockscreenVisibility(original.getLockscreenVisibility());
|
|
|
|
copy.setShowBadge(original.canShowBadge());
|
|
|
|
copy.setLightColor(original.getLightColor());
|
|
|
|
copy.enableLights(original.shouldShowLights());
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getMessagesChannelId(int version) {
|
|
|
|
return MESSAGES_PREFIX + version;
|
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
@WorkerThread
|
2018-08-16 09:47:43 -07:00
|
|
|
@TargetApi(26)
|
2018-08-30 17:59:15 -07:00
|
|
|
private static void updateAllRecipientChannelLedColors(@NonNull Context context, @NonNull NotificationManager notificationManager, @NonNull String color) {
|
|
|
|
RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context);
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
try (RecipientDatabase.RecipientReader recipients = database.getRecipientsWithNotificationChannels()) {
|
|
|
|
Recipient recipient;
|
|
|
|
while ((recipient = recipients.getNext()) != null) {
|
|
|
|
assert recipient.getNotificationChannel() != null;
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-09-03 17:48:55 -07:00
|
|
|
String newChannelId = generateChannelIdFor(recipient.getAddress());
|
|
|
|
boolean success = updateExistingChannel(notificationManager, recipient.getNotificationChannel(), newChannelId, channel -> setLedPreference(channel, color));
|
|
|
|
|
|
|
|
database.setNotificationChannel(recipient, success ? newChannelId : null);
|
2018-08-30 17:59:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
@TargetApi(26)
|
|
|
|
private static void updateMessageChannel(@NonNull Context context, @NonNull ChannelUpdater updater) {
|
|
|
|
NotificationManager notificationManager = getNotificationManager(context);
|
|
|
|
int existingVersion = TextSecurePreferences.getNotificationMessagesChannelVersion(context);
|
|
|
|
int newVersion = existingVersion + 1;
|
|
|
|
|
|
|
|
Log.i(TAG, "Updating message channel from version " + existingVersion + " to " + newVersion);
|
2018-09-03 17:48:55 -07:00
|
|
|
if (updateExistingChannel(notificationManager, getMessagesChannelId(existingVersion), getMessagesChannelId(newVersion), updater)) {
|
|
|
|
TextSecurePreferences.setNotificationMessagesChannelVersion(context, newVersion);
|
|
|
|
} else {
|
|
|
|
onCreate(context, notificationManager);
|
|
|
|
}
|
2018-08-16 09:47:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(26)
|
2018-09-03 17:48:55 -07:00
|
|
|
private static boolean updateExistingChannel(@NonNull NotificationManager notificationManager,
|
|
|
|
@NonNull String channelId,
|
|
|
|
@NonNull String newChannelId,
|
|
|
|
@NonNull ChannelUpdater updater)
|
2018-08-30 17:59:15 -07:00
|
|
|
{
|
|
|
|
NotificationChannel existingChannel = notificationManager.getNotificationChannel(channelId);
|
2018-09-03 17:48:55 -07:00
|
|
|
if (existingChannel == null) {
|
|
|
|
Log.w(TAG, "Tried to update a channel, but it didn't exist.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
notificationManager.deleteNotificationChannel(existingChannel.getId());
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
NotificationChannel newChannel = copyChannel(existingChannel, newChannelId);
|
|
|
|
updater.update(newChannel);
|
|
|
|
notificationManager.createNotificationChannel(newChannel);
|
2018-09-03 17:48:55 -07:00
|
|
|
return true;
|
2018-08-30 17:59:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(21)
|
|
|
|
private static AudioAttributes getRingtoneAudioAttributes() {
|
|
|
|
return new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
|
|
|
|
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
|
|
|
|
.build();
|
|
|
|
}
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
@TargetApi(26)
|
|
|
|
private static @NonNull NotificationManager getNotificationManager(@NonNull Context context) {
|
|
|
|
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
|
|
|
|
assert notificationManager != null;
|
|
|
|
return notificationManager;
|
|
|
|
}
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
@TargetApi(26)
|
|
|
|
private static boolean channelExists(@Nullable NotificationChannel channel) {
|
|
|
|
return channel != null && !NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel);
|
|
|
|
}
|
2018-08-16 09:47:43 -07:00
|
|
|
|
2018-08-30 17:59:15 -07:00
|
|
|
private interface ChannelUpdater {
|
|
|
|
@TargetApi(26)
|
|
|
|
void update(@NonNull NotificationChannel channel);
|
2018-08-15 12:11:10 -07:00
|
|
|
}
|
2018-08-06 12:20:24 -04:00
|
|
|
}
|