blob: 86bea873b737eac2c818809ab108907c59cea96b [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.util.HashMap;
22
23/**
24 * A classifier which looks at the speed and distance between successive points of a Stroke.
25 * It looks at two consecutive speeds between two points and calculates the ratio between them.
26 * The final result is the maximum of these values. It does the same for distances. If some speed
27 * or distance is equal to zero then the ratio between this and the next part is not calculated. To
28 * the duration of each part there is added one nanosecond so that it is always possible to
29 * calculate the speed of a part.
30 */
31public class AccelerationClassifier extends StrokeClassifier {
32 private final HashMap<Stroke, Data> mStrokeMap = new HashMap<>();
33
34 public AccelerationClassifier(ClassifierData classifierData) {
35 mClassifierData = classifierData;
36 }
37
38 @Override
39 public void onTouchEvent(MotionEvent event) {
40 int action = event.getActionMasked();
41
42 if (action == MotionEvent.ACTION_DOWN) {
43 mStrokeMap.clear();
44 }
45
46 for (int i = 0; i < event.getPointerCount(); i++) {
47 Stroke stroke = mClassifierData.getStroke(event.getPointerId(i));
48 Point point = stroke.getPoints().get(stroke.getPoints().size() - 1);
49 if (mStrokeMap.get(stroke) == null) {
50 mStrokeMap.put(stroke, new Data(point));
51 } else {
52 mStrokeMap.get(stroke).addPoint(point);
53 }
54 }
55 }
56
57 @Override
58 public float getFalseTouchEvaluation(int type, Stroke stroke) {
59 Data data = mStrokeMap.get(stroke);
60 return SpeedRatioEvaluator.evaluate(data.maxSpeedRatio)
61 + DistanceRatioEvaluator.evaluate(data.maxDistanceRatio);
62 }
63
64 private static class Data {
65 public Point previousPoint;
66 public float previousSpeed;
67 public float previousDistance;
68 public float maxSpeedRatio;
69 public float maxDistanceRatio;
70
71 public Data(Point point) {
72 previousPoint = point;
73 previousSpeed = previousDistance = 0.0f;
74 maxDistanceRatio = maxSpeedRatio = 0.0f;
75 }
76
77 public void addPoint(Point point) {
78 float distance = previousPoint.dist(point);
79 float duration = (float) (point.timeOffsetNano - previousPoint.timeOffsetNano + 1);
80 float speed = distance / duration;
81 if (previousDistance != 0.0f) {
82 maxDistanceRatio = Math.max(maxDistanceRatio, distance / previousDistance);
83 }
84
85 if (previousSpeed != 0.0f) {
86 maxSpeedRatio = Math.max(maxSpeedRatio, speed / previousSpeed);
87 }
88
89 previousDistance = distance;
90 previousSpeed = speed;
91 previousPoint = point;
92 }
93 }
94}