blob: 8069d52581d1b0023e10c09f4acb8b3db28ff8d9 [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;
Amith Yamasani394eaa22013-06-11 11:04:44 -070041import android.os.UserHandle;
42import android.os.UserManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080043import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080044import android.preference.PreferenceScreen;
PauloftheWest656c88c2014-08-24 11:59:35 -070045import android.preference.SwitchPreference;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070046import android.text.TextUtils;
Amith Yamasani476d7952011-01-21 09:28:31 -080047import android.view.ViewGroup;
48import android.view.ViewParent;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070049import android.webkit.WebView;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040050import android.widget.TextView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080051
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070052import java.io.InputStream;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080053import java.util.ArrayList;
Robert Greenwaltf60b92b2012-09-13 15:02:00 -070054import java.util.concurrent.atomic.AtomicReference;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070055import java.util.Locale;
Matthew Xieb5f144a2012-06-28 18:41:54 -070056
Robert Greenwalt3901edb2010-01-26 10:22:37 -080057/*
58 * Displays preferences for Tethering.
59 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080060public class TetherSettings extends SettingsPreferenceFragment
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070061 implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
zzy7f38f472012-04-14 17:25:17 -070062 private static final String TAG = "TetherSettings";
Danica Chang32711b62010-08-10 18:41:29 -070063
Robert Greenwalt3901edb2010-01-26 10:22:37 -080064 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080065 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070066 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080067
Amith Yamasanid3fed682012-04-27 17:38:40 -070068 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070069
70 private WebView mView;
PauloftheWest656c88c2014-08-24 11:59:35 -070071 private SwitchPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080072
Irfan Sheriff65cff172010-02-08 10:46:30 -080073 private WifiApEnabler mWifiApEnabler;
PauloftheWest656c88c2014-08-24 11:59:35 -070074 private SwitchPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070075
PauloftheWest656c88c2014-08-24 11:59:35 -070076 private SwitchPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070077
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;
Robert Greenwalt4f1970f2012-09-16 17:03:08 -070085 private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<BluetoothPan>();
Danica Chang32711b62010-08-10 18:41:29 -070086
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;
Amith Yamasani84a042c2011-03-02 11:25:04 -080092
93 private WifiApDialog mDialog;
94 private WifiManager mWifiManager;
95 private WifiConfiguration mWifiConfig = null;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040096 private UserManager mUm;
Amith Yamasani84a042c2011-03-02 11:25:04 -080097
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
Irfan Sheriff01b32362011-11-04 12:08:56 -0700103 private static final int INVALID = -1;
104 private static final int WIFI_TETHERING = 0;
105 private static final int USB_TETHERING = 1;
106 private static final int BLUETOOTH_TETHERING = 2;
107
108 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
109 private int mTetherChoice = INVALID;
110
111 /* Stores the package name and the class name of the provisioning app */
112 private String[] mProvisionApp;
113 private static final int PROVISION_REQUEST = 0;
114
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400115 private boolean mUnavailable;
116
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800117 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700118 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800119 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800120 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800121
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400122 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
123
124 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
125 mUnavailable = true;
126 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
127 return;
128 }
129
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700130 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800131 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
132 if (adapter != null) {
133 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
134 BluetoothProfile.PAN);
135 }
136
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700137 mEnableWifiAp =
PauloftheWest656c88c2014-08-24 11:59:35 -0700138 (SwitchPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700139 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
PauloftheWest656c88c2014-08-24 11:59:35 -0700140 mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
141 mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800142
143 ConnectivityManager cm =
144 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800145
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800146 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800147 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700148 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
149
Amith Yamasanie419bc12011-02-14 14:19:08 -0800150 final boolean usbAvailable = mUsbRegexs.length != 0;
151 final boolean wifiAvailable = mWifiRegexs.length != 0;
152 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700153
Danica Chang32711b62010-08-10 18:41:29 -0700154 if (!usbAvailable || Utils.isMonkeyRunning()) {
155 getPreferenceScreen().removePreference(mUsbTether);
156 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800157
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800158 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700159 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700160 initWifiTethering();
161 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700162 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800163 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700164 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800165
Danica Chang32711b62010-08-10 18:41:29 -0700166 if (!bluetoothAvailable) {
167 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700168 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700169 BluetoothPan pan = mBluetoothPan.get();
170 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700171 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700172 } else {
173 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700174 }
175 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800176
Irfan Sheriff01b32362011-11-04 12:08:56 -0700177 mProvisionApp = getResources().getStringArray(
178 com.android.internal.R.array.config_mobile_hotspot_provision_app);
179
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700180 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800181 }
182
183 private void initWifiTethering() {
184 final Activity activity = getActivity();
185 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
186 mWifiConfig = mWifiManager.getWifiApConfiguration();
187 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
188
189 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800190
191 if (mWifiConfig == null) {
192 final String s = activity.getString(
193 com.android.internal.R.string.wifi_tether_configure_ssid_default);
194 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
195 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
196 } else {
197 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
198 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
199 mWifiConfig.SSID,
200 mSecurityType[index]));
201 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800202 }
203
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800204 private BluetoothProfile.ServiceListener mProfileServiceListener =
205 new BluetoothProfile.ServiceListener() {
206 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700207 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800208 }
209 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700210 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800211 }
212 };
213
Irfan Sheriff0b266962010-04-06 15:16:44 -0700214 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700215 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700216 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800217 final Activity activity = getActivity();
218 mDialog = new WifiApDialog(activity, this, mWifiConfig);
219 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700220 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800221
Irfan Sheriff0b266962010-04-06 15:16:44 -0700222 return null;
223 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800224
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800225 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700226 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800227 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800228 String action = intent.getAction();
229 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800230 // TODO - this should understand the interface types
231 ArrayList<String> available = intent.getStringArrayListExtra(
232 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
233 ArrayList<String> active = intent.getStringArrayListExtra(
234 ConnectivityManager.EXTRA_ACTIVE_TETHER);
235 ArrayList<String> errored = intent.getStringArrayListExtra(
236 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700237 updateState(available.toArray(new String[available.size()]),
238 active.toArray(new String[active.size()]),
239 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700240 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
241 mMassStorageActive = true;
242 updateState();
243 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
244 mMassStorageActive = false;
245 updateState();
246 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
247 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800248 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800249 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
250 if (mBluetoothEnableForTether) {
251 switch (intent
252 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
253 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700254 BluetoothPan bluetoothPan = mBluetoothPan.get();
255 if (bluetoothPan != null) {
256 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700257 mBluetoothEnableForTether = false;
258 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800259 break;
260
261 case BluetoothAdapter.STATE_OFF:
262 case BluetoothAdapter.ERROR:
263 mBluetoothEnableForTether = false;
264 break;
265
266 default:
267 // ignore transition states
268 }
269 }
Danica Chang32711b62010-08-10 18:41:29 -0700270 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800271 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800272 }
273 }
274
275 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700276 public void onStart() {
277 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800278
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400279 if (mUnavailable) {
280 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
281 getListView().setEmptyView(emptyView);
282 if (emptyView != null) {
283 emptyView.setText(R.string.tethering_settings_not_available);
284 }
285 return;
286 }
287
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700288 final Activity activity = getActivity();
289
Mike Lockwood69a09572011-07-19 13:30:03 -0700290 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800291 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700292 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700293 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800294
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800295 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700296 filter.addAction(UsbManager.ACTION_USB_STATE);
297 activity.registerReceiver(mTetherChangeReceiver, filter);
298
299 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800300 filter.addAction(Intent.ACTION_MEDIA_SHARED);
301 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
302 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700303 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800304
Danica Chang32711b62010-08-10 18:41:29 -0700305 filter = new IntentFilter();
306 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700307 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700308
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700309 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700310 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700311 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700312 mWifiApEnabler.resume();
313 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800314
315 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800316 }
317
318 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700319 public void onStop() {
320 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400321
322 if (mUnavailable) {
323 return;
324 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700325 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800326 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700327 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700328 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700329 mWifiApEnabler.pause();
330 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800331 }
332
Robert Greenwalt209177a2010-03-04 13:29:02 -0800333 private void updateState() {
334 ConnectivityManager cm =
335 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
336
337 String[] available = cm.getTetherableIfaces();
338 String[] tethered = cm.getTetheredIfaces();
339 String[] errored = cm.getTetheringErroredIfaces();
340 updateState(available, tethered, errored);
341 }
342
Ben Clark0008d212010-07-27 16:20:59 +0100343 private void updateState(String[] available, String[] tethered,
344 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700345 updateUsbState(available, tethered, errored);
346 updateBluetoothState(available, tethered, errored);
347 }
348
349
350 private void updateUsbState(String[] available, String[] tethered,
351 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800352 ConnectivityManager cm =
353 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700354 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800355 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100356 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800357 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800358 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800359 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
360 usbError = cm.getLastTetherError(s);
361 }
362 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800363 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800364 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800365 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100366 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800367 for (String regex : mUsbRegexs) {
368 if (s.matches(regex)) usbTethered = true;
369 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800370 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800371 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100372 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800373 for (String regex : mUsbRegexs) {
374 if (s.matches(regex)) usbErrored = true;
375 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800376 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800377
378 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800379 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
380 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800381 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800382 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800383 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
384 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
385 } else {
386 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
387 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800388 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800389 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800390 } else if (usbErrored) {
391 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
392 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800393 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700394 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800395 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
396 mUsbTether.setEnabled(false);
397 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800398 } else {
399 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
400 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800401 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800402 }
403 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800404
Danica Chang32711b62010-08-10 18:41:29 -0700405 private void updateBluetoothState(String[] available, String[] tethered,
406 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800407 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700408 for (String s: errored) {
409 for (String regex : mBluetoothRegexs) {
410 if (s.matches(regex)) bluetoothErrored = true;
411 }
412 }
413
414 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800415 if (adapter == null)
416 return;
Danica Chang32711b62010-08-10 18:41:29 -0700417 int btState = adapter.getState();
418 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
419 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500420 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700421 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
422 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700423 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700424 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700425 BluetoothPan bluetoothPan = mBluetoothPan.get();
426 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
427 bluetoothPan.isTetheringOn()) {
428 mBluetoothTether.setChecked(true);
429 mBluetoothTether.setEnabled(true);
430 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
431 if (bluetoothTethered > 1) {
432 String summary = getString(
433 R.string.bluetooth_tethering_devices_connected_subtext,
434 bluetoothTethered);
435 mBluetoothTether.setSummary(summary);
436 } else if (bluetoothTethered == 1) {
437 mBluetoothTether.setSummary(
438 R.string.bluetooth_tethering_device_connected_subtext);
439 } else if (bluetoothErrored) {
440 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
441 } else {
442 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
443 }
444 } else {
445 mBluetoothTether.setEnabled(true);
446 mBluetoothTether.setChecked(false);
447 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
448 }
Danica Chang32711b62010-08-10 18:41:29 -0700449 }
450 }
451
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700452 public boolean onPreferenceChange(Preference preference, Object value) {
453 boolean enable = (Boolean) value;
454
455 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700456 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700457 } else {
458 mWifiApEnabler.setSoftapEnabled(false);
459 }
460 return false;
461 }
462
Irfan Sheriff01b32362011-11-04 12:08:56 -0700463 boolean isProvisioningNeeded() {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800464 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)) {
465 return false;
466 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700467 return mProvisionApp.length == 2;
468 }
469
470 private void startProvisioningIfNecessary(int choice) {
471 mTetherChoice = choice;
472 if (isProvisioningNeeded()) {
473 Intent intent = new Intent(Intent.ACTION_MAIN);
474 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
475 startActivityForResult(intent, PROVISION_REQUEST);
476 } else {
477 startTethering();
478 }
479 }
480
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700481 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
482 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700483 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700484 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700485 startTethering();
486 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700487 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700488 //Wifi tethering is never turned on until afterwards
489 switch (mTetherChoice) {
490 case BLUETOOTH_TETHERING:
491 mBluetoothTether.setChecked(false);
492 break;
493 case USB_TETHERING:
494 mUsbTether.setChecked(false);
495 break;
496 }
497 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700498 }
499 }
500 }
501
Irfan Sheriff01b32362011-11-04 12:08:56 -0700502 private void startTethering() {
503 switch (mTetherChoice) {
504 case WIFI_TETHERING:
505 mWifiApEnabler.setSoftapEnabled(true);
506 break;
507 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700508 // turn on Bluetooth first
509 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
510 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800511 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700512 adapter.enable();
513 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
514 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800515 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700516 BluetoothPan bluetoothPan = mBluetoothPan.get();
517 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800518 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700519 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700520 break;
521 case USB_TETHERING:
522 setUsbTethering(true);
523 break;
524 default:
525 //should not happen
526 break;
527 }
528 }
529
530 private void setUsbTethering(boolean enabled) {
531 ConnectivityManager cm =
532 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800533 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700534 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700535 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
536 return;
537 }
538 mUsbTether.setSummary("");
539 }
540
541 @Override
542 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
543 ConnectivityManager cm =
544 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
545
546 if (preference == mUsbTether) {
547 boolean newState = mUsbTether.isChecked();
548
549 if (newState) {
550 startProvisioningIfNecessary(USB_TETHERING);
551 } else {
552 setUsbTethering(newState);
553 }
554 } else if (preference == mBluetoothTether) {
555 boolean bluetoothTetherState = mBluetoothTether.isChecked();
556
557 if (bluetoothTetherState) {
558 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700559 } else {
560 boolean errored = false;
561
Danica Chang32711b62010-08-10 18:41:29 -0700562 String [] tethered = cm.getTetheredIfaces();
563 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
564 if (bluetoothIface != null &&
565 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
566 errored = true;
567 }
568
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700569 BluetoothPan bluetoothPan = mBluetoothPan.get();
570 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700571 if (errored) {
572 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
573 } else {
574 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
575 }
576 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800577 } else if (preference == mCreateNetwork) {
578 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800579 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700580
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700581 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800582 }
583
Jake Hamby436b29e2011-02-07 18:21:25 -0800584 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800585 for (String iface : ifaces) {
586 for (String regex : regexes) {
587 if (iface.matches(regex)) {
588 return iface;
589 }
590 }
591 }
592 return null;
593 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800594
595 public void onClick(DialogInterface dialogInterface, int button) {
596 if (button == DialogInterface.BUTTON_POSITIVE) {
597 mWifiConfig = mDialog.getConfig();
598 if (mWifiConfig != null) {
599 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700600 * if soft AP is stopped, bring up
601 * else restart with new config
602 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800603 */
604 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700605 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800606 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800607 } else {
608 mWifiManager.setWifiApConfiguration(mWifiConfig);
609 }
610 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
611 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
612 mWifiConfig.SSID,
613 mSecurityType[index]));
614 }
615 }
616 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700617
618 @Override
619 public int getHelpResource() {
620 return R.string.help_url_tether;
621 }
Amith Yamasani394eaa22013-06-11 11:04:44 -0700622
623 /**
624 * Checks whether this screen will have anything to show on this device. This is called by
625 * the shortcut picker for Settings shortcuts (home screen widget).
626 * @param context a context object for getting a system service.
627 * @return whether Tether & portable hotspot should be shown in the shortcuts picker.
628 */
629 public static boolean showInShortcuts(Context context) {
630 final ConnectivityManager cm =
631 (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
632 final boolean isSecondaryUser = UserHandle.myUserId() != UserHandle.USER_OWNER;
633 return !isSecondaryUser && cm.isTetheringSupported();
634 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800635}