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