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 android.telecom.cts; 18 19 import android.os.Bundle; 20 import android.telecom.CallAudioState; 21 import android.telecom.CallEndpoint; 22 import android.telecom.Connection; 23 import android.telecom.DisconnectCause; 24 import android.telecom.cts.TestUtils.InvokeCounter; 25 import android.util.Log; 26 27 import java.util.List; 28 import java.util.concurrent.CountDownLatch; 29 30 /** 31 * CTS Test self-managed {@link Connection} implementation. 32 */ 33 public class SelfManagedConnection extends Connection { 34 private static final String TAG = SelfManagedConnection.class.getSimpleName(); 35 InvokeCounter mCallAudioRouteInvokeCounter = new InvokeCounter("onCallAudioStateChanged"); 36 InvokeCounter mOnShowIncomingUiInvokeCounter = new InvokeCounter( 37 "onShowIncomingUiInvokeCounter"); 38 InvokeCounter mCallEventCounter = new InvokeCounter("onCallEvent"); 39 InvokeCounter mHandoverCompleteCounter = new InvokeCounter("handoverCompleteCounter"); 40 CountDownLatch mOnAnswerLatch = new CountDownLatch(1); 41 InvokeCounter mCallEndpointInvokeCounter = new InvokeCounter("onCallEndpointChanged"); 42 InvokeCounter mAvailableEndpointsInvokeCounter = 43 new InvokeCounter("onAvailableCallEndpointsChanged"); 44 CountDownLatch mOnHoldLatch = new CountDownLatch(1); 45 CountDownLatch mOnUnHoldLatch = new CountDownLatch(1); 46 CountDownLatch mOnDisconnectLatch = new CountDownLatch(1); 47 CountDownLatch mInCallServiceTrackingLatch = new CountDownLatch(1); 48 boolean mIsTracked = false; 49 boolean mIsAlternativeUiShowing = false; 50 51 public static abstract class Listener { onDestroyed(SelfManagedConnection connection)52 void onDestroyed(SelfManagedConnection connection) { }; 53 } 54 55 private final boolean mIsIncomingCall; 56 private final Listener mListener; 57 SelfManagedConnection(boolean isIncomingCall, Listener listener)58 public SelfManagedConnection(boolean isIncomingCall, Listener listener) { 59 mIsIncomingCall = isIncomingCall; 60 mListener = listener; 61 } 62 isIncomingCall()63 public boolean isIncomingCall() { 64 return mIsIncomingCall; 65 } 66 disconnectAndDestroy()67 public void disconnectAndDestroy() { 68 setDisconnected(new DisconnectCause(DisconnectCause.LOCAL)); 69 destroy(); 70 mListener.onDestroyed(this); 71 } 72 73 @Override onAnswer(int videoState)74 public void onAnswer(int videoState) { 75 this.setActive(); 76 mOnAnswerLatch.countDown(); 77 } 78 79 @Override onAnswer()80 public void onAnswer() { 81 this.setActive(); 82 mOnAnswerLatch.countDown(); 83 } 84 85 @Override onCallAudioStateChanged(CallAudioState state)86 public void onCallAudioStateChanged(CallAudioState state) { 87 mCallAudioRouteInvokeCounter.invoke(state); 88 } 89 90 @Override onUsingAlternativeUi(boolean isUsingAlternativeUi)91 public void onUsingAlternativeUi(boolean isUsingAlternativeUi) { 92 mIsAlternativeUiShowing = isUsingAlternativeUi; 93 mInCallServiceTrackingLatch.countDown(); 94 } 95 96 @Override onTrackedByNonUiService(boolean isTracked)97 public void onTrackedByNonUiService(boolean isTracked) { 98 mIsTracked = isTracked; 99 mInCallServiceTrackingLatch.countDown(); 100 } 101 102 @Override onDisconnect()103 public void onDisconnect() { 104 super.onDisconnect(); 105 disconnectAndDestroy(); 106 mOnDisconnectLatch.countDown(); 107 } 108 109 @Override onShowIncomingCallUi()110 public void onShowIncomingCallUi() { 111 mOnShowIncomingUiInvokeCounter.invoke(); 112 } 113 114 @Override onHold()115 public void onHold() { 116 this.setOnHold(); 117 mOnHoldLatch.countDown(); 118 } 119 120 @Override onUnhold()121 public void onUnhold() { 122 this.setActive(); 123 mOnUnHoldLatch.countDown(); 124 } 125 126 @Override onCallEvent(String event, Bundle extras)127 public void onCallEvent(String event, Bundle extras) { 128 mCallEventCounter.invoke(event, extras); 129 } 130 131 @Override onHandoverComplete()132 public void onHandoverComplete() { 133 mHandoverCompleteCounter.invoke(); 134 } 135 136 @Override onCallEndpointChanged(CallEndpoint endpoint)137 public void onCallEndpointChanged(CallEndpoint endpoint) { 138 Log.i(TAG, String.format("onCallEndpointChanged: endpoint=[%s]", endpoint)); 139 mCallEndpointInvokeCounter.invoke(endpoint); 140 } 141 142 @Override onAvailableCallEndpointsChanged(List<CallEndpoint> availableEndpoints)143 public void onAvailableCallEndpointsChanged(List<CallEndpoint> availableEndpoints) { 144 mAvailableEndpointsInvokeCounter.invoke(availableEndpoints); 145 } 146 getCallAudioStateChangedInvokeCounter()147 public InvokeCounter getCallAudioStateChangedInvokeCounter() { 148 return mCallAudioRouteInvokeCounter; 149 } 150 getOnShowIncomingUiInvokeCounter()151 public InvokeCounter getOnShowIncomingUiInvokeCounter() { 152 return mOnShowIncomingUiInvokeCounter; 153 } 154 getCallEventCounter()155 public InvokeCounter getCallEventCounter() { 156 return mCallEventCounter; 157 } 158 getHandoverCompleteCounter()159 public InvokeCounter getHandoverCompleteCounter() { 160 return mHandoverCompleteCounter; 161 } 162 waitOnAnswer()163 public boolean waitOnAnswer() { 164 mOnAnswerLatch = TestUtils.waitForLock(mOnAnswerLatch); 165 return mOnAnswerLatch != null; 166 } 167 waitOnHold()168 public boolean waitOnHold() { 169 mOnHoldLatch = TestUtils.waitForLock(mOnHoldLatch); 170 return mOnHoldLatch != null; 171 } 172 waitOnUnHold()173 public boolean waitOnUnHold() { 174 mOnUnHoldLatch = TestUtils.waitForLock(mOnUnHoldLatch); 175 return mOnUnHoldLatch != null; 176 } 177 waitOnDisconnect()178 public boolean waitOnDisconnect() { 179 mOnDisconnectLatch = TestUtils.waitForLock(mOnDisconnectLatch); 180 return mOnDisconnectLatch != null; 181 } 182 waitOnInCallServiceTrackingChanged()183 public boolean waitOnInCallServiceTrackingChanged() { 184 boolean result = TestUtils.waitForLatchCountDown(mInCallServiceTrackingLatch); 185 mInCallServiceTrackingLatch = new CountDownLatch(1); 186 return result; 187 } 188 isTracked()189 public boolean isTracked() { 190 return mIsTracked; 191 } 192 isAlternativeUiShowing()193 public boolean isAlternativeUiShowing() { 194 return mIsAlternativeUiShowing; 195 } 196 getCallEndpointChangedInvokeCounter()197 public InvokeCounter getCallEndpointChangedInvokeCounter() { 198 return mCallEndpointInvokeCounter; 199 } 200 getAvailableEndpointsChangedInvokeCounter()201 public InvokeCounter getAvailableEndpointsChangedInvokeCounter() { 202 return mAvailableEndpointsInvokeCounter; 203 } 204 } 205