1 /*
2  * Copyright (C) 2016 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.incallui.legacyblocking;
18 
19 import android.Manifest.permission;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.database.Cursor;
23 import android.os.AsyncTask;
24 import android.provider.CallLog;
25 import android.support.v4.content.ContextCompat;
26 import com.android.dialer.common.LogUtil;
27 import com.android.dialer.telecom.TelecomUtil;
28 import java.util.Objects;
29 
30 /**
31  * Deletes a blocked call from the call log. This is only used on Android Marshmallow. On later
32  * versions of the OS, call blocking is implemented in the system and there's no need to mess with
33  * the call log.
34  */
35 public class DeleteBlockedCallTask extends AsyncTask<Void, Void, Long> {
36 
37   public static final String IDENTIFIER = "DeleteBlockedCallTask";
38 
39   // Try to identify if a call log entry corresponds to a number which was blocked. We match by
40   // by comparing its creation time to the time it was added in the InCallUi and seeing if they
41   // fall within a certain threshold.
42   private static final int MATCH_BLOCKED_CALL_THRESHOLD_MS = 3000;
43 
44   private final Context context;
45   private final Listener listener;
46   private final String number;
47   private final long timeAddedMillis;
48 
49   /**
50    * Creates the task to delete the new {@link CallLog} entry from the given blocked number.
51    *
52    * @param number The blocked number.
53    * @param timeAddedMillis The time at which the call from the blocked number was placed.
54    */
DeleteBlockedCallTask( Context context, Listener listener, String number, long timeAddedMillis)55   public DeleteBlockedCallTask(
56       Context context, Listener listener, String number, long timeAddedMillis) {
57     this.context = Objects.requireNonNull(context);
58     this.listener = Objects.requireNonNull(listener);
59     this.number = number;
60     this.timeAddedMillis = timeAddedMillis;
61   }
62 
63   @Override
doInBackground(Void... params)64   public Long doInBackground(Void... params) {
65     if (ContextCompat.checkSelfPermission(context, permission.READ_CALL_LOG)
66             != PackageManager.PERMISSION_GRANTED
67         || ContextCompat.checkSelfPermission(context, permission.WRITE_CALL_LOG)
68             != PackageManager.PERMISSION_GRANTED) {
69       LogUtil.i("DeleteBlockedCallTask.doInBackground", "missing call log permissions");
70       return -1L;
71     }
72 
73     // First, lookup the call log entry of the most recent call with this number.
74     try (Cursor cursor =
75         context
76             .getContentResolver()
77             .query(
78                 TelecomUtil.getCallLogUri(context),
79                 CallLogDeleteBlockedCallQuery.PROJECTION,
80                 CallLog.Calls.NUMBER + "= ?",
81                 new String[] {number},
82                 CallLog.Calls.DATE + " DESC LIMIT 1")) {
83 
84       // If match is found, delete this call log entry and return the call log entry id.
85       if (cursor != null && cursor.moveToFirst()) {
86         long creationTime = cursor.getLong(CallLogDeleteBlockedCallQuery.DATE_COLUMN_INDEX);
87         if (timeAddedMillis > creationTime
88             && timeAddedMillis - creationTime < MATCH_BLOCKED_CALL_THRESHOLD_MS) {
89           long callLogEntryId = cursor.getLong(CallLogDeleteBlockedCallQuery.ID_COLUMN_INDEX);
90           context
91               .getContentResolver()
92               .delete(
93                   TelecomUtil.getCallLogUri(context),
94                   CallLog.Calls._ID + " IN (" + callLogEntryId + ")",
95                   null);
96           return callLogEntryId;
97         }
98       }
99     }
100     return -1L;
101   }
102 
103   @Override
onPostExecute(Long callLogEntryId)104   public void onPostExecute(Long callLogEntryId) {
105     listener.onDeleteBlockedCallTaskComplete(callLogEntryId >= 0);
106   }
107 
108   /** Callback invoked when delete is complete. */
109   public interface Listener {
110 
onDeleteBlockedCallTaskComplete(boolean didFindEntry)111     void onDeleteBlockedCallTaskComplete(boolean didFindEntry);
112   }
113 
114   private static class CallLogDeleteBlockedCallQuery {
115 
116     static final String[] PROJECTION = new String[] {CallLog.Calls._ID, CallLog.Calls.DATE};
117 
118     static final int ID_COLUMN_INDEX = 0;
119     static final int DATE_COLUMN_INDEX = 1;
120   }
121 }
122