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.ArrayList; 22 import java.util.List; 23 import java.util.Map; 24 25 /** 26 * A classifier which calculates the variance of differences between successive angles in a stroke. 27 * For each stroke it keeps its last three points. If some successive points are the same, it 28 * ignores the repetitions. If a new point is added, the classifier calculates the angle between the 29 * last three points. After that, it calculates the difference between this angle and the previously 30 * calculated angle. Then it calculates the variance of the differences from a stroke. To the 31 * differences there is artificially added value 0.0 and the difference between the first angle and 32 * PI (angles are in radians). It helps with strokes which have few points and punishes more strokes 33 * which are not smooth. 34 * 35 * <p>This classifier also tries to split the stroke into two parts in the place in which the 36 * biggest angle is. It calculates the angle variance of the two parts and sums them up. The reason 37 * the classifier is doing this, is because some human swipes at the beginning go for a moment in 38 * one direction and then they rapidly change direction for the rest of the stroke (like a tick). 39 * The final result is the minimum of angle variance of the whole stroke and the sum of angle 40 * variances of the two parts split up. The classifier tries the tick option only if the first part 41 * is shorter than the second part. 42 * 43 * <p>Additionally, the classifier classifies the angles as left angles (those angles which value is 44 * in [0.0, PI - ANGLE_DEVIATION) interval), straight angles ([PI - ANGLE_DEVIATION, PI + 45 * ANGLE_DEVIATION] interval) and right angles ((PI + ANGLE_DEVIATION, 2 * PI) interval) and then 46 * calculates the percentage of angles which are in the same direction (straight angles can be left 47 * angels or right angles) 48 */ 49 class AnglesClassifier extends StrokeClassifier { 50 private Map<Stroke, Data> strokeMap = new ArrayMap<>(); 51 AnglesClassifier(ClassifierData classifierData)52 public AnglesClassifier(ClassifierData classifierData) { 53 this.classifierData = classifierData; 54 } 55 56 @Override getTag()57 public String getTag() { 58 return "ANG"; 59 } 60 61 @Override onTouchEvent(MotionEvent event)62 public void onTouchEvent(MotionEvent event) { 63 int action = event.getActionMasked(); 64 65 if (action == MotionEvent.ACTION_DOWN) { 66 strokeMap.clear(); 67 } 68 69 for (int i = 0; i < event.getPointerCount(); i++) { 70 Stroke stroke = classifierData.getStroke(event.getPointerId(i)); 71 72 if (strokeMap.get(stroke) == null) { 73 strokeMap.put(stroke, new Data()); 74 } 75 strokeMap.get(stroke).addPoint(stroke.getPoints().get(stroke.getPoints().size() - 1)); 76 } 77 } 78 79 @Override getFalseTouchEvaluation(Stroke stroke)80 public float getFalseTouchEvaluation(Stroke stroke) { 81 Data data = strokeMap.get(stroke); 82 return AnglesVarianceEvaluator.evaluate(data.getAnglesVariance()) 83 + AnglesPercentageEvaluator.evaluate(data.getAnglesPercentage()); 84 } 85 86 private static class Data { 87 private static final float ANGLE_DEVIATION = (float) Math.PI / 20.0f; 88 private static final float MIN_MOVE_DIST_DP = .01f; 89 90 private List<Point> lastThreePoints = new ArrayList<>(); 91 private float firstAngleVariance; 92 private float previousAngle; 93 private float biggestAngle; 94 private float sumSquares; 95 private float secondSumSquares; 96 private float sum; 97 private float secondSum; 98 private float count; 99 private float secondCount; 100 private float firstLength; 101 private float length; 102 private float anglesCount; 103 private float leftAngles; 104 private float rightAngles; 105 private float straightAngles; 106 Data()107 public Data() { 108 firstAngleVariance = 0.0f; 109 previousAngle = (float) Math.PI; 110 biggestAngle = 0.0f; 111 sumSquares = secondSumSquares = 0.0f; 112 sum = secondSum = 0.0f; 113 count = secondCount = 1.0f; 114 length = firstLength = 0.0f; 115 anglesCount = leftAngles = rightAngles = straightAngles = 0.0f; 116 } 117 addPoint(Point point)118 public void addPoint(Point point) { 119 // Checking if the added point is different than the previously added point 120 // Repetitions and short distances are being ignored so that proper angles are calculated. 121 if (lastThreePoints.isEmpty() 122 || (!lastThreePoints.get(lastThreePoints.size() - 1).equals(point) 123 && (lastThreePoints.get(lastThreePoints.size() - 1).dist(point) 124 > MIN_MOVE_DIST_DP))) { 125 if (!lastThreePoints.isEmpty()) { 126 length += lastThreePoints.get(lastThreePoints.size() - 1).dist(point); 127 } 128 lastThreePoints.add(point); 129 if (lastThreePoints.size() == 4) { 130 lastThreePoints.remove(0); 131 132 float angle = 133 lastThreePoints.get(1).getAngle(lastThreePoints.get(0), lastThreePoints.get(2)); 134 135 anglesCount++; 136 if (angle < Math.PI - ANGLE_DEVIATION) { 137 leftAngles++; 138 } else if (angle <= Math.PI + ANGLE_DEVIATION) { 139 straightAngles++; 140 } else { 141 rightAngles++; 142 } 143 144 float difference = angle - previousAngle; 145 146 // If this is the biggest angle of the stroke so then we save the value of 147 // the angle variance so far and start to count the values for the angle 148 // variance of the second part. 149 if (biggestAngle < angle) { 150 biggestAngle = angle; 151 firstLength = length; 152 firstAngleVariance = getAnglesVariance(sumSquares, sum, count); 153 secondSumSquares = 0.0f; 154 secondSum = 0.0f; 155 secondCount = 1.0f; 156 } else { 157 secondSum += difference; 158 secondSumSquares += difference * difference; 159 secondCount += 1.0f; 160 } 161 162 sum += difference; 163 sumSquares += difference * difference; 164 count += 1.0f; 165 previousAngle = angle; 166 } 167 } 168 } 169 getAnglesVariance(float sumSquares, float sum, float count)170 public float getAnglesVariance(float sumSquares, float sum, float count) { 171 return sumSquares / count - (sum / count) * (sum / count); 172 } 173 getAnglesVariance()174 public float getAnglesVariance() { 175 float anglesVariance = getAnglesVariance(sumSquares, sum, count); 176 if (firstLength < length / 2f) { 177 anglesVariance = 178 Math.min( 179 anglesVariance, 180 firstAngleVariance + getAnglesVariance(secondSumSquares, secondSum, secondCount)); 181 } 182 return anglesVariance; 183 } 184 getAnglesPercentage()185 public float getAnglesPercentage() { 186 if (anglesCount == 0.0f) { 187 return 1.0f; 188 } 189 return (Math.max(leftAngles, rightAngles) + straightAngles) / anglesCount; 190 } 191 } 192 } 193