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 17 package com.android.systemui.classifier; 18 19 import android.view.MotionEvent; 20 21 import 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 */ 31 public class AccelerationClassifier extends StrokeClassifier { 32 private final HashMap<Stroke, Data> mStrokeMap = new HashMap<>(); 33 AccelerationClassifier(ClassifierData classifierData)34 public AccelerationClassifier(ClassifierData classifierData) { 35 mClassifierData = 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 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 getFalseTouchEvaluation(int type, Stroke stroke)63 public float getFalseTouchEvaluation(int type, Stroke stroke) { 64 Data data = mStrokeMap.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 }