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.Context; 20 import android.content.Intent; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.telecom.Connection; 24 import android.telecom.PhoneAccount; 25 import android.telecom.PhoneAccountHandle; 26 import android.telecom.TelecomManager; 27 import android.util.Log; 28 import android.view.View; 29 import android.widget.Button; 30 import android.widget.ImageView; 31 32 import com.android.compatibility.common.util.ApiTest; 33 import com.android.compatibility.common.util.CddTest; 34 import com.android.cts.verifier.PassFailButtons; 35 import com.android.cts.verifier.R; 36 37 import java.util.List; 38 39 /** 40 * Tests that an incoming call made from an enabled ConnectionService will ring the phone and be 41 * able to be answered. 42 */ 43 @ApiTest(apis={"android.telecom.ConnectionService"}) 44 @CddTest(requirement="7.4.1.2/C-1-1") 45 public class IncomingCallTestActivity extends PassFailButtons.Activity { 46 private static final String TAG = "TelecomIncomingCall"; 47 48 private Button mRegisterAndEnablePhoneAccount; 49 private Button mConfirmPhoneAccountEnabled; 50 private Button mAddIncomingCall; 51 private Button mConfirmIncomingCallAnswered; 52 53 private ImageView mStep1Status; 54 private ImageView mStep2Status; 55 private ImageView mStep3Status; 56 57 private Uri TEST_DIAL_NUMBER = Uri.fromParts("tel", "5551212", null); 58 59 @Override onCreate(Bundle savedInstanceState)60 protected void onCreate(Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 62 View view = getLayoutInflater().inflate(R.layout.telecom_incoming_call, null); 63 setContentView(view); 64 setInfoResources(R.string.telecom_incoming_call_test, 65 R.string.telecom_incoming_call_test_info, -1); 66 setPassFailButtonClickListeners(); 67 getPassButton().setEnabled(false); 68 69 mRegisterAndEnablePhoneAccount = view.findViewById( 70 R.id.telecom_incoming_call_register_enable_phone_account_button); 71 if (mRegisterAndEnablePhoneAccount == null) { 72 finish(); 73 return; 74 } 75 mRegisterAndEnablePhoneAccount.setOnClickListener(v -> { 76 PhoneAccountUtils.registerTestPhoneAccount(this); 77 PhoneAccount account = PhoneAccountUtils.getPhoneAccount(this); 78 if (account != null) { 79 // Open the phone accounts screen to have the user set CtsConnectionService as 80 // the default. 81 Intent intent = new Intent(TelecomManager.ACTION_CHANGE_PHONE_ACCOUNTS); 82 startActivity(intent); 83 84 mConfirmPhoneAccountEnabled.setEnabled(true); 85 } else { 86 Log.w(TAG, "Step 1 fail - couldn't register phone account"); 87 mStep1Status.setImageResource(R.drawable.fs_error); 88 } 89 }); 90 91 mConfirmPhoneAccountEnabled = view.findViewById(R.id 92 .telecom_incoming_call_confirm_register_button); 93 if (mConfirmPhoneAccountEnabled == null) { 94 finish(); 95 return; 96 } 97 mConfirmPhoneAccountEnabled.setOnClickListener(v -> { 98 PhoneAccount account = PhoneAccountUtils.getPhoneAccount(this); 99 if (account != null && account.isEnabled()) { 100 getPassButton().setEnabled(true); 101 Log.i(TAG, "Step 1 pass - account is enabled."); 102 mStep1Status.setImageResource(R.drawable.fs_good); 103 mConfirmPhoneAccountEnabled.setEnabled(false); 104 } else { 105 Log.w(TAG, "Step 1 fail - account is not enabled."); 106 mStep1Status.setImageResource(R.drawable.fs_error); 107 } 108 }); 109 110 mConfirmPhoneAccountEnabled.setEnabled(false); 111 112 mAddIncomingCall = view.findViewById(R.id.telecom_incoming_call_dial_button); 113 if (mAddIncomingCall == null) { 114 finish(); 115 return; 116 } 117 mAddIncomingCall.setOnClickListener(v -> { 118 Bundle extras = new Bundle(); 119 extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS, TEST_DIAL_NUMBER); 120 extras.putBoolean(CtsConnection.EXTRA_PLAY_CS_AUDIO, true); 121 TelecomManager telecomManager = 122 (TelecomManager) getSystemService(Context.TELECOM_SERVICE); 123 if (telecomManager == null) { 124 Log.w(TAG, "Step 2 fail - telecom service is null"); 125 mStep2Status.setImageResource(R.drawable.fs_error); 126 return; 127 } 128 Log.i(TAG, "Step 2 pass - adding new incoming call"); 129 telecomManager.addNewIncomingCall(PhoneAccountUtils.TEST_PHONE_ACCOUNT_HANDLE, extras); 130 mStep2Status.setImageResource(R.drawable.fs_good); 131 }); 132 133 mConfirmIncomingCallAnswered = view.findViewById(R.id.telecom_incoming_call_confirm_button); 134 if (mConfirmIncomingCallAnswered == null) { 135 finish(); 136 return; 137 } 138 mConfirmIncomingCallAnswered.setOnClickListener(v -> { 139 if (confirmIncomingCall()) { 140 Log.i(TAG, "Step 3 pass - new incoming call answered"); 141 mStep3Status.setImageResource(R.drawable.fs_good); 142 } else { 143 Log.w(TAG, "Step 3 fail - failed to answer new incoming call"); 144 mStep3Status.setImageResource(R.drawable.fs_error); 145 } 146 PhoneAccountUtils.unRegisterTestPhoneAccount(this); 147 }); 148 149 mStep1Status = view.findViewById(R.id.step_1_status); 150 mStep2Status = view.findViewById(R.id.step_2_status); 151 mStep3Status = view.findViewById(R.id.step_3_status); 152 mStep1Status.setImageResource(R.drawable.fs_indeterminate); 153 mStep2Status.setImageResource(R.drawable.fs_indeterminate); 154 mStep3Status.setImageResource(R.drawable.fs_indeterminate); 155 } 156 confirmIncomingCall()157 private boolean confirmIncomingCall() { 158 if (CtsConnectionService.getConnectionService() == null) { 159 return false; 160 } 161 List<CtsConnection> ongoingConnections = 162 CtsConnectionService.getConnectionService().getConnections(); 163 if (ongoingConnections == null || ongoingConnections.size() != 1) { 164 Log.w(TAG, "Step 3 fail - no ongoing call found"); 165 return false; 166 } 167 CtsConnection incomingConnection = ongoingConnections.get(0); 168 if (!incomingConnection.isIncomingCall()) { 169 Log.w(TAG, "Step 3 fail - ongoing call isn't incoming"); 170 return false; 171 } 172 if (incomingConnection.getState() != Connection.STATE_ACTIVE) { 173 Log.w(TAG, "Step 3 fail - ongoing call is not active"); 174 return false; 175 } 176 incomingConnection.onDisconnect(); 177 return true; 178 } 179 } 180