diff options
author | Shri Borde <shri@google.com> | 2014-08-19 18:05:28 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-08-19 18:05:28 +0000 |
commit | 616bb9922d352f5d9ecb38e81f3ba54e8234e5c8 (patch) | |
tree | 227bf34b62539b742187677e359c8f5814e4f4a1 | |
parent | 76fb8961292dfbded816d289d4d1dc6489e6b7ea (diff) | |
parent | e36676cf09e0f48164831479a6427f4c9c26a40b (diff) | |
download | photoviewer-616bb9922d352f5d9ecb38e81f3ba54e8234e5c8.tar.gz |
am e36676cf: Add PhotoPagerAdapter.getContentType api
* commit 'e36676cf09e0f48164831479a6427f4c9c26a40b':
Add PhotoPagerAdapter.getContentType api
-rw-r--r-- | src/com/android/ex/photo/adapters/PhotoPagerAdapter.java | 67 | ||||
-rw-r--r-- | src/com/android/ex/photo/provider/PhotoContract.java | 4 |
2 files changed, 47 insertions, 24 deletions
diff --git a/src/com/android/ex/photo/adapters/PhotoPagerAdapter.java b/src/com/android/ex/photo/adapters/PhotoPagerAdapter.java index b937365..a09bb63 100644 --- a/src/com/android/ex/photo/adapters/PhotoPagerAdapter.java +++ b/src/com/android/ex/photo/adapters/PhotoPagerAdapter.java @@ -21,19 +21,20 @@ import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.support.v4.app.Fragment; +import android.support.v4.util.SimpleArrayMap; import com.android.ex.photo.Intents; import com.android.ex.photo.Intents.PhotoViewIntentBuilder; import com.android.ex.photo.fragments.PhotoViewFragment; -import com.android.ex.photo.provider.PhotoContract; +import com.android.ex.photo.provider.PhotoContract.PhotoViewColumns; +import com.android.ex.photo.provider.PhotoContract.PhotoQuery; /** * Pager adapter for the photo view */ public class PhotoPagerAdapter extends BaseCursorPagerAdapter { - protected int mContentUriIndex; - protected int mThumbnailUriIndex; - protected int mLoadingIndex; + protected SimpleArrayMap<String, Integer> mColumnIndices = + new SimpleArrayMap<String, Integer>(PhotoQuery.PROJECTION.length); protected final float mMaxScale; protected boolean mDisplayThumbsFullScreen; @@ -47,14 +48,9 @@ public class PhotoPagerAdapter extends BaseCursorPagerAdapter { @Override public Fragment getItem(Context context, Cursor cursor, int position) { - final String photoUri = cursor.getString(mContentUriIndex); - final String thumbnailUri = cursor.getString(mThumbnailUriIndex); - boolean loading; - if(mLoadingIndex != -1) { - loading = Boolean.valueOf(cursor.getString(mLoadingIndex)); - } else { - loading = false; - } + final String photoUri = getPhotoUri(cursor); + final String thumbnailUri = getThumbnailUri(cursor); + boolean loading = shouldShowLoadingIndicator(cursor); boolean onlyShowSpinner = false; if(photoUri == null && loading) { onlyShowSpinner = true; @@ -83,27 +79,50 @@ public class PhotoPagerAdapter extends BaseCursorPagerAdapter { @Override public Cursor swapCursor(Cursor newCursor) { + mColumnIndices.clear(); + if (newCursor != null) { - mContentUriIndex = - newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.CONTENT_URI); - mThumbnailUriIndex = - newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.THUMBNAIL_URI); - mLoadingIndex = - newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.LOADING_INDICATOR); - } else { - mContentUriIndex = -1; - mThumbnailUriIndex = -1; - mLoadingIndex = -1; + for(String column : PhotoQuery.PROJECTION) { + mColumnIndices.put(column, newCursor.getColumnIndexOrThrow(column)); + } + + for(String column : PhotoQuery.OPTIONAL_COLUMNS) { + int index = newCursor.getColumnIndex(column); + if (index != -1) { + mColumnIndices.put(column, index); + } + } } return super.swapCursor(newCursor); } public String getPhotoUri(Cursor cursor) { - return cursor.getString(mContentUriIndex); + return getString(cursor, PhotoViewColumns.CONTENT_URI); } public String getThumbnailUri(Cursor cursor) { - return cursor.getString(mThumbnailUriIndex); + return getString(cursor, PhotoViewColumns.THUMBNAIL_URI); + } + + public String getContentType(Cursor cursor) { + return getString(cursor, PhotoViewColumns.CONTENT_TYPE); + } + + public boolean shouldShowLoadingIndicator(Cursor cursor) { + String value = getString(cursor, PhotoViewColumns.LOADING_INDICATOR); + if (value == null) { + return false; + } else { + return Boolean.valueOf(value); + } + } + + private String getString(Cursor cursor, String column) { + if (mColumnIndices.containsKey(column)) { + return cursor.getString(mColumnIndices.get(column)); + } else { + return null; + } } } diff --git a/src/com/android/ex/photo/provider/PhotoContract.java b/src/com/android/ex/photo/provider/PhotoContract.java index c7e49fd..b79310e 100644 --- a/src/com/android/ex/photo/provider/PhotoContract.java +++ b/src/com/android/ex/photo/provider/PhotoContract.java @@ -64,6 +64,10 @@ public final class PhotoContract { PhotoViewColumns.THUMBNAIL_URI, PhotoViewColumns.CONTENT_TYPE }; + + public final static String[] OPTIONAL_COLUMNS = { + PhotoViewColumns.LOADING_INDICATOR + }; } public static final class ContentTypeParameters { |