Merge "Add DUMP permission check to phone service." into nyc-mr1-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4906ba2..26b0c7a 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -337,7 +337,7 @@
              non-voice-capable tablets and regular phone devices. -->
         <activity android:name="MobileNetworkSettings"
             android:label="@string/settings_label"
-            android:theme="@style/SettingsLight">
+            android:theme="@style/NetworkOperatorsSettingsTheme">
             <intent-filter>
                 <action android:name="android.intent.action.VIEW" />
                 <action android:name="android.intent.action.MAIN" />
diff --git a/res/values/colors.xml b/res/values/colors.xml
index 67572ed..870d692 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -54,9 +54,4 @@
 
     <color name="dialer_dialpad_touch_tint">#330288d1</color>
     <color name="floating_action_button_touch_tint">#80ffffff</color>
-
-    <color name="network_operators_color_primary">#ff263238</color>
-    <color name="network_operators_color_primary_dark">#ff21272b</color>
-
-    <color name="emergency_dialer_background">#ff263238</color>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 9d2d47f..057352d 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -153,13 +153,6 @@
         <item name="android:layout_marginEnd">5dip</item>
     </style>
 
-    <!-- Theme for the activity com.android.phone.Settings, which is the
-         "Mobile network settings" screen (used on non-voice-capable
-         tablets as well as regular phone devices.) -->
-    <style name="Theme.Settings" parent="@android:style/Theme.Holo.DialogWhenLarge">
-        <item name="android:windowCloseOnTouchOutside">true</item>
-    </style>
-
     <style name="SettingsLight" parent="android:Theme.Material.Light">
         <item name="android:windowBackground">@color/phone_settings_background_color</item>
         <item name="android:windowContentOverlay">@null</item>
@@ -186,11 +179,7 @@
         <item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>
     </style>
 
-    <style name="NetworkOperatorsSettingsTheme" parent="@android:style/Theme.Material.Light">
-        <item name="android:actionBarTheme">@android:style/ThemeOverlay.Material.Dark.ActionBar</item>
-        <item name="android:colorPrimary">@color/network_operators_color_primary</item>
-        <item name="android:colorPrimaryDark">@color/network_operators_color_primary_dark</item>
-    </style>
+    <style name="NetworkOperatorsSettingsTheme" parent="@android:style/Theme.DeviceDefault.Settings" />
 
     <style name="Empty" parent="@android:style/Theme.Material.Light">
         <item name="android:windowIsTranslucent">true</item>
@@ -256,10 +245,9 @@
         <item name="android:src">@drawable/overflow_menu</item>
     </style>
 
-    <style name="EmergencyDialerTheme" parent="@android:style/Theme.Material.NoActionBar">
-        <item name="android:colorPrimary">@color/emergency_dialer_background</item>
-        <item name="android:colorPrimaryDark">@color/emergency_dialer_background</item>
-        <item name="android:windowBackground">@color/emergency_dialer_background</item>
+    <style name="EmergencyDialerTheme" parent="@*android:style/Theme.DeviceDefault.Settings.Dark.NoActionBar">
+        <item name="android:colorPrimaryDark">?android:attr/colorPrimary</item>
+        <item name="android:windowBackground">?android:attr/colorPrimary</item>
         <item name="android:statusBarColor">@android:color/transparent</item>
         <item name="android:navigationBarColor">@android:color/transparent</item>
         <item name="android:homeAsUpIndicator">@drawable/ic_back_arrow</item>
@@ -296,8 +284,4 @@
         <item name="android:backgroundDimEnabled">false</item>
     </style>
 
-    <style name="Theme.Material.Settings" parent="@android:style/Theme.Material.Settings">
-        <item name="@*android:actionBarSize">56dip</item>
-        <item name="preferenceBackgroundColor">@drawable/preference_background</item>
-    </style>
 </resources>
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index 404c976..fd4815e 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -421,7 +421,6 @@
     @Override
     protected void onCreate(Bundle icicle) {
         if (DBG) log("onCreate:+");
-        setTheme(R.style.Theme_Material_Settings);
         super.onCreate(icicle);
 
         mHandler = new MyHandler();
diff --git a/src/com/android/services/telephony/EmergencyCallStateListener.java b/src/com/android/services/telephony/EmergencyCallStateListener.java
index 19b3d36..2346a7f 100644
--- a/src/com/android/services/telephony/EmergencyCallStateListener.java
+++ b/src/com/android/services/telephony/EmergencyCallStateListener.java
@@ -26,6 +26,7 @@
 import com.android.internal.os.SomeArgs;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.SubscriptionController;
 
 /**
  * Helper class that listens to a Phone's radio state and sends a callback when the radio state of
@@ -141,7 +142,8 @@
      * finally come up. In that case, it's now safe to actually place the emergency call.
      */
     private void onServiceStateChanged(ServiceState state) {
-        Log.d(this, "onServiceStateChanged(), new state = %s, Phone ", state);
+        Log.d(this, "onServiceStateChanged(), new state = %s, Phone = %s", state,
+                mPhone.getPhoneId());
 
         // Possible service states:
         // - STATE_IN_SERVICE        // Normal operation
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index ad4859f..9f252a2 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -175,10 +175,83 @@
             }
         }
 
-        boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(this, number);
+        final boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(this, number);
 
-        // Get the right phone object from the account data passed in.
-        final Phone phone = getPhoneForAccount(request.getAccountHandle(), isEmergencyNumber);
+        if (isEmergencyNumber && !isRadioOn()) {
+            final Uri emergencyHandle = handle;
+            // By default, Connection based on the default Phone, since we need to return to Telecom
+            // now.
+            final int defaultPhoneType = PhoneFactory.getDefaultPhone().getPhoneType();
+            final Connection emergencyConnection = getTelephonyConnection(request, number,
+                    isEmergencyNumber, emergencyHandle, PhoneFactory.getDefaultPhone());
+            if (mEmergencyCallHelper == null) {
+                mEmergencyCallHelper = new EmergencyCallHelper(this);
+            }
+            mEmergencyCallHelper.enableEmergencyCalling(new EmergencyCallStateListener.Callback() {
+                @Override
+                public void onComplete(EmergencyCallStateListener listener, boolean isRadioReady) {
+                    if (isRadioReady) {
+                        // Get the right phone object since the radio has been turned on
+                        // successfully.
+                        final Phone phone = getPhoneForAccount(request.getAccountHandle(),
+                                isEmergencyNumber);
+                        // If the PhoneType of the Phone being used is different than the Default
+                        // Phone, then we need create a new Connection using that PhoneType and
+                        // replace it in Telecom.
+                        if (phone.getPhoneType() != defaultPhoneType) {
+                            Connection repConnection = getTelephonyConnection(request, number,
+                                    isEmergencyNumber, emergencyHandle, phone);
+                            // If there was a failure, the resulting connection will not be a
+                            // TelephonyConnection, so don't place the call, just return!
+                            if (repConnection instanceof TelephonyConnection) {
+                                placeOutgoingConnection((TelephonyConnection) repConnection, phone,
+                                        request);
+                            }
+                            // Notify Telecom of the new Connection type.
+                            // TODO: Switch out the underlying connection instead of creating a new
+                            // one and causing UI Jank.
+                            addExistingConnection(PhoneUtils.makePstnPhoneAccountHandle(phone),
+                                    repConnection);
+                            // Remove the old connection from Telecom after.
+                            emergencyConnection.setDisconnected(
+                                    DisconnectCauseUtil.toTelecomDisconnectCause(
+                                            android.telephony.DisconnectCause.OUTGOING_CANCELED,
+                                            "Reconnecting outgoing Emergency Call."));
+                            emergencyConnection.destroy();
+                        } else {
+                            placeOutgoingConnection((TelephonyConnection) emergencyConnection,
+                                    phone, request);
+                        }
+                    } else {
+                        Log.w(this, "onCreateOutgoingConnection, failed to turn on radio");
+                        emergencyConnection.setDisconnected(
+                                DisconnectCauseUtil.toTelecomDisconnectCause(
+                                        android.telephony.DisconnectCause.POWER_OFF,
+                                        "Failed to turn on radio."));
+                        emergencyConnection.destroy();
+                    }
+                }
+            });
+            // Return the still unconnected GsmConnection and wait for the Radios to boot before
+            // connecting it to the underlying Phone.
+            return emergencyConnection;
+        } else {
+            // Get the right phone object from the account data passed in.
+            final Phone phone = getPhoneForAccount(request.getAccountHandle(), isEmergencyNumber);
+            Connection resultConnection = getTelephonyConnection(request, number, isEmergencyNumber,
+                    handle, phone);
+            // If there was a failure, the resulting connection will not be a TelephonyConnection,
+            // so don't place the call!
+            if(resultConnection instanceof TelephonyConnection) {
+                placeOutgoingConnection((TelephonyConnection) resultConnection, phone, request);
+            }
+            return resultConnection;
+        }
+    }
+
+    private Connection getTelephonyConnection(final ConnectionRequest request, final String number,
+            boolean isEmergencyNumber, final Uri handle, Phone phone) {
+
         if (phone == null) {
             final Context context = getApplicationContext();
             if (context.getResources().getBoolean(R.bool.config_checkSimStateBeforeOutgoingCall)) {
@@ -228,7 +301,6 @@
                 state = phone.getServiceState().getDataRegState();
             }
         }
-        boolean useEmergencyCallHelper = false;
 
         // If we're dialing a non-emergency number and the phone is in ECM mode, reject the call if
         // carrier configuration specifies that we cannot make non-emergency calls in ECM mode.
@@ -250,11 +322,7 @@
             }
         }
 
-        if (isEmergencyNumber) {
-            if (!phone.isRadioOn()) {
-                useEmergencyCallHelper = true;
-            }
-        } else {
+        if (!isEmergencyNumber) {
             switch (state) {
                 case ServiceState.STATE_IN_SERVICE:
                 case ServiceState.STATE_EMERGENCY_ONLY:
@@ -309,34 +377,6 @@
         connection.setInitializing();
         connection.setVideoState(request.getVideoState());
 
-        if (useEmergencyCallHelper) {
-            if (mEmergencyCallHelper == null) {
-                mEmergencyCallHelper = new EmergencyCallHelper(this);
-            }
-            mEmergencyCallHelper.enableEmergencyCalling(new EmergencyCallStateListener.Callback() {
-                        @Override
-                        public void onComplete(EmergencyCallStateListener listener,
-                                boolean isRadioReady) {
-                            if (connection.getState() == Connection.STATE_DISCONNECTED) {
-                                // If the connection has already been disconnected, do nothing.
-                            } else if (isRadioReady) {
-                                connection.setInitialized();
-                                placeOutgoingConnection(connection, phone, request);
-                            } else {
-                                Log.d(this, "onCreateOutgoingConnection, failed to turn on radio");
-                                connection.setDisconnected(
-                                        DisconnectCauseUtil.toTelecomDisconnectCause(
-                                                android.telephony.DisconnectCause.POWER_OFF,
-                                                "Failed to turn on radio."));
-                                connection.destroy();
-                            }
-                        }
-                    });
-
-        } else {
-            placeOutgoingConnection(connection, phone, request);
-        }
-
         return connection;
     }
 
@@ -514,6 +554,14 @@
 
     }
 
+    private boolean isRadioOn() {
+        boolean result = false;
+        for (Phone phone : PhoneFactory.getPhones()) {
+            result |= phone.isRadioOn();
+        }
+        return result;
+    }
+
     private void placeOutgoingConnection(
             TelephonyConnection connection, Phone phone, ConnectionRequest request) {
         String number = connection.getAddress().getSchemeSpecificPart();