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 import android.support.v4.util.SimpleArrayMap;
     25 
     26 import com.android.ex.photo.Intents;
     27 import com.android.ex.photo.Intents.PhotoViewIntentBuilder;
     28 import com.android.ex.photo.fragments.PhotoViewFragment;
     29 import com.android.ex.photo.provider.PhotoContract.PhotoViewColumns;
     30 import com.android.ex.photo.provider.PhotoContract.PhotoQuery;
     31 
     32 /**
     33  * Pager adapter for the photo view
     34  */
     35 public class PhotoPagerAdapter extends BaseCursorPagerAdapter {
     36     protected SimpleArrayMap<String, Integer> mColumnIndices =
     37             new SimpleArrayMap<String, Integer>(PhotoQuery.PROJECTION.length);
     38     protected final float mMaxScale;
     39     protected boolean mDisplayThumbsFullScreen;
     40 
     41     public PhotoPagerAdapter(
     42             Context context, android.support.v4.app.FragmentManager fm, Cursor c,
     43             float maxScale, boolean thumbsFullScreen) {
     44         super(context, fm, c);
     45         mMaxScale = maxScale;
     46         mDisplayThumbsFullScreen = thumbsFullScreen;
     47     }
     48 
     49     @Override
     50     public Fragment getItem(Context context, Cursor cursor, int position) {
     51         final String photoUri = getPhotoUri(cursor);
     52         final String thumbnailUri = getThumbnailUri(cursor);
     53         boolean loading = shouldShowLoadingIndicator(cursor);
     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, getPhotoViewFragmentClass());
     62         builder
     63             .setResolvedPhotoUri(photoUri)
     64             .setThumbnailUri(thumbnailUri)
     65             .setDisplayThumbsFullScreen(mDisplayThumbsFullScreen)
     66             .setMaxInitialScale(mMaxScale);
     67 
     68         return createPhotoViewFragment(builder.build(), position, onlyShowSpinner);
     69     }
     70 
     71     protected Class<? extends PhotoViewFragment> getPhotoViewFragmentClass() {
     72         return PhotoViewFragment.class;
     73     }
     74 
     75     protected PhotoViewFragment createPhotoViewFragment(
     76             Intent intent, int position, boolean onlyShowSpinner) {
     77         return PhotoViewFragment.newInstance(intent, position, onlyShowSpinner);
     78     }
     79 
     80     @Override
     81     public Cursor swapCursor(Cursor newCursor) {
     82         mColumnIndices.clear();
     83 
     84         if (newCursor != null) {
     85             for(String column : PhotoQuery.PROJECTION) {
     86                 mColumnIndices.put(column, newCursor.getColumnIndexOrThrow(column));
     87             }
     88 
     89             for(String column : PhotoQuery.OPTIONAL_COLUMNS) {
     90                 int index = newCursor.getColumnIndex(column);
     91                 if (index != -1) {
     92                     mColumnIndices.put(column, index);
     93                 }
     94             }
     95         }
     96 
     97         return super.swapCursor(newCursor);
     98     }
     99 
    100     public String getPhotoUri(Cursor cursor) {
    101         return getString(cursor, PhotoViewColumns.CONTENT_URI);
    102     }
    103 
    104     public String getThumbnailUri(Cursor cursor) {
    105         return getString(cursor, PhotoViewColumns.THUMBNAIL_URI);
    106     }
    107 
    108     public String getContentType(Cursor cursor) {
    109         return getString(cursor, PhotoViewColumns.CONTENT_TYPE);
    110     }
    111 
    112     public boolean shouldShowLoadingIndicator(Cursor cursor) {
    113         String value = getString(cursor, PhotoViewColumns.LOADING_INDICATOR);
    114         if (value == null) {
    115             return false;
    116         } else {
    117             return Boolean.valueOf(value);
    118         }
    119     }
    120 
    121     private String getString(Cursor cursor, String column) {
    122         if (mColumnIndices.containsKey(column)) {
    123             return cursor.getString(mColumnIndices.get(column));
    124         } else {
    125             return null;
    126         }
    127     }
    128 }
    129