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