• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package android.view.cts;
17 
18 import android.app.Activity;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Paint;
24 import android.graphics.PorterDuff.Mode;
25 import android.os.Bundle;
26 import android.view.SurfaceHolder;
27 import android.view.SurfaceView;
28 
29 public class SurfaceViewStubActivity extends Activity {
30     private MockSurfaceView mSurfaceView;
31 
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35 
36         // New a MockSurfaceView
37         mSurfaceView = new MockSurfaceView(this);
38         setContentView(mSurfaceView);
39     }
40 
getSurfaceView()41     public MockSurfaceView getSurfaceView() {
42         return mSurfaceView;
43     }
44 
45     public class MockSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
46         private static final int FIX_WIDTH = 240;
47         private static final int FIX_HEIGHT = 240;
48         private static final int BITMAP_WIDTH = 100;
49         private static final int BITMAP_HEIGHT = 100;
50         private static final int RECT_LEFT = 20;
51         private static final int RECT_TOP = 100;
52         private static final int RECT_RIGHT = 200;
53         private static final int RECT_BOTTOM = 200;
54 
55         private SurfaceHolder mHolder;
56         private MockCanvas mCanvas;
57 
58         private boolean mIsDraw;
59         private boolean mIsAttachedToWindow;
60         private boolean mIsDetachedFromWindow;
61         private boolean mIsOnMeasure;
62         private boolean mIsOnScrollChanged;
63         private boolean mIsOnSizeChanged;
64         private boolean mIsOnWindowVisibilityChanged;
65         private boolean mIsDispatchDraw;
66         private boolean mIsSurfaceChanged;
67 
68         private int mWidthInOnMeasure;
69         private int mHeightInOnMeasure;
70         private int mOldLOnScrollChanged;
71         private int mOldTOnScrollChanged;
72         private int mOldWOnSizeChanged;
73         private int mOldHOnSizeChanged;
74         private int mVisibilityOnWindowVisibilityChanged;
75 
MockSurfaceView(Context context)76         public MockSurfaceView(Context context) {
77             super(context);
78             mHolder = getHolder();
79             mHolder.addCallback(this);
80             mHolder.setFixedSize(FIX_WIDTH, FIX_HEIGHT);
81         }
82 
83         @Override
onWindowVisibilityChanged(int visibility)84         public void onWindowVisibilityChanged(int visibility) {
85             super.onWindowVisibilityChanged(visibility);
86             mVisibilityOnWindowVisibilityChanged = visibility;
87             mIsOnWindowVisibilityChanged = true;
88         }
89 
getVInOnWindowVisibilityChanged()90         public int getVInOnWindowVisibilityChanged() {
91             return mVisibilityOnWindowVisibilityChanged;
92         }
93 
94         @Override
draw(Canvas canvas)95         public void draw(Canvas canvas) {
96             super.draw(canvas);
97             mIsDraw = true;
98         }
99 
100         @Override
onAttachedToWindow()101         public void onAttachedToWindow() {
102             super.onAttachedToWindow();
103             mIsAttachedToWindow = true;
104         }
105 
106         @Override
onDetachedFromWindow()107         public void onDetachedFromWindow() {
108             super.onDetachedFromWindow();
109             mIsDetachedFromWindow = true;
110         }
111 
112         @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)113         public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
114             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
115             mWidthInOnMeasure = getDefaultSize(FIX_WIDTH, widthMeasureSpec);
116             mHeightInOnMeasure = getDefaultSize(FIX_HEIGHT, heightMeasureSpec);
117             mIsOnMeasure = true;
118         }
119 
getWidthInOnMeasure()120         public int getWidthInOnMeasure() {
121             return mWidthInOnMeasure;
122         }
123 
getHeightInOnMeasure()124         public int getHeightInOnMeasure() {
125             return mHeightInOnMeasure;
126         }
127 
128         @Override
onScrollChanged(int l, int t, int oldl, int oldt)129         public void onScrollChanged(int l, int t, int oldl, int oldt) {
130             super.onScrollChanged(l, t, oldl, oldt);
131 
132             mOldLOnScrollChanged = oldl;
133             mOldTOnScrollChanged = oldt;
134             mIsOnScrollChanged = true;
135         }
136 
getOldHorizontal()137         public int getOldHorizontal() {
138             return mOldLOnScrollChanged;
139         }
140 
getOldVertical()141         public int getOldVertical() {
142             return mOldTOnScrollChanged;
143         }
144 
145         @Override
onSizeChanged(int w, int h, int oldw, int oldh)146         public void onSizeChanged(int w, int h, int oldw, int oldh) {
147             super.onSizeChanged(w, h, oldw, oldh);
148 
149             mOldWOnSizeChanged = oldw;
150             mOldHOnSizeChanged = oldh;
151             mIsOnSizeChanged = true;
152         }
153 
getOldWidth()154         public int getOldWidth() {
155             return mOldWOnSizeChanged;
156         }
157 
getOldHeight()158         public int getOldHeight() {
159             return mOldHOnSizeChanged;
160         }
161 
162         @Override
dispatchDraw(Canvas canvas)163         protected void dispatchDraw(Canvas canvas) {
164             super.dispatchDraw(canvas);
165             mIsDispatchDraw = true;
166         }
167 
setFormat(int format)168         public void setFormat(int format) {
169             getHolder().setFormat(format);
170         }
171 
surfaceCreated(SurfaceHolder holder)172         public void surfaceCreated(SurfaceHolder holder) {
173             // Use mock canvas listening to the drawColor() calling.
174             mCanvas = new MockCanvas(Bitmap.createBitmap( BITMAP_WIDTH,
175                                                           BITMAP_HEIGHT,
176                                                           Bitmap.Config.ARGB_8888));
177             draw(mCanvas);
178 
179             // Lock the surface, this returns a Canvas that can be used to render into.
180             Canvas canvas = mHolder.lockCanvas();
181             Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
182             paint.setColor(Color.BLUE);
183             canvas.drawRect(RECT_LEFT, RECT_TOP, RECT_RIGHT, RECT_BOTTOM, paint);
184 
185             // And finally unlock and post the surface.
186             mHolder.unlockCanvasAndPost(canvas);
187         }
188 
surfaceDestroyed(SurfaceHolder holder)189         public void surfaceDestroyed(SurfaceHolder holder) {
190         }
191 
surfaceChanged(SurfaceHolder holder, int format, int w, int h)192         public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
193             mIsSurfaceChanged = true;
194         }
195 
isDraw()196         public boolean isDraw() {
197             return mIsDraw;
198         }
199 
isOnAttachedToWindow()200         public boolean isOnAttachedToWindow() {
201             return mIsAttachedToWindow;
202         }
203 
isDetachedFromWindow()204         public boolean isDetachedFromWindow() {
205             return mIsDetachedFromWindow;
206         }
207 
isOnMeasureCalled()208         public boolean isOnMeasureCalled() {
209             return mIsOnMeasure;
210         }
211 
isOnScrollChanged()212         public boolean isOnScrollChanged() {
213             return mIsOnScrollChanged;
214         }
215 
isOnSizeChangedCalled()216         public boolean isOnSizeChangedCalled() {
217             return mIsOnSizeChanged;
218         }
219 
resetOnSizeChangedFlag(boolean b)220         public void resetOnSizeChangedFlag(boolean b) {
221             mIsOnSizeChanged = b;
222         }
223 
isOnWindowVisibilityChanged()224         public boolean isOnWindowVisibilityChanged() {
225             return mIsOnWindowVisibilityChanged;
226         }
227 
isDispatchDraw()228         public boolean isDispatchDraw() {
229             return mIsDispatchDraw;
230         }
231 
isDrawColor()232         public boolean isDrawColor() {
233             if (mCanvas != null) {
234                 return mCanvas.isDrawColor();
235             } else {
236                 return false;
237             }
238         }
239 
isSurfaceChanged()240         public boolean isSurfaceChanged() {
241             return mIsSurfaceChanged;
242         }
243 
setDrawColor(boolean isDrawColor)244         public void setDrawColor(boolean isDrawColor) {
245             if (mCanvas != null) {
246                 mCanvas.setDrawColor(isDrawColor);
247             }
248         }
249     }
250 
251     class MockCanvas extends Canvas {
252         private boolean mIsDrawColor;
253 
MockCanvas(Bitmap bitmap)254         public MockCanvas(Bitmap bitmap) {
255             super(bitmap);
256         }
257 
258         @Override
drawColor(int color, Mode mode)259         public void drawColor(int color, Mode mode) {
260             super.drawColor(color, mode);
261             mIsDrawColor = true;
262         }
263 
isDrawColor()264         public boolean isDrawColor() {
265             return mIsDrawColor;
266         }
267 
setDrawColor(boolean isDrawColor)268         public void setDrawColor(boolean isDrawColor) {
269             this.mIsDrawColor = isDrawColor;
270         }
271     }
272 }
273