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    private int mLoadingIndex;
37    private final float mMaxScale;
38
39    public PhotoPagerAdapter(Context context, FragmentManager fm, Cursor c, float maxScale) {
40        super(context, fm, c);
41        mMaxScale = maxScale;
42    }
43
44    @Override
45    public Fragment getItem(Context context, Cursor cursor, int position) {
46        final String photoUri = cursor.getString(mContentUriIndex);
47        final String thumbnailUri = cursor.getString(mThumbnailUriIndex);
48        boolean loading;
49        if(mLoadingIndex != -1) {
50            loading = Boolean.valueOf(cursor.getString(mLoadingIndex));
51        } else {
52            loading = false;
53        }
54        boolean onlyShowSpinner = false;
55        if(photoUri == null && loading) {
56            onlyShowSpinner = true;
57        }
58
59        // create new PhotoViewFragment
60        final PhotoViewIntentBuilder builder =
61                Intents.newPhotoViewFragmentIntentBuilder(mContext);
62        builder
63            .setResolvedPhotoUri(photoUri)
64            .setThumbnailUri(thumbnailUri)
65            .setMaxInitialScale(mMaxScale);
66
67        return new PhotoViewFragment(builder.build(), position, this, onlyShowSpinner);
68    }
69
70    @Override
71    public Cursor swapCursor(Cursor newCursor) {
72        if (newCursor != null) {
73            mContentUriIndex =
74                    newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.CONTENT_URI);
75            mThumbnailUriIndex =
76                    newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.THUMBNAIL_URI);
77            mLoadingIndex =
78                    newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.LOADING_INDICATOR);
79        } else {
80            mContentUriIndex = -1;
81            mThumbnailUriIndex = -1;
82            mLoadingIndex = -1;
83        }
84
85        return super.swapCursor(newCursor);
86    }
87
88    public String getPhotoUri(Cursor cursor) {
89        return cursor.getString(mContentUriIndex);
90    }
91}
92