1 /*
2  * Copyright (C) 2022 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.selfmanagedcstestapp;
18 
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.IBinder;
23 import android.telecom.PhoneAccount;
24 import android.telecom.PhoneAccountHandle;
25 import android.telecom.TelecomManager;
26 import android.telecom.cts.CtsSelfManagedConnectionService;
27 import android.util.Log;
28 
29 /**
30  * Control class for the self-managed connectionService app; allows CTS tests to perform operations
31  * using the self-managed connectionService test app.
32  */
33 public class CtsSelfManagedConnectionServiceControl extends Service {
34     private static final String TAG = CtsSelfManagedConnectionServiceControl.class.getSimpleName();
35     private static final String CONTROL_INTERFACE_ACTION =
36             "android.telecom.cts.selfmanagedcstestapp.ACTION_SELF_MANAGED_CS_CONTROL";
37 
38     private Context mContext;
39     private ConnectionServiceCallController mConnServiceCallController;
40     private TelecomManager mTelecomManager;
41 
42     private final IBinder mCtsControl = new ICtsSelfManagedConnectionServiceControl.Stub() {
43         @Override
44         public void init() {
45             mConnServiceCallController = ConnectionServiceCallController.getInstance();
46             mTelecomManager = mContext.getSystemService(TelecomManager.class);
47         }
48         @Override
49         public void deInit() {
50             CtsSelfManagedConnectionService s = CtsSelfManagedConnectionService
51                     .getConnectionService();
52             if (s != null) s.tearDown();
53         }
54         @Override
55         public boolean waitForBinding() {
56             return CtsSelfManagedConnectionService.waitForBinding();
57         }
58 
59         @Override
60         public boolean waitForUpdate(int lock) {
61             return CtsSelfManagedConnectionService.getConnectionService().waitForUpdate(lock);
62         }
63 
64         @Override
65         public void registerPhoneAccount(PhoneAccount phoneAccount) {
66             if (mTelecomManager != null) {
67                 mTelecomManager.registerPhoneAccount(phoneAccount);
68             }
69         }
70 
71         @Override
72         public void unregisterPhoneAccount(PhoneAccountHandle phoneAccountHandle) {
73             if (mTelecomManager != null) {
74                 mTelecomManager.unregisterPhoneAccount(phoneAccountHandle);
75             }
76         }
77 
78         @Override
79         public boolean isConnectionAvailable() {
80             return mConnServiceCallController.isConnectionAvailable();
81         }
82 
83         @Override
84         public boolean waitOnHold() {
85             return mConnServiceCallController.waitOnHold();
86         }
87 
88         @Override
89         public boolean waitOnUnHold() {
90             return mConnServiceCallController.waitOnUnHold();
91         }
92 
93         @Override
94         public boolean waitOnDisconnect() {
95             return mConnServiceCallController.waitOnDisconnect();
96         }
97 
98         @Override
99         public boolean initiateIncomingCall(PhoneAccountHandle handle, String uri) {
100             return mConnServiceCallController.initiateIncomingCall(
101                     mTelecomManager, handle, uri);
102         }
103 
104         @Override
105         public boolean placeOutgoingCall(PhoneAccountHandle handle, String uri) {
106             return mConnServiceCallController.placeOutgoingCall(mTelecomManager, handle, uri);
107         }
108 
109         @Override
110         public boolean placeIncomingCall(PhoneAccountHandle handle, String uri, int videoState) {
111             return mConnServiceCallController.placeIncomingCall(mTelecomManager,
112                     handle, uri, videoState);
113         }
114 
115         @Override
116         public boolean isIncomingCall() {
117             return mConnServiceCallController.isIncomingCall();
118         }
119 
120         @Override
121         public boolean waitOnAnswer() {
122             return mConnServiceCallController.waitOnAnswer();
123         }
124 
125         @Override
126         public int getOnShowIncomingUiInvokeCounter() {
127             return mConnServiceCallController.getOnShowIncomingUiInvokeCounter();
128         }
129 
130         @Override
131         public boolean getAudioModeIsVoip() {
132             return mConnServiceCallController.getAudioModeIsVoip();
133         }
134 
135         @Override
136         public int getConnectionState() {
137             return mConnServiceCallController.getState();
138         }
139 
140         @Override
141         public void setConnectionCapabilityNoHold() {
142             mConnServiceCallController.setConnectionCapabilityNoHold();
143         }
144 
145         @Override
146         public void setConnectionActive() {
147             mConnServiceCallController.setConnectionActive();
148         }
149 
150         @Override
151         public void disconnectConnection() {
152             mConnServiceCallController.disconnectConnection();
153         }
154     };
155 
156     @Override
onBind(Intent intent)157     public IBinder onBind(Intent intent) {
158         if (CONTROL_INTERFACE_ACTION.equals(intent.getAction())) {
159             mContext = this;
160             Log.d(TAG, "onBind: return control interface.");
161             return mCtsControl;
162         }
163         Log.d(TAG, "onBind: invalid intent.");
164         return null;
165     }
166 }
167