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.systemui.bouncer.ui.helper 18 19 import android.view.HapticFeedbackConstants 20 import android.view.View 21 import com.android.keyguard.AuthInteractionProperties 22 import com.android.systemui.Flags 23 //noinspection CleanArchitectureDependencyViolation: Data layer only referenced for this enum class 24 import com.google.android.msdl.data.model.MSDLToken 25 import com.google.android.msdl.domain.MSDLPlayer 26 import javax.inject.Inject 27 28 /** 29 * A helper class to deliver haptic feedback in bouncer interactions. 30 * 31 * @param[msdlPlayer] The [MSDLPlayer] used to deliver MSDL feedback. 32 */ 33 class BouncerHapticPlayer @Inject constructor(private val msdlPlayer: dagger.Lazy<MSDLPlayer>) { 34 35 private val authInteractionProperties by <lambda>null36 lazy(LazyThreadSafetyMode.NONE) { AuthInteractionProperties() } 37 38 val isEnabled: Boolean 39 get() = Flags.msdlFeedback() 40 41 /** 42 * Deliver MSDL feedback as a result of authenticating through a bouncer. 43 * 44 * @param[authenticationSucceeded] Whether the authentication was successful or not. 45 */ playAuthenticationFeedbacknull46 fun playAuthenticationFeedback(authenticationSucceeded: Boolean) { 47 if (!isEnabled) return 48 49 val token = 50 if (authenticationSucceeded) { 51 MSDLToken.UNLOCK 52 } else { 53 MSDLToken.FAILURE 54 } 55 msdlPlayer.get().playToken(token, authInteractionProperties) 56 } 57 58 /** 59 * Deliver feedback when dragging through cells in the pattern bouncer. This function can play 60 * MSDL feedback using a [MSDLPlayer], or fallback to a default haptic feedback using the 61 * [View.performHapticFeedback] API and a [View]. 62 * 63 * @param[view] A [View] for default haptic feedback using [View.performHapticFeedback] 64 */ playPatternDotFeedbacknull65 fun playPatternDotFeedback(view: View?) { 66 if (!isEnabled) { 67 view?.performHapticFeedback( 68 HapticFeedbackConstants.VIRTUAL_KEY, 69 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING, 70 ) 71 } else { 72 msdlPlayer.get().playToken(MSDLToken.DRAG_INDICATOR_DISCRETE) 73 } 74 } 75 76 /** Deliver MSDL feedback when the delete key of the pin bouncer is pressed */ playDeleteKeyPressFeedbacknull77 fun playDeleteKeyPressFeedback() = msdlPlayer.get().playToken(MSDLToken.KEYPRESS_DELETE) 78 79 /** Deliver MSDL feedback when the delete key of the pin bouncer is long-pressed. */ 80 fun playDeleteKeyLongPressedFeedback() = msdlPlayer.get().playToken(MSDLToken.LONG_PRESS) 81 82 /** Deliver MSDL feedback when a numpad key is pressed on the pin bouncer */ 83 fun playNumpadKeyFeedback() = msdlPlayer.get().playToken(MSDLToken.KEYPRESS_STANDARD) 84 85 /** Deliver MSDL feedback when clicking on the emergency button */ 86 fun playEmergencyButtonClickFeedback() { 87 if (isEnabled) { 88 msdlPlayer.get().playToken(MSDLToken.KEYPRESS_RETURN) 89 } 90 } 91 } 92