/**
   * Return the first of the cache coordinates, the waypoint coordinates or the extra coordinates.
   * <code>null</code> entities are skipped.
   *
   * @param cache a cache
   * @param waypoint a waypoint
   * @param coords extra coordinates
   * @return the first non-null coordinates, or null if none are set
   */
  private static Geopoint getCoordinates(
      final cgCache cache, final cgWaypoint waypoint, final Geopoint coords) {
    if (cache != null && cache.getCoords() != null) {
      return cache.getCoords();
    }

    if (waypoint != null && waypoint.getCoords() != null) {
      return waypoint.getCoords();
    }

    return coords;
  }
Example #2
0
    @Override
    protected Boolean doInBackground(Void... params) {
      // quick check for being able to write the GPX file
      if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return false;
      }

      try {
        final File exportLocation = new File(Settings.getGpxExportDir());
        exportLocation.mkdirs();

        final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        exportFile =
            new File(
                Settings.getGpxExportDir()
                    + File.separatorChar
                    + "export_"
                    + fileNameDateFormat.format(new Date())
                    + ".gpx");

        gpx = new BufferedWriter(new FileWriter(exportFile));

        gpx.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        gpx.write(
            "<gpx version=\"1.0\" creator=\"c:geo - http://www.cgeo.org\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/0\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0/1 http://www.groundspeak.com/cache/1/0/1/cache.xsd\">");

        for (int i = 0; i < caches.size(); i++) {
          // reload the cache. otherwise logs, attributes and other detailed information is not
          // available
          final cgCache cache =
              cgeoapplication
                  .getInstance()
                  .loadCache(caches.get(i).getGeocode(), LoadFlags.LOAD_ALL_DB_ONLY);

          gpx.write("<wpt ");
          gpx.write("lat=\"");
          gpx.write(Double.toString(cache.getCoords().getLatitude()));
          gpx.write("\" ");
          gpx.write("lon=\"");
          gpx.write(Double.toString(cache.getCoords().getLongitude()));
          gpx.write("\">");

          final Date hiddenDate = cache.getHiddenDate();
          if (hiddenDate != null) {
            gpx.write("<time>");
            gpx.write(StringEscapeUtils.escapeXml(dateFormatZ.format(hiddenDate)));
            gpx.write("</time>");
          }

          gpx.write("<name>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getGeocode()));
          gpx.write("</name>");

          gpx.write("<desc>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getName()));
          gpx.write("</desc>");

          gpx.write("<url>");
          gpx.write(cache.getUrl());
          gpx.write("</url>");

          gpx.write("<urlname>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getName()));
          gpx.write("</urlname>");

          gpx.write("<sym>");
          gpx.write(cache.isFound() ? "Geocache Found" : "Geocache");
          gpx.write("</sym>");

          gpx.write("<type>");
          gpx.write(StringEscapeUtils.escapeXml("Geocache|" + cache.getType().pattern));
          gpx.write("</type>");

          gpx.write("<groundspeak:cache ");
          gpx.write("id=\"");
          gpx.write(cache.getCacheId());
          gpx.write("\" available=\"");
          gpx.write(!cache.isDisabled() ? "True" : "False");
          gpx.write("\" archived=\"");
          gpx.write(cache.isArchived() ? "True" : "False");
          gpx.write("\" ");
          gpx.write("xmlns:groundspeak=\"http://www.groundspeak.com/cache/1/0/1\">");

          gpx.write("<groundspeak:name>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getName()));
          gpx.write("</groundspeak:name>");

          gpx.write("<groundspeak:placed_by>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getOwnerDisplayName()));
          gpx.write("</groundspeak:placed_by>");

          gpx.write("<groundspeak:owner>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getOwnerUserId()));
          gpx.write("</groundspeak:owner>");

          gpx.write("<groundspeak:type>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getType().pattern));
          gpx.write("</groundspeak:type>");

          gpx.write("<groundspeak:container>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getSize().id));
          gpx.write("</groundspeak:container>");

          writeAttributes(cache);

          gpx.write("<groundspeak:difficulty>");
          gpx.write(Float.toString(cache.getDifficulty()));
          gpx.write("</groundspeak:difficulty>");

          gpx.write("<groundspeak:terrain>");
          gpx.write(Float.toString(cache.getTerrain()));
          gpx.write("</groundspeak:terrain>");

          gpx.write("<groundspeak:country>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getLocation()));
          gpx.write("</groundspeak:country>");

          gpx.write(
              "<groundspeak:state></groundspeak:state>"); // c:geo cannot manage 2 separate fields,
                                                          // so we export as country

          gpx.write("<groundspeak:short_description html=\"");
          gpx.write(BaseUtils.containsHtml(cache.getShortDescription()) ? "True" : "False");
          gpx.write("\">");
          gpx.write(StringEscapeUtils.escapeXml(cache.getShortDescription()));
          gpx.write("</groundspeak:short_description>");

          gpx.write("<groundspeak:long_description html=\"");
          gpx.write(BaseUtils.containsHtml(cache.getDescription()) ? "True" : "False");
          gpx.write("\">");
          gpx.write(StringEscapeUtils.escapeXml(cache.getDescription()));
          gpx.write("</groundspeak:long_description>");

          gpx.write("<groundspeak:encoded_hints>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getHint()));
          gpx.write("</groundspeak:encoded_hints>");

          writeLogs(cache);

          gpx.write("</groundspeak:cache>");

          gpx.write("</wpt>");

          writeWaypoints(cache);

          publishProgress(i + 1);
        }

        gpx.write("</gpx>");

        gpx.close();
      } catch (Exception e) {
        Log.e("GpxExport.ExportTask export", e);

        if (gpx != null) {
          try {
            gpx.close();
          } catch (IOException ee) {
          }
        }

        // delete partial gpx file on error
        if (exportFile.exists()) {
          exportFile.delete();
        }

        return false;
      }

      return true;
    }