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.server.telecom.callfiltering; 18 19import android.content.Context; 20import android.os.AsyncTask; 21 22import com.android.server.telecom.Call; 23import com.android.server.telecom.Log; 24import com.android.server.telecom.Session; 25 26/** 27 * An {@link AsyncTask} that checks if a call needs to be blocked. 28 * <p> An {@link AsyncTask} is used to perform the block check to avoid blocking the main thread. 29 * The block check itself is performed in the {@link AsyncTask#doInBackground(Object[])}. 30 */ 31public class AsyncBlockCheckFilter extends AsyncTask<String, Void, Boolean> 32 implements IncomingCallFilter.CallFilter { 33 private final Context mContext; 34 private final BlockCheckerAdapter mBlockCheckerAdapter; 35 private Call mIncomingCall; 36 private Session mBackgroundTaskSubsession; 37 private Session mPostExecuteSubsession; 38 private CallFilterResultCallback mCallback; 39 40 public AsyncBlockCheckFilter(Context context, BlockCheckerAdapter blockCheckerAdapter) { 41 mContext = context; 42 mBlockCheckerAdapter = blockCheckerAdapter; 43 } 44 45 @Override 46 public void startFilterLookup(Call call, CallFilterResultCallback callback) { 47 mCallback = callback; 48 mIncomingCall = call; 49 String number = call.getHandle() == null ? 50 null : call.getHandle().getSchemeSpecificPart(); 51 this.execute(number); 52 } 53 54 @Override 55 protected void onPreExecute() { 56 mBackgroundTaskSubsession = Log.createSubsession(); 57 mPostExecuteSubsession = Log.createSubsession(); 58 } 59 60 @Override 61 protected Boolean doInBackground(String... params) { 62 try { 63 Log.continueSession(mBackgroundTaskSubsession, "ABCF.dIB"); 64 Log.event(mIncomingCall, Log.Events.BLOCK_CHECK_INITIATED); 65 return mBlockCheckerAdapter.isBlocked(mContext, params[0]); 66 } finally { 67 Log.endSession(); 68 } 69 } 70 71 @Override 72 protected void onPostExecute(Boolean isBlocked) { 73 Log.continueSession(mPostExecuteSubsession, "ABCF.oPE"); 74 try { 75 CallFilteringResult result; 76 if (isBlocked) { 77 result = new CallFilteringResult( 78 false, // shouldAllowCall 79 true, //shouldReject 80 false, //shouldAddToCallLog 81 false // shouldShowNotification 82 ); 83 } else { 84 result = new CallFilteringResult( 85 true, // shouldAllowCall 86 false, // shouldReject 87 true, // shouldAddToCallLog 88 true // shouldShowNotification 89 ); 90 } 91 Log.event(mIncomingCall, Log.Events.BLOCK_CHECK_FINISHED, result); 92 mCallback.onCallFilteringComplete(mIncomingCall, result); 93 } finally { 94 Log.endSession(); 95 } 96 } 97} 98