|
40 | 40 | import com.dropbox.sync.android.DbxFileSystem; |
41 | 41 | import com.dropbox.sync.android.DbxPath; |
42 | 42 | import com.google.android.gms.common.api.GoogleApiClient; |
43 | | -import com.google.android.gms.common.api.ResultCallback; |
44 | 43 | import com.google.android.gms.drive.Drive; |
45 | 44 | import com.google.android.gms.drive.DriveApi; |
46 | 45 | import com.google.android.gms.drive.DriveContents; |
|
80 | 79 | import java.util.ArrayList; |
81 | 80 | import java.util.Date; |
82 | 81 | import java.util.List; |
| 82 | +import java.util.concurrent.TimeUnit; |
83 | 83 |
|
84 | 84 | /** |
85 | 85 | * Asynchronous task for exporting transactions. |
@@ -276,62 +276,60 @@ protected void onPostExecute(Boolean exportResult) { |
276 | 276 |
|
277 | 277 | private void moveExportToGoogleDrive(){ |
278 | 278 | Log.i(TAG, "Moving exported file to Google Drive"); |
| 279 | + final long TIMEOUT = 5; // seconds |
279 | 280 | final GoogleApiClient googleApiClient = BackupPreferenceFragment.getGoogleApiClient(GnuCashApplication.getAppContext()); |
280 | 281 | googleApiClient.blockingConnect(); |
281 | | - final ResultCallback<DriveFolder.DriveFileResult> fileCallback = new |
282 | | - ResultCallback<DriveFolder.DriveFileResult>() { |
283 | | - @Override |
284 | | - public void onResult(DriveFolder.DriveFileResult result) { |
285 | | - if (!result.getStatus().isSuccess()) |
286 | | - Log.e(TAG, "Error while trying to sync to Google Drive"); |
287 | | - else |
288 | | - Log.i(TAG, "Created a file with content: " + result.getDriveFile().getDriveId()); |
289 | | - } |
290 | | - }; |
291 | | - |
292 | | - Drive.DriveApi.newDriveContents(googleApiClient).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { |
293 | | - @Override |
294 | | - public void onResult(DriveApi.DriveContentsResult result) { |
295 | | - if (!result.getStatus().isSuccess()) { |
296 | | - Log.e(TAG, "Error while trying to create new file contents"); |
297 | | - return; |
298 | | - } |
299 | | - final DriveContents driveContents = result.getDriveContents(); |
300 | | - try { |
301 | | - // write content to DriveContents |
302 | | - OutputStream outputStream = driveContents.getOutputStream(); |
303 | | - for (String exportedFilePath : mExportedFiles) { |
304 | | - File exportedFile = new File(exportedFilePath); |
305 | | - FileInputStream fileInputStream = new FileInputStream(exportedFile); |
306 | | - byte[] buffer = new byte[1024]; |
307 | | - int count; |
308 | | - |
309 | | - while ((count = fileInputStream.read(buffer)) >= 0) { |
310 | | - outputStream.write(buffer, 0, count); |
311 | | - } |
312 | | - fileInputStream.close(); |
313 | | - outputStream.flush(); |
314 | | - exportedFile.delete(); |
315 | | - |
316 | | - MetadataChangeSet changeSet = new MetadataChangeSet.Builder() |
317 | | - .setTitle(exportedFile.getName()) |
318 | | - .setMimeType(mExporter.getExportMimeType()) |
319 | | - .build(); |
320 | | - |
321 | | - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext); |
322 | | - String folderId = sharedPreferences.getString(mContext.getString(R.string.key_google_drive_app_folder_id), ""); |
323 | | - DriveFolder folder = Drive.DriveApi.getFolder(googleApiClient, DriveId.decodeFromString(folderId)); |
324 | | - // create a file on root folder |
325 | | - folder.createFile(googleApiClient, changeSet, driveContents) |
326 | | - .setResultCallback(fileCallback); |
327 | | - } |
328 | 282 |
|
329 | | - } catch (IOException e) { |
330 | | - Crashlytics.logException(e); |
331 | | - Log.e(TAG, e.getMessage()); |
| 283 | + DriveApi.DriveContentsResult driveContentsResult = |
| 284 | + Drive.DriveApi.newDriveContents(googleApiClient).await(TIMEOUT, TimeUnit.SECONDS); |
| 285 | + if (!driveContentsResult.getStatus().isSuccess()) { |
| 286 | + Log.e(TAG, "Error while trying to create new file contents"); |
| 287 | + return; |
| 288 | + } |
| 289 | + final DriveContents driveContents = driveContentsResult.getDriveContents(); |
| 290 | + DriveFolder.DriveFileResult driveFileResult = null; |
| 291 | + try { |
| 292 | + // write content to DriveContents |
| 293 | + OutputStream outputStream = driveContents.getOutputStream(); |
| 294 | + for (String exportedFilePath : mExportedFiles) { |
| 295 | + File exportedFile = new File(exportedFilePath); |
| 296 | + FileInputStream fileInputStream = new FileInputStream(exportedFile); |
| 297 | + byte[] buffer = new byte[1024]; |
| 298 | + int count; |
| 299 | + |
| 300 | + while ((count = fileInputStream.read(buffer)) >= 0) { |
| 301 | + outputStream.write(buffer, 0, count); |
332 | 302 | } |
| 303 | + fileInputStream.close(); |
| 304 | + outputStream.flush(); |
| 305 | + exportedFile.delete(); |
| 306 | + |
| 307 | + MetadataChangeSet changeSet = new MetadataChangeSet.Builder() |
| 308 | + .setTitle(exportedFile.getName()) |
| 309 | + .setMimeType(mExporter.getExportMimeType()) |
| 310 | + .build(); |
| 311 | + |
| 312 | + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext); |
| 313 | + String folderId = sharedPreferences.getString(mContext.getString(R.string.key_google_drive_app_folder_id), ""); |
| 314 | + DriveFolder folder = Drive.DriveApi.getFolder(googleApiClient, DriveId.decodeFromString(folderId)); |
| 315 | + // create a file on root folder |
| 316 | + driveFileResult = folder.createFile(googleApiClient, changeSet, driveContents) |
| 317 | + .await(TIMEOUT, TimeUnit.SECONDS); |
333 | 318 | } |
334 | | - }); |
| 319 | + } catch (IOException e) { |
| 320 | + Crashlytics.logException(e); |
| 321 | + Log.e(TAG, e.getMessage()); |
| 322 | + } |
| 323 | + |
| 324 | + if (driveFileResult == null) |
| 325 | + return; |
| 326 | + |
| 327 | + if (!driveFileResult.getStatus().isSuccess()) { |
| 328 | + Log.e(TAG, "Error creating file in Google Drive"); |
| 329 | + showToastFromNonUiThread("Couldn't create the file in Google Drive", Toast.LENGTH_LONG); |
| 330 | + } else { |
| 331 | + Log.i(TAG, "Created file with id: " + driveFileResult.getDriveFile().getDriveId()); |
| 332 | + } |
335 | 333 | } |
336 | 334 |
|
337 | 335 | private void moveExportToDropbox() { |
@@ -536,4 +534,14 @@ public void moveFile(String src, String dst) throws IOException { |
536 | 534 | srcFile.delete(); |
537 | 535 | } |
538 | 536 |
|
| 537 | + private void showToastFromNonUiThread(final String message, final int duration) { |
| 538 | + if (mContext instanceof Activity) { |
| 539 | + ((Activity) mContext).runOnUiThread(new Runnable() { |
| 540 | + @Override |
| 541 | + public void run() { |
| 542 | + Toast.makeText(mContext, message, duration).show(); |
| 543 | + } |
| 544 | + }); |
| 545 | + } |
| 546 | + } |
539 | 547 | } |
0 commit comments