blob: e27c96ee5f87327c87d241bcb8bbf36b31ee8a3d [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;
Danica Chang32711b62010-08-10 18:41:29 -070075
76 private CheckBoxPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070077
Robert Greenwaltc3c93e52010-03-15 17:21:16 -070078 private PreferenceScreen mTetherHelp;
Irfan Sheriff65cff172010-02-08 10:46:30 -080079
Robert Greenwalt3901edb2010-01-26 10:22:37 -080080 private BroadcastReceiver mTetherChangeReceiver;
81
Robert Greenwaltc4764d22010-02-12 14:21:37 -080082 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080083
84 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080085
Danica Chang32711b62010-08-10 18:41:29 -070086 private String[] mBluetoothRegexs;
87 private BluetoothPan mBluetoothPan;
88
Amith Yamasani84a042c2011-03-02 11:25:04 -080089 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
90 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
91
92 private String[] mSecurityType;
93 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080094
95 private WifiApDialog mDialog;
96 private WifiManager mWifiManager;
97 private WifiConfiguration mWifiConfig = null;
98
Mike Lockwood69a09572011-07-19 13:30:03 -070099 private boolean mUsbConnected;
100 private boolean mMassStorageActive;
101
Jake Hambyc777ee22011-03-04 15:37:39 -0800102 private boolean mBluetoothEnableForTether;
103
Irfan Sheriff01b32362011-11-04 12:08:56 -0700104 private static final int INVALID = -1;
105 private static final int WIFI_TETHERING = 0;
106 private static final int USB_TETHERING = 1;
107 private static final int BLUETOOTH_TETHERING = 2;
108
109 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
110 private int mTetherChoice = INVALID;
111
112 /* Stores the package name and the class name of the provisioning app */
113 private String[] mProvisionApp;
114 private static final int PROVISION_REQUEST = 0;
115
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800116 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700117 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800118 super.onCreate(icicle);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800119 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800120
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700121 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800122 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
123 if (adapter != null) {
124 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
125 BluetoothProfile.PAN);
126 }
127
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700128 mEnableWifiAp =
Jake Hamby436b29e2011-02-07 18:21:25 -0800129 (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700130 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800131 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700132 mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc3c93e52010-03-15 17:21:16 -0700133 mTetherHelp = (PreferenceScreen) findPreference(TETHERING_HELP);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800134
135 ConnectivityManager cm =
136 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800137
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800138 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800139 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700140 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
141
Amith Yamasanie419bc12011-02-14 14:19:08 -0800142 final boolean usbAvailable = mUsbRegexs.length != 0;
143 final boolean wifiAvailable = mWifiRegexs.length != 0;
144 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700145
Danica Chang32711b62010-08-10 18:41:29 -0700146 if (!usbAvailable || Utils.isMonkeyRunning()) {
147 getPreferenceScreen().removePreference(mUsbTether);
148 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800149
Amith Yamasani0f474652011-08-30 17:30:01 -0700150 if (wifiAvailable) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700151 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700152 initWifiTethering();
153 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700154 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800155 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700156 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800157
Danica Chang32711b62010-08-10 18:41:29 -0700158 if (!bluetoothAvailable) {
159 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700160 } else {
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800161 if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700162 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700163 } else {
164 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700165 }
166 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800167
Irfan Sheriff01b32362011-11-04 12:08:56 -0700168 mProvisionApp = getResources().getStringArray(
169 com.android.internal.R.array.config_mobile_hotspot_provision_app);
170
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700171 mView = new WebView(activity);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800172 }
173
174 private void initWifiTethering() {
175 final Activity activity = getActivity();
176 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
177 mWifiConfig = mWifiManager.getWifiApConfiguration();
178 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
179
180 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800181
182 if (mWifiConfig == null) {
183 final String s = activity.getString(
184 com.android.internal.R.string.wifi_tether_configure_ssid_default);
185 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
186 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
187 } else {
188 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
189 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
190 mWifiConfig.SSID,
191 mSecurityType[index]));
192 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800193 }
194
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800195 private BluetoothProfile.ServiceListener mProfileServiceListener =
196 new BluetoothProfile.ServiceListener() {
197 public void onServiceConnected(int profile, BluetoothProfile proxy) {
198 mBluetoothPan = (BluetoothPan) proxy;
199 }
200 public void onServiceDisconnected(int profile) {
201 mBluetoothPan = null;
202 }
203 };
204
Irfan Sheriff0b266962010-04-06 15:16:44 -0700205 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700206 public Dialog onCreateDialog(int id) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700207 if (id == DIALOG_TETHER_HELP) {
208 Locale locale = Locale.getDefault();
Irfan Sheriff0b266962010-04-06 15:16:44 -0700209
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700210 // check for the full language + country resource, if not there, try just language
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700211 final AssetManager am = getActivity().getAssets();
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700212 String path = HELP_PATH.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800213 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700214 boolean useCountry = true;
215 InputStream is = null;
216 try {
217 is = am.open(path);
Jake Hamby436b29e2011-02-07 18:21:25 -0800218 } catch (Exception ignored) {
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700219 useCountry = false;
220 } finally {
221 if (is != null) {
222 try {
223 is.close();
Jake Hamby436b29e2011-02-07 18:21:25 -0800224 } catch (Exception ignored) {}
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700225 }
226 }
227 String url = HELP_URL.replace("%y", locale.getLanguage().toLowerCase());
Jake Hamby436b29e2011-02-07 18:21:25 -0800228 url = url.replace("%z", useCountry ? '_'+locale.getCountry().toLowerCase() : "");
Irfan Sheriff0b266962010-04-06 15:16:44 -0700229 if ((mUsbRegexs.length != 0) && (mWifiRegexs.length == 0)) {
230 url = url.replace("%x", USB_HELP_MODIFIER);
231 } else if ((mWifiRegexs.length != 0) && (mUsbRegexs.length == 0)) {
232 url = url.replace("%x", WIFI_HELP_MODIFIER);
233 } else {
234 // could assert that both wifi and usb have regexs, but the default
235 // is to use this anyway so no check is needed
236 url = url.replace("%x", "");
237 }
Robert Greenwalt4c3b2f02010-04-19 12:22:48 -0700238
Irfan Sheriff0b266962010-04-06 15:16:44 -0700239 mView.loadUrl(url);
Amith Yamasani476d7952011-01-21 09:28:31 -0800240 // Detach from old parent first
241 ViewParent parent = mView.getParent();
242 if (parent != null && parent instanceof ViewGroup) {
243 ((ViewGroup) parent).removeView(mView);
244 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700245 return new AlertDialog.Builder(getActivity())
Irfan Sheriff0b266962010-04-06 15:16:44 -0700246 .setCancelable(true)
247 .setTitle(R.string.tethering_help_button_text)
248 .setView(mView)
249 .create();
Amith Yamasani84a042c2011-03-02 11:25:04 -0800250 } else if (id == DIALOG_AP_SETTINGS) {
251 final Activity activity = getActivity();
252 mDialog = new WifiApDialog(activity, this, mWifiConfig);
253 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700254 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800255
Irfan Sheriff0b266962010-04-06 15:16:44 -0700256 return null;
257 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800258
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800259 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700260 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800261 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800262 String action = intent.getAction();
263 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800264 // TODO - this should understand the interface types
265 ArrayList<String> available = intent.getStringArrayListExtra(
266 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
267 ArrayList<String> active = intent.getStringArrayListExtra(
268 ConnectivityManager.EXTRA_ACTIVE_TETHER);
269 ArrayList<String> errored = intent.getStringArrayListExtra(
270 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700271 updateState(available.toArray(new String[available.size()]),
272 active.toArray(new String[active.size()]),
273 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700274 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
275 mMassStorageActive = true;
276 updateState();
277 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
278 mMassStorageActive = false;
279 updateState();
280 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
281 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800282 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800283 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
284 if (mBluetoothEnableForTether) {
285 switch (intent
286 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
287 case BluetoothAdapter.STATE_ON:
288 mBluetoothPan.setBluetoothTethering(true);
289 mBluetoothEnableForTether = false;
290 break;
291
292 case BluetoothAdapter.STATE_OFF:
293 case BluetoothAdapter.ERROR:
294 mBluetoothEnableForTether = false;
295 break;
296
297 default:
298 // ignore transition states
299 }
300 }
Danica Chang32711b62010-08-10 18:41:29 -0700301 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800302 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800303 }
304 }
305
306 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700307 public void onStart() {
308 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800309
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700310 final Activity activity = getActivity();
311
Mike Lockwood69a09572011-07-19 13:30:03 -0700312 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800313 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700314 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700315 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800316
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800317 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700318 filter.addAction(UsbManager.ACTION_USB_STATE);
319 activity.registerReceiver(mTetherChangeReceiver, filter);
320
321 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800322 filter.addAction(Intent.ACTION_MEDIA_SHARED);
323 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
324 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700325 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800326
Danica Chang32711b62010-08-10 18:41:29 -0700327 filter = new IntentFilter();
328 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700329 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700330
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700331 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700332 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700333 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700334 mWifiApEnabler.resume();
335 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800336
337 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800338 }
339
340 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700341 public void onStop() {
342 super.onStop();
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700343 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800344 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700345 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700346 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700347 mWifiApEnabler.pause();
348 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800349 }
350
Robert Greenwalt209177a2010-03-04 13:29:02 -0800351 private void updateState() {
352 ConnectivityManager cm =
353 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
354
355 String[] available = cm.getTetherableIfaces();
356 String[] tethered = cm.getTetheredIfaces();
357 String[] errored = cm.getTetheringErroredIfaces();
358 updateState(available, tethered, errored);
359 }
360
Ben Clark0008d212010-07-27 16:20:59 +0100361 private void updateState(String[] available, String[] tethered,
362 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700363 updateUsbState(available, tethered, errored);
364 updateBluetoothState(available, tethered, errored);
365 }
366
367
368 private void updateUsbState(String[] available, String[] tethered,
369 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800370 ConnectivityManager cm =
371 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700372 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800373 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100374 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800375 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800376 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800377 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
378 usbError = cm.getLastTetherError(s);
379 }
380 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800381 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800382 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800383 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100384 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800385 for (String regex : mUsbRegexs) {
386 if (s.matches(regex)) usbTethered = true;
387 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800388 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800389 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100390 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800391 for (String regex : mUsbRegexs) {
392 if (s.matches(regex)) usbErrored = true;
393 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800394 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800395
396 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800397 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
398 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800399 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800400 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800401 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
402 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
403 } else {
404 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
405 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800406 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800407 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800408 } else if (usbErrored) {
409 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
410 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800411 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700412 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800413 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
414 mUsbTether.setEnabled(false);
415 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800416 } else {
417 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
418 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800419 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800420 }
421 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800422
Danica Chang32711b62010-08-10 18:41:29 -0700423 private void updateBluetoothState(String[] available, String[] tethered,
424 String[] errored) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800425 int bluetoothTethered = 0;
Danica Chang32711b62010-08-10 18:41:29 -0700426 for (String s : tethered) {
427 for (String regex : mBluetoothRegexs) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800428 if (s.matches(regex)) bluetoothTethered++;
Danica Chang32711b62010-08-10 18:41:29 -0700429 }
430 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800431 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700432 for (String s: errored) {
433 for (String regex : mBluetoothRegexs) {
434 if (s.matches(regex)) bluetoothErrored = true;
435 }
436 }
437
438 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
439 int btState = adapter.getState();
440 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
441 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700442 mBluetoothTether.setSummary(R.string.wifi_stopping);
443 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
444 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700445 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Jake Hambyc777ee22011-03-04 15:37:39 -0800446 } else if (btState == BluetoothAdapter.STATE_ON && mBluetoothPan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700447 mBluetoothTether.setChecked(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800448 mBluetoothTether.setEnabled(true);
449 if (bluetoothTethered > 1) {
450 String summary = getString(
451 R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
452 mBluetoothTether.setSummary(summary);
453 } else if (bluetoothTethered == 1) {
454 mBluetoothTether.setSummary(R.string.bluetooth_tethering_device_connected_subtext);
455 } else if (bluetoothErrored) {
456 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
457 } else {
458 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700459 }
460 } else {
461 mBluetoothTether.setEnabled(true);
462 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700463 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
464 }
465 }
466
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700467 public boolean onPreferenceChange(Preference preference, Object value) {
468 boolean enable = (Boolean) value;
469
470 if (enable) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700471 startProvisioningIfNecessary(WIFI_TETHERING);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700472 } else {
473 mWifiApEnabler.setSoftapEnabled(false);
474 }
475 return false;
476 }
477
Irfan Sheriff01b32362011-11-04 12:08:56 -0700478 boolean isProvisioningNeeded() {
479 return mProvisionApp.length == 2;
480 }
481
482 private void startProvisioningIfNecessary(int choice) {
483 mTetherChoice = choice;
484 if (isProvisioningNeeded()) {
485 Intent intent = new Intent(Intent.ACTION_MAIN);
486 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
487 startActivityForResult(intent, PROVISION_REQUEST);
488 } else {
489 startTethering();
490 }
491 }
492
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700493 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
494 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700495 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700496 if (resultCode == Activity.RESULT_OK) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700497 startTethering();
498 } else {
499 //BT and USB need checkbox turned off on failure
500 //Wifi tethering is never turned on until afterwards
501 switch (mTetherChoice) {
502 case BLUETOOTH_TETHERING:
503 mBluetoothTether.setChecked(false);
504 break;
505 case USB_TETHERING:
506 mUsbTether.setChecked(false);
507 break;
508 }
509 mTetherChoice = INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700510 }
511 }
512 }
513
Irfan Sheriff01b32362011-11-04 12:08:56 -0700514 private void startTethering() {
515 switch (mTetherChoice) {
516 case WIFI_TETHERING:
517 mWifiApEnabler.setSoftapEnabled(true);
518 break;
519 case BLUETOOTH_TETHERING:
Danica Chang32711b62010-08-10 18:41:29 -0700520 // turn on Bluetooth first
521 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
522 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800523 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700524 adapter.enable();
525 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
526 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800527 } else {
528 mBluetoothPan.setBluetoothTethering(true);
529 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700530 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700531 break;
532 case USB_TETHERING:
533 setUsbTethering(true);
534 break;
535 default:
536 //should not happen
537 break;
538 }
539 }
540
541 private void setUsbTethering(boolean enabled) {
542 ConnectivityManager cm =
543 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
544 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
545 mUsbTether.setChecked(false);
546 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
547 return;
548 }
549 mUsbTether.setSummary("");
550 }
551
552 @Override
553 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
554 ConnectivityManager cm =
555 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
556
557 if (preference == mUsbTether) {
558 boolean newState = mUsbTether.isChecked();
559
560 if (newState) {
561 startProvisioningIfNecessary(USB_TETHERING);
562 } else {
563 setUsbTethering(newState);
564 }
565 } else if (preference == mBluetoothTether) {
566 boolean bluetoothTetherState = mBluetoothTether.isChecked();
567
568 if (bluetoothTetherState) {
569 startProvisioningIfNecessary(BLUETOOTH_TETHERING);
Danica Chang32711b62010-08-10 18:41:29 -0700570 } else {
571 boolean errored = false;
572
Danica Chang32711b62010-08-10 18:41:29 -0700573 String [] tethered = cm.getTetheredIfaces();
574 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
575 if (bluetoothIface != null &&
576 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
577 errored = true;
578 }
579
Jaikumar Ganeshd2bed692010-09-02 12:11:06 -0700580 mBluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700581 if (errored) {
582 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
583 } else {
584 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
585 }
586 }
587 } else if (preference == mTetherHelp) {
Irfan Sheriff0b266962010-04-06 15:16:44 -0700588 showDialog(DIALOG_TETHER_HELP);
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700589 return true;
Amith Yamasani84a042c2011-03-02 11:25:04 -0800590 } else if (preference == mCreateNetwork) {
591 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800592 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700593
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700594 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800595 }
596
Jake Hamby436b29e2011-02-07 18:21:25 -0800597 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800598 for (String iface : ifaces) {
599 for (String regex : regexes) {
600 if (iface.matches(regex)) {
601 return iface;
602 }
603 }
604 }
605 return null;
606 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800607
608 public void onClick(DialogInterface dialogInterface, int button) {
609 if (button == DialogInterface.BUTTON_POSITIVE) {
610 mWifiConfig = mDialog.getConfig();
611 if (mWifiConfig != null) {
612 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700613 * if soft AP is stopped, bring up
614 * else restart with new config
615 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800616 */
617 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700618 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800619 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800620 } else {
621 mWifiManager.setWifiApConfiguration(mWifiConfig);
622 }
623 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
624 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
625 mWifiConfig.SSID,
626 mSecurityType[index]));
627 }
628 }
629 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800630}