1/*
2 * Copyright (C) 2009 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.cooliris.media;
18
19import java.nio.ByteBuffer;
20import java.nio.ByteOrder;
21import java.nio.IntBuffer;
22
23import javax.microedition.khronos.opengles.GL11;
24
25import android.os.SystemClock;
26
27import com.cooliris.app.Res;
28
29public final class LoadingLayer extends Layer {
30    private static final float FADE_INTERVAL = 0.5f;
31    private static final float GRAY_VALUE = 0.1f;
32    private static final int[] PRELOAD_RESOURCES_ASYNC_UNSCALED = { Res.drawable.stack_frame, Res.drawable.grid_frame,
33            Res.drawable.stack_frame_focus, Res.drawable.stack_frame_gold, Res.drawable.btn_location_filter_unscaled,
34            Res.drawable.videooverlay, Res.drawable.grid_check_on, Res.drawable.grid_check_off, Res.drawable.icon_camera_small_unscaled,
35            Res.drawable.icon_picasa_small_unscaled };
36
37    private static final int[] PRELOAD_RESOURCES_ASYNC_SCALED = {/*
38                                                                  * Res.drawable.btn_camera_pressed
39                                                                  * ,
40                                                                  * Res.drawable.
41                                                                  * btn_camera_focus
42                                                                  * ,
43                                                                  * Res.drawable.
44                                                                  * fullscreen_hud_bg
45                                                                  * ,
46                                                                  * Res.drawable.
47                                                                  * icon_delete,
48                                                                  * Res.drawable.
49                                                                  * icon_edit,
50                                                                  * Res.drawable.
51                                                                  * icon_more,
52                                                                  * Res.drawable.
53                                                                  * icon_share,
54                                                                  * Res.drawable.
55                                                                  * selection_bg_upper
56                                                                  * ,
57                                                                  * Res.drawable.
58                                                                  * selection_menu_bg
59                                                                  * ,
60                                                                  * Res.drawable.
61                                                                  * selection_menu_bg_pressed
62                                                                  * ,
63                                                                  * Res.drawable.
64                                                                  * selection_menu_bg_pressed_left
65                                                                  * ,
66                                                                  * Res.drawable.
67                                                                  * selection_menu_bg_pressed_right
68                                                                  * ,
69                                                                  * Res.drawable.
70                                                                  * selection_menu_divider
71                                                                  * ,
72                                                                  * Res.drawable.
73                                                                  * timebar_bg,
74                                                                  * Res.drawable.
75                                                                  * timebar_knob
76                                                                  * ,
77                                                                  * Res.drawable.
78                                                                  * timebar_knob_pressed
79                                                                  * ,
80                                                                  * Res.drawable.
81                                                                  * timebar_prev
82                                                                  * ,
83                                                                  * Res.drawable.
84                                                                  * timebar_next
85                                                                  * ,
86                                                                  * Res.drawable.
87                                                                  * mode_grid,
88                                                                  * Res.drawable.
89                                                                  * mode_stack,
90                                                                  * Res.drawable.
91                                                                  * icon_camera_small
92                                                                  * ,
93                                                                  * Res.drawable.
94                                                                  * icon_location_small
95                                                                  * ,
96                                                                  * Res.drawable.
97                                                                  * icon_picasa_small
98                                                                  * ,
99                                                                  * Res.drawable.
100                                                                  * icon_folder_small
101                                                                  * ,
102                                                                  * Res.drawable.
103                                                                  * scroller_new
104                                                                  * ,
105                                                                  * Res.drawable.
106                                                                  * scroller_pressed_new
107                                                                  * ,
108                                                                  * Res.drawable.
109                                                                  * btn_camera,
110                                                                  * Res.drawable.
111                                                                  * btn_play,
112                                                                  * Res.drawable
113                                                                  * .pathbar_bg,
114                                                                  * Res.drawable.
115                                                                  * pathbar_cap,
116                                                                  * Res.drawable.
117                                                                  * pathbar_join
118                                                                  * ,
119                                                                  * Res.drawable.
120                                                                  * transparent,
121                                                                  * Res.drawable.
122                                                                  * icon_home_small
123                                                                  * ,
124                                                                  * Res.drawable.
125                                                                  * ic_fs_details
126                                                                  * ,
127                                                                  * Res.drawable.
128                                                                  * ic_spinner1,
129                                                                  * Res.drawable.
130                                                                  * ic_spinner2,
131                                                                  * Res.drawable.
132                                                                  * ic_spinner3,
133                                                                  * Res.drawable.
134                                                                  * ic_spinner4,
135                                                                  * Res.drawable.
136                                                                  * ic_spinner5,
137                                                                  * Res.drawable.
138                                                                  * ic_spinner6,
139                                                                  * Res.drawable.
140                                                                  * ic_spinner7,
141                                                                  * Res.drawable.
142                                                                  * ic_spinner8
143                                                                  */};
144
145    private boolean mLoaded = false;
146    private final FloatAnim mOpacity = new FloatAnim(1f);
147    private IntBuffer mVertexBuffer;
148
149    public LoadingLayer() {
150        // Create vertex buffer for a screen-spanning quad.
151        int dimension = 10000 * 0x10000;
152        int[] vertices = { -dimension, -dimension, 0, dimension, -dimension, 0, -dimension, dimension, 0, dimension, dimension, 0 };
153        ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
154        vertexByteBuffer.order(ByteOrder.nativeOrder());
155        mVertexBuffer = vertexByteBuffer.asIntBuffer();
156        mVertexBuffer.put(vertices);
157        mVertexBuffer.position(0);
158    }
159
160    public boolean isLoaded() {
161        return true;
162    }
163
164    @Override
165    public void generate(RenderView view, RenderView.Lists lists) {
166        // Add to drawing list.
167        lists.blendedList.add(this);
168
169        // Start loading textures.
170        int[] textures = PRELOAD_RESOURCES_ASYNC_UNSCALED;
171        for (int i = 0; i != textures.length; ++i) {
172            view.loadTexture(view.getResource(textures[i], false));
173        }
174        textures = PRELOAD_RESOURCES_ASYNC_SCALED;
175        for (int i = 0; i != textures.length; ++i) {
176            view.loadTexture(view.getResource(textures[i]));
177        }
178    }
179
180    @Override
181    public void renderBlended(RenderView view, GL11 gl) {
182        // Wait for textures to finish loading before fading out.
183        if (!mLoaded) {
184            // Request that the view upload all loaded textures.
185            view.processAllTextures();
186
187            // Determine if all textures have loaded.
188            int[] textures = PRELOAD_RESOURCES_ASYNC_SCALED;
189            boolean complete = true;
190            for (int i = 0; i != textures.length; ++i) {
191                if (view.getResource(textures[i]).mState != Texture.STATE_LOADED) {
192                    complete = false;
193                    break;
194                }
195            }
196            textures = PRELOAD_RESOURCES_ASYNC_UNSCALED;
197            for (int i = 0; i != textures.length; ++i) {
198                if (view.getResource(textures[i], false).mState != Texture.STATE_LOADED) {
199                    complete = false;
200                    break;
201                }
202            }
203            if (complete) {
204                mLoaded = true;
205                mOpacity.animateValue(0f, FADE_INTERVAL, SystemClock.uptimeMillis());
206            }
207        }
208
209        // Draw the loading screen.
210        float alpha = mOpacity.getValue(view.getFrameTime());
211        if (alpha > 0.004f) {
212            float gray = GRAY_VALUE * alpha;
213            gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
214            gl.glColor4f(gray, gray, gray, alpha);
215            gl.glVertexPointer(3, GL11.GL_FIXED, 0, mVertexBuffer);
216            gl.glDisable(GL11.GL_TEXTURE_2D);
217            gl.glDisable(GL11.GL_DEPTH_TEST);
218            gl.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, 4);
219            gl.glEnable(GL11.GL_DEPTH_TEST);
220            gl.glEnable(GL11.GL_TEXTURE_2D);
221            view.resetColor();
222        } else {
223            // Hide the layer once completely faded out.
224            setHidden(true);
225        }
226    }
227
228    void reset() {
229        mLoaded = false;
230        mOpacity.setValue(1.0f);
231        setHidden(false);
232    }
233}
234