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