prefetch thumbnails if enabled. clean up thumbnails for read stories.

This commit is contained in:
dosiecki 2016-08-19 05:40:12 -07:00
parent 844ce68f7d
commit e59ef8f18c
4 changed files with 64 additions and 11 deletions

View file

@ -288,6 +288,16 @@ public class BlurDatabaseHelper {
return urls;
}
public Set<String> getAllStoryThumbnails() {
Cursor c = dbRO.query(DatabaseConstants.STORY_TABLE, new String[]{DatabaseConstants.STORY_THUMBNAIL_URL}, null, null, null, null, null);
Set<String> urls = new HashSet<String>(c.getCount());
while (c.moveToNext()) {
urls.add(c.getString(c.getColumnIndexOrThrow(DatabaseConstants.STORY_THUMBNAIL_URL)));
}
c.close();
return urls;
}
public void insertStories(StoriesResponse apiResponse, boolean forImmediateReading) {
StateFilter intelState = PrefsUtils.getStateFilter(context);
synchronized (RW_MUTEX) {

View file

@ -39,7 +39,7 @@ public class CleanupService extends SubService {
if (AppConstants.VERBOSE_LOG) Log.d(this.getClass().getName(), "cleaning up thumbnail cache");
FileCache thumbCache = FileCache.asThumbnailCache(parent);
// TODO iconCache.cleanupUnusedOrOld();
thumbCache.cleanupUnusedOrOld(parent.dbHelper.getAllStoryThumbnails());
PrefsUtils.updateLastCleanupTime(parent);
}

View file

@ -15,25 +15,29 @@ public class ImagePrefetchService extends SubService {
private static volatile boolean Running = false;
FileCache storyImageCache;
FileCache thumbnailCache;
/** URLs of images contained in recently fetched stories that are candidates for prefetch. */
static Set<String> ImageQueue;
static { ImageQueue = Collections.synchronizedSet(new HashSet<String>()); }
static Set<String> StoryImageQueue;
static { StoryImageQueue = Collections.synchronizedSet(new HashSet<String>()); }
/** URLs of thumbnails for recently fetched stories that are candidates for prefetch. */
static Set<String> ThumbnailQueue;
static { ThumbnailQueue = Collections.synchronizedSet(new HashSet<String>()); }
public ImagePrefetchService(NBSyncService parent) {
super(parent);
storyImageCache = FileCache.asStoryImageCache(parent);
thumbnailCache = FileCache.asThumbnailCache(parent);
}
@Override
protected void exec() {
if (!PrefsUtils.isImagePrefetchEnabled(parent)) return;
if (ImageQueue.size() < 1) return;
if (!PrefsUtils.isBackgroundNetworkAllowed(parent)) return;
gotWork();
while (ImageQueue.size() > 0) {
while (StoryImageQueue.size() > 0) {
if (! PrefsUtils.isImagePrefetchEnabled(parent)) return;
if (! PrefsUtils.isBackgroundNetworkAllowed(parent)) return;
@ -43,7 +47,7 @@ public class ImagePrefetchService extends SubService {
Set<String> unreadImages = parent.dbHelper.getAllStoryImages();
Set<String> fetchedImages = new HashSet<String>();
Set<String> batch = new HashSet<String>(AppConstants.IMAGE_PREFETCH_BATCH_SIZE);
batchloop: for (String url : ImageQueue) {
batchloop: for (String url : StoryImageQueue) {
batch.add(url);
if (batch.size() >= AppConstants.IMAGE_PREFETCH_BATCH_SIZE) break batchloop;
}
@ -58,7 +62,38 @@ public class ImagePrefetchService extends SubService {
fetchedImages.add(url);
}
} finally {
ImageQueue.removeAll(fetchedImages);
StoryImageQueue.removeAll(fetchedImages);
gotWork();
}
}
while (ThumbnailQueue.size() > 0) {
if (! PrefsUtils.isImagePrefetchEnabled(parent)) return;
if (! PrefsUtils.isBackgroundNetworkAllowed(parent)) return;
if (! PrefsUtils.isShowThumbnails(parent)) return;
startExpensiveCycle();
// on each batch, re-query the DB for images associated with yet-unread stories
// this is a bit expensive, but we are running totally async at a really low priority
Set<String> unreadImages = parent.dbHelper.getAllStoryThumbnails();
Set<String> fetchedImages = new HashSet<String>();
Set<String> batch = new HashSet<String>(AppConstants.IMAGE_PREFETCH_BATCH_SIZE);
batchloop: for (String url : ThumbnailQueue) {
batch.add(url);
if (batch.size() >= AppConstants.IMAGE_PREFETCH_BATCH_SIZE) break batchloop;
}
try {
for (String url : batch) {
if (parent.stopSync()) return;
// dont fetch the image if the associated story was marked read before we got to it
if (unreadImages.contains(url)) {
if (AppConstants.VERBOSE_LOG) Log.d(this.getClass().getName(), "prefetching thumbnail: " + url);
thumbnailCache.cacheFile(url);
}
fetchedImages.add(url);
}
} finally {
ThumbnailQueue.removeAll(fetchedImages);
gotWork();
}
}
@ -66,15 +101,20 @@ public class ImagePrefetchService extends SubService {
}
public void addUrl(String url) {
ImageQueue.add(url);
StoryImageQueue.add(url);
}
public static int getPendingCount() {
return ImageQueue.size();
public void addThumbnailUrl(String url) {
ThumbnailQueue.add(url);
}
public static int getPendingCount() {
return (StoryImageQueue.size() + ThumbnailQueue.size());
}
public static void clear() {
ImageQueue.clear();
StoryImageQueue.clear();
ThumbnailQueue.clear();
}
public static boolean running() {

View file

@ -142,6 +142,9 @@ public class UnreadsService extends SubService {
parent.imagePrefetchService.addUrl(url);
}
}
if (story.thumbnailUrl != null) {
parent.imagePrefetchService.addThumbnailUrl(story.thumbnailUrl);
}
DefaultFeedView mode = PrefsUtils.getDefaultFeedViewForFeed(parent, story.feedId);
if (mode == DefaultFeedView.TEXT) {
parent.originalTextService.addHash(story.storyHash);