1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.photos.adapters;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.graphics.drawable.Drawable;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.CursorAdapter;
26 import android.widget.ImageView;
27 
28 import com.android.gallery3d.R;
29 import com.android.photos.data.PhotoSetLoader;
30 import com.android.photos.shims.LoaderCompatShim;
31 import com.android.photos.views.GalleryThumbnailView.GalleryThumbnailAdapter;
32 
33 
34 public class PhotoThumbnailAdapter extends CursorAdapter implements GalleryThumbnailAdapter {
35     private LayoutInflater mInflater;
36     private LoaderCompatShim<Cursor> mDrawableFactory;
37 
PhotoThumbnailAdapter(Context context)38     public PhotoThumbnailAdapter(Context context) {
39         super(context, null, false);
40         mInflater = LayoutInflater.from(context);
41     }
42 
setDrawableFactory(LoaderCompatShim<Cursor> factory)43     public void setDrawableFactory(LoaderCompatShim<Cursor> factory) {
44         mDrawableFactory = factory;
45     }
46 
47     @Override
bindView(View view, Context context, Cursor cursor)48     public void bindView(View view, Context context, Cursor cursor) {
49         ImageView iv = (ImageView) view.findViewById(R.id.thumbnail);
50         Drawable recycle = iv.getDrawable();
51         Drawable drawable = mDrawableFactory.drawableForItem(cursor, recycle);
52         if (recycle != drawable) {
53             iv.setImageDrawable(drawable);
54         }
55     }
56 
57     @Override
newView(Context context, Cursor cursor, ViewGroup parent)58     public View newView(Context context, Cursor cursor, ViewGroup parent) {
59         View view = mInflater.inflate(R.layout.photo_set_item, parent, false);
60         return view;
61     }
62 
63     @Override
getIntrinsicAspectRatio(int position)64     public float getIntrinsicAspectRatio(int position) {
65         Cursor cursor = getItem(position);
66         float width = cursor.getInt(PhotoSetLoader.INDEX_WIDTH);
67         float height = cursor.getInt(PhotoSetLoader.INDEX_HEIGHT);
68         return width / height;
69     }
70 
71     @Override
getItem(int position)72     public Cursor getItem(int position) {
73         return (Cursor) super.getItem(position);
74     }
75 }