blob: f0b6b44e0358075561b6cda146bc9bb653b811ee [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();
390 int btState = adapter.getState();
391 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
392 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500393 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700394 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
395 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700396 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700397 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700398 BluetoothPan bluetoothPan = mBluetoothPan.get();
399 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
400 bluetoothPan.isTetheringOn()) {
401 mBluetoothTether.setChecked(true);
402 mBluetoothTether.setEnabled(true);
403 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
404 if (bluetoothTethered > 1) {
405 String summary = getString(
406 R.string.bluetooth_tethering_devices_connected_subtext,
407 bluetoothTethered);
408 mBluetoothTether.setSummary(summary);
409 } else if (bluetoothTethered == 1) {
410 mBluetoothTether.setSummary(
411 R.string.bluetooth_tethering_device_connected_subtext);
412 } else if (bluetoothErrored) {
413 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
414 } else {
415 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
416 }
417 } else {
418 mBluetoothTether.setEnabled(true);
419 mBluetoothTether.setChecked(false);
420 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
421 }
Danica Chang32711b62010-08-10 18:41:29 -0700422 }
423 }
424
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700425 public boolean onPreferenceChange(Preference preference, Object value) {
426 boolean enable = (Boolean) value;
427
428 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700429 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700430 } else {
431 mWifiApEnabler.setSoftapEnabled(false);
432 }
433 return false;
434 }
435
Irfan Sheriff01b32362011-11-04 12:08:56 -0700436 boolean isProvisioningNeeded() {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800437 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)) {
438 return false;
439 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700440 return mProvisionApp.length == 2;
441 }
442
443 private void startProvisioningIfNecessary(int choice) {
444 mTetherChoice = choice;
445 if (isProvisioningNeeded()) {
446 Intent intent = new Intent(Intent.ACTION_MAIN);
447 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
448 startActivityForResult(intent, PROVISION_REQUEST);
449 } else {
450 startTethering();
451 }
452 }
453
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700454 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
455 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700456 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700457 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700458 startTethering();
459 } else {
460 //BT and USB need checkbox turned off on failure
461 //Wifi tethering is never turned on until afterwards
462 switch (mTetherChoice) {
463 case BLUETOOTH_TETHERING:
464 mBluetoothTether.setChecked(false);
465 break;
466 case USB_TETHERING:
467 mUsbTether.setChecked(false);
468 break;
469 }
470 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700471 }
472 }
473 }
474
Irfan Sheriff01b32362011-11-04 12:08:56 -0700475 private void startTethering() {
476 switch (mTetherChoice) {
477 case WIFI_TETHERING:
478 mWifiApEnabler.setSoftapEnabled(true);
479 break;
480 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700481 // turn on Bluetooth first
482 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
483 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800484 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700485 adapter.enable();
486 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
487 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800488 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700489 BluetoothPan bluetoothPan = mBluetoothPan.get();
490 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800491 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700492 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700493 break;
494 case USB_TETHERING:
495 setUsbTethering(true);
496 break;
497 default:
498 //should not happen
499 break;
500 }
501 }
502
503 private void setUsbTethering(boolean enabled) {
504 ConnectivityManager cm =
505 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800506 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700507 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700508 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
509 return;
510 }
511 mUsbTether.setSummary("");
512 }
513
514 @Override
515 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
516 ConnectivityManager cm =
517 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
518
519 if (preference == mUsbTether) {
520 boolean newState = mUsbTether.isChecked();
521
522 if (newState) {
523 startProvisioningIfNecessary(USB_TETHERING);
524 } else {
525 setUsbTethering(newState);
526 }
527 } else if (preference == mBluetoothTether) {
528 boolean bluetoothTetherState = mBluetoothTether.isChecked();
529
530 if (bluetoothTetherState) {
531 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700532 } else {
533 boolean errored = false;
534
Danica Chang32711b62010-08-10 18:41:29 -0700535 String [] tethered = cm.getTetheredIfaces();
536 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
537 if (bluetoothIface != null &&
538 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
539 errored = true;
540 }
541
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700542 BluetoothPan bluetoothPan = mBluetoothPan.get();
543 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700544 if (errored) {
545 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
546 } else {
547 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
548 }
549 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800550 } else if (preference == mCreateNetwork) {
551 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800552 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700553
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700554 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800555 }
556
Jake Hamby436b29e2011-02-07 18:21:25 -0800557 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800558 for (String iface : ifaces) {
559 for (String regex : regexes) {
560 if (iface.matches(regex)) {
561 return iface;
562 }
563 }
564 }
565 return null;
566 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800567
568 public void onClick(DialogInterface dialogInterface, int button) {
569 if (button == DialogInterface.BUTTON_POSITIVE) {
570 mWifiConfig = mDialog.getConfig();
571 if (mWifiConfig != null) {
572 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700573 * if soft AP is stopped, bring up
574 * else restart with new config
575 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800576 */
577 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700578 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800579 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800580 } else {
581 mWifiManager.setWifiApConfiguration(mWifiConfig);
582 }
583 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
584 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
585 mWifiConfig.SSID,
586 mSecurityType[index]));
587 }
588 }
589 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700590
591 @Override
592 public int getHelpResource() {
593 return R.string.help_url_tether;
594 }
Amith Yamasani394eaa22013-06-11 11:04:44 -0700595
596 /**
597 * Checks whether this screen will have anything to show on this device. This is called by
598 * the shortcut picker for Settings shortcuts (home screen widget).
599 * @param context a context object for getting a system service.
600 * @return whether Tether & portable hotspot should be shown in the shortcuts picker.
601 */
602 public static boolean showInShortcuts(Context context) {
603 final ConnectivityManager cm =
604 (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
605 final boolean isSecondaryUser = UserHandle.myUserId() != UserHandle.USER_OWNER;
606 return !isSecondaryUser && cm.isTetheringSupported();
607 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800608}