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.service.dreams.DreamService; 20 import android.util.Log; 21 import android.view.SurfaceHolder; 22 import android.view.SurfaceView; 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 SurfaceHolder.Callback { 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 SurfaceView mSurfaceView; 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 mSurfaceView = new SurfaceView(this); 57 mSurfaceView.getHolder().addCallback(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 setFullscreen(true); 71 setContentView(mSurfaceView); 72 } 73 74 @Override surfaceCreated(SurfaceHolder holder)75 public void surfaceCreated(SurfaceHolder holder) { 76 LOG("surfaceCreated(%s, %d, %d)", holder.getSurface(), 77 holder.getSurfaceFrame().width(), holder.getSurfaceFrame().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(holder.getSurface(), 86 holder.getSurfaceFrame().width(), holder.getSurfaceFrame().height()); 87 mRenderer.start(); 88 } 89 }); 90 } 91 92 @Override surfaceChanged(SurfaceHolder holder, int format, int width, int height)93 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 94 LOG("surfaceChanged(%s, %d, %d, %d)", holder.getSurface(), format, 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 surfaceDestroyed(SurfaceHolder holder)107 public void surfaceDestroyed(SurfaceHolder holder) { 108 LOG("surfaceDestroyed(%s)", holder.getSurface()); 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 } 128