1 /*
2  * Copyright (C) 2022 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 package com.android.systemui.classifier;
17 
18 import android.view.MotionEvent;
19 
20 import java.util.List;
21 
22 /**
23  * Falsing classifier that accepts or rejects a gesture as a tap.
24  */
25 public abstract class TapClassifier extends FalsingClassifier{
26     private final float mTouchSlop;
27 
TapClassifier(FalsingDataProvider dataProvider, float touchSlop)28     TapClassifier(FalsingDataProvider dataProvider,
29             float touchSlop) {
30         super(dataProvider);
31         mTouchSlop = touchSlop;
32     }
33 
34     @Override
calculateFalsingResult( @lassifier.InteractionType int interactionType, double historyBelief, double historyConfidence)35     Result calculateFalsingResult(
36             @Classifier.InteractionType int interactionType,
37             double historyBelief, double historyConfidence) {
38         return isTap(getRecentMotionEvents(), 0.5);
39     }
40 
41     /** Given a list of {@link android.view.MotionEvent}'s, returns true if the look like a tap. */
isTap(List<MotionEvent> motionEvents, double falsePenalty)42     public Result isTap(List<MotionEvent> motionEvents, double falsePenalty) {
43         if (motionEvents.isEmpty()) {
44             return falsed(0, "no motion events");
45         }
46         float downX = motionEvents.get(0).getX();
47         float downY = motionEvents.get(0).getY();
48 
49         for (MotionEvent event : motionEvents) {
50             String reason;
51             if (Math.abs(event.getX() - downX) >= mTouchSlop) {
52                 reason = "dX too big for a tap: "
53                         + Math.abs(event.getX() - downX)
54                         + "vs "
55                         + mTouchSlop;
56                 return falsed(falsePenalty, reason);
57             } else if (Math.abs(event.getY() - downY) >= mTouchSlop) {
58                 reason = "dY too big for a tap: "
59                         + Math.abs(event.getY() - downY)
60                         + " vs "
61                         + mTouchSlop;
62                 return falsed(falsePenalty, reason);
63             }
64         }
65         return Result.passed(0);
66     }
67 }
68