Ejemplo n.º 1
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;
  }
  private List<ChangeRecord> remoteChange() throws IOException {
    List<Change> changes = new ArrayList<Change>();
    long lastChange = changes(changes);
    stub.storage().put(StorageService.REMOTE_CHANGE, lastChange);
    List<ChangeRecord> records = new ArrayList<ChangeRecord>();

    for (Change remoteChange : changes) {
      String local = stub.storage().remoteToLocal().get(remoteChange.getFileId());
      if (remoteChange.getDeleted()) {
        records.add(
            new ChangeRecord(
                Operation.REMOTE_DELETE, local, remoteChange.getFileId(), remoteChange.getFile()));
      } else if (remoteChange.getFile().getLabels().getTrashed()) {
        records.add(
            new ChangeRecord(
                Operation.REMOTE_DELETE, local, remoteChange.getFileId(), remoteChange.getFile()));
      } else if (StringUtils.isEmpty(local)) {
        // Cannot find this file, should be new
        records.add(
            new ChangeRecord(
                Operation.REMOTE_INSERT, null, remoteChange.getFileId(), remoteChange.getFile()));
      } else {
        if (!DriveUtils.isDirectory(remoteChange.getFile())
            && !DriveUtils.isGoogleDoc(remoteChange.getFile())) {
          String remoteName = remoteChange.getFile().getTitle();
          String remoteMd5 = remoteChange.getFile().getMd5Checksum();
          File localFile = DriveUtils.absolutePath(local);
          if (localFile.isFile()) {
            String localMd5 = DriveUtils.md5Checksum(localFile);
            if (localMd5.equals(remoteMd5)) {
              // Sometimes google drive will record change even if
              // the name and MD5 are exactly the same
              if (!remoteName.equals(localFile.getName())) {
                records.add(
                    new ChangeRecord(
                        Operation.REMOTE_RENAME,
                        local,
                        remoteChange.getFileId(),
                        remoteChange.getFile()));
              }
            } else {
              records.add(
                  new ChangeRecord(
                      Operation.REMOTE_CHANGE,
                      local,
                      remoteChange.getFileId(),
                      remoteChange.getFile()));
            }
          } else {
            records.add(
                new ChangeRecord(
                    Operation.REMOTE_INSERT,
                    null,
                    remoteChange.getFileId(),
                    remoteChange.getFile()));
          }
        } else {
          // For directory just check the name change
          String remoteName = remoteChange.getFile().getTitle();
          String localName = DriveUtils.absolutePath(local).getName();
          if (!remoteName.equals(localName)) {
            records.add(
                new ChangeRecord(
                    Operation.REMOTE_RENAME,
                    local,
                    remoteChange.getFileId(),
                    remoteChange.getFile()));
          }
        }
      }
    }

    return records;
  }