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