/** * Scan the filesystem for new events. * * @return */ public boolean scanFilesystem() { Set<String> knownPaths = new HashSet<String>(); Set<String> knownNames = new HashSet<String>(); for (StoredEvent s : storedEvents) { knownPaths.add(s.getPath()); knownNames.add(s.getName()); } File eventsDir = getEventsDir(); File[] eventDirFiles = eventsDir.listFiles(); if (eventDirFiles == null) { Log.e(TAG, "Failed to obtain listing for " + eventsDir.getAbsolutePath()); return false; } else { boolean eventsAdded = false; for (File eventDirFile : eventDirFiles) { if (!knownPaths.contains(eventDirFile.getAbsolutePath())) { StoredEvent newEvent = addEventFromPath(knownNames, eventDirFile.getAbsolutePath()); if (newEvent != null) { knownPaths.add(eventDirFile.getAbsolutePath()); eventsAdded = true; Log.i(TAG, "Found " + newEvent); } } } if (eventsAdded) { updateStoredEvents(); } return eventsAdded; } }
public StoredEvent getActiveStoredEvent() { for (StoredEvent se : storedEvents) { if (se.isActive()) { return se; } } return null; }
public boolean unsetActive(StoredEvent e) { try { if (e.equals(activeEvent)) { activeEvent = null; } e.setActive(false); return storedEventsDBHelper.update(e) == 1; } catch (Exception e1) { Log.e(TAG, "Error while updating " + e, e1); return false; } }
public boolean addDownloadedEvent(String path) { Set<String> knownNames = new HashSet<String>(); for (StoredEvent s : storedEvents) { knownNames.add(s.getName()); } if (addEventFromPath(knownNames, path) != null) { updateStoredEvents(); return true; } else { return false; } }
public boolean setAsActive(StoredEvent e) { try { if (activeEvent == null) { activeEvent = e; e.setActive(true); return storedEventsDBHelper.update(e) == 1; } else if (activeEvent.equals(e)) { Log.i(TAG, e + " is already active"); return true; } else { activeEvent.setActive(false); e.setActive(true); boolean up1 = storedEventsDBHelper.update(activeEvent) == 1; boolean up2 = storedEventsDBHelper.update(e) == 1; return up1 && up2; } } catch (Exception e1) { Log.e(TAG, "Error while updating an event"); return false; } }
public boolean load(StoredEvent storedEvent) { File f = new File(storedEvent.getPath()); if (!f.exists()) { return false; } FileInputStream fis = null; try { fis = new FileInputStream(f); storedEvent.setEvent(ModelMarshaller.unmarshallZip(fis)); return true; } catch (Exception e) { Log.e(TAG, "Error while loading " + storedEvent, e); return false; } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { Log.e(TAG, "Error while closing stream", e); } } } }
private StoredEvent addEventFromPath(Set<String> knownNames, String path) { StoredEvent unknownEvent = new StoredEvent(); unknownEvent.setPath(path); if (load(unknownEvent)) { String nameCandidate = unknownEvent.getEvent().getName(); while (knownNames.contains(nameCandidate)) { nameCandidate = nextNameCandidate(nameCandidate); } unknownEvent.setName(nameCandidate); unknownEvent.setVersion(unknownEvent.getEvent().getVersion()); unknownEvent.setUri(unknownEvent.getEvent().getUri()); try { storedEventsDBHelper.insert(unknownEvent); knownNames.add(unknownEvent.getName()); return unknownEvent; } catch (Exception e) { Log.e(TAG, "Error while inserting " + unknownEvent, e); return null; } } else { return null; } }