Home | History | Annotate | Download | only in sync
      1 /*
      2  * Copyright (C) 2015 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 package com.android.phone.vvm.omtp.sync;
     17 
     18 import android.content.ContentResolver;
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.net.Uri;
     22 import android.provider.VoicemailContract;
     23 import android.provider.VoicemailContract.Status;
     24 import android.telecom.PhoneAccountHandle;
     25 
     26 /**
     27  * Construct queries to interact with the voicemail status table.
     28  */
     29 public class VoicemailStatusQueryHelper {
     30 
     31     final static String[] PROJECTION = new String[] {
     32             Status._ID,                        // 0
     33             Status.CONFIGURATION_STATE,        // 1
     34             Status.NOTIFICATION_CHANNEL_STATE, // 2
     35             Status.SOURCE_PACKAGE              // 3
     36    };
     37 
     38     public static final int _ID = 0;
     39     public static final int CONFIGURATION_STATE = 1;
     40     public static final int NOTIFICATION_CHANNEL_STATE = 2;
     41     public static final int SOURCE_PACKAGE = 3;
     42 
     43     private Context mContext;
     44     private ContentResolver mContentResolver;
     45     private Uri mSourceUri;
     46 
     47     public VoicemailStatusQueryHelper(Context context) {
     48         mContext = context;
     49         mContentResolver = context.getContentResolver();
     50         mSourceUri = VoicemailContract.Status.buildSourceUri(mContext.getPackageName());
     51     }
     52 
     53     /**
     54      * Check if the configuration state for the voicemail source is "ok", meaning that the
     55      * source is set up.
     56      *
     57      * @param phoneAccount The phone account for the voicemail source to check.
     58      * @return {@code true} if the voicemail source is configured, {@code} false otherwise,
     59      * including if the voicemail source is not registered in the table.
     60      */
     61     public boolean isVoicemailSourceConfigured(PhoneAccountHandle phoneAccount) {
     62         return isFieldEqualTo(phoneAccount, CONFIGURATION_STATE, Status.CONFIGURATION_STATE_OK);
     63     }
     64 
     65     /**
     66      * Check if the notifications channel of a voicemail source is active. That is, when a new
     67      * voicemail is available, if the server able to notify the device.
     68      *
     69      * @return {@code true} if notifications channel is active, {@code false} otherwise.
     70      */
     71     public boolean isNotificationsChannelActive(PhoneAccountHandle phoneAccount) {
     72         return isFieldEqualTo(phoneAccount, NOTIFICATION_CHANNEL_STATE,
     73                 Status.NOTIFICATION_CHANNEL_STATE_OK);
     74     }
     75 
     76     /**
     77      * Check if a field for an entry in the status table is equal to a specific value.
     78      *
     79      * @param phoneAccount The phone account of the voicemail source to query for.
     80      * @param columnIndex The column index of the field in the returned query.
     81      * @param value The value to compare against.
     82      * @return {@code true} if the stored value is equal to the provided value. {@code false}
     83      * otherwise.
     84      */
     85     private boolean isFieldEqualTo(PhoneAccountHandle phoneAccount, int columnIndex, int value) {
     86         Cursor cursor = null;
     87         if (phoneAccount != null) {
     88             String phoneAccountComponentName = phoneAccount.getComponentName().flattenToString();
     89             String phoneAccountId = phoneAccount.getId();
     90             if (phoneAccountComponentName == null || phoneAccountId == null) {
     91                 return false;
     92             }
     93             try {
     94                 String whereClause =
     95                         Status.PHONE_ACCOUNT_COMPONENT_NAME + "=? AND " +
     96                         Status.PHONE_ACCOUNT_ID + "=? AND " + Status.SOURCE_PACKAGE + "=?";
     97                 String[] whereArgs = { phoneAccountComponentName, phoneAccountId,
     98                         mContext.getPackageName()};
     99                 cursor = mContentResolver.query(
    100                         mSourceUri, PROJECTION, whereClause, whereArgs, null);
    101                 if (cursor != null && cursor.moveToFirst()) {
    102                     return cursor.getInt(columnIndex) == value;
    103                 }
    104             }
    105             finally {
    106                 if (cursor != null) {
    107                     cursor.close();
    108                 }
    109             }
    110         }
    111         return false;
    112     }
    113 }
    114