From b181a961d87988b9ce3a75cb42e959615b4b76f1 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Sat, 18 Aug 2018 15:38:16 +0200 Subject: [PATCH] Fix #290 Signed-off-by: Mario Danic --- .../CallNotificationController.java | 23 +++++++++++++------ .../talk/jobs/NotificationWorker.java | 23 ++++++++++++------- .../receivers/PackageReplacedReceiver.java | 5 ++-- .../talk/utils/NotificationUtils.java | 4 +++- .../utils/preferences/AppPreferences.java | 12 +++++----- 5 files changed, 43 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/CallNotificationController.java b/app/src/main/java/com/nextcloud/talk/controllers/CallNotificationController.java index 0cead971f..b1206f7ee 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/CallNotificationController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/CallNotificationController.java @@ -325,13 +325,22 @@ public class CallNotificationController extends BaseController { } } - if (ringtoneUri != null) { - mediaPlayer = MediaPlayer.create(getApplicationContext(), ringtoneUri); - mediaPlayer.setLooping(true); - AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(AudioAttributes - .CONTENT_TYPE_SONIFICATION).setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build(); - mediaPlayer.setAudioAttributes(audioAttributes); - mediaPlayer.start(); + if (ringtoneUri != null && getActivity() != null) { + mediaPlayer = new MediaPlayer(); + try { + mediaPlayer.setDataSource(getActivity(), ringtoneUri); + + mediaPlayer.setLooping(true); + AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(AudioAttributes + .CONTENT_TYPE_SONIFICATION).setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build(); + mediaPlayer.setAudioAttributes(audioAttributes); + + mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start()); + + mediaPlayer.prepareAsync(); + } catch (IOException e) { + Log.e(TAG, "Failed to set data source"); + } } } diff --git a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java index 882274691..71ce12cae 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java @@ -28,7 +28,6 @@ import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.AudioAttributes; -import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Build; @@ -285,12 +284,12 @@ public class NotificationWorker extends Worker { notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES_V3); } else { NotificationUtils.createNotificationChannel(notificationManager, - NotificationUtils.NOTIFICATION_CHANNEL_CALLS_V2, context.getResources() + NotificationUtils.NOTIFICATION_CHANNEL_CALLS_V3, context.getResources() .getString(R.string.nc_notification_channel_calls), context.getResources() .getString(R.string.nc_notification_channel_calls_description), true, NotificationManager.IMPORTANCE_HIGH); - notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_CALLS_V2); + notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES_V3); } notificationBuilder.setGroup(Long.toString(crc32.getValue())); @@ -327,8 +326,6 @@ public class NotificationWorker extends Worker { if (soundUri != null & !ApplicationWideCurrentRoomHolder.getInstance().isInCall() && DoNotDisturbUtils.shouldPlaySound()) { - MediaPlayer mediaPlayer = MediaPlayer.create(context, soundUri); - mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder().setContentType (AudioAttributes.CONTENT_TYPE_SONIFICATION); @@ -338,9 +335,19 @@ public class NotificationWorker extends Worker { audioAttributesBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST); } - mediaPlayer.setAudioAttributes(audioAttributesBuilder.build()); - mediaPlayer.start(); - mediaPlayer.setOnCompletionListener(MediaPlayer::release); + MediaPlayer mediaPlayer = new MediaPlayer(); + try { + mediaPlayer.setDataSource(context, soundUri); + mediaPlayer.setAudioAttributes(audioAttributesBuilder.build()); + + mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start()); + mediaPlayer.setOnCompletionListener(MediaPlayer::release); + + mediaPlayer.prepareAsync(); + } catch (IOException e) { + Log.e(TAG, "Failed to set data source"); + } + } diff --git a/app/src/main/java/com/nextcloud/talk/receivers/PackageReplacedReceiver.java b/app/src/main/java/com/nextcloud/talk/receivers/PackageReplacedReceiver.java index d884e0b2e..46e1deb00 100644 --- a/app/src/main/java/com/nextcloud/talk/receivers/PackageReplacedReceiver.java +++ b/app/src/main/java/com/nextcloud/talk/receivers/PackageReplacedReceiver.java @@ -75,9 +75,10 @@ public class PackageReplacedReceiver extends BroadcastReceiver { appPreferences.setNotificationChannelIsUpgradedToV2(true); } - if (!appPreferences.getIsMessagesNotificationChannelUpgradedToV3() && packageInfo.versionCode > 51) { + if ((!appPreferences.getIsNotificationChannelUpgradedToV3()) && packageInfo.versionCode > 51) { notificationManager.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES_V2); - appPreferences.setNotificationChannelIsUpgradedToV2(true); + notificationManager.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_CALLS_V2); + appPreferences.setNotificationChannelIsUpgradedToV3(true); } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java index 005e4e3dc..cec35d5ac 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java @@ -40,7 +40,8 @@ public class NotificationUtils { public static final String NOTIFICATION_CHANNEL_MESSAGES = "NOTIFICATION_CHANNEL_MESSAGES"; public static final String NOTIFICATION_CHANNEL_CALLS_V2 = "NOTIFICATION_CHANNEL_CALLS_V2"; public static final String NOTIFICATION_CHANNEL_MESSAGES_V2 = "NOTIFICATION_CHANNEL_MESSAGES_V2"; - public static final String NOTIFICATION_CHANNEL_MESSAGES_V3 = "NOTIFICATION_CHANNEL_MESSAGES_V2"; + public static final String NOTIFICATION_CHANNEL_MESSAGES_V3 = "NOTIFICATION_CHANNEL_MESSAGES_V3"; + public static final String NOTIFICATION_CHANNEL_CALLS_V3 = "NOTIFICATION_CHANNEL_CALLS_V3"; @TargetApi(Build.VERSION_CODES.O) public static void createNotificationChannel(NotificationManager notificationManager, @@ -57,6 +58,7 @@ public class NotificationUtils { channel.setDescription(channelDescription); channel.enableLights(enableLights); channel.setLightColor(Color.RED); + channel.setSound(null, null); notificationManager.createNotificationChannel(channel); } diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java index 7b5aad4ba..078160dbb 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java @@ -171,15 +171,15 @@ public interface AppPreferences { @RemoveMethod void removeNotificationChannelUpgradeToV2(); - @KeyByString("messages_notification_channel_upgrade_to_v3") - boolean getIsMessagesNotificationChannelUpgradedToV3(); + @KeyByString("notification_channels_upgrade_to_v3") + boolean getIsNotificationChannelUpgradedToV3(); - @KeyByString("messages_notification_channel_upgrade_to_v3") - void setMessagesNotificationChannelIsUpgradedToV3(boolean value); + @KeyByString("notification_channels_upgrade_to_v3") + void setNotificationChannelIsUpgradedToV3(boolean value); - @KeyByString("messages_notification_channel_upgrade_to_v3") + @KeyByString("notification_channels_upgrade_to_v3") @RemoveMethod - void removeMessagesNotificationChannelUpgradeToV3(); + void removeNotificationChannelUpgradeToV3(); @KeyByString("notifications_vibrate") @DefaultValue(R.bool.value_true)