Fixed fusion tables upload warning - previously, it assumed all queries would be table-creation queries.

This commit is contained in:
Rodrigo Damazio
2011-12-06 17:20:48 -02:00
parent ebe8cbc798
commit bcba6dfe5d
@@ -43,6 +43,7 @@ import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport; import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.InputStreamContent; import com.google.api.client.http.InputStreamContent;
import com.google.api.client.util.Strings; import com.google.api.client.util.Strings;
import com.google.common.io.LineReader;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
@@ -53,6 +54,7 @@ import android.util.Log;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -234,13 +236,20 @@ public class SendToFusionTables implements Runnable {
Log.d(Constants.TAG, "Creating a new fusion table."); Log.d(Constants.TAG, "Creating a new fusion table.");
String query = "CREATE TABLE '" + sqlEscape(track.getName()) + String query = "CREATE TABLE '" + sqlEscape(track.getName()) +
"' (name:STRING,description:STRING,geometry:LOCATION,marker:STRING)"; "' (name:STRING,description:STRING,geometry:LOCATION,marker:STRING)";
return runUpdate(query);
List<String> resultLines = new ArrayList<String>();
boolean success = runUpdate(query, resultLines);
if (success && resultLines.size() >= 2 && resultLines.get(0).equals("tableid")) {
tableId = resultLines.get(1);
Log.d(Constants.TAG, "tableId = " + tableId);
}
return success;
} }
private boolean makeTableUnlisted() { private boolean makeTableUnlisted() {
Log.d(Constants.TAG, "Setting visibility to unlisted."); Log.d(Constants.TAG, "Setting visibility to unlisted.");
String query = "UPDATE TABLE " + tableId + " SET VISIBILITY = UNLISTED"; String query = "UPDATE TABLE " + tableId + " SET VISIBILITY = UNLISTED";
return runUpdate(query); return runUpdate(query, null);
} }
/** /**
@@ -280,7 +289,7 @@ public class SendToFusionTables implements Runnable {
Log.d(Constants.TAG, "Creating a new row with a point."); Log.d(Constants.TAG, "Creating a new row with a point.");
String query = "INSERT INTO " + tableId + " (name,description,geometry,marker) VALUES " String query = "INSERT INTO " + tableId + " (name,description,geometry,marker) VALUES "
+ values(name, description, getKmlPoint(location), marker); + values(name, description, getKmlPoint(location), marker);
return runUpdate(query); return runUpdate(query, null);
} }
/** /**
@@ -294,7 +303,7 @@ public class SendToFusionTables implements Runnable {
String query = "INSERT INTO " + tableId String query = "INSERT INTO " + tableId
+ " (name,description,geometry) VALUES " + " (name,description,geometry) VALUES "
+ values(track.getName(), track.getDescription(), getKmlLineString(track)); + values(track.getName(), track.getDescription(), getKmlLineString(track));
return runUpdate(query); return runUpdate(query, null);
} }
private boolean uploadAllTrackPoints(final Track track, String originalDescription) { private boolean uploadAllTrackPoints(final Track track, String originalDescription) {
@@ -654,9 +663,10 @@ public class SendToFusionTables implements Runnable {
* Runs an update query. Handles authentication. * Runs an update query. Handles authentication.
* *
* @param query The given SQL like query * @param query The given SQL like query
* @param resultLines a list to fill with the query result's lines (or null to ignore results)
* @return true in case of success * @return true in case of success
*/ */
private boolean runUpdate(final String query) { private boolean runUpdate(final String query, final List<String> resultLines) {
GDataWrapper<HttpRequestFactory> wrapper = new GDataWrapper<HttpRequestFactory>(); GDataWrapper<HttpRequestFactory> wrapper = new GDataWrapper<HttpRequestFactory>();
wrapper.setAuthManager(auth); wrapper.setAuthManager(auth);
wrapper.setRetryOnAuthFailure(true); wrapper.setRetryOnAuthFailure(true);
@@ -674,14 +684,14 @@ public class SendToFusionTables implements Runnable {
InputStreamContent isc = new InputStreamContent(null, inputStream ); InputStreamContent isc = new InputStreamContent(null, inputStream );
HttpRequest request = factory.buildPostRequest(url, isc); HttpRequest request = factory.buildPostRequest(url, isc);
GoogleHeaders headers = new GoogleHeaders(); GoogleHeaders headers = new GoogleHeaders();
headers.setApplicationName("Google-MyTracks-" + SystemUtils.getMyTracksVersion(context)); headers.setApplicationName("Google-MyTracks-" + SystemUtils.getMyTracksVersion(context));
headers.gdataVersion = GDATA_VERSION; headers.gdataVersion = GDATA_VERSION;
headers.setGoogleLogin(auth.getAuthToken()); headers.setGoogleLogin(auth.getAuthToken());
headers.setContentType(CONTENT_TYPE); headers.setContentType(CONTENT_TYPE);
request.setHeaders(headers); request.setHeaders(headers);
Log.d(Constants.TAG, "Running update query " + url.toString() + ": " + sql); Log.d(Constants.TAG, "Running update query " + url.toString() + ": " + sql);
HttpResponse response; HttpResponse response;
try { try {
@@ -692,15 +702,14 @@ public class SendToFusionTables implements Runnable {
} }
boolean success = response.isSuccessStatusCode(); boolean success = response.isSuccessStatusCode();
if (success) { if (success) {
byte[] result = new byte[1024]; if (resultLines != null) {
int read = response.getContent().read(result); InputStreamReader resultReader = new InputStreamReader(response.getContent(), "UTF8");
String s = new String(result, 0, read, "UTF8"); LineReader lineReader = new LineReader(resultReader);
String[] lines = s.split(Strings.LINE_SEPARATOR); String line;
if (lines[0].equals("tableid")) { while ((line = lineReader.readLine()) != null) {
tableId = lines[1]; resultLines.add(line);
Log.d(Constants.TAG, "tableId = " + tableId); }
} else { resultReader.close();
Log.w(Constants.TAG, "Unrecognized response: " + lines[0]);
} }
} else { } else {
Log.d(Constants.TAG, Log.d(Constants.TAG,