Skip to content

Commit

Permalink
Migrate notification channels to recipientId's.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Oct 17, 2019
1 parent 7d02bb8 commit b0b8377
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package org.thoughtcrime.securesms.database.helpers;


import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.text.TextUtils;

import com.annimon.stream.Stream;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteDatabaseHook;
import net.sqlcipher.database.SQLiteOpenHelper;

import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.crypto.DatabaseSecret;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.AttachmentDatabase;
import org.thoughtcrime.securesms.database.DraftDatabase;
import org.thoughtcrime.securesms.database.GroupDatabase;
Expand All @@ -41,9 +44,11 @@
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter;
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.ServiceUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;

import java.io.File;
import java.util.List;

public class SQLCipherOpenHelper extends SQLiteOpenHelper {

Expand Down Expand Up @@ -77,8 +82,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
private static final int RECIPIENT_CLEANUP = 26;
private static final int MMS_RECIPIENT_CLEANUP = 27;
private static final int ATTACHMENT_HASHING = 28;
private static final int NOTIFICATION_RECIPIENT_IDS = 29;

private static final int DATABASE_VERSION = 28;
private static final int DATABASE_VERSION = 29;
private static final String DATABASE_NAME = "signal.db";

private final Context context;
Expand Down Expand Up @@ -303,7 +309,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String messageSound = cursor.getString(cursor.getColumnIndexOrThrow("notification"));
Uri messageSoundUri = messageSound != null ? Uri.parse(messageSound) : null;
int vibrateState = cursor.getInt(cursor.getColumnIndexOrThrow("vibrate"));
String displayName = NotificationChannels.getChannelDisplayNameFor(context, systemName, profileName, Address.fromSerialized(address));
String displayName = NotificationChannels.getChannelDisplayNameFor(context, systemName, profileName, address);
boolean vibrateEnabled = vibrateState == 0 ? TextSecurePreferences.isNotificationVibrateEnabled(context) : vibrateState == 1;

if (GroupUtil.isEncodedGroup(address)) {
Expand All @@ -318,7 +324,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

String channelId = NotificationChannels.createChannelFor(context, Address.fromSerialized(address), displayName, messageSoundUri, vibrateEnabled);
String channelId = NotificationChannels.createChannelFor(context, "contact_" + address + "_" + System.currentTimeMillis(), displayName, messageSoundUri, vibrateEnabled);

ContentValues values = new ContentValues(1);
values.put("notification_channel", channelId);
Expand Down Expand Up @@ -528,6 +534,55 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("CREATE INDEX IF NOT EXISTS part_data_hash_index ON part (data_hash)");
}

if (oldVersion < NOTIFICATION_RECIPIENT_IDS && Build.VERSION.SDK_INT >= 26) {
NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
List<NotificationChannel> channels = Stream.of(notificationManager.getNotificationChannels())
.filter(c -> c.getId().startsWith("contact_"))
.toList();

Log.i(TAG, "Migrating " + channels.size() + " channels to use RecipientId's.");

for (NotificationChannel oldChannel : channels) {
notificationManager.deleteNotificationChannel(oldChannel.getId());

int startIndex = "contact_".length();
int endIndex = oldChannel.getId().lastIndexOf("_");
String address = oldChannel.getId().substring(startIndex, endIndex);

String recipientId;

try (Cursor cursor = db.query("recipient", new String[] { "_id" }, "phone = ? OR email = ? OR group_id = ?", new String[] { address, address, address}, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
recipientId = cursor.getString(0);
} else {
Log.w(TAG, "Couldn't find recipient for address: " + address);
continue;
}
}

String newId = "contact_" + recipientId + "_" + System.currentTimeMillis();
NotificationChannel newChannel = new NotificationChannel(newId, oldChannel.getName(), oldChannel.getImportance());

Log.i(TAG, "Updating channel ID from '" + oldChannel.getId() + "' to '" + newChannel.getId() + "'.");

newChannel.setGroup(oldChannel.getGroup());
newChannel.setSound(oldChannel.getSound(), oldChannel.getAudioAttributes());
newChannel.setBypassDnd(oldChannel.canBypassDnd());
newChannel.enableVibration(oldChannel.shouldVibrate());
newChannel.setVibrationPattern(oldChannel.getVibrationPattern());
newChannel.setLockscreenVisibility(oldChannel.getLockscreenVisibility());
newChannel.setShowBadge(oldChannel.canShowBadge());
newChannel.setLightColor(oldChannel.getLightColor());
newChannel.enableLights(oldChannel.shouldShowLights());

notificationManager.createNotificationChannel(newChannel);

ContentValues contentValues = new ContentValues(1);
contentValues.put("notification_channel", newChannel.getId());
db.update("recipient", contentValues, "_id = ?", new String[] { recipientId });
}
}

db.setTransactionSuccessful();
} finally {
db.endTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.R;
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;
Expand All @@ -41,8 +40,10 @@ public class NotificationChannels {

private static final String TAG = NotificationChannels.class.getSimpleName();

private static final int VERSION_MESSAGES_CATEGORY = 2;
private static final int VERSION_CALLS_PRIORITY_BUMP = 3;
private static class Version {
static final int MESSAGES_CATEGORY = 2;
static final int CALLS_PRIORITY_BUMP = 3;
}

private static final int VERSION = 3;

Expand Down Expand Up @@ -124,13 +125,13 @@ public static boolean supported() {
/**
* @return A name suitable to be displayed as the notification channel title.
*/
public static @NonNull String getChannelDisplayNameFor(@NonNull Context context, @Nullable String systemName, @Nullable String profileName, @NonNull Address address) {
public static @NonNull String getChannelDisplayNameFor(@NonNull Context context, @Nullable String systemName, @Nullable String profileName, @NonNull String address) {
if (!TextUtils.isEmpty(systemName)) {
return systemName;
} else if (!TextUtils.isEmpty(profileName)) {
return profileName;
} else if (!TextUtils.isEmpty(address.serialize())) {
return address.serialize();
} else if (!TextUtils.isEmpty(address)) {
return address;
} else {
return context.getString(R.string.NotificationChannel_missing_display_name);
}
Expand All @@ -146,16 +147,16 @@ public static boolean supported() {
VibrateState vibrateState = recipient.getMessageVibrate();
boolean vibrationEnabled = vibrateState == VibrateState.DEFAULT ? TextSecurePreferences.isNotificationVibrateEnabled(context) : vibrateState == VibrateState.ENABLED;
Uri messageRingtone = recipient.getMessageRingtone() != null ? recipient.getMessageRingtone() : getMessageRingtone(context);
String displayName = getChannelDisplayNameFor(context, recipient.getName(), recipient.getProfileName(), recipient.requireAddress());
String displayName = getChannelDisplayNameFor(context, recipient.getName(), recipient.getProfileName(), recipient.requireAddress().serialize());

return createChannelFor(context, recipient.requireAddress(), displayName, messageRingtone, vibrationEnabled);
return createChannelFor(context, generateChannelIdFor(recipient), displayName, messageRingtone, vibrationEnabled);
}

/**
* More verbose version of {@link #createChannelFor(Context, Recipient)}.
*/
public static synchronized @Nullable String createChannelFor(@NonNull Context context,
@NonNull Address address,
@NonNull String channelId,
@NonNull String displayName,
@Nullable Uri messageSound,
boolean vibrationEnabled)
Expand All @@ -164,7 +165,6 @@ public static boolean supported() {
return null;
}

String channelId = generateChannelIdFor(address);
NotificationChannel channel = new NotificationChannel(channelId, displayName, NotificationManager.IMPORTANCE_HIGH);

setLedPreference(channel, TextSecurePreferences.getNotificationLedColor(context));
Expand Down Expand Up @@ -289,10 +289,10 @@ public static synchronized void updateMessageRingtone(@NonNull Context context,
}
Log.i(TAG, "Updating recipient message ringtone with URI: " + String.valueOf(uri));

String newChannelId = generateChannelIdFor(recipient.requireAddress());
String newChannelId = generateChannelIdFor(recipient);
boolean success = updateExistingChannel(ServiceUtil.getNotificationManager(context),
recipient.getNotificationChannel(),
generateChannelIdFor(recipient.requireAddress()),
generateChannelIdFor(recipient),
channel -> channel.setSound(uri == null ? Settings.System.DEFAULT_NOTIFICATION_URI : uri, getRingtoneAudioAttributes()));

DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient.getId(), success ? newChannelId : null);
Expand Down Expand Up @@ -356,7 +356,7 @@ public static synchronized void updateMessageVibrate(@NonNull Context context, @
Log.i(TAG, "Updating recipient vibrate with value: " + vibrateState);

boolean enabled = vibrateState == VibrateState.DEFAULT ? getMessageVibrate(context) : vibrateState == VibrateState.ENABLED;
String newChannelId = generateChannelIdFor(recipient.requireAddress());
String newChannelId = generateChannelIdFor(recipient);
boolean success = updateExistingChannel(ServiceUtil.getNotificationManager(context),
recipient.getNotificationChannel(),
newChannelId,
Expand Down Expand Up @@ -384,7 +384,7 @@ public static synchronized void updateContactChannelName(@NonNull Context contex
}

NotificationChannel channel = new NotificationChannel(recipient.getNotificationChannel(),
getChannelDisplayNameFor(context, recipient.getName(), recipient.getProfileName(), recipient.requireAddress()),
getChannelDisplayNameFor(context, recipient.getName(), recipient.getProfileName(), recipient.requireAddress().serialize()),
NotificationManager.IMPORTANCE_HIGH);
channel.setGroup(CATEGORY_MESSAGES);
notificationManager.createNotificationChannel(channel);
Expand All @@ -393,11 +393,12 @@ public static synchronized void updateContactChannelName(@NonNull Context contex
@TargetApi(26)
@WorkerThread
public static synchronized void ensureCustomChannelConsistency(@NonNull Context context) {
Log.d(TAG, "ensureCustomChannelConsistency()");

NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context);
List<Recipient> customRecipients = new ArrayList<>();
Set<String> customChannelIds = new HashSet<>();
Set<String> existingChannelIds = Stream.of(notificationManager.getNotificationChannels()).map(NotificationChannel::getId).collect(Collectors.toSet());

try (RecipientDatabase.RecipientReader reader = db.getRecipientsWithNotificationChannels()) {
Recipient recipient;
Expand All @@ -407,6 +408,8 @@ public static synchronized void ensureCustomChannelConsistency(@NonNull Context
}
}

Set<String> existingChannelIds = Stream.of(notificationManager.getNotificationChannels()).map(NotificationChannel::getId).collect(Collectors.toSet());

for (NotificationChannel existingChannel : notificationManager.getNotificationChannels()) {
if (existingChannel.getId().startsWith(CONTACT_PREFIX) && !customChannelIds.contains(existingChannel.getId())) {
Log.i(TAG, "Consistency: Deleting channel '"+ existingChannel.getId() + "' because the DB has no record of it.");
Expand Down Expand Up @@ -461,15 +464,15 @@ private static void onCreate(@NonNull Context context, @NonNull NotificationMana
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) {
if (oldVersion < Version.MESSAGES_CATEGORY) {
notificationManager.deleteNotificationChannel("messages");
notificationManager.deleteNotificationChannel("calls");
notificationManager.deleteNotificationChannel("locked_status");
notificationManager.deleteNotificationChannel("backups");
notificationManager.deleteNotificationChannel("other");
}

if (oldVersion < VERSION_CALLS_PRIORITY_BUMP) {
if (oldVersion < Version.CALLS_PRIORITY_BUMP) {
notificationManager.deleteNotificationChannel("calls_v2");
}
}
Expand All @@ -485,8 +488,8 @@ private static void setLedPreference(@NonNull NotificationChannel channel, @NonN
}


private static @NonNull String generateChannelIdFor(@NonNull Address address) {
return CONTACT_PREFIX + address.serialize() + "_" + System.currentTimeMillis();
private static @NonNull String generateChannelIdFor(@NonNull Recipient recipient) {
return CONTACT_PREFIX + recipient.getId().serialize() + "_" + System.currentTimeMillis();
}

@TargetApi(26)
Expand Down Expand Up @@ -520,7 +523,7 @@ private static void updateAllRecipientChannelLedColors(@NonNull Context context,
while ((recipient = recipients.getNext()) != null) {
assert recipient.getNotificationChannel() != null;

String newChannelId = generateChannelIdFor(recipient.requireAddress());
String newChannelId = generateChannelIdFor(recipient);
boolean success = updateExistingChannel(notificationManager, recipient.getNotificationChannel(), newChannelId, channel -> setLedPreference(channel, color));

database.setNotificationChannel(recipient.getId(), success ? newChannelId : null);
Expand Down

0 comments on commit b0b8377

Please sign in to comment.