blob: 77a72a73e3c5b70105bceac98f948c25e9b95569 [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;
Irfan Sheriff65cff172010-02-08 10:46:30 -080052
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 {
Danica Chang32711b62010-08-10 18:41:29 -070058
Robert Greenwalt3901edb2010-01-26 10:22:37 -080059 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080060 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070061 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080062
Amith Yamasanid3fed682012-04-27 17:38:40 -070063 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070064
65 private WebView mView;
Robert Greenwalt209177a2010-03-04 13:29:02 -080066 private CheckBoxPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080067
Irfan Sheriff65cff172010-02-08 10:46:30 -080068 private WifiApEnabler mWifiApEnabler;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070069 private CheckBoxPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070070
71 private CheckBoxPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070072
Robert Greenwalt3901edb2010-01-26 10:22:37 -080073 private BroadcastReceiver mTetherChangeReceiver;
74
Robert Greenwaltc4764d22010-02-12 14:21:37 -080075 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080076
77 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080078
Danica Chang32711b62010-08-10 18:41:29 -070079 private String[] mBluetoothRegexs;
80 private BluetoothPan mBluetoothPan;
81
Amith Yamasani84a042c2011-03-02 11:25:04 -080082 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
83 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
84
85 private String[] mSecurityType;
86 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080087
88 private WifiApDialog mDialog;
89 private WifiManager mWifiManager;
90 private WifiConfiguration mWifiConfig = null;
91
Mike Lockwood69a09572011-07-19 13:30:03 -070092 private boolean mUsbConnected;
93 private boolean mMassStorageActive;
94
Jake Hambyc777ee22011-03-04 15:37:39 -080095 private boolean mBluetoothEnableForTether;
96
Irfan Sheriff01b32362011-11-04 12:08:56 -070097 private static final int INVALID = -1;
98 private static final int WIFI_TETHERING = 0;
99 private static final int USB_TETHERING = 1;
100 private static final int BLUETOOTH_TETHERING = 2;
101
102 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
103 private int mTetherChoice = INVALID;
104
105 /* Stores the package name and the class name of the provisioning app */
106 private String[] mProvisionApp;
107 private static final int PROVISION_REQUEST = 0;
108
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800109 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700110 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800111 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800112 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800113
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700114 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800115 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
116 if (adapter != null) {
117 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
118 BluetoothProfile.PAN);
119 }
120
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700121 mEnableWifiAp =
Jake Hamby436b29e2011-02-07 18:21:25 -0800122 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700123 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800124 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700125 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800126
127 ConnectivityManager cm =
128 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800129
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800130 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800131 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700132 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
133
Amith Yamasanie419bc12011-02-14 14:19:08 -0800134 final boolean usbAvailable = mUsbRegexs.length != 0;
135 final boolean wifiAvailable = mWifiRegexs.length != 0;
136 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700137
Danica Chang32711b62010-08-10 18:41:29 -0700138 if (!usbAvailable || Utils.isMonkeyRunning()) {
139 getPreferenceScreen().removePreference(mUsbTether);
140 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800141
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800142 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700143 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700144 initWifiTethering();
145 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700146 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800147 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700148 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800149
Danica Chang32711b62010-08-10 18:41:29 -0700150 if (!bluetoothAvailable) {
151 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700152 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800153 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700154 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700155 } else {
156 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700157 }
158 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800159
Irfan Sheriff01b32362011-11-04 12:08:56 -0700160 mProvisionApp = getResources().getStringArray(
161 com.android.internal.R.array.config_mobile_hotspot_provision_app);
162
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700163 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800164 }
165
166 private void initWifiTethering() {
167 final Activity activity = getActivity();
168 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
169 mWifiConfig = mWifiManager.getWifiApConfiguration();
170 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
171
172 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800173
174 if (mWifiConfig == null) {
175 final String s = activity.getString(
176 com.android.internal.R.string.wifi_tether_configure_ssid_default);
177 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
178 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
179 } else {
180 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
181 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
182 mWifiConfig.SSID,
183 mSecurityType[index]));
184 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800185 }
186
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800187 private BluetoothProfile.ServiceListener mProfileServiceListener =
188 new BluetoothProfile.ServiceListener() {
189 public void onServiceConnected(int profile, BluetoothProfile proxy) {
190 mBluetoothPan = (BluetoothPan) proxy;
191 }
192 public void onServiceDisconnected(int profile) {
193 mBluetoothPan = null;
194 }
195 };
196
Irfan Sheriff0b266962010-04-06 15:16:44 -0700197 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700198 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700199 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800200 final Activity activity = getActivity();
201 mDialog = new WifiApDialog(activity, this, mWifiConfig);
202 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700203 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800204
Irfan Sheriff0b266962010-04-06 15:16:44 -0700205 return null;
206 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800207
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800208 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700209 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800210 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800211 String action = intent.getAction();
212 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800213 // TODO - this should understand the interface types
214 ArrayList<String> available = intent.getStringArrayListExtra(
215 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
216 ArrayList<String> active = intent.getStringArrayListExtra(
217 ConnectivityManager.EXTRA_ACTIVE_TETHER);
218 ArrayList<String> errored = intent.getStringArrayListExtra(
219 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700220 updateState(available.toArray(new String[available.size()]),
221 active.toArray(new String[active.size()]),
222 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700223 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
224 mMassStorageActive = true;
225 updateState();
226 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
227 mMassStorageActive = false;
228 updateState();
229 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
230 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800231 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800232 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
233 if (mBluetoothEnableForTether) {
234 switch (intent
235 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
236 case BluetoothAdapter.STATE_ON:
237 mBluetoothPan.setBluetoothTethering(true);
238 mBluetoothEnableForTether = false;
239 break;
240
241 case BluetoothAdapter.STATE_OFF:
242 case BluetoothAdapter.ERROR:
243 mBluetoothEnableForTether = false;
244 break;
245
246 default:
247 // ignore transition states
248 }
249 }
Danica Chang32711b62010-08-10 18:41:29 -0700250 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800251 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800252 }
253 }
254
255 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700256 public void onStart() {
257 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800258
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700259 final Activity activity = getActivity();
260
Mike Lockwood69a09572011-07-19 13:30:03 -0700261 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800262 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700263 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700264 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800265
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800266 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700267 filter.addAction(UsbManager.ACTION_USB_STATE);
268 activity.registerReceiver(mTetherChangeReceiver, filter);
269
270 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800271 filter.addAction(Intent.ACTION_MEDIA_SHARED);
272 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
273 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700274 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800275
Danica Chang32711b62010-08-10 18:41:29 -0700276 filter = new IntentFilter();
277 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700278 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700279
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700280 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700281 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700282 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700283 mWifiApEnabler.resume();
284 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800285
286 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800287 }
288
289 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700290 public void onStop() {
291 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700292 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800293 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700294 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700295 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700296 mWifiApEnabler.pause();
297 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800298 }
299
Robert Greenwalt209177a2010-03-04 13:29:02 -0800300 private void updateState() {
301 ConnectivityManager cm =
302 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
303
304 String[] available = cm.getTetherableIfaces();
305 String[] tethered = cm.getTetheredIfaces();
306 String[] errored = cm.getTetheringErroredIfaces();
307 updateState(available, tethered, errored);
308 }
309
Ben Clark0008d212010-07-27 16:20:59 +0100310 private void updateState(String[] available, String[] tethered,
311 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700312 updateUsbState(available, tethered, errored);
313 updateBluetoothState(available, tethered, errored);
314 }
315
316
317 private void updateUsbState(String[] available, String[] tethered,
318 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800319 ConnectivityManager cm =
320 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700321 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800322 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100323 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800324 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800325 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800326 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
327 usbError = cm.getLastTetherError(s);
328 }
329 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800330 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800331 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800332 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100333 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800334 for (String regex : mUsbRegexs) {
335 if (s.matches(regex)) usbTethered = true;
336 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800337 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800338 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100339 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800340 for (String regex : mUsbRegexs) {
341 if (s.matches(regex)) usbErrored = true;
342 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800343 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800344
345 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800346 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
347 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800348 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800349 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800350 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
351 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
352 } else {
353 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
354 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800355 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800356 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800357 } else if (usbErrored) {
358 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
359 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800360 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700361 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800362 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
363 mUsbTether.setEnabled(false);
364 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800365 } else {
366 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
367 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800368 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800369 }
370 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800371
Danica Chang32711b62010-08-10 18:41:29 -0700372 private void updateBluetoothState(String[] available, String[] tethered,
373 String[] errored) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800374 int bluetoothTethered = 0;
Danica Chang32711b62010-08-10 18:41:29 -0700375 for (String s : tethered) {
376 for (String regex : mBluetoothRegexs) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800377 if (s.matches(regex)) bluetoothTethered++;
Danica Chang32711b62010-08-10 18:41:29 -0700378 }
379 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800380 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700381 for (String s: errored) {
382 for (String regex : mBluetoothRegexs) {
383 if (s.matches(regex)) bluetoothErrored = true;
384 }
385 }
386
387 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
388 int btState = adapter.getState();
389 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
390 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700391 mBluetoothTether.setSummary(R.string.wifi_stopping);
392 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
393 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700394 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Jake Hambyc777ee22011-03-04 15:37:39 -0800395 } else if (btState == BluetoothAdapter.STATE_ON && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700396 mBluetoothTether.setChecked(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800397 mBluetoothTether.setEnabled(true);
398 if (bluetoothTethered > 1) {
399 String summary = getString(
400 R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
401 mBluetoothTether.setSummary(summary);
402 } else if (bluetoothTethered == 1) {
403 mBluetoothTether.setSummary(R.string.bluetooth_tethering_device_connected_subtext);
404 } else if (bluetoothErrored) {
405 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
406 } else {
407 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700408 }
409 } else {
410 mBluetoothTether.setEnabled(true);
411 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700412 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
413 }
414 }
415
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700416 public boolean onPreferenceChange(Preference preference, Object value) {
417 boolean enable = (Boolean) value;
418
419 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700420 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700421 } else {
422 mWifiApEnabler.setSoftapEnabled(false);
423 }
424 return false;
425 }
426
Irfan Sheriff01b32362011-11-04 12:08:56 -0700427 boolean isProvisioningNeeded() {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800428 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)) {
429 return false;
430 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700431 return mProvisionApp.length == 2;
432 }
433
434 private void startProvisioningIfNecessary(int choice) {
435 mTetherChoice = choice;
436 if (isProvisioningNeeded()) {
437 Intent intent = new Intent(Intent.ACTION_MAIN);
438 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
439 startActivityForResult(intent, PROVISION_REQUEST);
440 } else {
441 startTethering();
442 }
443 }
444
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700445 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
446 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700447 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700448 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700449 startTethering();
450 } else {
451 //BT and USB need checkbox turned off on failure
452 //Wifi tethering is never turned on until afterwards
453 switch (mTetherChoice) {
454 case BLUETOOTH_TETHERING:
455 mBluetoothTether.setChecked(false);
456 break;
457 case USB_TETHERING:
458 mUsbTether.setChecked(false);
459 break;
460 }
461 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700462 }
463 }
464 }
465
Irfan Sheriff01b32362011-11-04 12:08:56 -0700466 private void startTethering() {
467 switch (mTetherChoice) {
468 case WIFI_TETHERING:
469 mWifiApEnabler.setSoftapEnabled(true);
470 break;
471 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700472 // turn on Bluetooth first
473 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
474 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800475 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700476 adapter.enable();
477 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
478 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800479 } else {
480 mBluetoothPan.setBluetoothTethering(true);
481 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700482 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700483 break;
484 case USB_TETHERING:
485 setUsbTethering(true);
486 break;
487 default:
488 //should not happen
489 break;
490 }
491 }
492
493 private void setUsbTethering(boolean enabled) {
494 ConnectivityManager cm =
495 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
496 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
497 mUsbTether.setChecked(false);
498 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
499 return;
500 }
501 mUsbTether.setSummary("");
502 }
503
504 @Override
505 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
506 ConnectivityManager cm =
507 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
508
509 if (preference == mUsbTether) {
510 boolean newState = mUsbTether.isChecked();
511
512 if (newState) {
513 startProvisioningIfNecessary(USB_TETHERING);
514 } else {
515 setUsbTethering(newState);
516 }
517 } else if (preference == mBluetoothTether) {
518 boolean bluetoothTetherState = mBluetoothTether.isChecked();
519
520 if (bluetoothTetherState) {
521 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700522 } else {
523 boolean errored = false;
524
Danica Chang32711b62010-08-10 18:41:29 -0700525 String [] tethered = cm.getTetheredIfaces();
526 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
527 if (bluetoothIface != null &&
528 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
529 errored = true;
530 }
531
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700532 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700533 if (errored) {
534 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
535 } else {
536 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
537 }
538 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800539 } else if (preference == mCreateNetwork) {
540 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800541 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700542
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700543 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800544 }
545
Jake Hamby436b29e2011-02-07 18:21:25 -0800546 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800547 for (String iface : ifaces) {
548 for (String regex : regexes) {
549 if (iface.matches(regex)) {
550 return iface;
551 }
552 }
553 }
554 return null;
555 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800556
557 public void onClick(DialogInterface dialogInterface, int button) {
558 if (button == DialogInterface.BUTTON_POSITIVE) {
559 mWifiConfig = mDialog.getConfig();
560 if (mWifiConfig != null) {
561 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700562 * if soft AP is stopped, bring up
563 * else restart with new config
564 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800565 */
566 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700567 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800568 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800569 } else {
570 mWifiManager.setWifiApConfiguration(mWifiConfig);
571 }
572 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
573 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
574 mWifiConfig.SSID,
575 mSecurityType[index]));
576 }
577 }
578 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700579
580 @Override
581 public int getHelpResource() {
582 return R.string.help_url_tether;
583 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800584}