blob: a30f5220ef6e55f67f34ec7cac39ee6c3bade29b [file] [log] [blame]
Robert Greenwalt3901edb2010-01-26 10:22:37 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Irfan Sheriff65cff172010-02-08 10:46:30 -080019import com.android.settings.wifi.WifiApEnabler;
20
Robert Greenwalt3901edb2010-01-26 10:22:37 -080021import android.os.Bundle;
22import android.os.SystemProperties;
23import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.net.ConnectivityManager;
Mike Lockwood26dad3e2010-03-03 06:19:55 -050028import android.os.Environment;
Robert Greenwalt209177a2010-03-04 13:29:02 -080029import android.preference.CheckBoxPreference;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080030import android.preference.Preference;
31import android.preference.PreferenceActivity;
32import android.preference.PreferenceScreen;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080033import android.provider.Settings;
34import android.util.Log;
35
Robert Greenwaltc4764d22010-02-12 14:21:37 -080036import java.util.ArrayList;
Irfan Sheriff65cff172010-02-08 10:46:30 -080037
Robert Greenwalt3901edb2010-01-26 10:22:37 -080038/*
39 * Displays preferences for Tethering.
40 */
41public class TetherSettings extends PreferenceActivity {
Robert Greenwalt3901edb2010-01-26 10:22:37 -080042 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
Irfan Sheriff65cff172010-02-08 10:46:30 -080043 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
44 private static final String WIFI_AP_SETTINGS = "wifi_ap_settings";
Robert Greenwalt3901edb2010-01-26 10:22:37 -080045
Robert Greenwalt209177a2010-03-04 13:29:02 -080046 private CheckBoxPreference mUsbTether;
Robert Greenwalt3901edb2010-01-26 10:22:37 -080047
Irfan Sheriff65cff172010-02-08 10:46:30 -080048 private CheckBoxPreference mEnableWifiAp;
49 private PreferenceScreen mWifiApSettings;
50 private WifiApEnabler mWifiApEnabler;
51
Robert Greenwalt3901edb2010-01-26 10:22:37 -080052 private BroadcastReceiver mTetherChangeReceiver;
53
Robert Greenwaltc4764d22010-02-12 14:21:37 -080054 private String[] mUsbRegexs;
55 private ArrayList mUsbIfaces;
56
57 private String[] mWifiRegexs;
58 private ArrayList mWifiIfaces;
59
Robert Greenwalt3901edb2010-01-26 10:22:37 -080060 @Override
61 protected void onCreate(Bundle icicle) {
62 super.onCreate(icicle);
63
64 addPreferencesFromResource(R.xml.tether_prefs);
65
Irfan Sheriff65cff172010-02-08 10:46:30 -080066 mEnableWifiAp = (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
67 mWifiApSettings = (PreferenceScreen) findPreference(WIFI_AP_SETTINGS);
Robert Greenwalt209177a2010-03-04 13:29:02 -080068 mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
Robert Greenwaltc4764d22010-02-12 14:21:37 -080069
70 ConnectivityManager cm =
71 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
72 mUsbRegexs = cm.getTetherableUsbRegexs();
73 if (mUsbRegexs.length == 0) {
74 getPreferenceScreen().removePreference(mUsbTether);
Robert Greenwaltc4764d22010-02-12 14:21:37 -080075 }
Irfan Sheriff65cff172010-02-08 10:46:30 -080076
Robert Greenwaltc4764d22010-02-12 14:21:37 -080077 mWifiRegexs = cm.getTetherableWifiRegexs();
Irfan Sheriff65cff172010-02-08 10:46:30 -080078 if (mWifiRegexs.length == 0) {
79 getPreferenceScreen().removePreference(mEnableWifiAp);
80 getPreferenceScreen().removePreference(mWifiApSettings);
81 }
82 mWifiApEnabler = new WifiApEnabler(this, mEnableWifiAp);
Robert Greenwalt3901edb2010-01-26 10:22:37 -080083 }
84
Robert Greenwaltc4764d22010-02-12 14:21:37 -080085
Robert Greenwalt3901edb2010-01-26 10:22:37 -080086 private class TetherChangeReceiver extends BroadcastReceiver {
87 public void onReceive(Context content, Intent intent) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -080088 // TODO - this should understand the interface types
89 ArrayList<String> available = intent.getStringArrayListExtra(
90 ConnectivityManager.EXTRA_AVAILABLE_TETHER);
91 ArrayList<String> active = intent.getStringArrayListExtra(
92 ConnectivityManager.EXTRA_ACTIVE_TETHER);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -080093 ArrayList<String> errored = intent.getStringArrayListExtra(
94 ConnectivityManager.EXTRA_ERRORED_TETHER);
Robert Greenwaltc4764d22010-02-12 14:21:37 -080095
Robert Greenwalt209177a2010-03-04 13:29:02 -080096 updateState(available.toArray(), active.toArray(), errored.toArray());
Robert Greenwalt3901edb2010-01-26 10:22:37 -080097 }
98 }
99
100 @Override
101 protected void onResume() {
102 super.onResume();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800103
104 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Mike Lockwood26dad3e2010-03-03 06:19:55 -0500105 filter.addAction(Intent.ACTION_MEDIA_SHARED);
106 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800107 mTetherChangeReceiver = new TetherChangeReceiver();
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800108 Intent intent = registerReceiver(mTetherChangeReceiver, filter);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800109
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800110 if (intent != null) mTetherChangeReceiver.onReceive(this, intent);
Irfan Sheriff65cff172010-02-08 10:46:30 -0800111 mWifiApEnabler.resume();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800112 }
113
114 @Override
115 protected void onPause() {
116 super.onPause();
117 unregisterReceiver(mTetherChangeReceiver);
118 mTetherChangeReceiver = null;
Irfan Sheriff65cff172010-02-08 10:46:30 -0800119 mWifiApEnabler.pause();
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800120 }
121
Robert Greenwalt209177a2010-03-04 13:29:02 -0800122 private void updateState() {
123 ConnectivityManager cm =
124 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
125
126 String[] available = cm.getTetherableIfaces();
127 String[] tethered = cm.getTetheredIfaces();
128 String[] errored = cm.getTetheringErroredIfaces();
129 updateState(available, tethered, errored);
130 }
131
132 private void updateState(Object[] available, Object[] tethered,
133 Object[] errored) {
134 ConnectivityManager cm =
135 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800136 boolean usbTethered = false;
137 boolean usbAvailable = false;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800138 int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800139 boolean usbErrored = false;
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800140 boolean wifiTethered = false;
141 boolean wifiAvailable = false;
Robert Greenwalt209177a2010-03-04 13:29:02 -0800142 int wifiError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
Mike Lockwood26dad3e2010-03-03 06:19:55 -0500143 boolean massStorageActive =
144 Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800145 boolean wifiErrored = false;
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800146
Robert Greenwalt209177a2010-03-04 13:29:02 -0800147 for (Object o : available) {
148 String s = (String)o;
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800149 for (String regex : mUsbRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800150 if (s.matches(regex)) {
151 usbAvailable = true;
152 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
153 usbError = cm.getLastTetherError(s);
154 }
155 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800156 }
157 for (String regex : mWifiRegexs) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800158 if (s.matches(regex)) {
159 wifiAvailable = true;
160 if (wifiError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
161 wifiError = cm.getLastTetherError(s);
162 }
163 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800164 }
165 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800166 for (Object o : tethered) {
167 String s = (String)o;
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800168 for (String regex : mUsbRegexs) {
169 if (s.matches(regex)) usbTethered = true;
170 }
171 for (String regex : mWifiRegexs) {
172 if (s.matches(regex)) wifiTethered = true;
173 }
174 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800175 for (Object o: errored) {
176 String s = (String)o;
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800177 for (String regex : mUsbRegexs) {
178 if (s.matches(regex)) usbErrored = true;
179 }
180 for (String regex : mWifiRegexs) {
181 if (s.matches(regex)) wifiErrored = true;
182 }
183 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800184
Robert Greenwalt209177a2010-03-04 13:29:02 -0800185 if (usbTethered || wifiTethered) {
186// showTetheredNotification();
187 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800188 if (usbTethered) {
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800189 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
190 mUsbTether.setEnabled(true);
Mike Lockwood26dad3e2010-03-03 06:19:55 -0500191 } else if (massStorageActive) {
192 mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
193 mUsbTether.setEnabled(false);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800194 } else if (usbAvailable) {
Robert Greenwalt209177a2010-03-04 13:29:02 -0800195 if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
196 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
197 } else {
198 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
199 }
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800200 mUsbTether.setEnabled(true);
Robert Greenwaltd5f121c2010-03-02 17:33:11 -0800201 } else if (usbErrored) {
202 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
203 mUsbTether.setEnabled(false);
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800204 } else {
205 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
206 mUsbTether.setEnabled(false);
207 }
208 }
Robert Greenwalt209177a2010-03-04 13:29:02 -0800209
210 @Override
211 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
212 if (preference == mUsbTether) {
213 boolean newState = mUsbTether.isChecked();
214
215 ConnectivityManager cm =
216 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
217
218 if (newState) {
219 String[] available = cm.getTetherableIfaces();
220
221 String usbIface = findIface(available, mUsbRegexs);
222 if (usbIface == null) {
223 updateState();
224 return true;
225 }
226 if (cm.tether(usbIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
227 mUsbTether.setChecked(false);
228 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
229 return true;
230 }
231 mUsbTether.setSummary("");
232 } else {
233 String [] tethered = cm.getTetheredIfaces();
234
235 String usbIface = findIface(tethered, mUsbRegexs);
236 if (usbIface == null) {
237 updateState();
238 return true;
239 }
240 if (cm.untether(usbIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
241 mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
242 return true;
243 }
244 mUsbTether.setSummary("");
245 }
246 }
247 return false;
248 }
249
250 private String findIface(String[] ifaces, String[] regexes) {
251 for (String iface : ifaces) {
252 for (String regex : regexes) {
253 if (iface.matches(regex)) {
254 return iface;
255 }
256 }
257 }
258 return null;
259 }
260
261// private showTetheredNotification() {
262// NotificationManager notificationManager = (NotificationManager)mContext.
263// getSystemService(Context.NOTIFICATION_SERVICE);
264// if (notificationManager == null) {
265// return;
266// }
267//
268// Intent intent = new Intent();
269//
270// PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
271//
272// Resources r = Resources.getSystem();
273// CharSequence title = r.getText(com.android.internal.R.string.
274// tether_stop_notification_title);
275// CharSequence message = r.getText(com.android.internal.R.string.
276// tether_stop_notification_message);
277//
278// if(mTetheringNotification == null) {
279// mTetheringNotification = new Notification();
280// mTetheringNotification.when = 0;
281// }
282// mTetheringNotification.icon = com.android.internal.R.drawable.stat_sys_tether_usb;
283//
284// boolean playSounds = false;
285// //playSounds = SystemProperties.get("persist.service.mount.playsnd", "1").equals("1");
286// if (playSounds) {
287// mTetheringNotification.defaults |= Notification.DEFAULT_SOUND;
288// } else {
289// mTetheringNotification.defaults &= ~Notification.DEFAULT_SOUND;
290// }
291// mTetheringNotification.flags = Notification.FLAG_ONGOING_EVENT;
292// mTetheringNotification.tickerText = title;
293// mTetheringNotification.setLatestEventInfo(mContext, title, message, pi);
294//
295// notificationManager.notify(mTetheringNotification.icon, mTetheringNotification);
296// }
297
Robert Greenwalt3901edb2010-01-26 10:22:37 -0800298}