PhotoPagerAdapter.java revision d19540fb7139fc0baba3b55260a6aabdb029c831
1/* 2 * Copyright (C) 2011 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.android.ex.photo.adapters; 19 20import android.app.Fragment; 21import android.app.FragmentManager; 22import android.content.Context; 23import android.database.Cursor; 24 25import com.android.ex.photo.Intents; 26import com.android.ex.photo.Intents.PhotoViewIntentBuilder; 27import com.android.ex.photo.fragments.PhotoViewFragment; 28import com.android.ex.photo.provider.PhotoContract; 29 30/** 31 * Pager adapter for the photo view 32 */ 33public class PhotoPagerAdapter extends BaseCursorPagerAdapter { 34 private int mContentUriIndex; 35 private int mPhotoNameIndex; 36 private int mThumbnailUriIndex; 37 38 public PhotoPagerAdapter(Context context, FragmentManager fm, Cursor c) { 39 super(context, fm, c); 40 } 41 42 @Override 43 public Fragment getItem(Context context, Cursor cursor, int position) { 44 final String photoUri = cursor.getString(mContentUriIndex); 45 final String photoName = cursor.getString(mPhotoNameIndex); 46 final String thumbnailUri = cursor.getString(mThumbnailUriIndex); 47 48 // create new PhotoViewFragment 49 final PhotoViewIntentBuilder builder = 50 Intents.newPhotoViewFragmentIntentBuilder(mContext); 51 builder 52 .setPhotoName(photoName) 53 .setResolvedPhotoUri(photoUri) 54 .setThumbnailUri(thumbnailUri); 55 56 return new PhotoViewFragment(builder.build(), position, this); 57 } 58 59 @Override 60 public Cursor swapCursor(Cursor newCursor) { 61 mContentUriIndex = 62 newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.CONTENT_URI); 63 mThumbnailUriIndex = 64 newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.THUMBNAIL_URI); 65 mPhotoNameIndex = 66 newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.NAME); 67 68 return super.swapCursor(newCursor); 69 } 70 71 public String getPhotoUri(Cursor cursor) { 72 return cursor.getString(mContentUriIndex); 73 } 74} 75