126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski/*
226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski * Copyright (C) 2010 The Android Open Source Project
326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski *
426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski * you may not use this file except in compliance with the License.
626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski * You may obtain a copy of the License at
726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski *
826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski *
1026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski * Unless required by applicable law or agreed to in writing, software
1126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
1226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski * See the License for the specific language governing permissions and
1426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski * limitations under the License.
1526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski */
1626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
1726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskipackage com.android.wallpaper.holospiral;
1826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
1926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskiimport android.graphics.PixelFormat;
2026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskiimport android.os.Bundle;
2126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskiimport android.os.Handler;
2226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskiimport android.os.Message;
2326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskiimport android.renderscript.RenderScript;
2426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskiimport android.renderscript.RenderScriptGL;
2526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskiimport android.service.wallpaper.WallpaperService;
2626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskiimport android.view.Surface;
2726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskiimport android.view.SurfaceHolder;
2826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
2926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinskipublic class HoloSpiralWallpaper extends WallpaperService {
3026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
3126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski    @Override
3226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski    public Engine onCreateEngine() {
3326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        return new RenderScriptEngine();
3426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski    }
3526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
3626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski    private class RenderScriptEngine extends Engine {
3726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        private RenderScriptGL mRenderScript = null;
3826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        private HoloSpiralRS mWallpaperRS = null;
3926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
4026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        @Override
4126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        public void onCreate(SurfaceHolder surfaceHolder) {
4226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            super.onCreate(surfaceHolder);
4326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            setTouchEventsEnabled(true);
4426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            surfaceHolder.setSizeFromLayout();
4526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            surfaceHolder.setFormat(PixelFormat.RGBA_8888);
4626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        }
4726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
4826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        @Override
4926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        public void onDestroy() {
5026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            super.onDestroy();
5126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            destroyRenderer();
5226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        }
5326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
5426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        public void destroyRenderer() {
5526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            if (mWallpaperRS != null) {
5626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                mWallpaperRS.stop();
5726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                mWallpaperRS = null;
5826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            }
5926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
6026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            if (mRenderScript != null) {
61213cc60e83d3d34e09b5907d8ded0a15892993f1Jason Sams                mRenderScript.setSurface(null, 0, 0);
6226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                mRenderScript.destroy();
6326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                mRenderScript = null;
6426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            }
6526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        }
6626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
6726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        @Override
6826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
6926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            super.onSurfaceCreated(surfaceHolder);
70213cc60e83d3d34e09b5907d8ded0a15892993f1Jason Sams
7126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
7239a9dc533ee259a62d5e9c9d2fd2833906bd073bShih-wei Liao            mRenderScript = new RenderScriptGL(HoloSpiralWallpaper.this, sc);
73213cc60e83d3d34e09b5907d8ded0a15892993f1Jason Sams            mRenderScript.setPriority(RenderScript.Priority.LOW);
7426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        }
7526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
7626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        @Override
7726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        public void onSurfaceDestroyed(SurfaceHolder surfaceHolder) {
7826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            super.onSurfaceDestroyed(surfaceHolder);
7926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            destroyRenderer();
8026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        }
8126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
8226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        @Override
8326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        public void onSurfaceChanged(SurfaceHolder surfaceHolder, int format,
8426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                int width, int height) {
8526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            super.onSurfaceChanged(surfaceHolder, format, width, height);
8626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
8726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            if (mRenderScript != null) {
88213cc60e83d3d34e09b5907d8ded0a15892993f1Jason Sams                mRenderScript.setSurface(surfaceHolder, width, height);
8926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            }
9026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
9126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            if (mWallpaperRS == null) {
9226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                mWallpaperRS = new HoloSpiralRS(mRenderScript, getResources());
9326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                mWallpaperRS.start();
9426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            }
9526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
9626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            mWallpaperRS.resize(width, height);
9726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        }
9826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
9926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        @Override
10026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        public Bundle onCommand(String action, int x, int y, int z, Bundle extras,
10126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                boolean resultRequested) {
10226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            if (mWallpaperRS != null) {
10326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                return mWallpaperRS.onCommand(action, x, y, z, extras, resultRequested);
10426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            }
10526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            return null;
10626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        }
10726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
10826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        @Override
10926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        public void onVisibilityChanged(boolean visible) {
11026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            super.onVisibilityChanged(visible);
11126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            if (mWallpaperRS != null) {
11226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                if (visible) {
11326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                    mWallpaperRS.start();
11426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                } else {
11526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                    mWallpaperRS.stop();
11626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                }
11726122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            }
11826122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        }
11926122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski
12026122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        @Override
12126122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep,
12226122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski                float yOffsetStep, int xPixelOffset, int yPixelOffset) {
12326122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski            mWallpaperRS.setOffset(xOffset, yOffset, xPixelOffset, yPixelOffset);
12426122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski        }
12526122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski    }
12626122dc2e3971577fd8bbcfbac2140d73b817377Adam Lesinski}
127