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.Intent;
20import android.os.PersistableBundle;
21import android.os.SystemProperties;
22import android.preference.Preference;
23import android.preference.PreferenceFragment;
24import android.preference.PreferenceScreen;
25import android.provider.Settings;
26import android.telephony.CarrierConfigManager;
27import android.text.TextUtils;
28
29import com.android.internal.telephony.Phone;
30
31/**
32 * List of Phone-specific settings screens.
33 */
34public class CdmaOptions {
35    private static final String LOG_TAG = "CdmaOptions";
36
37    private CdmaSystemSelectListPreference mButtonCdmaSystemSelect;
38    private CdmaSubscriptionListPreference mButtonCdmaSubscription;
39    private Preference mButtonAPNExpand;
40    private Preference mCategoryAPNExpand;
41    private Preference mButtonCarrierSettings;
42
43    private static final String BUTTON_CDMA_SYSTEM_SELECT_KEY = "cdma_system_select_key";
44    private static final String BUTTON_CDMA_SUBSCRIPTION_KEY = "cdma_subscription_key";
45    private static final String BUTTON_CARRIER_SETTINGS_KEY = "carrier_settings_key";
46    private static final String BUTTON_APN_EXPAND_KEY = "button_cdma_apn_key";
47    private static final String CATEGORY_APN_EXPAND_KEY = "category_cdma_apn_key";
48
49    private PreferenceFragment mPrefFragment;
50    private PreferenceScreen mPrefScreen;
51    private Phone mPhone;
52
53    public CdmaOptions(PreferenceFragment prefFragment, PreferenceScreen prefScreen, Phone phone) {
54        mPrefFragment = prefFragment;
55        mPrefScreen = prefScreen;
56        mPrefFragment.addPreferencesFromResource(R.xml.cdma_options);
57
58        // Initialize preferences.
59        mButtonCdmaSystemSelect = (CdmaSystemSelectListPreference) mPrefScreen
60                .findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY);
61        mButtonCdmaSubscription = (CdmaSubscriptionListPreference) mPrefScreen
62                .findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY);
63        mButtonCarrierSettings = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY);
64        mButtonAPNExpand = mPrefScreen.findPreference(BUTTON_APN_EXPAND_KEY);
65        mCategoryAPNExpand = mPrefScreen.findPreference(CATEGORY_APN_EXPAND_KEY);
66
67        update(phone);
68    }
69
70    // Unlike mPrefFragment or mPrefScreen, mPhone may change during lifecycle of CdmaOptions.
71    // For example, a new sim card is inserted. When that happens, we update CdmaOptions with new
72    // phone.
73    protected void update(Phone phone) {
74        mPhone = phone;
75
76        PersistableBundle carrierConfig =
77                PhoneGlobals.getInstance().getCarrierConfigForSubId(mPhone.getSubId());
78        // Some CDMA carriers want the APN settings.
79        boolean addAPNExpand =
80                carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL);
81        boolean addCdmaSubscription =
82                deviceSupportsNvAndRuim();
83        // Read platform settings for carrier settings
84        boolean addCarrierSettings =
85                carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL);
86
87        mPrefScreen.addPreference(mButtonCdmaSystemSelect);
88        mButtonCdmaSystemSelect.setEnabled(true);
89
90        // Making no assumptions of whether they are added or removed at this point.
91        // Calling add or remove explicitly to make sure they are updated.
92
93        if (addAPNExpand) {
94            mButtonAPNExpand.setOnPreferenceClickListener(
95                    new Preference.OnPreferenceClickListener() {
96                        @Override
97                        public boolean onPreferenceClick(Preference preference) {
98                            // We need to build the Intent by hand as the Preference Framework
99                            // does not allow to add an Intent with some extras into a Preference
100                            // XML file
101                            final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
102                            // This will setup the Home and Search affordance
103                            intent.putExtra(":settings:show_fragment_as_subsetting", true);
104                            intent.putExtra("sub_id", mPhone.getSubId());
105                            mPrefFragment.startActivity(intent);
106                            return true;
107                        }
108                    });
109            mPrefScreen.addPreference(mCategoryAPNExpand);
110        } else {
111            mPrefScreen.removePreference(mCategoryAPNExpand);
112        }
113
114        if (addCdmaSubscription) {
115            log("Both NV and Ruim supported, ENABLE subscription type selection");
116            mPrefScreen.addPreference(mButtonCdmaSubscription);
117            mButtonCdmaSubscription.setEnabled(true);
118        } else {
119            log("Both NV and Ruim NOT supported, REMOVE subscription type selection");
120            mPrefScreen.removePreference(mButtonCdmaSubscription);
121        }
122
123        if (addCarrierSettings) {
124            mPrefScreen.addPreference(mButtonCarrierSettings);
125        } else {
126            mPrefScreen.removePreference(mButtonCarrierSettings);
127        }
128    }
129
130    private boolean deviceSupportsNvAndRuim() {
131        // retrieve the list of subscription types supported by device.
132        String subscriptionsSupported = SystemProperties.get("ril.subscription.types");
133        boolean nvSupported = false;
134        boolean ruimSupported = false;
135
136        log("deviceSupportsnvAnRum: prop=" + subscriptionsSupported);
137        if (!TextUtils.isEmpty(subscriptionsSupported)) {
138            // Searches through the comma-separated list for a match for "NV"
139            // and "RUIM" to update nvSupported and ruimSupported.
140            for (String subscriptionType : subscriptionsSupported.split(",")) {
141                subscriptionType = subscriptionType.trim();
142                if (subscriptionType.equalsIgnoreCase("NV")) {
143                    nvSupported = true;
144                }
145                if (subscriptionType.equalsIgnoreCase("RUIM")) {
146                    ruimSupported = true;
147                }
148            }
149        }
150
151        log("deviceSupportsnvAnRum: nvSupported=" + nvSupported +
152                " ruimSupported=" + ruimSupported);
153        return (nvSupported && ruimSupported);
154    }
155
156    public boolean preferenceTreeClick(Preference preference) {
157        if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) {
158            log("preferenceTreeClick: return BUTTON_CDMA_ROAMING_KEY true");
159            return true;
160        }
161        if (preference.getKey().equals(BUTTON_CDMA_SUBSCRIPTION_KEY)) {
162            log("preferenceTreeClick: return CDMA_SUBSCRIPTION_KEY true");
163            return true;
164        }
165        return false;
166    }
167
168    public void showDialog(Preference preference) {
169        if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) {
170            mButtonCdmaSystemSelect.showDialog(null);
171        } else if (preference.getKey().equals(BUTTON_CDMA_SUBSCRIPTION_KEY)) {
172            mButtonCdmaSubscription.showDialog(null);
173        }
174    }
175
176    protected void log(String s) {
177        android.util.Log.d(LOG_TAG, s);
178    }
179}
180