Merge "Add plumbing for the 911 redial" am: 0c3e81b743
am: 40eff19973

Change-Id: I135af6890da5810882dc3806cff47bcd960d12af
diff --git a/src/com/android/phone/GsmUmtsAdditionalCallOptions.java b/src/com/android/phone/GsmUmtsAdditionalCallOptions.java
index 082eaa9..b79cdd8 100644
--- a/src/com/android/phone/GsmUmtsAdditionalCallOptions.java
+++ b/src/com/android/phone/GsmUmtsAdditionalCallOptions.java
@@ -2,8 +2,10 @@
 
 import android.app.ActionBar;
 import android.os.Bundle;
+import android.os.PersistableBundle;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
+import android.telephony.CarrierConfigManager;
 import android.util.Log;
 import android.view.MenuItem;
 
@@ -26,6 +28,9 @@
     private Phone mPhone;
     private SubscriptionInfoHelper mSubscriptionInfoHelper;
 
+    private boolean mShowCLIRButton;
+    private boolean mShowCWButton;
+
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -41,24 +46,60 @@
         mCLIRButton = (CLIRListPreference) prefSet.findPreference(BUTTON_CLIR_KEY);
         mCWButton = (CallWaitingSwitchPreference) prefSet.findPreference(BUTTON_CW_KEY);
 
-        mPreferences.add(mCLIRButton);
-        mPreferences.add(mCWButton);
-
-        if (icicle == null) {
-            if (DBG) Log.d(LOG_TAG, "start to init ");
-            mCLIRButton.init(this, false, mPhone);
+        PersistableBundle b = null;
+        if (mSubscriptionInfoHelper.hasSubId()) {
+            b = PhoneGlobals.getInstance().getCarrierConfigForSubId(
+                    mSubscriptionInfoHelper.getSubId());
         } else {
-            if (DBG) Log.d(LOG_TAG, "restore stored states");
-            mInitIndex = mPreferences.size();
-            mCLIRButton.init(this, true, mPhone);
-            mCWButton.init(this, true, mPhone);
-            int[] clirArray = icicle.getIntArray(mCLIRButton.getKey());
-            if (clirArray != null) {
-                if (DBG) Log.d(LOG_TAG, "onCreate:  clirArray[0]="
-                        + clirArray[0] + ", clirArray[1]=" + clirArray[1]);
-                mCLIRButton.handleGetCLIRResult(clirArray);
+            b = PhoneGlobals.getInstance().getCarrierConfig();
+        }
+
+        if (b != null) {
+            mShowCLIRButton = b.getBoolean(
+                    CarrierConfigManager.KEY_ADDITIONAL_SETTINGS_CALLER_ID_VISIBILITY_BOOL);
+            mShowCWButton = b.getBoolean(
+                    CarrierConfigManager.KEY_ADDITIONAL_SETTINGS_CALL_WAITING_VISIBILITY_BOOL);
+        }
+
+        if (mCLIRButton != null) {
+            if (mShowCLIRButton) {
+                mPreferences.add(mCLIRButton);
             } else {
-                mCLIRButton.init(this, false, mPhone);
+                prefSet.removePreference(mCLIRButton);
+            }
+        }
+
+        if (mCWButton != null) {
+            if (mShowCWButton) {
+                mPreferences.add(mCWButton);
+            } else {
+                prefSet.removePreference(mCWButton);
+            }
+        }
+
+        if (mPreferences.size() != 0) {
+            if (icicle == null) {
+                if (DBG) Log.d(LOG_TAG, "start to init ");
+                doPreferenceInit(mInitIndex);
+            } else {
+                if (DBG) Log.d(LOG_TAG, "restore stored states");
+                mInitIndex = mPreferences.size();
+                if (mShowCWButton) {
+                    mCWButton.init(this, true, mPhone);
+                }
+                if (mShowCLIRButton) {
+                    mCLIRButton.init(this, true, mPhone);
+                    int[] clirArray = icicle.getIntArray(mCLIRButton.getKey());
+                    if (clirArray != null) {
+                        if (DBG) {
+                            Log.d(LOG_TAG, "onCreate:  clirArray[0]="
+                                    + clirArray[0] + ", clirArray[1]=" + clirArray[1]);
+                        }
+                        mCLIRButton.handleGetCLIRResult(clirArray);
+                    } else {
+                        mCLIRButton.init(this, false, mPhone);
+                    }
+                }
             }
         }
 
@@ -73,7 +114,7 @@
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
 
-        if (mCLIRButton.clirArray != null) {
+        if (mShowCLIRButton && mCLIRButton.clirArray != null) {
             outState.putIntArray(mCLIRButton.getKey(), mCLIRButton.clirArray);
         }
     }
@@ -82,10 +123,7 @@
     public void onFinished(Preference preference, boolean reading) {
         if (mInitIndex < mPreferences.size()-1 && !isFinishing()) {
             mInitIndex++;
-            Preference pref = mPreferences.get(mInitIndex);
-            if (pref instanceof CallWaitingSwitchPreference) {
-                ((CallWaitingSwitchPreference) pref).init(this, false, mPhone);
-            }
+            doPreferenceInit(mInitIndex);
         }
         super.onFinished(preference, reading);
     }
@@ -99,4 +137,15 @@
         }
         return super.onOptionsItemSelected(item);
     }
+
+    private void doPreferenceInit(int index) {
+        if (mPreferences.size() != 0) {
+            Preference pref = mPreferences.get(index);
+            if (pref instanceof CallWaitingSwitchPreference) {
+                ((CallWaitingSwitchPreference) pref).init(this, false, mPhone);
+            } else if (pref instanceof CLIRListPreference) {
+                ((CLIRListPreference) pref).init(this, false, mPhone);
+            }
+        }
+    }
 }
diff --git a/src/com/android/phone/GsmUmtsCallOptions.java b/src/com/android/phone/GsmUmtsCallOptions.java
index 3b27d28..ab44b54 100644
--- a/src/com/android/phone/GsmUmtsCallOptions.java
+++ b/src/com/android/phone/GsmUmtsCallOptions.java
@@ -18,14 +18,12 @@
 
 import android.os.Bundle;
 import android.os.PersistableBundle;
-import android.preference.CheckBoxPreference;
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceScreen;
 import android.telephony.CarrierConfigManager;
 import android.view.MenuItem;
 
-import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
 
 public class GsmUmtsCallOptions extends PreferenceActivity {
@@ -64,25 +62,45 @@
     }
 
     public static void init(PreferenceScreen prefScreen, SubscriptionInfoHelper subInfoHelper) {
-        Preference callForwardingPref = prefScreen.findPreference(CALL_FORWARDING_KEY);
-        callForwardingPref.setIntent(subInfoHelper.getIntent(GsmUmtsCallForwardOptions.class));
-
-        Preference additionalGsmSettingsPref =
-                prefScreen.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
-        additionalGsmSettingsPref.setIntent(
-                subInfoHelper.getIntent(GsmUmtsAdditionalCallOptions.class));
-
-        Preference callBarringPref = prefScreen.findPreference(CALL_BARRING_KEY);
         PersistableBundle b = null;
         if (subInfoHelper.hasSubId()) {
             b = PhoneGlobals.getInstance().getCarrierConfigForSubId(subInfoHelper.getSubId());
         } else {
             b = PhoneGlobals.getInstance().getCarrierConfig();
         }
-        if (b != null && b.getBoolean(CarrierConfigManager.KEY_CALL_BARRING_VISIBILITY_BOOL)) {
-            callBarringPref.setIntent(subInfoHelper.getIntent(GsmUmtsCallBarringOptions.class));
-        } else {
-            prefScreen.removePreference(callBarringPref);
+
+        Preference callForwardingPref = prefScreen.findPreference(CALL_FORWARDING_KEY);
+        if (callForwardingPref != null) {
+            if (b != null && b.getBoolean(
+                    CarrierConfigManager.KEY_CALL_FORWARDING_VISIBILITY_BOOL)) {
+                callForwardingPref.setIntent(
+                        subInfoHelper.getIntent(GsmUmtsCallForwardOptions.class));
+            } else {
+                prefScreen.removePreference(callForwardingPref);
+            }
+        }
+
+        Preference additionalGsmSettingsPref =
+                prefScreen.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
+        if (additionalGsmSettingsPref != null) {
+            if (b != null && (b.getBoolean(
+                    CarrierConfigManager.KEY_ADDITIONAL_SETTINGS_CALL_WAITING_VISIBILITY_BOOL)
+                    || b.getBoolean(
+                    CarrierConfigManager.KEY_ADDITIONAL_SETTINGS_CALLER_ID_VISIBILITY_BOOL))) {
+                additionalGsmSettingsPref.setIntent(
+                        subInfoHelper.getIntent(GsmUmtsAdditionalCallOptions.class));
+            } else {
+                prefScreen.removePreference(additionalGsmSettingsPref);
+            }
+        }
+
+        Preference callBarringPref = prefScreen.findPreference(CALL_BARRING_KEY);
+        if (callBarringPref != null) {
+            if (b != null && b.getBoolean(CarrierConfigManager.KEY_CALL_BARRING_VISIBILITY_BOOL)) {
+                callBarringPref.setIntent(subInfoHelper.getIntent(GsmUmtsCallBarringOptions.class));
+            } else {
+                prefScreen.removePreference(callBarringPref);
+            }
         }
     }
 }
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 3f658bb..c163765 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -1724,6 +1724,10 @@
         // registered cell info, so return a NULL country instead.
         final long identity = Binder.clearCallingIdentity();
         try {
+            if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
+                // Get default phone in this case.
+                phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
+            }
             final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
             // Todo: fix this when we can get the actual cellular network info when the device
             // is on IWLAN.
@@ -2766,15 +2770,6 @@
     }
 
     /**
-     * @return true if the IMS resolver is busy resolving a binding and should not be considered
-     * available, false if the IMS resolver is idle.
-     */
-    public boolean isResolvingImsBinding() {
-        enforceModifyPermission();
-        return PhoneFactory.getImsResolver().isResolvingBinding();
-    }
-
-    /**
      * Sets the ImsService Package Name that Telephony will bind to.
      *
      * @param slotId the slot ID that the ImsService should bind for.
@@ -3704,7 +3699,7 @@
         synchronized (mLastModemActivityInfo) {
             ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO,
                     null);
-            if (info != null) {
+            if (isModemActivityInfoValid(info)) {
                 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
                 for (int i = 0; i < mergedTxTimeMs.length; i++) {
                     mergedTxTimeMs[i] =
@@ -3733,6 +3728,25 @@
         result.send(0, bundle);
     }
 
+    // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
+    // less than total activity duration.
+    private boolean isModemActivityInfoValid(ModemActivityInfo info) {
+        if (info == null) {
+            return false;
+        }
+        int activityDurationMs =
+            (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
+        int totalTxTimeMs = 0;
+        for (int i = 0; i < info.getTxTimeMillis().length; i++) {
+            totalTxTimeMs += info.getTxTimeMillis()[i];
+        }
+        return (info.isValid()
+            && (info.getSleepTimeMillis() <= activityDurationMs)
+            && (info.getIdleTimeMillis() <= activityDurationMs)
+            && (info.getRxTimeMillis() <= activityDurationMs)
+            && (totalTxTimeMs <= activityDurationMs));
+    }
+
     /**
      * {@hide}
      * Returns the service state information on specified subscription.
diff --git a/src/com/android/services/telephony/DisconnectCauseUtil.java b/src/com/android/services/telephony/DisconnectCauseUtil.java
index 7d6a86f..62aa3a1 100644
--- a/src/com/android/services/telephony/DisconnectCauseUtil.java
+++ b/src/com/android/services/telephony/DisconnectCauseUtil.java
@@ -187,6 +187,7 @@
             case android.telephony.DisconnectCause.IMEI_NOT_ACCEPTED:
             case android.telephony.DisconnectCause.WIFI_LOST:
             case android.telephony.DisconnectCause.IMS_ACCESS_BLOCKED:
+            case android.telephony.DisconnectCause.IMS_SIP_ALTERNATE_EMERGENCY_CALL:
                 return DisconnectCause.ERROR;
 
             case android.telephony.DisconnectCause.DIALED_MMI:
@@ -321,6 +322,10 @@
                 resourceId = R.string.callFailed_data_limit_reached;
                 break;
 
+            case android.telephony.DisconnectCause.IMS_SIP_ALTERNATE_EMERGENCY_CALL:
+                resourceId = R.string.incall_error_power_off;
+                break;
+
             default:
                 break;
         }
@@ -685,6 +690,10 @@
                 resourceId = R.string.callFailed_wifi_lost;
                 break;
 
+            case android.telephony.DisconnectCause.IMS_SIP_ALTERNATE_EMERGENCY_CALL:
+                resourceId = R.string.incall_error_power_off;
+                break;
+
             default:
                 break;
         }