Merge "CFI for individual subscriptions"
diff --git a/res/xml/network_setting_fragment.xml b/res/xml/network_setting_fragment.xml
index 0ea42bd..42a8e47 100644
--- a/res/xml/network_setting_fragment.xml
+++ b/res/xml/network_setting_fragment.xml
@@ -50,6 +50,28 @@
         android:persistent="false"
         android:summary="@string/enhanced_4g_lte_mode_summary"/>
 
+    <PreferenceCategory
+        android:key="calling"
+        android:title="@string/call_category">
+
+        <PreferenceScreen
+            android:key="wifi_calling_key"
+            android:title="@string/wifi_calling_settings_title">
+
+            <intent android:action="android.intent.action.MAIN"
+                    android:targetPackage="com.android.settings"
+                    android:targetClass="com.android.settings.Settings$WifiCallingSettingsActivity"/>
+
+        </PreferenceScreen>
+
+        <SwitchPreference
+            android:key="video_calling_key"
+            android:title="@string/video_calling_settings_title"
+            android:persistent="true"/>
+
+    </PreferenceCategory>
+
+
     <PreferenceScreen
         android:key="carrier_settings_euicc_key"
         android:title="@string/carrier_settings_euicc" />
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index febc9f5..03865fe 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -27,6 +27,7 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.os.AsyncResult;
 import android.os.Bundle;
@@ -37,10 +38,13 @@
 import android.os.UserManager;
 import android.preference.ListPreference;
 import android.preference.Preference;
+import android.preference.PreferenceCategory;
 import android.preference.PreferenceFragment;
 import android.preference.PreferenceScreen;
 import android.preference.SwitchPreference;
 import android.provider.Settings;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
 import android.telephony.CarrierConfigManager;
 import android.telephony.PhoneStateListener;
 import android.telephony.ServiceState;
@@ -56,11 +60,13 @@
 import android.view.ViewGroup;
 import android.widget.TabHost;
 
+import com.android.ims.ImsConfig;
 import com.android.ims.ImsManager;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.TelephonyIntents;
+import com.android.phone.settings.PhoneAccountSettingsFragment;
 import com.android.settingslib.RestrictedLockUtils;
 
 import java.util.ArrayList;
@@ -176,6 +182,9 @@
         private static final String BUTTON_CDMA_SYSTEM_SELECT_KEY = "cdma_system_select_key";
         private static final String BUTTON_CARRIER_SETTINGS_EUICC_KEY =
                 "carrier_settings_euicc_key";
+        private static final String BUTTON_WIFI_CALLING_KEY = "wifi_calling_key";
+        private static final String BUTTON_VIDEO_CALLING_KEY = "video_calling_key";
+        private static final String CATEGORY_CALLING_KEY = "calling";
 
         private final BroadcastReceiver mPhoneChangeReceiver = new PhoneChangeReceiver();
 
@@ -196,6 +205,9 @@
         private SwitchPreference mButton4glte;
         private Preference mLteDataServicePref;
         private Preference mEuiccSettingsPref;
+        private PreferenceCategory mCallingCategory;
+        private Preference mWiFiCallingPref;
+        private SwitchPreference mVideoCallingPref;
 
         private static final String iface = "rmnet0"; //TODO: this will go away
         private List<SubscriptionInfo> mActiveSubInfos;
@@ -315,6 +327,8 @@
                 Intent intent = new Intent(EuiccManager.ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS);
                 startActivity(intent);
                 return true;
+            } else if (preference == mWiFiCallingPref || preference == mVideoCallingPref) {
+                return false;
             } else {
                 // if the button is anything but the simple toggle preference,
                 // we'll need to disable all preferences to reject all click
@@ -514,6 +528,10 @@
             mButton4glte = (SwitchPreference)findPreference(BUTTON_4G_LTE_KEY);
             mButton4glte.setOnPreferenceChangeListener(this);
 
+            mCallingCategory = (PreferenceCategory) findPreference(CATEGORY_CALLING_KEY);
+            mWiFiCallingPref = findPreference(BUTTON_WIFI_CALLING_KEY);
+            mVideoCallingPref = (SwitchPreference) findPreference(BUTTON_VIDEO_CALLING_KEY);
+
             try {
                 Context con = activity.createPackageContext("com.android.systemui", 0);
                 int id = con.getResources().getIdentifier("config_show4GForLTE",
@@ -622,6 +640,9 @@
                     && ImsManager.isNonTtyOrTtyOnVolteEnabled(activity);
             mButton4glte.setChecked(enh4glteMode);
 
+            // Video calling and WiFi calling state might have changed.
+            updateCallingCategory();
+
             mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
 
             Log.i(LOG_TAG, "onResume:-");
@@ -826,6 +847,8 @@
                 }
             }
 
+            updateCallingCategory();
+
             ActionBar actionBar = activity.getActionBar();
             if (actionBar != null) {
                 // android.R.id.home will be triggered in onOptionsItemSelected()
@@ -913,6 +936,10 @@
             if (ps != null) {
                 ps.setEnabled(hasActiveSubscriptions);
             }
+            ps = findPreference(CATEGORY_CALLING_KEY);
+            if (ps != null) {
+                ps.setEnabled(hasActiveSubscriptions);
+            }
         }
 
         @Override
@@ -1066,6 +1093,17 @@
                     mPhone.setDataRoamingEnabled(false);
                 }
                 return true;
+            } else if (preference == mVideoCallingPref) {
+                // If mButton4glte is not checked, mVideoCallingPref should be disabled.
+                // So it only makes sense to call phoneMgr.enableVideoCalling if it's checked.
+                if (mButton4glte.isChecked()) {
+                    PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
+                    return true;
+                } else {
+                    loge("mVideoCallingPref should be disabled if mButton4glte is not checked.");
+                    mVideoCallingPref.setEnabled(false);
+                    return false;
+                }
             }
 
             updateBody();
@@ -1400,6 +1438,117 @@
             }
         }
 
+        private void updateWiFiCallState() {
+            if (mWiFiCallingPref == null || mCallingCategory == null) {
+                return;
+            }
+
+            boolean removePref = false;
+            final PhoneAccountHandle simCallManager =
+                    TelecomManager.from(getContext()).getSimCallManager();
+
+            if (simCallManager != null) {
+                Intent intent = PhoneAccountSettingsFragment.buildPhoneAccountConfigureIntent(
+                        getContext(), simCallManager);
+                if (intent != null) {
+                    PackageManager pm = mPhone.getContext().getPackageManager();
+                    List<ResolveInfo> resolutions = pm.queryIntentActivities(intent, 0);
+                    if (!resolutions.isEmpty()) {
+                        mWiFiCallingPref.setTitle(resolutions.get(0).loadLabel(pm));
+                        mWiFiCallingPref.setSummary(null);
+                        mWiFiCallingPref.setIntent(intent);
+                    } else {
+                        removePref = true;
+                    }
+                } else {
+                    removePref = true;
+                }
+            } else if (!ImsManager.isWfcEnabledByPlatform(mPhone.getContext())
+                    || !ImsManager.isWfcProvisionedOnDevice(mPhone.getContext())) {
+                removePref = true;
+            } else {
+                int resId = com.android.internal.R.string.wifi_calling_off_summary;
+                if (ImsManager.isWfcEnabledByUser(mPhone.getContext())) {
+                    boolean isRoaming = mButtonDataRoam.isChecked();
+                    int wfcMode = ImsManager.getWfcMode(mPhone.getContext(), isRoaming);
+                    switch (wfcMode) {
+                        case ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY:
+                            resId = com.android.internal.R.string.wfc_mode_wifi_only_summary;
+                            break;
+                        case ImsConfig.WfcModeFeatureValueConstants.CELLULAR_PREFERRED:
+                            resId = com.android.internal.R.string
+                                    .wfc_mode_cellular_preferred_summary;
+                            break;
+                        case ImsConfig.WfcModeFeatureValueConstants.WIFI_PREFERRED:
+                            resId = com.android.internal.R.string.wfc_mode_wifi_preferred_summary;
+                            break;
+                        default:
+                            if (DBG) log("Unexpected WFC mode value: " + wfcMode);
+                    }
+                }
+                mWiFiCallingPref.setSummary(resId);
+            }
+
+            if (removePref) {
+                mCallingCategory.removePreference(mWiFiCallingPref);
+            } else {
+                mCallingCategory.addPreference(mWiFiCallingPref);
+            }
+        }
+
+        private void updateVideoCallState() {
+            if (mVideoCallingPref == null || mCallingCategory == null) {
+                return;
+            }
+
+            PersistableBundle carrierConfig = PhoneGlobals.getInstance()
+                    .getCarrierConfigForSubId(mPhone.getSubId());
+
+            boolean removePref = false;
+
+            if (ImsManager.isVtEnabledByPlatform(mPhone.getContext())
+                    && ImsManager.isVtProvisionedOnDevice(mPhone.getContext())
+                    && (carrierConfig.getBoolean(
+                            CarrierConfigManager.KEY_IGNORE_DATA_ENABLED_CHANGED_FOR_VIDEO_CALLS)
+                            || mPhone.mDcTracker.isDataEnabled())) {
+                boolean enhanced4gLteEnabled = mButton4glte.isChecked();
+                mVideoCallingPref.setEnabled(enhanced4gLteEnabled);
+                boolean currentValue = enhanced4gLteEnabled
+                        ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled(
+                        getContext().getOpPackageName()) : false;
+                mVideoCallingPref.setChecked(currentValue);
+                if (enhanced4gLteEnabled) {
+                    mVideoCallingPref.setOnPreferenceChangeListener(this);
+                }
+            } else {
+                removePref = true;
+            }
+
+            if (removePref) {
+                mCallingCategory.removePreference(mVideoCallingPref);
+            } else {
+                mCallingCategory.addPreference(mVideoCallingPref);
+            }
+        }
+
+        private void updateCallingCategory() {
+            if (mCallingCategory == null) {
+                return;
+            }
+
+            updateWiFiCallState();
+            updateVideoCallState();
+
+            // If all items in calling category is removed, we remove it from
+            // the screen. Otherwise we'll see title of the category but nothing
+            // is in there.
+            if (mCallingCategory.getPreferenceCount() == 0) {
+                getPreferenceScreen().removePreference(mCallingCategory);
+            } else {
+                getPreferenceScreen().addPreference(mCallingCategory);
+            }
+        }
+
         private static void log(String msg) {
             Log.d(LOG_TAG, msg);
         }