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.concurrent.AsyncTaskExecutor;
     32 import com.android.dialer.common.concurrent.AsyncTaskExecutors;
     33 import com.android.dialer.util.PermissionsUtil;
     34 import com.android.voicemail.VoicemailClient;
     35 
     36 @TargetApi(VERSION_CODES.M)
     37 public class CallLogAsyncTaskUtil {
     38 
     39   private static final String TAG = "CallLogAsyncTaskUtil";
     40   private static AsyncTaskExecutor sAsyncTaskExecutor;
     41 
     42   private static void initTaskExecutor() {
     43     sAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
     44   }
     45 
     46   public static void markVoicemailAsRead(
     47       @NonNull final Context context, @NonNull final Uri voicemailUri) {
     48     if (sAsyncTaskExecutor == null) {
     49       initTaskExecutor();
     50     }
     51 
     52     sAsyncTaskExecutor.submit(
     53         Tasks.MARK_VOICEMAIL_READ,
     54         new AsyncTask<Void, Void, Void>() {
     55           @Override
     56           public Void doInBackground(Void... params) {
     57             ContentValues values = new ContentValues();
     58             values.put(Voicemails.IS_READ, true);
     59             // "External" changes to the database will be automatically marked as dirty, but this
     60             // voicemail might be from dialer so it need to be marked manually.
     61             values.put(Voicemails.DIRTY, 1);
     62             if (context
     63                     .getContentResolver()
     64                     .update(voicemailUri, values, Voicemails.IS_READ + " = 0", null)
     65                 > 0) {
     66               uploadVoicemailLocalChangesToServer(context);
     67             }
     68 
     69             Intent intent = new Intent(context, CallLogNotificationsService.class);
     70             intent.setAction(CallLogNotificationsService.ACTION_MARK_NEW_VOICEMAILS_AS_OLD);
     71             context.startService(intent);
     72             return null;
     73           }
     74         });
     75   }
     76 
     77   public static void deleteVoicemail(
     78       @NonNull final Context context,
     79       final Uri voicemailUri,
     80       @Nullable final CallLogAsyncTaskListener callLogAsyncTaskListener) {
     81     if (sAsyncTaskExecutor == null) {
     82       initTaskExecutor();
     83     }
     84 
     85     sAsyncTaskExecutor.submit(
     86         Tasks.DELETE_VOICEMAIL,
     87         new AsyncTask<Void, Void, Void>() {
     88           @Override
     89           public Void doInBackground(Void... params) {
     90             deleteVoicemailSynchronous(context, voicemailUri);
     91             return null;
     92           }
     93 
     94           @Override
     95           public void onPostExecute(Void result) {
     96             if (callLogAsyncTaskListener != null) {
     97               callLogAsyncTaskListener.onDeleteVoicemail();
     98             }
     99           }
    100         });
    101   }
    102 
    103   public static void deleteVoicemailSynchronous(Context context, Uri voicemailUri) {
    104     ContentValues values = new ContentValues();
    105     values.put(Voicemails.DELETED, "1");
    106     context.getContentResolver().update(voicemailUri, values, null, null);
    107     // TODO(b/35440541): check which source package is changed. Don't need
    108     // to upload changes on foreign voicemails, they will get a PROVIDER_CHANGED
    109     uploadVoicemailLocalChangesToServer(context);
    110   }
    111 
    112   public static void markCallAsRead(@NonNull final Context context, @NonNull final long[] callIds) {
    113     if (!PermissionsUtil.hasPhonePermissions(context)) {
    114       return;
    115     }
    116     if (sAsyncTaskExecutor == null) {
    117       initTaskExecutor();
    118     }
    119 
    120     sAsyncTaskExecutor.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