APK反编译源代码展示 - 南明离火平台提供

应用版本信息
应用名称:KBU Smart
版本号:1.7.3
包名称:com.rinary.kbusmart

MD5 校验值:4313c543e0d712b66b250ca5172b19dc

反编译源代码说明

n.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。


package s2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import s2.b;
import s2.d;
import s2.h;
import t2.a;

public class n implements b.a, s2.h {
    private static long H;
    private String A;
    private long F;
    private boolean G;

    private final h.a f12270a;

    private final s2.f f12271b;

    private String f12272c;

    private long f12275f;

    private s2.b f12276g;

    private Map<Long, j> f12281l;

    private List<l> f12282m;

    private Map<Long, o> f12283n;

    private Map<Long, m> f12284o;

    private Map<p, C0166n> f12285p;

    private String f12286q;

    private boolean f12287r;

    private String f12288s;

    private boolean f12289t;

    private final s2.c f12290u;

    private final s2.d f12291v;

    private final s2.d f12292w;

    private final ScheduledExecutorService f12293x;

    private final b3.c f12294y;

    private final t2.a f12295z;

    private HashSet<String> f12273d = new HashSet<>();

    private boolean f12274e = true;

    private k f12277h = k.Disconnected;

    private long f12278i = 0;

    private long f12279j = 0;

    private long f12280k = 0;
    private long B = 0;
    private int C = 0;
    private int D = 0;
    private ScheduledFuture<?> E = null;

    public class a implements d.a {

        final x1.j f12296a;

        a(x1.j jVar) {
            this.f12296a = jVar;
        }

        @Override
        public void a(String str) {
            this.f12296a.b(new Exception(str));
        }

        @Override
        public void b(String str) {
            this.f12296a.c(str);
        }
    }

    public class b implements d.a {

        final x1.j f12298a;

        b(x1.j jVar) {
            this.f12298a = jVar;
        }

        @Override
        public void a(String str) {
            this.f12298a.b(new Exception(str));
        }

        @Override
        public void b(String str) {
            this.f12298a.c(str);
        }
    }

    public class c implements j {

        final s2.p f12300a;

        c(s2.p pVar) {
            this.f12300a = pVar;
        }

        @Override
        public void a(Map<String, Object> map) {
            String str = (String) map.get("s");
            String str2 = null;
            if (str.equals("ok")) {
                str = null;
            } else {
                str2 = (String) map.get("d");
            }
            s2.p pVar = this.f12300a;
            if (pVar != null) {
                pVar.a(str, str2);
            }
        }
    }

    public class d implements j {

        final boolean f12302a;

        d(boolean z8) {
            this.f12302a = z8;
        }

        @Override
        public void a(Map<String, Object> map) {
            String str = (String) map.get("s");
            if (str.equals("ok")) {
                n.this.f12277h = k.Connected;
                n.this.C = 0;
                n.this.u0(this.f12302a);
                return;
            }
            n.this.f12286q = null;
            n.this.f12287r = true;
            n.this.f12270a.e(false);
            String str2 = (String) map.get("d");
            n.this.f12294y.b("Authentication failed: " + str + " (" + str2 + ")", new Object[0]);
            n.this.f12276g.c();
            if (str.equals("invalid_token")) {
                n.z(n.this);
                if (n.this.C >= 3) {
                    n.this.f12295z.d();
                    n.this.f12294y.i("Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your google-services.json file has the correct firebase_url and api_key. You can re-download google-services.json from https://console.firebase.google.com/.");
                }
            }
        }
    }

    public class e implements j {

        final String f12304a;

        final long f12305b;

        final o f12306c;

        final s2.p f12307d;

        e(String str, long j9, o oVar, s2.p pVar) {
            this.f12304a = str;
            this.f12305b = j9;
            this.f12306c = oVar;
            this.f12307d = pVar;
        }

        @Override
        public void a(Map<String, Object> map) {
            if (n.this.f12294y.f()) {
                n.this.f12294y.b(this.f12304a + " response: " + map, new Object[0]);
            }
            if (((o) n.this.f12283n.get(Long.valueOf(this.f12305b))) == this.f12306c) {
                n.this.f12283n.remove(Long.valueOf(this.f12305b));
                if (this.f12307d != null) {
                    String str = (String) map.get("s");
                    if (str.equals("ok")) {
                        this.f12307d.a(null, null);
                    } else {
                        this.f12307d.a(str, (String) map.get("d"));
                    }
                }
            } else if (n.this.f12294y.f()) {
                n.this.f12294y.b("Ignoring on complete for put " + this.f12305b + " because it was removed already.", new Object[0]);
            }
            n.this.U();
        }
    }

    public class f implements j {

        final Long f12309a;

        final m f12310b;

        f(Long l9, m mVar) {
            this.f12309a = l9;
            this.f12310b = mVar;
        }

        @Override
        public void a(Map<String, Object> map) {
            if (((m) n.this.f12284o.get(this.f12309a)) == this.f12310b) {
                n.this.f12284o.remove(this.f12309a);
                this.f12310b.d().a(map);
            } else if (n.this.f12294y.f()) {
                n.this.f12294y.b("Ignoring on complete for get " + this.f12309a + " because it was removed already.", new Object[0]);
            }
        }
    }

    public class g implements j {

        final C0166n f12312a;

        g(C0166n c0166n) {
            this.f12312a = c0166n;
        }

        @Override
        public void a(Map<String, Object> map) {
            String str = (String) map.get("s");
            if (str.equals("ok")) {
                Map map2 = (Map) map.get("d");
                if (map2.containsKey("w")) {
                    n.this.H0((List) map2.get("w"), this.f12312a.f12330b);
                }
            }
            if (((C0166n) n.this.f12285p.get(this.f12312a.d())) == this.f12312a) {
                if (str.equals("ok")) {
                    this.f12312a.f12329a.a(null, null);
                    return;
                }
                n.this.p0(this.f12312a.d());
                this.f12312a.f12329a.a(str, (String) map.get("d"));
            }
        }
    }

    public class h implements j {
        h() {
        }

        @Override
        public void a(Map<String, Object> map) {
            String str = (String) map.get("s");
            if (str.equals("ok")) {
                return;
            }
            String str2 = (String) map.get("d");
            if (n.this.f12294y.f()) {
                n.this.f12294y.b("Failed to send stats: " + str + " (message: " + str2 + ")", new Object[0]);
            }
        }
    }

    public class i implements Runnable {
        i() {
        }

        @Override
        public void run() {
            n.this.E = null;
            if (n.this.Z()) {
                n.this.l("connection_idle");
            } else {
                n.this.U();
            }
        }
    }

    public interface j {
        void a(Map<String, Object> map);
    }

    public enum k {
        Disconnected,
        GettingToken,
        Connecting,
        Authenticating,
        Connected
    }

    public static class l {

        private final String f12322a;

        private final List<String> f12323b;

        private final Object f12324c;

        private final s2.p f12325d;

        private l(String str, List<String> list, Object obj, s2.p pVar) {
            this.f12322a = str;
            this.f12323b = list;
            this.f12324c = obj;
            this.f12325d = pVar;
        }

        l(String str, List list, Object obj, s2.p pVar, a aVar) {
            this(str, list, obj, pVar);
        }

        public String b() {
            return this.f12322a;
        }

        public Object c() {
            return this.f12324c;
        }

        public s2.p d() {
            return this.f12325d;
        }

        public List<String> e() {
            return this.f12323b;
        }
    }

    public static class m {

        private final Map<String, Object> f12326a;

        private final j f12327b;

        private boolean f12328c;

        private m(String str, Map<String, Object> map, j jVar) {
            this.f12326a = map;
            this.f12327b = jVar;
            this.f12328c = false;
        }

        m(String str, Map map, j jVar, a aVar) {
            this(str, map, jVar);
        }

        public j d() {
            return this.f12327b;
        }

        public Map<String, Object> e() {
            return this.f12326a;
        }

        public boolean f() {
            if (this.f12328c) {
                return false;
            }
            this.f12328c = true;
            return true;
        }
    }

    public static class C0166n {

        private final s2.p f12329a;

        private final p f12330b;

        private final s2.g f12331c;

        private final Long f12332d;

        private C0166n(s2.p pVar, p pVar2, Long l9, s2.g gVar) {
            this.f12329a = pVar;
            this.f12330b = pVar2;
            this.f12331c = gVar;
            this.f12332d = l9;
        }

        C0166n(s2.p pVar, p pVar2, Long l9, s2.g gVar, a aVar) {
            this(pVar, pVar2, l9, gVar);
        }

        public s2.g c() {
            return this.f12331c;
        }

        public p d() {
            return this.f12330b;
        }

        public Long e() {
            return this.f12332d;
        }

        public String toString() {
            return this.f12330b.toString() + " (Tag: " + this.f12332d + ")";
        }
    }

    public static class o {

        private String f12333a;

        private Map<String, Object> f12334b;

        private s2.p f12335c;

        private boolean f12336d;

        private o(String str, Map<String, Object> map, s2.p pVar) {
            this.f12333a = str;
            this.f12334b = map;
            this.f12335c = pVar;
        }

        o(String str, Map map, s2.p pVar, a aVar) {
            this(str, map, pVar);
        }

        public String b() {
            return this.f12333a;
        }

        public s2.p c() {
            return this.f12335c;
        }

        public Map<String, Object> d() {
            return this.f12334b;
        }

        public void e() {
            this.f12336d = true;
        }

        public boolean f() {
            return this.f12336d;
        }
    }

    public static class p {

        private final List<String> f12337a;

        private final Map<String, Object> f12338b;

        public p(List<String> list, Map<String, Object> map) {
            this.f12337a = list;
            this.f12338b = map;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof p)) {
                return false;
            }
            p pVar = (p) obj;
            if (this.f12337a.equals(pVar.f12337a)) {
                return this.f12338b.equals(pVar.f12338b);
            }
            return false;
        }

        public int hashCode() {
            return (this.f12337a.hashCode() * 31) + this.f12338b.hashCode();
        }

        public String toString() {
            return s2.e.d(this.f12337a) + " (params: " + this.f12338b + ")";
        }
    }

    public n(s2.c cVar, s2.f fVar, h.a aVar) {
        this.f12270a = aVar;
        this.f12290u = cVar;
        ScheduledExecutorService e9 = cVar.e();
        this.f12293x = e9;
        this.f12291v = cVar.c();
        this.f12292w = cVar.a();
        this.f12271b = fVar;
        this.f12285p = new HashMap();
        this.f12281l = new HashMap();
        this.f12283n = new HashMap();
        this.f12284o = new ConcurrentHashMap();
        this.f12282m = new ArrayList();
        this.f12295z = new a.b(e9, cVar.f(), "ConnectionRetryHelper").d(1000L).e(1.3d).c(30000L).b(0.7d).a();
        long j9 = H;
        H = 1 + j9;
        this.f12294y = new b3.c(cVar.f(), "PersistentConnection", "pc_" + j9);
        this.A = null;
        U();
    }

    private void A0(String str, List<String> list, Object obj, s2.p pVar) {
        HashMap hashMap = new HashMap();
        hashMap.put("p", s2.e.d(list));
        hashMap.put("d", obj);
        t0(str, hashMap, new c(pVar));
    }

    private void B0(long j9) {
        s2.e.b(R(), "sendPut called when we can't send writes (we're disconnected or writes are paused).", new Object[0]);
        o oVar = this.f12283n.get(Long.valueOf(j9));
        s2.p c9 = oVar.c();
        String b9 = oVar.b();
        oVar.e();
        t0(b9, oVar.d(), new e(b9, j9, oVar, c9));
    }

    private void C0(String str, boolean z8, Map<String, Object> map, j jVar) {
        long h02 = h0();
        HashMap hashMap = new HashMap();
        hashMap.put("r", Long.valueOf(h02));
        hashMap.put("a", str);
        hashMap.put("b", map);
        this.f12276g.m(hashMap, z8);
        this.f12281l.put(Long.valueOf(h02), jVar);
    }

    private void D0(Map<String, Integer> map) {
        if (map.isEmpty()) {
            if (this.f12294y.f()) {
                this.f12294y.b("Not sending stats because stats are empty", new Object[0]);
            }
        } else {
            HashMap hashMap = new HashMap();
            hashMap.put("c", map);
            t0("s", hashMap, new h());
        }
    }

    private void E0(C0166n c0166n) {
        HashMap hashMap = new HashMap();
        hashMap.put("p", s2.e.d(c0166n.f12330b.f12337a));
        Long e9 = c0166n.e();
        if (e9 != null) {
            hashMap.put("q", c0166n.d().f12338b);
            hashMap.put("t", e9);
        }
        t0("n", hashMap, null);
    }

    private void G0() {
        if (F0()) {
            k kVar = this.f12277h;
            s2.e.b(kVar == k.Disconnected, "Not in disconnected state: %s", kVar);
            final boolean z8 = this.f12287r;
            final boolean z9 = this.f12289t;
            this.f12294y.b("Scheduling connection attempt", new Object[0]);
            this.f12287r = false;
            this.f12289t = false;
            this.f12295z.c(new Runnable() {
                @Override
                public final void run() {
                    n.this.g0(z8, z9);
                }
            });
        }
    }

    public void H0(List<String> list, p pVar) {
        if (list.contains("no_index")) {
            String str = "\".indexOn\": \"" + pVar.f12338b.get("i") + '\"';
            this.f12294y.i("Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding '" + str + "' at " + s2.e.d(pVar.f12337a) + " to your security and Firebase Database rules for better performance");
        }
    }

    private boolean Q() {
        return this.f12277h == k.Connected;
    }

    private boolean R() {
        return this.f12277h == k.Connected;
    }

    private void S() {
        ArrayList arrayList = new ArrayList();
        Iterator<Map.Entry<Long, o>> it = this.f12283n.entrySet().iterator();
        while (it.hasNext()) {
            o value = it.next().getValue();
            if (value.d().containsKey("h") && value.f()) {
                arrayList.add(value);
                it.remove();
            }
        }
        Iterator it2 = arrayList.iterator();
        while (it2.hasNext()) {
            ((o) it2.next()).c().a("disconnected", null);
        }
    }

    private boolean T() {
        k kVar = this.f12277h;
        return kVar == k.Authenticating || kVar == k.Connected;
    }

    public void U() {
        if (a0()) {
            ScheduledFuture<?> scheduledFuture = this.E;
            if (scheduledFuture != null) {
                scheduledFuture.cancel(false);
            }
            this.E = this.f12293x.schedule(new i(), 60000L, TimeUnit.MILLISECONDS);
            return;
        }
        if (b0("connection_idle")) {
            s2.e.a(!a0());
            o("connection_idle");
        }
    }

    private x1.i<String> V(boolean z8) {
        x1.j jVar = new x1.j();
        this.f12294y.b("Trying to fetch app check token", new Object[0]);
        this.f12292w.a(z8, new b(jVar));
        return jVar.a();
    }

    private x1.i<String> W(boolean z8) {
        x1.j jVar = new x1.j();
        this.f12294y.b("Trying to fetch auth token", new Object[0]);
        this.f12291v.a(z8, new a(jVar));
        return jVar.a();
    }

    private Map<String, Object> X(List<String> list, Object obj, String str) {
        HashMap hashMap = new HashMap();
        hashMap.put("p", s2.e.d(list));
        hashMap.put("d", obj);
        if (str != null) {
            hashMap.put("h", str);
        }
        return hashMap;
    }

    private void Y(long j9) {
        if (this.f12294y.f()) {
            this.f12294y.b("handling timestamp", new Object[0]);
        }
        long currentTimeMillis = j9 - System.currentTimeMillis();
        HashMap hashMap = new HashMap();
        hashMap.put("serverTimeOffset", Long.valueOf(currentTimeMillis));
        this.f12270a.b(hashMap);
    }

    public boolean Z() {
        return a0() && System.currentTimeMillis() > this.F + 60000;
    }

    private boolean a0() {
        return this.f12285p.isEmpty() && this.f12284o.isEmpty() && this.f12281l.isEmpty() && !this.G && this.f12283n.isEmpty();
    }

    public static void c0(x1.j jVar, Map map) {
        if (((String) map.get("s")).equals("ok")) {
            jVar.c(map.get("d"));
        } else {
            jVar.b(new Exception((String) map.get("d")));
        }
    }

    public void d0(boolean z8, Map map) {
        String str = (String) map.get("s");
        if (str.equals("ok")) {
            this.D = 0;
        } else {
            this.f12288s = null;
            this.f12289t = true;
            String str2 = (String) map.get("d");
            this.f12294y.b("App check failed: " + str + " (" + str2 + ")", new Object[0]);
        }
        if (z8) {
            r0();
        }
    }

    public void e0(long j9, x1.i iVar, x1.i iVar2, Void r72) {
        if (j9 != this.B) {
            this.f12294y.b("Ignoring getToken result, because this was not the latest attempt.", new Object[0]);
            return;
        }
        k kVar = this.f12277h;
        if (kVar == k.GettingToken) {
            this.f12294y.b("Successfully fetched token, opening connection", new Object[0]);
            n0((String) iVar.l(), (String) iVar2.l());
        } else if (kVar == k.Disconnected) {
            this.f12294y.b("Not opening connection after token refresh, because connection was set to disconnected", new Object[0]);
        }
    }

    public void f0(long j9, Exception exc) {
        if (j9 != this.B) {
            this.f12294y.b("Ignoring getToken error, because this was not the latest attempt.", new Object[0]);
            return;
        }
        this.f12277h = k.Disconnected;
        this.f12294y.b("Error fetching token: " + exc, new Object[0]);
        G0();
    }

    public void g0(boolean z8, boolean z9) {
        k kVar = this.f12277h;
        s2.e.b(kVar == k.Disconnected, "Not in disconnected state: %s", kVar);
        this.f12277h = k.GettingToken;
        final long j9 = this.B + 1;
        this.B = j9;
        final x1.i<String> W = W(z8);
        final x1.i<String> V = V(z9);
        x1.l.h(W, V).f(this.f12293x, new x1.f() {
            @Override
            public final void c(Object obj) {
                n.this.e0(j9, W, V, (Void) obj);
            }
        }).d(this.f12293x, new x1.e() {
            @Override
            public final void a(Exception exc) {
                n.this.f0(j9, exc);
            }
        });
    }

    private long h0() {
        long j9 = this.f12280k;
        this.f12280k = 1 + j9;
        return j9;
    }

    private void i0(String str, String str2) {
        this.f12294y.b("App check token revoked: " + str + " (" + str2 + ")", new Object[0]);
        this.f12288s = null;
        this.f12289t = true;
    }

    private void j0(String str, String str2) {
        this.f12294y.b("Auth token revoked: " + str + " (" + str2 + ")", new Object[0]);
        this.f12286q = null;
        this.f12287r = true;
        this.f12270a.e(false);
        this.f12276g.c();
    }

    private void k0(String str, Map<String, Object> map) {
        if (this.f12294y.f()) {
            this.f12294y.b("handleServerMessage: " + str + " " + map, new Object[0]);
        }
        if (str.equals("d") || str.equals("m")) {
            boolean equals = str.equals("m");
            String str2 = (String) map.get("p");
            Object obj = map.get("d");
            Long c9 = s2.e.c(map.get("t"));
            if (!equals || !(obj instanceof Map) || ((Map) obj).size() != 0) {
                this.f12270a.c(s2.e.e(str2), obj, equals, c9);
                return;
            }
            if (this.f12294y.f()) {
                this.f12294y.b("ignoring empty merge for path " + str2, new Object[0]);
                return;
            }
            return;
        }
        if (!str.equals("rm")) {
            if (str.equals("c")) {
                l0(s2.e.e((String) map.get("p")));
                return;
            }
            if (str.equals("ac")) {
                j0((String) map.get("s"), (String) map.get("d"));
                return;
            }
            if (str.equals("apc")) {
                i0((String) map.get("s"), (String) map.get("d"));
                return;
            }
            if (str.equals("sd")) {
                m0(map);
                return;
            }
            if (this.f12294y.f()) {
                this.f12294y.b("Unrecognized action from server: " + str, new Object[0]);
                return;
            }
            return;
        }
        String str3 = (String) map.get("p");
        List<String> e9 = s2.e.e(str3);
        Object obj2 = map.get("d");
        Long c10 = s2.e.c(map.get("t"));
        ArrayList arrayList = new ArrayList();
        for (Map map2 : (List) obj2) {
            String str4 = (String) map2.get("s");
            String str5 = (String) map2.get("e");
            List<String> list = null;
            List<String> e10 = str4 != null ? s2.e.e(str4) : null;
            if (str5 != null) {
                list = s2.e.e(str5);
            }
            arrayList.add(new s2.o(e10, list, map2.get("m")));
        }
        if (!arrayList.isEmpty()) {
            this.f12270a.f(e9, arrayList, c10);
            return;
        }
        if (this.f12294y.f()) {
            this.f12294y.b("Ignoring empty range merge for path " + str3, new Object[0]);
        }
    }

    private void l0(List<String> list) {
        Collection<C0166n> q02 = q0(list);
        if (q02 != null) {
            Iterator<C0166n> it = q02.iterator();
            while (it.hasNext()) {
                it.next().f12329a.a("permission_denied", null);
            }
        }
    }

    private void m0(Map<String, Object> map) {
        this.f12294y.e((String) map.get("msg"));
    }

    private void o0(String str, List<String> list, Object obj, String str2, s2.p pVar) {
        Map<String, Object> X = X(list, obj, str2);
        long j9 = this.f12278i;
        this.f12278i = 1 + j9;
        this.f12283n.put(Long.valueOf(j9), new o(str, X, pVar, null));
        if (R()) {
            B0(j9);
        }
        this.F = System.currentTimeMillis();
        U();
    }

    public C0166n p0(p pVar) {
        if (this.f12294y.f()) {
            this.f12294y.b("removing query " + pVar, new Object[0]);
        }
        if (this.f12285p.containsKey(pVar)) {
            C0166n c0166n = this.f12285p.get(pVar);
            this.f12285p.remove(pVar);
            U();
            return c0166n;
        }
        if (!this.f12294y.f()) {
            return null;
        }
        this.f12294y.b("Trying to remove listener for QuerySpec " + pVar + " but no listener exists.", new Object[0]);
        return null;
    }

    private Collection<C0166n> q0(List<String> list) {
        if (this.f12294y.f()) {
            this.f12294y.b("removing all listens at path " + list, new Object[0]);
        }
        ArrayList arrayList = new ArrayList();
        for (Map.Entry<p, C0166n> entry : this.f12285p.entrySet()) {
            p key = entry.getKey();
            C0166n value = entry.getValue();
            if (key.f12337a.equals(list)) {
                arrayList.add(value);
            }
        }
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            this.f12285p.remove(((C0166n) it.next()).d());
        }
        U();
        return arrayList;
    }

    private void r0() {
        k kVar = this.f12277h;
        s2.e.b(kVar == k.Connected, "Should be connected if we're restoring state, but we are: %s", kVar);
        if (this.f12294y.f()) {
            this.f12294y.b("Restoring outstanding listens", new Object[0]);
        }
        for (C0166n c0166n : this.f12285p.values()) {
            if (this.f12294y.f()) {
                this.f12294y.b("Restoring listen " + c0166n.d(), new Object[0]);
            }
            z0(c0166n);
        }
        if (this.f12294y.f()) {
            this.f12294y.b("Restoring writes.", new Object[0]);
        }
        ArrayList arrayList = new ArrayList(this.f12283n.keySet());
        Collections.sort(arrayList);
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            B0(((Long) it.next()).longValue());
        }
        for (l lVar : this.f12282m) {
            A0(lVar.b(), lVar.e(), lVar.c(), lVar.d());
        }
        this.f12282m.clear();
        if (this.f12294y.f()) {
            this.f12294y.b("Restoring reads.", new Object[0]);
        }
        ArrayList arrayList2 = new ArrayList(this.f12284o.keySet());
        Collections.sort(arrayList2);
        Iterator it2 = arrayList2.iterator();
        while (it2.hasNext()) {
            y0((Long) it2.next());
        }
    }

    private void s0() {
        if (this.f12294y.f()) {
            this.f12294y.b("calling restore tokens", new Object[0]);
        }
        k kVar = this.f12277h;
        s2.e.b(kVar == k.Connecting, "Wanted to restore tokens, but was in wrong state: %s", kVar);
        if (this.f12286q != null) {
            if (this.f12294y.f()) {
                this.f12294y.b("Restoring auth.", new Object[0]);
            }
            this.f12277h = k.Authenticating;
            v0();
            return;
        }
        if (this.f12294y.f()) {
            this.f12294y.b("Not restoring auth because auth token is null.", new Object[0]);
        }
        this.f12277h = k.Connected;
        u0(true);
    }

    private void t0(String str, Map<String, Object> map, j jVar) {
        C0(str, false, map, jVar);
    }

    public void u0(final boolean z8) {
        if (this.f12288s == null) {
            r0();
            return;
        }
        s2.e.b(T(), "Must be connected to send auth, but was: %s", this.f12277h);
        if (this.f12294y.f()) {
            this.f12294y.b("Sending app check.", new Object[0]);
        }
        j jVar = new j() {
            @Override
            public final void a(Map map) {
                n.this.d0(z8, map);
            }
        };
        HashMap hashMap = new HashMap();
        s2.e.b(this.f12288s != null, "App check token must be set!", new Object[0]);
        hashMap.put("token", this.f12288s);
        C0("appcheck", true, hashMap, jVar);
    }

    private void v0() {
        w0(true);
    }

    private void w0(boolean z8) {
        String str;
        s2.e.b(T(), "Must be connected to send auth, but was: %s", this.f12277h);
        if (this.f12294y.f()) {
            this.f12294y.b("Sending auth.", new Object[0]);
        }
        d dVar = new d(z8);
        HashMap hashMap = new HashMap();
        e3.a c9 = e3.a.c(this.f12286q);
        if (c9 != null) {
            hashMap.put("cred", c9.b());
            if (c9.a() != null) {
                hashMap.put("authvar", c9.a());
            }
            str = "gauth";
        } else {
            hashMap.put("cred", this.f12286q);
            str = "auth";
        }
        C0(str, true, hashMap, dVar);
    }

    private void x0() {
        HashMap hashMap = new HashMap();
        if (this.f12290u.i()) {
            hashMap.put("persistence.android.enabled", 1);
        }
        hashMap.put("sdk.android." + this.f12290u.d().replace('.', '-'), 1);
        if (this.f12294y.f()) {
            this.f12294y.b("Sending first connection stats", new Object[0]);
        }
        D0(hashMap);
    }

    private void y0(Long l9) {
        s2.e.b(Q(), "sendGet called when we can't send gets", new Object[0]);
        m mVar = this.f12284o.get(l9);
        if (mVar.f() || !this.f12294y.f()) {
            t0("g", mVar.e(), new f(l9, mVar));
            return;
        }
        this.f12294y.b("get" + l9 + " cancelled, ignoring.", new Object[0]);
    }

    static int z(n nVar) {
        int i9 = nVar.C;
        nVar.C = i9 + 1;
        return i9;
    }

    private void z0(C0166n c0166n) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("p", s2.e.d(c0166n.d().f12337a));
        Object e9 = c0166n.e();
        if (e9 != null) {
            hashMap.put("q", c0166n.f12330b.f12338b);
            hashMap.put("t", e9);
        }
        s2.g c9 = c0166n.c();
        hashMap.put("h", c9.d());
        if (c9.c()) {
            s2.a b9 = c9.b();
            ArrayList arrayList = new ArrayList();
            Iterator<List<String>> it = b9.b().iterator();
            while (it.hasNext()) {
                arrayList.add(s2.e.d(it.next()));
            }
            HashMap hashMap2 = new HashMap();
            hashMap2.put("hs", b9.a());
            hashMap2.put("ps", arrayList);
            hashMap.put("ch", hashMap2);
        }
        t0("q", hashMap, new g(c0166n));
    }

    boolean F0() {
        return this.f12273d.size() == 0;
    }

    @Override
    public void a() {
        G0();
    }

    @Override
    public void b(List<String> list, Map<String, Object> map, s2.p pVar) {
        this.G = true;
        if (R()) {
            A0("om", list, map, pVar);
        } else {
            this.f12282m.add(new l("om", list, map, pVar, null));
        }
        U();
    }

    public boolean b0(String str) {
        return this.f12273d.contains(str);
    }

    @Override
    public void c(List<String> list, Map<String, Object> map, s2.g gVar, Long l9, s2.p pVar) {
        p pVar2 = new p(list, map);
        if (this.f12294y.f()) {
            this.f12294y.b("Listening on " + pVar2, new Object[0]);
        }
        s2.e.b(!this.f12285p.containsKey(pVar2), "listen() called twice for same QuerySpec.", new Object[0]);
        if (this.f12294y.f()) {
            this.f12294y.b("Adding listen query: " + pVar2, new Object[0]);
        }
        C0166n c0166n = new C0166n(pVar, pVar2, l9, gVar, null);
        this.f12285p.put(pVar2, c0166n);
        if (T()) {
            z0(c0166n);
        }
        U();
    }

    @Override
    public void d(long j9, String str) {
        if (this.f12294y.f()) {
            this.f12294y.b("onReady", new Object[0]);
        }
        this.f12275f = System.currentTimeMillis();
        Y(j9);
        if (this.f12274e) {
            x0();
        }
        s0();
        this.f12274e = false;
        this.A = str;
        this.f12270a.a();
    }

    @Override
    public x1.i<Object> e(List<String> list, Map<String, Object> map) {
        p pVar = new p(list, map);
        final x1.j jVar = new x1.j();
        long j9 = this.f12279j;
        this.f12279j = 1 + j9;
        HashMap hashMap = new HashMap();
        hashMap.put("p", s2.e.d(pVar.f12337a));
        hashMap.put("q", pVar.f12338b);
        this.f12284o.put(Long.valueOf(j9), new m("g", hashMap, new j() {
            @Override
            public final void a(Map map2) {
                n.c0(x1.j.this, map2);
            }
        }, null));
        if (Q()) {
            y0(Long.valueOf(j9));
        }
        U();
        return jVar.a();
    }

    @Override
    public void f(b.EnumC0165b enumC0165b) {
        boolean z8 = false;
        if (this.f12294y.f()) {
            this.f12294y.b("Got on disconnect due to " + enumC0165b.name(), new Object[0]);
        }
        this.f12277h = k.Disconnected;
        this.f12276g = null;
        this.G = false;
        this.f12281l.clear();
        S();
        if (F0()) {
            long currentTimeMillis = System.currentTimeMillis();
            long j9 = this.f12275f;
            long j10 = currentTimeMillis - j9;
            if (j9 > 0 && j10 > 30000) {
                z8 = true;
            }
            if (enumC0165b == b.EnumC0165b.SERVER_RESET || z8) {
                this.f12295z.e();
            }
            G0();
        }
        this.f12275f = 0L;
        this.f12270a.d();
    }

    @Override
    public void g() {
        for (o oVar : this.f12283n.values()) {
            if (oVar.f12335c != null) {
                oVar.f12335c.a("write_canceled", null);
            }
        }
        for (l lVar : this.f12282m) {
            if (lVar.f12325d != null) {
                lVar.f12325d.a("write_canceled", null);
            }
        }
        this.f12283n.clear();
        this.f12282m.clear();
        if (!T()) {
            this.G = false;
        }
        U();
    }

    @Override
    public void h(List<String> list, Object obj, String str, s2.p pVar) {
        o0("p", list, obj, str, pVar);
    }

    @Override
    public void i(List<String> list, Object obj, s2.p pVar) {
        o0("p", list, obj, null, pVar);
    }

    @Override
    public void j(List<String> list, s2.p pVar) {
        if (R()) {
            A0("oc", list, null, pVar);
        } else {
            this.f12282m.add(new l("oc", list, null, pVar, null));
        }
        U();
    }

    @Override
    public void k(Map<String, Object> map) {
        if (map.containsKey("r")) {
            j remove = this.f12281l.remove(Long.valueOf(((Integer) map.get("r")).intValue()));
            if (remove != null) {
                remove.a((Map) map.get("b"));
                return;
            }
            return;
        }
        if (map.containsKey("error")) {
            return;
        }
        if (map.containsKey("a")) {
            k0((String) map.get("a"), (Map) map.get("b"));
            return;
        }
        if (this.f12294y.f()) {
            this.f12294y.b("Ignoring unknown message: " + map, new Object[0]);
        }
    }

    @Override
    public void l(String str) {
        if (this.f12294y.f()) {
            this.f12294y.b("Connection interrupted for: " + str, new Object[0]);
        }
        this.f12273d.add(str);
        s2.b bVar = this.f12276g;
        if (bVar != null) {
            bVar.c();
            this.f12276g = null;
        } else {
            this.f12295z.b();
            this.f12277h = k.Disconnected;
        }
        this.f12295z.e();
    }

    @Override
    public void m(List<String> list, Map<String, Object> map) {
        p pVar = new p(list, map);
        if (this.f12294y.f()) {
            this.f12294y.b("unlistening on " + pVar, new Object[0]);
        }
        C0166n p02 = p0(pVar);
        if (p02 != null && T()) {
            E0(p02);
        }
        U();
    }

    @Override
    public void n(String str) {
        this.f12272c = str;
    }

    public void n0(String str, String str2) {
        k kVar = this.f12277h;
        s2.e.b(kVar == k.GettingToken, "Trying to open network connection while in the wrong state: %s", kVar);
        if (str == null) {
            this.f12270a.e(false);
        }
        this.f12286q = str;
        this.f12288s = str2;
        this.f12277h = k.Connecting;
        s2.b bVar = new s2.b(this.f12290u, this.f12271b, this.f12272c, this, this.A, str2);
        this.f12276g = bVar;
        bVar.k();
    }

    @Override
    public void o(String str) {
        if (this.f12294y.f()) {
            this.f12294y.b("Connection no longer interrupted for: " + str, new Object[0]);
        }
        this.f12273d.remove(str);
        if (F0() && this.f12277h == k.Disconnected) {
            G0();
        }
    }

    @Override
    public void p(String str) {
        if (str.equals("Invalid appcheck token")) {
            int i9 = this.D;
            if (i9 < 3) {
                this.D = i9 + 1;
                this.f12294y.i("Detected invalid AppCheck token. Reconnecting (" + (3 - this.D) + " attempts remaining)");
                return;
            }
        }
        this.f12294y.i("Firebase Database connection was forcefully killed by the server. Will not attempt reconnect. Reason: " + str);
        l("server_kill");
    }

    @Override
    public void q(List<String> list, Object obj, s2.p pVar) {
        this.G = true;
        if (R()) {
            A0("o", list, obj, pVar);
        } else {
            this.f12282m.add(new l("o", list, obj, pVar, null));
        }
        U();
    }

    @Override
    public void r(List<String> list, Map<String, Object> map, s2.p pVar) {
        o0("m", list, map, null, pVar);
    }
}