package Q4;

import Z6.AbstractC0385z;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import java.util.ArrayList;
import v0.AbstractC1664c;

/* JADX INFO: loaded from: classes.dex */
public final class b0 extends Handler {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public boolean f5736a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public long f5737b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ArrayList f5738c;

    public b0(Looper looper) {
        super(looper);
        this.f5738c = new ArrayList();
    }

    public final void a() {
        Log.d("SessionLifecycleService", "Broadcasting new session");
        P p3 = (P) ((C0262i) ((InterfaceC0271s) W3.f.c().b(InterfaceC0271s.class))).f5789k.get();
        M m8 = ((V) ((C0262i) ((InterfaceC0271s) W3.f.c().b(InterfaceC0271s.class))).f5791m.get()).f5713e;
        if (m8 == null) {
            P6.g.h("currentSession");
            throw null;
        }
        T t6 = (T) p3;
        t6.getClass();
        AbstractC0385z.r(AbstractC0385z.b(t6.f5702e), null, new Q(t6, m8, null), 3);
        ArrayList arrayList = new ArrayList(this.f5738c);
        int size = arrayList.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList.get(i);
            i++;
            Messenger messenger = (Messenger) obj;
            P6.g.d(messenger, "it");
            b(messenger);
        }
    }

    public final void b(Messenger messenger) {
        try {
            if (this.f5736a) {
                M m8 = ((V) ((C0262i) ((InterfaceC0271s) W3.f.c().b(InterfaceC0271s.class))).f5791m.get()).f5713e;
                if (m8 != null) {
                    d(messenger, m8.f5675a);
                    return;
                } else {
                    P6.g.h("currentSession");
                    throw null;
                }
            }
            C0274v c0274v = (C0274v) ((K) ((C0262i) ((InterfaceC0271s) W3.f.c().b(InterfaceC0271s.class))).f5788j.get()).f5670c.get();
            String str = c0274v != null ? c0274v.f5821a : null;
            Log.d("SessionLifecycleService", "App has not yet foregrounded. Using previously stored session.");
            if (str != null) {
                d(messenger, str);
            }
        } catch (IllegalStateException e8) {
            Log.w("SessionLifecycleService", "Failed to send session to client.", e8);
        }
    }

    public final void c() {
        try {
            V v7 = (V) ((C0262i) ((InterfaceC0271s) W3.f.c().b(InterfaceC0271s.class))).f5791m.get();
            int i = v7.f5712d + 1;
            v7.f5712d = i;
            String strA = i == 0 ? v7.f5711c : v7.a();
            int i5 = v7.f5712d;
            v7.f5709a.getClass();
            v7.f5713e = new M(System.currentTimeMillis() * 1000, strA, v7.f5711c, i5);
            Log.d("SessionLifecycleService", "Generated new session.");
            a();
            K k8 = (K) ((C0262i) ((InterfaceC0271s) W3.f.c().b(InterfaceC0271s.class))).f5788j.get();
            M m8 = ((V) ((C0262i) ((InterfaceC0271s) W3.f.c().b(InterfaceC0271s.class))).f5791m.get()).f5713e;
            if (m8 == null) {
                P6.g.h("currentSession");
                throw null;
            }
            String str = m8.f5675a;
            k8.getClass();
            P6.g.e(str, "sessionId");
            AbstractC0385z.r(AbstractC0385z.b(k8.f5668a), null, new J(k8, str, null), 3);
        } catch (IllegalStateException e8) {
            Log.w("SessionLifecycleService", "Failed to generate new session.", e8);
        }
    }

    public final void d(Messenger messenger, String str) {
        try {
            Bundle bundle = new Bundle();
            bundle.putString("SessionUpdateExtra", str);
            Message messageObtain = Message.obtain(null, 3, 0, 0);
            messageObtain.setData(bundle);
            messenger.send(messageObtain);
        } catch (DeadObjectException unused) {
            Log.d("SessionLifecycleService", "Removing dead client from list: " + messenger);
            this.f5738c.remove(messenger);
        } catch (Exception e8) {
            Log.w("SessionLifecycleService", "Unable to push new session to " + messenger + '.', e8);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:28:0x010d  */
    /* JADX WARN: Removed duplicated region for block: B:35:0x0124  */
    @Override // android.os.Handler
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void handleMessage(Message message) {
        P6.g.e(message, "msg");
        if (this.f5737b > message.getWhen()) {
            Log.d("SessionLifecycleService", "Ignoring old message from " + message.getWhen() + " which is older than " + this.f5737b + '.');
            return;
        }
        int i = message.what;
        if (i != 1) {
            if (i == 2) {
                Log.d("SessionLifecycleService", "Activity backgrounding at " + message.getWhen());
                this.f5737b = message.getWhen();
                return;
            }
            if (i != 4) {
                Log.w("SessionLifecycleService", "Received unexpected event from the SessionLifecycleClient: " + message);
                super.handleMessage(message);
                return;
            }
            ArrayList arrayList = this.f5738c;
            arrayList.add(message.replyTo);
            Messenger messenger = message.replyTo;
            P6.g.d(messenger, "msg.replyTo");
            b(messenger);
            Log.d("SessionLifecycleService", "Client " + message.replyTo + " bound at " + message.getWhen() + ". Clients: " + arrayList.size());
            return;
        }
        Log.d("SessionLifecycleService", "Activity foregrounding at " + message.getWhen() + '.');
        if (this.f5736a) {
            long when = message.getWhen() - this.f5737b;
            U4.j jVar = (U4.j) ((C0262i) ((InterfaceC0271s) W3.f.c().b(InterfaceC0271s.class))).f5787h.get();
            Y6.a aVarA = jVar.f6753a.a();
            if (aVarA != null) {
                int i5 = Y6.a.f7349t;
                long jA = aVarA.f7350q;
                if (jA <= 0 || Y6.a.c(jA)) {
                    Y6.a aVarA2 = jVar.f6754b.a();
                    if (aVarA2 != null) {
                        int i8 = Y6.a.f7349t;
                        jA = aVarA2.f7350q;
                        if (jA <= 0 || Y6.a.c(jA)) {
                            int i9 = Y6.a.f7349t;
                            jA = AbstractC1664c.A(30, Y6.c.MINUTES);
                        }
                    }
                }
                if (when > (((((int) jA) & 1) != 1 || Y6.a.c(jA)) ? Y6.a.d(jA, Y6.c.MILLISECONDS) : jA >> 1)) {
                    Log.d("SessionLifecycleService", "Session too long in background. Creating new session.");
                    c();
                }
            }
        } else {
            Log.d("SessionLifecycleService", "Cold start detected.");
            this.f5736a = true;
            c();
        }
        this.f5737b = message.getWhen();
    }
}
