Romain Guy | 44da178 | 2009-08-28 11:03:21 -0700 | [diff] [blame] | 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 | |
Romain Guy | 44da178 | 2009-08-28 11:03:21 -0700 | [diff] [blame] | 17 | package com.android.wallpaper; |
| 18 | |
| 19 | import android.service.wallpaper.WallpaperService; |
Mike Cleron | c9e42c2 | 2009-10-23 16:54:59 -0700 | [diff] [blame^] | 20 | import android.os.Bundle; |
Romain Guy | 44da178 | 2009-08-28 11:03:21 -0700 | [diff] [blame] | 21 | import android.renderscript.RenderScript; |
| 22 | import android.view.SurfaceHolder; |
| 23 | import android.view.Surface; |
| 24 | |
| 25 | public abstract class RenderScriptWallpaper<T extends RenderScriptScene> extends WallpaperService { |
| 26 | public Engine onCreateEngine() { |
| 27 | return new RenderScriptEngine(); |
| 28 | } |
| 29 | |
| 30 | protected abstract T createScene(int width, int height); |
| 31 | |
| 32 | private class RenderScriptEngine extends Engine { |
| 33 | private RenderScript mRs; |
| 34 | private T mRenderer; |
| 35 | |
| 36 | @Override |
| 37 | public void onCreate(SurfaceHolder surfaceHolder) { |
| 38 | super.onCreate(surfaceHolder); |
| 39 | setTouchEventsEnabled(false); |
| 40 | surfaceHolder.setSizeFromLayout(); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public void onDestroy() { |
| 45 | super.onDestroy(); |
| 46 | destroyRenderer(); |
| 47 | } |
| 48 | |
| 49 | private void destroyRenderer() { |
| 50 | if (mRenderer != null) { |
| 51 | mRenderer.stop(); |
| 52 | mRenderer = null; |
| 53 | } |
| 54 | if (mRs != null) { |
| 55 | mRs.destroy(); |
| 56 | mRs = null; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void onVisibilityChanged(boolean visible) { |
| 62 | super.onVisibilityChanged(visible); |
Romain Guy | d115190 | 2009-09-16 12:35:46 -0700 | [diff] [blame] | 63 | if (mRenderer != null) { |
| 64 | if (visible) { |
| 65 | mRenderer.start(); |
| 66 | } else { |
| 67 | mRenderer.stop(); |
| 68 | } |
Romain Guy | 44da178 | 2009-08-28 11:03:21 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
| 74 | super.onSurfaceChanged(holder, format, width, height); |
| 75 | if (mRenderer == null) { |
| 76 | mRenderer = createScene(width, height); |
Romain Guy | 0ac0b3f | 2009-09-13 03:40:04 -0700 | [diff] [blame] | 77 | mRenderer.init(mRs, getResources(), isPreview()); |
Romain Guy | 44da178 | 2009-08-28 11:03:21 -0700 | [diff] [blame] | 78 | mRenderer.start(); |
| 79 | } else { |
| 80 | mRenderer.resize(width, height); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void onOffsetsChanged(float xOffset, float yOffset, int xPixels, int yPixels) { |
| 86 | mRenderer.setOffset(xOffset, yOffset, xPixels, yPixels); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onSurfaceCreated(SurfaceHolder holder) { |
| 91 | super.onSurfaceCreated(holder); |
| 92 | |
| 93 | Surface surface = null; |
| 94 | while (surface == null) { |
| 95 | surface = holder.getSurface(); |
| 96 | } |
Jason Sams | 5d43b50 | 2009-09-23 13:54:31 -0700 | [diff] [blame] | 97 | mRs = new RenderScript(surface, false, false); |
Romain Guy | 44da178 | 2009-08-28 11:03:21 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onSurfaceDestroyed(SurfaceHolder holder) { |
| 102 | super.onSurfaceDestroyed(holder); |
| 103 | destroyRenderer(); |
| 104 | } |
Mike Cleron | c9e42c2 | 2009-10-23 16:54:59 -0700 | [diff] [blame^] | 105 | |
| 106 | @Override |
| 107 | public Bundle onCommand(String action, int x, int y, int z, |
| 108 | Bundle extras, boolean resultRequested) { |
| 109 | return mRenderer.onCommand(action, x, y, z, extras, resultRequested); |
| 110 | } |
| 111 | |
Romain Guy | 44da178 | 2009-08-28 11:03:21 -0700 | [diff] [blame] | 112 | } |
| 113 | } |