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 17 package com.android.dreams.basic; 18 19 import android.graphics.SurfaceTexture; 20 import android.service.dreams.DreamService; 21 import android.util.Log; 22 import android.view.TextureView; 23 import android.os.Handler; 24 import 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 */ 32 public class Colors extends DreamService implements TextureView.SurfaceTextureListener { 33 static final String TAG = Colors.class.getSimpleName(); 34 static final boolean DEBUG = false; 35 LOG(String fmt, Object... args)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 onCreate()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 onAttachedToWindow()67 public void onAttachedToWindow() { 68 super.onAttachedToWindow(); 69 setInteractive(false); 70 setLowProfile(true); 71 setFullscreen(true); 72 setContentView(mTextureView); 73 } 74 75 @Override onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height)76 public void onSurfaceTextureAvailable(final SurfaceTexture surface, 77 final int width, final int height) { 78 LOG("onSurfaceTextureAvailable(%s, %d, %d)", surface, width, height); 79 80 mRendererHandler.post(new Runnable() { 81 @Override 82 public void run() { 83 if (mRenderer != null) { 84 mRenderer.stop(); 85 } 86 mRenderer = new ColorsGLRenderer(surface, width, height); 87 mRenderer.start(); 88 } 89 }); 90 } 91 92 @Override onSurfaceTextureSizeChanged(SurfaceTexture surface, final int width, final int height)93 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, 94 final int width, final int height) { 95 LOG("onSurfaceTextureSizeChanged(%s, %d, %d)", surface, width, height); 96 97 mRendererHandler.post(new Runnable() { 98 @Override 99 public void run() { 100 if (mRenderer != null) { 101 mRenderer.setSize(width, height); 102 } 103 } 104 }); 105 } 106 107 @Override onSurfaceTextureDestroyed(SurfaceTexture surface)108 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 109 LOG("onSurfaceTextureDestroyed(%s)", surface); 110 111 mRendererHandler.post(new Runnable() { 112 @Override 113 public void run() { 114 if (mRenderer != null) { 115 mRenderer.stop(); 116 mRenderer = null; 117 } 118 mRendererHandlerThread.quit(); 119 } 120 }); 121 122 try { 123 mRendererHandlerThread.join(); 124 } catch (InterruptedException e) { 125 LOG("Error while waiting for renderer", e); 126 } 127 128 return true; 129 } 130 131 @Override onSurfaceTextureUpdated(SurfaceTexture surface)132 public void onSurfaceTextureUpdated(SurfaceTexture surface) { 133 LOG("onSurfaceTextureUpdated(%s)", surface); 134 } 135 } 136