1 /* 2 * Copyright (C) 2008 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.deskclock; 18 19 import android.content.Context; 20 import android.media.Ringtone; 21 import android.media.RingtoneManager; 22 import android.net.Uri; 23 import android.os.AsyncTask; 24 import android.preference.RingtonePreference; 25 import android.provider.Settings; 26 import android.util.AttributeSet; 27 28 /** 29 * The RingtonePreference does not have a way to get/set the current ringtone so 30 * we override onSaveRingtone and onRestoreRingtone to get the same behavior. 31 */ 32 public class AlarmPreference extends RingtonePreference { 33 private Uri mAlert; 34 private boolean mChangeDefault; 35 private AsyncTask mRingtoneTask; 36 37 public AlarmPreference(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 } 40 41 @Override 42 protected void onSaveRingtone(Uri ringtoneUri) { 43 setAlert(ringtoneUri); 44 if (mChangeDefault) { 45 // Update the default alert in the system. 46 Settings.System.putString(getContext().getContentResolver(), 47 Settings.System.ALARM_ALERT, 48 ringtoneUri == null ? null : ringtoneUri.toString()); 49 } 50 } 51 52 @Override 53 protected Uri onRestoreRingtone() { 54 if (RingtoneManager.isDefault(mAlert)) { 55 return RingtoneManager.getActualDefaultRingtoneUri(getContext(), 56 RingtoneManager.TYPE_ALARM); 57 } 58 return mAlert; 59 } 60 61 public void setAlert(Uri alert) { 62 mAlert = alert; 63 if (alert != null) { 64 setSummary(R.string.loading_ringtone); 65 if (mRingtoneTask != null) { 66 mRingtoneTask.cancel(true); 67 } 68 mRingtoneTask = new AsyncTask<Uri, Void, String>() { 69 @Override 70 protected String doInBackground(Uri... params) { 71 Ringtone r = RingtoneManager.getRingtone( 72 getContext(), params[0]); 73 if (r == null) { 74 r = RingtoneManager.getRingtone(getContext(), 75 Settings.System.DEFAULT_ALARM_ALERT_URI); 76 } 77 if (r != null) { 78 return r.getTitle(getContext()); 79 } 80 return null; 81 } 82 83 @Override 84 protected void onPostExecute(String title) { 85 if (!isCancelled()) { 86 setSummary(title); 87 mRingtoneTask = null; 88 } 89 } 90 }.execute(alert); 91 } else { 92 setSummary(R.string.silent_alarm_summary); 93 } 94 } 95 96 public Uri getAlert() { 97 return mAlert; 98 } 99 100 public void setChangeDefault() { 101 mChangeDefault = true; 102 } 103 } 104