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 android.telecom.cts.screeningtestapp;
18 
19 import android.app.Service;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.graphics.drawable.Icon;
23 import android.net.Uri;
24 import android.os.IBinder;
25 import android.telecom.CallScreeningService;
26 import android.text.TextUtils;
27 import android.util.Log;
28 
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 
32 public class CallScreeningServiceControl extends Service {
33     private static final int ASYNC_TIMEOUT = 10000;
34     private static final String TAG = CallScreeningServiceControl.class.getSimpleName();
35     public static final String CONTROL_INTERFACE_ACTION =
36             "android.telecom.cts.screeningtestapp.ACTION_CONTROL_CALL_SCREENING_SERVICE";
37     public static final ComponentName CONTROL_INTERFACE_COMPONENT =
38             ComponentName.unflattenFromString(
39                     "android.telecom.cts.screeningtestapp/.CallScreeningServiceControl");
40 
41     private static CallScreeningServiceControl sCallScreeningServiceControl = null;
42     private CountDownLatch mBindingLatch = new CountDownLatch(1);
43 
44     private final IBinder mControlInterface =
45             new android.telecom.cts.screeningtestapp.ICallScreeningControl.Stub() {
46                 @Override
47                 public void reset() {
48                     mCallResponse = new CallScreeningService.CallResponse.Builder()
49                             .setDisallowCall(false)
50                             .setRejectCall(false)
51                             .setSkipCallLog(false)
52                             .setSkipNotification(false)
53                             .build();
54                     mBindingLatch = new CountDownLatch(1);
55                     CtsPostCallActivity.resetPostCallActivity();
56                 }
57 
58                 @Override
59                 public void setCallResponse(boolean shouldDisallowCall,
60                         boolean shouldRejectCall,
61                         boolean shouldSilenceCall,
62                         boolean shouldSkipCallLog,
63                         boolean shouldSkipNotification) {
64                     Log.i(TAG, "setCallResponse");
65                     mCallResponse = new CallScreeningService.CallResponse.Builder()
66                             .setSkipNotification(shouldSkipNotification)
67                             .setSkipCallLog(shouldSkipCallLog)
68                             .setDisallowCall(shouldDisallowCall)
69                             .setRejectCall(shouldRejectCall)
70                             .setSilenceCall(shouldSilenceCall)
71                             .build();
72                 }
73 
74                 @Override
75                 public boolean waitForBind() {
76                     try {
77                         return mBindingLatch.await(ASYNC_TIMEOUT, TimeUnit.MILLISECONDS);
78                     } catch (InterruptedException e) {
79                         return false;
80                     }
81                 }
82 
83                 @Override
84                 public boolean waitForActivity() {
85                     return CtsPostCallActivity.waitForActivity();
86                 }
87 
88                 @Override
89                 public String getCachedHandle() {
90                     return CtsPostCallActivity.getCachedHandle().getSchemeSpecificPart();
91                 }
92 
93                 @Override
94                 public int getCachedDisconnectCause() {
95                     return CtsPostCallActivity.getCachedDisconnectCause();
96                 }
97             };
98 
99     private CallScreeningService.CallResponse mCallResponse =
100             new CallScreeningService.CallResponse.Builder()
101                     .setDisallowCall(false)
102                     .setRejectCall(false)
103                     .setSilenceCall(false)
104                     .setSkipCallLog(false)
105                     .setSkipNotification(false)
106                     .build();
107 
getInstance()108     public static CallScreeningServiceControl getInstance() {
109         return sCallScreeningServiceControl;
110     }
111 
112     @Override
onBind(Intent intent)113     public IBinder onBind(Intent intent) {
114         if (CONTROL_INTERFACE_ACTION.equals(intent.getAction())) {
115             Log.i(TAG, "onBind: returning control interface");
116             sCallScreeningServiceControl = this;
117             return mControlInterface;
118         }
119         Log.i(TAG, "onBind: uh oh");
120         return null;
121     }
122 
123     @Override
onUnbind(Intent intent)124     public boolean onUnbind(Intent intent) {
125         sCallScreeningServiceControl = null;
126         return false;
127     }
128 
onScreeningServiceBound()129     public void onScreeningServiceBound() {
130         mBindingLatch.countDown();
131     }
132 
getCallResponse()133     public CallScreeningService.CallResponse getCallResponse() {
134         return mCallResponse;
135     }
136 }
137