blob: 3ecde80796a2a820d3dfc2eda2f6923695f88f46 [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";
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
Mike Lockwood69a09572011-07-19 13:30:03 -070098 private boolean mUsbConnected;
99 private boolean mMassStorageActive;
100
Jake Hambyc777ee22011-03-04 15:37:39 -0800101 private boolean mBluetoothEnableForTether;
102
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800103 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700104 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800105 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800106 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800107
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700108 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800109 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
110 if (adapter != null) {
111 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
112 BluetoothProfile.PAN);
113 }
114
Jake Hamby436b29e2011-02-07 18:21:25 -0800115 CheckBoxPreference enableWifiAp =
116 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700117 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800118 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700119 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc3c93e52010-03-15 17:21:16 -0700120 mTetherHelp = (PreferenceScreen) findPreference(TETHERING_HELP);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800121
122 ConnectivityManager cm =
123 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800124
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800125 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800126 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700127 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
128
Amith Yamasanie419bc12011-02-14 14:19:08 -0800129 final boolean usbAvailable = mUsbRegexs.length != 0;
130 final boolean wifiAvailable = mWifiRegexs.length != 0;
131 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700132
Danica Chang32711b62010-08-10 18:41:29 -0700133 if (!usbAvailable || Utils.isMonkeyRunning()) {
134 getPreferenceScreen().removePreference(mUsbTether);
135 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800136
Amith Yamasani0f474652011-08-30 17:30:01 -0700137 if (wifiAvailable) {
138 mWifiApEnabler = new WifiApEnabler(activity, enableWifiAp);
139 initWifiTethering();
140 } else {
Jake Hamby436b29e2011-02-07 18:21:25 -0800141 getPreferenceScreen().removePreference(enableWifiAp);
142 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700143 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800144
Danica Chang32711b62010-08-10 18:41:29 -0700145 if (!bluetoothAvailable) {
146 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700147 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800148 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700149 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700150 } else {
151 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700152 }
153 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800154
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700155 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800156 }
157
158 private void initWifiTethering() {
159 final Activity activity = getActivity();
160 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
161 mWifiConfig = mWifiManager.getWifiApConfiguration();
162 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
163
164 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
165 mEnableWifiAp = (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
166
167 if (mWifiConfig == null) {
168 final String s = activity.getString(
169 com.android.internal.R.string.wifi_tether_configure_ssid_default);
170 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
171 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
172 } else {
173 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
174 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
175 mWifiConfig.SSID,
176 mSecurityType[index]));
177 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800178 }
179
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800180 private BluetoothProfile.ServiceListener mProfileServiceListener =
181 new BluetoothProfile.ServiceListener() {
182 public void onServiceConnected(int profile, BluetoothProfile proxy) {
183 mBluetoothPan = (BluetoothPan) proxy;
184 }
185 public void onServiceDisconnected(int profile) {
186 mBluetoothPan = null;
187 }
188 };
189
Irfan Sheriff0b266962010-04-06 15:16:44 -0700190 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700191 public Dialog onCreateDialog(int id) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700192 if (id == DIALOG_TETHER_HELP) {
193 Locale locale = Locale.getDefault();
Irfan Sheriff0b266962010-04-06 15:16:44 -0700194
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700195 // check for the full language + country resource, if not there, try just language
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700196 final AssetManager am = getActivity().getAssets();
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700197 String path = HELP_PATH.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800198 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700199 boolean useCountry = true;
200 InputStream is = null;
201 try {
202 is = am.open(path);
Jake Hamby436b29e2011-02-07 18:21:25 -0800203 } catch (Exception ignored) {
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700204 useCountry = false;
205 } finally {
206 if (is != null) {
207 try {
208 is.close();
Jake Hamby436b29e2011-02-07 18:21:25 -0800209 } catch (Exception ignored) {}
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700210 }
211 }
212 String url = HELP_URL.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800213 url = url.replace("%z", useCountry ? '_'+locale.getCountry().toLowerCase() : "");
Irfan Sheriff0b266962010-04-06 15:16:44 -0700214 if ((mUsbRegexs.length != 0) && (mWifiRegexs.length == 0)) {
215 url = url.replace("%x", USB_HELP_MODIFIER);
216 } else if ((mWifiRegexs.length != 0) && (mUsbRegexs.length == 0)) {
217 url = url.replace("%x", WIFI_HELP_MODIFIER);
218 } else {
219 // could assert that both wifi and usb have regexs, but the default
220 // is to use this anyway so no check is needed
221 url = url.replace("%x", "");
222 }
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700223
Irfan Sheriff0b266962010-04-06 15:16:44 -0700224 mView.loadUrl(url);
Amith Yamasani476d7952011-01-21 09:28:31 -0800225 // Detach from old parent first
226 ViewParent parent = mView.getParent();
227 if (parent != null && parent instanceof ViewGroup) {
228 ((ViewGroup) parent).removeView(mView);
229 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700230 return new AlertDialog.Builder(getActivity())
Irfan Sheriff0b266962010-04-06 15:16:44 -0700231 .setCancelable(true)
232 .setTitle(R.string.tethering_help_button_text)
233 .setView(mView)
234 .create();
Amith Yamasani84a042c2011-03-02 11:25:04 -0800235 } else if (id == DIALOG_AP_SETTINGS) {
236 final Activity activity = getActivity();
237 mDialog = new WifiApDialog(activity, this, mWifiConfig);
238 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700239 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800240
Irfan Sheriff0b266962010-04-06 15:16:44 -0700241 return null;
242 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800243
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800244 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700245 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800246 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800247 String action = intent.getAction();
248 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800249 // TODO - this should understand the interface types
250 ArrayList<String> available = intent.getStringArrayListExtra(
251 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
252 ArrayList<String> active = intent.getStringArrayListExtra(
253 ConnectivityManager.EXTRA_ACTIVE_TETHER);
254 ArrayList<String> errored = intent.getStringArrayListExtra(
255 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700256 updateState(available.toArray(new String[available.size()]),
257 active.toArray(new String[active.size()]),
258 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700259 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
260 mMassStorageActive = true;
261 updateState();
262 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
263 mMassStorageActive = false;
264 updateState();
265 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
266 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800267 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800268 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
269 if (mBluetoothEnableForTether) {
270 switch (intent
271 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
272 case BluetoothAdapter.STATE_ON:
273 mBluetoothPan.setBluetoothTethering(true);
274 mBluetoothEnableForTether = false;
275 break;
276
277 case BluetoothAdapter.STATE_OFF:
278 case BluetoothAdapter.ERROR:
279 mBluetoothEnableForTether = false;
280 break;
281
282 default:
283 // ignore transition states
284 }
285 }
Danica Chang32711b62010-08-10 18:41:29 -0700286 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800287 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800288 }
289 }
290
291 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700292 public void onStart() {
293 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800294
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700295 final Activity activity = getActivity();
296
Mike Lockwood69a09572011-07-19 13:30:03 -0700297 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800298 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700299 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700300 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800301
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800302 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700303 filter.addAction(UsbManager.ACTION_USB_STATE);
304 activity.registerReceiver(mTetherChangeReceiver, filter);
305
306 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800307 filter.addAction(Intent.ACTION_MEDIA_SHARED);
308 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
309 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700310 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800311
Danica Chang32711b62010-08-10 18:41:29 -0700312 filter = new IntentFilter();
313 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700314 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700315
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700316 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700317 if (mWifiApEnabler != null) {
318 mWifiApEnabler.resume();
319 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800320
321 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800322 }
323
324 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700325 public void onStop() {
326 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700327 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800328 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700329 if (mWifiApEnabler != null) {
330 mWifiApEnabler.pause();
331 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800332 }
333
Robert Greenwalt209177a2010-03-04 13:29:02 -0800334 private void updateState() {
335 ConnectivityManager cm =
336 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
337
338 String[] available = cm.getTetherableIfaces();
339 String[] tethered = cm.getTetheredIfaces();
340 String[] errored = cm.getTetheringErroredIfaces();
341 updateState(available, tethered, errored);
342 }
343
Ben Clark0008d212010-07-27 16:20:59 +0100344 private void updateState(String[] available, String[] tethered,
345 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700346 updateUsbState(available, tethered, errored);
347 updateBluetoothState(available, tethered, errored);
348 }
349
350
351 private void updateUsbState(String[] available, String[] tethered,
352 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800353 ConnectivityManager cm =
354 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700355 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800356 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100357 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800358 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800359 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800360 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
361 usbError = cm.getLastTetherError(s);
362 }
363 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800364 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800365 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800366 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100367 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800368 for (String regex : mUsbRegexs) {
369 if (s.matches(regex)) usbTethered = true;
370 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800371 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800372 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100373 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800374 for (String regex : mUsbRegexs) {
375 if (s.matches(regex)) usbErrored = true;
376 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800377 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800378
379 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800380 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
381 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800382 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800383 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800384 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
385 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
386 } else {
387 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
388 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800389 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800390 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800391 } else if (usbErrored) {
392 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
393 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800394 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700395 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800396 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
397 mUsbTether.setEnabled(false);
398 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800399 } else {
400 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
401 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800402 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800403 }
404 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800405
Danica Chang32711b62010-08-10 18:41:29 -0700406 private void updateBluetoothState(String[] available, String[] tethered,
407 String[] errored) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800408 int bluetoothTethered = 0;
Danica Chang32711b62010-08-10 18:41:29 -0700409 for (String s : tethered) {
410 for (String regex : mBluetoothRegexs) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800411 if (s.matches(regex)) bluetoothTethered++;
Danica Chang32711b62010-08-10 18:41:29 -0700412 }
413 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800414 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700415 for (String s: errored) {
416 for (String regex : mBluetoothRegexs) {
417 if (s.matches(regex)) bluetoothErrored = true;
418 }
419 }
420
421 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
422 int btState = adapter.getState();
423 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
424 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700425 mBluetoothTether.setSummary(R.string.wifi_stopping);
426 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
427 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700428 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Jake Hambyc777ee22011-03-04 15:37:39 -0800429 } else if (btState == BluetoothAdapter.STATE_ON && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700430 mBluetoothTether.setChecked(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800431 mBluetoothTether.setEnabled(true);
432 if (bluetoothTethered > 1) {
433 String summary = getString(
434 R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
435 mBluetoothTether.setSummary(summary);
436 } else if (bluetoothTethered == 1) {
437 mBluetoothTether.setSummary(R.string.bluetooth_tethering_device_connected_subtext);
438 } else if (bluetoothErrored) {
439 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
440 } else {
441 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700442 }
443 } else {
444 mBluetoothTether.setEnabled(true);
445 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700446 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
447 }
448 }
449
Robert Greenwalt209177a2010-03-04 13:29:02 -0800450 @Override
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700451 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
Mike Lockwood69a09572011-07-19 13:30:03 -0700452 ConnectivityManager cm =
453 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
454
Robert Greenwalt209177a2010-03-04 13:29:02 -0800455 if (preference == mUsbTether) {
456 boolean newState = mUsbTether.isChecked();
457
Mike Lockwood69a09572011-07-19 13:30:03 -0700458 if (cm.setUsbTethering(newState) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
459 mUsbTether.setChecked(false);
460 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
461 return true;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800462 }
Mike Lockwood69a09572011-07-19 13:30:03 -0700463 mUsbTether.setSummary("");
Jake Hambyc777ee22011-03-04 15:37:39 -0800464 } else if (preference == mBluetoothTether) {
Danica Chang32711b62010-08-10 18:41:29 -0700465 boolean bluetoothTetherState = mBluetoothTether.isChecked();
Robert Greenwalt38925c02010-03-18 15:31:48 -0700466
Danica Chang32711b62010-08-10 18:41:29 -0700467 if (bluetoothTetherState) {
468 // turn on Bluetooth first
469 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
470 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800471 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700472 adapter.enable();
473 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
474 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800475 } else {
476 mBluetoothPan.setBluetoothTethering(true);
477 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700478 }
Danica Chang32711b62010-08-10 18:41:29 -0700479 } else {
480 boolean errored = false;
481
Danica Chang32711b62010-08-10 18:41:29 -0700482 String [] tethered = cm.getTetheredIfaces();
483 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
484 if (bluetoothIface != null &&
485 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
486 errored = true;
487 }
488
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700489 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700490 if (errored) {
491 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
492 } else {
493 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
494 }
495 }
496 } else if (preference == mTetherHelp) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700497 showDialog(DIALOG_TETHER_HELP);
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700498 return true;
Amith Yamasani84a042c2011-03-02 11:25:04 -0800499 } else if (preference == mCreateNetwork) {
500 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800501 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700502
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700503 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800504 }
505
Jake Hamby436b29e2011-02-07 18:21:25 -0800506 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800507 for (String iface : ifaces) {
508 for (String regex : regexes) {
509 if (iface.matches(regex)) {
510 return iface;
511 }
512 }
513 }
514 return null;
515 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800516
517 public void onClick(DialogInterface dialogInterface, int button) {
518 if (button == DialogInterface.BUTTON_POSITIVE) {
519 mWifiConfig = mDialog.getConfig();
520 if (mWifiConfig != null) {
521 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700522 * if soft AP is stopped, bring up
523 * else restart with new config
524 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800525 */
526 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700527 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800528 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800529 } 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}