Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -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.view.MotionEvent; |
| 20 | |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.HashMap; |
| 23 | import 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 Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 29 | * {@link AnglesClassifier} (without splitting it into two parts). The classifier ignores |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 30 | * the last point of a stroke because the UP event comes in with some delay and this ruins the |
Blazej Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 31 | * 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 Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 35 | */ |
Blazej Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 36 | public class SpeedAnglesClassifier extends StrokeClassifier { |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 37 | private HashMap<Stroke, Data> mStrokeMap = new HashMap<>(); |
| 38 | |
Blazej Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 39 | public SpeedAnglesClassifier(ClassifierData classifierData) { |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 40 | 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 Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 68 | Data data = mStrokeMap.get(stroke); |
| 69 | return SpeedVarianceEvaluator.evaluate(data.getAnglesVariance()) |
| 70 | + SpeedAnglesPercentageEvaluator.evaluate(data.getAnglesPercentage()); |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private static class Data { |
| 74 | private final float DURATION_SCALE = 1e8f; |
| 75 | private final float LENGTH_SCALE = 1.0f; |
Blazej Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 76 | private final float ANGLE_DEVIATION = (float) Math.PI / 10.0f; |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 77 | |
| 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 Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 85 | private float mAnglesCount; |
| 86 | private float mAcceleratingAngles; |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 87 | |
| 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 Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 95 | mAnglesCount = mAcceleratingAngles = 0.0f; |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 96 | } |
| 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 Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 118 | mAnglesCount++; |
| 119 | if (angle >= (float) Math.PI - ANGLE_DEVIATION) { |
| 120 | mAcceleratingAngles++; |
| 121 | } |
| 122 | |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 123 | 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 Magnowski | a28de57 | 2015-09-25 16:41:27 -0700 | [diff] [blame] | 135 | |
| 136 | public float getAnglesPercentage() { |
| 137 | if (mAnglesCount == 0.0f) { |
| 138 | return 1.0f; |
| 139 | } |
| 140 | return (mAcceleratingAngles) / mAnglesCount; |
| 141 | } |
Blazej Magnowski | 68d0c9b | 2015-09-18 17:25:55 -0700 | [diff] [blame] | 142 | } |
| 143 | } |