package n6;

import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.SparseIntArray;
import com.bumptech.glide.g;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import l6.e;
import m6.d;
import m6.f;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public final class a implements Handler.Callback {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final String f30883a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final d f30884b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final e f30886d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final SparseIntArray f30887e = new SparseIntArray();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public Handler f30888f;

    public a(d dVar, String str, e eVar) {
        this.f30883a = dVar.f30398b.lb();
        this.f30884b = dVar;
        this.f30885c = str;
        this.f30886d = eVar;
    }

    public final void a(int i10, int i11) {
        synchronized (this.f30887e) {
            Integer numValueOf = Integer.valueOf(this.f30887e.get(i10));
            SparseIntArray sparseIntArray = this.f30887e;
            if (numValueOf != null) {
                i11 += numValueOf.intValue();
            }
            sparseIntArray.put(i10, i11);
        }
    }

    public final void b(Looper looper, f fVar) {
        long j10 = c().getLong("last_upload_time", 0L);
        long jFm = this.f30886d.fm();
        if (j10 <= 0 || System.currentTimeMillis() - j10 <= jFm) {
            SharedPreferences sharedPreferencesC = c();
            String string = sharedPreferencesC.getString("event_counts", null);
            if (string != null) {
                try {
                    JSONObject jSONObject = new JSONObject(string);
                    Iterator<String> itKeys = jSONObject.keys();
                    synchronized (this.f30887e) {
                        while (itKeys.hasNext()) {
                            try {
                                String next = itKeys.next();
                                this.f30887e.put(Integer.parseInt(next), jSONObject.getInt(next));
                            } finally {
                            }
                        }
                    }
                } catch (Exception unused) {
                }
            }
            if (j10 == 0) {
                sharedPreferencesC.edit().putLong("last_upload_time", System.currentTimeMillis()).apply();
            }
            Objects.toString(this.f30887e);
        } else {
            try {
                JSONObject jSONObject2 = new JSONObject();
                jSONObject2.putOpt("instance_id", this.f30883a);
                jSONObject2.putOpt("event_type", this.f30885c);
                jSONObject2.putOpt("db_data_count", Integer.valueOf(fVar.f(this.f30884b.a())));
                SharedPreferences sharedPreferencesC2 = c();
                Map<String, ?> all = sharedPreferencesC2.getAll();
                for (String str : all.keySet()) {
                    if (!"last_upload_time".equals(str)) {
                        jSONObject2.put(str, all.get(str));
                    }
                }
                this.f30886d.fm(jSONObject2.toString());
                sharedPreferencesC2.edit().clear().putLong("last_upload_time", System.currentTimeMillis()).apply();
            } catch (Throwable unused2) {
            }
        }
        this.f30888f = new Handler(looper, this);
    }

    public final SharedPreferences c() {
        return g.f5563a.getSharedPreferences("event_monitor_" + this.f30883a + "_" + this.f30885c + "_" + nb.g.w(), 0);
    }

    @Override // android.os.Handler.Callback
    public final boolean handleMessage(Message message) {
        if (message.what != 10000) {
            return true;
        }
        Objects.toString(this.f30887e);
        JSONObject jSONObject = new JSONObject();
        synchronized (this.f30887e) {
            for (int i10 = 0; i10 < this.f30887e.size(); i10++) {
                try {
                    jSONObject.putOpt(String.valueOf(this.f30887e.keyAt(i10)), Integer.valueOf(this.f30887e.valueAt(i10)));
                } catch (Exception unused) {
                }
            }
        }
        if (jSONObject.length() <= 0) {
            return true;
        }
        SharedPreferences.Editor editorEdit = c().edit();
        editorEdit.putString("event_counts", jSONObject.toString());
        editorEdit.apply();
        return true;
    }
}
