Added preferences for configuring auto-download

This commit is contained in:
daniel oeh 2013-03-08 20:56:24 +01:00
parent 7f81f8a438
commit 3fde2349f9
6 changed files with 182 additions and 14 deletions

View File

@ -23,6 +23,7 @@
<uses-feature
android:name="android.hardware.screen.portrait"
android:required="false" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:name="de.danoeh.antennapod.PodcastApp"

View File

@ -21,6 +21,14 @@
<item>24</item>
</string-array>
<string-array name="autodl_select_networks_default_entries">
<item>N/A</item>
</string-array>
<string-array name="autodl_select_networks_default_values">
<item>0</item>
</string-array>
<string-array name="theme_options">
<item>Light</item>
<item>Dark</item>

View File

@ -175,7 +175,11 @@
<string name="pref_auto_delete_sum">Delete an episode when playback completes or when it is removed from the queue.</string>
<string name="pref_set_theme_title">Select theme</string>
<string name="pref_set_theme_sum">Change the appearance of AntennaPod.</string>
<string name="pref_automatic_download_title">Automatic download</string>
<string name="pref_automatic_download_sum">Configure the automatic download of episodes.</string>
<string name="pref_autodl_wifi_filter_title">Enable Wi-Fi filter</string>
<string name="pref_autodl_wifi_filter_sum">Allow automatic download only for selected Wi-Fi networks.</string>
<!-- Search -->
<string name="search_hint">Search for Feeds or Episodes</string>
<string name="found_in_shownotes_label">Found in shownotes</string>

View File

@ -36,6 +36,9 @@
android:summary="@string/pref_mobileUpdate_sum"
android:title="@string/pref_mobileUpdate_title" />
<CheckBoxPreference android:summary="@string/pref_autoQueue_sum" android:defaultValue="true" android:key="prefAutoQueue" android:title="@string/pref_autoQueue_title"/>
<PreferenceScreen android:summary="@string/pref_automatic_download_sum" android:key="prefAutoDownloadSettings" android:title="@string/pref_automatic_download_title">
<CheckBoxPreference android:key="prefEnableAutoDownloadWifiFilter" android:title="@string/pref_autodl_wifi_filter_title" android:summary="@string/pref_autodl_wifi_filter_sum"/>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="@string/flattr_settings_label" >
<PreferenceScreen

View File

@ -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;
}
});
@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());
}
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]);
}
}
}
}
}

View File

@ -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.