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 package com.android.wallpaper; 18 19 import android.service.wallpaper.WallpaperService; 20 import android.os.Bundle; 21 import android.renderscript.RenderScriptGL; 22 import android.renderscript.RenderScript; 23 import android.view.SurfaceHolder; 24 import android.view.Surface; 25 26 public abstract class RenderScriptWallpaper<T extends RenderScriptScene> extends WallpaperService { 27 public Engine onCreateEngine() { 28 return new RenderScriptEngine(); 29 } 30 31 protected abstract T createScene(int width, int height); 32 33 private class RenderScriptEngine extends Engine { 34 private RenderScriptGL mRs; 35 private T mRenderer; 36 37 @Override 38 public void onCreate(SurfaceHolder surfaceHolder) { 39 super.onCreate(surfaceHolder); 40 setTouchEventsEnabled(false); 41 surfaceHolder.setSizeFromLayout(); 42 } 43 44 @Override 45 public void onDestroy() { 46 super.onDestroy(); 47 destroyRenderer(); 48 } 49 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 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 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.setSurface(holder, width, height); 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 89 public void onOffsetsChanged(float xOffset, float yOffset, 90 float xStep, float yStep, int xPixels, int yPixels) { 91 mRenderer.setOffset(xOffset, yOffset, xPixels, yPixels); 92 } 93 94 @Override 95 public void onSurfaceCreated(SurfaceHolder holder) { 96 super.onSurfaceCreated(holder); 97 98 RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig(); 99 mRs = new RenderScriptGL(RenderScriptWallpaper.this, sc); 100 mRs.setPriority(RenderScript.Priority.LOW); 101 } 102 103 @Override 104 public void onSurfaceDestroyed(SurfaceHolder holder) { 105 super.onSurfaceDestroyed(holder); 106 destroyRenderer(); 107 } 108 109 @Override 110 public Bundle onCommand(String action, int x, int y, int z, 111 Bundle extras, boolean resultRequested) { 112 return mRenderer.onCommand(action, x, y, z, extras, resultRequested); 113 } 114 115 } 116 } 117