Пример #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
  @Override
  public void onCreate() {
    super.onCreate();

    Picasso picasso =
        new Picasso.Builder(this).memoryCache(new LruCache(calculateMemoryCacheSize())).build();
    Picasso.setSingletonInstance(picasso);
  }
  @Override
  public void onCreate() {
    super.onCreate();

    // Catch leaks, in debug builds (release builds use a no-op).
    LeakCanary.install(this);

    // Let us log errors from Picasso to give us some clues when things go wrong.
    // Unfortunately, we can't get these errors in the regular onError() callback:
    // https://github.com/square/picasso/issues/379
    final Picasso picasso =
        (new Picasso.Builder(this)).listener(GalaxyZooApplication.picassoListener).build();
    // This affects what, for instance, Picasso.with() will return:
    try {
      Picasso.setSingletonInstance(picasso);
    } catch (final IllegalStateException ex) {
      // Nevermind if this happens. It's not worth crashing the app because of this.
      // It would just mean that we don't log the errors.
      Log.error(
          "GalaxyZooApplication.onCreate(): It is too late to call Picasso.setSingletonInstance().",
          ex);
    }
  }