1 /*
2  * Copyright (C) 2020 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;
18 
19 import static com.android.systemui.classifier.Classifier.NOTIFICATION_DRAG_DOWN;
20 import static com.android.systemui.classifier.Classifier.QUICK_SETTINGS;
21 
22 import android.view.MotionEvent;
23 
24 import java.util.Locale;
25 
26 import javax.inject.Inject;
27 
28 /**
29  * False touch if more than one finger touches the screen.
30  *
31  * IMPORTANT: This should not be used for certain cases (i.e. a11y) as we expect multiple fingers
32  * for them.
33  */
34 class PointerCountClassifier extends FalsingClassifier {
35 
36     private static final int MAX_ALLOWED_POINTERS = 1;
37     private static final int MAX_ALLOWED_POINTERS_SWIPE_DOWN = 2;
38     private int mMaxPointerCount;
39 
40     @Inject
PointerCountClassifier(FalsingDataProvider dataProvider)41     PointerCountClassifier(FalsingDataProvider dataProvider) {
42         super(dataProvider);
43     }
44 
45     @Override
onTouchEvent(MotionEvent motionEvent)46     public void onTouchEvent(MotionEvent motionEvent) {
47         int pCount = mMaxPointerCount;
48         if (motionEvent.getActionMasked() == MotionEvent.ACTION_DOWN) {
49             mMaxPointerCount = motionEvent.getPointerCount();
50         } else {
51             mMaxPointerCount = Math.max(mMaxPointerCount, motionEvent.getPointerCount());
52         }
53         if (pCount != mMaxPointerCount) {
54             logDebug("Pointers observed:" + mMaxPointerCount);
55         }
56     }
57 
58     @Override
calculateFalsingResult( @lassifier.InteractionType int interactionType, double historyBelief, double historyConfidence)59     Result calculateFalsingResult(
60             @Classifier.InteractionType int interactionType,
61             double historyBelief, double historyConfidence) {
62         int allowedPointerCount =
63                 (interactionType == QUICK_SETTINGS || interactionType == NOTIFICATION_DRAG_DOWN)
64                 ? MAX_ALLOWED_POINTERS_SWIPE_DOWN : MAX_ALLOWED_POINTERS;
65 
66         return mMaxPointerCount > allowedPointerCount
67                 ? falsed(1, getReason(allowedPointerCount)) : Result.passed(0);
68     }
69 
getReason(int allowedPointerCount)70     private String getReason(int allowedPointerCount) {
71         return String.format(
72                 (Locale) null,
73                 "{pointersObserved=%d, threshold=%d}",
74                 mMaxPointerCount,
75                 allowedPointerCount);
76     }
77 }
78