blob: d5df40fe41c507254c5f2b8ae69c13e6408b4a5d [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)
Xiaohui Chen44879a32015-07-22 13:53:22 -0700128 || !mUm.isAdminUser()) {
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
186 public void onSaveInstanceState(Bundle savedInstanceState) {
187 savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice);
188 super.onSaveInstanceState(savedInstanceState);
189 }
190
Amith Yamasani84a042c2011-03-02 11:25:04 -0800191 private void initWifiTethering() {
192 final Activity activity = getActivity();
193 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
194 mWifiConfig = mWifiManager.getWifiApConfiguration();
195 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
196
197 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800198
199 if (mWifiConfig == null) {
200 final String s = activity.getString(
201 com.android.internal.R.string.wifi_tether_configure_ssid_default);
202 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
203 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
204 } else {
205 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
206 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
207 mWifiConfig.SSID,
208 mSecurityType[index]));
209 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800210 }
211
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800212 private BluetoothProfile.ServiceListener mProfileServiceListener =
213 new BluetoothProfile.ServiceListener() {
214 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700215 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800216 }
217 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700218 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800219 }
220 };
221
Irfan Sheriff0b266962010-04-06 15:16:44 -0700222 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700223 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700224 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800225 final Activity activity = getActivity();
226 mDialog = new WifiApDialog(activity, this, mWifiConfig);
227 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700228 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800229
Irfan Sheriff0b266962010-04-06 15:16:44 -0700230 return null;
231 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800232
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800233 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700234 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800235 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800236 String action = intent.getAction();
237 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800238 // TODO - this should understand the interface types
239 ArrayList<String> available = intent.getStringArrayListExtra(
240 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
241 ArrayList<String> active = intent.getStringArrayListExtra(
242 ConnectivityManager.EXTRA_ACTIVE_TETHER);
243 ArrayList<String> errored = intent.getStringArrayListExtra(
244 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700245 updateState(available.toArray(new String[available.size()]),
246 active.toArray(new String[active.size()]),
247 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700248 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
249 mMassStorageActive = true;
250 updateState();
251 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
252 mMassStorageActive = false;
253 updateState();
254 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
255 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800256 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800257 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
258 if (mBluetoothEnableForTether) {
259 switch (intent
260 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
261 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700262 BluetoothPan bluetoothPan = mBluetoothPan.get();
263 if (bluetoothPan != null) {
264 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700265 mBluetoothEnableForTether = false;
266 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800267 break;
268
269 case BluetoothAdapter.STATE_OFF:
270 case BluetoothAdapter.ERROR:
271 mBluetoothEnableForTether = false;
272 break;
273
274 default:
275 // ignore transition states
276 }
277 }
Danica Chang32711b62010-08-10 18:41:29 -0700278 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800279 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800280 }
281 }
282
283 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700284 public void onStart() {
285 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800286
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400287 if (mUnavailable) {
288 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
289 getListView().setEmptyView(emptyView);
290 if (emptyView != null) {
291 emptyView.setText(R.string.tethering_settings_not_available);
292 }
293 return;
294 }
295
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700296 final Activity activity = getActivity();
297
Mike Lockwood69a09572011-07-19 13:30:03 -0700298 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800299 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700300 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700301 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800302
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800303 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700304 filter.addAction(UsbManager.ACTION_USB_STATE);
305 activity.registerReceiver(mTetherChangeReceiver, filter);
306
307 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800308 filter.addAction(Intent.ACTION_MEDIA_SHARED);
309 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
310 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700311 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800312
Danica Chang32711b62010-08-10 18:41:29 -0700313 filter = new IntentFilter();
314 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700315 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700316
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700317 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700318 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700319 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700320 mWifiApEnabler.resume();
321 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800322
323 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800324 }
325
326 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700327 public void onStop() {
328 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400329
330 if (mUnavailable) {
331 return;
332 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700333 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800334 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700335 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700336 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700337 mWifiApEnabler.pause();
338 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800339 }
340
Robert Greenwalt209177a2010-03-04 13:29:02 -0800341 private void updateState() {
342 ConnectivityManager cm =
343 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
344
345 String[] available = cm.getTetherableIfaces();
346 String[] tethered = cm.getTetheredIfaces();
347 String[] errored = cm.getTetheringErroredIfaces();
348 updateState(available, tethered, errored);
349 }
350
Ben Clark0008d212010-07-27 16:20:59 +0100351 private void updateState(String[] available, String[] tethered,
352 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700353 updateUsbState(available, tethered, errored);
354 updateBluetoothState(available, tethered, errored);
355 }
356
357
358 private void updateUsbState(String[] available, String[] tethered,
359 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800360 ConnectivityManager cm =
361 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700362 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800363 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100364 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800365 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800366 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800367 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
368 usbError = cm.getLastTetherError(s);
369 }
370 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800371 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800372 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800373 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100374 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800375 for (String regex : mUsbRegexs) {
376 if (s.matches(regex)) usbTethered = true;
377 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800378 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800379 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100380 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800381 for (String regex : mUsbRegexs) {
382 if (s.matches(regex)) usbErrored = true;
383 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800384 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800385
386 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800387 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
388 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800389 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800390 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800391 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
392 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
393 } else {
394 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
395 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800396 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800397 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800398 } else if (usbErrored) {
399 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
400 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800401 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700402 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800403 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
404 mUsbTether.setEnabled(false);
405 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800406 } else {
407 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
408 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800409 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800410 }
411 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800412
Danica Chang32711b62010-08-10 18:41:29 -0700413 private void updateBluetoothState(String[] available, String[] tethered,
414 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800415 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700416 for (String s: errored) {
417 for (String regex : mBluetoothRegexs) {
418 if (s.matches(regex)) bluetoothErrored = true;
419 }
420 }
421
422 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800423 if (adapter == null)
424 return;
Danica Chang32711b62010-08-10 18:41:29 -0700425 int btState = adapter.getState();
426 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
427 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500428 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700429 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
430 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700431 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700432 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700433 BluetoothPan bluetoothPan = mBluetoothPan.get();
434 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
435 bluetoothPan.isTetheringOn()) {
436 mBluetoothTether.setChecked(true);
437 mBluetoothTether.setEnabled(true);
438 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
439 if (bluetoothTethered > 1) {
440 String summary = getString(
441 R.string.bluetooth_tethering_devices_connected_subtext,
442 bluetoothTethered);
443 mBluetoothTether.setSummary(summary);
444 } else if (bluetoothTethered == 1) {
445 mBluetoothTether.setSummary(
446 R.string.bluetooth_tethering_device_connected_subtext);
447 } else if (bluetoothErrored) {
448 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
449 } else {
450 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
451 }
452 } else {
453 mBluetoothTether.setEnabled(true);
454 mBluetoothTether.setChecked(false);
455 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
456 }
Danica Chang32711b62010-08-10 18:41:29 -0700457 }
458 }
459
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700460 public boolean onPreferenceChange(Preference preference, Object value) {
461 boolean enable = (Boolean) value;
462
463 if (enable) {
Jason Monk4896c012015-02-11 13:30:41 -0500464 startProvisioningIfNecessary(TETHERING_WIFI);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700465 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500466 if (TetherUtil.isProvisioningNeeded(getActivity())) {
467 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_WIFI);
Jason Monk37832d62014-12-10 17:21:51 -0500468 }
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700469 mWifiApEnabler.setSoftapEnabled(false);
470 }
471 return false;
472 }
473
PauloftheWest29aab7a2014-09-26 16:05:44 -0700474 public static boolean isProvisioningNeededButUnavailable(Context context) {
Jason Monk4896c012015-02-11 13:30:41 -0500475 return (TetherUtil.isProvisioningNeeded(context)
476 && !isIntentAvailable(context));
PauloftheWest29aab7a2014-09-26 16:05:44 -0700477 }
478
Jason Monk4896c012015-02-11 13:30:41 -0500479 private static boolean isIntentAvailable(Context context) {
480 String[] provisionApp = context.getResources().getStringArray(
481 com.android.internal.R.array.config_mobile_hotspot_provision_app);
PauloftheWest29aab7a2014-09-26 16:05:44 -0700482 final PackageManager packageManager = context.getPackageManager();
483 Intent intent = new Intent(Intent.ACTION_MAIN);
484 intent.setClassName(provisionApp[0], provisionApp[1]);
485
486 return (packageManager.queryIntentActivities(intent,
487 PackageManager.MATCH_DEFAULT_ONLY).size() > 0);
488 }
489
Irfan Sheriff01b32362011-11-04 12:08:56 -0700490 private void startProvisioningIfNecessary(int choice) {
491 mTetherChoice = choice;
Jason Monk4896c012015-02-11 13:30:41 -0500492 if (TetherUtil.isProvisioningNeeded(getActivity())) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700493 Intent intent = new Intent(Intent.ACTION_MAIN);
494 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500495 intent.putExtra(TETHER_CHOICE, mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700496 startActivityForResult(intent, PROVISION_REQUEST);
497 } else {
498 startTethering();
499 }
500 }
501
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700502 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
503 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700504 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700505 if (resultCode == Activity.RESULT_OK) {
Jason Monk37832d62014-12-10 17:21:51 -0500506 TetherService.scheduleRecheckAlarm(getActivity(), mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700507 startTethering();
508 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700509 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700510 //Wifi tethering is never turned on until afterwards
511 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500512 case TETHERING_BLUETOOTH:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700513 mBluetoothTether.setChecked(false);
514 break;
Jason Monk4896c012015-02-11 13:30:41 -0500515 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700516 mUsbTether.setChecked(false);
517 break;
518 }
Jason Monk4896c012015-02-11 13:30:41 -0500519 mTetherChoice = TETHERING_INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700520 }
521 }
522 }
523
Irfan Sheriff01b32362011-11-04 12:08:56 -0700524 private void startTethering() {
525 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500526 case TETHERING_WIFI:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700527 mWifiApEnabler.setSoftapEnabled(true);
528 break;
Jason Monk4896c012015-02-11 13:30:41 -0500529 case TETHERING_BLUETOOTH:
Danica Chang32711b62010-08-10 18:41:29 -0700530 // turn on Bluetooth first
531 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
532 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800533 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700534 adapter.enable();
535 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
536 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800537 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700538 BluetoothPan bluetoothPan = mBluetoothPan.get();
539 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800540 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700541 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700542 break;
Jason Monk4896c012015-02-11 13:30:41 -0500543 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700544 setUsbTethering(true);
545 break;
546 default:
547 //should not happen
548 break;
549 }
550 }
551
552 private void setUsbTethering(boolean enabled) {
553 ConnectivityManager cm =
554 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800555 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700556 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700557 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
558 return;
559 }
560 mUsbTether.setSummary("");
561 }
562
563 @Override
564 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
565 ConnectivityManager cm =
566 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
567
568 if (preference == mUsbTether) {
569 boolean newState = mUsbTether.isChecked();
570
571 if (newState) {
Jason Monk4896c012015-02-11 13:30:41 -0500572 startProvisioningIfNecessary(TETHERING_USB);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700573 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500574 if (TetherUtil.isProvisioningNeeded(getActivity())) {
575 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_USB);
Jason Monk37832d62014-12-10 17:21:51 -0500576 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700577 setUsbTethering(newState);
578 }
579 } else if (preference == mBluetoothTether) {
580 boolean bluetoothTetherState = mBluetoothTether.isChecked();
581
582 if (bluetoothTetherState) {
Jason Monk4896c012015-02-11 13:30:41 -0500583 startProvisioningIfNecessary(TETHERING_BLUETOOTH);
Danica Chang32711b62010-08-10 18:41:29 -0700584 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500585 if (TetherUtil.isProvisioningNeeded(getActivity())) {
586 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_BLUETOOTH);
Jason Monk37832d62014-12-10 17:21:51 -0500587 }
Danica Chang32711b62010-08-10 18:41:29 -0700588 boolean errored = false;
589
Danica Chang32711b62010-08-10 18:41:29 -0700590 String [] tethered = cm.getTetheredIfaces();
591 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
592 if (bluetoothIface != null &&
593 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
594 errored = true;
595 }
596
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700597 BluetoothPan bluetoothPan = mBluetoothPan.get();
598 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700599 if (errored) {
600 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
601 } else {
602 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
603 }
604 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800605 } else if (preference == mCreateNetwork) {
606 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800607 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700608
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700609 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800610 }
611
Jake Hamby436b29e2011-02-07 18:21:25 -0800612 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800613 for (String iface : ifaces) {
614 for (String regex : regexes) {
615 if (iface.matches(regex)) {
616 return iface;
617 }
618 }
619 }
620 return null;
621 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800622
623 public void onClick(DialogInterface dialogInterface, int button) {
624 if (button == DialogInterface.BUTTON_POSITIVE) {
625 mWifiConfig = mDialog.getConfig();
626 if (mWifiConfig != null) {
627 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700628 * if soft AP is stopped, bring up
629 * else restart with new config
630 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800631 */
632 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700633 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800634 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800635 } else {
636 mWifiManager.setWifiApConfiguration(mWifiConfig);
637 }
638 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
639 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
640 mWifiConfig.SSID,
641 mSecurityType[index]));
642 }
643 }
644 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700645
646 @Override
647 public int getHelpResource() {
648 return R.string.help_url_tether;
649 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800650}