2012-06-29 09:42:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
2012-07-13 02:17:25 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
// TODO: This class is not thread-safe.
|
2012-06-29 09:42:15 +00:00
|
|
|
public class InputPointers {
|
|
|
|
private final ScalableIntArray mXCoordinates = new ScalableIntArray();
|
|
|
|
private final ScalableIntArray mYCoordinates = new ScalableIntArray();
|
|
|
|
private final ScalableIntArray mPointerIds = new ScalableIntArray();
|
|
|
|
private final ScalableIntArray mTimes = new ScalableIntArray();
|
|
|
|
|
|
|
|
public void addPointer(int index, int x, int y, int pointerId, int time) {
|
|
|
|
mXCoordinates.add(index, x);
|
|
|
|
mYCoordinates.add(index, y);
|
|
|
|
mPointerIds.add(index, pointerId);
|
|
|
|
mTimes.add(index, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addPointer(int x, int y, int pointerId, int time) {
|
|
|
|
mXCoordinates.add(x);
|
|
|
|
mYCoordinates.add(y);
|
|
|
|
mPointerIds.add(pointerId);
|
|
|
|
mTimes.add(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void set(InputPointers ip) {
|
|
|
|
mXCoordinates.set(ip.mXCoordinates);
|
|
|
|
mYCoordinates.set(ip.mYCoordinates);
|
|
|
|
mPointerIds.set(ip.mPointerIds);
|
|
|
|
mTimes.set(ip.mTimes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copy(InputPointers ip) {
|
|
|
|
mXCoordinates.copy(ip.mXCoordinates);
|
|
|
|
mYCoordinates.copy(ip.mYCoordinates);
|
|
|
|
mPointerIds.copy(ip.mPointerIds);
|
|
|
|
mTimes.copy(ip.mTimes);
|
|
|
|
}
|
|
|
|
|
2012-07-09 07:40:13 +00:00
|
|
|
/**
|
|
|
|
* Append the pointers in the specified {@link InputPointers} to the end of this.
|
2012-07-13 02:17:25 +00:00
|
|
|
* @param src the source {@link InputPointers} to read the data from.
|
2012-07-09 07:40:13 +00:00
|
|
|
* @param startPos the starting index of the pointers in {@code src}.
|
|
|
|
* @param length the number of pointers to be appended.
|
|
|
|
*/
|
|
|
|
public void append(InputPointers src, int startPos, int length) {
|
2012-07-18 08:56:30 +00:00
|
|
|
if (length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-13 02:17:25 +00:00
|
|
|
mXCoordinates.append(src.mXCoordinates, startPos, length);
|
|
|
|
mYCoordinates.append(src.mYCoordinates, startPos, length);
|
|
|
|
mPointerIds.append(src.mPointerIds, startPos, length);
|
|
|
|
mTimes.append(src.mTimes, startPos, length);
|
2012-07-09 07:40:13 +00:00
|
|
|
}
|
|
|
|
|
2012-06-29 09:42:15 +00:00
|
|
|
public void reset() {
|
|
|
|
mXCoordinates.reset();
|
|
|
|
mYCoordinates.reset();
|
|
|
|
mPointerIds.reset();
|
|
|
|
mTimes.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPointerSize() {
|
|
|
|
return mXCoordinates.getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int[] getXCoordinates() {
|
2012-07-09 07:40:13 +00:00
|
|
|
return mXCoordinates.getPrimitiveArray();
|
2012-06-29 09:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int[] getYCoordinates() {
|
2012-07-09 07:40:13 +00:00
|
|
|
return mYCoordinates.getPrimitiveArray();
|
2012-06-29 09:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int[] getPointerIds() {
|
2012-07-09 07:40:13 +00:00
|
|
|
return mPointerIds.getPrimitiveArray();
|
2012-06-29 09:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int[] getTimes() {
|
2012-07-09 07:40:13 +00:00
|
|
|
return mTimes.getPrimitiveArray();
|
2012-06-29 09:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class ScalableIntArray {
|
|
|
|
private static final int DEFAULT_SIZE = BinaryDictionary.MAX_WORD_LENGTH;
|
|
|
|
private int[] mArray;
|
|
|
|
private int mLength;
|
|
|
|
|
|
|
|
public ScalableIntArray() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add(int index, int val) {
|
|
|
|
if (mLength < index + 1) {
|
|
|
|
mLength = index;
|
|
|
|
add(val);
|
|
|
|
} else {
|
|
|
|
mArray[index] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add(int val) {
|
2012-07-10 01:47:44 +00:00
|
|
|
final int nextLength = mLength + 1;
|
|
|
|
ensureCapacity(nextLength);
|
2012-06-29 09:42:15 +00:00
|
|
|
mArray[mLength] = val;
|
2012-07-10 01:47:44 +00:00
|
|
|
mLength = nextLength;
|
2012-06-29 09:42:15 +00:00
|
|
|
}
|
|
|
|
|
2012-07-13 02:17:25 +00:00
|
|
|
private void ensureCapacity(int minimumCapacity) {
|
2012-07-09 07:40:13 +00:00
|
|
|
if (mArray.length < minimumCapacity) {
|
|
|
|
final int nextCapacity = mArray.length * 2;
|
2012-07-13 02:17:25 +00:00
|
|
|
// The following is the same as newLength = Math.max(minimumCapacity, nextCapacity);
|
|
|
|
final int newLength = minimumCapacity > nextCapacity
|
|
|
|
? minimumCapacity
|
|
|
|
: nextCapacity;
|
|
|
|
mArray = Arrays.copyOf(mArray, newLength);
|
2012-07-09 07:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 09:42:15 +00:00
|
|
|
public int getLength() {
|
|
|
|
return mLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset() {
|
|
|
|
mArray = new int[DEFAULT_SIZE];
|
|
|
|
mLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int[] getPrimitiveArray() {
|
|
|
|
return mArray;
|
|
|
|
}
|
|
|
|
|
2012-07-13 02:17:25 +00:00
|
|
|
public void set(ScalableIntArray ip) {
|
|
|
|
mArray = ip.mArray;
|
|
|
|
mLength = ip.mLength;
|
|
|
|
}
|
|
|
|
|
2012-06-29 09:42:15 +00:00
|
|
|
public void copy(ScalableIntArray ip) {
|
2012-07-10 01:47:44 +00:00
|
|
|
ensureCapacity(ip.mLength);
|
|
|
|
System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength);
|
2012-07-09 07:40:13 +00:00
|
|
|
mLength = ip.mLength;
|
2012-06-29 09:42:15 +00:00
|
|
|
}
|
|
|
|
|
2012-07-13 02:17:25 +00:00
|
|
|
public void append(ScalableIntArray src, int startPos, int length) {
|
|
|
|
final int currentLength = mLength;
|
|
|
|
final int newLength = currentLength + length;
|
|
|
|
ensureCapacity(newLength);
|
|
|
|
System.arraycopy(src.mArray, startPos, mArray, currentLength, length);
|
|
|
|
mLength = newLength;
|
2012-06-29 09:42:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|