From 8472dbb195a11c267c5c2a574d8a950e56cb746a Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Wed, 25 Jan 2012 10:41:38 -0800 Subject: [PATCH] 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. --- .../mytracks/io/docs/SendDocsActivity.java | 13 ++++- .../mytracks/io/docs/SendDocsAsyncTask.java | 57 +++++++++++-------- .../apps/mytracks/io/docs/SendDocsUtils.java | 55 +++++++++++------- 3 files changed, 79 insertions(+), 46 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsActivity.java index cacb62a2d..d6839c51d 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsActivity.java @@ -55,8 +55,19 @@ public class SendDocsActivity extends Activity { } else { Intent intent = getIntent(); 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); - + if (trackId == -1L) { + setResult(RESULT_OK, new Intent().putExtra(SUCCESS, false)); + finish(); + return; + } + asyncTask = new SendDocsAsyncTask(this, account, trackId); asyncTask.execute(); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsAsyncTask.java index 2aa85211f..87e7b2677 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsAsyncTask.java @@ -107,7 +107,7 @@ public class SendDocsAsyncTask extends AsyncTask { */ public void setActivity(SendDocsActivity activity) { this.activity = activity; - if (completed) { + if (completed && activity != null) { activity.onAsyncTaskCompleted(success); } } @@ -168,10 +168,13 @@ public class SendDocsAsyncTask extends AsyncTask { spreadsheetsAuthToken = AccountManager.get(context).blockingGetAuthToken( account, spreadsheetsClient.getServiceName(), false); } catch (OperationCanceledException e) { + Log.d(TAG, "Unable to get auth token", e); return retryUpload(); } catch (AuthenticatorException e) { + Log.d(TAG, "Unable to get auth token", e); return retryUpload(); } catch (IOException e) { + Log.d(TAG, "Unable to get auth token", e); return retryUpload(); } @@ -188,7 +191,7 @@ public class SendDocsAsyncTask extends AsyncTask { // Get the spreadsheet ID publishProgress(PROGRESS_GET_SPREADSHEET_ID); - if (!getSpreadSheetId(title, false)) { + if (!fetchSpreadSheetId(title, false)) { return retryUpload(); } @@ -205,7 +208,7 @@ public class SendDocsAsyncTask extends AsyncTask { // See http://code.google.com/p/gdata-issues/issues/detail?id=929 // Try to find the created spreadsheet. if (spreadsheetId == null) { - if (!getSpreadSheetId(title, true)) { + if (!fetchSpreadSheetId(title, true)) { Log.d(TAG, "Unable to check if the new spreadsheet is created"); return false; } @@ -219,11 +222,11 @@ public class SendDocsAsyncTask extends AsyncTask { // Get the worksheet ID publishProgress(PROGRESS_GET_WORKSHEET_ID); - if (!getWorksheetId()) { + if (!fetchWorksheetId()) { return retryUpload(); } if (worksheetId == null) { - Log.d(TAG, "Unable to get the worksheet ID"); + Log.d(TAG, "Unable to get a worksheet ID"); return false; } @@ -239,13 +242,14 @@ public class SendDocsAsyncTask extends AsyncTask { } /** - * Gets the spreadsheet id. - * + * Fetches the spreadsheet id. Sets the instance variable + * {@link SendDocsAsyncTask#spreadsheetId}. + * * @param title the spreadsheet title * @param waitFirst wait before checking * @return true if completes. */ - private boolean getSpreadSheetId(String title, boolean waitFirst) { + private boolean fetchSpreadSheetId(String title, boolean waitFirst) { if (isCancelled()) { return false; } @@ -254,7 +258,7 @@ public class SendDocsAsyncTask extends AsyncTask { try { Thread.sleep(5000); } catch (InterruptedException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to wait", e); return false; } } @@ -262,13 +266,13 @@ public class SendDocsAsyncTask extends AsyncTask { try { spreadsheetId = SendDocsUtils.getSpreadsheetId(title, documentsClient, documentsAuthToken); } catch (ParseException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to fetch spreadsheet ID", e); return false; } catch (HttpException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to fetch spreadsheet ID", e); return false; } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to fetch spreadsheet ID", e); return false; } @@ -278,20 +282,20 @@ public class SendDocsAsyncTask extends AsyncTask { try { Thread.sleep(5000); } catch (InterruptedException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to wait", e); return false; } try { spreadsheetId = SendDocsUtils.getSpreadsheetId(title, documentsClient, documentsAuthToken); } catch (ParseException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to fetch spreadsheet ID", e); return false; } catch (HttpException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to fetch spreadsheet ID", e); return false; } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to fetch spreadsheet ID", e); return false; } } @@ -299,8 +303,9 @@ public class SendDocsAsyncTask extends AsyncTask { } /** - * Creates a spreadsheet. - * + * Creates a spreadsheet. If successful, sets the instance variable + * {@link SendDocsAsyncTask#spreadsheetId}. + * * @param spreadsheetTitle the spreadsheet title * @return true if completes. */ @@ -311,17 +316,19 @@ public class SendDocsAsyncTask extends AsyncTask { try { spreadsheetId = SendDocsUtils.createSpreadsheet(spreadsheetTitle, documentsAuthToken, context); } catch (IOException e) { + Log.d(TAG, "Unable to create spreadsheet", e); return false; } return true; } /** - * Gets the worksheet ID. - * + * Fetches the worksheet ID. Sets the instance variable + * {@link SendDocsAsyncTask#worksheetId}. + * * @return true if completes. */ - private boolean getWorksheetId() { + private boolean fetchWorksheetId() { if (isCancelled()) { return false; } @@ -329,13 +336,13 @@ public class SendDocsAsyncTask extends AsyncTask { worksheetId = SendDocsUtils.getWorksheetId( spreadsheetId, spreadsheetsClient, spreadsheetsAuthToken); } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to fetch worksheet ID", e); return false; } catch (AuthenticationException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to fetch worksheet ID", e); return false; } catch (ParseException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to fetch worksheet ID", e); return false; } return true; @@ -354,7 +361,7 @@ public class SendDocsAsyncTask extends AsyncTask { try { SendDocsUtils.addTrackInfo(track, spreadsheetId, worksheetId, spreadsheetsAuthToken, context); } catch (IOException e) { - Log.d(TAG, e.getMessage()); + Log.d(TAG, "Unable to add track info", e); return false; } return true; diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsUtils.java b/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsUtils.java index 93e0bb023..6187baade 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/docs/SendDocsUtils.java @@ -104,18 +104,25 @@ public class SendDocsUtils { public static String getSpreadsheetId( String title, DocumentsClient documentsClient, String authToken) throws IOException, ParseException, HttpException { - String uri = String.format(GET_SPREADSHEET_BY_TITLE_URI, URLEncoder.encode(title)); - GDataParser gdataParser = documentsClient.getParserForFeed(Entry.class, uri, authToken); - gdataParser.init(); + GDataParser gDataParser = null; + try { + String uri = String.format(GET_SPREADSHEET_BY_TITLE_URI, URLEncoder.encode(title)); + gDataParser = documentsClient.getParserForFeed(Entry.class, uri, authToken); + gDataParser.init(); - while (gdataParser.hasMoreData()) { - Entry entry = gdataParser.readNextEntry(null); - String entryTitle = entry.getTitle(); - if (entryTitle.equals(title)) { - return getEntryId(entry); + while (gDataParser.hasMoreData()) { + Entry entry = gDataParser.readNextEntry(null); + String entryTitle = entry.getTitle(); + if (entryTitle.equals(title)) { + 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 document succeeded. In that case let's just return. The caller // then needs to check if the doc actually exists. + Log.d(TAG, "Unable to read result after creating a spreadsheet", e); return null; } finally { outputStream.close(); @@ -212,18 +220,25 @@ public class SendDocsUtils { public static String getWorksheetId( String spreadsheetId, SpreadsheetsClient spreadsheetClient, String authToken) throws IOException, AuthenticationException, ParseException { - String uri = String.format(GET_WORKSHEETS_URI, spreadsheetId); - GDataParser gDataParser = spreadsheetClient.getParserForWorksheetsFeed(uri, authToken); - gDataParser.init(); - if (!gDataParser.hasMoreData()) { - Log.d(TAG, "No worksheet"); - return null; - } + GDataParser gDataParser = null; + try { + String uri = String.format(GET_WORKSHEETS_URI, spreadsheetId); + gDataParser = spreadsheetClient.getParserForWorksheetsFeed(uri, authToken); + gDataParser.init(); + if (!gDataParser.hasMoreData()) { + Log.d(TAG, "No worksheet"); + return null; + } - // Get the first worksheet - WorksheetEntry worksheetEntry = (WorksheetEntry) gDataParser.readNextEntry( - new WorksheetEntry()); - return getWorksheetEntryId(worksheetEntry); + // Get the first worksheet + WorksheetEntry worksheetEntry = + (WorksheetEntry) gDataParser.readNextEntry(new WorksheetEntry()); + return getWorksheetEntryId(worksheetEntry); + } finally { + if (gDataParser != null) { + gDataParser.close(); + } + } } /**