use of com.google.api.services.drive.model.ParentReference in project local-data-aragopedia by aragonopendata.
the class GoogleDriveAPI method createSpreadsheetFromFile.
/**
* Method to create a google spreadsheet from a file
*
* @param idParentFolder
* String to identify the parent folder
* @param emailUserOwner
* String to identify email user owner
* @param extensionFile
* String with extension file
* @param nameFile
* String with name google spreadsheet
* @param fileOrigin
* File with content file origin
* @param mimeType
* String with mime type file origin
*
* @return True if create is ok, False otherwise
*/
public boolean createSpreadsheetFromFile(String idParentFolder, String emailUserOwner, String extensionFile, String nameFile, java.io.File fileOrigin, String mimeType) {
boolean result = true;
Permission newPermission = createPermission();
File body = new File();
body.setTitle(nameFile);
body.setMimeType("application/vnd.google-apps.spreadsheet");
body.setEditable(true);
body.setShared(true);
body.setPermissions(Arrays.asList(newPermission));
body.setUserPermission(newPermission);
body.setWritersCanShare(true);
body.setFileExtension(extensionFile);
User user = new User();
user.setEmailAddress(emailUserOwner);
user.setIsAuthenticatedUser(true);
List<User> list = new ArrayList<User>();
list.add(user);
body.setOwners(list);
body.setParents(Arrays.asList(new ParentReference().setId(idParentFolder)));
FileContent mediaContent = new FileContent(mimeType, fileOrigin);
String fileId = "";
try {
File file = drive.files().insert(body, mediaContent).execute();
fileId = file.getId();
insertPermission(newPermission, fileId);
} catch (Exception e) {
log.error("Error create spreadsheet from file ", e);
result = false;
}
log.info("create Spreadsheet in google Drive from " + nameFile);
return result;
}
use of com.google.api.services.drive.model.ParentReference in project local-data-aragopedia by aragonopendata.
the class GoogleDriveAPI method createSpreadsheetFromFile.
public boolean createSpreadsheetFromFile(String idParentFolder, String emailUserOwner, String extensionFile, String nameFile, java.io.File fileOrigin, String mimeType) {
boolean result = true;
Permission newPermission = createPermission();
File body = new File();
body.setTitle(nameFile);
body.setMimeType("application/vnd.google-apps.spreadsheet");
body.setEditable(true);
body.setShared(true);
body.setPermissions(Arrays.asList(newPermission));
body.setUserPermission(newPermission);
body.setWritersCanShare(true);
body.setFileExtension(extensionFile);
User user = new User();
user.setEmailAddress(emailUserOwner);
user.setIsAuthenticatedUser(true);
List<User> list = new ArrayList<User>();
list.add(user);
body.setOwners(list);
body.setParents(Arrays.asList(new ParentReference().setId(idParentFolder)));
FileContent mediaContent = new FileContent(mimeType, fileOrigin);
String fileId = "";
try {
File file = service.files().insert(body, mediaContent).execute();
fileId = file.getId();
insertPermission(newPermission, fileId);
} catch (IOException e) {
log.error("Error create spreadsheet from file ", e);
result = false;
}
log.info("create Spreadsheet in google Drive from " + nameFile);
return result;
}
use of com.google.api.services.drive.model.ParentReference in project keepass2android by PhilippC.
the class GoogleDriveFileStorage method createFolder.
@Override
public String createFolder(String parentPath, String newDirName) throws Exception {
File body = new File();
body.setTitle(newDirName);
body.setMimeType(FOLDER_MIME_TYPE);
GDrivePath parentGdrivePath = new GDrivePath(parentPath);
body.setParents(Arrays.asList(new ParentReference().setId(parentGdrivePath.getGDriveId())));
try {
File file = getDriveService(parentGdrivePath.getAccount()).files().insert(body).execute();
logDebug("created folder " + newDirName + " in " + parentPath + ". id: " + file.getId());
return new GDrivePath(parentPath, file).getFullPath();
} catch (Exception e) {
throw convertException(e);
}
}
use of com.google.api.services.drive.model.ParentReference in project keepass2android by PhilippC.
the class GoogleDriveFileStorage method createFilePath.
@Override
public String createFilePath(String parentPath, String newFileName) throws Exception {
File body = new File();
body.setTitle(newFileName);
GDrivePath parentGdrivePath = new GDrivePath(parentPath);
body.setParents(Arrays.asList(new ParentReference().setId(parentGdrivePath.getGDriveId())));
try {
File file = getDriveService(parentGdrivePath.getAccount()).files().insert(body).execute();
return new GDrivePath(parentPath, file).getFullPath();
} catch (Exception e) {
throw convertException(e);
}
}
use of com.google.api.services.drive.model.ParentReference in project Aegis by Decad3nce.
the class BackupGoogleAccountsActivity method saveFileToDrive.
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
if (!isAegisFolderAvailable()) {
createAegisFolder();
}
FileContent mediaContent = null;
File body = new File();
File file = null;
if (callLogs) {
Log.i(TAG, "Generating new file to upload: " + callLogFileUri);
java.io.File callFileContent = new java.io.File(callLogFileUri.getPath());
mediaContent = new FileContent("text/plain", callFileContent);
body.setTitle(callFileContent.getName());
body.setParents(Arrays.asList(new ParentReference().setId(getAegisFolder())));
body.setMimeType("text/plain");
file = service.files().insert(body, mediaContent).execute();
if (file != null) {
Log.i(TAG, "File uploaded successfully: " + file.getTitle());
Utils.sendSMS(context, address, context.getResources().getString(R.string.util_sendsms_data_recovery_pass) + " " + file.getTitle() + " to Google Drive");
deleteFile(file.getTitle());
finish();
}
}
if (smsLogs) {
Log.i(TAG, "Generating new file to upload: " + smsLogFileUri);
java.io.File smsFileContent = new java.io.File(smsLogFileUri.getPath());
mediaContent = new FileContent("text/plain", smsFileContent);
body.setTitle(smsFileContent.getName());
body.setParents(Arrays.asList(new ParentReference().setId(getAegisFolder())));
body.setMimeType("text/plain");
file = service.files().insert(body, mediaContent).execute();
if (file != null) {
Log.i(TAG, "File uploaded successfully: " + file.getTitle());
Utils.sendSMS(context, address, context.getResources().getString(R.string.util_sendsms_data_recovery_pass) + " " + file.getTitle() + " to Google Drive");
deleteFile(file.getTitle());
finish();
}
}
} catch (UserRecoverableAuthIOException e) {
Log.i(TAG, "Exception: " + e.toString());
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
Log.i(TAG, "Exception: " + e.toString());
e.printStackTrace();
Utils.sendSMS(context, address, context.getResources().getString(R.string.util_sendsms_data_recovery_fail));
}
}
});
t.start();
}
Aggregations