Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Irfan Sheriff | 65cff17 | 2010-02-08 10:46:30 -0800 | [diff] [blame] | 19 | import com.android.settings.wifi.WifiApEnabler; |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 20 | import com.android.settings.wifi.WifiApDialog; |
Irfan Sheriff | 65cff17 | 2010-02-08 10:46:30 -0800 | [diff] [blame] | 21 | |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
Robert Greenwalt | c3c93e5 | 2010-03-15 17:21:16 -0700 | [diff] [blame] | 23 | import android.app.AlertDialog; |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 24 | import android.app.Dialog; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 25 | import android.bluetooth.BluetoothAdapter; |
| 26 | import android.bluetooth.BluetoothPan; |
Jaikumar Ganesh | 9ad703c | 2011-02-23 13:08:09 -0800 | [diff] [blame] | 27 | import android.bluetooth.BluetoothProfile; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
| 29 | import android.content.Context; |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 30 | import android.content.DialogInterface; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 31 | import android.content.Intent; |
| 32 | import android.content.IntentFilter; |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 33 | import android.content.res.AssetManager; |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 34 | import android.hardware.usb.UsbManager; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 35 | import android.net.ConnectivityManager; |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 36 | import android.net.wifi.WifiConfiguration; |
| 37 | import android.net.wifi.WifiManager; |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 38 | import android.os.Bundle; |
Mike Lockwood | 26dad3e | 2010-03-03 06:19:55 -0500 | [diff] [blame] | 39 | import android.os.Environment; |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 40 | import android.preference.CheckBoxPreference; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 41 | import android.preference.Preference; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 42 | import android.preference.PreferenceScreen; |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame^] | 43 | import android.text.TextUtils; |
Amith Yamasani | 476d795 | 2011-01-21 09:28:31 -0800 | [diff] [blame] | 44 | import android.view.ViewGroup; |
| 45 | import android.view.ViewParent; |
Robert Greenwalt | c3c93e5 | 2010-03-15 17:21:16 -0700 | [diff] [blame] | 46 | import android.webkit.WebView; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 47 | |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 48 | import java.io.InputStream; |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 49 | import java.util.ArrayList; |
Robert Greenwalt | c3c93e5 | 2010-03-15 17:21:16 -0700 | [diff] [blame] | 50 | import java.util.Locale; |
Irfan Sheriff | 65cff17 | 2010-02-08 10:46:30 -0800 | [diff] [blame] | 51 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 52 | /* |
| 53 | * Displays preferences for Tethering. |
| 54 | */ |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 55 | public class TetherSettings extends SettingsPreferenceFragment |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame^] | 56 | implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 57 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 58 | private static final String USB_TETHER_SETTINGS = "usb_tether_settings"; |
Irfan Sheriff | 65cff17 | 2010-02-08 10:46:30 -0800 | [diff] [blame] | 59 | private static final String ENABLE_WIFI_AP = "enable_wifi_ap"; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 60 | private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering"; |
Robert Greenwalt | c3c93e5 | 2010-03-15 17:21:16 -0700 | [diff] [blame] | 61 | private static final String TETHERING_HELP = "tethering_help"; |
Robert Greenwalt | 38925c0 | 2010-03-18 15:31:48 -0700 | [diff] [blame] | 62 | private static final String USB_HELP_MODIFIER = "usb_"; |
| 63 | private static final String WIFI_HELP_MODIFIER = "wifi_"; |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 64 | 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 Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 66 | |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 67 | private static final int DIALOG_TETHER_HELP = 1; |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 68 | private static final int DIALOG_AP_SETTINGS = 2; |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 69 | |
| 70 | private WebView mView; |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 71 | private CheckBoxPreference mUsbTether; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 72 | |
Irfan Sheriff | 65cff17 | 2010-02-08 10:46:30 -0800 | [diff] [blame] | 73 | private WifiApEnabler mWifiApEnabler; |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame^] | 74 | private CheckBoxPreference mEnableWifiAp; |
| 75 | private static final int MHS_REQUEST = 0; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 76 | |
| 77 | private CheckBoxPreference mBluetoothTether; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 78 | |
Robert Greenwalt | c3c93e5 | 2010-03-15 17:21:16 -0700 | [diff] [blame] | 79 | private PreferenceScreen mTetherHelp; |
Irfan Sheriff | 65cff17 | 2010-02-08 10:46:30 -0800 | [diff] [blame] | 80 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 81 | private BroadcastReceiver mTetherChangeReceiver; |
| 82 | |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 83 | private String[] mUsbRegexs; |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 84 | |
| 85 | private String[] mWifiRegexs; |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 86 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 87 | private String[] mBluetoothRegexs; |
| 88 | private BluetoothPan mBluetoothPan; |
| 89 | |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 90 | 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 Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 95 | |
| 96 | private WifiApDialog mDialog; |
| 97 | private WifiManager mWifiManager; |
| 98 | private WifiConfiguration mWifiConfig = null; |
| 99 | |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 100 | private boolean mUsbConnected; |
| 101 | private boolean mMassStorageActive; |
| 102 | |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 103 | private boolean mBluetoothEnableForTether; |
| 104 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 105 | @Override |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 106 | public void onCreate(Bundle icicle) { |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 107 | super.onCreate(icicle); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 108 | addPreferencesFromResource(R.xml.tether_prefs); |
Amith Yamasani | 476d795 | 2011-01-21 09:28:31 -0800 | [diff] [blame] | 109 | |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 110 | final Activity activity = getActivity(); |
Jaikumar Ganesh | 9ad703c | 2011-02-23 13:08:09 -0800 | [diff] [blame] | 111 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 112 | if (adapter != null) { |
| 113 | adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener, |
| 114 | BluetoothProfile.PAN); |
| 115 | } |
| 116 | |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame^] | 117 | mEnableWifiAp = |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 118 | (CheckBoxPreference) findPreference(ENABLE_WIFI_AP); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 119 | Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY); |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 120 | mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 121 | mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING); |
Robert Greenwalt | c3c93e5 | 2010-03-15 17:21:16 -0700 | [diff] [blame] | 122 | mTetherHelp = (PreferenceScreen) findPreference(TETHERING_HELP); |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 123 | |
| 124 | ConnectivityManager cm = |
| 125 | (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
Irfan Sheriff | 47ebb78 | 2010-03-10 08:27:15 -0800 | [diff] [blame] | 126 | |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 127 | mUsbRegexs = cm.getTetherableUsbRegexs(); |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 128 | mWifiRegexs = cm.getTetherableWifiRegexs(); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 129 | mBluetoothRegexs = cm.getTetherableBluetoothRegexs(); |
| 130 | |
Amith Yamasani | e419bc1 | 2011-02-14 14:19:08 -0800 | [diff] [blame] | 131 | final boolean usbAvailable = mUsbRegexs.length != 0; |
| 132 | final boolean wifiAvailable = mWifiRegexs.length != 0; |
| 133 | final boolean bluetoothAvailable = mBluetoothRegexs.length != 0; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 134 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 135 | if (!usbAvailable || Utils.isMonkeyRunning()) { |
| 136 | getPreferenceScreen().removePreference(mUsbTether); |
| 137 | } |
Amith Yamasani | e419bc1 | 2011-02-14 14:19:08 -0800 | [diff] [blame] | 138 | |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 139 | if (wifiAvailable) { |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame^] | 140 | mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 141 | initWifiTethering(); |
| 142 | } else { |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame^] | 143 | getPreferenceScreen().removePreference(mEnableWifiAp); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 144 | getPreferenceScreen().removePreference(wifiApSettings); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 145 | } |
Amith Yamasani | e419bc1 | 2011-02-14 14:19:08 -0800 | [diff] [blame] | 146 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 147 | if (!bluetoothAvailable) { |
| 148 | getPreferenceScreen().removePreference(mBluetoothTether); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 149 | } else { |
Jaikumar Ganesh | 9ad703c | 2011-02-23 13:08:09 -0800 | [diff] [blame] | 150 | if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 151 | mBluetoothTether.setChecked(true); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 152 | } else { |
| 153 | mBluetoothTether.setChecked(false); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
Amith Yamasani | e419bc1 | 2011-02-14 14:19:08 -0800 | [diff] [blame] | 156 | |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 157 | mView = new WebView(activity); |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 158 | } |
| 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 Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 167 | |
| 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 Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Jaikumar Ganesh | 9ad703c | 2011-02-23 13:08:09 -0800 | [diff] [blame] | 181 | 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 Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 191 | @Override |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 192 | public Dialog onCreateDialog(int id) { |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 193 | if (id == DIALOG_TETHER_HELP) { |
| 194 | Locale locale = Locale.getDefault(); |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 195 | |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 196 | // check for the full language + country resource, if not there, try just language |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 197 | final AssetManager am = getActivity().getAssets(); |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 198 | String path = HELP_PATH.replace("%y", locale.getLanguage().toLowerCase()); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 199 | path = path.replace("%z", '_'+locale.getCountry().toLowerCase()); |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 200 | boolean useCountry = true; |
| 201 | InputStream is = null; |
| 202 | try { |
| 203 | is = am.open(path); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 204 | } catch (Exception ignored) { |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 205 | useCountry = false; |
| 206 | } finally { |
| 207 | if (is != null) { |
| 208 | try { |
| 209 | is.close(); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 210 | } catch (Exception ignored) {} |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | String url = HELP_URL.replace("%y", locale.getLanguage().toLowerCase()); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 214 | url = url.replace("%z", useCountry ? '_'+locale.getCountry().toLowerCase() : ""); |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 215 | 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 Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 224 | |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 225 | mView.loadUrl(url); |
Amith Yamasani | 476d795 | 2011-01-21 09:28:31 -0800 | [diff] [blame] | 226 | // Detach from old parent first |
| 227 | ViewParent parent = mView.getParent(); |
| 228 | if (parent != null && parent instanceof ViewGroup) { |
| 229 | ((ViewGroup) parent).removeView(mView); |
| 230 | } |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 231 | return new AlertDialog.Builder(getActivity()) |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 232 | .setCancelable(true) |
| 233 | .setTitle(R.string.tethering_help_button_text) |
| 234 | .setView(mView) |
| 235 | .create(); |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 236 | } else if (id == DIALOG_AP_SETTINGS) { |
| 237 | final Activity activity = getActivity(); |
| 238 | mDialog = new WifiApDialog(activity, this, mWifiConfig); |
| 239 | return mDialog; |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 240 | } |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 241 | |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 242 | return null; |
| 243 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 244 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 245 | private class TetherChangeReceiver extends BroadcastReceiver { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 246 | @Override |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 247 | public void onReceive(Context content, Intent intent) { |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 248 | String action = intent.getAction(); |
| 249 | if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) { |
Robert Greenwalt | a248876 | 2010-03-10 16:57:08 -0800 | [diff] [blame] | 250 | // 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 Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 257 | updateState(available.toArray(new String[available.size()]), |
| 258 | active.toArray(new String[active.size()]), |
| 259 | errored.toArray(new String[errored.size()])); |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 260 | } 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 Greenwalt | a248876 | 2010-03-10 16:57:08 -0800 | [diff] [blame] | 268 | updateState(); |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 269 | } 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 Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 287 | updateState(); |
Robert Greenwalt | a248876 | 2010-03-10 16:57:08 -0800 | [diff] [blame] | 288 | } |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | @Override |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 293 | public void onStart() { |
| 294 | super.onStart(); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 295 | |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 296 | final Activity activity = getActivity(); |
| 297 | |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 298 | mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState()); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 299 | mTetherChangeReceiver = new TetherChangeReceiver(); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 300 | IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 301 | Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 302 | |
Robert Greenwalt | f0ed2f3 | 2010-03-12 10:28:35 -0800 | [diff] [blame] | 303 | filter = new IntentFilter(); |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 304 | filter.addAction(UsbManager.ACTION_USB_STATE); |
| 305 | activity.registerReceiver(mTetherChangeReceiver, filter); |
| 306 | |
| 307 | filter = new IntentFilter(); |
Robert Greenwalt | f0ed2f3 | 2010-03-12 10:28:35 -0800 | [diff] [blame] | 308 | filter.addAction(Intent.ACTION_MEDIA_SHARED); |
| 309 | filter.addAction(Intent.ACTION_MEDIA_UNSHARED); |
| 310 | filter.addDataScheme("file"); |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 311 | activity.registerReceiver(mTetherChangeReceiver, filter); |
Robert Greenwalt | f0ed2f3 | 2010-03-12 10:28:35 -0800 | [diff] [blame] | 312 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 313 | filter = new IntentFilter(); |
| 314 | filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 315 | activity.registerReceiver(mTetherChangeReceiver, filter); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 316 | |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 317 | if (intent != null) mTetherChangeReceiver.onReceive(activity, intent); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 318 | if (mWifiApEnabler != null) { |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame^] | 319 | mEnableWifiAp.setOnPreferenceChangeListener(this); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 320 | mWifiApEnabler.resume(); |
| 321 | } |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 322 | |
| 323 | updateState(); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @Override |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 327 | public void onStop() { |
| 328 | super.onStop(); |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 329 | getActivity().unregisterReceiver(mTetherChangeReceiver); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 330 | mTetherChangeReceiver = null; |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 331 | if (mWifiApEnabler != null) { |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame^] | 332 | mEnableWifiAp.setOnPreferenceChangeListener(null); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 333 | mWifiApEnabler.pause(); |
| 334 | } |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 337 | 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 Clark | 0008d21 | 2010-07-27 16:20:59 +0100 | [diff] [blame] | 347 | private void updateState(String[] available, String[] tethered, |
| 348 | String[] errored) { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 349 | updateUsbState(available, tethered, errored); |
| 350 | updateBluetoothState(available, tethered, errored); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | private void updateUsbState(String[] available, String[] tethered, |
| 355 | String[] errored) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 356 | ConnectivityManager cm = |
| 357 | (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 358 | boolean usbAvailable = mUsbConnected && !mMassStorageActive; |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 359 | int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
Ben Clark | 0008d21 | 2010-07-27 16:20:59 +0100 | [diff] [blame] | 360 | for (String s : available) { |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 361 | for (String regex : mUsbRegexs) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 362 | if (s.matches(regex)) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 363 | if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 364 | usbError = cm.getLastTetherError(s); |
| 365 | } |
| 366 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 367 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 368 | } |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 369 | boolean usbTethered = false; |
Ben Clark | 0008d21 | 2010-07-27 16:20:59 +0100 | [diff] [blame] | 370 | for (String s : tethered) { |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 371 | for (String regex : mUsbRegexs) { |
| 372 | if (s.matches(regex)) usbTethered = true; |
| 373 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 374 | } |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 375 | boolean usbErrored = false; |
Ben Clark | 0008d21 | 2010-07-27 16:20:59 +0100 | [diff] [blame] | 376 | for (String s: errored) { |
Robert Greenwalt | d5f121c | 2010-03-02 17:33:11 -0800 | [diff] [blame] | 377 | for (String regex : mUsbRegexs) { |
| 378 | if (s.matches(regex)) usbErrored = true; |
| 379 | } |
Robert Greenwalt | d5f121c | 2010-03-02 17:33:11 -0800 | [diff] [blame] | 380 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 381 | |
| 382 | if (usbTethered) { |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 383 | mUsbTether.setSummary(R.string.usb_tethering_active_subtext); |
| 384 | mUsbTether.setEnabled(true); |
Robert Greenwalt | 204e7c1 | 2010-03-05 19:00:30 -0800 | [diff] [blame] | 385 | mUsbTether.setChecked(true); |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 386 | } else if (usbAvailable) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 387 | 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 Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 392 | mUsbTether.setEnabled(true); |
Robert Greenwalt | 204e7c1 | 2010-03-05 19:00:30 -0800 | [diff] [blame] | 393 | mUsbTether.setChecked(false); |
Robert Greenwalt | d5f121c | 2010-03-02 17:33:11 -0800 | [diff] [blame] | 394 | } else if (usbErrored) { |
| 395 | mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); |
| 396 | mUsbTether.setEnabled(false); |
Robert Greenwalt | 204e7c1 | 2010-03-05 19:00:30 -0800 | [diff] [blame] | 397 | mUsbTether.setChecked(false); |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 398 | } else if (mMassStorageActive) { |
Robert Greenwalt | a248876 | 2010-03-10 16:57:08 -0800 | [diff] [blame] | 399 | mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext); |
| 400 | mUsbTether.setEnabled(false); |
| 401 | mUsbTether.setChecked(false); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 402 | } else { |
| 403 | mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext); |
| 404 | mUsbTether.setEnabled(false); |
Robert Greenwalt | 204e7c1 | 2010-03-05 19:00:30 -0800 | [diff] [blame] | 405 | mUsbTether.setChecked(false); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 408 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 409 | private void updateBluetoothState(String[] available, String[] tethered, |
| 410 | String[] errored) { |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 411 | int bluetoothTethered = 0; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 412 | for (String s : tethered) { |
| 413 | for (String regex : mBluetoothRegexs) { |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 414 | if (s.matches(regex)) bluetoothTethered++; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 417 | boolean bluetoothErrored = false; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 418 | 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 Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 428 | mBluetoothTether.setSummary(R.string.wifi_stopping); |
| 429 | } else if (btState == BluetoothAdapter.STATE_TURNING_ON) { |
| 430 | mBluetoothTether.setEnabled(false); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 431 | mBluetoothTether.setSummary(R.string.bluetooth_turning_on); |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 432 | } else if (btState == BluetoothAdapter.STATE_ON && mBluetoothPan.isTetheringOn()) { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 433 | mBluetoothTether.setChecked(true); |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 434 | 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 Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 445 | } |
| 446 | } else { |
| 447 | mBluetoothTether.setEnabled(true); |
| 448 | mBluetoothTether.setChecked(false); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 449 | mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext); |
| 450 | } |
| 451 | } |
| 452 | |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame^] | 453 | 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 Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 482 | @Override |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 483 | public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 484 | ConnectivityManager cm = |
| 485 | (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
| 486 | |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 487 | if (preference == mUsbTether) { |
| 488 | boolean newState = mUsbTether.isChecked(); |
| 489 | |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 490 | 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 Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 494 | } |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 495 | mUsbTether.setSummary(""); |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 496 | } else if (preference == mBluetoothTether) { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 497 | boolean bluetoothTetherState = mBluetoothTether.isChecked(); |
Robert Greenwalt | 38925c0 | 2010-03-18 15:31:48 -0700 | [diff] [blame] | 498 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 499 | if (bluetoothTetherState) { |
| 500 | // turn on Bluetooth first |
| 501 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 502 | if (adapter.getState() == BluetoothAdapter.STATE_OFF) { |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 503 | mBluetoothEnableForTether = true; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 504 | adapter.enable(); |
| 505 | mBluetoothTether.setSummary(R.string.bluetooth_turning_on); |
| 506 | mBluetoothTether.setEnabled(false); |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 507 | } else { |
| 508 | mBluetoothPan.setBluetoothTethering(true); |
| 509 | mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 510 | } |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 511 | } else { |
| 512 | boolean errored = false; |
| 513 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 514 | 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 Ganesh | d2bed69 | 2010-09-02 12:11:06 -0700 | [diff] [blame] | 521 | mBluetoothPan.setBluetoothTethering(false); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 522 | 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 Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 529 | showDialog(DIALOG_TETHER_HELP); |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 530 | return true; |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 531 | } else if (preference == mCreateNetwork) { |
| 532 | showDialog(DIALOG_AP_SETTINGS); |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 533 | } |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 534 | |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 535 | return super.onPreferenceTreeClick(screen, preference); |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 538 | private static String findIface(String[] ifaces, String[] regexes) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 539 | 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 Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 548 | |
| 549 | public void onClick(DialogInterface dialogInterface, int button) { |
| 550 | if (button == DialogInterface.BUTTON_POSITIVE) { |
| 551 | mWifiConfig = mDialog.getConfig(); |
| 552 | if (mWifiConfig != null) { |
| 553 | /** |
Irfan Sheriff | 233577c | 2011-07-26 14:01:22 -0700 | [diff] [blame] | 554 | * 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 Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 557 | */ |
| 558 | if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) { |
Irfan Sheriff | 233577c | 2011-07-26 14:01:22 -0700 | [diff] [blame] | 559 | mWifiManager.setWifiApEnabled(null, false); |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 560 | mWifiManager.setWifiApEnabled(mWifiConfig, true); |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 561 | } 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 Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 571 | } |