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
18package com.android.magicsmoke;
19
20import android.service.wallpaper.WallpaperService;
21import android.os.Bundle;
22import android.graphics.PixelFormat;
23import android.renderscript.RenderScriptGL;
24import android.renderscript.RenderScript;
25import android.util.Log;
26import android.view.MotionEvent;
27import android.view.SurfaceHolder;
28import android.view.Surface;
29
30public abstract class RenderScriptWallpaper<T extends RenderScriptScene> extends WallpaperService {
31    public Engine onCreateEngine() {
32        return new RenderScriptEngine();
33    }
34
35    protected abstract T createScene(int width, int height);
36
37    private class RenderScriptEngine extends Engine {
38        private RenderScriptGL mRs;
39        private T mRenderer;
40
41        @Override
42        public void onCreate(SurfaceHolder surfaceHolder) {
43            super.onCreate(surfaceHolder);
44            setTouchEventsEnabled(false);
45            surfaceHolder.setSizeFromLayout();
46            surfaceHolder.setFormat(PixelFormat.RGB_565);
47        }
48
49        @Override
50        public void onDestroy() {
51            super.onDestroy();
52            destroyRenderer();
53        }
54
55        private void destroyRenderer() {
56            if (mRenderer != null) {
57                mRenderer.stop(true);
58                mRenderer = null;
59            }
60            if (mRs != null) {
61                mRs.destroy();
62                mRs = null;
63            }
64        }
65
66        @Override
67        public void onVisibilityChanged(boolean visible) {
68            super.onVisibilityChanged(visible);
69            if (mRenderer != null) {
70                if (visible) {
71                    mRenderer.start();
72                } else {
73                    mRenderer.stop(false);
74                }
75            }
76        }
77
78        @Override
79        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
80            super.onSurfaceChanged(holder, format, width, height);
81            if (mRs != null) {
82                mRs.setSurface(holder, width, height);
83            }
84            if (mRenderer == null) {
85                mRenderer = createScene(width, height);
86                mRenderer.init(mRs, getResources(), isPreview());
87                mRenderer.start();
88            } else {
89                mRenderer.resize(width, height);
90            }
91        }
92
93        @Override
94        public void onOffsetsChanged(float xOffset, float yOffset,
95                float xStep, float yStep, int xPixels, int yPixels) {
96            mRenderer.setOffset(xOffset, yOffset, xStep, yStep, xPixels, yPixels);
97        }
98
99        @Override
100        public void onSurfaceCreated(SurfaceHolder holder) {
101            super.onSurfaceCreated(holder);
102
103            RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
104            mRs = new RenderScriptGL(RenderScriptWallpaper.this, sc);
105            mRs.setPriority(RenderScript.Priority.LOW);
106        }
107
108        @Override
109        public void onSurfaceDestroyed(SurfaceHolder holder) {
110            super.onSurfaceDestroyed(holder);
111            destroyRenderer();
112        }
113
114        @Override
115        public Bundle onCommand(String action, int x, int y, int z,
116                Bundle extras, boolean resultRequested) {
117            return mRenderer.onCommand(action, x, y, z, extras, resultRequested);
118        }
119
120    }
121}
122