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