package com.appsflyer.internal;

import X.4Ms;
import X.4N2;
import X.8Kc;
import X.8r0;
import X.8r1;
import android.content.Context;
import com.appsflyer.AFLogger;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.abmock.SettingsManager;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public final class af {
    public static af valueOf = new af();

    public static af values() {
        return valueOf;
    }

    static {
        Covode.recordClassIndex(2584);
    }

    public static File values(Context context) {
        return new File(INVOKEVIRTUAL_com_appsflyer_internal_af_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir(context), "AFRequestCache");
    }

    public static File INVOKEVIRTUAL_com_appsflyer_internal_af_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir(Context context) {
        if (4Ms.LIZJ != null && 4Ms.LJ) {
            return 4Ms.LIZJ;
        }
        File filesDir = context.getFilesDir();
        4Ms.LIZJ = filesDir;
        return filesDir;
    }

    public static boolean INVOKEVIRTUAL_com_appsflyer_internal_af_com_ss_android_ugc_aweme_storage_FileLancet_delete(File file) {
        8r1 r3;
        MethodCollector.i(17118);
        try {
            if (8Kc.LIZIZ()) {
                r3 = (8r1) SettingsManager.LIZ().LIZ("storage_intercepter_key", 8r1.class, 4N2.LIZ);
            } else {
                r3 = 4N2.LIZ;
            }
            if (8r0.LIZ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_delete_log", 8r0.LIZ(r3));
            }
            if (8r0.LIZJ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_handle", 8r0.LIZ(r3));
                MethodCollector.o(17118);
                return false;
            }
        } catch (Throwable unused) {
        }
        boolean delete = file.delete();
        MethodCollector.o(17118);
        return delete;
    }

    public static f valueOf(File file) {
        MethodCollector.i(17012);
        try {
            FileReader fileReader = new FileReader(file);
            try {
                char[] cArr = new char[((int) file.length())];
                fileReader.read(cArr);
                f fVar = new f(cArr);
                fVar.AFKeystoreWrapper = file.getName();
                try {
                    fileReader.close();
                } catch (IOException e) {
                    AFLogger.valueOf((Throwable) e);
                }
                MethodCollector.o(17012);
                return fVar;
            } catch (Exception unused) {
                if (fileReader != null) {
                    try {
                        fileReader.close();
                    } catch (IOException e2) {
                        AFLogger.valueOf((Throwable) e2);
                    }
                }
                MethodCollector.o(17012);
                return null;
            } catch (Throwable th) {
                th = th;
                try {
                    fileReader.close();
                } catch (IOException e3) {
                    AFLogger.valueOf((Throwable) e3);
                }
                MethodCollector.o(17012);
                throw th;
            }
        } catch (Exception unused2) {
            MethodCollector.o(17012);
            return null;
        } catch (Throwable th2) {
            th = th2;
            MethodCollector.o(17012);
            throw th;
        }
    }

    public static List<f> valueOf(Context context) {
        ArrayList arrayList = new ArrayList();
        try {
            File file = new File(INVOKEVIRTUAL_com_appsflyer_internal_af_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir(context), "AFRequestCache");
            if (!file.exists()) {
                file.mkdir();
            } else {
                for (File file2 : file.listFiles()) {
                    StringBuilder sb = new StringBuilder("Found cached request");
                    sb.append(file2.getName());
                    AFLogger.valueOf(sb.toString());
                    arrayList.add(valueOf(file2));
                }
            }
        } catch (Exception e) {
            AFLogger.valueOf("Could not cache request", e);
        }
        return arrayList;
    }

    public static void AFKeystoreWrapper(String str, Context context) {
        File file = new File(new File(INVOKEVIRTUAL_com_appsflyer_internal_af_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir(context), "AFRequestCache"), str);
        StringBuilder sb = new StringBuilder("Deleting ");
        sb.append(str);
        sb.append(" from cache");
        AFLogger.valueOf(sb.toString());
        if (file.exists()) {
            try {
                INVOKEVIRTUAL_com_appsflyer_internal_af_com_ss_android_ugc_aweme_storage_FileLancet_delete(file);
            } catch (Exception e) {
                StringBuilder sb2 = new StringBuilder("Could not delete ");
                sb2.append(str);
                sb2.append(" from cache");
                AFLogger.valueOf(sb2.toString(), e);
            }
        }
    }
}
