Skip to content

Commit

Permalink
Fix issue with cleaning up stream item photos.
Browse files Browse the repository at this point in the history
Bug 5250048

Change-Id: Ibb0daef7a089ec851a9201cfd4163173f713f839
  • Loading branch information
Dave Santoro committed Sep 7, 2011
1 parent 4358db8 commit c2714bb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/com/android/providers/contacts/ContactsProvider2.java
Original file line number Diff line number Diff line change
Expand Up @@ -1692,23 +1692,33 @@ protected void cleanupPhotoStore() {
}

// Also query for all social stream item photos.
c = db.query(Tables.STREAM_ITEM_PHOTOS,
c = db.query(Tables.STREAM_ITEM_PHOTOS + " JOIN " + Tables.STREAM_ITEMS
+ " ON " + StreamItemPhotos.STREAM_ITEM_ID + "=" + StreamItemsColumns.CONCRETE_ID
+ " JOIN " + Tables.RAW_CONTACTS
+ " ON " + StreamItems.RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID,
new String[]{
StreamItemPhotos._ID,
StreamItemPhotos.STREAM_ITEM_ID,
StreamItemPhotos.PHOTO_FILE_ID
StreamItemPhotosColumns.CONCRETE_ID,
StreamItemPhotosColumns.CONCRETE_STREAM_ITEM_ID,
StreamItemPhotos.PHOTO_FILE_ID,
RawContacts.ACCOUNT_TYPE,
RawContacts.ACCOUNT_NAME
},
null, null, null, null, null);
Map<Long, Long> photoFileIdToStreamItemPhotoId = Maps.newHashMap();
Map<Long, Long> streamItemPhotoIdToStreamItemId = Maps.newHashMap();
Map<Long, Account> streamItemPhotoIdToAccount = Maps.newHashMap();
try {
while (c.moveToNext()) {
long streamItemPhotoId = c.getLong(0);
long streamItemId = c.getLong(1);
long photoFileId = c.getLong(2);
String accountType = c.getString(3);
String accountName = c.getString(4);
usedPhotoFileIds.add(photoFileId);
photoFileIdToStreamItemPhotoId.put(photoFileId, streamItemPhotoId);
streamItemPhotoIdToStreamItemId.put(streamItemPhotoId, streamItemId);
Account account = new Account(accountName, accountType);
streamItemPhotoIdToAccount.put(photoFileId, account);
}
} finally {
c.close();
Expand All @@ -1734,11 +1744,14 @@ protected void cleanupPhotoStore() {
// item photo.
long streamItemPhotoId = photoFileIdToStreamItemPhotoId.get(missingPhotoId);
long streamItemId = streamItemPhotoIdToStreamItemId.get(streamItemPhotoId);
Account account = streamItemPhotoIdToAccount.get(missingPhotoId);
ops.add(ContentProviderOperation.newDelete(
StreamItems.CONTENT_URI.buildUpon()
.appendPath(String.valueOf(streamItemId))
.appendPath(StreamItems.StreamItemPhotos.CONTENT_DIRECTORY)
.appendPath(String.valueOf(streamItemPhotoId))
.appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
.appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type)
.build()).build());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4790,6 +4790,7 @@ public void testPhotoDimensionLimits() {

public void testPhotoStoreCleanup() throws IOException {
SynchronousContactsProvider2 provider = (SynchronousContactsProvider2) mActor.provider;
PhotoStore photoStore = provider.getPhotoStore();

// Trigger an initial cleanup so another one won't happen while we're running this test.
provider.cleanupPhotoStore();
Expand Down Expand Up @@ -4833,8 +4834,20 @@ public void testPhotoStoreCleanup() throws IOException {
values.put(DataRowHandlerForPhoto.SKIP_PROCESSING_KEY, true);
mResolver.insert(Data.CONTENT_URI, values);

// Insert a fourth raw contact with a stream item that has a photo, then remove that photo
// from the photo store.
Account socialAccount = new Account("social", "social");
long rawContactId4 = createRawContactWithName(socialAccount);
Uri streamItemUri =
insertStreamItem(rawContactId4, buildGenericStreamItemValues(), socialAccount);
long streamItemId = ContentUris.parseId(streamItemUri);
Uri streamItemPhotoUri = insertStreamItemPhoto(
streamItemId, buildGenericStreamItemPhotoValues(0), socialAccount);
long streamItemPhotoFileId = getStoredLongValue(streamItemPhotoUri,
StreamItemPhotos.PHOTO_FILE_ID);
photoStore.remove(streamItemPhotoFileId);

// Also insert a bogus photo that nobody is using.
PhotoStore photoStore = provider.getPhotoStore();
long bogusPhotoId = photoStore.insert(new PhotoProcessor(loadPhotoFromResource(
R.drawable.earth_huge, PhotoSize.ORIGINAL), 256, 96));

Expand All @@ -4861,6 +4874,12 @@ public void testPhotoStoreCleanup() throws IOException {

// 4. The bogus photo that nobody was using should be cleared from the photo store.
assertNull(photoStore.get(bogusPhotoId));

// 5. The bogus stream item photo should be cleared from the stream item.
assertStoredValues(Uri.withAppendedPath(
ContentUris.withAppendedId(StreamItems.CONTENT_URI, streamItemId),
StreamItems.StreamItemPhotos.CONTENT_DIRECTORY),
new ContentValues[0]);
}

public void testOverwritePhotoWithThumbnail() throws IOException {
Expand Down

0 comments on commit c2714bb

Please sign in to comment.