package com.google.firebase.messaging; import android.content.SharedPreferences; import android.text.TextUtils; import android.util.Log; import java.util.ArrayDeque; import java.util.Iterator; import java.util.concurrent.Executor; /* loaded from: classes3.dex */ public final class SharedPreferencesQueue { public final String itemSeparator; public final String queueName; public final SharedPreferences sharedPreferences; public final Executor syncExecutor; public final ArrayDeque internalQueue = new ArrayDeque(); public boolean bulkOperation = false; public SharedPreferencesQueue(SharedPreferences sharedPreferences, String str, String str2, Executor executor) { this.sharedPreferences = sharedPreferences; this.queueName = str; this.itemSeparator = str2; this.syncExecutor = executor; } public static SharedPreferencesQueue createInstance(SharedPreferences sharedPreferences, String str, String str2, Executor executor) { SharedPreferencesQueue sharedPreferencesQueue = new SharedPreferencesQueue(sharedPreferences, str, str2, executor); sharedPreferencesQueue.initQueue(); return sharedPreferencesQueue; } public final void initQueue() { synchronized (this.internalQueue) { try { this.internalQueue.clear(); String string = this.sharedPreferences.getString(this.queueName, ""); if (!TextUtils.isEmpty(string) && string.contains(this.itemSeparator)) { String[] split = string.split(this.itemSeparator, -1); if (split.length == 0) { Log.e("FirebaseMessaging", "Corrupted queue. Please check the queue contents and item separator provided"); } for (String str : split) { if (!TextUtils.isEmpty(str)) { this.internalQueue.add(str); } } } } finally { } } } public final boolean checkAndSyncState(boolean z) { if (z && !this.bulkOperation) { syncStateAsync(); } return z; } public final void syncStateAsync() { this.syncExecutor.execute(new Runnable() { // from class: com.google.firebase.messaging.SharedPreferencesQueue$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { SharedPreferencesQueue.this.syncState(); } }); } public final void syncState() { synchronized (this.internalQueue) { this.sharedPreferences.edit().putString(this.queueName, serialize()).commit(); } } public String serialize() { StringBuilder sb = new StringBuilder(); Iterator it = this.internalQueue.iterator(); while (it.hasNext()) { sb.append((String) it.next()); sb.append(this.itemSeparator); } return sb.toString(); } public boolean remove(Object obj) { boolean checkAndSyncState; synchronized (this.internalQueue) { checkAndSyncState = checkAndSyncState(this.internalQueue.remove(obj)); } return checkAndSyncState; } public String peek() { String str; synchronized (this.internalQueue) { str = (String) this.internalQueue.peek(); } return str; } }