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