PhotoPagerAdapter.java revision 8085e1fcda882074ed387d3e999c448a84d6eb3b
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;
36d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein    private int mThumbnailUriIndex;
371cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
381cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    public PhotoPagerAdapter(Context context, FragmentManager fm, Cursor c) {
391cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        super(context, fm, c);
401cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    }
411cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
421cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    @Override
43a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein    public Fragment getItem(Context context, Cursor cursor, int position) {
441cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        final String photoUri = cursor.getString(mContentUriIndex);
451cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        final String photoName = cursor.getString(mPhotoNameIndex);
46d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein        final String thumbnailUri = cursor.getString(mThumbnailUriIndex);
471cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
481cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        // create new PhotoViewFragment
491cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        final PhotoViewIntentBuilder builder =
501cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein                Intents.newPhotoViewFragmentIntentBuilder(mContext);
518085e1fcda882074ed387d3e999c448a84d6eb3bAndrew Sapperstein        builder
521cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein            .setPhotoName(photoName)
53d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein            .setResolvedPhotoUri(photoUri)
54d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein            .setThumbnailUri(thumbnailUri);
551cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
56a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein        return new PhotoViewFragment(builder.build(), position, this);
571cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    }
581cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
591cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    @Override
601cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    public Cursor swapCursor(Cursor newCursor) {
611cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        mContentUriIndex =
621cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein                newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.CONTENT_URI);
63d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein        mThumbnailUriIndex =
64d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein                newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.THUMBNAIL_URI);
651cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        mPhotoNameIndex =
661cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein                newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.NAME);
671cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
681cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        return super.swapCursor(newCursor);
691cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein    }
70a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein
71a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein    public String getPhotoUri(Cursor cursor) {
72a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein        return cursor.getString(mContentUriIndex);
73a6729336171ef61950d5f585b0f39c013ae6dcbcAndrew Sapperstein    }
741cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein}
75