AlbumSlotRenderer.java revision c3d8ac3b9504346dafc49e006b5f732dd1db21e8
1/*
2 * Copyright (C) 2010 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.gallery3d.ui;
18
19import android.content.Context;
20
21import com.android.gallery3d.app.AlbumDataLoader;
22import com.android.gallery3d.app.GalleryActivity;
23import com.android.gallery3d.data.MediaObject;
24import com.android.gallery3d.data.Path;
25
26public class AlbumSlotRenderer extends AbstractSlotRenderer {
27    private static final int PLACEHOLDER_COLOR = 0xFF222222;
28
29    @SuppressWarnings("unused")
30    private static final String TAG = "AlbumView";
31    private static final int CACHE_SIZE = 96;
32
33    private AlbumSlidingWindow mDataWindow;
34    private final GalleryActivity mActivity;
35    private final ColorTexture mWaitLoadingTexture;
36    private final SlotView mSlotView;
37    private final SelectionManager mSelectionManager;
38
39    private int mPressedIndex = -1;
40    private Path mHighlightItemPath = null;
41    private boolean mInSelectionMode;
42
43    public AlbumSlotRenderer(GalleryActivity activity, SlotView slotView,
44            SelectionManager selectionManager) {
45        super((Context) activity);
46        mActivity = activity;
47        mSlotView = slotView;
48        mSelectionManager = selectionManager;
49
50        mWaitLoadingTexture = new ColorTexture(PLACEHOLDER_COLOR);
51        mWaitLoadingTexture.setSize(1, 1);
52    }
53
54    public void setPressedIndex(int index) {
55        if (mPressedIndex == index) return;
56        mPressedIndex = index;
57        mSlotView.invalidate();
58    }
59
60    public void setHighlightItemPath(Path path) {
61        if (mHighlightItemPath == path) return;
62        mHighlightItemPath = path;
63        mSlotView.invalidate();
64    }
65
66    public void setModel(AlbumDataLoader model) {
67        if (mDataWindow != null) {
68            mDataWindow.setListener(null);
69            mSlotView.setSlotCount(0);
70            mDataWindow = null;
71        }
72        if (model != null) {
73            mDataWindow = new AlbumSlidingWindow(mActivity, model, CACHE_SIZE);
74            mDataWindow.setListener(new MyDataModelListener());
75            mSlotView.setSlotCount(model.size());
76        }
77    }
78
79    private static Texture checkTexture(GLCanvas canvas, Texture texture) {
80        return ((texture == null) || ((texture instanceof UploadedTexture)
81                && !((UploadedTexture) texture).isContentValid(canvas)))
82                ? null
83                : texture;
84    }
85
86    @Override
87    public int renderSlot(GLCanvas canvas, int index, int pass, int width, int height) {
88        AlbumSlidingWindow.AlbumEntry entry = mDataWindow.get(index);
89
90        int renderRequestFlags = 0;
91
92        Texture content = checkTexture(canvas, entry.content);
93        if (content == null) {
94            content = mWaitLoadingTexture;
95            entry.isWaitDisplayed = true;
96        } else if (entry.isWaitDisplayed) {
97            entry.isWaitDisplayed = false;
98            entry.content = new FadeInTexture(
99                    PLACEHOLDER_COLOR, (BitmapTexture) entry.content);
100            content = entry.content;
101        }
102        drawContent(canvas, content, width, height, entry.rotation);
103        if ((content instanceof FadeInTexture) &&
104                ((FadeInTexture) content).isAnimating()) {
105            renderRequestFlags |= SlotView.RENDER_MORE_FRAME;
106        }
107
108        if (entry.mediaType == MediaObject.MEDIA_TYPE_VIDEO) {
109            drawVideoOverlay(canvas, width, height);
110        }
111
112        if (entry.isPanorama) {
113            drawPanoramaBorder(canvas, width, height);
114        }
115
116        if (mPressedIndex == index) {
117            drawPressedFrame(canvas, width, height);
118        } else if ((entry.path != null) && (mHighlightItemPath == entry.path)) {
119            drawSelectedFrame(canvas, width, height);
120        } else if (mInSelectionMode && mSelectionManager.isItemSelected(entry.path)) {
121            drawSelectedFrame(canvas, width, height);
122        }
123
124        return renderRequestFlags;
125    }
126
127    private class MyDataModelListener implements AlbumSlidingWindow.Listener {
128        @Override
129        public void onContentChanged() {
130            mSlotView.invalidate();
131        }
132
133        @Override
134        public void onSizeChanged(int size) {
135            mSlotView.setSlotCount(size);
136        }
137    }
138
139    public void resume() {
140        mDataWindow.resume();
141    }
142
143    public void pause() {
144        mDataWindow.pause();
145    }
146
147    @Override
148    public void prepareDrawing() {
149        mInSelectionMode = mSelectionManager.inSelectionMode();
150    }
151
152    @Override
153    public void onVisibleRangeChanged(int visibleStart, int visibleEnd) {
154        if (mDataWindow != null) {
155            mDataWindow.setActiveWindow(visibleStart, visibleEnd);
156        }
157    }
158
159    @Override
160    public void onSlotSizeChanged(int width, int height) {
161        // Do nothing
162    }
163}
164