com.google.android.gms.drive.DriveFolder Java Examples
The following examples show how to use
com.google.android.gms.drive.DriveFolder.
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: CreateFolderInFolderActivity.java From android-samples with Apache License 2.0 | 6 votes |
private void createFolderInFolder(final DriveFolder parent) { MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle("New folder") .setMimeType(DriveFolder.MIME_TYPE) .setStarred(true) .build(); getDriveResourceClient() .createFolder(parent, changeSet) .addOnSuccessListener(this, driveFolder -> { showMessage(getString(R.string.file_created, driveFolder.getDriveId().encodeToString())); finish(); }) .addOnFailureListener(this, e -> { Log.e(TAG, "Unable to create file", e); showMessage(getString(R.string.file_create_error)); finish(); }); }
Example #2
Source File: CreateFolderActivity.java From android-samples with Apache License 2.0 | 6 votes |
private void createFolder() { getDriveResourceClient() .getRootFolder() .continueWithTask(task -> { DriveFolder parentFolder = task.getResult(); MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle("New folder") .setMimeType(DriveFolder.MIME_TYPE) .setStarred(true) .build(); return getDriveResourceClient().createFolder(parentFolder, changeSet); }) .addOnSuccessListener(this, driveFolder -> { showMessage(getString(R.string.file_created, driveFolder.getDriveId().encodeToString())); finish(); }) .addOnFailureListener(this, e -> { Log.e(TAG, "Unable to create file", e); showMessage(getString(R.string.file_create_error)); finish(); }); }
Example #3
Source File: SettingsBackup.java From Slide with GNU General Public License v3.0 | 6 votes |
@Override public void onResult(DriveFolder.DriveFileResult result) { progress.setProgress(progress.getCurrentProgress() + 1); if (!result.getStatus().isSuccess()) { errors += 1; return; } if (progress.getCurrentProgress() == progress.getMaxProgress()) { new AlertDialogWrapper.Builder(SettingsBackup.this).setTitle( R.string.backup_success) .setPositiveButton(R.string.btn_close, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setCancelable(false) .show(); } }
Example #4
Source File: FileManager.java From amiibo with GNU General Public License v2.0 | 6 votes |
@DebugLog private void createAmiiboFolder() { MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle(AMIIBO_FOLDER).build(); Drive.DriveApi.getRootFolder(_parent.getClient()) .createFolder(_parent.getClient(), changeSet) .setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() { @Override public void onResult(DriveFolder.DriveFolderResult driveFolderResult) { if (driveFolderResult.getStatus().isSuccess()) { app_folder_for_user = driveFolderResult.getDriveFolder(); listFiles(); } else { app_folder_for_user = null; _parent.onPushFinished(); } } }); }
Example #5
Source File: CreateFileInFolderActivity.java From android-samples with Apache License 2.0 | 6 votes |
private void createFileInFolder(final DriveFolder parent) { getDriveResourceClient() .createContents() .continueWithTask(task -> { DriveContents contents = task.getResult(); OutputStream outputStream = contents.getOutputStream(); try (Writer writer = new OutputStreamWriter(outputStream)) { writer.write("Hello World!"); } MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle("New file") .setMimeType("text/plain") .setStarred(true) .build(); return getDriveResourceClient().createFile(parent, changeSet, contents); }) .addOnSuccessListener(this, driveFile -> showMessage(getString(R.string.file_created, driveFile.getDriveId().encodeToString()))) .addOnFailureListener(this, e -> { Log.e(TAG, "Unable to create file", e); showMessage(getString(R.string.file_create_error)); }); }
Example #6
Source File: CreateEmptyFileActivity.java From android-samples with Apache License 2.0 | 6 votes |
private void createEmptyFile() { // [START drive_android_create_empty_file] getDriveResourceClient() .getRootFolder() .continueWithTask(task -> { DriveFolder parentFolder = task.getResult(); MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle("New file") .setMimeType("text/plain") .setStarred(true) .build(); return getDriveResourceClient().createFile(parentFolder, changeSet, null); }) .addOnSuccessListener(this, driveFile -> { showMessage(getString(R.string.file_created, driveFile.getDriveId().encodeToString())); finish(); }) .addOnFailureListener(this, e -> { Log.e(TAG, "Unable to create file", e); showMessage(getString(R.string.file_create_error)); finish(); }); // [END drive_android_create_empty_file] }
Example #7
Source File: QueryFilesInFolderActivity.java From android-samples with Apache License 2.0 | 6 votes |
/** * Retrieves results for the next page. For the first run, * it retrieves results for the first page. */ private void listFilesInFolder(DriveFolder folder) { Query query = new Query.Builder() .addFilter(Filters.eq(SearchableField.MIME_TYPE, "text/plain")) .build(); // [START drive_android_query_children] Task<MetadataBuffer> queryTask = getDriveResourceClient().queryChildren(folder, query); // END drive_android_query_children] queryTask .addOnSuccessListener(this, metadataBuffer -> mResultsAdapter.append(metadataBuffer)) .addOnFailureListener(this, e -> { Log.e(TAG, "Error retrieving files", e); showMessage(getString(R.string.query_failed)); finish(); }); }
Example #8
Source File: MainActivity.java From android-samples with Apache License 2.0 | 6 votes |
/** * Initializes the folder view after the given task completes. * * @return Task which resolves after the view has been initialized */ private Task<Void> initializeFolderView() { Task<DriveFolder> folderTask; if (mNavigationPath.isEmpty()) { folderTask = mDriveResourceClient.getRootFolder(); } else { folderTask = Tasks.forResult(mNavigationPath.peek().asDriveFolder()); } Task<Void> initFolderTask = folderTask.continueWith(task -> { DriveId id = task.getResult().getDriveId(); if (mNavigationPath.isEmpty()) { mNavigationPath.push(id); } return null; }); return updateUiAfterTask(initFolderTask); }
Example #9
Source File: GoogleDriveClient.java From financisto with GNU General Public License v2.0 | 6 votes |
public void uploadFile(File file) throws ImportExportException { try { String targetFolder = getDriveFolderName(); ConnectionResult connectionResult = connect(); if (connectionResult.isSuccess()) { DriveFolder folder = getDriveFolder(targetFolder); InputStream is = new FileInputStream(file); try { byte[] bytes = IOUtils.toByteArray(is); createFile(folder, file.getName(), bytes); } finally { IOUtil.closeInput(is); } } } catch (Exception e) { throw new ImportExportException(R.string.google_drive_connection_failed, e); } }
Example #10
Source File: GoogleDriveClient.java From financisto with GNU General Public License v2.0 | 6 votes |
private Status createFile(DriveFolder folder, String fileName, byte[] bytes) throws IOException { MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle(fileName) .setMimeType(Export.BACKUP_MIME_TYPE).build(); // Create a file in the root folder DriveApi.DriveContentsResult contentsResult = Drive.DriveApi.newDriveContents(googleApiClient).await(); Status contentsResultStatus = contentsResult.getStatus(); if (contentsResultStatus.isSuccess()) { DriveContents contents = contentsResult.getDriveContents(); contents.getOutputStream().write(bytes); DriveFolder.DriveFileResult fileResult = folder.createFile(googleApiClient, changeSet, contents).await(); return fileResult.getStatus(); } else { return contentsResultStatus; } }
Example #11
Source File: GoogleDriveClient.java From financisto with GNU General Public License v2.0 | 6 votes |
@Subscribe(threadMode = ThreadMode.BACKGROUND) public void doBackup(DoDriveBackup event) { DatabaseExport export = new DatabaseExport(context, db.db(), true); try { String targetFolder = getDriveFolderName(); ConnectionResult connectionResult = connect(); if (connectionResult.isSuccess()) { DriveFolder folder = getDriveFolder(targetFolder); String fileName = export.generateFilename(); byte[] bytes = export.generateBackupBytes(); Status status = createFile(folder, fileName, bytes); if (status.isSuccess()) { handleSuccess(fileName); } else { handleFailure(status); } } else { handleConnectionResult(connectionResult); } } catch (Exception e) { handleError(e); } }
Example #12
Source File: MainActivity.java From android-samples with Apache License 2.0 | 5 votes |
/** * Creates an empty file in the current folder. */ private void createFile() { setUiInteractionsEnabled(false); int fileCount = mFileFolderAdapter.getCount(); MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle("sample file " + (fileCount + 1)) .setMimeType("text/plain") .build(); DriveFolder driveFolder = mNavigationPath.peek().asDriveFolder(); Task<DriveFile> createFileTask = mDriveResourceClient.createFile(driveFolder, changeSet, null); Task<Void> updateTask = updateUiAfterTask(createFileTask); handleTaskError(updateTask, R.string.unexpected_error); }
Example #13
Source File: MainActivity.java From android-samples with Apache License 2.0 | 5 votes |
/** * Creates an empty folder in the current folder. */ private void createFolder() { setUiInteractionsEnabled(false); int fileCount = mFileFolderAdapter.getCount(); MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle("sample folder " + (fileCount + 1)) .build(); DriveFolder driveFolder = mNavigationPath.peek().asDriveFolder(); Task<DriveFolder> createFolderTask = mDriveResourceClient.createFolder(driveFolder, changeSet); Task<Void> updateTask = updateUiAfterTask(createFolderTask); handleTaskError(updateTask, R.string.unexpected_error); }
Example #14
Source File: MainActivity.java From android-samples with Apache License 2.0 | 5 votes |
private Task<DriveFile> createNewFile() { Log.d(TAG, "Creating new grocery list."); return getDriveResourceClient().getRootFolder().continueWithTask( task -> { DriveFolder folder = task.getResult(); MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle(getResources().getString( R.string.groceryListFileName)) .setMimeType("text/plain") .build(); return getDriveResourceClient().createFile(folder, changeSet, null); }); }
Example #15
Source File: CreateFileInAppFolderActivity.java From android-samples with Apache License 2.0 | 5 votes |
private void createFileInAppFolder() { final Task<DriveFolder> appFolderTask = getDriveResourceClient().getAppFolder(); final Task<DriveContents> createContentsTask = getDriveResourceClient().createContents(); Tasks.whenAll(appFolderTask, createContentsTask) .continueWithTask(task -> { DriveFolder parent = appFolderTask.getResult(); DriveContents contents = createContentsTask.getResult(); OutputStream outputStream = contents.getOutputStream(); try (Writer writer = new OutputStreamWriter(outputStream)) { writer.write("Hello World!"); } MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle("New file") .setMimeType("text/plain") .setStarred(true) .build(); return getDriveResourceClient().createFile(parent, changeSet, contents); }) .addOnSuccessListener(this, driveFile -> { showMessage(getString(R.string.file_created, driveFile.getDriveId().encodeToString())); finish(); }) .addOnFailureListener(this, e -> { Log.e(TAG, "Unable to create file", e); showMessage(getString(R.string.file_create_error)); finish(); }); }
Example #16
Source File: BaseDemoActivity.java From android-samples with Apache License 2.0 | 5 votes |
/** * Prompts the user to select a folder using OpenFileActivity. * * @return Task that resolves with the selected item's ID. */ protected Task<DriveId> pickFolder() { OpenFileActivityOptions openOptions = new OpenFileActivityOptions.Builder() .setSelectionFilter( Filters.eq(SearchableField.MIME_TYPE, DriveFolder.MIME_TYPE)) .setActivityTitle(getString(R.string.select_folder)) .build(); return pickItem(openOptions); }
Example #17
Source File: CreateFileActivity.java From android-samples with Apache License 2.0 | 5 votes |
private void createFile() { // [START drive_android_create_file] final Task<DriveFolder> rootFolderTask = getDriveResourceClient().getRootFolder(); final Task<DriveContents> createContentsTask = getDriveResourceClient().createContents(); Tasks.whenAll(rootFolderTask, createContentsTask) .continueWithTask(task -> { DriveFolder parent = rootFolderTask.getResult(); DriveContents contents = createContentsTask.getResult(); OutputStream outputStream = contents.getOutputStream(); try (Writer writer = new OutputStreamWriter(outputStream)) { writer.write("Hello World!"); } MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle("HelloWorld.txt") .setMimeType("text/plain") .setStarred(true) .build(); return getDriveResourceClient().createFile(parent, changeSet, contents); }) .addOnSuccessListener(this, driveFile -> { showMessage(getString(R.string.file_created, driveFile.getDriveId().encodeToString())); finish(); }) .addOnFailureListener(this, e -> { Log.e(TAG, "Unable to create file", e); showMessage(getString(R.string.file_create_error)); finish(); }); // [END drive_android_create_file] }
Example #18
Source File: MainActivity.java From android-samples with Apache License 2.0 | 5 votes |
private Task<Void> loadFolderContents(DriveFolder folder) { Query folderQuery = new Query.Builder() .setSortOrder(new SortOrder.Builder() .addSortAscending(SortableField.CREATED_DATE) .build()) .build(); Task<MetadataBuffer> task = mDriveResourceClient.queryChildren(folder, folderQuery); return task.continueWith(task1 -> { MetadataBuffer items = task1.getResult(); List<Metadata> children = new ArrayList<>(items.getCount()); try { for (int i = 0; i < items.getCount(); i++) { children.add(items.get(i).freeze()); } // Show folders first Collections.sort(children, new Comparator<Metadata>() { @Override public int compare(Metadata a, Metadata b) { int aVal = a.isFolder() ? 1 : 0; int bVal = b.isFolder() ? 1 : 0; return bVal - aVal; } }); mFileFolderAdapter.setFiles(children); mFileFolderAdapter.notifyDataSetChanged(); } finally { items.release(); } return null; }); }
Example #19
Source File: MainActivity.java From android-samples with Apache License 2.0 | 5 votes |
/** * Update the UI with the current folder name * * @param folder currently selected folder */ private Task<Void> loadFolderName(DriveFolder folder) { Log.i(TAG, "Fetching folder metadata for " + folder.getDriveId().encodeToString()); Task<Metadata> getMetadataTask = mDriveResourceClient.getMetadata(folder); return getMetadataTask.continueWith(task -> { mCurrentFolderNameTextView.setText(task.getResult().getTitle()); return null; }); }
Example #20
Source File: MainActivity.java From android-samples with Apache License 2.0 | 5 votes |
private <T> Task<Void> updateUiAfterTask(Task<T> task) { Task<Void> loadFolderTask = task.continueWithTask(task1 -> { DriveFolder currentFolder = mNavigationPath.peek().asDriveFolder(); return loadFolderNameAndContents(currentFolder); }); // Wait till Metadata is loaded to allow user interaction. loadFolderTask.addOnCompleteListener(task12 -> setUiInteractionsEnabled(true)); return loadFolderTask; }
Example #21
Source File: DriveFio.java From Drive-Database-Sync with Apache License 2.0 | 5 votes |
/** * Results handler for the asynchronous work methods in this class. Triages the Result and then * passes it up to the appropriate callback based on the type. * @param result the returned result. */ @Override public void onResult(Result result) { if (result instanceof DriveApi.DriveContentsResult) { callback.onFioResult((DriveApi.DriveContentsResult) result); } if (result instanceof DriveFolder.DriveFileResult) { callback.onFileCreatedResult((DriveFolder.DriveFileResult) result); } }
Example #22
Source File: DriveFio.java From Drive-Database-Sync with Apache License 2.0 | 5 votes |
/** * Work method to create a DriveFile within the passed params. Sets the result callback to this, * which calls {@link #onResult(Result)} * @param driveClient the {@link GoogleApiClient} to use for this Drive request * @param driveFolder the {@link DriveFolder} to create the DriveFile in * @param filename the filename for the to-be-created {@link DriveFile} */ public void createFile(GoogleApiClient driveClient, DriveFolder driveFolder, String filename) { MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() .setTitle(filename) .build(); driveFolder.createFile(driveClient, metadataChangeSet, null).setResultCallback(this); }
Example #23
Source File: DriveLayer.java From Drive-Database-Sync with Apache License 2.0 | 5 votes |
/** * {@link DriveFioResultsCallback} called when the DriveFile with the queried name is finished * being created. If the result was not a success, logs that. Regardless, attempts to move * forward with whatever request required that the DriveFile be created be attempting to load * a reference to the DriveFile * @param result the DriveFileResult indicating the exit status of the DriveFile creation * operation */ @Override public void onFileCreatedResult(DriveFolder.DriveFileResult result) { if (debug) { Log.d("DriveLayer", "File created"); } if (!result.getStatus().isSuccess()) { Log.e("DriveLayer", "Error creating file"); } new DriveFio(this).loadDriveFile(mDriveClient, result.getDriveFile(), callback.openModeWriteable()); }
Example #24
Source File: DriveLayer.java From Drive-Database-Sync with Apache License 2.0 | 5 votes |
/** * Helper method that retrieves the application's AppFolder * @return the DriveFolder representing the AppFolder */ private DriveFolder getAppFolder() { if (debug) { Log.d("DriveLayer", "Getting AppFolder"); } return Drive.DriveApi.getAppFolder(mDriveClient); }
Example #25
Source File: Querier.java From Drive-Database-Sync with Apache License 2.0 | 5 votes |
/** * Asynchronously queries Drive within the passed params to find a DriveFile * @param driveClient the {@link GoogleApiClient} to use for this Drive request * @param folder the {@link DriveFolder} to query within * @param filename the filename to query for */ public void findFile(GoogleApiClient driveClient, DriveFolder folder, String filename) { // Make filename filter Filter filenameFilter = Filters.eq(SearchableField.TITLE, filename); // Build query with filter Query query = new Query.Builder() .addFilter(filenameFilter) .build(); // Execute query on folder folder.queryChildren(driveClient, query).setResultCallback(this); }
Example #26
Source File: GoogleDriveService.java From WheelLogAndroid with GNU General Public License v3.0 | 5 votes |
@Override public void onResult(@NonNull DriveFolder.DriveFileResult result) { if (!result.getStatus().isSuccess()) { Timber.i("Error while trying to create the file"); exitUploadFailed(); return; } Timber.i("Created a file with content: %s", result.getDriveFile().getDriveId()); updateNotification("Upload Complete", true); stopSelf(); }
Example #27
Source File: GoogleDriveService.java From WheelLogAndroid with GNU General Public License v3.0 | 5 votes |
@Override public void onResult(@NonNull DriveFolder.DriveFolderResult result) { if (!result.getStatus().isSuccess()) { Timber.i("Error while trying to create the folder"); exitUploadFailed(); return; } Timber.i("Created a folder: %s", result.getDriveFolder().getDriveId()); folderDriveId = result.getDriveFolder().getDriveId(); createLogInFolder(); }
Example #28
Source File: GoogleDriveClient.java From financisto with GNU General Public License v2.0 | 5 votes |
private DriveFolder createDriveFolder(String targetFolder) { MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle(targetFolder).build(); DriveFolder.DriveFolderResult result = Drive.DriveApi.getRootFolder(googleApiClient).createFolder(googleApiClient, changeSet).await(); if (result.getStatus().isSuccess()) { return result.getDriveFolder(); } else { return null; } }
Example #29
Source File: GoogleDriveClient.java From financisto with GNU General Public License v2.0 | 5 votes |
private DriveFolder getOrCreateDriveFolder(String targetFolder) throws IOException { Query query = new Query.Builder().addFilter(Filters.and( Filters.eq(SearchableField.TRASHED, false), Filters.eq(SearchableField.TITLE, targetFolder), Filters.eq(SearchableField.MIME_TYPE, "application/vnd.google-apps.folder") )).build(); DriveApi.MetadataBufferResult result = Drive.DriveApi.query(googleApiClient, query).await(); if (result.getStatus().isSuccess()) { DriveId driveId = fetchDriveId(result); if (driveId != null) { return Drive.DriveApi.getFolder(googleApiClient, driveId); } } return createDriveFolder(targetFolder); }
Example #30
Source File: GoogleDriveClient.java From financisto with GNU General Public License v2.0 | 5 votes |
private DriveFolder getDriveFolder(String targetFolder) throws IOException, ImportExportException { DriveFolder folder = getOrCreateDriveFolder(targetFolder); if (folder == null) { throw new ImportExportException(R.string.gdocs_folder_not_found); } return folder; }