1 /* 2 * Copyright 2015, 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; 18 19 import android.content.Context; 20 import android.os.Handler; 21 import android.os.Looper; 22 import android.telecom.Log; 23 import android.telecom.Logging.Runnable; 24 import android.telecom.PhoneAccount; 25 import android.telecom.PhoneAccountHandle; 26 import android.telephony.TelephonyManager; 27 28 import com.android.internal.annotations.VisibleForTesting; 29 30 import java.util.Collection; 31 import java.util.Objects; 32 33 /** 34 * Registers a timeout for a call and disconnects the call when the timeout expires. 35 */ 36 @VisibleForTesting 37 public final class CreateConnectionTimeout extends Runnable { 38 private final Context mContext; 39 private final PhoneAccountRegistrar mPhoneAccountRegistrar; 40 private final ConnectionServiceWrapper mConnectionService; 41 private final Call mCall; 42 private final Handler mHandler = new Handler(Looper.getMainLooper()); 43 private boolean mIsRegistered; 44 private boolean mIsCallTimedOut; 45 private final Timeouts.Adapter mTimeoutsAdapter; 46 47 @VisibleForTesting CreateConnectionTimeout(Context context, PhoneAccountRegistrar phoneAccountRegistrar, ConnectionServiceWrapper service, Call call, Timeouts.Adapter timeoutsAdapter)48 public CreateConnectionTimeout(Context context, PhoneAccountRegistrar phoneAccountRegistrar, 49 ConnectionServiceWrapper service, Call call, Timeouts.Adapter timeoutsAdapter) { 50 super("CCT", null /*lock*/); 51 mContext = context; 52 mPhoneAccountRegistrar = phoneAccountRegistrar; 53 mConnectionService = service; 54 mCall = call; 55 mTimeoutsAdapter = timeoutsAdapter; 56 } 57 58 @VisibleForTesting isTimeoutNeededForCall(Collection<PhoneAccountHandle> accounts, PhoneAccountHandle currentAccount)59 public boolean isTimeoutNeededForCall(Collection<PhoneAccountHandle> accounts, 60 PhoneAccountHandle currentAccount) { 61 // Non-emergency calls timeout automatically at the radio layer. No need for a timeout here. 62 if (!mCall.isEmergencyCall()) { 63 Log.d(this, "isTimeoutNeededForCall, not an emergency call"); 64 return false; 65 } 66 67 // If there's no connection manager to fallback on then there's no point in having a 68 // timeout. 69 PhoneAccountHandle connectionManager = 70 mPhoneAccountRegistrar.getSimCallManagerFromCall(mCall); 71 if (!accounts.contains(connectionManager)) { 72 Log.d(this, "isTimeoutNeededForCall, no connection manager"); 73 return false; 74 } 75 76 // No need to add a timeout if the current attempt is over the connection manager. 77 if (Objects.equals(connectionManager, currentAccount)) { 78 Log.d(this, "isTimeoutNeededForCall, already attempting over connection manager"); 79 return false; 80 } 81 82 // Timeout is only supported for SIM call managers that are set by the carrier. 83 if (connectionManager != null && !Objects.equals(connectionManager.getComponentName(), 84 mPhoneAccountRegistrar.getSystemSimCallManagerComponent())) { 85 Log.d(this, "isTimeoutNeededForCall, not a system sim call manager"); 86 return false; 87 } 88 89 Log.i(this, "isTimeoutNeededForCall, returning true"); 90 return true; 91 } 92 registerTimeout()93 void registerTimeout() { 94 Log.d(this, "registerTimeout"); 95 mIsRegistered = true; 96 97 long timeoutLengthMillis = getTimeoutLengthMillis(); 98 if (timeoutLengthMillis <= 0) { 99 Log.d(this, "registerTimeout, timeout set to %d, skipping", timeoutLengthMillis); 100 } else { 101 mHandler.postDelayed(prepare(), timeoutLengthMillis); 102 } 103 } 104 unregisterTimeout()105 void unregisterTimeout() { 106 Log.d(this, "unregisterTimeout"); 107 mIsRegistered = false; 108 mHandler.removeCallbacksAndMessages(null); 109 cancel(); 110 } 111 isCallTimedOut()112 boolean isCallTimedOut() { 113 return mIsCallTimedOut; 114 } 115 116 @Override loggedRun()117 public void loggedRun() { 118 PhoneAccountHandle connectionManager = 119 mPhoneAccountRegistrar.getSimCallManagerFromCall(mCall); 120 if (connectionManager != null) { 121 PhoneAccount account = mPhoneAccountRegistrar.getPhoneAccount(connectionManager, 122 connectionManager.getUserHandle()); 123 if (account != null && account.hasCapabilities( 124 (PhoneAccount.CAPABILITY_SUPPORTS_VOICE_CALLING_INDICATIONS 125 | PhoneAccount.CAPABILITY_VOICE_CALLING_AVAILABLE))) { 126 // If we have encountered the timeout and there is an in service 127 // ConnectionManager, disconnect the call so that it can be attempted over 128 // the ConnectionManager. 129 timeoutCallIfNeeded(); 130 return; 131 } 132 Log.i( 133 this, 134 "loggedRun, no PhoneAccount with voice calling capabilities, not timing out call"); 135 } 136 } 137 timeoutCallIfNeeded()138 private void timeoutCallIfNeeded() { 139 if (mIsRegistered && isCallBeingPlaced(mCall)) { 140 Log.i(this, "timeoutCallIfNeeded, call timed out, calling disconnect"); 141 mIsCallTimedOut = true; 142 mConnectionService.disconnect(mCall); 143 } 144 } 145 isCallBeingPlaced(Call call)146 static boolean isCallBeingPlaced(Call call) { 147 int state = call.getState(); 148 return state == CallState.NEW 149 || state == CallState.CONNECTING 150 || state == CallState.DIALING 151 || state == CallState.PULLING; 152 } 153 getTimeoutLengthMillis()154 private long getTimeoutLengthMillis() { 155 // If the radio is off then use a longer timeout. This gives us more time to power on the 156 // radio. 157 try { 158 TelephonyManager telephonyManager = 159 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 160 if (telephonyManager.isRadioOn()) { 161 return mTimeoutsAdapter.getEmergencyCallTimeoutMillis( 162 mContext.getContentResolver()); 163 } else { 164 return mTimeoutsAdapter.getEmergencyCallTimeoutRadioOffMillis( 165 mContext.getContentResolver()); 166 } 167 } catch (UnsupportedOperationException uoe) { 168 Log.e(this, uoe, "getTimeoutLengthMillis - telephony is not supported"); 169 return mTimeoutsAdapter.getEmergencyCallTimeoutMillis(mContext.getContentResolver()); 170 } 171 } 172 } 173