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; 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 36 public class SoundPreferenceController extends NotificationPreferenceController 37 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener, 38 PreferenceManager.OnActivityResultListener { 39 40 private static final String KEY_SOUND = "ringtone"; 41 private final SettingsPreferenceFragment mFragment; 42 private final NotificationSettingsBase.ImportanceListener mListener; 43 private NotificationSoundPreference mPreference; 44 protected static final int CODE = 200; 45 SoundPreferenceController(Context context, SettingsPreferenceFragment hostFragment, NotificationSettingsBase.ImportanceListener importanceListener, NotificationBackend backend)46 public SoundPreferenceController(Context context, SettingsPreferenceFragment hostFragment, 47 NotificationSettingsBase.ImportanceListener importanceListener, 48 NotificationBackend backend) { 49 super(context, backend); 50 mFragment = hostFragment; 51 mListener = importanceListener; 52 } 53 54 @Override getPreferenceKey()55 public String getPreferenceKey() { 56 return KEY_SOUND; 57 } 58 59 @Override isAvailable()60 public boolean isAvailable() { 61 if (!super.isAvailable()) { 62 return false; 63 } 64 if (mChannel == null) { 65 return false; 66 } 67 return checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT) && !isDefaultChannel(); 68 } 69 70 @Override displayPreference(PreferenceScreen screen)71 public void displayPreference(PreferenceScreen screen) { 72 super.displayPreference(screen); 73 74 mPreference = screen.findPreference(getPreferenceKey()); 75 } 76 updateState(Preference preference)77 public void updateState(Preference preference) { 78 if (mAppRow!= null && mChannel != null) { 79 NotificationSoundPreference pref = (NotificationSoundPreference) preference; 80 pref.setEnabled(mAdmin == null); 81 pref.setRingtone(mChannel.getSound()); 82 } 83 } 84 85 @Override onPreferenceChange(Preference preference, Object newValue)86 public boolean onPreferenceChange(Preference preference, Object newValue) { 87 if (mChannel != null) { 88 mChannel.setSound((Uri) newValue, mChannel.getAudioAttributes()); 89 saveChannel(); 90 } 91 return true; 92 } 93 94 @Override handlePreferenceTreeClick(Preference preference)95 public boolean handlePreferenceTreeClick(Preference preference) { 96 if (KEY_SOUND.equals(preference.getKey()) && mFragment != null) { 97 NotificationSoundPreference pref = (NotificationSoundPreference) preference; 98 if (mChannel != null && mChannel.getAudioAttributes() != null) { 99 if (USAGE_ALARM == mChannel.getAudioAttributes().getUsage()) { 100 pref.setRingtoneType(RingtoneManager.TYPE_ALARM); 101 } else if (USAGE_NOTIFICATION_RINGTONE 102 == mChannel.getAudioAttributes().getUsage()) { 103 pref.setRingtoneType(RingtoneManager.TYPE_RINGTONE); 104 } else { 105 pref.setRingtoneType(RingtoneManager.TYPE_NOTIFICATION); 106 } 107 } 108 pref.onPrepareRingtonePickerIntent(pref.getIntent()); 109 mFragment.startActivityForResult(preference.getIntent(), CODE); 110 return true; 111 } 112 return false; 113 } 114 115 @Override onActivityResult(int requestCode, int resultCode, Intent data)116 public boolean onActivityResult(int requestCode, int resultCode, Intent data) { 117 if (CODE == requestCode) { 118 if (mPreference != null) { 119 mPreference.onActivityResult(requestCode, resultCode, data); 120 } 121 // the importance hasn't changed, but the importance description might as a result of 122 // user's selection. 123 mListener.onImportanceChanged(); 124 return true; 125 } 126 return false; 127 } 128 hasValidSound(NotificationChannel channel)129 protected static boolean hasValidSound(NotificationChannel channel) { 130 return channel != null 131 && channel.getSound() != null && !Uri.EMPTY.equals(channel.getSound()); 132 } 133 } 134