blob: 78b93c67b55be94fd0b5098df77958b4755ec480 [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;
Nicolas Prevota5df4772015-06-02 22:27:49 +010041import android.os.UserHandle;
Amith Yamasani394eaa22013-06-11 11:04:44 -070042import android.os.UserManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080043import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080044import android.preference.PreferenceScreen;
PauloftheWest656c88c2014-08-24 11:59:35 -070045import android.preference.SwitchPreference;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040046import android.widget.TextView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080047
Chris Wren8a963ba2015-03-20 10:29:14 -040048import com.android.internal.logging.MetricsLogger;
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
Chris Wren8a963ba2015-03-20 10:29:14 -0400112 protected int getMetricsCategory() {
113 return MetricsLogger.TETHER;
114 }
115
116 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700117 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800118 super.onCreate(icicle);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500119
120 if(icicle != null) {
121 mTetherChoice = icicle.getInt(TETHER_CHOICE);
122 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800123 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800124
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400125 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
126
Nicolas Prevota5df4772015-06-02 22:27:49 +0100127 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)
128 || UserHandle.myUserId() != UserHandle.USER_OWNER) {
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400129 mUnavailable = true;
130 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
131 return;
132 }
133
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700134 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800135 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
136 if (adapter != null) {
137 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
138 BluetoothProfile.PAN);
139 }
140
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700141 mEnableWifiAp =
PauloftheWest656c88c2014-08-24 11:59:35 -0700142 (SwitchPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700143 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
PauloftheWest656c88c2014-08-24 11:59:35 -0700144 mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
145 mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800146
147 ConnectivityManager cm =
148 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800149
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800150 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800151 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700152 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
153
Amith Yamasanie419bc12011-02-14 14:19:08 -0800154 final boolean usbAvailable = mUsbRegexs.length != 0;
155 final boolean wifiAvailable = mWifiRegexs.length != 0;
156 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700157
Danica Chang32711b62010-08-10 18:41:29 -0700158 if (!usbAvailable || Utils.isMonkeyRunning()) {
159 getPreferenceScreen().removePreference(mUsbTether);
160 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800161
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800162 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700163 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700164 initWifiTethering();
165 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700166 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800167 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700168 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800169
Danica Chang32711b62010-08-10 18:41:29 -0700170 if (!bluetoothAvailable) {
171 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700172 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700173 BluetoothPan pan = mBluetoothPan.get();
174 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700175 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700176 } else {
177 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700178 }
179 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800180
Irfan Sheriff01b32362011-11-04 12:08:56 -0700181 mProvisionApp = getResources().getStringArray(
182 com.android.internal.R.array.config_mobile_hotspot_provision_app);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800183 }
184
Rohit Sisodia37155e92014-09-22 16:34:56 -0500185 @Override
宋凯伦0fb2f2b2016-05-25 10:15:03 +0800186 public void onDestroy() {
187 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
188 BluetoothProfile profile = mBluetoothPan.getAndSet(null);
189 if (profile != null && adapter != null) {
190 adapter.closeProfileProxy(BluetoothProfile.PAN, profile);
191 }
192 super.onDestroy();
193 }
194
195 @Override
Rohit Sisodia37155e92014-09-22 16:34:56 -0500196 public void onSaveInstanceState(Bundle savedInstanceState) {
197 savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice);
198 super.onSaveInstanceState(savedInstanceState);
199 }
200
Amith Yamasani84a042c2011-03-02 11:25:04 -0800201 private void initWifiTethering() {
202 final Activity activity = getActivity();
203 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
204 mWifiConfig = mWifiManager.getWifiApConfiguration();
205 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
206
207 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800208
209 if (mWifiConfig == null) {
210 final String s = activity.getString(
211 com.android.internal.R.string.wifi_tether_configure_ssid_default);
212 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
213 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
214 } else {
215 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
216 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
217 mWifiConfig.SSID,
218 mSecurityType[index]));
219 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800220 }
221
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800222 private BluetoothProfile.ServiceListener mProfileServiceListener =
223 new BluetoothProfile.ServiceListener() {
224 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700225 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800226 }
227 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700228 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800229 }
230 };
231
Irfan Sheriff0b266962010-04-06 15:16:44 -0700232 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700233 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700234 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800235 final Activity activity = getActivity();
236 mDialog = new WifiApDialog(activity, this, mWifiConfig);
237 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700238 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800239
Irfan Sheriff0b266962010-04-06 15:16:44 -0700240 return null;
241 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800242
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800243 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700244 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800245 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800246 String action = intent.getAction();
247 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800248 // TODO - this should understand the interface types
249 ArrayList<String> available = intent.getStringArrayListExtra(
250 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
251 ArrayList<String> active = intent.getStringArrayListExtra(
252 ConnectivityManager.EXTRA_ACTIVE_TETHER);
253 ArrayList<String> errored = intent.getStringArrayListExtra(
254 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700255 updateState(available.toArray(new String[available.size()]),
256 active.toArray(new String[active.size()]),
257 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700258 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
259 mMassStorageActive = true;
260 updateState();
261 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
262 mMassStorageActive = false;
263 updateState();
264 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
265 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800266 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800267 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
268 if (mBluetoothEnableForTether) {
269 switch (intent
270 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
271 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700272 BluetoothPan bluetoothPan = mBluetoothPan.get();
273 if (bluetoothPan != null) {
274 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700275 mBluetoothEnableForTether = false;
276 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800277 break;
278
279 case BluetoothAdapter.STATE_OFF:
280 case BluetoothAdapter.ERROR:
281 mBluetoothEnableForTether = false;
282 break;
283
284 default:
285 // ignore transition states
286 }
287 }
Danica Chang32711b62010-08-10 18:41:29 -0700288 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800289 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800290 }
291 }
292
293 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700294 public void onStart() {
295 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800296
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400297 if (mUnavailable) {
298 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
299 getListView().setEmptyView(emptyView);
300 if (emptyView != null) {
301 emptyView.setText(R.string.tethering_settings_not_available);
302 }
303 return;
304 }
305
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700306 final Activity activity = getActivity();
307
Mike Lockwood69a09572011-07-19 13:30:03 -0700308 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800309 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700310 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700311 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800312
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800313 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700314 filter.addAction(UsbManager.ACTION_USB_STATE);
315 activity.registerReceiver(mTetherChangeReceiver, filter);
316
317 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800318 filter.addAction(Intent.ACTION_MEDIA_SHARED);
319 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
320 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700321 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800322
Danica Chang32711b62010-08-10 18:41:29 -0700323 filter = new IntentFilter();
324 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700325 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700326
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700327 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700328 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700329 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700330 mWifiApEnabler.resume();
331 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800332
333 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800334 }
335
336 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700337 public void onStop() {
338 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400339
340 if (mUnavailable) {
341 return;
342 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700343 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800344 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700345 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700346 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700347 mWifiApEnabler.pause();
348 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800349 }
350
Robert Greenwalt209177a2010-03-04 13:29:02 -0800351 private void updateState() {
352 ConnectivityManager cm =
353 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
354
355 String[] available = cm.getTetherableIfaces();
356 String[] tethered = cm.getTetheredIfaces();
357 String[] errored = cm.getTetheringErroredIfaces();
358 updateState(available, tethered, errored);
359 }
360
Ben Clark0008d212010-07-27 16:20:59 +0100361 private void updateState(String[] available, String[] tethered,
362 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700363 updateUsbState(available, tethered, errored);
364 updateBluetoothState(available, tethered, errored);
365 }
366
367
368 private void updateUsbState(String[] available, String[] tethered,
369 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800370 ConnectivityManager cm =
371 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700372 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800373 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100374 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800375 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800376 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800377 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
378 usbError = cm.getLastTetherError(s);
379 }
380 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800381 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800382 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800383 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100384 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800385 for (String regex : mUsbRegexs) {
386 if (s.matches(regex)) usbTethered = true;
387 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800388 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800389 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100390 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800391 for (String regex : mUsbRegexs) {
392 if (s.matches(regex)) usbErrored = true;
393 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800394 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800395
396 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800397 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
398 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800399 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800400 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800401 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
402 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
403 } else {
404 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
405 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800406 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800407 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800408 } else if (usbErrored) {
409 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
410 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800411 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700412 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800413 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
414 mUsbTether.setEnabled(false);
415 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800416 } else {
417 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
418 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800419 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800420 }
421 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800422
Danica Chang32711b62010-08-10 18:41:29 -0700423 private void updateBluetoothState(String[] available, String[] tethered,
424 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800425 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700426 for (String s: errored) {
427 for (String regex : mBluetoothRegexs) {
428 if (s.matches(regex)) bluetoothErrored = true;
429 }
430 }
431
432 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800433 if (adapter == null)
434 return;
Danica Chang32711b62010-08-10 18:41:29 -0700435 int btState = adapter.getState();
436 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
437 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500438 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700439 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
440 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700441 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700442 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700443 BluetoothPan bluetoothPan = mBluetoothPan.get();
444 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
445 bluetoothPan.isTetheringOn()) {
446 mBluetoothTether.setChecked(true);
447 mBluetoothTether.setEnabled(true);
448 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
449 if (bluetoothTethered > 1) {
450 String summary = getString(
451 R.string.bluetooth_tethering_devices_connected_subtext,
452 bluetoothTethered);
453 mBluetoothTether.setSummary(summary);
454 } else if (bluetoothTethered == 1) {
455 mBluetoothTether.setSummary(
456 R.string.bluetooth_tethering_device_connected_subtext);
457 } else if (bluetoothErrored) {
458 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
459 } else {
460 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
461 }
462 } else {
463 mBluetoothTether.setEnabled(true);
464 mBluetoothTether.setChecked(false);
465 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
466 }
Danica Chang32711b62010-08-10 18:41:29 -0700467 }
468 }
469
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700470 public boolean onPreferenceChange(Preference preference, Object value) {
471 boolean enable = (Boolean) value;
472
473 if (enable) {
Jason Monk4896c012015-02-11 13:30:41 -0500474 startProvisioningIfNecessary(TETHERING_WIFI);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700475 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500476 if (TetherUtil.isProvisioningNeeded(getActivity())) {
477 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_WIFI);
Jason Monk37832d62014-12-10 17:21:51 -0500478 }
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700479 mWifiApEnabler.setSoftapEnabled(false);
480 }
481 return false;
482 }
483
PauloftheWest29aab7a2014-09-26 16:05:44 -0700484 public static boolean isProvisioningNeededButUnavailable(Context context) {
Jason Monk4896c012015-02-11 13:30:41 -0500485 return (TetherUtil.isProvisioningNeeded(context)
486 && !isIntentAvailable(context));
PauloftheWest29aab7a2014-09-26 16:05:44 -0700487 }
488
Jason Monk4896c012015-02-11 13:30:41 -0500489 private static boolean isIntentAvailable(Context context) {
490 String[] provisionApp = context.getResources().getStringArray(
491 com.android.internal.R.array.config_mobile_hotspot_provision_app);
PauloftheWest29aab7a2014-09-26 16:05:44 -0700492 final PackageManager packageManager = context.getPackageManager();
493 Intent intent = new Intent(Intent.ACTION_MAIN);
494 intent.setClassName(provisionApp[0], provisionApp[1]);
495
496 return (packageManager.queryIntentActivities(intent,
497 PackageManager.MATCH_DEFAULT_ONLY).size() > 0);
498 }
499
Irfan Sheriff01b32362011-11-04 12:08:56 -0700500 private void startProvisioningIfNecessary(int choice) {
501 mTetherChoice = choice;
Jason Monk4896c012015-02-11 13:30:41 -0500502 if (TetherUtil.isProvisioningNeeded(getActivity())) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700503 Intent intent = new Intent(Intent.ACTION_MAIN);
504 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500505 intent.putExtra(TETHER_CHOICE, mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700506 startActivityForResult(intent, PROVISION_REQUEST);
507 } else {
508 startTethering();
509 }
510 }
511
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700512 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
513 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700514 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700515 if (resultCode == Activity.RESULT_OK) {
Jason Monk37832d62014-12-10 17:21:51 -0500516 TetherService.scheduleRecheckAlarm(getActivity(), mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700517 startTethering();
518 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700519 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700520 //Wifi tethering is never turned on until afterwards
521 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500522 case TETHERING_BLUETOOTH:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700523 mBluetoothTether.setChecked(false);
524 break;
Jason Monk4896c012015-02-11 13:30:41 -0500525 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700526 mUsbTether.setChecked(false);
527 break;
528 }
Jason Monk4896c012015-02-11 13:30:41 -0500529 mTetherChoice = TETHERING_INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700530 }
531 }
532 }
533
Irfan Sheriff01b32362011-11-04 12:08:56 -0700534 private void startTethering() {
535 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500536 case TETHERING_WIFI:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700537 mWifiApEnabler.setSoftapEnabled(true);
538 break;
Jason Monk4896c012015-02-11 13:30:41 -0500539 case TETHERING_BLUETOOTH:
Danica Chang32711b62010-08-10 18:41:29 -0700540 // turn on Bluetooth first
541 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
542 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800543 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700544 adapter.enable();
545 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
546 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800547 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700548 BluetoothPan bluetoothPan = mBluetoothPan.get();
549 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800550 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700551 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700552 break;
Jason Monk4896c012015-02-11 13:30:41 -0500553 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700554 setUsbTethering(true);
555 break;
556 default:
557 //should not happen
558 break;
559 }
560 }
561
562 private void setUsbTethering(boolean enabled) {
563 ConnectivityManager cm =
564 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800565 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700566 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700567 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
568 return;
569 }
570 mUsbTether.setSummary("");
571 }
572
573 @Override
574 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
575 ConnectivityManager cm =
576 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
577
578 if (preference == mUsbTether) {
579 boolean newState = mUsbTether.isChecked();
580
581 if (newState) {
Jason Monk4896c012015-02-11 13:30:41 -0500582 startProvisioningIfNecessary(TETHERING_USB);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700583 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500584 if (TetherUtil.isProvisioningNeeded(getActivity())) {
585 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_USB);
Jason Monk37832d62014-12-10 17:21:51 -0500586 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700587 setUsbTethering(newState);
588 }
589 } else if (preference == mBluetoothTether) {
590 boolean bluetoothTetherState = mBluetoothTether.isChecked();
591
592 if (bluetoothTetherState) {
Jason Monk4896c012015-02-11 13:30:41 -0500593 startProvisioningIfNecessary(TETHERING_BLUETOOTH);
Danica Chang32711b62010-08-10 18:41:29 -0700594 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500595 if (TetherUtil.isProvisioningNeeded(getActivity())) {
596 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_BLUETOOTH);
Jason Monk37832d62014-12-10 17:21:51 -0500597 }
Danica Chang32711b62010-08-10 18:41:29 -0700598 boolean errored = false;
599
Danica Chang32711b62010-08-10 18:41:29 -0700600 String [] tethered = cm.getTetheredIfaces();
601 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
602 if (bluetoothIface != null &&
603 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
604 errored = true;
605 }
606
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700607 BluetoothPan bluetoothPan = mBluetoothPan.get();
608 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700609 if (errored) {
610 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
611 } else {
612 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
613 }
614 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800615 } else if (preference == mCreateNetwork) {
616 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800617 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700618
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700619 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800620 }
621
Jake Hamby436b29e2011-02-07 18:21:25 -0800622 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800623 for (String iface : ifaces) {
624 for (String regex : regexes) {
625 if (iface.matches(regex)) {
626 return iface;
627 }
628 }
629 }
630 return null;
631 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800632
633 public void onClick(DialogInterface dialogInterface, int button) {
634 if (button == DialogInterface.BUTTON_POSITIVE) {
635 mWifiConfig = mDialog.getConfig();
636 if (mWifiConfig != null) {
637 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700638 * if soft AP is stopped, bring up
639 * else restart with new config
640 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800641 */
642 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700643 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800644 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800645 } else {
646 mWifiManager.setWifiApConfiguration(mWifiConfig);
647 }
648 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
649 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
650 mWifiConfig.SSID,
651 mSecurityType[index]));
652 }
653 }
654 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700655
656 @Override
657 public int getHelpResource() {
658 return R.string.help_url_tether;
659 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800660}