morristech
5/8/2018 - 12:26 PM

Execute Picasso request directly on OkHttp

Execute Picasso request directly on OkHttp

import android.content.Context;
import android.net.Uri;

import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.picasso.Downloader;

import java.io.File;
import java.io.IOException;

/**
 * Created by nikola on 12/29/14.
 */
public class OkHttp22Downloader implements Downloader {
    static final String RESPONSE_SOURCE_ANDROID = "X-Android-Response-Source";
    static final String RESPONSE_SOURCE_OKHTTP = "OkHttp-Response-Source";
    private static OkHttpClient sClient;

    public OkHttp22Downloader(final Context context){
        this(Utils.createDefaultCacheDir(context));
    }
    public OkHttp22Downloader(final File cacheDir){
        this(cacheDir, Utils.calculateDiskCacheSize(cacheDir));
    }
    public OkHttp22Downloader(final Context context, final long maxSize){
        this(Utils.createDefaultCacheDir(context), maxSize);
    }
    public OkHttp22Downloader(final File cacheDir, final long maxSize){
        this(new OkHttpClient());
        try {
            sClient.setCache(new Cache(cacheDir, maxSize));
        } catch (IOException e) {

        }
    }
    private OkHttp22Downloader(OkHttpClient client){
        sClient = client;
    }
@Override
    public Response load(Uri uri, int networkPolicy) throws IOException {
        Request.Builder builder = new Request.Builder();
        builder.url(uri.toString());
        if (networkPolicy != 0) {
            CacheControl cacheControl;
            if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
                cacheControl = CacheControl.FORCE_CACHE;
            } else {
                CacheControl.Builder cacheControlBuilder = new CacheControl.Builder();
                if (!NetworkPolicy.shouldReadFromDiskCache(networkPolicy)) {
                    cacheControlBuilder.noCache();
                }
                if (!NetworkPolicy.shouldWriteToDiskCache(networkPolicy)) {
                    cacheControlBuilder.noStore();
                }
                cacheControl = cacheControlBuilder.build();
            }
            builder.cacheControl(cacheControl);
        }
        com.squareup.okhttp.Response okResponse = sClient.newCall(builder.build()).execute();
        return  buildPicassoResponse(okResponse, networkPolicy);
    }

    private Response buildPicassoResponse(com.squareup.okhttp.Response okResponse, int networkPolicy) throws ResponseException {
        int responseCode = okResponse.code();
        if(responseCode >= 300) {
            throw new ResponseException(okResponse.message(), networkPolicy, responseCode );
        }
        long contentLength = okResponse.body().contentLength();
        boolean fromCache = okResponse.cacheResponse() != null;
        return new Response(okResponse.body().byteStream(), fromCache, contentLength);
    }

    public void shutdown() {
        com.squareup.okhttp.Cache cache = sClient.getCache();
        if (cache != null) {
            try {
                cache.close();
            } catch (IOException ignored) {
            }
        }
    }
    public void clearCache(){
        com.squareup.okhttp.Cache cache = sClient.getCache();
        if (cache != null) {
            try {
                cache.clear()
            } catch (IOException ignored) {
            }
        }
    }
}