Fix error when backing up channels
If an app has an invalid sound (for example, because they don't
have access to the sound file), catch the exception and use the default
sound, instead.
Test: NotificationChannelTest
Flag: EXEMPT bug fix
Fixes: 369641569
Change-Id: I664c2cf250ed27aa93fd37a1ddf3aca19af2d9c6
diff --git a/core/java/android/app/NotificationChannel.java b/core/java/android/app/NotificationChannel.java
index 4a2b016..ebe7b3a 100644
--- a/core/java/android/app/NotificationChannel.java
+++ b/core/java/android/app/NotificationChannel.java
@@ -38,6 +38,7 @@
import android.service.notification.NotificationListenerService;
import android.text.TextUtils;
import android.util.Log;
+import android.util.Slog;
import android.util.proto.ProtoOutputStream;
import com.android.internal.util.Preconditions;
@@ -1369,12 +1370,17 @@
if (sound == null || Uri.EMPTY.equals(sound)) {
return null;
}
- Uri canonicalSound = getCanonicalizedSoundUri(context.getContentResolver(), sound);
- if (canonicalSound == null) {
- // The content provider does not support canonical uris so we backup the default
+ try {
+ Uri canonicalSound = getCanonicalizedSoundUri(context.getContentResolver(), sound);
+ if (canonicalSound == null) {
+ // The content provider does not support canonical uris so we backup the default
+ return Settings.System.DEFAULT_NOTIFICATION_URI;
+ }
+ return canonicalSound;
+ } catch (Exception e) {
+ Slog.e(TAG, "Cannot find file for sound " + sound + " using default");
return Settings.System.DEFAULT_NOTIFICATION_URI;
}
- return canonicalSound;
}
/**
diff --git a/core/tests/coretests/src/android/app/NotificationChannelTest.java b/core/tests/coretests/src/android/app/NotificationChannelTest.java
index e19f887..e4b5407 100644
--- a/core/tests/coretests/src/android/app/NotificationChannelTest.java
+++ b/core/tests/coretests/src/android/app/NotificationChannelTest.java
@@ -27,6 +27,7 @@
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -51,6 +52,7 @@
import android.platform.test.flag.junit.FlagsParameterization;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.MediaStore.Audio.AudioColumns;
+import android.provider.Settings;
import android.test.mock.MockContentResolver;
import android.util.Xml;
@@ -399,6 +401,29 @@
}
@Test
+ public void testWriteXmlForBackup_noAccessToFile() throws Exception {
+ Uri uri = Uri.parse("content://media/1");
+
+ AudioAttributes mAudioAttributes =
+ new AudioAttributes.Builder()
+ .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
+ .setUsage(AudioAttributes.USAGE_NOTIFICATION)
+ .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
+ .build();
+
+ NotificationChannel channel = new NotificationChannel("id", "name", 3);
+ channel.setSound(uri, mAudioAttributes);
+
+ when(mIContentProvider.canonicalize(any(), any())).thenThrow(new SecurityException(""));
+ doThrow(new SecurityException("")).when(mIContentProvider)
+ .canonicalizeAsync(any(), any(), any());
+
+ NotificationChannel restoredChannel = backUpAndRestore(channel);
+ assertThat(restoredChannel.getSound())
+ .isEqualTo(Settings.System.DEFAULT_NOTIFICATION_URI);
+ }
+
+ @Test
public void testVibrationGetters_nonPatternBasedVibrationEffect_waveform() throws Exception {
mSetFlagsRule.enableFlags(Flags.FLAG_NOTIFICATION_CHANNEL_VIBRATION_EFFECT_API);
NotificationChannel channel = new NotificationChannel("id", "name", 3);