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.database.Cursor;
     22 import android.support.v4.app.Fragment;
     23 
     24 import com.android.ex.photo.Intents;
     25 import com.android.ex.photo.Intents.PhotoViewIntentBuilder;
     26 import com.android.ex.photo.fragments.PhotoViewFragment;
     27 import com.android.ex.photo.provider.PhotoContract;
     28 
     29 /**
     30  * Pager adapter for the photo view
     31  */
     32 public class PhotoPagerAdapter extends BaseCursorPagerAdapter {
     33     protected int mContentUriIndex;
     34     protected int mThumbnailUriIndex;
     35     protected int mLoadingIndex;
     36     protected final float mMaxScale;
     37 
     38     public PhotoPagerAdapter(
     39             Context context, android.support.v4.app.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 PhotoViewFragment.newInstance(builder.build(), position, 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     public String getThumbnailUri(Cursor cursor) {
     93         return cursor.getString(mThumbnailUriIndex);
     94     }
     95 }
     96