com.squareup.picasso.Downloader Java Examples

The following examples show how to use com.squareup.picasso.Downloader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageModule.java    From cathode with Apache License 2.0 6 votes vote down vote up
@Provides @Singleton Picasso providePicasso(Context context, Downloader downloader,
    ImageRequestHandler imageRequestHandler, ShowRequestHandler showRequestHandler,
    SeasonRequestHandler seasonRequestHandler, EpisodeRequestHandler episodeRequestHandler,
    MovieRequestHandler movieRequestHandler, PersonRequestHandler personRequestHandler) {
  Picasso.Builder builder =
      new Picasso.Builder(context).requestTransformer(new ImageRequestTransformer(context))
          .addRequestHandler(imageRequestHandler)
          .addRequestHandler(showRequestHandler)
          .addRequestHandler(seasonRequestHandler)
          .addRequestHandler(episodeRequestHandler)
          .addRequestHandler(movieRequestHandler)
          .addRequestHandler(personRequestHandler)
          .downloader(downloader);

  if (BuildConfig.DEBUG) {
    builder.listener(new Picasso.Listener() {
      @Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
        Timber.d(exception);
      }
    });
  }

  return builder.build();
}
 
Example #2
Source File: ImageRequestHandler.java    From cathode with Apache License 2.0 6 votes vote down vote up
@Override public RequestHandler.Result load(Request request, int networkPolicy)
    throws IOException {
  final String baseUrl = getBaseUrl();
  if (TextUtils.isEmpty(baseUrl)) {
    return null;
  }

  String path = transform(request, request.uri);

  if (TextUtils.isEmpty(path)) {
    return null;
  }

  Downloader.Response response = downloader.load(Uri.parse(path), networkPolicy);
  if (response == null) {
    return null;
  }

  InputStream is = response.getInputStream();
  if (is == null) {
    return null;
  }

  return new RequestHandler.Result(is, Picasso.LoadedFrom.NETWORK);
}
 
Example #3
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 6 votes vote down vote up
@Test public void networkPolicyNoCache() throws Exception {
  MockResponse response =
      new MockResponse().setHeader("Cache-Control", "max-age=31536000").setBody("Hi");
  server.enqueue(response);

  Downloader.Response response1 = downloader.load(uri, 0);
  assertThat((Boolean) cached.get(response1)).isFalse();
  // Exhaust input stream to ensure response is cached.
  Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();

  // Enqueue the same response again but this time use NetworkPolicy.NO_CACHE.
  server.enqueue(response);

  Downloader.Response response2 = downloader.load(uri, NO_CACHE);
  // Response should not be coming from cache even if it was cached previously.
  assertThat((Boolean) cached.get(response2)).isFalse();
}
 
Example #4
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 6 votes vote down vote up
@Test public void networkPolicyNoCache() throws Exception {
  MockResponse response =
      new MockResponse().setHeader("Cache-Control", "max-age=31536000").setBody("Hi");
  server.enqueue(response);

  Downloader.Response response1 = downloader.load(uri, 0);
  assertThat((Boolean) cached.get(response1)).isFalse();
  // Exhaust input stream to ensure response is cached.
  Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();

  // Enqueue the same response again but this time use NetworkPolicy.NO_CACHE.
  server.enqueue(response);

  Downloader.Response response2 = downloader.load(uri, NO_CACHE);
  // Response should not be coming from cache even if it was cached previously.
  assertThat((Boolean) cached.get(response2)).isFalse();
}
 
Example #5
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 5 votes vote down vote up
@Test public void cachedResponse() throws Exception {
  server.enqueue(new MockResponse()
      .setHeader("Cache-Control", "max-age=31536000")
      .setBody("Hi"));

  Downloader.Response response1 = downloader.load(uri, 0);
  assertThat((Boolean) cached.get(response1)).isFalse();
  // Exhaust input stream to ensure response is cached.
  Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();

  Downloader.Response response2 = downloader.load(uri, OFFLINE);
  assertThat((Boolean) cached.get(response2)).isTrue();
}
 
Example #6
Source File: ImageFragment.java    From SteamGifts with MIT License 5 votes vote down vote up
@Override
protected byte[] doInBackground(Void... params) {
    try {
        // Grab an input stream to the image
        OkHttpDownloader downloader = new OkHttpDownloader(getContext());
        Downloader.Response response = downloader.load(Uri.parse(url), 0);

        // Read the image into a byte array
        return Okio.buffer(Okio.source(response.getInputStream())).readByteArray();
    } catch (Exception e) {
        Log.d(ImageFragment.class.getSimpleName(), "Error fetching image", e);
        return null;
    }
}
 
Example #7
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 5 votes vote down vote up
@Test public void throwsResponseException() throws Exception {
  server.enqueue(new MockResponse().setStatus("HTTP/1.1 401 Not Authorized"));

  try {
    downloader.load(uri, 0);
    fail("Expected ResponseException.");
  } catch (Downloader.ResponseException e) {
    assertThat(e).hasMessage("401 Not Authorized");
  }
}
 
Example #8
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 5 votes vote down vote up
@Test public void offlineStaleResponse() throws Exception {
  server.enqueue(new MockResponse()
      .setHeader("Cache-Control", "max-age=1")
      .setHeader("Expires", "Mon, 29 Dec 2014 21:44:55 GMT")
      .setBody("Hi"));

  Downloader.Response response1 = downloader.load(uri, 0);
  assertThat((Boolean) cached.get(response1)).isFalse();
  // Exhaust input stream to ensure response is cached.
  Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();

  Downloader.Response response2 = downloader.load(uri, OFFLINE);
  assertThat((Boolean) cached.get(response2)).isTrue();
}
 
Example #9
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 5 votes vote down vote up
@Test public void cachedResponse() throws Exception {
  server.enqueue(new MockResponse()
      .setHeader("Cache-Control", "max-age=31536000")
      .setBody("Hi"));

  Downloader.Response response1 = downloader.load(uri, 0);
  assertThat((Boolean) cached.get(response1)).isFalse();
  // Exhaust input stream to ensure response is cached.
  Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();

  Downloader.Response response2 = downloader.load(uri, OFFLINE);
  assertThat((Boolean) cached.get(response2)).isTrue();
}
 
Example #10
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
  downloader = new OkHttp3Downloader(temporaryFolder.getRoot());
  uri = Uri.parse(server.url("/").toString());

  cached = Downloader.Response.class.getDeclaredField("cached");
  cached.setAccessible(true);
}
 
Example #11
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 5 votes vote down vote up
@Test public void throwsResponseException() throws Exception {
  server.enqueue(new MockResponse().setStatus("HTTP/1.1 401 Not Authorized"));

  try {
    downloader.load(uri, 0);
    fail("Expected ResponseException.");
  } catch (Downloader.ResponseException e) {
    assertThat(e).hasMessage("401 Not Authorized");
  }
}
 
Example #12
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 5 votes vote down vote up
@Test public void offlineStaleResponse() throws Exception {
  server.enqueue(new MockResponse()
      .setHeader("Cache-Control", "max-age=1")
      .setHeader("Expires", "Mon, 29 Dec 2014 21:44:55 GMT")
      .setBody("Hi"));

  Downloader.Response response1 = downloader.load(uri, 0);
  assertThat((Boolean) cached.get(response1)).isFalse();
  // Exhaust input stream to ensure response is cached.
  Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();

  Downloader.Response response2 = downloader.load(uri, OFFLINE);
  assertThat((Boolean) cached.get(response2)).isTrue();
}
 
Example #13
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
  downloader = new OkHttp3Downloader(temporaryFolder.getRoot());
  uri = Uri.parse(server.url("/").toString());

  cached = Downloader.Response.class.getDeclaredField("cached");
  cached.setAccessible(true);
}
 
Example #14
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 4 votes vote down vote up
@Test public void readsContentLengthHeader() throws Exception {
  server.enqueue(new MockResponse().addHeader("Content-Length", 1024));

  Downloader.Response response = downloader.load(uri, 0);
  assertThat(response.getContentLength()).isEqualTo(1024);
}
 
Example #15
Source File: OkHttp3DownloaderTest.java    From picasso2-okhttp3-downloader with Apache License 2.0 4 votes vote down vote up
@Test public void readsContentLengthHeader() throws Exception {
  server.enqueue(new MockResponse().addHeader("Content-Length", 1024));

  Downloader.Response response = downloader.load(uri, 0);
  assertThat(response.getContentLength()).isEqualTo(1024);
}
 
Example #16
Source File: ImageRequestHandler.java    From cathode with Apache License 2.0 4 votes vote down vote up
public ImageRequestHandler(Context context, ConfigurationService configurationService,
    Downloader downloader) {
  super(context, configurationService);
  this.downloader = downloader;
}
 
Example #17
Source File: ImageModule.java    From cathode with Apache License 2.0 4 votes vote down vote up
@Provides @Singleton Downloader provideDownloader(@Named("Images") OkHttpClient okClient) {
  return new OkHttp3Downloader(okClient);
}
 
Example #18
Source File: ImageModule.java    From cathode with Apache License 2.0 4 votes vote down vote up
@Provides @Singleton ImageRequestHandler provideImageRequestHandler(Context context,
    ConfigurationService configurationService, Downloader downloader) {
  return new ImageRequestHandler(context, configurationService, downloader);
}