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.os.SystemProperties;
20import android.preference.Preference;
21import android.preference.PreferenceActivity;
22import android.preference.PreferenceScreen;
23import android.text.TextUtils;
24
25/**
26 * List of Phone-specific settings screens.
27 */
28public class CdmaOptions {
29    private static final String LOG_TAG = "CdmaOptions";
30
31    private CdmaSystemSelectListPreference mButtonCdmaSystemSelect;
32    private CdmaSubscriptionListPreference mButtonCdmaSubscription;
33
34    private static final String BUTTON_CDMA_SYSTEM_SELECT_KEY = "cdma_system_select_key";
35    private static final String BUTTON_CDMA_SUBSCRIPTION_KEY = "cdma_subscription_key";
36
37    private PreferenceActivity mPrefActivity;
38    private PreferenceScreen mPrefScreen;
39
40    public CdmaOptions(PreferenceActivity prefActivity, PreferenceScreen prefScreen) {
41        mPrefActivity = prefActivity;
42        mPrefScreen = prefScreen;
43        create();
44    }
45
46    protected void create() {
47        mPrefActivity.addPreferencesFromResource(R.xml.cdma_options);
48
49        mButtonCdmaSystemSelect = (CdmaSystemSelectListPreference)mPrefScreen
50                .findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY);
51
52        mButtonCdmaSubscription = (CdmaSubscriptionListPreference)mPrefScreen
53                .findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY);
54
55        mButtonCdmaSystemSelect.setEnabled(true);
56        if(deviceSupportsNvAndRuim()) {
57            log("Both NV and Ruim supported, ENABLE subscription type selection");
58            mButtonCdmaSubscription.setEnabled(true);
59        } else {
60            log("Both NV and Ruim NOT supported, REMOVE subscription type selection");
61            mPrefScreen.removePreference(mPrefScreen
62                                .findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY));
63        }
64    }
65
66    private boolean deviceSupportsNvAndRuim() {
67        // retrieve the list of subscription types supported by device.
68        String subscriptionsSupported = SystemProperties.get("ril.subscription.types");
69        boolean nvSupported = false;
70        boolean ruimSupported = false;
71
72        log("deviceSupportsnvAnRum: prop=" + subscriptionsSupported);
73        if (!TextUtils.isEmpty(subscriptionsSupported)) {
74            // Searches through the comma-separated list for a match for "NV"
75            // and "RUIM" to update nvSupported and ruimSupported.
76            for (String subscriptionType : subscriptionsSupported.split(",")) {
77                subscriptionType = subscriptionType.trim();
78                if (subscriptionType.equalsIgnoreCase("NV")) {
79                    nvSupported = true;
80                }
81                if (subscriptionType.equalsIgnoreCase("RUIM")) {
82                    ruimSupported = true;
83                }
84            }
85        }
86
87        log("deviceSupportsnvAnRum: nvSupported=" + nvSupported +
88                " ruimSupported=" + ruimSupported);
89        return (nvSupported && ruimSupported);
90    }
91
92    public boolean preferenceTreeClick(Preference preference) {
93        if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) {
94            log("preferenceTreeClick: return BUTTON_CDMA_ROAMING_KEY true");
95            return true;
96        }
97        if (preference.getKey().equals(BUTTON_CDMA_SUBSCRIPTION_KEY)) {
98            log("preferenceTreeClick: return CDMA_SUBSCRIPTION_KEY true");
99            return true;
100        }
101        return false;
102    }
103
104    public void showDialog(Preference preference) {
105        if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) {
106            mButtonCdmaSystemSelect.showDialog(null);
107        } else if (preference.getKey().equals(BUTTON_CDMA_SUBSCRIPTION_KEY)) {
108            mButtonCdmaSubscription.showDialog(null);
109        }
110    }
111
112    protected void log(String s) {
113        android.util.Log.d(LOG_TAG, s);
114    }
115}
116