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