1 /*
2  * Copyright (C) 2019 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.carmodetestapp;
18 
19 import android.app.Service;
20 import android.app.UiModeManager;
21 import android.content.ComponentName;
22 import android.content.Intent;
23 import android.os.IBinder;
24 import android.telecom.PhoneAccount;
25 import android.telecom.PhoneAccountHandle;
26 import android.telecom.TelecomManager;
27 import android.util.Log;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * Control class for the car mode app; allows CTS tests to perform operations using the car mode
34  * test app.
35  */
36 public class CtsCarModeInCallServiceControl extends Service {
37     private static final String TAG = CtsCarModeInCallServiceControl.class.getSimpleName();
38     public static final String CONTROL_INTERFACE_ACTION =
39             "android.telecom.cts.carmodetestapp.ACTION_CAR_MODE_CONTROL";
40     public static final ComponentName CONTROL_INTERFACE_COMPONENT =
41             new ComponentName(CtsCarModeInCallServiceControl.class.getPackage().getName(),
42                     CtsCarModeInCallServiceControl.class.getName());
43 
44     private final IBinder mCtsControl = new ICtsCarModeInCallServiceControl.Stub() {
45         @Override
46         public boolean isBound() {
47             return CtsCarModeInCallService.isBound();
48         }
49 
50         @Override
51         public boolean isUnbound() {
52             return CtsCarModeInCallService.isUnbound();
53         }
54 
55         @Override
56         public void reset() {
57             CtsCarModeInCallService.reset();
58             UiModeManager uiModeManager = getSystemService(UiModeManager.class);
59             uiModeManager.disableCarMode(0);
60         }
61 
62         @Override
63         public void disconnectCalls() {
64             if (CtsCarModeInCallService.getInstance() != null) {
65                 CtsCarModeInCallService.getInstance().disconnectCalls();
66             }
67         }
68 
69         @Override
70         public int getCallCount() {
71             if (CtsCarModeInCallService.getInstance() != null) {
72                 return CtsCarModeInCallService.getInstance().getCallCount();
73             }
74             // if there's no instance, there's no calls
75             return 0;
76         }
77 
78         @Override
79         public void enableCarMode(int priority) {
80             UiModeManager uiModeManager = getSystemService(UiModeManager.class);
81             uiModeManager.enableCarMode(priority, 0);
82         }
83 
84         @Override
85         public void disableCarMode() {
86             UiModeManager uiModeManager = getSystemService(UiModeManager.class);
87             uiModeManager.disableCarMode(0);
88         }
89 
90         @Override
91         public boolean requestAutomotiveProjection() {
92             UiModeManager uiModeManager = getSystemService(UiModeManager.class);
93             return uiModeManager.requestProjection(UiModeManager.PROJECTION_TYPE_AUTOMOTIVE);
94         }
95 
96         @Override
97         public void releaseAutomotiveProjection() {
98             UiModeManager uiModeManager = getSystemService(UiModeManager.class);
99             uiModeManager.releaseProjection(UiModeManager.PROJECTION_TYPE_AUTOMOTIVE);
100         }
101 
102         @Override
103         public boolean checkBindStatus(boolean bind) {
104             return CtsCarModeInCallService.checkBindStatus(bind);
105         }
106 
107         @Override
108         public List<PhoneAccountHandle> getSelfManagedPhoneAccounts() {
109             TelecomManager telecomManager = getSystemService(TelecomManager.class);
110             return (telecomManager != null) ? telecomManager.getSelfManagedPhoneAccounts()
111                     : new ArrayList<>();
112         }
113 
114         @Override
115         public List<PhoneAccountHandle> getOwnSelfManagedPhoneAccounts() {
116             TelecomManager telecomManager = getSystemService(TelecomManager.class);
117             return (telecomManager != null) ? telecomManager.getOwnSelfManagedPhoneAccounts()
118                     : new ArrayList<>();
119         }
120 
121         @Override
122         public void registerPhoneAccount(PhoneAccount phoneAccount) {
123             TelecomManager telecomManager = getSystemService(TelecomManager.class);
124             if (telecomManager != null) {
125                 telecomManager.registerPhoneAccount(phoneAccount);
126             }
127         }
128 
129         @Override
130         public void unregisterPhoneAccount(PhoneAccountHandle phoneAccountHandle) {
131             TelecomManager telecomManager = getSystemService(TelecomManager.class);
132             if (telecomManager != null) {
133                 telecomManager.unregisterPhoneAccount(phoneAccountHandle);
134             }
135         }
136 
137         @Override
138         public boolean checkCallAddedStatus() {
139             return CtsCarModeInCallService.getInstance().checkCallAddedStatus();
140         }
141 
142         @Override
143         public int getCallVideoState() {
144             return CtsCarModeInCallService.getInstance().getLastCall().getDetails().getVideoState();
145         }
146 
147         @Override
148         public int getCallState() {
149             return CtsCarModeInCallService.getInstance().getLastCall().getState();
150         }
151 
152         @Override
153         public void hold() {
154             CtsCarModeInCallService.getInstance().getLastCall().hold();
155         }
156 
157         @Override
158         public void unhold() {
159             CtsCarModeInCallService.getInstance().getLastCall().unhold();
160         }
161 
162         @Override
163         public void disconnect() {
164             CtsCarModeInCallService.getInstance().getLastCall().disconnect();
165         }
166 
167         @Override
168         public void answerCall(int videoState) {
169             CtsCarModeInCallService.getInstance().getLastCall().answer(videoState);
170         }
171     };
172 
173     @Override
onBind(Intent intent)174     public IBinder onBind(Intent intent) {
175         if (CONTROL_INTERFACE_ACTION.equals(intent.getAction())) {
176             Log.d(TAG, "onBind: return control interface.");
177             return mCtsControl;
178         }
179         Log.d(TAG, "onBind: invalid intent.");
180         return null;
181     }
182 }
183