Code refactor: move managing list of deleted tracks to DeleteAsyncTask

This commit is contained in:
Jimmy Shih
2013-10-15 11:08:06 -07:00
parent cb3785de5c
commit 24113a4d7f
6 changed files with 28 additions and 67 deletions
@@ -17,6 +17,7 @@ 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.io.sync.SyncUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
@@ -97,7 +98,19 @@ public class DeleteAsyncTask extends AsyncTask<Void, Integer, Boolean> {
if (isCancelled()) {
return false;
}
Track track = myTracksProviderUtils.getTrack(id);
myTracksProviderUtils.deleteTrack(id);
if (track != null) {
String driveId = track.getDriveId();
if (driveId != null && !driveId.equals("")) {
boolean driveSync = PreferencesUtils.getBoolean(
context, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
if (driveSync) {
PreferencesUtils.addToList(context, R.string.drive_deleted_list_key,
PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT, driveId);
}
}
}
}
return true;
}
@@ -79,7 +79,6 @@ public class TrackDetailActivity extends AbstractSendToGoogleActivity implements
public static final String EXTRA_TRACK_ID = "track_id";
public static final String EXTRA_MARKER_ID = "marker_id";
private static final String TAG = TrackDetailActivity.class.getSimpleName();
private static final String CURRENT_TAB_TAG_KEY = "current_tab_tag_key";
private static final String PHOTO_URI_KEY = "photo_uri_key";
private static final String HAS_PHOTO_KEY = "has_photo_key";
@@ -16,7 +16,6 @@
package com.google.android.apps.mytracks.content;
import com.google.android.apps.mytracks.io.sync.SyncUtils;
import com.google.android.apps.mytracks.util.FileUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
@@ -42,7 +41,6 @@ import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
/**
* A {@link ContentProvider} that handles access to track points, tracks, and
@@ -218,18 +216,6 @@ public class MyTracksProvider extends ContentProvider {
throw new IllegalArgumentException("Unknown URL " + url);
}
boolean driveSync = false;
String driveIds = "";
if (table.equals(TracksColumns.TABLE_NAME)) {
driveSync = PreferencesUtils.getBoolean(
getContext(), R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
if (driveSync) {
driveIds = where != null ? getDriveIds(null, where, selectionArgs)
: getDriveIds(
new String[] { TracksColumns.DRIVEID }, SyncUtils.DRIVE_ID_TRACKS_QUERY, null);
}
}
Log.w(MyTracksProvider.TAG, "Deleting table " + table);
int count;
try {
@@ -240,17 +226,6 @@ public class MyTracksProvider extends ContentProvider {
db.endTransaction();
}
getContext().getContentResolver().notifyChange(url, null, false);
if (driveSync && table.equals(TracksColumns.TABLE_NAME)) {
String driveDeletedList = PreferencesUtils.getString(getContext(),
R.string.drive_deleted_list_key, PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
if (driveDeletedList.equals(PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT)) {
driveDeletedList = driveIds;
} else {
driveDeletedList += ";" + driveIds;
}
PreferencesUtils.setString(getContext(), R.string.drive_deleted_list_key, driveDeletedList);
}
if (shouldVacuum) {
// If a potentially large amount of data was deleted, reclaim its space.
@@ -536,33 +511,4 @@ public class MyTracksProvider extends ContentProvider {
}
throw new SQLException("Failed to insert a waypoint " + url);
}
/**
* Gets a list of dirve ids.
*
* @param projection the projection
* @param where where
* @param selectionArgs selection args
*/
private String getDriveIds(String[] projection, String where, String[] selectionArgs) {
ArrayList<String> driveIds = new ArrayList<String>();
Cursor cursor = null;
try {
cursor = query(TracksColumns.CONTENT_URI, projection, where, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
int index = cursor.getColumnIndex(TracksColumns.DRIVEID);
do {
String driveId = cursor.getString(index);
if (driveId != null && !driveId.equals("")) {
driveIds.add(driveId);
}
} while (cursor.moveToNext());
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return TextUtils.join(";", driveIds);
}
}
@@ -19,6 +19,7 @@ import com.google.android.apps.mytracks.content.ContentTypeIds;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.maps.mytracks.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
@@ -118,6 +119,7 @@ public class PreferenceBackupHelper {
* @param preferences the shared preferences to edit
* @throws IOException if there are any errors while reading
*/
@SuppressLint("CommitPrefEdits")
public void importPreferences(DataInputStream reader, SharedPreferences preferences)
throws IOException {
Editor editor = preferences.edit();
@@ -15,6 +15,7 @@
*/
package com.google.android.apps.mytracks.util;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
@@ -94,6 +95,7 @@ public class EulaUtils {
return sharedPreferences.getBoolean(key, defaultValue);
}
@SuppressLint("CommitPrefEdits")
private static void setValue(Context context, String key, boolean value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
EULA_PREFERENCE_FILE, Context.MODE_PRIVATE);
@@ -46,7 +46,7 @@ public class ChartFragmentTest extends AndroidTestCase {
/**
* Tests the logic to get the incorrect values of sensor in
* {@link ChartFragment#fillDataPoint(android.content.Context, Location, double[])}
* {@link ChartFragment#fillDataPoint(Location, double[])}
*/
public void testFillDataPoint_sensorIncorrect() {
MyTracksLocation myTracksLocation = TrackStubUtils.createMyTracksLocation();
@@ -83,7 +83,7 @@ public class ChartFragmentTest extends AndroidTestCase {
/**
* Tests the logic to get the correct values of sensor in
* {@link ChartFragment#fillDataPoint(android.content.Context, Location, double[])}.
* {@link ChartFragment#fillDataPoint(Location, double[])}.
*/
public void testFillDataPoint_sensorCorrect() {
MyTracksLocation myTracksLocation = TrackStubUtils.createMyTracksLocation();
@@ -118,7 +118,7 @@ public class ChartFragmentTest extends AndroidTestCase {
/**
* Tests the logic to get the value of metric Distance in
* {@link ChartFragment#fillDataPoint(android.content.Context, Location, double[])}.
* {@link ChartFragment#fillDataPoint(Location, double[])}.
*/
public void testFillDataPoint_distanceMetric() {
// By distance.
@@ -159,7 +159,7 @@ public class ChartFragmentTest extends AndroidTestCase {
/**
* Tests the logic to get the value of imperial Distance in
* {@link ChartFragment#fillDataPoint(android.content.Context, Location, double[])}.
* {@link ChartFragment#fillDataPoint(Location, double[])}.
*/
public void testFillDataPoint_distanceImperial() {
// By distance.
@@ -204,7 +204,7 @@ public class ChartFragmentTest extends AndroidTestCase {
/**
* Tests the logic to get the values of time in
* {@link ChartFragment#fillDataPoint(android.content.Context, Location, double[])}.
* {@link ChartFragment#fillDataPoint(Location, double[])}.
*/
public void testFillDataPoint_time() {
// By time
@@ -220,9 +220,9 @@ public class ChartFragmentTest extends AndroidTestCase {
}
/**
* Tests the logic to get the value of elevation in
* {@link ChartFragment#fillDataPoint(android.content.Context,
* Location, double[])} by one and two points.
* Tests the logic to get the value of elevation in
* {@link ChartFragment#fillDataPoint(Location, double[])} by one and two
* points.
*/
public void testFillDataPoint_elevation() {
MyTracksLocation myTracksLocation1 = TrackStubUtils.createMyTracksLocation();
@@ -247,10 +247,9 @@ public class ChartFragmentTest extends AndroidTestCase {
/**
* Tests the logic to get the value of speed in
* {@link ChartFragment#fillDataPoint(android.content.Context, Location, double[])}. In this test,
* firstly remove all points in memory,
* and then fill in two points one by one. The speed values of these points
* are 129, 130.
* {@link ChartFragment#fillDataPoint(Location, double[])}. In this test,
* firstly remove all points in memory, and then fill in two points one by
* one. The speed values of these points are 129, 130.
*/
public void testFillDataPoint_speed() {
/*