package tv.danmaku.ijk.media.player.ui;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import com.tekartik.sqflite.g;
import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/* JADX INFO: loaded from: classes3.dex */
public class CacheManager {
    private static final int MAX_CACHE_FILE_SIZE = 3;
    private static final String TAG = "CacheManager";
    public static final int eachFileSize = 31457280;

    @SuppressLint({"StaticFieldLeak"})
    private static volatile CacheManager instance;
    public String cachePath;
    private Context context;

    private void deleteFile(File file) {
        if (file == null || !file.exists() || file.delete()) {
            return;
        }
        file.getAbsolutePath();
    }

    public static CacheManager getInstance() {
        if (instance == null) {
            synchronized (CacheManager.class) {
                try {
                    if (instance == null) {
                        instance = new CacheManager();
                    }
                } finally {
                }
            }
        }
        return instance;
    }

    private boolean isLocalUrl(String str) {
        return (str == null || str.startsWith("http://") || str.startsWith("https://") || str.startsWith("file://") || str.startsWith("smb://") || str.startsWith("/data/") || str.startsWith("/storage") || str.startsWith("ftp://") || str.startsWith("rtsp://") || str.startsWith("rtmp://") || str.startsWith("mms://") || str.startsWith("ws://") || str.startsWith("wss://") || str.startsWith("sftp://") || str.startsWith("storage://") || str.startsWith("content://")) ? false : true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$deleteCacheFile$0(String str) {
        String cacheKey = getCacheKey(str);
        if (cacheKey == null || this.cachePath == null) {
            return;
        }
        File file = new File(defpackage.e.p(new StringBuilder(), this.cachePath, cacheKey, ".temp"));
        File file2 = new File(defpackage.e.p(new StringBuilder(), this.cachePath, cacheKey, ".map"));
        deleteFile(file);
        deleteFile(file2);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ int lambda$deleteCacheFile$1(File file, File file2) {
        if (file == null || file2 == null) {
            return 0;
        }
        return Long.compare(file.lastModified(), file2.lastModified());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$deleteCacheFile$2() {
        File[] fileArrListFiles;
        if (this.cachePath == null || (fileArrListFiles = new File(this.cachePath).listFiles()) == null || fileArrListFiles.length < 6) {
            return;
        }
        Arrays.sort(fileArrListFiles, new androidx.browser.trusted.b(8));
        for (int i10 = 0; i10 < fileArrListFiles.length - 6; i10++) {
            deleteFile(fileArrListFiles[i10]);
        }
    }

    private String md5(String str) {
        if (str != null && !str.isEmpty()) {
            try {
                byte[] bArrDigest = MessageDigest.getInstance("MD5").digest(str.getBytes());
                StringBuilder sb = new StringBuilder();
                for (byte b10 : bArrDigest) {
                    int i10 = b10 & 255;
                    if (i10 < 16) {
                        sb.append(SessionDescription.SUPPORTED_SDP_VERSION);
                    }
                    sb.append(Integer.toHexString(i10));
                }
                return sb.toString();
            } catch (NoSuchAlgorithmException unused) {
            }
        }
        return null;
    }

    public void deleteCacheFile(String str) {
        new Thread(new g(this, str, 3)).start();
    }

    public String getCacheKey(String str) {
        if (!isLocalUrl(str) || !str.contains("furl=") || !str.contains("&")) {
            return null;
        }
        String str2 = str.split("furl=")[1].split("&")[0];
        if (!str2.contains("/")) {
            return str2;
        }
        String[] strArrSplit = str2.split("/");
        return md5(strArrSplit[strArrSplit.length - 1]);
    }

    public void init(Context context) {
        if (context == null) {
            return;
        }
        this.context = context.getApplicationContext();
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(this.context.getCacheDir());
            String str = File.separator;
            sb.append(str);
            sb.append("ijk_cache");
            sb.append(str);
            this.cachePath = sb.toString();
            File file = new File(this.cachePath);
            if (file.exists()) {
                return;
            }
            file.mkdirs();
        } catch (Exception unused) {
        }
    }

    public boolean isMemoryEnough() {
        ActivityManager activityManager;
        Context context = this.context;
        if (context == null || (activityManager = (ActivityManager) context.getSystemService("activity")) == null) {
            return false;
        }
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        return memoryInfo.availMem > 62914560;
    }

    public void deleteCacheFile() {
        new Thread(new a(this, 0)).start();
    }
}
