blob: 393adf64352a9c999eba0a5facc274c19f91b89c [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;
Robert Greenwalt505766c2011-11-09 15:03:14 -080041import android.os.SystemProperties;
Amith Yamasani394eaa22013-06-11 11:04:44 -070042import android.os.UserHandle;
43import android.os.UserManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080044import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080045import android.preference.PreferenceScreen;
PauloftheWest656c88c2014-08-24 11:59:35 -070046import android.preference.SwitchPreference;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040047import android.widget.TextView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080048
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080049import com.android.settings.wifi.WifiApDialog;
50import com.android.settings.wifi.WifiApEnabler;
Jason Monk4896c012015-02-11 13:30:41 -050051import com.android.settingslib.TetherUtil;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080052
Robert Greenwaltc4764d22010-02-12 14:21:37 -080053import java.util.ArrayList;
Robert Greenwaltf60b92b2012-09-13 15:02:00 -070054import java.util.concurrent.atomic.AtomicReference;
Matthew Xieb5f144a2012-06-28 18:41:54 -070055
Robert Greenwalt3901edb2010-01-26 10:22:37 -080056/*
57 * Displays preferences for Tethering.
58 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080059public class TetherSettings extends SettingsPreferenceFragment
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070060 implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
zzy7f38f472012-04-14 17:25:17 -070061 private static final String TAG = "TetherSettings";
Danica Chang32711b62010-08-10 18:41:29 -070062
Robert Greenwalt3901edb2010-01-26 10:22:37 -080063 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080064 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070065 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Rohit Sisodia37155e92014-09-22 16:34:56 -050066 private static final String TETHER_CHOICE = "TETHER_TYPE";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080067
Amith Yamasanid3fed682012-04-27 17:38:40 -070068 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070069
PauloftheWest656c88c2014-08-24 11:59:35 -070070 private SwitchPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080071
Irfan Sheriff65cff172010-02-08 10:46:30 -080072 private WifiApEnabler mWifiApEnabler;
PauloftheWest656c88c2014-08-24 11:59:35 -070073 private SwitchPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070074
PauloftheWest656c88c2014-08-24 11:59:35 -070075 private SwitchPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070076
Robert Greenwalt3901edb2010-01-26 10:22:37 -080077 private BroadcastReceiver mTetherChangeReceiver;
78
Robert Greenwaltc4764d22010-02-12 14:21:37 -080079 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080080
81 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080082
Danica Chang32711b62010-08-10 18:41:29 -070083 private String[] mBluetoothRegexs;
Robert Greenwalt4f1970f2012-09-16 17:03:08 -070084 private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<BluetoothPan>();
Danica Chang32711b62010-08-10 18:41:29 -070085
Amith Yamasani84a042c2011-03-02 11:25:04 -080086 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
87 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
88
89 private String[] mSecurityType;
90 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080091
92 private WifiApDialog mDialog;
93 private WifiManager mWifiManager;
94 private WifiConfiguration mWifiConfig = null;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040095 private UserManager mUm;
Amith Yamasani84a042c2011-03-02 11:25:04 -080096
Mike Lockwood69a09572011-07-19 13:30:03 -070097 private boolean mUsbConnected;
98 private boolean mMassStorageActive;
99
Jake Hambyc777ee22011-03-04 15:37:39 -0800100 private boolean mBluetoothEnableForTether;
101
Irfan Sheriff01b32362011-11-04 12:08:56 -0700102 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
Jason Monk4896c012015-02-11 13:30:41 -0500103 private int mTetherChoice = TETHERING_INVALID;
Irfan Sheriff01b32362011-11-04 12:08:56 -0700104
105 /* Stores the package name and the class name of the provisioning app */
106 private String[] mProvisionApp;
107 private static final int PROVISION_REQUEST = 0;
108
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400109 private boolean mUnavailable;
110
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800111 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700112 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800113 super.onCreate(icicle);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500114
115 if(icicle != null) {
116 mTetherChoice = icicle.getInt(TETHER_CHOICE);
117 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800118 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800119
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400120 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
121
122 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
123 mUnavailable = true;
124 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
125 return;
126 }
127
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700128 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800129 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
130 if (adapter != null) {
131 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
132 BluetoothProfile.PAN);
133 }
134
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700135 mEnableWifiAp =
PauloftheWest656c88c2014-08-24 11:59:35 -0700136 (SwitchPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700137 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
PauloftheWest656c88c2014-08-24 11:59:35 -0700138 mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
139 mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800140
141 ConnectivityManager cm =
142 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800143
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800144 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800145 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700146 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
147
Amith Yamasanie419bc12011-02-14 14:19:08 -0800148 final boolean usbAvailable = mUsbRegexs.length != 0;
149 final boolean wifiAvailable = mWifiRegexs.length != 0;
150 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700151
Danica Chang32711b62010-08-10 18:41:29 -0700152 if (!usbAvailable || Utils.isMonkeyRunning()) {
153 getPreferenceScreen().removePreference(mUsbTether);
154 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800155
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800156 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700157 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700158 initWifiTethering();
159 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700160 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800161 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700162 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800163
Danica Chang32711b62010-08-10 18:41:29 -0700164 if (!bluetoothAvailable) {
165 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700166 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700167 BluetoothPan pan = mBluetoothPan.get();
168 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700169 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700170 } else {
171 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700172 }
173 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800174
Irfan Sheriff01b32362011-11-04 12:08:56 -0700175 mProvisionApp = getResources().getStringArray(
176 com.android.internal.R.array.config_mobile_hotspot_provision_app);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800177 }
178
Rohit Sisodia37155e92014-09-22 16:34:56 -0500179 @Override
180 public void onSaveInstanceState(Bundle savedInstanceState) {
181 savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice);
182 super.onSaveInstanceState(savedInstanceState);
183 }
184
Amith Yamasani84a042c2011-03-02 11:25:04 -0800185 private void initWifiTethering() {
186 final Activity activity = getActivity();
187 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
188 mWifiConfig = mWifiManager.getWifiApConfiguration();
189 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
190
191 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800192
193 if (mWifiConfig == null) {
194 final String s = activity.getString(
195 com.android.internal.R.string.wifi_tether_configure_ssid_default);
196 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
197 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
198 } else {
199 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
200 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
201 mWifiConfig.SSID,
202 mSecurityType[index]));
203 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800204 }
205
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800206 private BluetoothProfile.ServiceListener mProfileServiceListener =
207 new BluetoothProfile.ServiceListener() {
208 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700209 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800210 }
211 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700212 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800213 }
214 };
215
Irfan Sheriff0b266962010-04-06 15:16:44 -0700216 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700217 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700218 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800219 final Activity activity = getActivity();
220 mDialog = new WifiApDialog(activity, this, mWifiConfig);
221 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700222 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800223
Irfan Sheriff0b266962010-04-06 15:16:44 -0700224 return null;
225 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800226
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800227 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700228 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800229 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800230 String action = intent.getAction();
231 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800232 // TODO - this should understand the interface types
233 ArrayList<String> available = intent.getStringArrayListExtra(
234 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
235 ArrayList<String> active = intent.getStringArrayListExtra(
236 ConnectivityManager.EXTRA_ACTIVE_TETHER);
237 ArrayList<String> errored = intent.getStringArrayListExtra(
238 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700239 updateState(available.toArray(new String[available.size()]),
240 active.toArray(new String[active.size()]),
241 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700242 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
243 mMassStorageActive = true;
244 updateState();
245 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
246 mMassStorageActive = false;
247 updateState();
248 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
249 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800250 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800251 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
252 if (mBluetoothEnableForTether) {
253 switch (intent
254 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
255 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700256 BluetoothPan bluetoothPan = mBluetoothPan.get();
257 if (bluetoothPan != null) {
258 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700259 mBluetoothEnableForTether = false;
260 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800261 break;
262
263 case BluetoothAdapter.STATE_OFF:
264 case BluetoothAdapter.ERROR:
265 mBluetoothEnableForTether = false;
266 break;
267
268 default:
269 // ignore transition states
270 }
271 }
Danica Chang32711b62010-08-10 18:41:29 -0700272 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800273 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800274 }
275 }
276
277 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700278 public void onStart() {
279 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800280
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400281 if (mUnavailable) {
282 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
283 getListView().setEmptyView(emptyView);
284 if (emptyView != null) {
285 emptyView.setText(R.string.tethering_settings_not_available);
286 }
287 return;
288 }
289
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700290 final Activity activity = getActivity();
291
Mike Lockwood69a09572011-07-19 13:30:03 -0700292 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800293 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700294 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700295 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800296
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800297 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700298 filter.addAction(UsbManager.ACTION_USB_STATE);
299 activity.registerReceiver(mTetherChangeReceiver, filter);
300
301 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800302 filter.addAction(Intent.ACTION_MEDIA_SHARED);
303 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
304 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700305 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800306
Danica Chang32711b62010-08-10 18:41:29 -0700307 filter = new IntentFilter();
308 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700309 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700310
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700311 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700312 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700313 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700314 mWifiApEnabler.resume();
315 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800316
317 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800318 }
319
320 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700321 public void onStop() {
322 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400323
324 if (mUnavailable) {
325 return;
326 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700327 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800328 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700329 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700330 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700331 mWifiApEnabler.pause();
332 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800333 }
334
Robert Greenwalt209177a2010-03-04 13:29:02 -0800335 private void updateState() {
336 ConnectivityManager cm =
337 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
338
339 String[] available = cm.getTetherableIfaces();
340 String[] tethered = cm.getTetheredIfaces();
341 String[] errored = cm.getTetheringErroredIfaces();
342 updateState(available, tethered, errored);
343 }
344
Ben Clark0008d212010-07-27 16:20:59 +0100345 private void updateState(String[] available, String[] tethered,
346 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700347 updateUsbState(available, tethered, errored);
348 updateBluetoothState(available, tethered, errored);
349 }
350
351
352 private void updateUsbState(String[] available, String[] tethered,
353 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800354 ConnectivityManager cm =
355 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700356 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800357 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100358 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800359 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800360 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800361 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
362 usbError = cm.getLastTetherError(s);
363 }
364 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800365 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800366 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800367 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100368 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800369 for (String regex : mUsbRegexs) {
370 if (s.matches(regex)) usbTethered = true;
371 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800372 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800373 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100374 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800375 for (String regex : mUsbRegexs) {
376 if (s.matches(regex)) usbErrored = true;
377 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800378 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800379
380 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800381 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
382 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800383 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800384 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800385 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
386 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
387 } else {
388 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
389 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800390 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800391 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800392 } else if (usbErrored) {
393 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
394 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800395 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700396 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800397 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
398 mUsbTether.setEnabled(false);
399 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800400 } else {
401 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
402 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800403 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800404 }
405 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800406
Danica Chang32711b62010-08-10 18:41:29 -0700407 private void updateBluetoothState(String[] available, String[] tethered,
408 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800409 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700410 for (String s: errored) {
411 for (String regex : mBluetoothRegexs) {
412 if (s.matches(regex)) bluetoothErrored = true;
413 }
414 }
415
416 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800417 if (adapter == null)
418 return;
Danica Chang32711b62010-08-10 18:41:29 -0700419 int btState = adapter.getState();
420 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
421 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500422 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700423 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
424 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700425 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700426 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700427 BluetoothPan bluetoothPan = mBluetoothPan.get();
428 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
429 bluetoothPan.isTetheringOn()) {
430 mBluetoothTether.setChecked(true);
431 mBluetoothTether.setEnabled(true);
432 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
433 if (bluetoothTethered > 1) {
434 String summary = getString(
435 R.string.bluetooth_tethering_devices_connected_subtext,
436 bluetoothTethered);
437 mBluetoothTether.setSummary(summary);
438 } else if (bluetoothTethered == 1) {
439 mBluetoothTether.setSummary(
440 R.string.bluetooth_tethering_device_connected_subtext);
441 } else if (bluetoothErrored) {
442 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
443 } else {
444 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
445 }
446 } else {
447 mBluetoothTether.setEnabled(true);
448 mBluetoothTether.setChecked(false);
449 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
450 }
Danica Chang32711b62010-08-10 18:41:29 -0700451 }
452 }
453
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700454 public boolean onPreferenceChange(Preference preference, Object value) {
455 boolean enable = (Boolean) value;
456
457 if (enable) {
Jason Monk4896c012015-02-11 13:30:41 -0500458 startProvisioningIfNecessary(TETHERING_WIFI);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700459 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500460 if (TetherUtil.isProvisioningNeeded(getActivity())) {
461 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_WIFI);
Jason Monk37832d62014-12-10 17:21:51 -0500462 }
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700463 mWifiApEnabler.setSoftapEnabled(false);
464 }
465 return false;
466 }
467
PauloftheWest29aab7a2014-09-26 16:05:44 -0700468 public static boolean isProvisioningNeededButUnavailable(Context context) {
Jason Monk4896c012015-02-11 13:30:41 -0500469 return (TetherUtil.isProvisioningNeeded(context)
470 && !isIntentAvailable(context));
PauloftheWest29aab7a2014-09-26 16:05:44 -0700471 }
472
Jason Monk4896c012015-02-11 13:30:41 -0500473 private static boolean isIntentAvailable(Context context) {
474 String[] provisionApp = context.getResources().getStringArray(
475 com.android.internal.R.array.config_mobile_hotspot_provision_app);
PauloftheWest29aab7a2014-09-26 16:05:44 -0700476 final PackageManager packageManager = context.getPackageManager();
477 Intent intent = new Intent(Intent.ACTION_MAIN);
478 intent.setClassName(provisionApp[0], provisionApp[1]);
479
480 return (packageManager.queryIntentActivities(intent,
481 PackageManager.MATCH_DEFAULT_ONLY).size() > 0);
482 }
483
Irfan Sheriff01b32362011-11-04 12:08:56 -0700484 private void startProvisioningIfNecessary(int choice) {
485 mTetherChoice = choice;
Jason Monk4896c012015-02-11 13:30:41 -0500486 if (TetherUtil.isProvisioningNeeded(getActivity())) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700487 Intent intent = new Intent(Intent.ACTION_MAIN);
488 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500489 intent.putExtra(TETHER_CHOICE, mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700490 startActivityForResult(intent, PROVISION_REQUEST);
491 } else {
492 startTethering();
493 }
494 }
495
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700496 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
497 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700498 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700499 if (resultCode == Activity.RESULT_OK) {
Jason Monk37832d62014-12-10 17:21:51 -0500500 TetherService.scheduleRecheckAlarm(getActivity(), mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700501 startTethering();
502 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700503 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700504 //Wifi tethering is never turned on until afterwards
505 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500506 case TETHERING_BLUETOOTH:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700507 mBluetoothTether.setChecked(false);
508 break;
Jason Monk4896c012015-02-11 13:30:41 -0500509 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700510 mUsbTether.setChecked(false);
511 break;
512 }
Jason Monk4896c012015-02-11 13:30:41 -0500513 mTetherChoice = TETHERING_INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700514 }
515 }
516 }
517
Irfan Sheriff01b32362011-11-04 12:08:56 -0700518 private void startTethering() {
519 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500520 case TETHERING_WIFI:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700521 mWifiApEnabler.setSoftapEnabled(true);
522 break;
Jason Monk4896c012015-02-11 13:30:41 -0500523 case TETHERING_BLUETOOTH:
Danica Chang32711b62010-08-10 18:41:29 -0700524 // turn on Bluetooth first
525 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
526 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800527 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700528 adapter.enable();
529 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
530 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800531 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700532 BluetoothPan bluetoothPan = mBluetoothPan.get();
533 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800534 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700535 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700536 break;
Jason Monk4896c012015-02-11 13:30:41 -0500537 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700538 setUsbTethering(true);
539 break;
540 default:
541 //should not happen
542 break;
543 }
544 }
545
546 private void setUsbTethering(boolean enabled) {
547 ConnectivityManager cm =
548 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800549 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700550 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700551 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
552 return;
553 }
554 mUsbTether.setSummary("");
555 }
556
557 @Override
558 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
559 ConnectivityManager cm =
560 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
561
562 if (preference == mUsbTether) {
563 boolean newState = mUsbTether.isChecked();
564
565 if (newState) {
Jason Monk4896c012015-02-11 13:30:41 -0500566 startProvisioningIfNecessary(TETHERING_USB);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700567 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500568 if (TetherUtil.isProvisioningNeeded(getActivity())) {
569 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_USB);
Jason Monk37832d62014-12-10 17:21:51 -0500570 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700571 setUsbTethering(newState);
572 }
573 } else if (preference == mBluetoothTether) {
574 boolean bluetoothTetherState = mBluetoothTether.isChecked();
575
576 if (bluetoothTetherState) {
Jason Monk4896c012015-02-11 13:30:41 -0500577 startProvisioningIfNecessary(TETHERING_BLUETOOTH);
Danica Chang32711b62010-08-10 18:41:29 -0700578 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500579 if (TetherUtil.isProvisioningNeeded(getActivity())) {
580 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_BLUETOOTH);
Jason Monk37832d62014-12-10 17:21:51 -0500581 }
Danica Chang32711b62010-08-10 18:41:29 -0700582 boolean errored = false;
583
Danica Chang32711b62010-08-10 18:41:29 -0700584 String [] tethered = cm.getTetheredIfaces();
585 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
586 if (bluetoothIface != null &&
587 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
588 errored = true;
589 }
590
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700591 BluetoothPan bluetoothPan = mBluetoothPan.get();
592 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700593 if (errored) {
594 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
595 } else {
596 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
597 }
598 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800599 } else if (preference == mCreateNetwork) {
600 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800601 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700602
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700603 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800604 }
605
Jake Hamby436b29e2011-02-07 18:21:25 -0800606 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800607 for (String iface : ifaces) {
608 for (String regex : regexes) {
609 if (iface.matches(regex)) {
610 return iface;
611 }
612 }
613 }
614 return null;
615 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800616
617 public void onClick(DialogInterface dialogInterface, int button) {
618 if (button == DialogInterface.BUTTON_POSITIVE) {
619 mWifiConfig = mDialog.getConfig();
620 if (mWifiConfig != null) {
621 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700622 * if soft AP is stopped, bring up
623 * else restart with new config
624 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800625 */
626 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700627 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800628 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800629 } else {
630 mWifiManager.setWifiApConfiguration(mWifiConfig);
631 }
632 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
633 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
634 mWifiConfig.SSID,
635 mSecurityType[index]));
636 }
637 }
638 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700639
640 @Override
641 public int getHelpResource() {
642 return R.string.help_url_tether;
643 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800644}