1 /*
2  * Copyright (C) 2017 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.content.Intent;
20 import android.os.Bundle;
21 import android.telecom.Connection;
22 import android.telecom.ConnectionRequest;
23 import android.telecom.ConnectionService;
24 import android.telecom.Log;
25 import android.telecom.PhoneAccountHandle;
26 import android.telecom.TelecomManager;
27 
28 import java.util.Random;
29 
30 /**
31  * Sample implementation of the self-managed {@link ConnectionService} API.
32  * <p>
33  * See {@link android.telecom} for more information on self-managed {@link ConnectionService}s.
34  */
35 public class SelfManagedConnectionService extends ConnectionService {
36     public static final String EXTRA_HOLDABLE = "com.android.server.telecom.testapps.HOLDABLE";
37     private static final String[] TEST_NAMES = {"Tom Smith", "Jane Appleseed", "Joseph Engleton",
38             "Claudia McPherson", "Chris P. Bacon", "Seymour Butz", "Hugh Mungus", "Anita Bath"};
39     private final SelfManagedCallList mCallList = SelfManagedCallList.getInstance();
40 
41     @Override
onCreateOutgoingConnection( PhoneAccountHandle connectionManagerAccount, final ConnectionRequest request)42     public Connection onCreateOutgoingConnection(
43             PhoneAccountHandle connectionManagerAccount,
44             final ConnectionRequest request) {
45 
46         return createSelfManagedConnection(request, false, false /* isHandover */);
47     }
48 
49     @Override
onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)50     public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
51             ConnectionRequest request) {
52         return createSelfManagedConnection(request, true, false /* isHandover */);
53     }
54 
55     @Override
onCreateOutgoingHandoverConnection(PhoneAccountHandle fromPhoneAccountHandle, ConnectionRequest request)56     public Connection onCreateOutgoingHandoverConnection(PhoneAccountHandle fromPhoneAccountHandle,
57             ConnectionRequest request) {
58         return createSelfManagedConnection(request, false, true /* isHandover */);
59     }
60 
61     @Override
onCreateIncomingHandoverConnection(PhoneAccountHandle fromPhoneAccountHandle, ConnectionRequest request)62     public Connection onCreateIncomingHandoverConnection(PhoneAccountHandle fromPhoneAccountHandle,
63             ConnectionRequest request) {
64         return createSelfManagedConnection(request, true, true /* isHandover */);
65     }
66 
67     @Override
onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)68     public void onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
69                                                  ConnectionRequest request) {
70         mCallList.notifyCreateIncomingConnectionFailed(request);
71     }
72 
73     @Override
onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)74     public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
75                                                  ConnectionRequest request) {
76         mCallList.notifyCreateOutgoingConnectionFailed(request);
77     }
78 
79     @Override
onConnectionServiceFocusLost()80     public void onConnectionServiceFocusLost() {
81         mCallList.notifyConnectionServiceFocusLost();
82         connectionServiceFocusReleased();
83     }
84 
85     @Override
onConnectionServiceFocusGained()86     public void onConnectionServiceFocusGained() {
87         mCallList.notifyConnectionServiceFocusGained();
88     }
89 
createSelfManagedConnection(ConnectionRequest request, boolean isIncoming, boolean isHandover)90     private Connection createSelfManagedConnection(ConnectionRequest request, boolean isIncoming,
91             boolean isHandover) {
92         SelfManagedConnection connection = new SelfManagedConnection(mCallList,
93                 getApplicationContext(), isIncoming);
94         connection.setListener(mCallList.getConnectionListener());
95         connection.setConnectionProperties(Connection.PROPERTY_SELF_MANAGED);
96         connection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED);
97         // Purposely do not set the audio mode to voip since we expect this to be the default:
98         // connection.setAudioModeIsVoip(true);
99         connection.setVideoState(request.getVideoState());
100         Random random = new Random();
101         connection.setCallerDisplayName(TEST_NAMES[random.nextInt(TEST_NAMES.length)],
102                 TelecomManager.PRESENTATION_ALLOWED);
103         connection.setExtras(request.getExtras());
104         if (isIncoming) {
105             connection.setIsIncomingCallUiShowing(request.shouldShowIncomingCallUi());
106             connection.setRinging();
107         } else {
108             connection.setDialing();
109         }
110         Bundle requestExtras = request.getExtras();
111         if (requestExtras != null) {
112             boolean isHoldable = requestExtras.getBoolean(EXTRA_HOLDABLE, false);
113             Log.i(this, "createConnection: isHandover=%b, handoverFrom=%s, holdable=%b",
114                     isHandover,
115                     requestExtras.getString(TelecomManager.EXTRA_HANDOVER_FROM_PHONE_ACCOUNT),
116                     isHoldable);
117             connection.setIsHandover(isHandover);
118             if (isHoldable) {
119                 connection.setConnectionCapabilities(connection.getConnectionCapabilities() |
120                         Connection.CAPABILITY_HOLD | Connection.CAPABILITY_SUPPORT_HOLD);
121             }
122             if (!isIncoming && connection.isHandover()) {
123                 Intent intent = new Intent(Intent.ACTION_MAIN, null);
124                 intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
125                 intent.setClass(this, HandoverActivity.class);
126                 intent.putExtra(HandoverActivity.EXTRA_CALL_ID, connection.getCallId());
127                 startActivity(intent);
128             } else {
129                 Log.i(this, "Handover incoming call created.");
130             }
131         }
132 
133         // Track the phone account handle which created this connection so we can distinguish them
134         // in the sample call list later.
135         Bundle moreExtras = new Bundle();
136         moreExtras.putParcelable(SelfManagedConnection.EXTRA_PHONE_ACCOUNT_HANDLE,
137                 request.getAccountHandle());
138         connection.putExtras(moreExtras);
139         connection.setVideoState(request.getVideoState());
140         Log.i(this, "createSelfManagedConnection %s", connection);
141         mCallList.addConnection(connection);
142         return connection;
143     }
144 }
145