From ff3258c34a1797e75538b83baa1e867d0ea07ceb Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 27 Feb 2022 12:12:24 +0100 Subject: Move database to its own module --- .../antennapod/model/download/DownloadError.java | 49 +++++++ .../antennapod/model/download/DownloadStatus.java | 153 +++++++++++++++++++++ .../de/danoeh/antennapod/model/feed/Chapter.java | 32 +++-- .../danoeh/antennapod/model/feed/FeedCounter.java | 24 ++++ 4 files changed, 246 insertions(+), 12 deletions(-) create mode 100644 model/src/main/java/de/danoeh/antennapod/model/download/DownloadError.java create mode 100644 model/src/main/java/de/danoeh/antennapod/model/download/DownloadStatus.java create mode 100644 model/src/main/java/de/danoeh/antennapod/model/feed/FeedCounter.java (limited to 'model/src') diff --git a/model/src/main/java/de/danoeh/antennapod/model/download/DownloadError.java b/model/src/main/java/de/danoeh/antennapod/model/download/DownloadError.java new file mode 100644 index 000000000..a2f987a86 --- /dev/null +++ b/model/src/main/java/de/danoeh/antennapod/model/download/DownloadError.java @@ -0,0 +1,49 @@ +package de.danoeh.antennapod.model.download; + +/** Utility class for Download Errors. */ +public enum DownloadError { + SUCCESS(0), + ERROR_PARSER_EXCEPTION(1), + ERROR_UNSUPPORTED_TYPE(2), + ERROR_CONNECTION_ERROR(3), + ERROR_MALFORMED_URL(4), + ERROR_IO_ERROR(5), + ERROR_FILE_EXISTS(6), + ERROR_DOWNLOAD_CANCELLED(7), + ERROR_DEVICE_NOT_FOUND(8), + ERROR_HTTP_DATA_ERROR(9), + ERROR_NOT_ENOUGH_SPACE(10), + ERROR_UNKNOWN_HOST(11), + ERROR_REQUEST_ERROR(12), + ERROR_DB_ACCESS_ERROR(13), + ERROR_UNAUTHORIZED(14), + ERROR_FILE_TYPE(15), + ERROR_FORBIDDEN(16), + ERROR_IO_WRONG_SIZE(17), + ERROR_IO_BLOCKED(18), + ERROR_UNSUPPORTED_TYPE_HTML(19), + ERROR_NOT_FOUND(20), + ERROR_CERTIFICATE(21), + ERROR_PARSER_EXCEPTION_DUPLICATE(22); + + private final int code; + + DownloadError(int code) { + this.code = code; + } + + /** Return DownloadError from its associated code. */ + public static DownloadError fromCode(int code) { + for (DownloadError reason : values()) { + if (reason.getCode() == code) { + return reason; + } + } + throw new IllegalArgumentException("unknown code: " + code); + } + + /** Get machine-readable code. */ + public int getCode() { + return code; + } +} diff --git a/model/src/main/java/de/danoeh/antennapod/model/download/DownloadStatus.java b/model/src/main/java/de/danoeh/antennapod/model/download/DownloadStatus.java new file mode 100644 index 000000000..0a18973df --- /dev/null +++ b/model/src/main/java/de/danoeh/antennapod/model/download/DownloadStatus.java @@ -0,0 +1,153 @@ +package de.danoeh.antennapod.model.download; + +import androidx.annotation.NonNull; + +import java.util.Date; + +import de.danoeh.antennapod.model.feed.FeedFile; + +/** + * Contains status attributes for one download + */ +public class DownloadStatus { + /** + * Downloaders should use this constant for the size attribute if necessary + * so that the listadapters etc. can react properly. + */ + public static final int SIZE_UNKNOWN = -1; + + // ----------------------------------- ATTRIBUTES STORED IN DB + /** + * A human-readable string which is shown to the user so that he can + * identify the download. Should be the title of the item/feed/media or the + * URL if the download has no other title. + */ + private final String title; + private final long feedfileId; + /** + * Is used to determine the type of the feedfile even if the feedfile does + * not exist anymore. The value should be FEEDFILETYPE_FEED, + * FEEDFILETYPE_FEEDIMAGE or FEEDFILETYPE_FEEDMEDIA + */ + private final int feedfileType; + private final boolean initiatedByUser; + /** + * Unique id for storing the object in database. + */ + private long id; + private DownloadError reason; + /** + * A message which can be presented to the user to give more information. + * Should be null if Download was successful. + */ + private String reasonDetailed; + private boolean successful; + private final Date completionDate; + // ------------------------------------ NOT STORED IN DB + private boolean done; + private boolean cancelled; + + /** + * Constructor for creating new completed downloads. + */ + public DownloadStatus(@NonNull FeedFile feedfile, String title, DownloadError reason, boolean successful, + String reasonDetailed, boolean initiatedByUser) { + this(0, title, feedfile.getId(), feedfile.getTypeAsInt(), successful, false, true, reason, new Date(), + reasonDetailed, initiatedByUser); + } + + public DownloadStatus(long id, String title, long feedfileId, int feedfileType, boolean successful, + boolean cancelled, boolean done, DownloadError reason, Date completionDate, + String reasonDetailed, boolean initiatedByUser) { + this.id = id; + this.title = title; + this.feedfileId = feedfileId; + this.reason = reason; + this.successful = successful; + this.cancelled = cancelled; + this.done = done; + this.completionDate = (Date) completionDate.clone(); + this.reasonDetailed = reasonDetailed; + this.feedfileType = feedfileType; + this.initiatedByUser = initiatedByUser; + } + + @Override + @NonNull + public String toString() { + return "DownloadStatus [id=" + id + ", title=" + title + ", reason=" + + reason + ", reasonDetailed=" + reasonDetailed + + ", successful=" + successful + ", completionDate=" + + completionDate + ", feedfileId=" + feedfileId + + ", feedfileType=" + feedfileType + ", done=" + done + + ", cancelled=" + cancelled + "]"; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public DownloadError getReason() { + return reason; + } + + public String getReasonDetailed() { + return reasonDetailed; + } + + public boolean isSuccessful() { + return successful; + } + + public Date getCompletionDate() { + return (Date) completionDate.clone(); + } + + public long getFeedfileId() { + return feedfileId; + } + + public int getFeedfileType() { + return feedfileType; + } + + public boolean isInitiatedByUser() { + return initiatedByUser; + } + + public boolean isDone() { + return done; + } + + public boolean isCancelled() { + return cancelled; + } + + public void setSuccessful() { + this.successful = true; + this.reason = DownloadError.SUCCESS; + this.done = true; + } + + public void setFailed(DownloadError reason, String reasonDetailed) { + this.successful = false; + this.reason = reason; + this.reasonDetailed = reasonDetailed; + this.done = true; + } + + public void setCancelled() { + this.successful = false; + this.reason = DownloadError.ERROR_DOWNLOAD_CANCELLED; + this.done = true; + this.cancelled = true; + } +} \ No newline at end of file diff --git a/model/src/main/java/de/danoeh/antennapod/model/feed/Chapter.java b/model/src/main/java/de/danoeh/antennapod/model/feed/Chapter.java index 0508df901..e55364dd9 100644 --- a/model/src/main/java/de/danoeh/antennapod/model/feed/Chapter.java +++ b/model/src/main/java/de/danoeh/antennapod/model/feed/Chapter.java @@ -1,31 +1,23 @@ package de.danoeh.antennapod.model.feed; -public abstract class Chapter extends FeedComponent { - +public class Chapter extends FeedComponent { /** Defines starting point in milliseconds. */ private long start; private String title; private String link; private String imageUrl; + private String chapterId; - protected Chapter() { - } - - protected Chapter(long start) { - super(); - this.start = start; + public Chapter() { } - protected Chapter(long start, String title, String link, String imageUrl) { - super(); + public Chapter(long start, String title, String link, String imageUrl) { this.start = start; this.title = title; this.link = link; this.imageUrl = imageUrl; } - public abstract int getChapterType(); - public long getStart() { return start; } @@ -58,8 +50,24 @@ public abstract class Chapter extends FeedComponent { this.imageUrl = imageUrl; } + /** + * ID from the chapter source, not the database ID. + */ + public String getChapterId() { + return chapterId; + } + + public void setChapterId(String chapterId) { + this.chapterId = chapterId; + } + @Override public String getHumanReadableIdentifier() { return title; } + + @Override + public String toString() { + return "ID3Chapter [title=" + getTitle() + ", start=" + getStart() + ", url=" + getLink() + "]"; + } } diff --git a/model/src/main/java/de/danoeh/antennapod/model/feed/FeedCounter.java b/model/src/main/java/de/danoeh/antennapod/model/feed/FeedCounter.java new file mode 100644 index 000000000..eef1cc1ef --- /dev/null +++ b/model/src/main/java/de/danoeh/antennapod/model/feed/FeedCounter.java @@ -0,0 +1,24 @@ +package de.danoeh.antennapod.model.feed; + +public enum FeedCounter { + SHOW_NEW_UNPLAYED_SUM(0), + SHOW_NEW(1), + SHOW_UNPLAYED(2), + SHOW_NONE(3), + SHOW_DOWNLOADED(4); + + public final int id; + + FeedCounter(int id) { + this.id = id; + } + + public static FeedCounter fromOrdinal(int id) { + for (FeedCounter counter : values()) { + if (counter.id == id) { + return counter; + } + } + return SHOW_NONE; + } +} -- cgit v1.2.3