-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathTrackDetail.java
More file actions
373 lines (319 loc) · 12.1 KB
/
TrackDetail.java
File metadata and controls
373 lines (319 loc) · 12.1 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package me.guillaumin.android.osmtracker.activity;
import java.sql.Date;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import me.guillaumin.android.osmtracker.OSMTracker;
import me.guillaumin.android.osmtracker.R;
import me.guillaumin.android.osmtracker.db.TrackContentProvider;
import me.guillaumin.android.osmtracker.db.TrackContentProvider.Schema;
import me.guillaumin.android.osmtracker.db.TracklistAdapter;
import me.guillaumin.android.osmtracker.db.model.Track;
import me.guillaumin.android.osmtracker.db.model.TrackStatistics;
import me.guillaumin.android.osmtracker.gpx.ExportToStorageTask;
import me.guillaumin.android.osmtracker.util.MercatorProjection;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.database.ContentObserver;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
/**
* Display details about one track. Allow naming the track.
* The track ID is passed into the Bundle via {@link Schema#COL_TRACK_ID}.
*
* @author Jeremy D Monin <jdmonin@nand.net>
*
*/
public class TrackDetail extends TrackDetailEditor implements AdapterView.OnItemClickListener {
@SuppressWarnings("unused")
private static final String TAG = TrackDetail.class.getSimpleName();
/**
* Key to bind the "key" of each item using SimpleListAdapter
*/
private static final String ITEM_KEY = "key";
/**
* Key to bind the "value" of each item using SimpleListAdapter
*/
private static final String ITEM_VALUE = "value";
/**
* Position of the waypoints counts in the list
*/
private static final int WP_COUNT_INDEX = 0;
/** Does this track have any waypoints? If true, underline Waypoint count in the list. */
private boolean trackHasWaypoints = false;
/**
* List with track info
*/
private ListView lv;
/**
* Adapter for the above list
*/
TrackDetailSimpleAdapter adapter = null;
/**
* Observes changes on trackpoints
*/
private ContentObserver trackpointContentObserver;
/**
* Statistics for the track
*/
private TrackStatistics trackStatistics;
/**
* Data for the track info
*/
private List<HashMap<String, String>> trackData = new ArrayList<HashMap<String, String>> ();
@Override
protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
super.onCreate(savedInstanceState, R.layout.trackdetail, extras.getLong(Schema.COL_TRACK_ID));
trackStatistics = new TrackStatistics(trackId, getContentResolver(), extras);
lv = (ListView) findViewById(R.id.trackdetail_list);
final Button btnOk = (Button) findViewById(R.id.trackdetail_btn_ok);
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
save();
finish();
}
});
final Button btnCancel = (Button) findViewById(R.id.trackdetail_btn_cancel);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Just close the dialog
finish();
}
});
// Do not show soft keyboard by default
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// Create content observer for trackpoints
trackpointContentObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
pathChanged();
}
};
getContentResolver().registerContentObserver(
TrackContentProvider.trackPointsUri(trackId),
true, trackpointContentObserver);
// further work is done in onResume.
}
/**
* Update trackData (doesn't update the list view though)
*/
private void updateTrack() {
// Query the track values
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(
ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId),
null, null, null, null);
if (! cursor.moveToFirst()) {
// This shouldn't occur, it's here just in case.
// So, don't make each language translate/localize it.
Toast.makeText(this, "Track ID not found.", Toast.LENGTH_SHORT).show();
cursor.close();
finish();
return; // <--- Early return ---
}
trackData.clear();
// Bind WP count, TP count, start date, etc.
// Fill name-field only if empty (in case changed by user/restored by onRestoreInstanceState)
Track t = Track.build(trackId, cursor, cr, true);
trackStatistics.update();
bindTrack(t);
// Waypoint count
final int wpCount = t.getWpCount();
trackHasWaypoints = (wpCount > 0);
HashMap<String, String> map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackmgr_waypoints_count));
map.put(ITEM_VALUE, Integer.toString(wpCount));
trackData.add(WP_COUNT_INDEX, map);
// Trackpoint count
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackmgr_trackpoints_count));
map.put(ITEM_VALUE, Integer.toString(t.getTpCount()));
trackData.add(map);
// Distance
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackmgr_distance));
map.put(ITEM_VALUE, TracklistAdapter.distanceToString(trackStatistics.totalLength(), getResources()));
trackData.add(map);
// Speed
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_speed));
map.put(ITEM_VALUE, TracklistAdapter.speedToString(trackStatistics.averageSpeed(), getResources()) + " " +
getResources().getString(R.string.trackdetail_speed_average) + ", " +
TracklistAdapter.speedToString(trackStatistics.maximumSpeed(), getResources()) + " " +
getResources().getString(R.string.trackdetail_speed_max));
trackData.add(map);
// Start date
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_startdate));
map.put(ITEM_VALUE, t.getStartDateAsString());
trackData.add(map);
// End date
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_enddate));
map.put(ITEM_VALUE, t.getEndDateAsString());
trackData.add(map);
// Start point
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_startloc));
map.put(ITEM_VALUE, MercatorProjection.formatDegreesAsDMS(t.getStartLat(), true) + " " + MercatorProjection.formatDegreesAsDMS(t.getStartLong(), false));
trackData.add(map);
// End point
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_endloc));
map.put(ITEM_VALUE, MercatorProjection.formatDegreesAsDMS(t.getEndLat(), true) + " " + MercatorProjection.formatDegreesAsDMS(t.getEndLong(), false));
trackData.add(map);
// OSM Upload date
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_osm_upload_date));
if (cursor.isNull(cursor.getColumnIndex(Schema.COL_OSM_UPLOAD_DATE))) {
map.put(ITEM_VALUE, getResources().getString(R.string.trackdetail_osm_upload_notyet));
} else {
map.put(ITEM_VALUE, DateFormat.getDateTimeInstance().format(new Date(cursor.getLong(cursor.getColumnIndex(Schema.COL_EXPORT_DATE)))));
}
trackData.add(map);
// Exported date. Should be the last item in order to be refreshed
// if the user exports the track
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_exportdate));
if (cursor.isNull(cursor.getColumnIndex(Schema.COL_EXPORT_DATE))) {
map.put(ITEM_VALUE, getResources().getString(R.string.trackdetail_export_notyet));
} else {
map.put(ITEM_VALUE, (DateFormat.getDateTimeInstance().format(new Date(cursor.getLong(cursor.getColumnIndex(Schema.COL_EXPORT_DATE))))));
}
trackData.add(map);
cursor.close();
}
@Override
protected void onResume() {
super.onResume();
updateTrack();
String from[] = new String[]{ITEM_KEY, ITEM_VALUE};
int[] to = new int[] {R.id.trackdetail_item_key, R.id.trackdetail_item_value};
adapter = new TrackDetailSimpleAdapter(trackData, from, to);
lv.setAdapter(adapter);
// Click on Waypoint count to see the track's WaypointList
lv.setOnItemClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.trackdetail_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent i;
switch(item.getItemId()) {
case R.id.trackdetail_menu_save:
save();
finish();
break;
case R.id.trackdetail_menu_cancel:
finish();
break;
case R.id.trackdetail_menu_display:
boolean useOpenStreetMapBackground = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
OSMTracker.Preferences.KEY_UI_DISPLAYTRACK_OSM, OSMTracker.Preferences.VAL_UI_DISPLAYTRACK_OSM);
if (useOpenStreetMapBackground) {
i = new Intent(this, DisplayTrackMap.class);
} else {
i = new Intent(this, DisplayTrack.class);
}
i.putExtra(Schema.COL_TRACK_ID, trackId);
startActivity(i);
break;
case R.id.trackdetail_menu_export:
new ExportToStorageTask(this, trackId).execute();
// Pick last list item (Exported date) and update it
SimpleAdapter adapter = ((SimpleAdapter) lv.getAdapter());
@SuppressWarnings("unchecked")
Map<String, String> data = (Map<String, String>) adapter.getItem(adapter.getCount()-1);
data.put(ITEM_VALUE, DateFormat.getDateTimeInstance().format(new Date(System.currentTimeMillis())));
adapter.notifyDataSetChanged();
break;
case R.id.trackdetail_menu_osm_upload:
i = new Intent(this, OpenStreetMapUpload.class);
i.putExtra(Schema.COL_TRACK_ID, trackId);
startActivity(i);
break;
}
return super.onOptionsItemSelected(item);
}
/**
* Handle clicks on list items; for Waypoint count, show this track's list of waypoints ({@link WaypointList}).
* Ignore all other clicks.
* @param position Item number in the list; this method assumes Waypoint count is position 0 (first item).
*/
public void onItemClick(AdapterView<?> parent, View view, final int position, final long rowid) {
if (position != WP_COUNT_INDEX) {
return;
}
Intent i = new Intent(this, WaypointList.class);
i.putExtra(Schema.COL_TRACK_ID, trackId);
startActivity(i);
}
/**
* On track path changed, update track info
*/
private void pathChanged() {
updateTrack();
// Update the list view
if (adapter != null)
adapter.notifyDataSetChanged();
}
/**
* Extend SimpleAdapter so we can underline the clickable Waypoint count.
* Always uses <tt>R.layout.trackdetail_item</tt> as its list item resource.
*/
private class TrackDetailSimpleAdapter extends SimpleAdapter
{
public TrackDetailSimpleAdapter
(List<? extends Map<String, ?>> data, String[] from, int[] to)
{
super(TrackDetail.this, data, R.layout.trackdetail_item, from, to);
}
/**
* Get the layout for this list item. (<tt>trackdetail_item.xml</tt>)
* @param position Item number in the list
*/
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);
if (! (v instanceof ViewGroup))
return v; // should not happen; v is trackdetail_item, a LinearLayout
final boolean wantsUnderline = ((position == WP_COUNT_INDEX) && trackHasWaypoints);
View vi = ((ViewGroup) v).findViewById(R.id.trackdetail_item_key);
if ((vi != null) && (vi instanceof TextView))
{
final int flags = ((TextView) vi).getPaintFlags();
if (wantsUnderline)
((TextView) vi).setPaintFlags(flags | Paint.UNDERLINE_TEXT_FLAG);
else
((TextView) vi).setPaintFlags(flags & ~Paint.UNDERLINE_TEXT_FLAG);
}
return v;
}
} // inner class TrackDetailSimpleAdapter
} // public class TrackDetail