2010-12-02 09:46:21 +00:00
|
|
|
/*
|
2011-05-20 03:09:57 +00:00
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-06-22 02:53:02 +00:00
|
|
|
package com.android.inputmethod.keyboard.internal;
|
|
|
|
|
2012-02-16 19:45:35 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
import java.util.ArrayList;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
|
|
|
public class PointerTrackerQueue {
|
2012-02-16 19:45:35 +00:00
|
|
|
private static final String TAG = PointerTrackerQueue.class.getSimpleName();
|
|
|
|
private static final boolean DEBUG = false;
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
public interface Element {
|
2012-08-07 05:45:06 +00:00
|
|
|
public boolean isModifier();
|
|
|
|
public boolean isInSlidingKeyInput();
|
|
|
|
public void onPhantomUpEvent(long eventTime);
|
|
|
|
}
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
private static final int INITIAL_CAPACITY = 10;
|
|
|
|
private final ArrayList<Element> mExpandableArrayOfActivePointers =
|
|
|
|
new ArrayList<Element>(INITIAL_CAPACITY);
|
|
|
|
private int mArraySize = 0;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
public synchronized int size() {
|
|
|
|
return mArraySize;
|
2012-06-12 10:40:37 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
public synchronized void add(final Element pointer) {
|
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
if (arraySize < expandableArray.size()) {
|
|
|
|
expandableArray.set(arraySize, pointer);
|
|
|
|
} else {
|
|
|
|
expandableArray.add(pointer);
|
|
|
|
}
|
|
|
|
mArraySize = arraySize + 1;
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
public synchronized void remove(final Element pointer) {
|
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
int newSize = 0;
|
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (element == pointer) {
|
|
|
|
if (newSize != index) {
|
|
|
|
Log.w(TAG, "Found duplicated element in remove: " + pointer);
|
|
|
|
}
|
|
|
|
continue; // Remove this element from the expandableArray.
|
|
|
|
}
|
|
|
|
if (newSize != index) {
|
|
|
|
// Shift this element toward the beginning of the expandableArray.
|
|
|
|
expandableArray.set(newSize, element);
|
|
|
|
}
|
|
|
|
newSize++;
|
|
|
|
}
|
|
|
|
mArraySize = newSize;
|
2011-12-01 08:50:37 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
public synchronized void releaseAllPointersOlderThan(final Element pointer,
|
|
|
|
final long eventTime) {
|
2012-02-16 19:45:35 +00:00
|
|
|
if (DEBUG) {
|
2012-08-07 05:30:28 +00:00
|
|
|
Log.d(TAG, "releaseAllPoniterOlderThan: " + pointer + " " + this);
|
2012-02-16 19:45:35 +00:00
|
|
|
}
|
2012-08-07 05:30:28 +00:00
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
int newSize, index;
|
|
|
|
for (newSize = index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (element == pointer) {
|
|
|
|
break; // Stop releasing elements.
|
|
|
|
}
|
|
|
|
if (!element.isModifier()) {
|
|
|
|
element.onPhantomUpEvent(eventTime);
|
|
|
|
continue; // Remove this element from the expandableArray.
|
|
|
|
}
|
|
|
|
if (newSize != index) {
|
|
|
|
// Shift this element toward the beginning of the expandableArray.
|
|
|
|
expandableArray.set(newSize, element);
|
|
|
|
}
|
|
|
|
newSize++;
|
2010-12-21 05:01:13 +00:00
|
|
|
}
|
2012-08-07 05:30:28 +00:00
|
|
|
// Shift rest of the expandableArray.
|
|
|
|
int count = 0;
|
|
|
|
for (; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (element == pointer) {
|
|
|
|
if (count > 0) {
|
|
|
|
Log.w(TAG, "Found duplicated element in releaseAllPointersOlderThan: "
|
|
|
|
+ pointer);
|
|
|
|
}
|
|
|
|
count++;
|
2011-12-01 08:50:37 +00:00
|
|
|
}
|
2012-08-07 05:30:28 +00:00
|
|
|
if (newSize != index) {
|
|
|
|
expandableArray.set(newSize, expandableArray.get(index));
|
|
|
|
newSize++;
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 05:30:28 +00:00
|
|
|
mArraySize = newSize;
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
public void releaseAllPointers(final long eventTime) {
|
2011-07-23 08:27:49 +00:00
|
|
|
releaseAllPointersExcept(null, eventTime);
|
2010-12-21 05:01:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
public synchronized void releaseAllPointersExcept(final Element pointer,
|
|
|
|
final long eventTime) {
|
2012-02-16 19:45:35 +00:00
|
|
|
if (DEBUG) {
|
2012-08-07 05:30:28 +00:00
|
|
|
if (pointer == null) {
|
2012-02-16 19:45:35 +00:00
|
|
|
Log.d(TAG, "releaseAllPoniters: " + this);
|
|
|
|
} else {
|
2012-08-07 05:30:28 +00:00
|
|
|
Log.d(TAG, "releaseAllPoniterExcept: " + pointer + " " + this);
|
2012-02-16 19:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 05:30:28 +00:00
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
int newSize = 0, count = 0;
|
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (element == pointer) {
|
|
|
|
if (count > 0) {
|
|
|
|
Log.w(TAG, "Found duplicated element in releaseAllPointersExcept: " + pointer);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
element.onPhantomUpEvent(eventTime);
|
|
|
|
continue; // Remove this element from the expandableArray.
|
|
|
|
}
|
|
|
|
if (newSize != index) {
|
|
|
|
// Shift this element toward the beginning of the expandableArray.
|
|
|
|
expandableArray.set(newSize, element);
|
2011-11-16 23:53:03 +00:00
|
|
|
}
|
2012-08-07 05:30:28 +00:00
|
|
|
newSize++;
|
2011-11-16 23:53:03 +00:00
|
|
|
}
|
2012-08-07 05:30:28 +00:00
|
|
|
mArraySize = newSize;
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
public synchronized boolean hasModifierKeyOlderThan(final Element pointer) {
|
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (element == pointer) {
|
|
|
|
return false; // Stop searching modifier key.
|
2012-06-27 00:47:59 +00:00
|
|
|
}
|
2012-08-07 05:30:28 +00:00
|
|
|
if (element.isModifier()) {
|
2012-06-27 00:47:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-16 23:53:03 +00:00
|
|
|
public synchronized boolean isAnyInSlidingKeyInput() {
|
2012-08-07 05:30:28 +00:00
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (element.isInSlidingKeyInput()) {
|
2010-12-20 07:21:54 +00:00
|
|
|
return true;
|
2011-11-16 23:53:03 +00:00
|
|
|
}
|
2010-12-20 07:21:54 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-02 13:59:10 +00:00
|
|
|
@Override
|
2012-08-07 05:30:28 +00:00
|
|
|
public synchronized String toString() {
|
2011-12-01 08:50:37 +00:00
|
|
|
final StringBuilder sb = new StringBuilder();
|
2012-08-07 05:30:28 +00:00
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
2011-12-01 08:50:37 +00:00
|
|
|
if (sb.length() > 0)
|
2010-12-02 13:59:10 +00:00
|
|
|
sb.append(" ");
|
2012-08-07 05:30:28 +00:00
|
|
|
sb.append(element.toString());
|
2010-12-02 13:59:10 +00:00
|
|
|
}
|
2012-08-07 05:45:06 +00:00
|
|
|
return "[" + sb.toString() + "]";
|
2010-12-02 13:59:10 +00:00
|
|
|
}
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|