PhotoPagerAdapter.java revision 0c18e5deef5209d21b527a01feb6b789715eb48e
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 37 public PhotoPagerAdapter(Context context, FragmentManager fm, Cursor c) { 38 super(context, fm, c); 39 } 40 41 @Override 42 public Fragment getItem(Context context, Cursor cursor, int position) { 43 final String photoUri = cursor.getString(mContentUriIndex); 44 final String photoName = cursor.getString(mPhotoNameIndex); 45 46 // create new PhotoViewFragment 47 final PhotoViewIntentBuilder builder = 48 Intents.newPhotoViewFragmentIntentBuilder(mContext); 49 builder 50 .setPhotoName(photoName) 51 .setResolvedPhotoUri(photoUri); 52 53 return new PhotoViewFragment(builder.build(), position, this); 54 } 55 56 @Override 57 public Cursor swapCursor(Cursor newCursor) { 58 mContentUriIndex = 59 newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.CONTENT_URI); 60 mPhotoNameIndex = 61 newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.NAME); 62 63 return super.swapCursor(newCursor); 64 } 65 66 public String getPhotoUri(Cursor cursor) { 67 return cursor.getString(mContentUriIndex); 68 } 69} 70