Home | History | Annotate | Download | only in settings
      1 package com.android.phone.settings;
      2 
      3 import android.content.Context;
      4 import android.media.RingtoneManager;
      5 import android.net.Uri;
      6 import android.os.Handler;
      7 import android.os.Message;
      8 import android.preference.Preference;
      9 import android.preference.RingtonePreference;
     10 import android.util.AttributeSet;
     11 
     12 import com.android.internal.telephony.Phone;
     13 import com.android.phone.common.util.SettingsUtil;
     14 
     15 /**
     16  * Looks up the voicemail ringtone's name asynchronously and updates the preference's summary when
     17  * it is created or updated.
     18  */
     19 public class VoicemailRingtonePreference extends RingtonePreference {
     20     public interface VoicemailRingtoneNameChangeListener {
     21         void onVoicemailRingtoneNameChanged(CharSequence name);
     22     }
     23 
     24     private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
     25 
     26     private VoicemailRingtoneNameChangeListener mVoicemailRingtoneNameChangeListener;
     27     private Runnable mVoicemailRingtoneLookupRunnable;
     28     private Handler mVoicemailRingtoneLookupComplete;
     29 
     30     private Phone mPhone;
     31 
     32     public VoicemailRingtonePreference(Context context, AttributeSet attrs) {
     33         super(context, attrs);
     34 
     35         mVoicemailRingtoneLookupComplete = new Handler() {
     36             @Override
     37             public void handleMessage(Message msg) {
     38                 switch (msg.what) {
     39                     case MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY:
     40                         if (mVoicemailRingtoneNameChangeListener != null) {
     41                             mVoicemailRingtoneNameChangeListener.onVoicemailRingtoneNameChanged(
     42                                     (CharSequence) msg.obj);
     43                         }
     44                         setSummary((CharSequence) msg.obj);
     45                         break;
     46                 }
     47             }
     48         };
     49     }
     50 
     51     public void init(Phone phone, CharSequence oldRingtoneName) {
     52         mPhone = phone;
     53 
     54         // Requesting the ringtone will trigger migration if necessary.
     55         VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
     56 
     57         final Preference preference = this;
     58         final String preferenceKey =
     59                 VoicemailNotificationSettingsUtil.getVoicemailRingtoneSharedPrefsKey(mPhone);
     60         setSummary(oldRingtoneName);
     61         mVoicemailRingtoneLookupRunnable = new Runnable() {
     62             @Override
     63             public void run() {
     64                 SettingsUtil.updateRingtoneName(
     65                         preference.getContext(),
     66                         mVoicemailRingtoneLookupComplete,
     67                         RingtoneManager.TYPE_NOTIFICATION,
     68                         preferenceKey,
     69                         MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY);
     70             }
     71         };
     72 
     73         updateRingtoneName();
     74     }
     75 
     76     public void setVoicemailRingtoneNameChangeListener(VoicemailRingtoneNameChangeListener l) {
     77         mVoicemailRingtoneNameChangeListener = l;
     78     }
     79 
     80     @Override
     81     protected Uri onRestoreRingtone() {
     82         return VoicemailNotificationSettingsUtil.getRingtoneUri(mPhone);
     83     }
     84 
     85     @Override
     86     protected void onSaveRingtone(Uri ringtoneUri) {
     87         // Don't call superclass method because it uses the pref key as the SharedPreferences key.
     88         // Delegate to the voicemail notification utility to save the ringtone instead.
     89         VoicemailNotificationSettingsUtil.setRingtoneUri(mPhone, ringtoneUri);
     90 
     91         updateRingtoneName();
     92     }
     93 
     94     private void updateRingtoneName() {
     95         new Thread(mVoicemailRingtoneLookupRunnable).start();
     96     }
     97 }
     98