• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.voicemail.impl.settings;
18 
19 import android.annotation.TargetApi;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.os.Build.VERSION_CODES;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.preference.RingtonePreference;
26 import android.telecom.PhoneAccountHandle;
27 import android.telephony.TelephonyManager;
28 import android.util.AttributeSet;
29 import com.android.dialer.common.Assert;
30 import com.android.dialer.util.SettingsUtil;
31 
32 /**
33  * Looks up the voicemail ringtone's name asynchronously and updates the preference's summary when
34  * it is created or updated.
35  */
36 @TargetApi(VERSION_CODES.O)
37 public class VoicemailRingtonePreference extends RingtonePreference {
38 
39   /** Callback when the ringtone name has been fetched. */
40   public interface VoicemailRingtoneNameChangeListener {
onVoicemailRingtoneNameChanged(CharSequence name)41     void onVoicemailRingtoneNameChanged(CharSequence name);
42   }
43 
44   private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
45 
46   private PhoneAccountHandle phoneAccountHandle;
47   private final TelephonyManager telephonyManager;
48 
49   private VoicemailRingtoneNameChangeListener mVoicemailRingtoneNameChangeListener;
50   private Runnable mVoicemailRingtoneLookupRunnable;
51   private final Handler mVoicemailRingtoneLookupComplete =
52       new Handler() {
53         @Override
54         public void handleMessage(Message msg) {
55           switch (msg.what) {
56             case MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY:
57               if (mVoicemailRingtoneNameChangeListener != null) {
58                 mVoicemailRingtoneNameChangeListener.onVoicemailRingtoneNameChanged(
59                     (CharSequence) msg.obj);
60               }
61               setSummary((CharSequence) msg.obj);
62               break;
63             default:
64               Assert.fail();
65           }
66         }
67       };
68 
VoicemailRingtonePreference(Context context, AttributeSet attrs)69   public VoicemailRingtonePreference(Context context, AttributeSet attrs) {
70     super(context, attrs);
71     telephonyManager = context.getSystemService(TelephonyManager.class);
72   }
73 
init(PhoneAccountHandle phoneAccountHandle, CharSequence oldRingtoneName)74   public void init(PhoneAccountHandle phoneAccountHandle, CharSequence oldRingtoneName) {
75     this.phoneAccountHandle = phoneAccountHandle;
76     setSummary(oldRingtoneName);
77     mVoicemailRingtoneLookupRunnable =
78         new Runnable() {
79           @Override
80           public void run() {
81             SettingsUtil.getRingtoneName(
82                 getContext(),
83                 mVoicemailRingtoneLookupComplete,
84                 telephonyManager.getVoicemailRingtoneUri(phoneAccountHandle),
85                 MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY);
86           }
87         };
88 
89     updateRingtoneName();
90   }
91 
setVoicemailRingtoneNameChangeListener(VoicemailRingtoneNameChangeListener l)92   public void setVoicemailRingtoneNameChangeListener(VoicemailRingtoneNameChangeListener l) {
93     mVoicemailRingtoneNameChangeListener = l;
94   }
95 
96   @Override
onRestoreRingtone()97   protected Uri onRestoreRingtone() {
98     return telephonyManager.getVoicemailRingtoneUri(phoneAccountHandle);
99   }
100 
101   @Override
onSaveRingtone(Uri ringtoneUri)102   protected void onSaveRingtone(Uri ringtoneUri) {
103     telephonyManager.setVoicemailRingtoneUri(phoneAccountHandle, ringtoneUri);
104     updateRingtoneName();
105   }
106 
updateRingtoneName()107   private void updateRingtoneName() {
108     new Thread(mVoicemailRingtoneLookupRunnable).start();
109   }
110 }
111