1 /* <lambda>null2 * Copyright (C) 2022 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 distributed under the 11 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 12 * KIND, either express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 package com.android.systemui.unfold.util 16 17 import android.content.Context 18 import android.view.Surface 19 import com.android.systemui.unfold.UnfoldTransitionProgressProvider 20 import com.android.systemui.unfold.UnfoldTransitionProgressProvider.TransitionProgressListener 21 import com.android.systemui.unfold.updates.RotationChangeProvider 22 import com.android.systemui.unfold.updates.RotationChangeProvider.RotationListener 23 24 /** 25 * [UnfoldTransitionProgressProvider] that emits transition progress only when the display has 26 * default rotation or 180 degrees opposite rotation (ROTATION_0 or ROTATION_180). It could be 27 * helpful to run the animation only when the display's rotation is perpendicular to the fold. 28 */ 29 class NaturalRotationUnfoldProgressProvider( 30 private val context: Context, 31 private val rotationChangeProvider: RotationChangeProvider, 32 unfoldTransitionProgressProvider: UnfoldTransitionProgressProvider 33 ) : UnfoldTransitionProgressProvider { 34 35 private val scopedUnfoldTransitionProgressProvider = 36 ScopedUnfoldTransitionProgressProvider(unfoldTransitionProgressProvider) 37 38 private var isNaturalRotation: Boolean = false 39 40 fun init() { 41 rotationChangeProvider.addCallback(rotationListener) 42 rotationListener.onRotationChanged(context.display.rotation) 43 } 44 45 private val rotationListener = RotationListener { rotation -> 46 val isNewRotationNatural = 47 rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180 48 49 if (isNaturalRotation != isNewRotationNatural) { 50 isNaturalRotation = isNewRotationNatural 51 scopedUnfoldTransitionProgressProvider.setReadyToHandleTransition(isNewRotationNatural) 52 } 53 } 54 55 override fun destroy() { 56 rotationChangeProvider.removeCallback(rotationListener) 57 scopedUnfoldTransitionProgressProvider.destroy() 58 } 59 60 override fun addCallback(listener: TransitionProgressListener) { 61 scopedUnfoldTransitionProgressProvider.addCallback(listener) 62 } 63 64 override fun removeCallback(listener: TransitionProgressListener) { 65 scopedUnfoldTransitionProgressProvider.removeCallback(listener) 66 } 67 } 68