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