blob: ee8900e499ea6a4d63edce032052e1d796a76701 [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
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070019import android.app.Activity;
Irfan Sheriff0b266962010-04-06 15:16:44 -070020import android.app.Dialog;
Danica Chang32711b62010-08-10 18:41:29 -070021import android.bluetooth.BluetoothAdapter;
22import android.bluetooth.BluetoothPan;
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -080023import android.bluetooth.BluetoothProfile;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080024import android.content.BroadcastReceiver;
25import android.content.Context;
Amith Yamasani84a042c2011-03-02 11:25:04 -080026import android.content.DialogInterface;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080027import android.content.Intent;
28import android.content.IntentFilter;
PauloftheWest29aab7a2014-09-26 16:05:44 -070029import android.content.pm.PackageManager;
Mike Lockwood69a09572011-07-19 13:30:03 -070030import android.hardware.usb.UsbManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080031import android.net.ConnectivityManager;
Amith Yamasani84a042c2011-03-02 11:25:04 -080032import android.net.wifi.WifiConfiguration;
33import android.net.wifi.WifiManager;
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070034import android.os.Bundle;
Mike Lockwood26dad3e2010-03-03 06:19:55 -050035import android.os.Environment;
Amith Yamasani394eaa22013-06-11 11:04:44 -070036import android.os.UserManager;
Jason Monk39b46742015-09-10 15:52:51 -040037import android.support.v14.preference.SwitchPreference;
38import android.support.v7.preference.Preference;
39import android.support.v7.preference.PreferenceScreen;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040040import android.widget.TextView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080041
Chris Wren8a963ba2015-03-20 10:29:14 -040042import com.android.internal.logging.MetricsLogger;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080043import com.android.settings.wifi.WifiApDialog;
44import com.android.settings.wifi.WifiApEnabler;
Jason Monk4896c012015-02-11 13:30:41 -050045import com.android.settingslib.TetherUtil;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080046
Robert Greenwaltc4764d22010-02-12 14:21:37 -080047import java.util.ArrayList;
Robert Greenwaltf60b92b2012-09-13 15:02:00 -070048import java.util.concurrent.atomic.AtomicReference;
Matthew Xieb5f144a2012-06-28 18:41:54 -070049
Jason Monk39b46742015-09-10 15:52:51 -040050import static com.android.settingslib.TetherUtil.TETHERING_BLUETOOTH;
51import static com.android.settingslib.TetherUtil.TETHERING_INVALID;
52import static com.android.settingslib.TetherUtil.TETHERING_USB;
53import static com.android.settingslib.TetherUtil.TETHERING_WIFI;
54
Robert Greenwalt3901edb2010-01-26 10:22:37 -080055/*
56 * Displays preferences for Tethering.
57 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080058public class TetherSettings extends SettingsPreferenceFragment
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070059 implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
zzy7f38f472012-04-14 17:25:17 -070060 private static final String TAG = "TetherSettings";
Danica Chang32711b62010-08-10 18:41:29 -070061
Robert Greenwalt3901edb2010-01-26 10:22:37 -080062 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080063 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070064 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Rohit Sisodia37155e92014-09-22 16:34:56 -050065 private static final String TETHER_CHOICE = "TETHER_TYPE";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080066
Amith Yamasanid3fed682012-04-27 17:38:40 -070067 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070068
PauloftheWest656c88c2014-08-24 11:59:35 -070069 private SwitchPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080070
Irfan Sheriff65cff172010-02-08 10:46:30 -080071 private WifiApEnabler mWifiApEnabler;
PauloftheWest656c88c2014-08-24 11:59:35 -070072 private SwitchPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070073
PauloftheWest656c88c2014-08-24 11:59:35 -070074 private SwitchPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070075
Robert Greenwalt3901edb2010-01-26 10:22:37 -080076 private BroadcastReceiver mTetherChangeReceiver;
77
Robert Greenwaltc4764d22010-02-12 14:21:37 -080078 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080079
80 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080081
Danica Chang32711b62010-08-10 18:41:29 -070082 private String[] mBluetoothRegexs;
Robert Greenwalt4f1970f2012-09-16 17:03:08 -070083 private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<BluetoothPan>();
Danica Chang32711b62010-08-10 18:41:29 -070084
Amith Yamasani84a042c2011-03-02 11:25:04 -080085 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
86 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
87
88 private String[] mSecurityType;
89 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080090
91 private WifiApDialog mDialog;
92 private WifiManager mWifiManager;
93 private WifiConfiguration mWifiConfig = null;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040094 private UserManager mUm;
Amith Yamasani84a042c2011-03-02 11:25:04 -080095
Mike Lockwood69a09572011-07-19 13:30:03 -070096 private boolean mUsbConnected;
97 private boolean mMassStorageActive;
98
Jake Hambyc777ee22011-03-04 15:37:39 -080099 private boolean mBluetoothEnableForTether;
100
Irfan Sheriff01b32362011-11-04 12:08:56 -0700101 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
Jason Monk4896c012015-02-11 13:30:41 -0500102 private int mTetherChoice = TETHERING_INVALID;
Irfan Sheriff01b32362011-11-04 12:08:56 -0700103
104 /* Stores the package name and the class name of the provisioning app */
105 private String[] mProvisionApp;
106 private static final int PROVISION_REQUEST = 0;
107
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400108 private boolean mUnavailable;
109
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800110 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -0400111 protected int getMetricsCategory() {
112 return MetricsLogger.TETHER;
113 }
114
115 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700116 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800117 super.onCreate(icicle);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500118
119 if(icicle != null) {
120 mTetherChoice = icicle.getInt(TETHER_CHOICE);
121 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800122 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800123
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400124 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
125
Nicolas Prevota5df4772015-06-02 22:27:49 +0100126 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)
Xiaohui Chen44879a32015-07-22 13:53:22 -0700127 || !mUm.isAdminUser()) {
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400128 mUnavailable = true;
Jason Monk39b46742015-09-10 15:52:51 -0400129 setPreferenceScreen(new PreferenceScreen(getPrefContext(), null));
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400130 return;
131 }
132
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700133 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800134 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
135 if (adapter != null) {
136 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
137 BluetoothProfile.PAN);
138 }
139
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700140 mEnableWifiAp =
PauloftheWest656c88c2014-08-24 11:59:35 -0700141 (SwitchPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700142 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
PauloftheWest656c88c2014-08-24 11:59:35 -0700143 mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
144 mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800145
146 ConnectivityManager cm =
147 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800148
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800149 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800150 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700151 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
152
Amith Yamasanie419bc12011-02-14 14:19:08 -0800153 final boolean usbAvailable = mUsbRegexs.length != 0;
154 final boolean wifiAvailable = mWifiRegexs.length != 0;
155 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700156
Danica Chang32711b62010-08-10 18:41:29 -0700157 if (!usbAvailable || Utils.isMonkeyRunning()) {
158 getPreferenceScreen().removePreference(mUsbTether);
159 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800160
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800161 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700162 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700163 initWifiTethering();
164 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700165 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800166 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700167 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800168
Danica Chang32711b62010-08-10 18:41:29 -0700169 if (!bluetoothAvailable) {
170 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700171 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700172 BluetoothPan pan = mBluetoothPan.get();
173 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700174 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700175 } else {
176 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700177 }
178 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800179
Irfan Sheriff01b32362011-11-04 12:08:56 -0700180 mProvisionApp = getResources().getStringArray(
181 com.android.internal.R.array.config_mobile_hotspot_provision_app);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800182 }
183
Rohit Sisodia37155e92014-09-22 16:34:56 -0500184 @Override
185 public void onSaveInstanceState(Bundle savedInstanceState) {
186 savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice);
187 super.onSaveInstanceState(savedInstanceState);
188 }
189
Amith Yamasani84a042c2011-03-02 11:25:04 -0800190 private void initWifiTethering() {
191 final Activity activity = getActivity();
192 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
193 mWifiConfig = mWifiManager.getWifiApConfiguration();
194 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
195
196 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800197
198 if (mWifiConfig == null) {
199 final String s = activity.getString(
200 com.android.internal.R.string.wifi_tether_configure_ssid_default);
201 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
202 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
203 } else {
204 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
205 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
206 mWifiConfig.SSID,
207 mSecurityType[index]));
208 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800209 }
210
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800211 private BluetoothProfile.ServiceListener mProfileServiceListener =
212 new BluetoothProfile.ServiceListener() {
213 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700214 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800215 }
216 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700217 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800218 }
219 };
220
Irfan Sheriff0b266962010-04-06 15:16:44 -0700221 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700222 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700223 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800224 final Activity activity = getActivity();
225 mDialog = new WifiApDialog(activity, this, mWifiConfig);
226 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700227 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800228
Irfan Sheriff0b266962010-04-06 15:16:44 -0700229 return null;
230 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800231
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800232 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700233 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800234 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800235 String action = intent.getAction();
236 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800237 // TODO - this should understand the interface types
238 ArrayList<String> available = intent.getStringArrayListExtra(
239 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
240 ArrayList<String> active = intent.getStringArrayListExtra(
241 ConnectivityManager.EXTRA_ACTIVE_TETHER);
242 ArrayList<String> errored = intent.getStringArrayListExtra(
243 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700244 updateState(available.toArray(new String[available.size()]),
245 active.toArray(new String[active.size()]),
246 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700247 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
248 mMassStorageActive = true;
249 updateState();
250 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
251 mMassStorageActive = false;
252 updateState();
253 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
254 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800255 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800256 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
257 if (mBluetoothEnableForTether) {
258 switch (intent
259 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
260 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700261 BluetoothPan bluetoothPan = mBluetoothPan.get();
262 if (bluetoothPan != null) {
263 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700264 mBluetoothEnableForTether = false;
265 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800266 break;
267
268 case BluetoothAdapter.STATE_OFF:
269 case BluetoothAdapter.ERROR:
270 mBluetoothEnableForTether = false;
271 break;
272
273 default:
274 // ignore transition states
275 }
276 }
Danica Chang32711b62010-08-10 18:41:29 -0700277 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800278 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800279 }
280 }
281
282 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700283 public void onStart() {
284 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800285
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400286 if (mUnavailable) {
287 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
Jason Monk39b46742015-09-10 15:52:51 -0400288 setEmptyView(emptyView);
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400289 if (emptyView != null) {
290 emptyView.setText(R.string.tethering_settings_not_available);
291 }
292 return;
293 }
294
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700295 final Activity activity = getActivity();
296
Mike Lockwood69a09572011-07-19 13:30:03 -0700297 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800298 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700299 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700300 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800301
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800302 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700303 filter.addAction(UsbManager.ACTION_USB_STATE);
304 activity.registerReceiver(mTetherChangeReceiver, filter);
305
306 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800307 filter.addAction(Intent.ACTION_MEDIA_SHARED);
308 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
309 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700310 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800311
Danica Chang32711b62010-08-10 18:41:29 -0700312 filter = new IntentFilter();
313 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700314 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700315
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700316 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700317 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700318 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700319 mWifiApEnabler.resume();
320 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800321
322 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800323 }
324
325 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700326 public void onStop() {
327 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400328
329 if (mUnavailable) {
330 return;
331 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700332 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800333 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700334 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700335 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700336 mWifiApEnabler.pause();
337 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800338 }
339
Robert Greenwalt209177a2010-03-04 13:29:02 -0800340 private void updateState() {
341 ConnectivityManager cm =
342 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
343
344 String[] available = cm.getTetherableIfaces();
345 String[] tethered = cm.getTetheredIfaces();
346 String[] errored = cm.getTetheringErroredIfaces();
347 updateState(available, tethered, errored);
348 }
349
Ben Clark0008d212010-07-27 16:20:59 +0100350 private void updateState(String[] available, String[] tethered,
351 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700352 updateUsbState(available, tethered, errored);
353 updateBluetoothState(available, tethered, errored);
354 }
355
356
357 private void updateUsbState(String[] available, String[] tethered,
358 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800359 ConnectivityManager cm =
360 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700361 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800362 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100363 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800364 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800365 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800366 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
367 usbError = cm.getLastTetherError(s);
368 }
369 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800370 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800371 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800372 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100373 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800374 for (String regex : mUsbRegexs) {
375 if (s.matches(regex)) usbTethered = true;
376 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800377 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800378 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100379 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800380 for (String regex : mUsbRegexs) {
381 if (s.matches(regex)) usbErrored = true;
382 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800383 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800384
385 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800386 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
387 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800388 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800389 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800390 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
391 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
392 } else {
393 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
394 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800395 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800396 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800397 } else if (usbErrored) {
398 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
399 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800400 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700401 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800402 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
403 mUsbTether.setEnabled(false);
404 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800405 } else {
406 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
407 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800408 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800409 }
410 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800411
Danica Chang32711b62010-08-10 18:41:29 -0700412 private void updateBluetoothState(String[] available, String[] tethered,
413 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800414 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700415 for (String s: errored) {
416 for (String regex : mBluetoothRegexs) {
417 if (s.matches(regex)) bluetoothErrored = true;
418 }
419 }
420
421 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800422 if (adapter == null)
423 return;
Danica Chang32711b62010-08-10 18:41:29 -0700424 int btState = adapter.getState();
425 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
426 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500427 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700428 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
429 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700430 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700431 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700432 BluetoothPan bluetoothPan = mBluetoothPan.get();
433 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
434 bluetoothPan.isTetheringOn()) {
435 mBluetoothTether.setChecked(true);
436 mBluetoothTether.setEnabled(true);
437 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
438 if (bluetoothTethered > 1) {
439 String summary = getString(
440 R.string.bluetooth_tethering_devices_connected_subtext,
441 bluetoothTethered);
442 mBluetoothTether.setSummary(summary);
443 } else if (bluetoothTethered == 1) {
444 mBluetoothTether.setSummary(
445 R.string.bluetooth_tethering_device_connected_subtext);
446 } else if (bluetoothErrored) {
447 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
448 } else {
449 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
450 }
451 } else {
452 mBluetoothTether.setEnabled(true);
453 mBluetoothTether.setChecked(false);
454 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
455 }
Danica Chang32711b62010-08-10 18:41:29 -0700456 }
457 }
458
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700459 public boolean onPreferenceChange(Preference preference, Object value) {
460 boolean enable = (Boolean) value;
461
462 if (enable) {
Jason Monk4896c012015-02-11 13:30:41 -0500463 startProvisioningIfNecessary(TETHERING_WIFI);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700464 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500465 if (TetherUtil.isProvisioningNeeded(getActivity())) {
466 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_WIFI);
Jason Monk37832d62014-12-10 17:21:51 -0500467 }
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700468 mWifiApEnabler.setSoftapEnabled(false);
469 }
470 return false;
471 }
472
PauloftheWest29aab7a2014-09-26 16:05:44 -0700473 public static boolean isProvisioningNeededButUnavailable(Context context) {
Jason Monk4896c012015-02-11 13:30:41 -0500474 return (TetherUtil.isProvisioningNeeded(context)
475 && !isIntentAvailable(context));
PauloftheWest29aab7a2014-09-26 16:05:44 -0700476 }
477
Jason Monk4896c012015-02-11 13:30:41 -0500478 private static boolean isIntentAvailable(Context context) {
479 String[] provisionApp = context.getResources().getStringArray(
480 com.android.internal.R.array.config_mobile_hotspot_provision_app);
PauloftheWest29aab7a2014-09-26 16:05:44 -0700481 final PackageManager packageManager = context.getPackageManager();
482 Intent intent = new Intent(Intent.ACTION_MAIN);
483 intent.setClassName(provisionApp[0], provisionApp[1]);
484
485 return (packageManager.queryIntentActivities(intent,
486 PackageManager.MATCH_DEFAULT_ONLY).size() > 0);
487 }
488
Irfan Sheriff01b32362011-11-04 12:08:56 -0700489 private void startProvisioningIfNecessary(int choice) {
490 mTetherChoice = choice;
Jason Monk4896c012015-02-11 13:30:41 -0500491 if (TetherUtil.isProvisioningNeeded(getActivity())) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700492 Intent intent = new Intent(Intent.ACTION_MAIN);
493 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500494 intent.putExtra(TETHER_CHOICE, mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700495 startActivityForResult(intent, PROVISION_REQUEST);
496 } else {
497 startTethering();
498 }
499 }
500
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700501 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
502 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700503 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700504 if (resultCode == Activity.RESULT_OK) {
Jason Monk37832d62014-12-10 17:21:51 -0500505 TetherService.scheduleRecheckAlarm(getActivity(), mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700506 startTethering();
507 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700508 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700509 //Wifi tethering is never turned on until afterwards
510 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500511 case TETHERING_BLUETOOTH:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700512 mBluetoothTether.setChecked(false);
513 break;
Jason Monk4896c012015-02-11 13:30:41 -0500514 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700515 mUsbTether.setChecked(false);
516 break;
517 }
Jason Monk4896c012015-02-11 13:30:41 -0500518 mTetherChoice = TETHERING_INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700519 }
520 }
521 }
522
Irfan Sheriff01b32362011-11-04 12:08:56 -0700523 private void startTethering() {
524 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500525 case TETHERING_WIFI:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700526 mWifiApEnabler.setSoftapEnabled(true);
527 break;
Jason Monk4896c012015-02-11 13:30:41 -0500528 case TETHERING_BLUETOOTH:
Danica Chang32711b62010-08-10 18:41:29 -0700529 // turn on Bluetooth first
530 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
531 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800532 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700533 adapter.enable();
534 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
535 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800536 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700537 BluetoothPan bluetoothPan = mBluetoothPan.get();
538 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800539 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700540 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700541 break;
Jason Monk4896c012015-02-11 13:30:41 -0500542 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700543 setUsbTethering(true);
544 break;
545 default:
546 //should not happen
547 break;
548 }
549 }
550
551 private void setUsbTethering(boolean enabled) {
552 ConnectivityManager cm =
553 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800554 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700555 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700556 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
557 return;
558 }
559 mUsbTether.setSummary("");
560 }
561
562 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400563 public boolean onPreferenceTreeClick(Preference preference) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700564 ConnectivityManager cm =
565 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
566
567 if (preference == mUsbTether) {
568 boolean newState = mUsbTether.isChecked();
569
570 if (newState) {
Jason Monk4896c012015-02-11 13:30:41 -0500571 startProvisioningIfNecessary(TETHERING_USB);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700572 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500573 if (TetherUtil.isProvisioningNeeded(getActivity())) {
574 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_USB);
Jason Monk37832d62014-12-10 17:21:51 -0500575 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700576 setUsbTethering(newState);
577 }
578 } else if (preference == mBluetoothTether) {
579 boolean bluetoothTetherState = mBluetoothTether.isChecked();
580
581 if (bluetoothTetherState) {
Jason Monk4896c012015-02-11 13:30:41 -0500582 startProvisioningIfNecessary(TETHERING_BLUETOOTH);
Danica Chang32711b62010-08-10 18:41:29 -0700583 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500584 if (TetherUtil.isProvisioningNeeded(getActivity())) {
585 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_BLUETOOTH);
Jason Monk37832d62014-12-10 17:21:51 -0500586 }
Danica Chang32711b62010-08-10 18:41:29 -0700587 boolean errored = false;
588
Danica Chang32711b62010-08-10 18:41:29 -0700589 String [] tethered = cm.getTetheredIfaces();
590 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
591 if (bluetoothIface != null &&
592 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
593 errored = true;
594 }
595
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700596 BluetoothPan bluetoothPan = mBluetoothPan.get();
597 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700598 if (errored) {
599 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
600 } else {
601 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
602 }
603 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800604 } else if (preference == mCreateNetwork) {
605 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800606 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700607
Jason Monk39b46742015-09-10 15:52:51 -0400608 return super.onPreferenceTreeClick(preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800609 }
610
Jake Hamby436b29e2011-02-07 18:21:25 -0800611 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800612 for (String iface : ifaces) {
613 for (String regex : regexes) {
614 if (iface.matches(regex)) {
615 return iface;
616 }
617 }
618 }
619 return null;
620 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800621
622 public void onClick(DialogInterface dialogInterface, int button) {
623 if (button == DialogInterface.BUTTON_POSITIVE) {
624 mWifiConfig = mDialog.getConfig();
625 if (mWifiConfig != null) {
626 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700627 * if soft AP is stopped, bring up
628 * else restart with new config
629 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800630 */
631 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700632 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800633 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800634 } else {
635 mWifiManager.setWifiApConfiguration(mWifiConfig);
636 }
637 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
638 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
639 mWifiConfig.SSID,
640 mSecurityType[index]));
641 }
642 }
643 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700644
645 @Override
646 public int getHelpResource() {
647 return R.string.help_url_tether;
648 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800649}