|
1 | 1 | package net.osmtracker.gpx; |
2 | 2 |
|
3 | | -import android.content.ContentResolver; |
4 | | -import android.content.ContentUris; |
5 | 3 | import android.content.Context; |
6 | 4 | import android.database.Cursor; |
7 | 5 | import android.preference.PreferenceManager; |
8 | 6 | import android.util.Log; |
9 | 7 |
|
10 | | -import androidx.annotation.NonNull; |
11 | | - |
12 | 8 | import net.osmtracker.OSMTracker; |
13 | 9 | import net.osmtracker.db.DataHelper; |
14 | | -import net.osmtracker.db.TrackContentProvider; |
15 | 10 | import net.osmtracker.exception.ExportTrackException; |
16 | 11 |
|
17 | 12 | import java.io.File; |
18 | | -import java.io.IOException; |
19 | 13 | import java.util.Date; |
20 | 14 |
|
21 | 15 | /** |
|
26 | 20 | public abstract class ExportToTempFileTask extends ExportTrackTask { |
27 | 21 |
|
28 | 22 | private static final String TAG = ExportToTempFileTask.class.getSimpleName(); |
29 | | - |
| 23 | + |
30 | 24 | private final File tmpFile; |
31 | 25 | private String filename; |
32 | | - |
| 26 | + |
33 | 27 | public ExportToTempFileTask(Context context, long trackId) { |
34 | 28 | super(context, trackId); |
| 29 | + String desiredOutputFormat = PreferenceManager.getDefaultSharedPreferences(context).getString( |
| 30 | + OSMTracker.Preferences.KEY_OUTPUT_FILENAME, |
| 31 | + OSMTracker.Preferences.VAL_OUTPUT_FILENAME); |
35 | 32 | try { |
36 | | - String exportLabelName = PreferenceManager.getDefaultSharedPreferences(context).getString( |
37 | | - OSMTracker.Preferences.KEY_OUTPUT_FILENAME_LABEL, OSMTracker.Preferences.VAL_OUTPUT_FILENAME_LABEL); |
38 | 33 | String trackName = new DataHelper(context).getTrackById(trackId).getName(); |
39 | | - long date = new DataHelper(context).getTrackById(trackId).getTrackDate(); |
40 | 34 |
|
41 | | - String formattedTrackStartDate = DataHelper.FILENAME_FORMATTER.format(new Date(date)); |
| 35 | + long startDate = new DataHelper(context).getTrackById(trackId).getTrackDate(); |
| 36 | + String formattedTrackStartDate = DataHelper.FILENAME_FORMATTER.format(new Date(startDate)); |
42 | 37 |
|
43 | 38 | // Create temporary file |
44 | | - String namefinal = createFile(trackName, formattedTrackStartDate, exportLabelName); |
45 | | - tmpFile = new File(context.getCacheDir(),namefinal+".gpx"); |
| 39 | + String tmpFilename = super.formatGpxFilename(desiredOutputFormat, trackName, formattedTrackStartDate); |
| 40 | + tmpFile = new File(context.getCacheDir(),tmpFilename + DataHelper.EXTENSION_GPX); |
46 | 41 | Log.d(TAG, "Temporary file: "+ tmpFile.getAbsolutePath()); |
47 | 42 | } catch (Exception ioe) { |
48 | 43 | Log.e(TAG, "Could not create temporary file", ioe); |
49 | 44 | throw new IllegalStateException("Could not create temporary file", ioe); |
50 | 45 | } |
51 | 46 | } |
52 | | - //create temporary file |
53 | | - private String createFile(String sanitizedTrackName, String formattedTrackStartDate, String exportLabelName) throws IOException{ |
54 | | - String result = ""; |
55 | | - String desiredOutputFormat = PreferenceManager.getDefaultSharedPreferences(context).getString( |
56 | | - OSMTracker.Preferences.KEY_OUTPUT_FILENAME, |
57 | | - OSMTracker.Preferences.VAL_OUTPUT_FILENAME); |
58 | | - |
59 | | - boolean thereIsTrackName = sanitizedTrackName != null && sanitizedTrackName.length() >= 1; |
60 | | - switch(desiredOutputFormat){ |
61 | | - case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_NAME: |
62 | | - if(thereIsTrackName) |
63 | | - result += sanitizedTrackName; |
64 | | - else |
65 | | - result += formattedTrackStartDate; // fallback case |
66 | | - break; |
67 | | - case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_NAME_DATE: |
68 | | - if(thereIsTrackName) |
69 | | - if(sanitizedTrackName.equals(formattedTrackStartDate)) { |
70 | | - result += sanitizedTrackName; |
71 | | - }else{ |
72 | | - result += sanitizedTrackName + "_" + formattedTrackStartDate; // name is not equal |
73 | | - } |
74 | | - else |
75 | | - result += formattedTrackStartDate; |
76 | | - break; |
77 | | - case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_DATE_NAME: |
78 | | - if(thereIsTrackName){ |
79 | | - if(sanitizedTrackName.equals(formattedTrackStartDate)){ |
80 | | - result += formattedTrackStartDate; |
81 | | - }else{ |
82 | | - result += formattedTrackStartDate + "_" + sanitizedTrackName; |
83 | | - } |
84 | | - }else{ |
85 | | - result += formattedTrackStartDate; |
86 | | - } |
87 | | - break; |
88 | | - case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_DATE: |
89 | | - result += formattedTrackStartDate; |
90 | | - break; |
91 | | - } |
92 | | - if(!(exportLabelName.equals(""))) |
93 | | - result += "_"+ exportLabelName; |
94 | | - return result; |
95 | | - } |
96 | 47 |
|
97 | 48 | @Override |
98 | 49 | protected File getExportDirectory(Date startDate) throws ExportTrackException { |
|
0 commit comments