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.spam; 18 19 import android.annotation.TargetApi; 20 import android.content.Context; 21 import android.database.Cursor; 22 import android.database.sqlite.SQLiteException; 23 import android.os.AsyncTask; 24 import android.os.Build.VERSION_CODES; 25 import android.provider.CallLog; 26 import android.provider.CallLog.Calls; 27 import android.support.annotation.NonNull; 28 import android.telephony.PhoneNumberUtils; 29 import android.text.TextUtils; 30 import com.android.dialer.common.LogUtil; 31 import com.android.dialer.common.concurrent.AsyncTaskExecutor; 32 import com.android.dialer.common.concurrent.AsyncTaskExecutors; 33 import com.android.dialer.telecom.TelecomUtil; 34 import com.android.dialer.util.PermissionsUtil; 35 import com.android.incallui.call.DialerCall; 36 import com.android.incallui.call.DialerCall.CallHistoryStatus; 37 import java.util.Objects; 38 39 /** Checks if the number is in the call history. */ 40 @TargetApi(VERSION_CODES.M) 41 public class NumberInCallHistoryTask extends AsyncTask<Void, Void, Integer> { 42 43 public static final String TASK_ID = "number_in_call_history_status"; 44 45 private final Context context; 46 private final Listener listener; 47 private final String number; 48 private final String countryIso; 49 NumberInCallHistoryTask( @onNull Context context, @NonNull Listener listener, String number, String countryIso)50 public NumberInCallHistoryTask( 51 @NonNull Context context, @NonNull Listener listener, String number, String countryIso) { 52 this.context = Objects.requireNonNull(context); 53 this.listener = Objects.requireNonNull(listener); 54 this.number = number; 55 this.countryIso = countryIso; 56 } 57 submitTask()58 public void submitTask() { 59 if (!PermissionsUtil.hasPhonePermissions(context)) { 60 return; 61 } 62 AsyncTaskExecutor asyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor(); 63 asyncTaskExecutor.submit(TASK_ID, this); 64 } 65 66 @Override 67 @CallHistoryStatus doInBackground(Void... params)68 public Integer doInBackground(Void... params) { 69 String numberToQuery = number; 70 String fieldToQuery = Calls.NUMBER; 71 String normalizedNumber = PhoneNumberUtils.formatNumberToE164(number, countryIso); 72 73 // If we can normalize the number successfully, look in "normalized_number" 74 // field instead. Otherwise, look for number in "number" field. 75 if (!TextUtils.isEmpty(normalizedNumber)) { 76 numberToQuery = normalizedNumber; 77 fieldToQuery = Calls.CACHED_NORMALIZED_NUMBER; 78 } 79 try (Cursor cursor = 80 context 81 .getContentResolver() 82 .query( 83 TelecomUtil.getCallLogUri(context), 84 new String[] {CallLog.Calls._ID}, 85 fieldToQuery + " = ?", 86 new String[] {numberToQuery}, 87 null)) { 88 return cursor != null && cursor.getCount() > 0 89 ? DialerCall.CALL_HISTORY_STATUS_PRESENT 90 : DialerCall.CALL_HISTORY_STATUS_NOT_PRESENT; 91 } catch (SQLiteException e) { 92 LogUtil.e("NumberInCallHistoryTask.doInBackground", "query call log error", e); 93 return DialerCall.CALL_HISTORY_STATUS_UNKNOWN; 94 } 95 } 96 97 @Override onPostExecute(@allHistoryStatus Integer callHistoryStatus)98 public void onPostExecute(@CallHistoryStatus Integer callHistoryStatus) { 99 listener.onComplete(callHistoryStatus); 100 } 101 102 /** Callback for the async task. */ 103 public interface Listener { 104 onComplete(@allHistoryStatus int callHistoryStatus)105 void onComplete(@CallHistoryStatus int callHistoryStatus); 106 } 107 } 108