Skip to content

Instantly share code, notes, and snippets.

@Jthomas54
Created November 17, 2015 03:26
Show Gist options
  • Save Jthomas54/749beb2725220dd9de7f to your computer and use it in GitHub Desktop.
Save Jthomas54/749beb2725220dd9de7f to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.Metadata;
import com.google.android.gms.drive.MetadataChangeSet;
import com.google.android.gms.drive.query.Filters;
import com.google.android.gms.drive.query.Query;
import com.google.android.gms.drive.query.SearchableField;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
/**
* Utility class to hide the complexities of reading a google drive file and writing it to the local device
* <p>
* Requires a GoogleApiClient with an added scope of Drive.SCOPE_FILE to work
* </p>
*/
public final class DriveUtil {
private static final String TAG = DriveUtil.class.getSimpleName();
private static final String USER_DATA_FILE_NAME = "google-drive-file-name.ext";
private static final String LOCAL_FILE_PATH = "file-to-write-to.ext";
private static final MetadataChangeSet CHANGE_SET = new MetadataChangeSet.Builder()
.setMimeType("application/json")
.setTitle(USER_DATA_FILE_NAME)
.build();
private static final Query USER_DATA_FILE_QUERY = new Query.Builder()
.addFilter(Filters.eq(SearchableField.TITLE, USER_DATA_FILE_NAME))
.build();
public static void exportUserDataToGoogleDrive(final Activity activity, final GoogleApiClient client, final DriveCompletionListener listener) {
if (client == null) {
listener.onOperationComplete(false);
return;
}
Drive.DriveApi.query(client, USER_DATA_FILE_QUERY).setResultCallback(
new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
if (metadataBufferResult.getStatus().isSuccess()) {
Iterator<Metadata> iterator = metadataBufferResult.getMetadataBuffer().iterator();
if (iterator.hasNext()) {
Metadata metadata = iterator.next();
metadata
.getDriveId()
.asDriveFile()
.open(client, DriveFile.MODE_WRITE_ONLY, null)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
if (!driveContentsResult.getStatus().isSuccess()) {
listener.onOperationComplete(false);
return;
}
writeUserDataToDriveContents(activity, driveContentsResult.getDriveContents());
driveContentsResult.getDriveContents().commit(client, CHANGE_SET);
listener.onOperationComplete(true);
}
});
metadataBufferResult.getMetadataBuffer().release();
} else {
Drive.DriveApi.newDriveContents(client).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
if (!driveContentsResult.getStatus().isSuccess()) {
listener.onOperationComplete(false);
return;
}
DriveContents dc = driveContentsResult.getDriveContents();
writeUserDataToDriveContents(activity, dc);
Drive.DriveApi.getRootFolder(client).createFile(client, CHANGE_SET, dc);
listener.onOperationComplete(true);
}
});
}
}else{
listener.onOperationComplete(false);
}
}
}
);
}
public static void importUserDateFromGoogleDrive(final Activity activity, final GoogleApiClient client, final DriveCompletionListener listener) {
if (client == null) {
listener.onOperationComplete(false);
return;
}
Drive.DriveApi.query(client, USER_DATA_FILE_QUERY).setResultCallback(
new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
if (!metadataBufferResult.getStatus().isSuccess()) {
listener.onOperationComplete(false);
return;
}
Iterator<Metadata> iterator = metadataBufferResult.getMetadataBuffer().iterator();
if (iterator.hasNext()) {
Metadata metadata = iterator.next();
metadata
.getDriveId()
.asDriveFile()
.open(client, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
if (!driveContentsResult.getStatus().isSuccess()) {
listener.onOperationComplete(false);
return;
}
saveUserDataFromDriveContents(activity, driveContentsResult.getDriveContents());
driveContentsResult.getDriveContents().discard(client);
listener.onOperationComplete(true);
}
});
} else {
listener.onOperationComplete(true);
}
metadataBufferResult.getMetadataBuffer().release();
}
});
}
private static void writeUserDataToDriveContents(Activity activity, DriveContents contents) {
try {
File f = activity.getFileStreamPath(LOCAL_FILE_PATH);
FileInputStream inStream = new FileInputStream(f);
FileOutputStream outStream = new FileOutputStream(contents.getParcelFileDescriptor().getFileDescriptor());
byte[] buffer = new byte[(int) f.length()];
inStream.read(buffer, 0, (int) f.length());
outStream.write(buffer);
inStream.close();
} catch (IOException ex) {
Log.e(TAG, ex.getMessage());
}
}
private static void saveUserDataFromDriveContents(Activity activity, DriveContents contents) {
try {
InputStream inStream = contents.getInputStream();
FileOutputStream outStream = activity.openFileOutput(LOCAL_FILE_PATH, Context.MODE_PRIVATE);
int length;
byte[] buffer = new byte[1024];
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
outStream.close();
} catch (IOException ex) {
Log.e(TAG, ex.getMessage());
}
}
public interface DriveCompletionListener {
void onOperationComplete(boolean success);
}
}
@Jthomas54
Copy link
Author

This can easily be improved upon, but you get the idea and hey, you don't have to spend several days figuring this silly thing out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment