blob: 1513d43cf917e74c035ea0cc4240ea9c2763bbd5 [file] [log] [blame]
Robert Greenwalt3901edb2010-01-26 10:22:37 -08001/*
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.settings;
18
Irfan Sheriff65cff172010-02-08 10:46:30 -080019import com.android.settings.wifi.WifiApEnabler;
Amith Yamasani84a042c2011-03-02 11:25:04 -080020import com.android.settings.wifi.WifiApDialog;
Irfan Sheriff65cff172010-02-08 10:46:30 -080021
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070022import android.app.Activity;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070023import android.app.AlertDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -070024import android.app.Dialog;
Danica Chang32711b62010-08-10 18:41:29 -070025import android.bluetooth.BluetoothAdapter;
26import android.bluetooth.BluetoothPan;
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -080027import android.bluetooth.BluetoothProfile;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080028import android.content.BroadcastReceiver;
29import android.content.Context;
Amith Yamasani84a042c2011-03-02 11:25:04 -080030import android.content.DialogInterface;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080031import android.content.Intent;
32import android.content.IntentFilter;
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070033import android.content.res.AssetManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080034import android.net.ConnectivityManager;
Amith Yamasani84a042c2011-03-02 11:25:04 -080035import android.net.wifi.WifiConfiguration;
36import android.net.wifi.WifiManager;
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070037import android.os.Bundle;
Mike Lockwood26dad3e2010-03-03 06:19:55 -050038import android.os.Environment;
Robert Greenwalt209177a2010-03-04 13:29:02 -080039import android.preference.CheckBoxPreference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080040import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080041import android.preference.PreferenceScreen;
Amith Yamasani476d7952011-01-21 09:28:31 -080042import android.view.ViewGroup;
43import android.view.ViewParent;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070044import android.webkit.WebView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080045
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070046import java.io.InputStream;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080047import java.util.ArrayList;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070048import java.util.Locale;
Irfan Sheriff65cff172010-02-08 10:46:30 -080049
Robert Greenwalt3901edb2010-01-26 10:22:37 -080050/*
51 * Displays preferences for Tethering.
52 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080053public class TetherSettings extends SettingsPreferenceFragment
54 implements DialogInterface.OnClickListener {
Danica Chang32711b62010-08-10 18:41:29 -070055
Robert Greenwalt3901edb2010-01-26 10:22:37 -080056 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080057 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
58 private static final String WIFI_AP_SETTINGS = "wifi_ap_settings";
Danica Chang32711b62010-08-10 18:41:29 -070059 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070060 private static final String TETHERING_HELP = "tethering_help";
Robert Greenwalt38925c02010-03-18 15:31:48 -070061 private static final String USB_HELP_MODIFIER = "usb_";
62 private static final String WIFI_HELP_MODIFIER = "wifi_";
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070063 private static final String HELP_URL = "file:///android_asset/html/%y%z/tethering_%xhelp.html";
64 private static final String HELP_PATH = "html/%y%z/tethering_help.html";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080065
Irfan Sheriff0b266962010-04-06 15:16:44 -070066 private static final int DIALOG_TETHER_HELP = 1;
Amith Yamasani84a042c2011-03-02 11:25:04 -080067 private static final int DIALOG_AP_SETTINGS = 2;
Irfan Sheriff0b266962010-04-06 15:16:44 -070068
69 private WebView mView;
Robert Greenwalt209177a2010-03-04 13:29:02 -080070 private CheckBoxPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080071
Irfan Sheriff65cff172010-02-08 10:46:30 -080072 private WifiApEnabler mWifiApEnabler;
Danica Chang32711b62010-08-10 18:41:29 -070073
74 private CheckBoxPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070075
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070076 private PreferenceScreen mTetherHelp;
Irfan Sheriff65cff172010-02-08 10:46:30 -080077
Robert Greenwalt3901edb2010-01-26 10:22:37 -080078 private BroadcastReceiver mTetherChangeReceiver;
79
Robert Greenwaltc4764d22010-02-12 14:21:37 -080080 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080081
82 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080083
Danica Chang32711b62010-08-10 18:41:29 -070084 private String[] mBluetoothRegexs;
85 private BluetoothPan mBluetoothPan;
86
Amith Yamasani84a042c2011-03-02 11:25:04 -080087 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
88 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
89
90 private String[] mSecurityType;
91 private Preference mCreateNetwork;
92 private CheckBoxPreference mEnableWifiAp;
93
94 private WifiApDialog mDialog;
95 private WifiManager mWifiManager;
96 private WifiConfiguration mWifiConfig = null;
97
Jake Hambyc777ee22011-03-04 15:37:39 -080098 private boolean mBluetoothEnableForTether;
99
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800100 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700101 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800102 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800103 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800104
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700105 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800106 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
107 if (adapter != null) {
108 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
109 BluetoothProfile.PAN);
110 }
111
Jake Hamby436b29e2011-02-07 18:21:25 -0800112 CheckBoxPreference enableWifiAp =
113 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
114 Preference wifiApSettings = findPreference(WIFI_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800115 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700116 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc3c93e52010-03-15 17:21:16 -0700117 mTetherHelp = (PreferenceScreen) findPreference(TETHERING_HELP);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800118
119 ConnectivityManager cm =
120 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800121
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800122 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800123 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700124 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
125
Amith Yamasanie419bc12011-02-14 14:19:08 -0800126 final boolean usbAvailable = mUsbRegexs.length != 0;
127 final boolean wifiAvailable = mWifiRegexs.length != 0;
128 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700129
Danica Chang32711b62010-08-10 18:41:29 -0700130 if (!usbAvailable || Utils.isMonkeyRunning()) {
131 getPreferenceScreen().removePreference(mUsbTether);
132 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800133
Danica Chang32711b62010-08-10 18:41:29 -0700134 if (!wifiAvailable) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800135 getPreferenceScreen().removePreference(enableWifiAp);
136 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700137 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800138
Danica Chang32711b62010-08-10 18:41:29 -0700139 if (!bluetoothAvailable) {
140 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700141 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800142 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700143 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700144 } else {
145 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700146 }
147 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800148
Jake Hamby436b29e2011-02-07 18:21:25 -0800149 mWifiApEnabler = new WifiApEnabler(activity, enableWifiAp);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700150 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800151
152 initWifiTethering();
153 }
154
155 private void initWifiTethering() {
156 final Activity activity = getActivity();
157 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
158 mWifiConfig = mWifiManager.getWifiApConfiguration();
159 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
160
161 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
162 mEnableWifiAp = (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
163
164 if (mWifiConfig == null) {
165 final String s = activity.getString(
166 com.android.internal.R.string.wifi_tether_configure_ssid_default);
167 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
168 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
169 } else {
170 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
171 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
172 mWifiConfig.SSID,
173 mSecurityType[index]));
174 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800175 }
176
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800177 private BluetoothProfile.ServiceListener mProfileServiceListener =
178 new BluetoothProfile.ServiceListener() {
179 public void onServiceConnected(int profile, BluetoothProfile proxy) {
180 mBluetoothPan = (BluetoothPan) proxy;
181 }
182 public void onServiceDisconnected(int profile) {
183 mBluetoothPan = null;
184 }
185 };
186
Irfan Sheriff0b266962010-04-06 15:16:44 -0700187 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700188 public Dialog onCreateDialog(int id) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700189 if (id == DIALOG_TETHER_HELP) {
190 Locale locale = Locale.getDefault();
Irfan Sheriff0b266962010-04-06 15:16:44 -0700191
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700192 // check for the full language + country resource, if not there, try just language
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700193 final AssetManager am = getActivity().getAssets();
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700194 String path = HELP_PATH.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800195 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700196 boolean useCountry = true;
197 InputStream is = null;
198 try {
199 is = am.open(path);
Jake Hamby436b29e2011-02-07 18:21:25 -0800200 } catch (Exception ignored) {
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700201 useCountry = false;
202 } finally {
203 if (is != null) {
204 try {
205 is.close();
Jake Hamby436b29e2011-02-07 18:21:25 -0800206 } catch (Exception ignored) {}
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700207 }
208 }
209 String url = HELP_URL.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800210 url = url.replace("%z", useCountry ? '_'+locale.getCountry().toLowerCase() : "");
Irfan Sheriff0b266962010-04-06 15:16:44 -0700211 if ((mUsbRegexs.length != 0) && (mWifiRegexs.length == 0)) {
212 url = url.replace("%x", USB_HELP_MODIFIER);
213 } else if ((mWifiRegexs.length != 0) && (mUsbRegexs.length == 0)) {
214 url = url.replace("%x", WIFI_HELP_MODIFIER);
215 } else {
216 // could assert that both wifi and usb have regexs, but the default
217 // is to use this anyway so no check is needed
218 url = url.replace("%x", "");
219 }
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700220
Irfan Sheriff0b266962010-04-06 15:16:44 -0700221 mView.loadUrl(url);
Amith Yamasani476d7952011-01-21 09:28:31 -0800222 // Detach from old parent first
223 ViewParent parent = mView.getParent();
224 if (parent != null && parent instanceof ViewGroup) {
225 ((ViewGroup) parent).removeView(mView);
226 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700227 return new AlertDialog.Builder(getActivity())
Irfan Sheriff0b266962010-04-06 15:16:44 -0700228 .setCancelable(true)
229 .setTitle(R.string.tethering_help_button_text)
230 .setView(mView)
231 .create();
Amith Yamasani84a042c2011-03-02 11:25:04 -0800232 } else if (id == DIALOG_AP_SETTINGS) {
233 final Activity activity = getActivity();
234 mDialog = new WifiApDialog(activity, this, mWifiConfig);
235 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700236 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800237
Irfan Sheriff0b266962010-04-06 15:16:44 -0700238 return null;
239 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800240
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800241 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700242 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800243 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800244 String action = intent.getAction();
245 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800246 // TODO - this should understand the interface types
247 ArrayList<String> available = intent.getStringArrayListExtra(
248 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
249 ArrayList<String> active = intent.getStringArrayListExtra(
250 ConnectivityManager.EXTRA_ACTIVE_TETHER);
251 ArrayList<String> errored = intent.getStringArrayListExtra(
252 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700253 updateState(available.toArray(new String[available.size()]),
254 active.toArray(new String[active.size()]),
255 errored.toArray(new String[errored.size()]));
Jake Hambyc777ee22011-03-04 15:37:39 -0800256 } else if (action.equals(Intent.ACTION_MEDIA_SHARED) ||
257 action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800258 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800259 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
260 if (mBluetoothEnableForTether) {
261 switch (intent
262 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
263 case BluetoothAdapter.STATE_ON:
264 mBluetoothPan.setBluetoothTethering(true);
265 mBluetoothEnableForTether = false;
266 break;
267
268 case BluetoothAdapter.STATE_OFF:
269 case BluetoothAdapter.ERROR:
270 mBluetoothEnableForTether = false;
271 break;
272
273 default:
274 // ignore transition states
275 }
276 }
Danica Chang32711b62010-08-10 18:41:29 -0700277 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800278 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800279 }
280 }
281
282 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700283 public void onStart() {
284 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800285
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700286 final Activity activity = getActivity();
287
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800288 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700289 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700290 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800291
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800292 filter = new IntentFilter();
293 filter.addAction(Intent.ACTION_MEDIA_SHARED);
294 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
295 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700296 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800297
Danica Chang32711b62010-08-10 18:41:29 -0700298 filter = new IntentFilter();
299 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700300 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700301
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700302 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Irfan Sheriff65cff172010-02-08 10:46:30 -0800303 mWifiApEnabler.resume();
Jake Hambyc777ee22011-03-04 15:37:39 -0800304
305 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800306 }
307
308 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700309 public void onStop() {
310 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700311 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800312 mTetherChangeReceiver = null;
Irfan Sheriff65cff172010-02-08 10:46:30 -0800313 mWifiApEnabler.pause();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800314 }
315
Robert Greenwalt209177a2010-03-04 13:29:02 -0800316 private void updateState() {
317 ConnectivityManager cm =
318 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
319
320 String[] available = cm.getTetherableIfaces();
321 String[] tethered = cm.getTetheredIfaces();
322 String[] errored = cm.getTetheringErroredIfaces();
323 updateState(available, tethered, errored);
324 }
325
Ben Clark0008d212010-07-27 16:20:59 +0100326 private void updateState(String[] available, String[] tethered,
327 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700328 updateUsbState(available, tethered, errored);
329 updateBluetoothState(available, tethered, errored);
330 }
331
332
333 private void updateUsbState(String[] available, String[] tethered,
334 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800335 ConnectivityManager cm =
336 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800337 boolean usbAvailable = false;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800338 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Mike Lockwood26dad3e2010-03-03 06:19:55 -0500339 boolean massStorageActive =
340 Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Ben Clark0008d212010-07-27 16:20:59 +0100341 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800342 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800343 if (s.matches(regex)) {
344 usbAvailable = true;
345 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
346 usbError = cm.getLastTetherError(s);
347 }
348 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800349 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800350 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800351 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100352 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800353 for (String regex : mUsbRegexs) {
354 if (s.matches(regex)) usbTethered = true;
355 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800356 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800357 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100358 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800359 for (String regex : mUsbRegexs) {
360 if (s.matches(regex)) usbErrored = true;
361 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800362 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800363
364 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800365 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
366 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800367 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800368 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800369 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
370 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
371 } else {
372 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
373 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800374 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800375 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800376 } else if (usbErrored) {
377 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
378 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800379 mUsbTether.setChecked(false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800380 } else if (massStorageActive) {
381 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
382 mUsbTether.setEnabled(false);
383 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800384 } else {
385 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
386 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800387 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800388 }
389 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800390
Danica Chang32711b62010-08-10 18:41:29 -0700391 private void updateBluetoothState(String[] available, String[] tethered,
392 String[] errored) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800393 int bluetoothTethered = 0;
Danica Chang32711b62010-08-10 18:41:29 -0700394 for (String s : tethered) {
395 for (String regex : mBluetoothRegexs) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800396 if (s.matches(regex)) bluetoothTethered++;
Danica Chang32711b62010-08-10 18:41:29 -0700397 }
398 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800399 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700400 for (String s: errored) {
401 for (String regex : mBluetoothRegexs) {
402 if (s.matches(regex)) bluetoothErrored = true;
403 }
404 }
405
406 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
407 int btState = adapter.getState();
408 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
409 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700410 mBluetoothTether.setSummary(R.string.wifi_stopping);
411 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
412 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700413 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Jake Hambyc777ee22011-03-04 15:37:39 -0800414 } else if (btState == BluetoothAdapter.STATE_ON && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700415 mBluetoothTether.setChecked(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800416 mBluetoothTether.setEnabled(true);
417 if (bluetoothTethered > 1) {
418 String summary = getString(
419 R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
420 mBluetoothTether.setSummary(summary);
421 } else if (bluetoothTethered == 1) {
422 mBluetoothTether.setSummary(R.string.bluetooth_tethering_device_connected_subtext);
423 } else if (bluetoothErrored) {
424 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
425 } else {
426 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700427 }
428 } else {
429 mBluetoothTether.setEnabled(true);
430 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700431 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
432 }
433 }
434
Robert Greenwalt209177a2010-03-04 13:29:02 -0800435 @Override
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700436 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800437 if (preference == mUsbTether) {
438 boolean newState = mUsbTether.isChecked();
439
440 ConnectivityManager cm =
441 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
442
443 if (newState) {
444 String[] available = cm.getTetherableIfaces();
445
446 String usbIface = findIface(available, mUsbRegexs);
447 if (usbIface == null) {
448 updateState();
449 return true;
450 }
451 if (cm.tether(usbIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
452 mUsbTether.setChecked(false);
453 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
454 return true;
455 }
456 mUsbTether.setSummary("");
457 } else {
458 String [] tethered = cm.getTetheredIfaces();
459
460 String usbIface = findIface(tethered, mUsbRegexs);
461 if (usbIface == null) {
462 updateState();
463 return true;
464 }
465 if (cm.untether(usbIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
466 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
467 return true;
468 }
469 mUsbTether.setSummary("");
470 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800471 } else if (preference == mBluetoothTether) {
Danica Chang32711b62010-08-10 18:41:29 -0700472 boolean bluetoothTetherState = mBluetoothTether.isChecked();
Robert Greenwalt38925c02010-03-18 15:31:48 -0700473
Danica Chang32711b62010-08-10 18:41:29 -0700474 if (bluetoothTetherState) {
475 // turn on Bluetooth first
476 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
477 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800478 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700479 adapter.enable();
480 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
481 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800482 } else {
483 mBluetoothPan.setBluetoothTethering(true);
484 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700485 }
Danica Chang32711b62010-08-10 18:41:29 -0700486 } else {
487 boolean errored = false;
488
489 ConnectivityManager cm =
490 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
491 String [] tethered = cm.getTetheredIfaces();
492 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
493 if (bluetoothIface != null &&
494 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
495 errored = true;
496 }
497
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700498 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700499 if (errored) {
500 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
501 } else {
502 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
503 }
504 }
505 } else if (preference == mTetherHelp) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700506 showDialog(DIALOG_TETHER_HELP);
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700507 return true;
Amith Yamasani84a042c2011-03-02 11:25:04 -0800508 } else if (preference == mCreateNetwork) {
509 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800510 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700511
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700512 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800513 }
514
Jake Hamby436b29e2011-02-07 18:21:25 -0800515 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800516 for (String iface : ifaces) {
517 for (String regex : regexes) {
518 if (iface.matches(regex)) {
519 return iface;
520 }
521 }
522 }
523 return null;
524 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800525
526 public void onClick(DialogInterface dialogInterface, int button) {
527 if (button == DialogInterface.BUTTON_POSITIVE) {
528 mWifiConfig = mDialog.getConfig();
529 if (mWifiConfig != null) {
530 /**
531 * if soft AP is running, bring up with new config
532 * else update the configuration alone
533 */
534 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
535 mWifiManager.setWifiApEnabled(mWifiConfig, true);
536 /**
537 * There is no tether notification on changing AP
538 * configuration. Update status with new config.
539 */
540 mWifiApEnabler.updateConfigSummary(mWifiConfig);
541 } else {
542 mWifiManager.setWifiApConfiguration(mWifiConfig);
543 }
544 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
545 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
546 mWifiConfig.SSID,
547 mSecurityType[index]));
548 }
549 }
550 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800551}