package m4; import android.content.Context; import android.content.SharedPreferences; import android.os.Build; import com.google.android.gms.ads.RequestConfiguration; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; /* loaded from: classes2.dex */ class q { /* renamed from: a, reason: collision with root package name */ private final SharedPreferences f11852a; public q(Context context, String str) { this.f11852a = context.getSharedPreferences("FirebaseHeartBeat" + str, 0); } private synchronized void a() { long j10 = this.f11852a.getLong("fire-count", 0L); String str = RequestConfiguration.MAX_AD_CONTENT_RATING_UNSPECIFIED; String str2 = null; for (Map.Entry entry : this.f11852a.getAll().entrySet()) { if (entry.getValue() instanceof Set) { for (String str3 : (Set) entry.getValue()) { if (str2 == null || str2.compareTo(str3) > 0) { str = entry.getKey(); str2 = str3; } } } } HashSet hashSet = new HashSet(this.f11852a.getStringSet(str, new HashSet())); hashSet.remove(str2); this.f11852a.edit().putStringSet(str, hashSet).putLong("fire-count", j10 - 1).commit(); } private synchronized String d(long j10) { Instant instant; ZoneOffset zoneOffset; OffsetDateTime atOffset; LocalDateTime localDateTime; DateTimeFormatter dateTimeFormatter; String format; if (Build.VERSION.SDK_INT < 26) { return new SimpleDateFormat("yyyy-MM-dd", Locale.UK).format(new Date(j10)); } instant = new Date(j10).toInstant(); zoneOffset = ZoneOffset.UTC; atOffset = instant.atOffset(zoneOffset); localDateTime = atOffset.toLocalDateTime(); dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE; format = localDateTime.format(dateTimeFormatter); return format; } private synchronized String e(String str) { for (Map.Entry entry : this.f11852a.getAll().entrySet()) { if (entry.getValue() instanceof Set) { Iterator it = ((Set) entry.getValue()).iterator(); while (it.hasNext()) { if (str.equals((String) it.next())) { return entry.getKey(); } } } } return null; } private synchronized void h(String str) { String e10 = e(str); if (e10 == null) { return; } HashSet hashSet = new HashSet(this.f11852a.getStringSet(e10, new HashSet())); hashSet.remove(str); if (hashSet.isEmpty()) { this.f11852a.edit().remove(e10).commit(); } else { this.f11852a.edit().putStringSet(e10, hashSet).commit(); } } private synchronized void m(String str, String str2) { h(str2); HashSet hashSet = new HashSet(this.f11852a.getStringSet(str, new HashSet())); hashSet.add(str2); this.f11852a.edit().putStringSet(str, hashSet).commit(); } synchronized void b() { SharedPreferences.Editor edit = this.f11852a.edit(); int i10 = 0; for (Map.Entry entry : this.f11852a.getAll().entrySet()) { if (entry.getValue() instanceof Set) { Set set = (Set) entry.getValue(); String d10 = d(System.currentTimeMillis()); String key = entry.getKey(); if (set.contains(d10)) { HashSet hashSet = new HashSet(); hashSet.add(d10); i10++; edit.putStringSet(key, hashSet); } else { edit.remove(key); } } } if (i10 == 0) { edit.remove("fire-count"); } else { edit.putLong("fire-count", i10); } edit.commit(); } synchronized List c() { ArrayList arrayList; arrayList = new ArrayList(); for (Map.Entry entry : this.f11852a.getAll().entrySet()) { if (entry.getValue() instanceof Set) { HashSet hashSet = new HashSet((Set) entry.getValue()); hashSet.remove(d(System.currentTimeMillis())); if (!hashSet.isEmpty()) { arrayList.add(r.a(entry.getKey(), new ArrayList(hashSet))); } } } l(System.currentTimeMillis()); return arrayList; } synchronized boolean f(long j10, long j11) { return d(j10).equals(d(j11)); } synchronized void g() { String d10 = d(System.currentTimeMillis()); this.f11852a.edit().putString("last-used-date", d10).commit(); h(d10); } synchronized boolean i(long j10) { return j("fire-global", j10); } synchronized boolean j(String str, long j10) { if (!this.f11852a.contains(str)) { this.f11852a.edit().putLong(str, j10).commit(); return true; } if (f(this.f11852a.getLong(str, -1L), j10)) { return false; } this.f11852a.edit().putLong(str, j10).commit(); return true; } synchronized void k(long j10, String str) { String d10 = d(j10); if (this.f11852a.getString("last-used-date", RequestConfiguration.MAX_AD_CONTENT_RATING_UNSPECIFIED).equals(d10)) { String e10 = e(d10); if (e10 == null) { return; } if (e10.equals(str)) { return; } m(str, d10); return; } long j11 = this.f11852a.getLong("fire-count", 0L); if (j11 + 1 == 30) { a(); j11 = this.f11852a.getLong("fire-count", 0L); } HashSet hashSet = new HashSet(this.f11852a.getStringSet(str, new HashSet())); hashSet.add(d10); this.f11852a.edit().putStringSet(str, hashSet).putLong("fire-count", j11 + 1).putString("last-used-date", d10).commit(); } synchronized void l(long j10) { this.f11852a.edit().putLong("fire-global", j10).commit(); } }