blob: 0a63de30799e057b400e0727e305e860d00d7c4f [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
19import android.os.Bundle;
20import android.os.SystemProperties;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.net.ConnectivityManager;
26import android.preference.Preference;
27import android.preference.PreferenceActivity;
28import android.preference.PreferenceScreen;
29import android.preference.CheckBoxPreference;
30import android.provider.Settings;
31import android.util.Log;
32
33/*
34 * Displays preferences for Tethering.
35 */
36public class TetherSettings extends PreferenceActivity {
37
38 private static final String ENABLE_TETHER_NOTICE = "enable_tether_notice";
39 private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
40
41 private CheckBoxPreference mEnableTetherNotice;
42 private PreferenceScreen mUsbTether;
43
44 private BroadcastReceiver mTetherChangeReceiver;
45
46 @Override
47 protected void onCreate(Bundle icicle) {
48 super.onCreate(icicle);
49
50 addPreferencesFromResource(R.xml.tether_prefs);
51
52 mEnableTetherNotice = (CheckBoxPreference) findPreference(ENABLE_TETHER_NOTICE);
53 mUsbTether = (PreferenceScreen) findPreference(USB_TETHER_SETTINGS);
54 }
55
56 private class TetherChangeReceiver extends BroadcastReceiver {
57 public void onReceive(Context content, Intent intent) {
58 updateState(intent.getIntExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER_COUNT,0)>0,
59 intent.getIntExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER_COUNT,0)>0);
60 }
61 }
62
63 @Override
64 protected void onResume() {
65 super.onResume();
66 mEnableTetherNotice.setChecked(Settings.Secure.getInt(getContentResolver(),
67 Settings.Secure.TETHER_NOTIFY, 0) != 0);
68
69 IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
70 mTetherChangeReceiver = new TetherChangeReceiver();
71 registerReceiver(mTetherChangeReceiver, filter);
72
73 ConnectivityManager cm =
74 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
75 updateState(cm.getTetherableIfaces().length>0, cm.getTetheredIfaces().length>0);
76 }
77
78 @Override
79 protected void onPause() {
80 super.onPause();
81 unregisterReceiver(mTetherChangeReceiver);
82 mTetherChangeReceiver = null;
83 }
84
85 private void updateState(boolean isAvailable, boolean isTethered) {
86 if (isTethered) {
87 mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
88 mUsbTether.setEnabled(true);
89 } else if (isAvailable) {
90 mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
91 mUsbTether.setEnabled(true);
92 } else {
93 mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
94 mUsbTether.setEnabled(false);
95 }
96 }
97
98 @Override
99 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
100
101 if (preference == mEnableTetherNotice) {
102 boolean newState = mEnableTetherNotice.isChecked();
103 Settings.Secure.putInt(getContentResolver(),
104 Settings.Secure.TETHER_NOTIFY, newState ? 1 : 0);
105 return true;
106 }
107 return false;
108 }
109
110}