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.server.telecom.callfiltering; 18 19 import android.content.Context; 20 import android.os.Handler; 21 import android.os.Looper; 22 23 import com.android.internal.annotations.VisibleForTesting; 24 import com.android.server.telecom.Call; 25 import com.android.server.telecom.Log; 26 import com.android.server.telecom.Runnable; 27 import com.android.server.telecom.TelecomSystem; 28 import com.android.server.telecom.Timeouts; 29 30 import java.util.List; 31 32 public class IncomingCallFilter implements CallFilterResultCallback { 33 34 public interface CallFilter { startFilterLookup(Call call, CallFilterResultCallback listener)35 void startFilterLookup(Call call, CallFilterResultCallback listener); 36 } 37 38 private final TelecomSystem.SyncRoot mTelecomLock; 39 private final Context mContext; 40 private final Handler mHandler = new Handler(Looper.getMainLooper()); 41 private final List<CallFilter> mFilters; 42 private final Call mCall; 43 private final CallFilterResultCallback mListener; 44 private final Timeouts.Adapter mTimeoutsAdapter; 45 46 private CallFilteringResult mResult = new CallFilteringResult( 47 true, // shouldAllowCall 48 false, // shouldReject 49 true, // shouldAddToCallLog 50 true // shouldShowNotification 51 ); 52 53 private boolean mIsPending = true; 54 private int mNumPendingFilters; 55 IncomingCallFilter(Context context, CallFilterResultCallback listener, Call call, TelecomSystem.SyncRoot lock, Timeouts.Adapter timeoutsAdapter, List<CallFilter> filters)56 public IncomingCallFilter(Context context, CallFilterResultCallback listener, Call call, 57 TelecomSystem.SyncRoot lock, Timeouts.Adapter timeoutsAdapter, 58 List<CallFilter> filters) { 59 mContext = context; 60 mListener = listener; 61 mCall = call; 62 mTelecomLock = lock; 63 mFilters = filters; 64 mNumPendingFilters = filters.size(); 65 mTimeoutsAdapter = timeoutsAdapter; 66 } 67 performFiltering()68 public void performFiltering() { 69 Log.event(mCall, Log.Events.FILTERING_INITIATED); 70 for (CallFilter filter : mFilters) { 71 filter.startFilterLookup(mCall, this); 72 } 73 mHandler.postDelayed(new Runnable("ICF.pFTO") { // performFiltering time-out 74 @Override 75 public void loggedRun() { 76 // synchronized to prevent a race on mResult and to enter into Telecom. 77 synchronized (mTelecomLock) { 78 if (mIsPending) { 79 Log.i(IncomingCallFilter.this, "Call filtering has timed out."); 80 Log.event(mCall, Log.Events.FILTERING_TIMED_OUT); 81 mListener.onCallFilteringComplete(mCall, mResult); 82 mIsPending = false; 83 } 84 } 85 } 86 }.prepare(), mTimeoutsAdapter.getCallScreeningTimeoutMillis(mContext.getContentResolver())); 87 } 88 onCallFilteringComplete(Call call, CallFilteringResult result)89 public void onCallFilteringComplete(Call call, CallFilteringResult result) { 90 synchronized (mTelecomLock) { // synchronizing to prevent race on mResult 91 mNumPendingFilters--; 92 mResult = result.combine(mResult); 93 if (mNumPendingFilters == 0) { 94 mHandler.post(new Runnable("ICF.oCFC") { 95 @Override 96 public void loggedRun() { 97 // synchronized to enter into Telecom. 98 synchronized (mTelecomLock) { 99 if (mIsPending) { 100 Log.event(mCall, Log.Events.FILTERING_COMPLETED, mResult); 101 mListener.onCallFilteringComplete(mCall, mResult); 102 mIsPending = false; 103 } 104 } 105 } 106 }.prepare()); 107 } 108 } 109 } 110 111 /** 112 * Returns the handler, for testing purposes. 113 */ 114 @VisibleForTesting getHandler()115 public Handler getHandler() { 116 return mHandler; 117 } 118 } 119