• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 androidx.fragment.app.Fragment;
24 import androidx.collection.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 
PhotoPagerAdapter( Context context, androidx.fragment.app.FragmentManager fm, Cursor c, float maxScale, boolean thumbsFullScreen)41     public PhotoPagerAdapter(
42             Context context, androidx.fragment.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
getItem(Context context, Cursor cursor, int position)50     public Fragment getItem(Context context, Cursor cursor, int position) {
51         final String photoUri = getPhotoUri(cursor);
52         final String thumbnailUri = getThumbnailUri(cursor);
53         final String contentDescription = getPhotoName(cursor);
54         boolean loading = shouldShowLoadingIndicator(cursor);
55         boolean onlyShowSpinner = false;
56         if(photoUri == null && loading) {
57             onlyShowSpinner = true;
58         }
59 
60         // create new PhotoViewFragment
61         final PhotoViewIntentBuilder builder =
62                 Intents.newPhotoViewFragmentIntentBuilder(mContext, getPhotoViewFragmentClass());
63         builder
64             .setResolvedPhotoUri(photoUri)
65             .setThumbnailUri(thumbnailUri)
66             .setContentDescription(contentDescription)
67             .setDisplayThumbsFullScreen(mDisplayThumbsFullScreen)
68             .setMaxInitialScale(mMaxScale);
69 
70         return createPhotoViewFragment(builder.build(), position, onlyShowSpinner);
71     }
72 
getPhotoViewFragmentClass()73     protected Class<? extends PhotoViewFragment> getPhotoViewFragmentClass() {
74         return PhotoViewFragment.class;
75     }
76 
createPhotoViewFragment( Intent intent, int position, boolean onlyShowSpinner)77     protected PhotoViewFragment createPhotoViewFragment(
78             Intent intent, int position, boolean onlyShowSpinner) {
79         return PhotoViewFragment.newInstance(intent, position, onlyShowSpinner);
80     }
81 
82     @Override
swapCursor(Cursor newCursor)83     public Cursor swapCursor(Cursor newCursor) {
84         mColumnIndices.clear();
85 
86         if (newCursor != null) {
87             for(String column : PhotoQuery.PROJECTION) {
88                 mColumnIndices.put(column, newCursor.getColumnIndexOrThrow(column));
89             }
90 
91             for(String column : PhotoQuery.OPTIONAL_COLUMNS) {
92                 int index = newCursor.getColumnIndex(column);
93                 if (index != -1) {
94                     mColumnIndices.put(column, index);
95                 }
96             }
97         }
98 
99         return super.swapCursor(newCursor);
100     }
101 
getPhotoUri(Cursor cursor)102     public String getPhotoUri(Cursor cursor) {
103         return getString(cursor, PhotoViewColumns.CONTENT_URI);
104     }
105 
getThumbnailUri(Cursor cursor)106     public String getThumbnailUri(Cursor cursor) {
107         return getString(cursor, PhotoViewColumns.THUMBNAIL_URI);
108     }
109 
getContentType(Cursor cursor)110     public String getContentType(Cursor cursor) {
111         return getString(cursor, PhotoViewColumns.CONTENT_TYPE);
112     }
113 
getPhotoName(Cursor cursor)114     public String getPhotoName(Cursor cursor) {
115         return getString(cursor, PhotoViewColumns.NAME);
116     }
117 
shouldShowLoadingIndicator(Cursor cursor)118     public boolean shouldShowLoadingIndicator(Cursor cursor) {
119         String value = getString(cursor, PhotoViewColumns.LOADING_INDICATOR);
120         if (value == null) {
121             return false;
122         } else {
123             return Boolean.parseBoolean(value);
124         }
125     }
126 
getString(Cursor cursor, String column)127     private String getString(Cursor cursor, String column) {
128         if (mColumnIndices.containsKey(column)) {
129             return cursor.getString(mColumnIndices.get(column));
130         } else {
131             return null;
132         }
133     }
134 }
135