1 package com.android.dialer.voicemail; 2 3 4 import android.content.Context; 5 import android.content.SharedPreferences; 6 import android.database.Cursor; 7 import android.preference.PreferenceManager; 8 import android.support.annotation.Nullable; 9 10 import com.android.dialer.calllog.CallLogQueryHandler; 11 12 /** 13 * Helper class to check whether visual voicemail is enabled. 14 * 15 * Call isVisualVoicemailEnabled() to retrieve the result. 16 * 17 * The result is cached and saved in a SharedPreferences, stored as a boolean in 18 * PREF_KEY_HAS_ACTIVE_VOICEMAIL_PROVIDER. Every time a new instance is created, it will try to 19 * restore the cached result from the SharedPreferences. 20 * 21 * Call asyncUpdate() to make a CallLogQuery to check the actual status. This is a async call so 22 * isVisualVoicemailEnabled() will not be affected immediately. 23 * 24 * If the status has changed as a result of asyncUpdate(), 25 * Callback.onVisualVoicemailEnabledStatusChanged() will be called with the new value. 26 */ 27 public class VisualVoicemailEnabledChecker implements CallLogQueryHandler.Listener { 28 29 public static final String PREF_KEY_HAS_ACTIVE_VOICEMAIL_PROVIDER = 30 "has_active_voicemail_provider"; 31 private SharedPreferences mPrefs; 32 private boolean mHasActiveVoicemailProvider; 33 private CallLogQueryHandler mCallLogQueryHandler; 34 private VoicemailStatusHelper mVoicemailStatusHelper; 35 private Context mContext; 36 37 public interface Callback { 38 39 /** 40 * Callback to notify enabled status has changed to the @param newValue 41 */ onVisualVoicemailEnabledStatusChanged(boolean newValue)42 void onVisualVoicemailEnabledStatusChanged(boolean newValue); 43 } 44 45 private Callback mCallback; 46 VisualVoicemailEnabledChecker(Context context, @Nullable Callback callback)47 public VisualVoicemailEnabledChecker(Context context, @Nullable Callback callback) { 48 mContext = context; 49 mCallback = callback; 50 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); 51 mVoicemailStatusHelper = new VoicemailStatusHelperImpl(); 52 mHasActiveVoicemailProvider = mPrefs.getBoolean(PREF_KEY_HAS_ACTIVE_VOICEMAIL_PROVIDER, 53 false); 54 } 55 56 /** 57 * @return whether visual voicemail is enabled. Result is cached, call asyncUpdate() to 58 * update the result. 59 */ isVisualVoicemailEnabled()60 public boolean isVisualVoicemailEnabled() { 61 return mHasActiveVoicemailProvider; 62 } 63 64 /** 65 * Perform an async query into the system to check the status of visual voicemail. 66 * If the status has changed, Callback.onVisualVoicemailEnabledStatusChanged() will be called. 67 */ asyncUpdate()68 public void asyncUpdate() { 69 mCallLogQueryHandler = 70 new CallLogQueryHandler(mContext, mContext.getContentResolver(), this); 71 mCallLogQueryHandler.fetchVoicemailStatus(); 72 } 73 74 @Override onVoicemailStatusFetched(Cursor statusCursor)75 public void onVoicemailStatusFetched(Cursor statusCursor) { 76 boolean hasActiveVoicemailProvider = 77 mVoicemailStatusHelper.getNumberActivityVoicemailSources(statusCursor) > 0; 78 if (hasActiveVoicemailProvider != mHasActiveVoicemailProvider) { 79 mHasActiveVoicemailProvider = hasActiveVoicemailProvider; 80 mPrefs.edit().putBoolean(PREF_KEY_HAS_ACTIVE_VOICEMAIL_PROVIDER, 81 mHasActiveVoicemailProvider); 82 if (mCallback != null) { 83 mCallback.onVisualVoicemailEnabledStatusChanged(mHasActiveVoicemailProvider); 84 } 85 } 86 } 87 88 @Override onVoicemailUnreadCountFetched(Cursor cursor)89 public void onVoicemailUnreadCountFetched(Cursor cursor) { 90 // Do nothing 91 } 92 93 @Override onMissedCallsUnreadCountFetched(Cursor cursor)94 public void onMissedCallsUnreadCountFetched(Cursor cursor) { 95 // Do nothing 96 } 97 98 @Override onCallsFetched(Cursor combinedCursor)99 public boolean onCallsFetched(Cursor combinedCursor) { 100 // Do nothing 101 return false; 102 } 103 } 104