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_AUTO_DELETE = "prefAutoDelete";
public static final String PREF_THEME = "prefTheme";
public static final String PREF_DATA_FOLDER = "prefDataFolder";
private static float LOGICAL_DENSITY;
@ -45,7 +46,7 @@ public class PodcastApp extends Application implements
private boolean displayOnlyEpisodes;
private static long currentlyPlayingMediaId;
/** Resource id of the currently selected theme. */
private static int theme;
@ -93,7 +94,7 @@ public class PodcastApp extends Application implements
* available
*/
private void createImportDirectory() {
File importDir = getExternalFilesDir(OpmlImportActivity.IMPORT_DIR);
File importDir = getDataFolder(this, OpmlImportActivity.IMPORT_DIR);
if (importDir != null) {
if (importDir.exists()) {
if (AppConfig.DEBUG)
@ -190,14 +191,15 @@ public class PodcastApp extends Application implements
|| (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
public static int getThemeResourceId() {
return theme;
}
/** Read value of prefTheme and determine the correct resource id. */
private void readThemeValue() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
int prefTheme = Integer.parseInt(prefs.getString(PREF_THEME, "0"));
switch (prefTheme) {
case 0:
@ -208,4 +210,60 @@ public class PodcastApp extends Application implements
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.Collections;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileObserver;
@ -20,7 +21,6 @@ import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.Toast;
@ -42,6 +42,9 @@ public class DirectoryChooserActivity extends SherlockActivity {
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 butCancel;
private ImageButton butNavUp;
@ -73,6 +76,13 @@ public class DirectoryChooserActivity extends SherlockActivity {
@Override
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();
}
});
@ -81,6 +91,7 @@ public class DirectoryChooserActivity extends SherlockActivity {
@Override
public void onClick(View v) {
setResult(Activity.RESULT_CANCELED);
finish();
}
});

View File

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

View File

@ -1,16 +1,23 @@
package de.danoeh.antennapod.activity;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceManager;
import android.util.Log;
import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FlattrClickWorker;
@ -34,7 +41,7 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
setTheme(PodcastApp.getThemeResourceId());
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
addPreferencesFromResource(R.xml.preferences);
findPreference(PREF_FLATTR_THIS_APP).setOnPreferenceClickListener(
@ -85,26 +92,33 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
return true;
}
});
findPreference(PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivity(new Intent(PreferenceActivity.this, DirectoryChooserActivity.class));
return true;
}
});
findPreference(PodcastApp.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(PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener(
new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(
new Intent(PreferenceActivity.this,
DirectoryChooserActivity.class),
DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED);
return true;
}
});
findPreference(PodcastApp.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;
}
});
}
@ -112,6 +126,7 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
protected void onResume() {
super.onResume();
checkItemVisibility();
setDataFolderText();
}
@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
public boolean onCreateOptionsMenu(Menu menu) {
return true;
@ -147,7 +170,22 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
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();
if (output == null) {
output = new File(
context.getExternalFilesDir(PodcastApp.EXPORT_DIR),
PodcastApp.getDataFolder(context, PodcastApp.EXPORT_DIR),
DEFAULT_OUTPUT_NAME);
if (output.exists()) {
Log.w(TAG, "Overwriting previously exported file.");

View File

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