Home | History | Annotate | Download | only in voicemailstatus
      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 prefs;
     46   private boolean hasActiveVoicemailProvider;
     47   private CallLogQueryHandler callLogQueryHandler;
     48   private Context context;
     49   private Callback callback;
     50 
     51   public VisualVoicemailEnabledChecker(Context context, @Nullable Callback callback) {
     52     this.context = context;
     53     this.callback = callback;
     54     prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
     55     hasActiveVoicemailProvider = prefs.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 hasActiveVoicemailProvider;
     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     callLogQueryHandler = new CallLogQueryHandler(context, context.getContentResolver(), this);
     72     callLogQueryHandler.fetchVoicemailStatus();
     73   }
     74 
     75   @Override
     76   public void onVoicemailStatusFetched(Cursor statusCursor) {
     77     boolean hasActiveVoicemailProvider =
     78         VoicemailStatusHelper.getNumberActivityVoicemailSources(statusCursor) > 0;
     79     if (hasActiveVoicemailProvider != this.hasActiveVoicemailProvider) {
     80       this.hasActiveVoicemailProvider = hasActiveVoicemailProvider;
     81       prefs
     82           .edit()
     83           .putBoolean(PREF_KEY_HAS_ACTIVE_VOICEMAIL_PROVIDER, this.hasActiveVoicemailProvider)
     84           .apply();
     85       if (callback != null) {
     86         callback.onVisualVoicemailEnabledStatusChanged(this.hasActiveVoicemailProvider);
     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