MockRequestHandler.java 文件源码

java
阅读 25 收藏 0 点赞 0 评论 0

项目:superglue 作者:
@Override public Result load(Request request, int networkPolicy) throws IOException {
  String imagePath = request.uri.getPath().substring(1); // Grab only the path sans leading slash.

  // Check the disk cache for the image. A non-null return value indicates a hit.
  boolean cacheHit = emulatedDiskCache.get(imagePath) != null;

  // If there's a hit, grab the image stream and return it.
  if (cacheHit) {
    return new Result(loadBitmap(imagePath), Picasso.LoadedFrom.DISK);
  }

  // If we are not allowed to hit the network and the cache missed return a big fat nothing.
  if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
    return null;
  }

  // If we got this far there was a cache miss and hitting the network is required. See if we need
  // to fake an network error.
  if (behavior.calculateIsFailure()) {
    SystemClock.sleep(behavior.calculateDelay(MILLISECONDS));
    throw new IOException("Fake network error!");
  }

  // We aren't throwing a network error so fake a round trip delay.
  SystemClock.sleep(behavior.calculateDelay(MILLISECONDS));

  // Since we cache missed put it in the LRU.
  AssetFileDescriptor fileDescriptor = assetManager.openFd(imagePath);
  long size = fileDescriptor.getLength();
  fileDescriptor.close();

  emulatedDiskCache.put(imagePath, size);

  // Grab the image stream and return it.
  return new Result(loadBitmap(imagePath), Picasso.LoadedFrom.NETWORK);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号