Added safeguards during NotificationChannel creation.

We were getting an IllegalArgumentException during channel creation on
some Samsung phones. Stack trace didn't give me much more than that, so
just adding in some additional safeguards that make sense based on
reading AOSP.
This commit is contained in:
Greyson Parrelli 2018-08-22 13:19:59 -07:00
parent f1efe2b589
commit ca2efcac8a
3 changed files with 21 additions and 8 deletions

View File

@ -674,6 +674,7 @@
<string name="NotificationChannel_app_updates">App updates</string> <string name="NotificationChannel_app_updates">App updates</string>
<string name="NotificationChannel_other">Other</string> <string name="NotificationChannel_other">Other</string>
<string name="NotificationChannel_group_messages">Messages</string> <string name="NotificationChannel_group_messages">Messages</string>
<string name="NotificationChannel_missing_display_name">Unknown</string>
<!-- QuickResponseService --> <!-- QuickResponseService -->
<string name="QuickResponseService_quick_response_unavailable_when_Signal_is_locked">Quick response unavailable when Signal is locked!</string> <string name="QuickResponseService_quick_response_unavailable_when_Signal_is_locked">Quick response unavailable when Signal is locked!</string>

View File

@ -247,6 +247,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
if (oldVersion < NOTIFICATION_CHANNELS) { if (oldVersion < NOTIFICATION_CHANNELS) {
db.execSQL("ALTER TABLE recipient_preferences ADD COLUMN notification_channel TEXT DEFAULT NULL"); db.execSQL("ALTER TABLE recipient_preferences ADD COLUMN notification_channel TEXT DEFAULT NULL");
NotificationChannels.create(context);
try (Cursor cursor = db.rawQuery("SELECT recipient_ids, system_display_name, signal_profile_name, notification, vibrate FROM recipient_preferences WHERE notification NOT NULL OR vibrate != 0", null)) { try (Cursor cursor = db.rawQuery("SELECT recipient_ids, system_display_name, signal_profile_name, notification, vibrate FROM recipient_preferences WHERE notification NOT NULL OR vibrate != 0", null)) {
while (cursor != null && cursor.moveToNext()) { while (cursor != null && cursor.moveToNext()) {
@ -257,7 +258,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
String messageSound = cursor.getString(cursor.getColumnIndexOrThrow("notification")); String messageSound = cursor.getString(cursor.getColumnIndexOrThrow("notification"));
Uri messageSoundUri = messageSound != null ? Uri.parse(messageSound) : null; Uri messageSoundUri = messageSound != null ? Uri.parse(messageSound) : null;
int vibrateState = cursor.getInt(cursor.getColumnIndexOrThrow("vibrate")); int vibrateState = cursor.getInt(cursor.getColumnIndexOrThrow("vibrate"));
String displayName = NotificationChannels.getChannelDisplayNameFor(systemName, profileName, address); String displayName = NotificationChannels.getChannelDisplayNameFor(context, systemName, profileName, address);
boolean vibrateEnabled = vibrateState == 0 ? TextSecurePreferences.isNotificationVibrateEnabled(context) : vibrateState == 1; boolean vibrateEnabled = vibrateState == 0 ? TextSecurePreferences.isNotificationVibrateEnabled(context) : vibrateState == 1;
String channelId = NotificationChannels.createChannelFor(context, address, displayName, messageSoundUri, vibrateEnabled); String channelId = NotificationChannels.createChannelFor(context, address, displayName, messageSoundUri, vibrateEnabled);

View File

@ -86,8 +86,16 @@ public class NotificationChannels {
return Build.VERSION.SDK_INT >= 26; return Build.VERSION.SDK_INT >= 26;
} }
public static String getChannelDisplayNameFor(@Nullable String systemName, @Nullable String profileName, @NonNull Address address) { public static @NonNull String getChannelDisplayNameFor(@NonNull Context context, @Nullable String systemName, @Nullable String profileName, @NonNull Address address) {
return TextUtils.isEmpty(systemName) ? (TextUtils.isEmpty(profileName) ? address.serialize() : profileName) : systemName; 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);
}
} }
/** /**
@ -97,7 +105,7 @@ public class NotificationChannels {
public static String createChannelFor(@NonNull Context context, @NonNull Recipient recipient) { public static String createChannelFor(@NonNull Context context, @NonNull Recipient recipient) {
VibrateState vibrateState = recipient.getMessageVibrate(); VibrateState vibrateState = recipient.getMessageVibrate();
boolean vibrationEnabled = vibrateState == VibrateState.DEFAULT ? TextSecurePreferences.isNotificationVibrateEnabled(context) : vibrateState == VibrateState.ENABLED; boolean vibrationEnabled = vibrateState == VibrateState.DEFAULT ? TextSecurePreferences.isNotificationVibrateEnabled(context) : vibrateState == VibrateState.ENABLED;
String displayName = getChannelDisplayNameFor(recipient.getName(), recipient.getProfileName(), recipient.getAddress()); String displayName = getChannelDisplayNameFor(context, recipient.getName(), recipient.getProfileName(), recipient.getAddress());
return createChannelFor(context, recipient.getAddress(), displayName, recipient.getMessageRingtone(context), vibrationEnabled); return createChannelFor(context, recipient.getAddress(), displayName, recipient.getMessageRingtone(context), vibrationEnabled);
} }
@ -121,9 +129,12 @@ public class NotificationChannels {
setLedPreference(channel, TextSecurePreferences.getNotificationLedColor(context)); setLedPreference(channel, TextSecurePreferences.getNotificationLedColor(context));
channel.setGroup(CATEGORY_MESSAGES); channel.setGroup(CATEGORY_MESSAGES);
channel.enableVibration(vibrationEnabled); channel.enableVibration(vibrationEnabled);
if (messageSound != null) {
channel.setSound(messageSound, new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) channel.setSound(messageSound, new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build()); .build());
}
NotificationManager notificationManager = context.getSystemService(NotificationManager.class); NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
if (notificationManager == null) { if (notificationManager == null) {
@ -213,7 +224,7 @@ public class NotificationChannels {
} }
NotificationChannel channel = new NotificationChannel(recipient.getNotificationChannel(context), NotificationChannel channel = new NotificationChannel(recipient.getNotificationChannel(context),
getChannelDisplayNameFor(recipient.getName(), recipient.getProfileName(), recipient.getAddress()), getChannelDisplayNameFor(context, recipient.getName(), recipient.getProfileName(), recipient.getAddress()),
NotificationManager.IMPORTANCE_HIGH); NotificationManager.IMPORTANCE_HIGH);
channel.setGroup(CATEGORY_MESSAGES); channel.setGroup(CATEGORY_MESSAGES);
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);