package com.igaworks.ssp.common.k; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.widget.ImageView; import java.io.File; import java.lang.ref.WeakReference; import java.security.MessageDigest; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Comparator; import java.util.Iterator; import java.util.WeakHashMap; /* loaded from: classes2.dex */ public class a { /* renamed from: a, reason: collision with root package name */ public static WeakHashMap f6683a = new WeakHashMap<>(); /* renamed from: b, reason: collision with root package name */ public static ArrayList f6684b = new ArrayList<>(); /* renamed from: c, reason: collision with root package name */ public static int f6685c = 1; /* renamed from: com.igaworks.ssp.common.k.a$a, reason: collision with other inner class name */ static class C0081a implements Comparator { C0081a() { } @Override // java.util.Comparator /* renamed from: a, reason: merged with bridge method [inline-methods] */ public int compare(File file, File file2) { if (file.lastModified() > file2.lastModified()) { return 1; } return file.lastModified() == file2.lastModified() ? 0 : -1; } } static class b implements Comparator { b() { } @Override // java.util.Comparator /* renamed from: a, reason: merged with bridge method [inline-methods] */ public int compare(File file, File file2) { if (file.lastModified() > file2.lastModified()) { return 1; } return file.lastModified() == file2.lastModified() ? 0 : -1; } } public static class c extends ColorDrawable { /* renamed from: a, reason: collision with root package name */ private final WeakReference f6686a; public c(com.igaworks.ssp.common.k.b bVar) { super(0); this.f6686a = new WeakReference<>(bVar); } public com.igaworks.ssp.common.k.b a() { return this.f6686a.get(); } } public interface d { void a(Bitmap bitmap); } public static int a(BitmapFactory.Options options, int i10, int i11) { int i12 = options.outHeight; int i13 = options.outWidth; int i14 = f6685c; if (i12 > i11 || i13 > i10) { int i15 = i12 / 2; int i16 = i13 / 2; while (i15 / i14 > i11 && i16 / i14 > i10) { i14 *= 2; } } com.igaworks.ssp.common.o.m.a.c(Thread.currentThread(), "calculateInSampleSize : " + i14); return i14; } public static void b(Context context) { try { File file = new File(context.getCacheDir() + "/igaw/video"); if (file.exists() && file.listFiles().length > 5) { int length = file.listFiles().length; com.igaworks.ssp.common.o.m.a.c(Thread.currentThread(), "Delete local video file : " + length); File[] listFiles = file.listFiles(); Arrays.sort(listFiles, new b()); for (File file2 : listFiles) { if (file2.isFile()) { Calendar.getInstance().setTimeInMillis(file2.lastModified()); file2.delete(); length--; if (length <= 5) { return; } } } } } catch (Exception unused) { } } private static Bitmap a(Context context, String str, int i10, int i11, boolean z10) { String next; Bitmap bitmap; int i12; try { String a10 = a(context, str, false); if (a10 == null) { return null; } com.igaworks.ssp.common.o.m.a.c(Thread.currentThread(), "getBitmap : " + str); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; if (i10 != 0 && i11 != 0) { options.inJustDecodeBounds = true; BitmapFactory.decodeFile(a10, options); options.inJustDecodeBounds = false; i12 = a(options, i10, i11); options.inSampleSize = i12; return BitmapFactory.decodeFile(a10, options); } i12 = f6685c; options.inSampleSize = i12; return BitmapFactory.decodeFile(a10, options); } catch (Exception unused) { return null; } catch (OutOfMemoryError e10) { e10.printStackTrace(); try { Iterator it = f6683a.keySet().iterator(); if (it != null && it.hasNext() && (bitmap = f6683a.get((next = it.next()))) != null) { bitmap.recycle(); f6683a.remove(next); } System.gc(); if (z10) { return null; } return a(context, str, i10, i11, true); } catch (Exception unused2) { System.gc(); return null; } } } public static void b(Context context, String str, ImageView imageView, int i10, int i11, d dVar) { boolean z10; try { if (f6683a == null) { f6683a = new WeakHashMap<>(); } try { if (f6684b == null) { f6684b = new ArrayList<>(); } int indexOf = f6684b.indexOf(str); if (indexOf >= 0) { f6684b.remove(indexOf); } f6684b.add(0, str); } catch (Exception unused) { } Bitmap bitmap = f6683a.get(str); if (bitmap == null) { bitmap = a(context, str, i10, i11, false); if (bitmap != null) { f6683a.put(str, bitmap); } z10 = true; } else { z10 = false; } if (bitmap != null) { if (dVar != null) { dVar.a(bitmap); } } else if (a(str, imageView)) { if (f6683a.size() > 10) { f6683a.clear(); } com.igaworks.ssp.common.k.b bVar = new com.igaworks.ssp.common.k.b(context, str, imageView, dVar, i10, i11); c cVar = new c(bVar); imageView.setBackgroundColor(0); bVar.a(cVar); bVar.start(); } if (!z10 || f6683a.size() <= 10) { return; } try { int size = f6684b.size() - 1; if (f6683a.get(f6684b.get(size)) != null) { f6683a.remove(f6684b.get(size)); } f6684b.remove(size); } catch (Exception e10) { e10.printStackTrace(); } } catch (Exception e11) { com.igaworks.ssp.common.o.m.a.a(Thread.currentThread(), e11); } } private static com.igaworks.ssp.common.k.b a(ImageView imageView) { if (imageView == null) { return null; } Drawable drawable = imageView.getDrawable(); if (drawable instanceof c) { return ((c) drawable).a(); } return null; } public static String a(Context context, String str, boolean z10) { File file; try { if (z10) { file = new File(context.getCacheDir() + "/igaw/video"); } else { file = new File(context.getCacheDir() + "/igaw/image"); } if (!file.exists()) { file.mkdirs(); } String a10 = a(str); if (a10 != null && a10.length() > 0) { File file2 = new File(file, a10); if (file2.exists()) { return file2.getAbsolutePath(); } } } catch (Exception unused) { } return null; } public static String a(String str) { try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.update(str.getBytes()); byte[] digest = messageDigest.digest(); return String.format("%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", Byte.valueOf(digest[0]), Byte.valueOf(digest[1]), Byte.valueOf(digest[2]), Byte.valueOf(digest[3]), Byte.valueOf(digest[4]), Byte.valueOf(digest[5]), Byte.valueOf(digest[6]), Byte.valueOf(digest[7]), Byte.valueOf(digest[8]), Byte.valueOf(digest[9]), Byte.valueOf(digest[10]), Byte.valueOf(digest[11]), Byte.valueOf(digest[12]), Byte.valueOf(digest[13]), Byte.valueOf(digest[14]), Byte.valueOf(digest[15])); } catch (Exception unused) { return null; } } public static void a(Context context) { try { WeakHashMap weakHashMap = f6683a; if (weakHashMap != null) { for (Object obj : weakHashMap.keySet().toArray()) { if (f6683a.get(obj) != null) { f6683a.remove(obj); } } f6683a.clear(); } ArrayList arrayList = f6684b; if (arrayList != null) { arrayList.clear(); } } catch (Exception unused) { WeakHashMap weakHashMap2 = f6683a; if (weakHashMap2 != null) { weakHashMap2.clear(); } ArrayList arrayList2 = f6684b; if (arrayList2 != null) { arrayList2.clear(); } } try { File file = new File(context.getCacheDir() + "/igaw/image"); if (file.exists() && file.listFiles().length > 150) { int length = file.listFiles().length; com.igaworks.ssp.common.o.m.a.c(Thread.currentThread(), "Delete local image file : " + length); File[] listFiles = file.listFiles(); Arrays.sort(listFiles, new C0081a()); for (File file2 : listFiles) { if (file2.isFile()) { Calendar.getInstance().setTimeInMillis(file2.lastModified()); file2.delete(); length--; if (length <= 140) { break; } } } com.igaworks.ssp.common.o.m.a.c(Thread.currentThread(), "Delete local image file : " + length); } } catch (Exception unused2) { } } public static void a(Context context, String str, ImageView imageView, int i10, int i11, d dVar) { boolean z10; try { if (f6683a == null) { f6683a = new WeakHashMap<>(); } try { if (f6684b == null) { f6684b = new ArrayList<>(); } int indexOf = f6684b.indexOf(str); if (indexOf >= 0) { f6684b.remove(indexOf); } f6684b.add(0, str); } catch (Exception unused) { } Bitmap bitmap = f6683a.get(str); if (bitmap == null) { bitmap = a(context, str, i10, i11, false); if (bitmap != null) { f6683a.put(str, bitmap); } z10 = true; } else { z10 = false; } if (bitmap != null) { if (dVar != null) { dVar.a(bitmap); } } else if (a(str, imageView)) { if (f6683a.size() > 10) { f6683a.clear(); } imageView.setScaleType(ImageView.ScaleType.FIT_XY); com.igaworks.ssp.common.k.b bVar = new com.igaworks.ssp.common.k.b(context, str, imageView, dVar, i10, i11); c cVar = new c(bVar); imageView.setBackgroundColor(0); bVar.a(cVar); bVar.start(); } if (!z10 || f6683a.size() <= 10) { return; } try { int size = f6684b.size() - 1; if (f6683a.get(f6684b.get(size)) != null) { f6683a.remove(f6684b.get(size)); } f6684b.remove(size); } catch (Exception e10) { e10.printStackTrace(); } } catch (Exception e11) { com.igaworks.ssp.common.o.m.a.a(Thread.currentThread(), e11); } } public static void a(Context context, String str, d dVar) { try { if (a(context, str, true) == null) { new com.igaworks.ssp.common.k.b(context, str, dVar, 0, 0).start(); } else if (dVar != null) { dVar.a(null); } } catch (Exception unused) { if (dVar != null) { dVar.a(null); } } } private static boolean a(String str, ImageView imageView) { com.igaworks.ssp.common.k.b a10 = a(imageView); if (a10 != null) { String str2 = a10.f6688a; if (str2 != null && str2.equals(str)) { return false; } a10.a(true); } return true; } }