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