Android Open Source - ponyville-live-android Mock Downloader






From Project

Back to project page ponyville-live-android.

License

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

package com.ponyvillelive.app.prefs;
//from w  ww .j  a v a 2  s .  co  m
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.SystemClock;
import android.util.LruCache;

import com.ponyvillelive.app.net.NetModule;
import com.squareup.picasso.Downloader;
import java.io.IOException;
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) {
                @Override
                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;
    }

    @Override
    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(assetManager.open(imagePath), 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()) {
      SystemClock.sleep(mockRestAdapter.calculateDelayForError());
      throw new IOException("Fake network error!");
    }

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

    // 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(assetManager.open(imagePath), false);
  }

    @Override
    public void shutdown() {
        //noop
    }
}




Java Source Code List

com.ponyvillelive.app.DebugPvlModule.java
com.ponyvillelive.app.Modules.java
com.ponyvillelive.app.Modules.java
com.ponyvillelive.app.PvlApp.java
com.ponyvillelive.app.PvlModule.java
com.ponyvillelive.app.model.ArrayResponse.java
com.ponyvillelive.app.model.DebugData.java
com.ponyvillelive.app.model.Entity.java
com.ponyvillelive.app.model.MapResponse.java
com.ponyvillelive.app.model.NowPlayingMeta.java
com.ponyvillelive.app.model.ObjectResponse.java
com.ponyvillelive.app.model.Show.java
com.ponyvillelive.app.model.SongWrapper.java
com.ponyvillelive.app.model.Song.java
com.ponyvillelive.app.model.StationMeta.java
com.ponyvillelive.app.model.Station.java
com.ponyvillelive.app.net.API.java
com.ponyvillelive.app.net.DebugNetModule.java
com.ponyvillelive.app.net.MockAPI.java
com.ponyvillelive.app.net.NetModule.java
com.ponyvillelive.app.prefs.AnimationSpeed.java
com.ponyvillelive.app.prefs.ApiEndpoint.java
com.ponyvillelive.app.prefs.ApiEndpoints.java
com.ponyvillelive.app.prefs.BooleanPreference.java
com.ponyvillelive.app.prefs.Endpoint.java
com.ponyvillelive.app.prefs.Endpoints.java
com.ponyvillelive.app.prefs.IntPreference.java
com.ponyvillelive.app.prefs.IsMockMode.java
com.ponyvillelive.app.prefs.MockDownloader.java
com.ponyvillelive.app.prefs.NetworkProxy.java
com.ponyvillelive.app.prefs.ObjectPreference.java
com.ponyvillelive.app.prefs.PicassoDebugging.java
com.ponyvillelive.app.prefs.PixelGridEnabled.java
com.ponyvillelive.app.prefs.PixelRatioEnabled.java
com.ponyvillelive.app.prefs.ScalpelEnabled.java
com.ponyvillelive.app.prefs.ScalpelWireframeEnabled.java
com.ponyvillelive.app.prefs.SeenDebugDrawer.java
com.ponyvillelive.app.prefs.StringPreference.java
com.ponyvillelive.app.ui.ActionbarHideSlidePanelListener.java
com.ponyvillelive.app.ui.ActivityHierarchyServer.java
com.ponyvillelive.app.ui.AnimationSpeedAdapter.java
com.ponyvillelive.app.ui.AppContainer.java
com.ponyvillelive.app.ui.BindableAdapter.java
com.ponyvillelive.app.ui.BottomDrawerFragment.java
com.ponyvillelive.app.ui.DebugAppContainer.java
com.ponyvillelive.app.ui.DebugUiModule.java
com.ponyvillelive.app.ui.EnumAdapter.java
com.ponyvillelive.app.ui.HierarchyTreeChangeListener.java
com.ponyvillelive.app.ui.MainActivity.java
com.ponyvillelive.app.ui.NetworkDelayAdapter.java
com.ponyvillelive.app.ui.NetworkErrorAdapter.java
com.ponyvillelive.app.ui.NetworkVarianceAdapter.java
com.ponyvillelive.app.ui.ProxyAdapter.java
com.ponyvillelive.app.ui.ServerEndpointAdapter.java
com.ponyvillelive.app.ui.SocketActivityHierarchyServer.java
com.ponyvillelive.app.ui.StationAdapter.java
com.ponyvillelive.app.ui.StationFragment.java
com.ponyvillelive.app.ui.TrackListAdapter.java
com.ponyvillelive.app.ui.UiModule.java
com.ponyvillelive.app.util.Strings.java