aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/export/GpxExport.java
blob: 73287dae0e3cd43301f3a11756f17a808b05162a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package cgeo.geocaching.export;

import cgeo.geocaching.LogEntry;
import cgeo.geocaching.R;
import cgeo.geocaching.Settings;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgWaypoint;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.activity.Progress;
import cgeo.geocaching.enumerations.CacheAttribute;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.utils.BaseUtils;
import cgeo.geocaching.utils.Log;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

class GpxExport extends AbstractExport {
    private static final SimpleDateFormat dateFormatZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

    protected GpxExport() {
        super(getString(R.string.export_gpx));
    }

    @Override
    public void export(final List<cgCache> caches, final Activity activity) {
        if (null == activity) {
            // No activity given, so no user interaction possible.
            // Start export with default parameters.
            new ExportTask(caches, activity).execute((Void) null);

        } else {
            // Show configuration dialog
            new ExportOptionsDialog(caches, activity).show();
        }
    }

    /**
     * A dialog to allow the user to set options for the export.
     *
     * Currently available option is: opening of share menu after successful export
     */
    private class ExportOptionsDialog extends AlertDialog {
        public ExportOptionsDialog(final List<cgCache> caches, final Activity activity) {
            super(activity);

            View layout = activity.getLayoutInflater().inflate(R.layout.gpx_export_dialog, null);
            setView(layout);

            final TextView text = (TextView) layout.findViewById(R.id.info);
            text.setText(getString(R.string.export_gpx_info, Settings.getGpxExportDir()));

            final CheckBox shareOption = (CheckBox) layout.findViewById(R.id.share);

            shareOption.setChecked(Settings.getShareAfterExport());

            shareOption.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Settings.setShareAfterExport(shareOption.isChecked());
                }
            });

            layout.findViewById(R.id.export).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    new ExportTask(caches, activity).execute((Void) null);
                }
            });
        }
    }

    private class ExportTask extends AsyncTask<Void, Integer, Boolean> {
        private final List<cgCache> caches;
        private final Activity activity;
        private final Progress progress = new Progress();
        private File exportFile;
        private Writer gpx;

        /**
         * Instantiates and configures the task for exporting field notes.
         *
         * @param caches
         *            The {@link List} of {@link cgCache} to be exported
         * @param activity
         *            optional: Show a progress bar and toasts
         */
        public ExportTask(final List<cgCache> caches, final Activity activity) {
            this.caches = caches;
            this.activity = activity;
        }

        @Override
        protected void onPreExecute() {
            if (null != activity) {
                progress.show(activity, null, getString(R.string.export) + ": " + getName(), ProgressDialog.STYLE_HORIZONTAL, null);
                progress.setMaxProgressAndReset(caches.size());
            }
        }

        @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;
        }

        private void writeWaypoints(final cgCache cache) throws IOException {
            List<cgWaypoint> waypoints = cache.getWaypoints();
            List<cgWaypoint> ownWaypoints = new ArrayList<cgWaypoint>(waypoints.size());
            List<cgWaypoint> originWaypoints = new ArrayList<cgWaypoint>(waypoints.size());
            for (cgWaypoint wp : cache.getWaypoints()) {
                if (wp.isUserDefined()) {
                    ownWaypoints.add(wp);
                } else {
                    originWaypoints.add(wp);
                }
            }
            int maxPrefix = 0;
            for (cgWaypoint wp : originWaypoints) {
                String prefix = wp.getPrefix();
                try {
                    maxPrefix = Math.max(Integer.parseInt(prefix), maxPrefix);
                } catch (NumberFormatException ex) {
                    Log.e("Unexpected origin waypoint prefix='" + prefix + "'", ex);
                }
                writeCacheWaypoint(wp, prefix);
            }
            for (cgWaypoint wp : ownWaypoints) {
                maxPrefix++;
                String prefix = StringUtils.leftPad(String.valueOf(maxPrefix), 2, '0');
                writeCacheWaypoint(wp, prefix);
            }
        }

        /**
         * Writes one waypoint entry for cache waypoint.
         *
         * @param cache
         *            The
         * @param wp
         * @param prefix
         * @throws IOException
         */
        private void writeCacheWaypoint(final cgWaypoint wp, final String prefix) throws IOException {
            gpx.write("<wpt lat=\"");
            final Geopoint coords = wp.getCoords();
            gpx.write(coords != null ? Double.toString(coords.getLatitude()) : ""); // TODO: check whether is the best way to handle unknown waypoint coordinates
            gpx.write("\" lon=\"");
            gpx.write(coords != null ? Double.toString(coords.getLongitude()) : "");
            gpx.write("\">");

            gpx.write("<name>");
            gpx.write(StringEscapeUtils.escapeXml(prefix));
            gpx.write(StringEscapeUtils.escapeXml(wp.getGeocode().substring(2)));
            gpx.write("</name>");

            gpx.write("<cmt>");
            gpx.write(StringEscapeUtils.escapeXml(wp.getNote()));
            gpx.write("</cmt>");

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

            gpx.write("<sym>");
            gpx.write(StringEscapeUtils.escapeXml(wp.getWaypointType().toString())); //TODO: Correct identifier string
            gpx.write("</sym>");

            gpx.write("<type>Waypoint|");
            gpx.write(StringEscapeUtils.escapeXml(wp.getWaypointType().toString())); //TODO: Correct identifier string
            gpx.write("</type>");

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

        private void writeLogs(final cgCache cache) throws IOException {
            if (cache.getLogs().size() <= 0) {
                return;
            }
            gpx.write("<groundspeak:logs>");

            for (LogEntry log : cache.getLogs()) {
                gpx.write("<groundspeak:log id=\"");
                gpx.write(Integer.toString(log.id));
                gpx.write("\">");

                gpx.write("<groundspeak:date>");
                gpx.write(StringEscapeUtils.escapeXml(dateFormatZ.format(new Date(log.date))));
                gpx.write("</groundspeak:date>");

                gpx.write("<groundspeak:type>");
                gpx.write(StringEscapeUtils.escapeXml(log.type.type));
                gpx.write("</groundspeak:type>");

                gpx.write("<groundspeak:finder id=\"\">");
                gpx.write(StringEscapeUtils.escapeXml(log.author));
                gpx.write("</groundspeak:finder>");

                gpx.write("<groundspeak:text encoded=\"False\">");
                gpx.write(StringEscapeUtils.escapeXml(log.log));
                gpx.write("</groundspeak:text>");

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

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

        private void writeAttributes(final cgCache cache) throws IOException {
            if (!cache.hasAttributes()) {
                return;
            }
            //TODO: Attribute conversion required: English verbose name, gpx-id
            gpx.write("<groundspeak:attributes>");

            for (String attribute : cache.getAttributes()) {
                final CacheAttribute attr = CacheAttribute.getByGcRawName(CacheAttribute.trimAttributeName(attribute));
                final boolean enabled = CacheAttribute.isEnabled(attribute);

                gpx.write("<groundspeak:attribute id=\"");
                gpx.write(Integer.toString(attr.id));
                gpx.write("\" inc=\"");
                if (enabled) {
                    gpx.write('1');
                } else {
                    gpx.write('0');
                }
                gpx.write("\">");
                gpx.write(StringEscapeUtils.escapeXml(attr.getL10n(enabled)));
                gpx.write("</groundspeak:attribute>");
            }

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

        @Override
        protected void onPostExecute(Boolean result) {
            if (null != activity) {
                progress.dismiss();
                if (result) {
                    ActivityMixin.showToast(activity, getName() + ' ' + getString(R.string.export_exportedto) + ": " + exportFile.toString());
                    if (Settings.getShareAfterExport()) {
                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportFile));
                        shareIntent.setType("application/xml");
                        activity.startActivity(Intent.createChooser(shareIntent, getString(R.string.export_gpx_to)));
                    }
                } else {
                    ActivityMixin.showToast(activity, getString(R.string.export_failed));
                }
            }
        }

        @Override
        protected void onProgressUpdate(Integer... status) {
            if (null != activity) {
                progress.setProgress(status[0]);
            }
        }
    }
}