1 /*
2  * Copyright (C) 2021 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.biometrics
18 
19 import android.media.AudioAttributes
20 import android.os.VibrationEffect
21 import com.android.keyguard.KeyguardUpdateMonitor
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.statusbar.VibratorHelper
24 import com.android.systemui.statusbar.commandline.Command
25 import com.android.systemui.statusbar.commandline.CommandRegistry
26 import java.io.PrintWriter
27 import javax.inject.Inject
28 
29 /**
30  * Used to simulate haptics that may be used for udfps authentication.
31  */
32 @SysUISingleton
33 class UdfpsHapticsSimulator @Inject constructor(
34     commandRegistry: CommandRegistry,
35     val vibrator: VibratorHelper,
36     val keyguardUpdateMonitor: KeyguardUpdateMonitor
37 ) : Command {
38     val sonificationEffects =
39         AudioAttributes.Builder()
40             .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
41             .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
42             .build()
43     var udfpsController: UdfpsController? = null
44 
45     init {
<lambda>null46         commandRegistry.registerCommand("udfps-haptic") { this }
47     }
48 
executenull49     override fun execute(pw: PrintWriter, args: List<String>) {
50         if (args.isEmpty()) {
51             invalidCommand(pw)
52         } else {
53             when (args[0]) {
54                 "start" -> {
55                     udfpsController?.playStartHaptic()
56                 }
57                 "success" -> {
58                     // needs to be kept up to date with AcquisitionClient#SUCCESS_VIBRATION_EFFECT
59                     vibrator.vibrate(
60                         VibrationEffect.get(VibrationEffect.EFFECT_CLICK),
61                         sonificationEffects)
62                 }
63                 "error" -> {
64                     // needs to be kept up to date with AcquisitionClient#ERROR_VIBRATION_EFFECT
65                     vibrator.vibrate(
66                         VibrationEffect.get(VibrationEffect.EFFECT_DOUBLE_CLICK),
67                         sonificationEffects)
68                 }
69                 else -> invalidCommand(pw)
70             }
71         }
72     }
73 
helpnull74     override fun help(pw: PrintWriter) {
75         pw.println("Usage: adb shell cmd statusbar udfps-haptic <haptic>")
76         pw.println("Available commands:")
77         pw.println("  start")
78         pw.println("  success, always plays CLICK haptic")
79         pw.println("  error, always plays DOUBLE_CLICK haptic")
80     }
81 
invalidCommandnull82     fun invalidCommand(pw: PrintWriter) {
83         pw.println("invalid command")
84         help(pw)
85     }
86 }