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.Display.DEFAULT_DISPLAY 22 import com.android.launcher3.util.Executors 23 import com.android.wm.shell.shared.IFocusTransitionListener.Stub 24 import com.android.wm.shell.shared.IShellTransitions 25 26 /** Class to track focus state of displays and windows */ 27 class FocusState { 28 29 var focusedDisplayId = DEFAULT_DISPLAY 30 private set 31 32 private var listeners = mutableSetOf<FocusChangeListener>() 33 addListenernull34 fun addListener(l: FocusChangeListener) = listeners.add(l) 35 36 fun removeListener(l: FocusChangeListener) = listeners.remove(l) 37 38 fun init(transitions: IShellTransitions?) { 39 try { 40 transitions?.setFocusTransitionListener( 41 object : Stub() { 42 override fun onFocusedDisplayChanged(displayId: Int) { 43 Executors.MAIN_EXECUTOR.execute { 44 listeners.forEach { it.onFocusedDisplayChanged(displayId) } 45 } 46 } 47 } 48 ) 49 } catch (e: RemoteException) { 50 Log.w(TAG, "Failed call setFocusTransitionListener", e) 51 } 52 } 53 54 interface FocusChangeListener { onFocusedDisplayChangednull55 fun onFocusedDisplayChanged(displayId: Int) 56 } 57 58 override fun toString() = "{FocusState focusedDisplayId=$focusedDisplayId}" 59 60 companion object { 61 private const val TAG = "FocusState" 62 } 63 } 64