2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.systemui.classifier;
19 import android.view.MotionEvent;
21 import java.util.HashMap;
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.
31 public class AccelerationClassifier extends StrokeClassifier {
32 private final HashMap<Stroke, Data> mStrokeMap = new HashMap<>();
34 public AccelerationClassifier(ClassifierData classifierData) {
35 mClassifierData = classifierData;
39 public String getTag() {
44 public void onTouchEvent(MotionEvent event) {
45 int action = event.getActionMasked();
47 if (action == MotionEvent.ACTION_DOWN) {
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));
57 mStrokeMap.get(stroke).addPoint(point);
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);
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;
76 public Data(Point point) {
77 previousPoint = point;
78 previousSpeed = previousDistance = 0.0f;
79 maxDistanceRatio = maxSpeedRatio = 0.0f;
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);
90 if (previousSpeed != 0.0f) {
91 maxSpeedRatio = Math.max(maxSpeedRatio, speed / previousSpeed);
94 previousDistance = distance;
95 previousSpeed = speed;
96 previousPoint = point;