com.google.android.gms.drive.Metadata Java Examples

The following examples show how to use com.google.android.gms.drive.Metadata. 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: MainActivity.java    From android-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Trashes or untrashes the given item.
 *
 * @param metadata Item to (un)trash
 */
private void toggleTrashStatus(Metadata metadata) {
    // [START drive_android_trash]
    if (!metadata.isTrashable()) {
        showMessage(R.string.trashable_error);
        return;
    }
    DriveResource driveResource = metadata.getDriveId().asDriveResource();
    Task<Void> toggleTrashTask;
    if (metadata.isTrashed()) {
        toggleTrashTask = mDriveResourceClient.untrash(driveResource);
    } else {
        toggleTrashTask = mDriveResourceClient.trash(driveResource);
    }
    toggleTrashTask = updateUiAfterTask(toggleTrashTask);
    handleTaskError(toggleTrashTask, R.string.unexpected_error);
    // [END drive_android_trash]
}
 
Example #2
Source File: RetrieveMetadataActivity.java    From android-samples with Apache License 2.0 6 votes vote down vote up
private void retrieveMetadata(final DriveFile file) {
    // [START drive_android_retrieve_metadata]
    Task<Metadata> getMetadataTask = getDriveResourceClient().getMetadata(file);
    getMetadataTask
            .addOnSuccessListener(this,
                    metadata -> {
                        showMessage(getString(
                                R.string.metadata_retrieved, metadata.getTitle()));
                        finish();
                    })
            .addOnFailureListener(this, e -> {
                Log.e(TAG, "Unable to retrieve metadata", e);
                showMessage(getString(R.string.read_failed));
                finish();
            });
    // [END drive_android_retrieve_metadata]
}
 
Example #3
Source File: GoogleDriveClient.java    From financisto with GNU General Public License v2.0 6 votes vote down vote up
private List<DriveFileInfo> fetchFiles(DriveApi.MetadataBufferResult metadataBufferResult) {
    List<DriveFileInfo> files = new ArrayList<DriveFileInfo>();
    MetadataBuffer metadataBuffer = metadataBufferResult.getMetadataBuffer();
    if (metadataBuffer == null) return files;
    try {
        for (Metadata metadata : metadataBuffer) {
            if (metadata == null) continue;
            String title = metadata.getTitle();
            if (!title.endsWith(".backup")) continue;
            files.add(new DriveFileInfo(metadata.getDriveId(), title, metadata.getCreatedDate()));
        }
    } finally {
        metadataBuffer.close();
    }
    Collections.sort(files);
    return files;
}
 
Example #4
Source File: Querier.java    From Drive-Database-Sync with Apache License 2.0 6 votes vote down vote up
/**
 * Handles the result of the query. If the results are nonzero, passes up the {@link Metadata}
 * out of the result one at a time through {@link QuerierResultCallback#onQuerierResult(Metadata)}
 * with the {@link QuerierResultCallback} registered in ths {@link Querier}. If the results are
 * null, calls {@link QuerierResultCallback#onNoQuerierResult()}
 * @param result the result of the query
 */
@Override
public void onResult(DriveApi.MetadataBufferResult result) {
    if (!result.getStatus().isSuccess()) {
        Log.e("Querier", "Problem while retrieving files");
        return;
    }

    // Get the metadata buffer
    MetadataBuffer buffer = result.getMetadataBuffer();

    // Check for file dne
    if (buffer.getCount() == 0) {
        // Call null result callback
        callback.onNoQuerierResult();
    }

    // Get the metadata
    for (Metadata m : buffer) {
        if (m.isInAppFolder() && !m.isTrashed()) {
            callback.onQuerierResult(m);
            break;
        }
    }
}
 
Example #5
Source File: EditMetadataActivity.java    From android-samples with Apache License 2.0 6 votes vote down vote up
private void editMetadata(DriveFile file) {
    // [START drive_android_update_metadata]
    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                          .setStarred(true)
                                          .setIndexableText("Description about the file")
                                          .setTitle("A new title")
                                          .build();
    Task<Metadata> updateMetadataTask =
            getDriveResourceClient().updateMetadata(file, changeSet);
    updateMetadataTask
            .addOnSuccessListener(this,
                    metadata -> {
                        showMessage(getString(R.string.metadata_updated));
                        finish();
                    })
            .addOnFailureListener(this, e -> {
                Log.e(TAG, "Unable to update metadata", e);
                showMessage(getString(R.string.update_failed));
                finish();
            });
    // [END drive_android_update_metadata]
}
 
Example #6
Source File: DeleteCustomPropertyActivity.java    From android-samples with Apache License 2.0 6 votes vote down vote up
private void deleteCustomProperty(DriveFile file) {
    // [START drive_android_delete_custom_property]
    CustomPropertyKey approvalPropertyKey =
            new CustomPropertyKey("approved", CustomPropertyKey.PUBLIC);
    CustomPropertyKey submitPropertyKey =
            new CustomPropertyKey("submitted", CustomPropertyKey.PUBLIC);
    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                          .deleteCustomProperty(approvalPropertyKey)
                                          .deleteCustomProperty(submitPropertyKey)
                                          .build();
    Task<Metadata> updateMetadataTask =
            getDriveResourceClient().updateMetadata(file, changeSet);
    updateMetadataTask
            .addOnSuccessListener(this,
                    metadata -> {
                        showMessage(getString(R.string.custom_property_deleted));
                        finish();
                    })
            .addOnFailureListener(this, e -> {
                Log.e(TAG, "Unable to update metadata", e);
                showMessage(getString(R.string.update_failed));
                finish();
            });
    // [END drive_android_delete_custom_property]
}
 
Example #7
Source File: DriveSyncController.java    From Drive-Database-Sync with Apache License 2.0 6 votes vote down vote up
/**
 * Handles received DriveFile Metadata for comparing the newer status of the local and Drive
 * copies of the Database
 * @param m the received Metadata
 */
@Override
public void onMetaDataReceived(Metadata m) {
    if (mRequestQueue.size() > 0) {

        if (mRequestQueue.peek() == COMPARE) {
            mRequestQueue.poll();

            if (compareDriveLocalNewer(m.getModifiedDate())) {
                newerStatusCallback.driveNewer();
            } else {
                newerStatusCallback.localNewer();
            }
        }
    } else {
        Log.e("Controller", "Queue size 0?...");
    }
}
 
Example #8
Source File: InsertUpdateCustomPropertyActivity.java    From android-samples with Apache License 2.0 6 votes vote down vote up
private void updateCustomProperty(DriveFile file) {
    // [START drive_android_update_custom_property]
    CustomPropertyKey approvalPropertyKey =
            new CustomPropertyKey("approved", CustomPropertyKey.PUBLIC);
    CustomPropertyKey submitPropertyKey =
            new CustomPropertyKey("submitted", CustomPropertyKey.PUBLIC);
    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                          .setCustomProperty(approvalPropertyKey, "yes")
                                          .setCustomProperty(submitPropertyKey, "no")
                                          .build();
    Task<Metadata> updateMetadataTask =
            getDriveResourceClient().updateMetadata(file, changeSet);
    updateMetadataTask
            .addOnSuccessListener(this,
                    metadata -> {
                        showMessage(getString(R.string.custom_property_updated));
                        finish();
                    })
            .addOnFailureListener(this, e -> {
                Log.e(TAG, "Unable to update metadata", e);
                showMessage(getString(R.string.update_failed));
                finish();
            });
    // [END drive_android_update_custom_property]
}
 
Example #9
Source File: SettingsBackup.java    From Slide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onResult(DriveApi.MetadataBufferResult result) {

    int i = 0;
    for (Metadata a : result.getMetadataBuffer()) {
        i++;
        title = a.getTitle();
        new RetrieveDriveFileContentsAsyncTask(title).execute(a.getDriveId());


    }
    progress = new MaterialDialog.Builder(SettingsBackup.this)
            .cancelable(false)
            .title(R.string.backup_restoring)
            .progress(false, i)
            .build();
    progress.show();


}
 
Example #10
Source File: FileManager.java    From amiibo with GNU General Public License v2.0 6 votes vote down vote up
@DebugLog
public void readFileContent(final String file_name, final List<Metadata> remainings) {
    Query query = new Query.Builder()
            .addFilter(Filters.eq(SearchableField.TITLE, file_name))
            .build();

    app_folder_for_user.queryChildren(_parent.getClient(), query)
            .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
                @Override
                public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
                    MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
                    int count = buffer.getCount();
                    if (count > 0) {
                        readFileFromDrive(buffer, buffer.get(0).getDriveId());
                    } else {
                        _parent.onFileRead(null);
                    }

                    readFileContent(remainings);
                }
            });
}
 
Example #11
Source File: FileManager.java    From amiibo with GNU General Public License v2.0 5 votes vote down vote up
@DebugLog
public void readFileContent(List<Metadata> remainings) {
    if (remainings.size() > 0) {
        Metadata metadata = remainings.get(0);
        remainings.remove(0);
        readFileContent(metadata.getTitle(), remainings);
    } else {
        _parent.onReadFinished();
    }
}
 
Example #12
Source File: ResultsAdapter.java    From android-samples with Apache License 2.0 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = View.inflate(getContext(), android.R.layout.simple_list_item_1, null);
    }
    Metadata metadata = getItem(position);
    TextView titleTextView = convertView.findViewById(android.R.id.text1);
    titleTextView.setText(metadata.getTitle());
    return convertView;
}
 
Example #13
Source File: PinFileActivity.java    From android-samples with Apache License 2.0 5 votes vote down vote up
private void pinFile(final DriveFile file) {
    // [START drive_android_pin_file]
    Task<Metadata> pinFileTask = getDriveResourceClient().getMetadata(file).continueWithTask(
            task -> {
                Metadata metadata = task.getResult();
                if (!metadata.isPinnable()) {
                    showMessage(getString(R.string.file_not_pinnable));
                    return Tasks.forResult(metadata);
                }
                if (metadata.isPinned()) {
                    showMessage(getString(R.string.file_already_pinned));
                    return Tasks.forResult(metadata);
                }
                MetadataChangeSet changeSet =
                        new MetadataChangeSet.Builder().setPinned(true).build();
                return getDriveResourceClient().updateMetadata(file, changeSet);
            });
    // [END drive_android_pin_file]
    // [START drive_android_pin_file_completion]
    pinFileTask
            .addOnSuccessListener(this,
                    metadata -> {
                        showMessage(getString(R.string.metadata_updated));
                        finish();
                    })
            .addOnFailureListener(this, e -> {
                Log.e(TAG, "Unable to update metadata", e);
                showMessage(getString(R.string.update_failed));
                finish();
            });
    // [END drive_android_pin_file_completion]
}
 
Example #14
Source File: MainActivity.java    From android-samples with Apache License 2.0 5 votes vote down vote up
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 #15
Source File: MainActivity.java    From android-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #16
Source File: FileManager.java    From amiibo with GNU General Public License v2.0 5 votes vote down vote up
@DebugLog
private void getAmiiboFolder() {
    Drive.DriveApi.getRootFolder(_parent.getClient())
            .listChildren(_parent.getClient())
            .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
                @Override
                public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
                    boolean found = false;
                    try {
                        List<Metadata> files = new ArrayList<>();
                        MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
                        Metadata data;

                        for (int i = 0; i < buffer.getCount(); i++) {
                            data = buffer.get(i);
                            if (data.getTitle().equals(AMIIBO_FOLDER) && data.isFolder()
                                    && !data.isTrashed()) {
                                app_folder_for_user = Drive.DriveApi
                                        .getFolder(_parent.getClient(), data.getDriveId());
                                Log.d("FileManager", data.getDriveId() + " " + data.getTitle());
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    if (app_folder_for_user != null) {
                        listFiles();
                    } else {
                        createAmiiboFolder();
                    }
                }
            });
}
 
Example #17
Source File: FileManager.java    From amiibo with GNU General Public License v2.0 5 votes vote down vote up
@DebugLog
public void listFiles() {

    if (app_folder_for_user != null) {

        Log.d("FileManager", app_folder_for_user.getDriveId().toString());

        app_folder_for_user.listChildren(_parent.getClient())
                .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
                    @Override
                    public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
                        List<Metadata> files = new ArrayList<>();
                        MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
                        Metadata data;
                        Log.d("FileManager", "onResult " + buffer.getCount() + " " + metadataBufferResult.getStatus().isSuccess());

                        for (int i = 0; i < buffer.getCount(); i++) {
                            data = buffer.get(i);
                            Log.d("FileManager", "data :: " + data.getTitle());
                            if (data.getTitle().endsWith(EXTENSION)) {
                                if (!hasFileLocally(data.getTitle())) {
                                    files.add(data);
                                }
                            }
                        }

                        _parent.onListFilesResult(files);
                    }
                });
    } else {
        getAmiiboFolder();
    }
}
 
Example #18
Source File: TGDriveBrowser.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void listElements(final TGBrowserCallBack<List<TGBrowserElement>> cb) {
	try {
		if( this.folder != null ) {
			this.folder.getFolder().listChildren(this.client).setResultCallback(new ResultCallback<MetadataBufferResult>() {
				public void onResult(MetadataBufferResult result) {
		            if( result.getStatus().isSuccess() ) {
		            	List<TGBrowserElement> elements = new ArrayList<TGBrowserElement>();
		            	
		            	Iterator<Metadata> it = result.getMetadataBuffer().iterator();
		            	while(it.hasNext()) {
		            		Metadata metadata = it.next();
		            		DriveId driveId = metadata.getDriveId();
		            		String name = metadata.getTitle();
		            		
		            		if(!metadata.isTrashed() && !metadata.isExplicitlyTrashed()) {
			            		if( metadata.isFolder() ) {
			            			elements.add(new TGDriveBrowserFolder(TGDriveBrowser.this.folder, Drive.DriveApi.getFolder(TGDriveBrowser.this.client, driveId), name));
			            		} else {
			            			elements.add(new TGDriveBrowserFile(TGDriveBrowser.this.folder, Drive.DriveApi.getFile(TGDriveBrowser.this.client, driveId), name));
			            		}
		            		}
		            	}
		            	
						if( !elements.isEmpty() ){
							Collections.sort(elements, new TGBrowserElementComparator());
						}
						
		            	cb.onSuccess(elements);
		            } else {
		            	cb.handleError(new TGBrowserException(findActivity().getString(R.string.gdrive_list_children_error)));
		            }
				};
			});
		} else {
			cb.onSuccess(new ArrayList<TGBrowserElement>());
		}
	} catch (Throwable e) {
		cb.handleError(e);
	}
}
 
Example #19
Source File: DriveLayer.java    From Drive-Database-Sync with Apache License 2.0 5 votes vote down vote up
/**
 * {@link QuerierResultCallback} called when a DriveFile with the queried filename is found
 * @param m the {@link Metadata} for the found DriveFile
 */
@Override
public void onQuerierResult(Metadata m) {
    if (debug) {
        Log.d("DriveLayer", "Got Query results");
    }

    callback.onMetaDataReceived(m);

    DriveFio driveFio = new DriveFio(this);
    DriveFile driveFile = driveFio.getDriveFileFromMetadata(mDriveClient, m);
    driveFio.loadDriveFile(mDriveClient, driveFile, callback.openModeWriteable());
}
 
Example #20
Source File: GoogleDriveService.java    From WheelLogAndroid with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onResult(@NonNull DriveApi.MetadataBufferResult result) {
    if (!result.getStatus().isSuccess()) {
        Timber.i("Problem while retrieving files");
        exitUploadFailed();
        return;
    }
    for (Metadata m: result.getMetadataBuffer()) {
        if (m.isFolder() &&
                !m.isTrashed() &&
                Constants.LOG_FOLDER_NAME.equals(m.getTitle())) {
            folderDriveId = m.getDriveId();
            break;
        }
    }
    result.release();

    Timber.i("Successfully listed files.");

    if (folderDriveId == null) {
        Timber.i("Folder DriveId is null");
        createLogFolderInDriveRoot();
    } else {
        Timber.i("Folder DriveId is not null");
        createLogInFolder();
    }
}
 
Example #21
Source File: GoogleDriveClient.java    From financisto with GNU General Public License v2.0 5 votes vote down vote up
private DriveId fetchDriveId(DriveApi.MetadataBufferResult result) {
    MetadataBuffer buffer = result.getMetadataBuffer();
    try {
        for (Metadata metadata : buffer) {
            if (metadata == null) continue;
            return metadata.getDriveId();
        }
    } finally {
        buffer.close();
    }
    return null;
}
 
Example #22
Source File: FileFolderAdapter.java    From android-samples with Apache License 2.0 4 votes vote down vote up
void setFiles(List<Metadata> fileMetadata) {
    this.fileMetadata = fileMetadata;
}
 
Example #23
Source File: FileFolderAdapter.java    From android-samples with Apache License 2.0 4 votes vote down vote up
FileFolderAdapter(Context context, int resource, List<Metadata> fileMetadata) {
    super(context, resource);
    this.fileMetadata = fileMetadata;
    enabled = true;
}
 
Example #24
Source File: FileFolderAdapter.java    From android-samples with Apache License 2.0 4 votes vote down vote up
@Override
public Metadata getItem(int position) {
    return fileMetadata.get(position);
}
 
Example #25
Source File: DriveSyncService.java    From Passbook with Apache License 2.0 4 votes vote down vote up
@Override
public void read() {
    if (mDriveResourceClient == null) {
        mListener.onSyncFailed(CA.CONNECTION);
    }
    final Task<DriveFolder> appFolderTask = mDriveResourceClient.getAppFolder();
    final Query query = new Query.Builder().build();
    appFolderTask.continueWithTask(t -> mDriveResourceClient.queryChildren(t.getResult(), query))
            .continueWithTask(t -> {
                Task<DriveContents> contentsTask = null;
                for (Metadata metadata : t.getResult()) {
                    if (SAVED_DATA.equalsIgnoreCase(metadata.getTitle())) {
                        mDriveId = metadata.getDriveId();
                        contentsTask= mDriveResourceClient.openFile(mDriveId.asDriveFile(), DriveFile.MODE_READ_ONLY);
                        break;
                    }
                }
                if (contentsTask == null) {
                    return mDriveResourceClient.createContents();
                }
                return contentsTask;
            })
            .addOnSuccessListener(c -> {
                try {
                    InputStream fis = c.getInputStream();
                    if (fis.available() < 1) {
                        throw new IOException();
                    }
                    mData = new byte[fis.available()];
                    int totalBytesRead = fis.read(mData);
                    fis.close();
                    if (totalBytesRead < mData.length) {
                        throw new IOException();
                    }
                    mListener.onSyncProgress(CA.DATA_RECEIVED);

                } catch (IllegalStateException | IOException e) {
                    Log.e(LOG_TAG, "read: ", e.getCause());
                    mListener.onSyncFailed(CA.NO_DATA);
                }
            })
            .addOnFailureListener(f -> mListener.onSyncFailed(CA.NO_DATA));
}
 
Example #26
Source File: DriveFio.java    From Drive-Database-Sync with Apache License 2.0 3 votes vote down vote up
/**
 * Work method that retrieves a DriveFile from some passed Metadata.
 * @param driveClient the {@link GoogleApiClient} to use for this Drive request
 * @param metadata the Metadata to pull the {@link DriveFile} out of
 * @return the {@link DriveFile} extracted from the passed Metadata
 */
public DriveFile getDriveFileFromMetadata(GoogleApiClient driveClient, Metadata metadata) {

    DriveId driveId = metadata.getDriveId();

    return Drive.DriveApi.getFile(driveClient, driveId);
}
 
Example #27
Source File: SyncService.java    From amiibo with GNU General Public License v2.0 2 votes vote down vote up
/**
 * The list of files retrieved
 *
 * @param files the list of files metadata, not null
 */
@DebugLog
public void onListFilesResult(List<Metadata> files) {
    _file_manager.readFileContent(files);
}
 
Example #28
Source File: QuerierResultCallback.java    From Drive-Database-Sync with Apache License 2.0 2 votes vote down vote up
/**
 * Method called when the query returns results
 * @param m the {@link Metadata} of the result
 */
void onQuerierResult(Metadata m);
 
Example #29
Source File: FileResultsReadyCallback.java    From Drive-Database-Sync with Apache License 2.0 2 votes vote down vote up
/**
 * Method called when DriveFile Metadata has been retrieved
 * @param m the DriveFile Metadata retrieved
 */
void onMetaDataReceived(Metadata m);