PhotoPagerAdapter.java revision a6729336171ef61950d5f585b0f39c013ae6dcbc
11cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein/*
21cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * Copyright (C) 2011 Google Inc.
31cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * Licensed to The Android Open Source Project.
41cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein *
51cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * Licensed under the Apache License, Version 2.0 (the "License");
61cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * you may not use this file except in compliance with the License.
71cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * You may obtain a copy of the License at
81cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein *
91cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein *      http://www.apache.org/licenses/LICENSE-2.0
101cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein *
111cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * Unless required by applicable law or agreed to in writing, software
121cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * distributed under the License is distributed on an "AS IS" BASIS,
131cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
141cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * See the License for the specific language governing permissions and
151cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * limitations under the License.
161cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein */
171cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
181cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinpackage com.android.ex.photo.adapters;
191cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
201cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinimport android.app.Fragment;
211cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinimport android.app.FragmentManager;
221cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinimport android.content.Context;
231cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinimport android.database.Cursor;
241cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
251cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinimport com.android.ex.photo.Intents;
261cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinimport com.android.ex.photo.Intents.PhotoViewIntentBuilder;
271cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinimport com.android.ex.photo.fragments.PhotoViewFragment;
281cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinimport com.android.ex.photo.provider.PhotoContract;
291cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
301cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein/**
311cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein * Pager adapter for the photo view
321cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein */
331cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sappersteinpublic class PhotoPagerAdapter extends BaseCursorPagerAdapter {
341cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    private int mContentUriIndex;
351cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    private int mPhotoNameIndex;
361cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
371cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    public PhotoPagerAdapter(Context context, FragmentManager fm, Cursor c) {
381cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        super(context, fm, c);
391cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    }
401cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
411cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    @Override
42a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein    public Fragment getItem(Context context, Cursor cursor, int position) {
431cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        final String photoUri = cursor.getString(mContentUriIndex);
441cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        final String photoName = cursor.getString(mPhotoNameIndex);
451cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
461cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        // create new PhotoViewFragment
471cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        final PhotoViewIntentBuilder builder =
481cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein                Intents.newPhotoViewFragmentIntentBuilder(mContext);
491cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein          builder
501cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein            .setPhotoName(photoName)
511cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein            .setResolvedPhotoUri(photoUri);
521cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
53a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein        return new PhotoViewFragment(builder.build(), position, this);
541cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    }
551cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
561cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    @Override
571cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    public Cursor swapCursor(Cursor newCursor) {
581cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        mContentUriIndex =
591cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein                newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.CONTENT_URI);
601cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        mPhotoNameIndex =
611cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein                newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.NAME);
621cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
631cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        return super.swapCursor(newCursor);
641cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    }
65a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein
66a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein    public String getPhotoUri(Cursor cursor) {
67a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein        return cursor.getString(mContentUriIndex);
68a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein    }
691cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein}
70