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
17package com.android.mediaframeworktest;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.os.ConditionVariable;
22import android.os.SystemClock;
23import android.util.Log;
24import android.view.SurfaceHolder;
25import android.view.SurfaceView;
26import android.view.WindowManager;
27
28/**
29 * (non-Javadoc)
30 * @see android.hardware.camera2.cts.Camera2SurfaceViewCtsActivity
31 */
32public 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
42    protected void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44
45        setContentView(R.layout.surface_view_2);
46        mSurfaceView = (SurfaceView) 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
53    public SurfaceView getSurfaceView() {
54        return mSurfaceView;
55    }
56
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
98    public void surfaceCreated(SurfaceHolder holder) {
99    }
100
101    @Override
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
112    public void surfaceDestroyed(SurfaceHolder holder) {
113    }
114}
115