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 17 18 package com.android.musicvis; 19 20 import android.service.wallpaper.WallpaperService; 21 import android.renderscript.RenderScript; 22 import android.view.MotionEvent; 23 import android.view.SurfaceHolder; 24 import android.view.Surface; 25 26 public abstract class RenderScriptWallpaper<T extends RenderScriptScene> extends WallpaperService { onCreateEngine()27 public Engine onCreateEngine() { 28 return new RenderScriptEngine(); 29 } 30 createScene(int width, int height)31 protected abstract T createScene(int width, int height); 32 33 private class RenderScriptEngine extends Engine { 34 private RenderScript mRs; 35 private T mRenderer; 36 37 @Override onCreate(SurfaceHolder surfaceHolder)38 public void onCreate(SurfaceHolder surfaceHolder) { 39 super.onCreate(surfaceHolder); 40 setTouchEventsEnabled(true); 41 surfaceHolder.setSizeFromLayout(); 42 } 43 44 @Override onDestroy()45 public void onDestroy() { 46 super.onDestroy(); 47 destroyRenderer(); 48 } 49 destroyRenderer()50 private void destroyRenderer() { 51 if (mRenderer != null) { 52 mRenderer.stop(); 53 mRenderer = null; 54 } 55 if (mRs != null) { 56 mRs.destroy(); 57 mRs = null; 58 } 59 } 60 61 @Override onVisibilityChanged(boolean visible)62 public void onVisibilityChanged(boolean visible) { 63 super.onVisibilityChanged(visible); 64 if (mRenderer != null) { 65 if (visible) { 66 mRenderer.start(); 67 } else { 68 mRenderer.stop(); 69 } 70 } 71 } 72 73 @Override onSurfaceChanged(SurfaceHolder holder, int format, int width, int height)74 public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { 75 super.onSurfaceChanged(holder, format, width, height); 76 if (mRs != null) { 77 mRs.contextSetSurface(width, height, holder.getSurface()); 78 } 79 if (mRenderer == null) { 80 mRenderer = createScene(width, height); 81 mRenderer.init(mRs, getResources(), isPreview()); 82 mRenderer.start(); 83 } else { 84 mRenderer.resize(width, height); 85 } 86 } 87 88 @Override onTouchEvent(MotionEvent event)89 public void onTouchEvent(MotionEvent event) { 90 mRenderer.onTouchEvent(event); 91 } 92 93 @Override onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels)94 public void onOffsetsChanged(float xOffset, float yOffset, 95 float xStep, float yStep, int xPixels, int yPixels) { 96 mRenderer.setOffset(xOffset, yOffset, xStep, yStep, xPixels, yPixels); 97 } 98 99 @Override onSurfaceCreated(SurfaceHolder holder)100 public void onSurfaceCreated(SurfaceHolder holder) { 101 super.onSurfaceCreated(holder); 102 103 Surface surface = null; 104 while (surface == null) { 105 surface = holder.getSurface(); 106 } 107 mRs = new RenderScript(false, false); 108 } 109 110 @Override onSurfaceDestroyed(SurfaceHolder holder)111 public void onSurfaceDestroyed(SurfaceHolder holder) { 112 super.onSurfaceDestroyed(holder); 113 destroyRenderer(); 114 } 115 } 116 } 117