Home | History | Annotate | Download | only in adapters
      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 
     18 package com.android.ex.photo.adapters;
     19 
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.database.Cursor;
     23 import android.support.v4.app.Fragment;
     24 
     25 import com.android.ex.photo.Intents;
     26 import com.android.ex.photo.Intents.PhotoViewIntentBuilder;
     27 import com.android.ex.photo.fragments.PhotoViewFragment;
     28 import com.android.ex.photo.provider.PhotoContract;
     29 
     30 /**
     31  * Pager adapter for the photo view
     32  */
     33 public class PhotoPagerAdapter extends BaseCursorPagerAdapter {
     34     protected int mContentUriIndex;
     35     protected int mThumbnailUriIndex;
     36     protected int mLoadingIndex;
     37     protected final float mMaxScale;
     38     protected boolean mDisplayThumbsFullScreen;
     39 
     40     public PhotoPagerAdapter(
     41             Context context, android.support.v4.app.FragmentManager fm, Cursor c,
     42             float maxScale, boolean thumbsFullScreen) {
     43         super(context, fm, c);
     44         mMaxScale = maxScale;
     45         mDisplayThumbsFullScreen = thumbsFullScreen;
     46     }
     47 
     48     @Override
     49     public Fragment getItem(Context context, Cursor cursor, int position) {
     50         final String photoUri = cursor.getString(mContentUriIndex);
     51         final String thumbnailUri = cursor.getString(mThumbnailUriIndex);
     52         boolean loading;
     53         if(mLoadingIndex != -1) {
     54             loading = Boolean.valueOf(cursor.getString(mLoadingIndex));
     55         } else {
     56             loading = false;
     57         }
     58         boolean onlyShowSpinner = false;
     59         if(photoUri == null && loading) {
     60             onlyShowSpinner = true;
     61         }
     62 
     63         // create new PhotoViewFragment
     64         final PhotoViewIntentBuilder builder =
     65                 Intents.newPhotoViewFragmentIntentBuilder(mContext);
     66         builder
     67             .setResolvedPhotoUri(photoUri)
     68             .setThumbnailUri(thumbnailUri)
     69             .setDisplayThumbsFullScreen(mDisplayThumbsFullScreen)
     70             .setMaxInitialScale(mMaxScale);
     71 
     72         return PhotoViewFragment.newInstance(builder.build(), position, onlyShowSpinner);
     73     }
     74 
     75     @Override
     76     public Cursor swapCursor(Cursor newCursor) {
     77         if (newCursor != null) {
     78             mContentUriIndex =
     79                     newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.CONTENT_URI);
     80             mThumbnailUriIndex =
     81                     newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.THUMBNAIL_URI);
     82             mLoadingIndex =
     83                     newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.LOADING_INDICATOR);
     84         } else {
     85             mContentUriIndex = -1;
     86             mThumbnailUriIndex = -1;
     87             mLoadingIndex = -1;
     88         }
     89 
     90         return super.swapCursor(newCursor);
     91     }
     92 
     93     public String getPhotoUri(Cursor cursor) {
     94         return cursor.getString(mContentUriIndex);
     95     }
     96 
     97     public String getThumbnailUri(Cursor cursor) {
     98         return cursor.getString(mThumbnailUriIndex);
     99     }
    100 }
    101