2020-04-01 13:26:32 +02:00
|
|
|
package org.mian.gitnex.clients;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
import com.squareup.picasso.OkHttp3Downloader;
|
|
|
|
import com.squareup.picasso.Picasso;
|
2020-04-03 19:02:46 +02:00
|
|
|
import org.mian.gitnex.helpers.PicassoCache;
|
2020-04-01 17:28:14 +02:00
|
|
|
import org.mian.gitnex.helpers.ssl.MemorizingTrustManager;
|
2020-04-03 19:02:46 +02:00
|
|
|
import java.io.File;
|
2020-04-01 13:26:32 +02:00
|
|
|
import java.security.SecureRandom;
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
|
2020-04-01 14:38:34 +02:00
|
|
|
/**
|
2020-06-06 22:04:07 +02:00
|
|
|
* Author opyale
|
2020-04-01 14:38:34 +02:00
|
|
|
*/
|
|
|
|
|
2020-04-01 13:26:32 +02:00
|
|
|
public class PicassoService {
|
|
|
|
|
|
|
|
private static PicassoService picassoService;
|
2021-01-01 18:30:30 +01:00
|
|
|
private final File cachePath;
|
2020-04-01 13:26:32 +02:00
|
|
|
private Picasso picasso;
|
|
|
|
|
|
|
|
private PicassoService(Context context) {
|
|
|
|
|
2020-06-06 22:04:07 +02:00
|
|
|
cachePath = new File(context.getCacheDir() + "/picasso_cache/");
|
2020-04-01 13:26:32 +02:00
|
|
|
Picasso.Builder builder = new Picasso.Builder(context);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
|
|
|
|
|
|
|
MemorizingTrustManager memorizingTrustManager = new MemorizingTrustManager(context);
|
|
|
|
sslContext.init(null, new X509TrustManager[]{memorizingTrustManager}, new SecureRandom());
|
|
|
|
|
|
|
|
OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder()
|
|
|
|
.sslSocketFactory(sslContext.getSocketFactory(), memorizingTrustManager)
|
|
|
|
.hostnameVerifier(memorizingTrustManager.wrapHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier()));
|
|
|
|
|
|
|
|
builder.downloader(new OkHttp3Downloader(okHttpClient.build()));
|
|
|
|
builder.listener((picasso, uri, exception) -> {
|
|
|
|
|
2020-04-01 18:39:39 +02:00
|
|
|
//Log.e("PicassoService", Objects.requireNonNull(uri.toString()));
|
2020-04-01 18:58:52 +02:00
|
|
|
//Log.e("PicassoService", exception.toString());
|
2020-04-01 17:28:14 +02:00
|
|
|
|
2020-04-01 13:26:32 +02:00
|
|
|
});
|
|
|
|
|
2020-05-09 16:50:45 +02:00
|
|
|
picasso = builder.memoryCache(new PicassoCache(cachePath, context)).build();
|
2020-04-01 17:28:14 +02:00
|
|
|
|
2020-04-01 13:26:32 +02:00
|
|
|
}
|
|
|
|
catch(Exception e) {
|
|
|
|
|
2020-04-01 17:28:14 +02:00
|
|
|
Log.e("PicassoService", e.toString());
|
2020-04-01 13:26:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Picasso get() {
|
|
|
|
|
2020-06-06 22:04:07 +02:00
|
|
|
cachePath.mkdirs();
|
2020-04-01 13:26:32 +02:00
|
|
|
return picasso;
|
2020-06-06 22:04:07 +02:00
|
|
|
|
2020-04-01 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized PicassoService getInstance(Context context) {
|
|
|
|
|
|
|
|
if(picassoService == null) {
|
|
|
|
picassoService = new PicassoService(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return picassoService;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|