From ca2efcac8abb52a800153574e0a0ae99f5bdc91d Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Wed, 22 Aug 2018 13:19:59 -0700 Subject: [PATCH] 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. --- res/values/strings.xml | 1 + .../database/helpers/SQLCipherOpenHelper.java | 3 ++- .../notifications/NotificationChannels.java | 25 +++++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index e40f7e2236..c5eef2dd06 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -674,6 +674,7 @@ App updates Other Messages + Unknown Quick response unavailable when Signal is locked! diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 021208e660..da1c4f890b 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -247,6 +247,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { if (oldVersion < NOTIFICATION_CHANNELS) { 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)) { while (cursor != null && cursor.moveToNext()) { @@ -257,7 +258,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { 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(systemName, profileName, address); + String displayName = NotificationChannels.getChannelDisplayNameFor(context, systemName, profileName, address); boolean vibrateEnabled = vibrateState == 0 ? TextSecurePreferences.isNotificationVibrateEnabled(context) : vibrateState == 1; String channelId = NotificationChannels.createChannelFor(context, address, displayName, messageSoundUri, vibrateEnabled); diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java b/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java index 1dd6b34afd..591ffef654 100644 --- a/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java +++ b/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java @@ -86,8 +86,16 @@ public class NotificationChannels { return Build.VERSION.SDK_INT >= 26; } - public static String getChannelDisplayNameFor(@Nullable String systemName, @Nullable String profileName, @NonNull Address address) { - return TextUtils.isEmpty(systemName) ? (TextUtils.isEmpty(profileName) ? address.serialize() : profileName) : systemName; + 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); + } } /** @@ -97,7 +105,7 @@ public class NotificationChannels { 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; - 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); } @@ -121,9 +129,12 @@ public class NotificationChannels { setLedPreference(channel, TextSecurePreferences.getNotificationLedColor(context)); channel.setGroup(CATEGORY_MESSAGES); channel.enableVibration(vibrationEnabled); - channel.setSound(messageSound, new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) - .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) - .build()); + + if (messageSound != null) { + channel.setSound(messageSound, new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) + .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) + .build()); + } NotificationManager notificationManager = context.getSystemService(NotificationManager.class); if (notificationManager == null) { @@ -213,7 +224,7 @@ public class NotificationChannels { } 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); channel.setGroup(CATEGORY_MESSAGES); notificationManager.createNotificationChannel(channel);