1 /* 2 * Copyright (C) 2016 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.managedprofile; 18 19 import android.net.Uri; 20 import android.telecom.Connection; 21 import android.telecom.ConnectionRequest; 22 import android.telecom.ConnectionService; 23 import android.telecom.DisconnectCause; 24 import android.telecom.PhoneAccountHandle; 25 import android.telecom.RemoteConference; 26 import android.telecom.RemoteConnection; 27 import android.telecom.TelecomManager; 28 29 /** 30 * A simple connection service that hangs up automatically for incoming and outgoing call. 31 */ 32 public class TestConnectionService extends ConnectionService { 33 34 public static final String MISSED_PHONE_NUMBER = "520"; 35 public static final String NORMAL_PHONE_NUMBER = "886"; 36 37 @Override onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)38 public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, 39 ConnectionRequest request) { 40 final TestConnection connection = new TestConnection(); 41 connection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED); 42 connection.setVideoState(request.getVideoState()); 43 hangUpAsync(connection); 44 return connection; 45 } 46 47 @Override onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)48 public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, 49 ConnectionRequest request) { 50 final TestConnection connection = new TestConnection(); 51 connection.setVideoState(request.getVideoState()); 52 final Uri address = 53 request.getExtras().getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS); 54 connection.setAddress(address, TelecomManager.PRESENTATION_ALLOWED); 55 hangUpAsync(connection); 56 return connection; 57 } 58 59 @Override onConference(Connection connection1, Connection connection2)60 public void onConference(Connection connection1, Connection connection2) { 61 } 62 63 @Override onRemoteExistingConnectionAdded(RemoteConnection connection)64 public void onRemoteExistingConnectionAdded(RemoteConnection connection) { 65 } 66 67 @Override onRemoteConferenceAdded(RemoteConference conference)68 public void onRemoteConferenceAdded(RemoteConference conference) { 69 } 70 71 public static class TestConnection extends Connection { 72 73 @Override onAnswer()74 public void onAnswer() { 75 super.onAnswer(); 76 } 77 78 @Override onAnswer(int videoState)79 public void onAnswer(int videoState) { 80 super.onAnswer(videoState); 81 setActive(); 82 } 83 84 @Override onReject()85 public void onReject() { 86 super.onReject(); 87 setDisconnected(new DisconnectCause(DisconnectCause.REJECTED)); 88 destroy(); 89 } 90 91 @Override onHold()92 public void onHold() { 93 super.onHold(); 94 setOnHold(); 95 } 96 97 @Override onUnhold()98 public void onUnhold() { 99 super.onUnhold(); 100 setActive(); 101 } 102 103 @Override onDisconnect()104 public void onDisconnect() { 105 super.onDisconnect(); 106 setDisconnected(new DisconnectCause(DisconnectCause.LOCAL)); 107 destroy(); 108 } 109 110 @Override onAbort()111 public void onAbort() { 112 super.onAbort(); 113 setDisconnected(new DisconnectCause(DisconnectCause.UNKNOWN)); 114 destroy(); 115 } 116 } 117 convertNumberToCause(String number)118 private static int convertNumberToCause(String number) { 119 switch (number) { 120 case NORMAL_PHONE_NUMBER: 121 return DisconnectCause.LOCAL; 122 case MISSED_PHONE_NUMBER: 123 return DisconnectCause.MISSED; 124 } 125 throw new IllegalArgumentException("Should not happen"); 126 } 127 128 /** 129 * Hang up the call after 5 second in a background thread. 130 * TODO: It is better if we could have a callback to know when we can disconnect the call. 131 */ hangUpAsync(final Connection connection)132 private static void hangUpAsync(final Connection connection) { 133 final int cause = convertNumberToCause(connection.getAddress().getSchemeSpecificPart()); 134 new Thread(new Runnable() { 135 @Override 136 public void run() { 137 try { 138 Thread.sleep(5000); 139 connection.setDisconnected(new DisconnectCause(cause)); 140 } catch (InterruptedException ex) { 141 // let it be 142 } 143 } 144 }).start(); 145 } 146 } 147