1 /* 2 * Copyright (C) 2024 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.quickstep 18 19 import android.os.RemoteException 20 import android.util.Log 21 import android.view.InsetsState 22 import android.view.WindowInsets 23 24 import com.android.launcher3.Utilities 25 import com.android.launcher3.config.FeatureFlags 26 import com.android.launcher3.util.Executors 27 import com.android.wm.shell.shared.IHomeTransitionListener.Stub 28 import com.android.wm.shell.shared.IShellTransitions 29 30 /** Class to track visibility state of Launcher */ 31 class HomeVisibilityState { 32 33 var isHomeVisible = true 34 private set 35 36 @Volatile var navbarInsetPosition = 0 37 38 private var listeners = mutableSetOf<VisibilityChangeListener>() 39 addListenernull40 fun addListener(l: VisibilityChangeListener) = listeners.add(l) 41 42 fun removeListener(l: VisibilityChangeListener) = listeners.remove(l) 43 44 fun init(transitions: IShellTransitions?) { 45 if (!FeatureFlags.enableHomeTransitionListener()) return 46 try { 47 transitions?.setHomeTransitionListener( 48 object : Stub() { 49 override fun onHomeVisibilityChanged(isVisible: Boolean) { 50 Utilities.postAsyncCallback( 51 Executors.MAIN_EXECUTOR.handler, 52 { 53 isHomeVisible = isVisible 54 listeners.forEach { it.onHomeVisibilityChanged(isVisible) } 55 }, 56 ) 57 } 58 override fun onDisplayInsetsChanged(insetsState: InsetsState) { 59 val bottomInset = insetsState.calculateInsets(insetsState.displayFrame, 60 WindowInsets.Type.navigationBars(), false).bottom 61 navbarInsetPosition = insetsState.displayFrame.bottom - bottomInset 62 } 63 } 64 ) 65 } catch (e: RemoteException) { 66 Log.w(TAG, "Failed call setHomeTransitionListener", e) 67 } 68 } 69 70 interface VisibilityChangeListener { onHomeVisibilityChangednull71 fun onHomeVisibilityChanged(isVisible: Boolean) 72 } 73 74 override fun toString() = "{HomeVisibilityState isHomeVisible=$isHomeVisible}" 75 76 companion object { 77 78 private const val TAG = "HomeVisibilityState" 79 } 80 } 81