1 /*
2  * Copyright (C) 2018 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.testapps;
18 
19 import android.os.SystemProperties;
20 import android.telecom.Call;
21 import android.telecom.CallScreeningService;
22 import android.telecom.Log;
23 
24 /**
25  * To use this while testing, use:
26  * adb shell setprop com.android.server.telecom.testapps.callscreeningresult n,
27  * where n is one of the codes defined below.
28  */
29 public class TestCallScreeningService extends CallScreeningService {
30     private Call.Details mDetails;
31     private static TestCallScreeningService sTestCallScreeningService;
32 
getInstance()33     public static TestCallScreeningService getInstance() {
34         return sTestCallScreeningService;
35     }
36 
37     private static final int ALLOW_CALL = 0;
38     private static final int BLOCK_CALL = 1;
39     private static final int SCREEN_CALL_FURTHER = 2;
40 
41     private static final String SCREENING_RESULT_KEY =
42             TestCallScreeningService.class.getPackage().getName() + ".callscreeningresult";
43 
44     /**
45      * Handles request from the system to screen an incoming call.
46      * @param callDetails Information about a new incoming call, see {@link Call.Details}.
47      */
48     @Override
onScreenCall(Call.Details callDetails)49     public void onScreenCall(Call.Details callDetails) {
50         Log.i(this, "onScreenCall: received call %s", callDetails);
51         sTestCallScreeningService = this;
52 
53         mDetails = callDetails;
54 
55         if (callDetails.getCallDirection() == Call.Details.DIRECTION_INCOMING) {
56             Log.i(this, "%s = %d", SCREENING_RESULT_KEY,
57                     SystemProperties.getInt(SCREENING_RESULT_KEY, 0));
58             switch (SystemProperties.getInt(SCREENING_RESULT_KEY, 0)) {
59                 case ALLOW_CALL:
60                     allowCall();
61                     break;
62                 case BLOCK_CALL:
63                     blockCall();
64                     break;
65                 case SCREEN_CALL_FURTHER:
66                     screenCallFurther();
67                     break;
68             }
69         }
70     }
71 
blockCall()72     public void blockCall() {
73         CallScreeningService.CallResponse
74                 response = new CallScreeningService.CallResponse.Builder()
75                 .setDisallowCall(true)
76                 .setRejectCall(true)
77                 .setSkipCallLog(false)
78                 .setSkipNotification(true)
79                 .build();
80         respondToCall(mDetails, response);
81     }
82 
allowCall()83     public void allowCall() {
84         CallScreeningService.CallResponse
85                 response = new CallScreeningService.CallResponse.Builder()
86                 .setDisallowCall(false)
87                 .setRejectCall(false)
88                 .setSkipCallLog(false)
89                 .setSkipNotification(false)
90                 .build();
91         respondToCall(mDetails, response);
92     }
93 
screenCallFurther()94     void screenCallFurther() {
95         CallScreeningService.CallResponse
96                 response = new CallScreeningService.CallResponse.Builder()
97                 .setDisallowCall(false)
98                 .setRejectCall(false)
99                 .setSkipCallLog(false)
100                 .setSkipNotification(false)
101                 .setShouldScreenCallViaAudioProcessing(true)
102                 .build();
103         respondToCall(mDetails, response);
104     }
105 }
106