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.cts.verifier.telecom; 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.PhoneAccountHandle; 25 import android.telecom.TelecomManager; 26 27 import com.android.compatibility.common.util.ApiTest; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.concurrent.CountDownLatch; 32 import java.util.concurrent.TimeUnit; 33 34 /** 35 * CTS Verifier ConnectionService implementation. 36 */ 37 @ApiTest(apis={"android.telecom.ConnectionService"}) 38 public class CtsConnectionService extends ConnectionService { 39 static final int TIMEOUT_MILLIS = 10000; 40 41 private CtsConnection.Listener mConnectionListener = 42 new CtsConnection.Listener() { 43 @Override 44 void onDestroyed(CtsConnection connection) { 45 synchronized (mConnectionsLock) { 46 mConnections.remove(connection); 47 } 48 } 49 }; 50 51 private static CtsConnectionService sConnectionService; 52 private static CountDownLatch sBindingLatch = new CountDownLatch(1); 53 54 private List<CtsConnection> mConnections = new ArrayList<>(); 55 private Object mConnectionsLock = new Object(); 56 private CountDownLatch mConnectionLatch = new CountDownLatch(1); 57 getConnectionService()58 public static CtsConnectionService getConnectionService() { 59 return sConnectionService; 60 } 61 waitForAndGetConnectionService()62 public static CtsConnectionService waitForAndGetConnectionService() { 63 if (sConnectionService == null) { 64 try { 65 sBindingLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 66 } catch (InterruptedException e) { 67 } 68 } 69 return sConnectionService; 70 } 71 CtsConnectionService()72 public CtsConnectionService() throws Exception { 73 super(); 74 sConnectionService = this; 75 if (sBindingLatch != null) { 76 sBindingLatch.countDown(); 77 } 78 sBindingLatch = new CountDownLatch(1); 79 } 80 getConnections()81 public List<CtsConnection> getConnections() { 82 synchronized (mConnectionsLock) { 83 return new ArrayList<CtsConnection>(mConnections); 84 } 85 } 86 waitForAndGetConnection()87 public CtsConnection waitForAndGetConnection() { 88 try { 89 mConnectionLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 90 } catch (InterruptedException e) { 91 } 92 mConnectionLatch = new CountDownLatch(1); 93 synchronized (mConnectionsLock) { 94 if (mConnections.size() > 0) { 95 return mConnections.get(0); 96 } else { 97 return null; 98 } 99 } 100 } 101 102 @Override onUnbind(Intent intent)103 public boolean onUnbind(Intent intent) { 104 sConnectionService = null; 105 return super.onUnbind(intent); 106 } 107 108 @Override onCreateOutgoingConnection(PhoneAccountHandle connectionManagerAccount, final ConnectionRequest request)109 public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerAccount, 110 final ConnectionRequest request) { 111 112 return createManagedConnection(request, false); 113 } 114 115 @Override onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)116 public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, 117 ConnectionRequest request) { 118 119 return createManagedConnection(request, true); 120 } 121 122 @Override onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerHandle, ConnectionRequest request)123 public void onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerHandle, 124 ConnectionRequest request) { 125 } 126 127 @Override onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerHandle, ConnectionRequest request)128 public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerHandle, 129 ConnectionRequest request) { 130 } 131 createManagedConnection(ConnectionRequest request, boolean isIncoming)132 private Connection createManagedConnection(ConnectionRequest request, boolean isIncoming) { 133 boolean isSelfManaged = request.getAccountHandle().equals( 134 PhoneAccountUtils.TEST_SELF_MANAGED_PHONE_ACCOUNT_HANDLE); 135 136 boolean useAudioClip = 137 request.getExtras().getBoolean(CtsConnection.EXTRA_PLAY_CS_AUDIO, false); 138 CtsConnection connection = new CtsConnection(getApplicationContext(), isIncoming, 139 mConnectionListener, useAudioClip); 140 if (isSelfManaged) { 141 connection.setConnectionProperties(Connection.PROPERTY_SELF_MANAGED); 142 } 143 connection.setAudioModeIsVoip(true); 144 connection.setConnectionCapabilities(Connection.CAPABILITY_SUPPORT_HOLD | 145 Connection.CAPABILITY_HOLD); 146 connection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED); 147 connection.setExtras(request.getExtras()); 148 149 Bundle moreExtras = new Bundle(); 150 moreExtras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, 151 request.getAccountHandle()); 152 connection.putExtras(moreExtras); 153 connection.setVideoState(request.getVideoState()); 154 155 synchronized (mConnectionsLock) { 156 mConnections.add(connection); 157 } 158 if (mConnectionLatch != null) { 159 mConnectionLatch.countDown(); 160 } 161 return connection; 162 } 163 } 164