• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.inputmethod.latin;
18 
19 import com.android.inputmethod.annotations.UsedForTesting;
20 
21 import android.util.Log;
22 
23 // TODO: This class is not thread-safe.
24 public final class InputPointers {
25     private static final String TAG = InputPointers.class.getSimpleName();
26     private final int mDefaultCapacity;
27     private final ResizableIntArray mXCoordinates;
28     private final ResizableIntArray mYCoordinates;
29     private final ResizableIntArray mPointerIds;
30     private final ResizableIntArray mTimes;
31 
InputPointers(int defaultCapacity)32     public InputPointers(int defaultCapacity) {
33         mDefaultCapacity = defaultCapacity;
34         mXCoordinates = new ResizableIntArray(defaultCapacity);
35         mYCoordinates = new ResizableIntArray(defaultCapacity);
36         mPointerIds = new ResizableIntArray(defaultCapacity);
37         mTimes = new ResizableIntArray(defaultCapacity);
38     }
39 
addPointer(int index, int x, int y, int pointerId, int time)40     public void addPointer(int index, int x, int y, int pointerId, int time) {
41         mXCoordinates.add(index, x);
42         mYCoordinates.add(index, y);
43         mPointerIds.add(index, pointerId);
44         mTimes.add(index, time);
45     }
46 
47     @UsedForTesting
addPointer(int x, int y, int pointerId, int time)48     void addPointer(int x, int y, int pointerId, int time) {
49         mXCoordinates.add(x);
50         mYCoordinates.add(y);
51         mPointerIds.add(pointerId);
52         mTimes.add(time);
53     }
54 
set(InputPointers ip)55     public void set(InputPointers ip) {
56         mXCoordinates.set(ip.mXCoordinates);
57         mYCoordinates.set(ip.mYCoordinates);
58         mPointerIds.set(ip.mPointerIds);
59         mTimes.set(ip.mTimes);
60     }
61 
copy(InputPointers ip)62     public void copy(InputPointers ip) {
63         mXCoordinates.copy(ip.mXCoordinates);
64         mYCoordinates.copy(ip.mYCoordinates);
65         mPointerIds.copy(ip.mPointerIds);
66         mTimes.copy(ip.mTimes);
67     }
68 
69     /**
70      * Append the pointers in the specified {@link InputPointers} to the end of this.
71      * @param src the source {@link InputPointers} to read the data from.
72      * @param startPos the starting index of the pointers in {@code src}.
73      * @param length the number of pointers to be appended.
74      */
75     @UsedForTesting
append(InputPointers src, int startPos, int length)76     void append(InputPointers src, int startPos, int length) {
77         if (length == 0) {
78             return;
79         }
80         mXCoordinates.append(src.mXCoordinates, startPos, length);
81         mYCoordinates.append(src.mYCoordinates, startPos, length);
82         mPointerIds.append(src.mPointerIds, startPos, length);
83         mTimes.append(src.mTimes, startPos, length);
84     }
85 
86     /**
87      * Append the times, x-coordinates and y-coordinates in the specified {@link ResizableIntArray}
88      * to the end of this.
89      * @param pointerId the pointer id of the source.
90      * @param times the source {@link ResizableIntArray} to read the event times from.
91      * @param xCoordinates the source {@link ResizableIntArray} to read the x-coordinates from.
92      * @param yCoordinates the source {@link ResizableIntArray} to read the y-coordinates from.
93      * @param startPos the starting index of the data in {@code times} and etc.
94      * @param length the number of data to be appended.
95      */
append(int pointerId, ResizableIntArray times, ResizableIntArray xCoordinates, ResizableIntArray yCoordinates, int startPos, int length)96     public void append(int pointerId, ResizableIntArray times, ResizableIntArray xCoordinates,
97             ResizableIntArray yCoordinates, int startPos, int length) {
98         if (length == 0) {
99             return;
100         }
101         mXCoordinates.append(xCoordinates, startPos, length);
102         mYCoordinates.append(yCoordinates, startPos, length);
103         mPointerIds.fill(pointerId, mPointerIds.getLength(), length);
104         mTimes.append(times, startPos, length);
105     }
106 
reset()107     public void reset() {
108         final int defaultCapacity = mDefaultCapacity;
109         mXCoordinates.reset(defaultCapacity);
110         mYCoordinates.reset(defaultCapacity);
111         mPointerIds.reset(defaultCapacity);
112         mTimes.reset(defaultCapacity);
113     }
114 
getPointerSize()115     public int getPointerSize() {
116         return mXCoordinates.getLength();
117     }
118 
getXCoordinates()119     public int[] getXCoordinates() {
120         return mXCoordinates.getPrimitiveArray();
121     }
122 
getYCoordinates()123     public int[] getYCoordinates() {
124         return mYCoordinates.getPrimitiveArray();
125     }
126 
getPointerIds()127     public int[] getPointerIds() {
128         return mPointerIds.getPrimitiveArray();
129     }
130 
getTimes()131     public int[] getTimes() {
132         if (LatinImeLogger.sDBG) {
133             if (!isValidTimeStamps()) {
134                 throw new RuntimeException("Time stamps are invalid.");
135             }
136         }
137         return mTimes.getPrimitiveArray();
138     }
139 
140     @Override
toString()141     public String toString() {
142         return "size=" + getPointerSize() + " id=" + mPointerIds + " time=" + mTimes
143                 + " x=" + mXCoordinates + " y=" + mYCoordinates;
144     }
145 
isValidTimeStamps()146     private boolean isValidTimeStamps() {
147         final int[] times = mTimes.getPrimitiveArray();
148         for (int i = 1; i < getPointerSize(); ++i) {
149             if (times[i] < times[i - 1]) {
150                 // dump
151                 for (int j = 0; j < times.length; ++j) {
152                     Log.d(TAG, "--- (" + j + ") " + times[j]);
153                 }
154                 return false;
155             }
156         }
157         return true;
158     }
159 }
160