-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathDataHelper.java
More file actions
350 lines (306 loc) · 10.8 KB
/
Copy pathDataHelper.java
File metadata and controls
350 lines (306 loc) · 10.8 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
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
package me.guillaumin.android.osmtracker.db;
import java.io.File;
import java.text.SimpleDateFormat;
import me.guillaumin.android.osmtracker.OSMTracker;
import me.guillaumin.android.osmtracker.db.TrackContentProvider.Schema;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.location.Location;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
/**
* Data helper for dialoging with content resolver and filesystem.
*
* @author Nicolas Guillaumin
*
*/
public class DataHelper {
private static final String TAG = DataHelper.class.getSimpleName();
/**
* GPX file extension.
*/
public static final String EXTENSION_GPX = ".gpx";
/**
* 3GPP extension
*/
public static final String EXTENSION_3GPP = ".3gpp";
/**
* JPG file extension
*/
public static final String EXTENSION_JPG = ".jpg";
/**
* Number of tries to rename a media file for the current track if there are
* already a media file of this name.
*/
private static final int MAX_RENAME_ATTEMPTS = 20;
/**
* Formatter for various files (GPX, media)
*/
public static final SimpleDateFormat FILENAME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
/**
* Context
*/
private Context context;
/**
* ContentResolver to interact with content provider
*/
private ContentResolver contentResolver;
/**
* Constructor.
*
* @param c
* Application context.
*/
public DataHelper(Context c) {
context = c;
contentResolver = c.getContentResolver();
}
/**
* Track a point into DB.
*
* @param trackId
* Id of the track
* @param location
* The Location to track
*/
public void track(long trackId, Location location) {
Log.v(TAG, "Tracking (trackId=" + trackId + ") location: " + location);
ContentValues values = new ContentValues();
values.put(Schema.COL_TRACK_ID, trackId);
values.put(Schema.COL_LATITUDE, location.getLatitude());
values.put(Schema.COL_LONGITUDE, location.getLongitude());
if (location.hasAltitude()) {
values.put(Schema.COL_ELEVATION, location.getAltitude());
}
if (location.hasAccuracy()) {
values.put(Schema.COL_ACCURACY, location.getAccuracy());
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.getBoolean(OSMTracker.Preferences.KEY_GPS_IGNORE_CLOCK, OSMTracker.Preferences.VAL_GPS_IGNORE_CLOCK)) {
// Use OS clock
values.put(Schema.COL_TIMESTAMP, System.currentTimeMillis());
} else {
// Use GPS clock
values.put(Schema.COL_TIMESTAMP, location.getTime());
}
Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
contentResolver.insert(Uri.withAppendedPath(trackUri, Schema.TBL_TRACKPOINT + "s"), values);
}
/**
* Tracks a way point with link
*
* @param trackId
* Id of the track
* @param location
* Location of waypoint
* @param nbSatellites
* Number of satellites used for the location
* @param name
* Name of waypoint
* @param link
* Link of waypoint
* @param uuid
* Unique id of the waypoint
*/
public void wayPoint(long trackId, Location location, int nbSatellites, String name, String link, String uuid) {
Log.v(TAG, "Tracking waypoint '" + name + "', track=" + trackId + ", uuid=" + uuid + ", link='" + link + "', location=" + location);
// location should not be null, but sometime is.
// TODO investigate this issue.
if (location != null) {
ContentValues values = new ContentValues();
values.put(Schema.COL_TRACK_ID, trackId);
values.put(Schema.COL_LATITUDE, location.getLatitude());
values.put(Schema.COL_LONGITUDE, location.getLongitude());
values.put(Schema.COL_NAME, name);
values.put(Schema.COL_NBSATELLITES, nbSatellites);
if (uuid != null) {
values.put(Schema.COL_UUID, uuid);
}
if (location.hasAltitude()) {
values.put(Schema.COL_ELEVATION, location.getAltitude());
}
if (location.hasAccuracy()) {
values.put(Schema.COL_ACCURACY, location.getAccuracy());
}
if (link != null) {
// Rename file to match location timestamp
values.put(Schema.COL_LINK, renameFile(trackId, link, FILENAME_FORMATTER.format(location.getTime())));
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.getBoolean(OSMTracker.Preferences.KEY_GPS_IGNORE_CLOCK, OSMTracker.Preferences.VAL_GPS_IGNORE_CLOCK)) {
// Use OS clock
values.put(Schema.COL_TIMESTAMP, System.currentTimeMillis());
} else {
// Use GPS clock
values.put(Schema.COL_TIMESTAMP, location.getTime());
}
Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
contentResolver.insert(Uri.withAppendedPath(trackUri, Schema.TBL_WAYPOINT + "s"), values);
}
}
/**
* Updates a waypoint
*
* @param trackId
* Id of the track
* @param uuid
* Unique ID of the target waypoint
* @param name
* New name
* @param link
* New link
*/
public void updateWayPoint(long trackId, String uuid, String name, String link) {
Log.v(TAG, "Updating waypoint with uuid '" + uuid + "'. New values: name='" + name + "', link='" + link + "'");
if (uuid != null) {
ContentValues values = new ContentValues();
if (name != null) {
values.put(Schema.COL_NAME, name);
}
if (link != null) {
values.put(Schema.COL_LINK, link);
}
Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
contentResolver.update(Uri.withAppendedPath(trackUri, Schema.TBL_WAYPOINT + "s"), values,
"uuid = ?", new String[] { uuid });
}
}
/**
* Deletes a waypoint
*
* @param uuid
* Unique ID of the target waypoint
*/
public void deleteWayPoint(String uuid) {
Log.v(TAG, "Deleting waypoint with uuid '" + uuid);
if (uuid != null) {
contentResolver.delete(Uri.withAppendedPath(TrackContentProvider.CONTENT_URI_WAYPOINT_UUID, uuid), null, null);
}
}
/**
* Stop tracking by making the track inactive
* @param trackId Id of the track
*/
public void stopTracking(long trackId) {
Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
ContentValues values = new ContentValues();
values.put(Schema.COL_ACTIVE, Schema.VAL_TRACK_INACTIVE);
contentResolver.update(trackUri, values, null, null);
// The next time TrackManager displays this track in the list,
// Track.build will count the trackpoints and waypoints and
// save them to the track's tp_count and wp_count columns.
// We don't need to do that here.
}
/**
* Find the active track ID, if any.
* @param cr {@link ContentResolver} for query
* @return the active track ID, or -1
*/
public static long getActiveTrackId(ContentResolver cr) {
long currentTrackId = -1;
Cursor ca = cr.query(TrackContentProvider.CONTENT_URI_TRACK_ACTIVE, null, null, null, null);
if (ca.moveToFirst()) {
currentTrackId = ca.getLong(ca.getColumnIndex(Schema.COL_ID));
}
ca.close();
return currentTrackId;
}
/**
* Change the name of this track.
* @param trackId Id of the track
* @param name New name of track, or null to clear it
* @param cr Database connection for query
*/
public static void setTrackName(long trackId, String name, ContentResolver cr) {
Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
ContentValues values = new ContentValues();
values.put(Schema.COL_NAME, name);
cr.update(trackUri, values, null, null);
}
/**
* Mark the export date/time of this track.
* @param trackId Id of the track
* @param exportTime Time of export, from {@link System#currentTimeMillis()}
* @param cr {@link ContentResolver} for query
*/
public static void setTrackExportDate(long trackId, long exportTime, ContentResolver cr) {
Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
ContentValues values = new ContentValues();
values.put(Schema.COL_EXPORT_DATE, exportTime);
cr.update(trackUri, values, null, null);
}
public static void setTrackUploadDate(long trackId, long uploadTime, ContentResolver cr) {
Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
ContentValues values = new ContentValues();
values.put(Schema.COL_OSM_UPLOAD_DATE, uploadTime);
cr.update(trackUri, values, null, null);
}
/**
* Renames a file inside track directory, keeping the extension
*
* @param from
* File to rename (Ex: "abc.png")
* @param to
* Filename to use for new name (Ex: "def")
* @return Renamed filename (Ex: "def.png")
*/
private String renameFile(Long trackId, String from, String to) {
// If all goes terribly wrong and we can't rename the file,
// we will return the original file name we were given
String _return = from;
File trackDir = getTrackDirectory(trackId);
String ext = from.substring(from.lastIndexOf(".") + 1, from.length());
File origin = new File(trackDir + File.separator + from);
// No point in trying to rename the file unless it exist
if (origin.exists()) {
File target = new File(trackDir + File.separator + to + "." + ext);
// Check & manages if there is already a file with this name
for (int i = 0; i < MAX_RENAME_ATTEMPTS && target.exists(); i++) {
target = new File(trackDir + File.separator + to + i + "." + ext);
}
origin.renameTo(target);
_return = target.getName();
}
return _return;
}
/**
* @param cr Content Resolver to use
* @param trackId Track id
* @return A File to the track directory for the target track id.
*/
public static File getTrackDirFromDB(ContentResolver cr, long trackId) {
File trackDir = null;
Cursor c = cr.query(
ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId),
null, null, null, null);
if (c != null && c.getCount() != 0) {
c.moveToFirst();
@SuppressWarnings("deprecation")
String trackPath = c.getString(c.getColumnIndex(Schema.COL_DIR));
if (trackPath != null) {
trackDir = new File(trackPath);
}
c.close();
c = null;
}
return trackDir;
}
/**
* Generate a string of the directory path to external storage for the track id provided
* @param trackId Track id
* @return A the path where this track should store its files
*/
public static File getTrackDirectory(long trackId) {
File _return = null;
String trackStorageDirectory = Environment.getExternalStorageDirectory()
+ "/osmtracker/data/files/track" + trackId;
_return = new File(trackStorageDirectory);
return _return;
}
}