Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.phone;
     18 
     19 import com.android.ims.ImsManager;
     20 import com.android.internal.telephony.Phone;
     21 import com.android.internal.telephony.PhoneConstants;
     22 import com.android.internal.telephony.PhoneFactory;
     23 import com.android.internal.telephony.TelephonyIntents;
     24 import com.android.internal.telephony.TelephonyProperties;
     25 import com.android.settingslib.RestrictedLockUtils;
     26 
     27 import java.util.ArrayList;
     28 import java.util.Iterator;
     29 import java.util.List;
     30 
     31 import android.app.ActionBar;
     32 import android.app.AlertDialog;
     33 import android.content.BroadcastReceiver;
     34 import android.content.Context;
     35 import android.content.DialogInterface;
     36 import android.content.Intent;
     37 import android.content.IntentFilter;
     38 import android.content.pm.PackageManager.NameNotFoundException;
     39 import android.net.Uri;
     40 import android.os.AsyncResult;
     41 import android.os.Bundle;
     42 import android.os.Handler;
     43 import android.os.Message;
     44 import android.os.PersistableBundle;
     45 import android.os.SystemProperties;
     46 import android.os.UserHandle;
     47 import android.os.UserManager;
     48 import android.preference.ListPreference;
     49 import android.preference.Preference;
     50 import android.preference.PreferenceActivity;
     51 import android.preference.PreferenceScreen;
     52 import android.preference.SwitchPreference;
     53 import android.telephony.CarrierConfigManager;
     54 import android.telephony.PhoneStateListener;
     55 import android.telephony.SubscriptionInfo;
     56 import android.telephony.SubscriptionManager;
     57 import android.telephony.TelephonyManager;
     58 import android.text.TextUtils;
     59 import android.util.Log;
     60 import android.view.MenuItem;
     61 import android.view.View;
     62 import android.widget.TabHost.OnTabChangeListener;
     63 import android.widget.TabHost.TabContentFactory;
     64 import android.widget.TabHost.TabSpec;
     65 import android.widget.TabHost;
     66 
     67 /**
     68  * "Mobile network settings" screen.  This preference screen lets you
     69  * enable/disable mobile data, and control data roaming and other
     70  * network-specific mobile data features.  It's used on non-voice-capable
     71  * tablets as well as regular phone devices.
     72  *
     73  * Note that this PreferenceActivity is part of the phone app, even though
     74  * you reach it from the "Wireless & Networks" section of the main
     75  * Settings app.  It's not part of the "Call settings" hierarchy that's
     76  * available from the Phone app (see CallFeaturesSetting for that.)
     77  */
     78 public class MobileNetworkSettings extends PreferenceActivity
     79         implements DialogInterface.OnClickListener,
     80         DialogInterface.OnDismissListener, Preference.OnPreferenceChangeListener{
     81 
     82     // debug data
     83     private static final String LOG_TAG = "NetworkSettings";
     84     private static final boolean DBG = true;
     85     public static final int REQUEST_CODE_EXIT_ECM = 17;
     86 
     87     // Number of active Subscriptions to show tabs
     88     private static final int TAB_THRESHOLD = 2;
     89 
     90     //String keys for preference lookup
     91     private static final String BUTTON_PREFERED_NETWORK_MODE = "preferred_network_mode_key";
     92     private static final String BUTTON_ROAMING_KEY = "button_roaming_key";
     93     private static final String BUTTON_CDMA_LTE_DATA_SERVICE_KEY = "cdma_lte_data_service_key";
     94     private static final String BUTTON_ENABLED_NETWORKS_KEY = "enabled_networks_key";
     95     private static final String BUTTON_4G_LTE_KEY = "enhanced_4g_lte";
     96     private static final String BUTTON_CELL_BROADCAST_SETTINGS = "cell_broadcast_settings";
     97     private static final String BUTTON_APN_EXPAND_KEY = "button_apn_key";
     98     private static final String BUTTON_OPERATOR_SELECTION_EXPAND_KEY = "button_carrier_sel_key";
     99     private static final String BUTTON_CARRIER_SETTINGS_KEY = "carrier_settings_key";
    100     private static final String BUTTON_CDMA_SYSTEM_SELECT_KEY = "cdma_system_select_key";
    101 
    102     static final int preferredNetworkMode = Phone.PREFERRED_NT_MODE;
    103 
    104     //Information about logical "up" Activity
    105     private static final String UP_ACTIVITY_PACKAGE = "com.android.settings";
    106     private static final String UP_ACTIVITY_CLASS =
    107             "com.android.settings.Settings$WirelessSettingsActivity";
    108 
    109     private SubscriptionManager mSubscriptionManager;
    110 
    111     //UI objects
    112     private ListPreference mButtonPreferredNetworkMode;
    113     private ListPreference mButtonEnabledNetworks;
    114     private RestrictedSwitchPreference mButtonDataRoam;
    115     private SwitchPreference mButton4glte;
    116     private Preference mLteDataServicePref;
    117 
    118     private static final String iface = "rmnet0"; //TODO: this will go away
    119     private List<SubscriptionInfo> mActiveSubInfos;
    120 
    121     private UserManager mUm;
    122     private Phone mPhone;
    123     private MyHandler mHandler;
    124     private boolean mOkClicked;
    125 
    126     // We assume the the value returned by mTabHost.getCurrentTab() == slotId
    127     private TabHost mTabHost;
    128 
    129     //GsmUmts options and Cdma options
    130     GsmUmtsOptions mGsmUmtsOptions;
    131     CdmaOptions mCdmaOptions;
    132 
    133     private Preference mClickedPreference;
    134     private boolean mShow4GForLTE;
    135     private boolean mIsGlobalCdma;
    136     private boolean mUnavailable;
    137 
    138     private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
    139         /*
    140          * Enable/disable the 'Enhanced 4G LTE Mode' when in/out of a call
    141          * and depending on TTY mode and TTY support over VoLTE.
    142          * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
    143          * java.lang.String)
    144          */
    145         @Override
    146         public void onCallStateChanged(int state, String incomingNumber) {
    147             if (DBG) log("PhoneStateListener.onCallStateChanged: state=" + state);
    148             boolean enabled = (state == TelephonyManager.CALL_STATE_IDLE) &&
    149                     ImsManager.isNonTtyOrTtyOnVolteEnabled(getApplicationContext());
    150             Preference pref = getPreferenceScreen().findPreference(BUTTON_4G_LTE_KEY);
    151             if (pref != null) pref.setEnabled(enabled && hasActiveSubscriptions());
    152         }
    153     };
    154 
    155     private final BroadcastReceiver mPhoneChangeReceiver = new PhoneChangeReceiver();
    156 
    157     private class PhoneChangeReceiver extends BroadcastReceiver {
    158         @Override
    159         public void onReceive(Context context, Intent intent) {
    160             if (DBG) log("onReceive:");
    161             // When the radio changes (ex: CDMA->GSM), refresh all options.
    162             mGsmUmtsOptions = null;
    163             mCdmaOptions = null;
    164             updateBody();
    165         }
    166     }
    167 
    168     //This is a method implemented for DialogInterface.OnClickListener.
    169     //  Used to dismiss the dialogs when they come up.
    170     public void onClick(DialogInterface dialog, int which) {
    171         if (which == DialogInterface.BUTTON_POSITIVE) {
    172             mPhone.setDataRoamingEnabled(true);
    173             mOkClicked = true;
    174         } else {
    175             // Reset the toggle
    176             mButtonDataRoam.setChecked(false);
    177         }
    178     }
    179 
    180     @Override
    181     public void onDismiss(DialogInterface dialog) {
    182         // Assuming that onClick gets called first
    183         mButtonDataRoam.setChecked(mOkClicked);
    184     }
    185 
    186     /**
    187      * Invoked on each preference click in this hierarchy, overrides
    188      * PreferenceActivity's implementation.  Used to make sure we track the
    189      * preference click events.
    190      */
    191     @Override
    192     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    193         /** TODO: Refactor and get rid of the if's using subclasses */
    194         final int phoneSubId = mPhone.getSubId();
    195         if (preference.getKey().equals(BUTTON_4G_LTE_KEY)) {
    196             return true;
    197         } else if (mGsmUmtsOptions != null &&
    198                 mGsmUmtsOptions.preferenceTreeClick(preference) == true) {
    199             return true;
    200         } else if (mCdmaOptions != null &&
    201                    mCdmaOptions.preferenceTreeClick(preference) == true) {
    202             if (Boolean.parseBoolean(
    203                     SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
    204 
    205                 mClickedPreference = preference;
    206 
    207                 // In ECM mode launch ECM app dialog
    208                 startActivityForResult(
    209                     new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
    210                     REQUEST_CODE_EXIT_ECM);
    211             }
    212             return true;
    213         } else if (preference == mButtonPreferredNetworkMode) {
    214             //displays the value taken from the Settings.System
    215             int settingsNetworkMode = android.provider.Settings.Global.getInt(mPhone.getContext().
    216                     getContentResolver(),
    217                     android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    218                     preferredNetworkMode);
    219             mButtonPreferredNetworkMode.setValue(Integer.toString(settingsNetworkMode));
    220             return true;
    221         } else if (preference == mLteDataServicePref) {
    222             String tmpl = android.provider.Settings.Global.getString(getContentResolver(),
    223                         android.provider.Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL);
    224             if (!TextUtils.isEmpty(tmpl)) {
    225                 TelephonyManager tm = (TelephonyManager) getSystemService(
    226                         Context.TELEPHONY_SERVICE);
    227                 String imsi = tm.getSubscriberId();
    228                 if (imsi == null) {
    229                     imsi = "";
    230                 }
    231                 final String url = TextUtils.isEmpty(tmpl) ? null
    232                         : TextUtils.expandTemplate(tmpl, imsi).toString();
    233                 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    234                 startActivity(intent);
    235             } else {
    236                 android.util.Log.e(LOG_TAG, "Missing SETUP_PREPAID_DATA_SERVICE_URL");
    237             }
    238             return true;
    239         }  else if (preference == mButtonEnabledNetworks) {
    240             int settingsNetworkMode = android.provider.Settings.Global.getInt(mPhone.getContext().
    241                             getContentResolver(),
    242                     android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    243                     preferredNetworkMode);
    244             mButtonEnabledNetworks.setValue(Integer.toString(settingsNetworkMode));
    245             return true;
    246         } else if (preference == mButtonDataRoam) {
    247             // Do not disable the preference screen if the user clicks Data roaming.
    248             return true;
    249         } else {
    250             // if the button is anything but the simple toggle preference,
    251             // we'll need to disable all preferences to reject all click
    252             // events until the sub-activity's UI comes up.
    253             preferenceScreen.setEnabled(false);
    254             // Let the intents be launched by the Preference manager
    255             return false;
    256         }
    257     }
    258 
    259     private final SubscriptionManager.OnSubscriptionsChangedListener mOnSubscriptionsChangeListener
    260             = new SubscriptionManager.OnSubscriptionsChangedListener() {
    261         @Override
    262         public void onSubscriptionsChanged() {
    263             if (DBG) log("onSubscriptionsChanged:");
    264             initializeSubscriptions();
    265         }
    266     };
    267 
    268     private void initializeSubscriptions() {
    269         if (isDestroyed()) { // Process preferences in activity only if its not destroyed
    270             return;
    271         }
    272         int currentTab = 0;
    273         if (DBG) log("initializeSubscriptions:+");
    274 
    275         // Before updating the the active subscription list check
    276         // if tab updating is needed as the list is changing.
    277         List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList();
    278         TabState state = isUpdateTabsNeeded(sil);
    279 
    280         // Update to the active subscription list
    281         mActiveSubInfos.clear();
    282         if (sil != null) {
    283             mActiveSubInfos.addAll(sil);
    284             // If there is only 1 sim then currenTab should represent slot no. of the sim.
    285             if (sil.size() == 1) {
    286                 currentTab = sil.get(0).getSimSlotIndex();
    287             }
    288         }
    289 
    290         switch (state) {
    291             case UPDATE: {
    292                 if (DBG) log("initializeSubscriptions: UPDATE");
    293                 currentTab = mTabHost != null ? mTabHost.getCurrentTab() : 0;
    294 
    295                 setContentView(com.android.internal.R.layout.common_tab_settings);
    296 
    297                 mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    298                 mTabHost.setup();
    299 
    300                 // Update the tabName. Since the mActiveSubInfos are in slot order
    301                 // we can iterate though the tabs and subscription info in one loop. But
    302                 // we need to handle the case where a slot may be empty.
    303 
    304                 Iterator<SubscriptionInfo> siIterator = mActiveSubInfos.listIterator();
    305                 SubscriptionInfo si = siIterator.hasNext() ? siIterator.next() : null;
    306                 for (int simSlotIndex = 0; simSlotIndex  < mActiveSubInfos.size(); simSlotIndex++) {
    307                     String tabName;
    308                     if (si != null && si.getSimSlotIndex() == simSlotIndex) {
    309                         // Slot is not empty and we match
    310                         tabName = String.valueOf(si.getDisplayName());
    311                         si = siIterator.hasNext() ? siIterator.next() : null;
    312                     } else {
    313                         // Slot is empty, set name to unknown
    314                         tabName = getResources().getString(R.string.unknown);
    315                     }
    316                     if (DBG) {
    317                         log("initializeSubscriptions: tab=" + simSlotIndex + " name=" + tabName);
    318                     }
    319 
    320                     mTabHost.addTab(buildTabSpec(String.valueOf(simSlotIndex), tabName));
    321                 }
    322 
    323                 mTabHost.setOnTabChangedListener(mTabListener);
    324                 mTabHost.setCurrentTab(currentTab);
    325                 break;
    326             }
    327             case NO_TABS: {
    328                 if (DBG) log("initializeSubscriptions: NO_TABS");
    329 
    330                 if (mTabHost != null) {
    331                     mTabHost.clearAllTabs();
    332                     mTabHost = null;
    333                 }
    334                 setContentView(com.android.internal.R.layout.common_tab_settings);
    335                 break;
    336             }
    337             case DO_NOTHING: {
    338                 if (DBG) log("initializeSubscriptions: DO_NOTHING");
    339                 if (mTabHost != null) {
    340                     currentTab = mTabHost.getCurrentTab();
    341                 }
    342                 break;
    343             }
    344         }
    345         updatePhone(currentTab);
    346         updateBody();
    347         if (DBG) log("initializeSubscriptions:-");
    348     }
    349 
    350     private enum TabState {
    351         NO_TABS, UPDATE, DO_NOTHING
    352     }
    353     private TabState isUpdateTabsNeeded(List<SubscriptionInfo> newSil) {
    354         TabState state = TabState.DO_NOTHING;
    355         if (newSil == null) {
    356             if (mActiveSubInfos.size() >= TAB_THRESHOLD) {
    357                 if (DBG) log("isUpdateTabsNeeded: NO_TABS, size unknown and was tabbed");
    358                 state = TabState.NO_TABS;
    359             }
    360         } else if (newSil.size() < TAB_THRESHOLD && mActiveSubInfos.size() >= TAB_THRESHOLD) {
    361             if (DBG) log("isUpdateTabsNeeded: NO_TABS, size went to small");
    362             state = TabState.NO_TABS;
    363         } else if (newSil.size() >= TAB_THRESHOLD && mActiveSubInfos.size() < TAB_THRESHOLD) {
    364             if (DBG) log("isUpdateTabsNeeded: UPDATE, size changed");
    365             state = TabState.UPDATE;
    366         } else if (newSil.size() >= TAB_THRESHOLD) {
    367             Iterator<SubscriptionInfo> siIterator = mActiveSubInfos.iterator();
    368             for(SubscriptionInfo newSi : newSil) {
    369                 SubscriptionInfo curSi = siIterator.next();
    370                 if (!newSi.getDisplayName().equals(curSi.getDisplayName())) {
    371                     if (DBG) log("isUpdateTabsNeeded: UPDATE, new name=" + newSi.getDisplayName());
    372                     state = TabState.UPDATE;
    373                     break;
    374                 }
    375             }
    376         }
    377         if (DBG) {
    378             log("isUpdateTabsNeeded:- " + state
    379                 + " newSil.size()=" + ((newSil != null) ? newSil.size() : 0)
    380                 + " mActiveSubInfos.size()=" + mActiveSubInfos.size());
    381         }
    382         return state;
    383     }
    384 
    385     private OnTabChangeListener mTabListener = new OnTabChangeListener() {
    386         @Override
    387         public void onTabChanged(String tabId) {
    388             if (DBG) log("onTabChanged:");
    389             // The User has changed tab; update the body.
    390             updatePhone(Integer.parseInt(tabId));
    391             updateBody();
    392         }
    393     };
    394 
    395     private void updatePhone(int slotId) {
    396         final SubscriptionInfo sir = mSubscriptionManager
    397                 .getActiveSubscriptionInfoForSimSlotIndex(slotId);
    398         if (sir != null) {
    399             mPhone = PhoneFactory.getPhone(
    400                     SubscriptionManager.getPhoneId(sir.getSubscriptionId()));
    401         }
    402         if (mPhone == null) {
    403             // Do the best we can
    404             mPhone = PhoneGlobals.getPhone();
    405         }
    406         if (DBG) log("updatePhone:- slotId=" + slotId + " sir=" + sir);
    407     }
    408 
    409     private TabContentFactory mEmptyTabContent = new TabContentFactory() {
    410         @Override
    411         public View createTabContent(String tag) {
    412             return new View(mTabHost.getContext());
    413         }
    414     };
    415 
    416     private TabSpec buildTabSpec(String tag, String title) {
    417         return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
    418                 mEmptyTabContent);
    419     }
    420 
    421     @Override
    422     protected void onCreate(Bundle icicle) {
    423         if (DBG) log("onCreate:+");
    424         setTheme(R.style.Theme_Material_Settings);
    425         super.onCreate(icicle);
    426 
    427         mHandler = new MyHandler();
    428         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
    429         mSubscriptionManager = SubscriptionManager.from(this);
    430 
    431         if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
    432             mUnavailable = true;
    433             setContentView(R.layout.telephony_disallowed_preference_screen);
    434             return;
    435         }
    436 
    437         addPreferencesFromResource(R.xml.network_setting);
    438 
    439         mButton4glte = (SwitchPreference)findPreference(BUTTON_4G_LTE_KEY);
    440         mButton4glte.setOnPreferenceChangeListener(this);
    441 
    442         try {
    443             Context con = createPackageContext("com.android.systemui", 0);
    444             int id = con.getResources().getIdentifier("config_show4GForLTE",
    445                     "bool", "com.android.systemui");
    446             mShow4GForLTE = con.getResources().getBoolean(id);
    447         } catch (NameNotFoundException e) {
    448             loge("NameNotFoundException for show4GFotLTE");
    449             mShow4GForLTE = false;
    450         }
    451 
    452         //get UI object references
    453         PreferenceScreen prefSet = getPreferenceScreen();
    454 
    455         mButtonDataRoam = (RestrictedSwitchPreference) prefSet.findPreference(BUTTON_ROAMING_KEY);
    456         mButtonPreferredNetworkMode = (ListPreference) prefSet.findPreference(
    457                 BUTTON_PREFERED_NETWORK_MODE);
    458         mButtonEnabledNetworks = (ListPreference) prefSet.findPreference(
    459                 BUTTON_ENABLED_NETWORKS_KEY);
    460         mButtonDataRoam.setOnPreferenceChangeListener(this);
    461 
    462         mLteDataServicePref = prefSet.findPreference(BUTTON_CDMA_LTE_DATA_SERVICE_KEY);
    463 
    464         // Initialize mActiveSubInfo
    465         int max = mSubscriptionManager.getActiveSubscriptionInfoCountMax();
    466         mActiveSubInfos = new ArrayList<SubscriptionInfo>(max);
    467 
    468         initializeSubscriptions();
    469 
    470         IntentFilter intentFilter = new IntentFilter(
    471                 TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
    472         registerReceiver(mPhoneChangeReceiver, intentFilter);
    473         if (DBG) log("onCreate:-");
    474     }
    475 
    476     @Override
    477     protected void onDestroy() {
    478         super.onDestroy();
    479         unregisterReceiver(mPhoneChangeReceiver);
    480     }
    481 
    482     @Override
    483     protected void onResume() {
    484         super.onResume();
    485         if (DBG) log("onResume:+");
    486 
    487         if (mUnavailable) {
    488             if (DBG) log("onResume:- ignore mUnavailable == false");
    489             return;
    490         }
    491 
    492         // upon resumption from the sub-activity, make sure we re-enable the
    493         // preferences.
    494         getPreferenceScreen().setEnabled(true);
    495 
    496         // Set UI state in onResume because a user could go home, launch some
    497         // app to change this setting's backend, and re-launch this settings app
    498         // and the UI state would be inconsistent with actual state
    499         mButtonDataRoam.setChecked(mPhone.getDataRoamingEnabled());
    500 
    501         if (getPreferenceScreen().findPreference(BUTTON_PREFERED_NETWORK_MODE) != null
    502                 || getPreferenceScreen().findPreference(BUTTON_ENABLED_NETWORKS_KEY) != null)  {
    503             updatePreferredNetworkUIFromDb();
    504         }
    505 
    506         if (ImsManager.isVolteEnabledByPlatform(this)
    507                 && ImsManager.isVolteProvisionedOnDevice(this)) {
    508             TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    509             tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    510         }
    511 
    512         // NOTE: Buttons will be enabled/disabled in mPhoneStateListener
    513         boolean enh4glteMode = ImsManager.isEnhanced4gLteModeSettingEnabledByUser(this)
    514                 && ImsManager.isNonTtyOrTtyOnVolteEnabled(this);
    515         mButton4glte.setChecked(enh4glteMode);
    516 
    517         mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
    518 
    519         if (DBG) log("onResume:-");
    520 
    521     }
    522 
    523     private boolean hasActiveSubscriptions() {
    524         return mActiveSubInfos.size() > 0;
    525     }
    526 
    527     private void updateBody() {
    528         final Context context = getApplicationContext();
    529         PreferenceScreen prefSet = getPreferenceScreen();
    530         boolean isLteOnCdma = mPhone.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE;
    531         final int phoneSubId = mPhone.getSubId();
    532 
    533         if (DBG) {
    534             log("updateBody: isLteOnCdma=" + isLteOnCdma + " phoneSubId=" + phoneSubId);
    535         }
    536 
    537         if (prefSet != null) {
    538             prefSet.removeAll();
    539             prefSet.addPreference(mButtonDataRoam);
    540             prefSet.addPreference(mButtonPreferredNetworkMode);
    541             prefSet.addPreference(mButtonEnabledNetworks);
    542             prefSet.addPreference(mButton4glte);
    543         }
    544 
    545         int settingsNetworkMode = android.provider.Settings.Global.getInt(
    546                 mPhone.getContext().getContentResolver(),
    547                 android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    548                 preferredNetworkMode);
    549 
    550         PersistableBundle carrierConfig =
    551                 PhoneGlobals.getInstance().getCarrierConfigForSubId(mPhone.getSubId());
    552         mIsGlobalCdma = isLteOnCdma
    553                 && carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_CDMA_CHOICES_BOOL);
    554         if (carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)) {
    555             prefSet.removePreference(mButtonPreferredNetworkMode);
    556             prefSet.removePreference(mButtonEnabledNetworks);
    557             prefSet.removePreference(mLteDataServicePref);
    558         } else if (carrierConfig.getBoolean(CarrierConfigManager
    559                     .KEY_HIDE_PREFERRED_NETWORK_TYPE_BOOL)
    560                 && !mPhone.getServiceState().getRoaming()) {
    561             prefSet.removePreference(mButtonPreferredNetworkMode);
    562             prefSet.removePreference(mButtonEnabledNetworks);
    563 
    564             final int phoneType = mPhone.getPhoneType();
    565             if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
    566                 mCdmaOptions = new CdmaOptions(this, prefSet, mPhone);
    567                 // In World mode force a refresh of GSM Options.
    568                 if (isWorldMode()) {
    569                     mGsmUmtsOptions = null;
    570                 }
    571             } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
    572                 mGsmUmtsOptions = new GsmUmtsOptions(this, prefSet, phoneSubId);
    573             } else {
    574                 throw new IllegalStateException("Unexpected phone type: " + phoneType);
    575             }
    576             // Since pref is being hidden from user, set network mode to default
    577             // in case it is currently something else. That is possible if user
    578             // changed the setting while roaming and is now back to home network.
    579             settingsNetworkMode = preferredNetworkMode;
    580         } else if (carrierConfig.getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL) == true) {
    581             prefSet.removePreference(mButtonEnabledNetworks);
    582             // set the listener for the mButtonPreferredNetworkMode list preference so we can issue
    583             // change Preferred Network Mode.
    584             mButtonPreferredNetworkMode.setOnPreferenceChangeListener(this);
    585 
    586             mCdmaOptions = new CdmaOptions(this, prefSet, mPhone);
    587             mGsmUmtsOptions = new GsmUmtsOptions(this, prefSet, phoneSubId);
    588         } else {
    589             prefSet.removePreference(mButtonPreferredNetworkMode);
    590             final int phoneType = mPhone.getPhoneType();
    591             if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
    592                 int lteForced = android.provider.Settings.Global.getInt(
    593                         mPhone.getContext().getContentResolver(),
    594                         android.provider.Settings.Global.LTE_SERVICE_FORCED + mPhone.getSubId(),
    595                         0);
    596 
    597                 if (isLteOnCdma) {
    598                     if (lteForced == 0) {
    599                         mButtonEnabledNetworks.setEntries(
    600                                 R.array.enabled_networks_cdma_choices);
    601                         mButtonEnabledNetworks.setEntryValues(
    602                                 R.array.enabled_networks_cdma_values);
    603                     } else {
    604                         switch (settingsNetworkMode) {
    605                             case Phone.NT_MODE_CDMA:
    606                             case Phone.NT_MODE_CDMA_NO_EVDO:
    607                             case Phone.NT_MODE_EVDO_NO_CDMA:
    608                                 mButtonEnabledNetworks.setEntries(
    609                                         R.array.enabled_networks_cdma_no_lte_choices);
    610                                 mButtonEnabledNetworks.setEntryValues(
    611                                         R.array.enabled_networks_cdma_no_lte_values);
    612                                 break;
    613                             case Phone.NT_MODE_GLOBAL:
    614                             case Phone.NT_MODE_LTE_CDMA_AND_EVDO:
    615                             case Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
    616                             case Phone.NT_MODE_LTE_ONLY:
    617                                 mButtonEnabledNetworks.setEntries(
    618                                         R.array.enabled_networks_cdma_only_lte_choices);
    619                                 mButtonEnabledNetworks.setEntryValues(
    620                                         R.array.enabled_networks_cdma_only_lte_values);
    621                                 break;
    622                             default:
    623                                 mButtonEnabledNetworks.setEntries(
    624                                         R.array.enabled_networks_cdma_choices);
    625                                 mButtonEnabledNetworks.setEntryValues(
    626                                         R.array.enabled_networks_cdma_values);
    627                                 break;
    628                         }
    629                     }
    630                 }
    631                 mCdmaOptions = new CdmaOptions(this, prefSet, mPhone);
    632 
    633                 // In World mode force a refresh of GSM Options.
    634                 if (isWorldMode()) {
    635                     mGsmUmtsOptions = null;
    636                 }
    637             } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
    638                 if (isSupportTdscdma()) {
    639                     mButtonEnabledNetworks.setEntries(
    640                             R.array.enabled_networks_tdscdma_choices);
    641                     mButtonEnabledNetworks.setEntryValues(
    642                             R.array.enabled_networks_tdscdma_values);
    643                 } else if (!carrierConfig.getBoolean(CarrierConfigManager.KEY_PREFER_2G_BOOL)
    644                         && !getResources().getBoolean(R.bool.config_enabled_lte)) {
    645                     mButtonEnabledNetworks.setEntries(
    646                             R.array.enabled_networks_except_gsm_lte_choices);
    647                     mButtonEnabledNetworks.setEntryValues(
    648                             R.array.enabled_networks_except_gsm_lte_values);
    649                 } else if (!carrierConfig.getBoolean(CarrierConfigManager.KEY_PREFER_2G_BOOL)) {
    650                     int select = (mShow4GForLTE == true) ?
    651                             R.array.enabled_networks_except_gsm_4g_choices
    652                             : R.array.enabled_networks_except_gsm_choices;
    653                     mButtonEnabledNetworks.setEntries(select);
    654                     mButtonEnabledNetworks.setEntryValues(
    655                             R.array.enabled_networks_except_gsm_values);
    656                 } else if (!getResources().getBoolean(R.bool.config_enabled_lte)) {
    657                     mButtonEnabledNetworks.setEntries(
    658                             R.array.enabled_networks_except_lte_choices);
    659                     mButtonEnabledNetworks.setEntryValues(
    660                             R.array.enabled_networks_except_lte_values);
    661                 } else if (mIsGlobalCdma) {
    662                     mButtonEnabledNetworks.setEntries(
    663                             R.array.enabled_networks_cdma_choices);
    664                     mButtonEnabledNetworks.setEntryValues(
    665                             R.array.enabled_networks_cdma_values);
    666                 } else {
    667                     int select = (mShow4GForLTE == true) ? R.array.enabled_networks_4g_choices
    668                             : R.array.enabled_networks_choices;
    669                     mButtonEnabledNetworks.setEntries(select);
    670                     mButtonEnabledNetworks.setEntryValues(
    671                             R.array.enabled_networks_values);
    672                 }
    673                 mGsmUmtsOptions = new GsmUmtsOptions(this, prefSet, phoneSubId);
    674             } else {
    675                 throw new IllegalStateException("Unexpected phone type: " + phoneType);
    676             }
    677             if (isWorldMode()) {
    678                 mButtonEnabledNetworks.setEntries(
    679                         R.array.preferred_network_mode_choices_world_mode);
    680                 mButtonEnabledNetworks.setEntryValues(
    681                         R.array.preferred_network_mode_values_world_mode);
    682             }
    683             mButtonEnabledNetworks.setOnPreferenceChangeListener(this);
    684             if (DBG) log("settingsNetworkMode: " + settingsNetworkMode);
    685         }
    686 
    687         final boolean missingDataServiceUrl = TextUtils.isEmpty(
    688                 android.provider.Settings.Global.getString(getContentResolver(),
    689                         android.provider.Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL));
    690         if (!isLteOnCdma || missingDataServiceUrl) {
    691             prefSet.removePreference(mLteDataServicePref);
    692         } else {
    693             android.util.Log.d(LOG_TAG, "keep ltePref");
    694         }
    695 
    696         // Enable enhanced 4G LTE mode settings depending on whether exists on platform
    697         if (!(ImsManager.isVolteEnabledByPlatform(this)
    698                 && ImsManager.isVolteProvisionedOnDevice(this))) {
    699             Preference pref = prefSet.findPreference(BUTTON_4G_LTE_KEY);
    700             if (pref != null) {
    701                 prefSet.removePreference(pref);
    702             }
    703         }
    704 
    705         ActionBar actionBar = getActionBar();
    706         if (actionBar != null) {
    707             // android.R.id.home will be triggered in onOptionsItemSelected()
    708             actionBar.setDisplayHomeAsUpEnabled(true);
    709         }
    710 
    711         // Enable link to CMAS app settings depending on the value in config.xml.
    712         final boolean isCellBroadcastAppLinkEnabled = this.getResources().getBoolean(
    713                 com.android.internal.R.bool.config_cellBroadcastAppLinks);
    714         if (!mUm.isAdminUser() || !isCellBroadcastAppLinkEnabled
    715                 || mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS)) {
    716             PreferenceScreen root = getPreferenceScreen();
    717             Preference ps = findPreference(BUTTON_CELL_BROADCAST_SETTINGS);
    718             if (ps != null) {
    719                 root.removePreference(ps);
    720             }
    721         }
    722 
    723         // Get the networkMode from Settings.System and displays it
    724         mButtonDataRoam.setChecked(mPhone.getDataRoamingEnabled());
    725         mButtonEnabledNetworks.setValue(Integer.toString(settingsNetworkMode));
    726         mButtonPreferredNetworkMode.setValue(Integer.toString(settingsNetworkMode));
    727         UpdatePreferredNetworkModeSummary(settingsNetworkMode);
    728         UpdateEnabledNetworksValueAndSummary(settingsNetworkMode);
    729         // Display preferred network type based on what modem returns b/18676277
    730         mPhone.setPreferredNetworkType(settingsNetworkMode, mHandler
    731                 .obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
    732 
    733         /**
    734          * Enable/disable depending upon if there are any active subscriptions.
    735          *
    736          * I've decided to put this enable/disable code at the bottom as the
    737          * code above works even when there are no active subscriptions, thus
    738          * putting it afterwards is a smaller change. This can be refined later,
    739          * but you do need to remember that this all needs to work when subscriptions
    740          * change dynamically such as when hot swapping sims.
    741          */
    742         boolean hasActiveSubscriptions = hasActiveSubscriptions();
    743         TelephonyManager tm = (TelephonyManager) getSystemService(
    744                 Context.TELEPHONY_SERVICE);
    745         boolean canChange4glte = (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) &&
    746                 ImsManager.isNonTtyOrTtyOnVolteEnabled(getApplicationContext()) &&
    747                 carrierConfig.getBoolean(CarrierConfigManager.KEY_EDITABLE_ENHANCED_4G_LTE_BOOL);
    748         mButtonDataRoam.setDisabledByAdmin(false);
    749         mButtonDataRoam.setEnabled(hasActiveSubscriptions);
    750         if (mButtonDataRoam.isEnabled()) {
    751             if (RestrictedLockUtils.hasBaseUserRestriction(context,
    752                     UserManager.DISALLOW_DATA_ROAMING, UserHandle.myUserId())) {
    753                 mButtonDataRoam.setEnabled(false);
    754             } else {
    755                 mButtonDataRoam.checkRestrictionAndSetDisabled(UserManager.DISALLOW_DATA_ROAMING);
    756             }
    757         }
    758         mButtonPreferredNetworkMode.setEnabled(hasActiveSubscriptions);
    759         mButtonEnabledNetworks.setEnabled(hasActiveSubscriptions);
    760         mButton4glte.setEnabled(hasActiveSubscriptions && canChange4glte);
    761         mLteDataServicePref.setEnabled(hasActiveSubscriptions);
    762         Preference ps;
    763         PreferenceScreen root = getPreferenceScreen();
    764         ps = findPreference(BUTTON_CELL_BROADCAST_SETTINGS);
    765         if (ps != null) {
    766             ps.setEnabled(hasActiveSubscriptions);
    767         }
    768         ps = findPreference(BUTTON_APN_EXPAND_KEY);
    769         if (ps != null) {
    770             ps.setEnabled(hasActiveSubscriptions);
    771         }
    772         ps = findPreference(BUTTON_OPERATOR_SELECTION_EXPAND_KEY);
    773         if (ps != null) {
    774             ps.setEnabled(hasActiveSubscriptions);
    775         }
    776         ps = findPreference(BUTTON_CARRIER_SETTINGS_KEY);
    777         if (ps != null) {
    778             ps.setEnabled(hasActiveSubscriptions);
    779         }
    780         ps = findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY);
    781         if (ps != null) {
    782             ps.setEnabled(hasActiveSubscriptions);
    783         }
    784     }
    785 
    786     @Override
    787     protected void onPause() {
    788         super.onPause();
    789         if (DBG) log("onPause:+");
    790 
    791         if (ImsManager.isVolteEnabledByPlatform(this)
    792                 && ImsManager.isVolteProvisionedOnDevice(this)) {
    793             TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    794             tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
    795         }
    796 
    797         mSubscriptionManager
    798             .removeOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
    799         if (DBG) log("onPause:-");
    800     }
    801 
    802     /**
    803      * Implemented to support onPreferenceChangeListener to look for preference
    804      * changes specifically on CLIR.
    805      *
    806      * @param preference is the preference to be changed, should be mButtonCLIR.
    807      * @param objValue should be the value of the selection, NOT its localized
    808      * display value.
    809      */
    810     public boolean onPreferenceChange(Preference preference, Object objValue) {
    811         final int phoneSubId = mPhone.getSubId();
    812         if (preference == mButtonPreferredNetworkMode) {
    813             //NOTE onPreferenceChange seems to be called even if there is no change
    814             //Check if the button value is changed from the System.Setting
    815             mButtonPreferredNetworkMode.setValue((String) objValue);
    816             int buttonNetworkMode;
    817             buttonNetworkMode = Integer.parseInt((String) objValue);
    818             int settingsNetworkMode = android.provider.Settings.Global.getInt(
    819                     mPhone.getContext().getContentResolver(),
    820                     android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    821                     preferredNetworkMode);
    822             if (buttonNetworkMode != settingsNetworkMode) {
    823                 int modemNetworkMode;
    824                 // if new mode is invalid ignore it
    825                 switch (buttonNetworkMode) {
    826                     case Phone.NT_MODE_WCDMA_PREF:
    827                     case Phone.NT_MODE_GSM_ONLY:
    828                     case Phone.NT_MODE_WCDMA_ONLY:
    829                     case Phone.NT_MODE_GSM_UMTS:
    830                     case Phone.NT_MODE_CDMA:
    831                     case Phone.NT_MODE_CDMA_NO_EVDO:
    832                     case Phone.NT_MODE_EVDO_NO_CDMA:
    833                     case Phone.NT_MODE_GLOBAL:
    834                     case Phone.NT_MODE_LTE_CDMA_AND_EVDO:
    835                     case Phone.NT_MODE_LTE_GSM_WCDMA:
    836                     case Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
    837                     case Phone.NT_MODE_LTE_ONLY:
    838                     case Phone.NT_MODE_LTE_WCDMA:
    839                     case Phone.NT_MODE_TDSCDMA_ONLY:
    840                     case Phone.NT_MODE_TDSCDMA_WCDMA:
    841                     case Phone.NT_MODE_LTE_TDSCDMA:
    842                     case Phone.NT_MODE_TDSCDMA_GSM:
    843                     case Phone.NT_MODE_LTE_TDSCDMA_GSM:
    844                     case Phone.NT_MODE_TDSCDMA_GSM_WCDMA:
    845                     case Phone.NT_MODE_LTE_TDSCDMA_WCDMA:
    846                     case Phone.NT_MODE_LTE_TDSCDMA_GSM_WCDMA:
    847                     case Phone.NT_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
    848                     case Phone.NT_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
    849                         // This is one of the modes we recognize
    850                         modemNetworkMode = buttonNetworkMode;
    851                         break;
    852                     default:
    853                         loge("Invalid Network Mode (" + buttonNetworkMode + ") chosen. Ignore.");
    854                         return true;
    855                 }
    856 
    857                 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
    858                         android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    859                         buttonNetworkMode );
    860                 //Set the modem network mode
    861                 mPhone.setPreferredNetworkType(modemNetworkMode, mHandler
    862                         .obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
    863             }
    864         } else if (preference == mButtonEnabledNetworks) {
    865             mButtonEnabledNetworks.setValue((String) objValue);
    866             int buttonNetworkMode;
    867             buttonNetworkMode = Integer.parseInt((String) objValue);
    868             if (DBG) log("buttonNetworkMode: " + buttonNetworkMode);
    869             int settingsNetworkMode = android.provider.Settings.Global.getInt(
    870                     mPhone.getContext().getContentResolver(),
    871                     android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    872                     preferredNetworkMode);
    873             if (buttonNetworkMode != settingsNetworkMode) {
    874                 int modemNetworkMode;
    875                 // if new mode is invalid ignore it
    876                 switch (buttonNetworkMode) {
    877                     case Phone.NT_MODE_WCDMA_PREF:
    878                     case Phone.NT_MODE_GSM_ONLY:
    879                     case Phone.NT_MODE_LTE_GSM_WCDMA:
    880                     case Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
    881                     case Phone.NT_MODE_CDMA:
    882                     case Phone.NT_MODE_CDMA_NO_EVDO:
    883                     case Phone.NT_MODE_LTE_CDMA_AND_EVDO:
    884                     case Phone.NT_MODE_TDSCDMA_ONLY:
    885                     case Phone.NT_MODE_TDSCDMA_WCDMA:
    886                     case Phone.NT_MODE_LTE_TDSCDMA:
    887                     case Phone.NT_MODE_TDSCDMA_GSM:
    888                     case Phone.NT_MODE_LTE_TDSCDMA_GSM:
    889                     case Phone.NT_MODE_TDSCDMA_GSM_WCDMA:
    890                     case Phone.NT_MODE_LTE_TDSCDMA_WCDMA:
    891                     case Phone.NT_MODE_LTE_TDSCDMA_GSM_WCDMA:
    892                     case Phone.NT_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
    893                     case Phone.NT_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
    894                         // This is one of the modes we recognize
    895                         modemNetworkMode = buttonNetworkMode;
    896                         break;
    897                     default:
    898                         loge("Invalid Network Mode (" + buttonNetworkMode + ") chosen. Ignore.");
    899                         return true;
    900                 }
    901 
    902                 UpdateEnabledNetworksValueAndSummary(buttonNetworkMode);
    903 
    904                 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
    905                         android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    906                         buttonNetworkMode );
    907                 //Set the modem network mode
    908                 mPhone.setPreferredNetworkType(modemNetworkMode, mHandler
    909                         .obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
    910             }
    911         } else if (preference == mButton4glte) {
    912             SwitchPreference enhanced4gModePref = (SwitchPreference) preference;
    913             boolean enhanced4gMode = !enhanced4gModePref.isChecked();
    914             enhanced4gModePref.setChecked(enhanced4gMode);
    915             ImsManager.setEnhanced4gLteModeSetting(this, enhanced4gModePref.isChecked());
    916         } else if (preference == mButtonDataRoam) {
    917             if (DBG) log("onPreferenceTreeClick: preference == mButtonDataRoam.");
    918 
    919             //normally called on the toggle click
    920             if (!mButtonDataRoam.isChecked()) {
    921                 // First confirm with a warning dialog about charges
    922                 mOkClicked = false;
    923                 new AlertDialog.Builder(this).setMessage(
    924                         getResources().getString(R.string.roaming_warning))
    925                         .setTitle(R.string.roaming_alert_title)
    926                         .setIconAttribute(android.R.attr.alertDialogIcon)
    927                         .setPositiveButton(android.R.string.yes, this)
    928                         .setNegativeButton(android.R.string.no, this)
    929                         .show()
    930                         .setOnDismissListener(this);
    931             } else {
    932                 mPhone.setDataRoamingEnabled(false);
    933             }
    934             return true;
    935         }
    936 
    937         updateBody();
    938         // always let the preference setting proceed.
    939         return true;
    940     }
    941 
    942     private class MyHandler extends Handler {
    943 
    944         static final int MESSAGE_SET_PREFERRED_NETWORK_TYPE = 0;
    945 
    946         @Override
    947         public void handleMessage(Message msg) {
    948             switch (msg.what) {
    949                 case MESSAGE_SET_PREFERRED_NETWORK_TYPE:
    950                     handleSetPreferredNetworkTypeResponse(msg);
    951                     break;
    952             }
    953         }
    954 
    955         private void handleSetPreferredNetworkTypeResponse(Message msg) {
    956             if (isDestroyed()) { // Access preferences of activity only if it is not destroyed
    957                 return;
    958             }
    959 
    960             AsyncResult ar = (AsyncResult) msg.obj;
    961             final int phoneSubId = mPhone.getSubId();
    962 
    963             if (ar.exception == null) {
    964                 int networkMode;
    965                 if (getPreferenceScreen().findPreference(BUTTON_PREFERED_NETWORK_MODE) != null)  {
    966                     networkMode =  Integer.parseInt(mButtonPreferredNetworkMode.getValue());
    967                     android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
    968                             android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    969                             networkMode );
    970                 }
    971                 if (getPreferenceScreen().findPreference(BUTTON_ENABLED_NETWORKS_KEY) != null)  {
    972                     networkMode = Integer.parseInt(mButtonEnabledNetworks.getValue());
    973                     android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
    974                             android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    975                             networkMode );
    976                 }
    977             } else {
    978                 if (DBG) {
    979                     log("handleSetPreferredNetworkTypeResponse: exception in setting network mode.");
    980                 }
    981                 updatePreferredNetworkUIFromDb();
    982             }
    983         }
    984     }
    985 
    986     private void updatePreferredNetworkUIFromDb() {
    987         final int phoneSubId = mPhone.getSubId();
    988 
    989         int settingsNetworkMode = android.provider.Settings.Global.getInt(
    990                 mPhone.getContext().getContentResolver(),
    991                 android.provider.Settings.Global.PREFERRED_NETWORK_MODE + phoneSubId,
    992                 preferredNetworkMode);
    993 
    994         if (DBG) {
    995             log("updatePreferredNetworkUIFromDb: settingsNetworkMode = " +
    996                     settingsNetworkMode);
    997         }
    998 
    999         UpdatePreferredNetworkModeSummary(settingsNetworkMode);
   1000         UpdateEnabledNetworksValueAndSummary(settingsNetworkMode);
   1001         // changes the mButtonPreferredNetworkMode accordingly to settingsNetworkMode
   1002         mButtonPreferredNetworkMode.setValue(Integer.toString(settingsNetworkMode));
   1003     }
   1004 
   1005     private void UpdatePreferredNetworkModeSummary(int NetworkMode) {
   1006         switch(NetworkMode) {
   1007             case Phone.NT_MODE_TDSCDMA_GSM_WCDMA:
   1008             case Phone.NT_MODE_TDSCDMA_GSM:
   1009             case Phone.NT_MODE_WCDMA_PREF:
   1010                 mButtonPreferredNetworkMode.setSummary(
   1011                         R.string.preferred_network_mode_wcdma_perf_summary);
   1012                 break;
   1013             case Phone.NT_MODE_GSM_ONLY:
   1014                 mButtonPreferredNetworkMode.setSummary(
   1015                         R.string.preferred_network_mode_gsm_only_summary);
   1016                 break;
   1017             case Phone.NT_MODE_TDSCDMA_WCDMA:
   1018             case Phone.NT_MODE_WCDMA_ONLY:
   1019                 mButtonPreferredNetworkMode.setSummary(
   1020                         R.string.preferred_network_mode_wcdma_only_summary);
   1021                 break;
   1022             case Phone.NT_MODE_GSM_UMTS:
   1023                 mButtonPreferredNetworkMode.setSummary(
   1024                         R.string.preferred_network_mode_gsm_wcdma_summary);
   1025                 break;
   1026             case Phone.NT_MODE_CDMA:
   1027                 switch (mPhone.getLteOnCdmaMode()) {
   1028                     case PhoneConstants.LTE_ON_CDMA_TRUE:
   1029                         mButtonPreferredNetworkMode.setSummary(
   1030                             R.string.preferred_network_mode_cdma_summary);
   1031                     break;
   1032                     case PhoneConstants.LTE_ON_CDMA_FALSE:
   1033                     default:
   1034                         mButtonPreferredNetworkMode.setSummary(
   1035                             R.string.preferred_network_mode_cdma_evdo_summary);
   1036                         break;
   1037                 }
   1038                 break;
   1039             case Phone.NT_MODE_CDMA_NO_EVDO:
   1040                 mButtonPreferredNetworkMode.setSummary(
   1041                         R.string.preferred_network_mode_cdma_only_summary);
   1042                 break;
   1043             case Phone.NT_MODE_EVDO_NO_CDMA:
   1044                 mButtonPreferredNetworkMode.setSummary(
   1045                         R.string.preferred_network_mode_evdo_only_summary);
   1046                 break;
   1047             case Phone.NT_MODE_LTE_TDSCDMA:
   1048             case Phone.NT_MODE_LTE_ONLY:
   1049                 mButtonPreferredNetworkMode.setSummary(
   1050                         R.string.preferred_network_mode_lte_summary);
   1051                 break;
   1052             case Phone.NT_MODE_LTE_TDSCDMA_GSM:
   1053             case Phone.NT_MODE_LTE_TDSCDMA_GSM_WCDMA:
   1054             case Phone.NT_MODE_LTE_GSM_WCDMA:
   1055                 mButtonPreferredNetworkMode.setSummary(
   1056                         R.string.preferred_network_mode_lte_gsm_wcdma_summary);
   1057                 break;
   1058             case Phone.NT_MODE_LTE_CDMA_AND_EVDO:
   1059                 mButtonPreferredNetworkMode.setSummary(
   1060                         R.string.preferred_network_mode_lte_cdma_evdo_summary);
   1061                 break;
   1062             case Phone.NT_MODE_TDSCDMA_ONLY:
   1063                 mButtonPreferredNetworkMode.setSummary(
   1064                         R.string.preferred_network_mode_tdscdma_summary);
   1065                 break;
   1066             case Phone.NT_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
   1067             case Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
   1068                 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA ||
   1069                         mIsGlobalCdma ||
   1070                         isWorldMode()) {
   1071                     mButtonPreferredNetworkMode.setSummary(
   1072                             R.string.preferred_network_mode_global_summary);
   1073                 } else {
   1074                     mButtonPreferredNetworkMode.setSummary(
   1075                             R.string.preferred_network_mode_lte_summary);
   1076                 }
   1077                 break;
   1078             case Phone.NT_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
   1079             case Phone.NT_MODE_GLOBAL:
   1080                 mButtonPreferredNetworkMode.setSummary(
   1081                         R.string.preferred_network_mode_cdma_evdo_gsm_wcdma_summary);
   1082                 break;
   1083             case Phone.NT_MODE_LTE_TDSCDMA_WCDMA:
   1084             case Phone.NT_MODE_LTE_WCDMA:
   1085                 mButtonPreferredNetworkMode.setSummary(
   1086                         R.string.preferred_network_mode_lte_wcdma_summary);
   1087                 break;
   1088             default:
   1089                 mButtonPreferredNetworkMode.setSummary(
   1090                         R.string.preferred_network_mode_global_summary);
   1091         }
   1092     }
   1093 
   1094     private void UpdateEnabledNetworksValueAndSummary(int NetworkMode) {
   1095         switch (NetworkMode) {
   1096             case Phone.NT_MODE_TDSCDMA_WCDMA:
   1097             case Phone.NT_MODE_TDSCDMA_GSM_WCDMA:
   1098             case Phone.NT_MODE_TDSCDMA_GSM:
   1099                 mButtonEnabledNetworks.setValue(
   1100                         Integer.toString(Phone.NT_MODE_TDSCDMA_GSM_WCDMA));
   1101                 mButtonEnabledNetworks.setSummary(R.string.network_3G);
   1102                 break;
   1103             case Phone.NT_MODE_WCDMA_ONLY:
   1104             case Phone.NT_MODE_GSM_UMTS:
   1105             case Phone.NT_MODE_WCDMA_PREF:
   1106                 if (!mIsGlobalCdma) {
   1107                     mButtonEnabledNetworks.setValue(
   1108                             Integer.toString(Phone.NT_MODE_WCDMA_PREF));
   1109                     mButtonEnabledNetworks.setSummary(R.string.network_3G);
   1110                 } else {
   1111                     mButtonEnabledNetworks.setValue(
   1112                             Integer.toString(Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
   1113                     mButtonEnabledNetworks.setSummary(R.string.network_global);
   1114                 }
   1115                 break;
   1116             case Phone.NT_MODE_GSM_ONLY:
   1117                 if (!mIsGlobalCdma) {
   1118                     mButtonEnabledNetworks.setValue(
   1119                             Integer.toString(Phone.NT_MODE_GSM_ONLY));
   1120                     mButtonEnabledNetworks.setSummary(R.string.network_2G);
   1121                 } else {
   1122                     mButtonEnabledNetworks.setValue(
   1123                             Integer.toString(Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
   1124                     mButtonEnabledNetworks.setSummary(R.string.network_global);
   1125                 }
   1126                 break;
   1127             case Phone.NT_MODE_LTE_GSM_WCDMA:
   1128                 if (isWorldMode()) {
   1129                     mButtonEnabledNetworks.setSummary(
   1130                             R.string.preferred_network_mode_lte_gsm_umts_summary);
   1131                     controlCdmaOptions(false);
   1132                     controlGsmOptions(true);
   1133                     break;
   1134                 }
   1135             case Phone.NT_MODE_LTE_ONLY:
   1136             case Phone.NT_MODE_LTE_WCDMA:
   1137                 if (!mIsGlobalCdma) {
   1138                     mButtonEnabledNetworks.setValue(
   1139                             Integer.toString(Phone.NT_MODE_LTE_GSM_WCDMA));
   1140                     mButtonEnabledNetworks.setSummary((mShow4GForLTE == true)
   1141                             ? R.string.network_4G : R.string.network_lte);
   1142                 } else {
   1143                     mButtonEnabledNetworks.setValue(
   1144                             Integer.toString(Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
   1145                     mButtonEnabledNetworks.setSummary(R.string.network_global);
   1146                 }
   1147                 break;
   1148             case Phone.NT_MODE_LTE_CDMA_AND_EVDO:
   1149                 if (isWorldMode()) {
   1150                     mButtonEnabledNetworks.setSummary(
   1151                             R.string.preferred_network_mode_lte_cdma_summary);
   1152                     controlCdmaOptions(true);
   1153                     controlGsmOptions(false);
   1154                 } else {
   1155                     mButtonEnabledNetworks.setValue(
   1156                             Integer.toString(Phone.NT_MODE_LTE_CDMA_AND_EVDO));
   1157                     mButtonEnabledNetworks.setSummary(R.string.network_lte);
   1158                 }
   1159                 break;
   1160             case Phone.NT_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
   1161                 mButtonEnabledNetworks.setValue(
   1162                         Integer.toString(Phone.NT_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA));
   1163                 mButtonEnabledNetworks.setSummary(R.string.network_3G);
   1164                 break;
   1165             case Phone.NT_MODE_CDMA:
   1166             case Phone.NT_MODE_EVDO_NO_CDMA:
   1167             case Phone.NT_MODE_GLOBAL:
   1168                 mButtonEnabledNetworks.setValue(
   1169                         Integer.toString(Phone.NT_MODE_CDMA));
   1170                 mButtonEnabledNetworks.setSummary(R.string.network_3G);
   1171                 break;
   1172             case Phone.NT_MODE_CDMA_NO_EVDO:
   1173                 mButtonEnabledNetworks.setValue(
   1174                         Integer.toString(Phone.NT_MODE_CDMA_NO_EVDO));
   1175                 mButtonEnabledNetworks.setSummary(R.string.network_1x);
   1176                 break;
   1177             case Phone.NT_MODE_TDSCDMA_ONLY:
   1178                 mButtonEnabledNetworks.setValue(
   1179                         Integer.toString(Phone.NT_MODE_TDSCDMA_ONLY));
   1180                 mButtonEnabledNetworks.setSummary(R.string.network_3G);
   1181                 break;
   1182             case Phone.NT_MODE_LTE_TDSCDMA_GSM:
   1183             case Phone.NT_MODE_LTE_TDSCDMA_GSM_WCDMA:
   1184             case Phone.NT_MODE_LTE_TDSCDMA:
   1185             case Phone.NT_MODE_LTE_TDSCDMA_WCDMA:
   1186             case Phone.NT_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
   1187             case Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
   1188                 if (isSupportTdscdma()) {
   1189                     mButtonEnabledNetworks.setValue(
   1190                             Integer.toString(Phone.NT_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA));
   1191                     mButtonEnabledNetworks.setSummary(R.string.network_lte);
   1192                 } else {
   1193                     if (isWorldMode()) {
   1194                         controlCdmaOptions(true);
   1195                         controlGsmOptions(false);
   1196                     }
   1197                     mButtonEnabledNetworks.setValue(
   1198                             Integer.toString(Phone.NT_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
   1199                     if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA ||
   1200                             mIsGlobalCdma ||
   1201                             isWorldMode()) {
   1202                         mButtonEnabledNetworks.setSummary(R.string.network_global);
   1203                     } else {
   1204                         mButtonEnabledNetworks.setSummary((mShow4GForLTE == true)
   1205                                 ? R.string.network_4G : R.string.network_lte);
   1206                     }
   1207                 }
   1208                 break;
   1209             default:
   1210                 String errMsg = "Invalid Network Mode (" + NetworkMode + "). Ignore.";
   1211                 loge(errMsg);
   1212                 mButtonEnabledNetworks.setSummary(errMsg);
   1213         }
   1214     }
   1215 
   1216     @Override
   1217     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   1218         switch(requestCode) {
   1219         case REQUEST_CODE_EXIT_ECM:
   1220             Boolean isChoiceYes =
   1221                 data.getBooleanExtra(EmergencyCallbackModeExitDialog.EXTRA_EXIT_ECM_RESULT, false);
   1222             if (isChoiceYes) {
   1223                 // If the phone exits from ECM mode, show the CDMA Options
   1224                 mCdmaOptions.showDialog(mClickedPreference);
   1225             } else {
   1226                 // do nothing
   1227             }
   1228             break;
   1229 
   1230         default:
   1231             break;
   1232         }
   1233     }
   1234 
   1235     private static void log(String msg) {
   1236         Log.d(LOG_TAG, msg);
   1237     }
   1238 
   1239     private static void loge(String msg) {
   1240         Log.e(LOG_TAG, msg);
   1241     }
   1242 
   1243     @Override
   1244     public boolean onOptionsItemSelected(MenuItem item) {
   1245         final int itemId = item.getItemId();
   1246         if (itemId == android.R.id.home) {  // See ActionBar#setDisplayHomeAsUpEnabled()
   1247             // Commenting out "logical up" capability. This is a workaround for issue 5278083.
   1248             //
   1249             // Settings app may not launch this activity via UP_ACTIVITY_CLASS but the other
   1250             // Activity that looks exactly same as UP_ACTIVITY_CLASS ("SubSettings" Activity).
   1251             // At that moment, this Activity launches UP_ACTIVITY_CLASS on top of the Activity.
   1252             // which confuses users.
   1253             // TODO: introduce better mechanism for "up" capability here.
   1254             /*Intent intent = new Intent(Intent.ACTION_MAIN);
   1255             intent.setClassName(UP_ACTIVITY_PACKAGE, UP_ACTIVITY_CLASS);
   1256             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   1257             startActivity(intent);*/
   1258             finish();
   1259             return true;
   1260         }
   1261         return super.onOptionsItemSelected(item);
   1262     }
   1263 
   1264     private boolean isWorldMode() {
   1265         boolean worldModeOn = false;
   1266         final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
   1267         final String configString = getResources().getString(R.string.config_world_mode);
   1268 
   1269         if (!TextUtils.isEmpty(configString)) {
   1270             String[] configArray = configString.split(";");
   1271             // Check if we have World mode configuration set to True only or config is set to True
   1272             // and SIM GID value is also set and matches to the current SIM GID.
   1273             if (configArray != null &&
   1274                    ((configArray.length == 1 && configArray[0].equalsIgnoreCase("true")) ||
   1275                        (configArray.length == 2 && !TextUtils.isEmpty(configArray[1]) &&
   1276                            tm != null && configArray[1].equalsIgnoreCase(tm.getGroupIdLevel1())))) {
   1277                                worldModeOn = true;
   1278             }
   1279         }
   1280 
   1281         if (DBG) {
   1282             log("isWorldMode=" + worldModeOn);
   1283         }
   1284 
   1285         return worldModeOn;
   1286     }
   1287 
   1288     private void controlGsmOptions(boolean enable) {
   1289         PreferenceScreen prefSet = getPreferenceScreen();
   1290         if (prefSet == null) {
   1291             return;
   1292         }
   1293 
   1294         if (mGsmUmtsOptions == null) {
   1295             mGsmUmtsOptions = new GsmUmtsOptions(this, prefSet, mPhone.getSubId());
   1296         }
   1297         PreferenceScreen apnExpand =
   1298                 (PreferenceScreen) prefSet.findPreference(BUTTON_APN_EXPAND_KEY);
   1299         PreferenceScreen operatorSelectionExpand =
   1300                 (PreferenceScreen) prefSet.findPreference(BUTTON_OPERATOR_SELECTION_EXPAND_KEY);
   1301         PreferenceScreen carrierSettings =
   1302                 (PreferenceScreen) prefSet.findPreference(BUTTON_CARRIER_SETTINGS_KEY);
   1303         if (apnExpand != null) {
   1304             apnExpand.setEnabled(isWorldMode() || enable);
   1305         }
   1306         if (operatorSelectionExpand != null) {
   1307             if (enable) {
   1308                 operatorSelectionExpand.setEnabled(true);
   1309             } else {
   1310                 prefSet.removePreference(operatorSelectionExpand);
   1311             }
   1312         }
   1313         if (carrierSettings != null) {
   1314             prefSet.removePreference(carrierSettings);
   1315         }
   1316     }
   1317 
   1318     private void controlCdmaOptions(boolean enable) {
   1319         PreferenceScreen prefSet = getPreferenceScreen();
   1320         if (prefSet == null) {
   1321             return;
   1322         }
   1323         if (enable && mCdmaOptions == null) {
   1324             mCdmaOptions = new CdmaOptions(this, prefSet, mPhone);
   1325         }
   1326         CdmaSystemSelectListPreference systemSelect =
   1327                 (CdmaSystemSelectListPreference)prefSet.findPreference
   1328                         (BUTTON_CDMA_SYSTEM_SELECT_KEY);
   1329         if (systemSelect != null) {
   1330             systemSelect.setEnabled(enable);
   1331         }
   1332     }
   1333 
   1334     private boolean isSupportTdscdma() {
   1335         if (getResources().getBoolean(R.bool.config_support_tdscdma)) {
   1336             return true;
   1337         }
   1338 
   1339         String operatorNumeric = mPhone.getServiceState().getOperatorNumeric();
   1340         String[] numericArray = getResources().getStringArray(
   1341                 R.array.config_support_tdscdma_roaming_on_networks);
   1342         if (numericArray.length == 0 || operatorNumeric == null) {
   1343             return false;
   1344         }
   1345         for (String numeric : numericArray) {
   1346             if (operatorNumeric.equals(numeric)) {
   1347                 return true;
   1348             }
   1349         }
   1350         return false;
   1351     }
   1352 }
   1353