1/*
2 * Copyright (C) 2012 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.dreams.basic;
18
19import android.graphics.SurfaceTexture;
20import android.service.dreams.DreamService;
21import android.util.Log;
22import android.view.TextureView;
23import android.os.Handler;
24import android.os.HandlerThread;
25
26/**
27 * Plays a delightful show of colors.
28 * <p>
29 * This dream performs its rendering using OpenGL on a separate rendering thread.
30 * </p>
31 */
32public class Colors extends DreamService implements TextureView.SurfaceTextureListener {
33    static final String TAG = Colors.class.getSimpleName();
34    static final boolean DEBUG = false;
35
36    public static void LOG(String fmt, Object... args) {
37        if (!DEBUG) return;
38        Log.v(TAG, String.format(fmt, args));
39    }
40
41    private TextureView mTextureView;
42
43    // The handler thread and handler on which the GL renderer is running.
44    private HandlerThread mRendererHandlerThread;
45    private Handler mRendererHandler;
46
47    // The current GL renderer, or null if the dream is not running.
48    private ColorsGLRenderer mRenderer;
49
50    @Override
51    public void onCreate() {
52        super.onCreate();
53
54        setInteractive(false);
55
56        mTextureView = new TextureView(this);
57        mTextureView.setSurfaceTextureListener(this);
58
59        if (mRendererHandlerThread == null) {
60            mRendererHandlerThread = new HandlerThread(TAG);
61            mRendererHandlerThread.start();
62            mRendererHandler = new Handler(mRendererHandlerThread.getLooper());
63        }
64    }
65
66    @Override
67    public void onAttachedToWindow() {
68        setInteractive(false);
69        setLowProfile(true);
70        setFullscreen(true);
71        setContentView(mTextureView);
72    }
73
74    @Override
75    public void onSurfaceTextureAvailable(final SurfaceTexture surface,
76            final int width, final int height) {
77        LOG("onSurfaceTextureAvailable(%s, %d, %d)", surface, width, height);
78
79        mRendererHandler.post(new Runnable() {
80            @Override
81            public void run() {
82                if (mRenderer != null) {
83                    mRenderer.stop();
84                }
85                mRenderer = new ColorsGLRenderer(surface, width, height);
86                mRenderer.start();
87            }
88        });
89    }
90
91    @Override
92    public void onSurfaceTextureSizeChanged(SurfaceTexture surface,
93            final int width, final int height) {
94        LOG("onSurfaceTextureSizeChanged(%s, %d, %d)", surface, width, height);
95
96        mRendererHandler.post(new Runnable() {
97            @Override
98            public void run() {
99                if (mRenderer != null) {
100                    mRenderer.setSize(width, height);
101                }
102            }
103        });
104    }
105
106    @Override
107    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
108        LOG("onSurfaceTextureDestroyed(%s)", surface);
109
110        mRendererHandler.post(new Runnable() {
111            @Override
112            public void run() {
113                if (mRenderer != null) {
114                    mRenderer.stop();
115                    mRenderer = null;
116                }
117                mRendererHandlerThread.quit();
118            }
119        });
120
121        try {
122            mRendererHandlerThread.join();
123        } catch (InterruptedException e) {
124            LOG("Error while waiting for renderer", e);
125        }
126
127        return true;
128    }
129
130    @Override
131    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
132        LOG("onSurfaceTextureUpdated(%s)", surface);
133    }
134}
135