summaryrefslogtreecommitdiff
path: root/core/src/main/java
diff options
context:
space:
mode:
authorTom Hennen <tom.hennen@gmail.com>2016-03-25 10:50:41 -0400
committerTom Hennen <tom.hennen@gmail.com>2016-03-25 10:50:41 -0400
commit4cd58a9b37899e0c886a930f268b701d29a9a989 (patch)
tree42d5f7964c8d5114902f4e15538b39941ce82b4b /core/src/main/java
parent5cc6f12acb0213dbca37e02c019ee27c28f4a0bb (diff)
downloadAntennaPod-4cd58a9b37899e0c886a930f268b701d29a9a989.zip
updated and fixed PR AntennaPod/AntennPod#1624
Diffstat (limited to 'core/src/main/java')
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/ApplicationCallbacks.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/ClientConfig.java22
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/DBTasksCallbacks.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/DownloadServiceCallbacks.java10
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/FlattrCallbacks.java12
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/GpodnetCallbacks.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/PlaybackServiceCallbacks.java6
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/UpdateManager.java97
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/asynctask/FeedRemover.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrClickWorker.java30
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrStatusFetcher.java12
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/asynctask/ImageResource.java6
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java10
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/dialog/ConfirmationDialog.java24
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/dialog/DownloadRequestErrorDialogCreator.java9
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/EventDistributor.java10
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/Feed.java46
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/FeedComponent.java3
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/FeedFile.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java23
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java57
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java5
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java2
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/gpoddernet/GpodnetService.java25
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetDevice.java23
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeAction.java11
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeActionPostResponse.java2
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/menuhandler/MenuItemUtils.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/opml/OpmlReader.java6
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/preferences/GpodnetPreferences.java10
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java100
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/receiver/AlarmUpdateReceiver.java5
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/receiver/FeedUpdateReceiver.java2
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/receiver/MediaButtonReceiver.java9
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/GpodnetSyncService.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java32
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadRequest.java30
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java237
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/DownloaderCallback.java2
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java51
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/ProxyConfig.java32
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java14
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java441
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceTaskManager.java55
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java59
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java115
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java5
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java38
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java2
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java132
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java81
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java9
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java22
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java5
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java15
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java374
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/handler/HandlerState.java6
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/handler/TypeGetter.java41
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSContent.java6
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSDublinCore.java20
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java61
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSMedia.java59
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSRSS20.java88
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSSimpleChapters.java30
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/atom/NSAtom.java103
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/syndication/util/SyndTypeUtils.java3
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/ChapterUtils.java13
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/Converter.java35
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java19
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/DownloadError.java5
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/EpisodeFilter.java2
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/IntList.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/LongList.java2
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java54
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/ShownotesProvider.java2
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/URIUtil.java7
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/flattr/FlattrServiceCreator.java13
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/gui/FeedItemUndoToken.java55
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/gui/MoreContentListFooterUtil.java13
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/id3reader/ChapterReader.java99
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/playback/AudioPlayer.java12
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/playback/ExternalMedia.java14
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/playback/Playable.java54
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java328
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java64
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentChapterReader.java4
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentReader.java6
88 files changed, 1956 insertions, 1626 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/ApplicationCallbacks.java b/core/src/main/java/de/danoeh/antennapod/core/ApplicationCallbacks.java
index 1064e98ac..3acc84e3b 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/ApplicationCallbacks.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/ApplicationCallbacks.java
@@ -12,12 +12,12 @@ public interface ApplicationCallbacks {
/**
* Returns a non-null instance of the application class
*/
- public Application getApplicationInstance();
+ Application getApplicationInstance();
/**
* Returns a non-null intent that starts the storage error
* activity.
*/
- public Intent getStorageErrorActivity(Context context);
+ Intent getStorageErrorActivity(Context context);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/ClientConfig.java b/core/src/main/java/de/danoeh/antennapod/core/ClientConfig.java
index 6619e706b..a96affb23 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/ClientConfig.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/ClientConfig.java
@@ -1,5 +1,12 @@
package de.danoeh.antennapod.core;
+import android.content.Context;
+
+import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
+import de.danoeh.antennapod.core.preferences.UserPreferences;
+import de.danoeh.antennapod.core.storage.PodDBAdapter;
+import de.danoeh.antennapod.core.util.NetworkUtils;
+
/**
* Stores callbacks for core classes like Services, DB classes etc. and other configuration variables.
* Apps using the core module of AntennaPod should register implementations of all interfaces here.
@@ -22,4 +29,19 @@ public class ClientConfig {
public static FlattrCallbacks flattrCallbacks;
public static DBTasksCallbacks dbTasksCallbacks;
+
+ private static boolean initialized = false;
+
+ public static synchronized void initialize(Context context) {
+ if(initialized) {
+ return;
+ }
+ PodDBAdapter.init(context);
+ UserPreferences.init(context);
+ UpdateManager.init(context);
+ PlaybackPreferences.init(context);
+ NetworkUtils.init(context);
+ initialized = true;
+ }
+
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/DBTasksCallbacks.java b/core/src/main/java/de/danoeh/antennapod/core/DBTasksCallbacks.java
index edf3e3199..11a6b2c9f 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/DBTasksCallbacks.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/DBTasksCallbacks.java
@@ -11,10 +11,10 @@ public interface DBTasksCallbacks {
/**
* Returns the client's implementation of the AutomaticDownloadAlgorithm interface.
*/
- public AutomaticDownloadAlgorithm getAutomaticDownloadAlgorithm();
+ AutomaticDownloadAlgorithm getAutomaticDownloadAlgorithm();
/**
* Returns the client's implementation of the EpisodeCacheCleanupAlgorithm interface.
*/
- public EpisodeCleanupAlgorithm getEpisodeCacheCleanupAlgorithm();
+ EpisodeCleanupAlgorithm getEpisodeCacheCleanupAlgorithm();
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/DownloadServiceCallbacks.java b/core/src/main/java/de/danoeh/antennapod/core/DownloadServiceCallbacks.java
index 286e830c5..e56445489 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/DownloadServiceCallbacks.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/DownloadServiceCallbacks.java
@@ -19,7 +19,7 @@ public interface DownloadServiceCallbacks {
*
* @return A non-null PendingIntent for the notification.
*/
- public PendingIntent getNotificationContentIntent(Context context);
+ PendingIntent getNotificationContentIntent(Context context);
/**
* Returns a PendingIntent for a notification that tells the user to enter a username
@@ -30,7 +30,7 @@ public interface DownloadServiceCallbacks {
*
* @return A non-null PendingIntent for the notification.
*/
- public PendingIntent getAuthentificationNotificationContentIntent(Context context, DownloadRequest request);
+ PendingIntent getAuthentificationNotificationContentIntent(Context context, DownloadRequest request);
/**
* Returns a PendingIntent for notification that notifies the user about the completion of downloads
@@ -40,19 +40,19 @@ public interface DownloadServiceCallbacks {
*
* @return A non-null PendingIntent for the notification or null if shouldCreateReport()==false
*/
- public PendingIntent getReportNotificationContentIntent(Context context);
+ PendingIntent getReportNotificationContentIntent(Context context);
/**
* Called by the FeedSyncThread after a feed has been downloaded and parsed.
*
* @param feed The non-null feed that has been parsed.
*/
- public void onFeedParsed(Context context, Feed feed);
+ void onFeedParsed(Context context, Feed feed);
/**
* Returns true if the DownloadService should create a report that shows the number of failed
* downloads when the service shuts down.
* */
- public boolean shouldCreateReport();
+ boolean shouldCreateReport();
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/FlattrCallbacks.java b/core/src/main/java/de/danoeh/antennapod/core/FlattrCallbacks.java
index cee1029d8..5fa6faa13 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/FlattrCallbacks.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/FlattrCallbacks.java
@@ -15,7 +15,7 @@ public interface FlattrCallbacks {
* Returns if true if the flattr integration should be activated,
* false otherwise.
*/
- public boolean flattrEnabled();
+ boolean flattrEnabled();
/**
* Returns an intent that starts the activity that is responsible for
@@ -24,13 +24,13 @@ public interface FlattrCallbacks {
* @return The intent that starts the authentication activity or null
* if flattr integration is disabled (i.e. flattrEnabled() == false).
*/
- public Intent getFlattrAuthenticationActivityIntent(Context context);
+ Intent getFlattrAuthenticationActivityIntent(Context context);
- public PendingIntent getFlattrFailedNotificationContentIntent(Context context);
+ PendingIntent getFlattrFailedNotificationContentIntent(Context context);
- public String getFlattrAppKey();
+ String getFlattrAppKey();
- public String getFlattrAppSecret();
+ String getFlattrAppSecret();
- public void handleFlattrAuthenticationSuccess(AccessToken token);
+ void handleFlattrAuthenticationSuccess(AccessToken token);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/GpodnetCallbacks.java b/core/src/main/java/de/danoeh/antennapod/core/GpodnetCallbacks.java
index 6174bce29..10797ecfb 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/GpodnetCallbacks.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/GpodnetCallbacks.java
@@ -13,7 +13,7 @@ public interface GpodnetCallbacks {
* Returns if true if the gpodder.net integration should be activated,
* false otherwise.
*/
- public boolean gpodnetEnabled();
+ boolean gpodnetEnabled();
/**
* Returns a PendingIntent for the error notification of the GpodnetSyncService.
@@ -23,5 +23,5 @@ public interface GpodnetCallbacks {
* @return A PendingIntent for the notification or null if gpodder.net integration
* has been disabled (i.e. gpodnetEnabled() == false).
*/
- public PendingIntent getGpodnetSyncServiceErrorNotificationPendingIntent(Context context);
+ PendingIntent getGpodnetSyncServiceErrorNotificationPendingIntent(Context context);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/PlaybackServiceCallbacks.java b/core/src/main/java/de/danoeh/antennapod/core/PlaybackServiceCallbacks.java
index fb01fa703..08ccb6d71 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/PlaybackServiceCallbacks.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/PlaybackServiceCallbacks.java
@@ -17,17 +17,17 @@ public interface PlaybackServiceCallbacks {
* @param mediaType The type of media that is being played.
* @return A non-null activity intent.
*/
- public Intent getPlayerActivityIntent(Context context, MediaType mediaType);
+ Intent getPlayerActivityIntent(Context context, MediaType mediaType);
/**
* Returns true if the PlaybackService should load new episodes from the queue when playback ends
* and false if the PlaybackService should ignore the queue and load no more episodes when playback
* finishes.
*/
- public boolean useQueue();
+ boolean useQueue();
/**
* Returns a drawable resource that is used for the notification of the playback service.
*/
- public int getNotificationIconResource(Context context);
+ int getNotificationIconResource(Context context);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/UpdateManager.java b/core/src/main/java/de/danoeh/antennapod/core/UpdateManager.java
new file mode 100644
index 000000000..8362c4a4e
--- /dev/null
+++ b/core/src/main/java/de/danoeh/antennapod/core/UpdateManager.java
@@ -0,0 +1,97 @@
+package de.danoeh.antennapod.core;
+
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.os.Build;
+import android.util.Log;
+
+import org.antennapod.audio.MediaPlayer;
+
+import java.io.File;
+import java.util.List;
+
+import de.danoeh.antennapod.core.feed.Feed;
+import de.danoeh.antennapod.core.feed.FeedImage;
+import de.danoeh.antennapod.core.feed.FeedItem;
+import de.danoeh.antennapod.core.preferences.UserPreferences;
+import de.danoeh.antennapod.core.storage.DBReader;
+import de.danoeh.antennapod.core.storage.DBWriter;
+
+/*
+ * This class's job is do perform maintenance tasks whenever the app has been updated
+ */
+public class UpdateManager {
+
+ public static final String TAG = UpdateManager.class.getSimpleName();
+
+ private static final String PREF_NAME = "app_version";
+ private static final String KEY_VERSION_CODE = "version_code";
+
+ private static int currentVersionCode;
+
+ private static Context context;
+ private static SharedPreferences prefs;
+
+ public static void init(Context context) {
+ UpdateManager.context = context;
+ prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
+ PackageManager pm = context.getPackageManager();
+ try {
+ PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
+ currentVersionCode = info.versionCode;
+ } catch (PackageManager.NameNotFoundException e) {
+ Log.e(TAG, "Failed to obtain package info for package name: " + context.getPackageName(), e);
+ currentVersionCode = 0;
+ return;
+ }
+ final int oldVersionCode = getStoredVersionCode();
+ Log.d(TAG, "old: " + oldVersionCode + ", current: " + currentVersionCode);
+ if(oldVersionCode < currentVersionCode) {
+ onUpgrade(oldVersionCode, currentVersionCode);
+ setCurrentVersionCode();
+ }
+ }
+
+ public static int getStoredVersionCode() {
+ return prefs.getInt(KEY_VERSION_CODE, -1);
+ }
+
+ public static void setCurrentVersionCode() {
+ prefs.edit().putInt(KEY_VERSION_CODE, currentVersionCode).apply();
+ }
+
+ private static void onUpgrade(final int oldVersionCode, final int newVersionCode) {
+ if(oldVersionCode < 1030099) {
+ // delete the now obsolete image cache
+ // from now on, Glide will handle caching images
+ new Thread() {
+ public void run() {
+ List<Feed> feeds = DBReader.getFeedList();
+ for (Feed podcast : feeds) {
+ List<FeedItem> episodes = DBReader.getFeedItemList(podcast);
+ for (FeedItem episode : episodes) {
+ FeedImage image = episode.getImage();
+ if (image != null && image.isDownloaded() && image.getFile_url() != null) {
+ File imageFile = new File(image.getFile_url());
+ if (imageFile.exists()) {
+ imageFile.delete();
+ }
+ image.setFile_url(null); // calls setDownloaded(false)
+ DBWriter.setFeedImage(image);
+ }
+ }
+ }
+ }
+ }.start();
+ }
+ if(oldVersionCode < 1050004) {
+ if(MediaPlayer.isPrestoLibraryInstalled(context) && Build.VERSION.SDK_INT >= 16) {
+ UserPreferences.enableSonic(true);
+ }
+ }
+ }
+
+}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/asynctask/FeedRemover.java b/core/src/main/java/de/danoeh/antennapod/core/asynctask/FeedRemover.java
index 7ff622f34..e475e696c 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/asynctask/FeedRemover.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/asynctask/FeedRemover.java
@@ -30,9 +30,7 @@ public class FeedRemover extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
try {
DBWriter.deleteFeed(context, feed.getId()).get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return null;
diff --git a/core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrClickWorker.java b/core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrClickWorker.java
index ac032fcc0..3ddaba52e 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrClickWorker.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrClickWorker.java
@@ -17,6 +17,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicInteger;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.ClientConfig;
@@ -44,10 +45,10 @@ public class FlattrClickWorker extends AsyncTask<Void, Integer, FlattrClickWorke
private final Context context;
- public static enum ExitCode {EXIT_NORMAL, NO_TOKEN, NO_NETWORK, NO_THINGS}
+ public enum ExitCode {EXIT_NORMAL, NO_TOKEN, NO_NETWORK, NO_THINGS}
- private volatile int countFailed = 0;
- private volatile int countSuccess = 0;
+ private final AtomicInteger countFailed = new AtomicInteger();
+ private final AtomicInteger countSuccess = new AtomicInteger();
private volatile FlattrThing extraFlattrThing;
@@ -105,7 +106,7 @@ public class FlattrClickWorker extends AsyncTask<Void, Integer, FlattrClickWorke
return ExitCode.NO_THINGS;
}
- List<Future> dbFutures = new LinkedList<Future>();
+ List<Future> dbFutures = new LinkedList<>();
for (FlattrThing thing : flattrQueue) {
if (BuildConfig.DEBUG) Log.d(TAG, "Processing " + thing.getTitle());
@@ -114,12 +115,12 @@ public class FlattrClickWorker extends AsyncTask<Void, Integer, FlattrClickWorke
FlattrUtils.clickUrl(context, thing.getPaymentLink());
thing.getFlattrStatus().setFlattred();
publishProgress(R.string.flattr_click_success);
- countSuccess++;
+ countSuccess.incrementAndGet();
} catch (FlattrException e) {
e.printStackTrace();
- countFailed++;
- if (countFailed == 1) {
+ int failed = countFailed.incrementAndGet();
+ if (failed == 1) {
exception = e;
}
}
@@ -133,9 +134,7 @@ public class FlattrClickWorker extends AsyncTask<Void, Integer, FlattrClickWorke
for (Future f : dbFutures) {
try {
f.get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
@@ -148,7 +147,7 @@ public class FlattrClickWorker extends AsyncTask<Void, Integer, FlattrClickWorke
super.onPostExecute(exitCode);
switch (exitCode) {
case EXIT_NORMAL:
- if (countFailed > 0) {
+ if (countFailed.get() > 0) {
postFlattrFailedNotification();
}
break;
@@ -190,7 +189,8 @@ public class FlattrClickWorker extends AsyncTask<Void, Integer, FlattrClickWorke
}
private void postFlattrFailedNotification() {
- if (countFailed == 0) {
+ int failed = countFailed.get();
+ if (failed == 0) {
return;
}
@@ -198,15 +198,15 @@ public class FlattrClickWorker extends AsyncTask<Void, Integer, FlattrClickWorke
String title;
String subtext;
- if (countFailed == 1) {
+ if (failed == 1) {
title = context.getString(R.string.flattrd_failed_label);
String exceptionMsg = (exception.getMessage() != null) ? exception.getMessage() : "";
subtext = context.getString(R.string.flattr_click_failure, extraFlattrThing.getTitle())
+ "\n" + exceptionMsg;
} else {
title = context.getString(R.string.flattrd_label);
- subtext = context.getString(R.string.flattr_click_success_count, countSuccess) + "\n"
- + context.getString(R.string.flattr_click_failure_count, countFailed);
+ subtext = context.getString(R.string.flattr_click_success_count, countSuccess.get()) + "\n"
+ + context.getString(R.string.flattr_click_failure_count, failed);
}
Notification notification = new NotificationCompat.Builder(context)
diff --git a/core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrStatusFetcher.java b/core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrStatusFetcher.java
index 888591e89..4c084eaaf 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrStatusFetcher.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/asynctask/FlattrStatusFetcher.java
@@ -2,15 +2,17 @@ package de.danoeh.antennapod.core.asynctask;
import android.content.Context;
import android.util.Log;
-import de.danoeh.antennapod.core.BuildConfig;
-import de.danoeh.antennapod.core.storage.DBWriter;
-import de.danoeh.antennapod.core.util.flattr.FlattrUtils;
+
import org.shredzone.flattr4j.exception.FlattrException;
import org.shredzone.flattr4j.model.Flattr;
import java.util.List;
import java.util.concurrent.ExecutionException;
+import de.danoeh.antennapod.core.BuildConfig;
+import de.danoeh.antennapod.core.storage.DBWriter;
+import de.danoeh.antennapod.core.util.flattr.FlattrUtils;
+
/**
* Fetch list of flattred things and flattr status in database in a background thread.
*/
@@ -36,9 +38,7 @@ public class FlattrStatusFetcher extends Thread {
} catch (FlattrException e) {
e.printStackTrace();
Log.d(TAG, "flattrQueue exception retrieving list with flattred items " + e.getMessage());
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/asynctask/ImageResource.java b/core/src/main/java/de/danoeh/antennapod/core/asynctask/ImageResource.java
index edd69f15b..992321441 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/asynctask/ImageResource.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/asynctask/ImageResource.java
@@ -16,13 +16,13 @@ public interface ImageResource {
* <p/>
* For example implementations, see FeedMedia or ExternalMedia.
*/
- public static final String SCHEME_MEDIA = "media";
+ String SCHEME_MEDIA = "media";
/**
* Parameter key for an encoded fallback Uri. This Uri MUST point to a local image file
*/
- public static final String PARAM_FALLBACK = "fallback";
+ String PARAM_FALLBACK = "fallback";
/**
* Returns a Uri to the image or null if no image is available.
@@ -33,5 +33,5 @@ public interface ImageResource {
* The Uri can also have an optional fallback-URL if loading the default URL
* failed (see PARAM_FALLBACK).
*/
- public Uri getImageUri();
+ Uri getImageUri();
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java b/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java
index 5ea0ba904..982015314 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java
@@ -8,7 +8,6 @@ import android.content.Context;
import android.os.ParcelFileDescriptor;
import android.util.Log;
-import de.danoeh.antennapod.core.BuildConfig;
import org.xmlpull.v1.XmlPullParserException;
import java.io.ByteArrayOutputStream;
@@ -26,8 +25,8 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Date;
+import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.opml.OpmlElement;
import de.danoeh.antennapod.core.opml.OpmlReader;
@@ -45,13 +44,13 @@ public class OpmlBackupAgent extends BackupAgentHelper {
addHelper(OPML_BACKUP_KEY, new OpmlBackupHelper(this));
}
- private static final void LOGD(String tag, String msg) {
+ private static void LOGD(String tag, String msg) {
if (BuildConfig.DEBUG && Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, msg);
}
}
- private static final void LOGD(String tag, String msg, Throwable tr) {
+ private static void LOGD(String tag, String msg, Throwable tr) {
if (BuildConfig.DEBUG && Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, msg, tr);
}
@@ -156,10 +155,9 @@ public class OpmlBackupAgent extends BackupAgentHelper {
ArrayList<OpmlElement> opmlElements = new OpmlReader().readDocument(reader);
mChecksum = digester == null ? null : digester.digest();
DownloadRequester downloader = DownloadRequester.getInstance();
- Date lastUpdated = new Date();
for (OpmlElement opmlElem : opmlElements) {
- Feed feed = new Feed(opmlElem.getXmlUrl(), lastUpdated, opmlElem.getText());
+ Feed feed = new Feed(opmlElem.getXmlUrl(), null, opmlElem.getText());
try {
downloader.downloadFeed(mContext, feed);
diff --git a/core/src/main/java/de/danoeh/antennapod/core/dialog/ConfirmationDialog.java b/core/src/main/java/de/danoeh/antennapod/core/dialog/ConfirmationDialog.java
index abb75e5e7..266526d82 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/dialog/ConfirmationDialog.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/dialog/ConfirmationDialog.java
@@ -49,28 +49,10 @@ public abstract class ConfirmationDialog {
builder.setTitle(titleId);
builder.setMessage(messageId);
builder.setPositiveButton(positiveText != 0 ? positiveText : R.string.confirm_label,
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- onConfirmButtonPressed(dialog);
- }
- });
+ (dialog, which) -> onConfirmButtonPressed(dialog));
builder.setNegativeButton(negativeText != 0 ? negativeText : R.string.cancel_label,
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- onCancelButtonPressed(dialog);
- }
- });
- builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
-
- @Override
- public void onCancel(DialogInterface dialog) {
- onCancelButtonPressed(dialog);
- }
- });
+ (dialog, which) -> onCancelButtonPressed(dialog));
+ builder.setOnCancelListener(ConfirmationDialog.this::onCancelButtonPressed);
return builder.create();
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/dialog/DownloadRequestErrorDialogCreator.java b/core/src/main/java/de/danoeh/antennapod/core/dialog/DownloadRequestErrorDialogCreator.java
index b7e79431d..b1beac315 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/dialog/DownloadRequestErrorDialogCreator.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/dialog/DownloadRequestErrorDialogCreator.java
@@ -1,7 +1,6 @@
package de.danoeh.antennapod.core.dialog;
import android.content.Context;
-import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import de.danoeh.antennapod.core.R;
@@ -15,13 +14,7 @@ public class DownloadRequestErrorDialogCreator {
String errorMessage) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setNeutralButton(android.R.string.ok,
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- })
+ (dialog, which) -> dialog.dismiss())
.setTitle(R.string.download_error_request_error)
.setMessage(
context.getString(R.string.download_request_error_dialog_message_prefix)
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/EventDistributor.java b/core/src/main/java/de/danoeh/antennapod/core/feed/EventDistributor.java
index 7ccb742fb..dbad084c7 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/feed/EventDistributor.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/feed/EventDistributor.java
@@ -34,7 +34,7 @@ public class EventDistributor extends Observable {
private EventDistributor() {
this.handler = new Handler();
- events = new ConcurrentLinkedQueue<Integer>();
+ events = new ConcurrentLinkedQueue<>();
}
public static synchronized EventDistributor getInstance() {
@@ -54,13 +54,7 @@ public class EventDistributor extends Observable {
public void addEvent(Integer i) {
events.offer(i);
- handler.post(new Runnable() {
-
- @Override
- public void run() {
- processEventQueue();
- }
- });
+ handler.post(EventDistributor.this::processEventQueue);
}
private void processEventQueue() {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/Feed.java b/core/src/main/java/de/danoeh/antennapod/core/feed/Feed.java
index 4be788f33..67ac2d280 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/feed/Feed.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/feed/Feed.java
@@ -44,10 +44,12 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
private String author;
private FeedImage image;
private List<FeedItem> items;
+
/**
- * Date of last refresh.
+ * String that identifies the last update (adopted from Last-Modified or ETag header)
*/
- private Date lastUpdate;
+ private String lastUpdate;
+
private FlattrStatus flattrStatus;
private String paymentLink;
/**
@@ -91,18 +93,14 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
/**
* This constructor is used for restoring a feed from the database.
*/
- public Feed(long id, Date lastUpdate, String title, String link, String description, String paymentLink,
+ public Feed(long id, String lastUpdate, String title, String link, String description, String paymentLink,
String author, String language, String type, String feedIdentifier, FeedImage image, String fileUrl,
String downloadUrl, boolean downloaded, FlattrStatus status, boolean paged, String nextPageLink,
String filter, boolean lastUpdateFailed) {
super(fileUrl, downloadUrl, downloaded);
this.id = id;
this.title = title;
- if (lastUpdate != null) {
- this.lastUpdate = (Date) lastUpdate.clone();
- } else {
- this.lastUpdate = null;
- }
+ this.lastUpdate = lastUpdate;
this.link = link;
this.description = description;
this.paymentLink = paymentLink;
@@ -114,7 +112,7 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
this.flattrStatus = status;
this.paged = paged;
this.nextPageLink = nextPageLink;
- this.items = new ArrayList<FeedItem>();
+ this.items = new ArrayList<>();
if(filter != null) {
this.itemfilter = new FeedItemFilter(filter);
} else {
@@ -126,7 +124,7 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
/**
* This constructor is used for test purposes and uses a default flattr status object.
*/
- public Feed(long id, Date lastUpdate, String title, String link, String description, String paymentLink,
+ public Feed(long id, String lastUpdate, String title, String link, String description, String paymentLink,
String author, String language, String type, String feedIdentifier, FeedImage image, String fileUrl,
String downloadUrl, boolean downloaded) {
this(id, lastUpdate, title, link, description, paymentLink, author, language, type, feedIdentifier, image,
@@ -138,7 +136,6 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
*/
public Feed() {
super();
- lastUpdate = new Date();
this.flattrStatus = new FlattrStatus();
}
@@ -146,9 +143,9 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
* This constructor is used for requesting a feed download (it must not be used for anything else!). It should NOT be
* used if the title of the feed is already known.
*/
- public Feed(String url, Date lastUpdate) {
+ public Feed(String url, String lastUpdate) {
super(null, url, false);
- this.lastUpdate = (lastUpdate != null) ? (Date) lastUpdate.clone() : null;
+ this.lastUpdate = lastUpdate;
this.flattrStatus = new FlattrStatus();
}
@@ -156,7 +153,7 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
* This constructor is used for requesting a feed download (it must not be used for anything else!). It should be
* used if the title of the feed is already known.
*/
- public Feed(String url, Date lastUpdate, String title) {
+ public Feed(String url, String lastUpdate, String title) {
this(url, lastUpdate);
this.title = title;
this.flattrStatus = new FlattrStatus();
@@ -166,7 +163,7 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
* This constructor is used for requesting a feed download (it must not be used for anything else!). It should be
* used if the title of the feed is already known.
*/
- public Feed(String url, Date lastUpdate, String title, String username, String password) {
+ public Feed(String url, String lastUpdate, String title, String username, String password) {
this(url, lastUpdate, title);
preferences = new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL, username, password);
}
@@ -191,11 +188,9 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
int indexHide = cursor.getColumnIndex(PodDBAdapter.KEY_HIDE);
int indexLastUpdateFailed = cursor.getColumnIndex(PodDBAdapter.KEY_LAST_UPDATE_FAILED);
- Date lastUpdate = new Date(cursor.getLong(indexLastUpdate));
-
Feed feed = new Feed(
cursor.getLong(indexId),
- lastUpdate,
+ cursor.getString(indexLastUpdate),
cursor.getString(indexTitle),
cursor.getString(indexLink),
cursor.getString(indexDescription),
@@ -240,7 +235,7 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
*/
public boolean hasUnplayedItems() {
for (FeedItem item : items) {
- if (false == item.isNew() && false == item.isPlayed()) {
+ if (!item.isNew() && !item.isPlayed()) {
return true;
}
}
@@ -329,12 +324,11 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
if (super.compareWithOther(other)) {
return true;
}
- if (!title.equals(other.title)) {
+ if (!TextUtils.equals(title, other.title)) {
return true;
}
if (other.feedIdentifier != null) {
- if (feedIdentifier == null
- || !feedIdentifier.equals(other.feedIdentifier)) {
+ if (feedIdentifier == null || !feedIdentifier.equals(other.feedIdentifier)) {
return true;
}
}
@@ -430,12 +424,12 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
this.items = list;
}
- public Date getLastUpdate() {
- return (lastUpdate != null) ? (Date) lastUpdate.clone() : null;
+ public String getLastUpdate() {
+ return lastUpdate;
}
- public void setLastUpdate(Date lastUpdate) {
- this.lastUpdate = (lastUpdate != null) ? (Date) lastUpdate.clone() : null;
+ public void setLastUpdate(String lastModified) {
+ this.lastUpdate = lastModified;
}
public String getFeedIdentifier() {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedComponent.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedComponent.java
index 05115c1ea..90b5e50b7 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedComponent.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedComponent.java
@@ -54,9 +54,8 @@ public abstract class FeedComponent {
FeedComponent that = (FeedComponent) o;
- if (id != that.id) return false;
+ return id == that.id;
- return true;
}
@Override
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedFile.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedFile.java
index 3dc58654b..ca9af058b 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedFile.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedFile.java
@@ -1,5 +1,7 @@
package de.danoeh.antennapod.core.feed;
+import android.text.TextUtils;
+
import java.io.File;
/**
@@ -54,7 +56,7 @@ public abstract class FeedFile extends FeedComponent {
if (super.compareWithOther(other)) {
return true;
}
- if (!download_url.equals(other.download_url)) {
+ if (!TextUtils.equals(download_url, other.download_url)) {
return true;
}
return false;
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java
index d8c32f55e..4921c0576 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java
@@ -171,9 +171,8 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Flattr
String itemIdentifier = cursor.getString(indexItemIdentifier);
long autoDownload = cursor.getLong(indexAutoDownload);
- FeedItem item = new FeedItem(id, title, link, pubDate, paymentLink, feedId, flattrStatus,
+ return new FeedItem(id, title, link, pubDate, paymentLink, feedId, flattrStatus,
hasChapters, null, state, itemIdentifier, autoDownload);
- return item;
}
public void updateFromOther(FeedItem other) {
@@ -196,6 +195,8 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Flattr
if (other.media != null) {
if (media == null) {
setMedia(other.media);
+ // reset to new if feed item did link to a file before
+ setNew();
} else if (media.compareWithOther(other.media)) {
media.updateFromOther(other.media);
}
@@ -359,24 +360,16 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Flattr
}
private boolean isPlaying() {
- if (media != null) {
- return media.isPlaying();
- }
- return false;
+ return media != null && media.isPlaying();
}
@Override
public Callable<String> loadShownotes() {
- return new Callable<String>() {
- @Override
- public String call() throws Exception {
-
- if (contentEncoded == null || description == null) {
- DBReader.loadExtraInformationOfFeedItem(FeedItem.this);
-
- }
- return (contentEncoded != null) ? contentEncoded : description;
+ return () -> {
+ if (contentEncoded == null || description == null) {
+ DBReader.loadExtraInformationOfFeedItem(FeedItem.this);
}
+ return (contentEncoded != null) ? contentEncoded : description;
};
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java
index 56b996d1c..991499316 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java
@@ -8,7 +8,6 @@ import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
-import android.text.TextUtils;
import java.util.Date;
import java.util.List;
@@ -168,10 +167,6 @@ public class FeedMedia extends FeedFile implements Playable {
}
public void updateFromOther(FeedMedia other) {
- // reset to new if feed item did link to a file before
- if(TextUtils.isEmpty(download_url) && !TextUtils.isEmpty(other.download_url)) {
- item.setNew();
- }
super.updateFromOther(other);
if (other.size > 0) {
size = other.size;
@@ -386,11 +381,14 @@ public class FeedMedia extends FeedFile implements Playable {
// check if chapters are stored in db and not loaded yet.
if (item != null && item.hasChapters() && item.getChapters() == null) {
DBReader.loadChaptersOfFeedItem(item);
- } else if (item != null && item.getChapters() == null && !localFileAvailable()) {
- ChapterUtils.loadChaptersFromStreamUrl(this);
+ } else if (item != null && item.getChapters() == null) {
+ if(localFileAvailable()) {
+ ChapterUtils.loadChaptersFromFileUrl(this);
+ } else {
+ ChapterUtils.loadChaptersFromStreamUrl(this);
+ }
if (getChapters() != null && item != null) {
- DBWriter.setFeedItem(
- item);
+ DBWriter.setFeedItem(item);
}
}
}
@@ -400,10 +398,10 @@ public class FeedMedia extends FeedFile implements Playable {
if (item == null) {
return null;
}
- if (getItem().getTitle() != null) {
- return getItem().getTitle();
+ if (item.getTitle() != null) {
+ return item.getTitle();
} else {
- return getItem().getIdentifyingValue();
+ return item.getIdentifyingValue();
}
}
@@ -412,7 +410,7 @@ public class FeedMedia extends FeedFile implements Playable {
if (item == null) {
return null;
}
- return getItem().getChapters();
+ return item.getChapters();
}
@Override
@@ -420,15 +418,15 @@ public class FeedMedia extends FeedFile implements Playable {
if (item == null) {
return null;
}
- return getItem().getLink();
+ return item.getLink();
}
@Override
public String getFeedTitle() {
- if (item == null) {
+ if (item == null || item.getFeed() == null) {
return null;
}
- return getItem().getFeed().getTitle();
+ return item.getFeed().getTitle();
}
@Override
@@ -451,7 +449,7 @@ public class FeedMedia extends FeedFile implements Playable {
if (item == null) {
return null;
}
- return getItem().getPaymentLink();
+ return item.getPaymentLink();
}
@Override
@@ -489,25 +487,24 @@ public class FeedMedia extends FeedFile implements Playable {
@Override
public void setChapters(List<Chapter> chapters) {
- getItem().setChapters(chapters);
+ if(item != null) {
+ item.setChapters(chapters);
+ }
}
@Override
public Callable<String> loadShownotes() {
- return new Callable<String>() {
- @Override
- public String call() throws Exception {
- if (item == null) {
- item = DBReader.getFeedItem(
- itemID);
- }
- if (item.getContentEncoded() == null || item.getDescription() == null) {
- DBReader.loadExtraInformationOfFeedItem(
- item);
+ return () -> {
+ if (item == null) {
+ item = DBReader.getFeedItem(
+ itemID);
+ }
+ if (item.getContentEncoded() == null || item.getDescription() == null) {
+ DBReader.loadExtraInformationOfFeedItem(
+ item);
- }
- return (item.getContentEncoded() != null) ? item.getContentEncoded() : item.getDescription();
}
+ return (item.getContentEncoded() != null) ? item.getContentEncoded() : item.getDescription();
};
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java
index faf23a37a..e2b5dd7c2 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java
@@ -68,7 +68,7 @@ public class FeedPreferences {
/**
* @return the filter for this feed
*/
- public FeedFilter getFilter() {
+ public @NonNull FeedFilter getFilter() {
return filter;
}
@@ -95,8 +95,9 @@ public class FeedPreferences {
* @return True if the two objects are different.
*/
public boolean compareWithOther(FeedPreferences other) {
- if (other == null)
+ if (other == null) {
return true;
+ }
if (!TextUtils.equals(username, other.username)) {
return true;
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java b/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java
index 86baa459c..513264a05 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java
@@ -18,7 +18,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
-import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import de.danoeh.antennapod.core.service.download.HttpDownloader;
import de.danoeh.antennapod.core.storage.DBReader;
@@ -107,7 +106,6 @@ public class ApOkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
public Response intercept(Chain chain) throws IOException {
com.squareup.okhttp.Request request = chain.request();
String url = request.urlString();
- Context context = ClientConfig.applicationCallbacks.getApplicationInstance();
String authentication = DBReader.getImageAuthentication(url);
if(TextUtils.isEmpty(authentication)) {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/GpodnetService.java b/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/GpodnetService.java
index a24e3a485..9f716e546 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/GpodnetService.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/GpodnetService.java
@@ -80,7 +80,7 @@ public class GpodnetService {
String response = executeRequest(request);
try {
JSONArray jsonTagList = new JSONArray(response);
- List<GpodnetTag> tagList = new ArrayList<GpodnetTag>(
+ List<GpodnetTag> tagList = new ArrayList<>(
jsonTagList.length());
for (int i = 0; i < jsonTagList.length(); i++) {
JSONObject jObj = jsonTagList.getJSONObject(i);
@@ -318,8 +318,7 @@ public class GpodnetService {
URL url = new URI(BASE_SCHEME, BASE_HOST, String.format(
"/subscriptions/%s.opml", username), null).toURL();
Request.Builder request = new Request.Builder().url(url);
- String response = executeRequest(request);
- return response;
+ return executeRequest(request);
} catch (MalformedURLException | URISyntaxException e) {
e.printStackTrace();
throw new GpodnetServiceException(e);
@@ -660,7 +659,7 @@ public class GpodnetService {
private List<GpodnetPodcast> readPodcastListFromJSONArray(@NonNull JSONArray array)
throws JSONException {
- List<GpodnetPodcast> result = new ArrayList<GpodnetPodcast>(
+ List<GpodnetPodcast> result = new ArrayList<>(
array.length());
for (int i = 0; i < array.length(); i++) {
result.add(readPodcastFromJSONObject(array.getJSONObject(i)));
@@ -712,7 +711,7 @@ public class GpodnetService {
private List<GpodnetDevice> readDeviceListFromJSONArray(@NonNull JSONArray array)
throws JSONException {
- List<GpodnetDevice> result = new ArrayList<GpodnetDevice>(
+ List<GpodnetDevice> result = new ArrayList<>(
array.length());
for (int i = 0; i < array.length(); i++) {
result.add(readDeviceFromJSONObject(array.getJSONObject(i)));
@@ -732,16 +731,22 @@ public class GpodnetService {
private GpodnetSubscriptionChange readSubscriptionChangesFromJSONObject(
@NonNull JSONObject object) throws JSONException {
- List<String> added = new LinkedList<String>();
+ List<String> added = new LinkedList<>();
JSONArray jsonAdded = object.getJSONArray("add");
for (int i = 0; i < jsonAdded.length(); i++) {
- added.add(jsonAdded.getString(i));
+ String addedUrl = jsonAdded.getString(i);
+ // gpodder escapes colons unnecessarily
+ addedUrl = addedUrl.replace("%3A", ":");
+ added.add(addedUrl);
}
- List<String> removed = new LinkedList<String>();
+ List<String> removed = new LinkedList<>();
JSONArray jsonRemoved = object.getJSONArray("remove");
for (int i = 0; i < jsonRemoved.length(); i++) {
- removed.add(jsonRemoved.getString(i));
+ String removedUrl = jsonRemoved.getString(i);
+ // gpodder escapes colons unnecessarily
+ removedUrl = removedUrl.replace("%3A", ":");
+ removed.add(removedUrl);
}
long timestamp = object.getLong("timestamp");
@@ -751,7 +756,7 @@ public class GpodnetService {
private GpodnetEpisodeActionGetResponse readEpisodeActionsFromJSONObject(
@NonNull JSONObject object) throws JSONException {
- List<GpodnetEpisodeAction> episodeActions = new ArrayList<GpodnetEpisodeAction>();
+ List<GpodnetEpisodeAction> episodeActions = new ArrayList<>();
long timestamp = object.getLong("timestamp");
JSONArray jsonActions = object.getJSONArray("actions");
diff --git a/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetDevice.java b/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetDevice.java
index 2d49c170a..79eb33cb5 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetDevice.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetDevice.java
@@ -25,7 +25,7 @@ public class GpodnetDevice {
+ type + ", subscriptions=" + subscriptions + "]";
}
- public static enum DeviceType {
+ public enum DeviceType {
DESKTOP, LAPTOP, MOBILE, SERVER, OTHER;
static DeviceType fromString(String s) {
@@ -33,16 +33,17 @@ public class GpodnetDevice {
return OTHER;
}
- if (s.equals("desktop")) {
- return DESKTOP;
- } else if (s.equals("laptop")) {
- return LAPTOP;
- } else if (s.equals("mobile")) {
- return MOBILE;
- } else if (s.equals("server")) {
- return SERVER;
- } else {
- return OTHER;
+ switch (s) {
+ case "desktop":
+ return DESKTOP;
+ case "laptop":
+ return LAPTOP;
+ case "mobile":
+ return MOBILE;
+ case "server":
+ return SERVER;
+ default:
+ return OTHER;
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeAction.java b/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeAction.java
index 2d174a6bc..9627ecae6 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeAction.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeAction.java
@@ -61,14 +61,13 @@ public class GpodnetEpisodeAction {
String deviceId = fields[2];
try {
Action action = Action.valueOf(fields[3]);
- GpodnetEpisodeAction result = new Builder(podcast, episode, action)
+ return new Builder(podcast, episode, action)
.deviceId(deviceId)
- .timestamp(new Date(Long.valueOf(fields[4])))
- .started(Integer.valueOf(fields[5]))
- .position(Integer.valueOf(fields[6]))
- .total(Integer.valueOf(fields[7]))
+ .timestamp(new Date(Long.parseLong(fields[4])))
+ .started(Integer.parseInt(fields[5]))
+ .position(Integer.parseInt(fields[6]))
+ .total(Integer.parseInt(fields[7]))
.build();
- return result;
} catch(IllegalArgumentException e) {
Log.e(TAG, "readFromString(" + s + "): " + e.getMessage());
return null;
diff --git a/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeActionPostResponse.java b/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeActionPostResponse.java
index 5f096db14..03c33c9a1 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeActionPostResponse.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/gpoddernet/model/GpodnetEpisodeActionPostResponse.java
@@ -38,7 +38,7 @@ public class GpodnetEpisodeActionPostResponse {
final JSONObject object = new JSONObject(objectString);
final long timestamp = object.getLong("timestamp");
JSONArray urls = object.getJSONArray("update_urls");
- Map<String, String> updatedUrls = new ArrayMap<String, String>(urls.length());
+ Map<String, String> updatedUrls = new ArrayMap<>(urls.length());
for (int i = 0; i < urls.length(); i++) {
JSONArray urlPair = urls.getJSONArray(i);
updatedUrls.put(urlPair.getString(0), urlPair.getString(1));
diff --git a/core/src/main/java/de/danoeh/antennapod/core/menuhandler/MenuItemUtils.java b/core/src/main/java/de/danoeh/antennapod/core/menuhandler/MenuItemUtils.java
index b8d17bcce..f63c9aeb0 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/menuhandler/MenuItemUtils.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/menuhandler/MenuItemUtils.java
@@ -32,7 +32,7 @@ public class MenuItemUtils {
}
}
- public static interface UpdateRefreshMenuItemChecker {
- public boolean isRefreshing();
+ public interface UpdateRefreshMenuItemChecker {
+ boolean isRefreshing();
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/opml/OpmlReader.java b/core/src/main/java/de/danoeh/antennapod/core/opml/OpmlReader.java
index 775129d09..17afc7904 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/opml/OpmlReader.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/opml/OpmlReader.java
@@ -1,7 +1,7 @@
package de.danoeh.antennapod.core.opml;
import android.util.Log;
-import de.danoeh.antennapod.core.BuildConfig;
+
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
@@ -10,6 +10,8 @@ import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
+import de.danoeh.antennapod.core.BuildConfig;
+
/** Reads OPML documents. */
public class OpmlReader {
private static final String TAG = "OpmlReader";
@@ -27,7 +29,7 @@ public class OpmlReader {
*/
public ArrayList<OpmlElement> readDocument(Reader reader)
throws XmlPullParserException, IOException {
- elementList = new ArrayList<OpmlElement>();
+ elementList = new ArrayList<>();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/GpodnetPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/GpodnetPreferences.java
index edd7b807a..6d4d3baa6 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/preferences/GpodnetPreferences.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/GpodnetPreferences.java
@@ -189,7 +189,7 @@ public class GpodnetPreferences {
public static Set<String> getAddedFeedsCopy() {
ensurePreferencesLoaded();
- Set<String> copy = new HashSet<String>();
+ Set<String> copy = new HashSet<>();
feedListLock.lock();
copy.addAll(addedFeeds);
feedListLock.unlock();
@@ -206,7 +206,7 @@ public class GpodnetPreferences {
public static Set<String> getRemovedFeedsCopy() {
ensurePreferencesLoaded();
- Set<String> copy = new HashSet<String>();
+ Set<String> copy = new HashSet<>();
feedListLock.lock();
copy.addAll(removedFeeds);
feedListLock.unlock();
@@ -232,7 +232,7 @@ public class GpodnetPreferences {
public static List<GpodnetEpisodeAction> getQueuedEpisodeActions() {
ensurePreferencesLoaded();
- List<GpodnetEpisodeAction> copy = new ArrayList();
+ List<GpodnetEpisodeAction> copy = new ArrayList<>();
feedListLock.lock();
copy.addAll(queuedEpisodeActions);
feedListLock.unlock();
@@ -272,7 +272,7 @@ public class GpodnetPreferences {
}
private static Set<String> readListFromString(String s) {
- Set<String> result = new HashSet<String>();
+ Set<String> result = new HashSet<>();
for (String item : s.split(" ")) {
result.add(item);
}
@@ -290,7 +290,7 @@ public class GpodnetPreferences {
private static List<GpodnetEpisodeAction> readEpisodeActionsFromString(String s) {
String[] lines = s.split("\n");
- List<GpodnetEpisodeAction> result = new ArrayList<GpodnetEpisodeAction>(lines.length);
+ List<GpodnetEpisodeAction> result = new ArrayList<>(lines.length);
for(String line : lines) {
if(TextUtils.isEmpty(line)) {
GpodnetEpisodeAction action = GpodnetEpisodeAction.readFromString(line);
diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
index 6c0aff15e..a38f50cfc 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
@@ -17,6 +17,7 @@ import org.json.JSONException;
import java.io.File;
import java.io.IOException;
+import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@@ -25,10 +26,12 @@ import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.receiver.FeedUpdateReceiver;
+import de.danoeh.antennapod.core.service.download.ProxyConfig;
import de.danoeh.antennapod.core.storage.APCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.APNullCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.APQueueCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.EpisodeCleanupAlgorithm;
+import de.danoeh.antennapod.core.util.Converter;
/**
* Provides access to preferences set by the user in the settings screen. A
@@ -78,6 +81,11 @@ public class UserPreferences {
public static final String PREF_ENABLE_AUTODL_ON_BATTERY = "prefEnableAutoDownloadOnBattery";
public static final String PREF_ENABLE_AUTODL_WIFI_FILTER = "prefEnableAutoDownloadWifiFilter";
public static final String PREF_AUTODL_SELECTED_NETWORKS = "prefAutodownloadSelectedNetworks";
+ public static final String PREF_PROXY_TYPE = "prefProxyType";
+ public static final String PREF_PROXY_HOST = "prefProxyHost";
+ public static final String PREF_PROXY_PORT = "prefProxyPort";
+ public static final String PREF_PROXY_USER = "prefProxyUser";
+ public static final String PREF_PROXY_PASSWORD = "prefProxyPassword";
// Services
public static final String PREF_AUTO_FLATTR = "pref_auto_flattr";
@@ -104,6 +112,7 @@ public class UserPreferences {
public static final int EPISODE_CLEANUP_QUEUE = -1;
public static final int EPISODE_CLEANUP_NULL = -2;
public static final int EPISODE_CLEANUP_DEFAULT = 0;
+ private static final String PREF_SHOW_SUBSCRIPTIONS_IN_DRAWER = "prefShowSubscriptionsInDrawer";
// Constants
private static int EPISODE_CACHE_SIZE_UNLIMITED = -1;
@@ -158,12 +167,16 @@ public class UserPreferences {
public static int getFeedOrder() {
String value = prefs.getString(PREF_DRAWER_FEED_ORDER, "0");
- return Integer.valueOf(value);
+ return Integer.parseInt(value);
}
public static int getFeedCounterSetting() {
String value = prefs.getString(PREF_DRAWER_FEED_COUNTER, "0");
- return Integer.valueOf(value);
+ return Integer.parseInt(value);
+ }
+
+ public static boolean showSubscriptionsInDrawer() {
+ return prefs.getBoolean(PREF_SHOW_SUBSCRIPTIONS_IN_DRAWER, true);
}
/**
@@ -243,7 +256,7 @@ public class UserPreferences {
}
public static int getSmartMarkAsPlayedSecs() {
- return Integer.valueOf(prefs.getString(PREF_SMART_MARK_AS_PLAYED_SECS, "30"));
+ return Integer.parseInt(prefs.getString(PREF_SMART_MARK_AS_PLAYED_SECS, "30"));
}
public static boolean isAutoFlattr() {
@@ -260,20 +273,20 @@ public class UserPreferences {
public static float getLeftVolume() {
int volume = prefs.getInt(PREF_LEFT_VOLUME, 100);
- if(volume == 100) {
- return 1.0f;
- } else {
- return (float) (1 - (Math.log(100 - volume) / Math.log(100)));
- }
+ return Converter.getVolumeFromPercentage(volume);
}
public static float getRightVolume() {
int volume = prefs.getInt(PREF_RIGHT_VOLUME, 100);
- if(volume == 100) {
- return 1.0f;
- } else {
- return (float) (1 - (Math.log(100 - volume) / Math.log(100)));
- }
+ return Converter.getVolumeFromPercentage(volume);
+ }
+
+ public static int getLeftVolumePercentage() {
+ return prefs.getInt(PREF_LEFT_VOLUME, 100);
+ }
+
+ public static int getRightVolumePercentage() {
+ return prefs.getInt(PREF_RIGHT_VOLUME, 100);
}
public static boolean shouldPauseForFocusLoss() {
@@ -284,7 +297,7 @@ public class UserPreferences {
public static long getUpdateInterval() {
String updateInterval = prefs.getString(PREF_UPDATE_INTERVAL, "0");
- if(false == updateInterval.contains(":")) {
+ if(!updateInterval.contains(":")) {
return readUpdateInterval(updateInterval);
} else {
return 0;
@@ -295,8 +308,8 @@ public class UserPreferences {
String datetime = prefs.getString(PREF_UPDATE_INTERVAL, "");
if(datetime.length() >= 3 && datetime.contains(":")) {
String[] parts = datetime.split(":");
- int hourOfDay = Integer.valueOf(parts[0]);
- int minute = Integer.valueOf(parts[1]);
+ int hourOfDay = Integer.parseInt(parts[0]);
+ int minute = Integer.parseInt(parts[1]);
return new int[] { hourOfDay, minute };
} else {
return new int[0];
@@ -308,7 +321,7 @@ public class UserPreferences {
}
public static int getParallelDownloads() {
- return Integer.valueOf(prefs.getString(PREF_PARALLEL_DOWNLOADS, "4"));
+ return Integer.parseInt(prefs.getString(PREF_PARALLEL_DOWNLOADS, "4"));
}
public static int getEpisodeCacheSizeUnlimited() {
@@ -338,12 +351,12 @@ public class UserPreferences {
public static int getImageCacheSize() {
String cacheSizeString = prefs.getString(PREF_IMAGE_CACHE_SIZE, IMAGE_CACHE_DEFAULT_VALUE);
- int cacheSizeInt = Integer.valueOf(cacheSizeString);
+ int cacheSizeInt = Integer.parseInt(cacheSizeString);
// if the cache size is too small the user won't get any images at all
// that's bad, force it back to the default.
if (cacheSizeInt < IMAGE_CACHE_SIZE_MINIMUM) {
prefs.edit().putString(PREF_IMAGE_CACHE_SIZE, IMAGE_CACHE_DEFAULT_VALUE).apply();
- cacheSizeInt = Integer.valueOf(IMAGE_CACHE_DEFAULT_VALUE);
+ cacheSizeInt = Integer.parseInt(IMAGE_CACHE_DEFAULT_VALUE);
}
int cacheSizeMB = cacheSizeInt * 1024 * 1024;
return cacheSizeMB;
@@ -371,6 +384,41 @@ public class UserPreferences {
return TextUtils.split(selectedNetWorks, ",");
}
+ public static void setProxyConfig(ProxyConfig config) {
+ SharedPreferences.Editor editor = prefs.edit();
+ editor.putString(PREF_PROXY_TYPE, config.type.name());
+ if(TextUtils.isEmpty(config.host)) {
+ editor.remove(PREF_PROXY_HOST);
+ } else {
+ editor.putString(PREF_PROXY_HOST, config.host);
+ }
+ if(config.port <= 0 || config.port > 65535) {
+ editor.remove(PREF_PROXY_PORT);
+ } else {
+ editor.putInt(PREF_PROXY_PORT, config.port);
+ }
+ if(TextUtils.isEmpty(config.username)) {
+ editor.remove(PREF_PROXY_USER);
+ } else {
+ editor.putString(PREF_PROXY_USER, config.username);
+ }
+ if(TextUtils.isEmpty(config.password)) {
+ editor.remove(PREF_PROXY_PASSWORD);
+ } else {
+ editor.putString(PREF_PROXY_PASSWORD, config.password);
+ }
+ editor.apply();
+ }
+
+ public static ProxyConfig getProxyConfig() {
+ Proxy.Type type = Proxy.Type.valueOf(prefs.getString(PREF_PROXY_TYPE, Proxy.Type.DIRECT.name()));
+ String host = prefs.getString(PREF_PROXY_HOST, null);
+ int port = prefs.getInt(PREF_PROXY_PORT, 0);
+ String username = prefs.getString(PREF_PROXY_USER, null);
+ String password = prefs.getString(PREF_PROXY_PASSWORD, null);
+ return new ProxyConfig(type, host, port, username, password);
+ }
+
public static boolean shouldResumeAfterCall() {
return prefs.getBoolean(PREF_RESUME_AFTER_CALL, true);
}
@@ -494,7 +542,7 @@ public class UserPreferences {
if (valueFromPrefs.equals(context.getString(R.string.pref_episode_cache_unlimited))) {
return EPISODE_CACHE_SIZE_UNLIMITED;
} else {
- return Integer.valueOf(valueFromPrefs);
+ return Integer.parseInt(valueFromPrefs);
}
}
@@ -502,15 +550,7 @@ public class UserPreferences {
String[] selectedSpeeds = null;
// If this preference hasn't been set yet, return the default options
if (valueFromPrefs == null) {
- String[] allSpeeds = context.getResources().getStringArray(R.array.playback_speed_values);
- List<String> speedList = new ArrayList<>();
- for (String speedStr : allSpeeds) {
- float speed = Float.parseFloat(speedStr);
- if (speed < 2.0001 && speed * 10 % 1 == 0) {
- speedList.add(speedStr);
- }
- }
- selectedSpeeds = speedList.toArray(new String[speedList.size()]);
+ selectedSpeeds = new String[] { "1.00", "1.25", "1.50", "1.75", "2.00" };
} else {
try {
JSONArray jsonArray = new JSONArray(valueFromPrefs);
@@ -548,7 +588,7 @@ public class UserPreferences {
public static EpisodeCleanupAlgorithm getEpisodeCleanupAlgorithm() {
- int cleanupValue = Integer.valueOf(prefs.getString(PREF_EPISODE_CLEANUP, "-1"));
+ int cleanupValue = Integer.parseInt(prefs.getString(PREF_EPISODE_CLEANUP, "-1"));
if (cleanupValue == EPISODE_CLEANUP_QUEUE) {
return new APQueueCleanupAlgorithm();
} else if (cleanupValue == EPISODE_CLEANUP_NULL) {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/receiver/AlarmUpdateReceiver.java b/core/src/main/java/de/danoeh/antennapod/core/receiver/AlarmUpdateReceiver.java
index ce5004a98..33c15883b 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/receiver/AlarmUpdateReceiver.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/receiver/AlarmUpdateReceiver.java
@@ -6,7 +6,7 @@ import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
-import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
+import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.preferences.UserPreferences;
/** Listens for events that make it necessary to reset the update alarm. */
@@ -22,8 +22,7 @@ public class AlarmUpdateReceiver extends BroadcastReceiver {
} else if (TextUtils.equals(intent.getAction(), Intent.ACTION_PACKAGE_REPLACED)) {
Log.d(TAG, "Resetting update alarm after app upgrade");
}
- PlaybackPreferences.init(context);
- UserPreferences.init(context);
+ ClientConfig.initialize(context);
UserPreferences.restartUpdateAlarm(false);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/receiver/FeedUpdateReceiver.java b/core/src/main/java/de/danoeh/antennapod/core/receiver/FeedUpdateReceiver.java
index b959c7301..e0abe0efb 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/receiver/FeedUpdateReceiver.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/receiver/FeedUpdateReceiver.java
@@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;
+import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.util.NetworkUtils;
@@ -19,6 +20,7 @@ public class FeedUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received intent");
+ ClientConfig.initialize(context);
if (NetworkUtils.isDownloadAllowed()) {
DBTasks.refreshAllFeeds(context, null);
} else {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/receiver/MediaButtonReceiver.java b/core/src/main/java/de/danoeh/antennapod/core/receiver/MediaButtonReceiver.java
index a900248d2..51cc52e10 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/receiver/MediaButtonReceiver.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/receiver/MediaButtonReceiver.java
@@ -5,7 +5,8 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
-import de.danoeh.antennapod.core.BuildConfig;
+
+import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
/** Receives media button events. */
@@ -17,10 +18,10 @@ public class MediaButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
- if (BuildConfig.DEBUG) Log.d(TAG, "Received intent");
- KeyEvent event = (KeyEvent) intent.getExtras().get(
- Intent.EXTRA_KEY_EVENT);
+ Log.d(TAG, "Received intent");
+ KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
+ ClientConfig.initialize(context);
Intent serviceIntent = new Intent(context, PlaybackService.class);
int keycode = event.getKeyCode();
serviceIntent.putExtra(EXTRA_KEYCODE, keycode);
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/GpodnetSyncService.java b/core/src/main/java/de/danoeh/antennapod/core/service/GpodnetSyncService.java
index 0b90cef6c..d939c1007 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/GpodnetSyncService.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/GpodnetSyncService.java
@@ -13,7 +13,6 @@ import android.util.Log;
import android.util.Pair;
import java.util.Collection;
-import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -175,7 +174,7 @@ public class GpodnetSyncService extends Service {
for (String downloadUrl : changes.getAdded()) {
if (false == localSubscriptions.contains(downloadUrl) &&
false == localRemoved.contains(downloadUrl)) {
- Feed feed = new Feed(downloadUrl, new Date(0));
+ Feed feed = new Feed(downloadUrl, null);
DownloadRequester.getInstance().downloadFeed(this, feed);
}
}
@@ -315,6 +314,7 @@ public class GpodnetSyncService extends Service {
.setContentIntent(activityIntent)
.setSmallIcon(R.drawable.stat_notify_sync_error)
.setAutoCancel(true)
+ .setVisibility(Notification.VISIBILITY_PUBLIC)
.build();
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(id, notification);
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java
index b23819ef7..5dd1e2dfa 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java
@@ -2,8 +2,10 @@ package de.danoeh.antennapod.core.service.download;
import android.os.Build;
import android.support.annotation.NonNull;
+import android.text.TextUtils;
import android.util.Log;
+import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
@@ -14,7 +16,10 @@ import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
import java.net.Socket;
+import java.net.SocketAddress;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.concurrent.TimeUnit;
@@ -23,6 +28,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
+import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBWriter;
/**
@@ -44,12 +50,15 @@ public class AntennapodHttpClient {
*/
public static synchronized OkHttpClient getHttpClient() {
if (httpClient == null) {
-
httpClient = newHttpClient();
}
return httpClient;
}
+ public static synchronized void reinit() {
+ httpClient = newHttpClient();
+ }
+
/**
* Creates a new HTTP client. Most users should just use
* getHttpClient() to get the standard AntennaPod client,
@@ -69,13 +78,13 @@ public class AntennapodHttpClient {
client.networkInterceptors().add(chain -> {
Request request = chain.request();
Response response = chain.proceed(request);
- if(response.code() == HttpURLConnection.HTTP_MOVED_PERM ||
+ if (response.code() == HttpURLConnection.HTTP_MOVED_PERM ||
response.code() == StatusLine.HTTP_PERM_REDIRECT) {
String location = response.header("Location");
- if(location.startsWith("/")) { // URL is not absolute, but relative
+ if (location.startsWith("/")) { // URL is not absolute, but relative
URL url = request.url();
location = url.getProtocol() + "://" + url.getHost() + location;
- } else if(!location.toLowerCase().startsWith("http://") &&
+ } else if (!location.toLowerCase().startsWith("http://") &&
!location.toLowerCase().startsWith("https://")) {
// Reference is relative to current path
URL url = request.url();
@@ -106,6 +115,21 @@ public class AntennapodHttpClient {
client.setFollowRedirects(true);
client.setFollowSslRedirects(true);
+ ProxyConfig config = UserPreferences.getProxyConfig();
+ if (config.type != Proxy.Type.DIRECT) {
+ int port = config.port > 0 ? config.port : ProxyConfig.DEFAULT_PORT;
+ SocketAddress address = InetSocketAddress.createUnresolved(config.host, port);
+ Proxy proxy = new Proxy(config.type, address);
+ client.setProxy(proxy);
+ if (!TextUtils.isEmpty(config.username)) {
+ String credentials = Credentials.basic(config.username, config.password);
+ client.interceptors().add(chain -> {
+ Request request = chain.request().newBuilder()
+ .header("Proxy-Authorization", credentials).build();
+ return chain.proceed(request);
+ });
+ }
+ }
if(16 <= Build.VERSION.SDK_INT && Build.VERSION.SDK_INT < 21) {
client.setSslSocketFactory(new CustomSslSocketFactory());
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadRequest.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadRequest.java
index bc3006eea..de91916a9 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadRequest.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadRequest.java
@@ -4,6 +4,7 @@ import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
import de.danoeh.antennapod.core.feed.FeedFile;
import de.danoeh.antennapod.core.util.URLChecker;
@@ -15,7 +16,7 @@ public class DownloadRequest implements Parcelable {
private final String title;
private String username;
private String password;
- private long ifModifiedSince;
+ private String lastModified;
private boolean deleteOnFailure;
private final long feedfileId;
private final int feedfileType;
@@ -60,7 +61,7 @@ public class DownloadRequest implements Parcelable {
this.feedfileType = builder.feedfileType;
this.username = builder.username;
this.password = builder.password;
- this.ifModifiedSince = builder.ifModifiedSince;
+ this.lastModified = builder.lastModified;
this.deleteOnFailure = builder.deleteOnFailure;
this.arguments = (builder.arguments != null) ? builder.arguments : new Bundle();
}
@@ -71,7 +72,7 @@ public class DownloadRequest implements Parcelable {
title = in.readString();
feedfileId = in.readLong();
feedfileType = in.readInt();
- ifModifiedSince = in.readLong();
+ lastModified = in.readString();
deleteOnFailure = (in.readByte() > 0);
arguments = in.readBundle();
if (in.dataAvail() > 0) {
@@ -98,7 +99,7 @@ public class DownloadRequest implements Parcelable {
dest.writeString(title);
dest.writeLong(feedfileId);
dest.writeInt(feedfileType);
- dest.writeLong(ifModifiedSince);
+ dest.writeString(lastModified);
dest.writeByte((deleteOnFailure) ? (byte) 1 : 0);
dest.writeBundle(arguments);
if (username != null) {
@@ -127,7 +128,8 @@ public class DownloadRequest implements Parcelable {
DownloadRequest that = (DownloadRequest) o;
- if (ifModifiedSince != that.ifModifiedSince) return false;
+ if (lastModified != null ? !lastModified.equals(that.lastModified) : that.lastModified != null)
+ return false;
if (deleteOnFailure != that.deleteOnFailure) return false;
if (feedfileId != that.feedfileId) return false;
if (feedfileType != that.feedfileType) return false;
@@ -143,7 +145,6 @@ public class DownloadRequest implements Parcelable {
if (title != null ? !title.equals(that.title) : that.title != null) return false;
if (username != null ? !username.equals(that.username) : that.username != null)
return false;
-
return true;
}
@@ -154,7 +155,7 @@ public class DownloadRequest implements Parcelable {
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (username != null ? username.hashCode() : 0);
result = 31 * result + (password != null ? password.hashCode() : 0);
- result = 31 * result + (int)ifModifiedSince;
+ result = 31 * result + (lastModified != null ? lastModified.hashCode() : 0);
result = 31 * result + (deleteOnFailure ? 1 : 0);
result = 31 * result + (int) (feedfileId ^ (feedfileId >>> 32));
result = 31 * result + feedfileType;
@@ -234,13 +235,14 @@ public class DownloadRequest implements Parcelable {
this.password = password;
}
- public DownloadRequest setIfModifiedSince(long time) {
- this.ifModifiedSince = time;
+ public DownloadRequest setLastModified(@Nullable String lastModified) {
+ this.lastModified = lastModified;
return this;
}
- public long getIfModifiedSince() {
- return this.ifModifiedSince;
+ @Nullable
+ public String getLastModified() {
+ return lastModified;
}
public boolean isDeleteOnFailure() {
@@ -257,7 +259,7 @@ public class DownloadRequest implements Parcelable {
private String title;
private String username;
private String password;
- private long ifModifiedSince;
+ private String lastModified;
private boolean deleteOnFailure = false;
private long feedfileId;
private int feedfileType;
@@ -276,8 +278,8 @@ public class DownloadRequest implements Parcelable {
return this;
}
- public Builder ifModifiedSince(long time) {
- this.ifModifiedSince = time;
+ public Builder lastModified(String lastModified) {
+ this.lastModified = lastModified;
return this;
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java
index d69228ceb..3f0112b56 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java
@@ -41,11 +41,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
-import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.ThreadFactory;
-import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@@ -72,6 +69,7 @@ import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.syndication.handler.FeedHandler;
import de.danoeh.antennapod.core.syndication.handler.FeedHandlerResult;
import de.danoeh.antennapod.core.syndication.handler.UnsupportedFeedtypeException;
+import de.danoeh.antennapod.core.util.ChapterUtils;
import de.danoeh.antennapod.core.util.DownloadError;
import de.danoeh.antennapod.core.util.InvalidFeedException;
import de.greenrobot.event.EventBus;
@@ -187,7 +185,7 @@ public class DownloadService extends Service {
if (status.getReason() == DownloadError.ERROR_UNAUTHORIZED) {
postAuthenticationNotification(downloader.getDownloadRequest());
} else if (status.getReason() == DownloadError.ERROR_HTTP_DATA_ERROR
- && Integer.valueOf(status.getReasonDetailed()) == 416) {
+ && Integer.parseInt(status.getReasonDetailed()) == 416) {
Log.d(TAG, "Requested invalid range, restarting download from the beginning");
FileUtils.deleteQuietly(new File(downloader.getDownloadRequest().getDestination()));
@@ -251,54 +249,35 @@ public class DownloadService extends Service {
Log.d(TAG, "Service started");
isRunning = true;
handler = new Handler();
- reportQueue = Collections.synchronizedList(new ArrayList<DownloadStatus>());
- downloads = Collections.synchronizedList(new ArrayList<Downloader>());
+ reportQueue = Collections.synchronizedList(new ArrayList<>());
+ downloads = Collections.synchronizedList(new ArrayList<>());
numberOfDownloads = new AtomicInteger(0);
IntentFilter cancelDownloadReceiverFilter = new IntentFilter();
cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_ALL_DOWNLOADS);
cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_DOWNLOAD);
registerReceiver(cancelDownloadReceiver, cancelDownloadReceiverFilter);
- syncExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
-
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
- t.setPriority(Thread.MIN_PRIORITY);
- return t;
- }
+ syncExecutor = Executors.newSingleThreadExecutor(r -> {
+ Thread t = new Thread(r);
+ t.setPriority(Thread.MIN_PRIORITY);
+ return t;
});
Log.d(TAG, "parallel downloads: " + UserPreferences.getParallelDownloads());
- downloadExecutor = new ExecutorCompletionService<Downloader>(
+ downloadExecutor = new ExecutorCompletionService<>(
Executors.newFixedThreadPool(UserPreferences.getParallelDownloads(),
- new ThreadFactory() {
-
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
- t.setPriority(Thread.MIN_PRIORITY);
- return t;
- }
+ r -> {
+ Thread t = new Thread(r);
+ t.setPriority(Thread.MIN_PRIORITY);
+ return t;
}
)
);
schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOL_SIZE,
- new ThreadFactory() {
-
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
- t.setPriority(Thread.MIN_PRIORITY);
- return t;
- }
- }, new RejectedExecutionHandler() {
-
- @Override
- public void rejectedExecution(Runnable r,
- ThreadPoolExecutor executor) {
- Log.w(TAG, "SchedEx rejected submission of new task");
- }
- }
+ r -> {
+ Thread t = new Thread(r);
+ t.setPriority(Thread.MIN_PRIORITY);
+ return t;
+ }, (r, executor) -> Log.w(TAG, "SchedEx rejected submission of new task")
);
downloadCompletionThread.start();
feedSyncThread = new FeedSyncThread();
@@ -349,7 +328,9 @@ public class DownloadService extends Service {
.setOngoing(true)
.setContentIntent(ClientConfig.downloadServiceCallbacks.getNotificationContentIntent(this))
.setLargeIcon(icon)
- .setSmallIcon(R.drawable.stat_notify_sync);
+ .setSmallIcon(R.drawable.stat_notify_sync)
+ .setVisibility(Notification.VISIBILITY_PUBLIC);
+
Log.d(TAG, "Notification set up");
}
@@ -380,16 +361,16 @@ public class DownloadService extends Service {
if (i > 0) {
bigText.append("\n");
}
- bigText.append("\u2022 " + request.getTitle());
+ bigText.append("\u2022 ").append(request.getTitle());
}
} else if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
if (request.getTitle() != null) {
if (i > 0) {
bigText.append("\n");
}
- bigText.append("\u2022 " + request.getTitle()
- + " (" + request.getProgressPercent()
- + "%)");
+ bigText.append("\u2022 ").append(request.getTitle())
+ .append(" (").append(request.getProgressPercent())
+ .append("%)");
}
}
@@ -486,16 +467,13 @@ public class DownloadService extends Service {
* DownloadService list.
*/
private void removeDownload(final Downloader d) {
- handler.post(new Runnable() {
- @Override
- public void run() {
- Log.d(TAG, "Removing downloader: "
- + d.getDownloadRequest().getSource());
- boolean rc = downloads.remove(d);
- Log.d(TAG, "Result of downloads.remove: " + rc);
- DownloadRequester.getInstance().removeDownload(d.getDownloadRequest());
- postDownloaders();
- }
+ handler.post(() -> {
+ Log.d(TAG, "Removing downloader: "
+ + d.getDownloadRequest().getSource());
+ boolean rc = downloads.remove(d);
+ Log.d(TAG, "Result of downloads.remove: " + rc);
+ DownloadRequester.getInstance().removeDownload(d.getDownloadRequest());
+ postDownloaders();
});
}
@@ -555,7 +533,9 @@ public class DownloadService extends Service {
.setContentIntent(
ClientConfig.downloadServiceCallbacks.getReportNotificationContentIntent(this)
)
- .setAutoCancel(true).build();
+ .setAutoCancel(true)
+ .setVisibility(Notification.VISIBILITY_PUBLIC)
+ .build();
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(REPORT_ID, notification);
} else {
@@ -569,12 +549,7 @@ public class DownloadService extends Service {
* used from a thread other than the main thread.
*/
void queryDownloadsAsync() {
- handler.post(new Runnable() {
- public void run() {
- queryDownloads();
- ;
- }
- });
+ handler.post(DownloadService.this::queryDownloads);
}
/**
@@ -593,26 +568,24 @@ public class DownloadService extends Service {
}
private void postAuthenticationNotification(final DownloadRequest downloadRequest) {
- handler.post(new Runnable() {
- @Override
- public void run() {
- final String resourceTitle = (downloadRequest.getTitle() != null)
- ? downloadRequest.getTitle() : downloadRequest.getSource();
-
- NotificationCompat.Builder builder = new NotificationCompat.Builder(DownloadService.this);
- builder.setTicker(getText(R.string.authentication_notification_title))
- .setContentTitle(getText(R.string.authentication_notification_title))
- .setContentText(getText(R.string.authentication_notification_msg))
- .setStyle(new NotificationCompat.BigTextStyle().bigText(getText(R.string.authentication_notification_msg)
- + ": " + resourceTitle))
- .setSmallIcon(R.drawable.ic_stat_authentication)
- .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_authentication))
- .setAutoCancel(true)
- .setContentIntent(ClientConfig.downloadServiceCallbacks.getAuthentificationNotificationContentIntent(DownloadService.this, downloadRequest));
- Notification n = builder.build();
- NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- nm.notify(downloadRequest.getSource().hashCode(), n);
- }
+ handler.post(() -> {
+ final String resourceTitle = (downloadRequest.getTitle() != null)
+ ? downloadRequest.getTitle() : downloadRequest.getSource();
+
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(DownloadService.this);
+ builder.setTicker(getText(R.string.authentication_notification_title))
+ .setContentTitle(getText(R.string.authentication_notification_title))
+ .setContentText(getText(R.string.authentication_notification_msg))
+ .setStyle(new NotificationCompat.BigTextStyle().bigText(getText(R.string.authentication_notification_msg)
+ + ": " + resourceTitle))
+ .setSmallIcon(R.drawable.ic_stat_authentication)
+ .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_authentication))
+ .setAutoCancel(true)
+ .setContentIntent(ClientConfig.downloadServiceCallbacks.getAuthentificationNotificationContentIntent(DownloadService.this, downloadRequest))
+ .setVisibility(Notification.VISIBILITY_PUBLIC);
+ Notification n = builder.build();
+ NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ nm.notify(downloadRequest.getSource().hashCode(), n);
});
}
@@ -645,8 +618,8 @@ public class DownloadService extends Service {
class FeedSyncThread extends Thread {
private static final String TAG = "FeedSyncThread";
- private BlockingQueue<DownloadRequest> completedRequests = new LinkedBlockingDeque<DownloadRequest>();
- private CompletionService<Pair<DownloadRequest, FeedHandlerResult>> parserService = new ExecutorCompletionService<Pair<DownloadRequest, FeedHandlerResult>>(Executors.newSingleThreadExecutor());
+ private BlockingQueue<DownloadRequest> completedRequests = new LinkedBlockingDeque<>();
+ private CompletionService<Pair<DownloadRequest, FeedHandlerResult>> parserService = new ExecutorCompletionService<>(Executors.newSingleThreadExecutor());
private ExecutorService dbService = Executors.newSingleThreadExecutor();
private Future<?> dbUpdateFuture;
private volatile boolean isActive = true;
@@ -662,7 +635,7 @@ public class DownloadService extends Service {
* @return Collected feeds or null if the method has been interrupted during the first waiting period.
*/
private List<Pair<DownloadRequest, FeedHandlerResult>> collectCompletedRequests() {
- List<Pair<DownloadRequest, FeedHandlerResult>> results = new LinkedList<Pair<DownloadRequest, FeedHandlerResult>>();
+ List<Pair<DownloadRequest, FeedHandlerResult>> results = new LinkedList<>();
DownloadRequester requester = DownloadRequester.getInstance();
int tasks = 0;
@@ -706,11 +679,9 @@ public class DownloadService extends Service {
if (result != null) {
results.add(result);
}
- } catch (InterruptedException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
}
}
@@ -745,41 +716,36 @@ public class DownloadService extends Service {
if (dbUpdateFuture != null) {
try {
dbUpdateFuture.get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
- dbUpdateFuture = dbService.submit(new Runnable() {
- @Override
- public void run() {
- Feed[] savedFeeds = DBTasks.updateFeed(DownloadService.this, getFeeds(results));
-
- for (int i = 0; i < savedFeeds.length; i++) {
- Feed savedFeed = savedFeeds[i];
-
- // If loadAllPages=true, check if another page is available and queue it for download
- final boolean loadAllPages = results.get(i).first.getArguments().getBoolean(DownloadRequester.REQUEST_ARG_LOAD_ALL_PAGES);
- final Feed feed = results.get(i).second.feed;
- if (loadAllPages && feed.getNextPageLink() != null) {
- try {
- feed.setId(savedFeed.getId());
- DBTasks.loadNextPageOfFeed(DownloadService.this, savedFeed, true);
- } catch (DownloadRequestException e) {
- Log.e(TAG, "Error trying to load next page", e);
- }
+ dbUpdateFuture = dbService.submit(() -> {
+ Feed[] savedFeeds = DBTasks.updateFeed(DownloadService.this, getFeeds(results));
+
+ for (int i = 0; i < savedFeeds.length; i++) {
+ Feed savedFeed = savedFeeds[i];
+
+ // If loadAllPages=true, check if another page is available and queue it for download
+ final boolean loadAllPages = results.get(i).first.getArguments().getBoolean(DownloadRequester.REQUEST_ARG_LOAD_ALL_PAGES);
+ final Feed feed = results.get(i).second.feed;
+ if (loadAllPages && feed.getNextPageLink() != null) {
+ try {
+ feed.setId(savedFeed.getId());
+ DBTasks.loadNextPageOfFeed(DownloadService.this, savedFeed, true);
+ } catch (DownloadRequestException e) {
+ Log.e(TAG, "Error trying to load next page", e);
}
-
- ClientConfig.downloadServiceCallbacks.onFeedParsed(DownloadService.this,
- savedFeed);
-
- numberOfDownloads.decrementAndGet();
}
- queryDownloadsAsync();
+ ClientConfig.downloadServiceCallbacks.onFeedParsed(DownloadService.this,
+ savedFeed);
+
+ numberOfDownloads.decrementAndGet();
}
+
+ queryDownloadsAsync();
});
}
@@ -824,7 +790,7 @@ public class DownloadService extends Service {
}
private Pair<DownloadRequest, FeedHandlerResult> parseFeed(DownloadRequest request) {
- Feed feed = new Feed(request.getSource(), new Date());
+ Feed feed = new Feed(request.getSource(), request.getLastModified());
feed.setFile_url(request.getDestination());
feed.setId(request.getFeedfileId());
feed.setDownloaded(true);
@@ -841,21 +807,11 @@ public class DownloadService extends Service {
try {
result = feedHandler.parseFeed(feed);
Log.d(TAG, feed.getTitle() + " parsed");
- if (checkFeedData(feed) == false) {
+ if (!checkFeedData(feed)) {
throw new InvalidFeedException();
}
- } catch (SAXException e) {
- successful = false;
- e.printStackTrace();
- reason = DownloadError.ERROR_PARSER_EXCEPTION;
- reasonDetailed = e.getMessage();
- } catch (IOException e) {
- successful = false;
- e.printStackTrace();
- reason = DownloadError.ERROR_PARSER_EXCEPTION;
- reasonDetailed = e.getMessage();
- } catch (ParserConfigurationException e) {
+ } catch (SAXException | IOException | ParserConfigurationException e) {
successful = false;
e.printStackTrace();
reason = DownloadError.ERROR_PARSER_EXCEPTION;
@@ -878,7 +834,7 @@ public class DownloadService extends Service {
if (successful) {
// we create a 'successful' download log if the feed's last refresh failed
List<DownloadStatus> log = DBReader.getFeedDownloadLog(feed);
- if(log.size() > 0 && log.get(0).isSuccessful() == false) {
+ if(log.size() > 0 && !log.get(0).isSuccessful()) {
saveDownloadStatus(new DownloadStatus(feed,
feed.getHumanReadableIdentifier(), DownloadError.SUCCESS, successful,
reasonDetailed));
@@ -1011,9 +967,7 @@ public class DownloadService extends Service {
media.setFile_url(request.getDestination());
try {
DBWriter.setFeedMedia(media).get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
@@ -1046,6 +1000,9 @@ public class DownloadService extends Service {
media.setFile_url(request.getDestination());
media.setHasEmbeddedPicture(null);
+ // check if file has chapters
+ ChapterUtils.loadChaptersFromFileUrl(media);
+
// Get duration
MediaMetadataRetriever mmr = null;
try {
@@ -1078,10 +1035,7 @@ public class DownloadService extends Service {
if (item != null && !DBTasks.isInQueue(DownloadService.this, item.getId())) {
DBWriter.addQueueItem(DownloadService.this, item).get();
}
- } catch (ExecutionException e) {
- e.printStackTrace();
- status = new DownloadStatus(media, media.getEpisodeTitle(), DownloadError.ERROR_DB_ACCESS_ERROR, false, e.getMessage());
- } catch (InterruptedException e) {
+ } catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
status = new DownloadStatus(media, media.getEpisodeTitle(), DownloadError.ERROR_DB_ACCESS_ERROR, false, e.getMessage());
}
@@ -1125,14 +1079,11 @@ public class DownloadService extends Service {
private class NotificationUpdater implements Runnable {
public void run() {
- handler.post(new Runnable() {
- @Override
- public void run() {
- Notification n = updateNotifications();
- if (n != null) {
- NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- nm.notify(NOTIFICATION_ID, n);
- }
+ handler.post(() -> {
+ Notification n = updateNotifications();
+ if (n != null) {
+ NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ nm.notify(NOTIFICATION_ID, n);
}
});
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloaderCallback.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloaderCallback.java
index 2d9347b0a..b0829f084 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloaderCallback.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloaderCallback.java
@@ -6,5 +6,5 @@ package de.danoeh.antennapod.core.service.download;
*/
public interface DownloaderCallback {
- public void onDownloadCompleted(Downloader downloader);
+ void onDownloadCompleted(Downloader downloader);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java
index 0b9fba6f7..f79cf9685 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java
@@ -8,7 +8,6 @@ import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
-import com.squareup.okhttp.internal.http.HttpDate;
import org.apache.commons.io.IOUtils;
@@ -22,12 +21,14 @@ import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.UnknownHostException;
-import java.util.Arrays;
+import java.util.Collections;
import java.util.Date;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.feed.FeedImage;
+import de.danoeh.antennapod.core.feed.FeedMedia;
+import de.danoeh.antennapod.core.util.DateUtils;
import de.danoeh.antennapod.core.util.DownloadError;
import de.danoeh.antennapod.core.util.StorageUtils;
import de.danoeh.antennapod.core.util.URIUtil;
@@ -67,13 +68,24 @@ public class HttpDownloader extends Downloader {
final URI uri = URIUtil.getURIFromRequestUrl(request.getSource());
Request.Builder httpReq = new Request.Builder().url(uri.toURL())
.header("User-Agent", ClientConfig.USER_AGENT);
- if(request.getIfModifiedSince() > 0) {
- long threeDaysAgo = System.currentTimeMillis() - 1000*60*60*24*3;
- if(request.getIfModifiedSince() > threeDaysAgo) {
- Date date = new Date(request.getIfModifiedSince());
- String httpDate = HttpDate.format(date);
- Log.d(TAG, "addHeader(\"If-Modified-Since\", \"" + httpDate + "\")");
- httpReq.addHeader("If-Modified-Since", httpDate);
+ if(request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
+ // set header explicitly so that okhttp doesn't do transparent gzip
+ Log.d(TAG, "addHeader(\"Accept-Encoding\", \"identity\")");
+ httpReq.addHeader("Accept-Encoding", "identity");
+ }
+
+ if(!TextUtils.isEmpty(request.getLastModified())) {
+ String lastModified = request.getLastModified();
+ Date lastModifiedDate = DateUtils.parse(lastModified);
+ if(lastModifiedDate != null) {
+ long threeDaysAgo = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 3;
+ if (lastModifiedDate.getTime() > threeDaysAgo) {
+ Log.d(TAG, "addHeader(\"If-Modified-Since\", \"" + lastModified + "\")");
+ httpReq.addHeader("If-Modified-Since", lastModified);
+ }
+ } else {
+ Log.d(TAG, "addHeader(\"If-None-Match\", \"" + lastModified + "\")");
+ httpReq.addHeader("If-None-Match", lastModified);
}
}
@@ -98,19 +110,28 @@ public class HttpDownloader extends Downloader {
Log.d(TAG, "Adding range header: " + request.getSoFar());
}
- Response response = null;
+ Response response;
try {
response = httpClient.newCall(httpReq.build()).execute();
} catch(IOException e) {
Log.e(TAG, e.toString());
if(e.getMessage().contains("PROTOCOL_ERROR")) {
- httpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
+ httpClient.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
response = httpClient.newCall(httpReq.build()).execute();
}
else {
throw e;
}
}
+
+ if(request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
+ String contentType = response.header("Content-Type");
+ if(!contentType.startsWith("audio/") && !contentType.startsWith("video/")) {
+ onFail(DownloadError.ERROR_FILE_TYPE, null);
+ return;
+ }
+ }
+
responseBody = response.body();
String contentEncodingHeader = response.header("Content-Encoding");
boolean isGzip = false;
@@ -174,7 +195,7 @@ public class HttpDownloader extends Downloader {
&& !TextUtils.isEmpty(contentRangeHeader)) {
String start = contentRangeHeader.substring("bytes ".length(),
contentRangeHeader.indexOf("-"));
- request.setSoFar(Long.valueOf(start));
+ request.setSoFar(Long.parseLong(start));
Log.d(TAG, "Starting download at position " + request.getSoFar());
out = new RandomAccessFile(destination, "rw");
@@ -235,6 +256,12 @@ public class HttpDownloader extends Downloader {
onFail(DownloadError.ERROR_IO_ERROR, "Download completed, but nothing was read");
return;
}
+ String lastModified = response.header("Last-Modified");
+ if(lastModified != null) {
+ request.setLastModified(lastModified);
+ } else {
+ request.setLastModified(response.header("ETag"));
+ }
onSuccess();
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/ProxyConfig.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/ProxyConfig.java
new file mode 100644
index 000000000..6eb1f4118
--- /dev/null
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/ProxyConfig.java
@@ -0,0 +1,32 @@
+package de.danoeh.antennapod.core.service.download;
+
+import android.support.annotation.Nullable;
+
+import java.net.Proxy;
+
+public class ProxyConfig {
+
+ public final Proxy.Type type;
+ @Nullable public final String host;
+ public final int port;
+ @Nullable public final String username;
+ @Nullable public final String password;
+
+ public final static int DEFAULT_PORT = 8080;
+
+ public static ProxyConfig direct() {
+ return new ProxyConfig(Proxy.Type.DIRECT, null, 0, null, null);
+ }
+
+ public static ProxyConfig http(String host, int port, String username, String password) {
+ return new ProxyConfig(Proxy.Type.HTTP, host, port, username, password);
+ }
+
+ public ProxyConfig(Proxy.Type type, String host, int port, String username, String password) {
+ this.type = type;
+ this.host = host;
+ this.port = port;
+ this.username = username;
+ this.password = password;
+ }
+}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java
index 2be075a92..729ea9e7a 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java
@@ -142,6 +142,11 @@ public class PlaybackService extends Service {
public static final int NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE = 8;
/**
+ * Ability to set the playback speed has changed
+ */
+ public static final int NOTIFICATION_TYPE_SET_SPEED_ABILITY_CHANGED = 9;
+
+ /**
* Returned by getPositionSafe() or getDurationSafe() if the playbackService
* is in an invalid state.
*/
@@ -512,8 +517,11 @@ public class PlaybackService extends Service {
@Override
public void playbackSpeedChanged(float s) {
- sendNotificationBroadcast(
- NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE, 0);
+ sendNotificationBroadcast(NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE, 0);
+ }
+
+ public void setSpeedAbilityChanged() {
+ sendNotificationBroadcast(NOTIFICATION_TYPE_SET_SPEED_ABILITY_CHANGED, 0);
}
@Override
@@ -835,7 +843,7 @@ public class PlaybackService extends Service {
if (!Thread.currentThread().isInterrupted() && started && info.playable != null) {
String contentText = info.playable.getEpisodeTitle();
String contentTitle = info.playable.getFeedTitle();
- Notification notification = null;
+ Notification notification;
// Builder is v7, even if some not overwritten methods return its parent's v4 interface
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java
index a82e82506..aa51840a7 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java
@@ -25,6 +25,8 @@ import android.view.WindowManager;
import com.bumptech.glide.Glide;
+import org.antennapod.audio.MediaPlayer;
+
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
@@ -99,7 +101,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
this.playerLock = new ReentrantLock();
this.startWhenPrepared = new AtomicBoolean(false);
- executor = new ThreadPoolExecutor(1, 1, 5, TimeUnit.MINUTES, new LinkedBlockingDeque<Runnable>(),
+ executor = new ThreadPoolExecutor(1, 1, 5, TimeUnit.MINUTES, new LinkedBlockingDeque<>(),
new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
@@ -175,18 +177,15 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
*/
public void playMediaObject(@NonNull final Playable playable, final boolean stream, final boolean startWhenPrepared, final boolean prepareImmediately) {
Log.d(TAG, "playMediaObject(...)");
- executor.submit(new Runnable() {
- @Override
- public void run() {
- playerLock.lock();
- try {
- playMediaObject(playable, false, stream, startWhenPrepared, prepareImmediately);
- } catch (RuntimeException e) {
- e.printStackTrace();
- throw e;
- } finally {
- playerLock.unlock();
- }
+ executor.submit(() -> {
+ playerLock.lock();
+ try {
+ playMediaObject(playable, false, stream, startWhenPrepared, prepareImmediately);
+ } catch (RuntimeException e) {
+ e.printStackTrace();
+ throw e;
+ } finally {
+ playerLock.unlock();
}
});
}
@@ -258,24 +257,13 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
}
setPlayerStatus(PlayerStatus.INITIALIZED, media);
- if (mediaType == MediaType.VIDEO) {
- VideoPlayer vp = (VideoPlayer) mediaPlayer;
- // vp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
- }
-
if (prepareImmediately) {
setPlayerStatus(PlayerStatus.PREPARING, media);
mediaPlayer.prepare();
onPrepared(startWhenPrepared);
}
- } catch (Playable.PlayableException e) {
- e.printStackTrace();
- setPlayerStatus(PlayerStatus.ERROR, null);
- } catch (IOException e) {
- e.printStackTrace();
- setPlayerStatus(PlayerStatus.ERROR, null);
- } catch (IllegalStateException e) {
+ } catch (Playable.PlayableException | IOException | IllegalStateException e) {
e.printStackTrace();
setPlayerStatus(PlayerStatus.ERROR, null);
}
@@ -379,58 +367,52 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
* file is being streamed
*/
public void pause(final boolean abandonFocus, final boolean reinit) {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- playerLock.lock();
- releaseWifiLockIfNecessary();
- if (playerStatus == PlayerStatus.PLAYING) {
- Log.d(TAG, "Pausing playback.");
- mediaPlayer.pause();
- setPlayerStatus(PlayerStatus.PAUSED, media);
-
- if (abandonFocus) {
- audioManager.abandonAudioFocus(audioFocusChangeListener);
- pausedBecauseOfTransientAudiofocusLoss = false;
- }
- if (stream && reinit) {
- reinit();
- }
- } else {
- Log.d(TAG, "Ignoring call to pause: Player is in " + playerStatus + " state");
+ executor.submit(() -> {
+ playerLock.lock();
+ releaseWifiLockIfNecessary();
+ if (playerStatus == PlayerStatus.PLAYING) {
+ Log.d(TAG, "Pausing playback.");
+ mediaPlayer.pause();
+ setPlayerStatus(PlayerStatus.PAUSED, media);
+
+ if (abandonFocus) {
+ audioManager.abandonAudioFocus(audioFocusChangeListener);
+ pausedBecauseOfTransientAudiofocusLoss = false;
}
-
- playerLock.unlock();
+ if (stream && reinit) {
+ reinit();
+ }
+ } else {
+ Log.d(TAG, "Ignoring call to pause: Player is in " + playerStatus + " state");
}
+
+ playerLock.unlock();
});
}
/**
- * Prepared media player for playback if the service is in the INITALIZED
+ * Prepares media player for playback if the service is in the INITALIZED
* state.
* <p/>
* This method is executed on an internal executor service.
*/
public void prepare() {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- playerLock.lock();
-
- if (playerStatus == PlayerStatus.INITIALIZED) {
- Log.d(TAG, "Preparing media player");
- setPlayerStatus(PlayerStatus.PREPARING, media);
- try {
- mediaPlayer.prepare();
- onPrepared(startWhenPrepared.get());
- } catch (IOException e) {
- e.printStackTrace();
- setPlayerStatus(PlayerStatus.ERROR, null);
- }
- }
- playerLock.unlock();
+ executor.submit(() -> {
+ playerLock.lock();
+ if (playerStatus == PlayerStatus.INITIALIZED) {
+ Log.d(TAG, "Preparing media player");
+ setPlayerStatus(PlayerStatus.PREPARING, media);
+ try {
+ mediaPlayer.prepare();
+ onPrepared(startWhenPrepared.get());
+ } catch (IOException e) {
+ e.printStackTrace();
+ setPlayerStatus(PlayerStatus.ERROR, null);
+ }
}
+ playerLock.unlock();
+
});
}
@@ -449,7 +431,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
if (mediaType == MediaType.VIDEO) {
VideoPlayer vp = (VideoPlayer) mediaPlayer;
- videoSize = new Pair<Integer, Integer>(vp.getVideoWidth(), vp.getVideoHeight());
+ videoSize = new Pair<>(vp.getVideoWidth(), vp.getVideoHeight());
}
if (media.getPosition() > 0) {
@@ -475,20 +457,17 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
* This method is executed on an internal executor service.
*/
public void reinit() {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- playerLock.lock();
- releaseWifiLockIfNecessary();
- if (media != null) {
- playMediaObject(media, true, stream, startWhenPrepared.get(), false);
- } else if (mediaPlayer != null) {
- mediaPlayer.reset();
- } else {
- Log.d(TAG, "Call to reinit was ignored: media and mediaPlayer were null");
- }
- playerLock.unlock();
+ executor.submit(() -> {
+ playerLock.lock();
+ releaseWifiLockIfNecessary();
+ if (media != null) {
+ playMediaObject(media, true, stream, startWhenPrepared.get(), false);
+ } else if (mediaPlayer != null) {
+ mediaPlayer.reset();
+ } else {
+ Log.d(TAG, "Call to reinit was ignored: media and mediaPlayer were null");
}
+ playerLock.unlock();
});
}
@@ -542,12 +521,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
* This method is executed on an internal executor service.
*/
public void seekTo(final int t) {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- seekToSync(t);
- }
- });
+ executor.submit(() -> seekToSync(t));
}
/**
@@ -556,19 +530,16 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
* @param d offset from current position (positive or negative)
*/
public void seekDelta(final int d) {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- playerLock.lock();
- int currentPosition = getPosition();
- if (currentPosition != INVALID_TIME) {
- seekToSync(currentPosition + d);
- } else {
- Log.e(TAG, "getPosition() returned INVALID_TIME in seekDelta");
- }
-
- playerLock.unlock();
+ executor.submit(() -> {
+ playerLock.lock();
+ int currentPosition = getPosition();
+ if (currentPosition != INVALID_TIME) {
+ seekToSync(currentPosition + d);
+ } else {
+ Log.e(TAG, "getPosition() returned INVALID_TIME in seekDelta");
}
+
+ playerLock.unlock();
});
}
@@ -604,7 +575,11 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
* Returns the position of the current media object or INVALID_TIME if the position could not be retrieved.
*/
public int getPosition() {
- if (!playerLock.tryLock()) {
+ try {
+ if (!playerLock.tryLock(50, TimeUnit.MILLISECONDS)) {
+ return INVALID_TIME;
+ }
+ } catch (InterruptedException e) {
return INVALID_TIME;
}
@@ -614,10 +589,8 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
|| playerStatus == PlayerStatus.PREPARED
|| playerStatus == PlayerStatus.SEEKING) {
retVal = mediaPlayer.getCurrentPosition();
- if(retVal <= 0 && media != null && media.getPosition() > 0) {
- retVal = media.getPosition();
- }
- } else if (media != null && media.getPosition() > 0) {
+ }
+ if (retVal <= 0 && media != null && media.getPosition() >= 0) {
retVal = media.getPosition();
}
@@ -653,7 +626,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
playerLock.lock();
if (media != null && media.getMediaType() == MediaType.AUDIO) {
if (mediaPlayer.canSetSpeed()) {
- mediaPlayer.setPlaybackSpeed((float) speed);
+ mediaPlayer.setPlaybackSpeed(speed);
Log.d(TAG, "Playback speed was set to " + speed);
callback.playbackSpeedChanged(speed);
}
@@ -666,12 +639,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
* This method is executed on an internal executor service.
*/
public void setSpeed(final float speed) {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- setSpeedSync(speed);
- }
- });
+ executor.submit(() -> setSpeedSync(speed));
}
/**
@@ -693,7 +661,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
}
/**
- * Sets the playback speed.
+ * Sets the playback volume.
* This method is executed on an internal executor service.
*/
public void setVolume(final float volumeLeft, float volumeRight) {
@@ -701,7 +669,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
}
/**
- * Sets the playback speed.
+ * Sets the playback volume.
* This method is executed on the caller's thread.
*/
private void setVolumeSync(float volumeLeft, float volumeRight) {
@@ -757,28 +725,22 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
}
public void setVideoSurface(final SurfaceHolder surface) {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- playerLock.lock();
- if (mediaPlayer != null) {
- mediaPlayer.setDisplay(surface);
- }
- playerLock.unlock();
+ executor.submit(() -> {
+ playerLock.lock();
+ if (mediaPlayer != null) {
+ mediaPlayer.setDisplay(surface);
}
+ playerLock.unlock();
});
}
public void resetVideoSurface() {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- playerLock.lock();
- Log.d(TAG, "Resetting video surface");
- mediaPlayer.setDisplay(null);
- reinit();
- playerLock.unlock();
- }
+ executor.submit(() -> {
+ playerLock.lock();
+ Log.d(TAG, "Resetting video surface");
+ mediaPlayer.setDisplay(null);
+ reinit();
+ playerLock.unlock();
});
}
@@ -799,7 +761,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
res = null;
} else {
VideoPlayer vp = (VideoPlayer) mediaPlayer;
- videoSize = new Pair<Integer, Integer>(vp.getVideoWidth(), vp.getVideoHeight());
+ videoSize = new Pair<>(vp.getVideoWidth(), vp.getVideoHeight());
res = videoSize;
}
playerLock.unlock();
@@ -946,15 +908,16 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
if (pausedBecauseOfTransientAudiofocusLoss) { // we paused => play now
resume();
} else { // we ducked => raise audio level back
- audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
- AudioManager.ADJUST_RAISE, 0);
+ setVolumeSync(UserPreferences.getLeftVolume(),
+ UserPreferences.getRightVolume());
}
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
if (playerStatus == PlayerStatus.PLAYING) {
if (!UserPreferences.shouldPauseForFocusLoss()) {
Log.d(TAG, "Lost audio focus temporarily. Ducking...");
- audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
- AudioManager.ADJUST_LOWER, 0);
+ final float DUCK_FACTOR = 0.25f;
+ setVolumeSync(DUCK_FACTOR * UserPreferences.getLeftVolume(),
+ DUCK_FACTOR * UserPreferences.getRightVolume());
pausedBecauseOfTransientAudiofocusLoss = false;
} else {
Log.d(TAG, "Lost audio focus temporarily. Could duck, but won't, pausing...");
@@ -1004,20 +967,17 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
* abandoning audio focus have to be done with other methods.
*/
public void stop() {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- playerLock.lock();
- releaseWifiLockIfNecessary();
-
- if (playerStatus == PlayerStatus.INDETERMINATE) {
- setPlayerStatus(PlayerStatus.STOPPED, null);
- } else {
- Log.d(TAG, "Ignored call to stop: Current player state is: " + playerStatus);
- }
- playerLock.unlock();
+ executor.submit(() -> {
+ playerLock.lock();
+ releaseWifiLockIfNecessary();
+ if (playerStatus == PlayerStatus.INDETERMINATE) {
+ setPlayerStatus(PlayerStatus.STOPPED, null);
+ } else {
+ Log.d(TAG, "Ignored call to stop: Current player state is: " + playerStatus);
}
+ playerLock.unlock();
+
});
}
@@ -1058,6 +1018,8 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
void playbackSpeedChanged(float s);
+ void setSpeedAbilityChanged();
+
void onBufferingUpdate(int percent);
boolean onMediaPlayerInfo(int code);
@@ -1078,6 +1040,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
((AudioPlayer) mp)
.setOnBufferingUpdateListener(audioBufferingUpdateListener);
((AudioPlayer) mp).setOnInfoListener(audioInfoListener);
+ ((AudioPlayer) mp).setOnSpeedAdjustmentAvailableChangedListener(audioSetSpeedAbilityListener);
} else {
((VideoPlayer) mp)
.setOnCompletionListener(videoCompletionListener);
@@ -1092,100 +1055,68 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
return mp;
}
- private final org.antennapod.audio.MediaPlayer.OnCompletionListener audioCompletionListener = new org.antennapod.audio.MediaPlayer.OnCompletionListener() {
- @Override
- public void onCompletion(org.antennapod.audio.MediaPlayer mp) {
- genericOnCompletion();
- }
- };
+ private final MediaPlayer.OnCompletionListener audioCompletionListener =
+ mp -> genericOnCompletion();
- private final android.media.MediaPlayer.OnCompletionListener videoCompletionListener = new android.media.MediaPlayer.OnCompletionListener() {
- @Override
- public void onCompletion(android.media.MediaPlayer mp) {
- genericOnCompletion();
- }
- };
+ private final android.media.MediaPlayer.OnCompletionListener videoCompletionListener =
+ mp -> genericOnCompletion();
private void genericOnCompletion() {
endPlayback(false);
}
- private final org.antennapod.audio.MediaPlayer.OnBufferingUpdateListener audioBufferingUpdateListener = new org.antennapod.audio.MediaPlayer.OnBufferingUpdateListener() {
- @Override
- public void onBufferingUpdate(org.antennapod.audio.MediaPlayer mp,
- int percent) {
- genericOnBufferingUpdate(percent);
- }
- };
+ private final MediaPlayer.OnBufferingUpdateListener audioBufferingUpdateListener =
+ (mp, percent) -> genericOnBufferingUpdate(percent);
- private final android.media.MediaPlayer.OnBufferingUpdateListener videoBufferingUpdateListener = new android.media.MediaPlayer.OnBufferingUpdateListener() {
- @Override
- public void onBufferingUpdate(android.media.MediaPlayer mp, int percent) {
- genericOnBufferingUpdate(percent);
- }
- };
+ private final android.media.MediaPlayer.OnBufferingUpdateListener videoBufferingUpdateListener =
+ (mp, percent) -> genericOnBufferingUpdate(percent);
private void genericOnBufferingUpdate(int percent) {
callback.onBufferingUpdate(percent);
}
- private final org.antennapod.audio.MediaPlayer.OnInfoListener audioInfoListener = new org.antennapod.audio.MediaPlayer.OnInfoListener() {
- @Override
- public boolean onInfo(org.antennapod.audio.MediaPlayer mp, int what,
- int extra) {
- return genericInfoListener(what);
- }
- };
+ private final MediaPlayer.OnInfoListener audioInfoListener =
+ (mp, what, extra) -> genericInfoListener(what);
- private final android.media.MediaPlayer.OnInfoListener videoInfoListener = new android.media.MediaPlayer.OnInfoListener() {
- @Override
- public boolean onInfo(android.media.MediaPlayer mp, int what, int extra) {
- return genericInfoListener(what);
- }
- };
+ private final android.media.MediaPlayer.OnInfoListener videoInfoListener =
+ (mp, what, extra) -> genericInfoListener(what);
private boolean genericInfoListener(int what) {
return callback.onMediaPlayerInfo(what);
}
- private final org.antennapod.audio.MediaPlayer.OnErrorListener audioErrorListener = new org.antennapod.audio.MediaPlayer.OnErrorListener() {
+ private final MediaPlayer.OnSpeedAdjustmentAvailableChangedListener
+ audioSetSpeedAbilityListener = new MediaPlayer.OnSpeedAdjustmentAvailableChangedListener() {
@Override
- public boolean onError(org.antennapod.audio.MediaPlayer mp, int what, int extra) {
- if(mp.canFallback()) {
- mp.fallback();
- return true;
- } else {
- return genericOnError(mp, what, extra);
- }
+ public void onSpeedAdjustmentAvailableChanged(MediaPlayer arg0, boolean speedAdjustmentAvailable) {
+ callback.setSpeedAbilityChanged();
}
};
- private final android.media.MediaPlayer.OnErrorListener videoErrorListener = new android.media.MediaPlayer.OnErrorListener() {
- @Override
- public boolean onError(android.media.MediaPlayer mp, int what, int extra) {
- return genericOnError(mp, what, extra);
- }
- };
+
+ private final MediaPlayer.OnErrorListener audioErrorListener =
+ (mp, what, extra) -> {
+ if(mp.canFallback()) {
+ mp.fallback();
+ return true;
+ } else {
+ return genericOnError(mp, what, extra);
+ }
+ };
+
+ private final android.media.MediaPlayer.OnErrorListener videoErrorListener = this::genericOnError;
private boolean genericOnError(Object inObj, int what, int extra) {
return callback.onMediaPlayerError(inObj, what, extra);
}
- private final org.antennapod.audio.MediaPlayer.OnSeekCompleteListener audioSeekCompleteListener = new org.antennapod.audio.MediaPlayer.OnSeekCompleteListener() {
- @Override
- public void onSeekComplete(org.antennapod.audio.MediaPlayer mp) {
- genericSeekCompleteListener();
- }
- };
+ private final MediaPlayer.OnSeekCompleteListener audioSeekCompleteListener =
+ mp -> genericSeekCompleteListener();
- private final android.media.MediaPlayer.OnSeekCompleteListener videoSeekCompleteListener = new android.media.MediaPlayer.OnSeekCompleteListener() {
- @Override
- public void onSeekComplete(android.media.MediaPlayer mp) {
- genericSeekCompleteListener();
- }
- };
+ private final android.media.MediaPlayer.OnSeekCompleteListener videoSeekCompleteListener =
+ mp -> genericSeekCompleteListener();
- private final void genericSeekCompleteListener() {
+ private void genericSeekCompleteListener() {
Thread t = new Thread(() -> {
Log.d(TAG, "genericSeekCompleteListener");
if(seekLatch != null) {
@@ -1205,6 +1136,71 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
private static final String TAG = "MediaSessionCompat";
@Override
+ public void onPlay() {
+ Log.d(TAG, "onPlay()");
+ if (playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PREPARED) {
+ resume();
+ } else if (playerStatus == PlayerStatus.INITIALIZED) {
+ setStartWhenPrepared(true);
+ prepare();
+ }
+ }
+
+ @Override
+ public void onPause() {
+ Log.d(TAG, "onPause()");
+ if (playerStatus == PlayerStatus.PLAYING) {
+ pause(false, true);
+ }
+ if (UserPreferences.isPersistNotify()) {
+ pause(false, true);
+ } else {
+ pause(true, true);
+ }
+ }
+
+ @Override
+ public void onStop() {
+ Log.d(TAG, "onStop()");
+ stop();
+ }
+
+ @Override
+ public void onSkipToPrevious() {
+ Log.d(TAG, "onSkipToPrevious()");
+ seekDelta(-UserPreferences.getRewindSecs() * 1000);
+ }
+
+ @Override
+ public void onRewind() {
+ Log.d(TAG, "onRewind()");
+ seekDelta(-UserPreferences.getRewindSecs() * 1000);
+ }
+
+ @Override
+ public void onFastForward() {
+ Log.d(TAG, "onFastForward()");
+ seekDelta(UserPreferences.getFastFowardSecs() * 1000);
+ }
+
+ @Override
+ public void onSkipToNext() {
+ Log.d(TAG, "onSkipToNext()");
+ if(UserPreferences.shouldHardwareButtonSkip()) {
+ endPlayback(true);
+ } else {
+ seekDelta(UserPreferences.getFastFowardSecs() * 1000);
+ }
+ }
+
+
+ @Override
+ public void onSeekTo(long pos) {
+ Log.d(TAG, "onSeekTo()");
+ seekTo((int) pos);
+ }
+
+ @Override
public boolean onMediaButtonEvent(final Intent mediaButton) {
Log.d(TAG, "onMediaButtonEvent(" + mediaButton + ")");
if (mediaButton != null) {
@@ -1240,42 +1236,27 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre
return true;
}
case KeyEvent.KEYCODE_MEDIA_PLAY: {
- Log.d(TAG, "Received Play event from RemoteControlClient");
- if (playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PREPARED) {
- resume();
- } else if (playerStatus == PlayerStatus.INITIALIZED) {
- setStartWhenPrepared(true);
- prepare();
- }
+ sessionCallback.onPlay();
return true;
}
case KeyEvent.KEYCODE_MEDIA_PAUSE: {
- Log.d(TAG, "Received Pause event from RemoteControlClient");
- if (playerStatus == PlayerStatus.PLAYING) {
- pause(false, true);
- }
- if (UserPreferences.isPersistNotify()) {
- pause(false, true);
- } else {
- pause(true, true);
- }
+ sessionCallback.onPause();
return true;
}
case KeyEvent.KEYCODE_MEDIA_STOP: {
- Log.d(TAG, "Received Stop event from RemoteControlClient");
- stop();
+ sessionCallback.onStop();
return true;
}
case KeyEvent.KEYCODE_MEDIA_PREVIOUS: {
- seekDelta(-UserPreferences.getRewindSecs() * 1000);
+ sessionCallback.onSkipToPrevious();
return true;
}
case KeyEvent.KEYCODE_MEDIA_REWIND: {
- seekDelta(-UserPreferences.getRewindSecs() * 1000);
+ sessionCallback.onRewind();
return true;
}
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
- seekDelta(UserPreferences.getFastFowardSecs() * 1000);
+ sessionCallback.onFastForward();
return true;
}
case KeyEvent.KEYCODE_MEDIA_NEXT: {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceTaskManager.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceTaskManager.java
index 680fb8777..8a0964d36 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceTaskManager.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceTaskManager.java
@@ -6,12 +6,10 @@ import android.support.annotation.NonNull;
import android.util.Log;
import java.util.List;
-import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.event.QueueEvent;
@@ -65,13 +63,10 @@ public class PlaybackServiceTaskManager {
@NonNull PSTMCallback callback) {
this.context = context;
this.callback = callback;
- schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOL_SIZE, new ThreadFactory() {
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
- t.setPriority(Thread.MIN_PRIORITY);
- return t;
- }
+ schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOL_SIZE, r -> {
+ Thread t = new Thread(r);
+ t.setPriority(Thread.MIN_PRIORITY);
+ return t;
});
loadQueue();
EventBus.getDefault().register(this);
@@ -95,12 +90,7 @@ public class PlaybackServiceTaskManager {
private synchronized void loadQueue() {
if (!isQueueLoaderActive()) {
- queueFuture = schedExecutor.submit(new Callable<List<FeedItem>>() {
- @Override
- public List<FeedItem> call() throws Exception {
- return DBReader.getQueue();
- }
- });
+ queueFuture = schedExecutor.submit(DBReader::getQueue);
}
}
@@ -112,9 +102,7 @@ public class PlaybackServiceTaskManager {
if (queueFuture.isDone()) {
try {
return queueFuture.get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
@@ -137,12 +125,7 @@ public class PlaybackServiceTaskManager {
*/
public synchronized void startPositionSaver() {
if (!isPositionSaverActive()) {
- Runnable positionSaver = new Runnable() {
- @Override
- public void run() {
- callback.positionSaverTick();
- }
- };
+ Runnable positionSaver = callback::positionSaverTick;
positionSaverFuture = schedExecutor.scheduleWithFixedDelay(positionSaver, POSITION_SAVER_WAITING_INTERVAL,
POSITION_SAVER_WAITING_INTERVAL, TimeUnit.MILLISECONDS);
@@ -174,12 +157,7 @@ public class PlaybackServiceTaskManager {
*/
public synchronized void startWidgetUpdater() {
if (!isWidgetUpdaterActive()) {
- Runnable widgetUpdater = new Runnable() {
- @Override
- public void run() {
- callback.onWidgetUpdaterTick();
- }
- };
+ Runnable widgetUpdater = callback::onWidgetUpdaterTick;
widgetUpdaterFuture = schedExecutor.scheduleWithFixedDelay(widgetUpdater, WIDGET_UPDATER_NOTIFICATION_INTERVAL,
WIDGET_UPDATER_NOTIFICATION_INTERVAL, TimeUnit.MILLISECONDS);
@@ -279,18 +257,15 @@ public class PlaybackServiceTaskManager {
cancelChapterLoader();
}
- Runnable chapterLoader = new Runnable() {
- @Override
- public void run() {
- Log.d(TAG, "Chapter loader started");
- if (media.getChapters() == null) {
- media.loadChapterMarks();
- if (!Thread.currentThread().isInterrupted() && media.getChapters() != null) {
- callback.onChapterLoaded(media);
- }
+ Runnable chapterLoader = () -> {
+ Log.d(TAG, "Chapter loader started");
+ if (media.getChapters() == null) {
+ media.loadChapterMarks();
+ if (!Thread.currentThread().isInterrupted() && media.getChapters() != null) {
+ callback.onChapterLoaded(media);
}
- Log.d(TAG, "Chapter loader stopped");
}
+ Log.d(TAG, "Chapter loader stopped");
};
chapterLoaderFuture = schedExecutor.submit(chapterLoader);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java
index 0dc54fb6e..e94874453 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java
@@ -1,6 +1,7 @@
package de.danoeh.antennapod.core.storage;
import android.content.Context;
+import android.support.annotation.NonNull;
import android.util.Log;
import java.util.ArrayList;
@@ -12,8 +13,6 @@ import java.util.concurrent.ExecutionException;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
-import de.danoeh.antennapod.core.preferences.UserPreferences;
-import de.danoeh.antennapod.core.util.LongList;
/**
* Implementation of the EpisodeCleanupAlgorithm interface used by AntennaPod.
@@ -28,30 +27,18 @@ public class APCleanupAlgorithm extends EpisodeCleanupAlgorithm {
this.numberOfDaysAfterPlayback = numberOfDaysAfterPlayback;
}
+ /**
+ * @return the number of episodes that *could* be cleaned up, if needed
+ */
+ public int getReclaimableItems()
+ {
+ return getCandidates().size();
+ }
+
@Override
public int performCleanup(Context context, int numberOfEpisodesToDelete) {
- List<FeedItem> candidates = new ArrayList<>();
- List<FeedItem> downloadedItems = DBReader.getDownloadedItems();
+ List<FeedItem> candidates = getCandidates();
List<FeedItem> delete;
- Calendar cal = Calendar.getInstance();
- cal.add(Calendar.DAY_OF_MONTH, -1 * numberOfDaysAfterPlayback);
- Date mostRecentDateForDeletion = cal.getTime();
- for (FeedItem item : downloadedItems) {
- if (item.hasMedia()
- && item.getMedia().isDownloaded()
- && !item.isTagged(FeedItem.TAG_QUEUE)
- && item.isPlayed()
- && !item.isTagged(FeedItem.TAG_FAVORITE)) {
- FeedMedia media = item.getMedia();
- // make sure this candidate was played at least the proper amount of days prior
- // to now
- if (media != null
- && media.getPlaybackCompletionDate() != null
- && media.getPlaybackCompletionDate().before(mostRecentDateForDeletion)) {
- candidates.add(item);
- }
- }
- }
Collections.sort(candidates, (lhs, rhs) -> {
Date l = lhs.getMedia().getPlaybackCompletionDate();
@@ -90,6 +77,32 @@ public class APCleanupAlgorithm extends EpisodeCleanupAlgorithm {
return counter;
}
+ @NonNull
+ private List<FeedItem> getCandidates() {
+ List<FeedItem> candidates = new ArrayList<>();
+ List<FeedItem> downloadedItems = DBReader.getDownloadedItems();
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.DAY_OF_MONTH, -1 * numberOfDaysAfterPlayback);
+ Date mostRecentDateForDeletion = cal.getTime();
+ for (FeedItem item : downloadedItems) {
+ if (item.hasMedia()
+ && item.getMedia().isDownloaded()
+ && !item.isTagged(FeedItem.TAG_QUEUE)
+ && item.isPlayed()
+ && !item.isTagged(FeedItem.TAG_FAVORITE)) {
+ FeedMedia media = item.getMedia();
+ // make sure this candidate was played at least the proper amount of days prior
+ // to now
+ if (media != null
+ && media.getPlaybackCompletionDate() != null
+ && media.getPlaybackCompletionDate().before(mostRecentDateForDeletion)) {
+ candidates.add(item);
+ }
+ }
+ }
+ return candidates;
+ }
+
@Override
public int getDefaultCleanupParameter() {
return getNumEpisodesToCleanup(0);
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java
index 26dc027bf..aa97b321a 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java
@@ -33,73 +33,70 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm {
*/
@Override
public Runnable autoDownloadUndownloadedItems(final Context context) {
- return new Runnable() {
- @Override
- public void run() {
-
- // true if we should auto download based on network status
- boolean networkShouldAutoDl = NetworkUtils.autodownloadNetworkAvailable()
- && UserPreferences.isEnableAutodownload();
-
- // true if we should auto download based on power status
- boolean powerShouldAutoDl = PowerUtils.deviceCharging(context)
- || UserPreferences.isEnableAutodownloadOnBattery();
-
- // we should only auto download if both network AND power are happy
- if (networkShouldAutoDl && powerShouldAutoDl) {
-
- Log.d(TAG, "Performing auto-dl of undownloaded episodes");
-
- List<FeedItem> candidates;
- final List<FeedItem> queue = DBReader.getQueue();
- final List<FeedItem> newItems = DBReader.getNewItemsList();
- candidates = new ArrayList<FeedItem>(queue.size() + newItems.size());
- candidates.addAll(queue);
- for(FeedItem newItem : newItems) {
- FeedPreferences feedPrefs = newItem.getFeed().getPreferences();
- FeedFilter feedFilter = feedPrefs.getFilter();
- if(candidates.contains(newItem) == false && feedFilter.shouldAutoDownload(newItem)) {
- candidates.add(newItem);
- }
- }
-
- // filter items that are not auto downloadable
- Iterator<FeedItem> it = candidates.iterator();
- while(it.hasNext()) {
- FeedItem item = it.next();
- if(item.isAutoDownloadable() == false) {
- it.remove();
- }
+ return () -> {
+
+ // true if we should auto download based on network status
+ boolean networkShouldAutoDl = NetworkUtils.autodownloadNetworkAvailable()
+ && UserPreferences.isEnableAutodownload();
+
+ // true if we should auto download based on power status
+ boolean powerShouldAutoDl = PowerUtils.deviceCharging(context)
+ || UserPreferences.isEnableAutodownloadOnBattery();
+
+ // we should only auto download if both network AND power are happy
+ if (networkShouldAutoDl && powerShouldAutoDl) {
+
+ Log.d(TAG, "Performing auto-dl of undownloaded episodes");
+
+ List<FeedItem> candidates;
+ final List<FeedItem> queue = DBReader.getQueue();
+ final List<FeedItem> newItems = DBReader.getNewItemsList();
+ candidates = new ArrayList<>(queue.size() + newItems.size());
+ candidates.addAll(queue);
+ for(FeedItem newItem : newItems) {
+ FeedPreferences feedPrefs = newItem.getFeed().getPreferences();
+ FeedFilter feedFilter = feedPrefs.getFilter();
+ if(!candidates.contains(newItem) && feedFilter.shouldAutoDownload(newItem)) {
+ candidates.add(newItem);
}
+ }
- int autoDownloadableEpisodes = candidates.size();
- int downloadedEpisodes = DBReader.getNumberOfDownloadedEpisodes();
- int deletedEpisodes = UserPreferences.getEpisodeCleanupAlgorithm()
- .makeRoomForEpisodes(context, autoDownloadableEpisodes);
- boolean cacheIsUnlimited = UserPreferences.getEpisodeCacheSize() == UserPreferences
- .getEpisodeCacheSizeUnlimited();
- int episodeCacheSize = UserPreferences.getEpisodeCacheSize();
-
- int episodeSpaceLeft;
- if (cacheIsUnlimited ||
- episodeCacheSize >= downloadedEpisodes + autoDownloadableEpisodes) {
- episodeSpaceLeft = autoDownloadableEpisodes;
- } else {
- episodeSpaceLeft = episodeCacheSize - (downloadedEpisodes - deletedEpisodes);
+ // filter items that are not auto downloadable
+ Iterator<FeedItem> it = candidates.iterator();
+ while(it.hasNext()) {
+ FeedItem item = it.next();
+ if(!item.isAutoDownloadable()) {
+ it.remove();
}
+ }
- FeedItem[] itemsToDownload = candidates.subList(0, episodeSpaceLeft)
- .toArray(new FeedItem[episodeSpaceLeft]);
+ int autoDownloadableEpisodes = candidates.size();
+ int downloadedEpisodes = DBReader.getNumberOfDownloadedEpisodes();
+ int deletedEpisodes = UserPreferences.getEpisodeCleanupAlgorithm()
+ .makeRoomForEpisodes(context, autoDownloadableEpisodes);
+ boolean cacheIsUnlimited = UserPreferences.getEpisodeCacheSize() == UserPreferences
+ .getEpisodeCacheSizeUnlimited();
+ int episodeCacheSize = UserPreferences.getEpisodeCacheSize();
+
+ int episodeSpaceLeft;
+ if (cacheIsUnlimited ||
+ episodeCacheSize >= downloadedEpisodes + autoDownloadableEpisodes) {
+ episodeSpaceLeft = autoDownloadableEpisodes;
+ } else {
+ episodeSpaceLeft = episodeCacheSize - (downloadedEpisodes - deletedEpisodes);
+ }
- Log.d(TAG, "Enqueueing " + itemsToDownload.length + " items for download");
+ FeedItem[] itemsToDownload = candidates.subList(0, episodeSpaceLeft)
+ .toArray(new FeedItem[episodeSpaceLeft]);
- try {
- DBTasks.downloadFeedItems(false, context, itemsToDownload);
- } catch (DownloadRequestException e) {
- e.printStackTrace();
- }
+ Log.d(TAG, "Enqueueing " + itemsToDownload.length + " items for download");
+ try {
+ DBTasks.downloadFeedItems(false, context, itemsToDownload);
+ } catch (DownloadRequestException e) {
+ e.printStackTrace();
}
+
}
};
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java
index 132b61853..9cec62d83 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java
@@ -21,4 +21,9 @@ public class APNullCleanupAlgorithm extends EpisodeCleanupAlgorithm {
public int getDefaultCleanupParameter() {
return 0;
}
+
+ @Override
+ public int getReclaimableItems() {
+ return 0;
+ }
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java
index 234d6162c..04b200699 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java
@@ -1,18 +1,16 @@
package de.danoeh.antennapod.core.storage;
import android.content.Context;
+import android.support.annotation.NonNull;
import android.util.Log;
import java.util.ArrayList;
-import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import de.danoeh.antennapod.core.feed.FeedItem;
-import de.danoeh.antennapod.core.feed.FeedMedia;
-import de.danoeh.antennapod.core.util.LongList;
/**
* A cleanup algorithm that removes any item that isn't in the queue and isn't a favorite
@@ -22,19 +20,18 @@ public class APQueueCleanupAlgorithm extends EpisodeCleanupAlgorithm {
private static final String TAG = "APQueueCleanupAlgorithm";
+ /**
+ * @return the number of episodes that *could* be cleaned up, if needed
+ */
+ public int getReclaimableItems()
+ {
+ return getCandidates().size();
+ }
+
@Override
public int performCleanup(Context context, int numberOfEpisodesToDelete) {
- List<FeedItem> candidates = new ArrayList<>();
- List<FeedItem> downloadedItems = DBReader.getDownloadedItems();
+ List<FeedItem> candidates = getCandidates();
List<FeedItem> delete;
- for (FeedItem item : downloadedItems) {
- if (item.hasMedia()
- && item.getMedia().isDownloaded()
- && !item.isTagged(FeedItem.TAG_QUEUE)
- && !item.isTagged(FeedItem.TAG_FAVORITE)) {
- candidates.add(item);
- }
- }
// in the absence of better data, we'll sort by item publication date
Collections.sort(candidates, (lhs, rhs) -> {
@@ -74,6 +71,21 @@ public class APQueueCleanupAlgorithm extends EpisodeCleanupAlgorithm {
return counter;
}
+ @NonNull
+ private List<FeedItem> getCandidates() {
+ List<FeedItem> candidates = new ArrayList<>();
+ List<FeedItem> downloadedItems = DBReader.getDownloadedItems();
+ for (FeedItem item : downloadedItems) {
+ if (item.hasMedia()
+ && item.getMedia().isDownloaded()
+ && !item.isTagged(FeedItem.TAG_QUEUE)
+ && !item.isTagged(FeedItem.TAG_FAVORITE)) {
+ candidates.add(item);
+ }
+ }
+ return candidates;
+ }
+
@Override
public int getDefaultCleanupParameter() {
return getNumEpisodesToCleanup(0);
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java
index 72c68ddb6..dbb77e19c 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java
@@ -14,5 +14,5 @@ public interface AutomaticDownloadAlgorithm {
* @param context Used for accessing the DB.
* @return A Runnable that will be submitted to an ExecutorService.
*/
- public Runnable autoDownloadUndownloadedItems(Context context);
+ Runnable autoDownloadUndownloadedItems(Context context);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java
index 0563f878f..94629ba9d 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java
@@ -4,6 +4,7 @@ import android.database.Cursor;
import android.support.v4.util.ArrayMap;
import android.util.Log;
+import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -590,17 +591,19 @@ public final class DBReader {
FeedItem item = null;
Cursor itemCursor = adapter.getFeedItemCursor(Long.toString(itemId));
- if (itemCursor.moveToFirst()) {
- List<FeedItem> list = extractItemlistFromCursor(adapter, itemCursor);
- if (list.size() > 0) {
- item = list.get(0);
- loadAdditionalFeedItemListData(list);
- if (item.hasChapters()) {
- loadChaptersOfFeedItem(adapter, item);
- }
- }
+ if (!itemCursor.moveToFirst()) {
+ itemCursor.close();
+ return null;
}
+ List<FeedItem> list = extractItemlistFromCursor(adapter, itemCursor);
itemCursor.close();
+ if (list.size() > 0) {
+ item = list.get(0);
+ loadAdditionalFeedItemListData(list);
+ if (item.hasChapters()) {
+ loadChaptersOfFeedItem(adapter, item);
+ }
+ }
return item;
}
@@ -676,7 +679,7 @@ public final class DBReader {
* as well as chapter marks of the FeedItems will also be loaded from the database.
*/
public static List<FeedItem> getFeedItems(final long... itemIds) {
- Log.d(TAG, "getFeedItems() called with: " + "itemIds = [" + itemIds + "]");
+ Log.d(TAG, "getFeedItems() called with: " + "itemIds = [" + Arrays.toString(itemIds) + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
List<FeedItem> items = getFeedItems(adapter, itemIds);
@@ -898,25 +901,112 @@ public final class DBReader {
adapter.open();
Cursor mediaCursor = adapter.getSingleFeedMediaCursor(mediaId);
- FeedMedia media = null;
- if (mediaCursor.moveToFirst()) {
- int indexFeedItem = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM);
- final long itemId = mediaCursor.getLong(indexFeedItem);
- media = FeedMedia.fromCursor(mediaCursor);
+ if (!mediaCursor.moveToFirst()) {
+ mediaCursor.close();
+ return null;
+ }
+
+ int indexFeedItem = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM);
+ long itemId = mediaCursor.getLong(indexFeedItem);
+ FeedMedia media = FeedMedia.fromCursor(mediaCursor);
+ mediaCursor.close();
+
+ if(media != null) {
FeedItem item = getFeedItem(itemId);
- if (media != null && item != null) {
+ if (item != null) {
media.setItem(item);
item.setMedia(media);
}
}
- mediaCursor.close();
adapter.close();
return media;
}
/**
+ * Searches the DB for statistics
+ *
+ * @return The StatisticsInfo object
+ */
+ public static StatisticsData getStatistics() {
+ PodDBAdapter adapter = PodDBAdapter.getInstance();
+ adapter.open();
+
+ long totalTime = 0;
+ List<StatisticsItem> feedTime = new ArrayList<>();
+
+ List<Feed> feeds = getFeedList();
+ for (Feed feed : feeds) {
+ long feedPlayedTime = 0;
+ long feedTotalTime = 0;
+ long episodes = 0;
+ long episodesStarted = 0;
+ List<FeedItem> items = getFeed(feed.getId()).getItems();
+ for(FeedItem item : items) {
+ FeedMedia media = item.getMedia();
+ if(media == null) {
+ continue;
+ }
+
+ if(item.isPlayed()) {
+ feedPlayedTime += media.getDuration() / 1000;
+ } else {
+ feedPlayedTime += media.getPosition() / 1000;
+ }
+ if(item.isPlayed() || media.getPosition() != 0) {
+ episodesStarted++;
+ }
+ feedTotalTime += media.getDuration() / 1000;
+ episodes++;
+ }
+ feedTime.add(new StatisticsItem(
+ feed, feedTotalTime, feedPlayedTime, episodes, episodesStarted));
+ totalTime += feedPlayedTime;
+ }
+
+ Collections.sort(feedTime, (item1, item2) -> {
+ if(item1.timePlayed > item2.timePlayed) {
+ return -1;
+ } else if(item1.timePlayed < item2.timePlayed) {
+ return 1;
+ } else {
+ return 0;
+ }
+ });
+
+ adapter.close();
+ return new StatisticsData(totalTime, feedTime);
+ }
+
+ public static class StatisticsData {
+ public long totalTime;
+ public List<StatisticsItem> feedTime;
+
+ public StatisticsData(long totalTime, List<StatisticsItem> feedTime) {
+ this.totalTime = totalTime;
+ this.feedTime = feedTime;
+ }
+ }
+
+ public static class StatisticsItem {
+ public Feed feed;
+ public long time;
+ public long timePlayed;
+ public long episodes;
+ public long episodesStarted;
+
+ public StatisticsItem(Feed feed, long time, long timePlayed,
+ long episodes, long episodesStarted) {
+ this.feed = feed;
+ this.time = time;
+ this.timePlayed = timePlayed;
+ this.episodes = episodes;
+ this.episodesStarted = episodesStarted;
+ }
+ }
+
+ /**
* Returns the flattr queue as a List of FlattrThings. The list consists of Feeds and FeedItems.
*
* @return The flattr queue as a List.
@@ -1017,7 +1107,8 @@ public final class DBReader {
int numNewItems = adapter.getNumberOfNewItems();
int numDownloadedItems = adapter.getNumberOfDownloadedEpisodes();
- NavDrawerData result = new NavDrawerData(feeds, queueSize, numNewItems, numDownloadedItems, feedCounters);
+ NavDrawerData result = new NavDrawerData(feeds, queueSize, numNewItems, numDownloadedItems,
+ feedCounters, UserPreferences.getEpisodeCleanupAlgorithm().getReclaimableItems());
adapter.close();
return result;
}
@@ -1028,17 +1119,20 @@ public final class DBReader {
public int numNewItems;
public int numDownloadedItems;
public LongIntMap feedCounters;
+ public int reclaimableSpace;
public NavDrawerData(List<Feed> feeds,
int queueSize,
int numNewItems,
int numDownloadedItems,
- LongIntMap feedIndicatorValues) {
+ LongIntMap feedIndicatorValues,
+ int reclaimableSpace) {
this.feeds = feeds;
this.queueSize = queueSize;
this.numNewItems = numNewItems;
this.numDownloadedItems = numDownloadedItems;
this.feedCounters = feedIndicatorValues;
+ this.reclaimableSpace = reclaimableSpace;
}
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java
index efc60bfc2..47e2d8a26 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java
@@ -48,13 +48,10 @@ public final class DBTasks {
private static ExecutorService autodownloadExec;
static {
- autodownloadExec = Executors.newSingleThreadExecutor(new ThreadFactory() {
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
- t.setPriority(Thread.MIN_PRIORITY);
- return t;
- }
+ autodownloadExec = Executors.newSingleThreadExecutor(r -> {
+ Thread t = new Thread(r);
+ t.setPriority(Thread.MIN_PRIORITY);
+ return t;
});
}
@@ -85,9 +82,7 @@ public final class DBTasks {
if (feedID != 0) {
try {
DBWriter.deleteFeed(context, feedID).get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
} else {
@@ -114,7 +109,7 @@ public final class DBTasks {
boolean showPlayer, boolean startWhenPrepared, boolean shouldStream) {
try {
if (!shouldStream) {
- if (media.fileExists() == false) {
+ if (!media.fileExists()) {
throw new MediaFileNotFoundException(
"No episode was found at " + media.getFile_url(),
media);
@@ -224,7 +219,28 @@ public final class DBTasks {
*/
public static void refreshCompleteFeed(final Context context, final Feed feed) {
try {
- refreshFeed(context, feed, true);
+ refreshFeed(context, feed, true, false);
+ } catch (DownloadRequestException e) {
+ e.printStackTrace();
+ DBWriter.addDownloadStatus(
+ new DownloadStatus(feed, feed
+ .getHumanReadableIdentifier(),
+ DownloadError.ERROR_REQUEST_ERROR, false, e
+ .getMessage()
+ )
+ );
+ }
+ }
+
+ /**
+ * Downloads all pages of the given feed even if feed has not been modified since last refresh
+ *
+ * @param context Used for requesting the download.
+ * @param feed The Feed object.
+ */
+ public static void forceRefreshCompleteFeed(final Context context, final Feed feed) {
+ try {
+ refreshFeed(context, feed, true, true);
} catch (DownloadRequestException e) {
e.printStackTrace();
DBWriter.addDownloadStatus(
@@ -248,11 +264,11 @@ public final class DBTasks {
public static void loadNextPageOfFeed(final Context context, Feed feed, boolean loadAllPages) throws DownloadRequestException {
if (feed.isPaged() && feed.getNextPageLink() != null) {
int pageNr = feed.getPageNr() + 1;
- Feed nextFeed = new Feed(feed.getNextPageLink(), new Date(), feed.getTitle() + "(" + pageNr + ")");
+ Feed nextFeed = new Feed(feed.getNextPageLink(), null, feed.getTitle() + "(" + pageNr + ")");
nextFeed.setPageNr(pageNr);
nextFeed.setPaged(true);
nextFeed.setId(feed.getId());
- DownloadRequester.getInstance().downloadFeed(context, nextFeed, loadAllPages);
+ DownloadRequester.getInstance().downloadFeed(context, nextFeed, loadAllPages, false);
} else {
Log.e(TAG, "loadNextPageOfFeed: Feed was either not paged or contained no nextPageLink");
}
@@ -268,12 +284,25 @@ public final class DBTasks {
public static void refreshFeed(Context context, Feed feed)
throws DownloadRequestException {
Log.d(TAG, "refreshFeed(feed.id: " + feed.getId() +")");
- refreshFeed(context, feed, false);
+ refreshFeed(context, feed, false, false);
+ }
+
+ /**
+ * Refresh a specific feed even if feed has not been modified since last refresh
+ *
+ * @param context Used for requesting the download.
+ * @param feed The Feed object.
+ */
+ public static void forceRefreshFeed(Context context, Feed feed)
+ throws DownloadRequestException {
+ Log.d(TAG, "refreshFeed(feed.id: " + feed.getId() +")");
+ refreshFeed(context, feed, false, true);
}
- private static void refreshFeed(Context context, Feed feed, boolean loadAllPages) throws DownloadRequestException {
+ private static void refreshFeed(Context context, Feed feed, boolean loadAllPages, boolean force)
+ throws DownloadRequestException {
Feed f;
- Date lastUpdate = feed.hasLastUpdateFailed() ? new Date(0) : feed.getLastUpdate();
+ String lastUpdate = feed.hasLastUpdateFailed() ? null : feed.getLastUpdate();
if (feed.getPreferences() == null) {
f = new Feed(feed.getDownload_url(), lastUpdate, feed.getTitle());
} else {
@@ -281,7 +310,7 @@ public final class DBTasks {
feed.getPreferences().getUsername(), feed.getPreferences().getPassword());
}
f.setId(feed.getId());
- DownloadRequester.getInstance().downloadFeed(context, f, loadAllPages);
+ DownloadRequester.getInstance().downloadFeed(context, f, loadAllPages, force);
}
/**
@@ -484,8 +513,8 @@ public final class DBTasks {
*/
public static synchronized Feed[] updateFeed(final Context context,
final Feed... newFeeds) {
- List<Feed> newFeedsList = new ArrayList<Feed>();
- List<Feed> updatedFeedsList = new ArrayList<Feed>();
+ List<Feed> newFeedsList = new ArrayList<>();
+ List<Feed> updatedFeedsList = new ArrayList<>();
Feed[] resultFeeds = new Feed[newFeeds.length];
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
@@ -577,9 +606,7 @@ public final class DBTasks {
try {
DBWriter.addNewFeed(context, newFeedsList.toArray(new Feed[newFeedsList.size()])).get();
DBWriter.setCompleteFeed(updatedFeedsList.toArray(new Feed[updatedFeedsList.size()])).get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
@@ -599,7 +626,7 @@ public final class DBTasks {
*/
public static FutureTask<List<FeedItem>> searchFeedItemTitle(final Context context,
final long feedID, final String query) {
- return new FutureTask<List<FeedItem>>(new QueryTask<List<FeedItem>>(context) {
+ return new FutureTask<>(new QueryTask<List<FeedItem>>(context) {
@Override
public void execute(PodDBAdapter adapter) {
Cursor searchResult = adapter.searchItemTitles(feedID,
@@ -623,7 +650,7 @@ public final class DBTasks {
*/
public static FutureTask<List<FeedItem>> searchFeedItemDescription(final Context context,
final long feedID, final String query) {
- return new FutureTask<List<FeedItem>>(new QueryTask<List<FeedItem>>(context) {
+ return new FutureTask<>(new QueryTask<List<FeedItem>>(context) {
@Override
public void execute(PodDBAdapter adapter) {
Cursor searchResult = adapter.searchItemDescriptions(feedID,
@@ -647,7 +674,7 @@ public final class DBTasks {
*/
public static FutureTask<List<FeedItem>> searchFeedItemContentEncoded(final Context context,
final long feedID, final String query) {
- return new FutureTask<List<FeedItem>>(new QueryTask<List<FeedItem>>(context) {
+ return new FutureTask<>(new QueryTask<List<FeedItem>>(context) {
@Override
public void execute(PodDBAdapter adapter) {
Cursor searchResult = adapter.searchItemContentEncoded(feedID,
@@ -670,7 +697,7 @@ public final class DBTasks {
*/
public static FutureTask<List<FeedItem>> searchFeedItemChapters(final Context context,
final long feedID, final String query) {
- return new FutureTask<List<FeedItem>>(new QueryTask<List<FeedItem>>(context) {
+ return new FutureTask<>(new QueryTask<List<FeedItem>>(context) {
@Override
public void execute(PodDBAdapter adapter) {
Cursor searchResult = adapter.searchItemChapters(feedID,
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java
index e728abc3b..7dad9cb08 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java
@@ -14,7 +14,6 @@ import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
@@ -132,7 +131,7 @@ public class DBWriter {
}
}
Log.d(TAG, "Deleting File. Result: " + result);
- EventBus.getDefault().post(FeedItemEvent.deletedMedia(Arrays.asList(media.getItem())));
+ EventBus.getDefault().post(FeedItemEvent.deletedMedia(Collections.singletonList(media.getItem())));
EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast();
}
});
@@ -372,7 +371,7 @@ public class DBWriter {
if (queue != null) {
boolean queueModified = false;
LongList markAsUnplayedIds = new LongList();
- List<QueueEvent> events = new ArrayList<QueueEvent>();
+ List<QueueEvent> events = new ArrayList<>();
for (int i = 0; i < itemIds.length; i++) {
if (!itemListContains(queue, itemIds[i])) {
final FeedItem item = DBReader.getFeedItem(itemIds[i]);
@@ -545,9 +544,7 @@ public class DBWriter {
*/
public static Future<?> moveQueueItem(final int from,
final int to, final boolean broadcastUpdate) {
- return dbExec.submit(() -> {
- moveQueueItemHelper(from, to, broadcastUpdate);
- });
+ return dbExec.submit(() -> moveQueueItemHelper(from, to, broadcastUpdate));
}
/**
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java
index 0dc1dadeb..04afc504b 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java
@@ -51,7 +51,7 @@ public class DownloadRequester {
private Map<String, DownloadRequest> downloads;
private DownloadRequester() {
- downloads = new ConcurrentHashMap<String, DownloadRequest>();
+ downloads = new ConcurrentHashMap<>();
}
public static synchronized DownloadRequester getInstance() {
@@ -88,7 +88,7 @@ public class DownloadRequester {
private void download(Context context, FeedFile item, FeedFile container, File dest,
boolean overwriteIfExists, String username, String password,
- long ifModifiedSince, boolean deleteOnFailure, Bundle arguments) {
+ String lastModified, boolean deleteOnFailure, Bundle arguments) {
final boolean partiallyDownloadedFileExists = item.getFile_url() != null;
if (isDownloadingFile(item)) {
Log.e(TAG, "URL " + item.getDownload_url()
@@ -129,7 +129,7 @@ public class DownloadRequester {
DownloadRequest.Builder builder = new DownloadRequest.Builder(dest.toString(), item)
.withAuthentication(username, password)
- .ifModifiedSince(ifModifiedSince)
+ .lastModified(lastModified)
.deleteOnFailure(deleteOnFailure)
.withArguments(arguments);
DownloadRequest request = builder.build();
@@ -162,24 +162,25 @@ public class DownloadRequester {
* @param feed Feed to download
* @param loadAllPages Set to true to download all pages
*/
- public synchronized void downloadFeed(Context context, Feed feed, boolean loadAllPages)
+ public synchronized void downloadFeed(Context context, Feed feed, boolean loadAllPages,
+ boolean force)
throws DownloadRequestException {
if (feedFileValid(feed)) {
String username = (feed.getPreferences() != null) ? feed.getPreferences().getUsername() : null;
String password = (feed.getPreferences() != null) ? feed.getPreferences().getPassword() : null;
- long ifModifiedSince = feed.isPaged() ? 0 : feed.getLastUpdate().getTime();
+ String lastModified = feed.isPaged() || force ? null : feed.getLastUpdate();
Bundle args = new Bundle();
args.putInt(REQUEST_ARG_PAGE_NR, feed.getPageNr());
args.putBoolean(REQUEST_ARG_LOAD_ALL_PAGES, loadAllPages);
download(context, feed, null, new File(getFeedfilePath(context),
- getFeedfileName(feed)), true, username, password, ifModifiedSince, true, args);
+ getFeedfileName(feed)), true, username, password, lastModified, true, args);
}
}
public synchronized void downloadFeed(Context context, Feed feed) throws DownloadRequestException {
- downloadFeed(context, feed, false);
+ downloadFeed(context, feed, false, false);
}
public synchronized void downloadMedia(Context context, FeedMedia feedmedia)
@@ -204,7 +205,7 @@ public class DownloadRequester {
getMediafilename(feedmedia));
}
download(context, feedmedia, feed,
- dest, false, username, password, 0, false, null);
+ dest, false, username, password, null, false, null);
}
}
@@ -267,10 +268,7 @@ public class DownloadRequester {
* Checks if feedfile is in the downloads list
*/
public synchronized boolean isDownloadingFile(FeedFile item) {
- if (item.getDownload_url() != null) {
- return downloads.containsKey(item.getDownload_url());
- }
- return false;
+ return item.getDownload_url() != null && downloads.containsKey(item.getDownload_url());
}
public synchronized DownloadRequest getDownload(String downloadUrl) {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java
index 0f402745c..97cbdca33 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java
@@ -40,6 +40,11 @@ public abstract class EpisodeCleanupAlgorithm {
}
/**
+ * @return the number of episodes/items that *could* be cleaned up, if needed
+ */
+ public abstract int getReclaimableItems();
+
+ /**
* @param amountOfRoomNeeded the number of episodes we want to download
* @return the number of episodes to delete in order to make room
*/
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java b/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java
index 3a63685ba..d7c9e9108 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java
@@ -1,10 +1,6 @@
package de.danoeh.antennapod.core.storage;
import android.content.Context;
-import de.danoeh.antennapod.core.R;
-import de.danoeh.antennapod.core.feed.FeedItem;
-import de.danoeh.antennapod.core.feed.SearchResult;
-import de.danoeh.antennapod.core.util.comparator.SearchResultValueComparator;
import java.util.ArrayList;
import java.util.Collections;
@@ -12,6 +8,11 @@ import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
+import de.danoeh.antennapod.core.R;
+import de.danoeh.antennapod.core.feed.FeedItem;
+import de.danoeh.antennapod.core.feed.SearchResult;
+import de.danoeh.antennapod.core.util.comparator.SearchResultValueComparator;
+
/**
* Performs search on Feeds and FeedItems
*/
@@ -30,7 +31,7 @@ public class FeedSearcher {
context.getString(R.string.found_in_chapters_label),
context.getString(R.string.found_in_title_label)};
- List<SearchResult> result = new ArrayList<SearchResult>();
+ List<SearchResult> result = new ArrayList<>();
FutureTask<List<FeedItem>>[] tasks = new FutureTask[4];
(tasks[0] = DBTasks.searchFeedItemContentEncoded(context, selectedFeed, query)).run();
@@ -46,9 +47,7 @@ public class FeedSearcher {
}
}
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
Collections.sort(result, new SearchResultValueComparator());
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java
index 85ff8fc8c..c8df0f572 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java
@@ -10,12 +10,14 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.MediaMetadataRetriever;
+import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.event.ProgressEvent;
@@ -303,7 +305,7 @@ public class PodDBAdapter {
private static SQLiteDatabase db;
private static Context context;
private static PodDBHelper dbHelper;
- private static int counter = 0;
+ private static AtomicInteger counter = new AtomicInteger(0);
public static void init(Context context) {
PodDBAdapter.context = context.getApplicationContext();
@@ -318,11 +320,15 @@ public class PodDBAdapter {
private PodDBAdapter() {}
- public PodDBAdapter open() {
+ public synchronized PodDBAdapter open() {
+ int adapters = counter.incrementAndGet();
+ Log.v(TAG, "Opening DB #" + adapters);
if (db == null || !db.isOpen() || db.isReadOnly()) {
- Log.v(TAG, "Opening DB");
try {
db = dbHelper.getWritableDatabase();
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.enableWriteAheadLogging();
+ }
} catch (SQLException ex) {
Log.e(TAG, Log.getStackTraceString(ex));
db = dbHelper.getReadableDatabase();
@@ -331,8 +337,13 @@ public class PodDBAdapter {
return this;
}
- public void close() {
- // do nothing
+ public synchronized void close() {
+ int adapters = counter.decrementAndGet();
+ Log.v(TAG, "Closing DB #" + adapters);
+ if(adapters == 0) {
+ Log.v(TAG, "Closing DB, really");
+ db.close();
+ }
}
public static boolean deleteDatabase() {
@@ -368,7 +379,7 @@ public class PodDBAdapter {
values.put(KEY_FILE_URL, feed.getFile_url());
values.put(KEY_DOWNLOAD_URL, feed.getDownload_url());
values.put(KEY_DOWNLOADED, feed.isDownloaded());
- values.put(KEY_LASTUPDATE, feed.getLastUpdate().getTime());
+ values.put(KEY_LASTUPDATE, feed.getLastUpdate());
values.put(KEY_TYPE, feed.getType());
values.put(KEY_FEED_IDENTIFIER, feed.getFeedIdentifier());
@@ -425,34 +436,46 @@ public class PodDBAdapter {
*/
public long setImage(FeedImage image) {
boolean startedTransaction = false;
- if(false == db.inTransaction()) {
- db.beginTransaction();
- startedTransaction = true;
- }
- ContentValues values = new ContentValues();
- values.put(KEY_TITLE, image.getTitle());
- values.put(KEY_DOWNLOAD_URL, image.getDownload_url());
- values.put(KEY_DOWNLOADED, image.isDownloaded());
- values.put(KEY_FILE_URL, image.getFile_url());
- if (image.getId() == 0) {
- image.setId(db.insert(TABLE_NAME_FEED_IMAGES, null, values));
- } else {
- db.update(TABLE_NAME_FEED_IMAGES, values, KEY_ID + "=?",
+ try {
+ if (!db.inTransaction()) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.beginTransactionNonExclusive();
+ } else {
+ db.beginTransaction();
+ }
+ startedTransaction = true;
+ }
+
+ ContentValues values = new ContentValues();
+ values.put(KEY_TITLE, image.getTitle());
+ values.put(KEY_DOWNLOAD_URL, image.getDownload_url());
+ values.put(KEY_DOWNLOADED, image.isDownloaded());
+ values.put(KEY_FILE_URL, image.getFile_url());
+ if (image.getId() == 0) {
+ image.setId(db.insert(TABLE_NAME_FEED_IMAGES, null, values));
+ } else {
+ db.update(TABLE_NAME_FEED_IMAGES, values, KEY_ID + "=?",
new String[]{String.valueOf(image.getId())});
- }
+ }
- final FeedComponent owner = image.getOwner();
- if (owner != null && owner.getId() != 0) {
- values.clear();
- values.put(KEY_IMAGE, image.getId());
- if (owner instanceof Feed) {
- db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?", new String[]{String.valueOf(image.getOwner().getId())});
+ final FeedComponent owner = image.getOwner();
+ if (owner != null && owner.getId() != 0) {
+ values.clear();
+ values.put(KEY_IMAGE, image.getId());
+ if (owner instanceof Feed) {
+ db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?", new String[]{String.valueOf(image.getOwner().getId())});
+ }
+ }
+ if (startedTransaction) {
+ db.setTransactionSuccessful();
+ }
+ } catch (SQLException e) {
+ Log.e(TAG, Log.getStackTraceString(e));
+ } finally {
+ if (startedTransaction) {
+ db.endTransaction();
}
- }
- if(startedTransaction) {
- db.setTransactionSuccessful();
- db.endTransaction();
}
return image.getId();
}
@@ -522,20 +545,29 @@ public class PodDBAdapter {
* transaction
*/
public void setCompleteFeed(Feed... feeds) {
- db.beginTransaction();
- for (Feed feed : feeds) {
- setFeed(feed);
- if (feed.getItems() != null) {
- for (FeedItem item : feed.getItems()) {
- setFeedItem(item, false);
- }
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.beginTransactionNonExclusive();
+ } else {
+ db.beginTransaction();
}
- if (feed.getPreferences() != null) {
- setFeedPreferences(feed.getPreferences());
+ for (Feed feed : feeds) {
+ setFeed(feed);
+ if (feed.getItems() != null) {
+ for (FeedItem item : feed.getItems()) {
+ setFeedItem(item, false);
+ }
+ }
+ if (feed.getPreferences() != null) {
+ setFeedPreferences(feed.getPreferences());
+ }
}
+ db.setTransactionSuccessful();
+ } catch (SQLException e) {
+ Log.e(TAG, Log.getStackTraceString(e));
+ } finally {
+ db.endTransaction();
}
- db.setTransactionSuccessful();
- db.endTransaction();
}
/**
@@ -598,19 +630,38 @@ public class PodDBAdapter {
}
public void setFeedItemlist(List<FeedItem> items) {
- db.beginTransaction();
- for (FeedItem item : items) {
- setFeedItem(item, true);
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.beginTransactionNonExclusive();
+ } else {
+ db.beginTransaction();
+ }
+ for (FeedItem item : items) {
+ setFeedItem(item, true);
+ }
+ db.setTransactionSuccessful();
+ } catch (SQLException e) {
+ Log.e(TAG, Log.getStackTraceString(e));
+ } finally {
+ db.endTransaction();
}
- db.setTransactionSuccessful();
- db.endTransaction();
}
public long setSingleFeedItem(FeedItem item) {
- db.beginTransaction();
- long result = setFeedItem(item, true);
- db.setTransactionSuccessful();
- db.endTransaction();
+ long result = 0;
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.beginTransactionNonExclusive();
+ } else {
+ db.beginTransaction();
+ }
+ result = setFeedItem(item, true);
+ db.setTransactionSuccessful();
+ } catch (SQLException e) {
+ Log.e(TAG, Log.getStackTraceString(e));
+ } finally {
+ db.endTransaction();
+ }
return result;
}
@@ -728,20 +779,29 @@ public class PodDBAdapter {
public void setFeedItemRead(int played, long itemId, long mediaId,
boolean resetMediaPosition) {
- db.beginTransaction();
- ContentValues values = new ContentValues();
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.beginTransactionNonExclusive();
+ } else {
+ db.beginTransaction();
+ }
+ ContentValues values = new ContentValues();
- values.put(KEY_READ, played);
- db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", new String[]{String.valueOf(itemId)});
+ values.put(KEY_READ, played);
+ db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", new String[]{String.valueOf(itemId)});
- if (resetMediaPosition) {
- values.clear();
- values.put(KEY_POSITION, 0);
- db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID + "=?", new String[]{String.valueOf(mediaId)});
- }
+ if (resetMediaPosition) {
+ values.clear();
+ values.put(KEY_POSITION, 0);
+ db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID + "=?", new String[]{String.valueOf(mediaId)});
+ }
- db.setTransactionSuccessful();
- db.endTransaction();
+ db.setTransactionSuccessful();
+ } catch (SQLException e) {
+ Log.e(TAG, Log.getStackTraceString(e));
+ } finally {
+ db.endTransaction();
+ }
}
/**
@@ -750,15 +810,24 @@ public class PodDBAdapter {
* @param itemIds items to change the value of
*/
public void setFeedItemRead(int read, long... itemIds) {
- db.beginTransaction();
- ContentValues values = new ContentValues();
- for (long id : itemIds) {
- values.clear();
- values.put(KEY_READ, read);
- db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", new String[]{String.valueOf(id)});
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.beginTransactionNonExclusive();
+ } else {
+ db.beginTransaction();
+ }
+ ContentValues values = new ContentValues();
+ for (long id : itemIds) {
+ values.clear();
+ values.put(KEY_READ, read);
+ db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", new String[]{String.valueOf(id)});
+ }
+ db.setTransactionSuccessful();
+ } catch (SQLException e) {
+ Log.e(TAG, Log.getStackTraceString(e));
+ } finally {
+ db.endTransaction();
}
- db.setTransactionSuccessful();
- db.endTransaction();
}
public void setChapters(FeedItem item) {
@@ -822,17 +891,26 @@ public class PodDBAdapter {
public void setFavorites(List<FeedItem> favorites) {
ContentValues values = new ContentValues();
- db.beginTransaction();
- db.delete(TABLE_NAME_FAVORITES, null, null);
- for (int i = 0; i < favorites.size(); i++) {
- FeedItem item = favorites.get(i);
- values.put(KEY_ID, i);
- values.put(KEY_FEEDITEM, item.getId());
- values.put(KEY_FEED, item.getFeed().getId());
- db.insertWithOnConflict(TABLE_NAME_FAVORITES, null, values, SQLiteDatabase.CONFLICT_REPLACE);
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.beginTransactionNonExclusive();
+ } else {
+ db.beginTransaction();
+ }
+ db.delete(TABLE_NAME_FAVORITES, null, null);
+ for (int i = 0; i < favorites.size(); i++) {
+ FeedItem item = favorites.get(i);
+ values.put(KEY_ID, i);
+ values.put(KEY_FEEDITEM, item.getId());
+ values.put(KEY_FEED, item.getFeed().getId());
+ db.insertWithOnConflict(TABLE_NAME_FAVORITES, null, values, SQLiteDatabase.CONFLICT_REPLACE);
+ }
+ db.setTransactionSuccessful();
+ } catch (SQLException e) {
+ Log.e(TAG, Log.getStackTraceString(e));
+ } finally {
+ db.endTransaction();
}
- db.setTransactionSuccessful();
- db.endTransaction();
}
/**
@@ -880,17 +958,26 @@ public class PodDBAdapter {
public void setQueue(List<FeedItem> queue) {
ContentValues values = new ContentValues();
- db.beginTransaction();
- db.delete(TABLE_NAME_QUEUE, null, null);
- for (int i = 0; i < queue.size(); i++) {
- FeedItem item = queue.get(i);
- values.put(KEY_ID, i);
- values.put(KEY_FEEDITEM, item.getId());
- values.put(KEY_FEED, item.getFeed().getId());
- db.insertWithOnConflict(TABLE_NAME_QUEUE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.beginTransactionNonExclusive();
+ } else {
+ db.beginTransaction();
+ }
+ db.delete(TABLE_NAME_QUEUE, null, null);
+ for (int i = 0; i < queue.size(); i++) {
+ FeedItem item = queue.get(i);
+ values.put(KEY_ID, i);
+ values.put(KEY_FEEDITEM, item.getId());
+ values.put(KEY_FEED, item.getFeed().getId());
+ db.insertWithOnConflict(TABLE_NAME_QUEUE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
+ }
+ db.setTransactionSuccessful();
+ } catch (SQLException e) {
+ Log.e(TAG, Log.getStackTraceString(e));
+ } finally {
+ db.endTransaction();
}
- db.setTransactionSuccessful();
- db.endTransaction();
}
public void clearQueue() {
@@ -937,23 +1024,32 @@ public class PodDBAdapter {
* Remove a feed with all its FeedItems and Media entries.
*/
public void removeFeed(Feed feed) {
- db.beginTransaction();
- if (feed.getImage() != null) {
- removeFeedImage(feed.getImage());
- }
- if (feed.getItems() != null) {
- for (FeedItem item : feed.getItems()) {
- removeFeedItem(item);
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+ db.beginTransactionNonExclusive();
+ } else {
+ db.beginTransaction();
}
- }
- // delete download log entries for feed
- db.delete(TABLE_NAME_DOWNLOAD_LOG, KEY_FEEDFILE + "=? AND " + KEY_FEEDFILETYPE +"=?",
- new String[] { String.valueOf(feed.getId()), String.valueOf(Feed.FEEDFILETYPE_FEED) });
+ if (feed.getImage() != null) {
+ removeFeedImage(feed.getImage());
+ }
+ if (feed.getItems() != null) {
+ for (FeedItem item : feed.getItems()) {
+ removeFeedItem(item);
+ }
+ }
+ // delete download log entries for feed
+ db.delete(TABLE_NAME_DOWNLOAD_LOG, KEY_FEEDFILE + "=? AND " + KEY_FEEDFILETYPE + "=?",
+ new String[]{String.valueOf(feed.getId()), String.valueOf(Feed.FEEDFILETYPE_FEED)});
- db.delete(TABLE_NAME_FEEDS, KEY_ID + "=?",
+ db.delete(TABLE_NAME_FEEDS, KEY_ID + "=?",
new String[]{String.valueOf(feed.getId())});
- db.setTransactionSuccessful();
- db.endTransaction();
+ db.setTransactionSuccessful();
+ } catch (SQLException e) {
+ Log.e(TAG, Log.getStackTraceString(e));
+ } finally {
+ db.endTransaction();
+ }
}
public void clearPlaybackHistory() {
@@ -972,9 +1068,8 @@ public class PodDBAdapter {
* @return The cursor of the query
*/
public final Cursor getAllFeedsCursor() {
- Cursor c = db.query(TABLE_NAME_FEEDS, FEED_SEL_STD, null, null, null, null,
+ return db.query(TABLE_NAME_FEEDS, FEED_SEL_STD, null, null, null, null,
KEY_TITLE + " COLLATE NOCASE ASC");
- return c;
}
public final Cursor getFeedCursorDownloadUrls() {
@@ -992,22 +1087,19 @@ public class PodDBAdapter {
}
public final Cursor getAllItemsOfFeedCursor(final long feedId) {
- Cursor c = db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL, KEY_FEED
+ return db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL, KEY_FEED
+ "=?", new String[]{String.valueOf(feedId)}, null, null,
- null
- );
- return c;
+ null);
}
/**
* Return a cursor with the SEL_FI_EXTRA selection of a single feeditem.
*/
public final Cursor getExtraInformationOfItem(final FeedItem item) {
- Cursor c = db
+ return db
.query(TABLE_NAME_FEED_ITEMS, SEL_FI_EXTRA, KEY_ID + "=?",
new String[]{String.valueOf(item.getId())}, null,
null, null);
- return c;
}
/**
@@ -1017,10 +1109,9 @@ public class PodDBAdapter {
* @return The cursor of the query
*/
public final Cursor getFeedMediaOfItemCursor(final FeedItem item) {
- Cursor c = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?",
+ return db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?",
new String[]{String.valueOf(item.getMedia().getId())}, null,
null, null);
- return c;
}
/**
@@ -1065,25 +1156,22 @@ public class PodDBAdapter {
}
public final Cursor getSimpleChaptersOfFeedItemCursor(final FeedItem item) {
- Cursor c = db.query(TABLE_NAME_SIMPLECHAPTERS, null, KEY_FEEDITEM
+ return db.query(TABLE_NAME_SIMPLECHAPTERS, null, KEY_FEEDITEM
+ "=?", new String[]{String.valueOf(item.getId())}, null,
null, null
);
- return c;
}
public final Cursor getDownloadLog(final int feedFileType, final long feedFileId) {
final String query = "SELECT * FROM " + TABLE_NAME_DOWNLOAD_LOG +
" WHERE " + KEY_FEEDFILE + "=" + feedFileId + " AND " + KEY_FEEDFILETYPE + "=" + feedFileType
+ " ORDER BY " + KEY_ID + " DESC";
- Cursor c = db.rawQuery(query, null);
- return c;
+ return db.rawQuery(query, null);
}
public final Cursor getDownloadLogCursor(final int limit) {
- Cursor c = db.query(TABLE_NAME_DOWNLOAD_LOG, null, null, null, null,
+ return db.query(TABLE_NAME_DOWNLOAD_LOG, null, null, null, null,
null, KEY_COMPLETION_DATE + " DESC LIMIT " + limit);
- return c;
}
/**
@@ -1099,13 +1187,11 @@ public class PodDBAdapter {
TABLE_NAME_QUEUE + "." + KEY_FEEDITEM,
TABLE_NAME_QUEUE + "." + KEY_ID };
String query = String.format("SELECT %s FROM %s INNER JOIN %s ON %s=%s ORDER BY %s", args);
- Cursor c = db.rawQuery(query, null);
- return c;
+ return db.rawQuery(query, null);
}
public Cursor getQueueIDCursor() {
- Cursor c = db.query(TABLE_NAME_QUEUE, new String[]{KEY_FEEDITEM}, null, null, null, null, KEY_ID + " ASC", null);
- return c;
+ return db.query(TABLE_NAME_QUEUE, new String[]{KEY_FEEDITEM}, null, null, null, null, KEY_ID + " ASC", null);
}
@@ -1117,8 +1203,7 @@ public class PodDBAdapter {
TABLE_NAME_FAVORITES + "." + KEY_FEEDITEM,
TABLE_NAME_FEED_ITEMS + "." + KEY_PUBDATE };
String query = String.format("SELECT %s FROM %s INNER JOIN %s ON %s=%s ORDER BY %s DESC", args);
- Cursor c = db.rawQuery(query, null);
- return c;
+ return db.rawQuery(query, null);
}
/**
@@ -1126,9 +1211,8 @@ public class PodDBAdapter {
* The returned cursor uses the FEEDITEM_SEL_FI_SMALL selection.
*/
public final Cursor getUnreadItemsCursor() {
- Cursor c = db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL, KEY_READ
+ return db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL, KEY_READ
+ "<" + FeedItem.PLAYED, null, null, null, KEY_PUBDATE + " DESC");
- return c;
}
/**
@@ -1141,8 +1225,7 @@ public class PodDBAdapter {
+ " WHERE " + KEY_FEED + "=" + feedId
+ " AND " + KEY_READ + "=" + FeedItem.NEW
+ " ORDER BY " + KEY_PUBDATE + " DESC";
- Cursor c = db.rawQuery(query, null);
- return c;
+ return db.rawQuery(query, null);
}
/**
@@ -1151,7 +1234,7 @@ public class PodDBAdapter {
* The returned cursor uses the FEEDITEM_SEL_FI_SMALL selection.
*/
public final Cursor getNewItemsCursor() {
- String[] args = new String[] {
+ Object[] args = new String[] {
SEL_FI_SMALL_STR,
TABLE_NAME_FEED_ITEMS,
TABLE_NAME_FEEDS,
@@ -1160,13 +1243,11 @@ public class PodDBAdapter {
KEY_PUBDATE + " DESC"
};
final String query = String.format("SELECT %s FROM %s INNER JOIN %s ON %s WHERE %s ORDER BY %s", args);
- Cursor c = db.rawQuery(query, null);
- return c;
+ return db.rawQuery(query, null);
}
public final Cursor getRecentlyPublishedItemsCursor(int limit) {
- Cursor c = db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL, null, null, null, null, KEY_PUBDATE + " DESC LIMIT " + limit);
- return c;
+ return db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL, null, null, null, null, KEY_PUBDATE + " DESC LIMIT " + limit);
}
public Cursor getDownloadedItemsCursor() {
@@ -1175,8 +1256,7 @@ public class PodDBAdapter {
+ " INNER JOIN " + TABLE_NAME_FEED_MEDIA
+ " ON " + TABLE_NAME_FEED_ITEMS + "." + KEY_ID + "=" + TABLE_NAME_FEED_MEDIA + "." + KEY_FEEDITEM
+ " WHERE " + TABLE_NAME_FEED_MEDIA + "." + KEY_DOWNLOADED + ">0";
- Cursor c = db.rawQuery(query, null);
- return c;
+ return db.rawQuery(query, null);
}
/**
@@ -1192,10 +1272,9 @@ public class PodDBAdapter {
throw new IllegalArgumentException("Limit must be >= 0");
}
- Cursor c = db.query(TABLE_NAME_FEED_MEDIA, null,
+ return db.query(TABLE_NAME_FEED_MEDIA, null,
KEY_PLAYBACK_COMPLETION_DATE + " > 0", null, null,
null, String.format("%s DESC LIMIT %d", KEY_PLAYBACK_COMPLETION_DATE, limit));
- return c;
}
public final Cursor getSingleFeedMediaCursor(long id) {
@@ -1244,18 +1323,17 @@ public class PodDBAdapter {
if (size == 1) {
return "(?)";
}
- StringBuffer buffer = new StringBuffer("(");
+ StringBuilder builder = new StringBuilder("(");
for (int i = 0; i < size - 1; i++) {
- buffer.append("?,");
+ builder.append("?,");
}
- buffer.append("?)");
- return buffer.toString();
+ builder.append("?)");
+ return builder.toString();
}
public final Cursor getFeedCursor(final long id) {
- Cursor c = db.query(TABLE_NAME_FEEDS, FEED_SEL_STD, KEY_ID + "=" + id, null,
+ return db.query(TABLE_NAME_FEEDS, FEED_SEL_STD, KEY_ID + "=" + id, null,
null, null, null);
- return c;
}
public final Cursor getFeedItemCursor(final String id) {
@@ -1512,7 +1590,7 @@ public class PodDBAdapter {
*/
private static class PodDBHelper extends SQLiteOpenHelper {
- private final static int VERSION = 1050003;
+ private final static int VERSION = 1050004;
private Context context;
@@ -1756,7 +1834,6 @@ public class PodDBAdapter {
db.execSQL(PodDBAdapter.CREATE_INDEX_FEEDITEMS_PUBDATE);
db.execSQL(PodDBAdapter.CREATE_INDEX_FEEDITEMS_READ);
}
-
if (oldVersion < 1050003) {
// Migrates feed list filter data
@@ -1804,6 +1881,11 @@ public class PodDBAdapter {
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS
+ " ADD COLUMN " + PodDBAdapter.KEY_KEEP_UPDATED + " INTEGER DEFAULT 1");
}
+ if (oldVersion < 1050004) {
+ // prevent old timestamps to be misinterpreted as ETags
+ db.execSQL("UPDATE " + PodDBAdapter.TABLE_NAME_FEEDS
+ +" SET " + PodDBAdapter.KEY_LASTUPDATE + "=NULL");
+ }
EventBus.getDefault().post(ProgressEvent.end());
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/handler/HandlerState.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/handler/HandlerState.java
index 9280db8a3..66513a12e 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/handler/HandlerState.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/handler/HandlerState.java
@@ -47,10 +47,10 @@ public class HandlerState {
public HandlerState(Feed feed) {
this.feed = feed;
alternateUrls = new ArrayMap<>();
- items = new ArrayList<FeedItem>();
- tagstack = new Stack<SyndElement>();
+ items = new ArrayList<>();
+ tagstack = new Stack<>();
namespaces = new ArrayMap<>();
- defaultNamespaces = new Stack<Namespace>();
+ defaultNamespaces = new Stack<>();
tempObjects = new ArrayMap<>();
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/handler/TypeGetter.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/handler/TypeGetter.java
index 4d56e1365..f84d6ee96 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/handler/TypeGetter.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/handler/TypeGetter.java
@@ -41,27 +41,28 @@ public class TypeGetter {
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String tag = xpp.getName();
- if (tag.equals(ATOM_ROOT)) {
- feed.setType(Feed.TYPE_ATOM1);
- Log.d(TAG, "Recognized type Atom");
- return Type.ATOM;
- } else if (tag.equals(RSS_ROOT)) {
- String strVersion = xpp.getAttributeValue(null, "version");
- if (strVersion != null) {
- if (strVersion.equals("2.0")) {
- feed.setType(Feed.TYPE_RSS2);
- Log.d(TAG, "Recognized type RSS 2.0");
- return Type.RSS20;
- } else if (strVersion.equals("0.91")
- || strVersion.equals("0.92")) {
- Log.d(TAG, "Recognized type RSS 0.91/0.92");
- return Type.RSS091;
+ switch (tag) {
+ case ATOM_ROOT:
+ feed.setType(Feed.TYPE_ATOM1);
+ Log.d(TAG, "Recognized type Atom");
+ return Type.ATOM;
+ case RSS_ROOT:
+ String strVersion = xpp.getAttributeValue(null, "version");
+ if (strVersion != null) {
+ if (strVersion.equals("2.0")) {
+ feed.setType(Feed.TYPE_RSS2);
+ Log.d(TAG, "Recognized type RSS 2.0");
+ return Type.RSS20;
+ } else if (strVersion.equals("0.91")
+ || strVersion.equals("0.92")) {
+ Log.d(TAG, "Recognized type RSS 0.91/0.92");
+ return Type.RSS091;
+ }
}
- }
- throw new UnsupportedFeedtypeException(Type.INVALID);
- } else {
- Log.d(TAG, "Type is invalid");
- throw new UnsupportedFeedtypeException(Type.INVALID, tag);
+ throw new UnsupportedFeedtypeException(Type.INVALID);
+ default:
+ Log.d(TAG, "Type is invalid");
+ throw new UnsupportedFeedtypeException(Type.INVALID, tag);
}
} else {
eventType = xpp.next();
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSContent.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSContent.java
index 71bf69ffa..306b79c15 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSContent.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSContent.java
@@ -1,8 +1,9 @@
package de.danoeh.antennapod.core.syndication.namespace;
-import de.danoeh.antennapod.core.syndication.handler.HandlerState;
import org.xml.sax.Attributes;
+import de.danoeh.antennapod.core.syndication.handler.HandlerState;
+
public class NSContent extends Namespace {
public static final String NSTAG = "content";
public static final String NSURI = "http://purl.org/rss/1.0/modules/content/";
@@ -17,7 +18,8 @@ public class NSContent extends Namespace {
@Override
public void handleElementEnd(String localName, HandlerState state) {
- if (localName.equals(ENCODED)) {
+ if (ENCODED.equals(localName) && state.getCurrentItem() != null &&
+ state.getContentBuf() != null) {
state.getCurrentItem().setContentEncoded(state.getContentBuf().toString());
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSDublinCore.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSDublinCore.java
index 23f76186b..59d66a97e 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSDublinCore.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSDublinCore.java
@@ -2,6 +2,7 @@ package de.danoeh.antennapod.core.syndication.namespace;
import org.xml.sax.Attributes;
+import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.syndication.handler.HandlerState;
import de.danoeh.antennapod.core.util.DateUtils;
@@ -21,17 +22,16 @@ public class NSDublinCore extends Namespace {
@Override
public void handleElementEnd(String localName, HandlerState state) {
- if(state.getTagstack().size() >= 2
- && state.getContentBuf() != null) {
- String content = state.getContentBuf().toString();
- SyndElement topElement = state.getTagstack().peek();
- String top = topElement.getName();
- SyndElement secondElement = state.getSecondTag();
- String second = secondElement.getName();
- if (top.equals(DATE) && second.equals(ITEM)) {
- state.getCurrentItem().setPubDate(
- DateUtils.parse(content));
+ if (state.getCurrentItem() != null && state.getContentBuf() != null &&
+ state.getTagstack() != null && state.getTagstack().size() >= 2) {
+ FeedItem currentItem = state.getCurrentItem();
+ String top = state.getTagstack().peek().getName();
+ String second = state.getSecondTag().getName();
+ if (DATE.equals(top) && ITEM.equals(second)) {
+ String content = state.getContentBuf().toString();
+ currentItem.setPubDate(DateUtils.parse(content));
}
}
}
+
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java
index 99c4cd67a..1c424c6b5 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java
@@ -1,6 +1,7 @@
package de.danoeh.antennapod.core.syndication.namespace;
import android.text.TextUtils;
+import android.util.Log;
import org.xml.sax.Attributes;
@@ -10,6 +11,7 @@ import de.danoeh.antennapod.core.feed.FeedImage;
import de.danoeh.antennapod.core.syndication.handler.HandlerState;
public class NSITunes extends Namespace {
+
public static final String NSTAG = "itunes";
public static final String NSURI = "http://www.itunes.com/dtds/podcast-1.0.dtd";
@@ -26,69 +28,82 @@ public class NSITunes extends Namespace {
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
- if (localName.equals(IMAGE)) {
+ if (IMAGE.equals(localName)) {
FeedImage image = new FeedImage();
image.setTitle(IMAGE_TITLE);
image.setDownload_url(attributes.getValue(IMAGE_HREF));
if (state.getCurrentItem() != null) {
// this is an items image
- image.setTitle(state.getCurrentItem().getTitle()+IMAGE_TITLE);
+ image.setTitle(state.getCurrentItem().getTitle() + IMAGE_TITLE);
image.setOwner(state.getCurrentItem());
state.getCurrentItem().setImage(image);
-
- } else {
+ } else {
// this is the feed image
// prefer to all other images
- if(!TextUtils.isEmpty(image.getDownload_url())) {
+ if (!TextUtils.isEmpty(image.getDownload_url())) {
image.setOwner(state.getFeed());
state.getFeed().setImage(image);
}
}
-
}
-
return new SyndElement(localName, this);
}
@Override
public void handleElementEnd(String localName, HandlerState state) {
- if (localName.equals(AUTHOR)) {
- state.getFeed().setAuthor(state.getContentBuf().toString());
- } else if (localName.equals(DURATION)) {
- String[] parts = state.getContentBuf().toString().trim().split(":");
+ if(state.getContentBuf() == null) {
+ return;
+ }
+ if (AUTHOR.equals(localName)) {
+ if (state.getFeed() != null) {
+ String author = state.getContentBuf().toString();
+ state.getFeed().setAuthor(author);
+ }
+ } else if (DURATION.equals(localName)) {
+ String durationStr = state.getContentBuf().toString();
+ if(TextUtils.isEmpty(durationStr)) {
+ return;
+ }
+ String[] parts = durationStr.trim().split(":");
try {
- int duration = 0;
+ int durationMs = 0;
if (parts.length == 2) {
- duration += TimeUnit.MINUTES.toMillis(Long.valueOf(parts[0])) +
- TimeUnit.SECONDS.toMillis(Long.valueOf(parts[1]));
+ durationMs += TimeUnit.MINUTES.toMillis(Long.parseLong(parts[0])) +
+ TimeUnit.SECONDS.toMillis((long)Float.parseFloat(parts[1]));
} else if (parts.length >= 3) {
- duration += TimeUnit.HOURS.toMillis(Long.valueOf(parts[0])) +
- TimeUnit.MINUTES.toMillis(Long.valueOf(parts[1])) +
- TimeUnit.SECONDS.toMillis(Long.valueOf(parts[2]));
+ durationMs += TimeUnit.HOURS.toMillis(Long.parseLong(parts[0])) +
+ TimeUnit.MINUTES.toMillis(Long.parseLong(parts[1])) +
+ TimeUnit.SECONDS.toMillis((long)Float.parseFloat(parts[2]));
} else {
return;
}
- state.getTempObjects().put(DURATION, duration);
+ state.getTempObjects().put(DURATION, durationMs);
} catch (NumberFormatException e) {
- e.printStackTrace();
+ Log.e(NSTAG, "Duration \"" + durationStr + "\" could not be parsed");
}
- } else if (localName.equals(SUBTITLE)) {
+ } else if (SUBTITLE.equals(localName)) {
String subtitle = state.getContentBuf().toString();
+ if (TextUtils.isEmpty(subtitle)) {
+ return;
+ }
if (state.getCurrentItem() != null) {
if (TextUtils.isEmpty(state.getCurrentItem().getDescription())) {
state.getCurrentItem().setDescription(subtitle);
}
} else {
- if (TextUtils.isEmpty(state.getFeed().getDescription())) {
+ if (state.getFeed() != null && TextUtils.isEmpty(state.getFeed().getDescription())) {
state.getFeed().setDescription(subtitle);
}
}
- } else if (localName.equals(SUMMARY)) {
+ } else if (SUMMARY.equals(localName)) {
String summary = state.getContentBuf().toString();
+ if (TextUtils.isEmpty(summary)) {
+ return;
+ }
if (state.getCurrentItem() != null) {
state.getCurrentItem().setDescription(summary);
- } else {
+ } else if (state.getFeed() != null) {
state.getFeed().setDescription(summary);
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSMedia.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSMedia.java
index 7f03f1139..7a8b2bc03 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSMedia.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSMedia.java
@@ -1,14 +1,16 @@
package de.danoeh.antennapod.core.syndication.namespace;
+import android.text.TextUtils;
import android.util.Log;
-import de.danoeh.antennapod.core.BuildConfig;
-import de.danoeh.antennapod.core.feed.FeedMedia;
-import de.danoeh.antennapod.core.syndication.handler.HandlerState;
-import de.danoeh.antennapod.core.syndication.util.SyndTypeUtils;
+
import org.xml.sax.Attributes;
import java.util.concurrent.TimeUnit;
+import de.danoeh.antennapod.core.feed.FeedMedia;
+import de.danoeh.antennapod.core.syndication.handler.HandlerState;
+import de.danoeh.antennapod.core.syndication.util.SyndTypeUtils;
+
/** Processes tags from the http://search.yahoo.com/mrss/ namespace. */
public class NSMedia extends Namespace {
private static final String TAG = "NSMedia";
@@ -25,36 +27,41 @@ public class NSMedia extends Namespace {
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
- if (localName.equals(CONTENT)) {
+ if (CONTENT.equals(localName)) {
String url = attributes.getValue(DOWNLOAD_URL);
String type = attributes.getValue(MIME_TYPE);
- if (state.getCurrentItem().getMedia() == null
- && url != null
- && (SyndTypeUtils.enclosureTypeValid(type) || ((type = SyndTypeUtils
- .getValidMimeTypeFromUrl(url)) != null))) {
-
+ boolean validType;
+ if(SyndTypeUtils.enclosureTypeValid(type)) {
+ validType = true;
+ } else {
+ type = SyndTypeUtils.getValidMimeTypeFromUrl(url);
+ validType = type != null;
+ }
+ if (state.getCurrentItem() != null && state.getCurrentItem().getMedia() == null &&
+ url != null && validType) {
long size = 0;
+ String sizeStr = attributes.getValue(SIZE);
try {
- size = Long.parseLong(attributes.getValue(SIZE));
+ size = Long.parseLong(sizeStr);
} catch (NumberFormatException e) {
- if (BuildConfig.DEBUG)
- Log.d(TAG, "Length attribute could not be parsed.");
+ Log.e(TAG, "Size \"" + sizeStr + "\" could not be parsed.");
}
-
- int duration = 0;
- try {
- String durationStr = attributes.getValue(DURATION);
- if (durationStr != null) {
- duration = (int) TimeUnit.MILLISECONDS.convert(
- Long.parseLong(durationStr), TimeUnit.SECONDS);
+
+ int durationMs = 0;
+ String durationStr = attributes.getValue(DURATION);
+ if (!TextUtils.isEmpty(durationStr)) {
+ try {
+ long duration = Long.parseLong(durationStr);
+ durationMs = (int) TimeUnit.MILLISECONDS.convert(duration, TimeUnit.SECONDS);
+ } catch (NumberFormatException e) {
+ Log.e(TAG, "Duration \"" + durationStr + "\" could not be parsed");
}
- } catch (NumberFormatException e) {
- if (BuildConfig.DEBUG)
- Log.d(TAG, "Duration attribute could not be parsed");
}
-
- state.getCurrentItem().setMedia(
- new FeedMedia(state.getCurrentItem(), url, size, type));
+ FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, type);
+ if(durationMs > 0) {
+ media.setDuration(durationMs);
+ }
+ state.getCurrentItem().setMedia(media);
}
}
return new SyndElement(localName, this);
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSRSS20.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSRSS20.java
index 7e19213be..1c7952a56 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSRSS20.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSRSS20.java
@@ -1,10 +1,10 @@
package de.danoeh.antennapod.core.syndication.namespace;
+import android.text.TextUtils;
import android.util.Log;
import org.xml.sax.Attributes;
-import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedImage;
import de.danoeh.antennapod.core.feed.FeedItem;
@@ -43,18 +43,23 @@ public class NSRSS20 extends Namespace {
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
- if (localName.equals(ITEM)) {
+ if (ITEM.equals(localName)) {
state.setCurrentItem(new FeedItem());
state.getItems().add(state.getCurrentItem());
state.getCurrentItem().setFeed(state.getFeed());
- } else if (localName.equals(ENCLOSURE)) {
+ } else if (ENCLOSURE.equals(localName)) {
String type = attributes.getValue(ENC_TYPE);
String url = attributes.getValue(ENC_URL);
- if (state.getCurrentItem().getMedia() == null
- && (SyndTypeUtils.enclosureTypeValid(type) || ((type = SyndTypeUtils
- .getValidMimeTypeFromUrl(url)) != null))) {
-
+ boolean validType;
+ if(SyndTypeUtils.enclosureTypeValid(type)) {
+ validType = true;
+ } else {
+ type = type = SyndTypeUtils.getValidMimeTypeFromUrl(url);
+ validType = type != null;
+ }
+ if (state.getCurrentItem() != null && state.getCurrentItem().getMedia() == null &&
+ validType) {
long size = 0;
try {
size = Long.parseLong(attributes.getValue(ENC_LEN));
@@ -63,19 +68,18 @@ public class NSRSS20 extends Namespace {
size = 0;
}
} catch (NumberFormatException e) {
- if (BuildConfig.DEBUG)
- Log.d(TAG, "Length attribute could not be parsed.");
+ Log.d(TAG, "Length attribute could not be parsed.");
}
state.getCurrentItem().setMedia(
new FeedMedia(state.getCurrentItem(), url, size, type));
}
- } else if (localName.equals(IMAGE)) {
+ } else if (IMAGE.equals(localName)) {
if (state.getTagstack().size() >= 1) {
String parent = state.getTagstack().peek().getName();
- if (parent.equals(CHANNEL)) {
+ if (CHANNEL.equals(parent)) {
Feed feed = state.getFeed();
- if(feed.getImage() == null) {
+ if(feed != null && feed.getImage() == null) {
feed.setImage(new FeedImage());
feed.getImage().setOwner(state.getFeed());
}
@@ -87,26 +91,26 @@ public class NSRSS20 extends Namespace {
@Override
public void handleElementEnd(String localName, HandlerState state) {
- if (localName.equals(ITEM)) {
+ if (ITEM.equals(localName)) {
if (state.getCurrentItem() != null) {
+ FeedItem currentItem = state.getCurrentItem();
// the title tag is optional in RSS 2.0. The description is used
// as a
// title if the item has no title-tag.
- if (state.getCurrentItem().getTitle() == null) {
- state.getCurrentItem().setTitle(
- state.getCurrentItem().getDescription());
+ if (currentItem.getTitle() == null) {
+ currentItem.setTitle(currentItem.getDescription());
}
if (state.getTempObjects().containsKey(NSITunes.DURATION)) {
- if (state.getCurrentItem().hasMedia()) {
- state.getCurrentItem().getMedia().setDuration((Integer) state.getTempObjects().get(NSITunes.DURATION));
+ if (currentItem.hasMedia()) {
+ Integer duration = (Integer) state.getTempObjects().get(NSITunes.DURATION);
+ currentItem.getMedia().setDuration(duration);
}
state.getTempObjects().remove(NSITunes.DURATION);
}
}
state.setCurrentItem(null);
- } else if (state.getTagstack().size() >= 2
- && state.getContentBuf() != null) {
+ } else if (state.getTagstack().size() >= 2 && state.getContentBuf() != null) {
String content = state.getContentBuf().toString();
SyndElement topElement = state.getTagstack().peek();
String top = topElement.getName();
@@ -116,46 +120,44 @@ public class NSRSS20 extends Namespace {
if (state.getTagstack().size() >= 3) {
third = state.getThirdTag().getName();
}
-
- if (top.equals(GUID) && second.equals(ITEM)) {
+ if (GUID.equals(top) && ITEM.equals(second)) {
// some feed creators include an empty or non-standard guid-element in their feed, which should be ignored
- if (!content.isEmpty()) {
+ if (!TextUtils.isEmpty(content) && state.getCurrentItem() != null) {
state.getCurrentItem().setItemIdentifier(content);
}
- } else if (top.equals(TITLE)) {
+ } else if (TITLE.equals(top)) {
String title = content.trim();
- if (second.equals(ITEM)) {
+ if (ITEM.equals(second) && state.getCurrentItem() != null) {
state.getCurrentItem().setTitle(title);
- } else if (second.equals(CHANNEL)) {
+ } else if (CHANNEL.equals(second) && state.getFeed() != null) {
state.getFeed().setTitle(title);
- } else if (second.equals(IMAGE) && third != null
- && third.equals(CHANNEL)) {
- if(state.getFeed().getImage().getTitle() == null) {
+ } else if (IMAGE.equals(second) && CHANNEL.equals(third)) {
+ if(state.getFeed() != null && state.getFeed().getImage() != null &&
+ state.getFeed().getImage().getTitle() == null) {
state.getFeed().getImage().setTitle(title);
}
}
- } else if (top.equals(LINK)) {
- if (second.equals(CHANNEL)) {
+ } else if (LINK.equals(top)) {
+ if (CHANNEL.equals(second) && state.getFeed() != null) {
state.getFeed().setLink(content);
- } else if (second.equals(ITEM)) {
+ } else if (ITEM.equals(second) && state.getCurrentItem() != null) {
state.getCurrentItem().setLink(content);
}
- } else if (top.equals(PUBDATE) && second.equals(ITEM)) {
- state.getCurrentItem().setPubDate(
- DateUtils.parse(content));
- } else if (top.equals(URL) && second.equals(IMAGE) && third != null
- && third.equals(CHANNEL)) {
- if(state.getFeed().getImage().getDownload_url() == null) { // prefer itunes:image
+ } else if (PUBDATE.equals(top) && ITEM.equals(second) && state.getCurrentItem() != null) {
+ state.getCurrentItem().setPubDate(DateUtils.parse(content));
+ } else if (URL.equals(top) && IMAGE.equals(second) && CHANNEL.equals(third)) {
+ // prefer itunes:image
+ if(state.getFeed() != null && state.getFeed().getImage() != null &&
+ state.getFeed().getImage().getDownload_url() == null) {
state.getFeed().getImage().setDownload_url(content);
}
- } else if (localName.equals(DESCR)) {
- if (second.equals(CHANNEL)) {
+ } else if (DESCR.equals(localName)) {
+ if (CHANNEL.equals(second) && state.getFeed() != null) {
state.getFeed().setDescription(content);
- } else if (second.equals(ITEM)) {
+ } else if (ITEM.equals(second) && state.getCurrentItem() != null) {
state.getCurrentItem().setDescription(content);
}
-
- } else if (localName.equals(LANGUAGE)) {
+ } else if (LANGUAGE.equals(localName) && state.getFeed() != null) {
state.getFeed().setLanguage(content.toLowerCase());
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSSimpleChapters.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSSimpleChapters.java
index 64b82100e..703817a35 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSSimpleChapters.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSSimpleChapters.java
@@ -6,8 +6,7 @@ import org.xml.sax.Attributes;
import java.util.ArrayList;
-import de.danoeh.antennapod.core.BuildConfig;
-import de.danoeh.antennapod.core.feed.Chapter;
+import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.SimpleChapter;
import de.danoeh.antennapod.core.syndication.handler.HandlerState;
import de.danoeh.antennapod.core.util.DateUtils;
@@ -27,21 +26,22 @@ public class NSSimpleChapters extends Namespace {
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
- if (localName.equals(CHAPTERS)) {
- state.getCurrentItem().setChapters(new ArrayList<Chapter>());
- } else if (localName.equals(CHAPTER)) {
- try {
- state.getCurrentItem()
- .getChapters()
- .add(new SimpleChapter(DateUtils
- .parseTimeString(attributes.getValue(START)),
- attributes.getValue(TITLE), state.getCurrentItem(),
- attributes.getValue(HREF)));
- } catch (NumberFormatException e) {
- if (BuildConfig.DEBUG) Log.w(TAG, "Unable to read chapter", e);
+ FeedItem currentItem = state.getCurrentItem();
+ if(currentItem != null) {
+ if (localName.equals(CHAPTERS)) {
+ currentItem.setChapters(new ArrayList<>());
+ } else if (localName.equals(CHAPTER)) {
+ try {
+ long start = DateUtils.parseTimeString(attributes.getValue(START));
+ String title = attributes.getValue(TITLE);
+ String link = attributes.getValue(HREF);
+ SimpleChapter chapter = new SimpleChapter(start, title, currentItem, link);
+ currentItem.getChapters().add(chapter);
+ } catch (NumberFormatException e) {
+ Log.e(TAG, "Unable to read chapter", e);
+ }
}
}
-
return new SyndElement(localName, this);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/atom/NSAtom.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/atom/NSAtom.java
index b23a142af..7b5abf053 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/atom/NSAtom.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/atom/NSAtom.java
@@ -1,5 +1,6 @@
package de.danoeh.antennapod.core.syndication.namespace.atom;
+import android.text.TextUtils;
import android.util.Log;
import org.xml.sax.Attributes;
@@ -65,21 +66,21 @@ public class NSAtom extends Namespace {
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
- if (localName.equals(ENTRY)) {
+ if (ENTRY.equals(localName)) {
state.setCurrentItem(new FeedItem());
state.getItems().add(state.getCurrentItem());
state.getCurrentItem().setFeed(state.getFeed());
} else if (localName.matches(isText)) {
String type = attributes.getValue(TEXT_TYPE);
return new AtomText(localName, this, type);
- } else if (localName.equals(LINK)) {
+ } else if (LINK.equals(localName)) {
String href = attributes.getValue(LINK_HREF);
String rel = attributes.getValue(LINK_REL);
SyndElement parent = state.getTagstack().peek();
if (parent.getName().matches(isFeedItem)) {
- if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
+ if (LINK_REL_ALTERNATE.equals(rel)) {
state.getCurrentItem().setLink(href);
- } else if (rel.equals(LINK_REL_ENCLOSURE)) {
+ } else if (LINK_REL_ENCLOSURE.equals(rel)) {
String strSize = attributes.getValue(LINK_LENGTH);
long size = 0;
try {
@@ -90,40 +91,45 @@ public class NSAtom extends Namespace {
Log.d(TAG, "Length attribute could not be parsed.");
}
String type = attributes.getValue(LINK_TYPE);
- if (SyndTypeUtils.enclosureTypeValid(type)
- || (type = SyndTypeUtils.getValidMimeTypeFromUrl(href)) != null) {
+ boolean validType;
+ if(SyndTypeUtils.enclosureTypeValid(type)) {
+ validType = true;
+ } else {
+ type = SyndTypeUtils.getValidMimeTypeFromUrl(href);
+ validType = type != null;
+ }
+ if (validType) {
FeedItem currItem = state.getCurrentItem();
- if(!currItem.hasMedia()) {
+ if(currItem != null && !currItem.hasMedia()) {
currItem.setMedia(new FeedMedia(currItem, href, size, type));
}
}
- } else if (rel.equals(LINK_REL_PAYMENT)) {
+ } else if (LINK_REL_PAYMENT.equals(rel)) {
state.getCurrentItem().setPaymentLink(href);
}
} else if (parent.getName().matches(isFeed)) {
- if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
+ if (LINK_REL_ALTERNATE.equals(rel)) {
String type = attributes.getValue(LINK_TYPE);
/*
* Use as link if a) no type-attribute is given and
* feed-object has no link yet b) type of link is
* LINK_TYPE_HTML or LINK_TYPE_XHTML
*/
- if ((type == null && state.getFeed().getLink() == null)
- || (type != null && (type.equals(LINK_TYPE_HTML)
- || type.equals(LINK_TYPE_XHTML)))) {
+ if (state.getFeed() != null &&
+ ((type == null && state.getFeed().getLink() == null) ||
+ (LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)))) {
state.getFeed().setLink(href);
- } else if (type != null && (type.equals(LINK_TYPE_ATOM)
- || type.equals(LINK_TYPE_RSS))) {
+ } else if (LINK_TYPE_ATOM.equals(type) || LINK_TYPE_RSS.equals(type)) {
// treat as podlove alternate feed
String title = attributes.getValue(LINK_TITLE);
- if (title == null) {
+ if (TextUtils.isEmpty(title)) {
title = href;
}
state.addAlternateFeedUrl(title, href);
}
- } else if (rel.equals(LINK_REL_PAYMENT)) {
+ } else if (LINK_REL_PAYMENT.equals(rel) && state.getFeed() != null) {
state.getFeed().setPaymentLink(href);
- } else if (rel.equals(LINK_REL_NEXT)) {
+ } else if (LINK_REL_NEXT.equals(rel) && state.getFeed() != null) {
state.getFeed().setPaged(true);
state.getFeed().setNextPageLink(href);
}
@@ -134,11 +140,13 @@ public class NSAtom extends Namespace {
@Override
public void handleElementEnd(String localName, HandlerState state) {
- if (localName.equals(ENTRY)) {
+ if (ENTRY.equals(localName)) {
if (state.getCurrentItem() != null &&
state.getTempObjects().containsKey(NSITunes.DURATION)) {
- if (state.getCurrentItem().hasMedia()) {
- state.getCurrentItem().getMedia().setDuration((Integer) state.getTempObjects().get(NSITunes.DURATION));
+ FeedItem currentItem = state.getCurrentItem();
+ if (currentItem.hasMedia()) {
+ Integer duration = (Integer) state.getTempObjects().get(NSITunes.DURATION);
+ currentItem.getMedia().setDuration(duration);
}
state.getTempObjects().remove(NSITunes.DURATION);
}
@@ -163,47 +171,32 @@ public class NSAtom extends Namespace {
textElement.setContent(content);
}
- if (top.equals(ID)) {
- if (second.equals(FEED)) {
+ if (ID.equals(top)) {
+ if (FEED.equals(second) && state.getFeed() != null) {
state.getFeed().setFeedIdentifier(content);
- } else if (second.equals(ENTRY)) {
+ } else if (ENTRY.equals(second) && state.getCurrentItem() != null) {
state.getCurrentItem().setItemIdentifier(content);
}
- } else if (top.equals(TITLE)) {
-
- if (second.equals(FEED)) {
+ } else if (TITLE.equals(top) && textElement != null) {
+ if (FEED.equals(second) && state.getFeed() != null) {
state.getFeed().setTitle(textElement.getProcessedContent());
- } else if (second.equals(ENTRY)) {
- state.getCurrentItem().setTitle(
- textElement.getProcessedContent());
- }
- } else if (top.equals(SUBTITLE)) {
- if (second.equals(FEED)) {
- state.getFeed().setDescription(
- textElement.getProcessedContent());
- }
- } else if (top.equals(CONTENT)) {
- if (second.equals(ENTRY)) {
- state.getCurrentItem().setDescription(
- textElement.getProcessedContent());
- }
- } else if (top.equals(UPDATED)) {
- if (second.equals(ENTRY)
- && state.getCurrentItem().getPubDate() == null) {
- state.getCurrentItem().setPubDate(
- DateUtils.parse(content));
- }
- } else if (top.equals(PUBLISHED)) {
- if (second.equals(ENTRY)) {
- state.getCurrentItem().setPubDate(
- DateUtils.parse(content));
- }
- } else if (top.equals(IMAGE)) {
- if(state.getFeed().getImage() == null) {
- state.getFeed().setImage(new FeedImage(state.getFeed(), content, null));
+ } else if (ENTRY.equals(second) && state.getCurrentItem() != null) {
+ state.getCurrentItem().setTitle(textElement.getProcessedContent());
}
+ } else if (SUBTITLE.equals(top) && FEED.equals(second) && textElement != null &&
+ state.getFeed() != null) {
+ state.getFeed().setDescription(textElement.getProcessedContent());
+ } else if (CONTENT.equals(top) && ENTRY.equals(second) && textElement != null &&
+ state.getCurrentItem() != null) {
+ state.getCurrentItem().setDescription(textElement.getProcessedContent());
+ } else if (UPDATED.equals(top) && ENTRY.equals(second) && state.getCurrentItem() != null &&
+ state.getCurrentItem().getPubDate() == null) {
+ state.getCurrentItem().setPubDate(DateUtils.parse(content));
+ } else if (PUBLISHED.equals(top) && ENTRY.equals(second) && state.getCurrentItem() != null) {
+ state.getCurrentItem().setPubDate(DateUtils.parse(content));
+ } else if (IMAGE.equals(top) && state.getFeed() != null && state.getFeed().getImage() == null) {
+ state.getFeed().setImage(new FeedImage(state.getFeed(), content, null));
}
-
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/util/SyndTypeUtils.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/util/SyndTypeUtils.java
index 8d1d8ffde..e84361fb2 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/syndication/util/SyndTypeUtils.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/util/SyndTypeUtils.java
@@ -30,8 +30,7 @@ public class SyndTypeUtils {
if (url != null) {
String extension = FilenameUtils.getExtension(url);
if (extension != null) {
- String type = MimeTypeMap.getSingleton()
- .getMimeTypeFromExtension(extension);
+ String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (type != null && enclosureTypeValid(type)) {
return type;
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/ChapterUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/ChapterUtils.java
index 8dd9ffe4b..5169f7e76 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/ChapterUtils.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/ChapterUtils.java
@@ -10,7 +10,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
@@ -63,11 +62,7 @@ public class ChapterUtils {
} else {
Log.i(TAG, "ChapterReader could not find any ID3 chapters");
}
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ID3ReaderException e) {
+ } catch (IOException | ID3ReaderException e) {
e.printStackTrace();
} finally {
if (in != null) {
@@ -116,9 +111,7 @@ public class ChapterUtils {
Log.i(TAG,
"ChapterReader could not find any ID3 chapters");
}
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ID3ReaderException e) {
+ } catch (IOException | ID3ReaderException e) {
e.printStackTrace();
} finally {
if (in != null) {
@@ -144,8 +137,6 @@ public class ChapterUtils {
if (input != null) {
readOggChaptersFromInputStream(media, input);
}
- } catch (MalformedURLException e) {
- e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/Converter.java b/core/src/main/java/de/danoeh/antennapod/core/util/Converter.java
index 1b929b214..70a180913 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/Converter.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/Converter.java
@@ -3,6 +3,8 @@ package de.danoeh.antennapod.core.util;
import android.content.Context;
import android.util.Log;
+import java.util.Locale;
+
import de.danoeh.antennapod.core.R;
/** Provides methods for converting various units. */
@@ -88,9 +90,9 @@ public final class Converter {
if (parts.length != 3) {
return 0;
}
- return Integer.valueOf(parts[0]) * 3600 * 1000 +
- Integer.valueOf(parts[1]) * 60 * 1000 +
- Integer.valueOf(parts[2]) * 1000;
+ return Integer.parseInt(parts[0]) * 3600 * 1000 +
+ Integer.parseInt(parts[1]) * 60 * 1000 +
+ Integer.parseInt(parts[2]) * 1000;
}
/** Converts short duration string (HH:MM) to milliseconds. */
@@ -99,8 +101,8 @@ public final class Converter {
if (parts.length != 2) {
return 0;
}
- return Integer.valueOf(parts[0]) * 3600 * 1000 +
- Integer.valueOf(parts[1]) * 1000 * 60;
+ return Integer.parseInt(parts[0]) * 3600 * 1000 +
+ Integer.parseInt(parts[1]) * 1000 * 60;
}
/** Converts milliseconds to a localized string containing hours and minutes */
@@ -118,5 +120,26 @@ public final class Converter {
result += minutes;
return result;
}
-
+
+ /**
+ * Converts seconds to a localized representation
+ * @param time The time in seconds
+ * @return "HH:MM hours"
+ */
+ public static String shortLocalizedDuration(Context context, long time) {
+ float hours = (float) time / 3600f;
+ return String.format(Locale.getDefault(), "%.1f ", hours) + context.getString(R.string.time_hours);
+ }
+
+ /**
+ * Converts the volume as read as the progress from a SeekBar scaled to 100 and as saved in
+ * UserPreferences to the format taken by setVolume methods.
+ * @param progress integer between 0 to 100 taken from the SeekBar progress
+ * @return the appropriate volume as float taken by setVolume methods
+ */
+ public static float getVolumeFromPercentage(int progress){
+ if (progress==100)
+ return 1f;
+ return (float) (1 - (Math.log(101 - progress) / Math.log(101)));
+ }
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java
index 4b4201b50..314062e52 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java
@@ -19,14 +19,8 @@ public class DateUtils {
private static final String TAG = "DateUtils";
- private static final SimpleDateFormat parser = new SimpleDateFormat("", Locale.US);
private static final TimeZone defaultTimezone = TimeZone.getTimeZone("GMT");
- static {
- parser.setLenient(false);
- parser.setTimeZone(defaultTimezone);
- }
-
public static Date parse(final String input) {
if(input == null) {
throw new IllegalArgumentException("Date must not be null");
@@ -86,6 +80,10 @@ public class DateUtils {
"yyyy-MM-dd"
};
+ SimpleDateFormat parser = new SimpleDateFormat("", Locale.US);
+ parser.setLenient(false);
+ parser.setTimeZone(defaultTimezone);
+
ParsePosition pos = new ParsePosition(0);
for(String pattern : patterns) {
parser.applyPattern(pattern);
@@ -117,13 +115,13 @@ public class DateUtils {
int idx = 0;
if (parts.length == 3) {
// string has hours
- result += Integer.valueOf(parts[idx]) * 3600000L;
+ result += Integer.parseInt(parts[idx]) * 3600000L;
idx++;
}
if (parts.length >= 2) {
- result += Integer.valueOf(parts[idx]) * 60000L;
+ result += Integer.parseInt(parts[idx]) * 60000L;
idx++;
- result += (Float.valueOf(parts[idx])) * 1000L;
+ result += (Float.parseFloat(parts[idx])) * 1000L;
}
return result;
}
@@ -145,6 +143,9 @@ public class DateUtils {
}
public static String formatAbbrev(final Context context, final Date date) {
+ if(date == null) {
+ return "";
+ }
GregorianCalendar cal = new GregorianCalendar();
cal.add(GregorianCalendar.YEAR, -1);
// some padding, because no one really remembers what day of the month it is
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/DownloadError.java b/core/src/main/java/de/danoeh/antennapod/core/util/DownloadError.java
index 602c221bf..4ad88743f 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/DownloadError.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/DownloadError.java
@@ -19,12 +19,13 @@ public enum DownloadError {
ERROR_UNKNOWN_HOST(11, R.string.download_error_unknown_host),
ERROR_REQUEST_ERROR(12, R.string.download_error_request_error),
ERROR_DB_ACCESS_ERROR(13, R.string.download_error_db_access),
- ERROR_UNAUTHORIZED(14, R.string.download_error_unauthorized);
+ ERROR_UNAUTHORIZED(14, R.string.download_error_unauthorized),
+ ERROR_FILE_TYPE(15, R.string.download_error_file_type_type);
private final int code;
private final int resId;
- private DownloadError(int code, int resId) {
+ DownloadError(int code, int resId) {
this.code = code;
this.resId = resId;
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/EpisodeFilter.java b/core/src/main/java/de/danoeh/antennapod/core/util/EpisodeFilter.java
index 029e7fe84..89edd7dbe 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/EpisodeFilter.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/EpisodeFilter.java
@@ -13,7 +13,7 @@ public class EpisodeFilter {
/** Return a copy of the itemlist without items which have no media. */
public static ArrayList<FeedItem> getEpisodeList(List<FeedItem> items) {
- ArrayList<FeedItem> episodes = new ArrayList<FeedItem>(items);
+ ArrayList<FeedItem> episodes = new ArrayList<>(items);
for (FeedItem item : items) {
if (item.getMedia() == null) {
episodes.remove(item);
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/IntList.java b/core/src/main/java/de/danoeh/antennapod/core/util/IntList.java
index 673c81235..f48b9169b 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/IntList.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/IntList.java
@@ -35,7 +35,7 @@ public final class IntList {
int hashCode = 1;
for (int i = 0; i < size; i++) {
int value = values[i];
- hashCode = 31 * hashCode + (int)(value ^ (value >>> 32));
+ hashCode = 31 * hashCode + value;
}
return hashCode;
}
@@ -62,7 +62,7 @@ public final class IntList {
@Override
public String toString() {
- StringBuffer sb = new StringBuffer(size * 5 + 10);
+ StringBuilder sb = new StringBuilder(size * 5 + 10);
sb.append("IntList{");
for (int i = 0; i < size; i++) {
if (i != 0) {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/LongList.java b/core/src/main/java/de/danoeh/antennapod/core/util/LongList.java
index 6ed8b820e..fdc244517 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/LongList.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/LongList.java
@@ -73,7 +73,7 @@ public final class LongList {
@Override
public String toString() {
- StringBuffer sb = new StringBuffer(size * 5 + 10);
+ StringBuilder sb = new StringBuilder(size * 5 + 10);
sb.append("LongList{");
for (int i = 0; i < size; i++) {
if (i != 0) {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java
index c2cd273b8..927639e69 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java
@@ -96,7 +96,7 @@ public class NetworkUtils {
return Observable.create(new Observable.OnSubscribe<Long>() {
@Override
public void call(Subscriber<? super Long> subscriber) {
- if (false == NetworkUtils.isDownloadAllowed()) {
+ if (!NetworkUtils.isDownloadAllowed()) {
subscriber.onNext(0L);
subscriber.onCompleted();
return;
@@ -107,7 +107,7 @@ public class NetworkUtils {
if (mediaFile.exists()) {
size = mediaFile.length();
}
- } else if (false == media.checkedOnSizeButUnknown()) {
+ } else if (!media.checkedOnSizeButUnknown()) {
// only query the network if we haven't already checked
String url = media.getDownload_url();
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java b/core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java
index 71d6040ba..5dc194dbd 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java
@@ -2,12 +2,12 @@ package de.danoeh.antennapod.core.util;
import android.content.Context;
+import java.util.Comparator;
+
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.storage.DBWriter;
-import java.util.Comparator;
-
/**
* Provides method for sorting the queue according to rules.
*/
@@ -26,59 +26,39 @@ public class QueueSorter {
switch (rule) {
case ALPHA_ASC:
- comparator = new Comparator<FeedItem>() {
- public int compare(FeedItem f1, FeedItem f2) {
- return f1.getTitle().compareTo(f2.getTitle());
- }
- };
+ comparator = (f1, f2) -> f1.getTitle().compareTo(f2.getTitle());
break;
case ALPHA_DESC:
- comparator = new Comparator<FeedItem>() {
- public int compare(FeedItem f1, FeedItem f2) {
- return f2.getTitle().compareTo(f1.getTitle());
- }
- };
+ comparator = (f1, f2) -> f2.getTitle().compareTo(f1.getTitle());
break;
case DATE_ASC:
- comparator = new Comparator<FeedItem>() {
- public int compare(FeedItem f1, FeedItem f2) {
- return f1.getPubDate().compareTo(f2.getPubDate());
- }
- };
+ comparator = (f1, f2) -> f1.getPubDate().compareTo(f2.getPubDate());
break;
case DATE_DESC:
- comparator = new Comparator<FeedItem>() {
- public int compare(FeedItem f1, FeedItem f2) {
- return f2.getPubDate().compareTo(f1.getPubDate());
- }
- };
+ comparator = (f1, f2) -> f2.getPubDate().compareTo(f1.getPubDate());
break;
case DURATION_ASC:
- comparator = new Comparator<FeedItem>() {
- public int compare(FeedItem f1, FeedItem f2) {
- FeedMedia f1Media = f1.getMedia();
- FeedMedia f2Media = f2.getMedia();
- int duration1 = f1Media != null ? f1Media.getDuration() : -1;
- int duration2 = f2Media != null ? f2Media.getDuration() : -1;
+ comparator = (f1, f2) -> {
+ FeedMedia f1Media = f1.getMedia();
+ FeedMedia f2Media = f2.getMedia();
+ int duration1 = f1Media != null ? f1Media.getDuration() : -1;
+ int duration2 = f2Media != null ? f2Media.getDuration() : -1;
- if (duration1 == -1 || duration2 == -1)
- return duration2 - duration1;
- else
- return duration1 - duration2;
- }
+ if (duration1 == -1 || duration2 == -1)
+ return duration2 - duration1;
+ else
+ return duration1 - duration2;
};
break;
case DURATION_DESC:
- comparator = new Comparator<FeedItem>() {
- public int compare(FeedItem f1, FeedItem f2) {
+ comparator = (f1, f2) -> {
FeedMedia f1Media = f1.getMedia();
FeedMedia f2Media = f2.getMedia();
int duration1 = f1Media != null ? f1Media.getDuration() : -1;
int duration2 = f2Media != null ? f2Media.getDuration() : -1;
return -1 * (duration1 - duration2);
- }
- };
+ };
default:
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/ShownotesProvider.java b/core/src/main/java/de/danoeh/antennapod/core/util/ShownotesProvider.java
index 7e7c6c08b..a4cd83f70 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/ShownotesProvider.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/ShownotesProvider.java
@@ -11,6 +11,6 @@ public interface ShownotesProvider {
* database, it should be done in a separate thread. After the shownotes
* have been loaded, callback.onShownotesLoaded should be called.
*/
- public Callable<String> loadShownotes();
+ Callable<String> loadShownotes();
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/URIUtil.java b/core/src/main/java/de/danoeh/antennapod/core/util/URIUtil.java
index 092c06b4a..e093dc766 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/URIUtil.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/URIUtil.java
@@ -1,13 +1,14 @@
package de.danoeh.antennapod.core.util;
import android.util.Log;
-import de.danoeh.antennapod.core.BuildConfig;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
+import de.danoeh.antennapod.core.BuildConfig;
+
/**
* Utility methods for dealing with URL encoding.
*/
@@ -26,9 +27,7 @@ public class URIUtil {
try {
URL url = new URL(source);
return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
- } catch (MalformedURLException e) {
- throw new IllegalArgumentException(e);
- } catch (URISyntaxException e) {
+ } catch (MalformedURLException | URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/flattr/FlattrServiceCreator.java b/core/src/main/java/de/danoeh/antennapod/core/util/flattr/FlattrServiceCreator.java
index e4818214e..97958eea7 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/flattr/FlattrServiceCreator.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/flattr/FlattrServiceCreator.java
@@ -1,11 +1,13 @@
package de.danoeh.antennapod.core.util.flattr;
import android.util.Log;
-import de.danoeh.antennapod.core.BuildConfig;
+
import org.shredzone.flattr4j.FlattrFactory;
import org.shredzone.flattr4j.FlattrService;
import org.shredzone.flattr4j.oauth.AccessToken;
+import de.danoeh.antennapod.core.BuildConfig;
+
/** Ensures that only one instance of the FlattrService class exists at a time */
public class FlattrServiceCreator {
@@ -13,11 +15,14 @@ public class FlattrServiceCreator {
private static volatile FlattrService flattrService;
- public static FlattrService getService(AccessToken token) {
- return FlattrFactory.getInstance().createFlattrService(token);
+ public synchronized static FlattrService getService(AccessToken token) {
+ if (flattrService == null) {
+ flattrService = FlattrFactory.getInstance().createFlattrService(token);
+ }
+ return flattrService;
}
- public static void deleteFlattrService() {
+ public synchronized static void deleteFlattrService() {
if (BuildConfig.DEBUG) Log.d(TAG, "Deleting service instance");
flattrService = null;
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/gui/FeedItemUndoToken.java b/core/src/main/java/de/danoeh/antennapod/core/util/gui/FeedItemUndoToken.java
deleted file mode 100644
index 17581d3e9..000000000
--- a/core/src/main/java/de/danoeh/antennapod/core/util/gui/FeedItemUndoToken.java
+++ /dev/null
@@ -1,55 +0,0 @@
-package de.danoeh.antennapod.core.util.gui;
-
-import android.os.Parcel;
-import android.os.Parcelable;
-import de.danoeh.antennapod.core.feed.FeedItem;
-
-/**
- * Used by an UndoBarController for saving a removed FeedItem
- */
-public class FeedItemUndoToken implements Parcelable {
- private long itemId;
- private long feedId;
- private int position;
-
- public FeedItemUndoToken(FeedItem item, int position) {
- this.itemId = item.getId();
- this.feedId = item.getFeed().getId();
- this.position = position;
- }
-
- private FeedItemUndoToken(Parcel in) {
- itemId = in.readLong();
- feedId = in.readLong();
- position = in.readInt();
- }
-
- public static final Parcelable.Creator<FeedItemUndoToken> CREATOR = new Parcelable.Creator<FeedItemUndoToken>() {
- public FeedItemUndoToken createFromParcel(Parcel in) {
- return new FeedItemUndoToken(in);
- }
-
- public FeedItemUndoToken[] newArray(int size) {
- return new FeedItemUndoToken[size];
- }
- };
-
- public int describeContents() {
- return 0;
- }
-
- public void writeToParcel(Parcel out, int flags) {
- out.writeLong(itemId);
- out.writeLong(feedId);
- out.writeInt(position);
- }
-
- public long getFeedItemId() {
- return itemId;
- }
-
- public int getPosition() {
- return position;
- }
-}
-
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/gui/MoreContentListFooterUtil.java b/core/src/main/java/de/danoeh/antennapod/core/util/gui/MoreContentListFooterUtil.java
index d56871fd1..386f46724 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/gui/MoreContentListFooterUtil.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/gui/MoreContentListFooterUtil.java
@@ -19,12 +19,9 @@ public class MoreContentListFooterUtil {
public MoreContentListFooterUtil(View root) {
this.root = root;
- root.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (listener != null && !loading) {
- listener.onClick();
- }
+ root.setOnClickListener(v -> {
+ if (listener != null && !loading) {
+ listener.onClick();
}
});
}
@@ -46,8 +43,8 @@ public class MoreContentListFooterUtil {
listener = l;
}
- public static interface Listener {
- public void onClick();
+ public interface Listener {
+ void onClick();
}
public View getRoot() {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/id3reader/ChapterReader.java b/core/src/main/java/de/danoeh/antennapod/core/util/id3reader/ChapterReader.java
index 9f3c4c6d5..1807421b0 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/id3reader/ChapterReader.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/id3reader/ChapterReader.java
@@ -1,11 +1,6 @@
package de.danoeh.antennapod.core.util.id3reader;
import android.util.Log;
-import de.danoeh.antennapod.core.BuildConfig;
-import de.danoeh.antennapod.core.feed.Chapter;
-import de.danoeh.antennapod.core.feed.ID3Chapter;
-import de.danoeh.antennapod.core.util.id3reader.model.FrameHeader;
-import de.danoeh.antennapod.core.util.id3reader.model.TagHeader;
import java.io.IOException;
import java.io.InputStream;
@@ -13,6 +8,12 @@ import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
+import de.danoeh.antennapod.core.BuildConfig;
+import de.danoeh.antennapod.core.feed.Chapter;
+import de.danoeh.antennapod.core.feed.ID3Chapter;
+import de.danoeh.antennapod.core.util.id3reader.model.FrameHeader;
+import de.danoeh.antennapod.core.util.id3reader.model.TagHeader;
+
public class ChapterReader extends ID3Reader {
private static final String TAG = "ID3ChapterReader";
@@ -25,7 +26,7 @@ public class ChapterReader extends ID3Reader {
@Override
public int onStartTagHeader(TagHeader header) {
- chapters = new ArrayList<Chapter>();
+ chapters = new ArrayList<>();
System.out.println(header.toString());
return ID3Reader.ACTION_DONT_SKIP;
}
@@ -34,49 +35,53 @@ public class ChapterReader extends ID3Reader {
public int onStartFrameHeader(FrameHeader header, InputStream input)
throws IOException, ID3ReaderException {
System.out.println(header.toString());
- if (header.getId().equals(FRAME_ID_CHAPTER)) {
- if (currentChapter != null) {
- if (!hasId3Chapter(currentChapter)) {
- chapters.add(currentChapter);
- if (BuildConfig.DEBUG) Log.d(TAG, "Found chapter: " + currentChapter);
- currentChapter = null;
+ switch (header.getId()) {
+ case FRAME_ID_CHAPTER:
+ if (currentChapter != null) {
+ if (!hasId3Chapter(currentChapter)) {
+ chapters.add(currentChapter);
+ if (BuildConfig.DEBUG) Log.d(TAG, "Found chapter: " + currentChapter);
+ currentChapter = null;
+ }
}
- }
- StringBuffer elementId = new StringBuffer();
- readISOString(elementId, input, Integer.MAX_VALUE);
- char[] startTimeSource = readBytes(input, 4);
- long startTime = ((int) startTimeSource[0] << 24)
- | ((int) startTimeSource[1] << 16)
- | ((int) startTimeSource[2] << 8) | startTimeSource[3];
- currentChapter = new ID3Chapter(elementId.toString(), startTime);
- skipBytes(input, 12);
- return ID3Reader.ACTION_DONT_SKIP;
- } else if (header.getId().equals(FRAME_ID_TITLE)) {
- if (currentChapter != null && currentChapter.getTitle() == null) {
- StringBuffer title = new StringBuffer();
- readString(title, input, header.getSize());
- currentChapter
- .setTitle(title.toString());
- if (BuildConfig.DEBUG) Log.d(TAG, "Found title: " + currentChapter.getTitle());
-
+ StringBuffer elementId = new StringBuffer();
+ readISOString(elementId, input, Integer.MAX_VALUE);
+ char[] startTimeSource = readBytes(input, 4);
+ long startTime = ((int) startTimeSource[0] << 24)
+ | ((int) startTimeSource[1] << 16)
+ | ((int) startTimeSource[2] << 8) | startTimeSource[3];
+ currentChapter = new ID3Chapter(elementId.toString(), startTime);
+ skipBytes(input, 12);
return ID3Reader.ACTION_DONT_SKIP;
- }
- } else if (header.getId().equals(FRAME_ID_LINK)) {
- if (currentChapter != null) {
- // skip description
- int descriptionLength = readString(null, input, header.getSize());
- StringBuffer link = new StringBuffer();
- readISOString(link, input, header.getSize() - descriptionLength);
- String decodedLink = URLDecoder.decode(link.toString(), "UTF-8");
-
- currentChapter.setLink(decodedLink);
-
- if (BuildConfig.DEBUG) Log.d(TAG, "Found link: " + currentChapter.getLink());
- return ID3Reader.ACTION_DONT_SKIP;
- }
- } else if (header.getId().equals("APIC")) {
- Log.d(TAG, header.toString());
- }
+ case FRAME_ID_TITLE:
+ if (currentChapter != null && currentChapter.getTitle() == null) {
+ StringBuffer title = new StringBuffer();
+ readString(title, input, header.getSize());
+ currentChapter
+ .setTitle(title.toString());
+ if (BuildConfig.DEBUG) Log.d(TAG, "Found title: " + currentChapter.getTitle());
+
+ return ID3Reader.ACTION_DONT_SKIP;
+ }
+ break;
+ case FRAME_ID_LINK:
+ if (currentChapter != null) {
+ // skip description
+ int descriptionLength = readString(null, input, header.getSize());
+ StringBuffer link = new StringBuffer();
+ readISOString(link, input, header.getSize() - descriptionLength);
+ String decodedLink = URLDecoder.decode(link.toString(), "UTF-8");
+
+ currentChapter.setLink(decodedLink);
+
+ if (BuildConfig.DEBUG) Log.d(TAG, "Found link: " + currentChapter.getLink());
+ return ID3Reader.ACTION_DONT_SKIP;
+ }
+ break;
+ case "APIC":
+ Log.d(TAG, header.toString());
+ break;
+ }
return super.onStartFrameHeader(header, input);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/AudioPlayer.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/AudioPlayer.java
index f0850e6df..846733882 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/AudioPlayer.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/AudioPlayer.java
@@ -1,8 +1,11 @@
package de.danoeh.antennapod.core.util.playback;
import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
import android.util.Log;
import android.view.SurfaceHolder;
+
import org.antennapod.audio.MediaPlayer;
import de.danoeh.antennapod.core.preferences.UserPreferences;
@@ -12,8 +15,17 @@ public class AudioPlayer extends MediaPlayer implements IPlayer {
public AudioPlayer(Context context) {
super(context);
+ PreferenceManager.getDefaultSharedPreferences(context)
+ .registerOnSharedPreferenceChangeListener(sonicListener);
}
+ private final SharedPreferences.OnSharedPreferenceChangeListener sonicListener =
+ (sharedPreferences, key) -> {
+ if (key.equals(UserPreferences.PREF_SONIC)) {
+ checkMpi();
+ }
+ };
+
@Override
public void setScreenOnWhilePlaying(boolean screenOn) {
Log.e(TAG, "Setting screen on while playing not supported in Audio Player");
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/ExternalMedia.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/ExternalMedia.java
index ec50dce7c..412a27b95 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/ExternalMedia.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/ExternalMedia.java
@@ -6,13 +6,14 @@ import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
-import de.danoeh.antennapod.core.feed.Chapter;
-import de.danoeh.antennapod.core.feed.MediaType;
-import de.danoeh.antennapod.core.util.ChapterUtils;
import java.util.List;
import java.util.concurrent.Callable;
+import de.danoeh.antennapod.core.feed.Chapter;
+import de.danoeh.antennapod.core.feed.MediaType;
+import de.danoeh.antennapod.core.util.ChapterUtils;
+
/** Represents a media file that is stored on the local storage device. */
public class ExternalMedia implements Playable {
@@ -106,12 +107,7 @@ public class ExternalMedia implements Playable {
@Override
public Callable<String> loadShownotes() {
- return new Callable<String>() {
- @Override
- public String call() throws Exception {
- return "";
- }
- };
+ return () -> "";
}
@Override
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/Playable.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/Playable.java
index 86ec4fbd0..6459d86ed 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/Playable.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/Playable.java
@@ -26,7 +26,7 @@ public interface Playable extends Parcelable,
* Implementations must NOT call commit() after they have written the values
* to the preferences file.
*/
- public void writeToPreferences(SharedPreferences.Editor prefEditor);
+ void writeToPreferences(SharedPreferences.Editor prefEditor);
/**
* This method is called from a separate thread by the PlaybackService.
@@ -34,88 +34,88 @@ public interface Playable extends Parcelable,
* should execute as quickly as possible and NOT load chapter marks if no
* local file is available.
*/
- public void loadMetadata() throws PlayableException;
+ void loadMetadata() throws PlayableException;
/**
* This method is called from a separate thread by the PlaybackService.
* Playable objects should load their chapter marks in this method if no
* local file was available when loadMetadata() was called.
*/
- public void loadChapterMarks();
+ void loadChapterMarks();
/**
* Returns the title of the episode that this playable represents
*/
- public String getEpisodeTitle();
+ String getEpisodeTitle();
/**
* Returns a list of chapter marks or null if this Playable has no chapters.
*/
- public List<Chapter> getChapters();
+ List<Chapter> getChapters();
/**
* Returns a link to a website that is meant to be shown in a browser
*/
- public String getWebsiteLink();
+ String getWebsiteLink();
- public String getPaymentLink();
+ String getPaymentLink();
/**
* Returns the title of the feed this Playable belongs to.
*/
- public String getFeedTitle();
+ String getFeedTitle();
/**
* Returns a unique identifier, for example a file url or an ID from a
* database.
*/
- public Object getIdentifier();
+ Object getIdentifier();
/**
* Return duration of object or 0 if duration is unknown.
*/
- public int getDuration();
+ int getDuration();
/**
* Return position of object or 0 if position is unknown.
*/
- public int getPosition();
+ int getPosition();
/**
* Returns last time (in ms) when this playable was played or 0
* if last played time is unknown.
*/
- public long getLastPlayedTime();
+ long getLastPlayedTime();
/**
* Returns the type of media. This method should return the correct value
* BEFORE loadMetadata() is called.
*/
- public MediaType getMediaType();
+ MediaType getMediaType();
/**
* Returns an url to a local file that can be played or null if this file
* does not exist.
*/
- public String getLocalMediaUrl();
+ String getLocalMediaUrl();
/**
* Returns an url to a file that can be streamed by the player or null if
* this url is not known.
*/
- public String getStreamUrl();
+ String getStreamUrl();
/**
* Returns true if a local file that can be played is available. getFileUrl
* MUST return a non-null string if this method returns true.
*/
- public boolean localFileAvailable();
+ boolean localFileAvailable();
/**
* Returns true if a streamable file is available. getStreamUrl MUST return
* a non-null string if this method returns true.
*/
- public boolean streamAvailable();
+ boolean streamAvailable();
/**
* Saves the current position of this object. Implementations can use the
@@ -126,40 +126,40 @@ public interface Playable extends Parcelable,
* @param newPosition new playback position in ms
* @param timestamp current time in ms
*/
- public void saveCurrentPosition(SharedPreferences pref, int newPosition, long timestamp);
+ void saveCurrentPosition(SharedPreferences pref, int newPosition, long timestamp);
- public void setPosition(int newPosition);
+ void setPosition(int newPosition);
- public void setDuration(int newDuration);
+ void setDuration(int newDuration);
/**
* @param lastPlayedTimestamp timestamp in ms
*/
- public void setLastPlayedTime(long lastPlayedTimestamp);
+ void setLastPlayedTime(long lastPlayedTimestamp);
/**
* Is called by the PlaybackService when playback starts.
*/
- public void onPlaybackStart();
+ void onPlaybackStart();
/**
* Is called by the PlaybackService when playback is completed.
*/
- public void onPlaybackCompleted();
+ void onPlaybackCompleted();
/**
* Returns an integer that must be unique among all Playable classes. The
* return value is later used by PlayableUtils to determine the type of the
* Playable object that is restored.
*/
- public int getPlayableType();
+ int getPlayableType();
- public void setChapters(List<Chapter> chapters);
+ void setChapters(List<Chapter> chapters);
/**
* Provides utility methods for Playable objects.
*/
- public static class PlayableUtils {
+ class PlayableUtils {
private static final String TAG = "PlayableUtils";
/**
@@ -213,7 +213,7 @@ public interface Playable extends Parcelable,
}
}
- public static class PlayableException extends Exception {
+ class PlayableException extends Exception {
private static final long serialVersionUID = 1L;
public PlayableException() {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java
index 27935978c..1409ffe09 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java
@@ -10,7 +10,6 @@ import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.media.MediaPlayer;
-import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;
import android.preference.PreferenceManager;
@@ -19,7 +18,6 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.view.SurfaceHolder;
-import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
@@ -42,6 +40,10 @@ import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.playback.Playable.PlayableUtils;
+import rx.Observable;
+import rx.Subscription;
+import rx.android.schedulers.AndroidSchedulers;
+import rx.schedulers.Schedulers;
/**
* Communicates with the playback service. GUI classes should use this class to
@@ -51,28 +53,30 @@ public abstract class PlaybackController {
private static final String TAG = "PlaybackController";
- public static final int INVALID_TIME = -1;
+ private static final int INVALID_TIME = -1;
private final Activity activity;
private PlaybackService playbackService;
- protected Playable media;
+ private Playable media;
private PlayerStatus status;
- private ScheduledThreadPoolExecutor schedExecutor;
+ private final ScheduledThreadPoolExecutor schedExecutor;
private static final int SCHED_EX_POOLSIZE = 1;
- protected MediaPositionObserver positionObserver;
- protected ScheduledFuture positionObserverFuture;
+ private MediaPositionObserver positionObserver;
+ private ScheduledFuture positionObserverFuture;
private boolean mediaInfoLoaded = false;
private boolean released = false;
+ private Subscription serviceBinder;
+
/**
* True if controller should reinit playback service if 'pause' button is
* pressed.
*/
- private boolean reinitOnPause;
+ private final boolean reinitOnPause;
public PlaybackController(@NonNull Activity activity, boolean reinitOnPause) {
@@ -87,8 +91,7 @@ public abstract class PlaybackController {
@Override
public void rejectedExecution(Runnable r,
ThreadPoolExecutor executor) {
- Log.w(TAG,
- "Rejected execution of runnable in schedExecutor");
+ Log.w(TAG, "Rejected execution of runnable in schedExecutor");
}
}
);
@@ -111,8 +114,7 @@ public abstract class PlaybackController {
if (!released) {
bindToService();
} else {
- throw new IllegalStateException(
- "Can't call init() after release() has been called");
+ throw new IllegalStateException("Can't call init() after release() has been called");
}
checkMediaInfoLoaded();
}
@@ -136,6 +138,9 @@ public abstract class PlaybackController {
// ignore
}
+ if(serviceBinder != null) {
+ serviceBinder.unsubscribe();
+ }
try {
activity.unbindService(mConnection);
} catch (IllegalArgumentException e) {
@@ -168,34 +173,33 @@ public abstract class PlaybackController {
*/
private void bindToService() {
Log.d(TAG, "Trying to connect to service");
- AsyncTask<Void, Void, Intent> intentLoader = new AsyncTask<Void, Void, Intent>() {
- @Override
- protected Intent doInBackground(Void... voids) {
- return getPlayLastPlayedMediaIntent();
- }
-
- @Override
- protected void onPostExecute(Intent serviceIntent) {
- boolean bound = false;
- if (!PlaybackService.started) {
- if (serviceIntent != null) {
- Log.d(TAG, "Calling start service");
- activity.startService(serviceIntent);
- bound = activity.bindService(serviceIntent, mConnection, 0);
+ if(serviceBinder != null) {
+ serviceBinder.unsubscribe();
+ }
+ serviceBinder = Observable.fromCallable(() -> getPlayLastPlayedMediaIntent())
+ .subscribeOn(Schedulers.newThread())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(intent -> {
+ boolean bound = false;
+ if (!PlaybackService.started) {
+ if (intent != null) {
+ Log.d(TAG, "Calling start service");
+ activity.startService(intent);
+ bound = activity.bindService(intent, mConnection, 0);
+ } else {
+ status = PlayerStatus.STOPPED;
+ setupGUI();
+ handleStatus();
+ }
} else {
- status = PlayerStatus.STOPPED;
- setupGUI();
- handleStatus();
+ Log.d(TAG, "PlaybackService is running, trying to connect without start command.");
+ bound = activity.bindService(new Intent(activity, PlaybackService.class),
+ mConnection, 0);
}
- } else {
- Log.d(TAG, "PlaybackService is running, trying to connect without start command.");
- bound = activity.bindService(new Intent(activity,
- PlaybackService.class), mConnection, 0);
- }
- Log.d(TAG, "Result for service binding: " + bound);
- }
- };
- intentLoader.execute();
+ Log.d(TAG, "Result for service binding: " + bound);
+ }, error -> {
+ Log.e(TAG, Log.getStackTraceString(error));
+ });
}
/**
@@ -204,27 +208,21 @@ public abstract class PlaybackController {
*/
private Intent getPlayLastPlayedMediaIntent() {
Log.d(TAG, "Trying to restore last played media");
- SharedPreferences prefs = PreferenceManager
- .getDefaultSharedPreferences(activity.getApplicationContext());
- long currentlyPlayingMedia = PlaybackPreferences
- .getCurrentlyPlayingMedia();
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
+ activity.getApplicationContext());
+ long currentlyPlayingMedia = PlaybackPreferences.getCurrentlyPlayingMedia();
if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) {
Playable media = PlayableUtils.createInstanceFromPreferences(activity,
(int) currentlyPlayingMedia, prefs);
if (media != null) {
- Intent serviceIntent = new Intent(activity,
- PlaybackService.class);
+ Intent serviceIntent = new Intent(activity, PlaybackService.class);
serviceIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);
- serviceIntent.putExtra(
- PlaybackService.EXTRA_START_WHEN_PREPARED, false);
- serviceIntent.putExtra(
- PlaybackService.EXTRA_PREPARE_IMMEDIATELY, false);
+ serviceIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED, false);
+ serviceIntent.putExtra(PlaybackService.EXTRA_PREPARE_IMMEDIATELY, false);
boolean fileExists = media.localFileAvailable();
- boolean lastIsStream = PlaybackPreferences
- .getCurrentEpisodeIsStream();
+ boolean lastIsStream = PlaybackPreferences.getCurrentEpisodeIsStream();
if (!fileExists && !lastIsStream && media instanceof FeedMedia) {
- DBTasks.notifyMissingFeedMediaFile(
- activity, (FeedMedia) media);
+ DBTasks.notifyMissingFeedMediaFile(activity, (FeedMedia) media);
}
serviceIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM,
lastIsStream || !fileExists);
@@ -238,10 +236,9 @@ public abstract class PlaybackController {
private void setupPositionObserver() {
- if ((positionObserverFuture != null && positionObserverFuture
- .isCancelled())
- || (positionObserverFuture != null && positionObserverFuture
- .isDone()) || positionObserverFuture == null) {
+ if (positionObserverFuture == null ||
+ positionObserverFuture.isCancelled() ||
+ positionObserverFuture.isDone()) {
Log.d(TAG, "Setting up position observer");
positionObserver = new MediaPositionObserver();
@@ -259,7 +256,7 @@ public abstract class PlaybackController {
}
}
- private ServiceConnection mConnection = new ServiceConnection() {
+ private final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
playbackService = ((PlaybackService.LocalBinder) service)
.getService();
@@ -267,7 +264,8 @@ public abstract class PlaybackController {
queryService();
Log.d(TAG, "Connection to Service established");
} else {
- Log.i(TAG, "Connection to playback service has been established, but controller has already been released");
+ Log.i(TAG, "Connection to playback service has been established, " +
+ "but controller has already been released");
}
}
@@ -278,7 +276,7 @@ public abstract class PlaybackController {
}
};
- protected BroadcastReceiver statusUpdate = new BroadcastReceiver() {
+ private final BroadcastReceiver statusUpdate = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received statusUpdate Intent.");
@@ -288,66 +286,64 @@ public abstract class PlaybackController {
media = info.playable;
handleStatus();
} else {
- Log.w(TAG,
- "Couldn't receive status update: playbackService was null");
+ Log.w(TAG, "Couldn't receive status update: playbackService was null");
bindToService();
}
}
};
- protected BroadcastReceiver notificationReceiver = new BroadcastReceiver() {
+ private final BroadcastReceiver notificationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
- if (isConnectedToPlaybackService()) {
- int type = intent.getIntExtra(
- PlaybackService.EXTRA_NOTIFICATION_TYPE, -1);
- int code = intent.getIntExtra(
- PlaybackService.EXTRA_NOTIFICATION_CODE, -1);
- if (code != -1 && type != -1) {
- switch (type) {
- case PlaybackService.NOTIFICATION_TYPE_ERROR:
- handleError(code);
- break;
- case PlaybackService.NOTIFICATION_TYPE_BUFFER_UPDATE:
- float progress = ((float) code) / 100;
- onBufferUpdate(progress);
- break;
- case PlaybackService.NOTIFICATION_TYPE_RELOAD:
- cancelPositionObserver();
- mediaInfoLoaded = false;
- queryService();
- onReloadNotification(intent.getIntExtra(
- PlaybackService.EXTRA_NOTIFICATION_CODE, -1));
- break;
- case PlaybackService.NOTIFICATION_TYPE_SLEEPTIMER_UPDATE:
- onSleepTimerUpdate();
- break;
- case PlaybackService.NOTIFICATION_TYPE_BUFFER_START:
- onBufferStart();
- break;
- case PlaybackService.NOTIFICATION_TYPE_BUFFER_END:
- onBufferEnd();
- break;
- case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_END:
- onPlaybackEnd();
- break;
- case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE:
- onPlaybackSpeedChange();
- break;
- }
-
- } else {
- Log.d(TAG, "Bad arguments. Won't handle intent");
- }
- } else {
+ if (!isConnectedToPlaybackService()) {
bindToService();
+ return;
+ }
+ int type = intent.getIntExtra(PlaybackService.EXTRA_NOTIFICATION_TYPE, -1);
+ int code = intent.getIntExtra(PlaybackService.EXTRA_NOTIFICATION_CODE, -1);
+ if(code == -1 || type == -1) {
+ Log.d(TAG, "Bad arguments. Won't handle intent");
+ return;
+ }
+ switch (type) {
+ case PlaybackService.NOTIFICATION_TYPE_ERROR:
+ handleError(code);
+ break;
+ case PlaybackService.NOTIFICATION_TYPE_BUFFER_UPDATE:
+ float progress = ((float) code) / 100;
+ onBufferUpdate(progress);
+ break;
+ case PlaybackService.NOTIFICATION_TYPE_RELOAD:
+ cancelPositionObserver();
+ mediaInfoLoaded = false;
+ queryService();
+ onReloadNotification(intent.getIntExtra(
+ PlaybackService.EXTRA_NOTIFICATION_CODE, -1));
+ break;
+ case PlaybackService.NOTIFICATION_TYPE_SLEEPTIMER_UPDATE:
+ onSleepTimerUpdate();
+ break;
+ case PlaybackService.NOTIFICATION_TYPE_BUFFER_START:
+ onBufferStart();
+ break;
+ case PlaybackService.NOTIFICATION_TYPE_BUFFER_END:
+ onBufferEnd();
+ break;
+ case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_END:
+ onPlaybackEnd();
+ break;
+ case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE:
+ onPlaybackSpeedChange();
+ break;
+ case PlaybackService.NOTIFICATION_TYPE_SET_SPEED_ABILITY_CHANGED:
+ onSetSpeedAbilityChanged();
}
}
};
- private BroadcastReceiver shutdownReceiver = new BroadcastReceiver() {
+ private final BroadcastReceiver shutdownReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@@ -361,31 +357,33 @@ public abstract class PlaybackController {
}
};
- public void setupGUI() {};
+ public void setupGUI() {}
- public void onPositionObserverUpdate() {};
+ public void onPositionObserverUpdate() {}
- public void onPlaybackSpeedChange() {};
+ public void onPlaybackSpeedChange() {}
- public void onShutdownNotification() {};
+ public void onSetSpeedAbilityChanged() {}
+
+ public void onShutdownNotification() {}
/**
* Called when the currently displayed information should be refreshed.
*/
- public void onReloadNotification(int code) {};
+ public void onReloadNotification(int code) {}
- public void onBufferStart() {};
+ public void onBufferStart() {}
- public void onBufferEnd() {};
+ public void onBufferEnd() {}
- public void onBufferUpdate(float progress) {};
+ public void onBufferUpdate(float progress) {}
- public void onSleepTimerUpdate() {};
+ public void onSleepTimerUpdate() {}
- public void handleError(int code) {};
+ public void handleError(int code) {}
- public void onPlaybackEnd() {};
+ public void onPlaybackEnd() {}
public void repeatHandleStatus() {
if (status != null && playbackService != null) {
@@ -424,6 +422,7 @@ public abstract class PlaybackController {
clearStatusMsg();
checkMediaInfoLoaded();
cancelPositionObserver();
+ onPositionObserverUpdate();
updatePlayButtonAppearance(playResource, playText);
if (PlaybackService.getCurrentMediaType() == MediaType.VIDEO) {
setScreenOn(false);
@@ -484,23 +483,23 @@ public abstract class PlaybackController {
public ImageButton getPlayButton() {
return null;
- };
+ }
- public void postStatusMsg(int msg) {};
+ public void postStatusMsg(int msg) {}
- public void clearStatusMsg() {};
+ public void clearStatusMsg() {}
public boolean loadMediaInfo() {
return false;
- };
+ }
- public void onAwaitingVideoSurface() {};
+ public void onAwaitingVideoSurface() {}
/**
* Called when connection to playback service has been established or
* information has to be refreshed
*/
- void queryService() {
+ private void queryService() {
Log.d(TAG, "Querying service info");
if (playbackService != null) {
status = playbackService.getStatus();
@@ -528,7 +527,7 @@ public abstract class PlaybackController {
}
}
- public void onServiceQueried() {};
+ public void onServiceQueried() {}
/**
* Should be used by classes which implement the OnSeekBarChanged interface.
@@ -574,34 +573,32 @@ public abstract class PlaybackController {
}
- public OnClickListener newOnPlayButtonClickListener() {
- return v -> {
- if (playbackService == null) {
- Log.w(TAG, "Play/Pause button was pressed, but playbackservice was null!");
- return;
- }
- switch (status) {
- case PLAYING:
- playbackService.pause(true, reinitOnPause);
- break;
- case PAUSED:
- case PREPARED:
- playbackService.resume();
- break;
- case PREPARING:
- playbackService.setStartWhenPrepared(!playbackService
- .isStartWhenPrepared());
- if (reinitOnPause
- && playbackService.isStartWhenPrepared() == false) {
- playbackService.reinit();
- }
- break;
- case INITIALIZED:
- playbackService.setStartWhenPrepared(true);
- playbackService.prepare();
- break;
- }
- };
+ public void playPause() {
+ if (playbackService == null) {
+ Log.w(TAG, "Play/Pause button was pressed, but playbackservice was null!");
+ return;
+ }
+ switch (status) {
+ case PLAYING:
+ playbackService.pause(true, reinitOnPause);
+ break;
+ case PAUSED:
+ case PREPARED:
+ playbackService.resume();
+ break;
+ case PREPARING:
+ playbackService.setStartWhenPrepared(!playbackService
+ .isStartWhenPrepared());
+ if (reinitOnPause
+ && !playbackService.isStartWhenPrepared()) {
+ playbackService.reinit();
+ }
+ break;
+ case INITIALIZED:
+ playbackService.setStartWhenPrepared(true);
+ playbackService.prepare();
+ break;
+ }
}
public boolean serviceAvailable() {
@@ -679,12 +676,10 @@ public abstract class PlaybackController {
}
public boolean canSetPlaybackSpeed() {
- if (org.antennapod.audio.MediaPlayer.isPrestoLibraryInstalled(activity.getApplicationContext())
+ return org.antennapod.audio.MediaPlayer.isPrestoLibraryInstalled(activity.getApplicationContext())
|| UserPreferences.useSonic()
- || Build.VERSION.SDK_INT >= 23) {
- return true;
- }
- return playbackService != null && playbackService.canSetSpeed();
+ || Build.VERSION.SDK_INT >= 23
+ || playbackService != null && playbackService.canSetSpeed();
}
public void setPlaybackSpeed(float speed) {
@@ -718,10 +713,7 @@ public abstract class PlaybackController {
}
public boolean isPlayingVideo() {
- if (playbackService != null) {
- return PlaybackService.getCurrentMediaType() == MediaType.VIDEO;
- }
- return false;
+ return playbackService != null && PlaybackService.getCurrentMediaType() == MediaType.VIDEO;
}
public Pair<Integer, Integer> getVideoSize() {
@@ -737,7 +729,7 @@ public abstract class PlaybackController {
* Returns true if PlaybackController can communicate with the playback
* service.
*/
- public boolean isConnectedToPlaybackService() {
+ private boolean isConnectedToPlaybackService() {
return playbackService != null;
}
@@ -753,9 +745,9 @@ public abstract class PlaybackController {
public void reinitServiceIfPaused() {
if (playbackService != null
&& playbackService.isStreaming()
- && (playbackService.getStatus() == PlayerStatus.PAUSED || (playbackService
- .getStatus() == PlayerStatus.PREPARING && playbackService
- .isStartWhenPrepared() == false))) {
+ && (playbackService.getStatus() == PlayerStatus.PAUSED ||
+ (playbackService.getStatus() == PlayerStatus.PREPARING &&
+ !playbackService.isStartWhenPrepared()))) {
playbackService.reinit();
}
}
@@ -770,13 +762,7 @@ public abstract class PlaybackController {
@Override
public void run() {
if (playbackService != null && playbackService.getStatus() == PlayerStatus.PLAYING) {
- activity.runOnUiThread(new Runnable() {
-
- @Override
- public void run() {
- onPositionObserverUpdate();
- }
- });
+ activity.runOnUiThread(PlaybackController.this::onPositionObserverUpdate);
}
}
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java
index 2eee1ac87..efdf46a97 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java
@@ -2,7 +2,10 @@ package de.danoeh.antennapod.core.util.playback;
import android.content.Context;
import android.content.res.TypedArray;
+import android.graphics.Color;
+import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
+import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
@@ -14,6 +17,7 @@ import org.jsoup.select.Elements;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.ShownotesProvider;
@@ -32,33 +36,39 @@ public class Timeline {
private ShownotesProvider shownotesProvider;
-
- private final String colorString;
+ private final String noShownotesLabel;
+ private final String colorPrimaryString;
+ private final String colorSecondaryString;
private final int pageMargin;
public Timeline(Context context, ShownotesProvider shownotesProvider) {
if (shownotesProvider == null) throw new IllegalArgumentException("shownotesProvider = null");
this.shownotesProvider = shownotesProvider;
- TypedArray res = context
- .getTheme()
- .obtainStyledAttributes(
- new int[]{android.R.attr.textColorPrimary});
- int colorResource = res.getColor(0, 0);
- colorString = String.format("#%06X",
- 0xFFFFFF & colorResource);
+ noShownotesLabel = context.getString(R.string.no_shownotes_label);
+
+ TypedArray res = context.getTheme().obtainStyledAttributes(
+ new int[]{ android.R.attr.textColorPrimary});
+ @ColorInt int col = res.getColor(0, 0);
+ colorPrimaryString = "rgba(" + Color.red(col) + "," + Color.green(col) + "," +
+ Color.blue(col) + "," + (Color.alpha(col)/256.0) + ")";
+ res.recycle();
+ res = context.getTheme().obtainStyledAttributes(
+ new int[]{android.R.attr.textColorSecondary});
+ col = res.getColor(0, 0);
+ colorSecondaryString = "rgba(" + Color.red(col) + "," + Color.green(col) + "," +
+ Color.blue(col) + "," + (Color.alpha(col)/256.0) + ")";
res.recycle();
- pageMargin = (int) TypedValue.applyDimension(
- TypedValue.COMPLEX_UNIT_DIP, 8, context.getResources()
- .getDisplayMetrics()
+ pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,
+ context.getResources().getDisplayMetrics()
);
}
private static final Pattern TIMECODE_LINK_REGEX = Pattern.compile("antennapod://timecode/((\\d+))");
private static final String TIMECODE_LINK = "<a class=\"timecode\" href=\"antennapod://timecode/%d\">%s</a>";
private static final Pattern TIMECODE_REGEX = Pattern.compile("\\b(?:(?:(([0-9][0-9])):))?(([0-9][0-9])):(([0-9][0-9]))\\b");
- private static final Pattern LINE_BREAK_REGEX = Pattern.compile("<br *\\/?>");
+ private static final Pattern LINE_BREAK_REGEX = Pattern.compile("<br */?>");
/**
@@ -82,9 +92,24 @@ public class Timeline {
e.printStackTrace();
return null;
}
- if (shownotes == null) {
- Log.d(TAG, "shownotesProvider contained no shownotes. Returning empty string");
- return "";
+
+ if(TextUtils.isEmpty(shownotes)) {
+ Log.d(TAG, "shownotesProvider contained no shownotes. Returning 'no shownotes' message");
+ shownotes ="<html>" +
+ "<head>" +
+ "<style type='text/css'>" +
+ "html, body { margin: 0; padding: 0; width: 100%; height: 100%; } " +
+ "html { display: table; }" +
+ "body { display: table-cell; vertical-align: middle; text-align:center;" +
+ "-webkit-text-size-adjust: none; font-size: 87%; color: " + colorSecondaryString + ";} " +
+ "</style>" +
+ "</head>" +
+ "<body>" +
+ "<p>" + noShownotesLabel + "</p>" +
+ "</body>" +
+ "</html>";
+ Log.d(TAG, "shownotes: " + shownotes);
+ return shownotes;
}
// replace ASCII line breaks with HTML ones if shownotes don't contain HTML line breaks already
@@ -95,7 +120,7 @@ public class Timeline {
Document document = Jsoup.parse(shownotes);
// apply style
- String styleStr = String.format(WEBVIEW_STYLE, colorString, "100%", pageMargin,
+ String styleStr = String.format(WEBVIEW_STYLE, colorPrimaryString, "100%", pageMargin,
pageMargin, pageMargin, pageMargin);
document.head().appendElement("style").attr("type", "text/css").text(styleStr);
@@ -125,8 +150,7 @@ public class Timeline {
element.html(buffer.toString());
}
}
-
- Log.i(TAG, "Out: " + document.toString());
+
return document.toString();
}
@@ -148,7 +172,7 @@ public class Timeline {
try {
if (m.find()) {
- return Integer.valueOf(m.group(1));
+ return Integer.parseInt(m.group(1));
}
} catch (NumberFormatException e) {
e.printStackTrace();
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentChapterReader.java b/core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentChapterReader.java
index c4961a3ab..6243da5bc 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentChapterReader.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentChapterReader.java
@@ -9,7 +9,7 @@ import java.util.ArrayList;
import java.util.List;
public class VorbisCommentChapterReader extends VorbisCommentReader {
- private static final String TAG = "VorbisCommentChapterReader";
+ private static final String TAG = "VorbisCommentChptrReadr";
private static final String CHAPTER_KEY = "chapter\\d\\d\\d.*";
private static final String CHAPTER_ATTRIBUTE_TITLE = "name";
@@ -27,7 +27,7 @@ public class VorbisCommentChapterReader extends VorbisCommentReader {
@Override
public void onVorbisCommentHeaderFound(VorbisCommentHeader header) {
- chapters = new ArrayList<Chapter>();
+ chapters = new ArrayList<>();
System.out.println(header.toString());
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentReader.java b/core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentReader.java
index 9639b9c42..49ea18721 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentReader.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/vorbiscommentreader/VorbisCommentReader.java
@@ -180,13 +180,13 @@ public abstract class VorbisCommentReader {
private String readContentVectorKey(InputStream input, long vectorLength)
throws IOException {
- StringBuffer buffer = new StringBuffer();
+ StringBuilder builder = new StringBuilder();
for (int i = 0; i < vectorLength; i++) {
char c = (char) input.read();
if (c == '=') {
- return buffer.toString();
+ return builder.toString();
} else {
- buffer.append(c);
+ builder.append(c);
}
}
return null; // no key found