public void loadForm( final GoogleApiClient googleClient, final Activity activity, final String formName) { if (loadFormFromLocalStorage(activity, formName)) { return; } Query.Builder queryBuilder = new Query.Builder(); Log.i( MainActivity.PT_APP_INFO, "Load form from cloud. Google client state " + googleClient.isConnected()); queryBuilder.addFilter(Filters.eq(SearchableField.TITLE, formName)); Query query = queryBuilder.build(); final DriveFolder driveFolder = Drive.DriveApi.getFolder(googleClient, storageDriveId); Toast.makeText(activity, "Loading " + formName + " from google drive... ", Toast.LENGTH_LONG) .show(); driveFolder .queryChildren(googleClient, query) .setResultCallback( new ResultCallback<DriveApi.MetadataBufferResult>() { @Override public void onResult(DriveApi.MetadataBufferResult resultBufferResult) { if (resultBufferResult.getMetadataBuffer().getCount() > 0) { DriveFile driveFile = Drive.DriveApi.getFile( googleClient, resultBufferResult.getMetadataBuffer().get(0).getDriveId()); driveFile .open(googleClient, DriveFile.MODE_READ_ONLY, null) .setResultCallback( new ResultCallback<DriveApi.DriveContentsResult>() { @Override public void onResult(DriveApi.DriveContentsResult driveContentsResult) { try { InputStream is = driveContentsResult.getDriveContents().getInputStream(); FormTemplateManager.loadForm(is); LocalDriver.save( FormTemplateManager.getFormTemplate(), GoogleFileType.FORM, formName); goBackToMain(activity); } catch (Throwable th) { String errorStr = "Unable to retrieve form " + formName + " because " + th + " Retrieving from local drive"; Toast.makeText(activity, errorStr, Toast.LENGTH_LONG).show(); Log.e(MainActivity.PT_APP_INFO, errorStr); } } }); } } }); }
public void saveOrUpdate( final FormTemplate formToSave, final GoogleApiClient googleClient, final GoogleFileType googleFileType) throws GoogleDriverException { LocalDriver.save(formToSave, googleFileType, null); if (storageDriveId == null) { throw new GoogleDriverException("Cannot found form directory on google drive"); } final DriveId dirDriveId; if (googleFileType == GoogleFileType.REPORT) { dirDriveId = reportDriveId; } else { dirDriveId = storageDriveId; } final String fileName = PhysicalTherapyUtils.getFileName(formToSave); Query.Builder queryBuilder = new Query.Builder(); queryBuilder.addFilter(Filters.eq(SearchableField.TITLE, fileName)); Query query = queryBuilder.build(); try { final DriveFolder driveFolder = Drive.DriveApi.getFolder(googleClient, dirDriveId); driveFolder .queryChildren(googleClient, query) .setResultCallback( new ResultCallback<DriveApi.MetadataBufferResult>() { @Override public void onResult(DriveApi.MetadataBufferResult resultBufferResult) { DriveId fileDriveId = null; for (Metadata result : resultBufferResult.getMetadataBuffer()) { fileDriveId = result.getDriveId(); } if (fileDriveId == null) { save(formToSave, googleClient, dirDriveId, fileName); } else { update(formToSave, googleClient, fileDriveId, fileName); } } }); } catch (Throwable th) { throw new GoogleDriverException("Unable to find or save file to drive because " + th, th); } }
private void setStorageDirectory(final GoogleApiClient googleClient) throws GoogleDriverException { Query.Builder queryBuilder = new Query.Builder(); queryBuilder.addFilter(Filters.eq(SearchableField.TITLE, STORAGE_DIR)); Query query = queryBuilder.build(); try { final DriveFolder driveFolder = Drive.DriveApi.getRootFolder(googleClient); driveFolder .queryChildren(googleClient, query) .setResultCallback( new ResultCallback<DriveApi.MetadataBufferResult>() { @Override public void onResult(DriveApi.MetadataBufferResult resultBufferResult) { for (Metadata result : resultBufferResult.getMetadataBuffer()) { if (result.getTitle().equals(STORAGE_DIR)) { storageDriveId = result.getDriveId(); getAllForms(googleClient); } } if (storageDriveId == null) { MetadataChangeSet.Builder builder = new MetadataChangeSet.Builder(); builder.setTitle(STORAGE_DIR); driveFolder .createFolder(googleClient, builder.build()) .setResultCallback( new ResultCallback<DriveFolder.DriveFolderResult>() { @Override public void onResult( DriveFolder.DriveFolderResult driveFolderResult) { storageDriveId = driveFolderResult.getDriveFolder().getDriveId(); } }); } } }); } catch (Throwable th) { throw new GoogleDriverException( "Unable to find or create google drive storage directory because " + th, th); } }