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
17package com.android.photos.adapters;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.graphics.drawable.Drawable;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.CursorAdapter;
26import android.widget.ImageView;
27
28import com.android.gallery3d.R;
29import com.android.photos.data.PhotoSetLoader;
30import com.android.photos.shims.LoaderCompatShim;
31import com.android.photos.views.GalleryThumbnailView.GalleryThumbnailAdapter;
32
33
34public class PhotoThumbnailAdapter extends CursorAdapter implements GalleryThumbnailAdapter {
35    private LayoutInflater mInflater;
36    private LoaderCompatShim<Cursor> mDrawableFactory;
37
38    public PhotoThumbnailAdapter(Context context) {
39        super(context, null, false);
40        mInflater = LayoutInflater.from(context);
41    }
42
43    public void setDrawableFactory(LoaderCompatShim<Cursor> factory) {
44        mDrawableFactory = factory;
45    }
46
47    @Override
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
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
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
72    public Cursor getItem(int position) {
73        return (Cursor) super.getItem(position);
74    }
75}