Java Code Examples for com.google.api.services.drive.Drive.Files#List

The following examples show how to use com.google.api.services.drive.Drive.Files#List . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SyncAdapter.java    From mytracks with Apache License 2.0 6 votes vote down vote up
/**
 * Gets all the files from a request.
 * 
 * @param request the request
 * @param excludeSharedWithMe true to exclude shared with me files
 * @return a map of file id to file
 */
private Map<String, File> getFiles(Files.List request, boolean excludeSharedWithMe)
    throws IOException {
  Map<String, File> idToFileMap = new HashMap<String, File>();
  do {
    FileList files = request.execute();

    for (File file : files.getItems()) {
      if (excludeSharedWithMe && file.getSharedWithMeDate() != null) {
        continue;
      }
      idToFileMap.put(file.getId(), file);
    }
    request.setPageToken(files.getNextPageToken());
  } while (request.getPageToken() != null && request.getPageToken().length() > 0);
  return idToFileMap;
}
 
Example 2
Source File: GoogleDriveUtils.java    From components with Apache License 2.0 5 votes vote down vote up
public File putResource(GoogleDrivePutParameters parameters) throws IOException {
    String folderId = parameters.getDestinationFolderId();
    File putFile = new File();
    putFile.setParents(Collections.singletonList(folderId));
    Files.List fileRequest = drive.files().list()
            .setQ(format(QUERY_NOTTRASHED_NAME_NOTMIME_INPARENTS, parameters.getResourceName(), MIME_TYPE_FOLDER, folderId));
    LOG.debug("[putResource] `{}` Exists in `{}` ? with `{}`.", parameters.getResourceName(),
            parameters.getDestinationFolderId(), fileRequest.getQ());
    FileList existingFiles = fileRequest.execute();
    if (existingFiles.getFiles().size() > 1) {
        throw new IOException(messages.getMessage("error.file.more.than.one", parameters.getResourceName()));
    }
    if (existingFiles.getFiles().size() == 1) {
        if (!parameters.isOverwriteIfExist()) {
            throw new IOException(messages.getMessage("error.file.already.exist", parameters.getResourceName()));
        }
        LOG.debug("[putResource] {} will be overwritten...", parameters.getResourceName());
        drive.files().delete(existingFiles.getFiles().get(0).getId()).execute();
    }
    putFile.setName(parameters.getResourceName());
    String metadata = "id,parents,name";
    if (!StringUtils.isEmpty(parameters.getFromLocalFilePath())) {
        // Reading content from local fileName
        FileContent fContent = new FileContent(null, new java.io.File(parameters.getFromLocalFilePath()));
        putFile = drive.files().create(putFile, fContent).setFields(metadata).execute();
        //

    } else if (parameters.getFromBytes() != null) {
        AbstractInputStreamContent content = new ByteArrayContent(null, parameters.getFromBytes());
        putFile = drive.files().create(putFile, content).setFields(metadata).execute();
    }
    return putFile;
}
 
Example 3
Source File: SyncAdapter.java    From mytracks with Apache License 2.0 4 votes vote down vote up
/**
 * Performs initial sync.
 */
private void performInitialSync() throws IOException {

  // Get the largest change id first to avoid race conditions
  About about = drive.about().get().setFields(ABOUT_GET_FIELDS).execute();
  long largestChangeId = about.getLargestChangeId();

  // Get all the KML/KMZ files in the "My Drive:/My Tracks" folder
  Files.List myTracksFolderRequest = drive.files()
      .list().setQ(String.format(Locale.US, SyncUtils.MY_TRACKS_FOLDER_FILES_QUERY, folderId));
  Map<String, File> myTracksFolderMap = getFiles(myTracksFolderRequest, true);

  // Handle tracks that are already uploaded to Google Drive
  Set<String> syncedDriveIds = updateSyncedTracks();
  for (String driveId : syncedDriveIds) {
    myTracksFolderMap.remove(driveId);
  }

  // Get all the KML/KMZ files in the "Shared with me:/" folder
  Files.List sharedWithMeRequest = drive.files()
      .list().setQ(SyncUtils.SHARED_WITH_ME_FILES_QUERY);
  Map<String, File> sharedWithMeMap = getFiles(sharedWithMeRequest, false);

  try {
    insertNewTracks(myTracksFolderMap.values());
    insertNewTracks(sharedWithMeMap.values());
    PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId);
  } catch (IOException e) {

    // Remove all imported tracks
    Cursor cursor = null;
    try {
      cursor = myTracksProviderUtils.getTrackCursor(SyncUtils.DRIVE_ID_TRACKS_QUERY, null, null);
      if (cursor != null && cursor.moveToFirst()) {
        do {
          Track track = myTracksProviderUtils.createTrack(cursor);
          if (!syncedDriveIds.contains(track.getDriveId())) {
            myTracksProviderUtils.deleteTrack(context, track.getId());
          }
        } while (cursor.moveToNext());
      }
    } finally {
      if (cursor != null) {
        cursor.close();
      }
    }
    throw e;
  }
}