diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-03-08 20:56:24 +0100 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-03-08 20:56:24 +0100 |
commit | 3fde2349f9a859fb3b9b95dbf97ff9f7900774c2 (patch) | |
tree | 49c833ae354b85236b8c0e1296c69f3d58257d94 /src | |
parent | 7f81f8a438a9cbb306306d690731678815c6219d (diff) | |
download | AntennaPod-3fde2349f9a859fb3b9b95dbf97ff9f7900774c2.zip |
Added preferences for configuring auto-download
Diffstat (limited to 'src')
-rw-r--r-- | src/de/danoeh/antennapod/activity/PreferenceActivity.java | 142 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/preferences/UserPreferences.java | 36 |
2 files changed, 165 insertions, 13 deletions
diff --git a/src/de/danoeh/antennapod/activity/PreferenceActivity.java b/src/de/danoeh/antennapod/activity/PreferenceActivity.java index 360ff610d..7df0c17bd 100644 --- a/src/de/danoeh/antennapod/activity/PreferenceActivity.java +++ b/src/de/danoeh/antennapod/activity/PreferenceActivity.java @@ -1,13 +1,22 @@ package de.danoeh.antennapod.activity; import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.Resources.Theme; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; import android.os.Bundle; +import android.preference.CheckBoxPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceScreen; import android.util.Log; import com.actionbarsherlock.app.SherlockPreferenceActivity; @@ -32,6 +41,9 @@ public class PreferenceActivity extends SherlockPreferenceActivity { private static final String PREF_OPML_EXPORT = "prefOpmlExport"; private static final String PREF_ABOUT = "prefAbout"; private static final String PREF_CHOOSE_DATA_DIR = "prefChooseDataDir"; + private static final String AUTO_DL_PREF_SCREEN = "prefAutoDownloadSettings"; + + private CheckBoxPreference[] selectedNetworks; @SuppressWarnings("deprecation") @Override @@ -102,21 +114,48 @@ public class PreferenceActivity extends SherlockPreferenceActivity { return true; } }); - findPreference(UserPreferences.PREF_THEME).setOnPreferenceChangeListener( - new OnPreferenceChangeListener() { + findPreference(UserPreferences.PREF_THEME) + .setOnPreferenceChangeListener( + new OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange( + Preference preference, Object newValue) { + Intent i = getIntent(); + i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK + | Intent.FLAG_ACTIVITY_NEW_TASK); + finish(); + startActivity(i); + return true; + } + }); + findPreference(UserPreferences.PREF_ENABLE_AUTODL_WIFI_FILTER) + .setOnPreferenceChangeListener( + new OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange( + Preference preference, Object newValue) { + if (newValue instanceof Boolean) { + setSelectedNetworksEnabled((Boolean) newValue); + return true; + } else { + return false; + } + } + }); + buildAutodownloadSelectedNetworsPreference(); + setSelectedNetworksEnabled(UserPreferences + .isEnableAutodownloadWifiFilter()); - @Override - public boolean onPreferenceChange(Preference preference, - Object newValue) { - Intent i = getIntent(); - i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK - | Intent.FLAG_ACTIVITY_NEW_TASK); - finish(); - startActivity(i); - return true; - } - }); + } + private void setSelectedNetworksEnabled(boolean b) { + if (selectedNetworks != null) { + for (Preference p : selectedNetworks) { + p.setEnabled(b); + } + } } @Override @@ -180,4 +219,81 @@ public class PreferenceActivity extends SherlockPreferenceActivity { } } + private void buildAutodownloadSelectedNetworsPreference() { + if (selectedNetworks != null) { + clearAutodownloadSelectedNetworsPreference(); + } + // get configured networks + WifiManager wifiservice = (WifiManager) getSystemService(Context.WIFI_SERVICE); + List<WifiConfiguration> networks = wifiservice.getConfiguredNetworks(); + + if (networks != null) { + selectedNetworks = new CheckBoxPreference[networks.size()]; + List<String> prefValues = Arrays.asList(UserPreferences + .getAutodownloadSelectedNetworks()); + PreferenceScreen prefScreen = (PreferenceScreen) findPreference(AUTO_DL_PREF_SCREEN); + OnPreferenceClickListener clickListener = new OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference preference) { + if (preference instanceof CheckBoxPreference) { + String key = preference.getKey(); + ArrayList<String> prefValuesList = new ArrayList<String>( + Arrays.asList(UserPreferences + .getAutodownloadSelectedNetworks())); + boolean newValue = ((CheckBoxPreference) preference) + .isChecked(); + if (AppConfig.DEBUG) + Log.d(TAG, "Selected network " + key + + ". New state: " + newValue); + + int index = prefValuesList.indexOf(key); + if (index >= 0 && newValue == false) { + // remove network + prefValuesList.remove(index); + } else if (index < 0 && newValue == true) { + prefValuesList.add(key); + } + + UserPreferences.setAutodownloadSelectedNetworks( + PreferenceActivity.this, prefValuesList + .toArray(new String[prefValuesList + .size()])); + return true; + } else { + return false; + } + } + }; + // create preference for each known network. attach listener and set + // value + for (int i = 0; i < networks.size(); i++) { + WifiConfiguration config = networks.get(i); + + CheckBoxPreference pref = new CheckBoxPreference(this); + String key = Integer.toString(config.networkId); + pref.setTitle(config.SSID); + pref.setKey(key); + pref.setOnPreferenceClickListener(clickListener); + pref.setPersistent(false); + pref.setChecked(prefValues.contains(key)); + selectedNetworks[i] = pref; + prefScreen.addPreference(pref); + } + } else { + Log.e(TAG, "Couldn't get list of configure Wi-Fi networks"); + } + } + + private void clearAutodownloadSelectedNetworsPreference() { + if (selectedNetworks != null) { + PreferenceScreen prefScreen = (PreferenceScreen) findPreference(AUTO_DL_PREF_SCREEN); + + for (int i = 0; i < selectedNetworks.length; i++) { + if (selectedNetworks[i] != null) { + prefScreen.removePreference(selectedNetworks[i]); + } + } + } + } } diff --git a/src/de/danoeh/antennapod/preferences/UserPreferences.java b/src/de/danoeh/antennapod/preferences/UserPreferences.java index f4c0b94b0..72fd7a46a 100644 --- a/src/de/danoeh/antennapod/preferences/UserPreferences.java +++ b/src/de/danoeh/antennapod/preferences/UserPreferences.java @@ -2,8 +2,11 @@ package de.danoeh.antennapod.preferences; import java.io.File; import java.io.IOException; +import java.util.Set; import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.StringUtils; + import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; @@ -36,6 +39,8 @@ public class UserPreferences implements public static final String PREF_AUTO_DELETE = "prefAutoDelete"; public static final String PREF_THEME = "prefTheme"; public static final String PREF_DATA_FOLDER = "prefDataFolder"; + public static final String PREF_ENABLE_AUTODL_WIFI_FILTER = "prefEnableAutoDownloadWifiFilter"; + private static final String PREF_AUTODL_SELECTED_NETWORKS = "prefAutodownloadSelectedNetworks"; private static UserPreferences instance; private Context context; @@ -50,6 +55,8 @@ public class UserPreferences implements private boolean displayOnlyEpisodes; private boolean autoDelete; private int theme; + private boolean enableAutodownloadWifiFilter; + private String[] autodownloadSelectedNetworks; private UserPreferences(Context context) { this.context = context; @@ -90,6 +97,10 @@ public class UserPreferences implements displayOnlyEpisodes = sp.getBoolean(PREF_DISPLAY_ONLY_EPISODES, false); autoDelete = sp.getBoolean(PREF_AUTO_DELETE, false); theme = readThemeValue(sp.getString(PREF_THEME, "0")); + enableAutodownloadWifiFilter = sp.getBoolean( + PREF_ENABLE_AUTODL_WIFI_FILTER, false); + autodownloadSelectedNetworks = StringUtils.split( + sp.getString(PREF_AUTODL_SELECTED_NETWORKS, ""), ','); } private int readThemeValue(String valueFromPrefs) { @@ -160,6 +171,16 @@ public class UserPreferences implements return instance.theme; } + public static boolean isEnableAutodownloadWifiFilter() { + instanceAvailable(); + return instance.enableAutodownloadWifiFilter; + } + + public static String[] getAutodownloadSelectedNetworks() { + instanceAvailable(); + return instance.autodownloadSelectedNetworks; + } + @Override public void onSharedPreferenceChanged(SharedPreferences sp, String key) { if (AppConfig.DEBUG) @@ -204,9 +225,24 @@ public class UserPreferences implements false); } else if (key.equals(PREF_THEME)) { theme = readThemeValue(sp.getString(PREF_THEME, "")); + } else if (key.equals(PREF_ENABLE_AUTODL_WIFI_FILTER)) { + enableAutodownloadWifiFilter = sp.getBoolean( + PREF_ENABLE_AUTODL_WIFI_FILTER, false); + } else if (key.equals(PREF_AUTODL_SELECTED_NETWORKS)) { + autodownloadSelectedNetworks = StringUtils.split( + sp.getString(PREF_AUTODL_SELECTED_NETWORKS, ""), ','); } } + public static void setAutodownloadSelectedNetworks(Context context, String[] value) { + SharedPreferences.Editor editor = PreferenceManager + .getDefaultSharedPreferences(context.getApplicationContext()) + .edit(); + editor.putString(PREF_AUTODL_SELECTED_NETWORKS, + StringUtils.join(value, ',')); + editor.commit(); + } + /** * Return the folder where the app stores all of its data. This method will * return the standard data folder if none has been set by the user. |