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.PhoneAccountHandle; 23 import android.telephony.TelephonyManager; 24 25 import java.util.Collection; 26 import java.util.Objects; 27 28 /** 29 * Registers a timeout for a call and disconnects the call when the timeout expires. 30 */ 31 final class CreateConnectionTimeout implements Runnable { 32 private final Context mContext; 33 private final PhoneAccountRegistrar mPhoneAccountRegistrar; 34 private final ConnectionServiceWrapper mConnectionService; 35 private final Call mCall; 36 private final Handler mHandler = new Handler(Looper.getMainLooper()); 37 private boolean mIsRegistered; 38 private boolean mIsCallTimedOut; 39 CreateConnectionTimeout(Context context, PhoneAccountRegistrar phoneAccountRegistrar, ConnectionServiceWrapper service, Call call)40 CreateConnectionTimeout(Context context, PhoneAccountRegistrar phoneAccountRegistrar, 41 ConnectionServiceWrapper service, Call call) { 42 mContext = context; 43 mPhoneAccountRegistrar = phoneAccountRegistrar; 44 mConnectionService = service; 45 mCall = call; 46 } 47 isTimeoutNeededForCall(Collection<PhoneAccountHandle> accounts, PhoneAccountHandle currentAccount)48 boolean isTimeoutNeededForCall(Collection<PhoneAccountHandle> accounts, 49 PhoneAccountHandle currentAccount) { 50 // Non-emergency calls timeout automatically at the radio layer. No need for a timeout here. 51 if (!mCall.isEmergencyCall()) { 52 return false; 53 } 54 55 // If there's no connection manager to fallback on then there's no point in having a 56 // timeout. 57 PhoneAccountHandle connectionManager = mPhoneAccountRegistrar.getSimCallManager(); 58 if (!accounts.contains(connectionManager)) { 59 return false; 60 } 61 62 // No need to add a timeout if the current attempt is over the connection manager. 63 if (Objects.equals(connectionManager, currentAccount)) { 64 return false; 65 } 66 67 // Timeout is only supported for SIM call managers that are set by the carrier. 68 if (!Objects.equals(connectionManager.getComponentName(), 69 mPhoneAccountRegistrar.getSystemSimCallManagerComponent())) { 70 Log.d(this, "isTimeoutNeededForCall, not a system sim call manager"); 71 return false; 72 } 73 74 Log.i(this, "isTimeoutNeededForCall, returning true"); 75 return true; 76 } 77 registerTimeout()78 void registerTimeout() { 79 Log.d(this, "registerTimeout"); 80 mIsRegistered = true; 81 82 long timeoutLengthMillis = getTimeoutLengthMillis(); 83 if (timeoutLengthMillis <= 0) { 84 Log.d(this, "registerTimeout, timeout set to %d, skipping", timeoutLengthMillis); 85 } else { 86 mHandler.postDelayed(this, timeoutLengthMillis); 87 } 88 } 89 unregisterTimeout()90 void unregisterTimeout() { 91 Log.d(this, "unregisterTimeout"); 92 mIsRegistered = false; 93 mHandler.removeCallbacksAndMessages(null); 94 } 95 isCallTimedOut()96 boolean isCallTimedOut() { 97 return mIsCallTimedOut; 98 } 99 100 @Override run()101 public void run() { 102 if (mIsRegistered && isCallBeingPlaced(mCall)) { 103 Log.i(this, "run, call timed out, calling disconnect"); 104 mIsCallTimedOut = true; 105 mConnectionService.disconnect(mCall); 106 } 107 } 108 isCallBeingPlaced(Call call)109 static boolean isCallBeingPlaced(Call call) { 110 int state = call.getState(); 111 return state == CallState.NEW 112 || state == CallState.CONNECTING 113 || state == CallState.DIALING; 114 } 115 getTimeoutLengthMillis()116 private long getTimeoutLengthMillis() { 117 // If the radio is off then use a longer timeout. This gives us more time to power on the 118 // radio. 119 TelephonyManager telephonyManager = 120 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 121 if (telephonyManager.isRadioOn()) { 122 return Timeouts.getEmergencyCallTimeoutMillis(mContext.getContentResolver()); 123 } else { 124 return Timeouts.getEmergencyCallTimeoutRadioOffMillis( 125 mContext.getContentResolver()); 126 } 127 } 128 } 129