Merge pull request #4989 from ByteHamster/decouple-shownotes-from-model

Removed ShownotesProvider interface
This commit is contained in:
ByteHamster 2021-03-05 09:54:17 +01:00 committed by GitHub
commit c58aa40b21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 82 additions and 151 deletions

View File

@ -10,6 +10,9 @@ import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.danoeh.antennapod.core.util.playback.Timeline;
import de.danoeh.antennapod.view.ShownotesWebView;
@ -82,7 +85,15 @@ public class ItemDescriptionFragment extends Fragment {
webViewLoader.dispose();
}
webViewLoader = Maybe.<String>create(emitter -> {
Timeline timeline = new Timeline(getActivity(), controller.getMedia());
Playable media = controller.getMedia();
if (media instanceof FeedMedia) {
FeedMedia feedMedia = ((FeedMedia) media);
if (feedMedia.getItem() == null) {
feedMedia.setItem(DBReader.getFeedItem(feedMedia.getItemId()));
}
DBReader.loadDescriptionOfFeedItem(feedMedia.getItem());
}
Timeline timeline = new Timeline(getActivity(), media.getDescription(), media.getDuration());
emitter.onSuccess(timeline.processShownotes());
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())

View File

@ -439,7 +439,9 @@ public class ItemFragment extends Fragment {
FeedItem feedItem = DBReader.getFeedItem(itemId);
Context context = getContext();
if (feedItem != null && context != null) {
Timeline t = new Timeline(context, feedItem);
int duration = feedItem.getMedia() != null ? feedItem.getMedia().getDuration() : Integer.MAX_VALUE;
DBReader.loadDescriptionOfFeedItem(feedItem);
Timeline t = new Timeline(context, feedItem.getDescription(), duration);
webviewData = t.processShownotes();
}
return feedItem;

View File

@ -13,19 +13,16 @@ import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.ShownotesProvider;
/**
* Item (episode) within a feed.
*
* @author daniel
*/
public class FeedItem extends FeedComponent implements ShownotesProvider, Serializable {
public class FeedItem extends FeedComponent implements Serializable {
/** tag that indicates this item is in the queue */
public static final String TAG_QUEUE = "Queue";
@ -352,16 +349,6 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Serial
return media != null && media.isPlaying();
}
@Override
public Callable<String> loadShownotes() {
return () -> {
if (description == null) {
DBReader.loadDescriptionOfFeedItem(FeedItem.this);
}
return description;
};
}
public String getImageLocation() {
if (imageUrl != null) {
return imageUrl;

View File

@ -14,7 +14,6 @@ import android.support.v4.media.MediaDescriptionCompat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
@ -286,6 +285,14 @@ public class FeedMedia extends FeedFile implements Playable {
this.size = size;
}
@Override
public String getDescription() {
if (item != null) {
return item.getDescription();
}
return null;
}
/**
* Indicates we asked the service what the size was, but didn't
* get a valid answer and we shoudln't check using the network again.
@ -529,16 +536,6 @@ public class FeedMedia extends FeedFile implements Playable {
}
}
@Override
public Callable<String> loadShownotes() {
return () -> {
if (item == null) {
item = DBReader.getFeedItem(itemID);
}
return item.loadShownotes().call();
};
}
public static final Parcelable.Creator<FeedMedia> CREATOR = new Parcelable.Creator<FeedMedia>() {
public FeedMedia createFromParcel(Parcel in) {
final long id = in.readLong();

View File

@ -1,16 +0,0 @@
package de.danoeh.antennapod.core.util;
import java.util.concurrent.Callable;
/**
* Created by daniel on 04.08.13.
*/
public interface ShownotesProvider {
/**
* Loads shownotes. If the shownotes have to be loaded from a file or from a
* database, it should be done in a separate thread. After the shownotes
* have been loaded, callback.onShownotesLoaded should be called.
*/
Callable<String> loadShownotes();
}

View File

@ -7,14 +7,13 @@ import android.os.Parcelable;
import androidx.annotation.Nullable;
import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.util.ShownotesProvider;
import java.util.Date;
import java.util.List;
/**
* Interface for objects that can be played by the PlaybackService.
*/
public interface Playable extends Parcelable, ShownotesProvider {
public interface Playable extends Parcelable {
public static final int INVALID_TIME = -1;
/**
@ -82,6 +81,13 @@ public interface Playable extends Parcelable, ShownotesProvider {
*/
long getLastPlayedTime();
/**
* Returns the description of the item, if available.
* For FeedItems, the description needs to be loaded from the database first.
*/
@Nullable
String getDescription();
/**
* Returns the type of media. This method should return the correct value
* BEFORE loadMetadata() is called.

View File

@ -13,7 +13,6 @@ import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
@ -260,8 +259,8 @@ public class RemoteMedia implements Playable {
}
@Override
public Callable<String> loadShownotes() {
return () -> (notes != null) ? notes : "";
public String getDescription() {
return notes;
}
@Override

View File

@ -9,7 +9,7 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import de.danoeh.antennapod.core.feed.FeedItem;
import androidx.annotation.Nullable;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
@ -24,7 +24,6 @@ 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;
/**
* Connects chapter information and shownotes of a shownotesProvider, for example by making it possible to use the
@ -42,17 +41,16 @@ public class Timeline {
private static final Pattern TIMECODE_REGEX = Pattern.compile("\\b((\\d+):)?(\\d+):(\\d{2})\\b");
private static final Pattern LINE_BREAK_REGEX = Pattern.compile("<br */?>");
private final ShownotesProvider shownotesProvider;
private final String rawShownotes;
private final String noShownotesLabel;
private final int playableDuration;
private final String webviewStyle;
public Timeline(Context context, ShownotesProvider shownotesProvider) {
if (shownotesProvider == null) {
throw new IllegalArgumentException("shownotesProvider = null");
}
this.shownotesProvider = shownotesProvider;
public Timeline(Context context, @Nullable String rawShownotes, int playableDuration) {
this.rawShownotes = rawShownotes;
noShownotesLabel = context.getString(R.string.no_shownotes_label);
this.playableDuration = playableDuration;
final String colorPrimary = colorToHtml(context, android.R.attr.textColorPrimary);
final String colorAccent = colorToHtml(context, R.attr.colorAccent);
final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,
@ -87,13 +85,7 @@ public class Timeline {
*/
@NonNull
public String processShownotes() {
String shownotes;
try {
shownotes = shownotesProvider.loadShownotes().call();
} catch (Exception e) {
Log.e(TAG, "processShownotes() - encounters exceptions unexpectedly in load, treat as if no shownotes.", e);
shownotes = "";
}
String shownotes = rawShownotes;
if (TextUtils.isEmpty(shownotes)) {
Log.d(TAG, "shownotesProvider contained no shownotes. Returning 'no shownotes' message");
@ -147,14 +139,6 @@ public class Timeline {
// No elements with timecodes
return;
}
int playableDuration = Integer.MAX_VALUE;
if (shownotesProvider instanceof Playable) {
playableDuration = ((Playable) shownotesProvider).getDuration();
} else if (shownotesProvider instanceof FeedItem && ((FeedItem) shownotesProvider).getMedia() != null) {
playableDuration = ((FeedItem) shownotesProvider).getMedia().getDuration();
}
boolean useHourFormat = true;
if (playableDuration != Integer.MAX_VALUE) {

View File

@ -130,18 +130,12 @@ public class CastUtils {
if (!TextUtils.isEmpty(feedItem.getLink())) {
metadata.putString(KEY_EPISODE_LINK, feedItem.getLink());
}
}
String notes = null;
try {
notes = media.loadShownotes().call();
} catch (Exception e) {
Log.e(TAG, "Unable to load FeedMedia notes", e);
}
if (notes != null) {
if (notes.length() > EPISODE_NOTES_MAX_LENGTH) {
notes = notes.substring(0, EPISODE_NOTES_MAX_LENGTH);
try {
DBReader.loadDescriptionOfFeedItem(feedItem);
metadata.putString(KEY_EPISODE_NOTES, feedItem.getDescription());
} catch (Exception e) {
Log.e(TAG, "Unable to load FeedMedia notes", e);
}
metadata.putString(KEY_EPISODE_NOTES, notes);
}
// This field only identifies the id on the device that has the original version.
// Idea is to perhaps, on a first approach, check if the version on the local DB with the

View File

@ -2,14 +2,10 @@ package de.danoeh.antennapod.core.feed;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import java.text.SimpleDateFormat;
import java.util.Date;
import de.danoeh.antennapod.core.storage.DBReader;
import static de.danoeh.antennapod.core.feed.FeedItemMother.anyFeedItemWithImage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -113,7 +109,7 @@ public class FeedItemTest {
* If one of `description` or `content:encoded` is null, use the other one.
*/
@Test
public void testShownotesNullValues() throws Exception {
public void testShownotesNullValues() {
testShownotes(null, TEXT_LONG);
testShownotes(TEXT_LONG, null);
}
@ -122,7 +118,7 @@ public class FeedItemTest {
* If `description` is reasonably longer than `content:encoded`, use `description`.
*/
@Test
public void testShownotesLength() throws Exception {
public void testShownotesLength() {
testShownotes(TEXT_SHORT, TEXT_LONG);
testShownotes(TEXT_LONG, TEXT_SHORT);
}
@ -133,12 +129,10 @@ public class FeedItemTest {
* @param description Description of the feed item
* @param contentEncoded `content:encoded` of the feed item
*/
private void testShownotes(String description, String contentEncoded) throws Exception {
try (MockedStatic<DBReader> ignore = Mockito.mockStatic(DBReader.class)) {
FeedItem item = new FeedItem();
item.setDescriptionIfLonger(description);
item.setDescriptionIfLonger(contentEncoded);
assertEquals(TEXT_LONG, item.loadShownotes().call());
}
private void testShownotes(String description, String contentEncoded) {
FeedItem item = new FeedItem();
item.setDescriptionIfLonger(description);
item.setDescriptionIfLonger(contentEncoded);
assertEquals(TEXT_LONG, item.getDescription());
}
}

View File

@ -7,13 +7,6 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.Date;
import java.util.List;
import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.storage.DBReader;
import org.junit.After;
@ -51,25 +44,13 @@ public class TimelineTest {
dbReaderMock.close();
}
@SuppressWarnings("SameParameterValue")
private Playable newTestPlayable(List<Chapter> chapters, String shownotes, int duration) {
FeedItem item = new FeedItem(0, "Item", "item-id", "http://example.com/item", new Date(), FeedItem.PLAYED, null);
item.setChapters(chapters);
item.setDescriptionIfLonger(shownotes);
FeedMedia media = new FeedMedia(item, "http://example.com/episode", 100, "audio/mp3");
media.setDuration(duration);
item.setMedia(media);
return media;
}
@Test
public void testProcessShownotesAddTimecodeHhmmssNoChapters() {
final String timeStr = "10:11:12";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11 + 12 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>";
Timeline t = new Timeline(context, shownotes, Integer.MAX_VALUE);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@ -79,9 +60,8 @@ public class TimelineTest {
final String timeStr = "25:00:00";
final long time = 25 * 60 * 60 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>";
Timeline t = new Timeline(context, shownotes, Integer.MAX_VALUE);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@ -91,9 +71,8 @@ public class TimelineTest {
final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>";
Timeline t = new Timeline(context, shownotes, Integer.MAX_VALUE);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@ -103,9 +82,8 @@ public class TimelineTest {
final String timeStr = "10:11";
final long time = 10 * 60 * 1000 + 11 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", 11 * 60 * 1000);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>";
Timeline t = new Timeline(context, shownotes, 11 * 60 * 1000);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@ -114,9 +92,9 @@ public class TimelineTest {
public void testProcessShownotesAddTimecodeHmmssNoChapters() {
final String timeStr = "2:11:12";
final long time = 2 * 60 * 60 * 1000 + 11 * 60 * 1000 + 12 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>";
Timeline t = new Timeline(context, shownotes, Integer.MAX_VALUE);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@ -126,9 +104,8 @@ public class TimelineTest {
final String timeStr = "1:12";
final long time = 60 * 1000 + 12 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", 2 * 60 * 1000);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>";
Timeline t = new Timeline(context, shownotes, 2 * 60 * 1000);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@ -137,9 +114,9 @@ public class TimelineTest {
public void testProcessShownotesAddNoTimecodeDuration() {
final String timeStr = "2:11:12";
final int time = 2 * 60 * 60 * 1000 + 11 * 60 * 1000 + 12 * 1000;
String originalText = "<p> Some test text with a timecode " + timeStr + " here.</p>";
Playable p = newTestPlayable(null, originalText, time);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>";
Timeline t = new Timeline(context, shownotes, time);
String res = t.processShownotes();
Document d = Jsoup.parse(res);
assertEquals("Should not parse time codes that equal duration", 0, d.body().getElementsByTag("a").size());
@ -149,9 +126,9 @@ public class TimelineTest {
public void testProcessShownotesAddTimecodeMultipleFormatsNoChapters() {
final String[] timeStrings = new String[]{ "10:12", "1:10:12" };
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 2 * 60 * 60 * 1000);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode " + timeStrings[0]
+ " here. Hey look another one " + timeStrings[1] + " here!</p>";
Timeline t = new Timeline(context, shownotes, 2 * 60 * 60 * 1000);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{10 * 60 * 1000 + 12 * 1000,
60 * 60 * 1000 + 10 * 60 * 1000 + 12 * 1000}, timeStrings);
@ -163,9 +140,9 @@ public class TimelineTest {
// One of these timecodes fits as HH:MM and one does not so both should be parsed as MM:SS.
final String[] timeStrings = new String[]{ "10:12", "2:12" };
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 3 * 60 * 60 * 1000);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode " + timeStrings[0]
+ " here. Hey look another one " + timeStrings[1] + " here!</p>";
Timeline t = new Timeline(context, shownotes, 3 * 60 * 60 * 1000);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{10 * 60 * 1000 + 12 * 1000, 2 * 60 * 1000 + 12 * 1000}, timeStrings);
}
@ -175,9 +152,8 @@ public class TimelineTest {
final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode ("
+ timeStr + ") here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode (" + timeStr + ") here.</p>";
Timeline t = new Timeline(context, shownotes, Integer.MAX_VALUE);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@ -187,9 +163,8 @@ public class TimelineTest {
final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode ["
+ timeStr + "] here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode [" + timeStr + "] here.</p>";
Timeline t = new Timeline(context, shownotes, Integer.MAX_VALUE);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@ -199,9 +174,8 @@ public class TimelineTest {
final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode <"
+ timeStr + "> here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String shownotes = "<p> Some test text with a timecode <" + timeStr + "> here.</p>";
Timeline t = new Timeline(context, shownotes, Integer.MAX_VALUE);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@ -216,8 +190,7 @@ public class TimelineTest {
}
shownotes.append("here.</p>");
Playable p = newTestPlayable(null, shownotes.toString(), Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
Timeline t = new Timeline(context, shownotes.toString(), Integer.MAX_VALUE);
String res = t.processShownotes();
checkLinkCorrect(res, new long[0], new String[0]);
}