Fixed image loading bugs

- If a feed contained items with non-unique image URLs, those images were not displayed
- Images in a list were not loaded correctly if the list contained entries without an image
This commit is contained in:
daniel oeh 2014-03-28 20:17:15 +01:00
parent 2c68f8197b
commit ae09dbffb6
3 changed files with 41 additions and 28 deletions

View File

@ -26,6 +26,12 @@ public class ImageLoader {
public static final int IMAGE_TYPE_THUMBNAIL = 0;
public static final int IMAGE_TYPE_COVER = 1;
/**
* Used by loadThumbnailBitmap and loadCoverBitmap to denote an ImageView that displays the default image resource.
* This is the case if the given source to load the image from was null or did not return any image data.
*/
private static final Object DEFAULT_IMAGE_RESOURCE_TAG = new Object();
private Handler handler;
private ExecutorService executor;
@ -122,6 +128,7 @@ public class ImageLoader {
}
} else {
target.setImageResource(defaultCoverResource);
target.setTag(R.id.imageloader_key, DEFAULT_IMAGE_RESOURCE_TAG);
}
}
@ -163,6 +170,7 @@ public class ImageLoader {
}
} else {
target.setImageResource(defaultCoverResource);
target.setTag(R.id.imageloader_key, DEFAULT_IMAGE_RESOURCE_TAG);
}
}

View File

@ -335,7 +335,9 @@ public class FeedItem extends FeedComponent implements
public void setImage(FeedImage image) {
this.image = image;
image.setOwner(this);
if (image != null) {
image.setOwner(this);
}
}
/**

View File

@ -648,6 +648,9 @@ public class DownloadService extends Service {
if (checkFeedData(feed) == false) {
throw new InvalidFeedException();
}
removeDuplicateImages(feed); // duplicate images have to removed because the DownloadRequester does not accept two downloads with the same download URL yet.
// Save information of feed in DB
savedFeed = DBTasks.updateFeed(DownloadService.this, feed);
// Download Feed Image if provided and not downloaded
@ -675,33 +678,32 @@ public class DownloadService extends Service {
);
}
}
if (!hasDuplicateImages(savedFeed)) {
// download FeedItem images if provided and not downloaded
for (FeedItem item : savedFeed.getItems()) {
if (item.hasItemImage() && (!item.getImage().isDownloaded())) {
if (AppConfig.DEBUG)
Log.d(TAG, "Item has image; Downloading....");
try {
requester.downloadImage(DownloadService.this,
item.getImage());
} catch (DownloadRequestException e) {
e.printStackTrace();
DBWriter.addDownloadStatus(
DownloadService.this,
new DownloadStatus(
item.getImage(),
item
.getImage()
.getHumanReadableIdentifier(),
DownloadError.ERROR_REQUEST_ERROR,
false, e.getMessage()
)
);
}
// download FeedItem images if provided and not downloaded
for (FeedItem item : savedFeed.getItems()) {
if (item.hasItemImage() && (!item.getImage().isDownloaded())) {
if (AppConfig.DEBUG)
Log.d(TAG, "Item has image; Downloading....");
try {
requester.downloadImage(DownloadService.this,
item.getImage());
} catch (DownloadRequestException e) {
e.printStackTrace();
DBWriter.addDownloadStatus(
DownloadService.this,
new DownloadStatus(
item.getImage(),
item
.getImage()
.getHumanReadableIdentifier(),
DownloadError.ERROR_REQUEST_ERROR,
false, e.getMessage()
)
);
}
}
}
} catch (SAXException e) {
successful = false;
e.printStackTrace();
@ -761,21 +763,22 @@ public class DownloadService extends Service {
/**
* Checks if the FeedItems of this feed have images that point
* to the same URL.
* to the same URL. If two FeedItems have an image that points to
* the same URL, the reference of the second item is removed, so that every image
* reference is unique.
*/
private boolean hasDuplicateImages(Feed feed) {
private void removeDuplicateImages(Feed feed) {
for (int x = 0; x < feed.getItems().size(); x++) {
for (int y = x + 1; y < feed.getItems().size(); y++) {
FeedItem item1 = feed.getItems().get(x);
FeedItem item2 = feed.getItems().get(y);
if (item1.hasItemImage() && item2.hasItemImage()) {
if (StringUtils.equals(item1.getImage().getDownload_url(), item2.getImage().getDownload_url())) {
return true;
item2.setImage(null);
}
}
}
}
return false;
}
private boolean hasValidFeedItems(Feed feed) {