private void doUpload() { statusMessage = activity.getString(R.string.error_sending_to_docs); success = false; try { // Transmit info via GData feed: // ------------------------------- Log.d(LOG_TAG, "Uploading to spreadsheet"); success = uploadToDocs(); if (success) { statusMessage = activity.getString(R.string.status_have_been_uploaded_to_docs); } else { statusMessage = activity.getString(R.string.error_sending_to_docs); } SettingsActivity.getInstance() .getAndSetProgressValue(100 - SettingsActivity.getInstance().getProgressValue()); Log.d(LOG_TAG, "Done."); } finally { if (onCompletion != null) { activity.runOnUiThread(onCompletion); } } }
/** * Uploads the collection to Google Docs using the docs GData feed. * * @param c the collection */ private boolean uploadToDocs() { GDataWrapper<GDataServiceClient> docListWrapper = new GDataWrapper<GDataServiceClient>(); docListWrapper.setAuthManager(docListAuth); docListWrapper.setRetryOnAuthFailure(true); GDataWrapper<GDataServiceClient> trixWrapper = new GDataWrapper<GDataServiceClient>(); trixWrapper.setAuthManager(trixAuth); trixWrapper.setRetryOnAuthFailure(true); DocsHelper docsHelper = new DocsHelper(); GDataClient androidClient = null; try { androidClient = GDataClientFactory.getGDataClient(activity); SpreadsheetsClient gdataClient = new SpreadsheetsClient( androidClient, new XmlDocsGDataParserFactory(new AndroidXmlParserFactory())); trixWrapper.setClient(gdataClient); Log.d(LOG_TAG, "GData connection prepared: " + this.docListAuth); final int progressLeft = (50 / collectionsToSend.length) / 3; for (String name : collectionsToSend) { Log.d(LOG_TAG, "Sending to Google Docs: " + name); DocumentsClient docsGdataClient = new DocumentsClient( androidClient, new XmlDocsGDataParserFactory(new AndroidXmlParserFactory())); docListWrapper.setClient(docsGdataClient); final String sheetTitle = name + " (Shelves) "; String spreadsheetId = null; // First try to find the spreadsheet: /* * try { spreadsheetId = docsHelper.requestSpreadsheetId( * docListWrapper, sheetTitle); } catch (IOException e) { * Log.i(LOG_TAG, "Spreadsheet lookup failed.", e); return * false; } * * if (spreadsheetId == null) { * SettingsActivity.getInstance().getAndSetProgressValue( * progressLeft); // Waiting a few seconds and trying again. * Maybe the server // just // had a // hiccup (unfortunately * that happens quite a lot...). try { Thread.sleep(5000); } * catch (InterruptedException e) { Log.e(LOG_TAG, * "Sleep interrupted", e); } * * try { spreadsheetId = docsHelper.requestSpreadsheetId( * docListWrapper, sheetTitle); } catch (IOException e) { * Log.i(LOG_TAG, "2nd spreadsheet lookup failed.", e); return * false; } } */ // We were unable to find an existing spreadsheet, so create a // new one. if (spreadsheetId == null) { Log.i(LOG_TAG, "Creating new spreadsheet: " + sheetTitle); try { spreadsheetId = docsHelper.createSpreadsheet( activity, docListWrapper, name.toLowerCase(), sheetTitle); } catch (IOException e) { Log.i(LOG_TAG, "Failed to create new spreadsheet " + sheetTitle, e); return false; } createdNewSpreadSheet = true; if (spreadsheetId == null) { SettingsActivity.getInstance().getAndSetProgressValue(progressLeft); // The previous creation might have succeeded even // though GData reported an error. Seems to be a know // bug, // see // http://code.google.com/p/gdata-issues/issues/detail?id=929 Log.w(LOG_TAG, "Create might have failed. Trying to find created document."); try { Thread.sleep(5000); } catch (InterruptedException e) { Log.e(LOG_TAG, "Sleep interrupted", e); } try { spreadsheetId = docsHelper.requestSpreadsheetId(docListWrapper, sheetTitle); } catch (IOException e) { Log.i(LOG_TAG, "Failed create-failed lookup", e); return false; } if (spreadsheetId == null) { try { Thread.sleep(5000); } catch (InterruptedException e) { Log.e(LOG_TAG, "Sleep interrupted", e); } try { Log.w(LOG_TAG, "Checking id one more time"); spreadsheetId = docsHelper.requestSpreadsheetId(docListWrapper, sheetTitle); } catch (IOException e) { Log.i(LOG_TAG, "Failed create-failed relookup", e); return false; } } if (spreadsheetId == null) { Log.w(LOG_TAG, "Creating new spreadsheet really failed."); return false; } } } String worksheetId = null; try { worksheetId = docsHelper.getWorksheetId(trixWrapper, spreadsheetId); if (worksheetId == null) { throw new IOException("Worksheet ID lookup returned empty"); } } catch (IOException e) { Log.i(LOG_TAG, "Looking up worksheet id failed.", e); return false; } SettingsActivity.getInstance().getAndSetProgressValue(progressLeft); final String fileName = ExportUtilities.determineShelvesFileName(name); Uri uri = ShelvesApplication.TYPES_TO_URI.get(name.toLowerCase()); if (uri == null) uri = ShelvesApplication.TYPES_TO_URI.get(name); ExportUtilities.exportingToShelves(IOUtilities.getExternalFile(fileName), cr, uri); final String[] data = TextUtilities.readFileAsString(IOUtilities.getExternalFile(fileName).toString()) .replaceAll("\"", "") .split("\n"); Log.i(LOG_TAG, "Updating spreadsheet for " + name); final int dataLength = data.length; int updatePercentage = dataLength / progressLeft; if (data != null) { final String[] headers = data[0].split("\\t+"); for (int i = 1; i < dataLength; i++) { final String[] row = data[i].split("\\t+"); DocsTagBuilder dtb = new DocsTagBuilder(); /* * if (!createdNewSpreadSheet) { if * (querySpreadsheet(spreadsheetId, worksheetId, * trixAuth, row[0])) continue; } */ for (int j = 0; j < row.length; j++) { dtb.append(headers[j], row[j]); } putShelvesData(activity, trixAuth, spreadsheetId, worksheetId, dtb); if (updatePercentage <= 0) updatePercentage = 5; if (i % updatePercentage == 0) SettingsActivity.getInstance().getAndSetProgressValue(1); // GJT: Can't send more than 20 inserts/second... // just chill out no matter what if (dataLength % 20 == 0) { try { Thread.sleep(1500); } catch (InterruptedException e) { Log.e(LOG_TAG, "Sleep interrupted", e); } } } } Log.i(LOG_TAG, "Done uploading to docs."); } } catch (IOException e) { Log.e(LOG_TAG, "Unable to upload docs.", e); return false; } finally { if (androidClient != null) { androidClient.close(); } } return true; }