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
17package com.android.incallui.answer.impl.classifier;
18
19import android.util.ArrayMap;
20import android.view.MotionEvent;
21import java.util.ArrayList;
22import java.util.List;
23import java.util.Map;
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) and
28 * then it calculates the angle variance of these points like the class {@link AnglesClassifier}
29 * (without splitting it into two parts). The classifier ignores the last point of a stroke because
30 * the UP event comes in with some delay and this ruins the smoothness of this curve. Additionally,
31 * the classifier classifies calculates the percentage of angles which value is in [PI -
32 * ANGLE_DEVIATION, 2* PI) interval. The reason why the classifier does that is because the speed of
33 * a good stroke is most often increases, so most of these angels should be in this interval.
34 */
35class SpeedAnglesClassifier extends StrokeClassifier {
36  private Map<Stroke, Data> strokeMap = new ArrayMap<>();
37
38  public SpeedAnglesClassifier(ClassifierData classifierData) {
39    this.classifierData = classifierData;
40  }
41
42  @Override
43  public String getTag() {
44    return "SPD_ANG";
45  }
46
47  @Override
48  public void onTouchEvent(MotionEvent event) {
49    int action = event.getActionMasked();
50
51    if (action == MotionEvent.ACTION_DOWN) {
52      strokeMap.clear();
53    }
54
55    for (int i = 0; i < event.getPointerCount(); i++) {
56      Stroke stroke = classifierData.getStroke(event.getPointerId(i));
57
58      if (strokeMap.get(stroke) == null) {
59        strokeMap.put(stroke, new Data());
60      }
61
62      if (action != MotionEvent.ACTION_UP
63          && action != MotionEvent.ACTION_CANCEL
64          && !(action == MotionEvent.ACTION_POINTER_UP && i == event.getActionIndex())) {
65        strokeMap.get(stroke).addPoint(stroke.getPoints().get(stroke.getPoints().size() - 1));
66      }
67    }
68  }
69
70  @Override
71  public float getFalseTouchEvaluation(Stroke stroke) {
72    Data data = strokeMap.get(stroke);
73    return SpeedVarianceEvaluator.evaluate(data.getAnglesVariance())
74        + SpeedAnglesPercentageEvaluator.evaluate(data.getAnglesPercentage());
75  }
76
77  private static class Data {
78    private static final float DURATION_SCALE = 1e8f;
79    private static final float LENGTH_SCALE = 1.0f;
80    private static final float ANGLE_DEVIATION = (float) Math.PI / 10.0f;
81
82    private List<Point> lastThreePoints = new ArrayList<>();
83    private Point previousPoint;
84    private float previousAngle;
85    private float sumSquares;
86    private float sum;
87    private float count;
88    private float dist;
89    private float anglesCount;
90    private float acceleratingAngles;
91
92    public Data() {
93      previousPoint = null;
94      previousAngle = (float) Math.PI;
95      sumSquares = 0.0f;
96      sum = 0.0f;
97      count = 1.0f;
98      dist = 0.0f;
99      anglesCount = acceleratingAngles = 0.0f;
100    }
101
102    public void addPoint(Point point) {
103      if (previousPoint != null) {
104        dist += previousPoint.dist(point);
105      }
106
107      previousPoint = point;
108      Point speedPoint =
109          new Point((float) point.timeOffsetNano / DURATION_SCALE, dist / LENGTH_SCALE);
110
111      // Checking if the added point is different than the previously added point
112      // Repetitions are being ignored so that proper angles are calculated.
113      if (lastThreePoints.isEmpty()
114          || !lastThreePoints.get(lastThreePoints.size() - 1).equals(speedPoint)) {
115        lastThreePoints.add(speedPoint);
116        if (lastThreePoints.size() == 4) {
117          lastThreePoints.remove(0);
118
119          float angle =
120              lastThreePoints.get(1).getAngle(lastThreePoints.get(0), lastThreePoints.get(2));
121
122          anglesCount++;
123          if (angle >= (float) Math.PI - ANGLE_DEVIATION) {
124            acceleratingAngles++;
125          }
126
127          float difference = angle - previousAngle;
128          sum += difference;
129          sumSquares += difference * difference;
130          count += 1.0f;
131          previousAngle = angle;
132        }
133      }
134    }
135
136    public float getAnglesVariance() {
137      return sumSquares / count - (sum / count) * (sum / count);
138    }
139
140    public float getAnglesPercentage() {
141      if (anglesCount == 0.0f) {
142        return 1.0f;
143      }
144      return (acceleratingAngles) / anglesCount;
145    }
146  }
147}
148