コード例 #1
0
ファイル: GoogleUtils.java プロジェクト: rborisov/my_tracker
 /**
  * Searches a map in user's Google Maps.
  *
  * @param title the title of map
  * @param activity activity to get context
  * @param isDelete whether delete the map of this track in the Google Maps
  * @return true means find the map
  */
 private static boolean searchMapByTitle(String title, Activity activity, boolean isDelete) {
   Context context = activity.getApplicationContext();
   MapsClient mapsClient =
       new MapsClient(
           GDataClientFactory.getGDataClient(context),
           new XmlMapsGDataParserFactory(new AndroidXmlParserFactory()));
   ArrayList<MapsMapMetadata> mapData = getMaps(context, mapsClient);
   for (MapsMapMetadata oneData : mapData) {
     if (oneData.getDescription().indexOf(DOCUMENT_NAME_PREFIX) > -1
         && oneData.getTitle().equals(title)) {
       if (isDelete) {
         try {
           mapsClient.deleteEntry(
               oneData.getGDataEditUri(),
               AccountManager.get(context)
                   .blockingGetAuthToken(getAccount(context), MapsConstants.SERVICE_NAME, false));
           return true;
         } catch (Exception e) {
           Log.d(EndToEndTestUtils.LOG_TAG, "Unable to drop map", e);
           return false;
         }
       }
       return true;
     }
   }
   return false;
 }
コード例 #2
0
ファイル: GoogleUtils.java プロジェクト: rborisov/my_tracker
  /**
   * Removes old tracks on Google Maps created by MyTracks test.
   *
   * @param activity
   */
  public static void deleteTracksOnGoogleMaps(Activity activity) {
    Context context = activity.getApplicationContext();
    MapsClient mapsClient =
        new MapsClient(
            GDataClientFactory.getGDataClient(context),
            new XmlMapsGDataParserFactory(new AndroidXmlParserFactory()));
    ArrayList<MapsMapMetadata> mapData = getMaps(context, mapsClient);
    for (MapsMapMetadata oneData : mapData) {

      try {
        mapsClient.deleteEntry(
            oneData.getGDataEditUri(),
            AccountManager.get(context)
                .blockingGetAuthToken(getAccount(context), MapsConstants.SERVICE_NAME, false));
      } catch (Exception e) {
        Log.d(EndToEndTestUtils.LOG_TAG, "Unable to drop map", e);
      }
    }
  }
コード例 #3
0
ファイル: SendToDocs.java プロジェクト: wzx54321/Shelves
  /**
   * 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;
  }