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     private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
21 
22     private Runnable mVoicemailRingtoneLookupRunnable;
23     private Handler mVoicemailRingtoneLookupComplete;
24 
25     private Phone mPhone;
26 
VoicemailRingtonePreference(Context context, AttributeSet attrs)27     public VoicemailRingtonePreference(Context context, AttributeSet attrs) {
28         super(context, attrs);
29 
30         mVoicemailRingtoneLookupComplete = new Handler() {
31             @Override
32             public void handleMessage(Message msg) {
33                 switch (msg.what) {
34                     case MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY:
35                         setSummary((CharSequence) msg.obj);
36                         break;
37                 }
38             }
39         };
40     }
41 
init(Phone phone)42     public void init(Phone phone) {
43         mPhone = phone;
44 
45         // Requesting the ringtone will trigger migration if necessary.
46         VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
47 
48         final Preference preference = this;
49         final String preferenceKey =
50                 VoicemailNotificationSettingsUtil.getVoicemailRingtoneSharedPrefsKey(mPhone);
51         mVoicemailRingtoneLookupRunnable = new Runnable() {
52             @Override
53             public void run() {
54                 SettingsUtil.updateRingtoneName(
55                         preference.getContext(),
56                         mVoicemailRingtoneLookupComplete,
57                         RingtoneManager.TYPE_NOTIFICATION,
58                         preferenceKey,
59                         MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY);
60             }
61         };
62 
63         updateRingtoneName();
64     }
65 
66     @Override
onRestoreRingtone()67     protected Uri onRestoreRingtone() {
68         return VoicemailNotificationSettingsUtil.getRingtoneUri(mPhone);
69     }
70 
71     @Override
onSaveRingtone(Uri ringtoneUri)72     protected void onSaveRingtone(Uri ringtoneUri) {
73         // Don't call superclass method because it uses the pref key as the SharedPreferences key.
74         // Delegate to the voicemail notification utility to save the ringtone instead.
75         VoicemailNotificationSettingsUtil.setRingtoneUri(mPhone, ringtoneUri);
76 
77         updateRingtoneName();
78     }
79 
updateRingtoneName()80     private void updateRingtoneName() {
81         new Thread(mVoicemailRingtoneLookupRunnable).start();
82     }
83 }
84