diff options
author | Ricardo Cerqueira <cyanogenmod@cerqueira.org> | 2013-07-25 00:47:41 +0100 |
---|---|---|
committer | Ricardo Cerqueira <cyanogenmod@cerqueira.org> | 2013-07-25 00:47:41 +0100 |
commit | 412c2e04636a24e7da24bb26b751b9482a83d68f (patch) | |
tree | 14c693521ef1408797d22f722d066b41a936367d /src/com/android/dreams/phototable/LocalSource.java | |
parent | 1a180510e6421467db8cafa641130fb1dbb6aee7 (diff) | |
parent | dd5f6f03990d50bbb353b2c4d77a90f8211b5a74 (diff) | |
download | android_packages_screensavers_PhotoTable-412c2e04636a24e7da24bb26b751b9482a83d68f.tar.gz android_packages_screensavers_PhotoTable-412c2e04636a24e7da24bb26b751b9482a83d68f.tar.bz2 android_packages_screensavers_PhotoTable-412c2e04636a24e7da24bb26b751b9482a83d68f.zip |
Merge tag 'android-4.3_r2.1' into cm-10.2cm-10.2.1cm-10.2.0cm-10.2-M1stable/cm-10.2cm-10.2
Android 4.3 release 2.1
Diffstat (limited to 'src/com/android/dreams/phototable/LocalSource.java')
-rw-r--r-- | src/com/android/dreams/phototable/LocalSource.java | 96 |
1 files changed, 71 insertions, 25 deletions
diff --git a/src/com/android/dreams/phototable/LocalSource.java b/src/com/android/dreams/phototable/LocalSource.java index 1faf589..cf2e0ec 100644 --- a/src/com/android/dreams/phototable/LocalSource.java +++ b/src/com/android/dreams/phototable/LocalSource.java @@ -30,20 +30,20 @@ import java.util.Set; /** * Loads images from the local store. */ -public class LocalSource extends PhotoSource { +public class LocalSource extends CursorPhotoSource { private static final String TAG = "PhotoTable.LocalSource"; private final String mUnknownAlbumName; private final String mLocalSourceName; private Set<String> mFoundAlbumIds; - private int mNextPosition; + private int mLastPosition; public LocalSource(Context context, SharedPreferences settings) { super(context, settings); mLocalSourceName = mResources.getString(R.string.local_source_name, "Photos on Device"); mUnknownAlbumName = mResources.getString(R.string.unknown_album_name, "Unknown"); mSourceName = TAG; - mNextPosition = -1; + mLastPosition = INVALID; fillQueue(); } @@ -65,7 +65,7 @@ public class LocalSource extends PhotoSource { Cursor cursor = mResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null); if (cursor != null) { - cursor.moveToFirst(); + cursor.moveToPosition(-1); int dataIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); int bucketIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID); @@ -75,7 +75,7 @@ public class LocalSource extends PhotoSource { if (bucketIndex < 0) { log(TAG, "can't find the ID column!"); } else { - while (!cursor.isAfterLast()) { + while (cursor.moveToNext()) { String id = TAG + ":" + cursor.getString(bucketIndex); AlbumData data = foundAlbums.get(id); if (foundAlbums.get(id) == null) { @@ -102,7 +102,6 @@ public class LocalSource extends PhotoSource { updated : Math.min(data.updated, updated)); } - cursor.moveToNext(); } } cursor.close(); @@ -114,6 +113,59 @@ public class LocalSource extends PhotoSource { } @Override + protected void openCursor(ImageData data) { + log(TAG, "opening single album"); + + String[] projection = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION, + MediaStore.Images.Media.BUCKET_ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME}; + String selection = MediaStore.Images.Media.BUCKET_ID + " = '" + data.albumId + "'"; + + data.cursor = mResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + projection, selection, null, null); + } + + @Override + protected void findPosition(ImageData data) { + if (data.position == -1) { + if (data.cursor == null) { + openCursor(data); + } + if (data.cursor != null) { + int dataIndex = data.cursor.getColumnIndex(MediaStore.Images.Media.DATA); + data.cursor.moveToPosition(-1); + while (data.position == -1 && data.cursor.moveToNext()) { + String url = data.cursor.getString(dataIndex); + if (url != null && url.equals(data.url)) { + data.position = data.cursor.getPosition(); + } + } + if (data.position == -1) { + // oops! The image isn't in this album. How did we get here? + data.position = INVALID; + } + } + } + } + + @Override + protected ImageData unpackImageData(Cursor cursor, ImageData data) { + if (data == null) { + data = new ImageData(); + } + int dataIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); + int orientationIndex = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION); + int bucketIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID); + + data.url = cursor.getString(dataIndex); + data.albumId = cursor.getString(bucketIndex); + data.position = UNINITIALIZED; + data.cursor = null; + data.orientation = cursor.getInt(orientationIndex); + + return data; + } + + @Override protected Collection<ImageData> findImages(int howMany) { log(TAG, "finding images"); LinkedList<ImageData> foundImages = new LinkedList<ImageData>(); @@ -139,33 +191,26 @@ public class LocalSource extends PhotoSource { Cursor cursor = mResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null); if (cursor != null) { - if (cursor.getCount() > howMany && mNextPosition == -1) { - mNextPosition = mRNG.nextInt() % (cursor.getCount() - howMany); - } - if (mNextPosition == -1) { - mNextPosition = 0; - } - cursor.moveToPosition(mNextPosition); - int dataIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); - int orientationIndex = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION); - int bucketIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID); - int nameIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME); + + if (cursor.getCount() > howMany && mLastPosition == INVALID) { + mLastPosition = pickRandomStart(cursor.getCount(), howMany); + } + cursor.moveToPosition(mLastPosition); if (dataIndex < 0) { log(TAG, "can't find the DATA column!"); } else { - while (foundImages.size() < howMany && !cursor.isAfterLast()) { - ImageData data = new ImageData(); - data.url = cursor.getString(dataIndex); - data.orientation = cursor.getInt(orientationIndex); + while (foundImages.size() < howMany && cursor.moveToNext()) { + ImageData data = unpackImageData(cursor, null); foundImages.offer(data); - if (cursor.moveToNext()) { - mNextPosition++; - } + mLastPosition = cursor.getPosition(); } if (cursor.isAfterLast()) { - mNextPosition = 0; + mLastPosition = -1; + } + if (cursor.isBeforeFirst()) { + mLastPosition = INVALID; } } @@ -189,3 +234,4 @@ public class LocalSource extends PhotoSource { return (InputStream) fis; } } + |