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 
     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 {
     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 
     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 
     68     public void performFiltering() {
     69         Log.event(mCall, Log.Events.FILTERING_INITIATED);
     70         for (CallFilter filter : mFilters) {
     71             filter.startFilterLookup(mCall, this);
     72         }
     73         // synchronized to prevent a race on mResult and to enter into Telecom.
     74         mHandler.postDelayed(new Runnable("ICF.pFTO", mTelecomLock) { // performFiltering time-out
     75             @Override
     76             public void loggedRun() {
     77                 if (mIsPending) {
     78                     Log.i(IncomingCallFilter.this, "Call filtering has timed out.");
     79                     Log.event(mCall, Log.Events.FILTERING_TIMED_OUT);
     80                     mListener.onCallFilteringComplete(mCall, mResult);
     81                     mIsPending = false;
     82                 }
     83             }
     84         }.prepare(), mTimeoutsAdapter.getCallScreeningTimeoutMillis(mContext.getContentResolver()));
     85     }
     86 
     87     public void onCallFilteringComplete(Call call, CallFilteringResult result) {
     88         synchronized (mTelecomLock) { // synchronizing to prevent race on mResult
     89             mNumPendingFilters--;
     90             mResult = result.combine(mResult);
     91             if (mNumPendingFilters == 0) {
     92                 // synchronized on mTelecomLock to enter into Telecom.
     93                 mHandler.post(new Runnable("ICF.oCFC", mTelecomLock) {
     94                     @Override
     95                     public void loggedRun() {
     96                         if (mIsPending) {
     97                             Log.event(mCall, Log.Events.FILTERING_COMPLETED, mResult);
     98                             mListener.onCallFilteringComplete(mCall, mResult);
     99                             mIsPending = false;
    100                         }
    101                     }
    102                 }.prepare());
    103             }
    104         }
    105     }
    106 
    107     /**
    108      * Returns the handler, for testing purposes.
    109      */
    110     @VisibleForTesting
    111     public Handler getHandler() {
    112         return mHandler;
    113     }
    114 }
    115