Эх сурвалжийг харах

Fix #290

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 6 жил өмнө
parent
commit
b181a961d8

+ 16 - 7
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");
+                }
             }
         }
 

+ 15 - 8
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");
+                }
+
             }
 
 

+ 3 - 2
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);
                         }
                     }
 

+ 3 - 1
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);
         }

+ 6 - 6
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)