TetherSettings.java revision d5f121c66ab83f5304fca37c14ac519fcd530fb8
13901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt/*
23901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * Copyright (C) 2008 The Android Open Source Project
33901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt *
43901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
53901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * you may not use this file except in compliance with the License.
63901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * You may obtain a copy of the License at
73901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt *
83901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
93901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt *
103901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
113901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
123901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * See the License for the specific language governing permissions and
143901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * limitations under the License.
153901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt */
163901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
173901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltpackage com.android.settings;
183901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
193901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.os.Bundle;
203901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.os.SystemProperties;
213901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.content.BroadcastReceiver;
223901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.content.Context;
233901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.content.Intent;
243901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.content.IntentFilter;
253901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.net.ConnectivityManager;
2626dad3ecef45ffb75178fc2fe184307089815510Mike Lockwoodimport android.os.Environment;
273901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.preference.Preference;
283901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.preference.PreferenceActivity;
293901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.preference.PreferenceScreen;
303901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.preference.CheckBoxPreference;
313901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.provider.Settings;
323901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltimport android.util.Log;
333901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
34c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwaltimport java.util.ArrayList;
353901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt/*
363901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt * Displays preferences for Tethering.
373901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt */
383901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwaltpublic class TetherSettings extends PreferenceActivity {
393901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
403901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    private static final String ENABLE_TETHER_NOTICE = "enable_tether_notice";
413901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    private static final String USB_TETHER_SETTINGS = "usb_tether_settings";
423901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
433901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    private CheckBoxPreference mEnableTetherNotice;
443901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    private PreferenceScreen mUsbTether;
453901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
463901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    private BroadcastReceiver mTetherChangeReceiver;
473901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
48c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt    private String[] mUsbRegexs;
49c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt    private ArrayList mUsbIfaces;
50c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt
51c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt    private String[] mWifiRegexs;
52c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt    private ArrayList mWifiIfaces;
53c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt
543901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    @Override
553901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    protected void onCreate(Bundle icicle) {
563901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        super.onCreate(icicle);
573901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
583901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        addPreferencesFromResource(R.xml.tether_prefs);
593901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
603901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        mEnableTetherNotice = (CheckBoxPreference) findPreference(ENABLE_TETHER_NOTICE);
613901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        mUsbTether = (PreferenceScreen) findPreference(USB_TETHER_SETTINGS);
62c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt
63c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        ConnectivityManager cm =
64c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
65c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        mUsbRegexs = cm.getTetherableUsbRegexs();
66c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        if (mUsbRegexs.length == 0) {
67c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            getPreferenceScreen().removePreference(mUsbTether);
68c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            getPreferenceScreen().removePreference(mEnableTetherNotice);
69c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        }
70c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        mWifiRegexs = cm.getTetherableWifiRegexs();
713901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    }
723901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
73c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt
743901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    private class TetherChangeReceiver extends BroadcastReceiver {
753901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        public void onReceive(Context content, Intent intent) {
76c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            // TODO - this should understand the interface types
77c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            ArrayList<String> available = intent.getStringArrayListExtra(
78c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt                    ConnectivityManager.EXTRA_AVAILABLE_TETHER);
79c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            ArrayList<String> active = intent.getStringArrayListExtra(
80c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt                    ConnectivityManager.EXTRA_ACTIVE_TETHER);
81d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt            ArrayList<String> errored = intent.getStringArrayListExtra(
82d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt                    ConnectivityManager.EXTRA_ERRORED_TETHER);
83c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt
84d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt            updateState(available, active, errored);
853901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        }
863901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    }
873901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
883901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    @Override
893901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    protected void onResume() {
903901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        super.onResume();
913901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        mEnableTetherNotice.setChecked(Settings.Secure.getInt(getContentResolver(),
923901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt                Settings.Secure.TETHER_NOTIFY, 0) != 0);
933901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
943901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
9526dad3ecef45ffb75178fc2fe184307089815510Mike Lockwood        filter.addAction(Intent.ACTION_MEDIA_SHARED);
9626dad3ecef45ffb75178fc2fe184307089815510Mike Lockwood        filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
973901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        mTetherChangeReceiver = new TetherChangeReceiver();
98c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        Intent intent = registerReceiver(mTetherChangeReceiver, filter);
993901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
100c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        if (intent != null) mTetherChangeReceiver.onReceive(this, intent);
1013901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    }
1023901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
1033901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    @Override
1043901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    protected void onPause() {
1053901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        super.onPause();
1063901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        unregisterReceiver(mTetherChangeReceiver);
1073901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        mTetherChangeReceiver = null;
1083901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    }
1093901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
110d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt    private void updateState(ArrayList<String> available, ArrayList<String> tethered,
111d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt            ArrayList<String> errored) {
112c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        boolean usbTethered = false;
113c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        boolean usbAvailable = false;
114d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt        boolean usbErrored = false;
115c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        boolean wifiTethered = false;
116c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        boolean wifiAvailable = false;
11726dad3ecef45ffb75178fc2fe184307089815510Mike Lockwood        boolean massStorageActive =
11826dad3ecef45ffb75178fc2fe184307089815510Mike Lockwood                Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
119d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt        boolean wifiErrored = false;
120c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt
121c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        for (String s : available) {
122c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            for (String regex : mUsbRegexs) {
123c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt                if (s.matches(regex)) usbAvailable = true;
124c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            }
125c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            for (String regex : mWifiRegexs) {
126c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt                if (s.matches(regex)) wifiAvailable = true;
127c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            }
128c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        }
129c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        for (String s : tethered) {
130c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            for (String regex : mUsbRegexs) {
131c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt                if (s.matches(regex)) usbTethered = true;
132c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            }
133c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            for (String regex : mWifiRegexs) {
134c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt                if (s.matches(regex)) wifiTethered = true;
135c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt            }
136c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        }
137d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt        for (String s: errored) {
138d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt            for (String regex : mUsbRegexs) {
139d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt                if (s.matches(regex)) usbErrored = true;
140d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt            }
141d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt            for (String regex : mWifiRegexs) {
142d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt                if (s.matches(regex)) wifiErrored = true;
143d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt            }
144d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt        }
145c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt
146c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        if (usbTethered) {
1473901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt            mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
1483901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt            mUsbTether.setEnabled(true);
14926dad3ecef45ffb75178fc2fe184307089815510Mike Lockwood        } else if (massStorageActive) {
15026dad3ecef45ffb75178fc2fe184307089815510Mike Lockwood            mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
15126dad3ecef45ffb75178fc2fe184307089815510Mike Lockwood            mUsbTether.setEnabled(false);
152c4764d24837cf297440f0ee97fe8adb943c56fe0Robert Greenwalt        } else if (usbAvailable) {
1533901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt            mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
1543901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt            mUsbTether.setEnabled(true);
155d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt        } else if (usbErrored) {
156d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt            mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
157d5f121c66ab83f5304fca37c14ac519fcd530fb8Robert Greenwalt            mUsbTether.setEnabled(false);
1583901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        } else {
1593901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt            mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
1603901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt            mUsbTether.setEnabled(false);
1613901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        }
1623901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    }
1633901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
1643901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    @Override
1653901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
1663901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
1673901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        if (preference == mEnableTetherNotice) {
1683901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt            boolean newState = mEnableTetherNotice.isChecked();
1693901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt            Settings.Secure.putInt(getContentResolver(),
1703901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt                    Settings.Secure.TETHER_NOTIFY, newState ? 1 : 0);
1713901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt            return true;
1723901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        }
1733901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt        return false;
1743901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt    }
1753901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt
1763901edbdf5cb7b14d4773b3fc7adb3181519855cRobert Greenwalt}
177