Пример #1
0
  @Override
  public void onCreate() {
    super.onCreate();

    // Creates an interceptor to force image caching no matter the server instructions
    Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR =
        new Interceptor() {
          @Override
          public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            return originalResponse.newBuilder().header("Cache-Control", "max-age=60").build();
          }
        };

    // Retrieves the app cache directory and sets up a cache for the OkHttpClient
    File cacheDir = this.getExternalCacheDir();
    if (cacheDir == null) {
      // Fall back to using the internal cache directory
      cacheDir = this.getCacheDir();
    }
    client.setCache(new Cache(cacheDir, 100 * 1024 * 1024));
    client.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
    client.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);

    // Sets up the picasso global singleton instance
    Picasso.Builder builder = new Picasso.Builder(this);
    Picasso picasso = builder.downloader(new OkHttpDownloader(client)).build();

    Picasso.setSingletonInstance(picasso);
  }
Пример #2
0
 public static Picasso get(Context context, Picasso.Listener errorListener) {
   Picasso.Builder builder =
       new Picasso.Builder(context)
           .downloader(new OkHttpDownloader(context, PICASSO_CACHE_IN_BYTES));
   if (errorListener != null) {
     builder = builder.listener(errorListener);
   }
   return builder.build();
 }
Пример #3
0
 @Provides
 @Singleton
 Picasso providePicasso(
     OkHttpClient client,
     NetworkBehavior behavior,
     @IsMockMode boolean isMockMode,
     Application app) {
   Picasso.Builder builder = new Picasso.Builder(app).downloader(new OkHttp3Downloader(client));
   if (isMockMode) {
     builder.addRequestHandler(new MockRequestHandler(behavior, app.getAssets()));
   }
   builder.listener(
       (picasso, uri, exception) -> {
         Timber.e(exception, "Error while loading image %s", uri);
       });
   return builder.build();
 }
Пример #4
0
 @Provides
 @PerApp
 Picasso providePicasso(
     OkHttpClient client,
     NetworkBehavior behavior,
     @IsMockMode boolean isMockMode,
     Application app) {
   Picasso.Builder builder = new Picasso.Builder(app).downloader(new OkHttpDownloader(client));
   if (isMockMode) {
     builder.addRequestHandler(new MockRequestHandler(behavior, app.getAssets()));
   }
   builder.listener(
       new Picasso.Listener() {
         @Override
         public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
           Timber.e(exception, "Error while loading image " + uri);
         }
       });
   return builder.build();
 }