blob: ca1e8ec5b6d0328db09cafc5283441bef47b286a [file] [log] [blame]
Robert Greenwalt3901edb2010-01-26 10:22:37 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason Monk4896c012015-02-11 13:30:41 -050019import static com.android.settingslib.TetherUtil.TETHERING_INVALID;
20import static com.android.settingslib.TetherUtil.TETHERING_WIFI;
21import static com.android.settingslib.TetherUtil.TETHERING_USB;
22import static com.android.settingslib.TetherUtil.TETHERING_BLUETOOTH;
23
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070024import android.app.Activity;
Irfan Sheriff0b266962010-04-06 15:16:44 -070025import android.app.Dialog;
Danica Chang32711b62010-08-10 18:41:29 -070026import android.bluetooth.BluetoothAdapter;
27import android.bluetooth.BluetoothPan;
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -080028import android.bluetooth.BluetoothProfile;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080029import android.content.BroadcastReceiver;
30import android.content.Context;
Amith Yamasani84a042c2011-03-02 11:25:04 -080031import android.content.DialogInterface;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080032import android.content.Intent;
33import android.content.IntentFilter;
PauloftheWest29aab7a2014-09-26 16:05:44 -070034import android.content.pm.PackageManager;
Mike Lockwood69a09572011-07-19 13:30:03 -070035import android.hardware.usb.UsbManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080036import android.net.ConnectivityManager;
Amith Yamasani84a042c2011-03-02 11:25:04 -080037import android.net.wifi.WifiConfiguration;
38import android.net.wifi.WifiManager;
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -070039import android.os.Bundle;
Mike Lockwood26dad3e2010-03-03 06:19:55 -050040import android.os.Environment;
Amith Yamasani394eaa22013-06-11 11:04:44 -070041import android.os.UserManager;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080042import android.preference.Preference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080043import android.preference.PreferenceScreen;
PauloftheWest656c88c2014-08-24 11:59:35 -070044import android.preference.SwitchPreference;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040045import android.widget.TextView;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080046
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080047import com.android.settings.wifi.WifiApDialog;
48import com.android.settings.wifi.WifiApEnabler;
Jason Monk4896c012015-02-11 13:30:41 -050049import com.android.settingslib.TetherUtil;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080050
Robert Greenwaltc4764d22010-02-12 14:21:37 -080051import java.util.ArrayList;
Robert Greenwaltf60b92b2012-09-13 15:02:00 -070052import java.util.concurrent.atomic.AtomicReference;
Matthew Xieb5f144a2012-06-28 18:41:54 -070053
Robert Greenwalt3901edb2010-01-26 10:22:37 -080054/*
55 * Displays preferences for Tethering.
56 */
Amith Yamasani84a042c2011-03-02 11:25:04 -080057public class TetherSettings extends SettingsPreferenceFragment
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -070058 implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
zzy7f38f472012-04-14 17:25:17 -070059 private static final String TAG = "TetherSettings";
Danica Chang32711b62010-08-10 18:41:29 -070060
Robert Greenwalt3901edb2010-01-26 10:22:37 -080061 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080062 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
Danica Chang32711b62010-08-10 18:41:29 -070063 private static final String ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering";
Rohit Sisodia37155e92014-09-22 16:34:56 -050064 private static final String TETHER_CHOICE = "TETHER_TYPE";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080065
Amith Yamasanid3fed682012-04-27 17:38:40 -070066 private static final int DIALOG_AP_SETTINGS = 1;
Irfan Sheriff0b266962010-04-06 15:16:44 -070067
PauloftheWest656c88c2014-08-24 11:59:35 -070068 private SwitchPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080069
Irfan Sheriff65cff172010-02-08 10:46:30 -080070 private WifiApEnabler mWifiApEnabler;
PauloftheWest656c88c2014-08-24 11:59:35 -070071 private SwitchPreference mEnableWifiAp;
Danica Chang32711b62010-08-10 18:41:29 -070072
PauloftheWest656c88c2014-08-24 11:59:35 -070073 private SwitchPreference mBluetoothTether;
Danica Chang32711b62010-08-10 18:41:29 -070074
Robert Greenwalt3901edb2010-01-26 10:22:37 -080075 private BroadcastReceiver mTetherChangeReceiver;
76
Robert Greenwaltc4764d22010-02-12 14:21:37 -080077 private String[] mUsbRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080078
79 private String[] mWifiRegexs;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080080
Danica Chang32711b62010-08-10 18:41:29 -070081 private String[] mBluetoothRegexs;
Robert Greenwalt4f1970f2012-09-16 17:03:08 -070082 private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<BluetoothPan>();
Danica Chang32711b62010-08-10 18:41:29 -070083
Amith Yamasani84a042c2011-03-02 11:25:04 -080084 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
85 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
86
87 private String[] mSecurityType;
88 private Preference mCreateNetwork;
Amith Yamasani84a042c2011-03-02 11:25:04 -080089
90 private WifiApDialog mDialog;
91 private WifiManager mWifiManager;
92 private WifiConfiguration mWifiConfig = null;
Julia Reynoldsee27b9d2014-05-09 13:36:20 -040093 private UserManager mUm;
Amith Yamasani84a042c2011-03-02 11:25:04 -080094
Mike Lockwood69a09572011-07-19 13:30:03 -070095 private boolean mUsbConnected;
96 private boolean mMassStorageActive;
97
Jake Hambyc777ee22011-03-04 15:37:39 -080098 private boolean mBluetoothEnableForTether;
99
Irfan Sheriff01b32362011-11-04 12:08:56 -0700100 /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
Jason Monk4896c012015-02-11 13:30:41 -0500101 private int mTetherChoice = TETHERING_INVALID;
Irfan Sheriff01b32362011-11-04 12:08:56 -0700102
103 /* Stores the package name and the class name of the provisioning app */
104 private String[] mProvisionApp;
105 private static final int PROVISION_REQUEST = 0;
106
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400107 private boolean mUnavailable;
108
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800109 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700110 public void onCreate(Bundle icicle) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800111 super.onCreate(icicle);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500112
113 if(icicle != null) {
114 mTetherChoice = icicle.getInt(TETHER_CHOICE);
115 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800116 addPreferencesFromResource(R.xml.tether_prefs);
Amith Yamasani476d7952011-01-21 09:28:31 -0800117
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400118 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
119
120 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
121 mUnavailable = true;
122 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
123 return;
124 }
125
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700126 final Activity activity = getActivity();
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800127 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
128 if (adapter != null) {
129 adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
130 BluetoothProfile.PAN);
131 }
132
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700133 mEnableWifiAp =
PauloftheWest656c88c2014-08-24 11:59:35 -0700134 (SwitchPreference) findPreference(ENABLE_WIFI_AP);
Amith Yamasani0f474652011-08-30 17:30:01 -0700135 Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
PauloftheWest656c88c2014-08-24 11:59:35 -0700136 mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
137 mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800138
139 ConnectivityManager cm =
140 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Irfan Sheriff47ebb782010-03-10 08:27:15 -0800141
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800142 mUsbRegexs = cm.getTetherableUsbRegexs();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800143 mWifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700144 mBluetoothRegexs = cm.getTetherableBluetoothRegexs();
145
Amith Yamasanie419bc12011-02-14 14:19:08 -0800146 final boolean usbAvailable = mUsbRegexs.length != 0;
147 final boolean wifiAvailable = mWifiRegexs.length != 0;
148 final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;
Danica Chang32711b62010-08-10 18:41:29 -0700149
Danica Chang32711b62010-08-10 18:41:29 -0700150 if (!usbAvailable || Utils.isMonkeyRunning()) {
151 getPreferenceScreen().removePreference(mUsbTether);
152 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800153
Amith Yamasaniaaff44c2012-01-09 14:52:58 -0800154 if (wifiAvailable && !Utils.isMonkeyRunning()) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700155 mWifiApEnabler = new WifiApEnabler(activity, mEnableWifiAp);
Amith Yamasani0f474652011-08-30 17:30:01 -0700156 initWifiTethering();
157 } else {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700158 getPreferenceScreen().removePreference(mEnableWifiAp);
Jake Hamby436b29e2011-02-07 18:21:25 -0800159 getPreferenceScreen().removePreference(wifiApSettings);
Danica Chang32711b62010-08-10 18:41:29 -0700160 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800161
Danica Chang32711b62010-08-10 18:41:29 -0700162 if (!bluetoothAvailable) {
163 getPreferenceScreen().removePreference(mBluetoothTether);
Danica Chang32711b62010-08-10 18:41:29 -0700164 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700165 BluetoothPan pan = mBluetoothPan.get();
166 if (pan != null && pan.isTetheringOn()) {
Danica Chang32711b62010-08-10 18:41:29 -0700167 mBluetoothTether.setChecked(true);
Danica Chang32711b62010-08-10 18:41:29 -0700168 } else {
169 mBluetoothTether.setChecked(false);
Danica Chang32711b62010-08-10 18:41:29 -0700170 }
171 }
Amith Yamasanie419bc12011-02-14 14:19:08 -0800172
Irfan Sheriff01b32362011-11-04 12:08:56 -0700173 mProvisionApp = getResources().getStringArray(
174 com.android.internal.R.array.config_mobile_hotspot_provision_app);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800175 }
176
Rohit Sisodia37155e92014-09-22 16:34:56 -0500177 @Override
178 public void onSaveInstanceState(Bundle savedInstanceState) {
179 savedInstanceState.putInt(TETHER_CHOICE, mTetherChoice);
180 super.onSaveInstanceState(savedInstanceState);
181 }
182
Amith Yamasani84a042c2011-03-02 11:25:04 -0800183 private void initWifiTethering() {
184 final Activity activity = getActivity();
185 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
186 mWifiConfig = mWifiManager.getWifiApConfiguration();
187 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
188
189 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800190
191 if (mWifiConfig == null) {
192 final String s = activity.getString(
193 com.android.internal.R.string.wifi_tether_configure_ssid_default);
194 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
195 s, mSecurityType[WifiApDialog.OPEN_INDEX]));
196 } else {
197 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
198 mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
199 mWifiConfig.SSID,
200 mSecurityType[index]));
201 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800202 }
203
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800204 private BluetoothProfile.ServiceListener mProfileServiceListener =
205 new BluetoothProfile.ServiceListener() {
206 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700207 mBluetoothPan.set((BluetoothPan) proxy);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800208 }
209 public void onServiceDisconnected(int profile) {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700210 mBluetoothPan.set(null);
Jaikumar Ganesh9ad703c2011-02-23 13:08:09 -0800211 }
212 };
213
Irfan Sheriff0b266962010-04-06 15:16:44 -0700214 @Override
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700215 public Dialog onCreateDialog(int id) {
Amith Yamasanid3fed682012-04-27 17:38:40 -0700216 if (id == DIALOG_AP_SETTINGS) {
Amith Yamasani84a042c2011-03-02 11:25:04 -0800217 final Activity activity = getActivity();
218 mDialog = new WifiApDialog(activity, this, mWifiConfig);
219 return mDialog;
Irfan Sheriff0b266962010-04-06 15:16:44 -0700220 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800221
Irfan Sheriff0b266962010-04-06 15:16:44 -0700222 return null;
223 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800224
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800225 private class TetherChangeReceiver extends BroadcastReceiver {
Danica Chang32711b62010-08-10 18:41:29 -0700226 @Override
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800227 public void onReceive(Context content, Intent intent) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800228 String action = intent.getAction();
229 if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800230 // TODO - this should understand the interface types
231 ArrayList<String> available = intent.getStringArrayListExtra(
232 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
233 ArrayList<String> active = intent.getStringArrayListExtra(
234 ConnectivityManager.EXTRA_ACTIVE_TETHER);
235 ArrayList<String> errored = intent.getStringArrayListExtra(
236 ConnectivityManager.EXTRA_ERRORED_TETHER);
Danica Chang32711b62010-08-10 18:41:29 -0700237 updateState(available.toArray(new String[available.size()]),
238 active.toArray(new String[active.size()]),
239 errored.toArray(new String[errored.size()]));
Mike Lockwood69a09572011-07-19 13:30:03 -0700240 } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
241 mMassStorageActive = true;
242 updateState();
243 } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
244 mMassStorageActive = false;
245 updateState();
246 } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
247 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
Robert Greenwalta2488762010-03-10 16:57:08 -0800248 updateState();
Jake Hambyc777ee22011-03-04 15:37:39 -0800249 } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
250 if (mBluetoothEnableForTether) {
251 switch (intent
252 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
253 case BluetoothAdapter.STATE_ON:
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700254 BluetoothPan bluetoothPan = mBluetoothPan.get();
255 if (bluetoothPan != null) {
256 bluetoothPan.setBluetoothTethering(true);
zzy7f38f472012-04-14 17:25:17 -0700257 mBluetoothEnableForTether = false;
258 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800259 break;
260
261 case BluetoothAdapter.STATE_OFF:
262 case BluetoothAdapter.ERROR:
263 mBluetoothEnableForTether = false;
264 break;
265
266 default:
267 // ignore transition states
268 }
269 }
Danica Chang32711b62010-08-10 18:41:29 -0700270 updateState();
Robert Greenwalta2488762010-03-10 16:57:08 -0800271 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800272 }
273 }
274
275 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700276 public void onStart() {
277 super.onStart();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800278
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400279 if (mUnavailable) {
280 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
281 getListView().setEmptyView(emptyView);
282 if (emptyView != null) {
283 emptyView.setText(R.string.tethering_settings_not_available);
284 }
285 return;
286 }
287
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700288 final Activity activity = getActivity();
289
Mike Lockwood69a09572011-07-19 13:30:03 -0700290 mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800291 mTetherChangeReceiver = new TetherChangeReceiver();
Danica Chang32711b62010-08-10 18:41:29 -0700292 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700293 Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800294
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800295 filter = new IntentFilter();
Mike Lockwood69a09572011-07-19 13:30:03 -0700296 filter.addAction(UsbManager.ACTION_USB_STATE);
297 activity.registerReceiver(mTetherChangeReceiver, filter);
298
299 filter = new IntentFilter();
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800300 filter.addAction(Intent.ACTION_MEDIA_SHARED);
301 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
302 filter.addDataScheme("file");
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700303 activity.registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwaltf0ed2f32010-03-12 10:28:35 -0800304
Danica Chang32711b62010-08-10 18:41:29 -0700305 filter = new IntentFilter();
306 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700307 activity.registerReceiver(mTetherChangeReceiver, filter);
Danica Chang32711b62010-08-10 18:41:29 -0700308
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700309 if (intent != null) mTetherChangeReceiver.onReceive(activity, intent);
Amith Yamasani0f474652011-08-30 17:30:01 -0700310 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700311 mEnableWifiAp.setOnPreferenceChangeListener(this);
Amith Yamasani0f474652011-08-30 17:30:01 -0700312 mWifiApEnabler.resume();
313 }
Jake Hambyc777ee22011-03-04 15:37:39 -0800314
315 updateState();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800316 }
317
318 @Override
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700319 public void onStop() {
320 super.onStop();
Julia Reynoldsee27b9d2014-05-09 13:36:20 -0400321
322 if (mUnavailable) {
323 return;
324 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700325 getActivity().unregisterReceiver(mTetherChangeReceiver);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800326 mTetherChangeReceiver = null;
Amith Yamasani0f474652011-08-30 17:30:01 -0700327 if (mWifiApEnabler != null) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700328 mEnableWifiAp.setOnPreferenceChangeListener(null);
Amith Yamasani0f474652011-08-30 17:30:01 -0700329 mWifiApEnabler.pause();
330 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800331 }
332
Robert Greenwalt209177a2010-03-04 13:29:02 -0800333 private void updateState() {
334 ConnectivityManager cm =
335 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
336
337 String[] available = cm.getTetherableIfaces();
338 String[] tethered = cm.getTetheredIfaces();
339 String[] errored = cm.getTetheringErroredIfaces();
340 updateState(available, tethered, errored);
341 }
342
Ben Clark0008d212010-07-27 16:20:59 +0100343 private void updateState(String[] available, String[] tethered,
344 String[] errored) {
Danica Chang32711b62010-08-10 18:41:29 -0700345 updateUsbState(available, tethered, errored);
346 updateBluetoothState(available, tethered, errored);
347 }
348
349
350 private void updateUsbState(String[] available, String[] tethered,
351 String[] errored) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800352 ConnectivityManager cm =
353 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Mike Lockwood69a09572011-07-19 13:30:03 -0700354 boolean usbAvailable = mUsbConnected && !mMassStorageActive;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800355 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Ben Clark0008d212010-07-27 16:20:59 +0100356 for (String s : available) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800357 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800358 if (s.matches(regex)) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800359 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
360 usbError = cm.getLastTetherError(s);
361 }
362 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800363 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800364 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800365 boolean usbTethered = false;
Ben Clark0008d212010-07-27 16:20:59 +0100366 for (String s : tethered) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800367 for (String regex : mUsbRegexs) {
368 if (s.matches(regex)) usbTethered = true;
369 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800370 }
Jake Hamby436b29e2011-02-07 18:21:25 -0800371 boolean usbErrored = false;
Ben Clark0008d212010-07-27 16:20:59 +0100372 for (String s: errored) {
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800373 for (String regex : mUsbRegexs) {
374 if (s.matches(regex)) usbErrored = true;
375 }
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800376 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800377
378 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800379 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
380 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800381 mUsbTether.setChecked(true);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800382 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800383 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
384 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
385 } else {
386 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
387 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800388 mUsbTether.setEnabled(true);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800389 mUsbTether.setChecked(false);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800390 } else if (usbErrored) {
391 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
392 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800393 mUsbTether.setChecked(false);
Mike Lockwood69a09572011-07-19 13:30:03 -0700394 } else if (mMassStorageActive) {
Robert Greenwalta2488762010-03-10 16:57:08 -0800395 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
396 mUsbTether.setEnabled(false);
397 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800398 } else {
399 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
400 mUsbTether.setEnabled(false);
Robert Greenwalt204e7c12010-03-05 19:00:30 -0800401 mUsbTether.setChecked(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800402 }
403 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800404
Danica Chang32711b62010-08-10 18:41:29 -0700405 private void updateBluetoothState(String[] available, String[] tethered,
406 String[] errored) {
Jake Hamby436b29e2011-02-07 18:21:25 -0800407 boolean bluetoothErrored = false;
Danica Chang32711b62010-08-10 18:41:29 -0700408 for (String s: errored) {
409 for (String regex : mBluetoothRegexs) {
410 if (s.matches(regex)) bluetoothErrored = true;
411 }
412 }
413
414 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Jianzheng Zhou508c7c12013-08-15 14:34:52 +0800415 if (adapter == null)
416 return;
Danica Chang32711b62010-08-10 18:41:29 -0700417 int btState = adapter.getState();
418 if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
419 mBluetoothTether.setEnabled(false);
Ryan Baxter232e6fb2013-03-06 18:27:53 -0500420 mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
Danica Chang32711b62010-08-10 18:41:29 -0700421 } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
422 mBluetoothTether.setEnabled(false);
Danica Chang32711b62010-08-10 18:41:29 -0700423 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
Danica Chang32711b62010-08-10 18:41:29 -0700424 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700425 BluetoothPan bluetoothPan = mBluetoothPan.get();
426 if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
427 bluetoothPan.isTetheringOn()) {
428 mBluetoothTether.setChecked(true);
429 mBluetoothTether.setEnabled(true);
430 int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
431 if (bluetoothTethered > 1) {
432 String summary = getString(
433 R.string.bluetooth_tethering_devices_connected_subtext,
434 bluetoothTethered);
435 mBluetoothTether.setSummary(summary);
436 } else if (bluetoothTethered == 1) {
437 mBluetoothTether.setSummary(
438 R.string.bluetooth_tethering_device_connected_subtext);
439 } else if (bluetoothErrored) {
440 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
441 } else {
442 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
443 }
444 } else {
445 mBluetoothTether.setEnabled(true);
446 mBluetoothTether.setChecked(false);
447 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
448 }
Danica Chang32711b62010-08-10 18:41:29 -0700449 }
450 }
451
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700452 public boolean onPreferenceChange(Preference preference, Object value) {
453 boolean enable = (Boolean) value;
454
455 if (enable) {
Jason Monk4896c012015-02-11 13:30:41 -0500456 startProvisioningIfNecessary(TETHERING_WIFI);
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700457 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500458 if (TetherUtil.isProvisioningNeeded(getActivity())) {
459 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_WIFI);
Jason Monk37832d62014-12-10 17:21:51 -0500460 }
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700461 mWifiApEnabler.setSoftapEnabled(false);
462 }
463 return false;
464 }
465
PauloftheWest29aab7a2014-09-26 16:05:44 -0700466 public static boolean isProvisioningNeededButUnavailable(Context context) {
Jason Monk4896c012015-02-11 13:30:41 -0500467 return (TetherUtil.isProvisioningNeeded(context)
468 && !isIntentAvailable(context));
PauloftheWest29aab7a2014-09-26 16:05:44 -0700469 }
470
Jason Monk4896c012015-02-11 13:30:41 -0500471 private static boolean isIntentAvailable(Context context) {
472 String[] provisionApp = context.getResources().getStringArray(
473 com.android.internal.R.array.config_mobile_hotspot_provision_app);
PauloftheWest29aab7a2014-09-26 16:05:44 -0700474 final PackageManager packageManager = context.getPackageManager();
475 Intent intent = new Intent(Intent.ACTION_MAIN);
476 intent.setClassName(provisionApp[0], provisionApp[1]);
477
478 return (packageManager.queryIntentActivities(intent,
479 PackageManager.MATCH_DEFAULT_ONLY).size() > 0);
480 }
481
Irfan Sheriff01b32362011-11-04 12:08:56 -0700482 private void startProvisioningIfNecessary(int choice) {
483 mTetherChoice = choice;
Jason Monk4896c012015-02-11 13:30:41 -0500484 if (TetherUtil.isProvisioningNeeded(getActivity())) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700485 Intent intent = new Intent(Intent.ACTION_MAIN);
486 intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
Rohit Sisodia37155e92014-09-22 16:34:56 -0500487 intent.putExtra(TETHER_CHOICE, mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700488 startActivityForResult(intent, PROVISION_REQUEST);
489 } else {
490 startTethering();
491 }
492 }
493
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700494 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
495 super.onActivityResult(requestCode, resultCode, intent);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700496 if (requestCode == PROVISION_REQUEST) {
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700497 if (resultCode == Activity.RESULT_OK) {
Jason Monk37832d62014-12-10 17:21:51 -0500498 TetherService.scheduleRecheckAlarm(getActivity(), mTetherChoice);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700499 startTethering();
500 } else {
PauloftheWest656c88c2014-08-24 11:59:35 -0700501 //BT and USB need switch turned off on failure
Irfan Sheriff01b32362011-11-04 12:08:56 -0700502 //Wifi tethering is never turned on until afterwards
503 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500504 case TETHERING_BLUETOOTH:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700505 mBluetoothTether.setChecked(false);
506 break;
Jason Monk4896c012015-02-11 13:30:41 -0500507 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700508 mUsbTether.setChecked(false);
509 break;
510 }
Jason Monk4896c012015-02-11 13:30:41 -0500511 mTetherChoice = TETHERING_INVALID;
Irfan Sheriffaa3d2c42011-10-06 11:45:45 -0700512 }
513 }
514 }
515
Irfan Sheriff01b32362011-11-04 12:08:56 -0700516 private void startTethering() {
517 switch (mTetherChoice) {
Jason Monk4896c012015-02-11 13:30:41 -0500518 case TETHERING_WIFI:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700519 mWifiApEnabler.setSoftapEnabled(true);
520 break;
Jason Monk4896c012015-02-11 13:30:41 -0500521 case TETHERING_BLUETOOTH:
Danica Chang32711b62010-08-10 18:41:29 -0700522 // turn on Bluetooth first
523 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
524 if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
Jake Hambyc777ee22011-03-04 15:37:39 -0800525 mBluetoothEnableForTether = true;
Danica Chang32711b62010-08-10 18:41:29 -0700526 adapter.enable();
527 mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
528 mBluetoothTether.setEnabled(false);
Jake Hambyc777ee22011-03-04 15:37:39 -0800529 } else {
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700530 BluetoothPan bluetoothPan = mBluetoothPan.get();
531 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
Jake Hambyc777ee22011-03-04 15:37:39 -0800532 mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
Danica Chang32711b62010-08-10 18:41:29 -0700533 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700534 break;
Jason Monk4896c012015-02-11 13:30:41 -0500535 case TETHERING_USB:
Irfan Sheriff01b32362011-11-04 12:08:56 -0700536 setUsbTethering(true);
537 break;
538 default:
539 //should not happen
540 break;
541 }
542 }
543
544 private void setUsbTethering(boolean enabled) {
545 ConnectivityManager cm =
546 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Li Wenji83397ea2012-02-11 11:52:30 +0800547 mUsbTether.setChecked(false);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700548 if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Irfan Sheriff01b32362011-11-04 12:08:56 -0700549 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
550 return;
551 }
552 mUsbTether.setSummary("");
553 }
554
555 @Override
556 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
557 ConnectivityManager cm =
558 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
559
560 if (preference == mUsbTether) {
561 boolean newState = mUsbTether.isChecked();
562
563 if (newState) {
Jason Monk4896c012015-02-11 13:30:41 -0500564 startProvisioningIfNecessary(TETHERING_USB);
Irfan Sheriff01b32362011-11-04 12:08:56 -0700565 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500566 if (TetherUtil.isProvisioningNeeded(getActivity())) {
567 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_USB);
Jason Monk37832d62014-12-10 17:21:51 -0500568 }
Irfan Sheriff01b32362011-11-04 12:08:56 -0700569 setUsbTethering(newState);
570 }
571 } else if (preference == mBluetoothTether) {
572 boolean bluetoothTetherState = mBluetoothTether.isChecked();
573
574 if (bluetoothTetherState) {
Jason Monk4896c012015-02-11 13:30:41 -0500575 startProvisioningIfNecessary(TETHERING_BLUETOOTH);
Danica Chang32711b62010-08-10 18:41:29 -0700576 } else {
Jason Monk4896c012015-02-11 13:30:41 -0500577 if (TetherUtil.isProvisioningNeeded(getActivity())) {
578 TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_BLUETOOTH);
Jason Monk37832d62014-12-10 17:21:51 -0500579 }
Danica Chang32711b62010-08-10 18:41:29 -0700580 boolean errored = false;
581
Danica Chang32711b62010-08-10 18:41:29 -0700582 String [] tethered = cm.getTetheredIfaces();
583 String bluetoothIface = findIface(tethered, mBluetoothRegexs);
584 if (bluetoothIface != null &&
585 cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
586 errored = true;
587 }
588
Robert Greenwaltf60b92b2012-09-13 15:02:00 -0700589 BluetoothPan bluetoothPan = mBluetoothPan.get();
590 if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
Danica Chang32711b62010-08-10 18:41:29 -0700591 if (errored) {
592 mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
593 } else {
594 mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
595 }
596 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800597 } else if (preference == mCreateNetwork) {
598 showDialog(DIALOG_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800599 }
Daisuke Miyakawa9c602c42010-09-10 12:04:37 -0700600
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700601 return super.onPreferenceTreeClick(screen, preference);
Robert Greenwalt209177a2010-03-04 13:29:02 -0800602 }
603
Jake Hamby436b29e2011-02-07 18:21:25 -0800604 private static String findIface(String[] ifaces, String[] regexes) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800605 for (String iface : ifaces) {
606 for (String regex : regexes) {
607 if (iface.matches(regex)) {
608 return iface;
609 }
610 }
611 }
612 return null;
613 }
Amith Yamasani84a042c2011-03-02 11:25:04 -0800614
615 public void onClick(DialogInterface dialogInterface, int button) {
616 if (button == DialogInterface.BUTTON_POSITIVE) {
617 mWifiConfig = mDialog.getConfig();
618 if (mWifiConfig != null) {
619 /**
Irfan Sheriff233577c2011-07-26 14:01:22 -0700620 * if soft AP is stopped, bring up
621 * else restart with new config
622 * TODO: update config on a running access point when framework support is added
Amith Yamasani84a042c2011-03-02 11:25:04 -0800623 */
624 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Irfan Sheriff233577c2011-07-26 14:01:22 -0700625 mWifiManager.setWifiApEnabled(null, false);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800626 mWifiManager.setWifiApEnabled(mWifiConfig, true);
Amith Yamasani84a042c2011-03-02 11:25:04 -0800627 } else {
628 mWifiManager.setWifiApConfiguration(mWifiConfig);
629 }
630 int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
631 mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
632 mWifiConfig.SSID,
633 mSecurityType[index]));
634 }
635 }
636 }
Amith Yamasanid3fed682012-04-27 17:38:40 -0700637
638 @Override
639 public int getHelpResource() {
640 return R.string.help_url_tether;
641 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800642}