Android Open Source - ponyville-live-android Mock Downloader

From Project

Back to project page ponyville-live-android.


The source code is released under:

Apache License

If you think the Android project ponyville-live-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//from w  ww .j  a v a 2  s .  co  m
import android.content.res.AssetManager;
import android.os.SystemClock;
import android.util.LruCache;

import com.squareup.picasso.Downloader;
import retrofit.MockRestAdapter;

 * A Picasso {@link Downloader} which loads images from assets but attempts to emulate the
 * subtleties of a real HTTP client and its disk cache.
 * <p>
 * Images <em>must</em> be in the form {@code mock:///path/to/asset.png}.
public final class MockDownloader implements Downloader {
    private final MockRestAdapter mockRestAdapter;
    private final AssetManager    assetManager;

    /** Emulate the disk cache by storing the URLs in an LRU using its size as the value. */
    private final LruCache<String, Long> emulatedDiskCache =
            new LruCache<String, Long>(NetModule.CACHE_SIZE) {
                protected int sizeOf(String key, Long value) {
                    return (int) Math.min(value.longValue(), Integer.MAX_VALUE);

    public MockDownloader(MockRestAdapter mockRestAdapter, AssetManager assetManager) {
        this.mockRestAdapter = mockRestAdapter;
        this.assetManager = assetManager;

    public Response load(Uri uri, boolean localCacheOnly) throws IOException {
        if (!"mock".equals(uri.getScheme())) {
            throw new RuntimeException("Attempted to download non-mock image ("
                    + uri
                    + ") using the mock downloader. Mock URLs must use scheme 'mock'.");

        String imagePath = 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 Response(, true);

    // If we are not allowed to hit the network and the cache missed return a big fat nothing.
    if (localCacheOnly) {
      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 (mockRestAdapter.calculateIsFailure()) {
      throw new IOException("Fake network error!");

    // We aren't throwing a network error so fake a round trip delay.

    // Since we cache missed, load the file size and put it in the LRU.
    long size = assetManager.openFd(imagePath).getLength();
    emulatedDiskCache.put(imagePath, size);

    // Grab the image stream and return it.
    return new Response(, false);

    public void shutdown() {

Java Source Code List