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