summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/android/photos/data/PhotoProvider.java55
-rw-r--r--tests/src/com/android/photos/data/PhotoProviderTest.java18
2 files changed, 54 insertions, 19 deletions
diff --git a/src/com/android/photos/data/PhotoProvider.java b/src/com/android/photos/data/PhotoProvider.java
index a4a29c3..a4fe8fd 100644
--- a/src/com/android/photos/data/PhotoProvider.java
+++ b/src/com/android/photos/data/PhotoProvider.java
@@ -16,6 +16,7 @@
package com.android.photos.data;
import android.content.ContentProvider;
+import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
@@ -305,9 +306,6 @@ public class PhotoProvider extends ContentProvider {
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int match = matchUri(uri);
- if (match == MATCH_IMAGE) {
- throw new IllegalArgumentException("Cannot delete from image cache");
- }
selection = addIdToSelection(match, selection);
selectionArgs = addIdToSelectionArgs(match, uri, selectionArgs);
List<Uri> changeUris = new ArrayList<Uri>();
@@ -339,8 +337,24 @@ public class PhotoProvider extends ContentProvider {
@Override
public Uri insert(Uri uri, ContentValues values) {
- // Cannot insert into this ContentProvider
- return null;
+ int match = matchUri(uri);
+ validateMatchTable(match);
+ String table = getTableFromMatch(match, uri);
+ SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+ Uri insertedUri = null;
+ db.beginTransaction();
+ try {
+ long id = db.insert(table, null, values);
+ if (id != -1) {
+ // uri already matches the table.
+ insertedUri = ContentUris.withAppendedId(uri, id);
+ }
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ notifyChanges(insertedUri);
+ return insertedUri;
}
@Override
@@ -469,22 +483,15 @@ public class PhotoProvider extends ContentProvider {
}
private int modifyMetadata(SQLiteDatabase db, ContentValues values) {
- String[] selectionArgs = {
- values.getAsString(Metadata.PHOTO_ID),
- values.getAsString(Metadata.KEY),
- };
int rowCount;
if (values.get(Metadata.VALUE) == null) {
+ String[] selectionArgs = {
+ values.getAsString(Metadata.PHOTO_ID), values.getAsString(Metadata.KEY),
+ };
rowCount = db.delete(Metadata.TABLE, WHERE_METADATA_ID, selectionArgs);
} else {
- rowCount = (int) DatabaseUtils.queryNumEntries(db, Metadata.TABLE, WHERE_METADATA_ID,
- selectionArgs);
- if (rowCount > 0) {
- db.update(Metadata.TABLE, values, WHERE_METADATA_ID, selectionArgs);
- } else {
- db.insert(Metadata.TABLE, null, values);
- rowCount = 1;
- }
+ long rowId = db.replace(Metadata.TABLE, null, values);
+ rowCount = (rowId == -1) ? 0 : 1;
}
return rowCount;
}
@@ -494,6 +501,9 @@ public class PhotoProvider extends ContentProvider {
if (match == UriMatcher.NO_MATCH) {
throw unknownUri(uri);
}
+ if (match == MATCH_IMAGE || match == MATCH_ALBUM_COVER) {
+ throw new IllegalArgumentException("Operation not allowed on image cache database");
+ }
return match;
}
@@ -555,4 +565,15 @@ public class PhotoProvider extends ContentProvider {
changeUris.add(Metadata.CONTENT_URI);
}
}
+
+ private static void validateMatchTable(int match) {
+ switch (match) {
+ case MATCH_PHOTO:
+ case MATCH_ALBUM:
+ case MATCH_METADATA:
+ break;
+ default:
+ throw new IllegalArgumentException("Operation not allowed on an existing row.");
+ }
+ }
}
diff --git a/tests/src/com/android/photos/data/PhotoProviderTest.java b/tests/src/com/android/photos/data/PhotoProviderTest.java
index 2e644a3..927a9d0 100644
--- a/tests/src/com/android/photos/data/PhotoProviderTest.java
+++ b/tests/src/com/android/photos/data/PhotoProviderTest.java
@@ -188,9 +188,23 @@ public class PhotoProviderTest extends ProviderTestCase2<PhotoProvider> {
public void testInsert() {
ContentValues values = new ContentValues();
- values.put(Albums.NAME, "don't add me");
+ values.put(Albums.NAME, "add me");
values.put(Albums.VISIBILITY, Albums.VISIBILITY_PRIVATE);
- assertNull(mResolver.insert(Albums.CONTENT_URI, values));
+ values.put(Albums.ACCOUNT_ID, 100L);
+ values.put(Albums.DATE_MODIFIED, 100L);
+ values.put(Albums.DATE_PUBLISHED, 100L);
+ values.put(Albums.LOCATION_STRING, "Home");
+ values.put(Albums.NAME, "hello world");
+ values.putNull(Albums.PARENT_ID);
+ values.put(Albums.SUMMARY, "Nothing much to say about this");
+ values.put(Albums.TITLE, "Title");
+ Uri insertedUri = mResolver.insert(Albums.CONTENT_URI, values);
+ assertNotNull(insertedUri);
+ Cursor cursor = mResolver.query(insertedUri, PhotoDatabaseUtils.PROJECTION_ALBUMS, null,
+ null, null);
+ assertNotNull(cursor);
+ assertEquals(1, cursor.getCount());
+ cursor.close();
}
public void testUpdate() {