InputPointers.java revision 64ee09610024eb1436c51f9c9ef9fc3f77239d73
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19// TODO: This class is not thread-safe.
20public class InputPointers {
21    private final int mDefaultCapacity;
22    private final ResizableIntArray mXCoordinates;
23    private final ResizableIntArray mYCoordinates;
24    private final ResizableIntArray mPointerIds;
25    private final ResizableIntArray mTimes;
26
27    public InputPointers(int defaultCapacity) {
28        mDefaultCapacity = defaultCapacity;
29        mXCoordinates = new ResizableIntArray(defaultCapacity);
30        mYCoordinates = new ResizableIntArray(defaultCapacity);
31        mPointerIds = new ResizableIntArray(defaultCapacity);
32        mTimes = new ResizableIntArray(defaultCapacity);
33    }
34
35    public void addPointer(int index, int x, int y, int pointerId, int time) {
36        mXCoordinates.add(index, x);
37        mYCoordinates.add(index, y);
38        mPointerIds.add(index, pointerId);
39        mTimes.add(index, time);
40    }
41
42    public void addPointer(int x, int y, int pointerId, int time) {
43        mXCoordinates.add(x);
44        mYCoordinates.add(y);
45        mPointerIds.add(pointerId);
46        mTimes.add(time);
47    }
48
49    public void set(InputPointers ip) {
50        mXCoordinates.set(ip.mXCoordinates);
51        mYCoordinates.set(ip.mYCoordinates);
52        mPointerIds.set(ip.mPointerIds);
53        mTimes.set(ip.mTimes);
54    }
55
56    public void copy(InputPointers ip) {
57        mXCoordinates.copy(ip.mXCoordinates);
58        mYCoordinates.copy(ip.mYCoordinates);
59        mPointerIds.copy(ip.mPointerIds);
60        mTimes.copy(ip.mTimes);
61    }
62
63    /**
64     * Append the pointers in the specified {@link InputPointers} to the end of this.
65     * @param src the source {@link InputPointers} to read the data from.
66     * @param startPos the starting index of the pointers in {@code src}.
67     * @param length the number of pointers to be appended.
68     */
69    public void append(InputPointers src, int startPos, int length) {
70        if (length == 0) {
71            return;
72        }
73        mXCoordinates.append(src.mXCoordinates, startPos, length);
74        mYCoordinates.append(src.mYCoordinates, startPos, length);
75        mPointerIds.append(src.mPointerIds, startPos, length);
76        mTimes.append(src.mTimes, startPos, length);
77    }
78
79    /**
80     * Append the times, x-coordinates and y-coordinates in the specified {@link ResizableIntArray}
81     * to the end of this.
82     * @param pointerId the pointer id of the source.
83     * @param times the source {@link ResizableIntArray} to read the event times from.
84     * @param xCoordinates the source {@link ResizableIntArray} to read the x-coordinates from.
85     * @param yCoordinates the source {@link ResizableIntArray} to read the y-coordinates from.
86     * @param startPos the starting index of the data in {@code times} and etc.
87     * @param length the number of data to be appended.
88     */
89    public void append(int pointerId, ResizableIntArray times, ResizableIntArray xCoordinates,
90            ResizableIntArray yCoordinates, int startPos, int length) {
91        if (length == 0) {
92            return;
93        }
94        mXCoordinates.append(xCoordinates, startPos, length);
95        mYCoordinates.append(yCoordinates, startPos, length);
96        mPointerIds.fill(pointerId, startPos, length);
97        mTimes.append(times, startPos, length);
98    }
99
100    public void reset() {
101        final int defaultCapacity = mDefaultCapacity;
102        mXCoordinates.reset(defaultCapacity);
103        mYCoordinates.reset(defaultCapacity);
104        mPointerIds.reset(defaultCapacity);
105        mTimes.reset(defaultCapacity);
106    }
107
108    public int getPointerSize() {
109        return mXCoordinates.getLength();
110    }
111
112    public int[] getXCoordinates() {
113        return mXCoordinates.getPrimitiveArray();
114    }
115
116    public int[] getYCoordinates() {
117        return mYCoordinates.getPrimitiveArray();
118    }
119
120    public int[] getPointerIds() {
121        return mPointerIds.getPrimitiveArray();
122    }
123
124    public int[] getTimes() {
125        return mTimes.getPrimitiveArray();
126    }
127
128    @Override
129    public String toString() {
130        return "size=" + getPointerSize() + " id=" + mPointerIds + " time=" + mTimes
131                + " x=" + mXCoordinates + " y=" + mYCoordinates;
132    }
133}
134