blob: c611772488013a69c7684a380d296e6e748af4b2 [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
Irfan Sheriff65cff172010-02-08 10:46:30 -080019import com.android.settings.wifi.WifiApEnabler;
Amith Yamasani84a042c2011-03-02 11:25:04 -080020import com.android.settings.wifi.WifiApDialog;
Irfan Sheriff65cff172010-02-08 10:46:30 -080021
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070022import android.app.Activity;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070023import android.app.AlertDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -070024import android.app.Dialog;
Danica Chang32711b62010-08-10 18:41:29 -070025import android.bluetooth.BluetoothAdapter;
26import android.bluetooth.BluetoothPan;
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -080027import android.bluetooth.BluetoothProfile;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080028import android.content.BroadcastReceiver;
29import android.content.Context;
Amith Yamasani84a042c2011-03-02 11:25:04 -080030import android.content.DialogInterface;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080031import android.content.Intent;
32import android.content.IntentFilter;
PauloftheWest29aab7a2014-09-26 16:05:44 -070033import android.content.pm.PackageManager;
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070034import android.content.res.AssetManager;
Mike Lockwood69a09572011-07-19 13:30:03 -070035import android.hardware.usb.UsbManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080036import android.net.ConnectivityManager;
Amith Yamasani84a042c2011-03-02 11:25:04 -080037import android.net.wifi.WifiConfiguration;
38import android.net.wifi.WifiManager;
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070039import android.os.Bundle;
Mike Lockwood26dad3e2010-03-03 06:19:55 -050040import android.os.Environment;
Robert Greenwalt505766c2011-11-09 15:03:14 -080041import android.os.SystemProperties;
Amith Yamasani394eaa22013-06-11 11:04:44 -070042import android.os.UserHandle;
43import android.os.UserManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080044import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080045import android.preference.PreferenceScreen;
PauloftheWest656c88c2014-08-24 11:59:35 -070046import android.preference.SwitchPreference;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070047import android.text.TextUtils;
Amith Yamasani476d7952011-01-21 09:28:31 -080048import android.view.ViewGroup;
49import android.view.ViewParent;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070050import android.webkit.WebView;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040051import android.widget.TextView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080052
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070053import java.io.InputStream;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080054import java.util.ArrayList;
Robert Greenwaltf60b92b2012-09-13 15:02:00 -070055import java.util.concurrent.atomic.AtomicReference;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070056import java.util.Locale;
Matthew Xieb5f144a2012-06-28 18:41:54 -070057
Robert Greenwalt3901edb2010-01-26 10:22:37 -080058/*
59 * Displays preferences for Tethering.
60 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080061public class TetherSettings extends SettingsPreferenceFragment
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070062 implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
zzy7f38f472012-04-14 17:25:17 -070063 private static final String TAG = "TetherSettings";
Danica Chang32711b62010-08-10 18:41:29 -070064
Robert Greenwalt3901edb2010-01-26 10:22:37 -080065 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080066 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070067 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Rohit Sisodia37155e92014-09-22 16:34:56 -050068 private static final String TETHER_CHOICE = "TETHER_TYPE";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080069
Amith Yamasanid3fed682012-04-27 17:38:40 -070070 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070071
72 private WebView mView;
PauloftheWest656c88c2014-08-24 11:59:35 -070073 private SwitchPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080074
Irfan Sheriff65cff172010-02-08 10:46:30 -080075 private WifiApEnabler mWifiApEnabler;
PauloftheWest656c88c2014-08-24 11:59:35 -070076 private SwitchPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070077
PauloftheWest656c88c2014-08-24 11:59:35 -070078 private SwitchPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070079
Robert Greenwalt3901edb2010-01-26 10:22:37 -080080 private BroadcastReceiver mTetherChangeReceiver;
81
Robert Greenwaltc4764d22010-02-12 14:21:37 -080082 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080083
84 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080085
Danica Chang32711b62010-08-10 18:41:29 -070086 private String[] mBluetoothRegexs;
Robert Greenwalt4f1970f2012-09-16 17:03:08 -070087 private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<BluetoothPan>();
Danica Chang32711b62010-08-10 18:41:29 -070088
Amith Yamasani84a042c2011-03-02 11:25:04 -080089 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
90 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
91
92 private String[] mSecurityType;
93 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080094
95 private WifiApDialog mDialog;
96 private WifiManager mWifiManager;
97 private WifiConfiguration mWifiConfig = null;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040098 private UserManager mUm;
Amith Yamasani84a042c2011-03-02 11:25:04 -080099
Mike Lockwood69a09572011-07-19 13:30:03 -0700100 private boolean mUsbConnected;
101 private boolean mMassStorageActive;
102
Jake Hambyc777ee22011-03-04 15:37:39 -0800103 private boolean mBluetoothEnableForTether;
104
Irfan Sheriff01b32362011-11-04 12:08:56 -0700105 private static final int INVALID = -1;
106 private static final int WIFI_TETHERING = 0;
107 private static final int USB_TETHERING = 1;
108 private static final int BLUETOOTH_TETHERING = 2;
109
110 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
111 private int mTetherChoice = INVALID;
112
113 /* Stores the package name and the class name of the provisioning app */
114 private String[] mProvisionApp;
115 private static final int PROVISION_REQUEST = 0;
116
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400117 private boolean mUnavailable;
118
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800119 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700120 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800121 super.onCreate(icicle);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500122
123 if(icicle != null) {
124 mTetherChoice = icicle.getInt(TETHER_CHOICE);
125 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800126 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800127
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400128 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
129
130 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
131 mUnavailable = true;
132 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
133 return;
134 }
135
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700136 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800137 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
138 if (adapter != null) {
139 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
140 BluetoothProfile.PAN);
141 }
142
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700143 mEnableWifiAp =
PauloftheWest656c88c2014-08-24 11:59:35 -0700144 (SwitchPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700145 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
PauloftheWest656c88c2014-08-24 11:59:35 -0700146 mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
147 mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800148
149 ConnectivityManager cm =
150 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800151
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800152 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800153 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700154 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
155
Amith Yamasanie419bc12011-02-14 14:19:08 -0800156 final boolean usbAvailable = mUsbRegexs.length != 0;
157 final boolean wifiAvailable = mWifiRegexs.length != 0;
158 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700159
Danica Chang32711b62010-08-10 18:41:29 -0700160 if (!usbAvailable || Utils.isMonkeyRunning()) {
161 getPreferenceScreen().removePreference(mUsbTether);
162 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800163
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800164 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700165 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700166 initWifiTethering();
167 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700168 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800169 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700170 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800171
Danica Chang32711b62010-08-10 18:41:29 -0700172 if (!bluetoothAvailable) {
173 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700174 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700175 BluetoothPan pan = mBluetoothPan.get();
176 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700177 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700178 } else {
179 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700180 }
181 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800182
Irfan Sheriff01b32362011-11-04 12:08:56 -0700183 mProvisionApp = getResources().getStringArray(
184 com.android.internal.R.array.config_mobile_hotspot_provision_app);
185
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700186 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800187 }
188
Rohit Sisodia37155e92014-09-22 16:34:56 -0500189 @Override
190 public void onSaveInstanceState(Bundle savedInstanceState) {
191 savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice);
192 super.onSaveInstanceState(savedInstanceState);
193 }
194
Amith Yamasani84a042c2011-03-02 11:25:04 -0800195 private void initWifiTethering() {
196 final Activity activity = getActivity();
197 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
198 mWifiConfig = mWifiManager.getWifiApConfiguration();
199 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
200
201 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800202
203 if (mWifiConfig == null) {
204 final String s = activity.getString(
205 com.android.internal.R.string.wifi_tether_configure_ssid_default);
206 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
207 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
208 } else {
209 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
210 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
211 mWifiConfig.SSID,
212 mSecurityType[index]));
213 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800214 }
215
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800216 private BluetoothProfile.ServiceListener mProfileServiceListener =
217 new BluetoothProfile.ServiceListener() {
218 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700219 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800220 }
221 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700222 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800223 }
224 };
225
Irfan Sheriff0b266962010-04-06 15:16:44 -0700226 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700227 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700228 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800229 final Activity activity = getActivity();
230 mDialog = new WifiApDialog(activity, this, mWifiConfig);
231 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700232 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800233
Irfan Sheriff0b266962010-04-06 15:16:44 -0700234 return null;
235 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800236
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800237 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700238 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800239 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800240 String action = intent.getAction();
241 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800242 // TODO - this should understand the interface types
243 ArrayList<String> available = intent.getStringArrayListExtra(
244 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
245 ArrayList<String> active = intent.getStringArrayListExtra(
246 ConnectivityManager.EXTRA_ACTIVE_TETHER);
247 ArrayList<String> errored = intent.getStringArrayListExtra(
248 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700249 updateState(available.toArray(new String[available.size()]),
250 active.toArray(new String[active.size()]),
251 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700252 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
253 mMassStorageActive = true;
254 updateState();
255 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
256 mMassStorageActive = false;
257 updateState();
258 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
259 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800260 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800261 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
262 if (mBluetoothEnableForTether) {
263 switch (intent
264 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
265 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700266 BluetoothPan bluetoothPan = mBluetoothPan.get();
267 if (bluetoothPan != null) {
268 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700269 mBluetoothEnableForTether = false;
270 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800271 break;
272
273 case BluetoothAdapter.STATE_OFF:
274 case BluetoothAdapter.ERROR:
275 mBluetoothEnableForTether = false;
276 break;
277
278 default:
279 // ignore transition states
280 }
281 }
Danica Chang32711b62010-08-10 18:41:29 -0700282 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800283 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800284 }
285 }
286
287 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700288 public void onStart() {
289 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800290
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400291 if (mUnavailable) {
292 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
293 getListView().setEmptyView(emptyView);
294 if (emptyView != null) {
295 emptyView.setText(R.string.tethering_settings_not_available);
296 }
297 return;
298 }
299
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700300 final Activity activity = getActivity();
301
Mike Lockwood69a09572011-07-19 13:30:03 -0700302 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800303 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700304 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700305 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800306
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800307 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700308 filter.addAction(UsbManager.ACTION_USB_STATE);
309 activity.registerReceiver(mTetherChangeReceiver, filter);
310
311 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800312 filter.addAction(Intent.ACTION_MEDIA_SHARED);
313 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
314 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700315 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800316
Danica Chang32711b62010-08-10 18:41:29 -0700317 filter = new IntentFilter();
318 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700319 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700320
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700321 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700322 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700323 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700324 mWifiApEnabler.resume();
325 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800326
327 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800328 }
329
330 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700331 public void onStop() {
332 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400333
334 if (mUnavailable) {
335 return;
336 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700337 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800338 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700339 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700340 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700341 mWifiApEnabler.pause();
342 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800343 }
344
Robert Greenwalt209177a2010-03-04 13:29:02 -0800345 private void updateState() {
346 ConnectivityManager cm =
347 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
348
349 String[] available = cm.getTetherableIfaces();
350 String[] tethered = cm.getTetheredIfaces();
351 String[] errored = cm.getTetheringErroredIfaces();
352 updateState(available, tethered, errored);
353 }
354
Ben Clark0008d212010-07-27 16:20:59 +0100355 private void updateState(String[] available, String[] tethered,
356 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700357 updateUsbState(available, tethered, errored);
358 updateBluetoothState(available, tethered, errored);
359 }
360
361
362 private void updateUsbState(String[] available, String[] tethered,
363 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800364 ConnectivityManager cm =
365 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700366 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800367 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100368 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800369 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800370 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800371 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
372 usbError = cm.getLastTetherError(s);
373 }
374 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800375 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800376 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800377 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100378 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800379 for (String regex : mUsbRegexs) {
380 if (s.matches(regex)) usbTethered = true;
381 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800382 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800383 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100384 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800385 for (String regex : mUsbRegexs) {
386 if (s.matches(regex)) usbErrored = true;
387 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800388 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800389
390 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800391 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
392 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800393 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800394 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800395 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
396 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
397 } else {
398 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
399 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800400 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800401 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800402 } else if (usbErrored) {
403 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
404 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800405 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700406 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800407 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
408 mUsbTether.setEnabled(false);
409 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800410 } else {
411 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
412 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800413 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800414 }
415 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800416
Danica Chang32711b62010-08-10 18:41:29 -0700417 private void updateBluetoothState(String[] available, String[] tethered,
418 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800419 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700420 for (String s: errored) {
421 for (String regex : mBluetoothRegexs) {
422 if (s.matches(regex)) bluetoothErrored = true;
423 }
424 }
425
426 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800427 if (adapter == null)
428 return;
Danica Chang32711b62010-08-10 18:41:29 -0700429 int btState = adapter.getState();
430 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
431 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500432 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700433 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
434 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700435 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700436 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700437 BluetoothPan bluetoothPan = mBluetoothPan.get();
438 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
439 bluetoothPan.isTetheringOn()) {
440 mBluetoothTether.setChecked(true);
441 mBluetoothTether.setEnabled(true);
442 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
443 if (bluetoothTethered > 1) {
444 String summary = getString(
445 R.string.bluetooth_tethering_devices_connected_subtext,
446 bluetoothTethered);
447 mBluetoothTether.setSummary(summary);
448 } else if (bluetoothTethered == 1) {
449 mBluetoothTether.setSummary(
450 R.string.bluetooth_tethering_device_connected_subtext);
451 } else if (bluetoothErrored) {
452 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
453 } else {
454 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
455 }
456 } else {
457 mBluetoothTether.setEnabled(true);
458 mBluetoothTether.setChecked(false);
459 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
460 }
Danica Chang32711b62010-08-10 18:41:29 -0700461 }
462 }
463
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700464 public boolean onPreferenceChange(Preference preference, Object value) {
465 boolean enable = (Boolean) value;
466
467 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700468 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700469 } else {
470 mWifiApEnabler.setSoftapEnabled(false);
471 }
472 return false;
473 }
474
PauloftheWest29aab7a2014-09-26 16:05:44 -0700475 public static boolean isProvisioningNeededButUnavailable(Context context) {
476 String[] provisionApp = context.getResources().getStringArray(
477 com.android.internal.R.array.config_mobile_hotspot_provision_app);
478 return (isProvisioningNeeded(provisionApp)
479 && !isIntentAvailable(context, provisionApp));
480 }
481
482 private static boolean isIntentAvailable(Context context, String[] provisionApp) {
483 if (provisionApp.length < 2) {
484 throw new IllegalArgumentException("provisionApp length should at least be 2");
485 }
486 final PackageManager packageManager = context.getPackageManager();
487 Intent intent = new Intent(Intent.ACTION_MAIN);
488 intent.setClassName(provisionApp[0], provisionApp[1]);
489
490 return (packageManager.queryIntentActivities(intent,
491 PackageManager.MATCH_DEFAULT_ONLY).size() > 0);
492 }
493
494
495 private static boolean isProvisioningNeeded(String[] provisionApp) {
496 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)
497 || provisionApp == null) {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800498 return false;
499 }
PauloftheWest29aab7a2014-09-26 16:05:44 -0700500 return (provisionApp.length == 2);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700501 }
502
503 private void startProvisioningIfNecessary(int choice) {
504 mTetherChoice = choice;
PauloftheWest29aab7a2014-09-26 16:05:44 -0700505 if (isProvisioningNeeded(mProvisionApp)) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700506 Intent intent = new Intent(Intent.ACTION_MAIN);
507 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500508 intent.putExtra(TETHER_CHOICE, mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700509 startActivityForResult(intent, PROVISION_REQUEST);
510 } else {
511 startTethering();
512 }
513 }
514
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700515 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
516 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700517 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700518 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700519 startTethering();
520 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700521 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700522 //Wifi tethering is never turned on until afterwards
523 switch (mTetherChoice) {
524 case BLUETOOTH_TETHERING:
525 mBluetoothTether.setChecked(false);
526 break;
527 case USB_TETHERING:
528 mUsbTether.setChecked(false);
529 break;
530 }
531 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700532 }
533 }
534 }
535
Irfan Sheriff01b32362011-11-04 12:08:56 -0700536 private void startTethering() {
537 switch (mTetherChoice) {
538 case WIFI_TETHERING:
539 mWifiApEnabler.setSoftapEnabled(true);
540 break;
541 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700542 // turn on Bluetooth first
543 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
544 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800545 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700546 adapter.enable();
547 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
548 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800549 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700550 BluetoothPan bluetoothPan = mBluetoothPan.get();
551 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800552 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700553 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700554 break;
555 case USB_TETHERING:
556 setUsbTethering(true);
557 break;
558 default:
559 //should not happen
560 break;
561 }
562 }
563
564 private void setUsbTethering(boolean enabled) {
565 ConnectivityManager cm =
566 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800567 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700568 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700569 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
570 return;
571 }
572 mUsbTether.setSummary("");
573 }
574
575 @Override
576 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
577 ConnectivityManager cm =
578 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
579
580 if (preference == mUsbTether) {
581 boolean newState = mUsbTether.isChecked();
582
583 if (newState) {
584 startProvisioningIfNecessary(USB_TETHERING);
585 } else {
586 setUsbTethering(newState);
587 }
588 } else if (preference == mBluetoothTether) {
589 boolean bluetoothTetherState = mBluetoothTether.isChecked();
590
591 if (bluetoothTetherState) {
592 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700593 } else {
594 boolean errored = false;
595
Danica Chang32711b62010-08-10 18:41:29 -0700596 String [] tethered = cm.getTetheredIfaces();
597 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
598 if (bluetoothIface != null &&
599 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
600 errored = true;
601 }
602
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700603 BluetoothPan bluetoothPan = mBluetoothPan.get();
604 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700605 if (errored) {
606 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
607 } else {
608 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
609 }
610 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800611 } else if (preference == mCreateNetwork) {
612 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800613 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700614
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700615 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800616 }
617
Jake Hamby436b29e2011-02-07 18:21:25 -0800618 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800619 for (String iface : ifaces) {
620 for (String regex : regexes) {
621 if (iface.matches(regex)) {
622 return iface;
623 }
624 }
625 }
626 return null;
627 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800628
629 public void onClick(DialogInterface dialogInterface, int button) {
630 if (button == DialogInterface.BUTTON_POSITIVE) {
631 mWifiConfig = mDialog.getConfig();
632 if (mWifiConfig != null) {
633 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700634 * if soft AP is stopped, bring up
635 * else restart with new config
636 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800637 */
638 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700639 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800640 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800641 } else {
642 mWifiManager.setWifiApConfiguration(mWifiConfig);
643 }
644 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
645 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
646 mWifiConfig.SSID,
647 mSecurityType[index]));
648 }
649 }
650 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700651
652 @Override
653 public int getHelpResource() {
654 return R.string.help_url_tether;
655 }
Amith Yamasani394eaa22013-06-11 11:04:44 -0700656
657 /**
658 * Checks whether this screen will have anything to show on this device. This is called by
659 * the shortcut picker for Settings shortcuts (home screen widget).
660 * @param context a context object for getting a system service.
661 * @return whether Tether & portable hotspot should be shown in the shortcuts picker.
662 */
663 public static boolean showInShortcuts(Context context) {
664 final ConnectivityManager cm =
665 (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
666 final boolean isSecondaryUser = UserHandle.myUserId() != UserHandle.USER_OWNER;
667 return !isSecondaryUser && cm.isTetheringSupported();
668 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800669}