-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathExportToStorageTask.java
More file actions
117 lines (99 loc) · 4.43 KB
/
ExportToStorageTask.java
File metadata and controls
117 lines (99 loc) · 4.43 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
112
113
114
115
116
117
package me.guillaumin.android.osmtracker.gpx;
import java.io.File;
import java.util.Date;
import me.guillaumin.android.osmtracker.OSMTracker;
import me.guillaumin.android.osmtracker.R;
import me.guillaumin.android.osmtracker.db.DataHelper;
import me.guillaumin.android.osmtracker.exception.ExportTrackException;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
/**
* 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);
}
/**
* Calculate a track's export directory, and create if it doesn't exist already.
* @param startDate The track's starting date, from
* {@link me.guillaumin.android.osmtracker.db.TrackContentProvider.Schema#COL_START_DATE Schema.COL_START_DATE}
* @return The export directory
* @throws ExportTrackException if the directory can't be created
* @see #getExportDirectory(Context, Date)
*/
@Override
protected File getExportDirectory(Date startDate) throws ExportTrackException {
File sdRoot = Environment.getExternalStorageDirectory();
final String exportDirectoryPath = getExportDirectory(context, startDate);
// Create a file based on the path we've generated above
File trackGPXExportDirectory = new File(sdRoot + exportDirectoryPath);
// 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);
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;
}
/**
*<p>Calculate a track's export directory path. Does not create the directory if missing.</p>
*
*<p>The returned directory is relative to <tt>sdRoot</tt> because of bug #168
* mentioned in {@link #getExportDirectory(Date)}. If you can't find the
* returned directory at <tt>sdRoot + directory</tt>, try just <tt>directory</tt>.</p>
*
* @param context Calling activity context, for {@link PreferenceManager#getDefaultSharedPreferences(Context)}
* @param startDate The track's starting date, from
* {@link me.guillaumin.android.osmtracker.db.TrackContentProvider.Schema#COL_START_DATE Schema.COL_START_DATE}
* @return track export directory name within sdcard;
* The full path to dirname is {@link Environment#getExternalStorageDirectory()} + dirname
* @see #getExportDirectory(Date)
*/
public static String getExportDirectory(Context context, Date startDate) {
// 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);
// 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) {
// If the user wants a directory per track, then create a name for the destination directory
// based on the start date of the track
perTrackDirectory = File.separator + DataHelper.FILENAME_FORMATTER.format(startDate);
}
return exportDirectoryPath + perTrackDirectory;
}
}