Merge from head

This commit is contained in:
Rodrigo Damazio
2010-12-24 05:27:56 -02:00
25 changed files with 1544 additions and 307 deletions
@@ -25,7 +25,7 @@ import com.google.android.apps.mymaps.MyMapsConstants;
import com.google.android.apps.mymaps.MyMapsList;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.io.AuthManager;
import com.google.android.apps.mytracks.io.AuthManagerFactory;
import com.google.android.apps.mytracks.io.GpxImporter;
@@ -63,6 +63,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@@ -93,9 +94,6 @@ import org.xml.sax.SAXException;
public class MyTracks extends TabActivity implements OnTouchListener,
OnSharedPreferenceChangeListener, ProgressIndicator {
private static final String WAYPOINT_ICON_URL =
"http://maps.google.com/mapfiles/ms/micons/blue-pushpin.png";
/**
* Singleton instance
*/
@@ -432,7 +430,13 @@ public class MyTracks extends TabActivity implements OnTouchListener,
public boolean onTrackballEvent(MotionEvent event) {
if (isRecording()) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
insertStatisticsMarker();
try {
insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS);
} catch (RemoteException e) {
Log.e(MyTracksConstants.TAG, "Cannot insert statistics marker.", e);
} catch (IllegalStateException e) {
Log.e(MyTracksConstants.TAG, "Cannot insert statistics marker.", e);
}
return true;
}
}
@@ -888,90 +892,25 @@ public class MyTracks extends TabActivity implements OnTouchListener,
/**
* Inserts a waypoint marker.
*
* @return the id of the inserted statistics marker, or
* -1 unable to find location
* -2 track recording service is not running?
* -3 remote exception when contacting track recording service
* -4 inserting marker into provider failed
* @return Id of the inserted statistics marker.
* @throws RemoteException If the call on the service failed.
*/
public long insertWaypointMarker() {
Location location = getCurrentLocation();
if (location == null) {
public long insertWaypoint(WaypointCreationRequest request) throws RemoteException {
if (trackRecordingService == null) {
throw new IllegalStateException("The recording service is not bound.");
}
try {
long waypointId = trackRecordingService.insertWaypoint(request);
if (waypointId >= 0) {
Toast.makeText(this, R.string.status_statistics_inserted,
Toast.LENGTH_LONG).show();
}
return waypointId;
} catch (RemoteException e) {
Toast.makeText(this, R.string.error_unable_to_insert_marker,
Toast.LENGTH_LONG).show();
return -1;
throw e;
}
if (trackRecordingService != null) {
try {
Waypoint wpt = new Waypoint();
wpt.setName(getString(R.string.waypoint));
wpt.setType(Waypoint.TYPE_WAYPOINT);
wpt.setTrackId(recordingTrackId);
wpt.setIcon(WAYPOINT_ICON_URL);
wpt.setLocation(location);
long waypointId = trackRecordingService.insertWaypointMarker(wpt);
if (waypointId >= 0) {
Toast.makeText(this, R.string.status_waypoint_inserted,
Toast.LENGTH_LONG).show();
return waypointId;
} else {
Toast.makeText(this, R.string.error_unable_to_insert_marker,
Toast.LENGTH_LONG).show();
Log.e(MyTracksConstants.TAG, "Cannot insert waypoint marker?");
return -4;
}
// TODO: We catch Exception, because after eliminating the service process
// all exceptions it may throw are no longer wrapped in a RemoteException.
} catch (Exception e) {
Toast.makeText(this, R.string.error_unable_to_insert_marker,
Toast.LENGTH_LONG).show();
Log.e(MyTracksConstants.TAG, "Cannot insert waypoint marker.", e);
}
return -3;
}
return -2;
}
/**
* Inserts a statistics marker.
*
* @return the id of the inserted statistics marker, or
* -1 unable to find location
* -2 track recording service is not running?
* -3 remote exception when contacting track recording service
* -4 inserting marker into provider failed
*/
public long insertStatisticsMarker() {
Location location = getLastLocation();
if (location == null) {
Toast.makeText(this, R.string.error_unable_to_insert_marker,
Toast.LENGTH_LONG).show();
return -1;
}
if (trackRecordingService != null) {
try {
long waypointId =
trackRecordingService.insertStatisticsMarker(location);
if (waypointId >= 0) {
Toast.makeText(this, R.string.status_statistics_inserted,
Toast.LENGTH_LONG).show();
return waypointId;
} else {
Toast.makeText(this, R.string.error_unable_to_insert_marker,
Toast.LENGTH_LONG).show();
Log.e(MyTracksConstants.TAG, "Cannot insert statistics marker?");
return -4;
}
// TODO: We catch Exception, because after eliminating the service process
// all exceptions it may throw are no longer wrapped in a RemoteException.
} catch (Exception e) {
Toast.makeText(this, R.string.error_unable_to_insert_marker,
Toast.LENGTH_LONG).show();
Log.e(MyTracksConstants.TAG, "Cannot insert statistics marker?", e);
}
return -3;
}
return -2;
}
/**
@@ -42,6 +42,8 @@ import android.widget.ImageView;
public class MyTracksWaypointDetails extends Activity
implements OnClickListener {
public static final String WAYPOINT_ID_EXTRA = "com.google.android.apps.mytracks.WAYPOINT_ID";
/**
* The id of the way point being edited (taken from bundle, "waypointid")
*/
@@ -78,7 +80,7 @@ public class MyTracksWaypointDetails extends Activity
}
// Required extra when launching this intent:
waypointId = getIntent().getLongExtra("waypointid", -1);
waypointId = getIntent().getLongExtra(WAYPOINT_ID_EXTRA, -1);
if (waypointId < 0) {
Log.d(MyTracksConstants.TAG,
"MyTracksWaypointsDetails intent was launched w/o waypoint id.");
@@ -17,6 +17,7 @@ package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.content.WaypointsColumns;
import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R;
@@ -28,6 +29,8 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
@@ -87,7 +90,7 @@ public class MyTracksWaypointsList extends ListActivity
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent result = new Intent();
result.putExtra("trackid", trackId);
result.putExtra("waypointid", id);
result.putExtra(MyTracksWaypointDetails.WAYPOINT_ID_EXTRA, id);
setResult(MyTracksConstants.EDIT_WAYPOINT, result);
finish();
}
@@ -103,7 +106,7 @@ public class MyTracksWaypointsList extends ListActivity
case MyTracksConstants.MENU_EDIT: {
Intent intent = new Intent(this, MyTracksWaypointDetails.class);
intent.putExtra("trackid", trackId);
intent.putExtra("waypointid", waypointId);
intent.putExtra(MyTracksWaypointDetails.WAYPOINT_ID_EXTRA, waypointId);
startActivity(intent);
return true;
}
@@ -165,27 +168,34 @@ public class MyTracksWaypointsList extends ListActivity
@Override
public void onClick(View v) {
WaypointCreationRequest request;
switch (v.getId()) {
case R.id.waypointslist_btn_insert_waypoint: {
long id = MyTracks.getInstance().insertWaypointMarker();
if (id >= 0) {
Intent intent = new Intent(this, MyTracksWaypointDetails.class);
intent.putExtra("waypointid", id);
startActivity(intent);
}
case R.id.waypointslist_btn_insert_waypoint:
request = WaypointCreationRequest.DEFAULT_MARKER;
break;
}
case R.id.waypointslist_btn_insert_statistics: {
long id = MyTracks.getInstance().insertStatisticsMarker();
if (id >= 0) {
Intent intent = new Intent(this, MyTracksWaypointDetails.class);
intent.putExtra("waypointid", id);
startActivity(intent);
}
case R.id.waypointslist_btn_insert_statistics:
request = WaypointCreationRequest.DEFAULT_STATISTICS;
break;
}
default:
return;
}
long id;
try {
id = MyTracks.getInstance().insertWaypoint(request);
} catch (RemoteException e) {
Log.e(MyTracksConstants.TAG, "Cannot insert marker.", e);
return;
} catch (IllegalStateException e) {
Log.e(MyTracksConstants.TAG, "Cannot insert marker.", e);
return;
}
if (id < 0) {
Log.e(MyTracksConstants.TAG, "Failed to insert marker.");
return;
}
Intent intent = new Intent(this, MyTracksWaypointDetails.class);
intent.putExtra(MyTracksWaypointDetails.WAYPOINT_ID_EXTRA, id);
startActivity(intent);
}
private void setListAdapter() {
@@ -16,6 +16,7 @@
package com.google.android.apps.mytracks.services;
import com.google.android.apps.mytracks.MyTracksConstants;
import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.util.UnitConversions;
import android.util.Log;
@@ -111,7 +112,7 @@ public class SplitManager {
}
if (distance > this.nextSplitDistance) {
service.insertStatisticsMarker(service.getLastLocation());
service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS);
calculateNextSplit();
}
}
@@ -16,6 +16,9 @@
package com.google.android.apps.mytracks.services;
import com.google.android.apps.mytracks.content.WaypointCreationRequest;
/**
* A simple task to insert statistics markers every n minutes.
* @author Sandor Dornbush
@@ -24,7 +27,7 @@ public class TimeSplitTask implements PeriodicTask {
@Override
public void run(TrackRecordingService service) {
service.insertStatisticsMarker(service.getLastLocation());
service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS);
}
@Override
@@ -26,6 +26,7 @@ import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.content.WaypointsColumns;
import com.google.android.apps.mytracks.content.Sensor.SensorDataSet;
import com.google.android.apps.mytracks.services.sensors.SensorManager;
@@ -70,9 +71,6 @@ import java.util.TimerTask;
*/
public class TrackRecordingService extends Service implements LocationListener {
private static final String STATISTICS_ICON_URL =
"http://maps.google.com/mapfiles/ms/micons/ylw-pushpin.png";
static final int MAX_AUTO_RESUME_TRACK_RETRY_ATTEMPTS = 3;
private NotificationManager notificationManager;
@@ -429,7 +427,7 @@ public class TrackRecordingService extends Service implements LocationListener {
lastValidLocation = null;
Waypoint waypoint = providerUtils.getFirstWaypoint(recordingTrackId);
if (waypoint != null) {
if (waypoint != null && waypoint.getStatistics() != null) {
currentWaypointId = waypoint.getId();
waypointStatsBuilder = new TripStatisticsBuilder(
waypoint.getStatistics());
@@ -860,50 +858,62 @@ public class TrackRecordingService extends Service implements LocationListener {
return isRecording;
}
public long insertWaypointMarker(Waypoint waypoint) {
public long insertWaypoint(WaypointCreationRequest request) {
if (!isRecording()) {
throw new IllegalStateException(
"Unable to insert waypoint marker while not recording!");
}
if (waypoint.getLocation() == null) {
if (lastValidLocation == null) {
Log.w(MyTracksConstants.TAG, "Cannot insert waypoint with no location");
return -1;
}
waypoint.setLocation(lastValidLocation);
if (request == null) {
request = WaypointCreationRequest.DEFAULT_MARKER;
}
if (waypoint.getTrackId() < 0) {
waypoint.setTrackId(recordingTrackId);
Waypoint wpt = new Waypoint();
switch (request.getType()) {
case MARKER:
buildMarker(wpt, request);
break;
case STATISTICS:
buildStatisticsMarker(wpt);
break;
}
waypoint.setLength(length);
waypoint.setDuration(waypoint.getLocation().getTime()
- statsBuilder.getStatistics().getStartTime());
Uri uri = providerUtils.insertWaypoint(waypoint);
wpt.setTrackId(recordingTrackId);
wpt.setLength(length);
if (lastValidLocation != null) {
// A null location is ok, and expected on track start.
wpt.setLocation(lastLocation);
wpt.setDuration(lastLocation.getTime()
- statsBuilder.getStatistics().getStartTime());
}
Uri uri = providerUtils.insertWaypoint(wpt);
return Long.parseLong(uri.getLastPathSegment());
}
private void buildMarker(Waypoint wpt, WaypointCreationRequest request) {
wpt.setType(Waypoint.TYPE_WAYPOINT);
if (request.getIconUrl() == null) {
wpt.setIcon(getString(R.string.waypoint_icon_url));
} else {
wpt.setIcon(request.getIconUrl());
}
if (request.getName() == null) {
wpt.setName(getString(R.string.waypoint));
} else {
wpt.setName(request.getName());
}
if (request.getDescription() != null) {
wpt.setDescription(request.getDescription());
}
}
/**
* Inserts a statistics marker. A statistics marker holds the stats for the
* last segment up to this marker.
* Build a statistics marker.
* A statistics marker holds the stats for the* last segment up to this marker.
*
* @param location the location where to insert
* @param Waypoint The waypoint which will be populated with stats data.
* @return the unique id of the inserted marker
*/
public long insertStatisticsMarker(Location location) {
if (!isRecording()) {
throw new IllegalStateException(
"Unable to insert statistics marker while not recording!");
}
private void buildStatisticsMarker(Waypoint waypoint) {
StringUtils utils = new StringUtils(TrackRecordingService.this);
// Create a new waypoint to save
Waypoint waypoint = new Waypoint();
// Set stop and total time in the stats data
final long time = System.currentTimeMillis();
waypointStatsBuilder.pauseAt(time);
@@ -913,24 +923,18 @@ public class TrackRecordingService extends Service implements LocationListener {
waypoint.setDuration(time - statsBuilder.getStatistics().getStartTime());
// Set the rest of the waypoint data
waypoint.setTrackId(recordingTrackId);
waypoint.setType(Waypoint.TYPE_STATISTICS);
waypoint.setName(TrackRecordingService.this.getString(R.string.statistics));
waypoint.setName(getString(R.string.statistics));
waypoint.setStatistics(waypointStatsBuilder.getStatistics());
waypoint.setDescription(utils.generateWaypointDescription(waypoint));
waypoint.setLocation(location);
waypoint.setIcon(STATISTICS_ICON_URL);
waypoint.setLength(length);
waypoint.setIcon(getString(R.string.stats_icon_url));
waypoint.setStartId(providerUtils.getLastLocationId(recordingTrackId));
Uri uri = providerUtils.insertWaypoint(waypoint);
// Create a new stats keeper for the next marker
// Create a new stats keeper for the next marker.
waypointStatsBuilder = new TripStatisticsBuilder(time);
updateCurrentWaypoint();
return Long.parseLong(uri.getLastPathSegment());
}
private ServiceBinder binder = new ServiceBinder(this);
/**
@@ -961,79 +965,54 @@ public class TrackRecordingService extends Service implements LocationListener {
@Override
public boolean isRecording() {
checkService();
return service.isRecording();
}
private void checkService() {
if (service == null) {
throw new IllegalStateException("The service has been already detached!");
}
return service.isRecording();
}
@Override
public long getRecordingTrackId() {
if (service == null) {
throw new IllegalStateException("The service has been already detached!");
}
checkService();
return service.recordingTrackId;
}
@Override
public boolean hasRecorded() {
if (service == null) {
throw new IllegalStateException("The service has been already detached!");
}
checkService();
return service.providerUtils.getLastTrackId() >= 0;
}
@Override
public long startNewTrack() {
if (service == null) {
throw new IllegalStateException("The service has been already detached!");
}
checkService();
return service.startNewTrack();
}
/**
* Insert the given waypoint marker. Users can insert waypoint markers
* to tag locations with a name, description, category etc.
* Inserts a waypoint marker in the track being recorded.
*
* @param waypoint a waypoint
* @return the unique id of the inserted marker
* @param request Details of the waypoint to insert
* @return the unique ID of the inserted marker
*/
@Override
public long insertWaypointMarker(Waypoint waypoint) {
if (service == null) {
throw new IllegalStateException("The service has been already detached!");
}
return service.insertWaypointMarker(waypoint);
}
/**
* Insert a statistics marker. A statistics marker holds the stats for
* the last segment up to this marker.
*
* @param location the location where to insert
* @return the unique id of the inserted marker
*/
@Override
public long insertStatisticsMarker(Location location) {
if (service == null) {
throw new IllegalStateException("The service has been already detached!");
}
return service.insertStatisticsMarker(location);
public long insertWaypoint(WaypointCreationRequest request) {
checkService();
return service.insertWaypoint(request);
}
@Override
public void endCurrentTrack() {
if (service == null) {
throw new IllegalStateException("The service has been already detached!");
}
checkService();
service.endCurrentTrack();
}
@Override
public void deleteAllTracks() {
if (service == null) {
throw new IllegalStateException("The service has been already detached!");
}
checkService();
if (isRecording()) {
throw new IllegalStateException("Cannot delete all tracks while recording!");
}
@@ -1042,14 +1021,13 @@ public class TrackRecordingService extends Service implements LocationListener {
@Override
public void recordLocation(Location loc) {
if (service == null) {
throw new IllegalStateException("The service has been already detached!");
}
checkService();
service.onLocationChanged(loc);
}
@Override
public byte[] getSensorData() {
checkService();
if (service.sensorManager == null) {
Log.d(MyTracksConstants.TAG, "No sensor manager for data.");
return null;
@@ -1063,6 +1041,7 @@ public class TrackRecordingService extends Service implements LocationListener {
@Override
public int getSensorState() {
checkService();
if (service.sensorManager == null) {
Log.d(MyTracksConstants.TAG, "No sensor manager for data.");
return Sensor.SensorState.NONE.getNumber();
@@ -1095,7 +1074,7 @@ public class TrackRecordingService extends Service implements LocationListener {
providerUtils.updateTrack(track);
statsBuilder = new TripStatisticsBuilder(startTime);
waypointStatsBuilder = new TripStatisticsBuilder(startTime);
currentWaypointId = insertStatisticsMarker(null);
currentWaypointId = insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS);
setUpAnnouncer();
length = 0;
showNotification();