blob: bad739fdc7648e08b9f2f80af5c7def9cc05b58f [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
Adrian Roos401caae2016-03-04 13:35:21 -080039 public String getTag() {
40 return "ACC";
41 }
42
43 @Override
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -070044 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 Point point = stroke.getPoints().get(stroke.getPoints().size() - 1);
54 if (mStrokeMap.get(stroke) == null) {
55 mStrokeMap.put(stroke, new Data(point));
56 } else {
57 mStrokeMap.get(stroke).addPoint(point);
58 }
59 }
60 }
61
62 @Override
63 public float getFalseTouchEvaluation(int type, Stroke stroke) {
64 Data data = mStrokeMap.get(stroke);
65 return SpeedRatioEvaluator.evaluate(data.maxSpeedRatio)
66 + DistanceRatioEvaluator.evaluate(data.maxDistanceRatio);
67 }
68
69 private static class Data {
70 public Point previousPoint;
71 public float previousSpeed;
72 public float previousDistance;
73 public float maxSpeedRatio;
74 public float maxDistanceRatio;
75
76 public Data(Point point) {
77 previousPoint = point;
78 previousSpeed = previousDistance = 0.0f;
79 maxDistanceRatio = maxSpeedRatio = 0.0f;
80 }
81
82 public void addPoint(Point point) {
83 float distance = previousPoint.dist(point);
84 float duration = (float) (point.timeOffsetNano - previousPoint.timeOffsetNano + 1);
85 float speed = distance / duration;
86 if (previousDistance != 0.0f) {
87 maxDistanceRatio = Math.max(maxDistanceRatio, distance / previousDistance);
88 }
89
90 if (previousSpeed != 0.0f) {
91 maxSpeedRatio = Math.max(maxSpeedRatio, speed / previousSpeed);
92 }
93
94 previousDistance = distance;
95 previousSpeed = speed;
96 previousPoint = point;
97 }
98 }
99}