blob: 99917250661ac82d044c56b434a71adfbe7574d8 [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;
Mike Lockwood69a09572011-07-19 13:30:03 -070034import android.hardware.usb.UsbManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080035import android.net.ConnectivityManager;
Amith Yamasani84a042c2011-03-02 11:25:04 -080036import android.net.wifi.WifiConfiguration;
37import android.net.wifi.WifiManager;
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070038import android.os.Bundle;
Mike Lockwood26dad3e2010-03-03 06:19:55 -050039import android.os.Environment;
Robert Greenwalt209177a2010-03-04 13:29:02 -080040import android.preference.CheckBoxPreference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080041import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080042import android.preference.PreferenceScreen;
Amith Yamasani476d7952011-01-21 09:28:31 -080043import android.view.ViewGroup;
44import android.view.ViewParent;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070045import android.webkit.WebView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080046
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070047import java.io.InputStream;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080048import java.util.ArrayList;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070049import java.util.Locale;
Irfan Sheriff65cff172010-02-08 10:46:30 -080050
Robert Greenwalt3901edb2010-01-26 10:22:37 -080051/*
52 * Displays preferences for Tethering.
53 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080054public class TetherSettings extends SettingsPreferenceFragment
55 implements DialogInterface.OnClickListener {
Danica Chang32711b62010-08-10 18:41:29 -070056
Robert Greenwalt3901edb2010-01-26 10:22:37 -080057 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080058 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
59 private static final String WIFI_AP_SETTINGS = "wifi_ap_settings";
Danica Chang32711b62010-08-10 18:41:29 -070060 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070061 private static final String TETHERING_HELP = "tethering_help";
Robert Greenwalt38925c02010-03-18 15:31:48 -070062 private static final String USB_HELP_MODIFIER = "usb_";
63 private static final String WIFI_HELP_MODIFIER = "wifi_";
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070064 private static final String HELP_URL = "file:///android_asset/html/%y%z/tethering_%xhelp.html";
65 private static final String HELP_PATH = "html/%y%z/tethering_help.html";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080066
Irfan Sheriff0b266962010-04-06 15:16:44 -070067 private static final int DIALOG_TETHER_HELP = 1;
Amith Yamasani84a042c2011-03-02 11:25:04 -080068 private static final int DIALOG_AP_SETTINGS = 2;
Irfan Sheriff0b266962010-04-06 15:16:44 -070069
70 private WebView mView;
Robert Greenwalt209177a2010-03-04 13:29:02 -080071 private CheckBoxPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080072
Irfan Sheriff65cff172010-02-08 10:46:30 -080073 private WifiApEnabler mWifiApEnabler;
Danica Chang32711b62010-08-10 18:41:29 -070074
75 private CheckBoxPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070076
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070077 private PreferenceScreen mTetherHelp;
Irfan Sheriff65cff172010-02-08 10:46:30 -080078
Robert Greenwalt3901edb2010-01-26 10:22:37 -080079 private BroadcastReceiver mTetherChangeReceiver;
80
Robert Greenwaltc4764d22010-02-12 14:21:37 -080081 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080082
83 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080084
Danica Chang32711b62010-08-10 18:41:29 -070085 private String[] mBluetoothRegexs;
86 private BluetoothPan mBluetoothPan;
87
Amith Yamasani84a042c2011-03-02 11:25:04 -080088 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
89 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
90
91 private String[] mSecurityType;
92 private Preference mCreateNetwork;
93 private CheckBoxPreference mEnableWifiAp;
94
95 private WifiApDialog mDialog;
96 private WifiManager mWifiManager;
97 private WifiConfiguration mWifiConfig = null;
98
Mike Lockwood69a09572011-07-19 13:30:03 -070099 private boolean mUsbConnected;
100 private boolean mMassStorageActive;
101
Jake Hambyc777ee22011-03-04 15:37:39 -0800102 private boolean mBluetoothEnableForTether;
103
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800104 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700105 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800106 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800107 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800108
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700109 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800110 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
111 if (adapter != null) {
112 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
113 BluetoothProfile.PAN);
114 }
115
Jake Hamby436b29e2011-02-07 18:21:25 -0800116 CheckBoxPreference enableWifiAp =
117 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
118 Preference wifiApSettings = findPreference(WIFI_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800119 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700120 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc3c93e52010-03-15 17:21:16 -0700121 mTetherHelp = (PreferenceScreen) findPreference(TETHERING_HELP);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800122
123 ConnectivityManager cm =
124 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800125
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800126 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800127 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700128 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
129
Amith Yamasanie419bc12011-02-14 14:19:08 -0800130 final boolean usbAvailable = mUsbRegexs.length != 0;
131 final boolean wifiAvailable = mWifiRegexs.length != 0;
132 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700133
Danica Chang32711b62010-08-10 18:41:29 -0700134 if (!usbAvailable || Utils.isMonkeyRunning()) {
135 getPreferenceScreen().removePreference(mUsbTether);
136 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800137
Danica Chang32711b62010-08-10 18:41:29 -0700138 if (!wifiAvailable) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800139 getPreferenceScreen().removePreference(enableWifiAp);
140 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700141 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800142
Danica Chang32711b62010-08-10 18:41:29 -0700143 if (!bluetoothAvailable) {
144 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700145 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800146 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700147 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700148 } else {
149 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700150 }
151 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800152
Jake Hamby436b29e2011-02-07 18:21:25 -0800153 mWifiApEnabler = new WifiApEnabler(activity, enableWifiAp);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700154 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800155
156 initWifiTethering();
157 }
158
159 private void initWifiTethering() {
160 final Activity activity = getActivity();
161 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
162 mWifiConfig = mWifiManager.getWifiApConfiguration();
163 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
164
165 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
166 mEnableWifiAp = (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
167
168 if (mWifiConfig == null) {
169 final String s = activity.getString(
170 com.android.internal.R.string.wifi_tether_configure_ssid_default);
171 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
172 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
173 } else {
174 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
175 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
176 mWifiConfig.SSID,
177 mSecurityType[index]));
178 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800179 }
180
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800181 private BluetoothProfile.ServiceListener mProfileServiceListener =
182 new BluetoothProfile.ServiceListener() {
183 public void onServiceConnected(int profile, BluetoothProfile proxy) {
184 mBluetoothPan = (BluetoothPan) proxy;
185 }
186 public void onServiceDisconnected(int profile) {
187 mBluetoothPan = null;
188 }
189 };
190
Irfan Sheriff0b266962010-04-06 15:16:44 -0700191 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700192 public Dialog onCreateDialog(int id) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700193 if (id == DIALOG_TETHER_HELP) {
194 Locale locale = Locale.getDefault();
Irfan Sheriff0b266962010-04-06 15:16:44 -0700195
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700196 // check for the full language + country resource, if not there, try just language
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700197 final AssetManager am = getActivity().getAssets();
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700198 String path = HELP_PATH.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800199 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700200 boolean useCountry = true;
201 InputStream is = null;
202 try {
203 is = am.open(path);
Jake Hamby436b29e2011-02-07 18:21:25 -0800204 } catch (Exception ignored) {
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700205 useCountry = false;
206 } finally {
207 if (is != null) {
208 try {
209 is.close();
Jake Hamby436b29e2011-02-07 18:21:25 -0800210 } catch (Exception ignored) {}
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700211 }
212 }
213 String url = HELP_URL.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800214 url = url.replace("%z", useCountry ? '_'+locale.getCountry().toLowerCase() : "");
Irfan Sheriff0b266962010-04-06 15:16:44 -0700215 if ((mUsbRegexs.length != 0) && (mWifiRegexs.length == 0)) {
216 url = url.replace("%x", USB_HELP_MODIFIER);
217 } else if ((mWifiRegexs.length != 0) && (mUsbRegexs.length == 0)) {
218 url = url.replace("%x", WIFI_HELP_MODIFIER);
219 } else {
220 // could assert that both wifi and usb have regexs, but the default
221 // is to use this anyway so no check is needed
222 url = url.replace("%x", "");
223 }
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700224
Irfan Sheriff0b266962010-04-06 15:16:44 -0700225 mView.loadUrl(url);
Amith Yamasani476d7952011-01-21 09:28:31 -0800226 // Detach from old parent first
227 ViewParent parent = mView.getParent();
228 if (parent != null && parent instanceof ViewGroup) {
229 ((ViewGroup) parent).removeView(mView);
230 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700231 return new AlertDialog.Builder(getActivity())
Irfan Sheriff0b266962010-04-06 15:16:44 -0700232 .setCancelable(true)
233 .setTitle(R.string.tethering_help_button_text)
234 .setView(mView)
235 .create();
Amith Yamasani84a042c2011-03-02 11:25:04 -0800236 } else if (id == DIALOG_AP_SETTINGS) {
237 final Activity activity = getActivity();
238 mDialog = new WifiApDialog(activity, this, mWifiConfig);
239 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700240 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800241
Irfan Sheriff0b266962010-04-06 15:16:44 -0700242 return null;
243 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800244
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800245 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700246 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800247 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800248 String action = intent.getAction();
249 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800250 // TODO - this should understand the interface types
251 ArrayList<String> available = intent.getStringArrayListExtra(
252 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
253 ArrayList<String> active = intent.getStringArrayListExtra(
254 ConnectivityManager.EXTRA_ACTIVE_TETHER);
255 ArrayList<String> errored = intent.getStringArrayListExtra(
256 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700257 updateState(available.toArray(new String[available.size()]),
258 active.toArray(new String[active.size()]),
259 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700260 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
261 mMassStorageActive = true;
262 updateState();
263 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
264 mMassStorageActive = false;
265 updateState();
266 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
267 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800268 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800269 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
270 if (mBluetoothEnableForTether) {
271 switch (intent
272 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
273 case BluetoothAdapter.STATE_ON:
274 mBluetoothPan.setBluetoothTethering(true);
275 mBluetoothEnableForTether = false;
276 break;
277
278 case BluetoothAdapter.STATE_OFF:
279 case BluetoothAdapter.ERROR:
280 mBluetoothEnableForTether = false;
281 break;
282
283 default:
284 // ignore transition states
285 }
286 }
Danica Chang32711b62010-08-10 18:41:29 -0700287 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800288 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800289 }
290 }
291
292 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700293 public void onStart() {
294 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800295
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700296 final Activity activity = getActivity();
297
Mike Lockwood69a09572011-07-19 13:30:03 -0700298 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800299 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700300 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700301 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800302
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800303 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700304 filter.addAction(UsbManager.ACTION_USB_STATE);
305 activity.registerReceiver(mTetherChangeReceiver, filter);
306
307 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800308 filter.addAction(Intent.ACTION_MEDIA_SHARED);
309 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
310 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700311 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800312
Danica Chang32711b62010-08-10 18:41:29 -0700313 filter = new IntentFilter();
314 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700315 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700316
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700317 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Irfan Sheriff65cff172010-02-08 10:46:30 -0800318 mWifiApEnabler.resume();
Jake Hambyc777ee22011-03-04 15:37:39 -0800319
320 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800321 }
322
323 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700324 public void onStop() {
325 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700326 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800327 mTetherChangeReceiver = null;
Irfan Sheriff65cff172010-02-08 10:46:30 -0800328 mWifiApEnabler.pause();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800329 }
330
Robert Greenwalt209177a2010-03-04 13:29:02 -0800331 private void updateState() {
332 ConnectivityManager cm =
333 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
334
335 String[] available = cm.getTetherableIfaces();
336 String[] tethered = cm.getTetheredIfaces();
337 String[] errored = cm.getTetheringErroredIfaces();
338 updateState(available, tethered, errored);
339 }
340
Ben Clark0008d212010-07-27 16:20:59 +0100341 private void updateState(String[] available, String[] tethered,
342 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700343 updateUsbState(available, tethered, errored);
344 updateBluetoothState(available, tethered, errored);
345 }
346
347
348 private void updateUsbState(String[] available, String[] tethered,
349 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800350 ConnectivityManager cm =
351 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700352 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800353 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100354 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800355 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800356 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800357 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
358 usbError = cm.getLastTetherError(s);
359 }
360 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800361 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800362 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800363 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100364 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800365 for (String regex : mUsbRegexs) {
366 if (s.matches(regex)) usbTethered = true;
367 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800368 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800369 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100370 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800371 for (String regex : mUsbRegexs) {
372 if (s.matches(regex)) usbErrored = true;
373 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800374 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800375
376 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800377 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
378 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800379 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800380 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800381 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
382 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
383 } else {
384 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
385 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800386 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800387 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800388 } else if (usbErrored) {
389 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
390 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800391 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700392 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800393 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
394 mUsbTether.setEnabled(false);
395 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800396 } else {
397 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
398 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800399 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800400 }
401 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800402
Danica Chang32711b62010-08-10 18:41:29 -0700403 private void updateBluetoothState(String[] available, String[] tethered,
404 String[] errored) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800405 int bluetoothTethered = 0;
Danica Chang32711b62010-08-10 18:41:29 -0700406 for (String s : tethered) {
407 for (String regex : mBluetoothRegexs) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800408 if (s.matches(regex)) bluetoothTethered++;
Danica Chang32711b62010-08-10 18:41:29 -0700409 }
410 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800411 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700412 for (String s: errored) {
413 for (String regex : mBluetoothRegexs) {
414 if (s.matches(regex)) bluetoothErrored = true;
415 }
416 }
417
418 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
419 int btState = adapter.getState();
420 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
421 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700422 mBluetoothTether.setSummary(R.string.wifi_stopping);
423 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
424 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700425 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Jake Hambyc777ee22011-03-04 15:37:39 -0800426 } else if (btState == BluetoothAdapter.STATE_ON && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700427 mBluetoothTether.setChecked(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800428 mBluetoothTether.setEnabled(true);
429 if (bluetoothTethered > 1) {
430 String summary = getString(
431 R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
432 mBluetoothTether.setSummary(summary);
433 } else if (bluetoothTethered == 1) {
434 mBluetoothTether.setSummary(R.string.bluetooth_tethering_device_connected_subtext);
435 } else if (bluetoothErrored) {
436 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
437 } else {
438 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700439 }
440 } else {
441 mBluetoothTether.setEnabled(true);
442 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700443 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
444 }
445 }
446
Robert Greenwalt209177a2010-03-04 13:29:02 -0800447 @Override
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700448 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
Mike Lockwood69a09572011-07-19 13:30:03 -0700449 ConnectivityManager cm =
450 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
451
Robert Greenwalt209177a2010-03-04 13:29:02 -0800452 if (preference == mUsbTether) {
453 boolean newState = mUsbTether.isChecked();
454
Mike Lockwood69a09572011-07-19 13:30:03 -0700455 if (cm.setUsbTethering(newState) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
456 mUsbTether.setChecked(false);
457 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
458 return true;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800459 }
Mike Lockwood69a09572011-07-19 13:30:03 -0700460 mUsbTether.setSummary("");
Jake Hambyc777ee22011-03-04 15:37:39 -0800461 } else if (preference == mBluetoothTether) {
Danica Chang32711b62010-08-10 18:41:29 -0700462 boolean bluetoothTetherState = mBluetoothTether.isChecked();
Robert Greenwalt38925c02010-03-18 15:31:48 -0700463
Danica Chang32711b62010-08-10 18:41:29 -0700464 if (bluetoothTetherState) {
465 // turn on Bluetooth first
466 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
467 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800468 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700469 adapter.enable();
470 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
471 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800472 } else {
473 mBluetoothPan.setBluetoothTethering(true);
474 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700475 }
Danica Chang32711b62010-08-10 18:41:29 -0700476 } else {
477 boolean errored = false;
478
Danica Chang32711b62010-08-10 18:41:29 -0700479 String [] tethered = cm.getTetheredIfaces();
480 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
481 if (bluetoothIface != null &&
482 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
483 errored = true;
484 }
485
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700486 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700487 if (errored) {
488 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
489 } else {
490 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
491 }
492 }
493 } else if (preference == mTetherHelp) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700494 showDialog(DIALOG_TETHER_HELP);
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700495 return true;
Amith Yamasani84a042c2011-03-02 11:25:04 -0800496 } else if (preference == mCreateNetwork) {
497 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800498 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700499
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700500 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800501 }
502
Jake Hamby436b29e2011-02-07 18:21:25 -0800503 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800504 for (String iface : ifaces) {
505 for (String regex : regexes) {
506 if (iface.matches(regex)) {
507 return iface;
508 }
509 }
510 }
511 return null;
512 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800513
514 public void onClick(DialogInterface dialogInterface, int button) {
515 if (button == DialogInterface.BUTTON_POSITIVE) {
516 mWifiConfig = mDialog.getConfig();
517 if (mWifiConfig != null) {
518 /**
519 * if soft AP is running, bring up with new config
520 * else update the configuration alone
521 */
522 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
523 mWifiManager.setWifiApEnabled(mWifiConfig, true);
524 /**
525 * There is no tether notification on changing AP
526 * configuration. Update status with new config.
527 */
528 mWifiApEnabler.updateConfigSummary(mWifiConfig);
529 } else {
530 mWifiManager.setWifiApConfiguration(mWifiConfig);
531 }
532 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
533 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
534 mWifiConfig.SSID,
535 mSecurityType[index]));
536 }
537 }
538 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800539}