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.PhoneAccountHandle;
25 import android.telephony.TelephonyManager;
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  */
33 final class CreateConnectionTimeout extends Runnable {
34     private final Context mContext;
35     private final PhoneAccountRegistrar mPhoneAccountRegistrar;
36     private final ConnectionServiceWrapper mConnectionService;
37     private final Call mCall;
38     private final Handler mHandler = new Handler(Looper.getMainLooper());
39     private boolean mIsRegistered;
40     private boolean mIsCallTimedOut;
41 
CreateConnectionTimeout(Context context, PhoneAccountRegistrar phoneAccountRegistrar, ConnectionServiceWrapper service, Call call)42     CreateConnectionTimeout(Context context, PhoneAccountRegistrar phoneAccountRegistrar,
43             ConnectionServiceWrapper service, Call call) {
44         super("CCT", null /*lock*/);
45         mContext = context;
46         mPhoneAccountRegistrar = phoneAccountRegistrar;
47         mConnectionService = service;
48         mCall = call;
49     }
50 
isTimeoutNeededForCall(Collection<PhoneAccountHandle> accounts, PhoneAccountHandle currentAccount)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.
54         if (!mCall.isEmergencyCall()) {
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.
60         PhoneAccountHandle connectionManager =
61                 mPhoneAccountRegistrar.getSimCallManagerFromCall(mCall);
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 
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");
75             return false;
76         }
77 
78         Log.i(this, "isTimeoutNeededForCall, returning true");
79         return true;
80     }
81 
registerTimeout()82     void registerTimeout() {
83         Log.d(this, "registerTimeout");
84         mIsRegistered = true;
85 
86         long timeoutLengthMillis = getTimeoutLengthMillis();
87         if (timeoutLengthMillis <= 0) {
88             Log.d(this, "registerTimeout, timeout set to %d, skipping", timeoutLengthMillis);
89         } else {
90             mHandler.postDelayed(prepare(), timeoutLengthMillis);
91         }
92     }
93 
unregisterTimeout()94     void unregisterTimeout() {
95         Log.d(this, "unregisterTimeout");
96         mIsRegistered = false;
97         mHandler.removeCallbacksAndMessages(null);
98         cancel();
99     }
100 
isCallTimedOut()101     boolean isCallTimedOut() {
102         return mIsCallTimedOut;
103     }
104 
105     @Override
loggedRun()106     public void loggedRun() {
107         if (mIsRegistered && isCallBeingPlaced(mCall)) {
108             Log.i(this, "run, call timed out, calling disconnect");
109             mIsCallTimedOut = true;
110             mConnectionService.disconnect(mCall);
111         }
112     }
113 
isCallBeingPlaced(Call call)114     static boolean isCallBeingPlaced(Call call) {
115         int state = call.getState();
116         return state == CallState.NEW
117             || state == CallState.CONNECTING
118             || state == CallState.DIALING
119             || state == CallState.PULLING;
120     }
121 
getTimeoutLengthMillis()122     private long getTimeoutLengthMillis() {
123         // If the radio is off then use a longer timeout. This gives us more time to power on the
124         // radio.
125         TelephonyManager telephonyManager =
126             (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
127         if (telephonyManager.isRadioOn()) {
128             return Timeouts.getEmergencyCallTimeoutMillis(mContext.getContentResolver());
129         } else {
130             return Timeouts.getEmergencyCallTimeoutRadioOffMillis(
131                     mContext.getContentResolver());
132         }
133     }
134 }
135