diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/maps/ChooseMapActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/maps/ChooseMapActivity.java index 81e69f897..d744396e3 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/maps/ChooseMapActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/maps/ChooseMapActivity.java @@ -98,22 +98,25 @@ public class ChooseMapActivity extends Activity { list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - Intent result = new Intent(); - result.putExtra(MAP_ID, arrayAdapter.getItem(position).getMapId()); - setResult(RESULT_OK, result); + setResult( + RESULT_OK, new Intent().putExtra(MAP_ID, arrayAdapter.getItem(position).getMapId())); finish(); } }); list.setAdapter(arrayAdapter); - Intent intent = getIntent(); - Account account = intent.getParcelableExtra(ACCOUNT); - Object retained = getLastNonConfigurationInstance(); if (retained instanceof ChooseMapAsyncTask) { asyncTask = (ChooseMapAsyncTask) retained; asyncTask.setActivity(this); } else { + Intent intent = getIntent(); + Account account = intent.getParcelableExtra(ACCOUNT); + if (account == null) { + setResult(RESULT_OK, new Intent().putExtra(MAP_ID, (String) null)); + finish(); + return; + } asyncTask = new ChooseMapAsyncTask(this, account); asyncTask.execute(); } @@ -152,18 +155,14 @@ public class ChooseMapActivity extends Activity { builder.setPositiveButton(R.string.generic_ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int arg1) { - Intent result = new Intent(); - result.putExtra(MAP_ID, (String) null); - setResult(RESULT_OK, result); + setResult(RESULT_OK, new Intent().putExtra(MAP_ID, (String) null)); finish(); } }); builder.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { - Intent result = new Intent(); - result.putExtra(MAP_ID, (String) null); - setResult(RESULT_OK, result); + setResult(RESULT_OK, new Intent().putExtra(MAP_ID, (String) null)); finish(); } }); diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/maps/ChooseMapAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/maps/ChooseMapAsyncTask.java index c311226bc..73d5d4539 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/maps/ChooseMapAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/maps/ChooseMapAsyncTask.java @@ -1,5 +1,18 @@ -// Copyright 2012 Google Inc. All Rights Reserved. - +/* + * Copyright 2012 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ package com.google.android.apps.mytracks.io.maps; import com.google.android.apps.mytracks.io.gdata.GDataClientFactory; @@ -129,22 +142,22 @@ public class ChooseMapAsyncTask extends AsyncTask { authToken = AccountManager.get(context).blockingGetAuthToken( account, MapsConstants.SERVICE_NAME, false); } catch (OperationCanceledException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get auth token", e); return retryUpload(); } catch (AuthenticatorException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get auth token", e); return retryUpload(); } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get auth token", e); return retryUpload(); } if (isCancelled()) { return false; } - + GDataParser gDataParser = null; try { - GDataParser gDataParser = mapsClient.getParserForFeed( + gDataParser = mapsClient.getParserForFeed( MapFeatureEntry.class, MapsClient.getMapsFeed(), authToken); gDataParser.init(); while (gDataParser.hasMoreData()) { @@ -152,16 +165,19 @@ public class ChooseMapAsyncTask extends AsyncTask { mapIds.add(MapsGDataConverter.getMapidForEntry(entry)); mapData.add(MapsGDataConverter.getMapMetadataForEntry(entry)); } - gDataParser.close(); } catch (ParseException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get maps", e); return retryUpload(); } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get maps", e); return retryUpload(); } catch (HttpException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get maps", e); return retryUpload(); + } finally { + if (gDataParser != null) { + gDataParser.close(); + } } return true; diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesActivity.java index 226dfe0b4..91a856c25 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesActivity.java @@ -66,7 +66,19 @@ public class SendFusionTablesActivity extends Activity { } else { Intent intent = getIntent(); Account account = intent.getParcelableExtra(ACCOUNT); + if (account == null) { + setResult( + RESULT_OK, new Intent().putExtra(SUCCESS, false).putExtra(TABLE_ID, (String) null)); + finish(); + return; + } long trackId = intent.getLongExtra(TRACK_ID, -1L); + if (trackId == -1L) { + setResult( + RESULT_OK, new Intent().putExtra(SUCCESS, false).putExtra(TABLE_ID, (String) null)); + finish(); + return; + } asyncTask = new SendFusionTablesAsyncTask(this, account, trackId); asyncTask.execute(); @@ -112,8 +124,7 @@ public class SendFusionTablesActivity extends Activity { * @param tableId tableId if available */ public void onAsyncTaskCompleted(boolean success, String tableId) { - Intent intent = new Intent().putExtra(SUCCESS, success).putExtra(TABLE_ID, tableId); - setResult(RESULT_OK, intent); + setResult(RESULT_OK, new Intent().putExtra(SUCCESS, success).putExtra(TABLE_ID, tableId)); finish(); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesAsyncTask.java index 0ccbba77f..126083f01 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesAsyncTask.java @@ -178,13 +178,13 @@ public class SendFusionTablesAsyncTask extends AsyncTask try { authToken = AccountManager.get(context).blockingGetAuthToken(account, SERVICE_ID, false); } catch (OperationCanceledException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get auth token", e); return retryUpload(); } catch (AuthenticatorException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get auth token", e); return retryUpload(); } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get auth token", e); return retryUpload(); } @@ -370,18 +370,11 @@ public class SendFusionTablesAsyncTask extends AsyncTask * @param lastBatch true if it is the last batch of locations */ private boolean prepareAndUploadPoints(Track track, List locations, boolean lastBatch) { - int numLocations = locations.size(); - if (numLocations < 2) { - Log.d(TAG, "Not preparing/uploading, too few points"); - return true; - } - // Prepare locations ArrayList splitTracks = prepareLocations(track, locations); - boolean onlyOneSegment = lastBatch && currentSegment == 1 && splitTracks.size() == 1; - // Upload segments + boolean onlyOneSegment = lastBatch && currentSegment == 1 && splitTracks.size() == 1; for (Track splitTrack : splitTracks) { if (!onlyOneSegment) { splitTrack.setName(context.getString( @@ -447,7 +440,6 @@ public class SendFusionTablesAsyncTask extends AsyncTask if (segmentStats.getStartTime() < 0) { segmentStats.setStartTime(loc.getTime()); } - segmentStats.setStopTime(loc.getTime()); } } @@ -460,9 +452,9 @@ public class SendFusionTablesAsyncTask extends AsyncTask * Prepares a track segment for sending to Google Fusion Tables. The main * steps are: *
    - *
  • set the stop time
  • + *
  • make sure the segment has at least 2 points
  • + *
  • set the segment stop time if necessary
  • *
  • decimate locations precision
  • - *
  • make sure the segment has more than 1 point
  • *
* The prepared track will be added to the splitTracks. * @@ -470,11 +462,16 @@ public class SendFusionTablesAsyncTask extends AsyncTask * @param splitTracks an array of track segments */ private void prepareTrackSegment(Track segment, ArrayList splitTracks) { + // Make sure the segment has at least 2 points + if (segment.getLocations().size() < 2) { + Log.d(TAG, "segment has less than 2 points"); + return; + } + // For a new segment, sets it stop time TripStatistics segmentStats = segment.getStatistics(); - ArrayList locations = segment.getLocations(); - if (segmentStats.getStopTime() < 0 && locations.size() > 0) { - Location lastLocation = locations.get(locations.size() - 1); + if (segmentStats.getStopTime() < 0) { + Location lastLocation = segment.getLocations().get(segment.getLocations().size() - 1); segmentStats.setStopTime(lastLocation.getTime()); } @@ -482,10 +479,7 @@ public class SendFusionTablesAsyncTask extends AsyncTask // points. LocationUtils.decimate(segment, 2.0); - // Only add the segment if it has more than 1 point. - if (segment.getLocations().size() > 1) { - splitTracks.add(segment); - } + splitTracks.add(segment); } /** @@ -572,7 +566,7 @@ public class SendFusionTablesAsyncTask extends AsyncTask try { request = httpRequestFactory.buildPostRequest(url, inputStreamContent); } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to build request", e); return false; } @@ -587,7 +581,7 @@ public class SendFusionTablesAsyncTask extends AsyncTask try { response = request.execute(); } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to execute request", e); return false; } boolean isSuccess = response.isSuccessStatusCode(); @@ -596,7 +590,7 @@ public class SendFusionTablesAsyncTask extends AsyncTask try { content = response.getContent(); } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to get response", e); return false; } if (setTableId) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesUtils.java b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesUtils.java index c65870a36..9f70b4576 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendFusionTablesUtils.java @@ -52,7 +52,7 @@ public class SendFusionTablesUtils { */ public static String getMapUrl(Track track) { if (track == null || track.getStatistics() == null || track.getTableId() == null) { - Log.e(TAG, "Invalid track."); + Log.e(TAG, "Invalid track"); return null; } @@ -168,7 +168,7 @@ public class SendFusionTablesUtils { try { read = inputStream.read(result); } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to read result", e); return null; } if (read == -1) { @@ -179,7 +179,7 @@ public class SendFusionTablesUtils { try { s = new String(result, 0, read, UTF8); } catch (UnsupportedEncodingException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to parse result", e); return null; }