From d6c932cd7e3a1f2ae5deb1c316973d8f739244ee Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Tue, 12 Nov 2024 15:25:21 -0500 Subject: [PATCH] Handle null audio attributes Test: SoundPreferenceControllerTest Fixes: 358540739 Flag: EXEMPT bugfix Change-Id: I9a0e6066ccc2315b276564178c13ed59dd22162b --- .../app/SoundPreferenceController.java | 2 ++ .../app/SoundPreferenceControllerTest.java | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/com/android/settings/notification/app/SoundPreferenceController.java b/src/com/android/settings/notification/app/SoundPreferenceController.java index b23b4fc86cb..bc1cb24ce1b 100644 --- a/src/com/android/settings/notification/app/SoundPreferenceController.java +++ b/src/com/android/settings/notification/app/SoundPreferenceController.java @@ -101,6 +101,8 @@ public class SoundPreferenceController extends NotificationPreferenceController public boolean handlePreferenceTreeClick(Preference preference) { if (KEY_SOUND.equals(preference.getKey()) && mFragment != null) { NotificationSoundPreference pref = (NotificationSoundPreference) preference; + // default to notification + pref.setRingtoneType(RingtoneManager.TYPE_NOTIFICATION); if (mChannel != null && mChannel.getAudioAttributes() != null) { if (USAGE_ALARM == mChannel.getAudioAttributes().getUsage()) { pref.setRingtoneType(RingtoneManager.TYPE_ALARM); diff --git a/tests/robotests/src/com/android/settings/notification/app/SoundPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/app/SoundPreferenceControllerTest.java index 273bcdbe213..6ec3eb2bec0 100644 --- a/tests/robotests/src/com/android/settings/notification/app/SoundPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/app/SoundPreferenceControllerTest.java @@ -302,6 +302,26 @@ public class SoundPreferenceControllerTest { RingtoneManager.EXTRA_RINGTONE_TYPE, 0)); } + @Test + public void testOnPreferenceTreeClick_noAudioAttributes() { + NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); + NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); + channel.setSound(null, null); + mController.onResume(appRow, channel, null, null, null, null, null); + + AttributeSet attributeSet = Robolectric.buildAttributeSet().build(); + NotificationSoundPreference pref = + spy(new NotificationSoundPreference(mContext, attributeSet)); + pref.setKey(mController.getPreferenceKey()); + mController.handlePreferenceTreeClick(pref); + + ArgumentCaptor intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); + verify(pref, times(1)).onPrepareRingtonePickerIntent(intentArgumentCaptor.capture()); + assertEquals(RingtoneManager.TYPE_NOTIFICATION, + intentArgumentCaptor.getValue().getIntExtra( + RingtoneManager.EXTRA_RINGTONE_TYPE, 0)); + } + @Test public void testOnActivityResult() { NotificationSoundPreference pref = mock(NotificationSoundPreference.class);