Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2008 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 android.content.Intent;
     20 import android.os.PersistableBundle;
     21 import android.preference.Preference;
     22 import android.preference.PreferenceFragment;
     23 import android.preference.PreferenceScreen;
     24 import android.provider.Settings;
     25 import android.telephony.CarrierConfigManager;
     26 
     27 import com.android.internal.logging.MetricsLogger;
     28 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     29 import com.android.internal.telephony.Phone;
     30 import com.android.internal.telephony.PhoneConstants;
     31 import com.android.settingslib.RestrictedLockUtils;
     32 
     33 /**
     34  * List of Network-specific settings screens.
     35  */
     36 public class GsmUmtsOptions {
     37     private static final String LOG_TAG = "GsmUmtsOptions";
     38 
     39     private RestrictedPreference mButtonAPNExpand;
     40     private Preference mCategoryAPNExpand;
     41     Preference mCarrierSettingPref;
     42 
     43     private NetworkOperators mNetworkOperator;
     44 
     45     private static final String BUTTON_APN_EXPAND_KEY = "button_gsm_apn_key";
     46     private static final String CATEGORY_APN_EXPAND_KEY = "category_gsm_apn_key";
     47     private static final String BUTTON_CARRIER_SETTINGS_KEY = "carrier_settings_key";
     48 
     49     public static final String EXTRA_SUB_ID = "sub_id";
     50     private PreferenceFragment mPrefFragment;
     51     private PreferenceScreen mPrefScreen;
     52 
     53     public GsmUmtsOptions(PreferenceFragment prefFragment, PreferenceScreen prefScreen,
     54             final int subId, INetworkQueryService queryService) {
     55         mPrefFragment = prefFragment;
     56         mPrefScreen = prefScreen;
     57         mPrefFragment.addPreferencesFromResource(R.xml.gsm_umts_options);
     58         mButtonAPNExpand = (RestrictedPreference) mPrefScreen.findPreference(BUTTON_APN_EXPAND_KEY);
     59         mCategoryAPNExpand = mPrefScreen.findPreference(CATEGORY_APN_EXPAND_KEY);
     60         mNetworkOperator = (NetworkOperators) mPrefScreen
     61                 .findPreference(NetworkOperators.CATEGORY_NETWORK_OPERATORS_KEY);
     62         mCarrierSettingPref = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY);
     63 
     64         mNetworkOperator.initialize();
     65 
     66         update(subId, queryService);
     67     }
     68 
     69     // Unlike mPrefFragment or mPrefScreen, subId or queryService may change during lifecycle of
     70     // GsmUmtsOptions. When that happens, we update GsmUmtsOptions with new parameters.
     71     protected void update(final int subId, INetworkQueryService queryService) {
     72         boolean addAPNExpand = true;
     73         boolean addNetworkOperatorsCategory = true;
     74         boolean addCarrierSettings = true;
     75         Phone phone = PhoneGlobals.getPhone(subId);
     76         if (phone == null) return;
     77         if (phone.getPhoneType() != PhoneConstants.PHONE_TYPE_GSM) {
     78             log("Not a GSM phone");
     79             addAPNExpand = false;
     80             mNetworkOperator.setEnabled(false);
     81         } else {
     82             log("Not a CDMA phone");
     83             PersistableBundle carrierConfig =
     84                     PhoneGlobals.getInstance().getCarrierConfigForSubId(subId);
     85 
     86             // Determine which options to display. For GSM these are defaulted to true in
     87             // CarrierConfigManager, but they maybe overriden by DefaultCarrierConfigService or a
     88             // carrier app.
     89             // Note: these settings used to be controlled with overlays in
     90             // Telephony/res/values/config.xml
     91             if (!carrierConfig.getBoolean(CarrierConfigManager.KEY_APN_EXPAND_BOOL)
     92                     && mCategoryAPNExpand != null) {
     93                 addAPNExpand = false;
     94             }
     95             if (!carrierConfig.getBoolean(
     96                     CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL)) {
     97                 addNetworkOperatorsCategory = false;
     98             }
     99 
    100             if (carrierConfig.getBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL)) {
    101                 if (phone.isCspPlmnEnabled()) {
    102                     log("[CSP] Enabling Operator Selection menu.");
    103                     mNetworkOperator.setEnabled(true);
    104                 } else {
    105                     log("[CSP] Disabling Operator Selection menu.");
    106                     addNetworkOperatorsCategory = false;
    107                 }
    108             }
    109 
    110             // Read platform settings for carrier settings
    111             addCarrierSettings = carrierConfig.getBoolean(
    112                     CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL);
    113         }
    114 
    115         // Making no assumptions of whether they are added or removed at this point.
    116         // Calling add or remove explicitly to make sure they are updated.
    117 
    118         if (addAPNExpand) {
    119             log("update: addAPNExpand");
    120             mButtonAPNExpand.setDisabledByAdmin(
    121                     MobileNetworkSettings.isDpcApnEnforced(mButtonAPNExpand.getContext())
    122                             ? RestrictedLockUtils.getDeviceOwner(mButtonAPNExpand.getContext())
    123                             : null);
    124             mButtonAPNExpand.setOnPreferenceClickListener(
    125                     new Preference.OnPreferenceClickListener() {
    126                         @Override
    127                         public boolean onPreferenceClick(Preference preference) {
    128                             MetricsLogger.action(mButtonAPNExpand.getContext(),
    129                                     MetricsEvent.ACTION_MOBILE_NETWORK_APN_SETTINGS);
    130                             // We need to build the Intent by hand as the Preference Framework
    131                             // does not allow to add an Intent with some extras into a Preference
    132                             // XML file
    133                             final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
    134                             // This will setup the Home and Search affordance
    135                             intent.putExtra(":settings:show_fragment_as_subsetting", true);
    136                             intent.putExtra(EXTRA_SUB_ID, subId);
    137                             mPrefFragment.startActivity(intent);
    138                             return true;
    139                         }
    140                     });
    141             mPrefScreen.addPreference(mCategoryAPNExpand);
    142         } else {
    143             mPrefScreen.removePreference(mCategoryAPNExpand);
    144         }
    145 
    146         if (addNetworkOperatorsCategory) {
    147             mPrefScreen.addPreference(mNetworkOperator);
    148             mNetworkOperator.update(subId, queryService);
    149         } else {
    150             mPrefScreen.removePreference(mNetworkOperator);
    151         }
    152 
    153         if (addCarrierSettings) {
    154             mPrefScreen.addPreference(mCarrierSettingPref);
    155         } else {
    156             mPrefScreen.removePreference(mCarrierSettingPref);
    157         }
    158 
    159     }
    160 
    161     protected boolean preferenceTreeClick(Preference preference) {
    162         return mNetworkOperator.preferenceTreeClick(preference);
    163     }
    164 
    165     protected void log(String s) {
    166         android.util.Log.d(LOG_TAG, s);
    167     }
    168 }
    169