1 /* <lambda>null2 * Copyright 2020 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 androidx.camera.integration.uiwidgets.viewpager 18 19 import android.util.Log 20 import androidx.annotation.VisibleForTesting 21 import androidx.appcompat.app.AppCompatActivity 22 import androidx.camera.view.PreviewView 23 import androidx.lifecycle.Observer 24 import java.util.concurrent.CountDownLatch 25 import java.util.concurrent.TimeUnit 26 27 /** A basic activity used in UI widgets to share the common resources for testing. */ 28 open class BaseActivity : AppCompatActivity() { 29 30 companion object { 31 private const val TAG = "BasicActivity" 32 private const val LATCH_TIMEOUT: Long = 5000 33 internal const val INTENT_LENS_FACING = "lens-facing" 34 internal const val INTENT_IMPLEMENTATION_MODE = "implementation-mode" 35 internal const val PERFORMANCE_MODE = 0 36 internal const val COMPATIBLE_MODE = 1 37 } 38 39 // The expected final streamState 40 private var expectedStreamState: PreviewView.StreamState = PreviewView.StreamState.STREAMING 41 private var latchForState: CountDownLatch = CountDownLatch(0) 42 var previewView: PreviewView? = null 43 44 private val streamStateObserver = 45 Observer<PreviewView.StreamState> { state -> 46 when (state) { 47 PreviewView.StreamState.STREAMING -> { 48 Log.d(TAG, "PreviewView.StreamState.STREAMING") 49 if (expectedStreamState == PreviewView.StreamState.STREAMING) { 50 latchForState.countDown() 51 } 52 } 53 PreviewView.StreamState.IDLE -> { 54 Log.d(TAG, "PreviewView.StreamState.IDLE") 55 if (expectedStreamState == PreviewView.StreamState.IDLE) { 56 latchForState.countDown() 57 } 58 } 59 } 60 } 61 62 @VisibleForTesting 63 suspend fun waitForStreamState(expectedState: PreviewView.StreamState): Boolean { 64 latchForState = CountDownLatch(1) 65 expectedStreamState = expectedState 66 runOnUiThread { 67 previewView!!.previewStreamState.removeObservers(this) 68 previewView!!.previewStreamState.observe(this, streamStateObserver) 69 } 70 71 return latchForState.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS) 72 } 73 } 74