blob: 45d8dee6cdc5deb12fb696c5596f5e43c7577851 [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";
Rohit Sisodia37155e92014-09-22 16:34:56 -050067 private static final String TETHER_CHOICE = "TETHER_TYPE";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080068
Amith Yamasanid3fed682012-04-27 17:38:40 -070069 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070070
71 private WebView mView;
PauloftheWest656c88c2014-08-24 11:59:35 -070072 private SwitchPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080073
Irfan Sheriff65cff172010-02-08 10:46:30 -080074 private WifiApEnabler mWifiApEnabler;
PauloftheWest656c88c2014-08-24 11:59:35 -070075 private SwitchPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070076
PauloftheWest656c88c2014-08-24 11:59:35 -070077 private SwitchPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070078
Robert Greenwalt3901edb2010-01-26 10:22:37 -080079 private BroadcastReceiver mTetherChangeReceiver;
80
Robert Greenwaltc4764d22010-02-12 14:21:37 -080081 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080082
83 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080084
Danica Chang32711b62010-08-10 18:41:29 -070085 private String[] mBluetoothRegexs;
Robert Greenwalt4f1970f2012-09-16 17:03:08 -070086 private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<BluetoothPan>();
Danica Chang32711b62010-08-10 18:41:29 -070087
Amith Yamasani84a042c2011-03-02 11:25:04 -080088 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
89 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
90
91 private String[] mSecurityType;
92 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080093
94 private WifiApDialog mDialog;
95 private WifiManager mWifiManager;
96 private WifiConfiguration mWifiConfig = null;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040097 private UserManager mUm;
Amith Yamasani84a042c2011-03-02 11:25:04 -080098
Mike Lockwood69a09572011-07-19 13:30:03 -070099 private boolean mUsbConnected;
100 private boolean mMassStorageActive;
101
Jake Hambyc777ee22011-03-04 15:37:39 -0800102 private boolean mBluetoothEnableForTether;
103
Irfan Sheriff01b32362011-11-04 12:08:56 -0700104 private static final int INVALID = -1;
105 private static final int WIFI_TETHERING = 0;
106 private static final int USB_TETHERING = 1;
107 private static final int BLUETOOTH_TETHERING = 2;
108
109 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
110 private int mTetherChoice = INVALID;
111
112 /* Stores the package name and the class name of the provisioning app */
113 private String[] mProvisionApp;
114 private static final int PROVISION_REQUEST = 0;
115
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400116 private boolean mUnavailable;
117
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800118 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700119 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800120 super.onCreate(icicle);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500121
122 if(icicle != null) {
123 mTetherChoice = icicle.getInt(TETHER_CHOICE);
124 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800125 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800126
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400127 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
128
129 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
130 mUnavailable = true;
131 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
132 return;
133 }
134
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700135 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800136 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
137 if (adapter != null) {
138 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
139 BluetoothProfile.PAN);
140 }
141
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700142 mEnableWifiAp =
PauloftheWest656c88c2014-08-24 11:59:35 -0700143 (SwitchPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700144 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
PauloftheWest656c88c2014-08-24 11:59:35 -0700145 mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
146 mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800147
148 ConnectivityManager cm =
149 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800150
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800151 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800152 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700153 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
154
Amith Yamasanie419bc12011-02-14 14:19:08 -0800155 final boolean usbAvailable = mUsbRegexs.length != 0;
156 final boolean wifiAvailable = mWifiRegexs.length != 0;
157 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700158
Danica Chang32711b62010-08-10 18:41:29 -0700159 if (!usbAvailable || Utils.isMonkeyRunning()) {
160 getPreferenceScreen().removePreference(mUsbTether);
161 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800162
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800163 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700164 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700165 initWifiTethering();
166 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700167 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800168 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700169 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800170
Danica Chang32711b62010-08-10 18:41:29 -0700171 if (!bluetoothAvailable) {
172 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700173 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700174 BluetoothPan pan = mBluetoothPan.get();
175 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700176 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700177 } else {
178 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700179 }
180 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800181
Irfan Sheriff01b32362011-11-04 12:08:56 -0700182 mProvisionApp = getResources().getStringArray(
183 com.android.internal.R.array.config_mobile_hotspot_provision_app);
184
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700185 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800186 }
187
Rohit Sisodia37155e92014-09-22 16:34:56 -0500188 @Override
189 public void onSaveInstanceState(Bundle savedInstanceState) {
190 savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice);
191 super.onSaveInstanceState(savedInstanceState);
192 }
193
Amith Yamasani84a042c2011-03-02 11:25:04 -0800194 private void initWifiTethering() {
195 final Activity activity = getActivity();
196 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
197 mWifiConfig = mWifiManager.getWifiApConfiguration();
198 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
199
200 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800201
202 if (mWifiConfig == null) {
203 final String s = activity.getString(
204 com.android.internal.R.string.wifi_tether_configure_ssid_default);
205 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
206 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
207 } else {
208 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
209 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
210 mWifiConfig.SSID,
211 mSecurityType[index]));
212 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800213 }
214
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800215 private BluetoothProfile.ServiceListener mProfileServiceListener =
216 new BluetoothProfile.ServiceListener() {
217 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700218 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800219 }
220 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700221 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800222 }
223 };
224
Irfan Sheriff0b266962010-04-06 15:16:44 -0700225 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700226 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700227 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800228 final Activity activity = getActivity();
229 mDialog = new WifiApDialog(activity, this, mWifiConfig);
230 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700231 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800232
Irfan Sheriff0b266962010-04-06 15:16:44 -0700233 return null;
234 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800235
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800236 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700237 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800238 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800239 String action = intent.getAction();
240 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800241 // TODO - this should understand the interface types
242 ArrayList<String> available = intent.getStringArrayListExtra(
243 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
244 ArrayList<String> active = intent.getStringArrayListExtra(
245 ConnectivityManager.EXTRA_ACTIVE_TETHER);
246 ArrayList<String> errored = intent.getStringArrayListExtra(
247 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700248 updateState(available.toArray(new String[available.size()]),
249 active.toArray(new String[active.size()]),
250 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700251 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
252 mMassStorageActive = true;
253 updateState();
254 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
255 mMassStorageActive = false;
256 updateState();
257 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
258 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800259 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800260 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
261 if (mBluetoothEnableForTether) {
262 switch (intent
263 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
264 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700265 BluetoothPan bluetoothPan = mBluetoothPan.get();
266 if (bluetoothPan != null) {
267 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700268 mBluetoothEnableForTether = false;
269 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800270 break;
271
272 case BluetoothAdapter.STATE_OFF:
273 case BluetoothAdapter.ERROR:
274 mBluetoothEnableForTether = false;
275 break;
276
277 default:
278 // ignore transition states
279 }
280 }
Danica Chang32711b62010-08-10 18:41:29 -0700281 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800282 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800283 }
284 }
285
286 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700287 public void onStart() {
288 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800289
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400290 if (mUnavailable) {
291 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
292 getListView().setEmptyView(emptyView);
293 if (emptyView != null) {
294 emptyView.setText(R.string.tethering_settings_not_available);
295 }
296 return;
297 }
298
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700299 final Activity activity = getActivity();
300
Mike Lockwood69a09572011-07-19 13:30:03 -0700301 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800302 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700303 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700304 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800305
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800306 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700307 filter.addAction(UsbManager.ACTION_USB_STATE);
308 activity.registerReceiver(mTetherChangeReceiver, filter);
309
310 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800311 filter.addAction(Intent.ACTION_MEDIA_SHARED);
312 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
313 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700314 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800315
Danica Chang32711b62010-08-10 18:41:29 -0700316 filter = new IntentFilter();
317 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700318 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700319
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700320 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700321 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700322 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700323 mWifiApEnabler.resume();
324 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800325
326 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800327 }
328
329 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700330 public void onStop() {
331 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400332
333 if (mUnavailable) {
334 return;
335 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700336 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800337 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700338 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700339 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700340 mWifiApEnabler.pause();
341 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800342 }
343
Robert Greenwalt209177a2010-03-04 13:29:02 -0800344 private void updateState() {
345 ConnectivityManager cm =
346 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
347
348 String[] available = cm.getTetherableIfaces();
349 String[] tethered = cm.getTetheredIfaces();
350 String[] errored = cm.getTetheringErroredIfaces();
351 updateState(available, tethered, errored);
352 }
353
Ben Clark0008d212010-07-27 16:20:59 +0100354 private void updateState(String[] available, String[] tethered,
355 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700356 updateUsbState(available, tethered, errored);
357 updateBluetoothState(available, tethered, errored);
358 }
359
360
361 private void updateUsbState(String[] available, String[] tethered,
362 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800363 ConnectivityManager cm =
364 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700365 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800366 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100367 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800368 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800369 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800370 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
371 usbError = cm.getLastTetherError(s);
372 }
373 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800374 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800375 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800376 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100377 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800378 for (String regex : mUsbRegexs) {
379 if (s.matches(regex)) usbTethered = true;
380 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800381 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800382 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100383 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800384 for (String regex : mUsbRegexs) {
385 if (s.matches(regex)) usbErrored = true;
386 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800387 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800388
389 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800390 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
391 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800392 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800393 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800394 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
395 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
396 } else {
397 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
398 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800399 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800400 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800401 } else if (usbErrored) {
402 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
403 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800404 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700405 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800406 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
407 mUsbTether.setEnabled(false);
408 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800409 } else {
410 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
411 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800412 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800413 }
414 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800415
Danica Chang32711b62010-08-10 18:41:29 -0700416 private void updateBluetoothState(String[] available, String[] tethered,
417 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800418 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700419 for (String s: errored) {
420 for (String regex : mBluetoothRegexs) {
421 if (s.matches(regex)) bluetoothErrored = true;
422 }
423 }
424
425 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800426 if (adapter == null)
427 return;
Danica Chang32711b62010-08-10 18:41:29 -0700428 int btState = adapter.getState();
429 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
430 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500431 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700432 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
433 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700434 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700435 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700436 BluetoothPan bluetoothPan = mBluetoothPan.get();
437 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
438 bluetoothPan.isTetheringOn()) {
439 mBluetoothTether.setChecked(true);
440 mBluetoothTether.setEnabled(true);
441 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
442 if (bluetoothTethered > 1) {
443 String summary = getString(
444 R.string.bluetooth_tethering_devices_connected_subtext,
445 bluetoothTethered);
446 mBluetoothTether.setSummary(summary);
447 } else if (bluetoothTethered == 1) {
448 mBluetoothTether.setSummary(
449 R.string.bluetooth_tethering_device_connected_subtext);
450 } else if (bluetoothErrored) {
451 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
452 } else {
453 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
454 }
455 } else {
456 mBluetoothTether.setEnabled(true);
457 mBluetoothTether.setChecked(false);
458 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
459 }
Danica Chang32711b62010-08-10 18:41:29 -0700460 }
461 }
462
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700463 public boolean onPreferenceChange(Preference preference, Object value) {
464 boolean enable = (Boolean) value;
465
466 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700467 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700468 } else {
469 mWifiApEnabler.setSoftapEnabled(false);
470 }
471 return false;
472 }
473
Irfan Sheriff01b32362011-11-04 12:08:56 -0700474 boolean isProvisioningNeeded() {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800475 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)) {
476 return false;
477 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700478 return mProvisionApp.length == 2;
479 }
480
481 private void startProvisioningIfNecessary(int choice) {
482 mTetherChoice = choice;
483 if (isProvisioningNeeded()) {
484 Intent intent = new Intent(Intent.ACTION_MAIN);
485 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500486 intent.putExtra(TETHER_CHOICE, mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700487 startActivityForResult(intent, PROVISION_REQUEST);
488 } else {
489 startTethering();
490 }
491 }
492
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700493 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
494 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700495 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700496 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700497 startTethering();
498 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700499 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700500 //Wifi tethering is never turned on until afterwards
501 switch (mTetherChoice) {
502 case BLUETOOTH_TETHERING:
503 mBluetoothTether.setChecked(false);
504 break;
505 case USB_TETHERING:
506 mUsbTether.setChecked(false);
507 break;
508 }
509 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700510 }
511 }
512 }
513
Irfan Sheriff01b32362011-11-04 12:08:56 -0700514 private void startTethering() {
515 switch (mTetherChoice) {
516 case WIFI_TETHERING:
517 mWifiApEnabler.setSoftapEnabled(true);
518 break;
519 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700520 // turn on Bluetooth first
521 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
522 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800523 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700524 adapter.enable();
525 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
526 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800527 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700528 BluetoothPan bluetoothPan = mBluetoothPan.get();
529 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800530 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700531 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700532 break;
533 case USB_TETHERING:
534 setUsbTethering(true);
535 break;
536 default:
537 //should not happen
538 break;
539 }
540 }
541
542 private void setUsbTethering(boolean enabled) {
543 ConnectivityManager cm =
544 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800545 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700546 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700547 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
548 return;
549 }
550 mUsbTether.setSummary("");
551 }
552
553 @Override
554 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
555 ConnectivityManager cm =
556 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
557
558 if (preference == mUsbTether) {
559 boolean newState = mUsbTether.isChecked();
560
561 if (newState) {
562 startProvisioningIfNecessary(USB_TETHERING);
563 } else {
564 setUsbTethering(newState);
565 }
566 } else if (preference == mBluetoothTether) {
567 boolean bluetoothTetherState = mBluetoothTether.isChecked();
568
569 if (bluetoothTetherState) {
570 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700571 } else {
572 boolean errored = false;
573
Danica Chang32711b62010-08-10 18:41:29 -0700574 String [] tethered = cm.getTetheredIfaces();
575 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
576 if (bluetoothIface != null &&
577 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
578 errored = true;
579 }
580
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700581 BluetoothPan bluetoothPan = mBluetoothPan.get();
582 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700583 if (errored) {
584 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
585 } else {
586 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
587 }
588 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800589 } else if (preference == mCreateNetwork) {
590 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800591 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700592
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700593 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800594 }
595
Jake Hamby436b29e2011-02-07 18:21:25 -0800596 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800597 for (String iface : ifaces) {
598 for (String regex : regexes) {
599 if (iface.matches(regex)) {
600 return iface;
601 }
602 }
603 }
604 return null;
605 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800606
607 public void onClick(DialogInterface dialogInterface, int button) {
608 if (button == DialogInterface.BUTTON_POSITIVE) {
609 mWifiConfig = mDialog.getConfig();
610 if (mWifiConfig != null) {
611 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700612 * if soft AP is stopped, bring up
613 * else restart with new config
614 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800615 */
616 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700617 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800618 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800619 } else {
620 mWifiManager.setWifiApConfiguration(mWifiConfig);
621 }
622 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
623 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
624 mWifiConfig.SSID,
625 mSecurityType[index]));
626 }
627 }
628 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700629
630 @Override
631 public int getHelpResource() {
632 return R.string.help_url_tether;
633 }
Amith Yamasani394eaa22013-06-11 11:04:44 -0700634
635 /**
636 * Checks whether this screen will have anything to show on this device. This is called by
637 * the shortcut picker for Settings shortcuts (home screen widget).
638 * @param context a context object for getting a system service.
639 * @return whether Tether & portable hotspot should be shown in the shortcuts picker.
640 */
641 public static boolean showInShortcuts(Context context) {
642 final ConnectivityManager cm =
643 (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
644 final boolean isSecondaryUser = UserHandle.myUserId() != UserHandle.USER_OWNER;
645 return !isSecondaryUser && cm.isTetheringSupported();
646 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800647}