blob: 4e0933d534ad1a2ca8344c71d762d15a9229f35e [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;
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070033import android.content.res.AssetManager;
Mike Lockwood69a09572011-07-19 13:30:03 -070034import android.hardware.usb.UsbManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080035import android.net.ConnectivityManager;
Amith Yamasani84a042c2011-03-02 11:25:04 -080036import android.net.wifi.WifiConfiguration;
37import android.net.wifi.WifiManager;
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070038import android.os.Bundle;
Mike Lockwood26dad3e2010-03-03 06:19:55 -050039import android.os.Environment;
Robert Greenwalt505766c2011-11-09 15:03:14 -080040import android.os.SystemProperties;
Amith Yamasani394eaa22013-06-11 11:04:44 -070041import android.os.UserHandle;
42import android.os.UserManager;
Robert Greenwalt209177a2010-03-04 13:29:02 -080043import android.preference.CheckBoxPreference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080044import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080045import android.preference.PreferenceScreen;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070046import android.text.TextUtils;
Amith Yamasani476d7952011-01-21 09:28:31 -080047import android.view.ViewGroup;
48import android.view.ViewParent;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070049import android.webkit.WebView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080050
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070051import java.io.InputStream;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080052import java.util.ArrayList;
Robert Greenwaltf60b92b2012-09-13 15:02:00 -070053import java.util.concurrent.atomic.AtomicReference;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070054import java.util.Locale;
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";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080066
Amith Yamasanid3fed682012-04-27 17:38:40 -070067 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070068
69 private WebView mView;
Robert Greenwalt209177a2010-03-04 13:29:02 -080070 private CheckBoxPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080071
Irfan Sheriff65cff172010-02-08 10:46:30 -080072 private WifiApEnabler mWifiApEnabler;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070073 private CheckBoxPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070074
75 private CheckBoxPreference 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;
95
Mike Lockwood69a09572011-07-19 13:30:03 -070096 private boolean mUsbConnected;
97 private boolean mMassStorageActive;
98
Jake Hambyc777ee22011-03-04 15:37:39 -080099 private boolean mBluetoothEnableForTether;
100
Irfan Sheriff01b32362011-11-04 12:08:56 -0700101 private static final int INVALID = -1;
102 private static final int WIFI_TETHERING = 0;
103 private static final int USB_TETHERING = 1;
104 private static final int BLUETOOTH_TETHERING = 2;
105
106 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
107 private int mTetherChoice = INVALID;
108
109 /* Stores the package name and the class name of the provisioning app */
110 private String[] mProvisionApp;
111 private static final int PROVISION_REQUEST = 0;
112
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800113 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700114 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800115 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800116 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800117
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700118 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800119 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
120 if (adapter != null) {
121 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
122 BluetoothProfile.PAN);
123 }
124
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700125 mEnableWifiAp =
Jake Hamby436b29e2011-02-07 18:21:25 -0800126 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700127 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800128 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700129 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800130
131 ConnectivityManager cm =
132 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800133
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800134 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800135 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700136 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
137
Amith Yamasanie419bc12011-02-14 14:19:08 -0800138 final boolean usbAvailable = mUsbRegexs.length != 0;
139 final boolean wifiAvailable = mWifiRegexs.length != 0;
140 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700141
Danica Chang32711b62010-08-10 18:41:29 -0700142 if (!usbAvailable || Utils.isMonkeyRunning()) {
143 getPreferenceScreen().removePreference(mUsbTether);
144 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800145
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800146 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700147 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700148 initWifiTethering();
149 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700150 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800151 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700152 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800153
Danica Chang32711b62010-08-10 18:41:29 -0700154 if (!bluetoothAvailable) {
155 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700156 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700157 BluetoothPan pan = mBluetoothPan.get();
158 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700159 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700160 } else {
161 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700162 }
163 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800164
Irfan Sheriff01b32362011-11-04 12:08:56 -0700165 mProvisionApp = getResources().getStringArray(
166 com.android.internal.R.array.config_mobile_hotspot_provision_app);
167
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700168 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800169 }
170
171 private void initWifiTethering() {
172 final Activity activity = getActivity();
173 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
174 mWifiConfig = mWifiManager.getWifiApConfiguration();
175 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
176
177 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800178
179 if (mWifiConfig == null) {
180 final String s = activity.getString(
181 com.android.internal.R.string.wifi_tether_configure_ssid_default);
182 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
183 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
184 } else {
185 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
186 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
187 mWifiConfig.SSID,
188 mSecurityType[index]));
189 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800190 }
191
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800192 private BluetoothProfile.ServiceListener mProfileServiceListener =
193 new BluetoothProfile.ServiceListener() {
194 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700195 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800196 }
197 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700198 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800199 }
200 };
201
Irfan Sheriff0b266962010-04-06 15:16:44 -0700202 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700203 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700204 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800205 final Activity activity = getActivity();
206 mDialog = new WifiApDialog(activity, this, mWifiConfig);
207 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700208 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800209
Irfan Sheriff0b266962010-04-06 15:16:44 -0700210 return null;
211 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800212
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800213 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700214 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800215 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800216 String action = intent.getAction();
217 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800218 // TODO - this should understand the interface types
219 ArrayList<String> available = intent.getStringArrayListExtra(
220 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
221 ArrayList<String> active = intent.getStringArrayListExtra(
222 ConnectivityManager.EXTRA_ACTIVE_TETHER);
223 ArrayList<String> errored = intent.getStringArrayListExtra(
224 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700225 updateState(available.toArray(new String[available.size()]),
226 active.toArray(new String[active.size()]),
227 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700228 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
229 mMassStorageActive = true;
230 updateState();
231 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
232 mMassStorageActive = false;
233 updateState();
234 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
235 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800236 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800237 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
238 if (mBluetoothEnableForTether) {
239 switch (intent
240 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
241 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700242 BluetoothPan bluetoothPan = mBluetoothPan.get();
243 if (bluetoothPan != null) {
244 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700245 mBluetoothEnableForTether = false;
246 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800247 break;
248
249 case BluetoothAdapter.STATE_OFF:
250 case BluetoothAdapter.ERROR:
251 mBluetoothEnableForTether = false;
252 break;
253
254 default:
255 // ignore transition states
256 }
257 }
Danica Chang32711b62010-08-10 18:41:29 -0700258 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800259 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800260 }
261 }
262
263 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700264 public void onStart() {
265 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800266
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700267 final Activity activity = getActivity();
268
Mike Lockwood69a09572011-07-19 13:30:03 -0700269 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800270 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700271 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700272 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800273
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800274 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700275 filter.addAction(UsbManager.ACTION_USB_STATE);
276 activity.registerReceiver(mTetherChangeReceiver, filter);
277
278 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800279 filter.addAction(Intent.ACTION_MEDIA_SHARED);
280 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
281 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700282 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800283
Danica Chang32711b62010-08-10 18:41:29 -0700284 filter = new IntentFilter();
285 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700286 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700287
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700288 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700289 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700290 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700291 mWifiApEnabler.resume();
292 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800293
294 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800295 }
296
297 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700298 public void onStop() {
299 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700300 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800301 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700302 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700303 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700304 mWifiApEnabler.pause();
305 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800306 }
307
Robert Greenwalt209177a2010-03-04 13:29:02 -0800308 private void updateState() {
309 ConnectivityManager cm =
310 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
311
312 String[] available = cm.getTetherableIfaces();
313 String[] tethered = cm.getTetheredIfaces();
314 String[] errored = cm.getTetheringErroredIfaces();
315 updateState(available, tethered, errored);
316 }
317
Ben Clark0008d212010-07-27 16:20:59 +0100318 private void updateState(String[] available, String[] tethered,
319 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700320 updateUsbState(available, tethered, errored);
321 updateBluetoothState(available, tethered, errored);
322 }
323
324
325 private void updateUsbState(String[] available, String[] tethered,
326 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800327 ConnectivityManager cm =
328 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700329 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800330 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100331 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800332 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800333 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800334 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
335 usbError = cm.getLastTetherError(s);
336 }
337 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800338 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800339 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800340 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100341 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800342 for (String regex : mUsbRegexs) {
343 if (s.matches(regex)) usbTethered = true;
344 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800345 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800346 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100347 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800348 for (String regex : mUsbRegexs) {
349 if (s.matches(regex)) usbErrored = true;
350 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800351 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800352
353 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800354 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
355 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800356 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800357 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800358 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
359 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
360 } else {
361 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
362 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800363 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800364 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800365 } else if (usbErrored) {
366 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
367 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800368 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700369 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800370 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
371 mUsbTether.setEnabled(false);
372 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800373 } else {
374 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
375 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800376 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800377 }
378 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800379
Danica Chang32711b62010-08-10 18:41:29 -0700380 private void updateBluetoothState(String[] available, String[] tethered,
381 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800382 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700383 for (String s: errored) {
384 for (String regex : mBluetoothRegexs) {
385 if (s.matches(regex)) bluetoothErrored = true;
386 }
387 }
388
389 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800390 if (adapter == null)
391 return;
Danica Chang32711b62010-08-10 18:41:29 -0700392 int btState = adapter.getState();
393 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
394 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500395 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700396 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
397 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700398 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700399 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700400 BluetoothPan bluetoothPan = mBluetoothPan.get();
401 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
402 bluetoothPan.isTetheringOn()) {
403 mBluetoothTether.setChecked(true);
404 mBluetoothTether.setEnabled(true);
405 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
406 if (bluetoothTethered > 1) {
407 String summary = getString(
408 R.string.bluetooth_tethering_devices_connected_subtext,
409 bluetoothTethered);
410 mBluetoothTether.setSummary(summary);
411 } else if (bluetoothTethered == 1) {
412 mBluetoothTether.setSummary(
413 R.string.bluetooth_tethering_device_connected_subtext);
414 } else if (bluetoothErrored) {
415 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
416 } else {
417 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
418 }
419 } else {
420 mBluetoothTether.setEnabled(true);
421 mBluetoothTether.setChecked(false);
422 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
423 }
Danica Chang32711b62010-08-10 18:41:29 -0700424 }
425 }
426
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700427 public boolean onPreferenceChange(Preference preference, Object value) {
428 boolean enable = (Boolean) value;
429
430 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700431 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700432 } else {
433 mWifiApEnabler.setSoftapEnabled(false);
434 }
435 return false;
436 }
437
Irfan Sheriff01b32362011-11-04 12:08:56 -0700438 boolean isProvisioningNeeded() {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800439 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)) {
440 return false;
441 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700442 return mProvisionApp.length == 2;
443 }
444
445 private void startProvisioningIfNecessary(int choice) {
446 mTetherChoice = choice;
447 if (isProvisioningNeeded()) {
448 Intent intent = new Intent(Intent.ACTION_MAIN);
449 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
450 startActivityForResult(intent, PROVISION_REQUEST);
451 } else {
452 startTethering();
453 }
454 }
455
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700456 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
457 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700458 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700459 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700460 startTethering();
461 } else {
462 //BT and USB need checkbox turned off on failure
463 //Wifi tethering is never turned on until afterwards
464 switch (mTetherChoice) {
465 case BLUETOOTH_TETHERING:
466 mBluetoothTether.setChecked(false);
467 break;
468 case USB_TETHERING:
469 mUsbTether.setChecked(false);
470 break;
471 }
472 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700473 }
474 }
475 }
476
Irfan Sheriff01b32362011-11-04 12:08:56 -0700477 private void startTethering() {
478 switch (mTetherChoice) {
479 case WIFI_TETHERING:
480 mWifiApEnabler.setSoftapEnabled(true);
481 break;
482 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700483 // turn on Bluetooth first
484 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
485 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800486 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700487 adapter.enable();
488 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
489 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800490 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700491 BluetoothPan bluetoothPan = mBluetoothPan.get();
492 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800493 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700494 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700495 break;
496 case USB_TETHERING:
497 setUsbTethering(true);
498 break;
499 default:
500 //should not happen
501 break;
502 }
503 }
504
505 private void setUsbTethering(boolean enabled) {
506 ConnectivityManager cm =
507 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800508 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700509 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700510 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
511 return;
512 }
513 mUsbTether.setSummary("");
514 }
515
516 @Override
517 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
518 ConnectivityManager cm =
519 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
520
521 if (preference == mUsbTether) {
522 boolean newState = mUsbTether.isChecked();
523
524 if (newState) {
525 startProvisioningIfNecessary(USB_TETHERING);
526 } else {
527 setUsbTethering(newState);
528 }
529 } else if (preference == mBluetoothTether) {
530 boolean bluetoothTetherState = mBluetoothTether.isChecked();
531
532 if (bluetoothTetherState) {
533 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700534 } else {
535 boolean errored = false;
536
Danica Chang32711b62010-08-10 18:41:29 -0700537 String [] tethered = cm.getTetheredIfaces();
538 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
539 if (bluetoothIface != null &&
540 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
541 errored = true;
542 }
543
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700544 BluetoothPan bluetoothPan = mBluetoothPan.get();
545 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700546 if (errored) {
547 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
548 } else {
549 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
550 }
551 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800552 } else if (preference == mCreateNetwork) {
553 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800554 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700555
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700556 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800557 }
558
Jake Hamby436b29e2011-02-07 18:21:25 -0800559 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800560 for (String iface : ifaces) {
561 for (String regex : regexes) {
562 if (iface.matches(regex)) {
563 return iface;
564 }
565 }
566 }
567 return null;
568 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800569
570 public void onClick(DialogInterface dialogInterface, int button) {
571 if (button == DialogInterface.BUTTON_POSITIVE) {
572 mWifiConfig = mDialog.getConfig();
573 if (mWifiConfig != null) {
574 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700575 * if soft AP is stopped, bring up
576 * else restart with new config
577 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800578 */
579 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700580 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800581 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800582 } else {
583 mWifiManager.setWifiApConfiguration(mWifiConfig);
584 }
585 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
586 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
587 mWifiConfig.SSID,
588 mSecurityType[index]));
589 }
590 }
591 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700592
593 @Override
594 public int getHelpResource() {
595 return R.string.help_url_tether;
596 }
Amith Yamasani394eaa22013-06-11 11:04:44 -0700597
598 /**
599 * Checks whether this screen will have anything to show on this device. This is called by
600 * the shortcut picker for Settings shortcuts (home screen widget).
601 * @param context a context object for getting a system service.
602 * @return whether Tether & portable hotspot should be shown in the shortcuts picker.
603 */
604 public static boolean showInShortcuts(Context context) {
605 final ConnectivityManager cm =
606 (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
607 final boolean isSecondaryUser = UserHandle.myUserId() != UserHandle.USER_OWNER;
608 return !isSecondaryUser && cm.isTetheringSupported();
609 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800610}