mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 17:43:06 +02:00
Add Google Drive sync for kmz files
This commit is contained in:
@@ -95,7 +95,7 @@ public class SendDriveAsyncTask extends AbstractSendAsyncTask {
|
||||
addPermission(drive, driveFile);
|
||||
return true;
|
||||
}
|
||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, null);
|
||||
SyncUtils.updateTrack(myTracksProviderUtils, track, null);
|
||||
}
|
||||
|
||||
File file = SyncUtils.insertDriveFile(
|
||||
|
||||
@@ -180,7 +180,7 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
if (trackFileFormat == TrackFileFormat.KML) {
|
||||
String name = file.getName();
|
||||
if (name.endsWith("." + TrackFileFormat.KML.getExtension())) {
|
||||
trackImporter = new KmlFileTrackImporter(context, -1L, null);
|
||||
trackImporter = new KmlFileTrackImporter(context, -1L);
|
||||
} else {
|
||||
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
||||
Uri uri = myTracksProviderUtils.insertTrack(new Track());
|
||||
|
||||
+1
-1
@@ -82,7 +82,7 @@ public class KmlFileTrackImporter extends AbstractFileTrackImporter {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public KmlFileTrackImporter(
|
||||
KmlFileTrackImporter(
|
||||
Context context, long importTrackId, MyTracksProviderUtils myTracksProviderUtils) {
|
||||
super(context, importTrackId, myTracksProviderUtils);
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,6 +1,7 @@
|
||||
// Copyright 2010 Google Inc. All Rights Reserved.
|
||||
package com.google.android.apps.mytracks.io.gdata.maps;
|
||||
|
||||
import com.google.android.apps.mytracks.io.sync.SyncUtils;
|
||||
import com.google.wireless.gdata.data.StringUtils;
|
||||
import com.google.wireless.gdata.parser.ParseException;
|
||||
import com.google.wireless.gdata.parser.xml.XmlGDataParser;
|
||||
@@ -170,8 +171,7 @@ class XmlMapsGDataSerializer extends XmlEntryGDataSerializer {
|
||||
}
|
||||
serializer.startTag(null /* ns */, "content");
|
||||
if (content.contains("</Placemark>")) {
|
||||
serializer.attribute(
|
||||
null /* ns */, "type", "application/vnd.google-earth.kml+xml");
|
||||
serializer.attribute(null /* ns */, "type", SyncUtils.KML_MIME_TYPE);
|
||||
serializer.flush();
|
||||
stream.write(content.getBytes());
|
||||
} else {
|
||||
|
||||
@@ -18,7 +18,10 @@ package com.google.android.apps.mytracks.io.sync;
|
||||
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
||||
import com.google.android.apps.mytracks.content.Track;
|
||||
import com.google.android.apps.mytracks.io.file.exporter.KmzTrackExporter;
|
||||
import com.google.android.apps.mytracks.io.file.importer.KmlFileTrackImporter;
|
||||
import com.google.android.apps.mytracks.io.file.importer.KmzTrackImporter;
|
||||
import com.google.android.apps.mytracks.io.file.importer.TrackImporter;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.gms.auth.GoogleAuthException;
|
||||
@@ -43,6 +46,7 @@ import android.content.ContentProviderClient;
|
||||
import android.content.Context;
|
||||
import android.content.SyncResult;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
@@ -65,7 +69,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
|
||||
private static final String TAG = SyncAdapter.class.getSimpleName();
|
||||
|
||||
// drive.about.get fields to get the largestChangeId
|
||||
// drive.about.get fields. Contains one field, largestChangeId
|
||||
private static final String ABOUT_GET_FIELDS = "largestChangeId";
|
||||
|
||||
private final Context context;
|
||||
@@ -90,11 +94,13 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
if (account == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String googleAccount = PreferencesUtils.getString(
|
||||
context, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
if (googleAccount == null || googleAccount.equals(PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!googleAccount.equals(account.name)) {
|
||||
return;
|
||||
}
|
||||
@@ -126,7 +132,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
} else {
|
||||
performIncrementalSync(folderId, largestChangeId);
|
||||
}
|
||||
insertNewTracks(folderId);
|
||||
insertNewDriveFiles(folderId);
|
||||
} catch (UserRecoverableAuthException e) {
|
||||
SendToGoogleUtils.sendNotification(
|
||||
context, account.name, e.getIntent(), SendToGoogleUtils.DRIVE_NOTIFICATION_ID);
|
||||
@@ -151,7 +157,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
About about = drive.about().get().setFields(ABOUT_GET_FIELDS).execute();
|
||||
long largestChangeId = about.getLargestChangeId();
|
||||
|
||||
// Get all the KML files in the "My Drive:/My Tracks" folder
|
||||
// Get all the KML/KMZ files in the "My Drive:/My Tracks" folder
|
||||
Files.List myTracksFolderRequest = drive.files()
|
||||
.list().setQ(String.format(Locale.US, SyncUtils.MY_TRACKS_FOLDER_FILES_QUERY, folderId));
|
||||
Map<String, File> myTracksFolderMap = getFiles(myTracksFolderRequest, true);
|
||||
@@ -162,14 +168,14 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
myTracksFolderMap.remove(driveId);
|
||||
}
|
||||
|
||||
// Get all the KML files in the "Shared with me:/" folder
|
||||
// Get all the KML/KMZ files in the "Shared with me:/" folder
|
||||
Files.List sharedWithMeRequest = drive.files()
|
||||
.list().setQ(SyncUtils.SHARED_WITH_ME_FILES_QUERY);
|
||||
Map<String, File> sharedWithMeMap = getFiles(sharedWithMeRequest, false);
|
||||
|
||||
try {
|
||||
insertNewDriveFiles(myTracksFolderMap.values());
|
||||
insertNewDriveFiles(sharedWithMeMap.values());
|
||||
insertNewTracks(myTracksFolderMap.values());
|
||||
insertNewTracks(sharedWithMeMap.values());
|
||||
PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId);
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -221,7 +227,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
* E.g., the file is moved to another folder. Clear the drive
|
||||
* id.
|
||||
*/
|
||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, null);
|
||||
SyncUtils.updateTrack(myTracksProviderUtils, track, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -253,7 +259,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
context, R.string.drive_deleted_list_key, PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
|
||||
|
||||
Map<String, File> changes = new HashMap<String, File>();
|
||||
largestChangeId = getDriveChanges(folderId, largestChangeId, changes);
|
||||
largestChangeId = getDriveChangesInfo(folderId, largestChangeId, changes);
|
||||
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
@@ -288,7 +294,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
* E.g., the file is moved to another folder. Clear the drive
|
||||
* id.
|
||||
*/
|
||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, null);
|
||||
SyncUtils.updateTrack(myTracksProviderUtils, track, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -296,7 +302,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
}
|
||||
|
||||
// Handle new drive files
|
||||
insertNewDriveFiles(changes.values());
|
||||
insertNewTracks(changes.values());
|
||||
PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId);
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
@@ -306,11 +312,11 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts new tracks.
|
||||
* Inserts new drive files from tracks without a drive id.
|
||||
*
|
||||
* @param folderId the folder id
|
||||
*/
|
||||
private void insertNewTracks(String folderId) throws IOException {
|
||||
private void insertNewDriveFiles(String folderId) throws IOException {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = myTracksProviderUtils.getTrackCursor(SyncUtils.NO_DRIVE_ID_TRACKS_QUERY, null, null);
|
||||
@@ -334,31 +340,42 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts new Google Drive files.
|
||||
* Inserts new tracks from a collection of drive files.
|
||||
*
|
||||
* @param driveFiles a collection of drive files to insert
|
||||
* @throws IOException
|
||||
* @param driveFiles the drive files
|
||||
*/
|
||||
private void insertNewDriveFiles(Collection<File> driveFiles) throws IOException {
|
||||
private void insertNewTracks(Collection<File> driveFiles) throws IOException {
|
||||
for (File driveFile : driveFiles) {
|
||||
if (driveFile == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean useKmz = KmzTrackExporter.KMZ_EXTENSION.equals(driveFile.getFileExtension());
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
if (driveFile == null) {
|
||||
continue;
|
||||
}
|
||||
inputStream = downloadDriveFile(driveFile, true);
|
||||
if (inputStream == null) {
|
||||
continue;
|
||||
}
|
||||
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, -1L, null);
|
||||
long trackId = kmlFileTrackImporter.importFile(inputStream);
|
||||
|
||||
TrackImporter trackImporter;
|
||||
if (useKmz) {
|
||||
Uri uri = myTracksProviderUtils.insertTrack(new Track());
|
||||
long newId = Long.parseLong(uri.getLastPathSegment());
|
||||
|
||||
trackImporter = new KmzTrackImporter(context, newId);
|
||||
} else {
|
||||
trackImporter = new KmlFileTrackImporter(context, -1L);
|
||||
}
|
||||
|
||||
long trackId = trackImporter.importFile(inputStream);
|
||||
if (trackId != -1L) {
|
||||
Track track = myTracksProviderUtils.getTrack(trackId);
|
||||
if (track == null) {
|
||||
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId());
|
||||
continue;
|
||||
}
|
||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile);
|
||||
SyncUtils.updateTrack(myTracksProviderUtils, track, driveFile);
|
||||
Log.d(TAG, "Add from Google Drive " + track.getName());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@@ -396,14 +413,14 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Drive changes in the My Tracks folder. Includes deleted files.
|
||||
* Gets the drive changes info in the My Tracks folder, including deleted files.
|
||||
*
|
||||
* @param folderId the folder id
|
||||
* @param changeId the largest change id
|
||||
* @param changes a map of drive id to file for the changes
|
||||
* @return an updated largest change id
|
||||
*/
|
||||
private long getDriveChanges(String folderId, long changeId, Map<String, File> changes)
|
||||
private long getDriveChangesInfo(String folderId, long changeId, Map<String, File> changes)
|
||||
throws IOException {
|
||||
Changes.List request = drive.changes().list().setStartChangeId(changeId + 1);
|
||||
do {
|
||||
@@ -463,15 +480,16 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates track based on drive file changes. Returns true if successful.
|
||||
* Updates a track based on a drive file. Returns true if successful.
|
||||
*
|
||||
* @param track the track
|
||||
* @param driveFile the drive file
|
||||
*/
|
||||
private boolean updateTrack(Track track, File driveFile) throws IOException {
|
||||
boolean useKmz = KmzTrackExporter.KMZ_EXTENSION.equals(driveFile.getFileExtension());
|
||||
java.io.File file = null;
|
||||
try {
|
||||
file = SyncUtils.getFile(context, myTracksProviderUtils, track);
|
||||
file = SyncUtils.getFile(context, myTracksProviderUtils, track, useKmz);
|
||||
String digest = SyncUtils.md5(file);
|
||||
if (digest != null && digest.equals(driveFile.getMd5Checksum())) {
|
||||
track.setModifiedTime(driveFile.getModifiedDate().getValue());
|
||||
@@ -490,15 +508,21 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
Log.e(TAG, "Unable to update track. Input stream is null for track " + track.getName());
|
||||
return false;
|
||||
}
|
||||
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(
|
||||
context, track.getId(), null);
|
||||
long trackId = kmlFileTrackImporter.importFile(inputStream);
|
||||
|
||||
TrackImporter trackImporter;
|
||||
if (useKmz) {
|
||||
trackImporter = new KmzTrackImporter(context, track.getId());
|
||||
} else {
|
||||
trackImporter = new KmlFileTrackImporter(context, track.getId());
|
||||
}
|
||||
|
||||
long trackId = trackImporter.importFile(inputStream);
|
||||
if (trackId != -1L) {
|
||||
Track newTrack = myTracksProviderUtils.getTrack(trackId);
|
||||
if (newTrack == null) {
|
||||
Log.e(TAG, "Unable to merge, imported track is null");
|
||||
} else {
|
||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, newTrack, driveFile);
|
||||
SyncUtils.updateTrack(myTracksProviderUtils, newTrack, driveFile);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -22,6 +22,8 @@ import com.google.android.apps.mytracks.content.Track;
|
||||
import com.google.android.apps.mytracks.content.TracksColumns;
|
||||
import com.google.android.apps.mytracks.io.file.TrackFileFormat;
|
||||
import com.google.android.apps.mytracks.io.file.exporter.FileTrackExporter;
|
||||
import com.google.android.apps.mytracks.io.file.exporter.KmzTrackExporter;
|
||||
import com.google.android.apps.mytracks.io.file.exporter.TrackExporter;
|
||||
import com.google.android.apps.mytracks.util.FileUtils;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
@@ -69,17 +71,24 @@ public class SyncUtils {
|
||||
// Get tracks without drive id
|
||||
public static final String NO_DRIVE_ID_TRACKS_QUERY = TracksColumns.DRIVEID + " IS NULL OR "
|
||||
+ TracksColumns.DRIVEID + "=''";
|
||||
|
||||
|
||||
// KML mime type
|
||||
public static final String KML_MIME_TYPE = "application/vnd.google-earth.kml+xml";
|
||||
|
||||
// Get My Tracks folder KML files
|
||||
public static final String MY_TRACKS_FOLDER_FILES_QUERY = "'%s' in parents and mimeType = '"
|
||||
+ KML_MIME_TYPE + "' and trashed = false";
|
||||
// KMZ mime type
|
||||
public static final String KMZ_MIME_TYPE = "application/vnd.google-earth.kmz";
|
||||
|
||||
// Get shared with me KML files
|
||||
public static final String SHARED_WITH_ME_FILES_QUERY = "sharedWithMe and mimeType = '"
|
||||
+ KML_MIME_TYPE + "' and trashed = false";
|
||||
// KML and KMZ mime types
|
||||
private static final String KML_KMZ_MINE_TYPES = "not (mimeType != '" + KML_MIME_TYPE
|
||||
+ "' and mimeType != '" + KMZ_MIME_TYPE + "')";
|
||||
|
||||
// Get KML/KMZ files in the My Tracks folder
|
||||
public static final String MY_TRACKS_FOLDER_FILES_QUERY = "'%s' in parents and "
|
||||
+ KML_KMZ_MINE_TYPES + " and trashed = false";
|
||||
|
||||
// Get shared with me KML/KMZ files
|
||||
public static final String SHARED_WITH_ME_FILES_QUERY = "sharedWithMe and " + KML_KMZ_MINE_TYPES
|
||||
+ " and trashed = false";
|
||||
|
||||
// Folder mime type
|
||||
private static final String FOLDER_MIME_TYPE = "application/vnd.google-apps.folder";
|
||||
@@ -155,7 +164,7 @@ public class SyncUtils {
|
||||
|
||||
/**
|
||||
* Clears the sync state. Assumes sync is turned off. Do not want clearing the
|
||||
* state to cause sync activities.
|
||||
* sync state to cause sync activities.
|
||||
*/
|
||||
public static void clearSyncState(Context context) {
|
||||
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
||||
@@ -168,7 +177,7 @@ public class SyncUtils {
|
||||
if (track.isSharedWithMe()) {
|
||||
myTracksProviderUtils.deleteTrack(track.getId());
|
||||
} else {
|
||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, null);
|
||||
SyncUtils.updateTrack(myTracksProviderUtils, track, null);
|
||||
}
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
@@ -218,7 +227,7 @@ public class SyncUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the drive file is a Shared with me KML file.
|
||||
* Returns true if a drive file is a Shared with me KML or KMZ file.
|
||||
*
|
||||
* @param driveFile the drive file
|
||||
*/
|
||||
@@ -226,14 +235,15 @@ public class SyncUtils {
|
||||
if (driveFile == null) {
|
||||
return false;
|
||||
}
|
||||
if (!SyncUtils.KML_MIME_TYPE.equals(driveFile.getMimeType())) {
|
||||
String mimeType = driveFile.getMimeType();
|
||||
if (!SyncUtils.KML_MIME_TYPE.equals(mimeType) && !SyncUtils.KMZ_MIME_TYPE.equals(mimeType)) {
|
||||
return false;
|
||||
}
|
||||
return driveFile.getSharedWithMeDate() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the drive file is a valid KML file in the My Tracks folder
|
||||
* Returns true if a drive file is a KML or KMZ file in the My Tracks folder
|
||||
* and not trashed.
|
||||
*
|
||||
* @param driveFile the drive file
|
||||
@@ -244,7 +254,7 @@ public class SyncUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the drive file is a KML file in the My Tracks folder.
|
||||
* Returns true if a drive file is a KML or KMZ file in the My Tracks folder.
|
||||
*
|
||||
* @param driveFile the drive file
|
||||
* @param folderId the My Tracks folder id
|
||||
@@ -253,7 +263,8 @@ public class SyncUtils {
|
||||
if (driveFile == null) {
|
||||
return false;
|
||||
}
|
||||
if (!SyncUtils.KML_MIME_TYPE.equals(driveFile.getMimeType())) {
|
||||
String mimeType = driveFile.getMimeType();
|
||||
if (!SyncUtils.KML_MIME_TYPE.equals(mimeType) && !SyncUtils.KMZ_MIME_TYPE.equals(mimeType)) {
|
||||
return false;
|
||||
}
|
||||
if (driveFile.getSharedWithMeDate() != null) {
|
||||
@@ -269,7 +280,7 @@ public class SyncUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a Drive file.
|
||||
* Inserts a drive file using info from a track.
|
||||
*
|
||||
* @param drive the drive
|
||||
* @param folderId the folder id
|
||||
@@ -284,7 +295,7 @@ public class SyncUtils {
|
||||
throws IOException {
|
||||
java.io.File file = null;
|
||||
try {
|
||||
file = getFile(context, myTracksProviderUtils, track);
|
||||
file = getFile(context, myTracksProviderUtils, track, true);
|
||||
|
||||
if (file == null) {
|
||||
Log.e(TAG, "Unable to add Drive file. File is null for track " + track.getName());
|
||||
@@ -297,7 +308,7 @@ public class SyncUtils {
|
||||
Log.e(TAG, "Unable to add Drive file. Uploaded file is null for track " + track.getName());
|
||||
return null;
|
||||
}
|
||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, uploadedFile);
|
||||
SyncUtils.updateTrack(myTracksProviderUtils, track, uploadedFile);
|
||||
return uploadedFile;
|
||||
} finally {
|
||||
if (file != null) {
|
||||
@@ -307,20 +318,18 @@ public class SyncUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a Drive file.
|
||||
* Inserts a drive file using info from a track file.
|
||||
*
|
||||
* @param drive the drive
|
||||
* @param folderId the folder id
|
||||
* @param name the track name
|
||||
* @param trackName the track name
|
||||
* @param file the track file
|
||||
* @param canRetry true if can retry
|
||||
*/
|
||||
private static File insertDriveFile(
|
||||
Drive drive, String folderId, String name, java.io.File file, boolean canRetry)
|
||||
Drive drive, String folderId, String trackName, java.io.File file, boolean canRetry)
|
||||
throws IOException {
|
||||
try {
|
||||
FileContent fileContent = new FileContent(KML_MIME_TYPE, file);
|
||||
|
||||
// file's parent
|
||||
ParentReference parentReference = new ParentReference();
|
||||
parentReference.setId(folderId);
|
||||
@@ -329,23 +338,24 @@ public class SyncUtils {
|
||||
|
||||
// file's metadata
|
||||
File newMetaData = new File();
|
||||
newMetaData.setTitle(name + "." + TrackFileFormat.KML.getExtension());
|
||||
newMetaData.setMimeType(KML_MIME_TYPE);
|
||||
newMetaData.setTitle(trackName + "." + KmzTrackExporter.KMZ_EXTENSION);
|
||||
newMetaData.setMimeType(KMZ_MIME_TYPE);
|
||||
newMetaData.setParents(parents);
|
||||
|
||||
FileContent fileContent = new FileContent(KMZ_MIME_TYPE, file);
|
||||
return drive.files().insert(newMetaData, fileContent).execute();
|
||||
} catch (UserRecoverableAuthIOException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
if (canRetry) {
|
||||
return insertDriveFile(drive, folderId, name, file, false);
|
||||
return insertDriveFile(drive, folderId, trackName, file, false);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a Drive file. Returns true if successful.
|
||||
* Updates a drive file using info from a track. Returns true if successful.
|
||||
*
|
||||
* @param drive the drive
|
||||
* @param driveFile the drive file
|
||||
@@ -361,20 +371,21 @@ public class SyncUtils {
|
||||
java.io.File file = null;
|
||||
|
||||
try {
|
||||
file = SyncUtils.getFile(context, myTracksProviderUtils, track);
|
||||
file = SyncUtils.getFile(context, myTracksProviderUtils, track, true);
|
||||
|
||||
if (file == null) {
|
||||
Log.e(TAG, "Unable to update drive file. File is null for track " + track.getName());
|
||||
return false;
|
||||
}
|
||||
|
||||
String title = track.getName() + "." + TrackFileFormat.KML.getExtension();
|
||||
String title = track.getName() + "." + KmzTrackExporter.KMZ_EXTENSION;
|
||||
File updatedFile;
|
||||
String digest = md5(file);
|
||||
if (digest != null && digest.equals(driveFile.getMd5Checksum())) {
|
||||
if (title.equals(driveFile.getTitle())) {
|
||||
updatedFile = driveFile;
|
||||
} else {
|
||||
// Only update the title
|
||||
updatedFile = updateDriveFile(drive, driveFile, title, null, canRetry);
|
||||
}
|
||||
} else {
|
||||
@@ -399,12 +410,12 @@ public class SyncUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a Drive file.
|
||||
* Updates a drive file using a track file.
|
||||
*
|
||||
* @param drive the drive
|
||||
* @param driveFile the drive file
|
||||
* @param driveTitle the drive title
|
||||
* @param file the track file
|
||||
* @param file the track file. If null, just update the driveFile meta data
|
||||
* @param canRetry true if can retry
|
||||
*/
|
||||
private static File updateDriveFile(
|
||||
@@ -412,9 +423,11 @@ public class SyncUtils {
|
||||
throws IOException {
|
||||
try {
|
||||
driveFile.setTitle(driveTitle);
|
||||
driveFile.setMimeType(KMZ_MIME_TYPE);
|
||||
|
||||
if (file != null) {
|
||||
return drive.files()
|
||||
.update(driveFile.getId(), driveFile, new FileContent(KML_MIME_TYPE, file)).execute();
|
||||
FileContent fileContent = new FileContent(KMZ_MIME_TYPE, file);
|
||||
return drive.files().update(driveFile.getId(), driveFile, fileContent).execute();
|
||||
} else {
|
||||
return drive.files().update(driveFile.getId(), driveFile).execute();
|
||||
}
|
||||
@@ -432,30 +445,34 @@ public class SyncUtils {
|
||||
* Gets a file from a track.
|
||||
*
|
||||
* @param context the context
|
||||
* @param myTracksProviderUtils the myTracksProviderUtils
|
||||
* @param myTracksProviderUtils the myMyTracksProviderUtils
|
||||
* @param track the track
|
||||
* @param useKmz true to output kmz
|
||||
*/
|
||||
public static java.io.File getFile(
|
||||
Context context, MyTracksProviderUtils myTracksProviderUtils, Track track)
|
||||
Context context, MyTracksProviderUtils myTracksProviderUtils, Track track, boolean useKmz)
|
||||
throws FileNotFoundException {
|
||||
TrackFileFormat trackFileFormat = TrackFileFormat.KML;
|
||||
java.io.File directory = new java.io.File(
|
||||
context.getCacheDir(), trackFileFormat.getExtension());
|
||||
String extension = useKmz ? KmzTrackExporter.KMZ_EXTENSION : TrackFileFormat.KML.getExtension();
|
||||
java.io.File directory = new java.io.File(context.getCacheDir(), extension);
|
||||
|
||||
if (!FileUtils.ensureDirectoryExists(directory)) {
|
||||
Log.d(TAG, "Unable to create " + directory.getAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
|
||||
java.io.File file = new java.io.File(directory,
|
||||
FileUtils.buildUniqueFileName(directory, track.getName(), trackFileFormat.getExtension()));
|
||||
FileTrackExporter fileTrackExporter = new FileTrackExporter(myTracksProviderUtils,
|
||||
new Track[] { track }, trackFileFormat.newTrackWriter(context, false), null);
|
||||
Track[] tracks = new Track[] { track };
|
||||
java.io.File file = new java.io.File(
|
||||
directory, FileUtils.buildUniqueFileName(directory, track.getName(), extension));
|
||||
FileTrackExporter fileTrackExporter = new FileTrackExporter(
|
||||
myTracksProviderUtils, tracks, TrackFileFormat.KML.newTrackWriter(context, false), null);
|
||||
TrackExporter trackExporter = useKmz ? new KmzTrackExporter(
|
||||
myTracksProviderUtils, fileTrackExporter, tracks)
|
||||
: fileTrackExporter;
|
||||
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(file);
|
||||
if (fileTrackExporter.writeTrack(fileOutputStream)) {
|
||||
if (trackExporter.writeTrack(fileOutputStream)) {
|
||||
return file;
|
||||
} else {
|
||||
if (!file.delete()) {
|
||||
@@ -476,13 +493,13 @@ public class SyncUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates track with drive file info.
|
||||
* Updates a track with info from a drive file.
|
||||
*
|
||||
* @param myTracksProviderUtils the myTracksProviderUtils
|
||||
* @param track the track
|
||||
* @param driveFile the drive file
|
||||
*/
|
||||
public static void updateTrackWithDriveFileInfo(
|
||||
public static void updateTrack(
|
||||
MyTracksProviderUtils myTracksProviderUtils, Track track, File driveFile) {
|
||||
track.setDriveId(driveFile != null ? driveFile.getId() : "");
|
||||
track.setModifiedTime(driveFile != null ? driveFile.getModifiedDate().getValue() : -1L);
|
||||
@@ -494,7 +511,7 @@ public class SyncUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the md5 digest for the file.
|
||||
* Gets the md5 digest for a file.
|
||||
*
|
||||
* @param file the file
|
||||
*/
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.google.android.apps.mytracks.util;
|
||||
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
||||
import com.google.android.apps.mytracks.io.sync.SyncUtils;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
@@ -40,7 +41,6 @@ public class GoogleEarthUtils {
|
||||
private static final String TAG = GoogleEarthUtils.class.getSimpleName();
|
||||
|
||||
private static final String GOOGLE_EARTH_CLASS = "com.google.earth.EarthActivity";
|
||||
private static final String GOOGLE_EARTH_KML_MIME_TYPE = "application/vnd.google-earth.kml+xml";
|
||||
private static final int GOOGLE_EARTH_MIN_VERSION_CODE = 13246120;
|
||||
private static final String GOOGLE_EARTH_PACKAGE = "com.google.earth";
|
||||
private static final String
|
||||
@@ -60,7 +60,7 @@ public class GoogleEarthUtils {
|
||||
*/
|
||||
public static boolean isEarthInstalled(Context context) {
|
||||
List<ResolveInfo> infos = context.getPackageManager().queryIntentActivities(
|
||||
new Intent().setType(GOOGLE_EARTH_KML_MIME_TYPE), PackageManager.MATCH_DEFAULT_ONLY);
|
||||
new Intent().setType(SyncUtils.KMZ_MIME_TYPE), PackageManager.MATCH_DEFAULT_ONLY);
|
||||
for (ResolveInfo info : infos) {
|
||||
if (info.activityInfo != null && info.activityInfo.packageName != null
|
||||
&& info.activityInfo.packageName.equals(GOOGLE_EARTH_PACKAGE)) {
|
||||
@@ -89,6 +89,6 @@ public class GoogleEarthUtils {
|
||||
| Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
.putExtra(GOOGLE_EARTH_TOUR_FEATURE_ID, TOUR_FEATURE_ID_VALUE)
|
||||
.setClassName(GOOGLE_EARTH_PACKAGE, GOOGLE_EARTH_CLASS)
|
||||
.setDataAndType(uri, GOOGLE_EARTH_KML_MIME_TYPE);
|
||||
.setDataAndType(uri, SyncUtils.KMZ_MIME_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user