blob: d6f6ab62438f81b939fd0298aaa08275a0a01ab5 [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;
Robert Greenwalt209177a2010-03-04 13:29:02 -080041import android.preference.CheckBoxPreference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080042import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080043import android.preference.PreferenceScreen;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070044import android.text.TextUtils;
Amith Yamasani476d7952011-01-21 09:28:31 -080045import android.view.ViewGroup;
46import android.view.ViewParent;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070047import android.webkit.WebView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080048
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070049import java.io.InputStream;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080050import java.util.ArrayList;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070051import java.util.Locale;
Irfan Sheriff65cff172010-02-08 10:46:30 -080052
Robert Greenwalt3901edb2010-01-26 10:22:37 -080053/*
54 * Displays preferences for Tethering.
55 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080056public class TetherSettings extends SettingsPreferenceFragment
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070057 implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
Danica Chang32711b62010-08-10 18:41:29 -070058
Robert Greenwalt3901edb2010-01-26 10:22:37 -080059 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080060 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070061 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070062 private static final String TETHERING_HELP = "tethering_help";
Robert Greenwalt38925c02010-03-18 15:31:48 -070063 private static final String USB_HELP_MODIFIER = "usb_";
64 private static final String WIFI_HELP_MODIFIER = "wifi_";
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070065 private static final String HELP_URL = "file:///android_asset/html/%y%z/tethering_%xhelp.html";
66 private static final String HELP_PATH = "html/%y%z/tethering_help.html";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080067
Irfan Sheriff0b266962010-04-06 15:16:44 -070068 private static final int DIALOG_TETHER_HELP = 1;
Amith Yamasani84a042c2011-03-02 11:25:04 -080069 private static final int DIALOG_AP_SETTINGS = 2;
Irfan Sheriff0b266962010-04-06 15:16:44 -070070
71 private WebView mView;
Robert Greenwalt209177a2010-03-04 13:29:02 -080072 private CheckBoxPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080073
Irfan Sheriff65cff172010-02-08 10:46:30 -080074 private WifiApEnabler mWifiApEnabler;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070075 private CheckBoxPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070076
77 private CheckBoxPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070078
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070079 private PreferenceScreen mTetherHelp;
Irfan Sheriff65cff172010-02-08 10:46:30 -080080
Robert Greenwalt3901edb2010-01-26 10:22:37 -080081 private BroadcastReceiver mTetherChangeReceiver;
82
Robert Greenwaltc4764d22010-02-12 14:21:37 -080083 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080084
85 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080086
Danica Chang32711b62010-08-10 18:41:29 -070087 private String[] mBluetoothRegexs;
88 private BluetoothPan mBluetoothPan;
89
Amith Yamasani84a042c2011-03-02 11:25:04 -080090 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
91 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
92
93 private String[] mSecurityType;
94 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080095
96 private WifiApDialog mDialog;
97 private WifiManager mWifiManager;
98 private WifiConfiguration mWifiConfig = null;
99
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
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800117 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700118 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800119 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800120 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800121
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700122 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800123 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
124 if (adapter != null) {
125 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
126 BluetoothProfile.PAN);
127 }
128
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700129 mEnableWifiAp =
Jake Hamby436b29e2011-02-07 18:21:25 -0800130 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700131 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800132 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700133 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc3c93e52010-03-15 17:21:16 -0700134 mTetherHelp = (PreferenceScreen) findPreference(TETHERING_HELP);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800135
136 ConnectivityManager cm =
137 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800138
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800139 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800140 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700141 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
142
Amith Yamasanie419bc12011-02-14 14:19:08 -0800143 final boolean usbAvailable = mUsbRegexs.length != 0;
144 final boolean wifiAvailable = mWifiRegexs.length != 0;
145 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700146
Danica Chang32711b62010-08-10 18:41:29 -0700147 if (!usbAvailable || Utils.isMonkeyRunning()) {
148 getPreferenceScreen().removePreference(mUsbTether);
149 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800150
Amith Yamasani0f474652011-08-30 17:30:01 -0700151 if (wifiAvailable) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700152 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700153 initWifiTethering();
154 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700155 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800156 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700157 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800158
Danica Chang32711b62010-08-10 18:41:29 -0700159 if (!bluetoothAvailable) {
160 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700161 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800162 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700163 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700164 } else {
165 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700166 }
167 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800168
Irfan Sheriff01b32362011-11-04 12:08:56 -0700169 mProvisionApp = getResources().getStringArray(
170 com.android.internal.R.array.config_mobile_hotspot_provision_app);
171
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700172 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800173 }
174
175 private void initWifiTethering() {
176 final Activity activity = getActivity();
177 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
178 mWifiConfig = mWifiManager.getWifiApConfiguration();
179 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
180
181 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800182
183 if (mWifiConfig == null) {
184 final String s = activity.getString(
185 com.android.internal.R.string.wifi_tether_configure_ssid_default);
186 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
187 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
188 } else {
189 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
190 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
191 mWifiConfig.SSID,
192 mSecurityType[index]));
193 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800194 }
195
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800196 private BluetoothProfile.ServiceListener mProfileServiceListener =
197 new BluetoothProfile.ServiceListener() {
198 public void onServiceConnected(int profile, BluetoothProfile proxy) {
199 mBluetoothPan = (BluetoothPan) proxy;
200 }
201 public void onServiceDisconnected(int profile) {
202 mBluetoothPan = null;
203 }
204 };
205
Irfan Sheriff0b266962010-04-06 15:16:44 -0700206 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700207 public Dialog onCreateDialog(int id) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700208 if (id == DIALOG_TETHER_HELP) {
209 Locale locale = Locale.getDefault();
Irfan Sheriff0b266962010-04-06 15:16:44 -0700210
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700211 // check for the full language + country resource, if not there, try just language
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700212 final AssetManager am = getActivity().getAssets();
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700213 String path = HELP_PATH.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800214 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700215 boolean useCountry = true;
216 InputStream is = null;
217 try {
218 is = am.open(path);
Jake Hamby436b29e2011-02-07 18:21:25 -0800219 } catch (Exception ignored) {
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700220 useCountry = false;
221 } finally {
222 if (is != null) {
223 try {
224 is.close();
Jake Hamby436b29e2011-02-07 18:21:25 -0800225 } catch (Exception ignored) {}
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700226 }
227 }
228 String url = HELP_URL.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800229 url = url.replace("%z", useCountry ? '_'+locale.getCountry().toLowerCase() : "");
Irfan Sheriff0b266962010-04-06 15:16:44 -0700230 if ((mUsbRegexs.length != 0) && (mWifiRegexs.length == 0)) {
231 url = url.replace("%x", USB_HELP_MODIFIER);
232 } else if ((mWifiRegexs.length != 0) && (mUsbRegexs.length == 0)) {
233 url = url.replace("%x", WIFI_HELP_MODIFIER);
234 } else {
235 // could assert that both wifi and usb have regexs, but the default
236 // is to use this anyway so no check is needed
237 url = url.replace("%x", "");
238 }
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700239
Irfan Sheriff0b266962010-04-06 15:16:44 -0700240 mView.loadUrl(url);
Amith Yamasani476d7952011-01-21 09:28:31 -0800241 // Detach from old parent first
242 ViewParent parent = mView.getParent();
243 if (parent != null && parent instanceof ViewGroup) {
244 ((ViewGroup) parent).removeView(mView);
245 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700246 return new AlertDialog.Builder(getActivity())
Irfan Sheriff0b266962010-04-06 15:16:44 -0700247 .setCancelable(true)
248 .setTitle(R.string.tethering_help_button_text)
249 .setView(mView)
250 .create();
Amith Yamasani84a042c2011-03-02 11:25:04 -0800251 } else if (id == DIALOG_AP_SETTINGS) {
252 final Activity activity = getActivity();
253 mDialog = new WifiApDialog(activity, this, mWifiConfig);
254 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700255 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800256
Irfan Sheriff0b266962010-04-06 15:16:44 -0700257 return null;
258 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800259
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800260 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700261 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800262 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800263 String action = intent.getAction();
264 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800265 // TODO - this should understand the interface types
266 ArrayList<String> available = intent.getStringArrayListExtra(
267 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
268 ArrayList<String> active = intent.getStringArrayListExtra(
269 ConnectivityManager.EXTRA_ACTIVE_TETHER);
270 ArrayList<String> errored = intent.getStringArrayListExtra(
271 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700272 updateState(available.toArray(new String[available.size()]),
273 active.toArray(new String[active.size()]),
274 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700275 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
276 mMassStorageActive = true;
277 updateState();
278 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
279 mMassStorageActive = false;
280 updateState();
281 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
282 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800283 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800284 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
285 if (mBluetoothEnableForTether) {
286 switch (intent
287 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
288 case BluetoothAdapter.STATE_ON:
289 mBluetoothPan.setBluetoothTethering(true);
290 mBluetoothEnableForTether = false;
291 break;
292
293 case BluetoothAdapter.STATE_OFF:
294 case BluetoothAdapter.ERROR:
295 mBluetoothEnableForTether = false;
296 break;
297
298 default:
299 // ignore transition states
300 }
301 }
Danica Chang32711b62010-08-10 18:41:29 -0700302 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800303 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800304 }
305 }
306
307 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700308 public void onStart() {
309 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800310
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700311 final Activity activity = getActivity();
312
Mike Lockwood69a09572011-07-19 13:30:03 -0700313 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800314 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700315 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700316 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800317
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800318 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700319 filter.addAction(UsbManager.ACTION_USB_STATE);
320 activity.registerReceiver(mTetherChangeReceiver, filter);
321
322 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800323 filter.addAction(Intent.ACTION_MEDIA_SHARED);
324 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
325 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700326 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800327
Danica Chang32711b62010-08-10 18:41:29 -0700328 filter = new IntentFilter();
329 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700330 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700331
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700332 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700333 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700334 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700335 mWifiApEnabler.resume();
336 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800337
338 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800339 }
340
341 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700342 public void onStop() {
343 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700344 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800345 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700346 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700347 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700348 mWifiApEnabler.pause();
349 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800350 }
351
Robert Greenwalt209177a2010-03-04 13:29:02 -0800352 private void updateState() {
353 ConnectivityManager cm =
354 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
355
356 String[] available = cm.getTetherableIfaces();
357 String[] tethered = cm.getTetheredIfaces();
358 String[] errored = cm.getTetheringErroredIfaces();
359 updateState(available, tethered, errored);
360 }
361
Ben Clark0008d212010-07-27 16:20:59 +0100362 private void updateState(String[] available, String[] tethered,
363 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700364 updateUsbState(available, tethered, errored);
365 updateBluetoothState(available, tethered, errored);
366 }
367
368
369 private void updateUsbState(String[] available, String[] tethered,
370 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800371 ConnectivityManager cm =
372 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700373 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800374 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100375 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800376 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800377 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800378 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
379 usbError = cm.getLastTetherError(s);
380 }
381 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800382 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800383 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800384 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100385 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800386 for (String regex : mUsbRegexs) {
387 if (s.matches(regex)) usbTethered = true;
388 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800389 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800390 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100391 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800392 for (String regex : mUsbRegexs) {
393 if (s.matches(regex)) usbErrored = true;
394 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800395 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800396
397 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800398 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
399 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800400 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800401 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800402 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
403 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
404 } else {
405 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
406 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800407 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800408 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800409 } else if (usbErrored) {
410 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
411 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800412 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700413 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800414 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
415 mUsbTether.setEnabled(false);
416 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800417 } else {
418 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
419 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800420 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800421 }
422 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800423
Danica Chang32711b62010-08-10 18:41:29 -0700424 private void updateBluetoothState(String[] available, String[] tethered,
425 String[] errored) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800426 int bluetoothTethered = 0;
Danica Chang32711b62010-08-10 18:41:29 -0700427 for (String s : tethered) {
428 for (String regex : mBluetoothRegexs) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800429 if (s.matches(regex)) bluetoothTethered++;
Danica Chang32711b62010-08-10 18:41:29 -0700430 }
431 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800432 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700433 for (String s: errored) {
434 for (String regex : mBluetoothRegexs) {
435 if (s.matches(regex)) bluetoothErrored = true;
436 }
437 }
438
439 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
440 int btState = adapter.getState();
441 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
442 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700443 mBluetoothTether.setSummary(R.string.wifi_stopping);
444 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
445 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700446 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Jake Hambyc777ee22011-03-04 15:37:39 -0800447 } else if (btState == BluetoothAdapter.STATE_ON && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700448 mBluetoothTether.setChecked(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800449 mBluetoothTether.setEnabled(true);
450 if (bluetoothTethered > 1) {
451 String summary = getString(
452 R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
453 mBluetoothTether.setSummary(summary);
454 } else if (bluetoothTethered == 1) {
455 mBluetoothTether.setSummary(R.string.bluetooth_tethering_device_connected_subtext);
456 } else if (bluetoothErrored) {
457 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
458 } else {
459 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700460 }
461 } else {
462 mBluetoothTether.setEnabled(true);
463 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700464 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
465 }
466 }
467
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700468 public boolean onPreferenceChange(Preference preference, Object value) {
469 boolean enable = (Boolean) value;
470
471 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700472 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700473 } else {
474 mWifiApEnabler.setSoftapEnabled(false);
475 }
476 return false;
477 }
478
Irfan Sheriff01b32362011-11-04 12:08:56 -0700479 boolean isProvisioningNeeded() {
Robert Greenwalt505766c2011-11-09 15:03:14 -0800480 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)) {
481 return false;
482 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700483 return mProvisionApp.length == 2;
484 }
485
486 private void startProvisioningIfNecessary(int choice) {
487 mTetherChoice = choice;
488 if (isProvisioningNeeded()) {
489 Intent intent = new Intent(Intent.ACTION_MAIN);
490 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
491 startActivityForResult(intent, PROVISION_REQUEST);
492 } else {
493 startTethering();
494 }
495 }
496
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700497 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
498 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700499 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700500 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700501 startTethering();
502 } else {
503 //BT and USB need checkbox turned off on failure
504 //Wifi tethering is never turned on until afterwards
505 switch (mTetherChoice) {
506 case BLUETOOTH_TETHERING:
507 mBluetoothTether.setChecked(false);
508 break;
509 case USB_TETHERING:
510 mUsbTether.setChecked(false);
511 break;
512 }
513 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700514 }
515 }
516 }
517
Irfan Sheriff01b32362011-11-04 12:08:56 -0700518 private void startTethering() {
519 switch (mTetherChoice) {
520 case WIFI_TETHERING:
521 mWifiApEnabler.setSoftapEnabled(true);
522 break;
523 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700524 // turn on Bluetooth first
525 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
526 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800527 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700528 adapter.enable();
529 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
530 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800531 } else {
532 mBluetoothPan.setBluetoothTethering(true);
533 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700534 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700535 break;
536 case USB_TETHERING:
537 setUsbTethering(true);
538 break;
539 default:
540 //should not happen
541 break;
542 }
543 }
544
545 private void setUsbTethering(boolean enabled) {
546 ConnectivityManager cm =
547 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
548 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
549 mUsbTether.setChecked(false);
550 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
551 return;
552 }
553 mUsbTether.setSummary("");
554 }
555
556 @Override
557 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
558 ConnectivityManager cm =
559 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
560
561 if (preference == mUsbTether) {
562 boolean newState = mUsbTether.isChecked();
563
564 if (newState) {
565 startProvisioningIfNecessary(USB_TETHERING);
566 } else {
567 setUsbTethering(newState);
568 }
569 } else if (preference == mBluetoothTether) {
570 boolean bluetoothTetherState = mBluetoothTether.isChecked();
571
572 if (bluetoothTetherState) {
573 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700574 } else {
575 boolean errored = false;
576
Danica Chang32711b62010-08-10 18:41:29 -0700577 String [] tethered = cm.getTetheredIfaces();
578 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
579 if (bluetoothIface != null &&
580 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
581 errored = true;
582 }
583
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700584 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700585 if (errored) {
586 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
587 } else {
588 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
589 }
590 }
591 } else if (preference == mTetherHelp) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700592 showDialog(DIALOG_TETHER_HELP);
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700593 return true;
Amith Yamasani84a042c2011-03-02 11:25:04 -0800594 } else if (preference == mCreateNetwork) {
595 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800596 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700597
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700598 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800599 }
600
Jake Hamby436b29e2011-02-07 18:21:25 -0800601 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800602 for (String iface : ifaces) {
603 for (String regex : regexes) {
604 if (iface.matches(regex)) {
605 return iface;
606 }
607 }
608 }
609 return null;
610 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800611
612 public void onClick(DialogInterface dialogInterface, int button) {
613 if (button == DialogInterface.BUTTON_POSITIVE) {
614 mWifiConfig = mDialog.getConfig();
615 if (mWifiConfig != null) {
616 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700617 * if soft AP is stopped, bring up
618 * else restart with new config
619 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800620 */
621 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700622 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800623 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800624 } else {
625 mWifiManager.setWifiApConfiguration(mWifiConfig);
626 }
627 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
628 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
629 mWifiConfig.SSID,
630 mSecurityType[index]));
631 }
632 }
633 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800634}