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
17package com.android.dialer.voicemailstatus;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.database.Cursor;
22import android.preference.PreferenceManager;
23import android.support.annotation.Nullable;
24import 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 */
41public 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 Context mContext;
49  private Callback mCallback;
50
51  public VisualVoicemailEnabledChecker(Context context, @Nullable Callback callback) {
52    mContext = context;
53    mCallback = callback;
54    mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
55    mHasActiveVoicemailProvider = mPrefs.getBoolean(PREF_KEY_HAS_ACTIVE_VOICEMAIL_PROVIDER, false);
56  }
57
58  /**
59   * @return whether visual voicemail is enabled. Result is cached, call asyncUpdate() to update the
60   *     result.
61   */
62  public boolean isVisualVoicemailEnabled() {
63    return mHasActiveVoicemailProvider;
64  }
65
66  /**
67   * Perform an async query into the system to check the status of visual voicemail. If the status
68   * has changed, Callback.onVisualVoicemailEnabledStatusChanged() will be called.
69   */
70  public void asyncUpdate() {
71    mCallLogQueryHandler = new CallLogQueryHandler(mContext, mContext.getContentResolver(), this);
72    mCallLogQueryHandler.fetchVoicemailStatus();
73  }
74
75  @Override
76  public void onVoicemailStatusFetched(Cursor statusCursor) {
77    boolean hasActiveVoicemailProvider =
78        VoicemailStatusHelper.getNumberActivityVoicemailSources(statusCursor) > 0;
79    if (hasActiveVoicemailProvider != mHasActiveVoicemailProvider) {
80      mHasActiveVoicemailProvider = hasActiveVoicemailProvider;
81      mPrefs
82          .edit()
83          .putBoolean(PREF_KEY_HAS_ACTIVE_VOICEMAIL_PROVIDER, mHasActiveVoicemailProvider)
84          .apply();
85      if (mCallback != null) {
86        mCallback.onVisualVoicemailEnabledStatusChanged(mHasActiveVoicemailProvider);
87      }
88    }
89  }
90
91  @Override
92  public void onVoicemailUnreadCountFetched(Cursor cursor) {
93    // Do nothing
94  }
95
96  @Override
97  public void onMissedCallsUnreadCountFetched(Cursor cursor) {
98    // Do nothing
99  }
100
101  @Override
102  public boolean onCallsFetched(Cursor combinedCursor) {
103    // Do nothing
104    return false;
105  }
106
107  public interface Callback {
108
109    /** Callback to notify enabled status has changed to the @param newValue */
110    void onVisualVoicemailEnabledStatusChanged(boolean newValue);
111  }
112}
113