Fix comments on aa970ad1da92.

Additional fixes:
- In SendDocsAsyncTask, check that the activity is not null before
using it.
- In SendDocsUtils, close the GDataParser in the finally block.
- When catching an exception, always log it.
This commit is contained in:
Jimmy Shih
2012-01-25 10:41:38 -08:00
parent f682f347d5
commit 8472dbb195
3 changed files with 79 additions and 46 deletions
@@ -55,7 +55,18 @@ public class SendDocsActivity extends Activity {
} else { } else {
Intent intent = getIntent(); Intent intent = getIntent();
Account account = intent.getParcelableExtra(ACCOUNT); Account account = intent.getParcelableExtra(ACCOUNT);
if (account == null) {
setResult(RESULT_OK, new Intent().putExtra(SUCCESS, false));
finish();
return;
}
long trackId = intent.getLongExtra(TRACK_ID, -1L); long trackId = intent.getLongExtra(TRACK_ID, -1L);
if (trackId == -1L) {
setResult(RESULT_OK, new Intent().putExtra(SUCCESS, false));
finish();
return;
}
asyncTask = new SendDocsAsyncTask(this, account, trackId); asyncTask = new SendDocsAsyncTask(this, account, trackId);
asyncTask.execute(); asyncTask.execute();
@@ -107,7 +107,7 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
*/ */
public void setActivity(SendDocsActivity activity) { public void setActivity(SendDocsActivity activity) {
this.activity = activity; this.activity = activity;
if (completed) { if (completed && activity != null) {
activity.onAsyncTaskCompleted(success); activity.onAsyncTaskCompleted(success);
} }
} }
@@ -168,10 +168,13 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
spreadsheetsAuthToken = AccountManager.get(context).blockingGetAuthToken( spreadsheetsAuthToken = AccountManager.get(context).blockingGetAuthToken(
account, spreadsheetsClient.getServiceName(), false); account, spreadsheetsClient.getServiceName(), false);
} catch (OperationCanceledException e) { } catch (OperationCanceledException e) {
Log.d(TAG, "Unable to get auth token", e);
return retryUpload(); return retryUpload();
} catch (AuthenticatorException e) { } catch (AuthenticatorException e) {
Log.d(TAG, "Unable to get auth token", e);
return retryUpload(); return retryUpload();
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, "Unable to get auth token", e);
return retryUpload(); return retryUpload();
} }
@@ -188,7 +191,7 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
// Get the spreadsheet ID // Get the spreadsheet ID
publishProgress(PROGRESS_GET_SPREADSHEET_ID); publishProgress(PROGRESS_GET_SPREADSHEET_ID);
if (!getSpreadSheetId(title, false)) { if (!fetchSpreadSheetId(title, false)) {
return retryUpload(); return retryUpload();
} }
@@ -205,7 +208,7 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
// See http://code.google.com/p/gdata-issues/issues/detail?id=929 // See http://code.google.com/p/gdata-issues/issues/detail?id=929
// Try to find the created spreadsheet. // Try to find the created spreadsheet.
if (spreadsheetId == null) { if (spreadsheetId == null) {
if (!getSpreadSheetId(title, true)) { if (!fetchSpreadSheetId(title, true)) {
Log.d(TAG, "Unable to check if the new spreadsheet is created"); Log.d(TAG, "Unable to check if the new spreadsheet is created");
return false; return false;
} }
@@ -219,11 +222,11 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
// Get the worksheet ID // Get the worksheet ID
publishProgress(PROGRESS_GET_WORKSHEET_ID); publishProgress(PROGRESS_GET_WORKSHEET_ID);
if (!getWorksheetId()) { if (!fetchWorksheetId()) {
return retryUpload(); return retryUpload();
} }
if (worksheetId == null) { if (worksheetId == null) {
Log.d(TAG, "Unable to get the worksheet ID"); Log.d(TAG, "Unable to get a worksheet ID");
return false; return false;
} }
@@ -239,13 +242,14 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
} }
/** /**
* Gets the spreadsheet id. * Fetches the spreadsheet id. Sets the instance variable
* {@link SendDocsAsyncTask#spreadsheetId}.
* *
* @param title the spreadsheet title * @param title the spreadsheet title
* @param waitFirst wait before checking * @param waitFirst wait before checking
* @return true if completes. * @return true if completes.
*/ */
private boolean getSpreadSheetId(String title, boolean waitFirst) { private boolean fetchSpreadSheetId(String title, boolean waitFirst) {
if (isCancelled()) { if (isCancelled()) {
return false; return false;
} }
@@ -254,7 +258,7 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
try { try {
Thread.sleep(5000); Thread.sleep(5000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to wait", e);
return false; return false;
} }
} }
@@ -262,13 +266,13 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
try { try {
spreadsheetId = SendDocsUtils.getSpreadsheetId(title, documentsClient, documentsAuthToken); spreadsheetId = SendDocsUtils.getSpreadsheetId(title, documentsClient, documentsAuthToken);
} catch (ParseException e) { } catch (ParseException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to fetch spreadsheet ID", e);
return false; return false;
} catch (HttpException e) { } catch (HttpException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to fetch spreadsheet ID", e);
return false; return false;
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to fetch spreadsheet ID", e);
return false; return false;
} }
@@ -278,20 +282,20 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
try { try {
Thread.sleep(5000); Thread.sleep(5000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to wait", e);
return false; return false;
} }
try { try {
spreadsheetId = SendDocsUtils.getSpreadsheetId(title, documentsClient, documentsAuthToken); spreadsheetId = SendDocsUtils.getSpreadsheetId(title, documentsClient, documentsAuthToken);
} catch (ParseException e) { } catch (ParseException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to fetch spreadsheet ID", e);
return false; return false;
} catch (HttpException e) { } catch (HttpException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to fetch spreadsheet ID", e);
return false; return false;
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to fetch spreadsheet ID", e);
return false; return false;
} }
} }
@@ -299,7 +303,8 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
} }
/** /**
* Creates a spreadsheet. * Creates a spreadsheet. If successful, sets the instance variable
* {@link SendDocsAsyncTask#spreadsheetId}.
* *
* @param spreadsheetTitle the spreadsheet title * @param spreadsheetTitle the spreadsheet title
* @return true if completes. * @return true if completes.
@@ -311,17 +316,19 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
try { try {
spreadsheetId = SendDocsUtils.createSpreadsheet(spreadsheetTitle, documentsAuthToken, context); spreadsheetId = SendDocsUtils.createSpreadsheet(spreadsheetTitle, documentsAuthToken, context);
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, "Unable to create spreadsheet", e);
return false; return false;
} }
return true; return true;
} }
/** /**
* Gets the worksheet ID. * Fetches the worksheet ID. Sets the instance variable
* {@link SendDocsAsyncTask#worksheetId}.
* *
* @return true if completes. * @return true if completes.
*/ */
private boolean getWorksheetId() { private boolean fetchWorksheetId() {
if (isCancelled()) { if (isCancelled()) {
return false; return false;
} }
@@ -329,13 +336,13 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
worksheetId = SendDocsUtils.getWorksheetId( worksheetId = SendDocsUtils.getWorksheetId(
spreadsheetId, spreadsheetsClient, spreadsheetsAuthToken); spreadsheetId, spreadsheetsClient, spreadsheetsAuthToken);
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to fetch worksheet ID", e);
return false; return false;
} catch (AuthenticationException e) { } catch (AuthenticationException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to fetch worksheet ID", e);
return false; return false;
} catch (ParseException e) { } catch (ParseException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to fetch worksheet ID", e);
return false; return false;
} }
return true; return true;
@@ -354,7 +361,7 @@ public class SendDocsAsyncTask extends AsyncTask<Void, Integer, Boolean> {
try { try {
SendDocsUtils.addTrackInfo(track, spreadsheetId, worksheetId, spreadsheetsAuthToken, context); SendDocsUtils.addTrackInfo(track, spreadsheetId, worksheetId, spreadsheetsAuthToken, context);
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, e.getMessage()); Log.d(TAG, "Unable to add track info", e);
return false; return false;
} }
return true; return true;
@@ -104,18 +104,25 @@ public class SendDocsUtils {
public static String getSpreadsheetId( public static String getSpreadsheetId(
String title, DocumentsClient documentsClient, String authToken) String title, DocumentsClient documentsClient, String authToken)
throws IOException, ParseException, HttpException { throws IOException, ParseException, HttpException {
String uri = String.format(GET_SPREADSHEET_BY_TITLE_URI, URLEncoder.encode(title)); GDataParser gDataParser = null;
GDataParser gdataParser = documentsClient.getParserForFeed(Entry.class, uri, authToken); try {
gdataParser.init(); String uri = String.format(GET_SPREADSHEET_BY_TITLE_URI, URLEncoder.encode(title));
gDataParser = documentsClient.getParserForFeed(Entry.class, uri, authToken);
gDataParser.init();
while (gdataParser.hasMoreData()) { while (gDataParser.hasMoreData()) {
Entry entry = gdataParser.readNextEntry(null); Entry entry = gDataParser.readNextEntry(null);
String entryTitle = entry.getTitle(); String entryTitle = entry.getTitle();
if (entryTitle.equals(title)) { if (entryTitle.equals(title)) {
return getEntryId(entry); return getEntryId(entry);
}
}
return null;
} finally {
if (gDataParser != null) {
gDataParser.close();
} }
} }
return null;
} }
/** /**
@@ -166,6 +173,7 @@ public class SendDocsUtils {
// The GData API sometimes throws an error, even though creation of // The GData API sometimes throws an error, even though creation of
// the document succeeded. In that case let's just return. The caller // the document succeeded. In that case let's just return. The caller
// then needs to check if the doc actually exists. // then needs to check if the doc actually exists.
Log.d(TAG, "Unable to read result after creating a spreadsheet", e);
return null; return null;
} finally { } finally {
outputStream.close(); outputStream.close();
@@ -212,18 +220,25 @@ public class SendDocsUtils {
public static String getWorksheetId( public static String getWorksheetId(
String spreadsheetId, SpreadsheetsClient spreadsheetClient, String authToken) String spreadsheetId, SpreadsheetsClient spreadsheetClient, String authToken)
throws IOException, AuthenticationException, ParseException { throws IOException, AuthenticationException, ParseException {
String uri = String.format(GET_WORKSHEETS_URI, spreadsheetId); GDataParser gDataParser = null;
GDataParser gDataParser = spreadsheetClient.getParserForWorksheetsFeed(uri, authToken); try {
gDataParser.init(); String uri = String.format(GET_WORKSHEETS_URI, spreadsheetId);
if (!gDataParser.hasMoreData()) { gDataParser = spreadsheetClient.getParserForWorksheetsFeed(uri, authToken);
Log.d(TAG, "No worksheet"); gDataParser.init();
return null; if (!gDataParser.hasMoreData()) {
} Log.d(TAG, "No worksheet");
return null;
}
// Get the first worksheet // Get the first worksheet
WorksheetEntry worksheetEntry = (WorksheetEntry) gDataParser.readNextEntry( WorksheetEntry worksheetEntry =
new WorksheetEntry()); (WorksheetEntry) gDataParser.readNextEntry(new WorksheetEntry());
return getWorksheetEntryId(worksheetEntry); return getWorksheetEntryId(worksheetEntry);
} finally {
if (gDataParser != null) {
gDataParser.close();
}
}
} }
/** /**