@Override
  @NonNull
  public CacheDownloadLoader.Result loadInBackground() {
    if (mUri == null) {
      return Result.nullInstance();
    }
    final String scheme = mUri.getScheme();
    File cacheFile = null;
    if ("http".equals(scheme) || "https".equals(scheme)) {
      final String uriString = mUri.toString();
      if (uriString == null) return Result.nullInstance();
      cacheFile = mDiskCache.get(uriString);
      if (isValid(cacheFile)) {
        return Result.getInstance(CacheProvider.getCacheUri(uriString));
      }
      try {
        // from SD cache
        ContentLengthInputStream cis;
        final InputStream is = mDownloader.get(uriString);
        if (is == null) return Result.nullInstance();
        try {
          final long length = is.available();
          mHandler.post(new DownloadStartRunnable(this, mListener, length));

          cis = new ContentLengthInputStream(is, length);
          mDiskCache.save(
              uriString,
              cis,
              new IoUtils.CopyListener() {
                @Override
                public boolean onBytesCopied(int current, int total) {
                  mHandler.post(new ProgressUpdateRunnable(mListener, current, total));
                  return !isAbandoned();
                }
              });
          mHandler.post(new DownloadFinishRunnable(this, mListener));
        } finally {
          Utils.closeSilently(is);
        }
        cacheFile = mDiskCache.get(uriString);
        if (isValid(cacheFile)) {
          return Result.getInstance(CacheProvider.getCacheUri(uriString));
        } else {
          mDiskCache.remove(uriString);
          throw new IOException();
        }
      } catch (final Exception e) {
        mHandler.post(new DownloadErrorRunnable(this, mListener, e));
        return Result.getInstance(e);
      }
    }
    return Result.getInstance(mUri);
  }
Пример #2
0
 public File getCachedVideoFile(final String url, boolean loadIfNotFound) {
   if (url == null) return null;
   final File cache = mDiskCache.get(url);
   if (cache.exists()) return cache;
   else if (loadIfNotFound) {
     loadVideo(url, null);
   }
   return null;
 }
Пример #3
0
 @Override
 protected SingleResponse<File> doInBackground(Object... params) {
   final DiskCache diskCache = mPreLoader.mDiskCache;
   final File cachedFile = diskCache.get(mUri);
   if (!mForceReload && cachedFile != null && cachedFile.isFile() && cachedFile.length() > 0)
     return SingleResponse.getInstance(cachedFile);
   InputStream is = null;
   try {
     is = mPreLoader.mImageDownloader.getStream(mUri, null);
     diskCache.save(mUri, is, this);
     return SingleResponse.getInstance(diskCache.get(mUri));
   } catch (IOException e) {
     diskCache.remove(mUri);
     Log.w(LOGTAG, e);
     return SingleResponse.getInstance(e);
   } finally {
     IoUtils.closeSilently(is);
   }
 }
Пример #4
0
 @Override
 public void save(
     @NonNull final String key,
     @NonNull final InputStream is,
     byte[] extra,
     final CopyListener listener)
     throws IOException {
   cache.save(
       key,
       is,
       new IoUtils.CopyListener() {
         @Override
         public boolean onBytesCopied(final int current, final int total) {
           return listener == null || listener.onCopied(current);
         }
       });
   if (extra != null) {
     cache.save(CacheProvider.getExtraKey(key), new ByteArrayInputStream(extra), null);
   }
 }
Пример #5
0
 public static File findInCache(String paramString, DiskCache paramDiskCache) {
   File localFile = paramDiskCache.get(paramString);
   if ((localFile != null) && (localFile.exists())) return localFile;
   return null;
 }
Пример #6
0
 public static boolean removeFromCache(String paramString, DiskCache paramDiskCache) {
   File localFile = paramDiskCache.get(paramString);
   return (localFile != null) && (localFile.exists()) && (localFile.delete());
 }
 @Override
 public TileImageLoader.Result loadInBackground() {
   if (mUri == null) {
     return Result.nullInstance();
   }
   final String scheme = mUri.getScheme();
   if ("http".equals(scheme) || "https".equals(scheme)) {
     final String url = mUri.toString();
     if (url == null) return Result.nullInstance();
     File cacheFile = mDiskCache.get(url);
     if (cacheFile != null) {
       final int cachedValidity = ImageValidator.checkImageValidity(cacheFile);
       if (ImageValidator.isValid(cachedValidity)) {
         // The file is corrupted, so we remove it from
         // cache.
         return decodeBitmapOnly(
             cacheFile, ImageValidator.isValidForRegionDecoder(cachedValidity));
       }
     }
     try {
       // from SD cache
       final InputStream is = mDownloader.getStream(url, new AccountExtra(mAccountId));
       if (is == null) return Result.nullInstance();
       try {
         final long length = is.available();
         mHandler.post(new DownloadStartRunnable(this, mListener, length));
         mDiskCache.save(
             url,
             is,
             new IoUtils.CopyListener() {
               @Override
               public boolean onBytesCopied(int current, int total) {
                 mHandler.post(new ProgressUpdateRunnable(mListener, current));
                 return !isAbandoned();
               }
             });
         mHandler.post(new DownloadFinishRunnable(this, mListener));
       } finally {
         IoUtils.closeSilently(is);
       }
       cacheFile = mDiskCache.get(url);
       final int downloadedValidity = ImageValidator.checkImageValidity(cacheFile);
       if (ImageValidator.isValid(downloadedValidity)) {
         // The file is corrupted, so we remove it from
         // cache.
         return decodeBitmapOnly(
             cacheFile, ImageValidator.isValidForRegionDecoder(downloadedValidity));
       } else {
         mDiskCache.remove(url);
         throw new IOException();
       }
     } catch (final Exception e) {
       mHandler.post(new DownloadErrorRunnable(this, mListener, e));
       return Result.getInstance(cacheFile, e);
     }
   } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
     final File file = new File(mUri.getPath());
     try {
       return decodeBitmapOnly(
           file, ImageValidator.isValidForRegionDecoder(ImageValidator.checkImageValidity(file)));
     } catch (final Exception e) {
       return Result.getInstance(file, e);
     }
   }
   return Result.nullInstance();
 }
Пример #8
0
 @Override
 public void remove(@NonNull final String key) {
   cache.remove(key);
 }
Пример #9
0
 @Override
 public File get(@NonNull final String key) {
   return cache.get(key);
 }