Merge changes I773aeabd,I6a9d7044

* changes:
  Refactor DataServicePreference
  Refactor 4gForLTE preference
diff --git a/res/xml/network_setting_fragment.xml b/res/xml/network_setting_fragment.xml
index 044651c..e54a90d 100644
--- a/res/xml/network_setting_fragment.xml
+++ b/res/xml/network_setting_fragment.xml
@@ -20,10 +20,11 @@
     android:title="@string/network_settings_title"
     settings:initialExpandedChildrenCount="4">
 
-    <PreferenceScreen
+    <Preference
         android:key="cdma_lte_data_service_key"
-        android:title="@string/cdma_lte_data_service">
-    </PreferenceScreen>
+        android:title="@string/cdma_lte_data_service"
+        settings:controller="com.android.settings.network.telephony.DataServiceSetupPreferenceController">
+    </Preference>
 
     <SwitchPreference
         android:key="mobile_data_enable"
@@ -49,7 +50,8 @@
         android:key="enhanced_4g_lte"
         android:title="@string/enhanced_4g_lte_mode_title"
         android:persistent="false"
-        android:summary="@string/enhanced_4g_lte_mode_summary"/>
+        android:summary="@string/enhanced_4g_lte_mode_summary"
+        settings:controller="com.android.settings.network.telephony.Enhanced4gLtePreferenceController"/>
 
     <ListPreference
         android:key="preferred_network_mode_key"
diff --git a/src/com/android/settings/network/telephony/DataServiceSetupPreferenceController.java b/src/com/android/settings/network/telephony/DataServiceSetupPreferenceController.java
new file mode 100644
index 0000000..129ceb1
--- /dev/null
+++ b/src/com/android/settings/network/telephony/DataServiceSetupPreferenceController.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network.telephony;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.PersistableBundle;
+import android.provider.Settings;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.preference.Preference;
+
+import com.android.internal.telephony.PhoneConstants;
+import com.android.settings.core.BasePreferenceController;
+
+/**
+ * Preference controller for "Data service setup"
+ */
+public class DataServiceSetupPreferenceController extends BasePreferenceController {
+
+    private CarrierConfigManager mCarrierConfigManager;
+    private TelephonyManager mTelephonyManager;
+    private PersistableBundle mCarrierConfig;
+    private String mSetupUrl;
+    private int mSubId;
+
+    public DataServiceSetupPreferenceController(Context context, String key) {
+        super(context, key);
+        mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
+        mTelephonyManager = context.getSystemService(TelephonyManager.class);
+        mSetupUrl = Settings.Global.getString(mContext.getContentResolver(),
+                Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL);
+        mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        final boolean isLteOnCdma = mTelephonyManager.getLteOnCdmaMode()
+                == PhoneConstants.LTE_ON_CDMA_TRUE;
+        return mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
+                && mCarrierConfig != null
+                && !mCarrierConfig.getBoolean(
+                CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)
+                && isLteOnCdma && !TextUtils.isEmpty(mSetupUrl)
+                ? AVAILABLE
+                : CONDITIONALLY_UNAVAILABLE;
+    }
+
+    public void init(int subId) {
+        mSubId = subId;
+        mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId);
+        mCarrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
+    }
+
+    @Override
+    public boolean handlePreferenceTreeClick(Preference preference) {
+        if (getPreferenceKey().equals(preference.getKey())) {
+            if (!TextUtils.isEmpty(mSetupUrl)) {
+                String imsi = mTelephonyManager.getSubscriberId();
+                if (imsi == null) {
+                    imsi = "";
+                }
+                final String url = TextUtils.expandTemplate(mSetupUrl, imsi).toString();
+                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
+                mContext.startActivity(intent);
+            }
+            return true;
+        }
+
+        return false;
+    }
+}
diff --git a/src/com/android/settings/network/telephony/Enhanced4gLtePreferenceController.java b/src/com/android/settings/network/telephony/Enhanced4gLtePreferenceController.java
new file mode 100644
index 0000000..d6773ed
--- /dev/null
+++ b/src/com/android/settings/network/telephony/Enhanced4gLtePreferenceController.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network.telephony;
+
+import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+import android.telephony.PhoneStateListener;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
+
+import com.android.ims.ImsManager;
+import com.android.settings.R;
+import com.android.settings.core.TogglePreferenceController;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnStart;
+import com.android.settingslib.core.lifecycle.events.OnStop;
+
+/**
+ * Preference controller for "Enhanced 4G LTE"
+ */
+public class Enhanced4gLtePreferenceController extends TogglePreferenceController implements
+        LifecycleObserver, OnStart, OnStop {
+
+    private Preference mPreference;
+    private TelephonyManager mTelephonyManager;
+    private CarrierConfigManager mCarrierConfigManager;
+    private PersistableBundle mCarrierConfig;
+    @VisibleForTesting
+    ImsManager mImsManager;
+    private PhoneCallStateListener mPhoneStateListener;
+    private int mSubId;
+
+    public Enhanced4gLtePreferenceController(Context context, String key) {
+        super(context, key);
+        mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
+        mPhoneStateListener = new PhoneCallStateListener();
+        mSubId =  SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        final boolean isVisible = mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
+                && mImsManager != null && mCarrierConfig != null
+                && mImsManager.isVolteEnabledByPlatform()
+                && mImsManager.isVolteProvisionedOnDevice()
+                && MobileNetworkUtils.isImsServiceStateReady(mImsManager)
+                && !mCarrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_ENHANCED_4G_LTE_BOOL);
+        return isVisible
+                ? (is4gLtePrefEnabled() ? AVAILABLE : AVAILABLE_UNSEARCHABLE)
+                : CONDITIONALLY_UNAVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mPreference = screen.findPreference(getPreferenceKey());
+    }
+
+    @Override
+    public void onStart() {
+        mPhoneStateListener.register(mSubId);
+    }
+
+    @Override
+    public void onStop() {
+        mPhoneStateListener.unregister();
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        super.updateState(preference);
+        final SwitchPreference switchPreference = (SwitchPreference) preference;
+        final boolean useVariant4glteTitle = mCarrierConfig.getInt(
+                CarrierConfigManager.KEY_ENHANCED_4G_LTE_TITLE_VARIANT_INT) != 0;
+        int enhanced4glteModeTitleId = useVariant4glteTitle ?
+                R.string.enhanced_4g_lte_mode_title_variant :
+                R.string.enhanced_4g_lte_mode_title;
+        switchPreference.setTitle(enhanced4glteModeTitleId);
+        switchPreference.setEnabled(is4gLtePrefEnabled());
+        switchPreference.setChecked(mImsManager.isEnhanced4gLteModeSettingEnabledByUser()
+                && mImsManager.isNonTtyOrTtyOnVolteEnabled());
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        mImsManager.setEnhanced4gLteModeSetting(isChecked);
+        return true;
+    }
+
+    @Override
+    public boolean isChecked() {
+        return mImsManager.isEnhanced4gLteModeSettingEnabledByUser();
+    }
+
+    public void init(int subId) {
+        mSubId = subId;
+        mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId);
+        mCarrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
+        if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+            mImsManager = ImsManager.getInstance(mContext, SubscriptionManager.getPhoneId(mSubId));
+        }
+    }
+
+    private boolean is4gLtePrefEnabled() {
+        return mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
+                && mTelephonyManager.getCallState(mSubId) == TelephonyManager.CALL_STATE_IDLE
+                && mImsManager != null
+                && mImsManager.isNonTtyOrTtyOnVolteEnabled()
+                && mCarrierConfig.getBoolean(
+                CarrierConfigManager.KEY_EDITABLE_ENHANCED_4G_LTE_BOOL);
+    }
+
+    private class PhoneCallStateListener extends PhoneStateListener {
+        @Override
+        public void onCallStateChanged(int state, String incomingNumber) {
+            updateState(mPreference);
+        }
+
+        public void register(int subId) {
+            mSubId = subId;
+            mTelephonyManager.listen(this, PhoneStateListener.LISTEN_CALL_STATE);
+        }
+
+        public void unregister() {
+            mTelephonyManager.listen(this, PhoneStateListener.LISTEN_NONE);
+        }
+    }
+}
diff --git a/src/com/android/settings/network/telephony/MobileNetworkFragment.java b/src/com/android/settings/network/telephony/MobileNetworkFragment.java
index 57f6d54..cb54e5b 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkFragment.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkFragment.java
@@ -122,7 +122,6 @@
 
     //UI objects
     private SwitchPreference mButton4glte;
-    private Preference mLteDataServicePref;
     private Preference mEuiccSettingsPref;
     private PreferenceCategory mCallingCategory;
     private Preference mWiFiCallingPref;
@@ -160,7 +159,6 @@
         public void onCallStateChanged(int state, String incomingNumber) {
             if (DBG) log("PhoneStateListener.onCallStateChanged: state=" + state);
 
-            updateEnhanced4gLteState();
             updateWiFiCallState();
             updateVideoCallState();
             updatePreferredNetworkType();
@@ -224,23 +222,6 @@
                         REQUEST_CODE_EXIT_ECM);
             }
             return true;
-        } else if (preference == mLteDataServicePref) {
-            String tmpl = android.provider.Settings.Global.getString(
-                    getContext().getContentResolver(),
-                    android.provider.Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL);
-            if (!TextUtils.isEmpty(tmpl)) {
-                String imsi = mTelephonyManager.getSubscriberId();
-                if (imsi == null) {
-                    imsi = "";
-                }
-                final String url = TextUtils.isEmpty(tmpl) ? null
-                        : TextUtils.expandTemplate(tmpl, imsi).toString();
-                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
-                startActivity(intent);
-            } else {
-                android.util.Log.e(LOG_TAG, "Missing SETUP_PREPAID_DATA_SERVICE_URL");
-            }
-            return true;
         } else if (preference == mEuiccSettingsPref) {
             Intent intent = new Intent(EuiccManager.ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS);
             startActivity(intent);
@@ -296,6 +277,8 @@
         use(DataUsagePreferenceController.class).init(mSubId);
         use(PreferredNetworkModePreferenceController.class).init(mSubId);
         use(EnabledNetworkModePreferenceController.class).init(mSubId);
+        use(Enhanced4gLtePreferenceController.class).init(mSubId);
+        use(DataServiceSetupPreferenceController.class).init(mSubId);
 
         mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class);
         mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId);
@@ -321,7 +304,6 @@
         mCarrierConfigManager = new CarrierConfigManager(getContext());
 
         mButton4glte = (SwitchPreference)findPreference(BUTTON_4G_LTE_KEY);
-        mButton4glte.setOnPreferenceChangeListener(this);
 
         mCallingCategory = (PreferenceCategory) findPreference(CATEGORY_CALLING_KEY);
         mWiFiCallingPref = findPreference(BUTTON_WIFI_CALLING_KEY);
@@ -340,8 +322,6 @@
         //get UI object references
         PreferenceScreen prefSet = getPreferenceScreen();
 
-        mLteDataServicePref = prefSet.findPreference(BUTTON_CDMA_LTE_DATA_SERVICE_KEY);
-
         mEuiccSettingsPref = prefSet.findPreference(BUTTON_CARRIER_SETTINGS_EUICC_KEY);
         mEuiccSettingsPref.setOnPreferenceChangeListener(this);
 
@@ -410,9 +390,6 @@
 
         mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
 
-        // NOTE: Buttons will be enabled/disabled in mPhoneStateListener
-        updateEnhanced4gLteState();
-
         // Video calling and WiFi calling state might have changed.
         updateCallingCategory();
 
@@ -490,7 +467,6 @@
         if (DBG) {
             log("updateBody: isLteOnCdma=" + isLteOnCdma + " phoneSubId=" + phoneSubId);
         }
-        prefSet.addPreference(mButton4glte);
 
         if (MobileNetworkUtils.showEuiccSettings(getContext())) {
             prefSet.addPreference(mEuiccSettingsPref);
@@ -512,7 +488,6 @@
                 && carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_CDMA_CHOICES_BOOL);
         if (carrierConfig.getBoolean(
                 CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)) {
-            prefSet.removePreference(mLteDataServicePref);
         } else if (carrierConfig.getBoolean(CarrierConfigManager
                 .KEY_HIDE_PREFERRED_NETWORK_TYPE_BOOL)
                 && !mTelephonyManager.getServiceState().getRoaming()
@@ -531,8 +506,7 @@
             // in case it is currently something else. That is possible if user
             // changed the setting while roaming and is now back to home network.
             settingsNetworkMode = preferredNetworkMode;
-        } else if (carrierConfig.getBoolean(
-                CarrierConfigManager.KEY_WORLD_PHONE_BOOL) == true) {
+        } else if (carrierConfig.getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL)) {
             // set the listener for the mButtonPreferredNetworkMode list preference so we can issue
             // change Preferred Network Mode.
 
@@ -543,13 +517,7 @@
         final boolean missingDataServiceUrl = TextUtils.isEmpty(
                 android.provider.Settings.Global.getString(activity.getContentResolver(),
                         android.provider.Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL));
-        if (!isLteOnCdma || missingDataServiceUrl) {
-            prefSet.removePreference(mLteDataServicePref);
-        } else {
-            android.util.Log.d(LOG_TAG, "keep ltePref");
-        }
 
-        updateEnhanced4gLteState();
         updatePreferredNetworkType();
         updateCallingCategory();
 
@@ -565,25 +533,8 @@
             }
         }
 
-        /**
-         * Enable/disable depending upon if there are any active subscriptions.
-         *
-         * I've decided to put this enable/disable code at the bottom as the
-         * code above works even when there are no active subscriptions, thus
-         * putting it afterwards is a smaller change. This can be refined later,
-         * but you do need to remember that this all needs to work when subscriptions
-         * change dynamically such as when hot swapping sims.
-         */
-        boolean useVariant4glteTitle = carrierConfig.getBoolean(
-                CarrierConfigManager.KEY_ENHANCED_4G_LTE_TITLE_VARIANT_BOOL);
-        int enhanced4glteModeTitleId = useVariant4glteTitle ?
-                R.string.enhanced_4g_lte_mode_title_variant :
-                R.string.enhanced_4g_lte_mode_title;
-
         mOnlyAutoSelectInHomeNW = carrierConfig.getBoolean(
                 CarrierConfigManager.KEY_ONLY_AUTO_SELECT_IN_HOME_NETWORK_BOOL);
-        mButton4glte.setTitle(enhanced4glteModeTitleId);
-        mLteDataServicePref.setEnabled(hasActiveSubscriptions);
         Preference ps;
         ps = findPreference(BUTTON_CELL_BROADCAST_SETTINGS);
         if (ps != null) {
@@ -651,13 +602,7 @@
      */
     public boolean onPreferenceChange(Preference preference, Object objValue) {
         sendMetricsEventPreferenceChanged(getPreferenceScreen(), preference, objValue);
-
-        final int phoneSubId = mSubId;
-        if (preference == mButton4glte) {
-            boolean enhanced4gMode = !mButton4glte.isChecked();
-            mButton4glte.setChecked(enhanced4gMode);
-            mImsMgr.setEnhanced4gLteModeSetting(mButton4glte.isChecked());
-        } else if (preference == mVideoCallingPref) {
+        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()) {
@@ -761,29 +706,6 @@
                 == TelephonyManager.CALL_STATE_IDLE && hasActiveSubscriptions());
     }
 
-    private void updateEnhanced4gLteState() {
-        if (mButton4glte == null) {
-            return;
-        }
-
-        PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
-
-        if ((mImsMgr == null
-                || !mImsMgr.isVolteEnabledByPlatform()
-                || !mImsMgr.isVolteProvisionedOnDevice()
-                || !MobileNetworkUtils.isImsServiceStateReady(mImsMgr)
-                || carrierConfig.getBoolean(
-                CarrierConfigManager.KEY_HIDE_ENHANCED_4G_LTE_BOOL))) {
-            getPreferenceScreen().removePreference(mButton4glte);
-        } else {
-            mButton4glte.setEnabled(is4gLtePrefEnabled(carrierConfig)
-                    && hasActiveSubscriptions());
-            boolean enh4glteMode = mImsMgr.isEnhanced4gLteModeSettingEnabledByUser()
-                    && mImsMgr.isNonTtyOrTtyOnVolteEnabled();
-            mButton4glte.setChecked(enh4glteMode);
-        }
-    }
-
     private void updateVideoCallState() {
         if (mVideoCallingPref == null || mCallingCategory == null) {
             return;
@@ -958,8 +880,7 @@
         // For ListPreferences, we log it here without a value, only indicating it's clicked to
         // open the list dialog. When a value is chosen, another MetricsEvent is logged with
         // new value in onPreferenceChange.
-        if (preference == mLteDataServicePref
-                || preference == mEuiccSettingsPref
+        if (preference == mEuiccSettingsPref
                 || preference == mWiFiCallingPref
                 || preference == preferenceScreen.findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY)
                 || preference == preferenceScreen.findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY)
@@ -978,7 +899,7 @@
         }
 
         // MetricsEvent logging with new value, for SwitchPreferences and ListPreferences.
-        if (preference == mButton4glte || preference == mVideoCallingPref) {
+        if (preference == mVideoCallingPref) {
             MetricsLogger.action(getContext(), category, (Boolean) newValue);
         } else if (preference == preferenceScreen
                 .findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY)
@@ -994,10 +915,6 @@
 
         if (preference == null) {
             return MetricsProto.MetricsEvent.VIEW_UNKNOWN;
-        } else if (preference == mLteDataServicePref) {
-            return MetricsProto.MetricsEvent.ACTION_MOBILE_NETWORK_SET_UP_DATA_SERVICE;
-        } else if (preference == mButton4glte) {
-            return MetricsProto.MetricsEvent.ACTION_MOBILE_ENHANCED_4G_LTE_MODE_TOGGLE;
         } else if (preference == mEuiccSettingsPref) {
             return MetricsProto.MetricsEvent.ACTION_MOBILE_NETWORK_EUICC_SETTING;
         } else if (preference == mWiFiCallingPref) {
diff --git a/tests/robotests/src/com/android/settings/network/telephony/DataServiceSetupPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/DataServiceSetupPreferenceControllerTest.java
new file mode 100644
index 0000000..644a5bc
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/network/telephony/DataServiceSetupPreferenceControllerTest.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network.telephony;
+
+import static com.android.settings.core.BasePreferenceController.AVAILABLE;
+import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.PersistableBundle;
+import android.provider.Settings;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+
+import androidx.preference.Preference;
+
+import com.android.internal.telephony.PhoneConstants;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settingslib.RestrictedPreference;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+public class DataServiceSetupPreferenceControllerTest {
+    private static final int SUB_ID = 2;
+
+    private static final String SETUP_URL = "url://tmp_url:^1";
+
+    @Mock
+    private TelephonyManager mTelephonyManager;
+    @Mock
+    private TelephonyManager mInvalidTelephonyManager;
+    @Mock
+    private CarrierConfigManager mCarrierConfigManager;
+
+    private PersistableBundle mCarrierConfig;
+    private DataServiceSetupPreferenceController mController;
+    private Preference mPreference;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mContext = spy(RuntimeEnvironment.application);
+        doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
+        doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
+        doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
+        Settings.Global.putString(mContext.getContentResolver(),
+                Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL, SETUP_URL);
+
+        mCarrierConfig = new PersistableBundle();
+        doReturn(mCarrierConfig).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
+
+        mPreference = new RestrictedPreference(mContext);
+        mController = new DataServiceSetupPreferenceController(mContext, "data_service_setup");
+        mController.init(SUB_ID);
+        mPreference.setKey(mController.getPreferenceKey());
+    }
+
+    @Test
+    public void getAvailabilityStatus_allConfigOn_returnAvailable() {
+        doReturn(PhoneConstants.LTE_ON_CDMA_TRUE).when(mTelephonyManager).getLteOnCdmaMode();
+        mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
+                false);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_missUrl_returnUnavailable() {
+        Settings.Global.putString(mContext.getContentResolver(),
+                Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL, "");
+        doReturn(PhoneConstants.LTE_ON_CDMA_TRUE).when(mTelephonyManager).getLteOnCdmaMode();
+        mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
+                false);
+
+        mController = new DataServiceSetupPreferenceController(mContext, "data_service_setup");
+        mController.init(SUB_ID);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_notCdma_returnUnavailable() {
+        doReturn(PhoneConstants.LTE_ON_CDMA_FALSE).when(mTelephonyManager).getLteOnCdmaMode();
+        mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
+                false);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void handlePreferenceTreeClick_startActivity() {
+        ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
+
+        mController.handlePreferenceTreeClick(mPreference);
+
+        verify(mContext).startActivity(captor.capture());
+
+        final Intent intent = captor.getValue();
+        assertThat(intent.getAction()).isEqualTo(Intent.ACTION_VIEW);
+        assertThat(intent.getData()).isEqualTo(
+                Uri.parse(TextUtils.expandTemplate(SETUP_URL, "").toString()));
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/network/telephony/Enhanced4gLtePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/Enhanced4gLtePreferenceControllerTest.java
new file mode 100644
index 0000000..dee4c5e
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/network/telephony/Enhanced4gLtePreferenceControllerTest.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network.telephony;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import androidx.preference.SwitchPreference;
+
+import com.android.ims.ImsManager;
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settingslib.RestrictedSwitchPreference;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+public class Enhanced4gLtePreferenceControllerTest {
+    private static final int SUB_ID = 2;
+
+    @Mock
+    private TelephonyManager mTelephonyManager;
+    @Mock
+    private TelephonyManager mInvalidTelephonyManager;
+    @Mock
+    private SubscriptionManager mSubscriptionManager;
+    @Mock
+    private CarrierConfigManager mCarrierConfigManager;
+    @Mock
+    private ImsManager mImsManager;
+
+    private Enhanced4gLtePreferenceController mController;
+    private SwitchPreference mPreference;
+    private PersistableBundle mCarrierConfig;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mContext = spy(RuntimeEnvironment.application);
+        doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
+        doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
+        doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
+        doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
+        doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+
+        mCarrierConfig = new PersistableBundle();
+        doReturn(mCarrierConfig).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
+
+        mPreference = new RestrictedSwitchPreference(mContext);
+        mController = new Enhanced4gLtePreferenceController(mContext, "roaming");
+        mController.mImsManager = mImsManager;
+        mController.init(SUB_ID);
+        mPreference.setKey(mController.getPreferenceKey());
+    }
+
+    @Test
+    public void getAvailabilityStatus_invalidSubId_returnUnavailable() {
+        mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(
+                BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_volteDisabled_returnUnavailable() {
+        doReturn(false).when(mImsManager).isVolteEnabledByPlatform();
+        doReturn(true).when(mImsManager).isVolteProvisionedOnDevice();
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(
+                BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void updateState_variant4gLte_useVariantTitle() {
+        mCarrierConfig.putInt(CarrierConfigManager.KEY_ENHANCED_4G_LTE_TITLE_VARIANT_INT, 1);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.getTitle()).isEqualTo(
+                mContext.getString(R.string.enhanced_4g_lte_mode_title_variant));
+    }
+
+    @Test
+    public void updateState_configEnabled_prefEnabled() {
+        mPreference.setEnabled(false);
+        mCarrierConfig.putInt(CarrierConfigManager.KEY_ENHANCED_4G_LTE_TITLE_VARIANT_INT, 1);
+        doReturn(TelephonyManager.CALL_STATE_IDLE).when(mTelephonyManager).getCallState(SUB_ID);
+        doReturn(true).when(mImsManager).isNonTtyOrTtyOnVolteEnabled();
+        mCarrierConfig.putBoolean(CarrierConfigManager.KEY_EDITABLE_ENHANCED_4G_LTE_BOOL, true);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isTrue();
+    }
+
+    @Test
+    public void updateState_configOn_prefChecked() {
+        mPreference.setChecked(false);
+        doReturn(true).when(mImsManager).isEnhanced4gLteModeSettingEnabledByUser();
+        doReturn(true).when(mImsManager).isNonTtyOrTtyOnVolteEnabled();
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isChecked()).isTrue();
+    }
+
+}