aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/files/GPXImporter.java
blob: 0b2e49fdc141a89db222e1fbcb7004eb1c392f76 (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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package cgeo.geocaching.files;

import cgeo.geocaching.Geocache;
import cgeo.geocaching.R;
import cgeo.geocaching.SearchResult;
import cgeo.geocaching.Settings;
import cgeo.geocaching.StaticMapsProvider;
import cgeo.geocaching.cgData;
import cgeo.geocaching.activity.IAbstractActivity;
import cgeo.geocaching.activity.Progress;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.Log;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class GPXImporter {
    static final int IMPORT_STEP_START = 0;
    static final int IMPORT_STEP_READ_FILE = 1;
    static final int IMPORT_STEP_READ_WPT_FILE = 2;
    static final int IMPORT_STEP_STORE_STATIC_MAPS = 4;
    static final int IMPORT_STEP_FINISHED = 5;
    static final int IMPORT_STEP_FINISHED_WITH_ERROR = 6;
    static final int IMPORT_STEP_CANCEL = 7;
    static final int IMPORT_STEP_CANCELED = 8;
    static final int IMPORT_STEP_STATIC_MAPS_SKIPPED = 9;

    public static final String GPX_FILE_EXTENSION = ".gpx";
    public static final String LOC_FILE_EXTENSION = ".loc";
    public static final String ZIP_FILE_EXTENSION = ".zip";
    public static final String WAYPOINTS_FILE_SUFFIX = "-wpts";
    public static final String WAYPOINTS_FILE_SUFFIX_AND_EXTENSION = WAYPOINTS_FILE_SUFFIX + GPX_FILE_EXTENSION;

    private static final List<String> GPX_MIME_TYPES = Arrays.asList("text/xml", "application/xml");
    private static final List<String> ZIP_MIME_TYPES = Arrays.asList("application/zip", "application/x-compressed", "application/x-zip-compressed", "application/x-zip", "application/octet-stream");

    private final Progress progress = new Progress(true);

    private final Resources res;
    private final int listId;
    private final IAbstractActivity fromActivity;
    private final Handler importFinishedHandler;

    public GPXImporter(final IAbstractActivity fromActivity, final int listId, final Handler importFinishedHandler) {
        this.listId = listId;
        this.fromActivity = fromActivity;
        res = ((Activity) fromActivity).getResources();
        this.importFinishedHandler = importFinishedHandler;
    }

    /**
     * Import GPX file. Currently supports *.gpx, *.zip (containing gpx files, e.g. PQ queries) or *.loc files.
     *
     * @param file
     *            the file to import
     */
    public void importGPX(final File file) {
        if (StringUtils.endsWithIgnoreCase(file.getName(), GPX_FILE_EXTENSION)) {
            new ImportGpxFileThread(file, listId, importStepHandler, progressHandler).start();
        } else if (StringUtils.endsWithIgnoreCase(file.getName(), ZIP_FILE_EXTENSION)) {
            new ImportGpxZipFileThread(file, listId, importStepHandler, progressHandler).start();
        } else {
            new ImportLocFileThread(file, listId, importStepHandler, progressHandler).start();
        }
    }

    /**
     * Import GPX provided via intent of activity that instantiated this GPXImporter.
     */
    public void importGPX() {
        final ContentResolver contentResolver = ((Activity) fromActivity).getContentResolver();
        final Intent intent = ((Activity) fromActivity).getIntent();
        final Uri uri = intent.getData();

        String mimeType = intent.getType();
        // if mimetype can't be determined (e.g. for emulators email app), derive it from uri file extension
        // contentResolver.getType(uri) doesn't help but throws exception for emulators email app
        //   Permission Denial: reading com.android.email.provider.EmailProvider uri
        // Google search says: there is no solution for this problem
        // Gmail doesn't work at all, see #967
        if (mimeType == null) {
            if (StringUtils.endsWithIgnoreCase(uri.getPath(), GPX_FILE_EXTENSION) || StringUtils.endsWithIgnoreCase(uri.getPath(), LOC_FILE_EXTENSION)) {
                mimeType = "application/xml";
            } else {
                // if we can't determine a better type, default to zip import
                // emulator email sends e.g. content://com.android.email.attachmentprovider/1/1/RAW, mimetype=null
                mimeType = "application/zip";
            }
        }

        Log.i("importGPX: " + uri + ", mimetype=" + mimeType);
        if (GPX_MIME_TYPES.contains(mimeType)) {
            if (StringUtils.endsWithIgnoreCase(uri.getPath(), LOC_FILE_EXTENSION)) {
                new ImportLocAttachmentThread(uri, contentResolver, listId, importStepHandler, progressHandler).start();
            } else {
                new ImportGpxAttachmentThread(uri, contentResolver, listId, importStepHandler, progressHandler).start();
            }
        } else if (ZIP_MIME_TYPES.contains(mimeType)) {
            new ImportGpxZipAttachmentThread(uri, contentResolver, listId, importStepHandler, progressHandler).start();
        } else {
            importFinished();
        }
    }

    static abstract class ImportThread extends Thread {
        final int listId;
        final Handler importStepHandler;
        final CancellableHandler progressHandler;

        protected ImportThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
            this.listId = listId;
            this.importStepHandler = importStepHandler;
            this.progressHandler = progressHandler;
        }

        @Override
        public void run() {
            try {
                importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_START));
                final Collection<Geocache> caches = doImport();
                Log.i("Imported successfully " + caches.size() + " caches.");

                final SearchResult search = new SearchResult(caches);
                // Do not put imported caches into the cachecache. That would consume lots of memory for no benefit.

                if (Settings.isStoreOfflineMaps() || Settings.isStoreOfflineWpMaps()) {
                    importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_STORE_STATIC_MAPS, R.string.gpx_import_store_static_maps, search.getCount()));
                    final boolean finishedWithoutCancel = importStaticMaps(search);
                    // Skip last message if static maps where canceled
                    if (!finishedWithoutCancel) {
                        return;
                    }
                }

                importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED, search.getCount(), 0, search));
            } catch (final IOException e) {
                Log.i("Importing caches failed - error reading data: ", e);
                importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED_WITH_ERROR, R.string.gpx_import_error_io, 0, e.getLocalizedMessage()));
            } catch (final ParserException e) {
                Log.i("Importing caches failed - data format error", e);
                importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED_WITH_ERROR, R.string.gpx_import_error_parser, 0, e.getLocalizedMessage()));
            } catch (final CancellationException e) {
                Log.i("Importing caches canceled");
                importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_CANCELED));
            } catch (final Exception e) {
                Log.e("Importing caches failed - unknown error: ", e);
                importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED_WITH_ERROR, R.string.gpx_import_error_unexpected, 0, e.getLocalizedMessage()));
            }
        }

        protected abstract Collection<Geocache> doImport() throws IOException, ParserException;

        private boolean importStaticMaps(final SearchResult importedCaches) {
            int storedCacheMaps = 0;
            for (final String geocode : importedCaches.getGeocodes()) {
                final Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
                if (cache != null) {
                    Log.d("GPXImporter.ImportThread.importStaticMaps start downloadMaps for cache " + geocode);
                    StaticMapsProvider.downloadMaps(cache);
                } else {
                    Log.d("GPXImporter.ImportThread.importStaticMaps: no data found for " + geocode);
                }
                storedCacheMaps++;
                if (progressHandler.isCancelled()) {
                    return false;
                }
                progressHandler.sendMessage(progressHandler.obtainMessage(0, storedCacheMaps, 0));
            }
            return true;
        }
    }

    static class ImportLocFileThread extends ImportThread {
        private final File file;

        public ImportLocFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
            super(listId, importStepHandler, progressHandler);
            this.file = file;
        }

        @Override
        protected Collection<Geocache> doImport() throws IOException, ParserException {
            Log.i("Import LOC file: " + file.getAbsolutePath());
            importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, (int) file.length()));
            final LocParser parser = new LocParser(listId);
            return parser.parse(file, progressHandler);
        }
    }

    static class ImportLocAttachmentThread extends ImportThread {
        private final Uri uri;
        private final ContentResolver contentResolver;

        public ImportLocAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
            super(listId, importStepHandler, progressHandler);
            this.uri = uri;
            this.contentResolver = contentResolver;
        }

        @Override
        protected Collection<Geocache> doImport() throws IOException, ParserException {
            Log.i("Import LOC from uri: " + uri);
            importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, -1));
            final InputStream is = contentResolver.openInputStream(uri);
            final LocParser parser = new LocParser(listId);
            try {
                return parser.parse(is, progressHandler);
            } finally {
                is.close();
            }
        }
    }

    static abstract class ImportGpxThread extends ImportThread {

        protected ImportGpxThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
            super(listId, importStepHandler, progressHandler);
        }

        @Override
        protected Collection<Geocache> doImport() throws IOException, ParserException {
            try {
                // try to parse cache file as GPX 10
                return doImport(new GPX10Parser(listId));
            } catch (final ParserException pe) {
                // didn't work -> lets try GPX11
                return doImport(new GPX11Parser(listId));
            }
        }

        protected abstract Collection<Geocache> doImport(GPXParser parser) throws IOException, ParserException;
    }

    static class ImportGpxFileThread extends ImportGpxThread {
        private final File cacheFile;

        public ImportGpxFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
            super(listId, importStepHandler, progressHandler);
            this.cacheFile = file;
        }

        @Override
        protected Collection<Geocache> doImport(GPXParser parser) throws IOException, ParserException {
            Log.i("Import GPX file: " + cacheFile.getAbsolutePath());
            importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, (int) cacheFile.length()));
            Collection<Geocache> caches = parser.parse(cacheFile, progressHandler);

            final String wptsFilename = getWaypointsFileNameForGpxFile(cacheFile);
            if (wptsFilename != null) {
                final File wptsFile = new File(cacheFile.getParentFile(), wptsFilename);
                if (wptsFile.canRead()) {
                    Log.i("Import GPX waypoint file: " + wptsFile.getAbsolutePath());
                    importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_WPT_FILE, R.string.gpx_import_loading_waypoints, (int) wptsFile.length()));
                    caches = parser.parse(wptsFile, progressHandler);
                }
            }
            return caches;
        }
    }

    static class ImportGpxAttachmentThread extends ImportGpxThread {
        private final Uri uri;
        private final ContentResolver contentResolver;

        public ImportGpxAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
            super(listId, importStepHandler, progressHandler);
            this.uri = uri;
            this.contentResolver = contentResolver;
        }

        @Override
        protected Collection<Geocache> doImport(GPXParser parser) throws IOException, ParserException {
            Log.i("Import GPX from uri: " + uri);
            importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, -1));
            final InputStream is = contentResolver.openInputStream(uri);
            try {
                return parser.parse(is, progressHandler);
            } finally {
                is.close();
            }
        }
    }

    static abstract class ImportGpxZipThread extends ImportGpxThread {

        protected ImportGpxZipThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
            super(listId, importStepHandler, progressHandler);
        }

        @Override
        protected Collection<Geocache> doImport(GPXParser parser) throws IOException, ParserException {
            Collection<Geocache> caches = Collections.emptySet();
            // can't assume that GPX file comes before waypoint file in zip -> so we need two passes
            // 1. parse GPX files
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(getInputStream()));
            try {
                for (ZipEntry zipEntry = zis.getNextEntry(); zipEntry != null; zipEntry = zis.getNextEntry()) {
                    if (StringUtils.endsWithIgnoreCase(zipEntry.getName(), GPX_FILE_EXTENSION)) {
                        if (!StringUtils.endsWithIgnoreCase(zipEntry.getName(), WAYPOINTS_FILE_SUFFIX_AND_EXTENSION)) {
                            importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, (int) zipEntry.getSize()));
                            caches = parser.parse(new NoCloseInputStream(zis), progressHandler);
                        }
                    } else {
                        throw new ParserException("Imported zip is not a GPX zip file.");
                    }
                    zis.closeEntry();
                }
            } finally {
                zis.close();
            }

            // 2. parse waypoint files
            zis = new ZipInputStream(new BufferedInputStream(getInputStream()));
            try {
                for (ZipEntry zipEntry = zis.getNextEntry(); zipEntry != null; zipEntry = zis.getNextEntry()) {
                    if (StringUtils.endsWithIgnoreCase(zipEntry.getName(), WAYPOINTS_FILE_SUFFIX_AND_EXTENSION)) {
                        importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_WPT_FILE, R.string.gpx_import_loading_waypoints, (int) zipEntry.getSize()));
                        caches = parser.parse(new NoCloseInputStream(zis), progressHandler);
                    }
                    zis.closeEntry();
                }
            } finally {
                zis.close();
            }

            return caches;
        }

        protected abstract InputStream getInputStream() throws IOException;
    }

    static class ImportGpxZipFileThread extends ImportGpxZipThread {
        private final File cacheFile;

        public ImportGpxZipFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
            super(listId, importStepHandler, progressHandler);
            this.cacheFile = file;
            Log.i("Import zipped GPX: " + file);
        }

        @Override
        protected InputStream getInputStream() throws IOException {
            return new FileInputStream(cacheFile);
        }
    }

    static class ImportGpxZipAttachmentThread extends ImportGpxZipThread {
        private final Uri uri;
        private final ContentResolver contentResolver;

        public ImportGpxZipAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
            super(listId, importStepHandler, progressHandler);
            this.uri = uri;
            this.contentResolver = contentResolver;
            Log.i("Import zipped GPX from uri: " + uri);
        }

        @Override
        protected InputStream getInputStream() throws IOException {
            return contentResolver.openInputStream(uri);
        }
    }

    final private CancellableHandler progressHandler = new CancellableHandler() {
        @Override
        public void handleRegularMessage(Message msg) {
            progress.setProgress(msg.arg1);
        }
    };

    final private Handler importStepHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case IMPORT_STEP_START:
                    final Message cancelMessage = importStepHandler.obtainMessage(IMPORT_STEP_CANCEL);
                    progress.show((Context) fromActivity, res.getString(R.string.gpx_import_title_reading_file), res.getString(R.string.gpx_import_loading_caches), ProgressDialog.STYLE_HORIZONTAL, cancelMessage);
                    break;

                case IMPORT_STEP_READ_FILE:
                case IMPORT_STEP_READ_WPT_FILE:
                    progress.setMessage(res.getString(msg.arg1));
                    progress.setMaxProgressAndReset(msg.arg2);
                    break;

                case IMPORT_STEP_STORE_STATIC_MAPS:
                    progress.dismiss();
                    final Message skipMessage = importStepHandler.obtainMessage(IMPORT_STEP_STATIC_MAPS_SKIPPED, msg.arg2, 0);
                    progress.show((Context) fromActivity, res.getString(R.string.gpx_import_title_static_maps), res.getString(R.string.gpx_import_store_static_maps), ProgressDialog.STYLE_HORIZONTAL, skipMessage);
                    progress.setMaxProgressAndReset(msg.arg2);
                    break;

                case IMPORT_STEP_STATIC_MAPS_SKIPPED:
                    progress.dismiss();
                    progressHandler.cancel();
                    final StringBuilder bufferSkipped = new StringBuilder(20);
                    bufferSkipped.append(res.getString(R.string.gpx_import_static_maps_skipped)).append(", ").append(msg.arg1).append(' ').append(res.getString(R.string.gpx_import_caches_imported));
                    fromActivity.helpDialog(res.getString(R.string.gpx_import_title_caches_imported), bufferSkipped.toString());
                    importFinished();
                    break;

                case IMPORT_STEP_FINISHED:
                    progress.dismiss();
                    fromActivity.helpDialog(res.getString(R.string.gpx_import_title_caches_imported), msg.arg1 + " " + res.getString(R.string.gpx_import_caches_imported));
                    importFinished();
                    break;

                case IMPORT_STEP_FINISHED_WITH_ERROR:
                    progress.dismiss();
                    fromActivity.helpDialog(res.getString(R.string.gpx_import_title_caches_import_failed), res.getString(msg.arg1) + "\n\n" + msg.obj);
                    importFinished();
                    break;

                case IMPORT_STEP_CANCEL:
                    progress.dismiss();
                    progressHandler.cancel();
                    break;

                case IMPORT_STEP_CANCELED:
                    final StringBuilder bufferCanceled = new StringBuilder(20);
                    bufferCanceled.append(res.getString(R.string.gpx_import_canceled));
                    fromActivity.showShortToast(bufferCanceled.toString());
                    importFinished();
                    break;

                default:
                    break;
            }
        }
    };

    /**
     * @param gpxfile
     *            the gpx file
     * @return the expected file name of the waypoints file
     */
    static String getWaypointsFileNameForGpxFile(final File gpxfile) {
        if (gpxfile == null || !gpxfile.canRead()) {
            return null;
        }
        final String gpxFileName = gpxfile.getName();
        final File dir = gpxfile.getParentFile();
        final String[] filenameList = dir.list();
        for (final String filename : filenameList) {
            if (!StringUtils.containsIgnoreCase(filename, WAYPOINTS_FILE_SUFFIX)) {
                continue;
            }
            final String expectedGpxFileName = StringUtils.substringBeforeLast(filename, WAYPOINTS_FILE_SUFFIX)
                    + StringUtils.substringAfterLast(filename, WAYPOINTS_FILE_SUFFIX);
            if (gpxFileName.equals(expectedGpxFileName)) {
                return filename;
            }
        }
        return null;
    }

    protected void importFinished() {
        if (importFinishedHandler != null) {
            importFinishedHandler.sendEmptyMessage(0);
        }
    }
}