AlbumSetSlotRenderer.java revision 132db75a1fa9b41d3000c6cb29bd3d90a7d8ef28
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.AlbumSetDataLoader;
22import com.android.gallery3d.app.GalleryActivity;
23import com.android.gallery3d.data.MediaObject;
24import com.android.gallery3d.data.Path;
25import com.android.gallery3d.ui.AlbumSetSlidingWindow.AlbumSetEntry;
26
27public class AlbumSetSlotRenderer extends AbstractSlotRenderer {
28    @SuppressWarnings("unused")
29    private static final String TAG = "AlbumSetView";
30    private static final int CACHE_SIZE = 96;
31    private static final int PLACEHOLDER_COLOR = 0xFF222222;
32
33    private final ColorTexture mWaitLoadingTexture;
34    private final GalleryActivity mActivity;
35    private final SelectionManager mSelectionManager;
36    protected final LabelSpec mLabelSpec;
37
38    protected AlbumSetSlidingWindow mDataWindow;
39    private SlotView mSlotView;
40
41    private int mPressedIndex = -1;
42    private boolean mAnimatePressedUp;
43    private Path mHighlightItemPath = null;
44    private boolean mInSelectionMode;
45
46    public static class LabelSpec {
47        public int labelBackgroundHeight;
48        public int titleOffset;
49        public int countOffset;
50        public int titleFontSize;
51        public int countFontSize;
52        public int leftMargin;
53        public int iconSize;
54    }
55
56    public AlbumSetSlotRenderer(GalleryActivity activity, SelectionManager selectionManager,
57            SlotView slotView, LabelSpec labelSpec) {
58        super ((Context) activity);
59        mActivity = activity;
60        mSelectionManager = selectionManager;
61        mSlotView = slotView;
62        mLabelSpec = labelSpec;
63
64        mWaitLoadingTexture = new ColorTexture(PLACEHOLDER_COLOR);
65        mWaitLoadingTexture.setSize(1, 1);
66
67        Context context = activity.getAndroidContext();
68    }
69
70    public void setPressedIndex(int index) {
71        if (mPressedIndex == index) return;
72        mPressedIndex = index;
73        mSlotView.invalidate();
74    }
75
76    public void setPressedUp() {
77        if (mPressedIndex == -1) return;
78        mAnimatePressedUp = true;
79        mSlotView.invalidate();
80    }
81
82    public void setHighlightItemPath(Path path) {
83        if (mHighlightItemPath == path) return;
84        mHighlightItemPath = path;
85        mSlotView.invalidate();
86    }
87
88    public void setModel(AlbumSetDataLoader model) {
89        if (mDataWindow != null) {
90            mDataWindow.setListener(null);
91            mDataWindow = null;
92            mSlotView.setSlotCount(0);
93        }
94        if (model != null) {
95            mDataWindow = new AlbumSetSlidingWindow(
96                    mActivity, model, mLabelSpec, CACHE_SIZE);
97            mDataWindow.setListener(new MyCacheListener());
98            mSlotView.setSlotCount(mDataWindow.size());
99        }
100    }
101
102    private static Texture checkTexture(Texture texture) {
103        return ((texture instanceof UploadedTexture)
104                && ((UploadedTexture) texture).isUploading())
105                ? null
106                : texture;
107    }
108
109    @Override
110    public int renderSlot(GLCanvas canvas, int index, int pass, int width, int height) {
111        AlbumSetEntry entry = mDataWindow.get(index);
112        int renderRequestFlags = 0;
113        renderRequestFlags |= renderContent(canvas, entry, width, height);
114        renderRequestFlags |= renderLabel(canvas, entry, width, height);
115        renderRequestFlags |= renderOverlay(canvas, index, entry, width, height);
116        return renderRequestFlags;
117    }
118
119    protected int renderOverlay(
120            GLCanvas canvas, int index, AlbumSetEntry entry, int width, int height) {
121        int renderRequestFlags = 0;
122        if (mPressedIndex == index) {
123            if (mAnimatePressedUp) {
124                drawPressedUpFrame(canvas, width, height);
125                renderRequestFlags |= SlotView.RENDER_MORE_FRAME;
126                if (isPressedUpFrameFinished()) {
127                    mAnimatePressedUp = false;
128                    mPressedIndex = -1;
129                }
130            } else {
131                drawPressedFrame(canvas, width, height);
132            }
133        } else if ((mHighlightItemPath != null) && (mHighlightItemPath == entry.setPath)) {
134            drawSelectedFrame(canvas, width, height);
135        } else if (mInSelectionMode && mSelectionManager.isItemSelected(entry.setPath)) {
136            drawSelectedFrame(canvas, width, height);
137        }
138        return renderRequestFlags;
139    }
140
141    protected int renderContent(
142            GLCanvas canvas, AlbumSetEntry entry, int width, int height) {
143        int renderRequestFlags = 0;
144
145        Texture content = checkTexture(entry.content);
146        if (content == null) {
147            content = mWaitLoadingTexture;
148            entry.isWaitLoadingDisplayed = true;
149        } else if (entry.isWaitLoadingDisplayed) {
150            entry.isWaitLoadingDisplayed = false;
151            content = new FadeInTexture(PLACEHOLDER_COLOR, entry.bitmapTexture);
152            entry.content = content;
153        }
154        drawContent(canvas, content, width, height, entry.rotation);
155        if ((content instanceof FadeInTexture) &&
156                ((FadeInTexture) content).isAnimating()) {
157            renderRequestFlags |= SlotView.RENDER_MORE_FRAME;
158        }
159
160        if (entry.mediaType == MediaObject.MEDIA_TYPE_VIDEO) {
161            drawVideoOverlay(canvas, width, height);
162        }
163
164        if (entry.isPanorama) {
165            drawPanoramaBorder(canvas, width, height);
166        }
167
168        return renderRequestFlags;
169    }
170
171    protected int renderLabel(
172            GLCanvas canvas, AlbumSetEntry entry, int width, int height) {
173        Texture content = checkTexture(entry.labelTexture);
174        if (content != null) {
175            int b = AlbumLabelMaker.getBorderSize();
176            int h = content.getHeight();
177            content.draw(canvas, -b, height - h + b, width + b + b, h);
178        }
179        return 0;
180    }
181
182    @Override
183    public void prepareDrawing() {
184        mInSelectionMode = mSelectionManager.inSelectionMode();
185    }
186
187    private class MyCacheListener implements AlbumSetSlidingWindow.Listener {
188
189        @Override
190        public void onSizeChanged(int size) {
191            mSlotView.setSlotCount(size);
192        }
193
194        @Override
195        public void onContentChanged() {
196            mSlotView.invalidate();
197        }
198    }
199
200    public void pause() {
201        mDataWindow.pause();
202    }
203
204    public void resume() {
205        mDataWindow.resume();
206    }
207
208    @Override
209    public void onVisibleRangeChanged(int visibleStart, int visibleEnd) {
210        if (mDataWindow != null) {
211            mDataWindow.setActiveWindow(visibleStart, visibleEnd);
212        }
213    }
214
215    @Override
216    public void onSlotSizeChanged(int width, int height) {
217        if (mDataWindow != null) {
218            mDataWindow.onSlotSizeChanged(width, height);
219        }
220    }
221}
222