package com.appsflyer.internal;

import X.8e1;
import X.8e2;
import X.8eG;
import X.8eL;
import X.9Vv;
import X.9Vw;
import X.9dN;
import X.9dm;
import X.9dq;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import com.appsflyer.AFLogger;
import com.appsflyer.deeplink.DeepLink;
import com.appsflyer.deeplink.DeepLinkListener;
import com.appsflyer.deeplink.DeepLinkResult;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONException;
import org.json.JSONObject;

public final class i {
    public static Intent AFInAppEventParameterName;
    public static final int AFInAppEventType = ((int) TimeUnit.SECONDS.toMillis(2));
    public static String[] AFKeystoreWrapper;
    public static i AFVersionDeclaration;
    public static String[] AppsFlyer2dXConversionCallback;
    public static volatile boolean values;
    public String AFLogger$LogLevel;
    public Map<String, String> getLevel;
    public List<List<String>> init = new ArrayList();
    public DeepLinkListener valueOf;

    public static i values() {
        if (AFVersionDeclaration == null) {
            AFVersionDeclaration = new i();
        }
        return AFVersionDeclaration;
    }

    static {
        Covode.recordClassIndex(2701);
    }

    public static Bundle INVOKEVIRTUAL_com_appsflyer_internal_i_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(Intent intent) {
        try {
            return intent.getExtras();
        } catch (Exception unused) {
            return null;
        }
    }

    public static Uri AFKeystoreWrapper(Intent intent) {
        if (intent == null || !"android.intent.action.VIEW".equals(intent.getAction())) {
            return null;
        }
        return intent.getData();
    }

    public static ScheduledExecutorService INVOKESTATIC_com_appsflyer_internal_i_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
        8e1 LIZ = 8e2.LIZ(8eG.SCHEDULED);
        LIZ.LIZJ = 1;
        LIZ.LJI = threadFactory;
        return (ScheduledExecutorService) 8eL.LIZ(LIZ.LIZ());
    }

    public static boolean AFKeystoreWrapper(String str) {
        if (AFKeystoreWrapper == null || str.contains("af_tranid=")) {
            return false;
        }
        StringBuilder sb = new StringBuilder("Validate if link ");
        sb.append(str);
        sb.append(" belongs to ESP domains: ");
        sb.append(Arrays.asList(AFKeystoreWrapper));
        AFLogger.AFInAppEventParameterName(sb.toString());
        try {
            return Arrays.asList(AFKeystoreWrapper).contains(new URL(str).getHost());
        } catch (MalformedURLException unused) {
            return false;
        }
    }

    private Uri valueOf(Object obj, Iterator<String> it) {
        while (obj != JSONObject.NULL) {
            if (!it.hasNext()) {
                Uri parse = Uri.parse(obj.toString());
                if (parse == null || parse.getScheme() == null || parse.getHost() == null) {
                    return null;
                }
                return parse;
            }
            try {
                obj = new JSONObject(obj.toString()).get(it.next());
            } catch (JSONException unused) {
                return null;
            }
        }
        return null;
    }

    public final void AFKeystoreWrapper(final Context context, final Map<String, Object> map, final Uri uri) {
        if (AFKeystoreWrapper(uri.toString())) {
            values = true;
            if (k.values == null) {
                k.values = new k();
            }
            k kVar = k.values;
            if (kVar.AFInAppEventParameterName == null) {
                kVar.AFInAppEventParameterName = INVOKESTATIC_com_appsflyer_internal_i_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newSingleThreadScheduledExecutor(kVar.AFInAppEventType);
            }
            kVar.AFInAppEventParameterName.execute(new Runnable() {
                static {
                    Covode.recordClassIndex(2702);
                }

                public final void run() {
                    Number number;
                    Object obj;
                    String str;
                    int i;
                    Uri uri;
                    MethodCollector.i(14815);
                    long currentTimeMillis = System.currentTimeMillis();
                    String obj2 = uri.toString();
                    ArrayList arrayList = new ArrayList();
                    int i2 = 0;
                    do {
                        Map<String, Object> values2 = values(Uri.parse(obj2));
                        obj2 = (String) values2.get("res");
                        number = (Number) values2.get("status");
                        obj = values2.get("error");
                        if (obj2 == null || !i.AFKeystoreWrapper(obj2)) {
                            HashMap hashMap = new HashMap();
                        } else {
                            if (i2 < 4) {
                                arrayList.add(obj2);
                            }
                            i2++;
                        }
                    } while (i2 < 5);
                    HashMap hashMap2 = new HashMap();
                    if (obj2 != null) {
                        str = obj2;
                    } else {
                        str = "";
                    }
                    hashMap2.put("res", str);
                    if (number != null) {
                        i = number.intValue();
                    } else {
                        i = -1;
                    }
                    hashMap2.put("status", Integer.valueOf(i));
                    if (obj != null) {
                        hashMap2.put("error", obj);
                    }
                    if (!arrayList.isEmpty()) {
                        hashMap2.put("redirects", arrayList);
                    }
                    hashMap2.put("latency", Long.valueOf(System.currentTimeMillis() - currentTimeMillis));
                    synchronized (map) {
                        try {
                            map.put("af_deeplink_r", hashMap2);
                            map.put("af_deeplink", uri.toString());
                        } catch (Throwable th) {
                            MethodCollector.o(14815);
                            throw th;
                        }
                    }
                    ai valueOf2 = ai.valueOf();
                    Context context = context;
                    Map map = map;
                    if (obj2 != null) {
                        uri = Uri.parse(obj2);
                    } else {
                        uri = uri;
                    }
                    valueOf2.valueOf(context, (Map<String, Object>) map, uri);
                    i.values = false;
                    MethodCollector.o(14815);
                }

                public static URLConnection INVOKEVIRTUAL_com_appsflyer_internal_i$2_com_ss_android_ugc_aweme_net_lancet_UrlConnectionLancet_openConnection(URL url) {
                    9dq r0;
                    9dq r02;
                    9Vv LJIIL = 9dN.LJ.LJIIL(new 9Vv(url, (WebView) null, (JSONObject) null, 9Vw.CONTINUE));
                    if (LJIIL.LJFF == 9Vw.INTERCEPT && LJIIL.LIZIZ != null) {
                        URLConnection uRLConnection = (URLConnection) LJIIL.LIZIZ;
                        if (uRLConnection instanceof HttpsURLConnection) {
                            r02 = new 9dq((HttpsURLConnection) uRLConnection);
                        } else if (!(uRLConnection instanceof HttpURLConnection)) {
                            return uRLConnection;
                        } else {
                            r02 = new 9dm((HttpURLConnection) uRLConnection);
                        }
                        return r02;
                    } else if (LJIIL.LJFF != 9Vw.EXCEPTION || LJIIL.LJ == null) {
                        URLConnection openConnection = url.openConnection();
                        if (openConnection instanceof HttpsURLConnection) {
                            r0 = new 9dq((HttpsURLConnection) openConnection);
                        } else if (!(openConnection instanceof HttpURLConnection)) {
                            return openConnection;
                        } else {
                            r0 = new 9dm((HttpURLConnection) openConnection);
                        }
                        return r0;
                    } else {
                        throw LJIIL.LJ;
                    }
                }

                public static Map<String, Object> values(Uri uri) {
                    HashMap hashMap = new HashMap();
                    try {
                        StringBuilder sb = new StringBuilder("ESP deeplink resolving is started: ");
                        sb.append(uri.toString());
                        AFLogger.AFInAppEventType(sb.toString());
                        HttpURLConnection httpURLConnection = (HttpURLConnection) INVOKEVIRTUAL_com_appsflyer_internal_i$2_com_ss_android_ugc_aweme_net_lancet_UrlConnectionLancet_openConnection(new URL(uri.toString()));
                        httpURLConnection.setInstanceFollowRedirects(false);
                        httpURLConnection.setReadTimeout(i.AFInAppEventType);
                        httpURLConnection.setConnectTimeout(i.AFInAppEventType);
                        httpURLConnection.setRequestProperty("User-agent", "Dalvik/2.1.0 (Linux; U; Android 6.0.1; Nexus 5 Build/M4B30Z)");
                        httpURLConnection.setRequestProperty("af-esp", "6.4.0");
                        int responseCode = httpURLConnection.getResponseCode();
                        hashMap.put("status", Integer.valueOf(responseCode));
                        if (300 <= responseCode && responseCode <= 305) {
                            hashMap.put("res", httpURLConnection.getHeaderField("Location"));
                        }
                        httpURLConnection.disconnect();
                        AFLogger.AFInAppEventType("ESP deeplink resolving is finished");
                    } catch (Throwable th) {
                        hashMap.put("error", th.getLocalizedMessage());
                        AFLogger.valueOf(th.getMessage(), th);
                    }
                    return hashMap;
                }
            });
        } else {
            ai.valueOf().valueOf(context, map, uri);
        }
        AFInAppEventParameterName = null;
    }

    private boolean AFInAppEventParameterName(Intent intent, Context context, Map<String, Object> map) {
        Uri uri;
        Uri uri2;
        String string;
        Uri valueOf2;
        Uri uri3 = null;
        if (intent == null || !"android.intent.action.VIEW".equals(intent.getAction())) {
            uri = null;
        } else {
            uri = intent.getData();
        }
        Intent intent2 = AFInAppEventParameterName;
        if (intent2 == null || !"android.intent.action.VIEW".equals(intent2.getAction())) {
            uri2 = null;
        } else {
            uri2 = intent2.getData();
        }
        Bundle INVOKEVIRTUAL_com_appsflyer_internal_i_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras = INVOKEVIRTUAL_com_appsflyer_internal_i_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(intent);
        List<List<String>> list = this.init;
        if (list != null && !list.isEmpty() && INVOKEVIRTUAL_com_appsflyer_internal_i_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras != null) {
            Iterator<List<String>> it = this.init.iterator();
            while (true) {
                if (it.hasNext()) {
                    List next = it.next();
                    if (next != null) {
                        Iterator it2 = next.iterator();
                        if (!(!it2.hasNext() || (string = INVOKEVIRTUAL_com_appsflyer_internal_i_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras.getString((String) it2.next())) == null || (valueOf2 = valueOf(string, it2)) == null)) {
                            StringBuilder sb = new StringBuilder("Found deeplink in push payload at ");
                            sb.append(next.toString());
                            AFLogger.AFInAppEventType(sb.toString());
                            ai.valueOf(map).put("payloadKey", this.init);
                            uri3 = valueOf2;
                            break;
                        }
                    }
                } else {
                    break;
                }
            }
        }
        if (uri != null) {
            if (!intent.hasExtra("af_consumed")) {
                intent.putExtra("af_consumed", System.currentTimeMillis());
                AFKeystoreWrapper(context, map, uri);
                return true;
            }
            StringBuilder sb2 = new StringBuilder("skipping re-use of previously consumed deep link: ");
            sb2.append(uri.toString());
            sb2.append(" w/af_consumed");
            AFLogger.valueOf(sb2.toString());
            return false;
        } else if (uri2 != null) {
            if (!AFInAppEventParameterName.hasExtra("af_consumed")) {
                AFInAppEventParameterName.putExtra("af_consumed", System.currentTimeMillis());
                AFKeystoreWrapper(context, map, uri2);
                return true;
            }
            StringBuilder sb3 = new StringBuilder("skipping re-use of previously consumed trampoline deep link: ");
            sb3.append(uri2.toString());
            sb3.append(" w/af_consumed");
            AFLogger.valueOf(sb3.toString());
            return false;
        } else if (uri3 == null) {
            AFLogger.AFInAppEventType("No deep link detected");
            return false;
        } else if (!intent.hasExtra("af_consumed")) {
            intent.putExtra("af_consumed", System.currentTimeMillis());
            AFKeystoreWrapper(context, map, uri3);
            return true;
        } else {
            StringBuilder sb4 = new StringBuilder("skipping re-use of previously consumed deep link from push: ");
            sb4.append(uri3.toString());
            sb4.append(" w/af_consumed");
            AFLogger.valueOf(sb4.toString());
            return false;
        }
    }

    public final void AFKeystoreWrapper(Map<String, Object> map, au auVar, Intent intent, Context context) {
        DeepLinkResult.Error error;
        SharedPreferences values2 = ai.values(context);
        if (!AFInAppEventParameterName(intent, context, map) && this.valueOf != null) {
            ai.valueOf();
            if (ai.AFInAppEventParameterName(values2, "appsFlyerCount", false) == 0 && !values2.getBoolean("ddl_sent", false)) {
                ar arVar = new ar(context, auVar);
                AFLogger.AFInAppEventType("[DDL] start");
                FutureTask futureTask = new FutureTask(new Callable<DeepLinkResult>() {
                    static {
                        /*
                            r0 = 2622(0xa3e, float:3.674E-42)
                            com.bytedance.covode.number.Covode.recordClassIndex(r0)
                            return
                        */
                        throw new UnsupportedOperationException("Method not decompiled: com.appsflyer.internal.ar.AnonymousClass5.<clinit>():void");
                    }

                    public final /* synthetic */ java.lang.Object call(
/*
Method generation error in method: com.appsflyer.internal.ar.5.call():java.lang.Object, dex: classes.dex
                    jadx.core.utils.exceptions.JadxRuntimeException: Method args not loaded: com.appsflyer.internal.ar.5.call():java.lang.Object, class status: UNLOADED
                    	at jadx.core.dex.nodes.MethodNode.getArgRegs(MethodNode.java:278)
                    	at jadx.core.codegen.MethodGen.addDefinition(MethodGen.java:116)
                    	at jadx.core.codegen.ClassGen.addMethodCode(ClassGen.java:313)
                    	at jadx.core.codegen.ClassGen.addMethod(ClassGen.java:271)
                    	at jadx.core.codegen.ClassGen.lambda$addInnerClsAndMethods$2(ClassGen.java:240)
                    	at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
                    	at java.util.ArrayList.forEach(ArrayList.java:1259)
                    	at java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:395)
                    	at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
                    	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:483)
                    	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
                    	at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
                    	at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
                    	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
                    	at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:485)
                    	at jadx.core.codegen.ClassGen.addInnerClsAndMethods(ClassGen.java:236)
                    	at jadx.core.codegen.ClassGen.addClassBody(ClassGen.java:227)
                    	at jadx.core.codegen.InsnGen.inlineAnonymousConstructor(InsnGen.java:676)
                    	at jadx.core.codegen.InsnGen.makeConstructor(InsnGen.java:607)
                    	at jadx.core.codegen.InsnGen.makeInsnBody(InsnGen.java:364)
                    	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:231)
                    	at jadx.core.codegen.InsnGen.addWrappedArg(InsnGen.java:123)
                    	at jadx.core.codegen.InsnGen.addArg(InsnGen.java:107)
                    	at jadx.core.codegen.InsnGen.generateMethodArguments(InsnGen.java:787)
                    	at jadx.core.codegen.InsnGen.makeConstructor(InsnGen.java:640)
                    	at jadx.core.codegen.InsnGen.makeInsnBody(InsnGen.java:364)
                    	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:250)
                    	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:221)
                    	at jadx.core.codegen.RegionGen.makeSimpleBlock(RegionGen.java:109)
                    	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:55)
                    	at jadx.core.codegen.RegionGen.makeSimpleRegion(RegionGen.java:92)
                    	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:58)
                    	at jadx.core.codegen.RegionGen.makeRegionIndent(RegionGen.java:98)
                    	at jadx.core.codegen.RegionGen.makeIf(RegionGen.java:142)
                    	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:62)
                    	at jadx.core.codegen.RegionGen.makeSimpleRegion(RegionGen.java:92)
                    	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:58)
                    	at jadx.core.codegen.RegionGen.makeRegionIndent(RegionGen.java:98)
                    	at jadx.core.codegen.RegionGen.makeIf(RegionGen.java:142)
                    	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:62)
                    	at jadx.core.codegen.RegionGen.makeSimpleRegion(RegionGen.java:92)
                    	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:58)
                    	at jadx.core.codegen.MethodGen.addRegionInsns(MethodGen.java:211)
                    	at jadx.core.codegen.MethodGen.addInstructions(MethodGen.java:204)
                    	at jadx.core.codegen.ClassGen.addMethodCode(ClassGen.java:318)
                    	at jadx.core.codegen.ClassGen.addMethod(ClassGen.java:271)
                    	at jadx.core.codegen.ClassGen.lambda$addInnerClsAndMethods$2(ClassGen.java:240)
                    	at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
                    	at java.util.ArrayList.forEach(ArrayList.java:1259)
                    	at java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:395)
                    	at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
                    	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:483)
                    	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
                    	at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
                    	at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
                    	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
                    	at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:485)
                    	at jadx.core.codegen.ClassGen.addInnerClsAndMethods(ClassGen.java:236)
                    	at jadx.core.codegen.ClassGen.addClassBody(ClassGen.java:227)
                    	at jadx.core.codegen.ClassGen.addClassCode(ClassGen.java:112)
                    	at jadx.core.codegen.ClassGen.makeClass(ClassGen.java:78)
                    	at jadx.core.codegen.CodeGen.wrapCodeGen(CodeGen.java:44)
                    	at jadx.core.codegen.CodeGen.generateJavaCode(CodeGen.java:33)
                    	at jadx.core.codegen.CodeGen.generate(CodeGen.java:21)
                    	at jadx.core.ProcessClass.generateCode(ProcessClass.java:61)
                    	at jadx.core.dex.nodes.ClassNode.decompile(ClassNode.java:273)
                    
*/
                });
                new Thread(futureTask).start();
                try {
                    arVar.valueOf((DeepLinkResult) futureTask.get(ar.onInstallConversionDataLoadedNative, TimeUnit.MILLISECONDS));
                } catch (InterruptedException | ExecutionException e) {
                    AFLogger.values("[DDL] Error occurred", e);
                    if (e.getCause() instanceof IOException) {
                        error = DeepLinkResult.Error.NETWORK;
                    } else {
                        error = DeepLinkResult.Error.UNEXPECTED;
                    }
                    arVar.valueOf(new DeepLinkResult((DeepLink) null, error));
                } catch (TimeoutException unused) {
                    StringBuilder sb = new StringBuilder("[DDL] Timeout, didn't manage to find deferred deep link after ");
                    sb.append(arVar.onInstallConversionFailureNative);
                    sb.append(" attempt(s) within ");
                    sb.append(ar.onInstallConversionDataLoadedNative);
                    sb.append(" milliseconds");
                    AFLogger.AFInAppEventType(sb.toString());
                    arVar.valueOf(new DeepLinkResult((DeepLink) null, DeepLinkResult.Error.TIMEOUT));
                }
            }
        }
        values2.edit().putBoolean("ddl_sent", true).apply();
    }
}
