294 lines
11 KiB
Java
294 lines
11 KiB
Java
package com.ubt.jimu.base.http.cache;
|
|
|
|
import android.content.Context;
|
|
import android.content.pm.PackageInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.graphics.Bitmap;
|
|
import android.os.Environment;
|
|
import android.text.TextUtils;
|
|
import android.util.Base64;
|
|
import com.ubt.jimu.JimuApplication;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.StreamCorruptedException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import okhttp3.internal.cache.DiskLruCache;
|
|
import okhttp3.internal.io.FileSystem;
|
|
|
|
/* loaded from: classes.dex */
|
|
public final class CacheManager<T> {
|
|
private static final String CACHE_DIR = "jimuCacheDir";
|
|
private static final int DISK_CACHE_INDEX = 0;
|
|
private static final long DISK_CACHE_SIZE = 10485760;
|
|
public static final String TAG = "CacheManager";
|
|
private static CacheManager mCacheManager;
|
|
private Context context;
|
|
private DiskLruCache mDiskLruCache;
|
|
|
|
public CacheManager() {
|
|
File diskCacheDir = getDiskCacheDir(JimuApplication.l(), CACHE_DIR);
|
|
if (!diskCacheDir.exists()) {
|
|
diskCacheDir.mkdirs();
|
|
}
|
|
if (diskCacheDir.getUsableSpace() > DISK_CACHE_SIZE) {
|
|
try {
|
|
this.mDiskLruCache = DiskLruCache.create(FileSystem.SYSTEM, diskCacheDir, 3960075, 10, DISK_CACHE_SIZE);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
private String encryptMD5(String str) {
|
|
if (TextUtils.isEmpty(str)) {
|
|
return "";
|
|
}
|
|
try {
|
|
byte[] digest = MessageDigest.getInstance("MD5").digest(str.getBytes("UTF-8"));
|
|
StringBuilder sb = new StringBuilder(digest.length * 2);
|
|
for (byte b : digest) {
|
|
int i = b & 255;
|
|
if (i < 16) {
|
|
sb.append("0");
|
|
}
|
|
sb.append(Integer.toHexString(i));
|
|
}
|
|
return sb.toString();
|
|
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
|
|
e.printStackTrace();
|
|
return str;
|
|
}
|
|
}
|
|
|
|
private int getAppVersion(Context context) {
|
|
try {
|
|
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
|
if (packageInfo == null) {
|
|
return 0;
|
|
}
|
|
return packageInfo.versionCode;
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
e.printStackTrace();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private Bitmap getBitmapCache(String str) {
|
|
return null;
|
|
}
|
|
|
|
private File getDiskCacheDir(Context context, String str) {
|
|
String absolutePath;
|
|
if ("mounted".equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
|
|
File externalCacheDir = context.getExternalCacheDir();
|
|
absolutePath = externalCacheDir != null ? externalCacheDir.getAbsolutePath() : context.getCacheDir().getPath();
|
|
} else {
|
|
absolutePath = context.getCacheDir().getPath();
|
|
}
|
|
return new File(absolutePath + File.separator + str);
|
|
}
|
|
|
|
public static synchronized CacheManager getInstance(Context context) {
|
|
CacheManager cacheManager;
|
|
synchronized (CacheManager.class) {
|
|
if (mCacheManager == null) {
|
|
mCacheManager = new CacheManager();
|
|
}
|
|
cacheManager = mCacheManager;
|
|
}
|
|
return cacheManager;
|
|
}
|
|
|
|
private void putBitmapCache(Bitmap bitmap) {
|
|
}
|
|
|
|
public void closeDiskLruCache() {
|
|
DiskLruCache diskLruCache = this.mDiskLruCache;
|
|
if (diskLruCache == null) {
|
|
return;
|
|
}
|
|
try {
|
|
diskLruCache.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public T getObjectCache(String str, long j) {
|
|
String stringCache = getStringCache(str);
|
|
if (TextUtils.isEmpty(stringCache)) {
|
|
return null;
|
|
}
|
|
String[] split = stringCache.split("@");
|
|
if (System.currentTimeMillis() - Long.parseLong(split[0]) > j) {
|
|
return null;
|
|
}
|
|
try {
|
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Base64.decode(split[1].getBytes(), 0));
|
|
try {
|
|
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
|
|
try {
|
|
try {
|
|
T t = (T) objectInputStream.readObject();
|
|
objectInputStream.close();
|
|
byteArrayInputStream.close();
|
|
return t;
|
|
} catch (ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
objectInputStream.close();
|
|
byteArrayInputStream.close();
|
|
return null;
|
|
}
|
|
} finally {
|
|
}
|
|
} finally {
|
|
}
|
|
} catch (StreamCorruptedException e2) {
|
|
e2.printStackTrace();
|
|
return null;
|
|
} catch (IOException e3) {
|
|
e3.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public String getStringCache(String str) {
|
|
if (this.mDiskLruCache == null) {
|
|
}
|
|
return "";
|
|
}
|
|
|
|
/* JADX WARN: Removed duplicated region for block: B:38:0x0079 A[EXC_TOP_SPLITTER, SYNTHETIC] */
|
|
/* JADX WARN: Removed duplicated region for block: B:45:? A[SYNTHETIC] */
|
|
/* JADX WARN: Removed duplicated region for block: B:46:0x006f A[EXC_TOP_SPLITTER, SYNTHETIC] */
|
|
/* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:55:0x0067 -> B:12:0x006a). Please report as a decompilation issue!!! */
|
|
/*
|
|
Code decompiled incorrectly, please refer to instructions dump.
|
|
To view partially-correct code enable 'Show inconsistent code' option in preferences
|
|
*/
|
|
public void putObjectCache(java.lang.String r6, java.lang.Object r7) {
|
|
/*
|
|
r5 = this;
|
|
r0 = 0
|
|
java.io.ByteArrayOutputStream r1 = new java.io.ByteArrayOutputStream // Catch: java.lang.Throwable -> L4d java.io.IOException -> L51
|
|
r1.<init>() // Catch: java.lang.Throwable -> L4d java.io.IOException -> L51
|
|
java.io.ObjectOutputStream r2 = new java.io.ObjectOutputStream // Catch: java.lang.Throwable -> L46 java.io.IOException -> L49
|
|
r2.<init>(r1) // Catch: java.lang.Throwable -> L46 java.io.IOException -> L49
|
|
r2.writeObject(r7) // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
byte[] r7 = r1.toByteArray() // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
r0 = 0
|
|
java.lang.String r7 = android.util.Base64.encodeToString(r7, r0) // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
long r3 = java.lang.System.currentTimeMillis() // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
java.lang.Long r0 = java.lang.Long.valueOf(r3) // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
java.lang.StringBuilder r3 = new java.lang.StringBuilder // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
r3.<init>() // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
r3.append(r0) // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
java.lang.String r0 = "@"
|
|
r3.append(r0) // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
r3.append(r7) // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
java.lang.String r7 = r3.toString() // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
r5.putStringCache(r6, r7) // Catch: java.lang.Throwable -> L42 java.io.IOException -> L44
|
|
r1.close() // Catch: java.io.IOException -> L3a
|
|
goto L3e
|
|
L3a:
|
|
r6 = move-exception
|
|
r6.printStackTrace()
|
|
L3e:
|
|
r2.close() // Catch: java.io.IOException -> L66
|
|
goto L6a
|
|
L42:
|
|
r6 = move-exception
|
|
goto L6d
|
|
L44:
|
|
r6 = move-exception
|
|
goto L4b
|
|
L46:
|
|
r6 = move-exception
|
|
r2 = r0
|
|
goto L6d
|
|
L49:
|
|
r6 = move-exception
|
|
r2 = r0
|
|
L4b:
|
|
r0 = r1
|
|
goto L53
|
|
L4d:
|
|
r6 = move-exception
|
|
r1 = r0
|
|
r2 = r1
|
|
goto L6d
|
|
L51:
|
|
r6 = move-exception
|
|
r2 = r0
|
|
L53:
|
|
r6.printStackTrace() // Catch: java.lang.Throwable -> L6b
|
|
if (r0 == 0) goto L60
|
|
r0.close() // Catch: java.io.IOException -> L5c
|
|
goto L60
|
|
L5c:
|
|
r6 = move-exception
|
|
r6.printStackTrace()
|
|
L60:
|
|
if (r2 == 0) goto L6a
|
|
r2.close() // Catch: java.io.IOException -> L66
|
|
goto L6a
|
|
L66:
|
|
r6 = move-exception
|
|
r6.printStackTrace()
|
|
L6a:
|
|
return
|
|
L6b:
|
|
r6 = move-exception
|
|
r1 = r0
|
|
L6d:
|
|
if (r1 == 0) goto L77
|
|
r1.close() // Catch: java.io.IOException -> L73
|
|
goto L77
|
|
L73:
|
|
r7 = move-exception
|
|
r7.printStackTrace()
|
|
L77:
|
|
if (r2 == 0) goto L81
|
|
r2.close() // Catch: java.io.IOException -> L7d
|
|
goto L81
|
|
L7d:
|
|
r7 = move-exception
|
|
r7.printStackTrace()
|
|
L81:
|
|
throw r6
|
|
*/
|
|
throw new UnsupportedOperationException("Method not decompiled: com.ubt.jimu.base.http.cache.CacheManager.putObjectCache(java.lang.String, java.lang.Object):void");
|
|
}
|
|
|
|
public void putStringCache(String str, String str2) {
|
|
if (this.mDiskLruCache == null) {
|
|
}
|
|
}
|
|
|
|
public boolean removeCache(String str) {
|
|
DiskLruCache diskLruCache = this.mDiskLruCache;
|
|
if (diskLruCache == null) {
|
|
return false;
|
|
}
|
|
try {
|
|
return diskLruCache.remove(encryptMD5(str));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void setCache(final String str, final String str2) {
|
|
new Thread() { // from class: com.ubt.jimu.base.http.cache.CacheManager.1
|
|
@Override // java.lang.Thread, java.lang.Runnable
|
|
public void run() {
|
|
CacheManager.this.putStringCache(str, str2);
|
|
}
|
|
}.start();
|
|
}
|
|
}
|