-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathExportToStorageTask.java
More file actions
111 lines (87 loc) · 3.58 KB
/
ExportToStorageTask.java
File metadata and controls
111 lines (87 loc) · 3.58 KB
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
package net.osmtracker.gpx;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import net.osmtracker.OSMTracker;
import net.osmtracker.R;
import net.osmtracker.db.TrackContentProvider;
import net.osmtracker.exception.ExportTrackException;
import java.io.File;
import java.util.Date;
/**
* Exports to the external storage / SD card
* in a folder defined by the user.
*/
public class ExportToStorageTask extends ExportTrackTask {
private static final String TAG = ExportToStorageTask.class.getSimpleName();
public ExportToStorageTask(Context context, long... trackId) {
super(context, trackId);
}
@Override
protected File getExportDirectory(Date startDate) throws ExportTrackException {
File sdRoot = Environment.getExternalStorageDirectory();
// The location that the user has specified gpx files
// and associated content to be written
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String userGPXExportDirectoryName = prefs.getString(
OSMTracker.Preferences.KEY_STORAGE_DIR, OSMTracker.Preferences.VAL_STORAGE_DIR);
boolean directoryPerTrack = prefs.getBoolean(OSMTracker.Preferences.KEY_OUTPUT_DIR_PER_TRACK,
OSMTracker.Preferences.VAL_OUTPUT_GPX_OUTPUT_DIR_PER_TRACK);
boolean gpxFormatShort = prefs.getBoolean(OSMTracker.Preferences.KEY_GPX_FORMAT_SHORT,
OSMTracker.Preferences.VAL_GPX_FORMAT_SHORT);
// Create the path to the directory to which we will be writing
// Trim the directory name, as additional spaces at the end will
// not allow the directory to be created if required
String exportDirectoryPath = userGPXExportDirectoryName.trim();
String perTrackDirectory = "";
if (directoryPerTrack) {
String trackName = "";
// Get the name of the track with the received start date
String selection = TrackContentProvider.Schema.COL_START_DATE + " = ?";
String[] args = {String.valueOf(startDate.getTime())};
Cursor c = context.getContentResolver().query(
TrackContentProvider.CONTENT_URI_TRACK, null, selection, args, null);
if(c != null && c.moveToFirst()){
int i = c.getColumnIndex(TrackContentProvider.Schema.COL_NAME);
trackName = c.getString(i);
}
if(trackName != null && trackName.length() >= 1) {
trackName = trackName.replace("/", "_");
perTrackDirectory = File.separator + trackName.trim();
}
}
// Create a file based on the path we've generated above
File trackGPXExportDirectory = new File(sdRoot + exportDirectoryPath + perTrackDirectory);
// Create track directory if needed
if (! trackGPXExportDirectory.exists()) {
if (! trackGPXExportDirectory.mkdirs()) {
Log.w(TAG,"Failed to create directory ["
+trackGPXExportDirectory.getAbsolutePath()+ "]");
}
if (! trackGPXExportDirectory.exists()) {
// Specific hack for Google Nexus S(See issue #168)
if (android.os.Build.MODEL.equals(OSMTracker.Devices.NEXUS_S)) {
// exportDirectoryPath always starts with "/"
trackGPXExportDirectory = new File(exportDirectoryPath + perTrackDirectory);
trackGPXExportDirectory.mkdirs();
}
}
if (! trackGPXExportDirectory.exists()) {
throw new ExportTrackException(context.getResources().getString(R.string.error_create_track_dir,
trackGPXExportDirectory.getAbsolutePath()));
}
}
return trackGPXExportDirectory;
}
@Override
protected boolean exportMediaFiles() {
return true;
}
@Override
protected boolean updateExportDate() {
return true;
}
}