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 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 class Stroke {
26 
27   private static final float NANOS_TO_SECONDS = 1e9f;
28 
29   private ArrayList<Point> points = new ArrayList<>();
30   private long startTimeNano;
31   private long endTimeNano;
32   private float length;
33   private final float dpi;
34 
Stroke(long eventTimeNano, float dpi)35   public Stroke(long eventTimeNano, float dpi) {
36     this.dpi = dpi;
37     startTimeNano = endTimeNano = eventTimeNano;
38   }
39 
addPoint(float x, float y, long eventTimeNano)40   public void addPoint(float x, float y, long eventTimeNano) {
41     endTimeNano = eventTimeNano;
42     Point point = new Point(x / dpi, y / dpi, eventTimeNano - startTimeNano);
43     if (!points.isEmpty()) {
44       length += points.get(points.size() - 1).dist(point);
45     }
46     points.add(point);
47   }
48 
getCount()49   public int getCount() {
50     return points.size();
51   }
52 
getTotalLength()53   public float getTotalLength() {
54     return length;
55   }
56 
getEndPointLength()57   public float getEndPointLength() {
58     return points.get(0).dist(points.get(points.size() - 1));
59   }
60 
getDurationNanos()61   public long getDurationNanos() {
62     return endTimeNano - startTimeNano;
63   }
64 
getDurationSeconds()65   public float getDurationSeconds() {
66     return (float) getDurationNanos() / NANOS_TO_SECONDS;
67   }
68 
getPoints()69   public ArrayList<Point> getPoints() {
70     return points;
71   }
72 }
73