blob: d544a3d68671f3d4cc27600b3837c78555bfdcad [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
21import java.lang.Math;
22
23import 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 {
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070039 private HashMap<Stroke, Data> mStrokeMap = new HashMap<>();
40
Blazej Magnowskia28de572015-09-25 16:41:27 -070041 public SpeedAnglesClassifier(ClassifierData classifierData) {
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070042 mClassifierData = classifierData;
43 }
44
45 @Override
46 public void onTouchEvent(MotionEvent event) {
47 int action = event.getActionMasked();
48
49 if (action == MotionEvent.ACTION_DOWN) {
50 mStrokeMap.clear();
51 }
52
53 for (int i = 0; i < event.getPointerCount(); i++) {
54 Stroke stroke = mClassifierData.getStroke(event.getPointerId(i));
55
56 if (mStrokeMap.get(stroke) == null) {
57 mStrokeMap.put(stroke, new Data());
58 }
59
60 if (action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_CANCEL
61 && !(action == MotionEvent.ACTION_POINTER_UP && i == event.getActionIndex())) {
62 mStrokeMap.get(stroke).addPoint(
63 stroke.getPoints().get(stroke.getPoints().size() - 1));
64 }
65 }
66 }
67
68 @Override
69 public float getFalseTouchEvaluation(int type, Stroke stroke) {
Blazej Magnowskia28de572015-09-25 16:41:27 -070070 Data data = mStrokeMap.get(stroke);
71 return SpeedVarianceEvaluator.evaluate(data.getAnglesVariance())
72 + SpeedAnglesPercentageEvaluator.evaluate(data.getAnglesPercentage());
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070073 }
74
75 private static class Data {
76 private final float DURATION_SCALE = 1e8f;
77 private final float LENGTH_SCALE = 1.0f;
Blazej Magnowskia28de572015-09-25 16:41:27 -070078 private final float ANGLE_DEVIATION = (float) Math.PI / 10.0f;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070079
80 private List<Point> mLastThreePoints = new ArrayList<>();
81 private Point mPreviousPoint;
82 private float mPreviousAngle;
83 private float mSumSquares;
84 private float mSum;
85 private float mCount;
86 private float mDist;
Blazej Magnowskia28de572015-09-25 16:41:27 -070087 private float mAnglesCount;
88 private float mAcceleratingAngles;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070089
90 public Data() {
91 mPreviousPoint = null;
92 mPreviousAngle = (float) Math.PI;
93 mSumSquares = 0.0f;
94 mSum = 0.0f;
95 mCount = 1.0f;
96 mDist = 0.0f;
Blazej Magnowskia28de572015-09-25 16:41:27 -070097 mAnglesCount = mAcceleratingAngles = 0.0f;
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070098 }
99
100 public void addPoint(Point point) {
101 if (mPreviousPoint != null) {
102 mDist += mPreviousPoint.dist(point);
103 }
104
105 mPreviousPoint = point;
106 Point speedPoint = new Point((float) point.timeOffsetNano / DURATION_SCALE,
107 mDist / LENGTH_SCALE);
108
109 // Checking if the added point is different than the previously added point
110 // Repetitions are being ignored so that proper angles are calculated.
111 if (mLastThreePoints.isEmpty()
112 || !mLastThreePoints.get(mLastThreePoints.size() - 1).equals(speedPoint)) {
113 mLastThreePoints.add(speedPoint);
114 if (mLastThreePoints.size() == 4) {
115 mLastThreePoints.remove(0);
116
117 float angle = mLastThreePoints.get(1).getAngle(mLastThreePoints.get(0),
118 mLastThreePoints.get(2));
119
Blazej Magnowskia28de572015-09-25 16:41:27 -0700120 mAnglesCount++;
121 if (angle >= (float) Math.PI - ANGLE_DEVIATION) {
122 mAcceleratingAngles++;
123 }
124
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -0700125 float difference = angle - mPreviousAngle;
126 mSum += difference;
127 mSumSquares += difference * difference;
128 mCount += 1.0;
129 mPreviousAngle = angle;
130 }
131 }
132 }
133
134 public float getAnglesVariance() {
135 return mSumSquares / mCount - (mSum / mCount) * (mSum / mCount);
136 }
Blazej Magnowskia28de572015-09-25 16:41:27 -0700137
138 public float getAnglesPercentage() {
139 if (mAnglesCount == 0.0f) {
140 return 1.0f;
141 }
142 return (mAcceleratingAngles) / mAnglesCount;
143 }
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -0700144 }
145}