public void load(ArrayList<AirspaceArea> areas, String storage) { this.storage = storage; try { File extpath = Storage.getStorage(storage); File srcpath = new File(extpath, Config.path + "clearance.dat"); FileInputStream fs = new FileInputStream(srcpath); DataInputStream ds = new DataInputStream(fs); HashMap<String, ArrayList<AirspaceArea>> id2areas = new HashMap<String, ArrayList<AirspaceArea>>(); for (AirspaceArea a : areas) { a.cleared = 0; String id = a.getIdentity(); ArrayList<AirspaceArea> l = id2areas.get(id); if (l == null) { l = new ArrayList<AirspaceArea>(); id2areas.put(id, l); } l.add(a); } int version = ds.readByte(); if (version != 1) throw new RuntimeException("Unknown on-disk format"); int len = ds.readInt(); for (int i = 0; i < len; ++i) { String id = ds.readUTF(); long when = ds.readLong(); ArrayList<AirspaceArea> l = id2areas.get(id); if (l != null) for (AirspaceArea a : l) a.cleared = when; } } catch (Throwable e) { e.printStackTrace(); Log.i("fplan", "Couldn't load clearances"); } }
private HistoryController(Context context) { this.context = context; historyStorage = Storage.getStorage(PreferencesHistoryItemStorage.class, context); if (historyStorage == null) throw new RuntimeException("HistoryStorage wasn't created in the history controller!"); this.listeners = new LinkedList<HistoryListener>(); Log.d("HistoryController", "Created controller"); }
@Override protected Void doInBackground(ArrayList<AirspaceArea>... params) { try { if (params.length == 0) return null; ArrayList<AirspaceArea> last = params[params.length - 1]; File extpath = Storage.getStorage(storage); File tmppath = new File(extpath, Config.path + "clearance.dat.tmp"); File dstpath = new File(extpath, Config.path + "clearance.dat"); FileOutputStream fs = new FileOutputStream(tmppath); DataOutputStream ds = new DataOutputStream(fs); ds.writeByte(1); // Version ArrayList<String> cleared = new ArrayList<String>(); ArrayList<Long> clearedwhen = new ArrayList<Long>(); long now = new Date().getTime(); for (AirspaceArea a : last) { long ago = now - a.cleared; if (ago > Config.clearance_valid_time) a.cleared = 0; if (a.cleared != 0) { cleared.add(a.getIdentity()); clearedwhen.add(a.cleared); } } ds.writeInt(cleared.size()); for (int i = 0; i < cleared.size(); ++i) { String id = cleared.get(i); Long when = clearedwhen.get(i); ds.writeUTF(id); ds.writeLong(when); } tmppath.renameTo(dstpath); } catch (Throwable e) { e.printStackTrace(); Log.i("fplan", "Couldn't persist clearances"); } return null; }