protected long changes(List<Change> changes) throws IOException {
    com.google.api.services.drive.Drive.Changes.List request = drive.changes().list();
    request.setMaxResults(100);
    long lastChange = -1;
    Object lastChangeData = stub.storage().get(StorageService.REMOTE_CHANGE);
    if (lastChangeData != null) {
      if (lastChangeData instanceof Double) {
        lastChange = ((Double) lastChangeData).longValue();
        request.setStartChangeId(lastChange + 1);
      } else if (lastChangeData instanceof Long) {
        lastChange = (Long) lastChangeData;
        request.setStartChangeId(lastChange + 1);
      } else {
        throw new IllegalArgumentException(lastChangeData.getClass().getName());
      }
    }

    while (true) {
      ChangeList response = execute(request);
      if (changes != null) {
        changes.addAll(response.getItems());
      }
      if (StringUtils.isEmpty(response.getNextPageToken())) {
        List<Change> items = response.getItems();
        if (!items.isEmpty()) lastChange = items.get(items.size() - 1).getId();
        break;
      } else {
        request.setPageToken(response.getNextPageToken());
      }
    }
    return lastChange;
  }
Пример #2
0
  /**
   * Retrieve a collection of files that have changed since the provided {@code changeId}.
   *
   * @param changeId Change ID to retrieve changed files from.
   * @return Map of changed files key'ed by their file ID.
   */
  private Map<String, File> getChangedFiles(long changeId) {
    Map<String, File> result = new HashMap<String, File>();

    try {
      Changes.List request = mService.changes().list().setStartChangeId(changeId);
      do {
        ChangeList changes = request.execute();
        long largestChangeId = changes.getLargestChangeId().longValue();

        for (Change change : changes.getItems()) {
          if (change.getDeleted()) {
            result.put(change.getFileId(), null);
          } else if (TEXT_PLAIN.equals(change.getFile().getMimeType())) {
            result.put(change.getFileId(), change.getFile());
          }
        }

        if (largestChangeId > mLargestChangeId) {
          mLargestChangeId = largestChangeId;
        }
        request.setPageToken(changes.getNextPageToken());
      } while (request.getPageToken() != null && request.getPageToken().length() > 0);
    } catch (IOException e) {
      e.printStackTrace();
    }

    Log.d(TAG, "Got changed Drive files: " + result.size() + " - " + mLargestChangeId);
    return result;
  }