• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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.classifier.domain.interactor
18 
19 import android.view.MotionEvent
20 import com.android.systemui.classifier.Classifier
21 import com.android.systemui.classifier.FalsingClassifier
22 import com.android.systemui.classifier.FalsingCollector
23 import com.android.systemui.classifier.FalsingCollectorActual
24 import com.android.systemui.dagger.SysUISingleton
25 import com.android.systemui.plugins.FalsingManager
26 import com.android.systemui.plugins.FalsingManager.Penalty
27 import javax.inject.Inject
28 
29 /**
30  * Exposes the subset of the [FalsingCollector] and [FalsingManager] APIs that's required by
31  * external callers.
32  *
33  * E.g. methods of the above APIs that are not exposed by this class either don't need to be invoked
34  * by external callers (as they're already called by the scene framework) or haven't been added yet.
35  */
36 @SysUISingleton
37 class FalsingInteractor
38 @Inject
39 constructor(
40     @FalsingCollectorActual private val collector: FalsingCollector,
41     private val manager: FalsingManager,
42 ) {
43     /**
44      * Notifies of a [MotionEvent] that passed through the UI.
45      *
46      * Must call [onMotionEventComplete] when done with this event.
47      */
onTouchEventnull48     fun onTouchEvent(event: MotionEvent) = collector.onTouchEvent(event)
49 
50     /**
51      * Notifies that a [MotionEvent] has finished being dispatched through the UI.
52      *
53      * Must be called after each call to [onTouchEvent].
54      */
55     fun onMotionEventComplete() = collector.onMotionEventComplete()
56 
57     /**
58      * Instructs the falsing system to ignore the rest of the current input gesture; automatically
59      * resets when another gesture is started (with the next down event).
60      */
61     fun avoidGesture() = collector.avoidGesture()
62 
63     /**
64      * Inserts the given [result] into the falsing system, affecting future runs of the classifier
65      * as if this was a result that had organically happened before.
66      */
67     fun updateFalseConfidence(result: FalsingClassifier.Result) =
68         collector.updateFalseConfidence(result)
69 
70     /** Returns `true` if the gesture should be rejected. */
71     fun isFalseTouch(@Classifier.InteractionType interactionType: Int): Boolean =
72         manager.isFalseTouch(interactionType)
73 
74     /** Returns `true` if the tap gesture should be rejected */
75     fun isFalseTap(@Penalty penalty: Int): Boolean = manager.isFalseTap(penalty)
76 }
77 
78 inline fun FalsingInteractor.runIfNotFalseTap(
79     penalty: Int = FalsingManager.LOW_PENALTY,
80     action: () -> Unit,
81 ) {
82     if (!isFalseTap(penalty)) {
83         action()
84     }
85 }
86