1/*
2 * Copyright (C) 2009 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.settings;
18
19import android.bluetooth.BluetoothAdapter;
20import android.content.Context;
21import android.content.Intent;
22import android.net.ConnectivityManager;
23import android.net.wifi.WifiManager;
24import android.os.Bundle;
25import android.os.ServiceManager;
26import android.os.SystemProperties;
27import android.preference.CheckBoxPreference;
28import android.preference.Preference;
29import android.preference.PreferenceActivity;
30import android.preference.PreferenceScreen;
31import android.provider.Settings;
32import android.util.Log;
33
34import com.android.internal.telephony.TelephonyIntents;
35import com.android.internal.telephony.TelephonyProperties;
36import com.android.settings.bluetooth.BluetoothEnabler;
37import com.android.settings.wifi.WifiEnabler;
38
39public class WirelessSettings extends PreferenceActivity {
40
41    private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
42    private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
43    private static final String KEY_TOGGLE_WIFI = "toggle_wifi";
44    private static final String KEY_WIFI_SETTINGS = "wifi_settings";
45    private static final String KEY_BT_SETTINGS = "bt_settings";
46    private static final String KEY_VPN_SETTINGS = "vpn_settings";
47    private static final String KEY_TETHER_SETTINGS = "tether_settings";
48    public static final String EXIT_ECM_RESULT = "exit_ecm_result";
49    public static final int REQUEST_CODE_EXIT_ECM = 1;
50
51    private AirplaneModeEnabler mAirplaneModeEnabler;
52    private CheckBoxPreference mAirplaneModePreference;
53    private WifiEnabler mWifiEnabler;
54    private BluetoothEnabler mBtEnabler;
55
56    /**
57     * Invoked on each preference click in this hierarchy, overrides
58     * PreferenceActivity's implementation.  Used to make sure we track the
59     * preference click events.
60     */
61    @Override
62    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
63        if (preference == mAirplaneModePreference && Boolean.parseBoolean(
64                SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
65            // In ECM mode launch ECM app dialog
66            startActivityForResult(
67                new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
68                REQUEST_CODE_EXIT_ECM);
69            return true;
70        }
71        // Let the intents be launched by the Preference manager
72        return false;
73    }
74
75    public static boolean isRadioAllowed(Context context, String type) {
76        if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
77            return true;
78        }
79        // Here we use the same logic in onCreate().
80        String toggleable = Settings.System.getString(context.getContentResolver(),
81                Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
82        return toggleable != null && toggleable.contains(type);
83    }
84
85    @Override
86    protected void onCreate(Bundle savedInstanceState) {
87        super.onCreate(savedInstanceState);
88
89        addPreferencesFromResource(R.xml.wireless_settings);
90
91        CheckBoxPreference airplane = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
92        CheckBoxPreference wifi = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI);
93        CheckBoxPreference bt = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH);
94
95        mAirplaneModeEnabler = new AirplaneModeEnabler(this, airplane);
96        mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
97        mWifiEnabler = new WifiEnabler(this, wifi);
98        mBtEnabler = new BluetoothEnabler(this, bt);
99
100        String toggleable = Settings.System.getString(getContentResolver(),
101                Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
102
103        // Manually set dependencies for Wifi when not toggleable.
104        if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
105            wifi.setDependency(KEY_TOGGLE_AIRPLANE);
106            findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
107            findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
108        }
109
110        // Manually set dependencies for Bluetooth when not toggleable.
111        if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
112            bt.setDependency(KEY_TOGGLE_AIRPLANE);
113            findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
114        }
115
116        // Disable Bluetooth Settings if Bluetooth service is not available.
117        if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
118            findPreference(KEY_BT_SETTINGS).setEnabled(false);
119        }
120
121        // Disable Tethering if it's not allowed
122        ConnectivityManager cm =
123                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
124        if (!cm.isTetheringSupported()) {
125            getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
126        } else {
127            String[] usbRegexs = cm.getTetherableUsbRegexs();
128            String[] wifiRegexs = cm.getTetherableWifiRegexs();
129            Preference p = findPreference(KEY_TETHER_SETTINGS);
130            if (wifiRegexs.length == 0) {
131                p.setTitle(R.string.tether_settings_title_usb);
132                p.setSummary(R.string.tether_settings_summary_usb);
133            } else {
134                if (usbRegexs.length == 0) {
135                    p.setTitle(R.string.tether_settings_title_wifi);
136                    p.setSummary(R.string.tether_settings_summary_wifi);
137                } else {
138                    p.setTitle(R.string.tether_settings_title_both);
139                    p.setSummary(R.string.tether_settings_summary_both);
140                }
141            }
142        }
143    }
144
145    @Override
146    protected void onResume() {
147        super.onResume();
148
149        mAirplaneModeEnabler.resume();
150        mWifiEnabler.resume();
151        mBtEnabler.resume();
152    }
153
154    @Override
155    protected void onPause() {
156        super.onPause();
157
158        mAirplaneModeEnabler.pause();
159        mWifiEnabler.pause();
160        mBtEnabler.pause();
161    }
162
163    @Override
164    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
165        if (requestCode == REQUEST_CODE_EXIT_ECM) {
166            Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
167            // Set Airplane mode based on the return value and checkbox state
168            mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
169                    mAirplaneModePreference.isChecked());
170        }
171    }
172}
173