blob: 7a4b651a6ea3ca36da39740cd04f46563c6c39cb [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
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.app.GalleryActivity;
Owen Lina2fba682011-08-17 22:07:43 +080020import com.android.gallery3d.data.MediaItem;
21import com.android.gallery3d.data.MediaSet;
Owen Lina2fba682011-08-17 22:07:43 +080022
Owen Lin83a036c2012-03-22 14:14:40 +080023public class AlbumSetView implements SlotView.SlotRenderer {
Owen Lina2fba682011-08-17 22:07:43 +080024 @SuppressWarnings("unused")
25 private static final String TAG = "AlbumSetView";
26 private static final int CACHE_SIZE = 32;
Owen Lina2fba682011-08-17 22:07:43 +080027
28 private AlbumSetSlidingWindow mDataWindow;
29 private final GalleryActivity mActivity;
Chih-Chung Chang07069de2011-09-14 20:50:28 +080030 private final LabelSpec mLabelSpec;
Owen Lina2fba682011-08-17 22:07:43 +080031
32 private SelectionDrawer mSelectionDrawer;
Owen Lin83a036c2012-03-22 14:14:40 +080033 private SlotView mSlotView;
Owen Lina2fba682011-08-17 22:07:43 +080034
35 public static interface Model {
36 public MediaItem[] getCoverItems(int index);
37 public MediaSet getMediaSet(int index);
38 public int size();
39 public void setActiveWindow(int start, int end);
40 public void setModelListener(ModelListener listener);
41 }
42
43 public static interface ModelListener {
44 public void onWindowContentChanged(int index);
45 public void onSizeChanged(int size);
46 }
47
48 public static class AlbumSetItem {
49 public DisplayItem[] covers;
50 public DisplayItem labelItem;
51 public long setDataVersion;
52 }
53
Chih-Chung Chang07069de2011-09-14 20:50:28 +080054 public static class LabelSpec {
Chih-Chung Chang70a73a72011-09-19 11:09:39 +080055 public int labelBackgroundHeight;
Chih-Chung Chang07069de2011-09-14 20:50:28 +080056 public int titleOffset;
Chih-Chung Chang70a73a72011-09-19 11:09:39 +080057 public int countOffset;
Chih-Chung Chang07069de2011-09-14 20:50:28 +080058 public int titleFontSize;
Chih-Chung Chang70a73a72011-09-19 11:09:39 +080059 public int countFontSize;
Chih-Chung Chang07069de2011-09-14 20:50:28 +080060 public int leftMargin;
61 public int iconSize;
62 }
63
Owen Lina2fba682011-08-17 22:07:43 +080064 public AlbumSetView(GalleryActivity activity, SelectionDrawer drawer,
Owen Lin83a036c2012-03-22 14:14:40 +080065 SlotView slotView, LabelSpec labelSpec) {
Owen Lina2fba682011-08-17 22:07:43 +080066 mActivity = activity;
67 setSelectionDrawer(drawer);
Chih-Chung Chang07069de2011-09-14 20:50:28 +080068 mLabelSpec = labelSpec;
Owen Lin83a036c2012-03-22 14:14:40 +080069 mSlotView = slotView;
Owen Lina2fba682011-08-17 22:07:43 +080070 }
71
72 public void setSelectionDrawer(SelectionDrawer drawer) {
73 mSelectionDrawer = drawer;
74 if (mDataWindow != null) {
75 mDataWindow.setSelectionDrawer(drawer);
76 }
77 }
78
79 public void setModel(AlbumSetView.Model model) {
80 if (mDataWindow != null) {
81 mDataWindow.setListener(null);
Owen Lina2fba682011-08-17 22:07:43 +080082 mDataWindow = null;
Owen Lin83a036c2012-03-22 14:14:40 +080083 mSlotView.setSlotCount(0);
Owen Lina2fba682011-08-17 22:07:43 +080084 }
85 if (model != null) {
Chih-Chung Chang07069de2011-09-14 20:50:28 +080086 mDataWindow = new AlbumSetSlidingWindow(mActivity, mLabelSpec,
Owen Lina2fba682011-08-17 22:07:43 +080087 mSelectionDrawer, model, CACHE_SIZE);
88 mDataWindow.setListener(new MyCacheListener());
Owen Lin83a036c2012-03-22 14:14:40 +080089 mSlotView.setSlotCount(mDataWindow.size());
Owen Lina2fba682011-08-17 22:07:43 +080090 }
91 }
92
Owen Lin83a036c2012-03-22 14:14:40 +080093 @Override
94 public int renderSlot(GLCanvas canvas, int index, int pass, int width, int height) {
95 AlbumSetItem entry = mDataWindow.get(index);
96 DisplayItem cover = entry.covers.length > 0 ? entry.covers[0] : null;
97 DisplayItem label = entry.labelItem;
Owen Lina2fba682011-08-17 22:07:43 +080098
99 // Put the cover items in reverse order, so that the first item is on
100 // top of the rest.
Owen Lin83a036c2012-03-22 14:14:40 +0800101 canvas.translate(width / 2, height / 2);
102 int r = 0;
103 if (cover != null) {
104 cover.setBox(width, height);
105 r |= cover.render(canvas, pass);
Owen Lina2fba682011-08-17 22:07:43 +0800106 }
Owen Lin83a036c2012-03-22 14:14:40 +0800107 if (label != null) {
108 label.setBox(width, height);
109 r |= entry.labelItem.render(canvas, pass);
Owen Lina2fba682011-08-17 22:07:43 +0800110 }
Owen Lin83a036c2012-03-22 14:14:40 +0800111 canvas.translate(-width / 2, -height / 2);
112 return r;
Owen Lina2fba682011-08-17 22:07:43 +0800113 }
114
Owen Lina2fba682011-08-17 22:07:43 +0800115 @Override
Owen Lin83a036c2012-03-22 14:14:40 +0800116 public void prepareDrawing() {
Owen Lina2fba682011-08-17 22:07:43 +0800117 mSelectionDrawer.prepareDrawing();
Owen Lina2fba682011-08-17 22:07:43 +0800118 }
119
120 private class MyCacheListener implements AlbumSetSlidingWindow.Listener {
121
Owen Lin83a036c2012-03-22 14:14:40 +0800122 @Override
Owen Lina2fba682011-08-17 22:07:43 +0800123 public void onSizeChanged(int size) {
Owen Lin83a036c2012-03-22 14:14:40 +0800124 mSlotView.setSlotCount(size);
Owen Lina2fba682011-08-17 22:07:43 +0800125 }
126
Owen Lin83a036c2012-03-22 14:14:40 +0800127 @Override
128 public void onContentChanged() {
129 mSlotView.invalidate();
Owen Lina2fba682011-08-17 22:07:43 +0800130 }
131 }
132
133 public void pause() {
Owen Lina2fba682011-08-17 22:07:43 +0800134 mDataWindow.pause();
135 }
136
137 public void resume() {
138 mDataWindow.resume();
Owen Lin83a036c2012-03-22 14:14:40 +0800139 }
140
141 @Override
142 public void onVisibleRangeChanged(int visibleStart, int visibleEnd) {
143 if (mDataWindow != null) {
144 mDataWindow.setActiveWindow(visibleStart, visibleEnd);
Owen Lina2fba682011-08-17 22:07:43 +0800145 }
146 }
147}