1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.notification.app; 18 19 import static android.media.AudioAttributes.USAGE_ALARM; 20 import static android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE; 21 22 import android.app.NotificationChannel; 23 import android.app.NotificationManager; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.media.RingtoneManager; 27 import android.net.Uri; 28 import android.preference.PreferenceManager; 29 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.SettingsPreferenceFragment; 34 import com.android.settings.core.PreferenceControllerMixin; 35 import com.android.settings.notification.NotificationBackend; 36 37 public class SoundPreferenceController extends NotificationPreferenceController 38 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener, 39 PreferenceManager.OnActivityResultListener { 40 41 private static final String KEY_SOUND = "ringtone"; 42 private final SettingsPreferenceFragment mFragment; 43 private final NotificationSettings.DependentFieldListener mListener; 44 private NotificationSoundPreference mPreference; 45 protected static final int CODE = 200; 46 SoundPreferenceController(Context context, SettingsPreferenceFragment hostFragment, NotificationSettings.DependentFieldListener dependentFieldListener, NotificationBackend backend)47 public SoundPreferenceController(Context context, SettingsPreferenceFragment hostFragment, 48 NotificationSettings.DependentFieldListener dependentFieldListener, 49 NotificationBackend backend) { 50 super(context, backend); 51 mFragment = hostFragment; 52 mListener = dependentFieldListener; 53 } 54 55 @Override getPreferenceKey()56 public String getPreferenceKey() { 57 return KEY_SOUND; 58 } 59 60 @Override isAvailable()61 public boolean isAvailable() { 62 if (!super.isAvailable()) { 63 return false; 64 } 65 if (mChannel == null) { 66 return false; 67 } 68 return checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT) && !isDefaultChannel(); 69 } 70 71 @Override displayPreference(PreferenceScreen screen)72 public void displayPreference(PreferenceScreen screen) { 73 super.displayPreference(screen); 74 75 mPreference = screen.findPreference(getPreferenceKey()); 76 } 77 updateState(Preference preference)78 public void updateState(Preference preference) { 79 if (mAppRow!= null && mChannel != null) { 80 NotificationSoundPreference pref = (NotificationSoundPreference) preference; 81 pref.setEnabled(mAdmin == null); 82 pref.setRingtone(mChannel.getSound()); 83 } 84 } 85 86 @Override onPreferenceChange(Preference preference, Object newValue)87 public boolean onPreferenceChange(Preference preference, Object newValue) { 88 if (mChannel != null) { 89 mChannel.setSound((Uri) newValue, mChannel.getAudioAttributes()); 90 saveChannel(); 91 } 92 return true; 93 } 94 95 @Override handlePreferenceTreeClick(Preference preference)96 public boolean handlePreferenceTreeClick(Preference preference) { 97 if (KEY_SOUND.equals(preference.getKey()) && mFragment != null) { 98 NotificationSoundPreference pref = (NotificationSoundPreference) preference; 99 if (mChannel != null && mChannel.getAudioAttributes() != null) { 100 if (USAGE_ALARM == mChannel.getAudioAttributes().getUsage()) { 101 pref.setRingtoneType(RingtoneManager.TYPE_ALARM); 102 } else if (USAGE_NOTIFICATION_RINGTONE 103 == mChannel.getAudioAttributes().getUsage()) { 104 pref.setRingtoneType(RingtoneManager.TYPE_RINGTONE); 105 } else { 106 pref.setRingtoneType(RingtoneManager.TYPE_NOTIFICATION); 107 } 108 } 109 pref.onPrepareRingtonePickerIntent(pref.getIntent()); 110 mFragment.startActivityForResult(preference.getIntent(), CODE); 111 return true; 112 } 113 return false; 114 } 115 116 @Override onActivityResult(int requestCode, int resultCode, Intent data)117 public boolean onActivityResult(int requestCode, int resultCode, Intent data) { 118 if (CODE == requestCode) { 119 if (mPreference != null) { 120 mPreference.onActivityResult(requestCode, resultCode, data); 121 } 122 // the importance hasn't changed, but the importance description might as a result of 123 // user's selection. 124 mListener.onFieldValueChanged(); 125 return true; 126 } 127 return false; 128 } 129 hasValidSound(NotificationChannel channel)130 protected static boolean hasValidSound(NotificationChannel channel) { 131 return channel != null 132 && channel.getSound() != null && !Uri.EMPTY.equals(channel.getSound()); 133 } 134 } 135