blob: e6d35965b84bb8889a5ee9afa5785ba4e5d91640 [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 Greenwalt505766c2011-11-09 15:03:14 -080040import android.os.SystemProperties;
Robert Greenwalt209177a2010-03-04 13:29:02 -080041import android.preference.CheckBoxPreference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080042import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080043import android.preference.PreferenceScreen;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070044import android.text.TextUtils;
Amith Yamasani476d7952011-01-21 09:28:31 -080045import android.view.ViewGroup;
46import android.view.ViewParent;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070047import android.webkit.WebView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080048
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070049import java.io.InputStream;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080050import java.util.ArrayList;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070051import java.util.Locale;
zzy7f38f472012-04-14 17:25:17 -070052import android.util.Log;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080053/*
54 * Displays preferences for Tethering.
55 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080056public class TetherSettings extends SettingsPreferenceFragment
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070057 implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
zzy7f38f472012-04-14 17:25:17 -070058 private static final String TAG = "TetherSettings";
59 private static final boolean DBG = false;
Danica Chang32711b62010-08-10 18:41:29 -070060
Robert Greenwalt3901edb2010-01-26 10:22:37 -080061 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080062 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070063 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080064
Amith Yamasanid3fed682012-04-27 17:38:40 -070065 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070066
67 private WebView mView;
Robert Greenwalt209177a2010-03-04 13:29:02 -080068 private CheckBoxPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080069
Irfan Sheriff65cff172010-02-08 10:46:30 -080070 private WifiApEnabler mWifiApEnabler;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070071 private CheckBoxPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070072
73 private CheckBoxPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070074
Robert Greenwalt3901edb2010-01-26 10:22:37 -080075 private BroadcastReceiver mTetherChangeReceiver;
76
Robert Greenwaltc4764d22010-02-12 14:21:37 -080077 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080078
79 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080080
Danica Chang32711b62010-08-10 18:41:29 -070081 private String[] mBluetoothRegexs;
82 private BluetoothPan mBluetoothPan;
83
Amith Yamasani84a042c2011-03-02 11:25:04 -080084 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
85 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
86
87 private String[] mSecurityType;
88 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080089
90 private WifiApDialog mDialog;
91 private WifiManager mWifiManager;
92 private WifiConfiguration mWifiConfig = null;
93
Mike Lockwood69a09572011-07-19 13:30:03 -070094 private boolean mUsbConnected;
95 private boolean mMassStorageActive;
96
Jake Hambyc777ee22011-03-04 15:37:39 -080097 private boolean mBluetoothEnableForTether;
98
Irfan Sheriff01b32362011-11-04 12:08:56 -070099 private static final int INVALID = -1;
100 private static final int WIFI_TETHERING = 0;
101 private static final int USB_TETHERING = 1;
102 private static final int BLUETOOTH_TETHERING = 2;
103
104 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
105 private int mTetherChoice = INVALID;
106
107 /* Stores the package name and the class name of the provisioning app */
108 private String[] mProvisionApp;
109 private static final int PROVISION_REQUEST = 0;
110
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800111 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700112 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800113 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800114 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800115
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700116 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800117 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
118 if (adapter != null) {
119 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
120 BluetoothProfile.PAN);
121 }
122
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700123 mEnableWifiAp =
Jake Hamby436b29e2011-02-07 18:21:25 -0800124 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700125 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800126 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700127 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800128
129 ConnectivityManager cm =
130 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800131
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800132 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800133 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700134 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
135
Amith Yamasanie419bc12011-02-14 14:19:08 -0800136 final boolean usbAvailable = mUsbRegexs.length != 0;
137 final boolean wifiAvailable = mWifiRegexs.length != 0;
138 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700139
Danica Chang32711b62010-08-10 18:41:29 -0700140 if (!usbAvailable || Utils.isMonkeyRunning()) {
141 getPreferenceScreen().removePreference(mUsbTether);
142 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800143
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800144 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700145 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700146 initWifiTethering();
147 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700148 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800149 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700150 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800151
Danica Chang32711b62010-08-10 18:41:29 -0700152 if (!bluetoothAvailable) {
153 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700154 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800155 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700156 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700157 } else {
158 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700159 }
160 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800161
Irfan Sheriff01b32362011-11-04 12:08:56 -0700162 mProvisionApp = getResources().getStringArray(
163 com.android.internal.R.array.config_mobile_hotspot_provision_app);
164
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700165 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800166 }
167
168 private void initWifiTethering() {
169 final Activity activity = getActivity();
170 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
171 mWifiConfig = mWifiManager.getWifiApConfiguration();
172 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
173
174 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800175
176 if (mWifiConfig == null) {
177 final String s = activity.getString(
178 com.android.internal.R.string.wifi_tether_configure_ssid_default);
179 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
180 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
181 } else {
182 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
183 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
184 mWifiConfig.SSID,
185 mSecurityType[index]));
186 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800187 }
188
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800189 private BluetoothProfile.ServiceListener mProfileServiceListener =
190 new BluetoothProfile.ServiceListener() {
191 public void onServiceConnected(int profile, BluetoothProfile proxy) {
192 mBluetoothPan = (BluetoothPan) proxy;
193 }
194 public void onServiceDisconnected(int profile) {
195 mBluetoothPan = null;
196 }
197 };
198
Irfan Sheriff0b266962010-04-06 15:16:44 -0700199 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700200 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700201 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800202 final Activity activity = getActivity();
203 mDialog = new WifiApDialog(activity, this, mWifiConfig);
204 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700205 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800206
Irfan Sheriff0b266962010-04-06 15:16:44 -0700207 return null;
208 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800209
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800210 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700211 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800212 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800213 String action = intent.getAction();
214 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800215 // TODO - this should understand the interface types
216 ArrayList<String> available = intent.getStringArrayListExtra(
217 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
218 ArrayList<String> active = intent.getStringArrayListExtra(
219 ConnectivityManager.EXTRA_ACTIVE_TETHER);
220 ArrayList<String> errored = intent.getStringArrayListExtra(
221 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700222 updateState(available.toArray(new String[available.size()]),
223 active.toArray(new String[active.size()]),
224 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700225 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
226 mMassStorageActive = true;
227 updateState();
228 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
229 mMassStorageActive = false;
230 updateState();
231 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
232 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800233 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800234 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
235 if (mBluetoothEnableForTether) {
236 switch (intent
237 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
238 case BluetoothAdapter.STATE_ON:
zzy7f38f472012-04-14 17:25:17 -0700239 if(mBluetoothPan != null) {
240 mBluetoothPan.setBluetoothTethering(true);
241 mBluetoothEnableForTether = false;
242 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800243 break;
244
245 case BluetoothAdapter.STATE_OFF:
246 case BluetoothAdapter.ERROR:
247 mBluetoothEnableForTether = false;
248 break;
249
250 default:
251 // ignore transition states
252 }
253 }
Danica Chang32711b62010-08-10 18:41:29 -0700254 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800255 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800256 }
257 }
258
259 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700260 public void onStart() {
261 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800262
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700263 final Activity activity = getActivity();
264
Mike Lockwood69a09572011-07-19 13:30:03 -0700265 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800266 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700267 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700268 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800269
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800270 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700271 filter.addAction(UsbManager.ACTION_USB_STATE);
272 activity.registerReceiver(mTetherChangeReceiver, filter);
273
274 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800275 filter.addAction(Intent.ACTION_MEDIA_SHARED);
276 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
277 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700278 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800279
Danica Chang32711b62010-08-10 18:41:29 -0700280 filter = new IntentFilter();
281 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700282 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700283
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700284 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700285 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700286 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700287 mWifiApEnabler.resume();
288 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800289
290 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800291 }
292
293 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700294 public void onStop() {
295 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700296 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800297 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700298 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700299 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700300 mWifiApEnabler.pause();
301 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800302 }
303
Robert Greenwalt209177a2010-03-04 13:29:02 -0800304 private void updateState() {
305 ConnectivityManager cm =
306 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
307
308 String[] available = cm.getTetherableIfaces();
309 String[] tethered = cm.getTetheredIfaces();
310 String[] errored = cm.getTetheringErroredIfaces();
311 updateState(available, tethered, errored);
312 }
313
Ben Clark0008d212010-07-27 16:20:59 +0100314 private void updateState(String[] available, String[] tethered,
315 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700316 updateUsbState(available, tethered, errored);
317 updateBluetoothState(available, tethered, errored);
318 }
319
320
321 private void updateUsbState(String[] available, String[] tethered,
322 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800323 ConnectivityManager cm =
324 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700325 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800326 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100327 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800328 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800329 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800330 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
331 usbError = cm.getLastTetherError(s);
332 }
333 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800334 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800335 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800336 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100337 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800338 for (String regex : mUsbRegexs) {
339 if (s.matches(regex)) usbTethered = true;
340 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800341 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800342 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100343 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800344 for (String regex : mUsbRegexs) {
345 if (s.matches(regex)) usbErrored = true;
346 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800347 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800348
349 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800350 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
351 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800352 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800353 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800354 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
355 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
356 } else {
357 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
358 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800359 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800360 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800361 } else if (usbErrored) {
362 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
363 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800364 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700365 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800366 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
367 mUsbTether.setEnabled(false);
368 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800369 } else {
370 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
371 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800372 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800373 }
374 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800375
Danica Chang32711b62010-08-10 18:41:29 -0700376 private void updateBluetoothState(String[] available, String[] tethered,
377 String[] errored) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800378 int bluetoothTethered = 0;
Danica Chang32711b62010-08-10 18:41:29 -0700379 for (String s : tethered) {
380 for (String regex : mBluetoothRegexs) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800381 if (s.matches(regex)) bluetoothTethered++;
Danica Chang32711b62010-08-10 18:41:29 -0700382 }
383 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800384 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700385 for (String s: errored) {
386 for (String regex : mBluetoothRegexs) {
387 if (s.matches(regex)) bluetoothErrored = true;
388 }
389 }
390
391 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
392 int btState = adapter.getState();
393 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
394 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700395 mBluetoothTether.setSummary(R.string.wifi_stopping);
396 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
397 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700398 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
zzy7f38f472012-04-14 17:25:17 -0700399 } else if (btState == BluetoothAdapter.STATE_ON &&
400 mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700401 mBluetoothTether.setChecked(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800402 mBluetoothTether.setEnabled(true);
403 if (bluetoothTethered > 1) {
404 String summary = getString(
405 R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
406 mBluetoothTether.setSummary(summary);
407 } else if (bluetoothTethered == 1) {
408 mBluetoothTether.setSummary(R.string.bluetooth_tethering_device_connected_subtext);
409 } else if (bluetoothErrored) {
410 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
411 } else {
412 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700413 }
414 } else {
415 mBluetoothTether.setEnabled(true);
416 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700417 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
418 }
419 }
420
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700421 public boolean onPreferenceChange(Preference preference, Object value) {
422 boolean enable = (Boolean) value;
423
424 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700425 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700426 } else {
427 mWifiApEnabler.setSoftapEnabled(false);
428 }
429 return false;
430 }
431
Irfan Sheriff01b32362011-11-04 12:08:56 -0700432 boolean isProvisioningNeeded() {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800433 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)) {
434 return false;
435 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700436 return mProvisionApp.length == 2;
437 }
438
439 private void startProvisioningIfNecessary(int choice) {
440 mTetherChoice = choice;
441 if (isProvisioningNeeded()) {
442 Intent intent = new Intent(Intent.ACTION_MAIN);
443 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
444 startActivityForResult(intent, PROVISION_REQUEST);
445 } else {
446 startTethering();
447 }
448 }
449
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700450 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
451 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700452 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700453 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700454 startTethering();
455 } else {
456 //BT and USB need checkbox turned off on failure
457 //Wifi tethering is never turned on until afterwards
458 switch (mTetherChoice) {
459 case BLUETOOTH_TETHERING:
460 mBluetoothTether.setChecked(false);
461 break;
462 case USB_TETHERING:
463 mUsbTether.setChecked(false);
464 break;
465 }
466 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700467 }
468 }
469 }
470
Irfan Sheriff01b32362011-11-04 12:08:56 -0700471 private void startTethering() {
472 switch (mTetherChoice) {
473 case WIFI_TETHERING:
474 mWifiApEnabler.setSoftapEnabled(true);
475 break;
476 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700477 // turn on Bluetooth first
478 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
479 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800480 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700481 adapter.enable();
482 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
483 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800484 } else {
485 mBluetoothPan.setBluetoothTethering(true);
486 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700487 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700488 break;
489 case USB_TETHERING:
490 setUsbTethering(true);
491 break;
492 default:
493 //should not happen
494 break;
495 }
496 }
497
498 private void setUsbTethering(boolean enabled) {
499 ConnectivityManager cm =
500 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
501 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
502 mUsbTether.setChecked(false);
503 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
504 return;
505 }
506 mUsbTether.setSummary("");
507 }
508
509 @Override
510 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
511 ConnectivityManager cm =
512 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
513
514 if (preference == mUsbTether) {
515 boolean newState = mUsbTether.isChecked();
516
517 if (newState) {
518 startProvisioningIfNecessary(USB_TETHERING);
519 } else {
520 setUsbTethering(newState);
521 }
522 } else if (preference == mBluetoothTether) {
523 boolean bluetoothTetherState = mBluetoothTether.isChecked();
524
525 if (bluetoothTetherState) {
526 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700527 } else {
528 boolean errored = false;
529
Danica Chang32711b62010-08-10 18:41:29 -0700530 String [] tethered = cm.getTetheredIfaces();
531 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
532 if (bluetoothIface != null &&
533 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
534 errored = true;
535 }
536
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700537 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700538 if (errored) {
539 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
540 } else {
541 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
542 }
543 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800544 } else if (preference == mCreateNetwork) {
545 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800546 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700547
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700548 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800549 }
550
Jake Hamby436b29e2011-02-07 18:21:25 -0800551 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800552 for (String iface : ifaces) {
553 for (String regex : regexes) {
554 if (iface.matches(regex)) {
555 return iface;
556 }
557 }
558 }
559 return null;
560 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800561
562 public void onClick(DialogInterface dialogInterface, int button) {
563 if (button == DialogInterface.BUTTON_POSITIVE) {
564 mWifiConfig = mDialog.getConfig();
565 if (mWifiConfig != null) {
566 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700567 * if soft AP is stopped, bring up
568 * else restart with new config
569 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800570 */
571 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700572 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800573 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800574 } else {
575 mWifiManager.setWifiApConfiguration(mWifiConfig);
576 }
577 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
578 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
579 mWifiConfig.SSID,
580 mSecurityType[index]));
581 }
582 }
583 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700584
585 @Override
586 public int getHelpResource() {
587 return R.string.help_url_tether;
588 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800589}