blob: 107b53a94f1c3a92b64ea6e9d5339234aeb00533 [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 Greenwalt209177a2010-03-04 13:29:02 -080040import android.preference.CheckBoxPreference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080041import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080042import android.preference.PreferenceScreen;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070043import android.text.TextUtils;
Amith Yamasani476d7952011-01-21 09:28:31 -080044import android.view.ViewGroup;
45import android.view.ViewParent;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070046import android.webkit.WebView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080047
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070048import java.io.InputStream;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080049import java.util.ArrayList;
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070050import java.util.Locale;
Irfan Sheriff65cff172010-02-08 10:46:30 -080051
Robert Greenwalt3901edb2010-01-26 10:22:37 -080052/*
53 * Displays preferences for Tethering.
54 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080055public class TetherSettings extends SettingsPreferenceFragment
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070056 implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
Danica Chang32711b62010-08-10 18:41:29 -070057
Robert Greenwalt3901edb2010-01-26 10:22:37 -080058 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080059 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070060 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070061 private static final String TETHERING_HELP = "tethering_help";
Robert Greenwalt38925c02010-03-18 15:31:48 -070062 private static final String USB_HELP_MODIFIER = "usb_";
63 private static final String WIFI_HELP_MODIFIER = "wifi_";
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -070064 private static final String HELP_URL = "file:///android_asset/html/%y%z/tethering_%xhelp.html";
65 private static final String HELP_PATH = "html/%y%z/tethering_help.html";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080066
Irfan Sheriff0b266962010-04-06 15:16:44 -070067 private static final int DIALOG_TETHER_HELP = 1;
Amith Yamasani84a042c2011-03-02 11:25:04 -080068 private static final int DIALOG_AP_SETTINGS = 2;
Irfan Sheriff0b266962010-04-06 15:16:44 -070069
70 private WebView mView;
Robert Greenwalt209177a2010-03-04 13:29:02 -080071 private CheckBoxPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080072
Irfan Sheriff65cff172010-02-08 10:46:30 -080073 private WifiApEnabler mWifiApEnabler;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070074 private CheckBoxPreference mEnableWifiAp;
75 private static final int MHS_REQUEST = 0;
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
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800105 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700106 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800107 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800108 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800109
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700110 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800111 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
112 if (adapter != null) {
113 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
114 BluetoothProfile.PAN);
115 }
116
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700117 mEnableWifiAp =
Jake Hamby436b29e2011-02-07 18:21:25 -0800118 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700119 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800120 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700121 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc3c93e52010-03-15 17:21:16 -0700122 mTetherHelp = (PreferenceScreen) findPreference(TETHERING_HELP);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800123
124 ConnectivityManager cm =
125 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800126
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800127 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800128 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700129 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
130
Amith Yamasanie419bc12011-02-14 14:19:08 -0800131 final boolean usbAvailable = mUsbRegexs.length != 0;
132 final boolean wifiAvailable = mWifiRegexs.length != 0;
133 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700134
Danica Chang32711b62010-08-10 18:41:29 -0700135 if (!usbAvailable || Utils.isMonkeyRunning()) {
136 getPreferenceScreen().removePreference(mUsbTether);
137 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800138
Amith Yamasani0f474652011-08-30 17:30:01 -0700139 if (wifiAvailable) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700140 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700141 initWifiTethering();
142 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700143 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800144 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700145 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800146
Danica Chang32711b62010-08-10 18:41:29 -0700147 if (!bluetoothAvailable) {
148 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700149 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800150 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700151 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700152 } else {
153 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700154 }
155 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800156
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700157 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800158 }
159
160 private void initWifiTethering() {
161 final Activity activity = getActivity();
162 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
163 mWifiConfig = mWifiManager.getWifiApConfiguration();
164 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
165
166 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800167
168 if (mWifiConfig == null) {
169 final String s = activity.getString(
170 com.android.internal.R.string.wifi_tether_configure_ssid_default);
171 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
172 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
173 } else {
174 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
175 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
176 mWifiConfig.SSID,
177 mSecurityType[index]));
178 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800179 }
180
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800181 private BluetoothProfile.ServiceListener mProfileServiceListener =
182 new BluetoothProfile.ServiceListener() {
183 public void onServiceConnected(int profile, BluetoothProfile proxy) {
184 mBluetoothPan = (BluetoothPan) proxy;
185 }
186 public void onServiceDisconnected(int profile) {
187 mBluetoothPan = null;
188 }
189 };
190
Irfan Sheriff0b266962010-04-06 15:16:44 -0700191 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700192 public Dialog onCreateDialog(int id) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700193 if (id == DIALOG_TETHER_HELP) {
194 Locale locale = Locale.getDefault();
Irfan Sheriff0b266962010-04-06 15:16:44 -0700195
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700196 // check for the full language + country resource, if not there, try just language
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700197 final AssetManager am = getActivity().getAssets();
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700198 String path = HELP_PATH.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800199 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700200 boolean useCountry = true;
201 InputStream is = null;
202 try {
203 is = am.open(path);
Jake Hamby436b29e2011-02-07 18:21:25 -0800204 } catch (Exception ignored) {
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700205 useCountry = false;
206 } finally {
207 if (is != null) {
208 try {
209 is.close();
Jake Hamby436b29e2011-02-07 18:21:25 -0800210 } catch (Exception ignored) {}
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700211 }
212 }
213 String url = HELP_URL.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800214 url = url.replace("%z", useCountry ? '_'+locale.getCountry().toLowerCase() : "");
Irfan Sheriff0b266962010-04-06 15:16:44 -0700215 if ((mUsbRegexs.length != 0) && (mWifiRegexs.length == 0)) {
216 url = url.replace("%x", USB_HELP_MODIFIER);
217 } else if ((mWifiRegexs.length != 0) && (mUsbRegexs.length == 0)) {
218 url = url.replace("%x", WIFI_HELP_MODIFIER);
219 } else {
220 // could assert that both wifi and usb have regexs, but the default
221 // is to use this anyway so no check is needed
222 url = url.replace("%x", "");
223 }
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700224
Irfan Sheriff0b266962010-04-06 15:16:44 -0700225 mView.loadUrl(url);
Amith Yamasani476d7952011-01-21 09:28:31 -0800226 // Detach from old parent first
227 ViewParent parent = mView.getParent();
228 if (parent != null && parent instanceof ViewGroup) {
229 ((ViewGroup) parent).removeView(mView);
230 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700231 return new AlertDialog.Builder(getActivity())
Irfan Sheriff0b266962010-04-06 15:16:44 -0700232 .setCancelable(true)
233 .setTitle(R.string.tethering_help_button_text)
234 .setView(mView)
235 .create();
Amith Yamasani84a042c2011-03-02 11:25:04 -0800236 } else if (id == DIALOG_AP_SETTINGS) {
237 final Activity activity = getActivity();
238 mDialog = new WifiApDialog(activity, this, mWifiConfig);
239 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700240 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800241
Irfan Sheriff0b266962010-04-06 15:16:44 -0700242 return null;
243 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800244
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800245 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700246 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800247 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800248 String action = intent.getAction();
249 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800250 // TODO - this should understand the interface types
251 ArrayList<String> available = intent.getStringArrayListExtra(
252 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
253 ArrayList<String> active = intent.getStringArrayListExtra(
254 ConnectivityManager.EXTRA_ACTIVE_TETHER);
255 ArrayList<String> errored = intent.getStringArrayListExtra(
256 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700257 updateState(available.toArray(new String[available.size()]),
258 active.toArray(new String[active.size()]),
259 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700260 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
261 mMassStorageActive = true;
262 updateState();
263 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
264 mMassStorageActive = false;
265 updateState();
266 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
267 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800268 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800269 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
270 if (mBluetoothEnableForTether) {
271 switch (intent
272 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
273 case BluetoothAdapter.STATE_ON:
274 mBluetoothPan.setBluetoothTethering(true);
275 mBluetoothEnableForTether = false;
276 break;
277
278 case BluetoothAdapter.STATE_OFF:
279 case BluetoothAdapter.ERROR:
280 mBluetoothEnableForTether = false;
281 break;
282
283 default:
284 // ignore transition states
285 }
286 }
Danica Chang32711b62010-08-10 18:41:29 -0700287 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800288 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800289 }
290 }
291
292 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700293 public void onStart() {
294 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800295
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700296 final Activity activity = getActivity();
297
Mike Lockwood69a09572011-07-19 13:30:03 -0700298 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800299 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700300 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700301 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800302
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800303 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700304 filter.addAction(UsbManager.ACTION_USB_STATE);
305 activity.registerReceiver(mTetherChangeReceiver, filter);
306
307 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800308 filter.addAction(Intent.ACTION_MEDIA_SHARED);
309 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
310 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700311 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800312
Danica Chang32711b62010-08-10 18:41:29 -0700313 filter = new IntentFilter();
314 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700315 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700316
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700317 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700318 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700319 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700320 mWifiApEnabler.resume();
321 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800322
323 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800324 }
325
326 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700327 public void onStop() {
328 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700329 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800330 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700331 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700332 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700333 mWifiApEnabler.pause();
334 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800335 }
336
Robert Greenwalt209177a2010-03-04 13:29:02 -0800337 private void updateState() {
338 ConnectivityManager cm =
339 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
340
341 String[] available = cm.getTetherableIfaces();
342 String[] tethered = cm.getTetheredIfaces();
343 String[] errored = cm.getTetheringErroredIfaces();
344 updateState(available, tethered, errored);
345 }
346
Ben Clark0008d212010-07-27 16:20:59 +0100347 private void updateState(String[] available, String[] tethered,
348 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700349 updateUsbState(available, tethered, errored);
350 updateBluetoothState(available, tethered, errored);
351 }
352
353
354 private void updateUsbState(String[] available, String[] tethered,
355 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800356 ConnectivityManager cm =
357 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700358 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800359 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100360 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800361 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800362 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800363 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
364 usbError = cm.getLastTetherError(s);
365 }
366 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800367 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800368 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800369 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100370 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800371 for (String regex : mUsbRegexs) {
372 if (s.matches(regex)) usbTethered = true;
373 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800374 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800375 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100376 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800377 for (String regex : mUsbRegexs) {
378 if (s.matches(regex)) usbErrored = true;
379 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800380 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800381
382 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800383 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
384 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800385 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800386 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800387 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
388 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
389 } else {
390 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
391 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800392 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800393 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800394 } else if (usbErrored) {
395 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
396 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800397 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700398 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800399 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
400 mUsbTether.setEnabled(false);
401 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800402 } else {
403 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
404 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800405 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800406 }
407 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800408
Danica Chang32711b62010-08-10 18:41:29 -0700409 private void updateBluetoothState(String[] available, String[] tethered,
410 String[] errored) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800411 int bluetoothTethered = 0;
Danica Chang32711b62010-08-10 18:41:29 -0700412 for (String s : tethered) {
413 for (String regex : mBluetoothRegexs) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800414 if (s.matches(regex)) bluetoothTethered++;
Danica Chang32711b62010-08-10 18:41:29 -0700415 }
416 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800417 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700418 for (String s: errored) {
419 for (String regex : mBluetoothRegexs) {
420 if (s.matches(regex)) bluetoothErrored = true;
421 }
422 }
423
424 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
425 int btState = adapter.getState();
426 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
427 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700428 mBluetoothTether.setSummary(R.string.wifi_stopping);
429 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
430 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700431 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Jake Hambyc777ee22011-03-04 15:37:39 -0800432 } else if (btState == BluetoothAdapter.STATE_ON && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700433 mBluetoothTether.setChecked(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800434 mBluetoothTether.setEnabled(true);
435 if (bluetoothTethered > 1) {
436 String summary = getString(
437 R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
438 mBluetoothTether.setSummary(summary);
439 } else if (bluetoothTethered == 1) {
440 mBluetoothTether.setSummary(R.string.bluetooth_tethering_device_connected_subtext);
441 } else if (bluetoothErrored) {
442 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
443 } else {
444 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700445 }
446 } else {
447 mBluetoothTether.setEnabled(true);
448 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700449 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
450 }
451 }
452
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700453 public boolean onPreferenceChange(Preference preference, Object value) {
454 boolean enable = (Boolean) value;
455
456 if (enable) {
457 //Check if provisioning is needed
458 String intentStr = getActivity().getString(
459 com.android.internal.R.string.config_mobile_hotspot_provision_intent);
460
461 if (TextUtils.isEmpty(intentStr)) {
462 mWifiApEnabler.setSoftapEnabled(true);
463 } else {
464 Intent intent = new Intent(intentStr);
465 startActivityForResult(intent, MHS_REQUEST);
466 }
467 } else {
468 mWifiApEnabler.setSoftapEnabled(false);
469 }
470 return false;
471 }
472
473 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
474 super.onActivityResult(requestCode, resultCode, intent);
475 if (requestCode == MHS_REQUEST) {
476 if (resultCode == Activity.RESULT_OK) {
477 mWifiApEnabler.setSoftapEnabled(true);
478 }
479 }
480 }
481
Robert Greenwalt209177a2010-03-04 13:29:02 -0800482 @Override
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700483 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
Mike Lockwood69a09572011-07-19 13:30:03 -0700484 ConnectivityManager cm =
485 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
486
Robert Greenwalt209177a2010-03-04 13:29:02 -0800487 if (preference == mUsbTether) {
488 boolean newState = mUsbTether.isChecked();
489
Mike Lockwood69a09572011-07-19 13:30:03 -0700490 if (cm.setUsbTethering(newState) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
491 mUsbTether.setChecked(false);
492 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
493 return true;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800494 }
Mike Lockwood69a09572011-07-19 13:30:03 -0700495 mUsbTether.setSummary("");
Jake Hambyc777ee22011-03-04 15:37:39 -0800496 } else if (preference == mBluetoothTether) {
Danica Chang32711b62010-08-10 18:41:29 -0700497 boolean bluetoothTetherState = mBluetoothTether.isChecked();
Robert Greenwalt38925c02010-03-18 15:31:48 -0700498
Danica Chang32711b62010-08-10 18:41:29 -0700499 if (bluetoothTetherState) {
500 // turn on Bluetooth first
501 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
502 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800503 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700504 adapter.enable();
505 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
506 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800507 } else {
508 mBluetoothPan.setBluetoothTethering(true);
509 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700510 }
Danica Chang32711b62010-08-10 18:41:29 -0700511 } else {
512 boolean errored = false;
513
Danica Chang32711b62010-08-10 18:41:29 -0700514 String [] tethered = cm.getTetheredIfaces();
515 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
516 if (bluetoothIface != null &&
517 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
518 errored = true;
519 }
520
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700521 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700522 if (errored) {
523 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
524 } else {
525 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
526 }
527 }
528 } else if (preference == mTetherHelp) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700529 showDialog(DIALOG_TETHER_HELP);
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700530 return true;
Amith Yamasani84a042c2011-03-02 11:25:04 -0800531 } else if (preference == mCreateNetwork) {
532 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800533 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700534
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700535 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800536 }
537
Jake Hamby436b29e2011-02-07 18:21:25 -0800538 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800539 for (String iface : ifaces) {
540 for (String regex : regexes) {
541 if (iface.matches(regex)) {
542 return iface;
543 }
544 }
545 }
546 return null;
547 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800548
549 public void onClick(DialogInterface dialogInterface, int button) {
550 if (button == DialogInterface.BUTTON_POSITIVE) {
551 mWifiConfig = mDialog.getConfig();
552 if (mWifiConfig != null) {
553 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700554 * if soft AP is stopped, bring up
555 * else restart with new config
556 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800557 */
558 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700559 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800560 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800561 } else {
562 mWifiManager.setWifiApConfiguration(mWifiConfig);
563 }
564 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
565 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
566 mWifiConfig.SSID,
567 mSecurityType[index]));
568 }
569 }
570 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800571}