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