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