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.app.calllog;
17 
18 import android.app.NotificationManager;
19 import android.content.AsyncQueryHandler;
20 import android.content.ContentResolver;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.net.Uri;
24 import android.provider.CallLog.Calls;
25 import android.support.annotation.MainThread;
26 import android.support.annotation.Nullable;
27 import com.android.dialer.app.R;
28 import com.android.dialer.common.Assert;
29 import com.android.dialer.notification.GroupedNotificationUtil;
30 
31 /** Handles asynchronous queries to the call log for voicemail. */
32 public class VoicemailQueryHandler extends AsyncQueryHandler {
33 
34   private static final String TAG = "VoicemailQueryHandler";
35 
36   /** The token for the query to mark all new voicemails as old. */
37   private static final int UPDATE_MARK_VOICEMAILS_AS_OLD_TOKEN = 50;
38 
39   private Context mContext;
40 
41   @MainThread
VoicemailQueryHandler(Context context, ContentResolver contentResolver)42   public VoicemailQueryHandler(Context context, ContentResolver contentResolver) {
43     super(contentResolver);
44     Assert.isMainThread();
45     mContext = context;
46   }
47 
48   /** Updates all new voicemails to mark them as old. */
markNewVoicemailsAsOld(@ullable Uri voicemailUri)49   public void markNewVoicemailsAsOld(@Nullable Uri voicemailUri) {
50     // Mark all "new" voicemails as not new anymore.
51     StringBuilder where = new StringBuilder();
52     where.append(Calls.NEW);
53     where.append(" = 1 AND ");
54     where.append(Calls.TYPE);
55     where.append(" = ?");
56 
57     if (voicemailUri != null) {
58       where.append(" AND ").append(Calls.VOICEMAIL_URI).append(" = ?");
59     }
60 
61     ContentValues values = new ContentValues(1);
62     values.put(Calls.NEW, "0");
63 
64     startUpdate(
65         UPDATE_MARK_VOICEMAILS_AS_OLD_TOKEN,
66         null,
67         Calls.CONTENT_URI_WITH_VOICEMAIL,
68         values,
69         where.toString(),
70         voicemailUri == null
71             ? new String[] {Integer.toString(Calls.VOICEMAIL_TYPE)}
72             : new String[] {Integer.toString(Calls.VOICEMAIL_TYPE), voicemailUri.toString()});
73 
74     GroupedNotificationUtil.removeNotification(
75         mContext.getSystemService(NotificationManager.class),
76         voicemailUri != null ? voicemailUri.toString() : null,
77         R.id.notification_visual_voicemail,
78         DefaultVoicemailNotifier.VISUAL_VOICEMAIL_NOTIFICATION_TAG);
79   }
80 }
81