SharedContentProvider now uses parent ContentProvider directly instead of ContentProviderUtils.

This commit is contained in:
Dennis Guse
2020-10-27 21:51:49 +01:00
parent 68f34d9682
commit 2b280f050c
4 changed files with 16 additions and 86 deletions
@@ -1,43 +0,0 @@
package de.dennisguse.opentracks.android;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Wraps a {@link ContentResolver} and provides access as {@link IContentResolver}.
*/
public class ContentResolverWrapper implements IContentResolver {
private final ContentResolver contentResolver;
public ContentResolverWrapper(ContentResolver contentResolver) {
this.contentResolver = contentResolver;
}
@Nullable
public Uri insert(@NonNull Uri url, @Nullable ContentValues values) {
return contentResolver.insert(url, values);
}
public int bulkInsert(@NonNull Uri url, @NonNull ContentValues[] values) {
return contentResolver.bulkInsert(url, values);
}
@Nullable
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
return contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
}
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String where, @Nullable String[] selectionArgs) {
return contentResolver.update(uri, values, where, selectionArgs);
}
public int delete(@NonNull Uri url, @Nullable String where, @Nullable String[] selectionArgs) {
return contentResolver.delete(url, where, selectionArgs);
}
}
@@ -1,27 +0,0 @@
package de.dennisguse.opentracks.android;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresPermission;
/**
* Defines the interface actually shared by {@link android.content.ContentProvider} and {@link android.content.ContentResolver}.
* So, both can be used interchangeably.
*/
public interface IContentResolver {
@Nullable
Uri insert(@RequiresPermission.Write @NonNull Uri url, @Nullable ContentValues values);
int bulkInsert(@RequiresPermission.Write @NonNull Uri url, @NonNull ContentValues[] values);
@Nullable
Cursor query(@RequiresPermission.Read @NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder);
int update(@RequiresPermission.Write @NonNull Uri uri, @Nullable ContentValues values, @Nullable String where, @Nullable String[] selectionArgs);
int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where, @Nullable String[] selectionArgs);
}
@@ -38,8 +38,6 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import de.dennisguse.opentracks.BuildConfig; import de.dennisguse.opentracks.BuildConfig;
import de.dennisguse.opentracks.android.ContentResolverWrapper;
import de.dennisguse.opentracks.android.IContentResolver;
import de.dennisguse.opentracks.content.data.Marker; import de.dennisguse.opentracks.content.data.Marker;
import de.dennisguse.opentracks.content.data.MarkerColumns; import de.dennisguse.opentracks.content.data.MarkerColumns;
import de.dennisguse.opentracks.content.data.Track; import de.dennisguse.opentracks.content.data.Track;
@@ -53,7 +51,6 @@ import de.dennisguse.opentracks.util.UUIDUtils;
/** /**
* {@link ContentProviderUtils} implementation. * {@link ContentProviderUtils} implementation.
* Allows to use {@link ContentResolver} and {@link android.content.ContentProvider} interchangeably via {@link IContentResolver}.
* *
* @author Leif Hendrik Wilden * @author Leif Hendrik Wilden
*/ */
@@ -73,18 +70,15 @@ public class ContentProviderUtils {
public static final int MAX_LOADED_MARKERS = 10000; public static final int MAX_LOADED_MARKERS = 10000;
private static final String ID_SEPARATOR = ","; private static final String ID_SEPARATOR = ",";
private final IContentResolver contentResolver; private final ContentResolver contentResolver;
private int defaultCursorBatchSize = 2000; private int defaultCursorBatchSize = 2000;
public ContentProviderUtils(Context context) { public ContentProviderUtils(Context context) {
this(context.getContentResolver()); contentResolver = context.getContentResolver();
} }
@VisibleForTesting
public ContentProviderUtils(ContentResolver contentResolver) { public ContentProviderUtils(ContentResolver contentResolver) {
this.contentResolver = new ContentResolverWrapper(contentResolver);
}
public ContentProviderUtils(IContentResolver contentResolver) {
this.contentResolver = contentResolver; this.contentResolver = contentResolver;
} }
@@ -94,6 +88,7 @@ public class ContentProviderUtils {
* *
* @param trackId the track id * @param trackId the track id
*/ */
@Deprecated //TODO Figure out why we need this.
public void clearTrack(Track.Id trackId) { public void clearTrack(Track.Id trackId) {
deleteTrackPointsAndMarkers(trackId); deleteTrackPointsAndMarkers(trackId);
Track track = new Track(); Track track = new Track();
@@ -106,7 +101,7 @@ public class ContentProviderUtils {
* *
* @param cursor the cursor pointing to the track * @param cursor the cursor pointing to the track
*/ */
public Track createTrack(Cursor cursor) { public static Track createTrack(Cursor cursor) {
int idIndex = cursor.getColumnIndexOrThrow(TracksColumns._ID); int idIndex = cursor.getColumnIndexOrThrow(TracksColumns._ID);
int uuidIndex = cursor.getColumnIndexOrThrow(TracksColumns.UUID); int uuidIndex = cursor.getColumnIndexOrThrow(TracksColumns.UUID);
int nameIndex = cursor.getColumnIndexOrThrow(TracksColumns.NAME); int nameIndex = cursor.getColumnIndexOrThrow(TracksColumns.NAME);
@@ -8,6 +8,7 @@ import android.database.MatrixCursor;
import android.net.Uri; import android.net.Uri;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
@@ -22,8 +23,8 @@ import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Stream;
import de.dennisguse.opentracks.android.IContentResolver;
import de.dennisguse.opentracks.content.data.Track; import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.content.data.TracksColumns; import de.dennisguse.opentracks.content.data.TracksColumns;
import de.dennisguse.opentracks.io.file.TrackFileFormat; import de.dennisguse.opentracks.io.file.TrackFileFormat;
@@ -41,7 +42,7 @@ import de.dennisguse.opentracks.io.file.exporter.TrackExporter;
* While handling a request {@link ShareContentProvider} could `grantPermissions()` to the calling app for {@link CustomContentProvider}'s URI. * While handling a request {@link ShareContentProvider} could `grantPermissions()` to the calling app for {@link CustomContentProvider}'s URI.
* However, while handling the request this would allow the calling app to actually contact {@link CustomContentProvider} directly and get access to stored data that should remain private. * However, while handling the request this would allow the calling app to actually contact {@link CustomContentProvider} directly and get access to stored data that should remain private.
*/ */
public class ShareContentProvider extends CustomContentProvider implements IContentResolver { public class ShareContentProvider extends CustomContentProvider {
private static final String[] COLUMNS = {OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE}; private static final String[] COLUMNS = {OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE};
@@ -209,12 +210,16 @@ public class ShareContentProvider extends CustomContentProvider implements ICont
@Nullable @Nullable
@Override @Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException { public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(this);
Set<Track.Id> trackIds = parseURI(uri); Set<Track.Id> trackIds = parseURI(uri);
final ArrayList<Track> tracks = new ArrayList<>(); final ArrayList<Track> tracks = new ArrayList<>();
for (Track.Id trackId : trackIds) { String[] trackIdsString = trackIds.stream().map(Track.Id::toString).toArray(String[]::new);
tracks.add(contentProviderUtils.getTrack(trackId)); String trackIdsPlaceholder = TextUtils.join(",", Stream.of(trackIdsString).map(it -> "?").toArray(String[]::new));
TextUtils.join(",", trackIds);
try (Cursor cursor = super.query(TracksColumns.CONTENT_URI, null, TracksColumns._ID + " IN (" + trackIdsPlaceholder + ")", trackIdsString, TracksColumns._ID)) {
while (cursor.moveToNext()) {
tracks.add(ContentProviderUtils.createTrack(cursor));
}
} }
final TrackExporter trackExporter = getTrackFileFormat(uri).newTrackExporter(getContext()); final TrackExporter trackExporter = getTrackFileFormat(uri).newTrackExporter(getContext());