blob: 230bbb2b164609feb74f747fb7f0b2418eb455d0 [file] [log] [blame]
Robert Greenwalt3901edb2010-01-26 10:22:37 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070019import android.app.Activity;
Irfan Sheriff0b266962010-04-06 15:16:44 -070020import android.app.Dialog;
Danica Chang32711b62010-08-10 18:41:29 -070021import android.bluetooth.BluetoothAdapter;
22import android.bluetooth.BluetoothPan;
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -080023import android.bluetooth.BluetoothProfile;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080024import android.content.BroadcastReceiver;
25import android.content.Context;
Amith Yamasani84a042c2011-03-02 11:25:04 -080026import android.content.DialogInterface;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080027import android.content.Intent;
28import android.content.IntentFilter;
PauloftheWest29aab7a2014-09-26 16:05:44 -070029import android.content.pm.PackageManager;
Mike Lockwood69a09572011-07-19 13:30:03 -070030import android.hardware.usb.UsbManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080031import android.net.ConnectivityManager;
Amith Yamasani84a042c2011-03-02 11:25:04 -080032import android.net.wifi.WifiConfiguration;
33import android.net.wifi.WifiManager;
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070034import android.os.Bundle;
Mike Lockwood26dad3e2010-03-03 06:19:55 -050035import android.os.Environment;
Robert Greenwalt505766c2011-11-09 15:03:14 -080036import android.os.SystemProperties;
Amith Yamasani394eaa22013-06-11 11:04:44 -070037import android.os.UserHandle;
38import android.os.UserManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080039import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080040import android.preference.PreferenceScreen;
PauloftheWest656c88c2014-08-24 11:59:35 -070041import android.preference.SwitchPreference;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040042import android.widget.TextView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080043
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080044import com.android.settings.wifi.WifiApDialog;
45import com.android.settings.wifi.WifiApEnabler;
46
Robert Greenwaltc4764d22010-02-12 14:21:37 -080047import java.util.ArrayList;
Robert Greenwaltf60b92b2012-09-13 15:02:00 -070048import java.util.concurrent.atomic.AtomicReference;
Matthew Xieb5f144a2012-06-28 18:41:54 -070049
Robert Greenwalt3901edb2010-01-26 10:22:37 -080050/*
51 * Displays preferences for Tethering.
52 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080053public class TetherSettings extends SettingsPreferenceFragment
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070054 implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
zzy7f38f472012-04-14 17:25:17 -070055 private static final String TAG = "TetherSettings";
Danica Chang32711b62010-08-10 18:41:29 -070056
Robert Greenwalt3901edb2010-01-26 10:22:37 -080057 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080058 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070059 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Rohit Sisodia37155e92014-09-22 16:34:56 -050060 private static final String TETHER_CHOICE = "TETHER_TYPE";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080061
Amith Yamasanid3fed682012-04-27 17:38:40 -070062 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070063
PauloftheWest656c88c2014-08-24 11:59:35 -070064 private SwitchPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080065
Irfan Sheriff65cff172010-02-08 10:46:30 -080066 private WifiApEnabler mWifiApEnabler;
PauloftheWest656c88c2014-08-24 11:59:35 -070067 private SwitchPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070068
PauloftheWest656c88c2014-08-24 11:59:35 -070069 private SwitchPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070070
Robert Greenwalt3901edb2010-01-26 10:22:37 -080071 private BroadcastReceiver mTetherChangeReceiver;
72
Robert Greenwaltc4764d22010-02-12 14:21:37 -080073 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080074
75 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080076
Danica Chang32711b62010-08-10 18:41:29 -070077 private String[] mBluetoothRegexs;
Robert Greenwalt4f1970f2012-09-16 17:03:08 -070078 private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<BluetoothPan>();
Danica Chang32711b62010-08-10 18:41:29 -070079
Amith Yamasani84a042c2011-03-02 11:25:04 -080080 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
81 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
82
83 private String[] mSecurityType;
84 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080085
86 private WifiApDialog mDialog;
87 private WifiManager mWifiManager;
88 private WifiConfiguration mWifiConfig = null;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040089 private UserManager mUm;
Amith Yamasani84a042c2011-03-02 11:25:04 -080090
Mike Lockwood69a09572011-07-19 13:30:03 -070091 private boolean mUsbConnected;
92 private boolean mMassStorageActive;
93
Jake Hambyc777ee22011-03-04 15:37:39 -080094 private boolean mBluetoothEnableForTether;
95
Irfan Sheriff01b32362011-11-04 12:08:56 -070096 private static final int INVALID = -1;
97 private static final int WIFI_TETHERING = 0;
98 private static final int USB_TETHERING = 1;
99 private static final int BLUETOOTH_TETHERING = 2;
100
101 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
102 private int mTetherChoice = INVALID;
103
104 /* Stores the package name and the class name of the provisioning app */
105 private String[] mProvisionApp;
106 private static final int PROVISION_REQUEST = 0;
107
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400108 private boolean mUnavailable;
109
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800110 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700111 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800112 super.onCreate(icicle);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500113
114 if(icicle != null) {
115 mTetherChoice = icicle.getInt(TETHER_CHOICE);
116 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800117 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800118
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400119 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
120
121 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
122 mUnavailable = true;
123 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
124 return;
125 }
126
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700127 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800128 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
129 if (adapter != null) {
130 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
131 BluetoothProfile.PAN);
132 }
133
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700134 mEnableWifiAp =
PauloftheWest656c88c2014-08-24 11:59:35 -0700135 (SwitchPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700136 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
PauloftheWest656c88c2014-08-24 11:59:35 -0700137 mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
138 mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800139
140 ConnectivityManager cm =
141 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800142
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800143 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800144 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700145 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
146
Amith Yamasanie419bc12011-02-14 14:19:08 -0800147 final boolean usbAvailable = mUsbRegexs.length != 0;
148 final boolean wifiAvailable = mWifiRegexs.length != 0;
149 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700150
Danica Chang32711b62010-08-10 18:41:29 -0700151 if (!usbAvailable || Utils.isMonkeyRunning()) {
152 getPreferenceScreen().removePreference(mUsbTether);
153 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800154
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800155 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700156 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700157 initWifiTethering();
158 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700159 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800160 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700161 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800162
Danica Chang32711b62010-08-10 18:41:29 -0700163 if (!bluetoothAvailable) {
164 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700165 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700166 BluetoothPan pan = mBluetoothPan.get();
167 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700168 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700169 } else {
170 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700171 }
172 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800173
Irfan Sheriff01b32362011-11-04 12:08:56 -0700174 mProvisionApp = getResources().getStringArray(
175 com.android.internal.R.array.config_mobile_hotspot_provision_app);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800176 }
177
Rohit Sisodia37155e92014-09-22 16:34:56 -0500178 @Override
179 public void onSaveInstanceState(Bundle savedInstanceState) {
180 savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice);
181 super.onSaveInstanceState(savedInstanceState);
182 }
183
Amith Yamasani84a042c2011-03-02 11:25:04 -0800184 private void initWifiTethering() {
185 final Activity activity = getActivity();
186 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
187 mWifiConfig = mWifiManager.getWifiApConfiguration();
188 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
189
190 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800191
192 if (mWifiConfig == null) {
193 final String s = activity.getString(
194 com.android.internal.R.string.wifi_tether_configure_ssid_default);
195 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
196 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
197 } else {
198 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
199 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
200 mWifiConfig.SSID,
201 mSecurityType[index]));
202 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800203 }
204
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800205 private BluetoothProfile.ServiceListener mProfileServiceListener =
206 new BluetoothProfile.ServiceListener() {
207 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700208 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800209 }
210 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700211 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800212 }
213 };
214
Irfan Sheriff0b266962010-04-06 15:16:44 -0700215 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700216 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700217 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800218 final Activity activity = getActivity();
219 mDialog = new WifiApDialog(activity, this, mWifiConfig);
220 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700221 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800222
Irfan Sheriff0b266962010-04-06 15:16:44 -0700223 return null;
224 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800225
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800226 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700227 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800228 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800229 String action = intent.getAction();
230 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800231 // TODO - this should understand the interface types
232 ArrayList<String> available = intent.getStringArrayListExtra(
233 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
234 ArrayList<String> active = intent.getStringArrayListExtra(
235 ConnectivityManager.EXTRA_ACTIVE_TETHER);
236 ArrayList<String> errored = intent.getStringArrayListExtra(
237 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700238 updateState(available.toArray(new String[available.size()]),
239 active.toArray(new String[active.size()]),
240 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700241 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
242 mMassStorageActive = true;
243 updateState();
244 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
245 mMassStorageActive = false;
246 updateState();
247 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
248 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800249 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800250 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
251 if (mBluetoothEnableForTether) {
252 switch (intent
253 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
254 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700255 BluetoothPan bluetoothPan = mBluetoothPan.get();
256 if (bluetoothPan != null) {
257 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700258 mBluetoothEnableForTether = false;
259 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800260 break;
261
262 case BluetoothAdapter.STATE_OFF:
263 case BluetoothAdapter.ERROR:
264 mBluetoothEnableForTether = false;
265 break;
266
267 default:
268 // ignore transition states
269 }
270 }
Danica Chang32711b62010-08-10 18:41:29 -0700271 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800272 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800273 }
274 }
275
276 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700277 public void onStart() {
278 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800279
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400280 if (mUnavailable) {
281 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
282 getListView().setEmptyView(emptyView);
283 if (emptyView != null) {
284 emptyView.setText(R.string.tethering_settings_not_available);
285 }
286 return;
287 }
288
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700289 final Activity activity = getActivity();
290
Mike Lockwood69a09572011-07-19 13:30:03 -0700291 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800292 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700293 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700294 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800295
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800296 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700297 filter.addAction(UsbManager.ACTION_USB_STATE);
298 activity.registerReceiver(mTetherChangeReceiver, filter);
299
300 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800301 filter.addAction(Intent.ACTION_MEDIA_SHARED);
302 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
303 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700304 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800305
Danica Chang32711b62010-08-10 18:41:29 -0700306 filter = new IntentFilter();
307 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700308 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700309
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700310 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700311 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700312 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700313 mWifiApEnabler.resume();
314 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800315
316 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800317 }
318
319 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700320 public void onStop() {
321 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400322
323 if (mUnavailable) {
324 return;
325 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700326 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800327 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700328 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700329 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700330 mWifiApEnabler.pause();
331 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800332 }
333
Robert Greenwalt209177a2010-03-04 13:29:02 -0800334 private void updateState() {
335 ConnectivityManager cm =
336 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
337
338 String[] available = cm.getTetherableIfaces();
339 String[] tethered = cm.getTetheredIfaces();
340 String[] errored = cm.getTetheringErroredIfaces();
341 updateState(available, tethered, errored);
342 }
343
Ben Clark0008d212010-07-27 16:20:59 +0100344 private void updateState(String[] available, String[] tethered,
345 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700346 updateUsbState(available, tethered, errored);
347 updateBluetoothState(available, tethered, errored);
348 }
349
350
351 private void updateUsbState(String[] available, String[] tethered,
352 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800353 ConnectivityManager cm =
354 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700355 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800356 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100357 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800358 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800359 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800360 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
361 usbError = cm.getLastTetherError(s);
362 }
363 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800364 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800365 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800366 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100367 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800368 for (String regex : mUsbRegexs) {
369 if (s.matches(regex)) usbTethered = true;
370 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800371 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800372 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100373 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800374 for (String regex : mUsbRegexs) {
375 if (s.matches(regex)) usbErrored = true;
376 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800377 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800378
379 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800380 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
381 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800382 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800383 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800384 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
385 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
386 } else {
387 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
388 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800389 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800390 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800391 } else if (usbErrored) {
392 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
393 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800394 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700395 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800396 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
397 mUsbTether.setEnabled(false);
398 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800399 } else {
400 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
401 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800402 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800403 }
404 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800405
Danica Chang32711b62010-08-10 18:41:29 -0700406 private void updateBluetoothState(String[] available, String[] tethered,
407 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800408 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700409 for (String s: errored) {
410 for (String regex : mBluetoothRegexs) {
411 if (s.matches(regex)) bluetoothErrored = true;
412 }
413 }
414
415 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800416 if (adapter == null)
417 return;
Danica Chang32711b62010-08-10 18:41:29 -0700418 int btState = adapter.getState();
419 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
420 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500421 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700422 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
423 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700424 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700425 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700426 BluetoothPan bluetoothPan = mBluetoothPan.get();
427 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
428 bluetoothPan.isTetheringOn()) {
429 mBluetoothTether.setChecked(true);
430 mBluetoothTether.setEnabled(true);
431 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
432 if (bluetoothTethered > 1) {
433 String summary = getString(
434 R.string.bluetooth_tethering_devices_connected_subtext,
435 bluetoothTethered);
436 mBluetoothTether.setSummary(summary);
437 } else if (bluetoothTethered == 1) {
438 mBluetoothTether.setSummary(
439 R.string.bluetooth_tethering_device_connected_subtext);
440 } else if (bluetoothErrored) {
441 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
442 } else {
443 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
444 }
445 } else {
446 mBluetoothTether.setEnabled(true);
447 mBluetoothTether.setChecked(false);
448 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
449 }
Danica Chang32711b62010-08-10 18:41:29 -0700450 }
451 }
452
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700453 public boolean onPreferenceChange(Preference preference, Object value) {
454 boolean enable = (Boolean) value;
455
456 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700457 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700458 } else {
459 mWifiApEnabler.setSoftapEnabled(false);
460 }
461 return false;
462 }
463
PauloftheWest29aab7a2014-09-26 16:05:44 -0700464 public static boolean isProvisioningNeededButUnavailable(Context context) {
465 String[] provisionApp = context.getResources().getStringArray(
466 com.android.internal.R.array.config_mobile_hotspot_provision_app);
467 return (isProvisioningNeeded(provisionApp)
468 && !isIntentAvailable(context, provisionApp));
469 }
470
471 private static boolean isIntentAvailable(Context context, String[] provisionApp) {
472 if (provisionApp.length < 2) {
473 throw new IllegalArgumentException("provisionApp length should at least be 2");
474 }
475 final PackageManager packageManager = context.getPackageManager();
476 Intent intent = new Intent(Intent.ACTION_MAIN);
477 intent.setClassName(provisionApp[0], provisionApp[1]);
478
479 return (packageManager.queryIntentActivities(intent,
480 PackageManager.MATCH_DEFAULT_ONLY).size() > 0);
481 }
482
483
484 private static boolean isProvisioningNeeded(String[] provisionApp) {
485 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)
486 || provisionApp == null) {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800487 return false;
488 }
PauloftheWest29aab7a2014-09-26 16:05:44 -0700489 return (provisionApp.length == 2);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700490 }
491
492 private void startProvisioningIfNecessary(int choice) {
493 mTetherChoice = choice;
PauloftheWest29aab7a2014-09-26 16:05:44 -0700494 if (isProvisioningNeeded(mProvisionApp)) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700495 Intent intent = new Intent(Intent.ACTION_MAIN);
496 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500497 intent.putExtra(TETHER_CHOICE, mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700498 startActivityForResult(intent, PROVISION_REQUEST);
499 } else {
500 startTethering();
501 }
502 }
503
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700504 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
505 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700506 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700507 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700508 startTethering();
509 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700510 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700511 //Wifi tethering is never turned on until afterwards
512 switch (mTetherChoice) {
513 case BLUETOOTH_TETHERING:
514 mBluetoothTether.setChecked(false);
515 break;
516 case USB_TETHERING:
517 mUsbTether.setChecked(false);
518 break;
519 }
520 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700521 }
522 }
523 }
524
Irfan Sheriff01b32362011-11-04 12:08:56 -0700525 private void startTethering() {
526 switch (mTetherChoice) {
527 case WIFI_TETHERING:
528 mWifiApEnabler.setSoftapEnabled(true);
529 break;
530 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700531 // turn on Bluetooth first
532 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
533 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800534 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700535 adapter.enable();
536 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
537 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800538 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700539 BluetoothPan bluetoothPan = mBluetoothPan.get();
540 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800541 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700542 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700543 break;
544 case USB_TETHERING:
545 setUsbTethering(true);
546 break;
547 default:
548 //should not happen
549 break;
550 }
551 }
552
553 private void setUsbTethering(boolean enabled) {
554 ConnectivityManager cm =
555 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800556 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700557 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700558 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
559 return;
560 }
561 mUsbTether.setSummary("");
562 }
563
564 @Override
565 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
566 ConnectivityManager cm =
567 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
568
569 if (preference == mUsbTether) {
570 boolean newState = mUsbTether.isChecked();
571
572 if (newState) {
573 startProvisioningIfNecessary(USB_TETHERING);
574 } else {
575 setUsbTethering(newState);
576 }
577 } else if (preference == mBluetoothTether) {
578 boolean bluetoothTetherState = mBluetoothTether.isChecked();
579
580 if (bluetoothTetherState) {
581 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700582 } else {
583 boolean errored = false;
584
Danica Chang32711b62010-08-10 18:41:29 -0700585 String [] tethered = cm.getTetheredIfaces();
586 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
587 if (bluetoothIface != null &&
588 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
589 errored = true;
590 }
591
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700592 BluetoothPan bluetoothPan = mBluetoothPan.get();
593 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700594 if (errored) {
595 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
596 } else {
597 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
598 }
599 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800600 } else if (preference == mCreateNetwork) {
601 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800602 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700603
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700604 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800605 }
606
Jake Hamby436b29e2011-02-07 18:21:25 -0800607 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800608 for (String iface : ifaces) {
609 for (String regex : regexes) {
610 if (iface.matches(regex)) {
611 return iface;
612 }
613 }
614 }
615 return null;
616 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800617
618 public void onClick(DialogInterface dialogInterface, int button) {
619 if (button == DialogInterface.BUTTON_POSITIVE) {
620 mWifiConfig = mDialog.getConfig();
621 if (mWifiConfig != null) {
622 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700623 * if soft AP is stopped, bring up
624 * else restart with new config
625 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800626 */
627 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700628 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800629 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800630 } else {
631 mWifiManager.setWifiApConfiguration(mWifiConfig);
632 }
633 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
634 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
635 mWifiConfig.SSID,
636 mSecurityType[index]));
637 }
638 }
639 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700640
641 @Override
642 public int getHelpResource() {
643 return R.string.help_url_tether;
644 }
Amith Yamasani394eaa22013-06-11 11:04:44 -0700645
646 /**
647 * Checks whether this screen will have anything to show on this device. This is called by
648 * the shortcut picker for Settings shortcuts (home screen widget).
649 * @param context a context object for getting a system service.
650 * @return whether Tether & portable hotspot should be shown in the shortcuts picker.
651 */
652 public static boolean showInShortcuts(Context context) {
653 final ConnectivityManager cm =
654 (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
655 final boolean isSecondaryUser = UserHandle.myUserId() != UserHandle.USER_OWNER;
656 return !isSecondaryUser && cm.isTetheringSupported();
657 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800658}