• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.wallpaper.holospiral;
18 
19 import android.content.Context;
20 import android.graphics.PixelFormat;
21 import android.renderscript.RSSurfaceView;
22 import android.renderscript.RenderScript;
23 import android.renderscript.RenderScriptGL;
24 import android.view.MotionEvent;
25 import android.view.Surface;
26 import android.view.SurfaceHolder;
27 
28 public class HoloSpiralView extends RSSurfaceView {
29     private static final String LOG_TAG = "HoloRSView";
30     private boolean mDragging = false;
31     private float mStartX = 0;
32     private float mCurrentPosition = 0;
33     private float mWallpaperWidth = 0;
34 
HoloSpiralView(Context context)35     public HoloSpiralView(Context context) {
36         super(context);
37     }
38 
39     private RenderScriptGL mRenderScript = null;
40     private HoloSpiralRS mWallpaperRS = null;
41 
destroyRenderer()42     public void destroyRenderer() {
43         if (mWallpaperRS != null) {
44             mWallpaperRS.stop();
45             mWallpaperRS = null;
46         }
47 
48         if (mRenderScript != null) {
49             mRenderScript.setSurface(null, 0, 0);
50             mRenderScript = null;
51             destroyRenderScriptGL();
52         }
53     }
54 
55     @Override
surfaceCreated(SurfaceHolder surfaceHolder)56     public void surfaceCreated(SurfaceHolder surfaceHolder) {
57         super.surfaceCreated(surfaceHolder);
58 
59         Surface surface = null;
60         while (surface == null) {
61             surface = surfaceHolder.getSurface();
62         }
63         RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
64         mRenderScript = createRenderScriptGL(sc);
65         mRenderScript.setPriority(RenderScript.Priority.LOW);
66 
67         surfaceHolder.setSizeFromLayout();
68         surfaceHolder.setFormat(PixelFormat.RGBA_8888);
69     }
70 
71     @Override
surfaceDestroyed(SurfaceHolder surfaceHolder)72     public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
73         super.surfaceDestroyed(surfaceHolder);
74         destroyRenderer();
75     }
76 
77     @Override
surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height)78     public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
79         super.surfaceChanged(surfaceHolder, format, width, height);
80         if (mRenderScript != null) {
81             mRenderScript.setSurface(surfaceHolder, width, height);
82         }
83 
84         if (mWallpaperRS == null) {
85             mWallpaperRS = new HoloSpiralRS(mRenderScript, getResources());
86             mWallpaperRS.start();
87         }
88 
89         mWallpaperRS.resize(width, height);
90         mWallpaperWidth = width;
91     }
92 
onResume()93     public void onResume() {
94         resume();
95         if (mWallpaperRS != null) {
96             mWallpaperRS.start();
97         }
98     }
99 
onPause()100     public void onPause() {
101         pause();
102         if (mWallpaperRS != null) {
103             mWallpaperRS.stop();
104         }
105     }
106 
107     @Override
onDetachedFromWindow()108     protected void onDetachedFromWindow() {
109         super.onDetachedFromWindow();
110         destroyRenderer();
111     }
112 
113     @Override
onTouchEvent(MotionEvent event)114     public boolean onTouchEvent(MotionEvent event) {
115         if (event.getActionIndex() == 0) {
116             float realPosition = mCurrentPosition + (event.getX() - mStartX);
117             if (realPosition < 0.0f) {
118                 realPosition = 0.0f;
119             } else if (realPosition > mWallpaperWidth * 4) {
120                 realPosition = mWallpaperWidth * 4;
121             }
122 
123             if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
124                 mDragging = true;
125                 mStartX = event.getX(0);
126             } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
127                 mDragging = false;
128                 mCurrentPosition = realPosition;
129             } else if (mDragging) {
130                 float ratio = realPosition / (mWallpaperWidth * 4);
131                 mWallpaperRS.setOffset(ratio, 0, 0, 0);
132             }
133             return true;
134         }
135         return false;
136     }
137 }
138