Fixed NPE during channel update.

This commit is contained in:
Greyson Parrelli 2018-09-03 17:48:55 -07:00
parent 5cc91274d1
commit d9ba6962c7

View File

@ -246,13 +246,12 @@ public class NotificationChannels {
Log.i(TAG, "Updating recipient message ringtone with URI: " + String.valueOf(uri)); Log.i(TAG, "Updating recipient message ringtone with URI: " + String.valueOf(uri));
String newChannelId = generateChannelIdFor(recipient.getAddress()); String newChannelId = generateChannelIdFor(recipient.getAddress());
boolean success = updateExistingChannel(getNotificationManager(context),
updateExistingChannel(getNotificationManager(context),
recipient.getNotificationChannel(), recipient.getNotificationChannel(),
generateChannelIdFor(recipient.getAddress()), generateChannelIdFor(recipient.getAddress()),
channel -> channel.setSound(uri == null ? Settings.System.DEFAULT_NOTIFICATION_URI : uri, getRingtoneAudioAttributes())); 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);
} }
/** /**
@ -313,13 +312,12 @@ public class NotificationChannels {
boolean enabled = vibrateState == VibrateState.DEFAULT ? getMessageVibrate(context) : vibrateState == VibrateState.ENABLED; boolean enabled = vibrateState == VibrateState.DEFAULT ? getMessageVibrate(context) : vibrateState == VibrateState.ENABLED;
String newChannelId = generateChannelIdFor(recipient.getAddress()); String newChannelId = generateChannelIdFor(recipient.getAddress());
boolean success = updateExistingChannel(getNotificationManager(context),
updateExistingChannel(getNotificationManager(context),
recipient.getNotificationChannel(), recipient.getNotificationChannel(),
newChannelId, newChannelId,
channel -> channel.enableVibration(enabled)); channel -> channel.enableVibration(enabled));
DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient, newChannelId); DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient, success ? newChannelId : null);
} }
/** /**
@ -438,8 +436,9 @@ public class NotificationChannels {
assert recipient.getNotificationChannel() != null; assert recipient.getNotificationChannel() != null;
String newChannelId = generateChannelIdFor(recipient.getAddress()); String newChannelId = generateChannelIdFor(recipient.getAddress());
updateExistingChannel(notificationManager, recipient.getNotificationChannel(), newChannelId, channel -> setLedPreference(channel, color)); boolean success = updateExistingChannel(notificationManager, recipient.getNotificationChannel(), newChannelId, channel -> setLedPreference(channel, color));
database.setNotificationChannel(recipient, newChannelId);
database.setNotificationChannel(recipient, success ? newChannelId : null);
} }
} }
} }
@ -451,23 +450,31 @@ public class NotificationChannels {
int newVersion = existingVersion + 1; int newVersion = existingVersion + 1;
Log.i(TAG, "Updating message channel from version " + existingVersion + " to " + newVersion); Log.i(TAG, "Updating message channel from version " + existingVersion + " to " + newVersion);
updateExistingChannel(notificationManager, getMessagesChannelId(existingVersion), getMessagesChannelId(newVersion), updater); if (updateExistingChannel(notificationManager, getMessagesChannelId(existingVersion), getMessagesChannelId(newVersion), updater)) {
TextSecurePreferences.setNotificationMessagesChannelVersion(context, newVersion); TextSecurePreferences.setNotificationMessagesChannelVersion(context, newVersion);
} else {
onCreate(context, notificationManager);
}
} }
@TargetApi(26) @TargetApi(26)
private static void updateExistingChannel(@NonNull NotificationManager notificationManager, private static boolean updateExistingChannel(@NonNull NotificationManager notificationManager,
@NonNull String channelId, @NonNull String channelId,
@NonNull String newChannelId, @NonNull String newChannelId,
@NonNull ChannelUpdater updater) @NonNull ChannelUpdater updater)
{ {
NotificationChannel existingChannel = notificationManager.getNotificationChannel(channelId); 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()); notificationManager.deleteNotificationChannel(existingChannel.getId());
NotificationChannel newChannel = copyChannel(existingChannel, newChannelId); NotificationChannel newChannel = copyChannel(existingChannel, newChannelId);
updater.update(newChannel); updater.update(newChannel);
notificationManager.createNotificationChannel(newChannel); notificationManager.createNotificationChannel(newChannel);
return true;
} }
@TargetApi(21) @TargetApi(21)