1 /*
2  * 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.rotations
18 
19 import android.content.Context
20 import android.hardware.display.DisplayManager
21 import androidx.annotation.VisibleForTesting
22 import java.util.concurrent.Semaphore
23 
24 class OrientationConfigChangesOverriddenActivity : CameraActivity() {
25 
<lambda>null26     private val mDisplayManager by lazy {
27         getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
28     }
29 
<lambda>null30     private val mDisplayListener by lazy {
31         object : DisplayManager.DisplayListener {
32             override fun onDisplayChanged(displayId: Int) {
33                 if (!isImageAnalysisInitialized() || !isImageCaptureInitialized()) {
34                     return
35                 }
36 
37                 val display = mDisplayManager.getDisplay(displayId)
38                 if (display != null) {
39                     val rotation = display.rotation
40                     mImageAnalysis.targetRotation = rotation
41                     mImageCapture.targetRotation = rotation
42                 }
43                 mDisplayChanged.release()
44             }
45 
46             override fun onDisplayAdded(displayId: Int) {}
47 
48             override fun onDisplayRemoved(displayId: Int) {}
49         }
50     }
51 
onStartnull52     override fun onStart() {
53         super.onStart()
54         mDisplayManager.registerDisplayListener(mDisplayListener, null)
55     }
56 
onStopnull57     override fun onStop() {
58         super.onStop()
59         mDisplayManager.unregisterDisplayListener(mDisplayListener)
60     }
61 
62     // region For testing
63     @VisibleForTesting val mDisplayChanged = Semaphore(0)
64     // endregion
65 }
66