package com.czhj.sdk.common.utils; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.os.Handler; import android.text.TextUtils; import android.util.LruCache; import android.widget.ImageView; import com.czhj.sdk.logger.SigmobLog; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ImageManager { private static ImageManager a; private Context b; private final String c = "SigImageCache"; private ExecutorService d = Executors.newFixedThreadPool(4); private LruCache<String, Bitmap> e = new LruCache<>(4194304); private Handler f = new Handler(); private File g; private ImageView h; public interface BitmapLoadedListener { void onBitmapLoadFailed(); void onBitmapLoaded(Bitmap bitmap); } public class RequestCreatorRunnable implements Runnable { String a; int b; int c; ImageView d; public RequestCreatorRunnable(String str) { this.a = str; } private Bitmap a() { String str = this.a; File file = new File(ImageManager.this.a(), Md5Util.md5(str.substring(str.lastIndexOf("/") + 1))); if (!file.exists() || file.length() <= 0) { return null; } return BitmapFactory.decodeFile(file.getAbsolutePath()); } private void b() { ImageManager.this.f.post(new Runnable() { /* class com.czhj.sdk.common.utils.ImageManager.RequestCreatorRunnable.AnonymousClass2 */ public void run() { if (RequestCreatorRunnable.this.c != 0 && RequestCreatorRunnable.this.d != null) { RequestCreatorRunnable.this.d.setImageResource(RequestCreatorRunnable.this.c); } } }); } public RequestCreatorRunnable error(int i) { this.c = i; return this; } public void into(ImageView imageView) { this.d = imageView; int i = this.b; if (!(i == 0 || imageView == null)) { imageView.setImageResource(i); } if (!TextUtils.isEmpty(this.a)) { Bitmap bitmap = (Bitmap) ImageManager.this.e.get(this.a); if (bitmap != null) { imageView.setImageBitmap(bitmap); return; } Bitmap a2 = a(); if (a2 != null) { imageView.setImageBitmap(a2); ImageManager.this.e.put(this.a, a2); return; } ImageManager.this.d.submit(this); } } public RequestCreatorRunnable placeholder(int i) { this.b = i; return this; } public void run() { try { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(this.a).openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(2000); if (httpURLConnection.getResponseCode() == 200) { final Bitmap decodeStream = BitmapFactory.decodeStream(httpURLConnection.getInputStream()); ImageManager.this.f.post(new Runnable() { /* class com.czhj.sdk.common.utils.ImageManager.RequestCreatorRunnable.AnonymousClass1 */ public void run() { RequestCreatorRunnable.this.d.setImageBitmap(decodeStream); } }); ImageManager.this.e.put(this.a, decodeStream); String str = this.a; decodeStream.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(ImageManager.this.a(), Md5Util.md5(str.substring(str.lastIndexOf("/") + 1))))); return; } b(); } catch (FileNotFoundException unused) { } catch (Exception e2) { e2.printStackTrace(); b(); } } } public ImageManager(Context context) { this.b = context.getApplicationContext(); } private static ImageManager a(Context context) { if (a == null) { synchronized (ImageManager.class) { if (a == null) { a = new ImageManager(context); } } } return a; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private File a() { File file = this.g; return (file == null || !file.isDirectory() || !this.g.exists()) ? Environment.getExternalStorageState().equals("mounted") ? new File(this.b.getExternalCacheDir(), "SigImageCache") : new File(this.b.getCacheDir(), "SigImageCache") : this.g; } public static ImageManager with(Context context) { return a(context); } public void clearCache() { String str; try { File[] clearCacheFileByCount = FileUtil.clearCacheFileByCount(FileUtil.orderByDate(a().getAbsolutePath()), 100); if (clearCacheFileByCount == null) { str = "native ad file list is null"; } else { str = "native ad file remain num: " + clearCacheFileByCount.length; } SigmobLog.i(str); } catch (Throwable th) { SigmobLog.e("clean native ad file error", th); } } public ImageManager customCachePath(File file) { this.g = file; return this; } public void getBitmap(final String str, final BitmapLoadedListener bitmapLoadedListener) { if (!TextUtils.isEmpty(str)) { Bitmap bitmap = this.e.get(str); Bitmap bitmap2 = null; if (bitmap != null) { bitmapLoadedListener.onBitmapLoaded(bitmap); return; } File file = new File(a(), Md5Util.md5(str.substring(str.lastIndexOf("/") + 1))); if (file.exists() && file.length() > 0) { bitmap2 = BitmapFactory.decodeFile(file.getAbsolutePath()); } if (bitmap2 != null) { this.e.put(str, bitmap2); bitmapLoadedListener.onBitmapLoaded(bitmap2); return; } this.d.submit(new Runnable() { /* class com.czhj.sdk.common.utils.ImageManager.AnonymousClass1 */ public void run() { try { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(2000); if (httpURLConnection.getResponseCode() == 200) { final Bitmap decodeStream = BitmapFactory.decodeStream(httpURLConnection.getInputStream()); httpURLConnection.disconnect(); ImageManager.this.f.post(new Runnable() { /* class com.czhj.sdk.common.utils.ImageManager.AnonymousClass1.AnonymousClass1 */ public void run() { bitmapLoadedListener.onBitmapLoaded(decodeStream); } }); ImageManager.this.e.put(str, decodeStream); String str = str; decodeStream.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(ImageManager.this.a(), Md5Util.md5(str.substring(str.lastIndexOf("/") + 1))))); } } catch (Exception unused) { ImageManager.this.f.post(new Runnable() { /* class com.czhj.sdk.common.utils.ImageManager.AnonymousClass1.AnonymousClass2 */ public void run() { bitmapLoadedListener.onBitmapLoadFailed(); } }); } } }); } } public RequestCreatorRunnable load(String str) { return new RequestCreatorRunnable(str); } }