1 /* 2 * Copyright (C) 2023 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.systemui.keyevent.domain.interactor 18 19 import android.view.KeyEvent 20 import com.android.systemui.back.domain.interactor.BackActionInteractor 21 import com.android.systemui.dagger.SysUISingleton 22 import com.android.systemui.keyguard.domain.interactor.KeyguardKeyEventInteractor 23 import javax.inject.Inject 24 25 /** 26 * Sends key events to the appropriate interactors and then acts upon key events that haven't 27 * already been handled but should be handled by SystemUI. 28 * 29 * To observe any key event states, see [KeyEventInteractor]. 30 */ 31 @SysUISingleton 32 class SysUIKeyEventHandler 33 @Inject 34 constructor( 35 private val backActionInteractor: BackActionInteractor, 36 private val keyguardKeyEventInteractor: KeyguardKeyEventInteractor, 37 ) { dispatchKeyEventnull38 fun dispatchKeyEvent(event: KeyEvent): Boolean { 39 if (keyguardKeyEventInteractor.dispatchKeyEvent(event)) { 40 return true 41 } 42 43 when (event.keyCode) { 44 KeyEvent.KEYCODE_BACK -> { 45 if (event.handleAction()) { 46 backActionInteractor.onBackRequested() 47 } 48 return true 49 } 50 } 51 return false 52 } 53 interceptMediaKeynull54 fun interceptMediaKey(event: KeyEvent): Boolean { 55 return keyguardKeyEventInteractor.interceptMediaKey(event) 56 } 57 dispatchKeyEventPreImenull58 fun dispatchKeyEventPreIme(event: KeyEvent): Boolean { 59 return keyguardKeyEventInteractor.dispatchKeyEventPreIme(event) 60 } 61 62 companion object { 63 // Most actions shouldn't be handled on the down event and instead handled on subsequent 64 // key events like ACTION_UP. KeyEventnull65 fun KeyEvent.handleAction(): Boolean { 66 return action != KeyEvent.ACTION_DOWN 67 } 68 } 69 } 70