1/* 2 * Copyright (C) 2007 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 17package android.preference; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.res.TypedArray; 22import android.media.RingtoneManager; 23import android.net.Uri; 24import android.provider.Settings.System; 25import android.text.TextUtils; 26import android.util.AttributeSet; 27import android.util.Log; 28 29/** 30 * A {@link Preference} that allows the user to choose a ringtone from those on the device. 31 * The chosen ringtone's URI will be persisted as a string. 32 * <p> 33 * If the user chooses the "Default" item, the saved string will be one of 34 * {@link System#DEFAULT_RINGTONE_URI} or 35 * {@link System#DEFAULT_NOTIFICATION_URI}. If the user chooses the "Silent" 36 * item, the saved string will be an empty string. 37 * 38 * @attr ref android.R.styleable#RingtonePreference_ringtoneType 39 * @attr ref android.R.styleable#RingtonePreference_showDefault 40 * @attr ref android.R.styleable#RingtonePreference_showSilent 41 */ 42public class RingtonePreference extends Preference implements 43 PreferenceManager.OnActivityResultListener { 44 45 private static final String TAG = "RingtonePreference"; 46 47 private int mRingtoneType; 48 private boolean mShowDefault; 49 private boolean mShowSilent; 50 51 private int mRequestCode; 52 53 public RingtonePreference(Context context, AttributeSet attrs, int defStyle) { 54 super(context, attrs, defStyle); 55 56 TypedArray a = context.obtainStyledAttributes(attrs, 57 com.android.internal.R.styleable.RingtonePreference, defStyle, 0); 58 mRingtoneType = a.getInt(com.android.internal.R.styleable.RingtonePreference_ringtoneType, 59 RingtoneManager.TYPE_RINGTONE); 60 mShowDefault = a.getBoolean(com.android.internal.R.styleable.RingtonePreference_showDefault, 61 true); 62 mShowSilent = a.getBoolean(com.android.internal.R.styleable.RingtonePreference_showSilent, 63 true); 64 a.recycle(); 65 } 66 67 public RingtonePreference(Context context, AttributeSet attrs) { 68 this(context, attrs, com.android.internal.R.attr.ringtonePreferenceStyle); 69 } 70 71 public RingtonePreference(Context context) { 72 this(context, null); 73 } 74 75 /** 76 * Returns the sound type(s) that are shown in the picker. 77 * 78 * @return The sound type(s) that are shown in the picker. 79 * @see #setRingtoneType(int) 80 */ 81 public int getRingtoneType() { 82 return mRingtoneType; 83 } 84 85 /** 86 * Sets the sound type(s) that are shown in the picker. 87 * 88 * @param type The sound type(s) that are shown in the picker. 89 * @see RingtoneManager#EXTRA_RINGTONE_TYPE 90 */ 91 public void setRingtoneType(int type) { 92 mRingtoneType = type; 93 } 94 95 /** 96 * Returns whether to a show an item for the default sound/ringtone. 97 * 98 * @return Whether to show an item for the default sound/ringtone. 99 */ 100 public boolean getShowDefault() { 101 return mShowDefault; 102 } 103 104 /** 105 * Sets whether to show an item for the default sound/ringtone. The default 106 * to use will be deduced from the sound type(s) being shown. 107 * 108 * @param showDefault Whether to show the default or not. 109 * @see RingtoneManager#EXTRA_RINGTONE_SHOW_DEFAULT 110 */ 111 public void setShowDefault(boolean showDefault) { 112 mShowDefault = showDefault; 113 } 114 115 /** 116 * Returns whether to a show an item for 'Silent'. 117 * 118 * @return Whether to show an item for 'Silent'. 119 */ 120 public boolean getShowSilent() { 121 return mShowSilent; 122 } 123 124 /** 125 * Sets whether to show an item for 'Silent'. 126 * 127 * @param showSilent Whether to show 'Silent'. 128 * @see RingtoneManager#EXTRA_RINGTONE_SHOW_SILENT 129 */ 130 public void setShowSilent(boolean showSilent) { 131 mShowSilent = showSilent; 132 } 133 134 @Override 135 protected void onClick() { 136 // Launch the ringtone picker 137 Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); 138 onPrepareRingtonePickerIntent(intent); 139 getPreferenceManager().getActivity().startActivityForResult(intent, mRequestCode); 140 } 141 142 /** 143 * Prepares the intent to launch the ringtone picker. This can be modified 144 * to adjust the parameters of the ringtone picker. 145 * 146 * @param ringtonePickerIntent The ringtone picker intent that can be 147 * modified by putting extras. 148 */ 149 protected void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) { 150 151 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, 152 onRestoreRingtone()); 153 154 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, mShowDefault); 155 if (mShowDefault) { 156 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, 157 RingtoneManager.getDefaultUri(getRingtoneType())); 158 } 159 160 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, mShowSilent); 161 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, mRingtoneType); 162 } 163 164 /** 165 * Called when a ringtone is chosen. 166 * <p> 167 * By default, this saves the ringtone URI to the persistent storage as a 168 * string. 169 * 170 * @param ringtoneUri The chosen ringtone's {@link Uri}. Can be null. 171 */ 172 protected void onSaveRingtone(Uri ringtoneUri) { 173 persistString(ringtoneUri != null ? ringtoneUri.toString() : ""); 174 } 175 176 /** 177 * Called when the chooser is about to be shown and the current ringtone 178 * should be marked. Can return null to not mark any ringtone. 179 * <p> 180 * By default, this restores the previous ringtone URI from the persistent 181 * storage. 182 * 183 * @return The ringtone to be marked as the current ringtone. 184 */ 185 protected Uri onRestoreRingtone() { 186 final String uriString = getPersistedString(null); 187 return !TextUtils.isEmpty(uriString) ? Uri.parse(uriString) : null; 188 } 189 190 @Override 191 protected Object onGetDefaultValue(TypedArray a, int index) { 192 return a.getString(index); 193 } 194 195 @Override 196 protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValueObj) { 197 String defaultValue = (String) defaultValueObj; 198 199 /* 200 * This method is normally to make sure the internal state and UI 201 * matches either the persisted value or the default value. Since we 202 * don't show the current value in the UI (until the dialog is opened) 203 * and we don't keep local state, if we are restoring the persisted 204 * value we don't need to do anything. 205 */ 206 if (restorePersistedValue) { 207 return; 208 } 209 210 // If we are setting to the default value, we should persist it. 211 if (!TextUtils.isEmpty(defaultValue)) { 212 onSaveRingtone(Uri.parse(defaultValue)); 213 } 214 } 215 216 @Override 217 protected void onAttachedToHierarchy(PreferenceManager preferenceManager) { 218 super.onAttachedToHierarchy(preferenceManager); 219 220 preferenceManager.registerOnActivityResultListener(this); 221 mRequestCode = preferenceManager.getNextRequestCode(); 222 } 223 224 public boolean onActivityResult(int requestCode, int resultCode, Intent data) { 225 226 if (requestCode == mRequestCode) { 227 228 if (data != null) { 229 Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 230 231 if (callChangeListener(uri != null ? uri.toString() : "")) { 232 onSaveRingtone(uri); 233 } 234 } 235 236 return true; 237 } 238 239 return false; 240 } 241 242} 243