blob: 399c28a5db6bd13b5c742341adc6559332402725 [file] [log] [blame]
Sailesh Nepal646fa3d2015-01-28 02:55:36 -08001/*
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
17package com.android.server.telecom;
18
19import android.content.Context;
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080020import android.os.Handler;
Ihab Awade6dbc9d2015-03-26 10:33:44 -070021import android.os.Looper;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070022import android.telecom.Log;
23import android.telecom.Logging.Runnable;
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080024import android.telecom.PhoneAccountHandle;
25import android.telephony.TelephonyManager;
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080026
27import java.util.Collection;
28import java.util.Objects;
29
30/**
31 * Registers a timeout for a call and disconnects the call when the timeout expires.
32 */
Brad Ebingere62e9e82016-02-01 18:26:40 -080033final class CreateConnectionTimeout extends Runnable {
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080034 private final Context mContext;
35 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
36 private final ConnectionServiceWrapper mConnectionService;
37 private final Call mCall;
Ihab Awade6dbc9d2015-03-26 10:33:44 -070038 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080039 private boolean mIsRegistered;
40 private boolean mIsCallTimedOut;
41
42 CreateConnectionTimeout(Context context, PhoneAccountRegistrar phoneAccountRegistrar,
43 ConnectionServiceWrapper service, Call call) {
Brad Ebingerf5e06662016-08-25 16:16:27 -070044 super("CCT", null /*lock*/);
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080045 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 Gunn6ffe5312015-08-12 08:19:20 -070054 if (!mCall.isEmergencyCall()) {
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080055 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 Mak240656f2015-12-04 11:36:22 +000060 PhoneAccountHandle connectionManager =
61 mPhoneAccountRegistrar.getSimCallManagerFromCall(mCall);
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080062 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 Nepal8ed7eeb2015-09-08 12:30:13 -070071 // 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 Nepal646fa3d2015-01-28 02:55:36 -080075 return false;
76 }
77
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -070078 Log.i(this, "isTimeoutNeededForCall, returning true");
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080079 return true;
80 }
81
82 void registerTimeout() {
83 Log.d(this, "registerTimeout");
84 mIsRegistered = true;
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -070085
86 long timeoutLengthMillis = getTimeoutLengthMillis();
87 if (timeoutLengthMillis <= 0) {
88 Log.d(this, "registerTimeout, timeout set to %d, skipping", timeoutLengthMillis);
89 } else {
Brad Ebingere62e9e82016-02-01 18:26:40 -080090 mHandler.postDelayed(prepare(), timeoutLengthMillis);
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -070091 }
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080092 }
93
94 void unregisterTimeout() {
95 Log.d(this, "unregisterTimeout");
96 mIsRegistered = false;
97 mHandler.removeCallbacksAndMessages(null);
Brad Ebingere62e9e82016-02-01 18:26:40 -080098 cancel();
Sailesh Nepal646fa3d2015-01-28 02:55:36 -080099 }
100
101 boolean isCallTimedOut() {
102 return mIsCallTimedOut;
103 }
104
105 @Override
Brad Ebingere62e9e82016-02-01 18:26:40 -0800106 public void loggedRun() {
Sailesh Nepal646fa3d2015-01-28 02:55:36 -0800107 if (mIsRegistered && isCallBeingPlaced(mCall)) {
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700108 Log.i(this, "run, call timed out, calling disconnect");
Sailesh Nepal646fa3d2015-01-28 02:55:36 -0800109 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 Gunn1e37be52016-07-11 08:54:23 -0700118 || state == CallState.DIALING
119 || state == CallState.PULLING;
Sailesh Nepal646fa3d2015-01-28 02:55:36 -0800120 }
121
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700122 private long getTimeoutLengthMillis() {
Sailesh Nepal646fa3d2015-01-28 02:55:36 -0800123 // If the radio is off then use a longer timeout. This gives us more time to power on the
124 // radio.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700125 TelephonyManager telephonyManager =
126 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
127 if (telephonyManager.isRadioOn()) {
128 return Timeouts.getEmergencyCallTimeoutMillis(mContext.getContentResolver());
129 } else {
Sailesh Nepal646fa3d2015-01-28 02:55:36 -0800130 return Timeouts.getEmergencyCallTimeoutRadioOffMillis(
131 mContext.getContentResolver());
Sailesh Nepal646fa3d2015-01-28 02:55:36 -0800132 }
133 }
Sailesh Nepal646fa3d2015-01-28 02:55:36 -0800134}