forked from upstream-mirrors/OpenTracks
Fixed comments for 68f1788b686b.
Also fixed: - In ChooseMapActivity and SendFusionTablesActivity, check that the intent inputs are valid. - In ChooseMapAsyncTask, close the GDataParser in the finally block. - In SendFusionTablesAsyncTask, check that a segment has more than 2 points and that the segment stop time is set in prepareTrackSegment.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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<Void, Integer, Boolean> {
|
||||
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<Void, Integer, Boolean> {
|
||||
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;
|
||||
|
||||
+13
-2
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
+18
-24
@@ -178,13 +178,13 @@ public class SendFusionTablesAsyncTask extends AsyncTask<Void, Integer, Boolean>
|
||||
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<Void, Integer, Boolean>
|
||||
* @param lastBatch true if it is the last batch of locations
|
||||
*/
|
||||
private boolean prepareAndUploadPoints(Track track, List<Location> locations, boolean lastBatch) {
|
||||
int numLocations = locations.size();
|
||||
if (numLocations < 2) {
|
||||
Log.d(TAG, "Not preparing/uploading, too few points");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Prepare locations
|
||||
ArrayList<Track> 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<Void, Integer, Boolean>
|
||||
if (segmentStats.getStartTime() < 0) {
|
||||
segmentStats.setStartTime(loc.getTime());
|
||||
}
|
||||
segmentStats.setStopTime(loc.getTime());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -460,9 +452,9 @@ public class SendFusionTablesAsyncTask extends AsyncTask<Void, Integer, Boolean>
|
||||
* Prepares a track segment for sending to Google Fusion Tables. The main
|
||||
* steps are:
|
||||
* <ul>
|
||||
* <li>set the stop time</li>
|
||||
* <li>make sure the segment has at least 2 points</li>
|
||||
* <li>set the segment stop time if necessary</li>
|
||||
* <li>decimate locations precision</li>
|
||||
* <li>make sure the segment has more than 1 point</li>
|
||||
* </ul>
|
||||
* The prepared track will be added to the splitTracks.
|
||||
*
|
||||
@@ -470,11 +462,16 @@ public class SendFusionTablesAsyncTask extends AsyncTask<Void, Integer, Boolean>
|
||||
* @param splitTracks an array of track segments
|
||||
*/
|
||||
private void prepareTrackSegment(Track segment, ArrayList<Track> 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<Location> 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<Void, Integer, Boolean>
|
||||
// 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<Void, Integer, Boolean>
|
||||
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<Void, Integer, Boolean>
|
||||
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<Void, Integer, Boolean>
|
||||
try {
|
||||
content = response.getContent();
|
||||
} catch (IOException e) {
|
||||
Log.d(TAG, e.getMessage());
|
||||
Log.d(TAG, "Unable to get response", e);
|
||||
return false;
|
||||
}
|
||||
if (setTableId) {
|
||||
|
||||
+3
-3
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user