package com.appsflyer.internal;

import X.2FV;
import X.2He;
import X.3GY;
import X.7lW;
import X.7s1;
import X.8e1;
import X.8e2;
import X.8eG;
import X.8eL;
import X.8jA;
import android.content.ClipboardManager;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Pair;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.lang.reflect.Field;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

public final class ab {
    public static final BitSet AFLogger$LogLevel;
    public static final Handler AppsFlyer2dXConversionCallback = new Handler(Looper.getMainLooper());
    public static volatile ab init;
    public final Object AFInAppEventParameterName = new Object();
    public final Handler AFInAppEventType;
    public final Runnable AFKeystoreWrapper;
    public final Executor AFVersionDeclaration;
    public final Runnable getLevel;
    public final SensorManager onAppOpenAttributionNative;
    public long onAttributionFailure;
    public final Map<w, Map<String, Object>> onAttributionFailureNative;
    public final Runnable onDeepLinking;
    public final Map<w, w> onDeepLinkingNative;
    public int onInstallConversionDataLoadedNative;
    public boolean onInstallConversionFailureNative;
    public final Runnable valueOf;
    public boolean values;

    public static ExecutorService INVOKESTATIC_com_appsflyer_internal_ab_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newSingleThreadExecutor() {
        8e1 LIZ = 8e2.LIZ(8eG.FIXED);
        LIZ.LIZJ = 1;
        return 8eL.LIZ(LIZ.LIZ());
    }

    static {
        Covode.recordClassIndex(2574);
        BitSet bitSet = new BitSet(6);
        AFLogger$LogLevel = bitSet;
        bitSet.set(1);
        bitSet.set(2);
        bitSet.set(4);
    }

    public final List<Map<String, Object>> AFInAppEventType() {
        for (w values2 : this.onDeepLinkingNative.values()) {
            values2.values(this.onAttributionFailureNative, true);
        }
        Map<w, Map<String, Object>> map = this.onAttributionFailureNative;
        if (map == null || map.isEmpty()) {
            return new CopyOnWriteArrayList(Collections.emptyList());
        }
        return new CopyOnWriteArrayList(this.onAttributionFailureNative.values());
    }

    public final void valueOf() {
        long currentTimeMillis = System.currentTimeMillis();
        long j = this.onAttributionFailure;
        if (j != 0) {
            this.onInstallConversionDataLoadedNative++;
            if (j - currentTimeMillis < 500) {
                this.AFInAppEventType.removeCallbacks(this.AFKeystoreWrapper);
                this.AFInAppEventType.post(this.valueOf);
            }
        } else {
            this.AFInAppEventType.post(this.getLevel);
            this.AFInAppEventType.post(this.valueOf);
        }
        this.onAttributionFailure = currentTimeMillis;
    }

    public final List<Map<String, Object>> values() {
        MethodCollector.i(12745);
        synchronized (this.AFInAppEventParameterName) {
            try {
                if (!this.onDeepLinkingNative.isEmpty() && this.onInstallConversionFailureNative) {
                    for (w values2 : this.onDeepLinkingNative.values()) {
                        values2.values(this.onAttributionFailureNative, false);
                    }
                }
                if (this.onAttributionFailureNative.isEmpty()) {
                    CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList(Collections.emptyList());
                    return copyOnWriteArrayList;
                }
                CopyOnWriteArrayList copyOnWriteArrayList2 = new CopyOnWriteArrayList(this.onAttributionFailureNative.values());
                MethodCollector.o(12745);
                return copyOnWriteArrayList2;
            } finally {
                MethodCollector.o(12745);
            }
        }
    }

    public static boolean AFInAppEventParameterName(int i) {
        if (i < 0 || !AFLogger$LogLevel.get(i)) {
            return false;
        }
        return true;
    }

    public static Context INVOKEVIRTUAL_com_appsflyer_internal_ab_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(Context context) {
        Context applicationContext = context.getApplicationContext();
        if (7lW.LIZJ && applicationContext == null) {
            return 7lW.LIZ;
        }
        return applicationContext;
    }

    public static ab AFKeystoreWrapper(Context context) {
        if (init != null) {
            return init;
        }
        return AFInAppEventParameterName((SensorManager) INVOKEVIRTUAL_com_appsflyer_internal_ab_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(INVOKEVIRTUAL_com_appsflyer_internal_ab_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(context), "sensor"), AppsFlyer2dXConversionCallback);
    }

    public static ab AFInAppEventParameterName(SensorManager sensorManager, Handler handler) {
        MethodCollector.i(12483);
        if (init == null) {
            synchronized (ab.class) {
                try {
                    if (init == null) {
                        init = new ab(sensorManager, handler);
                    }
                } catch (Throwable th) {
                    MethodCollector.o(12483);
                    throw th;
                }
            }
        }
        ab abVar = init;
        MethodCollector.o(12483);
        return abVar;
    }

    public ab(SensorManager sensorManager, Handler handler) {
        BitSet bitSet = AFLogger$LogLevel;
        this.onDeepLinkingNative = new HashMap(bitSet.size());
        this.onAttributionFailureNative = new ConcurrentHashMap(bitSet.size());
        this.valueOf = new Runnable() {
            static {
                Covode.recordClassIndex(2578);
            }

            public final void run() {
                MethodCollector.i(15617);
                synchronized (ab.this.AFInAppEventParameterName) {
                    try {
                        ab abVar = ab.this;
                        abVar.AFVersionDeclaration.execute(new Runnable() {
                            static {
                                Covode.recordClassIndex(2577);
                            }

                            public static List com_appsflyer_internal_ab$3_android_hardware_SensorManager_getSensorList(SensorManager sensorManager, int i) {
                                3GY.LIZ(100702);
                                SensorManager sensorManager2 = sensorManager;
                                Pair LIZ = 3GY.LIZ((Object) null, sensorManager2, new Object[]{Integer.valueOf(i)}, 100702, "java.util.List", false, -736586939, 2);
                                return ((Boolean) LIZ.first).booleanValue() ? (List) LIZ.second : sensorManager2.getSensorList(i);
                            }

                            public final void run() {
                                try {
                                    for (Sensor sensor : com_appsflyer_internal_ab$3_android_hardware_SensorManager_getSensorList(ab.this.onAppOpenAttributionNative, -1)) {
                                        if (ab.AFInAppEventParameterName(sensor.getType())) {
                                            w wVar = new w(sensor, ab.this.AFVersionDeclaration);
                                            if (!ab.this.onDeepLinkingNative.containsKey(wVar)) {
                                                ab.this.onDeepLinkingNative.put(wVar, wVar);
                                            }
                                            ab.this.onAppOpenAttributionNative.registerListener((SensorEventListener) ab.this.onDeepLinkingNative.get(wVar), sensor, 0);
                                        }
                                    }
                                } catch (Throwable unused) {
                                }
                                boolean unused2 = ab.this.onInstallConversionFailureNative = true;
                            }
                        });
                        ab.this.AFInAppEventType.postDelayed(ab.this.onDeepLinking, 100);
                        ab.this.values = true;
                    } finally {
                        MethodCollector.o(15617);
                    }
                }
            }
        };
        this.AFKeystoreWrapper = new Runnable() {
            static {
                Covode.recordClassIndex(2579);
            }

            public final void run() {
                MethodCollector.i(17823);
                synchronized (ab.this.AFInAppEventParameterName) {
                    try {
                        ab abVar = ab.this;
                        abVar.AFVersionDeclaration.execute(new Runnable() {
                            static {
                                Covode.recordClassIndex(2580);
                            }

                            public final void run() {
                                try {
                                    if (!ab.this.onDeepLinkingNative.isEmpty()) {
                                        for (w wVar : ab.this.onDeepLinkingNative.values()) {
                                            ab.this.onAppOpenAttributionNative.unregisterListener(wVar);
                                            wVar.values((Map<w, Map<String, Object>>) ab.this.onAttributionFailureNative, true);
                                        }
                                    }
                                } catch (Throwable unused) {
                                }
                                int unused2 = ab.this.onInstallConversionDataLoadedNative = 0;
                                boolean unused3 = ab.this.onInstallConversionFailureNative = false;
                            }
                        });
                    } finally {
                        MethodCollector.o(17823);
                    }
                }
            }
        };
        this.getLevel = new Runnable() {
            static {
                Covode.recordClassIndex(2575);
            }

            public final void run() {
                MethodCollector.i(15997);
                synchronized (ab.this.AFInAppEventParameterName) {
                    try {
                        if (ab.this.values) {
                            ab.this.AFInAppEventType.removeCallbacks(ab.this.valueOf);
                            ab.this.AFInAppEventType.removeCallbacks(ab.this.AFKeystoreWrapper);
                            ab abVar = ab.this;
                            abVar.AFVersionDeclaration.execute(new Runnable() {
                                static {
                                    Covode.recordClassIndex(2580);
                                }

                                public final void run() {
                                    try {
                                        if (!ab.this.onDeepLinkingNative.isEmpty()) {
                                            for (w wVar : ab.this.onDeepLinkingNative.values()) {
                                                ab.this.onAppOpenAttributionNative.unregisterListener(wVar);
                                                wVar.values((Map<w, Map<String, Object>>) ab.this.onAttributionFailureNative, true);
                                            }
                                        }
                                    } catch (Throwable unused) {
                                    }
                                    int unused2 = ab.this.onInstallConversionDataLoadedNative = 0;
                                    boolean unused3 = ab.this.onInstallConversionFailureNative = false;
                                }
                            });
                            ab.this.values = false;
                        }
                    } finally {
                        MethodCollector.o(15997);
                    }
                }
            }
        };
        this.onInstallConversionDataLoadedNative = 1;
        this.onAttributionFailure = 0;
        this.onDeepLinking = new Runnable() {
            static {
                Covode.recordClassIndex(2576);
            }

            public final void run() {
                MethodCollector.i(15809);
                synchronized (ab.this.AFInAppEventParameterName) {
                    try {
                        if (ab.this.onInstallConversionDataLoadedNative == 0) {
                            int unused = ab.this.onInstallConversionDataLoadedNative = 1;
                        }
                        ab.this.AFInAppEventType.postDelayed(ab.this.AFKeystoreWrapper, ((long) ab.this.onInstallConversionDataLoadedNative) * 500);
                    } finally {
                        MethodCollector.o(15809);
                    }
                }
            }
        };
        this.AFVersionDeclaration = INVOKESTATIC_com_appsflyer_internal_ab_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newSingleThreadExecutor();
        this.onAppOpenAttributionNative = sensorManager;
        this.AFInAppEventType = handler;
    }

    public static Object INVOKEVIRTUAL_com_appsflyer_internal_ab_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(Context context, String str) {
        Object obj;
        MethodCollector.i(17320);
        if (Build.VERSION.SDK_INT > 27 || !"clipboard".equals(str)) {
            if (!2FV.LIZIZ && "connectivity".equals(str)) {
                try {
                    new 7s1().LIZ();
                    2FV.LIZIZ = true;
                    obj = context.getSystemService(str);
                } catch (Throwable unused) {
                }
            }
            obj = context.getSystemService(str);
        } else if (2FV.LIZ) {
            synchronized (ClipboardManager.class) {
                try {
                    obj = context.getSystemService(str);
                    if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
                        Field declaredField = ClipboardManager.class.getDeclaredField("mHandler");
                        declaredField.setAccessible(true);
                        declaredField.set(obj, new 2He((Handler) declaredField.get(obj)));
                    }
                } catch (Exception e) {
                    8jA.LIZ(e, "ClipboardManager Handler Reflect Fail");
                } catch (Throwable th) {
                    MethodCollector.o(17320);
                    throw th;
                }
                2FV.LIZ = false;
            }
        } else {
            obj = context.getSystemService(str);
        }
        MethodCollector.o(17320);
        return obj;
    }
}
