Merge "Handle null audio attributes" into main

This commit is contained in:
Treehugger Robot
2024-11-12 23:15:13 +00:00
committed by Android (Google) Code Review
2 changed files with 22 additions and 0 deletions

View File

@@ -101,6 +101,8 @@ public class SoundPreferenceController extends NotificationPreferenceController
public boolean handlePreferenceTreeClick(Preference preference) { public boolean handlePreferenceTreeClick(Preference preference) {
if (KEY_SOUND.equals(preference.getKey()) && mFragment != null) { if (KEY_SOUND.equals(preference.getKey()) && mFragment != null) {
NotificationSoundPreference pref = (NotificationSoundPreference) preference; NotificationSoundPreference pref = (NotificationSoundPreference) preference;
// default to notification
pref.setRingtoneType(RingtoneManager.TYPE_NOTIFICATION);
if (mChannel != null && mChannel.getAudioAttributes() != null) { if (mChannel != null && mChannel.getAudioAttributes() != null) {
if (USAGE_ALARM == mChannel.getAudioAttributes().getUsage()) { if (USAGE_ALARM == mChannel.getAudioAttributes().getUsage()) {
pref.setRingtoneType(RingtoneManager.TYPE_ALARM); pref.setRingtoneType(RingtoneManager.TYPE_ALARM);

View File

@@ -302,6 +302,26 @@ public class SoundPreferenceControllerTest {
RingtoneManager.EXTRA_RINGTONE_TYPE, 0)); 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<Intent> 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 @Test
public void testOnActivityResult() { public void testOnActivityResult() {
NotificationSoundPreference pref = mock(NotificationSoundPreference.class); NotificationSoundPreference pref = mock(NotificationSoundPreference.class);