blob: d58274d293a170d837d0fff13fa95ba07bba1d20 [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
19import android.view.MotionEvent;
20
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070021import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.List;
24
25/**
26 * A classifier which for each point from a stroke, it creates a point on plane with coordinates
27 * (timeOffsetNano, distanceCoveredUpToThisPoint) (scaled by DURATION_SCALE and LENGTH_SCALE)
28 * and then it calculates the angle variance of these points like the class
Blazej Magnowskia28de572015-09-25 16:41:27 -070029 * {@link AnglesClassifier} (without splitting it into two parts). The classifier ignores
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070030 * 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 -070031 * smoothness of this curve. Additionally, the classifier classifies calculates the percentage of
32 * angles which value is in [PI - ANGLE_DEVIATION, 2* PI) interval. The reason why the classifier
33 * does that is because the speed of a good stroke is most often increases, so most of these angels
34 * should be in this interval.
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070035 */
Blazej Magnowskia28de572015-09-25 16:41:27 -070036public class SpeedAnglesClassifier extends StrokeClassifier {
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070037 private HashMap<Stroke, Data> mStrokeMap = new HashMap<>();
38
Blazej Magnowskia28de572015-09-25 16:41:27 -070039 public SpeedAnglesClassifier(ClassifierData classifierData) {
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070040 mClassifierData = classifierData;
41 }
42
43 @Override
44 public void onTouchEvent(MotionEvent event) {
45 int action = event.getActionMasked();
46
47 if (action == MotionEvent.ACTION_DOWN) {
48 mStrokeMap.clear();
49 }
50
51 for (int i = 0; i < event.getPointerCount(); i++) {
52 Stroke stroke = mClassifierData.getStroke(event.getPointerId(i));
53
54 if (mStrokeMap.get(stroke) == null) {
55 mStrokeMap.put(stroke, new Data());
56 }
57
58 if (action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_CANCEL
59 && !(action == MotionEvent.ACTION_POINTER_UP && i == event.getActionIndex())) {
60 mStrokeMap.get(stroke).addPoint(
61 stroke.getPoints().get(stroke.getPoints().size() - 1));
62 }
63 }
64 }
65
66 @Override
67 public float getFalseTouchEvaluation(int type, Stroke stroke) {
Blazej Magnowskia28de572015-09-25 16:41:27 -070068 Data data = mStrokeMap.get(stroke);
69 return SpeedVarianceEvaluator.evaluate(data.getAnglesVariance())
70 + SpeedAnglesPercentageEvaluator.evaluate(data.getAnglesPercentage());
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070071 }
72
73 private static class Data {
74 private final float DURATION_SCALE = 1e8f;
75 private final float LENGTH_SCALE = 1.0f;
Blazej Magnowskia28de572015-09-25 16:41:27 -070076 private final float ANGLE_DEVIATION = (float) Math.PI / 10.0f;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070077
78 private List<Point> mLastThreePoints = new ArrayList<>();
79 private Point mPreviousPoint;
80 private float mPreviousAngle;
81 private float mSumSquares;
82 private float mSum;
83 private float mCount;
84 private float mDist;
Blazej Magnowskia28de572015-09-25 16:41:27 -070085 private float mAnglesCount;
86 private float mAcceleratingAngles;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070087
88 public Data() {
89 mPreviousPoint = null;
90 mPreviousAngle = (float) Math.PI;
91 mSumSquares = 0.0f;
92 mSum = 0.0f;
93 mCount = 1.0f;
94 mDist = 0.0f;
Blazej Magnowskia28de572015-09-25 16:41:27 -070095 mAnglesCount = mAcceleratingAngles = 0.0f;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070096 }
97
98 public void addPoint(Point point) {
99 if (mPreviousPoint != null) {
100 mDist += mPreviousPoint.dist(point);
101 }
102
103 mPreviousPoint = point;
104 Point speedPoint = new Point((float) point.timeOffsetNano / DURATION_SCALE,
105 mDist / LENGTH_SCALE);
106
107 // Checking if the added point is different than the previously added point
108 // Repetitions are being ignored so that proper angles are calculated.
109 if (mLastThreePoints.isEmpty()
110 || !mLastThreePoints.get(mLastThreePoints.size() - 1).equals(speedPoint)) {
111 mLastThreePoints.add(speedPoint);
112 if (mLastThreePoints.size() == 4) {
113 mLastThreePoints.remove(0);
114
115 float angle = mLastThreePoints.get(1).getAngle(mLastThreePoints.get(0),
116 mLastThreePoints.get(2));
117
Blazej Magnowskia28de572015-09-25 16:41:27 -0700118 mAnglesCount++;
119 if (angle >= (float) Math.PI - ANGLE_DEVIATION) {
120 mAcceleratingAngles++;
121 }
122
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -0700123 float difference = angle - mPreviousAngle;
124 mSum += difference;
125 mSumSquares += difference * difference;
126 mCount += 1.0;
127 mPreviousAngle = angle;
128 }
129 }
130 }
131
132 public float getAnglesVariance() {
133 return mSumSquares / mCount - (mSum / mCount) * (mSum / mCount);
134 }
Blazej Magnowskia28de572015-09-25 16:41:27 -0700135
136 public float getAnglesPercentage() {
137 if (mAnglesCount == 0.0f) {
138 return 1.0f;
139 }
140 return (mAcceleratingAngles) / mAnglesCount;
141 }
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -0700142 }
143}