Home | History | Annotate | Download | only in calllog
      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.content.AsyncQueryHandler;
     19 import android.content.ContentResolver;
     20 import android.content.ContentValues;
     21 import android.content.Context;
     22 import android.net.Uri;
     23 import android.provider.CallLog.Calls;
     24 import android.support.annotation.MainThread;
     25 import android.support.annotation.NonNull;
     26 import android.support.annotation.Nullable;
     27 import android.support.annotation.WorkerThread;
     28 import com.android.dialer.common.Assert;
     29 import com.android.dialer.common.LogUtil;
     30 import com.android.dialer.common.concurrent.ThreadUtil;
     31 
     32 /** Handles asynchronous queries to the call log for voicemail. */
     33 public class VoicemailQueryHandler extends AsyncQueryHandler {
     34 
     35   /** The token for the query to mark all new voicemails as old. */
     36   private static final int UPDATE_MARK_VOICEMAILS_AS_OLD_TOKEN = 50;
     37 
     38   @MainThread
     39   private VoicemailQueryHandler(ContentResolver contentResolver) {
     40     super(contentResolver);
     41     Assert.isMainThread();
     42   }
     43 
     44   @WorkerThread
     45   public static void markAllNewVoicemailsAsRead(final @NonNull Context context) {
     46     ThreadUtil.postOnUiThread(
     47         () -> {
     48           new VoicemailQueryHandler(context.getContentResolver())
     49               .markNewVoicemailsAsOld(context, null);
     50         });
     51   }
     52 
     53   @WorkerThread
     54   public static void markSingleNewVoicemailAsRead(
     55       final @NonNull Context context, final Uri voicemailUri) {
     56     if (voicemailUri == null) {
     57       LogUtil.e("VoicemailQueryHandler.markSingleNewVoicemailAsRead", "voicemail URI is null");
     58       return;
     59     }
     60     ThreadUtil.postOnUiThread(
     61         () -> {
     62           new VoicemailQueryHandler(context.getContentResolver())
     63               .markNewVoicemailsAsOld(context, voicemailUri);
     64         });
     65   }
     66 
     67   /** Updates all new voicemails to mark them as old. */
     68   private void markNewVoicemailsAsOld(Context context, @Nullable Uri voicemailUri) {
     69     // Mark all "new" voicemails as not new anymore.
     70     StringBuilder where = new StringBuilder();
     71     where.append(Calls.NEW);
     72     where.append(" = 1 AND ");
     73     where.append(Calls.TYPE);
     74     where.append(" = ?");
     75 
     76     if (voicemailUri != null) {
     77       where.append(" AND ").append(Calls.VOICEMAIL_URI).append(" = ?");
     78     }
     79 
     80     ContentValues values = new ContentValues(1);
     81     values.put(Calls.NEW, "0");
     82 
     83     startUpdate(
     84         UPDATE_MARK_VOICEMAILS_AS_OLD_TOKEN,
     85         null,
     86         Calls.CONTENT_URI_WITH_VOICEMAIL,
     87         values,
     88         where.toString(),
     89         voicemailUri == null
     90             ? new String[] {Integer.toString(Calls.VOICEMAIL_TYPE)}
     91             : new String[] {Integer.toString(Calls.VOICEMAIL_TYPE), voicemailUri.toString()});
     92 
     93     // No more notifications, stop monitoring the voicemail provider
     94     VoicemailNotificationJobService.cancelJob(context);
     95   }
     96 }
     97