Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.classifier.brightline; |
| 18 | |
Dave Mankoff | 1b80884 | 2019-07-18 17:20:11 -0400 | [diff] [blame] | 19 | import static com.android.internal.config.sysui.SystemUiDeviceConfigFlags.BRIGHTLINE_FALSING_PROXIMITY_PERCENT_COVERED_THRESHOLD; |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 20 | import static com.android.systemui.classifier.Classifier.QUICK_SETTINGS; |
| 21 | |
Dave Mankoff | 1b80884 | 2019-07-18 17:20:11 -0400 | [diff] [blame] | 22 | import android.provider.DeviceConfig; |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 23 | import android.view.MotionEvent; |
| 24 | |
Dave Mankoff | 46b9d68 | 2019-09-12 13:39:42 -0400 | [diff] [blame] | 25 | import com.android.systemui.util.ProximitySensor; |
| 26 | |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 27 | |
| 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 | */ |
| 34 | class ProximityClassifier extends FalsingClassifier { |
| 35 | |
Dave Mankoff | 1b80884 | 2019-07-18 17:20:11 -0400 | [diff] [blame] | 36 | private static final float PERCENT_COVERED_THRESHOLD = 0.1f; |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 37 | private final DistanceClassifier mDistanceClassifier; |
Dave Mankoff | 1b80884 | 2019-07-18 17:20:11 -0400 | [diff] [blame] | 38 | private final float mPercentCoveredThreshold; |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 39 | |
| 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 Mankoff | 1b80884 | 2019-07-18 17:20:11 -0400 | [diff] [blame] | 50 | |
| 51 | mPercentCoveredThreshold = DeviceConfig.getFloat( |
| 52 | DeviceConfig.NAMESPACE_SYSTEMUI, |
| 53 | BRIGHTLINE_FALSING_PROXIMITY_PERCENT_COVERED_THRESHOLD, |
| 54 | PERCENT_COVERED_THRESHOLD); |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 55 | } |
| 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 Mankoff | 46b9d68 | 2019-09-12 13:39:42 -0400 | [diff] [blame] | 100 | 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 Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 106 | } |
| 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 Mankoff | 1b80884 | 2019-07-18 17:20:11 -0400 | [diff] [blame] | 116 | if (mPercentNear > mPercentCoveredThreshold) { |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 117 | 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 | } |