1 /*
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 package com.android.dialer.calllog;
17 
18 import android.content.AsyncQueryHandler;
19 import android.content.ContentResolver;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.provider.CallLog.Calls;
24 import android.util.Log;
25 
26 /**
27  * Handles asynchronous queries to the call log for voicemail.
28  */
29 public class VoicemailQueryHandler extends AsyncQueryHandler {
30     private static final String TAG = "VoicemailQueryHandler";
31 
32     /** The token for the query to mark all new voicemails as old. */
33     private static final int UPDATE_MARK_VOICEMAILS_AS_OLD_TOKEN = 50;
34     private Context mContext;
35 
VoicemailQueryHandler(Context context, ContentResolver contentResolver)36     public VoicemailQueryHandler(Context context, ContentResolver contentResolver) {
37         super(contentResolver);
38         mContext = context;
39     }
40 
41     /** Updates all new voicemails to mark them as old. */
markNewVoicemailsAsOld()42     public void markNewVoicemailsAsOld() {
43         // Mark all "new" voicemails as not new anymore.
44         StringBuilder where = new StringBuilder();
45         where.append(Calls.NEW);
46         where.append(" = 1 AND ");
47         where.append(Calls.TYPE);
48         where.append(" = ?");
49 
50         ContentValues values = new ContentValues(1);
51         values.put(Calls.NEW, "0");
52 
53         startUpdate(UPDATE_MARK_VOICEMAILS_AS_OLD_TOKEN, null, Calls.CONTENT_URI_WITH_VOICEMAIL,
54                 values, where.toString(), new String[]{ Integer.toString(Calls.VOICEMAIL_TYPE) });
55     }
56 
57     @Override
onUpdateComplete(int token, Object cookie, int result)58     protected void onUpdateComplete(int token, Object cookie, int result) {
59         if (token == UPDATE_MARK_VOICEMAILS_AS_OLD_TOKEN) {
60             if (mContext != null) {
61                 Intent serviceIntent = new Intent(mContext, CallLogNotificationsService.class);
62                 serviceIntent.setAction(
63                         CallLogNotificationsService.ACTION_UPDATE_VOICEMAIL_NOTIFICATIONS);
64                 mContext.startService(serviceIntent);
65             } else {
66                 Log.w(TAG, "Unknown update completed: ignoring: " + token);
67             }
68         }
69     }
70 }
71