package com.appsflyer.internal;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import com.appsflyer.AFExecutor;
import com.appsflyer.AFLogger;
import com.appsflyer.AppsFlyerLibCore;
import com.appsflyer.AppsFlyerProperties;
import com.appsflyer.ServerParameters;
import com.appsflyer.share.Constants;
import com.facebook.internal.AnalyticsEvents;
import com.facebook.internal.ServerProtocol;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.json.JSONObject;

public final class g {

    /* renamed from: ʽ  reason: contains not printable characters */
    public static g f2073 = null;

    /* renamed from: ˊ  reason: contains not printable characters */
    public static String[] f2074;

    /* renamed from: ˋ  reason: contains not printable characters */
    public static final int f2075 = ((int) TimeUnit.SECONDS.toMillis(2));

    /* renamed from: ˎ  reason: contains not printable characters */
    public static volatile boolean f2076;

    /* renamed from: ˏ  reason: contains not printable characters */
    public static Uri f2077 = null;

    /* renamed from: ॱ  reason: contains not printable characters */
    public static String[] f2078;

    /* renamed from: com.appsflyer.internal.g$1  reason: invalid class name */
    public class AnonymousClass1 implements Runnable {

        /* renamed from: ˊ  reason: contains not printable characters */
        public /* synthetic */ Map f2079;

        /* renamed from: ˋ  reason: contains not printable characters */
        public /* synthetic */ Context f2080;

        /* renamed from: ˏ  reason: contains not printable characters */
        public /* synthetic */ Uri f2082;

        public AnonymousClass1(Uri uri, Map map, Context context) {
            this.f2082 = uri;
            this.f2079 = map;
            this.f2080 = context;
        }

        /* renamed from: ˋ  reason: contains not printable characters */
        public static void m1230(Context context) {
            Context applicationContext = context.getApplicationContext();
            AFLogger.afInfoLog("onBecameBackground");
            AppsFlyerLibCore.getInstance().f1876 = System.currentTimeMillis();
            AFLogger.afInfoLog("callStatsBackground background call");
            WeakReference weakReference = new WeakReference(applicationContext);
            AppsFlyerLibCore instance = AppsFlyerLibCore.getInstance();
            if (weakReference.get() != null) {
                AFLogger.afInfoLog("app went to background");
                SharedPreferences r2 = AppsFlyerLibCore.m1100((Context) weakReference.get());
                AppsFlyerProperties.getInstance().saveProperties(r2);
                long j = instance.f1876 - instance.f1898;
                HashMap hashMap = new HashMap();
                String r8 = AppsFlyerLibCore.m1102(AppsFlyerProperties.AF_KEY);
                if (r8 == null) {
                    AFLogger.afWarnLog("[callStats] AppsFlyer's SDK cannot send any event without providing DevKey.");
                } else {
                    String r9 = AppsFlyerLibCore.m1102("KSAppsFlyerId");
                    if (AppsFlyerProperties.getInstance().getBoolean(AppsFlyerProperties.DEVICE_TRACKING_DISABLED, false)) {
                        hashMap.put(AppsFlyerProperties.DEVICE_TRACKING_DISABLED, ServerProtocol.DIALOG_RETURN_SCOPES_TRUE);
                    }
                    u r10 = v.m1248(((Context) weakReference.get()).getContentResolver());
                    if (r10 != null) {
                        hashMap.put("amazon_aid", r10.f2138);
                        hashMap.put("amazon_aid_limit", String.valueOf(r10.f2140));
                    }
                    String string = AppsFlyerProperties.getInstance().getString(ServerParameters.ADVERTISING_ID_PARAM);
                    if (string != null) {
                        hashMap.put(ServerParameters.ADVERTISING_ID_PARAM, string);
                    }
                    hashMap.put("app_id", ((Context) weakReference.get()).getPackageName());
                    hashMap.put("devkey", r8);
                    hashMap.put("uid", af.m1186(weakReference));
                    hashMap.put("time_in_app", String.valueOf(j / 1000));
                    hashMap.put("statType", "user_closed_app");
                    hashMap.put("platform", "Android");
                    hashMap.put("launch_counter", Integer.toString(AppsFlyerLibCore.m1108(r2, "appsFlyerCount", false)));
                    hashMap.put(AppsFlyerProperties.CHANNEL, AppsFlyerLibCore.m1103(weakReference));
                    if (r9 == null) {
                        r9 = "";
                    }
                    hashMap.put("originalAppsflyerId", r9);
                    if (instance.f1878) {
                        try {
                            ab abVar = new ab(null, instance.isTrackingStopped());
                            abVar.f1965 = new JSONObject(hashMap).toString();
                            AFLogger.afDebugLog("Running callStats task");
                            new Thread(
                            /*  JADX ERROR: Method code generation error
                                jadx.core.utils.exceptions.CodegenException: Error generate insn: 0x0126: INVOKE  
                                  (wrap: java.lang.Thread : 0x0123: CONSTRUCTOR  (r2v12 java.lang.Thread) = 
                                  (wrap: com.appsflyer.internal.ab$3 : 0x0120: CONSTRUCTOR  (r5v11 com.appsflyer.internal.ab$3) = 
                                  (r0v15 'abVar' com.appsflyer.internal.ab)
                                  (wrap: java.lang.String : 0x0118: INVOKE  (r1v11 java.lang.String) = ("https://%sstats.%s/stats") type: STATIC call: com.appsflyer.ServerConfigHandler.getUrl(java.lang.String):java.lang.String)
                                 call: com.appsflyer.internal.ab.3.<init>(com.appsflyer.internal.ab, java.lang.String):void type: CONSTRUCTOR)
                                 call: java.lang.Thread.<init>(java.lang.Runnable):void type: CONSTRUCTOR)
                                 type: VIRTUAL call: java.lang.Thread.start():void in method: com.appsflyer.internal.g.1.ￋﾋ(android.content.Context):void, file: classes.dex
                                	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:255)
                                	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:217)
                                	at jadx.core.codegen.RegionGen.makeSimpleBlock(RegionGen.java:110)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:56)
                                	at jadx.core.codegen.RegionGen.makeSimpleRegion(RegionGen.java:93)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:59)
                                	at jadx.core.codegen.RegionGen.makeRegionIndent(RegionGen.java:99)
                                	at jadx.core.codegen.RegionGen.makeTryCatch(RegionGen.java:306)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:69)
                                	at jadx.core.codegen.RegionGen.makeSimpleRegion(RegionGen.java:93)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:59)
                                	at jadx.core.codegen.RegionGen.makeRegionIndent(RegionGen.java:99)
                                	at jadx.core.codegen.RegionGen.makeIf(RegionGen.java:143)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:63)
                                	at jadx.core.codegen.RegionGen.makeSimpleRegion(RegionGen.java:93)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:59)
                                	at jadx.core.codegen.RegionGen.makeRegionIndent(RegionGen.java:99)
                                	at jadx.core.codegen.RegionGen.makeIf(RegionGen.java:157)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:63)
                                	at jadx.core.codegen.RegionGen.makeSimpleRegion(RegionGen.java:93)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:59)
                                	at jadx.core.codegen.RegionGen.makeRegionIndent(RegionGen.java:99)
                                	at jadx.core.codegen.RegionGen.makeIf(RegionGen.java:143)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:63)
                                	at jadx.core.codegen.RegionGen.makeSimpleRegion(RegionGen.java:93)
                                	at jadx.core.codegen.RegionGen.makeRegion(RegionGen.java:59)
                                	at jadx.core.codegen.MethodGen.addRegionInsns(MethodGen.java:244)
                                	at jadx.core.codegen.MethodGen.addInstructions(MethodGen.java:237)
                                	at jadx.core.codegen.ClassGen.addMethodCode(ClassGen.java:342)
                                	at jadx.core.codegen.ClassGen.addMethod(ClassGen.java:295)
                                	at jadx.core.codegen.ClassGen.lambda$addInnerClsAndMethods$2(ClassGen.java:264)
                                	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)
                                Caused by: jadx.core.utils.exceptions.CodegenException: Error generate insn: 0x0123: CONSTRUCTOR  (r2v12 java.lang.Thread) = 
                                  (wrap: com.appsflyer.internal.ab$3 : 0x0120: CONSTRUCTOR  (r5v11 com.appsflyer.internal.ab$3) = 
                                  (r0v15 'abVar' com.appsflyer.internal.ab)
                                  (wrap: java.lang.String : 0x0118: INVOKE  (r1v11 java.lang.String) = ("https://%sstats.%s/stats") type: STATIC call: com.appsflyer.ServerConfigHandler.getUrl(java.lang.String):java.lang.String)
                                 call: com.appsflyer.internal.ab.3.<init>(com.appsflyer.internal.ab, java.lang.String):void type: CONSTRUCTOR)
                                 call: java.lang.Thread.<init>(java.lang.Runnable):void type: CONSTRUCTOR in method: com.appsflyer.internal.g.1.ￋﾋ(android.content.Context):void, file: classes.dex
                                	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:255)
                                	at jadx.core.codegen.InsnGen.addWrappedArg(InsnGen.java:119)
                                	at jadx.core.codegen.InsnGen.addArg(InsnGen.java:103)
                                	at jadx.core.codegen.InsnGen.addArgDot(InsnGen.java:87)
                                	at jadx.core.codegen.InsnGen.makeInvoke(InsnGen.java:715)
                                	at jadx.core.codegen.InsnGen.makeInsnBody(InsnGen.java:367)
                                	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:249)
                                	... 34 more
                                Caused by: jadx.core.utils.exceptions.CodegenException: Error generate insn: 0x0120: CONSTRUCTOR  (r5v11 com.appsflyer.internal.ab$3) = 
                                  (r0v15 'abVar' com.appsflyer.internal.ab)
                                  (wrap: java.lang.String : 0x0118: INVOKE  (r1v11 java.lang.String) = ("https://%sstats.%s/stats") type: STATIC call: com.appsflyer.ServerConfigHandler.getUrl(java.lang.String):java.lang.String)
                                 call: com.appsflyer.internal.ab.3.<init>(com.appsflyer.internal.ab, java.lang.String):void type: CONSTRUCTOR in method: com.appsflyer.internal.g.1.ￋﾋ(android.content.Context):void, file: classes.dex
                                	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:255)
                                	at jadx.core.codegen.InsnGen.addWrappedArg(InsnGen.java:119)
                                	at jadx.core.codegen.InsnGen.addArg(InsnGen.java:103)
                                	at jadx.core.codegen.InsnGen.generateMethodArguments(InsnGen.java:806)
                                	at jadx.core.codegen.InsnGen.makeConstructor(InsnGen.java:663)
                                	at jadx.core.codegen.InsnGen.makeInsnBody(InsnGen.java:363)
                                	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:230)
                                	... 40 more
                                Caused by: jadx.core.utils.exceptions.JadxRuntimeException: Expected class to be processed at this point, class: com.appsflyer.internal.ab, state: GENERATED_AND_UNLOADED
                                	at jadx.core.dex.nodes.ClassNode.ensureProcessed(ClassNode.java:215)
                                	at jadx.core.codegen.InsnGen.makeConstructor(InsnGen.java:630)
                                	at jadx.core.codegen.InsnGen.makeInsnBody(InsnGen.java:363)
                                	at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:230)
                                	... 46 more
                                */
                            /*
                            // Method dump skipped, instructions count: 461
                            */
                            throw new UnsupportedOperationException("Method not decompiled: com.appsflyer.internal.g.AnonymousClass1.m1230(android.content.Context):void");
                        }

                        public final void run() {
                            long j;
                            HashMap hashMap = new HashMap();
                            long currentTimeMillis = System.currentTimeMillis();
                            Uri uri = null;
                            try {
                                StringBuilder sb = new StringBuilder("ESP deeplink resolving is started: ");
                                sb.append(this.f2082.toString());
                                AFLogger.afDebugLog(sb.toString());
                                HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(this.f2082.toString()).openConnection();
                                httpURLConnection.setInstanceFollowRedirects(false);
                                httpURLConnection.setReadTimeout(g.f2075);
                                httpURLConnection.setConnectTimeout(g.f2075);
                                httpURLConnection.setRequestProperty("User-agent", "Dalvik/2.1.0 (Linux; U; Android 6.0.1; Nexus 5 Build/M4B30Z)");
                                httpURLConnection.connect();
                                AFLogger.afDebugLog("ESP deeplink resolving is finished");
                                hashMap.put(AnalyticsEvents.PARAMETER_SHARE_DIALOG_CONTENT_STATUS, String.valueOf(httpURLConnection.getResponseCode()));
                                if (httpURLConnection.getResponseCode() >= 300 && httpURLConnection.getResponseCode() <= 305) {
                                    uri = Uri.parse(httpURLConnection.getHeaderField(Constants.HTTP_REDIRECT_URL_HEADER_FIELD));
                                }
                                j = System.currentTimeMillis() - currentTimeMillis;
                                httpURLConnection.disconnect();
                            } catch (Throwable th) {
                                hashMap.put("error", th.getLocalizedMessage());
                                hashMap.put(AnalyticsEvents.PARAMETER_SHARE_DIALOG_CONTENT_STATUS, "-1");
                                j = System.currentTimeMillis() - currentTimeMillis;
                                AFLogger.afErrorLog(th.getMessage(), th);
                            }
                            hashMap.put("latency", Long.toString(j));
                            if (uri != null) {
                                hashMap.put("res", uri.toString());
                            } else {
                                hashMap.put("res", "");
                            }
                            synchronized (this.f2079) {
                                this.f2079.put("af_deeplink_r", hashMap);
                                this.f2079.put("af_deeplink", this.f2082.toString());
                            }
                            g.f2076 = false;
                            if (uri == null) {
                                uri = this.f2082;
                            }
                            AppsFlyerLibCore.getInstance().handleDeepLinkCallback(this.f2080, this.f2079, uri);
                        }

                        public AnonymousClass1() {
                        }
                    }

                    /* renamed from: ˋ  reason: contains not printable characters */
                    private void m1226(Context context, Map<String, Object> map, Uri uri) {
                        String obj = uri.toString();
                        boolean z = false;
                        if (f2078 != null && !obj.contains("af_tranid=")) {
                            StringBuilder sb = new StringBuilder("Validate ESP URLs :");
                            sb.append(Arrays.asList(f2078));
                            AFLogger.afRDLog(sb.toString());
                            String[] strArr = f2078;
                            int length = strArr.length;
                            int i = 0;
                            while (true) {
                                if (i >= length) {
                                    break;
                                }
                                String str = strArr[i];
                                if (obj.contains("://".concat(String.valueOf(str)))) {
                                    AFLogger.afRDLog("Deeplink matches ESP domain: ".concat(String.valueOf(str)));
                                    z = true;
                                    break;
                                }
                                i++;
                            }
                        }
                        if (z) {
                            f2076 = true;
                            AFExecutor instance = AFExecutor.getInstance();
                            if (instance.f1848 == null) {
                                instance.f1848 = Executors.newSingleThreadScheduledExecutor(instance.f1849);
                            }
                            instance.f1848.execute(new AnonymousClass1(uri, map, context));
                        } else {
                            AppsFlyerLibCore.getInstance().handleDeepLinkCallback(context, map, uri);
                        }
                        f2077 = null;
                    }

                    /* renamed from: ˏ  reason: contains not printable characters */
                    public static g m1228() {
                        if (f2073 == null) {
                            f2073 = new g();
                        }
                        return f2073;
                    }

                    /* renamed from: ॱ  reason: contains not printable characters */
                    public final void m1229(Intent intent, Context context, Map<String, Object> map) {
                        Uri data = (intent == null || !"android.intent.action.VIEW".equals(intent.getAction())) ? null : intent.getData();
                        if (data == null) {
                            Uri uri = f2077;
                            if (uri != null) {
                                m1226(context, map, uri);
                                StringBuilder sb = new StringBuilder("using trampoline Intent fallback with URI: ");
                                sb.append(f2077.toString());
                                AFLogger.afInfoLog(sb.toString());
                            } else if (AppsFlyerLibCore.getInstance().latestDeepLink != null) {
                                StringBuilder sb2 = new StringBuilder("using Unity/plugin Intent fallback with URI: ");
                                sb2.append(AppsFlyerLibCore.getInstance().latestDeepLink.toString());
                                AFLogger.afInfoLog(sb2.toString());
                                m1226(context, map, AppsFlyerLibCore.getInstance().latestDeepLink);
                            } else {
                                AFLogger.afDebugLog("No deep link detected");
                            }
                        } else if (!intent.hasExtra("af_consumed")) {
                            intent.putExtra("af_consumed", System.currentTimeMillis());
                            m1226(context, map, data);
                        } else {
                            StringBuilder sb3 = new StringBuilder("skipping re-use of previously consumed deep link: ");
                            sb3.append(data.toString());
                            sb3.append(" w/af_consumed");
                            AFLogger.afInfoLog(sb3.toString());
                        }
                    }

                    /* renamed from: ˏ  reason: contains not printable characters */
                    public static Uri m1227(Intent intent) {
                        if (intent == null || !"android.intent.action.VIEW".equals(intent.getAction())) {
                            return null;
                        }
                        return intent.getData();
                    }
                }
