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 
90     @SuppressWarnings("CatchAndPrintStackTrace")
createSelfManagedConnection(ConnectionRequest request, boolean isIncoming, boolean isHandover)91     private Connection createSelfManagedConnection(ConnectionRequest request, boolean isIncoming,
92             boolean isHandover) {
93         SelfManagedConnection connection = new SelfManagedConnection(mCallList,
94                 getApplicationContext(), isIncoming);
95         connection.setListener(mCallList.getConnectionListener());
96         connection.setConnectionProperties(Connection.PROPERTY_SELF_MANAGED);
97         connection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED);
98         // Purposely do not set the audio mode to voip since we expect this to be the default:
99         // connection.setAudioModeIsVoip(true);
100         connection.setVideoState(request.getVideoState());
101         Random random = new Random();
102         connection.setCallerDisplayName(TEST_NAMES[random.nextInt(TEST_NAMES.length)],
103                 TelecomManager.PRESENTATION_ALLOWED);
104         connection.setExtras(request.getExtras());
105         if (!request.getAddress().getSchemeSpecificPart().equals("123")) {
106             if (isIncoming) {
107                 connection.setIsIncomingCallUiShowing(request.shouldShowIncomingCallUi());
108                 connection.setRinging();
109             } else {
110                 connection.setDialing();
111             }
112         }
113         Bundle requestExtras = request.getExtras();
114         if (requestExtras != null) {
115             boolean isHoldable = requestExtras.getBoolean(EXTRA_HOLDABLE, false);
116             Log.i(this, "createConnection: isHandover=%b, handoverFrom=%s, holdable=%b",
117                     isHandover,
118                     requestExtras.getString(TelecomManager.EXTRA_HANDOVER_FROM_PHONE_ACCOUNT),
119                     isHoldable);
120             connection.setIsHandover(isHandover);
121             if (isHoldable) {
122                 connection.setConnectionCapabilities(connection.getConnectionCapabilities() |
123                         Connection.CAPABILITY_HOLD | Connection.CAPABILITY_SUPPORT_HOLD);
124             }
125             if (!isIncoming && connection.isHandover()) {
126                 Intent intent = new Intent(Intent.ACTION_MAIN, null);
127                 intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
128                 intent.setClass(this, HandoverActivity.class);
129                 intent.putExtra(HandoverActivity.EXTRA_CALL_ID, connection.getCallId());
130                 startActivity(intent);
131             } else {
132                 Log.i(this, "Handover incoming call created.");
133             }
134         }
135 
136         // Track the phone account handle which created this connection so we can distinguish them
137         // in the sample call list later.
138         Bundle moreExtras = new Bundle();
139         moreExtras.putParcelable(SelfManagedConnection.EXTRA_PHONE_ACCOUNT_HANDLE,
140                 request.getAccountHandle());
141         connection.putExtras(moreExtras);
142         connection.setVideoState(request.getVideoState());
143         Log.i(this, "createSelfManagedConnection %s", connection);
144         mCallList.addConnection(connection);
145         return connection;
146     }
147 }
148