From 55418a92f2876ff40ac2a9eb8abf442bb1dd0aaa Mon Sep 17 00:00:00 2001 From: Daniel Oeh Date: Wed, 11 Apr 2012 16:39:16 +0200 Subject: [PATCH] Fixed Download Process and filename generation --- src/de/podfetcher/service/DownloadService.java | 4 ++-- src/de/podfetcher/service/FeedSyncService.java | 2 +- src/de/podfetcher/storage/DownloadRequester.java | 16 +++++++++------- src/de/podfetcher/util/NumberGenerator.java | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 src/de/podfetcher/util/NumberGenerator.java diff --git a/src/de/podfetcher/service/DownloadService.java b/src/de/podfetcher/service/DownloadService.java index e2faf5433..625b4ab29 100644 --- a/src/de/podfetcher/service/DownloadService.java +++ b/src/de/podfetcher/service/DownloadService.java @@ -105,7 +105,7 @@ public class DownloadService extends Service { private void handleCompletedFeedDownload(Context context, Feed feed) { Log.d(this.toString(), "Handling completed Feed Download"); // Get Feed Information - feed.setFile_url((new File(requester.getFeedfilePath(context), requester.getFeedfileName(feed.getId()))).toString()); + //feed.setFile_url((new File(requester.getFeedfilePath(context), requester.getFeedfileName(feed.getId()))).toString()); syncExecutor.execute(new FeedSyncThread(feed, this, requester)); @@ -115,7 +115,7 @@ public class DownloadService extends Service { private void handleCompletedImageDownload(Context context, FeedImage image) { Log.d(this.toString(), "Handling completed Image Download"); requester.removeFeedImage(image); - image.setFile_url(requester.getImagefilePath(context) + requester.getImagefileName(image.getId())); + //image.setFile_url(requester.getImagefilePath(context) + requester.getImagefileName(image.getId())); manager.setFeedImage(this, image); } diff --git a/src/de/podfetcher/service/FeedSyncService.java b/src/de/podfetcher/service/FeedSyncService.java index 7cb54d855..76ca6e0d8 100644 --- a/src/de/podfetcher/service/FeedSyncService.java +++ b/src/de/podfetcher/service/FeedSyncService.java @@ -59,7 +59,7 @@ public class FeedSyncService extends Service { /** Extracts a Feed object from the given Intent */ private Feed handleIntent(Intent intent) { Feed feed = manager.getFeed(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1)); - feed.setFile_url(requester.getFeedfilePath(this) + requester.getFeedfileName(feed.getId())); + //feed.setFile_url(requester.getFeedfilePath(this) + requester.getFeedfileName(feed.getId())); return feed; } diff --git a/src/de/podfetcher/storage/DownloadRequester.java b/src/de/podfetcher/storage/DownloadRequester.java index bfc2f07f5..f25a394ab 100644 --- a/src/de/podfetcher/storage/DownloadRequester.java +++ b/src/de/podfetcher/storage/DownloadRequester.java @@ -5,6 +5,7 @@ import java.io.File; import de.podfetcher.feed.*; import de.podfetcher.service.DownloadService; +import de.podfetcher.util.NumberGenerator; import android.util.Log; import android.app.DownloadManager; @@ -57,17 +58,18 @@ public class DownloadRequester { // TODO Set Allowed Network Types DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); context.startService(new Intent(context, DownloadService.class)); - item.setDownloadId(manager.enqueue(request)); + item.setDownloadId(manager.enqueue(request)); + item.setFile_url(dest.toString()); } public void downloadFeed(Context context, Feed feed) { download(context, feeds, feed, - new File(getFeedfilePath(context), getFeedfileName(feeds.size())), + new File(getFeedfilePath(context), getFeedfileName(feed)), true); } public void downloadImage(Context context, FeedImage image) { download(context, images, image, - new File(getImagefilePath(context), getImagefileName(images.size())), + new File(getImagefilePath(context), getImagefileName(image)), true); } @@ -134,15 +136,15 @@ public class DownloadRequester { return context.getExternalFilesDir(FEED_DOWNLOADPATH).toString() + "/"; } - public String getFeedfileName(long id) { - return "feed-" + id; + public String getFeedfileName(Feed feed) { + return "feed-" + NumberGenerator.generateLong(feed.getDownload_url()); } public String getImagefilePath(Context context) { return context.getExternalFilesDir(IMAGE_DOWNLOADPATH).toString() + "/"; } - public String getImagefileName(long id) { - return "image-" + id; + public String getImagefileName(FeedImage image) { + return "image-" + NumberGenerator.generateLong(image.getDownload_url()); } } diff --git a/src/de/podfetcher/util/NumberGenerator.java b/src/de/podfetcher/util/NumberGenerator.java new file mode 100644 index 000000000..81220820a --- /dev/null +++ b/src/de/podfetcher/util/NumberGenerator.java @@ -0,0 +1,15 @@ +package de.podfetcher.util; + +import java.util.Random; +import android.util.Log; + +/** Utility class for creating large random numbers */ +public class NumberGenerator { + private static final String TAG = "NumberGenerator"; + + public static long generateLong(String strSeed) { + long seed = (long) strSeed.hashCode(); + Log.d(TAG, "Taking " + seed + " as seed."); + return new Random(seed).nextLong(); + } +}