Change requirements for emergency call timeout

Previously if a SIM call manager was set we allowed
emergency calls to timeout if:
  - the device was not in service
  - and the device was connected to Wi-Fi
  - and a connection manager with a emergency capability
    was registered

Unfortunately this prevented scenarios like making an
e911 call over LTE. This CL changes the requirements to
the following:
  - a connection manager with a emergency capability
    is registered
  - and the connection manager was set by the carrier

Bug: 19199181
Bug: 23900801

Change-Id: I8fd05e88e3c02b90170b5809366b71627b2cfbd5
diff --git a/src/com/android/server/telecom/CreateConnectionTimeout.java b/src/com/android/server/telecom/CreateConnectionTimeout.java
index 8acc2b4..06dc9ed 100644
--- a/src/com/android/server/telecom/CreateConnectionTimeout.java
+++ b/src/com/android/server/telecom/CreateConnectionTimeout.java
@@ -17,14 +17,10 @@
 package com.android.server.telecom;
 
 import android.content.Context;
-import android.net.ConnectivityManager;
-import android.net.NetworkInfo;
 import android.os.Handler;
 import android.os.Looper;
 import android.telecom.PhoneAccountHandle;
 import android.telephony.TelephonyManager;
-import android.telephony.PhoneStateListener;
-import android.telephony.ServiceState;
 
 import java.util.Collection;
 import java.util.Objects;
@@ -32,7 +28,7 @@
 /**
  * Registers a timeout for a call and disconnects the call when the timeout expires.
  */
-final class CreateConnectionTimeout extends PhoneStateListener implements Runnable {
+final class CreateConnectionTimeout implements Runnable {
     private final Context mContext;
     private final PhoneAccountRegistrar mPhoneAccountRegistrar;
     private final ConnectionServiceWrapper mConnectionService;
@@ -43,7 +39,6 @@
 
     CreateConnectionTimeout(Context context, PhoneAccountRegistrar phoneAccountRegistrar,
             ConnectionServiceWrapper service, Call call) {
-        super(Looper.getMainLooper());
         mContext = context;
         mPhoneAccountRegistrar = phoneAccountRegistrar;
         mConnectionService = service;
@@ -69,27 +64,27 @@
             return false;
         }
 
-        // To reduce the number of scenarios where a timeout is needed, only use a timeout if
-        // we're connected to Wi-Fi. This ensures that the fallback connection manager has an
-        // alternate route to place the call. TODO: remove this condition or allow connection
-        // managers to specify transports. See http://b/19199181.
-        if (!isConnectedToWifi()) {
+        // Timeout is only supported for SIM call managers that are set by the carrier.
+        if (!Objects.equals(connectionManager.getComponentName(),
+                mPhoneAccountRegistrar.getSystemSimCallManagerComponent())) {
+            Log.d(this, "isTimeoutNeededForCall, not a system sim call manager");
             return false;
         }
 
-        Log.d(this, "isTimeoutNeededForCall, returning true");
+        Log.i(this, "isTimeoutNeededForCall, returning true");
         return true;
     }
 
     void registerTimeout() {
         Log.d(this, "registerTimeout");
         mIsRegistered = true;
-        // First find out the cellular service state. Based on the state we decide whether a timeout
-        // will actually be enforced and if so how long it should be.
-        TelephonyManager telephonyManager =
-            (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
-        telephonyManager.listen(this, PhoneStateListener.LISTEN_SERVICE_STATE);
-        telephonyManager.listen(this, 0);
+
+        long timeoutLengthMillis = getTimeoutLengthMillis();
+        if (timeoutLengthMillis <= 0) {
+            Log.d(this, "registerTimeout, timeout set to %d, skipping", timeoutLengthMillis);
+        } else {
+            mHandler.postDelayed(this, timeoutLengthMillis);
+        }
     }
 
     void unregisterTimeout() {
@@ -103,24 +98,9 @@
     }
 
     @Override
-    public void onServiceStateChanged(ServiceState serviceState) {
-        long timeoutLengthMillis = getTimeoutLengthMillis(serviceState);
-        if (!mIsRegistered) {
-            Log.d(this, "onServiceStateChanged, timeout no longer registered, skipping");
-        } else if (timeoutLengthMillis  <= 0) {
-            Log.d(this, "onServiceStateChanged, timeout set to %d, skipping", timeoutLengthMillis);
-        } else if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
-            // If cellular service is available then don't bother with a timeout.
-            Log.d(this, "onServiceStateChanged, cellular service available, skipping");
-        } else {
-            mHandler.postDelayed(this, timeoutLengthMillis);
-        }
-    }
-
-    @Override
     public void run() {
         if (mIsRegistered && isCallBeingPlaced(mCall)) {
-            Log.d(this, "run, call timed out, calling disconnect");
+            Log.i(this, "run, call timed out, calling disconnect");
             mIsCallTimedOut = true;
             mConnectionService.disconnect(mCall);
         }
@@ -133,24 +113,16 @@
             || state == CallState.DIALING;
     }
 
-    private long getTimeoutLengthMillis(ServiceState serviceState) {
+    private long getTimeoutLengthMillis() {
         // If the radio is off then use a longer timeout. This gives us more time to power on the
         // radio.
-        if (serviceState.getState() == ServiceState.STATE_POWER_OFF) {
+        TelephonyManager telephonyManager =
+            (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
+        if (telephonyManager.isRadioOn()) {
+            return Timeouts.getEmergencyCallTimeoutMillis(mContext.getContentResolver());
+        } else {
             return Timeouts.getEmergencyCallTimeoutRadioOffMillis(
                     mContext.getContentResolver());
-        } else {
-            return Timeouts.getEmergencyCallTimeoutMillis(mContext.getContentResolver());
         }
     }
-
-    private boolean isConnectedToWifi() {
-        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(
-            Context.CONNECTIVITY_SERVICE);
-        if (cm != null) {
-          NetworkInfo ni = cm.getActiveNetworkInfo();
-          return ni != null && ni.isConnected() && ni.getType() == ConnectivityManager.TYPE_WIFI;
-        }
-        return false;
-    }
 }
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index 65847b8..a795d6f 100644
--- a/src/com/android/server/telecom/PhoneAccountRegistrar.java
+++ b/src/com/android/server/telecom/PhoneAccountRegistrar.java
@@ -286,6 +286,19 @@
         return getSimCallManager(user);
     }
 
+    public ComponentName getSystemSimCallManagerComponent() {
+        String defaultSimCallManager = null;
+        CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
+                Context.CARRIER_CONFIG_SERVICE);
+        PersistableBundle configBundle = configManager.getConfig();
+        if (configBundle != null) {
+            defaultSimCallManager = configBundle.getString(
+                    CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
+        }
+        return TextUtils.isEmpty(defaultSimCallManager)
+            ?  null : ComponentName.unflattenFromString(defaultSimCallManager);
+    }
+
     /**
      * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
      * Manager. SIM Call Manager returned corresponds to the following priority order:
@@ -300,17 +313,7 @@
         String dialerPackage = DefaultDialerManager.getDefaultDialerApplication(mContext, user);
 
         // Check carrier config.
-        String defaultSimCallManager = null;
-        CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
-                Context.CARRIER_CONFIG_SERVICE);
-        PersistableBundle configBundle = configManager.getConfig();
-        if (configBundle != null) {
-            defaultSimCallManager = configBundle.getString(
-                    CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
-        }
-
-        ComponentName systemSimCallManagerComponent = TextUtils.isEmpty(defaultSimCallManager) ?
-                null : ComponentName.unflattenFromString(defaultSimCallManager);
+        ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
 
         PhoneAccountHandle dialerSimCallManager = null;
         PhoneAccountHandle systemSimCallManager = null;