blob: c1d87b2e60c73e1b1fa144f39eaff03d060b929d [file] [log] [blame]
Debashish Chatterjee0cb82242011-07-19 19:29:37 +01001/*
2 * Copyright (C) 2011 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.contacts;
18
19import com.android.common.io.MoreCloseables;
20import com.android.contacts.voicemail.VoicemailStatusHelperImpl;
21
22import android.content.AsyncQueryHandler;
23import android.database.Cursor;
24import android.net.Uri;
25import android.provider.VoicemailContract.Status;
26import android.provider.VoicemailContract.Voicemails;
27import android.util.Log;
28
29/**
30 * Class used by {@link CallDetailActivity} to fire async content resolver queries.
31 */
32public class CallDetailActivityQueryHandler extends AsyncQueryHandler {
33 private static final String TAG = "CallDetail";
34 private static final int QUERY_VOICEMAIL_CONTENT_TOKEN = 101;
35 private static final int QUERY_VOICEMAIL_STATUS_TOKEN = 102;
36
37 private final String[] VOICEMAIL_CONTENT_PROJECTION = new String[] {
38 Voicemails.SOURCE_PACKAGE,
39 Voicemails.HAS_CONTENT
40 };
41 private static final int SOURCE_PACKAGE_COLUMN_INDEX = 0;
42 private static final int HAS_CONTENT_COLUMN_INDEX = 1;
43
44 private final CallDetailActivity mCallDetailActivity;
45
46 public CallDetailActivityQueryHandler(CallDetailActivity callDetailActivity) {
47 super(callDetailActivity.getContentResolver());
48 mCallDetailActivity = callDetailActivity;
49 }
50
51 /**
52 * Fires a query to update voicemail status for the given voicemail record. On completion of the
53 * query a call to {@link CallDetailActivity#updateVoicemailStatusMessage(Cursor)} is made.
54 * <p>
55 * if this is a voicemail record then it makes up to two asynchronous content resolver queries.
56 * The first one to fetch voicemail content details and check if the voicemail record has audio.
57 * If the voicemail record does not have an audio yet then it fires the second query to get the
58 * voicemail status of the associated source.
59 */
60 public void startVoicemailStatusQuery(Uri voicemaiUri) {
61 startQuery(QUERY_VOICEMAIL_CONTENT_TOKEN, null, voicemaiUri, VOICEMAIL_CONTENT_PROJECTION,
62 null, null, null);
63 }
64
65 @Override
66 protected synchronized void onQueryComplete(int token, Object cookie, Cursor cursor) {
67 try {
68 if (token == QUERY_VOICEMAIL_CONTENT_TOKEN) {
69 // Query voicemail status only if this voicemail record does not have audio.
70 if (cursor.moveToFirst() && hasNoAudio(cursor)) {
71 startQuery(QUERY_VOICEMAIL_STATUS_TOKEN, null,
72 Status.buildSourceUri(getSourcePackage(cursor)),
73 VoicemailStatusHelperImpl.PROJECTION, null, null, null);
74 } else {
75 mCallDetailActivity.updateVoicemailStatusMessage(null);
76 }
77 } else if (token == QUERY_VOICEMAIL_STATUS_TOKEN) {
78 mCallDetailActivity.updateVoicemailStatusMessage(cursor);
79 } else {
80 Log.w(TAG, "Unknown query completed: ignoring: " + token);
81 }
82 } finally {
83 MoreCloseables.closeQuietly(cursor);
84 }
85 }
86
87 private boolean hasNoAudio(Cursor voicemailCursor) {
88 return voicemailCursor.getInt(HAS_CONTENT_COLUMN_INDEX) == 0;
89 }
90
91 private String getSourcePackage(Cursor voicemailCursor) {
92 return voicemailCursor.getString(SOURCE_PACKAGE_COLUMN_INDEX);
93 }
94}