1package com.android.phasebeam;
2
3import android.app.Service;
4import android.graphics.PixelFormat;
5import android.os.Bundle;
6import android.renderscript.RenderScript;
7import android.renderscript.RenderScriptGL;
8import android.service.wallpaper.WallpaperService;
9import android.util.DisplayMetrics;
10import android.view.SurfaceHolder;
11import android.view.WindowManager;
12
13public class PhaseBeamWallpaper extends WallpaperService {
14
15    @Override
16    public Engine onCreateEngine() {
17        return new RenderScriptEngine();
18    }
19
20    private class RenderScriptEngine extends Engine {
21        private RenderScriptGL mRenderScript = null;
22        private PhaseBeamRS mWallpaperRS = null;
23        private int mDensityDPI;
24
25        @Override
26        public void onCreate(SurfaceHolder surfaceHolder) {
27            super.onCreate(surfaceHolder);
28            setTouchEventsEnabled(true);
29            surfaceHolder.setSizeFromLayout();
30            surfaceHolder.setFormat(PixelFormat.OPAQUE);
31
32            DisplayMetrics metrics = new DisplayMetrics();
33            ((WindowManager) getApplication().getSystemService(Service.WINDOW_SERVICE))
34                    .getDefaultDisplay().getMetrics(metrics);
35            mDensityDPI = metrics.densityDpi;
36        }
37
38        @Override
39        public void onDestroy() {
40            super.onDestroy();
41            destroyRenderer();
42        }
43
44        public void destroyRenderer() {
45            if (mWallpaperRS != null) {
46                mWallpaperRS.stop();
47                mWallpaperRS = null;
48            }
49
50            if (mRenderScript != null) {
51                mRenderScript.setSurface(null, 0, 0);
52                mRenderScript.destroy();
53                mRenderScript = null;
54            }
55        }
56
57        @Override
58        public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
59            super.onSurfaceCreated(surfaceHolder);
60
61            RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
62            mRenderScript = new RenderScriptGL(PhaseBeamWallpaper.this, sc);
63            mRenderScript.setPriority(RenderScript.Priority.NORMAL);
64        }
65
66        @Override
67        public void onSurfaceDestroyed(SurfaceHolder surfaceHolder) {
68            super.onSurfaceDestroyed(surfaceHolder);
69            destroyRenderer();
70        }
71
72        @Override
73        public void onSurfaceChanged(SurfaceHolder surfaceHolder, int format, int width,
74                int height) {
75            super.onSurfaceChanged(surfaceHolder, format, width, height);
76
77            if (mRenderScript != null) {
78                mRenderScript.setSurface(surfaceHolder, width, height);
79            }
80
81            if (mWallpaperRS == null) {
82                mWallpaperRS = new PhaseBeamRS();
83                mWallpaperRS.init(mDensityDPI, mRenderScript, getResources(), width, height);
84                mWallpaperRS.start();
85            }
86
87            mWallpaperRS.resize(width, height);
88        }
89
90        @Override
91        public Bundle onCommand(String action, int x, int y, int z, Bundle extras,
92                boolean resultRequested) {
93            return null;
94        }
95
96        @Override
97        public void onVisibilityChanged(boolean visible) {
98            super.onVisibilityChanged(visible);
99            if (mWallpaperRS != null) {
100                if (visible) {
101                    mWallpaperRS.start();
102                } else {
103                    mWallpaperRS.stop();
104                }
105            }
106        }
107
108        @Override
109        public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep,
110                float yOffsetStep, int xPixelOffset, int yPixelOffset) {
111            // TODO: Uncomment this once we can work out framerate issues
112            //mWallpaperRS.setOffset(xOffset, yOffset, xPixelOffset, yPixelOffset);
113        }
114    }
115}