Added data folder preference

This commit is contained in:
daniel oeh 2012-12-26 00:39:35 +01:00
parent 67622d447d
commit 0e5ffeb9ca
6 changed files with 141 additions and 33 deletions

View File

@ -37,6 +37,7 @@ public class PodcastApp extends Application implements
public static final String PREF_DISPLAY_ONLY_EPISODES = "prefDisplayOnlyEpisodes"; public static final String PREF_DISPLAY_ONLY_EPISODES = "prefDisplayOnlyEpisodes";
public static final String PREF_AUTO_DELETE = "prefAutoDelete"; public static final String PREF_AUTO_DELETE = "prefAutoDelete";
public static final String PREF_THEME = "prefTheme"; public static final String PREF_THEME = "prefTheme";
public static final String PREF_DATA_FOLDER = "prefDataFolder";
private static float LOGICAL_DENSITY; private static float LOGICAL_DENSITY;
@ -93,7 +94,7 @@ public class PodcastApp extends Application implements
* available * available
*/ */
private void createImportDirectory() { private void createImportDirectory() {
File importDir = getExternalFilesDir(OpmlImportActivity.IMPORT_DIR); File importDir = getDataFolder(this, OpmlImportActivity.IMPORT_DIR);
if (importDir != null) { if (importDir != null) {
if (importDir.exists()) { if (importDir.exists()) {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
@ -197,7 +198,8 @@ public class PodcastApp extends Application implements
/** Read value of prefTheme and determine the correct resource id. */ /** Read value of prefTheme and determine the correct resource id. */
private void readThemeValue() { private void readThemeValue() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
int prefTheme = Integer.parseInt(prefs.getString(PREF_THEME, "0")); int prefTheme = Integer.parseInt(prefs.getString(PREF_THEME, "0"));
switch (prefTheme) { switch (prefTheme) {
case 0: case 0:
@ -208,4 +210,60 @@ public class PodcastApp extends Application implements
break; break;
} }
} }
/**
* 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.
*
* @param type
* The name of the folder inside the data folder. May be null
* when accessing the root of the data folder.
* @return The data folder that has been requested or null if the folder
* could not be created.
*/
public static File getDataFolder(Context context, String type) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
String strDir = prefs.getString(PREF_DATA_FOLDER, null);
if (strDir == null) {
if (AppConfig.DEBUG)
Log.d(TAG, "Using default data folder");
return context.getExternalFilesDir(type);
} else {
File dataDir = new File(strDir);
if (!dataDir.exists()) {
if (!dataDir.mkdir()) {
Log.w(TAG, "Could not create data folder");
return null;
}
}
if (type == null) {
return dataDir;
} else {
File typeDir = new File(dataDir, type);
if (!typeDir.exists()) {
if (dataDir.canWrite()) {
if (!typeDir.mkdir()) {
Log.e(TAG, "Could not create data folder named "
+ type);
return null;
}
}
}
return typeDir;
}
}
}
public void setDataFolder(String dir) {
if (AppConfig.DEBUG)
Log.d(TAG, "Result from DirectoryChooser: " + dir);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PodcastApp.PREF_DATA_FOLDER, dir);
editor.commit();
}
} }

View File

@ -5,9 +5,10 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.FileObserver; import android.os.FileObserver;
@ -20,7 +21,6 @@ import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -42,6 +42,9 @@ public class DirectoryChooserActivity extends SherlockActivity {
private static final String CREATE_DIRECTORY_NAME = "AntennaPod"; private static final String CREATE_DIRECTORY_NAME = "AntennaPod";
public static final String RESULT_SELECTED_DIR = "selected_dir";
public static final int RESULT_CODE_DIR_SELECTED = 1;
private Button butConfirm; private Button butConfirm;
private Button butCancel; private Button butCancel;
private ImageButton butNavUp; private ImageButton butNavUp;
@ -73,6 +76,13 @@ public class DirectoryChooserActivity extends SherlockActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (AppConfig.DEBUG)
Log.d(TAG, "Returning " + selectedDir.getAbsolutePath()
+ " as result");
Intent resultData = new Intent();
resultData.putExtra(RESULT_SELECTED_DIR,
selectedDir.getAbsolutePath());
setResult(RESULT_CODE_DIR_SELECTED, resultData);
finish(); finish();
} }
}); });
@ -81,6 +91,7 @@ public class DirectoryChooserActivity extends SherlockActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
setResult(Activity.RESULT_CANCELED);
finish(); finish();
} }
}); });

View File

@ -72,7 +72,7 @@ public class OpmlImportActivity extends SherlockActivity {
* directory. * directory.
*/ */
private void setImportPath() { private void setImportPath() {
File importDir = getExternalFilesDir(IMPORT_DIR); File importDir = PodcastApp.getDataFolder(this, IMPORT_DIR);
boolean success = true; boolean success = true;
if (!importDir.exists()) { if (!importDir.exists()) {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)

View File

@ -1,16 +1,23 @@
package de.danoeh.antennapod.activity; package de.danoeh.antennapod.activity;
import java.io.File;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources.Theme; import android.content.res.Resources.Theme;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener; import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceManager;
import android.util.Log;
import com.actionbarsherlock.app.SherlockPreferenceActivity; import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FlattrClickWorker; import de.danoeh.antennapod.asynctask.FlattrClickWorker;
@ -86,20 +93,27 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
} }
}); });
findPreference(PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener(new OnPreferenceClickListener() { findPreference(PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener(
new OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
startActivity(new Intent(PreferenceActivity.this, DirectoryChooserActivity.class)); startActivityForResult(
new Intent(PreferenceActivity.this,
DirectoryChooserActivity.class),
DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED);
return true; return true;
} }
}); });
findPreference(PodcastApp.PREF_THEME).setOnPreferenceChangeListener(new OnPreferenceChangeListener() { findPreference(PodcastApp.PREF_THEME).setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference,
Object newValue) {
Intent i = getIntent(); Intent i = getIntent();
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
finish(); finish();
startActivity(i); startActivity(i);
return true; return true;
@ -112,6 +126,7 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
checkItemVisibility(); checkItemVisibility();
setDataFolderText();
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -124,6 +139,14 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
} }
private void setDataFolderText() {
File f = PodcastApp.getDataFolder(this, null);
if (f != null) {
findPreference(PREF_CHOOSE_DATA_DIR)
.setSummary(f.getAbsolutePath());
}
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
return true; return true;
@ -148,6 +171,21 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
theme.applyStyle(PodcastApp.getThemeResourceId(), true); theme.applyStyle(PodcastApp.getThemeResourceId(), true);
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) {
String dir = data
.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR);
if (dir != null) {
if (AppConfig.DEBUG)
Log.d(TAG, "Setting data folder");
PodcastApp.getInstance().setDataFolder(dir);
} else {
if (AppConfig.DEBUG)
Log.d(TAG, "Result from DirectoryChooser was null");
}
}
}
} }

View File

@ -40,7 +40,7 @@ public class OpmlExportWorker extends AsyncTask<Void, Void, Void> {
OpmlWriter opmlWriter = new OpmlWriter(); OpmlWriter opmlWriter = new OpmlWriter();
if (output == null) { if (output == null) {
output = new File( output = new File(
context.getExternalFilesDir(PodcastApp.EXPORT_DIR), PodcastApp.getDataFolder(context, PodcastApp.EXPORT_DIR),
DEFAULT_OUTPUT_NAME); DEFAULT_OUTPUT_NAME);
if (output.exists()) { if (output.exists()) {
Log.w(TAG, "Overwriting previously exported file."); Log.w(TAG, "Overwriting previously exported file.");

View File

@ -11,6 +11,7 @@ import android.content.Intent;
import android.util.Log; import android.util.Log;
import android.webkit.URLUtil; import android.webkit.URLUtil;
import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedFile; import de.danoeh.antennapod.feed.FeedFile;
import de.danoeh.antennapod.feed.FeedImage; import de.danoeh.antennapod.feed.FeedImage;
@ -281,7 +282,7 @@ public class DownloadRequester {
private File getExternalFilesDirOrThrowException(Context context, private File getExternalFilesDirOrThrowException(Context context,
String type) throws DownloadRequestException { String type) throws DownloadRequestException {
File result = context.getExternalFilesDir(type); File result = PodcastApp.getDataFolder(context, type);
if (result == null) { if (result == null) {
throw new DownloadRequestException( throw new DownloadRequestException(
"Failed to access external storage"); "Failed to access external storage");