package com.appsflyer.internal;

import X.4Ms;
import X.4N2;
import X.8Kc;
import X.8r0;
import X.8r1;
import android.content.Context;
import android.content.SharedPreferences;
import com.appsflyer.AFLogger;
import com.appsflyer.AppsFlyerProperties;
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.lang.ref.WeakReference;
import java.security.SecureRandom;

public final class ak {
    public static String AFKeystoreWrapper;

    static {
        Covode.recordClassIndex(2607);
    }

    public static File INVOKEVIRTUAL_com_appsflyer_internal_ak_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;
    }

    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v0, resolved type: java.io.RandomAccessFile} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v5, resolved type: java.io.RandomAccessFile} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r2v0, resolved type: byte[]} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r2v1, resolved type: byte[]} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v8, resolved type: java.io.RandomAccessFile} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r2v2, resolved type: java.io.RandomAccessFile} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v9, resolved type: java.io.RandomAccessFile} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v10, resolved type: byte[]} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v14, resolved type: java.io.RandomAccessFile} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v15, resolved type: java.io.RandomAccessFile} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v16, resolved type: java.io.RandomAccessFile} */
    /* JADX WARNING: Multi-variable type inference failed */
    /* JADX WARNING: Removed duplicated region for block: B:15:0x0028 A[SYNTHETIC, Splitter:B:15:0x0028] */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x003e  */
    /* JADX WARNING: Removed duplicated region for block: B:31:0x0048 A[SYNTHETIC, Splitter:B:31:0x0048] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static java.lang.String AFInAppEventParameterName(java.io.File r6) {
        /*
            java.lang.String r5 = "Exception while trying to close the InstallationFile"
            r4 = 0
            java.io.RandomAccessFile r3 = new java.io.RandomAccessFile     // Catch:{ IOException -> 0x001f }
            java.lang.String r0 = "r"
            r3.<init>(r6, r0)     // Catch:{ IOException -> 0x001f }
            long r1 = r3.length()     // Catch:{ IOException -> 0x001b, all -> 0x0044 }
            int r0 = (int) r1     // Catch:{ IOException -> 0x001b, all -> 0x0044 }
            byte[] r4 = new byte[r0]     // Catch:{ IOException -> 0x001b, all -> 0x0044 }
            r3.readFully(r4)     // Catch:{ IOException -> 0x001b, all -> 0x0044 }
            r3.close()     // Catch:{ IOException -> 0x001b, all -> 0x0044 }
            r3.close()     // Catch:{ IOException -> 0x0032 }
            goto L_0x0036
        L_0x001b:
            r1 = move-exception
            r2 = r4
            r4 = r3
            goto L_0x0021
        L_0x001f:
            r1 = move-exception
            r2 = r4
        L_0x0021:
            java.lang.String r0 = "Exception while reading InstallationFile: "
            com.appsflyer.AFLogger.valueOf(r0, r1)     // Catch:{ all -> 0x0042 }
            if (r4 == 0) goto L_0x0030
            r4.close()     // Catch:{ IOException -> 0x002c }
            goto L_0x0030
        L_0x002c:
            r0 = move-exception
            com.appsflyer.AFLogger.valueOf(r5, r0)
        L_0x0030:
            r4 = r2
            goto L_0x0036
        L_0x0032:
            r0 = move-exception
            com.appsflyer.AFLogger.valueOf(r5, r0)
        L_0x0036:
            java.lang.String r1 = new java.lang.String
            if (r4 == 0) goto L_0x003e
        L_0x003a:
            r1.<init>(r4)
            return r1
        L_0x003e:
            r0 = 0
            byte[] r4 = new byte[r0]
            goto L_0x003a
        L_0x0042:
            r1 = move-exception
            goto L_0x0046
        L_0x0044:
            r1 = move-exception
            r4 = r3
        L_0x0046:
            if (r4 == 0) goto L_0x0050
            r4.close()     // Catch:{ IOException -> 0x004c }
            goto L_0x0050
        L_0x004c:
            r0 = move-exception
            com.appsflyer.AFLogger.valueOf(r5, r0)
        L_0x0050:
            throw r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.appsflyer.internal.ak.AFInAppEventParameterName(java.io.File):java.lang.String");
    }

    public static boolean INVOKEVIRTUAL_com_appsflyer_internal_ak_com_ss_android_ugc_aweme_storage_FileLancet_delete(File file) {
        8r1 r3;
        MethodCollector.i(14577);
        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(14577);
                return false;
            }
        } catch (Throwable unused) {
        }
        boolean delete = file.delete();
        MethodCollector.o(14577);
        return delete;
    }

    public static synchronized String AFInAppEventParameterName(WeakReference<Context> weakReference) {
        synchronized (ak.class) {
            MethodCollector.i(14371);
            if (weakReference.get() == null) {
                String str = AFKeystoreWrapper;
                MethodCollector.o(14371);
                return str;
            }
            if (AFKeystoreWrapper == null) {
                String str2 = null;
                if (weakReference.get() != null) {
                    str2 = ai.values(weakReference.get()).getString("AF_INSTALLATION", (String) null);
                }
                if (str2 != null) {
                    AFKeystoreWrapper = str2;
                } else {
                    try {
                        File file = new File(INVOKEVIRTUAL_com_appsflyer_internal_ak_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir(weakReference.get()), "AF_INSTALLATION");
                        if (!file.exists()) {
                            long currentTimeMillis = System.currentTimeMillis();
                            StringBuilder sb = new StringBuilder();
                            sb.append(currentTimeMillis);
                            sb.append("-");
                            sb.append(Math.abs(new SecureRandom().nextLong()));
                            AFKeystoreWrapper = sb.toString();
                        } else {
                            AFKeystoreWrapper = AFInAppEventParameterName(file);
                            INVOKEVIRTUAL_com_appsflyer_internal_ak_com_ss_android_ugc_aweme_storage_FileLancet_delete(file);
                        }
                        String str3 = AFKeystoreWrapper;
                        SharedPreferences.Editor edit = ai.values(weakReference.get()).edit();
                        edit.putString("AF_INSTALLATION", str3);
                        edit.apply();
                    } catch (Exception e) {
                        AFLogger.valueOf("Error getting AF unique ID", e);
                    }
                }
                if (AFKeystoreWrapper != null) {
                    AppsFlyerProperties.getInstance().set("uid", AFKeystoreWrapper);
                }
            }
            String str4 = AFKeystoreWrapper;
            MethodCollector.o(14371);
            return str4;
        }
    }
}
