1 /* 2 * Copyright 2016 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.mediaframeworktest; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.os.ConditionVariable; 22 import android.os.SystemClock; 23 import android.util.Log; 24 import android.view.SurfaceHolder; 25 import android.view.SurfaceView; 26 import android.view.WindowManager; 27 28 /** 29 * (non-Javadoc) 30 * @see android.hardware.camera2.cts.Camera2SurfaceViewCtsActivity 31 */ 32 public class Camera2SurfaceViewActivity extends Activity implements SurfaceHolder.Callback { 33 private static final String TAG = "SurfaceViewActivity"; 34 private final ConditionVariable surfaceChangedDone = new ConditionVariable(); 35 36 private SurfaceView mSurfaceView; 37 private int currentWidth = 0; 38 private int currentHeight = 0; 39 private final Object sizeLock = new Object(); 40 41 @Override onCreate(Bundle savedInstanceState)42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 45 setContentView(R.layout.surface_view_2); 46 mSurfaceView = findViewById(R.id.surface_view); 47 mSurfaceView.getHolder().addCallback(this); 48 49 //Acquire the full wake lock to keep the device up 50 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 51 } 52 getSurfaceView()53 public SurfaceView getSurfaceView() { 54 return mSurfaceView; 55 } 56 waitForSurfaceSizeChanged(int timeOutMs, int expectWidth, int expectHeight)57 public boolean waitForSurfaceSizeChanged(int timeOutMs, int expectWidth, int expectHeight) { 58 if (timeOutMs <= 0 || expectWidth <= 0 || expectHeight <= 0) { 59 throw new IllegalArgumentException( 60 String.format( 61 "timeout(%d), expectWidth(%d), and expectHeight(%d) " + 62 "should all be positive numbers", 63 timeOutMs, expectWidth, expectHeight)); 64 } 65 66 synchronized(sizeLock) { 67 if (expectWidth == currentWidth && expectHeight == currentHeight) { 68 return true; 69 } 70 } 71 72 int waitTimeMs = timeOutMs; 73 boolean changeSucceeded = false; 74 while (!changeSucceeded && waitTimeMs > 0) { 75 long startTimeMs = SystemClock.elapsedRealtime(); 76 changeSucceeded = surfaceChangedDone.block(waitTimeMs); 77 if (!changeSucceeded) { 78 Log.e(TAG, "Wait for surface change timed out after " + timeOutMs + " ms"); 79 return changeSucceeded; 80 } else { 81 // Get a surface change callback, need to check if the size is expected. 82 surfaceChangedDone.close(); 83 if (currentWidth == expectWidth && currentHeight == expectHeight) { 84 return changeSucceeded; 85 } 86 // Do a further iteration surface change check as surfaceChanged could be called 87 // again. 88 changeSucceeded = false; 89 } 90 waitTimeMs -= (SystemClock.elapsedRealtime() - startTimeMs); 91 } 92 93 // Couldn't get expected surface size change. 94 return false; 95 } 96 97 @Override surfaceCreated(SurfaceHolder holder)98 public void surfaceCreated(SurfaceHolder holder) { 99 } 100 101 @Override surfaceChanged(SurfaceHolder holder, int format, int width, int height)102 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 103 Log.i(TAG, "Surface Changed to: " + width + "x" + height); 104 synchronized (sizeLock) { 105 currentWidth = width; 106 currentHeight = height; 107 } 108 surfaceChangedDone.open(); 109 } 110 111 @Override surfaceDestroyed(SurfaceHolder holder)112 public void surfaceDestroyed(SurfaceHolder holder) { 113 } 114 } 115