Example #1
0
  public static String uploadDriveFile(
      String title,
      String description,
      String parentId,
      String mimeType,
      String filename,
      String path,
      String shareTo) {

    // File's metadata.
    File body = new File();
    body.setTitle(title);
    body.setDescription(description);
    body.setMimeType(mimeType);

    body.setParents(Arrays.asList(new ParentReference().setId(parentId)));
    // File's content.
    java.io.File fileContent = new java.io.File(path);
    FileContent mediaContent = new FileContent(mimeType, fileContent);
    try {
      File file = service.files().insert(body, mediaContent).execute();
      service.permissions().insert(file.getId(), perm).execute();

      // Uncomment the following line to print the File ID.
      // System.out.println("File ID: " + file.getId());

      return file.getId();
    } catch (IOException e) {
      System.out.println("An error occured: " + e);
      return null;
    }
  }
Example #2
0
  /** Insert all new local files in Google Drive. */
  private void insertNewLocalFiles() {
    Uri uri = getNotesUri(mAccount.name);
    try {
      Cursor cursor =
          mProvider.query(
              uri, PROJECTION, NotePad.Notes.COLUMN_NAME_FILE_ID + " is NULL", null, null);

      Log.d(TAG, "Inserting new local files: " + cursor.getCount());

      if (cursor.moveToFirst()) {
        do {
          Uri localFileUri = getNoteUri(mAccount.name, cursor.getString(COLUMN_INDEX_ID));

          if (cursor.getShort(COLUMN_INDEX_DELETED) != 0) {
            mProvider.delete(localFileUri, null, null);
          } else {
            File newFile = new File();

            newFile.setTitle(cursor.getString(COLUMN_INDEX_TITLE));
            newFile.setMimeType(TEXT_PLAIN);
            String content = cursor.getString(COLUMN_INDEX_NOTE);

            try {
              File insertedFile = null;

              if (content != null && content.length() > 0) {
                insertedFile =
                    mService
                        .files()
                        .insert(newFile, ByteArrayContent.fromString(TEXT_PLAIN, content))
                        .execute();
              } else {
                insertedFile = mService.files().insert(newFile).execute();
              }

              // Update the local file to add the file ID.
              ContentValues values = new ContentValues();
              values.put(
                  NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,
                  insertedFile.getModifiedDate().getValue());
              values.put(
                  NotePad.Notes.COLUMN_NAME_CREATE_DATE, insertedFile.getCreatedDate().getValue());
              values.put(NotePad.Notes.COLUMN_NAME_FILE_ID, insertedFile.getId());

              mProvider.update(localFileUri, values, null, null);
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        } while (cursor.moveToNext());
      }
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }
  protected File addFile(
      String extRepositoryParentFolderKey,
      String mimeType,
      String title,
      String description,
      InputStream inputStream)
      throws PortalException {

    try {
      File file = new File();

      file.setDescription(description);
      file.setMimeType(mimeType);

      Drive drive = getDrive();

      Drive.Files driveFiles = drive.files();

      File extRepositoryParentFolderFile = getFile(drive, extRepositoryParentFolderKey);

      ParentReference parentReference = new ParentReference();

      parentReference.setId(extRepositoryParentFolderFile.getId());

      file.setParents(Arrays.asList(parentReference));

      file.setTitle(title);

      if (inputStream != null) {
        InputStreamContent inputStreamContent = new InputStreamContent(mimeType, inputStream);

        Drive.Files.Insert driveFilesInsert = driveFiles.insert(file, inputStreamContent);

        return driveFilesInsert.execute();
      } else {
        Drive.Files.Insert driveFilesInsert = driveFiles.insert(file);

        return driveFilesInsert.execute();
      }
    } catch (IOException ioe) {
      _log.error(ioe, ioe);

      throw new SystemException(ioe);
    }
  }
  private void syncNotes(final String syncAccountName, final String accessToken) {
    final Drive drive = getDriveService(syncAccountName, accessToken);
    ContentResolver cr = getContentResolver();
    try {
      // loop over saved files and add any new notes to drive
      Cursor savedNotes =
          cr.query(NotesProvider.CONTENT_URI, NotesSyncQuery.PROJECTION, null, null, null);
      if (savedNotes.moveToFirst()) {
        do {
          final String driveId = savedNotes.getString(NotesSyncQuery.DRIVE_ID);
          if (TextUtils.isEmpty(driveId)) {
            // exists locally but not in drive Ð upload it
            File newNote = new File();
            newNote.setTitle(savedNotes.getString(NotesSyncQuery.TITLE));
            newNote.setMimeType(NOTE_MIME_TYPE);

            File inserted =
                drive
                    .files()
                    .insert(
                        newNote,
                        ByteArrayContent.fromString(
                            NOTE_MIME_TYPE, savedNotes.getString(NotesSyncQuery.BODY)))
                    .execute();

            // save the drive id to the db
            ContentValues cv = new ContentValues();
            cv.put(NotesProvider.KEY_DRIVE_ID, inserted.getId());
            Uri noteUri =
                ContentUris.withAppendedId(
                    NotesProvider.CONTENT_URI, savedNotes.getLong(NotesSyncQuery.ID));
            cr.update(noteUri, cv, null, null);
          } else {
            // TODO compare timestamps etc.
          }
        } while (savedNotes.moveToNext());
      }

      // loop over all files in drive and see if any need to be
      // synced locally
      FileList driveFilesList = drive.files().list().execute();
      for (File remote : driveFilesList.getItems()) {
        if (remote.getLabels().getTrashed()) {
          // skip deleted files
          continue;
        }
        final String where = NotesProvider.KEY_DRIVE_ID + "=?";
        final String[] arguments = new String[] {remote.getId()};
        Cursor c =
            cr.query(
                NotesProvider.CONTENT_URI, NotesDownloadQuery.PROJECTION, where, arguments, null);
        if (c.getCount() == 0) {
          // exists in drive but not locally Ð download it
          final String title = remote.getTitle();
          final String body = getFileContents(drive, remote.getDownloadUrl());
          final ContentValues cv = new ContentValues();
          cv.put(NotesProvider.KEY_TITLE, title);
          cv.put(NotesProvider.KEY_BODY, body);
          cv.put(NotesProvider.KEY_DRIVE_ID, remote.getId());
          cv.put(NotesProvider.KEY_LAST_MODIFIED, remote.getModifiedDate().getValue());
          cr.insert(NotesProvider.CONTENT_URI, cv);
        } else {
          // TODO compare timestamps etc.
        }
      }

    } catch (IOException e) {
      // FIXME error handling
      Log.e(getClass().getSimpleName(), "Drive esplode", e);
    }
  }