package com.google.firebase.heartbeatinfo; import android.content.Context; import android.content.SharedPreferences; 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.Map; import java.util.Set; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public class HeartBeatInfoStorage { private final SharedPreferences firebaseSharedPreferences; public HeartBeatInfoStorage(Context context, String str) { this.firebaseSharedPreferences = context.getSharedPreferences("FirebaseHeartBeat".concat(String.valueOf(str)), 0); } /* JADX INFO: Access modifiers changed from: package-private */ public void deleteAllHeartBeats() { synchronized (this) { SharedPreferences.Editor edit = this.firebaseSharedPreferences.edit(); for (Map.Entry entry : this.firebaseSharedPreferences.getAll().entrySet()) { if (entry.getValue() instanceof Set) { edit.remove(entry.getKey()); } } edit.remove("fire-count"); edit.commit(); } } /* JADX INFO: Access modifiers changed from: package-private */ public List getAllHeartBeats() { ArrayList arrayList; synchronized (this) { arrayList = new ArrayList(); for (Map.Entry entry : this.firebaseSharedPreferences.getAll().entrySet()) { if (entry.getValue() instanceof Set) { arrayList.add(HeartBeatResult.create(entry.getKey(), new ArrayList((Set) entry.getValue()))); } } updateGlobalHeartBeat(System.currentTimeMillis()); } return arrayList; } private String getStoredUserAgentString(String str) { synchronized (this) { for (Map.Entry entry : this.firebaseSharedPreferences.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 void removeStoredDate(String str) { synchronized (this) { String storedUserAgentString = getStoredUserAgentString(str); if (storedUserAgentString == null) { return; } HashSet hashSet = new HashSet(this.firebaseSharedPreferences.getStringSet(storedUserAgentString, new HashSet())); hashSet.remove(str); if (hashSet.isEmpty()) { this.firebaseSharedPreferences.edit().remove(storedUserAgentString).commit(); } else { this.firebaseSharedPreferences.edit().putStringSet(storedUserAgentString, hashSet).commit(); } } } /* JADX INFO: Access modifiers changed from: package-private */ public void postHeartBeatCleanUp() { synchronized (this) { String formattedDate = getFormattedDate(System.currentTimeMillis()); this.firebaseSharedPreferences.edit().putString("last-used-date", formattedDate).commit(); removeStoredDate(formattedDate); } } private String getFormattedDate(long j) { String format; synchronized (this) { format = new Date(j).toInstant().atOffset(ZoneOffset.UTC).toLocalDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE); } return format; } /* JADX INFO: Access modifiers changed from: package-private */ public void storeHeartBeat(long j, String str) { synchronized (this) { String formattedDate = getFormattedDate(j); if (this.firebaseSharedPreferences.getString("last-used-date", "").equals(formattedDate)) { return; } long j2 = this.firebaseSharedPreferences.getLong("fire-count", 0L); if (j2 + 1 == 30) { cleanUpStoredHeartBeats(); j2 = this.firebaseSharedPreferences.getLong("fire-count", 0L); } HashSet hashSet = new HashSet(this.firebaseSharedPreferences.getStringSet(str, new HashSet())); hashSet.add(formattedDate); this.firebaseSharedPreferences.edit().putStringSet(str, hashSet).putLong("fire-count", j2 + 1).putString("last-used-date", formattedDate).commit(); } } private void cleanUpStoredHeartBeats() { synchronized (this) { long j = this.firebaseSharedPreferences.getLong("fire-count", 0L); String str = ""; String str2 = null; for (Map.Entry entry : this.firebaseSharedPreferences.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.firebaseSharedPreferences.getStringSet(str, new HashSet())); hashSet.remove(str2); this.firebaseSharedPreferences.edit().putStringSet(str, hashSet).putLong("fire-count", j - 1).commit(); } } void updateGlobalHeartBeat(long j) { synchronized (this) { this.firebaseSharedPreferences.edit().putLong("fire-global", j).commit(); } } boolean isSameDateUtc(long j, long j2) { boolean equals; synchronized (this) { equals = getFormattedDate(j).equals(getFormattedDate(j2)); } return equals; } boolean shouldSendSdkHeartBeat(String str, long j) { synchronized (this) { if (this.firebaseSharedPreferences.contains(str)) { if (isSameDateUtc(this.firebaseSharedPreferences.getLong(str, -1L), j)) { return false; } this.firebaseSharedPreferences.edit().putLong(str, j).commit(); return true; } this.firebaseSharedPreferences.edit().putLong(str, j).commit(); return true; } } /* JADX INFO: Access modifiers changed from: package-private */ public boolean shouldSendGlobalHeartBeat(long j) { boolean shouldSendSdkHeartBeat; synchronized (this) { shouldSendSdkHeartBeat = shouldSendSdkHeartBeat("fire-global", j); } return shouldSendSdkHeartBeat; } }