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 java.util.ArrayList; 20 21 /** 22 * Contains data about a stroke (a single trace, all the events from a given id from the 23 * DOWN/POINTER_DOWN event till the UP/POINTER_UP/CANCEL event.) 24 */ 25 public class Stroke { 26 private final float NANOS_TO_SECONDS = 1e9f; 27 28 private ArrayList<Point> mPoints = new ArrayList<>(); 29 private long mStartTimeNano; 30 private long mEndTimeNano; 31 private float mLength; 32 private final float mDpi; 33 Stroke(long eventTimeNano, float dpi)34 public Stroke(long eventTimeNano, float dpi) { 35 mDpi = dpi; 36 mStartTimeNano = mEndTimeNano = eventTimeNano; 37 } 38 addPoint(float x, float y, long eventTimeNano)39 public void addPoint(float x, float y, long eventTimeNano) { 40 mEndTimeNano = eventTimeNano; 41 Point point = new Point(x / mDpi, y / mDpi, eventTimeNano - mStartTimeNano); 42 if (!mPoints.isEmpty()) { 43 mLength += mPoints.get(mPoints.size() - 1).dist(point); 44 } 45 mPoints.add(point); 46 } 47 getCount()48 public int getCount() { 49 return mPoints.size(); 50 } 51 getTotalLength()52 public float getTotalLength() { 53 return mLength; 54 } 55 getEndPointLength()56 public float getEndPointLength() { 57 return mPoints.get(0).dist(mPoints.get(mPoints.size() - 1)); 58 } 59 getDurationNanos()60 public long getDurationNanos() { 61 return mEndTimeNano - mStartTimeNano; 62 } 63 getDurationSeconds()64 public float getDurationSeconds() { 65 return (float) getDurationNanos() / NANOS_TO_SECONDS; 66 } 67 getPoints()68 public ArrayList<Point> getPoints() { 69 return mPoints; 70 } 71 getLastEventTimeNano()72 public long getLastEventTimeNano() { 73 if (mPoints.isEmpty()) { 74 return mStartTimeNano; 75 } 76 77 return mPoints.get(mPoints.size() - 1).timeOffsetNano; 78 } 79 } 80