Skip to content

Commit

Permalink
Fixed NPE during channel update.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Sep 4, 2018
1 parent 5cc9127 commit d9ba696
Showing 1 changed file with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,13 @@ public static void updateMessageRingtone(@NonNull Context context, @NonNull Reci
}
Log.i(TAG, "Updating recipient message ringtone with URI: " + String.valueOf(uri));

String newChannelId = generateChannelIdFor(recipient.getAddress());

updateExistingChannel(getNotificationManager(context),
recipient.getNotificationChannel(),
generateChannelIdFor(recipient.getAddress()),
channel -> channel.setSound(uri == null ? Settings.System.DEFAULT_NOTIFICATION_URI : uri, getRingtoneAudioAttributes()));
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()));

DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient, newChannelId);
DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient, success ? newChannelId : null);
}

/**
Expand Down Expand Up @@ -313,13 +312,12 @@ public static void updateMessageVibrate(@NonNull Context context, @NonNull Recip

boolean enabled = vibrateState == VibrateState.DEFAULT ? getMessageVibrate(context) : vibrateState == VibrateState.ENABLED;
String newChannelId = generateChannelIdFor(recipient.getAddress());
boolean success = updateExistingChannel(getNotificationManager(context),
recipient.getNotificationChannel(),
newChannelId,
channel -> channel.enableVibration(enabled));

updateExistingChannel(getNotificationManager(context),
recipient.getNotificationChannel(),
newChannelId,
channel -> channel.enableVibration(enabled));

DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient, newChannelId);
DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient, success ? newChannelId : null);
}

/**
Expand Down Expand Up @@ -437,9 +435,10 @@ private static void updateAllRecipientChannelLedColors(@NonNull Context context,
while ((recipient = recipients.getNext()) != null) {
assert recipient.getNotificationChannel() != null;

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

database.setNotificationChannel(recipient, success ? newChannelId : null);
}
}
}
Expand All @@ -451,23 +450,31 @@ private static void updateMessageChannel(@NonNull Context context, @NonNull Chan
int newVersion = existingVersion + 1;

Log.i(TAG, "Updating message channel from version " + existingVersion + " to " + newVersion);
updateExistingChannel(notificationManager, getMessagesChannelId(existingVersion), getMessagesChannelId(newVersion), updater);

TextSecurePreferences.setNotificationMessagesChannelVersion(context, newVersion);
if (updateExistingChannel(notificationManager, getMessagesChannelId(existingVersion), getMessagesChannelId(newVersion), updater)) {
TextSecurePreferences.setNotificationMessagesChannelVersion(context, newVersion);
} else {
onCreate(context, notificationManager);
}
}

@TargetApi(26)
private static void updateExistingChannel(@NonNull NotificationManager notificationManager,
@NonNull String channelId,
@NonNull String newChannelId,
@NonNull ChannelUpdater updater)
private static boolean updateExistingChannel(@NonNull NotificationManager notificationManager,
@NonNull String channelId,
@NonNull String newChannelId,
@NonNull ChannelUpdater updater)
{
NotificationChannel existingChannel = notificationManager.getNotificationChannel(channelId);
if (existingChannel == null) {
Log.w(TAG, "Tried to update a channel, but it didn't exist.");
return false;
}

notificationManager.deleteNotificationChannel(existingChannel.getId());

NotificationChannel newChannel = copyChannel(existingChannel, newChannelId);
updater.update(newChannel);
notificationManager.createNotificationChannel(newChannel);
return true;
}

@TargetApi(21)
Expand Down

0 comments on commit d9ba696

Please sign in to comment.