Created
August 15, 2016 16:48
-
-
Save yccheok/2676fc3383116945ca1fac3bbf5a3b1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static boolean saveToGoogleDrive(GoogleApiClient googleApiClient, File file, HandleStatusable h, PublishProgressable p) { | |
// Should we new or replace? | |
GoogleCloudFile googleCloudFile = searchFromGoogleDrive(googleApiClient, h, p); | |
try { | |
p.publishProgress(JStockApplication.instance().getString(R.string.uploading)); | |
final long checksum = org.yccheok.jstock.gui.Utils.getChecksum(file); | |
final long date = new Date().getTime(); | |
final int version = org.yccheok.jstock.gui.Utils.getCloudFileVersionID(); | |
final String title = getGoogleDriveTitle(checksum, date, version); | |
DriveContents driveContents; | |
DriveFile driveFile = null; | |
if (googleCloudFile == null) { | |
DriveApi.DriveContentsResult driveContentsResult = Drive.DriveApi.newDriveContents(googleApiClient).await(); | |
if (driveContentsResult == null) { | |
return false; | |
} | |
Status status = driveContentsResult.getStatus(); | |
if (!status.isSuccess()) { | |
h.handleStatus(status); | |
return false; | |
} | |
driveContents = driveContentsResult.getDriveContents(); | |
} else { | |
driveFile = googleCloudFile.metadata.getDriveId().asDriveFile(); | |
DriveApi.DriveContentsResult driveContentsResult = driveFile.open(googleApiClient, DriveFile.MODE_WRITE_ONLY, null).await(); | |
if (driveContentsResult == null) { | |
return false; | |
} | |
Status status = driveContentsResult.getStatus(); | |
if (!status.isSuccess()) { | |
h.handleStatus(status); | |
return false; | |
} | |
driveContents = driveContentsResult.getDriveContents(); | |
} | |
OutputStream outputStream = driveContents.getOutputStream(); | |
InputStream inputStream = null; | |
byte[] buf = new byte[8192]; | |
try { | |
inputStream = new FileInputStream(file); | |
int c; | |
while ((c = inputStream.read(buf, 0, buf.length)) > 0) { | |
outputStream.write(buf, 0, c); | |
} | |
} catch (IOException e) { | |
Log.e(TAG, "", e); | |
return false; | |
} finally { | |
org.yccheok.jstock.file.Utils.close(outputStream); | |
org.yccheok.jstock.file.Utils.close(inputStream); | |
} | |
final MetadataChangeSet metadataChangeSet; | |
if (googleCloudFile == null) { | |
// Create the metadata for the new file including title and MIME | |
// type. | |
metadataChangeSet = new MetadataChangeSet.Builder() | |
.setTitle(title) | |
.setMimeType("application/zip").build(); | |
DriveFolder driveFolder = Drive.DriveApi.getAppFolder(googleApiClient); | |
DriveFolder.DriveFileResult driveFileResult = driveFolder.createFile(googleApiClient, metadataChangeSet, driveContents).await(); | |
if (driveFileResult == null) { | |
return false; | |
} | |
Status status = driveFileResult.getStatus(); | |
if (!status.isSuccess()) { | |
h.handleStatus(status); | |
return false; | |
} | |
} else { | |
metadataChangeSet = new MetadataChangeSet.Builder() | |
.setTitle(title).build(); | |
DriveResource.MetadataResult metadataResult = driveFile.updateMetadata(googleApiClient, metadataChangeSet).await(); | |
Status status = metadataResult.getStatus(); | |
if (!status.isSuccess()) { | |
h.handleStatus(status); | |
return false; | |
} | |
} | |
Status status; | |
try { | |
status = driveContents.commit(googleApiClient, metadataChangeSet).await(); | |
} catch (java.lang.IllegalStateException e) { | |
// java.lang.IllegalStateException: DriveContents already closed. | |
Log.e(TAG, "", e); | |
return false; | |
} | |
if (!status.isSuccess()) { | |
h.handleStatus(status); | |
return false; | |
} | |
status = Drive.DriveApi.requestSync(googleApiClient).await(); | |
if (!status.isSuccess()) { | |
// Sync request rate limit exceeded. | |
// | |
//h.handleStatus(status); | |
//return false; | |
} | |
return true; | |
} finally { | |
if (googleCloudFile != null) { | |
googleCloudFile.metadataBuffer.release(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment