package com.bytedance.sdk.openadsdk.gqi;

import android.content.Context;
import android.os.Build;
import android.os.Process;
import android.text.TextUtils;
import android.webkit.WebView;
import com.bytedance.sdk.component.utils.sds;
import com.bytedance.sdk.component.utils.wey;
import com.bytedance.sdk.openadsdk.hlt.jnr;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes2.dex */
public class fm {
    private static boolean fm = true;
    private static final AtomicBoolean ro = new AtomicBoolean(false);

    public static void fm() {
        fm = false;
    }

    /* JADX WARN: Removed duplicated region for block: B:65:0x0091 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:71:0x0087 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:79:? A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private static void ro(Context context) throws Throwable {
        RandomAccessFile randomAccessFile;
        FileChannel channel;
        Throwable th;
        String strRo = ro();
        File file = new File(context.getDir(TextUtils.isEmpty(strRo) ? "webview" : "webview_".concat(String.valueOf(strRo)), 0).getPath(), "webview_data.lock");
        file.getAbsolutePath();
        if (!file.exists()) {
            return;
        }
        FileChannel fileChannel = null;
        FileLock fileLockTryLock = null;
        fileChannel = null;
        fileChannel = null;
        try {
            randomAccessFile = new RandomAccessFile(file, "rw");
            try {
                try {
                    channel = randomAccessFile.getChannel();
                    if (channel != null) {
                        try {
                            fileLockTryLock = channel.tryLock();
                        } catch (Exception unused) {
                            fileChannel = channel;
                            fm(file);
                            if (fileChannel != null) {
                                try {
                                    fileChannel.close();
                                } catch (Throwable th2) {
                                    th2.getMessage();
                                }
                            }
                            if (randomAccessFile != null) {
                                try {
                                    randomAccessFile.close();
                                    return;
                                } catch (Throwable th3) {
                                    th3.getMessage();
                                    return;
                                }
                            }
                            return;
                        } catch (Throwable th4) {
                            th = th4;
                            if (channel != null) {
                            }
                            if (randomAccessFile == null) {
                            }
                        }
                    }
                    if (fileLockTryLock != null) {
                        fileLockTryLock.close();
                    } else {
                        fm(file);
                    }
                    if (channel != null) {
                        try {
                            channel.close();
                        } catch (Throwable th5) {
                            th5.getMessage();
                        }
                    }
                    try {
                        randomAccessFile.close();
                    } catch (Throwable th6) {
                        th6.getMessage();
                    }
                } catch (Exception unused2) {
                }
            } catch (Throwable th7) {
                th = th7;
                channel = fileChannel;
                th = th;
                if (channel != null) {
                    try {
                        channel.close();
                    } catch (Throwable th8) {
                        th8.getMessage();
                    }
                }
                if (randomAccessFile == null) {
                    throw th;
                }
                try {
                    randomAccessFile.close();
                    throw th;
                } catch (Throwable th9) {
                    th9.getMessage();
                    throw th;
                }
            }
        } catch (Exception unused3) {
            randomAccessFile = null;
        } catch (Throwable th10) {
            th = th10;
            randomAccessFile = null;
            channel = null;
            th = th;
            if (channel != null) {
            }
            if (randomAccessFile == null) {
            }
        }
    }

    public static void fm(Context context) {
        if (context != null && fm && ro.compareAndSet(false, true)) {
            try {
                if (Build.VERSION.SDK_INT >= 28) {
                    if (sds.fm(context)) {
                        if (jnr.fm("webview_defend", false)) {
                            ro(context);
                            return;
                        }
                        return;
                    }
                    String strRo = sds.ro(context);
                    try {
                        if (TextUtils.isEmpty(strRo)) {
                            strRo = context.getPackageName() + Process.myPid();
                        }
                        WebView.setDataDirectorySuffix(strRo);
                        wey.fm("TTAD.TTMultiInitHelper", "init: WebView.setDataDirectorySuffix with ".concat(String.valueOf(strRo)));
                    } catch (IllegalStateException e10) {
                        wey.fm("TTAD.TTMultiInitHelper", "init: ", e10);
                        fm(strRo);
                    } catch (Exception e11) {
                        wey.fm("TTAD.TTMultiInitHelper", "init: ", e11);
                    }
                }
            } catch (Throwable th) {
                wey.fm("TTAD.TTMultiInitHelper", "init: ", th);
            }
        }
    }

    private static void fm(String str) {
        try {
            Method declaredMethod = Class.class.getDeclaredMethod("forName", String.class);
            Method declaredMethod2 = Class.class.getDeclaredMethod("getDeclaredField", String.class);
            declaredMethod2.setAccessible(true);
            Class cls = (Class) declaredMethod.invoke(null, "android.webkit.WebViewFactory");
            Field field = (Field) declaredMethod2.invoke(cls, "sDataDirectorySuffix");
            field.setAccessible(true);
            if (TextUtils.isEmpty((String) field.get(cls))) {
                field.set(cls, str);
            }
        } catch (Throwable unused) {
        }
    }

    private static void fm(File file) {
        fm(file, file.exists() ? file.delete() : false);
    }

    private static String ro() {
        try {
            Method declaredMethod = Class.class.getDeclaredMethod("forName", String.class);
            Method declaredMethod2 = Class.class.getDeclaredMethod("getDeclaredField", String.class);
            declaredMethod2.setAccessible(true);
            Class cls = (Class) declaredMethod.invoke(null, "android.webkit.WebViewFactory");
            return (String) ((Field) declaredMethod2.invoke(cls, "sDataDirectorySuffix")).get(cls);
        } catch (Throwable unused) {
            return null;
        }
    }

    private static void fm(File file, boolean z10) {
        if (!z10 || file.exists()) {
            return;
        }
        try {
            file.createNewFile();
        } catch (IOException e10) {
            wey.lb("TTAD.TTMultiInitHelper", e10.getMessage());
        }
    }
}
