PhotoPagerAdapter.java revision 114cef99189d693ed9cd8cf778a60823d06d5298
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 mThumbnailUriIndex;
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 thumbnailUri = cursor.getString(mThumbnailUriIndex);
45
46        // create new PhotoViewFragment
47        final PhotoViewIntentBuilder builder =
48                Intents.newPhotoViewFragmentIntentBuilder(mContext);
49        builder
50            .setResolvedPhotoUri(photoUri)
51            .setThumbnailUri(thumbnailUri);
52
53        return new PhotoViewFragment(builder.build(), position, this);
54    }
55
56    @Override
57    public Cursor swapCursor(Cursor newCursor) {
58        if (newCursor != null) {
59            mContentUriIndex =
60                    newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.CONTENT_URI);
61            mThumbnailUriIndex =
62                    newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.THUMBNAIL_URI);
63        } else {
64            mContentUriIndex = -1;
65            mThumbnailUriIndex = -1;
66        }
67
68        return super.swapCursor(newCursor);
69    }
70
71    public String getPhotoUri(Cursor cursor) {
72        return cursor.getString(mContentUriIndex);
73    }
74}
75