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; |
PauloftheWest | 29aab7a | 2014-09-26 16:05:44 -0700 | [diff] [blame^] | 33 | import android.content.pm.PackageManager; |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 34 | import android.content.res.AssetManager; |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 35 | import android.hardware.usb.UsbManager; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 36 | import android.net.ConnectivityManager; |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 37 | import android.net.wifi.WifiConfiguration; |
| 38 | import android.net.wifi.WifiManager; |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 39 | import android.os.Bundle; |
Mike Lockwood | 26dad3e | 2010-03-03 06:19:55 -0500 | [diff] [blame] | 40 | import android.os.Environment; |
Robert Greenwalt | 505766c | 2011-11-09 15:03:14 -0800 | [diff] [blame] | 41 | import android.os.SystemProperties; |
Amith Yamasani | 394eaa2 | 2013-06-11 11:04:44 -0700 | [diff] [blame] | 42 | import android.os.UserHandle; |
| 43 | import android.os.UserManager; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 44 | import android.preference.Preference; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 45 | import android.preference.PreferenceScreen; |
PauloftheWest | 656c88c | 2014-08-24 11:59:35 -0700 | [diff] [blame] | 46 | import android.preference.SwitchPreference; |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 47 | import android.text.TextUtils; |
Amith Yamasani | 476d795 | 2011-01-21 09:28:31 -0800 | [diff] [blame] | 48 | import android.view.ViewGroup; |
| 49 | import android.view.ViewParent; |
Robert Greenwalt | c3c93e5 | 2010-03-15 17:21:16 -0700 | [diff] [blame] | 50 | import android.webkit.WebView; |
Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 51 | import android.widget.TextView; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 52 | |
Robert Greenwalt | 4c3b2f0 | 2010-04-19 12:22:48 -0700 | [diff] [blame] | 53 | import java.io.InputStream; |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 54 | import java.util.ArrayList; |
Robert Greenwalt | f60b92b | 2012-09-13 15:02:00 -0700 | [diff] [blame] | 55 | import java.util.concurrent.atomic.AtomicReference; |
Robert Greenwalt | c3c93e5 | 2010-03-15 17:21:16 -0700 | [diff] [blame] | 56 | import java.util.Locale; |
Matthew Xie | b5f144a | 2012-06-28 18:41:54 -0700 | [diff] [blame] | 57 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 58 | /* |
| 59 | * Displays preferences for Tethering. |
| 60 | */ |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 61 | public class TetherSettings extends SettingsPreferenceFragment |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 62 | implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener { |
zzy | 7f38f47 | 2012-04-14 17:25:17 -0700 | [diff] [blame] | 63 | private static final String TAG = "TetherSettings"; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 64 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 65 | private static final String USB_TETHER_SETTINGS = "usb_tether_settings"; |
Irfan Sheriff | 65cff17 | 2010-02-08 10:46:30 -0800 | [diff] [blame] | 66 | private static final String ENABLE_WIFI_AP = "enable_wifi_ap"; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 67 | private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering"; |
Rohit Sisodia | 37155e9 | 2014-09-22 16:34:56 -0500 | [diff] [blame] | 68 | private static final String TETHER_CHOICE = "TETHER_TYPE"; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 69 | |
Amith Yamasani | d3fed68 | 2012-04-27 17:38:40 -0700 | [diff] [blame] | 70 | private static final int DIALOG_AP_SETTINGS = 1; |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 71 | |
| 72 | private WebView mView; |
PauloftheWest | 656c88c | 2014-08-24 11:59:35 -0700 | [diff] [blame] | 73 | private SwitchPreference mUsbTether; |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 74 | |
Irfan Sheriff | 65cff17 | 2010-02-08 10:46:30 -0800 | [diff] [blame] | 75 | private WifiApEnabler mWifiApEnabler; |
PauloftheWest | 656c88c | 2014-08-24 11:59:35 -0700 | [diff] [blame] | 76 | private SwitchPreference mEnableWifiAp; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 77 | |
PauloftheWest | 656c88c | 2014-08-24 11:59:35 -0700 | [diff] [blame] | 78 | private SwitchPreference mBluetoothTether; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 79 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 80 | private BroadcastReceiver mTetherChangeReceiver; |
| 81 | |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 82 | private String[] mUsbRegexs; |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 83 | |
| 84 | private String[] mWifiRegexs; |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 85 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 86 | private String[] mBluetoothRegexs; |
Robert Greenwalt | 4f1970f | 2012-09-16 17:03:08 -0700 | [diff] [blame] | 87 | private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<BluetoothPan>(); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 88 | |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 89 | 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 Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 94 | |
| 95 | private WifiApDialog mDialog; |
| 96 | private WifiManager mWifiManager; |
| 97 | private WifiConfiguration mWifiConfig = null; |
Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 98 | private UserManager mUm; |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 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 | |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 105 | private static final int INVALID = -1; |
| 106 | private static final int WIFI_TETHERING = 0; |
| 107 | private static final int USB_TETHERING = 1; |
| 108 | private static final int BLUETOOTH_TETHERING = 2; |
| 109 | |
| 110 | /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */ |
| 111 | private int mTetherChoice = INVALID; |
| 112 | |
| 113 | /* Stores the package name and the class name of the provisioning app */ |
| 114 | private String[] mProvisionApp; |
| 115 | private static final int PROVISION_REQUEST = 0; |
| 116 | |
Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 117 | private boolean mUnavailable; |
| 118 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 119 | @Override |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 120 | public void onCreate(Bundle icicle) { |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 121 | super.onCreate(icicle); |
Rohit Sisodia | 37155e9 | 2014-09-22 16:34:56 -0500 | [diff] [blame] | 122 | |
| 123 | if(icicle != null) { |
| 124 | mTetherChoice = icicle.getInt(TETHER_CHOICE); |
| 125 | } |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 126 | addPreferencesFromResource(R.xml.tether_prefs); |
Amith Yamasani | 476d795 | 2011-01-21 09:28:31 -0800 | [diff] [blame] | 127 | |
Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 128 | mUm = (UserManager) getSystemService(Context.USER_SERVICE); |
| 129 | |
| 130 | if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { |
| 131 | mUnavailable = true; |
| 132 | setPreferenceScreen(new PreferenceScreen(getActivity(), null)); |
| 133 | return; |
| 134 | } |
| 135 | |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 136 | final Activity activity = getActivity(); |
Jaikumar Ganesh | 9ad703c | 2011-02-23 13:08:09 -0800 | [diff] [blame] | 137 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 138 | if (adapter != null) { |
| 139 | adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener, |
| 140 | BluetoothProfile.PAN); |
| 141 | } |
| 142 | |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 143 | mEnableWifiAp = |
PauloftheWest | 656c88c | 2014-08-24 11:59:35 -0700 | [diff] [blame] | 144 | (SwitchPreference) findPreference(ENABLE_WIFI_AP); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 145 | Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY); |
PauloftheWest | 656c88c | 2014-08-24 11:59:35 -0700 | [diff] [blame] | 146 | mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS); |
| 147 | mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING); |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 148 | |
| 149 | ConnectivityManager cm = |
| 150 | (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
Irfan Sheriff | 47ebb78 | 2010-03-10 08:27:15 -0800 | [diff] [blame] | 151 | |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 152 | mUsbRegexs = cm.getTetherableUsbRegexs(); |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 153 | mWifiRegexs = cm.getTetherableWifiRegexs(); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 154 | mBluetoothRegexs = cm.getTetherableBluetoothRegexs(); |
| 155 | |
Amith Yamasani | e419bc1 | 2011-02-14 14:19:08 -0800 | [diff] [blame] | 156 | final boolean usbAvailable = mUsbRegexs.length != 0; |
| 157 | final boolean wifiAvailable = mWifiRegexs.length != 0; |
| 158 | final boolean bluetoothAvailable = mBluetoothRegexs.length != 0; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 159 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 160 | if (!usbAvailable || Utils.isMonkeyRunning()) { |
| 161 | getPreferenceScreen().removePreference(mUsbTether); |
| 162 | } |
Amith Yamasani | e419bc1 | 2011-02-14 14:19:08 -0800 | [diff] [blame] | 163 | |
Amith Yamasani | aaff44c | 2012-01-09 14:52:58 -0800 | [diff] [blame] | 164 | if (wifiAvailable && !Utils.isMonkeyRunning()) { |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 165 | mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 166 | initWifiTethering(); |
| 167 | } else { |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 168 | getPreferenceScreen().removePreference(mEnableWifiAp); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 169 | getPreferenceScreen().removePreference(wifiApSettings); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 170 | } |
Amith Yamasani | e419bc1 | 2011-02-14 14:19:08 -0800 | [diff] [blame] | 171 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 172 | if (!bluetoothAvailable) { |
| 173 | getPreferenceScreen().removePreference(mBluetoothTether); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 174 | } else { |
Robert Greenwalt | f60b92b | 2012-09-13 15:02:00 -0700 | [diff] [blame] | 175 | BluetoothPan pan = mBluetoothPan.get(); |
| 176 | if (pan != null && pan.isTetheringOn()) { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 177 | mBluetoothTether.setChecked(true); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 178 | } else { |
| 179 | mBluetoothTether.setChecked(false); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
Amith Yamasani | e419bc1 | 2011-02-14 14:19:08 -0800 | [diff] [blame] | 182 | |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 183 | mProvisionApp = getResources().getStringArray( |
| 184 | com.android.internal.R.array.config_mobile_hotspot_provision_app); |
| 185 | |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 186 | mView = new WebView(activity); |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Rohit Sisodia | 37155e9 | 2014-09-22 16:34:56 -0500 | [diff] [blame] | 189 | @Override |
| 190 | public void onSaveInstanceState(Bundle savedInstanceState) { |
| 191 | savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice); |
| 192 | super.onSaveInstanceState(savedInstanceState); |
| 193 | } |
| 194 | |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 195 | private void initWifiTethering() { |
| 196 | final Activity activity = getActivity(); |
| 197 | mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); |
| 198 | mWifiConfig = mWifiManager.getWifiApConfiguration(); |
| 199 | mSecurityType = getResources().getStringArray(R.array.wifi_ap_security); |
| 200 | |
| 201 | mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY); |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 202 | |
| 203 | if (mWifiConfig == null) { |
| 204 | final String s = activity.getString( |
| 205 | com.android.internal.R.string.wifi_tether_configure_ssid_default); |
| 206 | mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT), |
| 207 | s, mSecurityType[WifiApDialog.OPEN_INDEX])); |
| 208 | } else { |
| 209 | int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig); |
| 210 | mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT), |
| 211 | mWifiConfig.SSID, |
| 212 | mSecurityType[index])); |
| 213 | } |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Jaikumar Ganesh | 9ad703c | 2011-02-23 13:08:09 -0800 | [diff] [blame] | 216 | private BluetoothProfile.ServiceListener mProfileServiceListener = |
| 217 | new BluetoothProfile.ServiceListener() { |
| 218 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Robert Greenwalt | f60b92b | 2012-09-13 15:02:00 -0700 | [diff] [blame] | 219 | mBluetoothPan.set((BluetoothPan) proxy); |
Jaikumar Ganesh | 9ad703c | 2011-02-23 13:08:09 -0800 | [diff] [blame] | 220 | } |
| 221 | public void onServiceDisconnected(int profile) { |
Robert Greenwalt | f60b92b | 2012-09-13 15:02:00 -0700 | [diff] [blame] | 222 | mBluetoothPan.set(null); |
Jaikumar Ganesh | 9ad703c | 2011-02-23 13:08:09 -0800 | [diff] [blame] | 223 | } |
| 224 | }; |
| 225 | |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 226 | @Override |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 227 | public Dialog onCreateDialog(int id) { |
Amith Yamasani | d3fed68 | 2012-04-27 17:38:40 -0700 | [diff] [blame] | 228 | if (id == DIALOG_AP_SETTINGS) { |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 229 | final Activity activity = getActivity(); |
| 230 | mDialog = new WifiApDialog(activity, this, mWifiConfig); |
| 231 | return mDialog; |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 232 | } |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 233 | |
Irfan Sheriff | 0b26696 | 2010-04-06 15:16:44 -0700 | [diff] [blame] | 234 | return null; |
| 235 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 236 | |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 237 | private class TetherChangeReceiver extends BroadcastReceiver { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 238 | @Override |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 239 | public void onReceive(Context content, Intent intent) { |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 240 | String action = intent.getAction(); |
| 241 | if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) { |
Robert Greenwalt | a248876 | 2010-03-10 16:57:08 -0800 | [diff] [blame] | 242 | // TODO - this should understand the interface types |
| 243 | ArrayList<String> available = intent.getStringArrayListExtra( |
| 244 | ConnectivityManager.EXTRA_AVAILABLE_TETHER); |
| 245 | ArrayList<String> active = intent.getStringArrayListExtra( |
| 246 | ConnectivityManager.EXTRA_ACTIVE_TETHER); |
| 247 | ArrayList<String> errored = intent.getStringArrayListExtra( |
| 248 | ConnectivityManager.EXTRA_ERRORED_TETHER); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 249 | updateState(available.toArray(new String[available.size()]), |
| 250 | active.toArray(new String[active.size()]), |
| 251 | errored.toArray(new String[errored.size()])); |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 252 | } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) { |
| 253 | mMassStorageActive = true; |
| 254 | updateState(); |
| 255 | } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) { |
| 256 | mMassStorageActive = false; |
| 257 | updateState(); |
| 258 | } else if (action.equals(UsbManager.ACTION_USB_STATE)) { |
| 259 | mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false); |
Robert Greenwalt | a248876 | 2010-03-10 16:57:08 -0800 | [diff] [blame] | 260 | updateState(); |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 261 | } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { |
| 262 | if (mBluetoothEnableForTether) { |
| 263 | switch (intent |
| 264 | .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) { |
| 265 | case BluetoothAdapter.STATE_ON: |
Robert Greenwalt | f60b92b | 2012-09-13 15:02:00 -0700 | [diff] [blame] | 266 | BluetoothPan bluetoothPan = mBluetoothPan.get(); |
| 267 | if (bluetoothPan != null) { |
| 268 | bluetoothPan.setBluetoothTethering(true); |
zzy | 7f38f47 | 2012-04-14 17:25:17 -0700 | [diff] [blame] | 269 | mBluetoothEnableForTether = false; |
| 270 | } |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 271 | break; |
| 272 | |
| 273 | case BluetoothAdapter.STATE_OFF: |
| 274 | case BluetoothAdapter.ERROR: |
| 275 | mBluetoothEnableForTether = false; |
| 276 | break; |
| 277 | |
| 278 | default: |
| 279 | // ignore transition states |
| 280 | } |
| 281 | } |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 282 | updateState(); |
Robert Greenwalt | a248876 | 2010-03-10 16:57:08 -0800 | [diff] [blame] | 283 | } |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | @Override |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 288 | public void onStart() { |
| 289 | super.onStart(); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 290 | |
Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 291 | if (mUnavailable) { |
| 292 | TextView emptyView = (TextView) getView().findViewById(android.R.id.empty); |
| 293 | getListView().setEmptyView(emptyView); |
| 294 | if (emptyView != null) { |
| 295 | emptyView.setText(R.string.tethering_settings_not_available); |
| 296 | } |
| 297 | return; |
| 298 | } |
| 299 | |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 300 | final Activity activity = getActivity(); |
| 301 | |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 302 | mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState()); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 303 | mTetherChangeReceiver = new TetherChangeReceiver(); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 304 | IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 305 | Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 306 | |
Robert Greenwalt | f0ed2f3 | 2010-03-12 10:28:35 -0800 | [diff] [blame] | 307 | filter = new IntentFilter(); |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 308 | filter.addAction(UsbManager.ACTION_USB_STATE); |
| 309 | activity.registerReceiver(mTetherChangeReceiver, filter); |
| 310 | |
| 311 | filter = new IntentFilter(); |
Robert Greenwalt | f0ed2f3 | 2010-03-12 10:28:35 -0800 | [diff] [blame] | 312 | filter.addAction(Intent.ACTION_MEDIA_SHARED); |
| 313 | filter.addAction(Intent.ACTION_MEDIA_UNSHARED); |
| 314 | filter.addDataScheme("file"); |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 315 | activity.registerReceiver(mTetherChangeReceiver, filter); |
Robert Greenwalt | f0ed2f3 | 2010-03-12 10:28:35 -0800 | [diff] [blame] | 316 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 317 | filter = new IntentFilter(); |
| 318 | filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 319 | activity.registerReceiver(mTetherChangeReceiver, filter); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 320 | |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 321 | if (intent != null) mTetherChangeReceiver.onReceive(activity, intent); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 322 | if (mWifiApEnabler != null) { |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 323 | mEnableWifiAp.setOnPreferenceChangeListener(this); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 324 | mWifiApEnabler.resume(); |
| 325 | } |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 326 | |
| 327 | updateState(); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 331 | public void onStop() { |
| 332 | super.onStop(); |
Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 333 | |
| 334 | if (mUnavailable) { |
| 335 | return; |
| 336 | } |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 337 | getActivity().unregisterReceiver(mTetherChangeReceiver); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 338 | mTetherChangeReceiver = null; |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 339 | if (mWifiApEnabler != null) { |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 340 | mEnableWifiAp.setOnPreferenceChangeListener(null); |
Amith Yamasani | 0f47465 | 2011-08-30 17:30:01 -0700 | [diff] [blame] | 341 | mWifiApEnabler.pause(); |
| 342 | } |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 345 | private void updateState() { |
| 346 | ConnectivityManager cm = |
| 347 | (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
| 348 | |
| 349 | String[] available = cm.getTetherableIfaces(); |
| 350 | String[] tethered = cm.getTetheredIfaces(); |
| 351 | String[] errored = cm.getTetheringErroredIfaces(); |
| 352 | updateState(available, tethered, errored); |
| 353 | } |
| 354 | |
Ben Clark | 0008d21 | 2010-07-27 16:20:59 +0100 | [diff] [blame] | 355 | private void updateState(String[] available, String[] tethered, |
| 356 | String[] errored) { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 357 | updateUsbState(available, tethered, errored); |
| 358 | updateBluetoothState(available, tethered, errored); |
| 359 | } |
| 360 | |
| 361 | |
| 362 | private void updateUsbState(String[] available, String[] tethered, |
| 363 | String[] errored) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 364 | ConnectivityManager cm = |
| 365 | (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 366 | boolean usbAvailable = mUsbConnected && !mMassStorageActive; |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 367 | int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
Ben Clark | 0008d21 | 2010-07-27 16:20:59 +0100 | [diff] [blame] | 368 | for (String s : available) { |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 369 | for (String regex : mUsbRegexs) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 370 | if (s.matches(regex)) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 371 | if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 372 | usbError = cm.getLastTetherError(s); |
| 373 | } |
| 374 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 375 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 376 | } |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 377 | boolean usbTethered = false; |
Ben Clark | 0008d21 | 2010-07-27 16:20:59 +0100 | [diff] [blame] | 378 | for (String s : tethered) { |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 379 | for (String regex : mUsbRegexs) { |
| 380 | if (s.matches(regex)) usbTethered = true; |
| 381 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 382 | } |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 383 | boolean usbErrored = false; |
Ben Clark | 0008d21 | 2010-07-27 16:20:59 +0100 | [diff] [blame] | 384 | for (String s: errored) { |
Robert Greenwalt | d5f121c | 2010-03-02 17:33:11 -0800 | [diff] [blame] | 385 | for (String regex : mUsbRegexs) { |
| 386 | if (s.matches(regex)) usbErrored = true; |
| 387 | } |
Robert Greenwalt | d5f121c | 2010-03-02 17:33:11 -0800 | [diff] [blame] | 388 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 389 | |
| 390 | if (usbTethered) { |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 391 | mUsbTether.setSummary(R.string.usb_tethering_active_subtext); |
| 392 | mUsbTether.setEnabled(true); |
Robert Greenwalt | 204e7c1 | 2010-03-05 19:00:30 -0800 | [diff] [blame] | 393 | mUsbTether.setChecked(true); |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 394 | } else if (usbAvailable) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 395 | if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 396 | mUsbTether.setSummary(R.string.usb_tethering_available_subtext); |
| 397 | } else { |
| 398 | mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); |
| 399 | } |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 400 | mUsbTether.setEnabled(true); |
Robert Greenwalt | 204e7c1 | 2010-03-05 19:00:30 -0800 | [diff] [blame] | 401 | mUsbTether.setChecked(false); |
Robert Greenwalt | d5f121c | 2010-03-02 17:33:11 -0800 | [diff] [blame] | 402 | } else if (usbErrored) { |
| 403 | mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); |
| 404 | mUsbTether.setEnabled(false); |
Robert Greenwalt | 204e7c1 | 2010-03-05 19:00:30 -0800 | [diff] [blame] | 405 | mUsbTether.setChecked(false); |
Mike Lockwood | 69a0957 | 2011-07-19 13:30:03 -0700 | [diff] [blame] | 406 | } else if (mMassStorageActive) { |
Robert Greenwalt | a248876 | 2010-03-10 16:57:08 -0800 | [diff] [blame] | 407 | mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext); |
| 408 | mUsbTether.setEnabled(false); |
| 409 | mUsbTether.setChecked(false); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 410 | } else { |
| 411 | mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext); |
| 412 | mUsbTether.setEnabled(false); |
Robert Greenwalt | 204e7c1 | 2010-03-05 19:00:30 -0800 | [diff] [blame] | 413 | mUsbTether.setChecked(false); |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 416 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 417 | private void updateBluetoothState(String[] available, String[] tethered, |
| 418 | String[] errored) { |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 419 | boolean bluetoothErrored = false; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 420 | for (String s: errored) { |
| 421 | for (String regex : mBluetoothRegexs) { |
| 422 | if (s.matches(regex)) bluetoothErrored = true; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
Jianzheng Zhou | 508c7c1 | 2013-08-15 14:34:52 +0800 | [diff] [blame] | 427 | if (adapter == null) |
| 428 | return; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 429 | int btState = adapter.getState(); |
| 430 | if (btState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 431 | mBluetoothTether.setEnabled(false); |
Ryan Baxter | 232e6fb | 2013-03-06 18:27:53 -0500 | [diff] [blame] | 432 | mBluetoothTether.setSummary(R.string.bluetooth_turning_off); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 433 | } else if (btState == BluetoothAdapter.STATE_TURNING_ON) { |
| 434 | mBluetoothTether.setEnabled(false); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 435 | mBluetoothTether.setSummary(R.string.bluetooth_turning_on); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 436 | } else { |
Robert Greenwalt | f60b92b | 2012-09-13 15:02:00 -0700 | [diff] [blame] | 437 | BluetoothPan bluetoothPan = mBluetoothPan.get(); |
| 438 | if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null && |
| 439 | bluetoothPan.isTetheringOn()) { |
| 440 | mBluetoothTether.setChecked(true); |
| 441 | mBluetoothTether.setEnabled(true); |
| 442 | int bluetoothTethered = bluetoothPan.getConnectedDevices().size(); |
| 443 | if (bluetoothTethered > 1) { |
| 444 | String summary = getString( |
| 445 | R.string.bluetooth_tethering_devices_connected_subtext, |
| 446 | bluetoothTethered); |
| 447 | mBluetoothTether.setSummary(summary); |
| 448 | } else if (bluetoothTethered == 1) { |
| 449 | mBluetoothTether.setSummary( |
| 450 | R.string.bluetooth_tethering_device_connected_subtext); |
| 451 | } else if (bluetoothErrored) { |
| 452 | mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext); |
| 453 | } else { |
| 454 | mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext); |
| 455 | } |
| 456 | } else { |
| 457 | mBluetoothTether.setEnabled(true); |
| 458 | mBluetoothTether.setChecked(false); |
| 459 | mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext); |
| 460 | } |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 464 | public boolean onPreferenceChange(Preference preference, Object value) { |
| 465 | boolean enable = (Boolean) value; |
| 466 | |
| 467 | if (enable) { |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 468 | startProvisioningIfNecessary(WIFI_TETHERING); |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 469 | } else { |
| 470 | mWifiApEnabler.setSoftapEnabled(false); |
| 471 | } |
| 472 | return false; |
| 473 | } |
| 474 | |
PauloftheWest | 29aab7a | 2014-09-26 16:05:44 -0700 | [diff] [blame^] | 475 | public static boolean isProvisioningNeededButUnavailable(Context context) { |
| 476 | String[] provisionApp = context.getResources().getStringArray( |
| 477 | com.android.internal.R.array.config_mobile_hotspot_provision_app); |
| 478 | return (isProvisioningNeeded(provisionApp) |
| 479 | && !isIntentAvailable(context, provisionApp)); |
| 480 | } |
| 481 | |
| 482 | private static boolean isIntentAvailable(Context context, String[] provisionApp) { |
| 483 | if (provisionApp.length < 2) { |
| 484 | throw new IllegalArgumentException("provisionApp length should at least be 2"); |
| 485 | } |
| 486 | final PackageManager packageManager = context.getPackageManager(); |
| 487 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 488 | intent.setClassName(provisionApp[0], provisionApp[1]); |
| 489 | |
| 490 | return (packageManager.queryIntentActivities(intent, |
| 491 | PackageManager.MATCH_DEFAULT_ONLY).size() > 0); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | private static boolean isProvisioningNeeded(String[] provisionApp) { |
| 496 | if (SystemProperties.getBoolean("net.tethering.noprovisioning", false) |
| 497 | || provisionApp == null) { |
Robert Greenwalt | 505766c | 2011-11-09 15:03:14 -0800 | [diff] [blame] | 498 | return false; |
| 499 | } |
PauloftheWest | 29aab7a | 2014-09-26 16:05:44 -0700 | [diff] [blame^] | 500 | return (provisionApp.length == 2); |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | private void startProvisioningIfNecessary(int choice) { |
| 504 | mTetherChoice = choice; |
PauloftheWest | 29aab7a | 2014-09-26 16:05:44 -0700 | [diff] [blame^] | 505 | if (isProvisioningNeeded(mProvisionApp)) { |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 506 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 507 | intent.setClassName(mProvisionApp[0], mProvisionApp[1]); |
Rohit Sisodia | 37155e9 | 2014-09-22 16:34:56 -0500 | [diff] [blame] | 508 | intent.putExtra(TETHER_CHOICE, mTetherChoice); |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 509 | startActivityForResult(intent, PROVISION_REQUEST); |
| 510 | } else { |
| 511 | startTethering(); |
| 512 | } |
| 513 | } |
| 514 | |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 515 | public void onActivityResult(int requestCode, int resultCode, Intent intent) { |
| 516 | super.onActivityResult(requestCode, resultCode, intent); |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 517 | if (requestCode == PROVISION_REQUEST) { |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 518 | if (resultCode == Activity.RESULT_OK) { |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 519 | startTethering(); |
| 520 | } else { |
PauloftheWest | 656c88c | 2014-08-24 11:59:35 -0700 | [diff] [blame] | 521 | //BT and USB need switch turned off on failure |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 522 | //Wifi tethering is never turned on until afterwards |
| 523 | switch (mTetherChoice) { |
| 524 | case BLUETOOTH_TETHERING: |
| 525 | mBluetoothTether.setChecked(false); |
| 526 | break; |
| 527 | case USB_TETHERING: |
| 528 | mUsbTether.setChecked(false); |
| 529 | break; |
| 530 | } |
| 531 | mTetherChoice = INVALID; |
Irfan Sheriff | aa3d2c4 | 2011-10-06 11:45:45 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 536 | private void startTethering() { |
| 537 | switch (mTetherChoice) { |
| 538 | case WIFI_TETHERING: |
| 539 | mWifiApEnabler.setSoftapEnabled(true); |
| 540 | break; |
| 541 | case BLUETOOTH_TETHERING: |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 542 | // turn on Bluetooth first |
| 543 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 544 | if (adapter.getState() == BluetoothAdapter.STATE_OFF) { |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 545 | mBluetoothEnableForTether = true; |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 546 | adapter.enable(); |
| 547 | mBluetoothTether.setSummary(R.string.bluetooth_turning_on); |
| 548 | mBluetoothTether.setEnabled(false); |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 549 | } else { |
Robert Greenwalt | f60b92b | 2012-09-13 15:02:00 -0700 | [diff] [blame] | 550 | BluetoothPan bluetoothPan = mBluetoothPan.get(); |
| 551 | if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true); |
Jake Hamby | c777ee2 | 2011-03-04 15:37:39 -0800 | [diff] [blame] | 552 | mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 553 | } |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 554 | break; |
| 555 | case USB_TETHERING: |
| 556 | setUsbTethering(true); |
| 557 | break; |
| 558 | default: |
| 559 | //should not happen |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | private void setUsbTethering(boolean enabled) { |
| 565 | ConnectivityManager cm = |
| 566 | (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
Li Wenji | 83397ea | 2012-02-11 11:52:30 +0800 | [diff] [blame] | 567 | mUsbTether.setChecked(false); |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 568 | if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Irfan Sheriff | 01b3236 | 2011-11-04 12:08:56 -0700 | [diff] [blame] | 569 | mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); |
| 570 | return; |
| 571 | } |
| 572 | mUsbTether.setSummary(""); |
| 573 | } |
| 574 | |
| 575 | @Override |
| 576 | public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { |
| 577 | ConnectivityManager cm = |
| 578 | (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
| 579 | |
| 580 | if (preference == mUsbTether) { |
| 581 | boolean newState = mUsbTether.isChecked(); |
| 582 | |
| 583 | if (newState) { |
| 584 | startProvisioningIfNecessary(USB_TETHERING); |
| 585 | } else { |
| 586 | setUsbTethering(newState); |
| 587 | } |
| 588 | } else if (preference == mBluetoothTether) { |
| 589 | boolean bluetoothTetherState = mBluetoothTether.isChecked(); |
| 590 | |
| 591 | if (bluetoothTetherState) { |
| 592 | startProvisioningIfNecessary(BLUETOOTH_TETHERING); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 593 | } else { |
| 594 | boolean errored = false; |
| 595 | |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 596 | String [] tethered = cm.getTetheredIfaces(); |
| 597 | String bluetoothIface = findIface(tethered, mBluetoothRegexs); |
| 598 | if (bluetoothIface != null && |
| 599 | cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 600 | errored = true; |
| 601 | } |
| 602 | |
Robert Greenwalt | f60b92b | 2012-09-13 15:02:00 -0700 | [diff] [blame] | 603 | BluetoothPan bluetoothPan = mBluetoothPan.get(); |
| 604 | if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 605 | if (errored) { |
| 606 | mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext); |
| 607 | } else { |
| 608 | mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext); |
| 609 | } |
| 610 | } |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 611 | } else if (preference == mCreateNetwork) { |
| 612 | showDialog(DIALOG_AP_SETTINGS); |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 613 | } |
Daisuke Miyakawa | 9c602c4 | 2010-09-10 12:04:37 -0700 | [diff] [blame] | 614 | |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 615 | return super.onPreferenceTreeClick(screen, preference); |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 618 | private static String findIface(String[] ifaces, String[] regexes) { |
Robert Greenwalt | 209177a | 2010-03-04 13:29:02 -0800 | [diff] [blame] | 619 | for (String iface : ifaces) { |
| 620 | for (String regex : regexes) { |
| 621 | if (iface.matches(regex)) { |
| 622 | return iface; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | return null; |
| 627 | } |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 628 | |
| 629 | public void onClick(DialogInterface dialogInterface, int button) { |
| 630 | if (button == DialogInterface.BUTTON_POSITIVE) { |
| 631 | mWifiConfig = mDialog.getConfig(); |
| 632 | if (mWifiConfig != null) { |
| 633 | /** |
Irfan Sheriff | 233577c | 2011-07-26 14:01:22 -0700 | [diff] [blame] | 634 | * if soft AP is stopped, bring up |
| 635 | * else restart with new config |
| 636 | * 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] | 637 | */ |
| 638 | if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) { |
Irfan Sheriff | 233577c | 2011-07-26 14:01:22 -0700 | [diff] [blame] | 639 | mWifiManager.setWifiApEnabled(null, false); |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 640 | mWifiManager.setWifiApEnabled(mWifiConfig, true); |
Amith Yamasani | 84a042c | 2011-03-02 11:25:04 -0800 | [diff] [blame] | 641 | } else { |
| 642 | mWifiManager.setWifiApConfiguration(mWifiConfig); |
| 643 | } |
| 644 | int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig); |
| 645 | mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT), |
| 646 | mWifiConfig.SSID, |
| 647 | mSecurityType[index])); |
| 648 | } |
| 649 | } |
| 650 | } |
Amith Yamasani | d3fed68 | 2012-04-27 17:38:40 -0700 | [diff] [blame] | 651 | |
| 652 | @Override |
| 653 | public int getHelpResource() { |
| 654 | return R.string.help_url_tether; |
| 655 | } |
Amith Yamasani | 394eaa2 | 2013-06-11 11:04:44 -0700 | [diff] [blame] | 656 | |
| 657 | /** |
| 658 | * Checks whether this screen will have anything to show on this device. This is called by |
| 659 | * the shortcut picker for Settings shortcuts (home screen widget). |
| 660 | * @param context a context object for getting a system service. |
| 661 | * @return whether Tether & portable hotspot should be shown in the shortcuts picker. |
| 662 | */ |
| 663 | public static boolean showInShortcuts(Context context) { |
| 664 | final ConnectivityManager cm = |
| 665 | (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 666 | final boolean isSecondaryUser = UserHandle.myUserId() != UserHandle.USER_OWNER; |
| 667 | return !isSecondaryUser && cm.isTetheringSupported(); |
| 668 | } |
Robert Greenwalt | 3901edb | 2010-01-26 10:22:37 -0800 | [diff] [blame] | 669 | } |