package com.ea.nimble; import com.ea.nimble.Log; import com.ea.nimble.Persistence; import com.ea.nimble.PersistenceService; import java.io.File; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /* loaded from: classes2.dex */ public class PersistenceServiceImpl extends Component implements IPersistenceService, LogSource { private Encryptor m_encryptor; protected ConcurrentMap m_persistences; @Override // com.ea.nimble.Component public String getComponentId() { return PersistenceService.COMPONENT_ID; } @Override // com.ea.nimble.LogSource public String getLogSourceTitle() { return "Persistence"; } @Override // com.ea.nimble.Component public void setup() { this.m_persistences = new ConcurrentHashMap(); this.m_encryptor = new Encryptor(); } @Override // com.ea.nimble.Component public void suspend() { synchronize(); } @Override // com.ea.nimble.Component public void teardown() { synchronize(); synchronized (Persistence.s_dataLock) { this.m_persistences = null; this.m_encryptor = null; } } @Override // com.ea.nimble.IPersistenceService public Persistence getPersistence(String str, Persistence.Storage storage) { Log.Helper.LOGPUBLICFUNC(this); if (!Utility.validString(str)) { Log.Helper.LOGF(this, "Invalid identifier " + str + " for persistence", new Object[0]); return null; } synchronized (Persistence.s_dataLock) { try { Persistence loadPersistenceById = loadPersistenceById(str, storage); if (loadPersistenceById != null) { return loadPersistenceById; } Persistence persistence = new Persistence(str, storage, this.m_encryptor); this.m_persistences.put(str + "-" + storage.toString(), persistence); return persistence; } catch (Throwable th) { throw th; } } } @Override // com.ea.nimble.IPersistenceService public void removePersistence(String str, Persistence.Storage storage) { Log.Helper.LOGPUBLICFUNC(this); if (!Utility.validString(str)) { Log.Helper.LOGF(this, "Invalid identifier " + str + " for persistence", new Object[0]); return; } cleanPersistenceReference(str, storage); } @Override // com.ea.nimble.IPersistenceService public void cleanPersistenceReference(String str, Persistence.Storage storage) { Log.Helper.LOGPUBLICFUNC(this); if (!Utility.validString(str)) { Log.Helper.LOGF(this, "Invalid identifier " + str + " for persistence", new Object[0]); return; } synchronized (Persistence.s_dataLock) { this.m_persistences.remove(str + "-" + storage.toString()); } } @Override // com.ea.nimble.IPersistenceService public void wipeAllDataAndForceTerminate() { Log.Helper.LOGPUBLICFUNC(this); String documentPath = ApplicationEnvironment.getComponent().getDocumentPath(); String tempPath = ApplicationEnvironment.getComponent().getTempPath(); String cachePath = ApplicationEnvironment.getComponent().getCachePath(); BaseCore.getInstance().onApplicationQuit(); try { Log.Helper.LOGW(this, "!!! Wipe begin !!!", new Object[0]); Log.Helper.LOGD(this, "Clearing DOC folder", new Object[0]); if (deletePath(documentPath)) { Log.Helper.LOGD(this, "Successfully deleted doc directory", new Object[0]); } else { Log.Helper.LOGE(this, "Failed to delete doc directory", new Object[0]); } Log.Helper.LOGD(this, "Clearing TEMP folder", new Object[0]); if (deletePath(tempPath)) { Log.Helper.LOGD(this, "Successfully deleted temp directory", new Object[0]); } else { Log.Helper.LOGE(this, "Failed to delete temp directory", new Object[0]); } Log.Helper.LOGD(this, "Clearing CACHE folder", new Object[0]); if (deletePath(cachePath)) { Log.Helper.LOGD(this, "Successfully deleted cache directory", new Object[0]); } else { Log.Helper.LOGE(this, "Failed to delete cache directory", new Object[0]); } Log.Helper.LOGW(this, "!!! Wipe complete. Force terminating the application !!!", new Object[0]); } catch (Exception e) { Log.Helper.LOGE(this, "!!! Wipe exception !!!\n" + e.toString(), new Object[0]); } System.exit(0); } @Override // com.ea.nimble.IPersistenceService public void migratePersistence(String str, Persistence.Storage storage, String str2, PersistenceService.PersistenceMergePolicy persistenceMergePolicy) { Log.Helper.LOGPUBLICFUNC(this); if (!Utility.validString(str) || !Utility.validString(str2)) { Log.Helper.LOGF(this, "Invalid identifiers " + str + " or " + str2 + " for component persistence", new Object[0]); return; } synchronized (Persistence.s_dataLock) { try { String str3 = str2 + "-" + storage.toString(); Persistence loadPersistenceById = loadPersistenceById(str, storage); if (loadPersistenceById == null) { if (persistenceMergePolicy == PersistenceService.PersistenceMergePolicy.OVERWRITE) { this.m_persistences.remove(str3); String persistencePath = Persistence.getPersistencePath(str2, storage); File file = persistencePath != null ? new File(persistencePath) : null; if (file == null || !file.delete()) { Log.Helper.LOGE(this, "Could not delete file: " + persistencePath, new Object[0]); } } return; } Persistence loadPersistenceById2 = loadPersistenceById(str2, storage); if (loadPersistenceById2 == null) { Persistence persistence = new Persistence(loadPersistenceById, str2); this.m_persistences.put(str3, persistence); persistence.lambda$new$0(); } else { loadPersistenceById2.merge(loadPersistenceById, persistenceMergePolicy); } } catch (Throwable th) { throw th; } } } private void synchronize() { Log.Helper.LOGFUNC(this); Iterator it = this.m_persistences.values().iterator(); while (it.hasNext()) { it.next().lambda$new$0(); } } private Persistence loadPersistenceById(String str, Persistence.Storage storage) { Log.Helper.LOGFUNC(this); synchronized (Persistence.s_dataLock) { try { String str2 = str + "-" + storage.toString(); Persistence persistence = this.m_persistences.get(str2); if (persistence != null) { return persistence; } String persistencePath = Persistence.getPersistencePath(str, storage); File file = persistencePath != null ? new File(persistencePath) : null; if (file != null && file.exists()) { Persistence persistence2 = new Persistence(str, storage, this.m_encryptor); persistence2.restore(false, null); this.m_persistences.put(str2, persistence2); return persistence2; } return null; } finally { } } } private boolean deletePath(String str) { File file = new File(str); if (file.isDirectory()) { for (String str2 : file.list()) { deletePath(new File(file, str2).getPath()); } } return file.delete(); } }