1 /*
2  * Copyright (C) 2016 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.incallui.answer.impl.classifier;
18 
19 import android.util.ArrayMap;
20 import android.view.MotionEvent;
21 import java.util.Map;
22 
23 /**
24  * A classifier which looks at the speed and distance between successive points of a Stroke. It
25  * looks at two consecutive speeds between two points and calculates the ratio between them. The
26  * final result is the maximum of these values. It does the same for distances. If some speed or
27  * distance is equal to zero then the ratio between this and the next part is not calculated. To the
28  * duration of each part there is added one nanosecond so that it is always possible to calculate
29  * the speed of a part.
30  */
31 class AccelerationClassifier extends StrokeClassifier {
32   private final Map<Stroke, Data> strokeMap = new ArrayMap<>();
33 
AccelerationClassifier(ClassifierData classifierData)34   public AccelerationClassifier(ClassifierData classifierData) {
35     this.classifierData = classifierData;
36   }
37 
38   @Override
getTag()39   public String getTag() {
40     return "ACC";
41   }
42 
43   @Override
onTouchEvent(MotionEvent event)44   public void onTouchEvent(MotionEvent event) {
45     int action = event.getActionMasked();
46 
47     if (action == MotionEvent.ACTION_DOWN) {
48       strokeMap.clear();
49     }
50 
51     for (int i = 0; i < event.getPointerCount(); i++) {
52       Stroke stroke = classifierData.getStroke(event.getPointerId(i));
53       Point point = stroke.getPoints().get(stroke.getPoints().size() - 1);
54       if (strokeMap.get(stroke) == null) {
55         strokeMap.put(stroke, new Data(point));
56       } else {
57         strokeMap.get(stroke).addPoint(point);
58       }
59     }
60   }
61 
62   @Override
getFalseTouchEvaluation(Stroke stroke)63   public float getFalseTouchEvaluation(Stroke stroke) {
64     Data data = strokeMap.get(stroke);
65     return 2 * SpeedRatioEvaluator.evaluate(data.maxSpeedRatio);
66   }
67 
68   private static class Data {
69 
70     static final float MILLIS_TO_NANOS = 1e6f;
71 
72     Point previousPoint;
73     float previousSpeed = 0;
74     float maxSpeedRatio = 0;
75 
Data(Point point)76     public Data(Point point) {
77       previousPoint = point;
78     }
79 
addPoint(Point point)80     public void addPoint(Point point) {
81       float distance = previousPoint.dist(point);
82       float duration = (float) (point.timeOffsetNano - previousPoint.timeOffsetNano + 1);
83       float speed = distance / duration;
84 
85       if (duration > 20 * MILLIS_TO_NANOS || duration < 5 * MILLIS_TO_NANOS) {
86         // reject this segment and ensure we won't use data about it in the next round.
87         previousSpeed = 0;
88         previousPoint = point;
89         return;
90       }
91       if (previousSpeed != 0.0f) {
92         maxSpeedRatio = Math.max(maxSpeedRatio, speed / previousSpeed);
93       }
94 
95       previousSpeed = speed;
96       previousPoint = point;
97     }
98   }
99 }
100