// Opens/creates a coordinate file with the given name public CoordFile(Context context, String filename) { mContext = context; Filename = filename; if (Filename == null) { Filename = String.format("CoordLog_%s.txt", DateStrings.GetDateTimeString(Calendar.getInstance())); } try { mFile = new File(filename); boolean exists = mFile.exists(); mWriter = new FileWriter(filename, true); if (exists) { ReadFile(); } else { String entry = "Date,Ticks,Longitude,Latitude,Altitude,Accuracy,Bearing,Speed\n"; mWriter.append(entry); Toast t = Toast.makeText( mContext, String.format("Created output file %s", filename), Toast.LENGTH_SHORT); t.show(); } } catch (Exception e) { ErrorFile.WriteException(e, context); } }
private void ReadFile() { long ticks = 0; try { BufferedReader br = new BufferedReader(new FileReader(mFile)); char[] buf = new char[(int) mFile.length()]; br.read(buf, 0, (int) mFile.length()); br.close(); String text = String.copyValueOf(buf); String[] lines = text.split("\n"); mCoords.clear(); for (int i = 0; i < lines.length; i++) { String[] parts = lines[i].split(","); if (parts.length < 8) continue; if (parts[0].trim().equals("Date")) continue; Location location = new Location("File"); int strength = 0; try { ticks = Long.parseLong(parts[1].trim()); location.setTime(ticks); location.setLongitude(Double.parseDouble(parts[2].trim())); location.setLatitude(Double.parseDouble(parts[3].trim())); location.setAltitude(Double.parseDouble(parts[4].trim())); location.setAccuracy(Float.parseFloat(parts[5].trim())); location.setBearing(Float.parseFloat(parts[6].trim())); location.setSpeed(Float.parseFloat(parts[7].trim())); strength = Integer.parseInt(parts[8].trim()); } catch (Exception e) { ErrorFile.WriteException(e, null); } mCoords.add(new GPSCoordinate(location, strength)); } } catch (Exception e) { ErrorFile.WriteException(e, mContext); } }
public void WriteEntry(Location location, int strength) { if (mClosing) return; mWriting = true; try { GPSCoordinate coord = new GPSCoordinate(location, strength); mCoords.add(coord); mWriter.append(coord.ToString() + ", " + strength + "\n"); // Toast t = Toast.makeText(mContext, "Wrote log entry", Toast.LENGTH_SHORT); // t.show(); } catch (Exception e) { ErrorFile.WriteException(e, mContext); } mWriting = false; }
public void Close() { mClosing = true; if (mWriter != null) { for (int i = 0; i < 10; i++) { if (!mWriting) break; try { Thread.sleep(100); } catch (Exception e) { } } try { mWriter.close(); } catch (Exception e) { ErrorFile.WriteException(e, mContext); } mWriter = null; } }