diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-02-24 10:46:10 +0100 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-02-24 10:46:10 +0100 |
commit | 56f199dc8c69d7e24d0e05613f9911c323cb46a6 (patch) | |
tree | 6ef20851c871d7ef4f47d5c75df0a8fb7262eb8b | |
parent | cca91b12b9df7c23121b4c01172863ff64d35cfe (diff) | |
download | AntennaPod-56f199dc8c69d7e24d0e05613f9911c323cb46a6.zip |
Added UserPreferences class, limit access to unreadItems and queue
44 files changed, 521 insertions, 363 deletions
diff --git a/src/de/danoeh/antennapod/PodcastApp.java b/src/de/danoeh/antennapod/PodcastApp.java index 0e3fab80d..217a0aed4 100644 --- a/src/de/danoeh/antennapod/PodcastApp.java +++ b/src/de/danoeh/antennapod/PodcastApp.java @@ -1,23 +1,14 @@ package de.danoeh.antennapod; -import java.io.File; -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import android.app.AlarmManager; import android.app.Application; -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.preference.PreferenceManager; import android.util.Log; -import de.danoeh.antennapod.activity.OpmlImportFromPathActivity; import de.danoeh.antennapod.asynctask.FeedImageLoader; import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.feed.FeedMedia; -import de.danoeh.antennapod.receiver.FeedUpdateReceiver; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.PlaybackService; /** Main application class. */ @@ -27,28 +18,12 @@ public class PodcastApp extends Application implements private static final String TAG = "PodcastApp"; public static final String EXPORT_DIR = "export/"; - public static final String PREF_PAUSE_ON_HEADSET_DISCONNECT = "prefPauseOnHeadsetDisconnect"; - public static final String PREF_FOLLOW_QUEUE = "prefFollowQueue"; - public static final String PREF_DOWNLOAD_MEDIA_ON_WIFI_ONLY = "prefDownloadMediaOnWifiOnly"; - public static final String PREF_UPDATE_INTERVALL = "prefAutoUpdateIntervall"; - public static final String PREF_MOBILE_UPDATE = "prefMobileUpdate"; - public static final String PREF_AUTO_QUEUE = "prefAutoQueue"; - 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; private static PodcastApp singleton; - private boolean displayOnlyEpisodes; - private static long currentlyPlayingMediaId; - /** Resource id of the currently selected theme. */ - private static int theme; - public static PodcastApp getInstance() { return singleton; } @@ -60,55 +35,15 @@ public class PodcastApp extends Application implements LOGICAL_DENSITY = getResources().getDisplayMetrics().density; SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(this); - displayOnlyEpisodes = prefs.getBoolean(PREF_DISPLAY_ONLY_EPISODES, - false); currentlyPlayingMediaId = prefs.getLong( PlaybackService.PREF_CURRENTLY_PLAYING_MEDIA, PlaybackService.NO_MEDIA_PLAYING); - readThemeValue(); - createImportDirectory(); - createNoMediaFile(); prefs.registerOnSharedPreferenceChangeListener(this); + UserPreferences.createInstance(this); FeedManager manager = FeedManager.getInstance(); manager.loadDBData(getApplicationContext()); } - /** Create a .nomedia file to prevent scanning by the media scanner. */ - private void createNoMediaFile() { - File f = new File(getExternalFilesDir(null), ".nomedia"); - if (!f.exists()) { - try { - f.createNewFile(); - } catch (IOException e) { - Log.e(TAG, "Could not create .nomedia file"); - e.printStackTrace(); - } - if (AppConfig.DEBUG) - Log.d(TAG, ".nomedia file created"); - } - } - - /** - * Creates the import directory if it doesn't exist and if storage is - * available - */ - private void createImportDirectory() { - File importDir = getDataFolder(this, OpmlImportFromPathActivity.IMPORT_DIR); - if (importDir != null) { - if (importDir.exists()) { - if (AppConfig.DEBUG) - Log.d(TAG, "Import directory already exists"); - } else { - if (AppConfig.DEBUG) - Log.d(TAG, "Creating import directory"); - importDir.mkdir(); - } - } else { - if (AppConfig.DEBUG) - Log.d(TAG, "Could not access external storage."); - } - } - @Override public void onLowMemory() { super.onLowMemory(); @@ -125,29 +60,8 @@ public class PodcastApp extends Application implements String key) { if (AppConfig.DEBUG) Log.d(TAG, "Registered change of application preferences"); - if (key.equals(PREF_UPDATE_INTERVALL)) { - AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); - int hours = Integer.parseInt(sharedPreferences.getString( - PREF_UPDATE_INTERVALL, "0")); - PendingIntent updateIntent = PendingIntent.getBroadcast(this, 0, - new Intent(FeedUpdateReceiver.ACTION_REFRESH_FEEDS), 0); - alarmManager.cancel(updateIntent); - if (hours != 0) { - long newIntervall = TimeUnit.HOURS.toMillis(hours); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, - newIntervall, newIntervall, updateIntent); - if (AppConfig.DEBUG) - Log.d(TAG, "Changed alarm to new intervall"); - } else { - if (AppConfig.DEBUG) - Log.d(TAG, "Automatic update was deactivated"); - } - } else if (key.equals(PREF_DISPLAY_ONLY_EPISODES)) { - if (AppConfig.DEBUG) - Log.d(TAG, "PREF_DISPLAY_ONLY_EPISODES changed"); - displayOnlyEpisodes = sharedPreferences.getBoolean( - PREF_DISPLAY_ONLY_EPISODES, false); - } else if (key.equals(PlaybackService.PREF_LAST_PLAYED_ID)) { + + if (key.equals(PlaybackService.PREF_LAST_PLAYED_ID)) { if (AppConfig.DEBUG) Log.d(TAG, "PREF_LAST_PLAYED_ID changed"); long mediaId = sharedPreferences.getLong( @@ -168,8 +82,6 @@ public class PodcastApp extends Application implements if (id != currentlyPlayingMediaId) { currentlyPlayingMediaId = id; } - } else if (key.equals(PREF_THEME)) { - readThemeValue(); } } @@ -177,10 +89,6 @@ public class PodcastApp extends Application implements return LOGICAL_DENSITY; } - public boolean displayOnlyEpisodes() { - return displayOnlyEpisodes; - } - public static long getCurrentlyPlayingMediaId() { return currentlyPlayingMediaId; } @@ -191,92 +99,4 @@ public class PodcastApp extends Application implements } - public static int getThemeResourceId() { - return theme; - } - - /** Read value of prefTheme and determine the correct resource id. */ - private void readThemeValue() { - SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(this); - int prefTheme = Integer.parseInt(prefs.getString(PREF_THEME, "0")); - switch (prefTheme) { - case 0: - theme = R.style.Theme_AntennaPod_Light; - break; - case 1: - theme = R.style.Theme_AntennaPod_Dark; - 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 { - // handle path separators - String[] dirs = type.split("/"); - for (int i = 0; i < dirs.length; i++) { - if (dirs.length > 0) { - if (i < dirs.length - 1) { - dataDir = getDataFolder(context, dirs[i]); - if (dataDir == null) { - return null; - } - } - type = dirs[i]; - } - } - 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(); - createImportDirectory(); - } } diff --git a/src/de/danoeh/antennapod/activity/AddFeedActivity.java b/src/de/danoeh/antennapod/activity/AddFeedActivity.java index 44486b5ef..39434fa87 100644 --- a/src/de/danoeh/antennapod/activity/AddFeedActivity.java +++ b/src/de/danoeh/antennapod/activity/AddFeedActivity.java @@ -18,9 +18,9 @@ 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.feed.Feed; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.storage.DownloadRequester; import de.danoeh.antennapod.util.ConnectionTester; @@ -44,7 +44,7 @@ public class AddFeedActivity extends SherlockActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); StorageUtils.checkStorageAvailability(this); diff --git a/src/de/danoeh/antennapod/activity/DirectoryChooserActivity.java b/src/de/danoeh/antennapod/activity/DirectoryChooserActivity.java index 6e1163508..54c4f0589 100644 --- a/src/de/danoeh/antennapod/activity/DirectoryChooserActivity.java +++ b/src/de/danoeh/antennapod/activity/DirectoryChooserActivity.java @@ -30,8 +30,8 @@ import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.preferences.UserPreferences; /** * Let's the user choose a directory on the storage device. The selected folder @@ -61,7 +61,7 @@ public class DirectoryChooserActivity extends SherlockActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/src/de/danoeh/antennapod/activity/DownloadActivity.java b/src/de/danoeh/antennapod/activity/DownloadActivity.java index 0c10abefd..10ebb1285 100644 --- a/src/de/danoeh/antennapod/activity/DownloadActivity.java +++ b/src/de/danoeh/antennapod/activity/DownloadActivity.java @@ -22,10 +22,10 @@ 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.adapter.DownloadlistAdapter; import de.danoeh.antennapod.asynctask.DownloadStatus; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.download.DownloadService; import de.danoeh.antennapod.storage.DownloadRequester; @@ -52,7 +52,7 @@ public class DownloadActivity extends SherlockListActivity implements @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); if (AppConfig.DEBUG) Log.d(TAG, "Creating Activity"); diff --git a/src/de/danoeh/antennapod/activity/DownloadLogActivity.java b/src/de/danoeh/antennapod/activity/DownloadLogActivity.java index 45d31cda9..535f585db 100644 --- a/src/de/danoeh/antennapod/activity/DownloadLogActivity.java +++ b/src/de/danoeh/antennapod/activity/DownloadLogActivity.java @@ -10,9 +10,9 @@ import com.actionbarsherlock.app.SherlockListActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.adapter.DownloadLogAdapter; import de.danoeh.antennapod.feed.FeedManager; +import de.danoeh.antennapod.preferences.UserPreferences; /** * Displays completed and failed downloads in a list. The data comes from the @@ -26,7 +26,7 @@ public class DownloadLogActivity extends SherlockListActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); manager = FeedManager.getInstance(); diff --git a/src/de/danoeh/antennapod/activity/FeedInfoActivity.java b/src/de/danoeh/antennapod/activity/FeedInfoActivity.java index 0ff1d7c1c..da6ba7e1c 100644 --- a/src/de/danoeh/antennapod/activity/FeedInfoActivity.java +++ b/src/de/danoeh/antennapod/activity/FeedInfoActivity.java @@ -11,12 +11,12 @@ import com.actionbarsherlock.view.MenuInflater; 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.FeedImageLoader; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedManager; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.util.LangUtils; import de.danoeh.antennapod.util.menuhandler.FeedMenuHandler; @@ -37,7 +37,7 @@ public class FeedInfoActivity extends SherlockActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); setContentView(R.layout.feedinfo); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/src/de/danoeh/antennapod/activity/FeedItemlistActivity.java b/src/de/danoeh/antennapod/activity/FeedItemlistActivity.java index 1be29ddbe..fdca48e8a 100644 --- a/src/de/danoeh/antennapod/activity/FeedItemlistActivity.java +++ b/src/de/danoeh/antennapod/activity/FeedItemlistActivity.java @@ -15,7 +15,6 @@ import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.Window; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.FeedRemover; import de.danoeh.antennapod.dialog.ConfirmationDialog; @@ -25,6 +24,7 @@ import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.fragment.ExternalPlayerFragment; import de.danoeh.antennapod.fragment.FeedlistFragment; import de.danoeh.antennapod.fragment.ItemlistFragment; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.util.StorageUtils; import de.danoeh.antennapod.util.menuhandler.FeedMenuHandler; @@ -42,7 +42,7 @@ public class FeedItemlistActivity extends SherlockFragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); StorageUtils.checkStorageAvailability(this); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); diff --git a/src/de/danoeh/antennapod/activity/FlattrAuthActivity.java b/src/de/danoeh/antennapod/activity/FlattrAuthActivity.java index b78b37afa..75e513816 100644 --- a/src/de/danoeh/antennapod/activity/FlattrAuthActivity.java +++ b/src/de/danoeh/antennapod/activity/FlattrAuthActivity.java @@ -17,8 +17,8 @@ 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.preferences.UserPreferences; import de.danoeh.antennapod.util.flattr.FlattrUtils; /** Guides the user through the authentication process */ @@ -36,7 +36,7 @@ public class FlattrAuthActivity extends SherlockActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); singleton = this; authSuccessful = false; diff --git a/src/de/danoeh/antennapod/activity/ItemviewActivity.java b/src/de/danoeh/antennapod/activity/ItemviewActivity.java index 668cb3a28..63dcb78f1 100644 --- a/src/de/danoeh/antennapod/activity/ItemviewActivity.java +++ b/src/de/danoeh/antennapod/activity/ItemviewActivity.java @@ -16,7 +16,6 @@ import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.Window; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; import de.danoeh.antennapod.feed.Feed; @@ -25,6 +24,7 @@ import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.fragment.FeedlistFragment; import de.danoeh.antennapod.fragment.ItemDescriptionFragment; import de.danoeh.antennapod.fragment.ItemlistFragment; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.util.StorageUtils; import de.danoeh.antennapod.util.menuhandler.FeedItemMenuHandler; @@ -42,7 +42,7 @@ public class ItemviewActivity extends SherlockFragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); StorageUtils.checkStorageAvailability(this); manager = FeedManager.getInstance(); diff --git a/src/de/danoeh/antennapod/activity/MainActivity.java b/src/de/danoeh/antennapod/activity/MainActivity.java index 67d7424b6..abdd53fd7 100644 --- a/src/de/danoeh/antennapod/activity/MainActivity.java +++ b/src/de/danoeh/antennapod/activity/MainActivity.java @@ -20,12 +20,12 @@ import com.actionbarsherlock.view.Window; import com.viewpagerindicator.TabPageIndicator; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.fragment.EpisodesFragment; import de.danoeh.antennapod.fragment.ExternalPlayerFragment; import de.danoeh.antennapod.fragment.FeedlistFragment; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.PlaybackService; import de.danoeh.antennapod.service.download.DownloadService; import de.danoeh.antennapod.storage.DownloadRequester; @@ -45,7 +45,7 @@ public class MainActivity extends SherlockFragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); StorageUtils.checkStorageAvailability(this); manager = FeedManager.getInstance(); @@ -69,7 +69,7 @@ public class MainActivity extends SherlockFragmentActivity { if (!appLaunched && getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN)) { appLaunched = true; - if (manager.getUnreadItems().size() > 0) { + if (manager.getUnreadItemsSize(true) > 0) { viewpager.setCurrentItem(MainPagerAdapter.POS_EPISODES); } @@ -109,7 +109,7 @@ public class MainActivity extends SherlockFragmentActivity { } else { setSupportProgressBarIndeterminateVisibility(false); } - invalidateOptionsMenu(); + supportInvalidateOptionsMenu(); } @Override diff --git a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java index 952cddf8b..7344c2b9d 100644 --- a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -18,12 +18,12 @@ import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; import de.danoeh.antennapod.dialog.TimeDialog; import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.PlaybackService; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.util.Converter; @@ -138,7 +138,7 @@ public abstract class MediaplayerActivity extends SherlockFragmentActivity @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); if (AppConfig.DEBUG) Log.d(TAG, "Creating Activity"); diff --git a/src/de/danoeh/antennapod/activity/MiroGuideCategoryActivity.java b/src/de/danoeh/antennapod/activity/MiroGuideCategoryActivity.java index 7a765a893..bb50944cc 100644 --- a/src/de/danoeh/antennapod/activity/MiroGuideCategoryActivity.java +++ b/src/de/danoeh/antennapod/activity/MiroGuideCategoryActivity.java @@ -12,9 +12,9 @@ import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import com.viewpagerindicator.TabPageIndicator; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.fragment.MiroGuideChannellistFragment; +import de.danoeh.antennapod.preferences.UserPreferences; /** * Shows channels of a category sorted by different criteria in lists. The @@ -34,7 +34,7 @@ public class MiroGuideCategoryActivity extends SherlockFragmentActivity { @Override protected void onCreate(Bundle arg0) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(arg0); getSupportActionBar().setDisplayHomeAsUpEnabled(true); setContentView(R.layout.miroguide_category); diff --git a/src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java b/src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java index 05d346bb4..f9fe912cd 100644 --- a/src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java +++ b/src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java @@ -21,7 +21,6 @@ import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.adapter.MiroGuideItemlistAdapter; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; @@ -30,6 +29,7 @@ import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.miroguide.conn.MiroGuideException; import de.danoeh.antennapod.miroguide.conn.MiroGuideService; import de.danoeh.antennapod.miroguide.model.MiroGuideChannel; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.storage.DownloadRequester; @@ -62,7 +62,7 @@ public class MiroGuideChannelViewActivity extends SherlockActivity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); setContentView(R.layout.miroguide_channelview); diff --git a/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java b/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java index 6dcb1bdca..8b33ef1da 100644 --- a/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java +++ b/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java @@ -15,10 +15,10 @@ 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.miroguide.conn.MiroGuideException; import de.danoeh.antennapod.miroguide.conn.MiroGuideService; +import de.danoeh.antennapod.preferences.UserPreferences; /** * Shows a list of available categories and offers a search button. If the user @@ -34,7 +34,7 @@ public class MiroGuideMainActivity extends SherlockListActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); setContentView(R.layout.miroguide_categorylist); diff --git a/src/de/danoeh/antennapod/activity/MiroGuideSearchActivity.java b/src/de/danoeh/antennapod/activity/MiroGuideSearchActivity.java index 2943339ad..a30777fb1 100644 --- a/src/de/danoeh/antennapod/activity/MiroGuideSearchActivity.java +++ b/src/de/danoeh/antennapod/activity/MiroGuideSearchActivity.java @@ -11,9 +11,9 @@ 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.fragment.MiroGuideChannellistFragment; +import de.danoeh.antennapod.preferences.UserPreferences; /** * Displays results when a search for miroguide channels has been performed. It @@ -26,7 +26,7 @@ public class MiroGuideSearchActivity extends SherlockFragmentActivity { @Override protected void onCreate(Bundle arg0) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(arg0); getSupportActionBar().setDisplayHomeAsUpEnabled(true); setContentView(R.layout.miroguidesearch); diff --git a/src/de/danoeh/antennapod/activity/OpmlFeedChooserActivity.java b/src/de/danoeh/antennapod/activity/OpmlFeedChooserActivity.java index 2301d2f0f..9ba355baf 100644 --- a/src/de/danoeh/antennapod/activity/OpmlFeedChooserActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlFeedChooserActivity.java @@ -16,9 +16,9 @@ import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.opml.OpmlElement; +import de.danoeh.antennapod.preferences.UserPreferences; /** * Displays the feeds that the OPML-Importer has read and lets the user choose @@ -36,7 +36,7 @@ public class OpmlFeedChooserActivity extends SherlockActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); setContentView(R.layout.opml_selection); diff --git a/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java index cf3028307..f887fdd94 100644 --- a/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java @@ -1,14 +1,17 @@ package de.danoeh.antennapod.activity; +import java.io.Reader; +import java.util.ArrayList; + import android.content.Intent; import android.util.Log; + import com.actionbarsherlock.app.SherlockActivity; + import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.asynctask.OpmlFeedQueuer; import de.danoeh.antennapod.asynctask.OpmlImportWorker; import de.danoeh.antennapod.opml.OpmlElement; -import java.io.Reader; -import java.util.ArrayList; /** * Base activity for Opml Import - e.g. with code what to do afterwards diff --git a/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java index cbe69d48c..dc698a851 100644 --- a/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java @@ -1,17 +1,19 @@ package de.danoeh.antennapod.activity; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; + import android.app.AlertDialog; import android.os.Bundle; -import de.danoeh.antennapod.PodcastApp; -import java.io.*; -import java.net.URL; +import de.danoeh.antennapod.preferences.UserPreferences; /** Lets the user start the OPML-import process. */ public class OpmlImportFromIntentActivity extends OpmlImportBaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java index bb5734b57..b38e0c443 100644 --- a/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java @@ -1,5 +1,10 @@ package de.danoeh.antennapod.activity; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.Reader; + import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; @@ -9,18 +14,15 @@ import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; + 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.preferences.UserPreferences; import de.danoeh.antennapod.util.StorageUtils; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.Reader; - /** * Lets the user start the OPML-import process from a path */ @@ -33,7 +35,7 @@ public class OpmlImportFromPathActivity extends OpmlImportBaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -63,7 +65,7 @@ public class OpmlImportFromPathActivity extends OpmlImportBaseActivity { * directory. */ private void setImportPath() { - File importDir = PodcastApp.getDataFolder(this, IMPORT_DIR); + File importDir = UserPreferences.getDataFolder(this, IMPORT_DIR); boolean success = true; if (!importDir.exists()) { if (AppConfig.DEBUG) diff --git a/src/de/danoeh/antennapod/activity/OpmlImportHolder.java b/src/de/danoeh/antennapod/activity/OpmlImportHolder.java index ec53ed7b6..8d51eb3de 100644 --- a/src/de/danoeh/antennapod/activity/OpmlImportHolder.java +++ b/src/de/danoeh/antennapod/activity/OpmlImportHolder.java @@ -1,9 +1,9 @@ package de.danoeh.antennapod.activity; -import de.danoeh.antennapod.opml.OpmlElement; - import java.util.ArrayList; +import de.danoeh.antennapod.opml.OpmlElement; + /** * Hold infos gathered by Ompl-Import * <p/> diff --git a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java index a7017d2fb..ec869ea32 100644 --- a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java +++ b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java @@ -1,7 +1,5 @@ package de.danoeh.antennapod.activity; -import java.util.List; - import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -11,7 +9,7 @@ import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; @@ -20,11 +18,11 @@ import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import com.mobeta.android.dslv.DragSortListView; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.FeedImageLoader; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedManager; +import de.danoeh.antennapod.preferences.UserPreferences; public class OrganizeQueueActivity extends SherlockListActivity { private static final String TAG = "OrganizeQueueActivity"; @@ -35,7 +33,7 @@ public class OrganizeQueueActivity extends SherlockListActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); setContentView(R.layout.organize_queue); @@ -43,8 +41,7 @@ public class OrganizeQueueActivity extends SherlockListActivity { listView.setDropListener(dropListener); listView.setRemoveListener(removeListener); - adapter = new OrganizeAdapter(this, 0, FeedManager.getInstance() - .getQueue()); + adapter = new OrganizeAdapter(this); setListAdapter(adapter); } @@ -120,13 +117,12 @@ public class OrganizeQueueActivity extends SherlockListActivity { } } - private static class OrganizeAdapter extends ArrayAdapter<FeedItem> { + private static class OrganizeAdapter extends BaseAdapter { private Context context; - public OrganizeAdapter(Context context, int textViewResourceId, - List<FeedItem> objects) { - super(context, textViewResourceId, objects); + public OrganizeAdapter(Context context) { + super(); this.context = context; } @@ -172,6 +168,21 @@ public class OrganizeQueueActivity extends SherlockListActivity { ImageView feedImage; } + @Override + public int getCount() { + return FeedManager.getInstance().getQueueSize(true); + } + + @Override + public FeedItem getItem(int position) { + return FeedManager.getInstance().getQueueItemAtIndex(position, true); + } + + @Override + public long getItemId(int position) { + return position; + } + } } diff --git a/src/de/danoeh/antennapod/activity/PlaybackHistoryActivity.java b/src/de/danoeh/antennapod/activity/PlaybackHistoryActivity.java index 22958b058..1a5a2cac9 100644 --- a/src/de/danoeh/antennapod/activity/PlaybackHistoryActivity.java +++ b/src/de/danoeh/antennapod/activity/PlaybackHistoryActivity.java @@ -10,10 +10,10 @@ 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.feed.FeedManager; import de.danoeh.antennapod.fragment.PlaybackHistoryFragment; +import de.danoeh.antennapod.preferences.UserPreferences; public class PlaybackHistoryActivity extends SherlockFragmentActivity { private static final String TAG = "PlaybackHistoryActivity"; @@ -44,7 +44,7 @@ public class PlaybackHistoryActivity extends SherlockFragmentActivity { @Override protected void onCreate(Bundle arg0) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(arg0); if (AppConfig.DEBUG) diff --git a/src/de/danoeh/antennapod/activity/PreferenceActivity.java b/src/de/danoeh/antennapod/activity/PreferenceActivity.java index fa0c42b37..10888209a 100644 --- a/src/de/danoeh/antennapod/activity/PreferenceActivity.java +++ b/src/de/danoeh/antennapod/activity/PreferenceActivity.java @@ -15,11 +15,11 @@ 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; import de.danoeh.antennapod.asynctask.OpmlExportWorker; import de.danoeh.antennapod.feed.FeedManager; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.util.flattr.FlattrUtils; /** The main preference activity */ @@ -36,7 +36,7 @@ public class PreferenceActivity extends SherlockPreferenceActivity { @SuppressWarnings("deprecation") @Override public void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -102,7 +102,7 @@ public class PreferenceActivity extends SherlockPreferenceActivity { return true; } }); - findPreference(PodcastApp.PREF_THEME).setOnPreferenceChangeListener( + findPreference(UserPreferences.PREF_THEME).setOnPreferenceChangeListener( new OnPreferenceChangeListener() { @Override @@ -137,7 +137,7 @@ public class PreferenceActivity extends SherlockPreferenceActivity { } private void setDataFolderText() { - File f = PodcastApp.getDataFolder(this, null); + File f = UserPreferences.getDataFolder(this, null); if (f != null) { findPreference(PREF_CHOOSE_DATA_DIR) .setSummary(f.getAbsolutePath()); @@ -165,7 +165,7 @@ public class PreferenceActivity extends SherlockPreferenceActivity { @Override protected void onApplyThemeResource(Theme theme, int resid, boolean first) { - theme.applyStyle(PodcastApp.getThemeResourceId(), true); + theme.applyStyle(UserPreferences.getTheme(), true); } @Override @@ -176,7 +176,7 @@ public class PreferenceActivity extends SherlockPreferenceActivity { .getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR); if (AppConfig.DEBUG) Log.d(TAG, "Setting data folder"); - PodcastApp.getInstance().setDataFolder(dir); + UserPreferences.setDataFolder(dir); } } diff --git a/src/de/danoeh/antennapod/activity/SearchActivity.java b/src/de/danoeh/antennapod/activity/SearchActivity.java index bb2eb54cc..152710112 100644 --- a/src/de/danoeh/antennapod/activity/SearchActivity.java +++ b/src/de/danoeh/antennapod/activity/SearchActivity.java @@ -16,7 +16,6 @@ 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.adapter.SearchlistAdapter; import de.danoeh.antennapod.feed.Feed; @@ -26,6 +25,7 @@ import de.danoeh.antennapod.feed.FeedSearcher; import de.danoeh.antennapod.feed.SearchResult; import de.danoeh.antennapod.fragment.FeedlistFragment; import de.danoeh.antennapod.fragment.ItemlistFragment; +import de.danoeh.antennapod.preferences.UserPreferences; /** Displays the results when the user searches for FeedItems or Feeds. */ public class SearchActivity extends SherlockListActivity { @@ -43,7 +43,7 @@ public class SearchActivity extends SherlockListActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/src/de/danoeh/antennapod/activity/StorageErrorActivity.java b/src/de/danoeh/antennapod/activity/StorageErrorActivity.java index 514b1f69d..4d9184dcf 100644 --- a/src/de/danoeh/antennapod/activity/StorageErrorActivity.java +++ b/src/de/danoeh/antennapod/activity/StorageErrorActivity.java @@ -10,8 +10,8 @@ import android.util.Log; import com.actionbarsherlock.app.SherlockActivity; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.util.StorageUtils; /** Is show if there is now external storage available. */ @@ -20,7 +20,7 @@ public class StorageErrorActivity extends SherlockActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); setContentView(R.layout.storage_error); diff --git a/src/de/danoeh/antennapod/activity/VideoplayerActivity.java b/src/de/danoeh/antennapod/activity/VideoplayerActivity.java index f6ea4f69e..7ab5df572 100644 --- a/src/de/danoeh/antennapod/activity/VideoplayerActivity.java +++ b/src/de/danoeh/antennapod/activity/VideoplayerActivity.java @@ -18,9 +18,9 @@ import android.widget.VideoView; import com.actionbarsherlock.view.Window; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.PlaybackService; import de.danoeh.antennapod.service.PlayerStatus; @@ -41,7 +41,7 @@ public class VideoplayerActivity extends MediaplayerActivity implements @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); - setTheme(PodcastApp.getThemeResourceId()); + setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); } diff --git a/src/de/danoeh/antennapod/adapter/AbstractFeedItemlistAdapter.java b/src/de/danoeh/antennapod/adapter/AbstractFeedItemlistAdapter.java index 5c720d652..74b54c105 100644 --- a/src/de/danoeh/antennapod/adapter/AbstractFeedItemlistAdapter.java +++ b/src/de/danoeh/antennapod/adapter/AbstractFeedItemlistAdapter.java @@ -6,13 +6,13 @@ import android.content.Context; import android.widget.ArrayAdapter; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.util.EpisodeFilter; public abstract class AbstractFeedItemlistAdapter extends ArrayAdapter<FeedItem> { private List<FeedItem> objects; - private boolean isExpanded = true; public AbstractFeedItemlistAdapter(Context context, int textViewResourceId, List<FeedItem> objects) { @@ -22,28 +22,20 @@ public abstract class AbstractFeedItemlistAdapter extends @Override public int getCount() { - if (isExpanded) { - if (PodcastApp.getInstance().displayOnlyEpisodes()) { + if (UserPreferences.isDisplayOnlyEpisodes()) { return EpisodeFilter.countItemsWithEpisodes(objects); } else { return super.getCount(); } - } else { - return 0; - } + } @Override public FeedItem getItem(int position) { - if (PodcastApp.getInstance().displayOnlyEpisodes()) { + if (UserPreferences.isDisplayOnlyEpisodes()) { return EpisodeFilter.accessEpisodeByIndex(objects, position); } else { return super.getItem(position); } } - - public void toggleExpandedState() { - isExpanded = !isExpanded; - notifyDataSetChanged(); - } } diff --git a/src/de/danoeh/antennapod/adapter/ExternalEpisodesListAdapter.java b/src/de/danoeh/antennapod/adapter/ExternalEpisodesListAdapter.java index 2670dfa44..ff3684d80 100644 --- a/src/de/danoeh/antennapod/adapter/ExternalEpisodesListAdapter.java +++ b/src/de/danoeh/antennapod/adapter/ExternalEpisodesListAdapter.java @@ -1,7 +1,5 @@ package de.danoeh.antennapod.adapter; -import java.util.List; - import android.content.Context; import android.content.res.TypedArray; import android.view.LayoutInflater; @@ -16,6 +14,7 @@ import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.FeedImageLoader; import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.storage.DownloadRequester; import de.danoeh.antennapod.util.Converter; @@ -32,21 +31,17 @@ public class ExternalEpisodesListAdapter extends BaseExpandableListAdapter { public static final int GROUP_POS_UNREAD = 1; private Context context; - - private List<FeedItem> unreadItems; - private List<FeedItem> queueItems; + private FeedManager manager = FeedManager.getInstance(); private ActionButtonCallback feedItemActionCallback; private OnGroupActionClicked groupActionCallback; public ExternalEpisodesListAdapter(Context context, - List<FeedItem> unreadItems, List<FeedItem> queueItems, ActionButtonCallback callback, OnGroupActionClicked groupActionCallback) { super(); this.context = context; - this.unreadItems = unreadItems; - this.queueItems = queueItems; + this.feedItemActionCallback = callback; this.groupActionCallback = groupActionCallback; } @@ -58,22 +53,10 @@ public class ExternalEpisodesListAdapter extends BaseExpandableListAdapter { @Override public FeedItem getChild(int groupPosition, int childPosition) { - final boolean displayOnlyEpisodes = PodcastApp.getInstance() - .displayOnlyEpisodes(); if (groupPosition == GROUP_POS_QUEUE) { - if (displayOnlyEpisodes) { - return EpisodeFilter.accessEpisodeByIndex(queueItems, - childPosition); - } else { - return queueItems.get(childPosition); - } + return manager.getQueueItemAtIndex(childPosition, true); } else if (groupPosition == GROUP_POS_UNREAD) { - if (displayOnlyEpisodes) { - return EpisodeFilter.accessEpisodeByIndex(unreadItems, - childPosition); - } else { - return unreadItems.get(childPosition); - } + return manager.getUnreadItemAtIndex(childPosition, true); } return null; } @@ -213,20 +196,10 @@ public class ExternalEpisodesListAdapter extends BaseExpandableListAdapter { @Override public int getChildrenCount(int groupPosition) { - final boolean displayOnlyEpisodes = PodcastApp.getInstance() - .displayOnlyEpisodes(); if (groupPosition == GROUP_POS_QUEUE) { - if (displayOnlyEpisodes) { - return EpisodeFilter.countItemsWithEpisodes(queueItems); - } else { - return queueItems.size(); - } + return manager.getQueueSize(true); } else if (groupPosition == GROUP_POS_UNREAD) { - if (displayOnlyEpisodes) { - return EpisodeFilter.countItemsWithEpisodes(unreadItems); - } else { - return unreadItems.size(); - } + return manager.getUnreadItemsSize(true); } return 0; } @@ -254,12 +227,12 @@ public class ExternalEpisodesListAdapter extends BaseExpandableListAdapter { String headerString = null; if (groupPosition == 0) { headerString = context.getString(R.string.queue_label); - if (!queueItems.isEmpty()) { + if (manager.getQueueSize(true) > 0) { headerString += " (" + getChildrenCount(GROUP_POS_QUEUE) + ")"; } } else { headerString = context.getString(R.string.new_label); - if (!unreadItems.isEmpty()) { + if (manager.getUnreadItemsSize(true) > 0) { headerString += " (" + getChildrenCount(GROUP_POS_UNREAD) + ")"; } } @@ -277,7 +250,8 @@ public class ExternalEpisodesListAdapter extends BaseExpandableListAdapter { @Override public boolean isEmpty() { - return unreadItems.isEmpty() && queueItems.isEmpty(); + return manager.getUnreadItemsSize(true) == 0 + && manager.getQueueSize(true) == 0; } @Override diff --git a/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java b/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java index 5941d52ea..b03dd0c1f 100644 --- a/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java +++ b/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java @@ -5,7 +5,6 @@ import java.util.List; import android.content.Context; import android.content.res.TypedArray; -import android.graphics.Typeface; import android.text.format.DateUtils; import android.view.LayoutInflater; import android.view.View; diff --git a/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java b/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java index db9ae999c..5f26e900e 100644 --- a/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java +++ b/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java @@ -15,6 +15,7 @@ import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.opml.OpmlWriter; +import de.danoeh.antennapod.preferences.UserPreferences; /** Writes an OPML file into the export directory in the background. */ public class OpmlExportWorker extends AsyncTask<Void, Void, Void> { @@ -40,7 +41,7 @@ public class OpmlExportWorker extends AsyncTask<Void, Void, Void> { OpmlWriter opmlWriter = new OpmlWriter(); if (output == null) { output = new File( - PodcastApp.getDataFolder(context, PodcastApp.EXPORT_DIR), + UserPreferences.getDataFolder(context, PodcastApp.EXPORT_DIR), DEFAULT_OUTPUT_NAME); if (output.exists()) { Log.w(TAG, "Overwriting previously exported file."); diff --git a/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java b/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java index a3652e05d..4d9c9867e 100644 --- a/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java +++ b/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java @@ -7,7 +7,6 @@ import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.activity.OpmlImportFromPathActivity; import de.danoeh.antennapod.activity.OpmlImportHolder; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.opml.OpmlElement; diff --git a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java index 2c1d30bdb..5af06895f 100644 --- a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java +++ b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.asynctask; -import java.io.*; +import java.io.IOException; +import java.io.Reader; import java.util.ArrayList; import org.xmlpull.v1.XmlPullParserException; diff --git a/src/de/danoeh/antennapod/feed/Feed.java b/src/de/danoeh/antennapod/feed/Feed.java index 4df9fc43f..ed5d49dbd 100644 --- a/src/de/danoeh/antennapod/feed/Feed.java +++ b/src/de/danoeh/antennapod/feed/Feed.java @@ -7,6 +7,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import android.preference.PreferenceManager; import de.danoeh.antennapod.PodcastApp; +import de.danoeh.antennapod.preferences.UserPreferences; /** * Data Object for a whole feed @@ -68,13 +69,9 @@ public class Feed extends FeedFile { * */ public int getNumOfNewItems() { int count = 0; - boolean displayOnlyEpisodes = PreferenceManager - .getDefaultSharedPreferences(PodcastApp.getInstance()) - .getBoolean(PodcastApp.PREF_DISPLAY_ONLY_EPISODES, false); - for (FeedItem item : items) { if (item.getState() == FeedItem.State.NEW) { - if (!displayOnlyEpisodes || item.getMedia() != null) { + if (!UserPreferences.isDisplayOnlyEpisodes() || item.getMedia() != null) { count++; } } @@ -105,12 +102,9 @@ public class Feed extends FeedFile { * only count items with episodes. */ public boolean hasNewItems() { - boolean displayOnlyEpisodes = PreferenceManager - .getDefaultSharedPreferences(PodcastApp.getInstance()) - .getBoolean(PodcastApp.PREF_DISPLAY_ONLY_EPISODES, false); for (FeedItem item : items) { if (item.getState() == FeedItem.State.NEW) { - if (!displayOnlyEpisodes || item.getMedia() != null) { + if (!UserPreferences.isDisplayOnlyEpisodes() || item.getMedia() != null) { return true; } } @@ -125,11 +119,8 @@ public class Feed extends FeedFile { * */ public int getNumOfItems() { int count = 0; - boolean displayOnlyEpisodes = PreferenceManager - .getDefaultSharedPreferences(PodcastApp.getInstance()) - .getBoolean(PodcastApp.PREF_DISPLAY_ONLY_EPISODES, false); for (FeedItem item : items) { - if (!displayOnlyEpisodes || item.getMedia() != null) { + if (!UserPreferences.isDisplayOnlyEpisodes() || item.getMedia() != null) { count++; } } diff --git a/src/de/danoeh/antennapod/feed/FeedManager.java b/src/de/danoeh/antennapod/feed/FeedManager.java index 6c3471e5c..6a8ed9b6e 100644 --- a/src/de/danoeh/antennapod/feed/FeedManager.java +++ b/src/de/danoeh/antennapod/feed/FeedManager.java @@ -21,11 +21,13 @@ import android.util.Log; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.asynctask.DownloadStatus; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.PlaybackService; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.storage.DownloadRequester; import de.danoeh.antennapod.storage.PodDBAdapter; import de.danoeh.antennapod.util.DownloadError; +import de.danoeh.antennapod.util.EpisodeFilter; import de.danoeh.antennapod.util.FeedtitleComparator; import de.danoeh.antennapod.util.comparator.DownloadStatusComparator; import de.danoeh.antennapod.util.comparator.FeedItemPubdateComparator; @@ -591,9 +593,6 @@ public class FeedManager { public void downloadFeedItem(final Context context, FeedItem... items) throws DownloadRequestException { - boolean autoQueue = PreferenceManager.getDefaultSharedPreferences( - context.getApplicationContext()).getBoolean( - PodcastApp.PREF_AUTO_QUEUE, true); List<FeedItem> addToQueue = new ArrayList<FeedItem>(); for (FeedItem item : items) { @@ -618,7 +617,7 @@ public class FeedManager { addToQueue.add(item); } } - if (autoQueue) { + if (UserPreferences.isAutoQueue()) { addQueueItem(context, addToQueue.toArray(new FeedItem[addToQueue.size()])); } @@ -732,9 +731,7 @@ public class FeedManager { SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(context .getApplicationContext()); - boolean autoDelete = prefs.getBoolean(PodcastApp.PREF_AUTO_DELETE, - false); - if (autoDelete) { + if (UserPreferences.isAutoDelete()) { long lastPlayedId = prefs.getLong( PlaybackService.PREF_LAST_PLAYED_ID, -1); long autoDeleteId = prefs.getLong( @@ -1479,16 +1476,74 @@ public class FeedManager { return feeds; } - public List<FeedItem> getUnreadItems() { - return unreadItems; + /** + * Returns the number of items that are currently in the queue. + * + * @param enableEpisodeFilter + * true if items without episodes should be ignored by this + * method if the episode filter was enabled by the user. + * */ + public int getQueueSize(boolean enableEpisodeFilter) { + if (UserPreferences.isDisplayOnlyEpisodes() && enableEpisodeFilter) { + return EpisodeFilter.countItemsWithEpisodes(queue); + } else { + return queue.size(); + } } - public ArrayList<DownloadStatus> getDownloadLog() { - return downloadLog; + /** + * Returns the FeedItem at the specified index of the queue. + * + * @param enableEpisodeFilter + * true if items without episodes should be ignored by this + * method if the episode filter was enabled by the user. + * + * @throws IndexOutOfBoundsException + * if index is out of range + * */ + public FeedItem getQueueItemAtIndex(int index, boolean enableEpisodeFilter) { + if (UserPreferences.isDisplayOnlyEpisodes() && enableEpisodeFilter) { + return EpisodeFilter.accessEpisodeByIndex(queue, index); + } else { + return queue.get(index); + } + } + + /** + * Returns the number of unread items. + * + * @param enableEpisodeFilter + * true if items without episodes should be ignored by this + * method if the episode filter was enabled by the user. + * */ + public int getUnreadItemsSize(boolean enableEpisodeFilter) { + if (UserPreferences.isDisplayOnlyEpisodes() && enableEpisodeFilter) { + return EpisodeFilter.countItemsWithEpisodes(unreadItems); + } else { + return unreadItems.size(); + } } - public List<FeedItem> getQueue() { - return queue; + /** + * Returns the FeedItem at the specified index of the unread items list. + * + * @param enableEpisodeFilter + * true if items without episodes should be ignored by this + * method if the episode filter was enabled by the user. + * + * @throws IndexOutOfBoundsException + * if index is out of range + * */ + public FeedItem getUnreadItemAtIndex(int index, boolean enableEpisodeFilter) { + if (UserPreferences.isDisplayOnlyEpisodes() && enableEpisodeFilter) { + return EpisodeFilter.accessEpisodeByIndex(unreadItems, index); + } else { + return unreadItems.get(index); + } + } + + public ArrayList<DownloadStatus> getDownloadLog() { + return downloadLog; } public List<FeedItem> getPlaybackHistory() { diff --git a/src/de/danoeh/antennapod/fragment/EpisodesFragment.java b/src/de/danoeh/antennapod/fragment/EpisodesFragment.java index 843cf5af0..2046b45ba 100644 --- a/src/de/danoeh/antennapod/fragment/EpisodesFragment.java +++ b/src/de/danoeh/antennapod/fragment/EpisodesFragment.java @@ -99,10 +99,8 @@ public class EpisodesFragment extends SherlockFragment { @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - FeedManager manager = FeedManager.getInstance(); adapter = new ExternalEpisodesListAdapter(getActivity(), - manager.getUnreadItems(), manager.getQueue(), adapterCallback, - groupActionCallback); + adapterCallback, groupActionCallback); listView.setAdapter(adapter); listView.expandGroup(ExternalEpisodesListAdapter.GROUP_POS_QUEUE); listView.expandGroup(ExternalEpisodesListAdapter.GROUP_POS_UNREAD); diff --git a/src/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java b/src/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java index 8f5350c8e..e819a1000 100644 --- a/src/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java +++ b/src/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java @@ -23,8 +23,6 @@ import android.view.View; import android.view.ViewGroup; import android.webkit.WebSettings.LayoutAlgorithm; import android.webkit.WebView; -import android.widget.FrameLayout; -import android.widget.LinearLayout; import android.widget.Toast; import com.actionbarsherlock.app.SherlockFragment; @@ -35,6 +33,7 @@ import de.danoeh.antennapod.R; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedManager; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.util.ShareUtils; /** Displays the description of a FeedItem in a Webview. */ @@ -72,7 +71,7 @@ public class ItemDescriptionFragment extends SherlockFragment { Log.d(TAG, "Creating view"); webvDescription = new WebView(getActivity()); - if (PodcastApp.getThemeResourceId() == R.style.Theme_AntennaPod_Dark) { + if (UserPreferences.getTheme() == R.style.Theme_AntennaPod_Dark) { if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { webvDescription.setLayerType(View.LAYER_TYPE_SOFTWARE, null); diff --git a/src/de/danoeh/antennapod/preferences/UserPreferences.java b/src/de/danoeh/antennapod/preferences/UserPreferences.java new file mode 100644 index 000000000..7625f88e4 --- /dev/null +++ b/src/de/danoeh/antennapod/preferences/UserPreferences.java @@ -0,0 +1,316 @@ +package de.danoeh.antennapod.preferences; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.util.Log; +import de.danoeh.antennapod.AppConfig; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.R.style; +import de.danoeh.antennapod.activity.OpmlImportFromPathActivity; +import de.danoeh.antennapod.receiver.FeedUpdateReceiver; + +/** + * Provides access to preferences set by the user in the settings screen. A + * private instance of this class must first be instantiated via + * createInstance() or otherwise every public method will throw an Exception + * when called. + */ +public class UserPreferences implements + SharedPreferences.OnSharedPreferenceChangeListener { + private static final String TAG = "UserPreferences"; + + public static final String PREF_PAUSE_ON_HEADSET_DISCONNECT = "prefPauseOnHeadsetDisconnect"; + public static final String PREF_FOLLOW_QUEUE = "prefFollowQueue"; + public static final String PREF_DOWNLOAD_MEDIA_ON_WIFI_ONLY = "prefDownloadMediaOnWifiOnly"; + public static final String PREF_UPDATE_INTERVAL = "prefAutoUpdateIntervall"; + public static final String PREF_MOBILE_UPDATE = "prefMobileUpdate"; + public static final String PREF_AUTO_QUEUE = "prefAutoQueue"; + 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 UserPreferences instance; + private Context context; + + // Preferences + private boolean pauseOnHeadsetDisconnect; + private boolean followQueue; + private boolean downloadMediaOnWifiOnly; + private int updateInterval; + private boolean allowMobileUpdate; + private boolean autoQueue; + private boolean displayOnlyEpisodes; + private boolean autoDelete; + private int theme; + + private UserPreferences(Context context) { + this.context = context; + loadPreferences(); + createImportDirectory(); + createNoMediaFile(); + PreferenceManager.getDefaultSharedPreferences(context) + .registerOnSharedPreferenceChangeListener(this); + } + + /** + * Sets up the UserPreferences class. + * + * @throws IllegalArgumentException + * if context is null + * */ + public static void createInstance(Context context) { + if (AppConfig.DEBUG) + Log.d(TAG, "Creating new instance of UserPreferences"); + if (context == null) + throw new IllegalArgumentException("Context must not be null"); + instance = new UserPreferences(context); + } + + private void loadPreferences() { + SharedPreferences sp = PreferenceManager + .getDefaultSharedPreferences(context); + pauseOnHeadsetDisconnect = sp.getBoolean( + PREF_PAUSE_ON_HEADSET_DISCONNECT, true); + followQueue = sp.getBoolean(PREF_FOLLOW_QUEUE, false); + downloadMediaOnWifiOnly = sp.getBoolean( + PREF_DOWNLOAD_MEDIA_ON_WIFI_ONLY, true); + updateInterval = sp.getInt(PREF_UPDATE_INTERVAL, 0); + allowMobileUpdate = sp.getBoolean(PREF_MOBILE_UPDATE, false); + autoQueue = sp.getBoolean(PREF_AUTO_QUEUE, true); + displayOnlyEpisodes = sp.getBoolean(PREF_DISPLAY_ONLY_EPISODES, false); + autoDelete = sp.getBoolean(PREF_AUTO_DELETE, false); + theme = readThemeValue(sp.getString(PREF_THEME, "0")); + } + + private int readThemeValue(String valueFromPrefs) { + switch (Integer.parseInt(valueFromPrefs)) { + case 0: + return R.style.Theme_AntennaPod_Light; + case 1: + return R.style.Theme_AntennaPod_Dark; + default: + return R.style.Theme_AntennaPod_Light; + } + } + + private static void instanceAvailable() { + if (instance == null) { + throw new IllegalStateException( + "UserPreferences was used before being set up"); + } + } + + public static boolean isPauseOnHeadsetDisconnect() { + instanceAvailable(); + return instance.pauseOnHeadsetDisconnect; + } + + public static boolean isFollowQueue() { + instanceAvailable(); + return instance.followQueue; + } + + public static boolean isDownloadMediaOnWifiOnly() { + instanceAvailable(); + return instance.downloadMediaOnWifiOnly; + } + + public static int getUpdateInterval() { + instanceAvailable(); + return instance.updateInterval; + } + + public static boolean isAllowMobileUpdate() { + instanceAvailable(); + return instance.allowMobileUpdate; + } + + public static boolean isAutoQueue() { + instanceAvailable(); + return instance.autoQueue; + } + + public static boolean isDisplayOnlyEpisodes() { + instanceAvailable(); + return instance.displayOnlyEpisodes; + } + + public static boolean isAutoDelete() { + instanceAvailable(); + return instance.autoDelete; + } + + public static int getTheme() { + instanceAvailable(); + return instance.theme; + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sp, String key) { + if (AppConfig.DEBUG) + Log.d(TAG, "Registered change of user preferences. Key: " + key); + + if (key.equals(PREF_DOWNLOAD_MEDIA_ON_WIFI_ONLY)) { + downloadMediaOnWifiOnly = sp.getBoolean( + PREF_DOWNLOAD_MEDIA_ON_WIFI_ONLY, true); + + } else if (key.equals(PREF_MOBILE_UPDATE)) { + allowMobileUpdate = sp.getBoolean(PREF_MOBILE_UPDATE, false); + + } else if (key.equals(PREF_FOLLOW_QUEUE)) { + followQueue = sp.getBoolean(PREF_FOLLOW_QUEUE, false); + + } else if (key.equals(PREF_UPDATE_INTERVAL)) { + updateInterval = sp.getInt(PREF_UPDATE_INTERVAL, 0); + AlarmManager alarmManager = (AlarmManager) context + .getSystemService(Context.ALARM_SERVICE); + int hours = Integer.parseInt(sp + .getString(PREF_UPDATE_INTERVAL, "0")); + PendingIntent updateIntent = PendingIntent.getBroadcast(context, 0, + new Intent(FeedUpdateReceiver.ACTION_REFRESH_FEEDS), 0); + alarmManager.cancel(updateIntent); + if (hours != 0) { + long newIntervall = TimeUnit.HOURS.toMillis(hours); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, + newIntervall, newIntervall, updateIntent); + if (AppConfig.DEBUG) + Log.d(TAG, "Changed alarm to new intervall"); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Automatic update was deactivated"); + } + + } else if (key.equals(PREF_AUTO_DELETE)) { + autoDelete = sp.getBoolean(PREF_AUTO_DELETE, false); + + } else if (key.equals(PREF_AUTO_QUEUE)) { + autoQueue = sp.getBoolean(PREF_AUTO_QUEUE, true); + + } else if (key.equals(PREF_DISPLAY_ONLY_EPISODES)) { + displayOnlyEpisodes = sp.getBoolean(PREF_DISPLAY_ONLY_EPISODES, + false); + } else if (key.equals(PREF_THEME)) { + theme = readThemeValue(sp.getString(PREF_THEME, "")); + } + } + + /** + * 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) { + instanceAvailable(); + 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 { + // handle path separators + String[] dirs = type.split("/"); + for (int i = 0; i < dirs.length; i++) { + if (dirs.length > 0) { + if (i < dirs.length - 1) { + dataDir = getDataFolder(context, dirs[i]); + if (dataDir == null) { + return null; + } + } + type = dirs[i]; + } + } + 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 static void setDataFolder(String dir) { + if (AppConfig.DEBUG) + Log.d(TAG, "Result from DirectoryChooser: " + dir); + instanceAvailable(); + SharedPreferences prefs = PreferenceManager + .getDefaultSharedPreferences(instance.context); + SharedPreferences.Editor editor = prefs.edit(); + editor.putString(PREF_DATA_FOLDER, dir); + editor.commit(); + createImportDirectory(); + } + + /** Create a .nomedia file to prevent scanning by the media scanner. */ + private static void createNoMediaFile() { + File f = new File(instance.context.getExternalFilesDir(null), + ".nomedia"); + if (!f.exists()) { + try { + f.createNewFile(); + } catch (IOException e) { + Log.e(TAG, "Could not create .nomedia file"); + e.printStackTrace(); + } + if (AppConfig.DEBUG) + Log.d(TAG, ".nomedia file created"); + } + } + + /** + * Creates the import directory if it doesn't exist and if storage is + * available + */ + private static void createImportDirectory() { + File importDir = getDataFolder(instance.context, + OpmlImportFromPathActivity.IMPORT_DIR); + if (importDir != null) { + if (importDir.exists()) { + if (AppConfig.DEBUG) + Log.d(TAG, "Import directory already exists"); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Creating import directory"); + importDir.mkdir(); + } + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Could not access external storage."); + } + } + +} diff --git a/src/de/danoeh/antennapod/receiver/FeedUpdateReceiver.java b/src/de/danoeh/antennapod/receiver/FeedUpdateReceiver.java index ed6672ccb..0ceaf5fbd 100644 --- a/src/de/danoeh/antennapod/receiver/FeedUpdateReceiver.java +++ b/src/de/danoeh/antennapod/receiver/FeedUpdateReceiver.java @@ -10,6 +10,7 @@ import android.util.Log; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.feed.FeedManager; +import de.danoeh.antennapod.preferences.UserPreferences; /** Refreshes all feeds when it receives an intent */ public class FeedUpdateReceiver extends BroadcastReceiver { @@ -20,10 +21,7 @@ public class FeedUpdateReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_REFRESH_FEEDS)) { if (AppConfig.DEBUG) Log.d(TAG, "Received intent"); - boolean mobileUpdate = PreferenceManager - .getDefaultSharedPreferences( - context.getApplicationContext()).getBoolean( - PodcastApp.PREF_MOBILE_UPDATE, false); + boolean mobileUpdate = UserPreferences.isAllowMobileUpdate(); if (mobileUpdate || connectedToWifi(context)) { FeedManager.getInstance().refreshAllFeeds(context); } else { diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java index 62370ff6d..e19e456e1 100644 --- a/src/de/danoeh/antennapod/service/PlaybackService.java +++ b/src/de/danoeh/antennapod/service/PlaybackService.java @@ -46,6 +46,7 @@ import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.feed.MediaType; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.receiver.MediaButtonReceiver; import de.danoeh.antennapod.receiver.PlayerWidget; import de.danoeh.antennapod.util.BitmapDecoder; @@ -717,9 +718,7 @@ public class PlaybackService extends Service { editor.commit(); // Prepare for playing next item - boolean followQueue = prefs.getBoolean( - PodcastApp.PREF_FOLLOW_QUEUE, false); - boolean playNextItem = isInQueue && followQueue && nextItem != null; + boolean playNextItem = isInQueue && UserPreferences.isFollowQueue() && nextItem != null; if (playNextItem) { if (AppConfig.DEBUG) Log.d(TAG, "Loading next item in queue"); @@ -937,8 +936,9 @@ public class PlaybackService extends Service { Intent pauseButtonIntent = new Intent(this, PlaybackService.class); pauseButtonIntent.putExtra(MediaButtonReceiver.EXTRA_KEYCODE, KeyEvent.KEYCODE_MEDIA_PAUSE); - PendingIntent pauseButtonPendingIntent = PendingIntent - .getService(this, 0, pauseButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pauseButtonPendingIntent = PendingIntent.getService( + this, 0, pauseButtonIntent, + PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder notificationBuilder = new Notification.Builder( this) .setContentTitle(contentTitle) @@ -1155,10 +1155,8 @@ public class PlaybackService extends Service { /** Pauses playback if PREF_PAUSE_ON_HEADSET_DISCONNECT was set to true. */ private void pauseIfPauseOnDisconnect() { - boolean pauseOnDisconnect = PreferenceManager - .getDefaultSharedPreferences(getApplicationContext()) - .getBoolean(PodcastApp.PREF_PAUSE_ON_HEADSET_DISCONNECT, false); - if (pauseOnDisconnect && status == PlayerStatus.PLAYING) { + if (UserPreferences.isPauseOnHeadsetDisconnect() + && status == PlayerStatus.PLAYING) { pause(true, true); } } diff --git a/src/de/danoeh/antennapod/service/download/DownloadService.java b/src/de/danoeh/antennapod/service/download/DownloadService.java index 59f0a5b97..18262c396 100644 --- a/src/de/danoeh/antennapod/service/download/DownloadService.java +++ b/src/de/danoeh/antennapod/service/download/DownloadService.java @@ -30,7 +30,6 @@ import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; -import android.app.Notification.Builder; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index 38b143a7f..1d46ceb72 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -16,6 +16,7 @@ import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedFile; import de.danoeh.antennapod.feed.FeedImage; import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.download.DownloadService; import de.danoeh.antennapod.util.FileNameGenerator; import de.danoeh.antennapod.util.URLChecker; @@ -282,7 +283,7 @@ public class DownloadRequester { private File getExternalFilesDirOrThrowException(Context context, String type) throws DownloadRequestException { - File result = PodcastApp.getDataFolder(context, type); + File result = UserPreferences.getDataFolder(context, type); if (result == null) { throw new DownloadRequestException( "Failed to access external storage"); diff --git a/src/de/danoeh/antennapod/util/BitmapDecoder.java b/src/de/danoeh/antennapod/util/BitmapDecoder.java index 8dd4953c6..e9ef2ad01 100644 --- a/src/de/danoeh/antennapod/util/BitmapDecoder.java +++ b/src/de/danoeh/antennapod/util/BitmapDecoder.java @@ -2,9 +2,6 @@ package de.danoeh.antennapod.util; import java.io.File; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.FilenameUtils; - import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; diff --git a/src/de/danoeh/antennapod/util/StorageUtils.java b/src/de/danoeh/antennapod/util/StorageUtils.java index cc0ba7136..9003ee907 100644 --- a/src/de/danoeh/antennapod/util/StorageUtils.java +++ b/src/de/danoeh/antennapod/util/StorageUtils.java @@ -10,13 +10,14 @@ import android.util.Log; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.activity.StorageErrorActivity; +import de.danoeh.antennapod.preferences.UserPreferences; /** Utility functions for handling storage errors */ public class StorageUtils { private static final String TAG = "StorageUtils"; public static boolean storageAvailable(Context context) { - File dir = PodcastApp.getDataFolder(context, null); + File dir = UserPreferences.getDataFolder(context, null); if (dir != null) { return dir.exists() && dir.canRead() && dir.canWrite(); } else { @@ -48,7 +49,7 @@ public class StorageUtils { /** Get the number of free bytes that are available on the external storage. */ public static long getFreeSpaceAvailable() { - StatFs stat = new StatFs(PodcastApp.getDataFolder( + StatFs stat = new StatFs(UserPreferences.getDataFolder( PodcastApp.getInstance(), null).getAbsolutePath()); long availableBlocks = stat.getAvailableBlocks(); long blockSize = stat.getBlockSize(); diff --git a/src/de/danoeh/antennapod/util/ThemeUtils.java b/src/de/danoeh/antennapod/util/ThemeUtils.java index ec47ed48e..ad183b97a 100644 --- a/src/de/danoeh/antennapod/util/ThemeUtils.java +++ b/src/de/danoeh/antennapod/util/ThemeUtils.java @@ -3,12 +3,13 @@ package de.danoeh.antennapod.util; import android.util.Log; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.preferences.UserPreferences; public class ThemeUtils { private static final String TAG = "ThemeUtils"; public static int getSelectionBackgroundColor() { - switch (PodcastApp.getThemeResourceId()) { + switch (UserPreferences.getTheme()) { case R.style.Theme_AntennaPod_Dark: return R.color.selection_background_color_dark; case R.style.Theme_AntennaPod_Light: |