blob: 66f0cf68d8c16ffd657b1178d9a6827b9bec4f20 [file] [log] [blame]
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -07001/*
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
17package com.android.systemui.classifier;
18
Dan Sandler39fd33b2018-02-06 16:15:06 -050019import android.os.Build;
20import android.os.SystemProperties;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070021import android.view.MotionEvent;
22
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070023import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.List;
26
27/**
28 * A classifier which for each point from a stroke, it creates a point on plane with coordinates
29 * (timeOffsetNano, distanceCoveredUpToThisPoint) (scaled by DURATION_SCALE and LENGTH_SCALE)
30 * and then it calculates the angle variance of these points like the class
Blazej Magnowskia28de572015-09-25 16:41:27 -070031 * {@link AnglesClassifier} (without splitting it into two parts). The classifier ignores
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070032 * the last point of a stroke because the UP event comes in with some delay and this ruins the
Blazej Magnowskia28de572015-09-25 16:41:27 -070033 * smoothness of this curve. Additionally, the classifier classifies calculates the percentage of
34 * angles which value is in [PI - ANGLE_DEVIATION, 2* PI) interval. The reason why the classifier
35 * does that is because the speed of a good stroke is most often increases, so most of these angels
36 * should be in this interval.
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070037 */
Blazej Magnowskia28de572015-09-25 16:41:27 -070038public class SpeedAnglesClassifier extends StrokeClassifier {
Dan Sandler39fd33b2018-02-06 16:15:06 -050039 public static final boolean VERBOSE = SystemProperties.getBoolean("debug.falsing_log.spd_ang",
40 Build.IS_DEBUGGABLE);
41 public static final String TAG = "SPD_ANG";
42
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070043 private HashMap<Stroke, Data> mStrokeMap = new HashMap<>();
44
Blazej Magnowskia28de572015-09-25 16:41:27 -070045 public SpeedAnglesClassifier(ClassifierData classifierData) {
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070046 mClassifierData = classifierData;
47 }
48
49 @Override
Adrian Roos401caae2016-03-04 13:35:21 -080050 public String getTag() {
Dan Sandler39fd33b2018-02-06 16:15:06 -050051 return TAG;
Adrian Roos401caae2016-03-04 13:35:21 -080052 }
53
54 @Override
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070055 public void onTouchEvent(MotionEvent event) {
56 int action = event.getActionMasked();
57
58 if (action == MotionEvent.ACTION_DOWN) {
59 mStrokeMap.clear();
60 }
61
62 for (int i = 0; i < event.getPointerCount(); i++) {
63 Stroke stroke = mClassifierData.getStroke(event.getPointerId(i));
64
65 if (mStrokeMap.get(stroke) == null) {
66 mStrokeMap.put(stroke, new Data());
67 }
68
69 if (action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_CANCEL
70 && !(action == MotionEvent.ACTION_POINTER_UP && i == event.getActionIndex())) {
71 mStrokeMap.get(stroke).addPoint(
72 stroke.getPoints().get(stroke.getPoints().size() - 1));
73 }
74 }
75 }
76
77 @Override
78 public float getFalseTouchEvaluation(int type, Stroke stroke) {
Blazej Magnowskia28de572015-09-25 16:41:27 -070079 Data data = mStrokeMap.get(stroke);
80 return SpeedVarianceEvaluator.evaluate(data.getAnglesVariance())
81 + SpeedAnglesPercentageEvaluator.evaluate(data.getAnglesPercentage());
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070082 }
83
84 private static class Data {
85 private final float DURATION_SCALE = 1e8f;
86 private final float LENGTH_SCALE = 1.0f;
Blazej Magnowskia28de572015-09-25 16:41:27 -070087 private final float ANGLE_DEVIATION = (float) Math.PI / 10.0f;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070088
89 private List<Point> mLastThreePoints = new ArrayList<>();
90 private Point mPreviousPoint;
91 private float mPreviousAngle;
92 private float mSumSquares;
93 private float mSum;
94 private float mCount;
95 private float mDist;
Blazej Magnowskia28de572015-09-25 16:41:27 -070096 private float mAnglesCount;
97 private float mAcceleratingAngles;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070098
99 public Data() {
100 mPreviousPoint = null;
101 mPreviousAngle = (float) Math.PI;
102 mSumSquares = 0.0f;
103 mSum = 0.0f;
104 mCount = 1.0f;
105 mDist = 0.0f;
Blazej Magnowskia28de572015-09-25 16:41:27 -0700106 mAnglesCount = mAcceleratingAngles = 0.0f;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -0700107 }
108
109 public void addPoint(Point point) {
110 if (mPreviousPoint != null) {
111 mDist += mPreviousPoint.dist(point);
112 }
113
114 mPreviousPoint = point;
115 Point speedPoint = new Point((float) point.timeOffsetNano / DURATION_SCALE,
116 mDist / LENGTH_SCALE);
117
118 // Checking if the added point is different than the previously added point
119 // Repetitions are being ignored so that proper angles are calculated.
120 if (mLastThreePoints.isEmpty()
121 || !mLastThreePoints.get(mLastThreePoints.size() - 1).equals(speedPoint)) {
122 mLastThreePoints.add(speedPoint);
123 if (mLastThreePoints.size() == 4) {
124 mLastThreePoints.remove(0);
125
126 float angle = mLastThreePoints.get(1).getAngle(mLastThreePoints.get(0),
127 mLastThreePoints.get(2));
128
Blazej Magnowskia28de572015-09-25 16:41:27 -0700129 mAnglesCount++;
130 if (angle >= (float) Math.PI - ANGLE_DEVIATION) {
131 mAcceleratingAngles++;
132 }
133
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -0700134 float difference = angle - mPreviousAngle;
135 mSum += difference;
136 mSumSquares += difference * difference;
137 mCount += 1.0;
138 mPreviousAngle = angle;
139 }
140 }
141 }
142
143 public float getAnglesVariance() {
Dan Sandler39fd33b2018-02-06 16:15:06 -0500144 final float v = mSumSquares / mCount - (mSum / mCount) * (mSum / mCount);
145 if (VERBOSE) {
146 FalsingLog.i(TAG, "getAnglesVariance: sum^2=" + mSumSquares
147 + " count=" + mCount + " result=" + v);
148 }
149 return v;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -0700150 }
Blazej Magnowskia28de572015-09-25 16:41:27 -0700151
152 public float getAnglesPercentage() {
153 if (mAnglesCount == 0.0f) {
154 return 1.0f;
155 }
Dan Sandler39fd33b2018-02-06 16:15:06 -0500156 final float v = (mAcceleratingAngles) / mAnglesCount;
157 if (VERBOSE) {
158 FalsingLog.i(TAG, "getAnglesPercentage: angles=" + mAcceleratingAngles
159 + " count=" + mAnglesCount + " result=" + v);
160 }
161 return v;
Blazej Magnowskia28de572015-09-25 16:41:27 -0700162 }
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -0700163 }
164}