Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 android.content.Context; |
| 20 | import android.database.ContentObserver; |
| 21 | import android.hardware.SensorEvent; |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
Selim Cinek | f8c4add | 2017-06-08 09:54:58 -0700 | [diff] [blame] | 23 | import android.os.Looper; |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 24 | import android.os.UserHandle; |
| 25 | import android.provider.Settings; |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 26 | import android.util.DisplayMetrics; |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 27 | import android.view.MotionEvent; |
| 28 | |
Adrian Roos | 633b6ba | 2017-09-05 16:11:10 +0200 | [diff] [blame] | 29 | import com.android.systemui.R; |
| 30 | |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 31 | import java.util.ArrayDeque; |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 32 | |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 33 | /** |
| 34 | * An classifier trying to determine whether it is a human interacting with the phone or not. |
| 35 | */ |
| 36 | public class HumanInteractionClassifier extends Classifier { |
| 37 | private static final String HIC_ENABLE = "HIC_enable"; |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 38 | private static final float FINGER_DISTANCE = 0.1f; |
Adrian Roos | 5c42df1 | 2016-01-27 08:48:42 -0800 | [diff] [blame] | 39 | |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 40 | private static HumanInteractionClassifier sInstance = null; |
| 41 | |
Selim Cinek | f8c4add | 2017-06-08 09:54:58 -0700 | [diff] [blame] | 42 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 43 | private final Context mContext; |
| 44 | |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 45 | private final StrokeClassifier[] mStrokeClassifiers; |
| 46 | private final GestureClassifier[] mGestureClassifiers; |
| 47 | private final ArrayDeque<MotionEvent> mBufferedEvents = new ArrayDeque<>(); |
| 48 | private final HistoryEvaluator mHistoryEvaluator; |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 49 | private final float mDpi; |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 50 | |
Blazej Magnowski | 52af6b6 | 2015-09-30 15:46:17 -0700 | [diff] [blame] | 51 | private boolean mEnableClassifier = false; |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 52 | private int mCurrentType = Classifier.GENERIC; |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 53 | |
| 54 | protected final ContentObserver mSettingsObserver = new ContentObserver(mHandler) { |
| 55 | @Override |
| 56 | public void onChange(boolean selfChange) { |
| 57 | updateConfiguration(); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | private HumanInteractionClassifier(Context context) { |
| 62 | mContext = context; |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 63 | DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); |
| 64 | |
| 65 | // If the phone is rotated to landscape, the calculations would be wrong if xdpi and ydpi |
| 66 | // were to be used separately. Due negligible differences in xdpi and ydpi we can just |
| 67 | // take the average. |
Adrian Roos | 5691850 | 2016-04-20 18:36:18 -0700 | [diff] [blame] | 68 | // Note that xdpi and ydpi are the physical pixels per inch and are not affected by scaling. |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 69 | mDpi = (displayMetrics.xdpi + displayMetrics.ydpi) / 2.0f; |
| 70 | mClassifierData = new ClassifierData(mDpi); |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 71 | mHistoryEvaluator = new HistoryEvaluator(); |
| 72 | |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 73 | mStrokeClassifiers = new StrokeClassifier[]{ |
| 74 | new AnglesClassifier(mClassifierData), |
| 75 | new SpeedClassifier(mClassifierData), |
| 76 | new DurationCountClassifier(mClassifierData), |
| 77 | new EndPointRatioClassifier(mClassifierData), |
| 78 | new EndPointLengthClassifier(mClassifierData), |
| 79 | new AccelerationClassifier(mClassifierData), |
| 80 | new SpeedAnglesClassifier(mClassifierData), |
| 81 | new LengthCountClassifier(mClassifierData), |
| 82 | new DirectionClassifier(mClassifierData), |
| 83 | }; |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 84 | |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 85 | mGestureClassifiers = new GestureClassifier[] { |
| 86 | new PointerCountClassifier(mClassifierData), |
| 87 | new ProximityClassifier(mClassifierData) |
| 88 | }; |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 89 | |
| 90 | mContext.getContentResolver().registerContentObserver( |
| 91 | Settings.Global.getUriFor(HIC_ENABLE), false, |
| 92 | mSettingsObserver, |
| 93 | UserHandle.USER_ALL); |
| 94 | |
| 95 | updateConfiguration(); |
| 96 | } |
| 97 | |
| 98 | public static HumanInteractionClassifier getInstance(Context context) { |
| 99 | if (sInstance == null) { |
| 100 | sInstance = new HumanInteractionClassifier(context); |
| 101 | } |
| 102 | return sInstance; |
| 103 | } |
| 104 | |
| 105 | private void updateConfiguration() { |
Adrian Roos | 633b6ba | 2017-09-05 16:11:10 +0200 | [diff] [blame] | 106 | boolean defaultValue = mContext.getResources().getBoolean( |
| 107 | R.bool.config_lockscreenAntiFalsingClassifierEnabled); |
| 108 | |
Adrian Roos | 5c42df1 | 2016-01-27 08:48:42 -0800 | [diff] [blame] | 109 | mEnableClassifier = 0 != Settings.Global.getInt( |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 110 | mContext.getContentResolver(), |
Adrian Roos | 633b6ba | 2017-09-05 16:11:10 +0200 | [diff] [blame] | 111 | HIC_ENABLE, defaultValue ? 1 : 0); |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 114 | public void setType(int type) { |
| 115 | mCurrentType = type; |
| 116 | } |
| 117 | |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 118 | @Override |
| 119 | public void onTouchEvent(MotionEvent event) { |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 120 | if (!mEnableClassifier) { |
| 121 | return; |
| 122 | } |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 123 | |
Adrian Roos | c5584ce | 2016-02-24 14:17:19 -0800 | [diff] [blame] | 124 | // If the user is dragging down the notification, they might want to drag it down |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 125 | // enough to see the content, read it for a while and then lift the finger to open |
| 126 | // the notification. This kind of motion scores very bad in the Classifier so the |
| 127 | // MotionEvents which are close to the current position of the finger are not |
| 128 | // sent to the classifiers until the finger moves far enough. When the finger if lifted |
| 129 | // up, the last MotionEvent which was far enough from the finger is set as the final |
| 130 | // MotionEvent and sent to the Classifiers. |
Selim Cinek | 3d6ae23 | 2019-01-04 14:14:33 -0800 | [diff] [blame] | 131 | if (mCurrentType == Classifier.NOTIFICATION_DRAG_DOWN |
| 132 | || mCurrentType == Classifier.PULSE_EXPAND) { |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 133 | mBufferedEvents.add(MotionEvent.obtain(event)); |
| 134 | Point pointEnd = new Point(event.getX() / mDpi, event.getY() / mDpi); |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 135 | |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 136 | while (pointEnd.dist(new Point(mBufferedEvents.getFirst().getX() / mDpi, |
| 137 | mBufferedEvents.getFirst().getY() / mDpi)) > FINGER_DISTANCE) { |
| 138 | addTouchEvent(mBufferedEvents.getFirst()); |
| 139 | mBufferedEvents.remove(); |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | int action = event.getActionMasked(); |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 143 | if (action == MotionEvent.ACTION_UP) { |
| 144 | mBufferedEvents.getFirst().setAction(MotionEvent.ACTION_UP); |
| 145 | addTouchEvent(mBufferedEvents.getFirst()); |
| 146 | mBufferedEvents.clear(); |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 147 | } |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 148 | } else { |
| 149 | addTouchEvent(event); |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 153 | private void addTouchEvent(MotionEvent event) { |
| 154 | mClassifierData.update(event); |
| 155 | |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 156 | for (StrokeClassifier c : mStrokeClassifiers) { |
| 157 | c.onTouchEvent(event); |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 160 | for (GestureClassifier c : mGestureClassifiers) { |
| 161 | c.onTouchEvent(event); |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | int size = mClassifierData.getEndingStrokes().size(); |
| 165 | for (int i = 0; i < size; i++) { |
| 166 | Stroke stroke = mClassifierData.getEndingStrokes().get(i); |
| 167 | float evaluation = 0.0f; |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 168 | StringBuilder sb = FalsingLog.ENABLED ? new StringBuilder("stroke") : null; |
| 169 | for (StrokeClassifier c : mStrokeClassifiers) { |
| 170 | float e = c.getFalseTouchEvaluation(mCurrentType, stroke); |
| 171 | if (FalsingLog.ENABLED) { |
| 172 | String tag = c.getTag(); |
| 173 | sb.append(" ").append(e >= 1f ? tag : tag.toLowerCase()).append("=").append(e); |
| 174 | } |
| 175 | evaluation += e; |
| 176 | } |
| 177 | |
| 178 | if (FalsingLog.ENABLED) { |
| 179 | FalsingLog.i(" addTouchEvent", sb.toString()); |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 180 | } |
| 181 | mHistoryEvaluator.addStroke(evaluation); |
| 182 | } |
| 183 | |
| 184 | int action = event.getActionMasked(); |
| 185 | if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { |
| 186 | float evaluation = 0.0f; |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 187 | StringBuilder sb = FalsingLog.ENABLED ? new StringBuilder("gesture") : null; |
| 188 | for (GestureClassifier c : mGestureClassifiers) { |
| 189 | float e = c.getFalseTouchEvaluation(mCurrentType); |
| 190 | if (FalsingLog.ENABLED) { |
| 191 | String tag = c.getTag(); |
| 192 | sb.append(" ").append(e >= 1f ? tag : tag.toLowerCase()).append("=").append(e); |
| 193 | } |
| 194 | evaluation += e; |
| 195 | } |
| 196 | if (FalsingLog.ENABLED) { |
| 197 | FalsingLog.i(" addTouchEvent", sb.toString()); |
Blazej Magnowski | 91be2f8 | 2015-09-24 16:41:58 -0700 | [diff] [blame] | 198 | } |
| 199 | mHistoryEvaluator.addGesture(evaluation); |
| 200 | setType(Classifier.GENERIC); |
| 201 | } |
| 202 | |
| 203 | mClassifierData.cleanUp(event); |
| 204 | } |
| 205 | |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public void onSensorChanged(SensorEvent event) { |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 208 | for (Classifier c : mStrokeClassifiers) { |
| 209 | c.onSensorChanged(event); |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 212 | for (Classifier c : mGestureClassifiers) { |
| 213 | c.onSensorChanged(event); |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 214 | } |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Blazej Magnowski | 9f01c5b | 2015-09-17 15:14:29 -0700 | [diff] [blame] | 217 | public boolean isFalseTouch() { |
Blazej Magnowski | 6dc59b4 | 2015-09-22 15:14:20 -0700 | [diff] [blame] | 218 | if (mEnableClassifier) { |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 219 | float evaluation = mHistoryEvaluator.getEvaluation(); |
| 220 | boolean result = evaluation >= 5.0f; |
| 221 | if (FalsingLog.ENABLED) { |
| 222 | FalsingLog.i("isFalseTouch", new StringBuilder() |
| 223 | .append("eval=").append(evaluation).append(" result=") |
| 224 | .append(result ? 1 : 0).toString()); |
| 225 | } |
| 226 | return result; |
Blazej Magnowski | 6dc59b4 | 2015-09-22 15:14:20 -0700 | [diff] [blame] | 227 | } |
| 228 | return false; |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | public boolean isEnabled() { |
| 232 | return mEnableClassifier; |
| 233 | } |
Adrian Roos | 401caae | 2016-03-04 13:35:21 -0800 | [diff] [blame] | 234 | |
| 235 | @Override |
| 236 | public String getTag() { |
| 237 | return "HIC"; |
| 238 | } |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 239 | } |