forked from upstream-mirrors/OpenTracks
Add transaction around insert, delete, and update calls in
MyTracksProvider.java
This commit is contained in:
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package com.google.android.apps.mytracks.content;
|
package com.google.android.apps.mytracks.content;
|
||||||
|
|
||||||
import com.google.android.apps.mytracks.Constants;
|
|
||||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||||
import com.google.android.maps.mytracks.R;
|
import com.google.android.maps.mytracks.R;
|
||||||
|
|
||||||
@@ -38,24 +37,19 @@ import android.text.TextUtils;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A provider that handles recorded (GPS) tracks and their track points.
|
* A {@link ContentProvider} that handles access to track points, tracks, and
|
||||||
|
* waypoints tables.
|
||||||
*
|
*
|
||||||
* @author Leif Hendrik Wilden
|
* @author Leif Hendrik Wilden
|
||||||
*/
|
*/
|
||||||
public class MyTracksProvider extends ContentProvider {
|
public class MyTracksProvider extends ContentProvider {
|
||||||
|
|
||||||
|
private static final String TAG = MyTracksProvider.class.getSimpleName();
|
||||||
private static final String DATABASE_NAME = "mytracks.db";
|
private static final String DATABASE_NAME = "mytracks.db";
|
||||||
private static final int DATABASE_VERSION = 20;
|
private static final int DATABASE_VERSION = 20;
|
||||||
private static final int TRACKPOINTS = 1;
|
|
||||||
private static final int TRACKPOINTS_ID = 2;
|
|
||||||
private static final int TRACKS = 3;
|
|
||||||
private static final int TRACKS_ID = 4;
|
|
||||||
private static final int WAYPOINTS = 5;
|
|
||||||
private static final int WAYPOINTS_ID = 6;
|
|
||||||
private static final String TAG = MyTracksProvider.class.getSimpleName();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper which creates or upgrades the database if necessary.
|
* Database helper for creating and upgrading the databasae.
|
||||||
*/
|
*/
|
||||||
private static class DatabaseHelper extends SQLiteOpenHelper {
|
private static class DatabaseHelper extends SQLiteOpenHelper {
|
||||||
|
|
||||||
@@ -74,28 +68,27 @@ public class MyTracksProvider extends ContentProvider {
|
|||||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
|
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
|
||||||
if (oldVersion < 17) {
|
if (oldVersion < 17) {
|
||||||
// Delete the old data
|
Log.w(TAG, "Deleting all old data.");
|
||||||
Log.w(TAG, "Delete all old data");
|
|
||||||
db.execSQL("DROP TABLE IF EXISTS " + TrackPointsColumns.TABLE_NAME);
|
db.execSQL("DROP TABLE IF EXISTS " + TrackPointsColumns.TABLE_NAME);
|
||||||
db.execSQL("DROP TABLE IF EXISTS " + TracksColumns.TABLE_NAME);
|
db.execSQL("DROP TABLE IF EXISTS " + TracksColumns.TABLE_NAME);
|
||||||
db.execSQL("DROP TABLE IF EXISTS " + WaypointsColumns.TABLE_NAME);
|
db.execSQL("DROP TABLE IF EXISTS " + WaypointsColumns.TABLE_NAME);
|
||||||
onCreate(db);
|
onCreate(db);
|
||||||
} else {
|
} else {
|
||||||
// Incremental updates go here. For each DB version, add a corresponding if clause.
|
// Incremental upgrades. One if statement per DB version.
|
||||||
|
|
||||||
// Track points sensor column
|
// Add track points SENSOR column
|
||||||
if (oldVersion <= 17) {
|
if (oldVersion <= 17) {
|
||||||
Log.w(TAG, "Upgrade DB: Adding sensor column.");
|
Log.w(TAG, "Upgrade DB: Adding sensor column.");
|
||||||
db.execSQL("ALTER TABLE " + TrackPointsColumns.TABLE_NAME + " ADD "
|
db.execSQL("ALTER TABLE " + TrackPointsColumns.TABLE_NAME + " ADD "
|
||||||
+ TrackPointsColumns.SENSOR + " BLOB");
|
+ TrackPointsColumns.SENSOR + " BLOB");
|
||||||
}
|
}
|
||||||
// Tracks table id column
|
// Add tracks TABLEID column
|
||||||
if (oldVersion <= 18) {
|
if (oldVersion <= 18) {
|
||||||
Log.w(TAG, "Upgrade DB: Adding tableid column.");
|
Log.w(TAG, "Upgrade DB: Adding tableid column.");
|
||||||
db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.TABLEID
|
db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.TABLEID
|
||||||
+ " STRING");
|
+ " STRING");
|
||||||
}
|
}
|
||||||
// Tracks table icon column
|
// Add tracks ICON column
|
||||||
if (oldVersion <= 19) {
|
if (oldVersion <= 19) {
|
||||||
Log.w(TAG, "Upgrade DB: Adding icon column.");
|
Log.w(TAG, "Upgrade DB: Adding icon column.");
|
||||||
db.execSQL(
|
db.execSQL(
|
||||||
@@ -105,30 +98,32 @@ public class MyTracksProvider extends ContentProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final UriMatcher urlMatcher;
|
/**
|
||||||
|
* Types of url.
|
||||||
|
*
|
||||||
|
* @author Jimmy Shih
|
||||||
|
*/
|
||||||
|
private enum UrlType {
|
||||||
|
TRACKPOINTS, TRACKPOINTS_ID, TRACKS, TRACKS_ID, WAYPOINTS, WAYPOINTS_ID
|
||||||
|
}
|
||||||
|
|
||||||
|
private final UriMatcher uriMatcher;
|
||||||
private SQLiteDatabase db;
|
private SQLiteDatabase db;
|
||||||
|
|
||||||
public MyTracksProvider() {
|
public MyTracksProvider() {
|
||||||
urlMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
||||||
urlMatcher.addURI(MyTracksProviderUtils.AUTHORITY,
|
uriMatcher.addURI(MyTracksProviderUtils.AUTHORITY, TrackPointsColumns.TABLE_NAME,
|
||||||
"trackpoints", TRACKPOINTS);
|
UrlType.TRACKPOINTS.ordinal());
|
||||||
urlMatcher.addURI(MyTracksProviderUtils.AUTHORITY,
|
uriMatcher.addURI(MyTracksProviderUtils.AUTHORITY, TrackPointsColumns.TABLE_NAME + "/#",
|
||||||
"trackpoints/#", TRACKPOINTS_ID);
|
UrlType.TRACKPOINTS_ID.ordinal());
|
||||||
urlMatcher.addURI(MyTracksProviderUtils.AUTHORITY, "tracks", TRACKS);
|
uriMatcher.addURI(
|
||||||
urlMatcher.addURI(MyTracksProviderUtils.AUTHORITY, "tracks/#", TRACKS_ID);
|
MyTracksProviderUtils.AUTHORITY, TracksColumns.TABLE_NAME, UrlType.TRACKS.ordinal());
|
||||||
urlMatcher.addURI(MyTracksProviderUtils.AUTHORITY, "waypoints", WAYPOINTS);
|
uriMatcher.addURI(MyTracksProviderUtils.AUTHORITY, TracksColumns.TABLE_NAME + "/#",
|
||||||
urlMatcher.addURI(MyTracksProviderUtils.AUTHORITY,
|
UrlType.TRACKS_ID.ordinal());
|
||||||
"waypoints/#", WAYPOINTS_ID);
|
uriMatcher.addURI(
|
||||||
}
|
MyTracksProviderUtils.AUTHORITY, WaypointsColumns.TABLE_NAME, UrlType.WAYPOINTS.ordinal());
|
||||||
|
uriMatcher.addURI(MyTracksProviderUtils.AUTHORITY, WaypointsColumns.TABLE_NAME + "/#",
|
||||||
private boolean canAccess() {
|
UrlType.WAYPOINTS_ID.ordinal());
|
||||||
if (Binder.getCallingPid() == Process.myPid()) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return PreferencesUtils.getBoolean(
|
|
||||||
getContext(), R.string.allow_access_key, PreferencesUtils.ALLOW_ACCESS_DEFAULT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -136,11 +131,11 @@ public class MyTracksProvider extends ContentProvider {
|
|||||||
if (!canAccess()) {
|
if (!canAccess()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
DatabaseHelper dbHelper = new DatabaseHelper(getContext());
|
DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
|
||||||
try {
|
try {
|
||||||
db = dbHelper.getWritableDatabase();
|
db = databaseHelper.getWritableDatabase();
|
||||||
} catch (SQLiteException e) {
|
} catch (SQLiteException e) {
|
||||||
Log.e(TAG, "Unable to open database for writing", e);
|
Log.e(TAG, "Unable to open database for writing.", e);
|
||||||
}
|
}
|
||||||
return db != null;
|
return db != null;
|
||||||
}
|
}
|
||||||
@@ -152,7 +147,7 @@ public class MyTracksProvider extends ContentProvider {
|
|||||||
}
|
}
|
||||||
String table;
|
String table;
|
||||||
boolean shouldVacuum = false;
|
boolean shouldVacuum = false;
|
||||||
switch (urlMatcher.match(url)) {
|
switch (getUrlType(url)) {
|
||||||
case TRACKPOINTS:
|
case TRACKPOINTS:
|
||||||
table = TrackPointsColumns.TABLE_NAME;
|
table = TrackPointsColumns.TABLE_NAME;
|
||||||
break;
|
break;
|
||||||
@@ -167,16 +162,22 @@ public class MyTracksProvider extends ContentProvider {
|
|||||||
throw new IllegalArgumentException("Unknown URL " + url);
|
throw new IllegalArgumentException("Unknown URL " + url);
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.w(MyTracksProvider.TAG, "provider delete in " + table + "!");
|
Log.w(MyTracksProvider.TAG, "Deleting table " + table);
|
||||||
int count = db.delete(table, where, selectionArgs);
|
int count;
|
||||||
|
try {
|
||||||
|
db.beginTransaction();
|
||||||
|
count = db.delete(table, where, selectionArgs);
|
||||||
|
db.setTransactionSuccessful();
|
||||||
|
} finally {
|
||||||
|
db.endTransaction();
|
||||||
|
}
|
||||||
getContext().getContentResolver().notifyChange(url, null, true);
|
getContext().getContentResolver().notifyChange(url, null, true);
|
||||||
|
|
||||||
if (shouldVacuum) {
|
if (shouldVacuum) {
|
||||||
// If a potentially large amount of data was deleted, we want to reclaim its space.
|
// If a potentially large amount of data was deleted, reclaim its space.
|
||||||
Log.i(TAG, "Vacuuming the database");
|
Log.i(TAG, "Vacuuming the database.");
|
||||||
db.execSQL("VACUUM");
|
db.execSQL("VACUUM");
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,7 +186,7 @@ public class MyTracksProvider extends ContentProvider {
|
|||||||
if (!canAccess()) {
|
if (!canAccess()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
switch (urlMatcher.match(url)) {
|
switch (getUrlType(url)) {
|
||||||
case TRACKPOINTS:
|
case TRACKPOINTS:
|
||||||
return TrackPointsColumns.CONTENT_TYPE;
|
return TrackPointsColumns.CONTENT_TYPE;
|
||||||
case TRACKPOINTS_ID:
|
case TRACKPOINTS_ID:
|
||||||
@@ -208,197 +209,240 @@ public class MyTracksProvider extends ContentProvider {
|
|||||||
if (!canAccess()) {
|
if (!canAccess()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Log.d(MyTracksProvider.TAG, "MyTracksProvider.insert");
|
if (initialValues == null) {
|
||||||
ContentValues values;
|
initialValues = new ContentValues();
|
||||||
if (initialValues != null) {
|
|
||||||
values = initialValues;
|
|
||||||
} else {
|
|
||||||
values = new ContentValues();
|
|
||||||
}
|
}
|
||||||
|
Uri result = null;
|
||||||
int urlMatchType = urlMatcher.match(url);
|
try {
|
||||||
return insertType(url, urlMatchType, values);
|
db.beginTransaction();
|
||||||
}
|
result = insertContentValues(url, getUrlType(url), initialValues);
|
||||||
|
db.setTransactionSuccessful();
|
||||||
private Uri insertType(Uri url, int urlMatchType, ContentValues values) {
|
} finally {
|
||||||
switch (urlMatchType) {
|
db.endTransaction();
|
||||||
case TRACKPOINTS:
|
|
||||||
return insertTrackPoint(url, values);
|
|
||||||
case TRACKS:
|
|
||||||
return insertTrack(url, values);
|
|
||||||
case WAYPOINTS:
|
|
||||||
return insertWaypoint(url, values);
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URL " + url);
|
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int bulkInsert(Uri url, ContentValues[] valuesBulk) {
|
public int bulkInsert(Uri url, ContentValues[] valuesBulk) {
|
||||||
if (!canAccess()) {
|
if (!canAccess()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Log.d(MyTracksProvider.TAG, "MyTracksProvider.bulkInsert");
|
|
||||||
int numInserted = 0;
|
int numInserted = 0;
|
||||||
try {
|
try {
|
||||||
// Use a transaction in order to make the insertions run as a single batch
|
// Use a transaction in order to make the insertions run as a single batch
|
||||||
db.beginTransaction();
|
db.beginTransaction();
|
||||||
|
|
||||||
int urlMatch = urlMatcher.match(url);
|
UrlType urlType = getUrlType(url);
|
||||||
for (numInserted = 0; numInserted < valuesBulk.length; numInserted++) {
|
for (numInserted = 0; numInserted < valuesBulk.length; numInserted++) {
|
||||||
ContentValues values = valuesBulk[numInserted];
|
ContentValues contentValues = valuesBulk[numInserted];
|
||||||
if (values == null) { values = new ContentValues(); }
|
if (contentValues == null) {
|
||||||
|
contentValues = new ContentValues();
|
||||||
insertType(url, urlMatch, values);
|
}
|
||||||
|
insertContentValues(url, urlType, contentValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
db.setTransactionSuccessful();
|
db.setTransactionSuccessful();
|
||||||
} finally {
|
} finally {
|
||||||
db.endTransaction();
|
db.endTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
return numInserted;
|
return numInserted;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Uri insertTrackPoint(Uri url, ContentValues values) {
|
|
||||||
boolean hasLat = values.containsKey(TrackPointsColumns.LATITUDE);
|
|
||||||
boolean hasLong = values.containsKey(TrackPointsColumns.LONGITUDE);
|
|
||||||
boolean hasTime = values.containsKey(TrackPointsColumns.TIME);
|
|
||||||
if (!hasLat || !hasLong || !hasTime) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"Latitude, longitude, and time values are required.");
|
|
||||||
}
|
|
||||||
long rowId = db.insert(TrackPointsColumns.TABLE_NAME, TrackPointsColumns._ID, values);
|
|
||||||
if (rowId >= 0) {
|
|
||||||
Uri uri = ContentUris.appendId(
|
|
||||||
TrackPointsColumns.CONTENT_URI.buildUpon(), rowId).build();
|
|
||||||
getContext().getContentResolver().notifyChange(url, null, true);
|
|
||||||
return uri;
|
|
||||||
}
|
|
||||||
throw new SQLiteException("Failed to insert row into " + url);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Uri insertTrack(Uri url, ContentValues values) {
|
|
||||||
boolean hasStartTime = values.containsKey(TracksColumns.STARTTIME);
|
|
||||||
boolean hasStartId = values.containsKey(TracksColumns.STARTID);
|
|
||||||
if (!hasStartTime || !hasStartId) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"Both start time and start id values are required.");
|
|
||||||
}
|
|
||||||
long rowId = db.insert(TracksColumns.TABLE_NAME, TracksColumns._ID, values);
|
|
||||||
if (rowId > 0) {
|
|
||||||
Uri uri = ContentUris.appendId(
|
|
||||||
TracksColumns.CONTENT_URI.buildUpon(), rowId).build();
|
|
||||||
getContext().getContentResolver().notifyChange(url, null, true);
|
|
||||||
return uri;
|
|
||||||
}
|
|
||||||
throw new SQLException("Failed to insert row into " + url);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Uri insertWaypoint(Uri url, ContentValues values) {
|
|
||||||
long rowId = db.insert(WaypointsColumns.TABLE_NAME, WaypointsColumns._ID, values);
|
|
||||||
if (rowId > 0) {
|
|
||||||
Uri uri = ContentUris.appendId(
|
|
||||||
WaypointsColumns.CONTENT_URI.buildUpon(), rowId).build();
|
|
||||||
getContext().getContentResolver().notifyChange(url, null, true);
|
|
||||||
return uri;
|
|
||||||
}
|
|
||||||
throw new SQLException("Failed to insert row into " + url);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Cursor query(
|
public Cursor query(
|
||||||
Uri url, String[] projection, String selection, String[] selectionArgs,
|
Uri url, String[] projection, String selection, String[] selectionArgs, String sort) {
|
||||||
String sort) {
|
|
||||||
if (!canAccess()) {
|
if (!canAccess()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
|
||||||
int match = urlMatcher.match(url);
|
|
||||||
String sortOrder = null;
|
String sortOrder = null;
|
||||||
if (match == TRACKPOINTS) {
|
switch (getUrlType(url)) {
|
||||||
qb.setTables(TrackPointsColumns.TABLE_NAME);
|
case TRACKPOINTS:
|
||||||
if (sort != null) {
|
queryBuilder.setTables(TrackPointsColumns.TABLE_NAME);
|
||||||
sortOrder = sort;
|
sortOrder = sort != null ? sort : TrackPointsColumns.DEFAULT_SORT_ORDER;
|
||||||
} else {
|
break;
|
||||||
sortOrder = TrackPointsColumns.DEFAULT_SORT_ORDER;
|
case TRACKPOINTS_ID:
|
||||||
}
|
queryBuilder.setTables(TrackPointsColumns.TABLE_NAME);
|
||||||
} else if (match == TRACKPOINTS_ID) {
|
queryBuilder.appendWhere("_id=" + url.getPathSegments().get(1));
|
||||||
qb.setTables(TrackPointsColumns.TABLE_NAME);
|
break;
|
||||||
qb.appendWhere("_id=" + url.getPathSegments().get(1));
|
case TRACKS:
|
||||||
} else if (match == TRACKS) {
|
queryBuilder.setTables(TracksColumns.TABLE_NAME);
|
||||||
qb.setTables(TracksColumns.TABLE_NAME);
|
sortOrder = sort != null ? sort : TracksColumns.DEFAULT_SORT_ORDER;
|
||||||
if (sort != null) {
|
break;
|
||||||
sortOrder = sort;
|
case TRACKS_ID:
|
||||||
} else {
|
queryBuilder.setTables(TracksColumns.TABLE_NAME);
|
||||||
sortOrder = TracksColumns.DEFAULT_SORT_ORDER;
|
queryBuilder.appendWhere("_id=" + url.getPathSegments().get(1));
|
||||||
}
|
break;
|
||||||
} else if (match == TRACKS_ID) {
|
case WAYPOINTS:
|
||||||
qb.setTables(TracksColumns.TABLE_NAME);
|
queryBuilder.setTables(WaypointsColumns.TABLE_NAME);
|
||||||
qb.appendWhere("_id=" + url.getPathSegments().get(1));
|
sortOrder = sort != null ? sort : WaypointsColumns.DEFAULT_SORT_ORDER;
|
||||||
} else if (match == WAYPOINTS) {
|
break;
|
||||||
qb.setTables(WaypointsColumns.TABLE_NAME);
|
case WAYPOINTS_ID:
|
||||||
if (sort != null) {
|
queryBuilder.setTables(WaypointsColumns.TABLE_NAME);
|
||||||
sortOrder = sort;
|
queryBuilder.appendWhere("_id=" + url.getPathSegments().get(1));
|
||||||
} else {
|
break;
|
||||||
sortOrder = WaypointsColumns.DEFAULT_SORT_ORDER;
|
default:
|
||||||
}
|
throw new IllegalArgumentException("Unknown url " + url);
|
||||||
} else if (match == WAYPOINTS_ID) {
|
|
||||||
qb.setTables(WaypointsColumns.TABLE_NAME);
|
|
||||||
qb.appendWhere("_id=" + url.getPathSegments().get(1));
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("Unknown URL " + url);
|
|
||||||
}
|
}
|
||||||
Log.i(Constants.TAG, "Build query: "
|
Cursor cursor = queryBuilder.query(
|
||||||
+ qb.buildQuery(projection, selection, selectionArgs, null, null, sortOrder, null));
|
db, projection, selection, selectionArgs, null, null, sortOrder);
|
||||||
|
cursor.setNotificationUri(getContext().getContentResolver(), url);
|
||||||
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null,
|
return cursor;
|
||||||
sortOrder);
|
|
||||||
c.setNotificationUri(getContext().getContentResolver(), url);
|
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int update(Uri url, ContentValues values, String where,
|
public int update(Uri url, ContentValues values, String where, String[] selectionArgs) {
|
||||||
String[] selectionArgs) {
|
|
||||||
if (!canAccess()) {
|
if (!canAccess()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
String table;
|
||||||
|
String whereClause;
|
||||||
|
switch (getUrlType(url)) {
|
||||||
|
case TRACKPOINTS:
|
||||||
|
table = TrackPointsColumns.TABLE_NAME;
|
||||||
|
whereClause = where;
|
||||||
|
break;
|
||||||
|
case TRACKPOINTS_ID:
|
||||||
|
table = TrackPointsColumns.TABLE_NAME;
|
||||||
|
whereClause = TrackPointsColumns._ID + "=" + url.getPathSegments().get(1);
|
||||||
|
if (!TextUtils.isEmpty(where)) {
|
||||||
|
whereClause += " AND (" + where + ")";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case TRACKS:
|
||||||
|
table = TracksColumns.TABLE_NAME;
|
||||||
|
whereClause = where;
|
||||||
|
break;
|
||||||
|
case TRACKS_ID:
|
||||||
|
table = TracksColumns.TABLE_NAME;
|
||||||
|
whereClause = TracksColumns._ID + "=" + url.getPathSegments().get(1);
|
||||||
|
if (!TextUtils.isEmpty(where)) {
|
||||||
|
whereClause += " AND (" + where + ")";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case WAYPOINTS:
|
||||||
|
table = WaypointsColumns.TABLE_NAME;
|
||||||
|
whereClause = where;
|
||||||
|
break;
|
||||||
|
case WAYPOINTS_ID:
|
||||||
|
table = WaypointsColumns.TABLE_NAME;
|
||||||
|
whereClause = WaypointsColumns._ID + "=" + url.getPathSegments().get(1);
|
||||||
|
if (!TextUtils.isEmpty(where)) {
|
||||||
|
whereClause += " AND (" + where + ")";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unknown url " + url);
|
||||||
|
}
|
||||||
int count;
|
int count;
|
||||||
int match = urlMatcher.match(url);
|
try {
|
||||||
if (match == TRACKPOINTS) {
|
db.beginTransaction();
|
||||||
count = db.update(TrackPointsColumns.TABLE_NAME, values, where, selectionArgs);
|
count = db.update(table, values, whereClause, selectionArgs);
|
||||||
} else if (match == TRACKPOINTS_ID) {
|
db.setTransactionSuccessful();
|
||||||
String segment = url.getPathSegments().get(1);
|
} finally {
|
||||||
count = db.update(TrackPointsColumns.TABLE_NAME, values, "_id=" + segment
|
db.endTransaction();
|
||||||
+ (!TextUtils.isEmpty(where)
|
|
||||||
? " AND (" + where + ')'
|
|
||||||
: ""),
|
|
||||||
selectionArgs);
|
|
||||||
} else if (match == TRACKS) {
|
|
||||||
count = db.update(TracksColumns.TABLE_NAME, values, where, selectionArgs);
|
|
||||||
} else if (match == TRACKS_ID) {
|
|
||||||
String segment = url.getPathSegments().get(1);
|
|
||||||
count = db.update(TracksColumns.TABLE_NAME, values, "_id=" + segment
|
|
||||||
+ (!TextUtils.isEmpty(where)
|
|
||||||
? " AND (" + where + ')'
|
|
||||||
: ""),
|
|
||||||
selectionArgs);
|
|
||||||
} else if (match == WAYPOINTS) {
|
|
||||||
count = db.update(WaypointsColumns.TABLE_NAME, values, where, selectionArgs);
|
|
||||||
} else if (match == WAYPOINTS_ID) {
|
|
||||||
String segment = url.getPathSegments().get(1);
|
|
||||||
count = db.update(WaypointsColumns.TABLE_NAME, values, "_id=" + segment
|
|
||||||
+ (!TextUtils.isEmpty(where)
|
|
||||||
? " AND (" + where + ')'
|
|
||||||
: ""),
|
|
||||||
selectionArgs);
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("Unknown URL " + url);
|
|
||||||
}
|
}
|
||||||
getContext().getContentResolver().notifyChange(url, null, true);
|
getContext().getContentResolver().notifyChange(url, null, true);
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the caller can access the content provider.
|
||||||
|
*/
|
||||||
|
private boolean canAccess() {
|
||||||
|
if (Binder.getCallingPid() == Process.myPid()) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return PreferencesUtils.getBoolean(
|
||||||
|
getContext(), R.string.allow_access_key, PreferencesUtils.ALLOW_ACCESS_DEFAULT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the {@link UrlType} for a url.
|
||||||
|
*
|
||||||
|
* @param url the url
|
||||||
|
*/
|
||||||
|
private UrlType getUrlType(Uri url) {
|
||||||
|
return UrlType.values()[uriMatcher.match(url)];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a content based on the url type.
|
||||||
|
*
|
||||||
|
* @param url the content url
|
||||||
|
* @param urlType the url type
|
||||||
|
* @param contentValues the content values
|
||||||
|
*/
|
||||||
|
private Uri insertContentValues(Uri url, UrlType urlType, ContentValues contentValues) {
|
||||||
|
switch (urlType) {
|
||||||
|
case TRACKPOINTS:
|
||||||
|
return insertTrackPoint(url, contentValues);
|
||||||
|
case TRACKS:
|
||||||
|
return insertTrack(url, contentValues);
|
||||||
|
case WAYPOINTS:
|
||||||
|
return insertWaypoint(url, contentValues);
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unknown url " + url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a track point.
|
||||||
|
*
|
||||||
|
* @param url the content url
|
||||||
|
* @param values the content values
|
||||||
|
*/
|
||||||
|
private Uri insertTrackPoint(Uri url, ContentValues values) {
|
||||||
|
boolean hasLatitude = values.containsKey(TrackPointsColumns.LATITUDE);
|
||||||
|
boolean hasLongitude = values.containsKey(TrackPointsColumns.LONGITUDE);
|
||||||
|
boolean hasTime = values.containsKey(TrackPointsColumns.TIME);
|
||||||
|
if (!hasLatitude || !hasLongitude || !hasTime) {
|
||||||
|
throw new IllegalArgumentException("Latitude, longitude, and time values are required.");
|
||||||
|
}
|
||||||
|
long rowId = db.insert(TrackPointsColumns.TABLE_NAME, TrackPointsColumns._ID, values);
|
||||||
|
if (rowId >= 0) {
|
||||||
|
Uri uri = ContentUris.appendId(TrackPointsColumns.CONTENT_URI.buildUpon(), rowId).build();
|
||||||
|
getContext().getContentResolver().notifyChange(url, null, true);
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
throw new SQLiteException("Failed to insert a track point " + url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a track.
|
||||||
|
*
|
||||||
|
* @param url the content url
|
||||||
|
* @param contentValues the content values
|
||||||
|
*/
|
||||||
|
private Uri insertTrack(Uri url, ContentValues contentValues) {
|
||||||
|
boolean hasStartTime = contentValues.containsKey(TracksColumns.STARTTIME);
|
||||||
|
boolean hasStartId = contentValues.containsKey(TracksColumns.STARTID);
|
||||||
|
if (!hasStartTime || !hasStartId) {
|
||||||
|
throw new IllegalArgumentException("Both start time and start id values are required.");
|
||||||
|
}
|
||||||
|
long rowId = db.insert(TracksColumns.TABLE_NAME, TracksColumns._ID, contentValues);
|
||||||
|
if (rowId >= 0) {
|
||||||
|
Uri uri = ContentUris.appendId(TracksColumns.CONTENT_URI.buildUpon(), rowId).build();
|
||||||
|
getContext().getContentResolver().notifyChange(url, null, true);
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
throw new SQLException("Failed to insert a track " + url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a waypoint.
|
||||||
|
*
|
||||||
|
* @param url the content url
|
||||||
|
* @param contentValues the content values
|
||||||
|
*/
|
||||||
|
private Uri insertWaypoint(Uri url, ContentValues contentValues) {
|
||||||
|
long rowId = db.insert(WaypointsColumns.TABLE_NAME, WaypointsColumns._ID, contentValues);
|
||||||
|
if (rowId >= 0) {
|
||||||
|
Uri uri = ContentUris.appendId(WaypointsColumns.CONTENT_URI.buildUpon(), rowId).build();
|
||||||
|
getContext().getContentResolver().notifyChange(url, null, true);
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
throw new SQLException("Failed to insert a waypoint " + url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -172,9 +172,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteAllTracks() {
|
public void deleteAllTracks() {
|
||||||
contentResolver.delete(TracksColumns.CONTENT_URI, null, null);
|
|
||||||
contentResolver.delete(TrackPointsColumns.CONTENT_URI, null, null);
|
contentResolver.delete(TrackPointsColumns.CONTENT_URI, null, null);
|
||||||
contentResolver.delete(WaypointsColumns.CONTENT_URI, null, null);
|
contentResolver.delete(WaypointsColumns.CONTENT_URI, null, null);
|
||||||
|
// Delete tracks last since it triggers a database vaccum call
|
||||||
|
contentResolver.delete(TracksColumns.CONTENT_URI, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -188,6 +189,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
|||||||
}
|
}
|
||||||
contentResolver.delete(WaypointsColumns.CONTENT_URI, WaypointsColumns.TRACKID + "=?",
|
contentResolver.delete(WaypointsColumns.CONTENT_URI, WaypointsColumns.TRACKID + "=?",
|
||||||
new String[] { Long.toString(trackId) });
|
new String[] { Long.toString(trackId) });
|
||||||
|
// Delete tracks last since it triggers a database vaccum call
|
||||||
contentResolver.delete(TracksColumns.CONTENT_URI, TracksColumns._ID + "=?", new String[] {
|
contentResolver.delete(TracksColumns.CONTENT_URI, TracksColumns._ID + "=?", new String[] {
|
||||||
Long.toString(trackId) });
|
Long.toString(trackId) });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user