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 import android.util.Log; 14 import android.view.MotionEvent; 15 16 public class NoiseFieldWallpaper extends WallpaperService { 17 18 @Override onCreateEngine()19 public Engine onCreateEngine() { 20 return new RenderScriptEngine(); 21 } 22 23 private class RenderScriptEngine extends Engine { 24 private RenderScriptGL mRenderScript = null; 25 private NoiseFieldRS mWallpaperRS = null; 26 private int mDensityDPI; 27 28 @Override onCreate(SurfaceHolder surfaceHolder)29 public void onCreate(SurfaceHolder surfaceHolder) { 30 super.onCreate(surfaceHolder); 31 setTouchEventsEnabled(true); 32 surfaceHolder.setSizeFromLayout(); 33 surfaceHolder.setFormat(PixelFormat.RGB_888); 34 35 DisplayMetrics metrics = new DisplayMetrics(); 36 ((WindowManager) getApplication().getSystemService(Service.WINDOW_SERVICE)) 37 .getDefaultDisplay().getMetrics(metrics); 38 mDensityDPI = metrics.densityDpi; 39 } 40 41 @Override onDestroy()42 public void onDestroy() { 43 super.onDestroy(); 44 destroyRenderer(); 45 } 46 destroyRenderer()47 public void destroyRenderer() { 48 if (mWallpaperRS != null) { 49 mWallpaperRS.stop(); 50 mWallpaperRS = null; 51 } 52 53 if (mRenderScript != null) { 54 mRenderScript.setSurface(null, 0, 0); 55 mRenderScript.destroy(); 56 mRenderScript = null; 57 } 58 } 59 60 @Override onSurfaceCreated(SurfaceHolder surfaceHolder)61 public void onSurfaceCreated(SurfaceHolder surfaceHolder) { 62 super.onSurfaceCreated(surfaceHolder); 63 64 RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig(); 65 mRenderScript = new RenderScriptGL(NoiseFieldWallpaper.this, sc); 66 mRenderScript.setPriority(RenderScript.Priority.LOW); 67 } 68 69 @Override onSurfaceDestroyed(SurfaceHolder surfaceHolder)70 public void onSurfaceDestroyed(SurfaceHolder surfaceHolder) { 71 super.onSurfaceDestroyed(surfaceHolder); 72 destroyRenderer(); 73 } 74 75 @Override onSurfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height)76 public void onSurfaceChanged(SurfaceHolder surfaceHolder, 77 int format, 78 int width, 79 int height) { 80 super.onSurfaceChanged(surfaceHolder, format, width, height); 81 82 if (mRenderScript != null) { 83 mRenderScript.setSurface(surfaceHolder, width, height); 84 } 85 86 if (mWallpaperRS == null) { 87 mWallpaperRS = new NoiseFieldRS(); 88 mWallpaperRS.init(mDensityDPI, mRenderScript, getResources(), width, height); 89 mWallpaperRS.start(); 90 } 91 92 mWallpaperRS.resize(width, height); 93 } 94 95 @Override onTouchEvent(MotionEvent ev)96 public void onTouchEvent(MotionEvent ev) { 97 super.onTouchEvent(ev); 98 if (mWallpaperRS != null) { 99 mWallpaperRS.onTouchEvent(ev); 100 } 101 } 102 103 @Override onVisibilityChanged(boolean visible)104 public void onVisibilityChanged(boolean visible) { 105 super.onVisibilityChanged(visible); 106 if (mWallpaperRS != null) { 107 if (visible) { 108 mWallpaperRS.start(); 109 } else { 110 mWallpaperRS.stop(); 111 } 112 } 113 } 114 } 115 } 116