1 package com.android.noisefield; 2 3 import android.app.Service; 4 import android.graphics.PixelFormat; 5 import android.os.Bundle; 6 import android.renderscript.RenderScript; 7 import android.renderscript.RenderScriptGL; 8 import android.service.wallpaper.WallpaperService; 9 import android.service.wallpaper.WallpaperService.Engine; 10 import android.util.DisplayMetrics; 11 import android.view.SurfaceHolder; 12 import android.view.WindowManager; 13 14 public class NoiseFieldWallpaper extends WallpaperService { 15 16 @Override 17 public Engine onCreateEngine() { 18 return new RenderScriptEngine(); 19 } 20 21 private class RenderScriptEngine extends Engine { 22 private RenderScriptGL mRenderScript = null; 23 private NoiseFieldRS mWallpaperRS = null; 24 private int densityDPI; 25 26 @Override 27 public void onCreate(SurfaceHolder surfaceHolder) { 28 super.onCreate(surfaceHolder); 29 setTouchEventsEnabled(true); 30 surfaceHolder.setSizeFromLayout(); 31 surfaceHolder.setFormat(PixelFormat.RGB_888); 32 33 DisplayMetrics metrics = new DisplayMetrics(); 34 ((WindowManager) getApplication().getSystemService(Service.WINDOW_SERVICE)) 35 .getDefaultDisplay().getMetrics(metrics); 36 densityDPI = metrics.densityDpi; 37 } 38 39 @Override 40 public void onDestroy() { 41 super.onDestroy(); 42 destroyRenderer(); 43 } 44 45 public void destroyRenderer() { 46 if (mWallpaperRS != null) { 47 mWallpaperRS.stop(); 48 mWallpaperRS = null; 49 } 50 51 if (mRenderScript != null) { 52 mRenderScript.setSurface(null, 0, 0); 53 mRenderScript.destroy(); 54 mRenderScript = null; 55 } 56 } 57 58 @Override 59 public void onSurfaceCreated(SurfaceHolder surfaceHolder) { 60 super.onSurfaceCreated(surfaceHolder); 61 62 RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig(); 63 mRenderScript = new RenderScriptGL(NoiseFieldWallpaper.this, sc); 64 mRenderScript.setPriority(RenderScript.Priority.LOW); 65 } 66 67 @Override 68 public void onSurfaceDestroyed(SurfaceHolder surfaceHolder) { 69 super.onSurfaceDestroyed(surfaceHolder); 70 destroyRenderer(); 71 } 72 73 @Override 74 public void onSurfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) { 75 super.onSurfaceChanged(surfaceHolder, format, width, height); 76 77 if (mRenderScript != null) { 78 mRenderScript.setSurface(surfaceHolder, width, height); 79 } 80 81 if (mWallpaperRS == null) { 82 mWallpaperRS = new NoiseFieldRS(); 83 mWallpaperRS.init(densityDPI, mRenderScript, getResources(), width, height); 84 mWallpaperRS.start(); 85 } 86 87 mWallpaperRS.resize(width, height); 88 } 89 90 @Override 91 public Bundle onCommand(String action, int x, int y, int z, Bundle extras, 92 boolean resultRequested) { 93 if (mWallpaperRS != null) { 94 // return mWallpaperRS.onCommand(action, x, y, z, extras, resultRequested); 95 } 96 return null; 97 } 98 99 @Override 100 public void onVisibilityChanged(boolean visible) { 101 super.onVisibilityChanged(visible); 102 if (mWallpaperRS != null) { 103 if (visible) { 104 mWallpaperRS.start(); 105 } else { 106 mWallpaperRS.stop(); 107 } 108 } 109 } 110 111 @Override 112 public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, 113 float yOffsetStep, int xPixelOffset, int yPixelOffset) { 114 mWallpaperRS.setOffset(xOffset, yOffset, xPixelOffset, yPixelOffset); 115 } 116 } 117 } 118