blob: 5045095a475f80cd572a96f79a6795486245fe8f [file] [log] [blame]
Dave Mankoff8bfbe332019-06-12 17:58:30 -04001/*
2 * Copyright (C) 2019 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
17package com.android.systemui.classifier.brightline;
18
Dave Mankoff1b808842019-07-18 17:20:11 -040019import static com.android.internal.config.sysui.SystemUiDeviceConfigFlags.BRIGHTLINE_FALSING_PROXIMITY_PERCENT_COVERED_THRESHOLD;
Dave Mankoff8bfbe332019-06-12 17:58:30 -040020import static com.android.systemui.classifier.Classifier.QUICK_SETTINGS;
21
Dave Mankoff1b808842019-07-18 17:20:11 -040022import android.provider.DeviceConfig;
Dave Mankoff8bfbe332019-06-12 17:58:30 -040023import android.view.MotionEvent;
24
Dave Mankoff46b9d682019-09-12 13:39:42 -040025import com.android.systemui.util.ProximitySensor;
26
Dave Mankoff8bfbe332019-06-12 17:58:30 -040027
28/**
29 * False touch if proximity sensor is covered for more than a certain percentage of the gesture.
30 *
31 * This classifer is essentially a no-op for QUICK_SETTINGS, as we assume the sensor may be
32 * covered when swiping from the top.
33 */
34class ProximityClassifier extends FalsingClassifier {
35
Dave Mankoff1b808842019-07-18 17:20:11 -040036 private static final float PERCENT_COVERED_THRESHOLD = 0.1f;
Dave Mankoff8bfbe332019-06-12 17:58:30 -040037 private final DistanceClassifier mDistanceClassifier;
Dave Mankoff1b808842019-07-18 17:20:11 -040038 private final float mPercentCoveredThreshold;
Dave Mankoff8bfbe332019-06-12 17:58:30 -040039
40 private boolean mNear;
41 private long mGestureStartTimeNs;
42 private long mPrevNearTimeNs;
43 private long mNearDurationNs;
44 private float mPercentNear;
45
46 ProximityClassifier(DistanceClassifier distanceClassifier,
47 FalsingDataProvider dataProvider) {
48 super(dataProvider);
49 this.mDistanceClassifier = distanceClassifier;
Dave Mankoff1b808842019-07-18 17:20:11 -040050
51 mPercentCoveredThreshold = DeviceConfig.getFloat(
52 DeviceConfig.NAMESPACE_SYSTEMUI,
53 BRIGHTLINE_FALSING_PROXIMITY_PERCENT_COVERED_THRESHOLD,
54 PERCENT_COVERED_THRESHOLD);
Dave Mankoff8bfbe332019-06-12 17:58:30 -040055 }
56
57 @Override
58 void onSessionStarted() {
59 mPrevNearTimeNs = 0;
60 mPercentNear = 0;
61 }
62
63 @Override
64 void onSessionEnded() {
65 mPrevNearTimeNs = 0;
66 mPercentNear = 0;
67 }
68
69 @Override
70 public void onTouchEvent(MotionEvent motionEvent) {
71 int action = motionEvent.getActionMasked();
72
73 if (action == MotionEvent.ACTION_DOWN) {
74 mGestureStartTimeNs = motionEvent.getEventTimeNano();
75 if (mPrevNearTimeNs > 0) {
76 // We only care about if the proximity sensor is triggered while a move event is
77 // happening.
78 mPrevNearTimeNs = motionEvent.getEventTimeNano();
79 }
80 logDebug("Gesture start time: " + mGestureStartTimeNs);
81 mNearDurationNs = 0;
82 }
83
84 if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
85 update(mNear, motionEvent.getEventTimeNano());
86 long duration = motionEvent.getEventTimeNano() - mGestureStartTimeNs;
87
88 logDebug("Gesture duration, Proximity duration: " + duration + ", " + mNearDurationNs);
89
90 if (duration == 0) {
91 mPercentNear = mNear ? 1.0f : 0.0f;
92 } else {
93 mPercentNear = (float) mNearDurationNs / (float) duration;
94 }
95 }
96
97 }
98
99 @Override
Dave Mankoff46b9d682019-09-12 13:39:42 -0400100 public void onProximityEvent(
101 ProximitySensor.ProximityEvent proximityEvent) {
102 boolean near = proximityEvent.getNear();
103 long timestampNs = proximityEvent.getTimestampNs();
104 logDebug("Sensor is: " + near + " at time " + timestampNs);
105 update(near, timestampNs);
Dave Mankoff8bfbe332019-06-12 17:58:30 -0400106 }
107
108 @Override
109 public boolean isFalseTouch() {
110 if (getInteractionType() == QUICK_SETTINGS) {
111 return false;
112 }
113
114 logInfo("Percent of gesture in proximity: " + mPercentNear);
115
Dave Mankoff1b808842019-07-18 17:20:11 -0400116 if (mPercentNear > mPercentCoveredThreshold) {
Dave Mankoff8bfbe332019-06-12 17:58:30 -0400117 return !mDistanceClassifier.isLongSwipe();
118 }
119
120 return false;
121 }
122
123 /**
124 * @param near is the sensor showing the near state right now
125 * @param timeStampNs time of this event in nanoseconds
126 */
127 private void update(boolean near, long timeStampNs) {
128 if (mPrevNearTimeNs != 0 && timeStampNs > mPrevNearTimeNs && mNear) {
129 mNearDurationNs += timeStampNs - mPrevNearTimeNs;
130 logDebug("Updating duration: " + mNearDurationNs);
131 }
132
133 if (near) {
134 logDebug("Set prevNearTimeNs: " + timeStampNs);
135 mPrevNearTimeNs = timeStampNs;
136 }
137
138 mNear = near;
139 }
140}