Reduce number of Google Drive api calls by using a list to keep track of the edits

This commit is contained in:
Jimmy Shih
2013-10-14 14:51:26 -07:00
parent 6676f6b760
commit 75c58c556a
7 changed files with 76 additions and 10 deletions
+1
View File
@@ -63,6 +63,7 @@ limitations under the License.
<string name="auto_resume_track_current_retry_key">autoResumeTrackCurrentRetry</string>
<string name="bluetooth_sensor_key">bluetoothSensor</string>
<string name="drive_deleted_list_key">driveDeletedList</string>
<string name="drive_edited_list_key">driveEditedList</string>
<string name="drive_largest_change_id_key">driveLargestChangeId</string>
<string name="drive_sync_key">driveSync</string>
<string name="google_account_key">googleAccount</string>
@@ -17,10 +17,12 @@
package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.content.Waypoint.WaypointType;
import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.services.TrackRecordingServiceConnection;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.TrackRecordingServiceConnectionUtils;
import com.google.android.maps.mytracks.R;
@@ -123,6 +125,17 @@ public class MarkerEditActivity extends AbstractMyTracksActivity {
} else {
saveMarker();
}
boolean driveSync = PreferencesUtils.getBoolean(
MarkerEditActivity.this, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
if (driveSync) {
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(
MarkerEditActivity.this);
Track track = myTracksProviderUtils.getTrack(newMarker ? trackId : waypoint.getTrackId());
track.setModifiedTime(System.currentTimeMillis());
myTracksProviderUtils.updateTrack(track);
PreferencesUtils.addToList(MarkerEditActivity.this, R.string.drive_edited_list_key,
PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT, String.valueOf(track.getId()));
}
finish();
}
});
@@ -23,6 +23,7 @@ import com.google.android.apps.mytracks.fragments.ChooseActivityTypeDialogFragme
import com.google.android.apps.mytracks.services.TrackRecordingServiceConnection;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.CalorieUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.TrackIconUtils;
import com.google.android.apps.mytracks.util.TrackNameUtils;
import com.google.android.apps.mytracks.util.TrackRecordingServiceConnectionUtils;
@@ -199,6 +200,12 @@ public class TrackEditActivity extends AbstractMyTracksActivity
track.setDescription(description.getText().toString());
track.setModifiedTime(System.currentTimeMillis());
myTracksProviderUtils.updateTrack(track);
boolean driveSync = PreferencesUtils.getBoolean(
TrackEditActivity.this, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
if (driveSync) {
PreferencesUtils.addToList(TrackEditActivity.this, R.string.drive_edited_list_key,
PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT, String.valueOf(track.getId()));
}
finish();
}
});
@@ -52,6 +52,7 @@ public class PreferenceBackupHelper {
doNotBackup.add(context.getString(R.string.auto_resume_track_current_retry_key));
doNotBackup.add(context.getString(R.string.bluetooth_sensor_key));
doNotBackup.add(context.getString(R.string.drive_deleted_list_key));
doNotBackup.add(context.getString(R.string.drive_edited_list_key));
doNotBackup.add(context.getString(R.string.drive_largest_change_id_key));
doNotBackup.add(context.getString(R.string.drive_sync_key));
doNotBackup.add(context.getString(R.string.google_account_key));
@@ -251,6 +251,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
*/
private void performIncrementalSync(String folderId, long largestChangeId) throws IOException {
// Handle deleted tracks
String driveDeletedList = PreferencesUtils.getString(
context, R.string.drive_deleted_list_key, PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
String deletedIds[] = TextUtils.split(driveDeletedList, ";");
@@ -260,9 +261,33 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
PreferencesUtils.setString(
context, R.string.drive_deleted_list_key, PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
// Handle edited tracks
String driveEditedList = PreferencesUtils.getString(
context, R.string.drive_edited_list_key, PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT);
String editedIds[] = TextUtils.split(driveEditedList, ";");
for (String id : editedIds) {
Track track = myTracksProviderUtils.getTrack(Long.valueOf(id));
if (track == null) {
continue;
}
if (track.isSharedWithMe()) {
continue;
}
String driveId = track.getDriveId();
if (driveId == null || driveId.equals("")) {
continue;
}
File driveFile = drive.files().get(driveId).execute();
if (SyncUtils.isInMyTracksAndValid(driveFile, folderId)) {
merge(track, driveFile);
}
}
PreferencesUtils.setString(
context, R.string.drive_edited_list_key, PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT);
Map<String, File> changes = new HashMap<String, File>();
largestChangeId = getDriveChangesInfo(largestChangeId, changes);
Cursor cursor = null;
try {
// Get all the local tracks with drive file id
@@ -288,15 +313,6 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
}
}
changes.remove(driveId);
} else {
if (!track.isSharedWithMe()) {
// Handle the case the track has changed
File driveFile = drive.files().get(driveId).execute();
if (SyncUtils.isInMyTracksAndValid(driveFile, folderId)) {
merge(track, driveFile);
}
}
}
} while (cursor.moveToNext());
}
@@ -185,6 +185,9 @@ public class SyncUtils {
PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key,
PreferencesUtils.DRIVE_LARGEST_CHANGE_ID_DEFAULT);
PreferencesUtils.setString(
context, R.string.drive_edited_list_key, PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT);
// Clear the drive_deleted_list_key last
PreferencesUtils.setString(
context, R.string.drive_deleted_list_key, PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
@@ -26,6 +26,7 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
/**
* Utilities to access preferences stored in {@link SharedPreferences}.
@@ -60,6 +61,7 @@ public class PreferencesUtils {
public static final String DEFAULT_ACTIVITY_DEFAULT = "";
public static final String DRIVE_DELETED_LIST_DEFAULT = "";
public static final String DRIVE_EDITED_LIST_DEFAULT = "";
public static final long DRIVE_LARGEST_CHANGE_ID_DEFAULT = -1L;
public static final boolean DRIVE_SYNC_DEFAULT = false;
@@ -278,4 +280,27 @@ public class PreferencesUtils {
return PreferencesUtils.CHART_X_AXIS_DEFAULT.equals(
getString(context, R.string.chart_x_axis_key, PreferencesUtils.CHART_X_AXIS_DEFAULT));
}
/**
* Adds a value to a list.
*
* @param context the context
* @param keyId the key id
* @param defaultValue the default value
* @param value the value
*/
public static void addToList(Context context, int keyId, String defaultValue, String value) {
String list = getString(context, keyId, defaultValue);
if (defaultValue.equals(list)) {
setString(context, keyId, value);
return;
}
String[] items = TextUtils.split(list, ";");
for (String item : items) {
if (value.equals(item)) {
return;
}
}
setString(context, keyId, list + ";" + value);
}
}