blob: 368973ca829eed1fbb9cfd21504c1789e5f3a5f6 [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;
Matthew Xieb5f144a2012-06-28 18:41:54 -070052
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";
Danica Chang32711b62010-08-10 18:41:29 -070059
Robert Greenwalt3901edb2010-01-26 10:22:37 -080060 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080061 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070062 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080063
Amith Yamasanid3fed682012-04-27 17:38:40 -070064 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070065
66 private WebView mView;
Robert Greenwalt209177a2010-03-04 13:29:02 -080067 private CheckBoxPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080068
Irfan Sheriff65cff172010-02-08 10:46:30 -080069 private WifiApEnabler mWifiApEnabler;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070070 private CheckBoxPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070071
72 private CheckBoxPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070073
Robert Greenwalt3901edb2010-01-26 10:22:37 -080074 private BroadcastReceiver mTetherChangeReceiver;
75
Robert Greenwaltc4764d22010-02-12 14:21:37 -080076 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080077
78 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080079
Danica Chang32711b62010-08-10 18:41:29 -070080 private String[] mBluetoothRegexs;
81 private BluetoothPan mBluetoothPan;
82
Amith Yamasani84a042c2011-03-02 11:25:04 -080083 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
84 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
85
86 private String[] mSecurityType;
87 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080088
89 private WifiApDialog mDialog;
90 private WifiManager mWifiManager;
91 private WifiConfiguration mWifiConfig = null;
92
Mike Lockwood69a09572011-07-19 13:30:03 -070093 private boolean mUsbConnected;
94 private boolean mMassStorageActive;
95
Jake Hambyc777ee22011-03-04 15:37:39 -080096 private boolean mBluetoothEnableForTether;
97
Irfan Sheriff01b32362011-11-04 12:08:56 -070098 private static final int INVALID = -1;
99 private static final int WIFI_TETHERING = 0;
100 private static final int USB_TETHERING = 1;
101 private static final int BLUETOOTH_TETHERING = 2;
102
103 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
104 private int mTetherChoice = INVALID;
105
106 /* Stores the package name and the class name of the provisioning app */
107 private String[] mProvisionApp;
108 private static final int PROVISION_REQUEST = 0;
109
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800110 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700111 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800112 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800113 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800114
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700115 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800116 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
117 if (adapter != null) {
118 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
119 BluetoothProfile.PAN);
120 }
121
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700122 mEnableWifiAp =
Jake Hamby436b29e2011-02-07 18:21:25 -0800123 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700124 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800125 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700126 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800127
128 ConnectivityManager cm =
129 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800130
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800131 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800132 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700133 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
134
Amith Yamasanie419bc12011-02-14 14:19:08 -0800135 final boolean usbAvailable = mUsbRegexs.length != 0;
136 final boolean wifiAvailable = mWifiRegexs.length != 0;
137 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700138
Danica Chang32711b62010-08-10 18:41:29 -0700139 if (!usbAvailable || Utils.isMonkeyRunning()) {
140 getPreferenceScreen().removePreference(mUsbTether);
141 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800142
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800143 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700144 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700145 initWifiTethering();
146 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700147 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800148 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700149 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800150
Danica Chang32711b62010-08-10 18:41:29 -0700151 if (!bluetoothAvailable) {
152 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700153 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800154 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700155 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700156 } else {
157 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700158 }
159 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800160
Irfan Sheriff01b32362011-11-04 12:08:56 -0700161 mProvisionApp = getResources().getStringArray(
162 com.android.internal.R.array.config_mobile_hotspot_provision_app);
163
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700164 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800165 }
166
167 private void initWifiTethering() {
168 final Activity activity = getActivity();
169 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
170 mWifiConfig = mWifiManager.getWifiApConfiguration();
171 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
172
173 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800174
175 if (mWifiConfig == null) {
176 final String s = activity.getString(
177 com.android.internal.R.string.wifi_tether_configure_ssid_default);
178 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
179 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
180 } else {
181 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
182 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
183 mWifiConfig.SSID,
184 mSecurityType[index]));
185 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800186 }
187
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800188 private BluetoothProfile.ServiceListener mProfileServiceListener =
189 new BluetoothProfile.ServiceListener() {
190 public void onServiceConnected(int profile, BluetoothProfile proxy) {
191 mBluetoothPan = (BluetoothPan) proxy;
192 }
193 public void onServiceDisconnected(int profile) {
194 mBluetoothPan = null;
195 }
196 };
197
Irfan Sheriff0b266962010-04-06 15:16:44 -0700198 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700199 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700200 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800201 final Activity activity = getActivity();
202 mDialog = new WifiApDialog(activity, this, mWifiConfig);
203 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700204 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800205
Irfan Sheriff0b266962010-04-06 15:16:44 -0700206 return null;
207 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800208
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800209 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700210 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800211 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800212 String action = intent.getAction();
213 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800214 // TODO - this should understand the interface types
215 ArrayList<String> available = intent.getStringArrayListExtra(
216 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
217 ArrayList<String> active = intent.getStringArrayListExtra(
218 ConnectivityManager.EXTRA_ACTIVE_TETHER);
219 ArrayList<String> errored = intent.getStringArrayListExtra(
220 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700221 updateState(available.toArray(new String[available.size()]),
222 active.toArray(new String[active.size()]),
223 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700224 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
225 mMassStorageActive = true;
226 updateState();
227 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
228 mMassStorageActive = false;
229 updateState();
230 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
231 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800232 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800233 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
234 if (mBluetoothEnableForTether) {
235 switch (intent
236 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
237 case BluetoothAdapter.STATE_ON:
zzy7f38f472012-04-14 17:25:17 -0700238 if(mBluetoothPan != null) {
239 mBluetoothPan.setBluetoothTethering(true);
240 mBluetoothEnableForTether = false;
241 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800242 break;
243
244 case BluetoothAdapter.STATE_OFF:
245 case BluetoothAdapter.ERROR:
246 mBluetoothEnableForTether = false;
247 break;
248
249 default:
250 // ignore transition states
251 }
252 }
Danica Chang32711b62010-08-10 18:41:29 -0700253 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800254 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800255 }
256 }
257
258 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700259 public void onStart() {
260 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800261
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700262 final Activity activity = getActivity();
263
Mike Lockwood69a09572011-07-19 13:30:03 -0700264 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800265 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700266 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700267 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800268
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800269 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700270 filter.addAction(UsbManager.ACTION_USB_STATE);
271 activity.registerReceiver(mTetherChangeReceiver, filter);
272
273 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800274 filter.addAction(Intent.ACTION_MEDIA_SHARED);
275 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
276 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700277 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800278
Danica Chang32711b62010-08-10 18:41:29 -0700279 filter = new IntentFilter();
280 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700281 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700282
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700283 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700284 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700285 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700286 mWifiApEnabler.resume();
287 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800288
289 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800290 }
291
292 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700293 public void onStop() {
294 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700295 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800296 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700297 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700298 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700299 mWifiApEnabler.pause();
300 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800301 }
302
Robert Greenwalt209177a2010-03-04 13:29:02 -0800303 private void updateState() {
304 ConnectivityManager cm =
305 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
306
307 String[] available = cm.getTetherableIfaces();
308 String[] tethered = cm.getTetheredIfaces();
309 String[] errored = cm.getTetheringErroredIfaces();
310 updateState(available, tethered, errored);
311 }
312
Ben Clark0008d212010-07-27 16:20:59 +0100313 private void updateState(String[] available, String[] tethered,
314 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700315 updateUsbState(available, tethered, errored);
316 updateBluetoothState(available, tethered, errored);
317 }
318
319
320 private void updateUsbState(String[] available, String[] tethered,
321 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800322 ConnectivityManager cm =
323 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700324 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800325 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100326 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800327 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800328 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800329 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
330 usbError = cm.getLastTetherError(s);
331 }
332 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800333 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800334 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800335 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100336 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800337 for (String regex : mUsbRegexs) {
338 if (s.matches(regex)) usbTethered = true;
339 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800340 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800341 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100342 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800343 for (String regex : mUsbRegexs) {
344 if (s.matches(regex)) usbErrored = true;
345 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800346 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800347
348 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800349 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
350 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800351 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800352 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800353 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
354 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
355 } else {
356 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
357 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800358 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800359 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800360 } else if (usbErrored) {
361 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
362 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800363 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700364 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800365 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
366 mUsbTether.setEnabled(false);
367 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800368 } else {
369 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
370 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800371 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800372 }
373 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800374
Danica Chang32711b62010-08-10 18:41:29 -0700375 private void updateBluetoothState(String[] available, String[] tethered,
376 String[] errored) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800377 int bluetoothTethered = 0;
Danica Chang32711b62010-08-10 18:41:29 -0700378 for (String s : tethered) {
379 for (String regex : mBluetoothRegexs) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800380 if (s.matches(regex)) bluetoothTethered++;
Danica Chang32711b62010-08-10 18:41:29 -0700381 }
382 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800383 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700384 for (String s: errored) {
385 for (String regex : mBluetoothRegexs) {
386 if (s.matches(regex)) bluetoothErrored = true;
387 }
388 }
389
390 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
391 int btState = adapter.getState();
392 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
393 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700394 mBluetoothTether.setSummary(R.string.wifi_stopping);
395 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
396 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700397 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
zzy7f38f472012-04-14 17:25:17 -0700398 } else if (btState == BluetoothAdapter.STATE_ON &&
399 mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700400 mBluetoothTether.setChecked(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800401 mBluetoothTether.setEnabled(true);
402 if (bluetoothTethered > 1) {
403 String summary = getString(
404 R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
405 mBluetoothTether.setSummary(summary);
406 } else if (bluetoothTethered == 1) {
407 mBluetoothTether.setSummary(R.string.bluetooth_tethering_device_connected_subtext);
408 } else if (bluetoothErrored) {
409 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
410 } else {
411 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700412 }
413 } else {
414 mBluetoothTether.setEnabled(true);
415 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700416 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
417 }
418 }
419
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700420 public boolean onPreferenceChange(Preference preference, Object value) {
421 boolean enable = (Boolean) value;
422
423 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700424 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700425 } else {
426 mWifiApEnabler.setSoftapEnabled(false);
427 }
428 return false;
429 }
430
Irfan Sheriff01b32362011-11-04 12:08:56 -0700431 boolean isProvisioningNeeded() {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800432 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)) {
433 return false;
434 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700435 return mProvisionApp.length == 2;
436 }
437
438 private void startProvisioningIfNecessary(int choice) {
439 mTetherChoice = choice;
440 if (isProvisioningNeeded()) {
441 Intent intent = new Intent(Intent.ACTION_MAIN);
442 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
443 startActivityForResult(intent, PROVISION_REQUEST);
444 } else {
445 startTethering();
446 }
447 }
448
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700449 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
450 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700451 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700452 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700453 startTethering();
454 } else {
455 //BT and USB need checkbox turned off on failure
456 //Wifi tethering is never turned on until afterwards
457 switch (mTetherChoice) {
458 case BLUETOOTH_TETHERING:
459 mBluetoothTether.setChecked(false);
460 break;
461 case USB_TETHERING:
462 mUsbTether.setChecked(false);
463 break;
464 }
465 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700466 }
467 }
468 }
469
Irfan Sheriff01b32362011-11-04 12:08:56 -0700470 private void startTethering() {
471 switch (mTetherChoice) {
472 case WIFI_TETHERING:
473 mWifiApEnabler.setSoftapEnabled(true);
474 break;
475 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700476 // turn on Bluetooth first
477 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
478 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800479 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700480 adapter.enable();
481 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
482 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800483 } else {
484 mBluetoothPan.setBluetoothTethering(true);
485 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700486 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700487 break;
488 case USB_TETHERING:
489 setUsbTethering(true);
490 break;
491 default:
492 //should not happen
493 break;
494 }
495 }
496
497 private void setUsbTethering(boolean enabled) {
498 ConnectivityManager cm =
499 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
500 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
501 mUsbTether.setChecked(false);
502 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
503 return;
504 }
505 mUsbTether.setSummary("");
506 }
507
508 @Override
509 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
510 ConnectivityManager cm =
511 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
512
513 if (preference == mUsbTether) {
514 boolean newState = mUsbTether.isChecked();
515
516 if (newState) {
517 startProvisioningIfNecessary(USB_TETHERING);
518 } else {
519 setUsbTethering(newState);
520 }
521 } else if (preference == mBluetoothTether) {
522 boolean bluetoothTetherState = mBluetoothTether.isChecked();
523
524 if (bluetoothTetherState) {
525 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700526 } else {
527 boolean errored = false;
528
Danica Chang32711b62010-08-10 18:41:29 -0700529 String [] tethered = cm.getTetheredIfaces();
530 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
531 if (bluetoothIface != null &&
532 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
533 errored = true;
534 }
535
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700536 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700537 if (errored) {
538 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
539 } else {
540 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
541 }
542 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800543 } else if (preference == mCreateNetwork) {
544 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800545 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700546
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700547 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800548 }
549
Jake Hamby436b29e2011-02-07 18:21:25 -0800550 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800551 for (String iface : ifaces) {
552 for (String regex : regexes) {
553 if (iface.matches(regex)) {
554 return iface;
555 }
556 }
557 }
558 return null;
559 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800560
561 public void onClick(DialogInterface dialogInterface, int button) {
562 if (button == DialogInterface.BUTTON_POSITIVE) {
563 mWifiConfig = mDialog.getConfig();
564 if (mWifiConfig != null) {
565 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700566 * if soft AP is stopped, bring up
567 * else restart with new config
568 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800569 */
570 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700571 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800572 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800573 } else {
574 mWifiManager.setWifiApConfiguration(mWifiConfig);
575 }
576 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
577 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
578 mWifiConfig.SSID,
579 mSecurityType[index]));
580 }
581 }
582 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700583
584 @Override
585 public int getHelpResource() {
586 return R.string.help_url_tether;
587 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800588}