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