Add generatedBySystem to DownloadRequest

This commit is contained in:
Nathan Mascitelli 2020-02-07 10:55:08 -05:00
parent 3d7f93771a
commit b5244bbe99
9 changed files with 45 additions and 34 deletions

View File

@ -137,9 +137,9 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
feedUrl = feedUrl.replaceFirst("((www.)?(subscribeonandroid.com/))", ""); feedUrl = feedUrl.replaceFirst("((www.)?(subscribeonandroid.com/))", "");
} }
if (savedInstanceState == null) { if (savedInstanceState == null) {
startFeedDownload(feedUrl, null, null); startFeedDownload(feedUrl, null, null, false);
} else { } else {
startFeedDownload(feedUrl, savedInstanceState.getString("username"), savedInstanceState.getString("password")); startFeedDownload(feedUrl, savedInstanceState.getString("username"), savedInstanceState.getString("password"), false);
} }
} }
} }
@ -223,7 +223,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
private void startFeedDownload(String url, String username, String password) { private void startFeedDownload(String url, String username, String password, boolean generatedBysystem) {
Log.d(TAG, "Starting feed download"); Log.d(TAG, "Starting feed download");
url = URLChecker.prepareURL(url); url = URLChecker.prepareURL(url);
feed = new Feed(url, null); feed = new Feed(url, null);
@ -235,7 +235,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
feed.setFile_url(fileUrl); feed.setFile_url(fileUrl);
final DownloadRequest request = new DownloadRequest(feed.getFile_url(), final DownloadRequest request = new DownloadRequest(feed.getFile_url(),
feed.getDownload_url(), "OnlineFeed", 0, Feed.FEEDFILETYPE_FEED, username, password, feed.getDownload_url(), "OnlineFeed", 0, Feed.FEEDFILETYPE_FEED, username, password,
true, null); true, null, generatedBysystem);
download = Observable.fromCallable(() -> { download = Observable.fromCallable(() -> {
feeds = DBReader.getFeedList(); feeds = DBReader.getFeedList();
@ -589,9 +589,9 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
resetIntent(selectedUrl, titles.get(which)); resetIntent(selectedUrl, titles.get(which));
FeedPreferences prefs = feed.getPreferences(); FeedPreferences prefs = feed.getPreferences();
if(prefs != null) { if(prefs != null) {
startFeedDownload(selectedUrl, prefs.getUsername(), prefs.getPassword()); startFeedDownload(selectedUrl, prefs.getUsername(), prefs.getPassword(), false);
} else { } else {
startFeedDownload(selectedUrl, null, null); startFeedDownload(selectedUrl, null, null, false);
} }
}; };
@ -627,7 +627,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
@Override @Override
protected void onConfirmed(String username, String password, boolean saveUsernamePassword) { protected void onConfirmed(String username, String password, boolean saveUsernamePassword) {
startFeedDownload(feedUrl, username, password); startFeedDownload(feedUrl, username, password, false);
} }
} }

View File

@ -101,7 +101,7 @@ public class DownloadLogAdapter extends BaseAdapter {
return; return;
} }
try { try {
DBTasks.forceRefreshFeed(context, feed); DBTasks.forceRefreshFeed(context, feed, false);
} catch (DownloadRequestException e) { } catch (DownloadRequestException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -48,7 +48,7 @@ class MobileDownloadHelper {
private static void downloadFeedItems(Context context, FeedItem item) { private static void downloadFeedItems(Context context, FeedItem item) {
allowMobileDownloadTimestamp = System.currentTimeMillis(); allowMobileDownloadTimestamp = System.currentTimeMillis();
try { try {
DownloadRequester.getInstance().downloadMedia(context, item); DownloadRequester.getInstance().downloadMedia(context, false, item);
Toast.makeText(context, R.string.status_downloading_label, Toast.LENGTH_SHORT).show(); Toast.makeText(context, R.string.status_downloading_label, Toast.LENGTH_SHORT).show();
} catch (DownloadRequestException e) { } catch (DownloadRequestException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -449,7 +449,7 @@ public class EpisodesApplyActionFragment extends Fragment {
} }
} }
try { try {
DownloadRequester.getInstance().downloadMedia(getActivity(), toDownload.toArray(new FeedItem[0])); DownloadRequester.getInstance().downloadMedia(getActivity(), false, toDownload.toArray(new FeedItem[0]));
} catch (DownloadRequestException e) { } catch (DownloadRequestException e) {
e.printStackTrace(); e.printStackTrace();
DownloadRequestErrorDialogCreator.newRequestErrorDialog(getActivity(), e.getMessage()); DownloadRequestErrorDialogCreator.newRequestErrorDialog(getActivity(), e.getMessage());

View File

@ -64,7 +64,7 @@ public class FeedMenuHandler {
final Feed selectedFeed) throws DownloadRequestException { final Feed selectedFeed) throws DownloadRequestException {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.refresh_item: case R.id.refresh_item:
DBTasks.forceRefreshFeed(context, selectedFeed); DBTasks.forceRefreshFeed(context, selectedFeed, false);
break; break;
case R.id.refresh_complete_item: case R.id.refresh_complete_item:
DBTasks.forceRefreshCompleteFeed(context, selectedFeed); DBTasks.forceRefreshCompleteFeed(context, selectedFeed);

View File

@ -29,6 +29,7 @@ public class DownloadRequest implements Parcelable {
private long size; private long size;
private int statusMsg; private int statusMsg;
private boolean mediaEnqueued; private boolean mediaEnqueued;
private boolean generatedBySystem;
public DownloadRequest(@NonNull String destination, public DownloadRequest(@NonNull String destination,
@NonNull String source, @NonNull String source,
@ -38,7 +39,8 @@ public class DownloadRequest implements Parcelable {
String username, String username,
String password, String password,
boolean deleteOnFailure, boolean deleteOnFailure,
Bundle arguments) { Bundle arguments,
boolean generatedBySystem) {
this.destination = destination; this.destination = destination;
this.source = source; this.source = source;
@ -50,11 +52,12 @@ public class DownloadRequest implements Parcelable {
this.deleteOnFailure = deleteOnFailure; this.deleteOnFailure = deleteOnFailure;
this.mediaEnqueued = false; this.mediaEnqueued = false;
this.arguments = (arguments != null) ? arguments : new Bundle(); this.arguments = (arguments != null) ? arguments : new Bundle();
this.generatedBySystem = generatedBySystem;
} }
public DownloadRequest(String destination, String source, String title, public DownloadRequest(String destination, String source, String title,
long feedfileId, int feedfileType) { long feedfileId, int feedfileType, boolean generatedBySystem) {
this(destination, source, title, feedfileId, feedfileType, null, null, true, null); this(destination, source, title, feedfileId, feedfileType, null, null, true, null, generatedBySystem);
} }
private DownloadRequest(Builder builder) { private DownloadRequest(Builder builder) {
@ -68,6 +71,7 @@ public class DownloadRequest implements Parcelable {
this.lastModified = builder.lastModified; this.lastModified = builder.lastModified;
this.deleteOnFailure = builder.deleteOnFailure; this.deleteOnFailure = builder.deleteOnFailure;
this.arguments = (builder.arguments != null) ? builder.arguments : new Bundle(); this.arguments = (builder.arguments != null) ? builder.arguments : new Bundle();
this.generatedBySystem = builder.generatedBySystem;
} }
private DownloadRequest(Parcel in) { private DownloadRequest(Parcel in) {
@ -82,6 +86,7 @@ public class DownloadRequest implements Parcelable {
password = nullIfEmpty(in.readString()); password = nullIfEmpty(in.readString());
mediaEnqueued = (in.readByte() > 0); mediaEnqueued = (in.readByte() > 0);
arguments = in.readBundle(); arguments = in.readBundle();
generatedBySystem = (in.readByte() > 0);
} }
@Override @Override
@ -107,6 +112,7 @@ public class DownloadRequest implements Parcelable {
dest.writeString(nonNullString(password)); dest.writeString(nonNullString(password));
dest.writeByte((mediaEnqueued) ? (byte) 1 : 0); dest.writeByte((mediaEnqueued) ? (byte) 1 : 0);
dest.writeBundle(arguments); dest.writeBundle(arguments);
dest.writeByte(generatedBySystem ? (byte)1 : 0);
} }
private static String nonNullString(String str) { private static String nonNullString(String str) {
@ -153,6 +159,7 @@ public class DownloadRequest implements Parcelable {
if (username != null ? !username.equals(that.username) : that.username != null) if (username != null ? !username.equals(that.username) : that.username != null)
return false; return false;
if (mediaEnqueued != that.mediaEnqueued) return false; if (mediaEnqueued != that.mediaEnqueued) return false;
if (generatedBySystem != that.generatedBySystem) return false;
return true; return true;
} }
@ -281,13 +288,15 @@ public class DownloadRequest implements Parcelable {
private final long feedfileId; private final long feedfileId;
private final int feedfileType; private final int feedfileType;
private Bundle arguments; private Bundle arguments;
private boolean generatedBySystem;
public Builder(@NonNull String destination, @NonNull FeedFile item) { public Builder(@NonNull String destination, @NonNull FeedFile item, boolean generatedBySystem) {
this.destination = destination; this.destination = destination;
this.source = URLChecker.prepareURL(item.getDownload_url()); this.source = URLChecker.prepareURL(item.getDownload_url());
this.title = item.getHumanReadableIdentifier(); this.title = item.getHumanReadableIdentifier();
this.feedfileId = item.getId(); this.feedfileId = item.getId();
this.feedfileType = item.getTypeAsInt(); this.feedfileType = item.getTypeAsInt();
this.generatedBySystem = generatedBySystem;
} }
public Builder deleteOnFailure(boolean deleteOnFailure) { public Builder deleteOnFailure(boolean deleteOnFailure) {

View File

@ -92,7 +92,7 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm {
Log.d(TAG, "Enqueueing " + itemsToDownload.length + " items for download"); Log.d(TAG, "Enqueueing " + itemsToDownload.length + " items for download");
try { try {
DownloadRequester.getInstance().downloadMedia(false, context, itemsToDownload); DownloadRequester.getInstance().downloadMedia(false, context, true, itemsToDownload);
} catch (DownloadRequestException e) { } catch (DownloadRequestException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -168,7 +168,7 @@ public final class DBTasks {
*/ */
public static void forceRefreshCompleteFeed(final Context context, final Feed feed) { public static void forceRefreshCompleteFeed(final Context context, final Feed feed) {
try { try {
refreshFeed(context, feed, true, true); refreshFeed(context, feed, true, true, true);
} catch (DownloadRequestException e) { } catch (DownloadRequestException e) {
e.printStackTrace(); e.printStackTrace();
DBWriter.addDownloadStatus( DBWriter.addDownloadStatus(
@ -196,7 +196,8 @@ public final class DBTasks {
nextFeed.setPageNr(pageNr); nextFeed.setPageNr(pageNr);
nextFeed.setPaged(true); nextFeed.setPaged(true);
nextFeed.setId(feed.getId()); nextFeed.setId(feed.getId());
DownloadRequester.getInstance().downloadFeed(context, nextFeed, loadAllPages, false); DownloadRequester.getInstance().downloadFeed(context, nextFeed, loadAllPages, false, false
);
} else { } else {
Log.e(TAG, "loadNextPageOfFeed: Feed was either not paged or contained no nextPageLink"); Log.e(TAG, "loadNextPageOfFeed: Feed was either not paged or contained no nextPageLink");
} }
@ -212,7 +213,7 @@ public final class DBTasks {
private static void refreshFeed(Context context, Feed feed) private static void refreshFeed(Context context, Feed feed)
throws DownloadRequestException { throws DownloadRequestException {
Log.d(TAG, "refreshFeed(feed.id: " + feed.getId() +")"); Log.d(TAG, "refreshFeed(feed.id: " + feed.getId() +")");
refreshFeed(context, feed, false, false); refreshFeed(context, feed, false, false, true);
} }
/** /**
@ -221,13 +222,13 @@ public final class DBTasks {
* @param context Used for requesting the download. * @param context Used for requesting the download.
* @param feed The Feed object. * @param feed The Feed object.
*/ */
public static void forceRefreshFeed(Context context, Feed feed) public static void forceRefreshFeed(Context context, Feed feed, boolean generatedBySystem)
throws DownloadRequestException { throws DownloadRequestException {
Log.d(TAG, "refreshFeed(feed.id: " + feed.getId() +")"); Log.d(TAG, "refreshFeed(feed.id: " + feed.getId() +")");
refreshFeed(context, feed, false, true); refreshFeed(context, feed, false, true, generatedBySystem);
} }
private static void refreshFeed(Context context, Feed feed, boolean loadAllPages, boolean force) private static void refreshFeed(Context context, Feed feed, boolean loadAllPages, boolean force, boolean generatedBySystem)
throws DownloadRequestException { throws DownloadRequestException {
Feed f; Feed f;
String lastUpdate = feed.hasLastUpdateFailed() ? null : feed.getLastUpdate(); String lastUpdate = feed.hasLastUpdateFailed() ? null : feed.getLastUpdate();
@ -238,7 +239,7 @@ public final class DBTasks {
feed.getPreferences().getUsername(), feed.getPreferences().getPassword()); feed.getPreferences().getUsername(), feed.getPreferences().getPassword());
} }
f.setId(feed.getId()); f.setId(feed.getId());
DownloadRequester.getInstance().downloadFeed(context, f, loadAllPages, force); DownloadRequester.getInstance().downloadFeed(context, f, loadAllPages, force, generatedBySystem);
} }
/** /**

View File

@ -115,7 +115,7 @@ public class DownloadRequester implements DownloadStateProvider {
@Nullable @Nullable
private DownloadRequest createRequest(FeedFile item, FeedFile container, File dest, private DownloadRequest createRequest(FeedFile item, FeedFile container, File dest,
boolean overwriteIfExists, String username, String password, boolean overwriteIfExists, String username, String password,
String lastModified, boolean deleteOnFailure, Bundle arguments) { String lastModified, boolean deleteOnFailure, Bundle arguments, boolean generatedBySystem) {
final boolean partiallyDownloadedFileExists = item.getFile_url() != null && new File(item.getFile_url()).exists(); final boolean partiallyDownloadedFileExists = item.getFile_url() != null && new File(item.getFile_url()).exists();
Log.d(TAG, "partiallyDownloadedFileExists: " + partiallyDownloadedFileExists); Log.d(TAG, "partiallyDownloadedFileExists: " + partiallyDownloadedFileExists);
@ -156,7 +156,7 @@ public class DownloadRequester implements DownloadStateProvider {
String baseUrl = (container != null) ? container.getDownload_url() : null; String baseUrl = (container != null) ? container.getDownload_url() : null;
item.setDownload_url(URLChecker.prepareURL(item.getDownload_url(), baseUrl)); item.setDownload_url(URLChecker.prepareURL(item.getDownload_url(), baseUrl));
DownloadRequest.Builder builder = new DownloadRequest.Builder(dest.toString(), item) DownloadRequest.Builder builder = new DownloadRequest.Builder(dest.toString(), item, generatedBySystem)
.withAuthentication(username, password) .withAuthentication(username, password)
.lastModified(lastModified) .lastModified(lastModified)
.deleteOnFailure(deleteOnFailure) .deleteOnFailure(deleteOnFailure)
@ -191,7 +191,7 @@ public class DownloadRequester implements DownloadStateProvider {
* @param loadAllPages Set to true to download all pages * @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) boolean force, boolean generatedBySystem)
throws DownloadRequestException { throws DownloadRequestException {
if (feedFileValid(feed)) { if (feedFileValid(feed)) {
String username = (feed.getPreferences() != null) ? feed.getPreferences().getUsername() : null; String username = (feed.getPreferences() != null) ? feed.getPreferences().getUsername() : null;
@ -203,7 +203,8 @@ public class DownloadRequester implements DownloadStateProvider {
args.putBoolean(REQUEST_ARG_LOAD_ALL_PAGES, loadAllPages); args.putBoolean(REQUEST_ARG_LOAD_ALL_PAGES, loadAllPages);
DownloadRequest request = createRequest(feed, null, new File(getFeedfilePath(), getFeedfileName(feed)), DownloadRequest request = createRequest(feed, null, new File(getFeedfilePath(), getFeedfileName(feed)),
true, username, password, lastModified, true, args); true, username, password, lastModified, true, args, generatedBySystem
);
if (request != null) { if (request != null) {
download(context, request); download(context, request);
} }
@ -211,17 +212,17 @@ public class DownloadRequester implements DownloadStateProvider {
} }
public synchronized void downloadFeed(Context context, Feed feed) throws DownloadRequestException { public synchronized void downloadFeed(Context context, Feed feed) throws DownloadRequestException {
downloadFeed(context, feed, false, false); downloadFeed(context, feed, false, false, false);
} }
public synchronized void downloadMedia(@NonNull Context context, FeedItem... feedItems) public synchronized void downloadMedia(@NonNull Context context, boolean generatedBySystem, FeedItem... feedItems)
throws DownloadRequestException { throws DownloadRequestException {
downloadMedia(true, context, feedItems); downloadMedia(true, context, generatedBySystem, feedItems);
} }
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
public synchronized void downloadMedia(boolean performAutoCleanup, @NonNull Context context, public synchronized void downloadMedia(boolean performAutoCleanup, @NonNull Context context, boolean generatedBySystem,
FeedItem... items) FeedItem... items)
throws DownloadRequestException { throws DownloadRequestException {
Log.d(TAG, "downloadMedia() called with: performAutoCleanup = [" + performAutoCleanup Log.d(TAG, "downloadMedia() called with: performAutoCleanup = [" + performAutoCleanup
@ -230,7 +231,7 @@ public class DownloadRequester implements DownloadStateProvider {
List<DownloadRequest> requests = new ArrayList<>(items.length); List<DownloadRequest> requests = new ArrayList<>(items.length);
for (FeedItem item : items) { for (FeedItem item : items) {
try { try {
DownloadRequest request = createRequest(item.getMedia()); DownloadRequest request = createRequest(item.getMedia(), generatedBySystem);
if (request != null) { if (request != null) {
requests.add(request); requests.add(request);
} }
@ -256,7 +257,7 @@ public class DownloadRequester implements DownloadStateProvider {
} }
@Nullable @Nullable
private DownloadRequest createRequest(@Nullable FeedMedia feedmedia) private DownloadRequest createRequest(@Nullable FeedMedia feedmedia, boolean generatedBySystem)
throws DownloadRequestException { throws DownloadRequestException {
if (!feedFileValid(feedmedia)) { if (!feedFileValid(feedmedia)) {
return null; return null;
@ -279,7 +280,7 @@ public class DownloadRequester implements DownloadStateProvider {
dest = new File(getMediafilePath(feedmedia), getMediafilename(feedmedia)); dest = new File(getMediafilePath(feedmedia), getMediafilename(feedmedia));
} }
return createRequest(feedmedia, feed, return createRequest(feedmedia, feed,
dest, false, username, password, null, false, null); dest, false, username, password, null, false, null, generatedBySystem);
} }
/** /**