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