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;
35d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein    private int mThumbnailUriIndex;
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);
44d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein        final String thumbnailUri = cursor.getString(mThumbnailUriIndex);
451cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein
461cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        // create new PhotoViewFragment
471cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein        final PhotoViewIntentBuilder builder =
481cc4b2144a45abb495c8b14f6cfc5a10fb5e8ba8Andrew Sapperstein                Intents.newPhotoViewFragmentIntentBuilder(mContext);
498085e1fcda882074ed387d3e999c448a84d6eb3bAndrew Sapperstein        builder
50d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein            .setResolvedPhotoUri(photoUri)
51d19540fb7139fc0baba3b55260a6aabdb029c831Andrew Sapperstein            .setThumbnailUri(thumbnailUri);
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) {
58114cef99189d693ed9cd8cf778a60823d06d5298Paul Westbrook        if (newCursor != null) {
59114cef99189d693ed9cd8cf778a60823d06d5298Paul Westbrook            mContentUriIndex =
60114cef99189d693ed9cd8cf778a60823d06d5298Paul Westbrook                    newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.CONTENT_URI);
61114cef99189d693ed9cd8cf778a60823d06d5298Paul Westbrook            mThumbnailUriIndex =
62114cef99189d693ed9cd8cf778a60823d06d5298Paul Westbrook                    newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.THUMBNAIL_URI);
63114cef99189d693ed9cd8cf778a60823d06d5298Paul Westbrook        } else {
64114cef99189d693ed9cd8cf778a60823d06d5298Paul Westbrook            mContentUriIndex = -1;
65114cef99189d693ed9cd8cf778a60823d06d5298Paul Westbrook            mThumbnailUriIndex = -1;
66114cef99189d693ed9cd8cf778a60823d06d5298Paul Westbrook        }
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