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