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