blob: 39f05356ffb6893cdf335b4e12104b8ee6dc4508 [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
Robert Greenwalt3901edb2010-01-26 10:22:37 -080098 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070099 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800100 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800101 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800102
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700103 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800104 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
105 if (adapter != null) {
106 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
107 BluetoothProfile.PAN);
108 }
109
Jake Hamby436b29e2011-02-07 18:21:25 -0800110 CheckBoxPreference enableWifiAp =
111 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
112 Preference wifiApSettings = findPreference(WIFI_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800113 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700114 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc3c93e52010-03-15 17:21:16 -0700115 mTetherHelp = (PreferenceScreen) findPreference(TETHERING_HELP);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800116
117 ConnectivityManager cm =
118 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800119
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800120 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800121 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700122 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
123
Amith Yamasanie419bc12011-02-14 14:19:08 -0800124 final boolean usbAvailable = mUsbRegexs.length != 0;
125 final boolean wifiAvailable = mWifiRegexs.length != 0;
126 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700127
Danica Chang32711b62010-08-10 18:41:29 -0700128 if (!usbAvailable || Utils.isMonkeyRunning()) {
129 getPreferenceScreen().removePreference(mUsbTether);
130 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800131
Danica Chang32711b62010-08-10 18:41:29 -0700132 if (!wifiAvailable) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800133 getPreferenceScreen().removePreference(enableWifiAp);
134 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700135 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800136
Danica Chang32711b62010-08-10 18:41:29 -0700137 if (!bluetoothAvailable) {
138 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700139 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800140 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700141 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700142 } else {
143 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700144 }
145 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800146
Jake Hamby436b29e2011-02-07 18:21:25 -0800147 mWifiApEnabler = new WifiApEnabler(activity, enableWifiAp);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700148 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800149
150 initWifiTethering();
151 }
152
153 private void initWifiTethering() {
154 final Activity activity = getActivity();
155 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
156 mWifiConfig = mWifiManager.getWifiApConfiguration();
157 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
158
159 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
160 mEnableWifiAp = (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
161
162 if (mWifiConfig == null) {
163 final String s = activity.getString(
164 com.android.internal.R.string.wifi_tether_configure_ssid_default);
165 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
166 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
167 } else {
168 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
169 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
170 mWifiConfig.SSID,
171 mSecurityType[index]));
172 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800173 }
174
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800175 private BluetoothProfile.ServiceListener mProfileServiceListener =
176 new BluetoothProfile.ServiceListener() {
177 public void onServiceConnected(int profile, BluetoothProfile proxy) {
178 mBluetoothPan = (BluetoothPan) proxy;
179 }
180 public void onServiceDisconnected(int profile) {
181 mBluetoothPan = null;
182 }
183 };
184
Irfan Sheriff0b266962010-04-06 15:16:44 -0700185 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700186 public Dialog onCreateDialog(int id) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700187 if (id == DIALOG_TETHER_HELP) {
188 Locale locale = Locale.getDefault();
Irfan Sheriff0b266962010-04-06 15:16:44 -0700189
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700190 // check for the full language + country resource, if not there, try just language
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700191 final AssetManager am = getActivity().getAssets();
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700192 String path = HELP_PATH.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800193 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700194 boolean useCountry = true;
195 InputStream is = null;
196 try {
197 is = am.open(path);
Jake Hamby436b29e2011-02-07 18:21:25 -0800198 } catch (Exception ignored) {
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700199 useCountry = false;
200 } finally {
201 if (is != null) {
202 try {
203 is.close();
Jake Hamby436b29e2011-02-07 18:21:25 -0800204 } catch (Exception ignored) {}
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700205 }
206 }
207 String url = HELP_URL.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800208 url = url.replace("%z", useCountry ? '_'+locale.getCountry().toLowerCase() : "");
Irfan Sheriff0b266962010-04-06 15:16:44 -0700209 if ((mUsbRegexs.length != 0) && (mWifiRegexs.length == 0)) {
210 url = url.replace("%x", USB_HELP_MODIFIER);
211 } else if ((mWifiRegexs.length != 0) && (mUsbRegexs.length == 0)) {
212 url = url.replace("%x", WIFI_HELP_MODIFIER);
213 } else {
214 // could assert that both wifi and usb have regexs, but the default
215 // is to use this anyway so no check is needed
216 url = url.replace("%x", "");
217 }
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700218
Irfan Sheriff0b266962010-04-06 15:16:44 -0700219 mView.loadUrl(url);
Amith Yamasani476d7952011-01-21 09:28:31 -0800220 // Detach from old parent first
221 ViewParent parent = mView.getParent();
222 if (parent != null && parent instanceof ViewGroup) {
223 ((ViewGroup) parent).removeView(mView);
224 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700225 return new AlertDialog.Builder(getActivity())
Irfan Sheriff0b266962010-04-06 15:16:44 -0700226 .setCancelable(true)
227 .setTitle(R.string.tethering_help_button_text)
228 .setView(mView)
229 .create();
Amith Yamasani84a042c2011-03-02 11:25:04 -0800230 } else if (id == DIALOG_AP_SETTINGS) {
231 final Activity activity = getActivity();
232 mDialog = new WifiApDialog(activity, this, mWifiConfig);
233 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700234 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800235
Irfan Sheriff0b266962010-04-06 15:16:44 -0700236 return null;
237 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800238
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800239 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700240 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800241 public void onReceive(Context content, Intent intent) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800242 if (intent.getAction().equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
243 // TODO - this should understand the interface types
244 ArrayList<String> available = intent.getStringArrayListExtra(
245 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
246 ArrayList<String> active = intent.getStringArrayListExtra(
247 ConnectivityManager.EXTRA_ACTIVE_TETHER);
248 ArrayList<String> errored = intent.getStringArrayListExtra(
249 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700250 updateState(available.toArray(new String[available.size()]),
251 active.toArray(new String[active.size()]),
252 errored.toArray(new String[errored.size()]));
Robert Greenwalta2488762010-03-10 16:57:08 -0800253 } else if (intent.getAction().equals(Intent.ACTION_MEDIA_SHARED) ||
254 intent.getAction().equals(Intent.ACTION_MEDIA_UNSHARED)) {
255 updateState();
Danica Chang32711b62010-08-10 18:41:29 -0700256 } else if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
257 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800258 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800259 }
260 }
261
262 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700263 public void onStart() {
264 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800265
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700266 final Activity activity = getActivity();
267
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800268 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700269 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700270 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800271
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800272 filter = new IntentFilter();
273 filter.addAction(Intent.ACTION_MEDIA_SHARED);
274 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
275 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700276 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800277
Danica Chang32711b62010-08-10 18:41:29 -0700278 filter = new IntentFilter();
279 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700280 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700281
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700282 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Irfan Sheriff65cff172010-02-08 10:46:30 -0800283 mWifiApEnabler.resume();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800284 }
285
286 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700287 public void onStop() {
288 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700289 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800290 mTetherChangeReceiver = null;
Irfan Sheriff65cff172010-02-08 10:46:30 -0800291 mWifiApEnabler.pause();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800292 }
293
Robert Greenwalt209177a2010-03-04 13:29:02 -0800294 private void updateState() {
295 ConnectivityManager cm =
296 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
297
298 String[] available = cm.getTetherableIfaces();
299 String[] tethered = cm.getTetheredIfaces();
300 String[] errored = cm.getTetheringErroredIfaces();
301 updateState(available, tethered, errored);
302 }
303
Ben Clark0008d212010-07-27 16:20:59 +0100304 private void updateState(String[] available, String[] tethered,
305 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700306 updateUsbState(available, tethered, errored);
307 updateBluetoothState(available, tethered, errored);
308 }
309
310
311 private void updateUsbState(String[] available, String[] tethered,
312 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800313 ConnectivityManager cm =
314 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800315 boolean usbAvailable = false;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800316 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Mike Lockwood26dad3e2010-03-03 06:19:55 -0500317 boolean massStorageActive =
318 Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Ben Clark0008d212010-07-27 16:20:59 +0100319 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800320 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800321 if (s.matches(regex)) {
322 usbAvailable = true;
323 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
324 usbError = cm.getLastTetherError(s);
325 }
326 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800327 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800328 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800329 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100330 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800331 for (String regex : mUsbRegexs) {
332 if (s.matches(regex)) usbTethered = true;
333 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800334 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800335 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100336 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800337 for (String regex : mUsbRegexs) {
338 if (s.matches(regex)) usbErrored = true;
339 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800340 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800341
342 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800343 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
344 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800345 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800346 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800347 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
348 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
349 } else {
350 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
351 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800352 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800353 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800354 } else if (usbErrored) {
355 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
356 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800357 mUsbTether.setChecked(false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800358 } else if (massStorageActive) {
359 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
360 mUsbTether.setEnabled(false);
361 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800362 } else {
363 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
364 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800365 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800366 }
367 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800368
Danica Chang32711b62010-08-10 18:41:29 -0700369 private void updateBluetoothState(String[] available, String[] tethered,
370 String[] errored) {
371 ConnectivityManager cm =
372 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Danica Chang32711b62010-08-10 18:41:29 -0700373 int bluetoothError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Danica Chang32711b62010-08-10 18:41:29 -0700374 for (String s : available) {
375 for (String regex : mBluetoothRegexs) {
376 if (s.matches(regex)) {
Danica Chang32711b62010-08-10 18:41:29 -0700377 if (bluetoothError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
378 bluetoothError = cm.getLastTetherError(s);
379 }
380 }
381 }
382 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800383 boolean bluetoothTethered = false;
Danica Chang32711b62010-08-10 18:41:29 -0700384 for (String s : tethered) {
385 for (String regex : mBluetoothRegexs) {
386 if (s.matches(regex)) bluetoothTethered = true;
387 }
388 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800389 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700390 for (String s: errored) {
391 for (String regex : mBluetoothRegexs) {
392 if (s.matches(regex)) bluetoothErrored = true;
393 }
394 }
395
396 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
397 int btState = adapter.getState();
398 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
399 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700400 mBluetoothTether.setSummary(R.string.wifi_stopping);
401 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
402 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700403 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
404 } else if (mBluetoothPan.isTetheringOn()) {
405 mBluetoothTether.setChecked(true);
406 if (btState == BluetoothAdapter.STATE_ON) {
407 mBluetoothTether.setEnabled(true);
Danica Chang32711b62010-08-10 18:41:29 -0700408 if (bluetoothTethered) {
409 mBluetoothTether.setSummary(R.string.bluetooth_tethering_connected_subtext);
410 } else if (bluetoothErrored) {
411 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
412 } else {
413 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
414 }
415 }
416 } else {
417 mBluetoothTether.setEnabled(true);
418 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700419 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
420 }
421 }
422
Robert Greenwalt209177a2010-03-04 13:29:02 -0800423 @Override
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700424 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800425 if (preference == mUsbTether) {
426 boolean newState = mUsbTether.isChecked();
427
428 ConnectivityManager cm =
429 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
430
431 if (newState) {
432 String[] available = cm.getTetherableIfaces();
433
434 String usbIface = findIface(available, mUsbRegexs);
435 if (usbIface == null) {
436 updateState();
437 return true;
438 }
439 if (cm.tether(usbIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
440 mUsbTether.setChecked(false);
441 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
442 return true;
443 }
444 mUsbTether.setSummary("");
445 } else {
446 String [] tethered = cm.getTetheredIfaces();
447
448 String usbIface = findIface(tethered, mUsbRegexs);
449 if (usbIface == null) {
450 updateState();
451 return true;
452 }
453 if (cm.untether(usbIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
454 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
455 return true;
456 }
457 mUsbTether.setSummary("");
458 }
Danica Chang32711b62010-08-10 18:41:29 -0700459 } else if(preference == mBluetoothTether) {
460 boolean bluetoothTetherState = mBluetoothTether.isChecked();
Robert Greenwalt38925c02010-03-18 15:31:48 -0700461
Danica Chang32711b62010-08-10 18:41:29 -0700462 if (bluetoothTetherState) {
463 // turn on Bluetooth first
464 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
465 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
466 adapter.enable();
467 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
468 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700469 }
470
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700471 mBluetoothPan.setBluetoothTethering(true);
Danica Chang32711b62010-08-10 18:41:29 -0700472 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
473 } else {
474 boolean errored = false;
475
476 ConnectivityManager cm =
477 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
478 String [] tethered = cm.getTetheredIfaces();
479 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
480 if (bluetoothIface != null &&
481 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
482 errored = true;
483 }
484
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700485 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700486 if (errored) {
487 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
488 } else {
489 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
490 }
491 }
492 } else if (preference == mTetherHelp) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700493 showDialog(DIALOG_TETHER_HELP);
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700494 return true;
Amith Yamasani84a042c2011-03-02 11:25:04 -0800495 } else if (preference == mCreateNetwork) {
496 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800497 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700498
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700499 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800500 }
501
Jake Hamby436b29e2011-02-07 18:21:25 -0800502 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800503 for (String iface : ifaces) {
504 for (String regex : regexes) {
505 if (iface.matches(regex)) {
506 return iface;
507 }
508 }
509 }
510 return null;
511 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800512
513 public void onClick(DialogInterface dialogInterface, int button) {
514 if (button == DialogInterface.BUTTON_POSITIVE) {
515 mWifiConfig = mDialog.getConfig();
516 if (mWifiConfig != null) {
517 /**
518 * if soft AP is running, bring up with new config
519 * else update the configuration alone
520 */
521 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
522 mWifiManager.setWifiApEnabled(mWifiConfig, true);
523 /**
524 * There is no tether notification on changing AP
525 * configuration. Update status with new config.
526 */
527 mWifiApEnabler.updateConfigSummary(mWifiConfig);
528 } else {
529 mWifiManager.setWifiApConfiguration(mWifiConfig);
530 }
531 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
532 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
533 mWifiConfig.SSID,
534 mSecurityType[index]));
535 }
536 }
537 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800538}