summaryrefslogtreecommitdiff
path: root/java/com/android/voicemailomtp/sync/VoicemailStatusQueryHelper.java
blob: 89ba0b49450e4a3af5f0659ed58cd94c2ebb62b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package com.android.voicemailomtp.sync;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.VoicemailContract;
import android.provider.VoicemailContract.Status;
import android.telecom.PhoneAccountHandle;

/**
 * Construct queries to interact with the voicemail status table.
 */
public class VoicemailStatusQueryHelper {

    final static String[] PROJECTION = new String[] {
            Status._ID,                        // 0
            Status.CONFIGURATION_STATE,        // 1
            Status.NOTIFICATION_CHANNEL_STATE, // 2
            Status.SOURCE_PACKAGE              // 3
   };

    public static final int _ID = 0;
    public static final int CONFIGURATION_STATE = 1;
    public static final int NOTIFICATION_CHANNEL_STATE = 2;
    public static final int SOURCE_PACKAGE = 3;

    private Context mContext;
    private ContentResolver mContentResolver;
    private Uri mSourceUri;

    public VoicemailStatusQueryHelper(Context context) {
        mContext = context;
        mContentResolver = context.getContentResolver();
        mSourceUri = VoicemailContract.Status.buildSourceUri(mContext.getPackageName());
    }

    /**
     * Check if the configuration state for the voicemail source is "ok", meaning that the
     * source is set up.
     *
     * @param phoneAccount The phone account for the voicemail source to check.
     * @return {@code true} if the voicemail source is configured, {@code} false otherwise,
     * including if the voicemail source is not registered in the table.
     */
    public boolean isVoicemailSourceConfigured(PhoneAccountHandle phoneAccount) {
        return isFieldEqualTo(phoneAccount, CONFIGURATION_STATE, Status.CONFIGURATION_STATE_OK);
    }

    /**
     * Check if the notifications channel of a voicemail source is active. That is, when a new
     * voicemail is available, if the server able to notify the device.
     *
     * @return {@code true} if notifications channel is active, {@code false} otherwise.
     */
    public boolean isNotificationsChannelActive(PhoneAccountHandle phoneAccount) {
        return isFieldEqualTo(phoneAccount, NOTIFICATION_CHANNEL_STATE,
                Status.NOTIFICATION_CHANNEL_STATE_OK);
    }

    /**
     * Check if a field for an entry in the status table is equal to a specific value.
     *
     * @param phoneAccount The phone account of the voicemail source to query for.
     * @param columnIndex The column index of the field in the returned query.
     * @param value The value to compare against.
     * @return {@code true} if the stored value is equal to the provided value. {@code false}
     * otherwise.
     */
    private boolean isFieldEqualTo(PhoneAccountHandle phoneAccount, int columnIndex, int value) {
        Cursor cursor = null;
        if (phoneAccount != null) {
            String phoneAccountComponentName = phoneAccount.getComponentName().flattenToString();
            String phoneAccountId = phoneAccount.getId();
            if (phoneAccountComponentName == null || phoneAccountId == null) {
                return false;
            }
            try {
                String whereClause =
                        Status.PHONE_ACCOUNT_COMPONENT_NAME + "=? AND " +
                        Status.PHONE_ACCOUNT_ID + "=? AND " + Status.SOURCE_PACKAGE + "=?";
                String[] whereArgs = { phoneAccountComponentName, phoneAccountId,
                        mContext.getPackageName()};
                cursor = mContentResolver.query(
                        mSourceUri, PROJECTION, whereClause, whereArgs, null);
                if (cursor != null && cursor.moveToFirst()) {
                    return cursor.getInt(columnIndex) == value;
                }
            }
            finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }
        return false;
    }
}