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"); } }
@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; }
@SuppressWarnings("unchecked") void update(Location loc, AirspaceLookupIf lookup) { if (loc == null || lookup == null) return; final float gs = (float) (loc.getSpeed() * 3.6 / 1.852); if (gs < 4) { ++count_standstill; if (last_update == 0) last_update = SystemClock.elapsedRealtime(); if (count_standstill > 20 && SystemClock.elapsedRealtime() - last_update > 1000l * 60 * 10) { last_update = SystemClock.elapsedRealtime(); count_standstill = 0; boolean needswork = false; for (AirspaceArea area : lookup.getAllAirspace()) { if (area.cleared != 0) { area.cleared = 0; needswork = true; } } if (needswork) save(lookup); } } else { last_update = SystemClock.elapsedRealtime(); count_standstill = 0; } }