Add option to share a track via Google Drive.

This commit is contained in:
Jimmy Shih
2013-01-29 16:40:01 -08:00
parent f9bd84dba0
commit 3f173d4434
14 changed files with 345 additions and 41 deletions
@@ -38,7 +38,8 @@ public class SendDriveActivity extends AbstractSendActivity {
@Override
protected AbstractSendAsyncTask createAsyncTask() {
return new SendDriveAsyncTask(this, sendRequest.getTrackId(), sendRequest.getAccount());
return new SendDriveAsyncTask(
this, sendRequest.getTrackId(), sendRequest.getAccount(), sendRequest.getAcl());
}
@Override
@@ -23,6 +23,7 @@ import com.google.android.apps.mytracks.io.sync.SyncUtils;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.Permission;
import android.accounts.Account;
import android.content.Context;
@@ -42,12 +43,18 @@ public class SendDriveAsyncTask extends AbstractSendAsyncTask {
private final long trackId;
private final Account account;
private final Context context;
private final String[] acl;
private final MyTracksProviderUtils myTracksProviderUtils;
public SendDriveAsyncTask(SendDriveActivity activity, long trackId, Account account) {
public SendDriveAsyncTask(SendDriveActivity activity, long trackId, Account account, String acl) {
super(activity);
this.trackId = trackId;
this.account = account;
if (acl != null) {
this.acl = acl.split(",");
} else {
this.acl = null;
}
context = activity.getApplicationContext();
myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
@@ -78,13 +85,22 @@ public class SendDriveAsyncTask extends AbstractSendAsyncTask {
if (driveId != null && !driveId.equals("")) {
File driveFile = drive.files().get(driveId).execute();
if (SyncUtils.isDriveFileValid(driveFile, folderId)) {
return SyncUtils.updateDriveFile(context, myTracksProviderUtils, drive, driveFile, track);
if (SyncUtils.updateDriveFile(context, myTracksProviderUtils, drive, driveFile, track)) {
addPermission(drive, driveId);
return true;
}
}
track.setDriveId("");
track.setModifiedTime(-1L);
myTracksProviderUtils.updateTrack(track);
}
return SyncUtils.addDriveFile(context, myTracksProviderUtils, drive, folderId, track);
String id = SyncUtils.addDriveFile(context, myTracksProviderUtils, drive, folderId, track);
if (id == null) {
return false;
}
addPermission(drive, id);
return true;
} catch (IOException e) {
Log.e(TAG, "IOException", e);
return false;
@@ -93,4 +109,25 @@ public class SendDriveAsyncTask extends AbstractSendAsyncTask {
@Override
protected void invalidateToken() {}
/**
* Adds permision.
*
* @param drive the drive
* @param driveId the drive id
*/
private void addPermission(Drive drive, String driveId) throws IOException {
if (acl != null) {
for (String email : acl) {
email = email.trim();
if (!email.equals("")) {
Permission permission = new Permission();
permission.setValue(email);
permission.setType("user");
permission.setRole("reader");
drive.permissions().insert(driveId, permission).execute();
}
}
}
}
}
@@ -33,6 +33,7 @@ public class SendRequest implements Parcelable {
private long trackId = -1L;
private String sharingAppPackageName = null;
private String sharingAppClassName = null;
private String acl = null;
private boolean sendDrive = false;
private boolean sendMaps = false;
private boolean sendFusionTables = false;
@@ -93,6 +94,22 @@ public class SendRequest implements Parcelable {
this.sharingAppClassName = sharingAppClassName;
}
/**
* Gets the email address of the user to share the track with.
*/
public String getAcl() {
return acl;
}
/**
* Sets the email address of the user to share the track with.
*
* @param acl the email address
*/
public void setAcl(String acl) {
this.acl = acl;
}
/**
* True if the user has selected the send to Google Drive option.
*/
@@ -279,6 +296,7 @@ public class SendRequest implements Parcelable {
trackId = in.readLong();
sharingAppPackageName = in.readString();
sharingAppClassName = in.readString();
acl = in.readString();
sendDrive = in.readByte() == 1;
sendMaps = in.readByte() == 1;
sendFusionTables = in.readByte() == 1;
@@ -302,6 +320,7 @@ public class SendRequest implements Parcelable {
out.writeLong(trackId);
out.writeString(sharingAppPackageName);
out.writeString(sharingAppClassName);
out.writeString(acl);
out.writeByte((byte) (sendDrive ? 1 : 0));
out.writeByte((byte) (sendMaps ? 1 : 0));
out.writeByte((byte) (sendFusionTables ? 1 : 0));
@@ -295,13 +295,14 @@ public class SyncUtils {
* @param drive the drive
* @param folderId the folder id
* @param track the track
* @return the added drive file id or null.
*/
public static boolean addDriveFile(Context context, MyTracksProviderUtils myTracksProviderUtils,
public static String addDriveFile(Context context, MyTracksProviderUtils myTracksProviderUtils,
Drive drive, String folderId, Track track) throws IOException {
java.io.File file = getFile(context, myTracksProviderUtils, track);
if (file == null) {
Log.e(TAG, "Unable to add Drive file. File is null for track " + track.getName());
return false;
return null;
}
try {
@@ -323,12 +324,13 @@ public class SyncUtils {
File uploadedFile = drive.files().insert(newMetaData, fileContent).execute();
if (uploadedFile == null) {
Log.e(TAG, "Unable to add Drive file. Uploaded file is null for track " + track.getName());
return false;
return null;
}
track.setDriveId(uploadedFile.getId());
String id = uploadedFile.getId();
track.setDriveId(id);
track.setModifiedTime(uploadedFile.getModifiedDate().getValue());
myTracksProviderUtils.updateTrack(track);
return true;
return id;
} finally {
file.delete();
}