Home | History | Annotate | Download | only in calllog
      1 /*
      2  * Copyright (C) 2015 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 
     17 package com.android.dialer.app.calllog;
     18 
     19 import android.annotation.TargetApi;
     20 import android.content.ContentValues;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.net.Uri;
     24 import android.os.AsyncTask;
     25 import android.os.Build.VERSION_CODES;
     26 import android.provider.CallLog;
     27 import android.provider.VoicemailContract.Voicemails;
     28 import android.support.annotation.NonNull;
     29 import android.support.annotation.Nullable;
     30 import android.text.TextUtils;
     31 import com.android.dialer.common.LogUtil;
     32 import com.android.dialer.common.concurrent.AsyncTaskExecutor;
     33 import com.android.dialer.common.concurrent.AsyncTaskExecutors;
     34 import com.android.dialer.util.PermissionsUtil;
     35 import com.android.voicemail.VoicemailClient;
     36 
     37 @TargetApi(VERSION_CODES.M)
     38 public class CallLogAsyncTaskUtil {
     39 
     40   private static final String TAG = "CallLogAsyncTaskUtil";
     41   private static AsyncTaskExecutor asyncTaskExecutor;
     42 
     43   private static void initTaskExecutor() {
     44     asyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
     45   }
     46 
     47   public static void markVoicemailAsRead(
     48       @NonNull final Context context, @NonNull final Uri voicemailUri) {
     49     LogUtil.enterBlock("CallLogAsyncTaskUtil.markVoicemailAsRead, voicemailUri: " + voicemailUri);
     50     if (asyncTaskExecutor == null) {
     51       initTaskExecutor();
     52     }
     53 
     54     asyncTaskExecutor.submit(
     55         Tasks.MARK_VOICEMAIL_READ,
     56         new AsyncTask<Void, Void, Void>() {
     57           @Override
     58           public Void doInBackground(Void... params) {
     59             ContentValues values = new ContentValues();
     60             values.put(Voicemails.IS_READ, true);
     61             // "External" changes to the database will be automatically marked as dirty, but this
     62             // voicemail might be from dialer so it need to be marked manually.
     63             values.put(Voicemails.DIRTY, 1);
     64             if (context
     65                     .getContentResolver()
     66                     .update(voicemailUri, values, Voicemails.IS_READ + " = 0", null)
     67                 > 0) {
     68               uploadVoicemailLocalChangesToServer(context);
     69               CallLogNotificationsService.markAllNewVoicemailsAsOld(context);
     70             }
     71             return null;
     72           }
     73         });
     74   }
     75 
     76   public static void deleteVoicemail(
     77       @NonNull final Context context,
     78       final Uri voicemailUri,
     79       @Nullable final CallLogAsyncTaskListener callLogAsyncTaskListener) {
     80     if (asyncTaskExecutor == null) {
     81       initTaskExecutor();
     82     }
     83 
     84     asyncTaskExecutor.submit(
     85         Tasks.DELETE_VOICEMAIL,
     86         new AsyncTask<Void, Void, Void>() {
     87           @Override
     88           public Void doInBackground(Void... params) {
     89             deleteVoicemailSynchronous(context, voicemailUri);
     90             return null;
     91           }
     92 
     93           @Override
     94           public void onPostExecute(Void result) {
     95             if (callLogAsyncTaskListener != null) {
     96               callLogAsyncTaskListener.onDeleteVoicemail();
     97             }
     98           }
     99         });
    100   }
    101 
    102   public static void deleteVoicemailSynchronous(Context context, Uri voicemailUri) {
    103     ContentValues values = new ContentValues();
    104     values.put(Voicemails.DELETED, "1");
    105     context.getContentResolver().update(voicemailUri, values, null, null);
    106     // TODO(a bug): check which source package is changed. Don't need
    107     // to upload changes on foreign voicemails, they will get a PROVIDER_CHANGED
    108     uploadVoicemailLocalChangesToServer(context);
    109   }
    110 
    111   public static void markCallAsRead(@NonNull final Context context, @NonNull final long[] callIds) {
    112     if (!PermissionsUtil.hasPhonePermissions(context)
    113         || !PermissionsUtil.hasCallLogWritePermissions(context)) {
    114       return;
    115     }
    116     if (asyncTaskExecutor == null) {
    117       initTaskExecutor();
    118     }
    119 
    120     asyncTaskExecutor.submit(
    121         Tasks.MARK_CALL_READ,
    122         new AsyncTask<Void, Void, Void>() {
    123           @Override
    124           public Void doInBackground(Void... params) {
    125 
    126             StringBuilder where = new StringBuilder();
    127             where.append(CallLog.Calls.TYPE).append(" = ").append(CallLog.Calls.MISSED_TYPE);
    128             where.append(" AND ");
    129 
    130             Long[] callIdLongs = new Long[callIds.length];
    131             for (int i = 0; i < callIds.length; i++) {
    132               callIdLongs[i] = callIds[i];
    133             }
    134             where
    135                 .append(CallLog.Calls._ID)
    136                 .append(" IN (" + TextUtils.join(",", callIdLongs) + ")");
    137 
    138             ContentValues values = new ContentValues(1);
    139             values.put(CallLog.Calls.IS_READ, "1");
    140             context
    141                 .getContentResolver()
    142                 .update(CallLog.Calls.CONTENT_URI, values, where.toString(), null);
    143             return null;
    144           }
    145         });
    146   }
    147 
    148   /** The enumeration of {@link AsyncTask} objects used in this class. */
    149   public enum Tasks {
    150     DELETE_VOICEMAIL,
    151     DELETE_CALL,
    152     MARK_VOICEMAIL_READ,
    153     MARK_CALL_READ,
    154     GET_CALL_DETAILS,
    155     UPDATE_DURATION,
    156   }
    157 
    158   public interface CallLogAsyncTaskListener {
    159     void onDeleteVoicemail();
    160   }
    161 
    162   private static void uploadVoicemailLocalChangesToServer(Context context) {
    163     Intent intent = new Intent(VoicemailClient.ACTION_UPLOAD);
    164     intent.setPackage(context.getPackageName());
    165     context.sendBroadcast(intent);
    166   }
    167 }
    168