mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-04 10:23:06 +02:00
Protect MyTracksProvider based on the MyTracks settings bit.
Move MyTracksProvider to DatabaseProvider and protect the DatabaseProvider using the AndroidManifest.xml. Create a MyTracksProviderUtilFactory to create an instance of MyTracksProviderUtils that accesses the DatabaseProvider. Recreate MyTracksProvider by extending DatabaseProvider. The recreated provider checks access, changes the authority, and invokes the DatabaseProvider methods.
This commit is contained in:
@@ -36,6 +36,13 @@ public interface MyTracksProviderUtils {
|
||||
*/
|
||||
public static final String AUTHORITY = "com.google.android.maps.mytracks";
|
||||
|
||||
/**
|
||||
* Authority for the Database content provider. The Database content provider
|
||||
* is configured, through the MyTracks AndroidManifest.xml, to only allow the
|
||||
* MyTracks app access.
|
||||
*/
|
||||
public static final String DATABASE_AUTHORITY = "com.google.android.apps.mytracks.content";
|
||||
|
||||
/**
|
||||
* Deletes all tracks (including track points) from the provider.
|
||||
*/
|
||||
@@ -437,7 +444,7 @@ public interface MyTracksProviderUtils {
|
||||
* Creates an instance of {@link MyTracksProviderUtils}.
|
||||
*/
|
||||
protected MyTracksProviderUtils newForContext(Context context) {
|
||||
return new MyTracksProviderUtilsImpl(context.getContentResolver());
|
||||
return new MyTracksProviderUtilsImpl(context.getContentResolver(), AUTHORITY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+57
-45
@@ -16,7 +16,7 @@
|
||||
package com.google.android.apps.mytracks.content;
|
||||
|
||||
import static com.google.android.apps.mytracks.lib.MyTracksLibConstants.TAG;
|
||||
import com.google.android.apps.mytracks.content.Sensor;
|
||||
|
||||
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
|
||||
@@ -40,13 +40,39 @@ import java.util.NoSuchElementException;
|
||||
public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
|
||||
private final ContentResolver contentResolver;
|
||||
private final String authority; // authority of the content provider
|
||||
|
||||
private int defaultCursorBatchSize = 2000;
|
||||
|
||||
public MyTracksProviderUtilsImpl(ContentResolver contentResolver) {
|
||||
public MyTracksProviderUtilsImpl(ContentResolver contentResolver, String authority) {
|
||||
this.contentResolver = contentResolver;
|
||||
this.authority = authority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tracks table URI.
|
||||
*/
|
||||
private Uri getTracksUri() {
|
||||
return authority.equals(DATABASE_AUTHORITY) ? TracksColumns.DATABASE_CONTENT_URI
|
||||
: TracksColumns.CONTENT_URI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the track points table URI.
|
||||
*/
|
||||
private Uri getTrackPointsUri() {
|
||||
return authority.equals(DATABASE_AUTHORITY) ? TrackPointsColumns.DATABASE_CONTENT_URI
|
||||
: TrackPointsColumns.CONTENT_URI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the waypoints table URI.
|
||||
*/
|
||||
private Uri getWaypointsUri() {
|
||||
return authority.equals(DATABASE_AUTHORITY) ? WaypointsColumns.DATABASE_CONTENT_URI
|
||||
: WaypointsColumns.CONTENT_URI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the ContentValues for a given location object.
|
||||
*
|
||||
@@ -518,24 +544,20 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
|
||||
@Override
|
||||
public void deleteAllTracks() {
|
||||
contentResolver.delete(TracksColumns.CONTENT_URI, null, null);
|
||||
contentResolver.delete(TrackPointsColumns.CONTENT_URI,
|
||||
null, null);
|
||||
contentResolver.delete(WaypointsColumns.CONTENT_URI, null, null);
|
||||
contentResolver.delete(getTracksUri(), null, null);
|
||||
contentResolver.delete(getTrackPointsUri(), null, null);
|
||||
contentResolver.delete(getWaypointsUri(), null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteTrack(long trackId) {
|
||||
Track track = getTrack(trackId);
|
||||
if (track != null) {
|
||||
contentResolver.delete(TrackPointsColumns.CONTENT_URI,
|
||||
"_id>=" + track.getStartId() + " AND _id<=" + track.getStopId(),
|
||||
null);
|
||||
contentResolver.delete(getTrackPointsUri(),
|
||||
"_id>=" + track.getStartId() + " AND _id<=" + track.getStopId(), null);
|
||||
}
|
||||
contentResolver.delete(WaypointsColumns.CONTENT_URI,
|
||||
WaypointsColumns.TRACKID + "=" + trackId, null);
|
||||
contentResolver.delete(
|
||||
TracksColumns.CONTENT_URI, "_id=" + trackId, null);
|
||||
contentResolver.delete(getWaypointsUri(), WaypointsColumns.TRACKID + "=" + trackId, null);
|
||||
contentResolver.delete(getTracksUri(), "_id=" + trackId, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -559,8 +581,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
Log.d(TAG, "No statistics marker after the deleted one was found.");
|
||||
}
|
||||
}
|
||||
contentResolver.delete(
|
||||
WaypointsColumns.CONTENT_URI, "_id=" + waypointId, null);
|
||||
contentResolver.delete(getWaypointsUri(), "_id=" + waypointId, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -572,7 +593,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = contentResolver.query(
|
||||
WaypointsColumns.CONTENT_URI,
|
||||
getWaypointsUri(),
|
||||
null /*projection*/,
|
||||
selection,
|
||||
null /*selectionArgs*/,
|
||||
@@ -594,7 +615,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
public boolean updateWaypoint(Waypoint waypoint) {
|
||||
try {
|
||||
final int rows = contentResolver.update(
|
||||
WaypointsColumns.CONTENT_URI,
|
||||
getWaypointsUri(),
|
||||
createContentValues(waypoint),
|
||||
"_id=" + waypoint.getId(),
|
||||
null /*selectionArgs*/);
|
||||
@@ -614,8 +635,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
private Location findLocationBy(String select) {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = contentResolver.query(
|
||||
TrackPointsColumns.CONTENT_URI, null, select, null, null);
|
||||
cursor = contentResolver.query(getTrackPointsUri(), null, select, null, null);
|
||||
if (cursor != null && cursor.moveToNext()) {
|
||||
return createLocation(cursor);
|
||||
}
|
||||
@@ -638,8 +658,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
private Track findTrackBy(String select) {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = contentResolver.query(
|
||||
TracksColumns.CONTENT_URI, null, select, null, null);
|
||||
cursor = contentResolver.query(getTracksUri(), null, select, null, null);
|
||||
if (cursor != null && cursor.moveToNext()) {
|
||||
return createTrack(cursor);
|
||||
}
|
||||
@@ -665,7 +684,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
}
|
||||
|
||||
Cursor cursor = contentResolver.query(
|
||||
WaypointsColumns.CONTENT_URI,
|
||||
getWaypointsUri(),
|
||||
null /*projection*/,
|
||||
"trackid=" + trackId,
|
||||
null /*selectionArgs*/,
|
||||
@@ -691,7 +710,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
}
|
||||
|
||||
Cursor cursor = contentResolver.query(
|
||||
WaypointsColumns.CONTENT_URI,
|
||||
getWaypointsUri(),
|
||||
null /*projection*/,
|
||||
"_id=" + waypointId,
|
||||
null /*selectionArgs*/,
|
||||
@@ -718,7 +737,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
|
||||
final String[] projection = {"_id"};
|
||||
Cursor cursor = contentResolver.query(
|
||||
TrackPointsColumns.CONTENT_URI,
|
||||
getTrackPointsUri(),
|
||||
projection,
|
||||
"_id=(select max(_id) from trackpoints WHERE trackid=" + trackId + ")",
|
||||
null /*selectionArgs*/,
|
||||
@@ -746,7 +765,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
|
||||
final String[] projection = {"_id"};
|
||||
Cursor cursor = contentResolver.query(
|
||||
WaypointsColumns.CONTENT_URI,
|
||||
getWaypointsUri(),
|
||||
projection,
|
||||
"trackid=" + trackId,
|
||||
null /*selectionArgs*/,
|
||||
@@ -774,7 +793,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
|
||||
final String[] projection = {"_id"};
|
||||
Cursor cursor = contentResolver.query(
|
||||
WaypointsColumns.CONTENT_URI,
|
||||
getWaypointsUri(),
|
||||
projection,
|
||||
WaypointsColumns.TRACKID + "=" + trackId,
|
||||
null /*selectionArgs*/,
|
||||
@@ -799,8 +818,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = contentResolver.query(
|
||||
TracksColumns.CONTENT_URI, null, "_id=(select max(_id) from tracks)",
|
||||
null, null);
|
||||
getTracksUri(), null, "_id=(select max(_id) from tracks)", null, null);
|
||||
if (cursor != null && cursor.moveToNext()) {
|
||||
return createTrack(cursor);
|
||||
}
|
||||
@@ -818,8 +836,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
public long getLastTrackId() {
|
||||
String[] proj = { TracksColumns._ID };
|
||||
Cursor cursor = contentResolver.query(
|
||||
TracksColumns.CONTENT_URI, proj, "_id=(select max(_id) from tracks)",
|
||||
null, null);
|
||||
getTracksUri(), proj, "_id=(select max(_id) from tracks)", null, null);
|
||||
if (cursor != null) {
|
||||
try {
|
||||
if (cursor.moveToFirst()) {
|
||||
@@ -864,7 +881,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
sortOrder += " LIMIT " + maxLocations;
|
||||
}
|
||||
|
||||
return contentResolver.query(TrackPointsColumns.CONTENT_URI, null, selection, null, sortOrder);
|
||||
return contentResolver.query(getTrackPointsUri(), null, selection, null, sortOrder);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -889,8 +906,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
sortOrder += " LIMIT " + maxWaypoints;
|
||||
}
|
||||
|
||||
return contentResolver.query(
|
||||
WaypointsColumns.CONTENT_URI, null, selection, null, sortOrder);
|
||||
return contentResolver.query(getWaypointsUri(), null, selection, null, sortOrder);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -924,23 +940,20 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
|
||||
@Override
|
||||
public Cursor getTracksCursor(String selection) {
|
||||
Cursor cursor = contentResolver.query(
|
||||
TracksColumns.CONTENT_URI, null, selection, null, "_id");
|
||||
Cursor cursor = contentResolver.query(getTracksUri(), null, selection, null, "_id");
|
||||
return cursor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uri insertTrack(Track track) {
|
||||
Log.d(TAG, "MyTracksProviderUtilsImpl.insertTrack");
|
||||
return contentResolver.insert(TracksColumns.CONTENT_URI,
|
||||
createContentValues(track));
|
||||
return contentResolver.insert(getTracksUri(), createContentValues(track));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uri insertTrackPoint(Location location, long trackId) {
|
||||
Log.d(TAG, "MyTracksProviderUtilsImpl.insertTrackPoint");
|
||||
return contentResolver.insert(TrackPointsColumns.CONTENT_URI,
|
||||
createContentValues(location, trackId));
|
||||
return contentResolver.insert(getTrackPointsUri(), createContentValues(location, trackId));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -952,15 +965,14 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
values[i] = createContentValues(locations[i], trackId);
|
||||
}
|
||||
|
||||
return contentResolver.bulkInsert(TrackPointsColumns.CONTENT_URI, values);
|
||||
return contentResolver.bulkInsert(getTrackPointsUri(), values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uri insertWaypoint(Waypoint waypoint) {
|
||||
Log.d(TAG, "MyTracksProviderUtilsImpl.insertWaypoint");
|
||||
waypoint.setId(-1);
|
||||
return contentResolver.insert(WaypointsColumns.CONTENT_URI,
|
||||
createContentValues(waypoint));
|
||||
return contentResolver.insert(getWaypointsUri(), createContentValues(waypoint));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -973,7 +985,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
try {
|
||||
final String[] projection = { TracksColumns._ID };
|
||||
cursor = contentResolver.query(
|
||||
TracksColumns.CONTENT_URI,
|
||||
getTracksUri(),
|
||||
projection,
|
||||
TracksColumns._ID + "=" + id/*selection*/,
|
||||
null/*selectionArgs*/,
|
||||
@@ -992,8 +1004,8 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
||||
@Override
|
||||
public void updateTrack(Track track) {
|
||||
Log.d(TAG, "MyTracksProviderUtilsImpl.updateTrack");
|
||||
contentResolver.update(TracksColumns.CONTENT_URI,
|
||||
createContentValues(track), "_id=" + track.getId(), null);
|
||||
contentResolver.update(
|
||||
getTracksUri(), createContentValues(track), "_id=" + track.getId(), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -25,8 +25,14 @@ import android.provider.BaseColumns;
|
||||
* @author Leif Hendrik Wilden
|
||||
*/
|
||||
public interface TrackPointsColumns extends BaseColumns {
|
||||
public static final Uri CONTENT_URI =
|
||||
Uri.parse("content://com.google.android.maps.mytracks/trackpoints");
|
||||
|
||||
// MyTracks content provider URI for the track points table.
|
||||
public static final Uri CONTENT_URI = Uri.parse(
|
||||
"content://" + MyTracksProviderUtils.AUTHORITY + "/trackpoints");
|
||||
|
||||
// Database content provider URI for the track points table.
|
||||
public static final Uri DATABASE_CONTENT_URI = Uri.parse(
|
||||
"content://" + MyTracksProviderUtils.DATABASE_AUTHORITY + "/trackpoints");
|
||||
public static final String CONTENT_TYPE =
|
||||
"vnd.android.cursor.dir/vnd.google.trackpoint";
|
||||
public static final String CONTENT_ITEMTYPE =
|
||||
|
||||
@@ -26,8 +26,13 @@ import android.provider.BaseColumns;
|
||||
*/
|
||||
public interface TracksColumns extends BaseColumns {
|
||||
|
||||
public static final Uri CONTENT_URI =
|
||||
Uri.parse("content://com.google.android.maps.mytracks/tracks");
|
||||
// MyTracks content provider URI for the tracks table.
|
||||
public static final Uri CONTENT_URI = Uri.parse(
|
||||
"content://" + MyTracksProviderUtils.AUTHORITY + "/tracks");
|
||||
|
||||
// Database content provider URI for the tracks table.
|
||||
public static final Uri DATABASE_CONTENT_URI = Uri.parse(
|
||||
"content://" + MyTracksProviderUtils.DATABASE_AUTHORITY + "/tracks");
|
||||
public static final String CONTENT_TYPE =
|
||||
"vnd.android.cursor.dir/vnd.google.track";
|
||||
public static final String CONTENT_ITEMTYPE =
|
||||
|
||||
@@ -26,8 +26,13 @@ import android.provider.BaseColumns;
|
||||
*/
|
||||
public interface WaypointsColumns extends BaseColumns {
|
||||
|
||||
public static final Uri CONTENT_URI =
|
||||
Uri.parse("content://com.google.android.maps.mytracks/waypoints");
|
||||
// MyTracks content provider URI for the waypoints table.
|
||||
public static final Uri CONTENT_URI = Uri.parse(
|
||||
"content://" + MyTracksProviderUtils.AUTHORITY + "/waypoints");
|
||||
|
||||
// Database content provider URI for the waypoints table.
|
||||
public static final Uri DATABASE_CONTENT_URI = Uri.parse(
|
||||
"content://" + MyTracksProviderUtils.DATABASE_AUTHORITY + "/waypoints");
|
||||
public static final String CONTENT_TYPE =
|
||||
"vnd.android.cursor.dir/vnd.google.waypoint";
|
||||
public static final String CONTENT_ITEMTYPE =
|
||||
|
||||
Reference in New Issue
Block a user