package com.appsflyer.internal;

import X.7lW;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.appsflyer.AFLogger;
import com.bytedance.covode.number.Covode;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executor;

public final class ag {
    public static b valueOf;
    public static long values = 500;

    public interface b {
        static {
            Covode.recordClassIndex(2591);
        }

        void AFKeystoreWrapper(Activity activity);

        void valueOf(Context context);
    }

    static {
        Covode.recordClassIndex(2585);
    }

    public static Context INVOKEVIRTUAL_com_appsflyer_internal_ag_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 void AFKeystoreWrapper(Context context, final b bVar, final Executor executor) {
        valueOf = bVar;
        AnonymousClass1 r1 = new Application.ActivityLifecycleCallbacks() {
            public boolean AFKeystoreWrapper;
            public boolean values = true;

            static {
                Covode.recordClassIndex(2586);
            }

            public final void onActivityDestroyed(Activity activity) {
            }

            public final void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
            }

            public final void onActivityStarted(Activity activity) {
            }

            public final void onActivityStopped(Activity activity) {
            }

            public final void onActivityPaused(final Activity activity) {
                executor.execute(new Runnable() {
                    static {
                        Covode.recordClassIndex(2587);
                    }

                    public final void run() {
                        AnonymousClass1.this.values = true;
                        final Context INVOKEVIRTUAL_com_appsflyer_internal_ag$1$1_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext = INVOKEVIRTUAL_com_appsflyer_internal_ag$1$1_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(activity);
                        try {
                            new Timer().schedule(new TimerTask() {
                                static {
                                    Covode.recordClassIndex(2588);
                                }

                                public final void run() {
                                    if (AnonymousClass1.this.AFKeystoreWrapper && AnonymousClass1.this.values) {
                                        AnonymousClass1.this.AFKeystoreWrapper = false;
                                        try {
                                            bVar.valueOf(INVOKEVIRTUAL_com_appsflyer_internal_ag$1$1_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext);
                                        } catch (Exception e) {
                                            AFLogger.valueOf("Listener threw exception! ", e);
                                        }
                                    }
                                }
                            }, ag.values);
                        } catch (Throwable th) {
                            AFLogger.valueOf("Background task failed with a throwable: ", th);
                        }
                    }

                    public static Context INVOKEVIRTUAL_com_appsflyer_internal_ag$1$1_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 final void onActivityResumed(final Activity activity) {
                executor.execute(new Runnable() {
                    static {
                        Covode.recordClassIndex(2589);
                    }

                    public final void run() {
                        if (!AnonymousClass1.this.AFKeystoreWrapper) {
                            try {
                                bVar.AFKeystoreWrapper(activity);
                            } catch (Exception e) {
                                AFLogger.values("Listener thrown an exception: ", (Throwable) e);
                            }
                        }
                        AnonymousClass1.this.values = false;
                        AnonymousClass1.this.AFKeystoreWrapper = true;
                    }
                });
            }

            public final void onActivityCreated(final Activity activity, Bundle bundle) {
                executor.execute(new Runnable() {
                    static {
                        Covode.recordClassIndex(2590);
                    }

                    public final void run() {
                        i.values();
                        Intent intent = activity.getIntent();
                        if (i.AFKeystoreWrapper(intent) != null && intent != i.AFInAppEventParameterName) {
                            i.AFInAppEventParameterName = intent;
                        }
                    }
                });
            }
        };
        if (context instanceof Activity) {
            r1.onActivityResumed((Activity) context);
        }
        ((Application) INVOKEVIRTUAL_com_appsflyer_internal_ag_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(context)).registerActivityLifecycleCallbacks(r1);
    }
}
