Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 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; |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 20 | import android.os.Handler; |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 21 | import android.os.Looper; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 22 | import android.telecom.Log; |
| 23 | import android.telecom.Logging.Runnable; |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 24 | import android.telecom.PhoneAccountHandle; |
| 25 | import android.telephony.TelephonyManager; |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 26 | |
| 27 | import java.util.Collection; |
| 28 | import java.util.Objects; |
| 29 | |
| 30 | /** |
| 31 | * Registers a timeout for a call and disconnects the call when the timeout expires. |
| 32 | */ |
Brad Ebinger | e62e9e8 | 2016-02-01 18:26:40 -0800 | [diff] [blame] | 33 | final class CreateConnectionTimeout extends Runnable { |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 34 | private final Context mContext; |
| 35 | private final PhoneAccountRegistrar mPhoneAccountRegistrar; |
| 36 | private final ConnectionServiceWrapper mConnectionService; |
| 37 | private final Call mCall; |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 38 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 39 | private boolean mIsRegistered; |
| 40 | private boolean mIsCallTimedOut; |
| 41 | |
| 42 | CreateConnectionTimeout(Context context, PhoneAccountRegistrar phoneAccountRegistrar, |
| 43 | ConnectionServiceWrapper service, Call call) { |
Brad Ebinger | f5e0666 | 2016-08-25 16:16:27 -0700 | [diff] [blame] | 44 | super("CCT", null /*lock*/); |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 45 | mContext = context; |
| 46 | mPhoneAccountRegistrar = phoneAccountRegistrar; |
| 47 | mConnectionService = service; |
| 48 | mCall = call; |
| 49 | } |
| 50 | |
| 51 | boolean isTimeoutNeededForCall(Collection<PhoneAccountHandle> accounts, |
| 52 | PhoneAccountHandle currentAccount) { |
| 53 | // Non-emergency calls timeout automatically at the radio layer. No need for a timeout here. |
Tyler Gunn | 6ffe531 | 2015-08-12 08:19:20 -0700 | [diff] [blame] | 54 | if (!mCall.isEmergencyCall()) { |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // If there's no connection manager to fallback on then there's no point in having a |
| 59 | // timeout. |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 60 | PhoneAccountHandle connectionManager = |
| 61 | mPhoneAccountRegistrar.getSimCallManagerFromCall(mCall); |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 62 | if (!accounts.contains(connectionManager)) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // No need to add a timeout if the current attempt is over the connection manager. |
| 67 | if (Objects.equals(connectionManager, currentAccount)) { |
| 68 | return false; |
| 69 | } |
| 70 | |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 71 | // Timeout is only supported for SIM call managers that are set by the carrier. |
| 72 | if (!Objects.equals(connectionManager.getComponentName(), |
| 73 | mPhoneAccountRegistrar.getSystemSimCallManagerComponent())) { |
| 74 | Log.d(this, "isTimeoutNeededForCall, not a system sim call manager"); |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 78 | Log.i(this, "isTimeoutNeededForCall, returning true"); |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | void registerTimeout() { |
| 83 | Log.d(this, "registerTimeout"); |
| 84 | mIsRegistered = true; |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 85 | |
| 86 | long timeoutLengthMillis = getTimeoutLengthMillis(); |
| 87 | if (timeoutLengthMillis <= 0) { |
| 88 | Log.d(this, "registerTimeout, timeout set to %d, skipping", timeoutLengthMillis); |
| 89 | } else { |
Brad Ebinger | e62e9e8 | 2016-02-01 18:26:40 -0800 | [diff] [blame] | 90 | mHandler.postDelayed(prepare(), timeoutLengthMillis); |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 91 | } |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void unregisterTimeout() { |
| 95 | Log.d(this, "unregisterTimeout"); |
| 96 | mIsRegistered = false; |
| 97 | mHandler.removeCallbacksAndMessages(null); |
Brad Ebinger | e62e9e8 | 2016-02-01 18:26:40 -0800 | [diff] [blame] | 98 | cancel(); |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | boolean isCallTimedOut() { |
| 102 | return mIsCallTimedOut; |
| 103 | } |
| 104 | |
| 105 | @Override |
Brad Ebinger | e62e9e8 | 2016-02-01 18:26:40 -0800 | [diff] [blame] | 106 | public void loggedRun() { |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 107 | if (mIsRegistered && isCallBeingPlaced(mCall)) { |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 108 | Log.i(this, "run, call timed out, calling disconnect"); |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 109 | mIsCallTimedOut = true; |
| 110 | mConnectionService.disconnect(mCall); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static boolean isCallBeingPlaced(Call call) { |
| 115 | int state = call.getState(); |
| 116 | return state == CallState.NEW |
| 117 | || state == CallState.CONNECTING |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 118 | || state == CallState.DIALING |
| 119 | || state == CallState.PULLING; |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 122 | private long getTimeoutLengthMillis() { |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 123 | // If the radio is off then use a longer timeout. This gives us more time to power on the |
| 124 | // radio. |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 125 | TelephonyManager telephonyManager = |
| 126 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 127 | if (telephonyManager.isRadioOn()) { |
| 128 | return Timeouts.getEmergencyCallTimeoutMillis(mContext.getContentResolver()); |
| 129 | } else { |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 130 | return Timeouts.getEmergencyCallTimeoutRadioOffMillis( |
| 131 | mContext.getContentResolver()); |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
Sailesh Nepal | 646fa3d | 2015-01-28 02:55:36 -0800 | [diff] [blame] | 134 | } |