1 /* 2 * Copyright (C) 2010 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.galaxy4; 18 19 import android.graphics.PixelFormat; 20 import android.os.Bundle; 21 import android.os.Handler; 22 import android.os.Message; 23 import android.renderscript.RenderScript; 24 import android.renderscript.RenderScriptGL; 25 import android.util.DisplayMetrics; 26 import android.service.wallpaper.WallpaperService; 27 import android.view.Surface; 28 import android.view.SurfaceHolder; 29 import android.view.WindowManager; 30 import android.app.Service; 31 32 public class Galaxy4Wallpaper extends WallpaperService { 33 34 @Override onCreateEngine()35 public Engine onCreateEngine() { 36 return new RenderScriptEngine(); 37 } 38 39 private class RenderScriptEngine extends Engine { 40 private RenderScriptGL mRenderScript = null; 41 private GalaxyRS mWallpaperRS = null; 42 private int mDensityDPI; 43 44 @Override onCreate(SurfaceHolder surfaceHolder)45 public void onCreate(SurfaceHolder surfaceHolder) { 46 super.onCreate(surfaceHolder); 47 setTouchEventsEnabled(true); 48 surfaceHolder.setSizeFromLayout(); 49 surfaceHolder.setFormat(PixelFormat.RGBA_8888); 50 51 DisplayMetrics metrics = new DisplayMetrics(); 52 ((WindowManager) getApplication().getSystemService(Service.WINDOW_SERVICE)) 53 .getDefaultDisplay().getMetrics(metrics); 54 mDensityDPI = metrics.densityDpi; 55 } 56 57 @Override onDestroy()58 public void onDestroy() { 59 super.onDestroy(); 60 destroyRenderer(); 61 } 62 destroyRenderer()63 public void destroyRenderer() { 64 if (mWallpaperRS != null) { 65 mWallpaperRS.stop(); 66 mWallpaperRS = null; 67 } 68 69 if (mRenderScript != null) { 70 mRenderScript.setSurface(null, 0, 0); 71 mRenderScript.destroy(); 72 mRenderScript = null; 73 } 74 } 75 76 @Override onSurfaceCreated(SurfaceHolder surfaceHolder)77 public void onSurfaceCreated(SurfaceHolder surfaceHolder) { 78 super.onSurfaceCreated(surfaceHolder); 79 80 RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig(); 81 mRenderScript = new RenderScriptGL(Galaxy4Wallpaper.this, sc); 82 mRenderScript.setPriority(RenderScript.Priority.LOW); 83 } 84 85 @Override onSurfaceDestroyed(SurfaceHolder surfaceHolder)86 public void onSurfaceDestroyed(SurfaceHolder surfaceHolder) { 87 super.onSurfaceDestroyed(surfaceHolder); 88 destroyRenderer(); 89 } 90 91 @Override onSurfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height)92 public void onSurfaceChanged(SurfaceHolder surfaceHolder, 93 int format, int width, int height) { 94 super.onSurfaceChanged(surfaceHolder, format, width, height); 95 96 if (mRenderScript != null) { 97 mRenderScript.setSurface(surfaceHolder, width, height); 98 } 99 100 if (mWallpaperRS == null) { 101 mWallpaperRS = new GalaxyRS(); 102 mWallpaperRS.init(mDensityDPI, mRenderScript, getResources(), width, height); 103 mWallpaperRS.start(); 104 } else { 105 mWallpaperRS.resize(width, height); 106 } 107 108 } 109 110 @Override onCommand(String action, int x, int y, int z, Bundle extras, boolean resultRequested)111 public Bundle onCommand(String action, int x, int y, int z, Bundle extras, 112 boolean resultRequested) { 113 if (mWallpaperRS != null) { 114 // return mWallpaperRS.onCommand(action, x, y, z, extras, resultRequested); 115 } 116 return null; 117 } 118 119 @Override onVisibilityChanged(boolean visible)120 public void onVisibilityChanged(boolean visible) { 121 super.onVisibilityChanged(visible); 122 if (mWallpaperRS != null) { 123 if (visible) { 124 mWallpaperRS.start(); 125 } else { 126 mWallpaperRS.stop(); 127 } 128 } 129 } 130 } 131 }