summaryrefslogtreecommitdiff
path: root/java/com/android/incallui/spam/NumberInCallHistoryTask.java
blob: 886933fb505c7be25afe742f1da4e1c018bb9be2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.incallui.spam;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.AsyncTask;
import android.os.Build.VERSION_CODES;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.support.annotation.NonNull;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import com.android.dialer.common.LogUtil;
import com.android.dialer.common.concurrent.AsyncTaskExecutor;
import com.android.dialer.common.concurrent.AsyncTaskExecutors;
import com.android.dialer.telecom.TelecomUtil;
import com.android.dialer.util.PermissionsUtil;
import com.android.incallui.call.DialerCall;
import com.android.incallui.call.DialerCall.CallHistoryStatus;
import java.util.Objects;

/** Checks if the number is in the call history. */
@TargetApi(VERSION_CODES.M)
public class NumberInCallHistoryTask extends AsyncTask<Void, Void, Integer> {

  public static final String TASK_ID = "number_in_call_history_status";

  private final Context context;
  private final Listener listener;
  private final String number;
  private final String countryIso;

  public NumberInCallHistoryTask(
      @NonNull Context context, @NonNull Listener listener, String number, String countryIso) {
    this.context = Objects.requireNonNull(context);
    this.listener = Objects.requireNonNull(listener);
    this.number = number;
    this.countryIso = countryIso;
  }

  public void submitTask() {
    if (!PermissionsUtil.hasPhonePermissions(context)) {
      return;
    }
    AsyncTaskExecutor asyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor();
    asyncTaskExecutor.submit(TASK_ID, this);
  }

  @Override
  @CallHistoryStatus
  public Integer doInBackground(Void... params) {
    String numberToQuery = number;
    String fieldToQuery = Calls.NUMBER;
    String normalizedNumber = PhoneNumberUtils.formatNumberToE164(number, countryIso);

    // If we can normalize the number successfully, look in "normalized_number"
    // field instead. Otherwise, look for number in "number" field.
    if (!TextUtils.isEmpty(normalizedNumber)) {
      numberToQuery = normalizedNumber;
      fieldToQuery = Calls.CACHED_NORMALIZED_NUMBER;
    }
    try (Cursor cursor =
        context
            .getContentResolver()
            .query(
                TelecomUtil.getCallLogUri(context),
                new String[] {CallLog.Calls._ID},
                fieldToQuery + " = ?",
                new String[] {numberToQuery},
                null)) {
      return cursor != null && cursor.getCount() > 0
          ? DialerCall.CALL_HISTORY_STATUS_PRESENT
          : DialerCall.CALL_HISTORY_STATUS_NOT_PRESENT;
    } catch (SQLiteException e) {
      LogUtil.e("NumberInCallHistoryTask.doInBackground", "query call log error", e);
      return DialerCall.CALL_HISTORY_STATUS_UNKNOWN;
    }
  }

  @Override
  public void onPostExecute(@CallHistoryStatus Integer callHistoryStatus) {
    listener.onComplete(callHistoryStatus);
  }

  /** Callback for the async task. */
  public interface Listener {

    void onComplete(@CallHistoryStatus int callHistoryStatus);
  }
}