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 Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #2
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #3
Source Project: cathode   Author: SimonVT   File: ImageRequestHandler.java    License: 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 #4
Source Project: cathode   Author: SimonVT   File: ImageModule.java    License: 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 #5
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #6
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #7
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #8
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #9
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #10
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #11
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #12
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 #13
Source Project: SteamGifts   Author: SteamGifts   File: ImageFragment.java    License: 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 #14
Source Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 Project: picasso2-okhttp3-downloader   Author: JakeWharton   File: OkHttp3DownloaderTest.java    License: 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 Project: cathode   Author: SimonVT   File: ImageRequestHandler.java    License: 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 Project: cathode   Author: SimonVT   File: ImageModule.java    License: 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 Project: cathode   Author: SimonVT   File: ImageModule.java    License: 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);
}