blob: 125411a5c8c8c586735344d77987e608ea4f47ef [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
Jason Monk4896c012015-02-11 13:30:41 -050019import static com.android.settingslib.TetherUtil.TETHERING_INVALID;
20import static com.android.settingslib.TetherUtil.TETHERING_WIFI;
21import static com.android.settingslib.TetherUtil.TETHERING_USB;
22import static com.android.settingslib.TetherUtil.TETHERING_BLUETOOTH;
23
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070024import android.app.Activity;
Irfan Sheriff0b266962010-04-06 15:16:44 -070025import android.app.Dialog;
Danica Chang32711b62010-08-10 18:41:29 -070026import android.bluetooth.BluetoothAdapter;
27import android.bluetooth.BluetoothPan;
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -080028import android.bluetooth.BluetoothProfile;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080029import android.content.BroadcastReceiver;
30import android.content.Context;
Amith Yamasani84a042c2011-03-02 11:25:04 -080031import android.content.DialogInterface;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080032import android.content.Intent;
33import android.content.IntentFilter;
PauloftheWest29aab7a2014-09-26 16:05:44 -070034import android.content.pm.PackageManager;
Mike Lockwood69a09572011-07-19 13:30:03 -070035import android.hardware.usb.UsbManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080036import android.net.ConnectivityManager;
Amith Yamasani84a042c2011-03-02 11:25:04 -080037import android.net.wifi.WifiConfiguration;
38import android.net.wifi.WifiManager;
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070039import android.os.Bundle;
Mike Lockwood26dad3e2010-03-03 06:19:55 -050040import android.os.Environment;
Amith Yamasani394eaa22013-06-11 11:04:44 -070041import android.os.UserManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080042import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080043import android.preference.PreferenceScreen;
PauloftheWest656c88c2014-08-24 11:59:35 -070044import android.preference.SwitchPreference;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040045import android.widget.TextView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080046
Chris Wren8a963ba2015-03-20 10:29:14 -040047import com.android.internal.logging.MetricsLogger;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080048import com.android.settings.wifi.WifiApDialog;
49import com.android.settings.wifi.WifiApEnabler;
Jason Monk4896c012015-02-11 13:30:41 -050050import com.android.settingslib.TetherUtil;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080051
Robert Greenwaltc4764d22010-02-12 14:21:37 -080052import java.util.ArrayList;
Robert Greenwaltf60b92b2012-09-13 15:02:00 -070053import java.util.concurrent.atomic.AtomicReference;
Matthew Xieb5f144a2012-06-28 18:41:54 -070054
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
126 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
127 mUnavailable = true;
128 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
129 return;
130 }
131
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700132 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800133 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
134 if (adapter != null) {
135 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
136 BluetoothProfile.PAN);
137 }
138
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700139 mEnableWifiAp =
PauloftheWest656c88c2014-08-24 11:59:35 -0700140 (SwitchPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700141 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
PauloftheWest656c88c2014-08-24 11:59:35 -0700142 mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
143 mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800144
145 ConnectivityManager cm =
146 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800147
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800148 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800149 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700150 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
151
Amith Yamasanie419bc12011-02-14 14:19:08 -0800152 final boolean usbAvailable = mUsbRegexs.length != 0;
153 final boolean wifiAvailable = mWifiRegexs.length != 0;
154 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700155
Danica Chang32711b62010-08-10 18:41:29 -0700156 if (!usbAvailable || Utils.isMonkeyRunning()) {
157 getPreferenceScreen().removePreference(mUsbTether);
158 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800159
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800160 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700161 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700162 initWifiTethering();
163 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700164 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800165 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700166 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800167
Danica Chang32711b62010-08-10 18:41:29 -0700168 if (!bluetoothAvailable) {
169 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700170 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700171 BluetoothPan pan = mBluetoothPan.get();
172 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700173 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700174 } else {
175 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700176 }
177 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800178
Irfan Sheriff01b32362011-11-04 12:08:56 -0700179 mProvisionApp = getResources().getStringArray(
180 com.android.internal.R.array.config_mobile_hotspot_provision_app);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800181 }
182
Rohit Sisodia37155e92014-09-22 16:34:56 -0500183 @Override
184 public void onSaveInstanceState(Bundle savedInstanceState) {
185 savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice);
186 super.onSaveInstanceState(savedInstanceState);
187 }
188
Amith Yamasani84a042c2011-03-02 11:25:04 -0800189 private void initWifiTethering() {
190 final Activity activity = getActivity();
191 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
192 mWifiConfig = mWifiManager.getWifiApConfiguration();
193 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
194
195 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800196
197 if (mWifiConfig == null) {
198 final String s = activity.getString(
199 com.android.internal.R.string.wifi_tether_configure_ssid_default);
200 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
201 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
202 } else {
203 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
204 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
205 mWifiConfig.SSID,
206 mSecurityType[index]));
207 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800208 }
209
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800210 private BluetoothProfile.ServiceListener mProfileServiceListener =
211 new BluetoothProfile.ServiceListener() {
212 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700213 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800214 }
215 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700216 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800217 }
218 };
219
Irfan Sheriff0b266962010-04-06 15:16:44 -0700220 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700221 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700222 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800223 final Activity activity = getActivity();
224 mDialog = new WifiApDialog(activity, this, mWifiConfig);
225 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700226 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800227
Irfan Sheriff0b266962010-04-06 15:16:44 -0700228 return null;
229 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800230
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800231 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700232 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800233 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800234 String action = intent.getAction();
235 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800236 // TODO - this should understand the interface types
237 ArrayList<String> available = intent.getStringArrayListExtra(
238 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
239 ArrayList<String> active = intent.getStringArrayListExtra(
240 ConnectivityManager.EXTRA_ACTIVE_TETHER);
241 ArrayList<String> errored = intent.getStringArrayListExtra(
242 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700243 updateState(available.toArray(new String[available.size()]),
244 active.toArray(new String[active.size()]),
245 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700246 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
247 mMassStorageActive = true;
248 updateState();
249 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
250 mMassStorageActive = false;
251 updateState();
252 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
253 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800254 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800255 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
256 if (mBluetoothEnableForTether) {
257 switch (intent
258 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
259 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700260 BluetoothPan bluetoothPan = mBluetoothPan.get();
261 if (bluetoothPan != null) {
262 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700263 mBluetoothEnableForTether = false;
264 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800265 break;
266
267 case BluetoothAdapter.STATE_OFF:
268 case BluetoothAdapter.ERROR:
269 mBluetoothEnableForTether = false;
270 break;
271
272 default:
273 // ignore transition states
274 }
275 }
Danica Chang32711b62010-08-10 18:41:29 -0700276 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800277 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800278 }
279 }
280
281 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700282 public void onStart() {
283 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800284
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400285 if (mUnavailable) {
286 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
287 getListView().setEmptyView(emptyView);
288 if (emptyView != null) {
289 emptyView.setText(R.string.tethering_settings_not_available);
290 }
291 return;
292 }
293
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700294 final Activity activity = getActivity();
295
Mike Lockwood69a09572011-07-19 13:30:03 -0700296 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800297 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700298 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700299 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800300
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800301 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700302 filter.addAction(UsbManager.ACTION_USB_STATE);
303 activity.registerReceiver(mTetherChangeReceiver, filter);
304
305 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800306 filter.addAction(Intent.ACTION_MEDIA_SHARED);
307 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
308 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700309 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800310
Danica Chang32711b62010-08-10 18:41:29 -0700311 filter = new IntentFilter();
312 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700313 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700314
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700315 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700316 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700317 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700318 mWifiApEnabler.resume();
319 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800320
321 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800322 }
323
324 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700325 public void onStop() {
326 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400327
328 if (mUnavailable) {
329 return;
330 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700331 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800332 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700333 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700334 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700335 mWifiApEnabler.pause();
336 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800337 }
338
Robert Greenwalt209177a2010-03-04 13:29:02 -0800339 private void updateState() {
340 ConnectivityManager cm =
341 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
342
343 String[] available = cm.getTetherableIfaces();
344 String[] tethered = cm.getTetheredIfaces();
345 String[] errored = cm.getTetheringErroredIfaces();
346 updateState(available, tethered, errored);
347 }
348
Ben Clark0008d212010-07-27 16:20:59 +0100349 private void updateState(String[] available, String[] tethered,
350 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700351 updateUsbState(available, tethered, errored);
352 updateBluetoothState(available, tethered, errored);
353 }
354
355
356 private void updateUsbState(String[] available, String[] tethered,
357 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800358 ConnectivityManager cm =
359 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700360 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800361 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100362 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800363 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800364 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800365 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
366 usbError = cm.getLastTetherError(s);
367 }
368 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800369 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800370 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800371 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100372 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800373 for (String regex : mUsbRegexs) {
374 if (s.matches(regex)) usbTethered = true;
375 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800376 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800377 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100378 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800379 for (String regex : mUsbRegexs) {
380 if (s.matches(regex)) usbErrored = true;
381 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800382 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800383
384 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800385 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
386 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800387 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800388 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800389 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
390 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
391 } else {
392 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
393 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800394 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800395 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800396 } else if (usbErrored) {
397 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
398 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800399 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700400 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800401 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
402 mUsbTether.setEnabled(false);
403 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800404 } else {
405 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
406 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800407 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800408 }
409 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800410
Danica Chang32711b62010-08-10 18:41:29 -0700411 private void updateBluetoothState(String[] available, String[] tethered,
412 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800413 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700414 for (String s: errored) {
415 for (String regex : mBluetoothRegexs) {
416 if (s.matches(regex)) bluetoothErrored = true;
417 }
418 }
419
420 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800421 if (adapter == null)
422 return;
Danica Chang32711b62010-08-10 18:41:29 -0700423 int btState = adapter.getState();
424 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
425 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500426 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700427 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
428 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700429 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700430 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700431 BluetoothPan bluetoothPan = mBluetoothPan.get();
432 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
433 bluetoothPan.isTetheringOn()) {
434 mBluetoothTether.setChecked(true);
435 mBluetoothTether.setEnabled(true);
436 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
437 if (bluetoothTethered > 1) {
438 String summary = getString(
439 R.string.bluetooth_tethering_devices_connected_subtext,
440 bluetoothTethered);
441 mBluetoothTether.setSummary(summary);
442 } else if (bluetoothTethered == 1) {
443 mBluetoothTether.setSummary(
444 R.string.bluetooth_tethering_device_connected_subtext);
445 } else if (bluetoothErrored) {
446 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
447 } else {
448 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
449 }
450 } else {
451 mBluetoothTether.setEnabled(true);
452 mBluetoothTether.setChecked(false);
453 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
454 }
Danica Chang32711b62010-08-10 18:41:29 -0700455 }
456 }
457
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700458 public boolean onPreferenceChange(Preference preference, Object value) {
459 boolean enable = (Boolean) value;
460
461 if (enable) {
Jason Monk4896c012015-02-11 13:30:41 -0500462 startProvisioningIfNecessary(TETHERING_WIFI);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700463 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500464 if (TetherUtil.isProvisioningNeeded(getActivity())) {
465 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_WIFI);
Jason Monk37832d62014-12-10 17:21:51 -0500466 }
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700467 mWifiApEnabler.setSoftapEnabled(false);
468 }
469 return false;
470 }
471
PauloftheWest29aab7a2014-09-26 16:05:44 -0700472 public static boolean isProvisioningNeededButUnavailable(Context context) {
Jason Monk4896c012015-02-11 13:30:41 -0500473 return (TetherUtil.isProvisioningNeeded(context)
474 && !isIntentAvailable(context));
PauloftheWest29aab7a2014-09-26 16:05:44 -0700475 }
476
Jason Monk4896c012015-02-11 13:30:41 -0500477 private static boolean isIntentAvailable(Context context) {
478 String[] provisionApp = context.getResources().getStringArray(
479 com.android.internal.R.array.config_mobile_hotspot_provision_app);
PauloftheWest29aab7a2014-09-26 16:05:44 -0700480 final PackageManager packageManager = context.getPackageManager();
481 Intent intent = new Intent(Intent.ACTION_MAIN);
482 intent.setClassName(provisionApp[0], provisionApp[1]);
483
484 return (packageManager.queryIntentActivities(intent,
485 PackageManager.MATCH_DEFAULT_ONLY).size() > 0);
486 }
487
Irfan Sheriff01b32362011-11-04 12:08:56 -0700488 private void startProvisioningIfNecessary(int choice) {
489 mTetherChoice = choice;
Jason Monk4896c012015-02-11 13:30:41 -0500490 if (TetherUtil.isProvisioningNeeded(getActivity())) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700491 Intent intent = new Intent(Intent.ACTION_MAIN);
492 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500493 intent.putExtra(TETHER_CHOICE, mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700494 startActivityForResult(intent, PROVISION_REQUEST);
495 } else {
496 startTethering();
497 }
498 }
499
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700500 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
501 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700502 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700503 if (resultCode == Activity.RESULT_OK) {
Jason Monk37832d62014-12-10 17:21:51 -0500504 TetherService.scheduleRecheckAlarm(getActivity(), mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700505 startTethering();
506 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700507 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700508 //Wifi tethering is never turned on until afterwards
509 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500510 case TETHERING_BLUETOOTH:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700511 mBluetoothTether.setChecked(false);
512 break;
Jason Monk4896c012015-02-11 13:30:41 -0500513 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700514 mUsbTether.setChecked(false);
515 break;
516 }
Jason Monk4896c012015-02-11 13:30:41 -0500517 mTetherChoice = TETHERING_INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700518 }
519 }
520 }
521
Irfan Sheriff01b32362011-11-04 12:08:56 -0700522 private void startTethering() {
523 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500524 case TETHERING_WIFI:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700525 mWifiApEnabler.setSoftapEnabled(true);
526 break;
Jason Monk4896c012015-02-11 13:30:41 -0500527 case TETHERING_BLUETOOTH:
Danica Chang32711b62010-08-10 18:41:29 -0700528 // turn on Bluetooth first
529 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
530 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800531 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700532 adapter.enable();
533 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
534 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800535 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700536 BluetoothPan bluetoothPan = mBluetoothPan.get();
537 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800538 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700539 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700540 break;
Jason Monk4896c012015-02-11 13:30:41 -0500541 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700542 setUsbTethering(true);
543 break;
544 default:
545 //should not happen
546 break;
547 }
548 }
549
550 private void setUsbTethering(boolean enabled) {
551 ConnectivityManager cm =
552 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800553 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700554 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700555 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
556 return;
557 }
558 mUsbTether.setSummary("");
559 }
560
561 @Override
562 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
563 ConnectivityManager cm =
564 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
565
566 if (preference == mUsbTether) {
567 boolean newState = mUsbTether.isChecked();
568
569 if (newState) {
Jason Monk4896c012015-02-11 13:30:41 -0500570 startProvisioningIfNecessary(TETHERING_USB);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700571 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500572 if (TetherUtil.isProvisioningNeeded(getActivity())) {
573 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_USB);
Jason Monk37832d62014-12-10 17:21:51 -0500574 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700575 setUsbTethering(newState);
576 }
577 } else if (preference == mBluetoothTether) {
578 boolean bluetoothTetherState = mBluetoothTether.isChecked();
579
580 if (bluetoothTetherState) {
Jason Monk4896c012015-02-11 13:30:41 -0500581 startProvisioningIfNecessary(TETHERING_BLUETOOTH);
Danica Chang32711b62010-08-10 18:41:29 -0700582 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500583 if (TetherUtil.isProvisioningNeeded(getActivity())) {
584 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_BLUETOOTH);
Jason Monk37832d62014-12-10 17:21:51 -0500585 }
Danica Chang32711b62010-08-10 18:41:29 -0700586 boolean errored = false;
587
Danica Chang32711b62010-08-10 18:41:29 -0700588 String [] tethered = cm.getTetheredIfaces();
589 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
590 if (bluetoothIface != null &&
591 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
592 errored = true;
593 }
594
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700595 BluetoothPan bluetoothPan = mBluetoothPan.get();
596 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700597 if (errored) {
598 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
599 } else {
600 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
601 }
602 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800603 } else if (preference == mCreateNetwork) {
604 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800605 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700606
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700607 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800608 }
609
Jake Hamby436b29e2011-02-07 18:21:25 -0800610 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800611 for (String iface : ifaces) {
612 for (String regex : regexes) {
613 if (iface.matches(regex)) {
614 return iface;
615 }
616 }
617 }
618 return null;
619 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800620
621 public void onClick(DialogInterface dialogInterface, int button) {
622 if (button == DialogInterface.BUTTON_POSITIVE) {
623 mWifiConfig = mDialog.getConfig();
624 if (mWifiConfig != null) {
625 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700626 * if soft AP is stopped, bring up
627 * else restart with new config
628 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800629 */
630 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700631 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800632 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800633 } else {
634 mWifiManager.setWifiApConfiguration(mWifiConfig);
635 }
636 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
637 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
638 mWifiConfig.SSID,
639 mSecurityType[index]));
640 }
641 }
642 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700643
644 @Override
645 public int getHelpResource() {
646 return R.string.help_url_tether;
647 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800648}