2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.res.TypedArray;
22 import android.media.AudioAttributes;
23 import android.media.RingtoneManager;
24 import android.net.Uri;
25 import android.os.UserHandle;
26 import android.provider.Settings.System;
27 import android.support.v7.preference.Preference;
28 import android.support.v7.preference.PreferenceManager;
29 import android.text.TextUtils;
30 import android.util.AttributeSet;
33 * A {@link Preference} that allows the user to choose a ringtone from those on the device.
34 * The chosen ringtone's URI will be persisted as a string.
36 * If the user chooses the "Default" item, the saved string will be one of
37 * {@link System#DEFAULT_RINGTONE_URI},
38 * {@link System#DEFAULT_NOTIFICATION_URI}, or
39 * {@link System#DEFAULT_ALARM_ALERT_URI}. If the user chooses the "Silent"
40 * item, the saved string will be an empty string.
42 * @attr ref android.R.styleable#RingtonePreference_ringtoneType
43 * @attr ref android.R.styleable#RingtonePreference_showDefault
44 * @attr ref android.R.styleable#RingtonePreference_showSilent
46 * Based of frameworks/base/core/java/android/preference/RingtonePreference.java
47 * but extends android.support.v7.preference.Preference instead.
49 public class RingtonePreference extends Preference {
51 private static final String TAG = "RingtonePreference";
53 private int mRingtoneType;
54 private boolean mShowDefault;
55 private boolean mShowSilent;
57 private int mRequestCode;
58 protected int mUserId;
59 protected Context mUserContext;
61 public RingtonePreference(Context context, AttributeSet attrs) {
62 super(context, attrs);
64 final TypedArray a = context.obtainStyledAttributes(attrs,
65 com.android.internal.R.styleable.RingtonePreference, 0, 0);
66 mRingtoneType = a.getInt(com.android.internal.R.styleable.RingtonePreference_ringtoneType,
67 RingtoneManager.TYPE_RINGTONE);
68 mShowDefault = a.getBoolean(com.android.internal.R.styleable.RingtonePreference_showDefault,
70 mShowSilent = a.getBoolean(com.android.internal.R.styleable.RingtonePreference_showSilent,
72 setIntent(new Intent(RingtoneManager.ACTION_RINGTONE_PICKER));
73 setUserId(UserHandle.myUserId());
77 public void setUserId(int userId) {
79 mUserContext = Utils.createPackageContextAsUser(getContext(), mUserId);
82 public int getUserId() {
87 * Returns the sound type(s) that are shown in the picker.
89 * @return The sound type(s) that are shown in the picker.
90 * @see #setRingtoneType(int)
92 public int getRingtoneType() {
97 * Sets the sound type(s) that are shown in the picker.
99 * @param type The sound type(s) that are shown in the picker.
100 * @see RingtoneManager#EXTRA_RINGTONE_TYPE
102 public void setRingtoneType(int type) {
103 mRingtoneType = type;
107 * Returns whether to a show an item for the default sound/ringtone.
109 * @return Whether to show an item for the default sound/ringtone.
111 public boolean getShowDefault() {
116 * Sets whether to show an item for the default sound/ringtone. The default
117 * to use will be deduced from the sound type(s) being shown.
119 * @param showDefault Whether to show the default or not.
120 * @see RingtoneManager#EXTRA_RINGTONE_SHOW_DEFAULT
122 public void setShowDefault(boolean showDefault) {
123 mShowDefault = showDefault;
127 * Returns whether to a show an item for 'Silent'.
129 * @return Whether to show an item for 'Silent'.
131 public boolean getShowSilent() {
136 * Sets whether to show an item for 'Silent'.
138 * @param showSilent Whether to show 'Silent'.
139 * @see RingtoneManager#EXTRA_RINGTONE_SHOW_SILENT
141 public void setShowSilent(boolean showSilent) {
142 mShowSilent = showSilent;
145 public int getRequestCode() {
150 * Prepares the intent to launch the ringtone picker. This can be modified
151 * to adjust the parameters of the ringtone picker.
153 * @param ringtonePickerIntent The ringtone picker intent that can be
154 * modified by putting extras.
156 public void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) {
158 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
159 onRestoreRingtone());
161 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, mShowDefault);
163 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,
164 RingtoneManager.getDefaultUri(getRingtoneType()));
167 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, mShowSilent);
168 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, mRingtoneType);
169 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getTitle());
170 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_AUDIO_ATTRIBUTES_FLAGS,
171 AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY);
175 * Called when a ringtone is chosen.
177 * By default, this saves the ringtone URI to the persistent storage as a
180 * @param ringtoneUri The chosen ringtone's {@link Uri}. Can be null.
182 protected void onSaveRingtone(Uri ringtoneUri) {
183 persistString(ringtoneUri != null ? ringtoneUri.toString() : "");
187 * Called when the chooser is about to be shown and the current ringtone
188 * should be marked. Can return null to not mark any ringtone.
190 * By default, this restores the previous ringtone URI from the persistent
193 * @return The ringtone to be marked as the current ringtone.
195 protected Uri onRestoreRingtone() {
196 final String uriString = getPersistedString(null);
197 return !TextUtils.isEmpty(uriString) ? Uri.parse(uriString) : null;
201 protected Object onGetDefaultValue(TypedArray a, int index) {
202 return a.getString(index);
206 protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValueObj) {
207 String defaultValue = (String) defaultValueObj;
210 * This method is normally to make sure the internal state and UI
211 * matches either the persisted value or the default value. Since we
212 * don't show the current value in the UI (until the dialog is opened)
213 * and we don't keep local state, if we are restoring the persisted
214 * value we don't need to do anything.
216 if (restorePersistedValue) {
220 // If we are setting to the default value, we should persist it.
221 if (!TextUtils.isEmpty(defaultValue)) {
222 onSaveRingtone(Uri.parse(defaultValue));
225 protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
226 super.onAttachedToHierarchy(preferenceManager);
229 public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
231 Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
233 if (callChangeListener(uri != null ? uri.toString() : "")) {