1 /*
2  * Copyright (C) 2023 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 package android.telecom.cts.apps.managedapp;
17 
18 import android.content.Intent;
19 import android.telecom.Connection;
20 import android.telecom.ConnectionRequest;
21 import android.telecom.ConnectionService;
22 import android.telecom.PhoneAccountHandle;
23 import android.telecom.TelecomManager;
24 import android.telecom.cts.apps.ManagedConnection;
25 import android.util.Log;
26 
27 public class ManagedConnectionService extends ConnectionService {
28     private static final String LOG_TAG = "ManagedConnectionService";
29     public static ManagedConnectionService sConnectionService;
30     public static ManagedConnection sLastConnection = null;
31 
32     @Override
onBindClient(Intent intent)33     public void onBindClient(Intent intent) {
34         Log.i(LOG_TAG, String.format("onBindClient: intent=[%s]", intent));
35         sConnectionService = this;
36         sLastConnection = null;
37     }
38 
39     @Override
onUnbind(Intent intent)40     public boolean onUnbind(Intent intent) {
41         Log.i(LOG_TAG, String.format("onUnbind: intent=[%s]", intent));
42         sConnectionService = null;
43         sLastConnection = null;
44         return super.onUnbind(intent);
45     }
46 
47     @Override
onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)48     public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
49             ConnectionRequest request) {
50         Log.i(LOG_TAG, String.format("onCreateOutgoingConnection: account=[%s], request=[%s]",
51                 connectionManagerPhoneAccount, request));
52         return createConnection(request, true);
53     }
54 
55     @Override
onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)56     public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
57             ConnectionRequest request) {
58         Log.i(LOG_TAG, String.format("onCreateOutgoingConnectionFailed: account=[%s], request=[%s]",
59                 connectionManagerPhoneAccount, request));
60         super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request);
61     }
62 
63     @Override
onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)64     public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
65             ConnectionRequest request) {
66         Log.i(LOG_TAG, String.format("onCreateIncomingConnection: account=[%s], request=[%s]",
67                 connectionManagerPhoneAccount, request));
68         return createConnection(request, false);
69     }
70 
71     @Override
onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)72     public void onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
73             ConnectionRequest request) {
74         Log.i(LOG_TAG, String.format("onCreateIncomingConnectionFailed: account=[%s], request=[%s]",
75                 connectionManagerPhoneAccount, request));
76         super.onCreateIncomingConnectionFailed(connectionManagerPhoneAccount, request);
77     }
78 
createConnection(ConnectionRequest request, boolean isOutgoing)79     private Connection createConnection(ConnectionRequest request, boolean isOutgoing) {
80         ManagedConnection connection = new ManagedConnection(getApplicationContext(), isOutgoing);
81         sLastConnection = connection;
82 
83         if (isOutgoing) {
84             connection.setDialing();
85         } else {
86             connection.setRinging();
87         }
88 
89         connection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED);
90         connection.setConnectionCapabilities(
91                 Connection.CAPABILITY_HOLD | Connection.CAPABILITY_SUPPORT_HOLD
92         );
93         connection.setAudioModeIsVoip(false);
94         return connection;
95     }
96 }
97