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
|
|
|
*
|
2013-01-21 12:52:57 +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
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2010-12-02 09:46:21 +00:00
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
2012-08-21 07:34:55 +00:00
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
import java.util.ArrayList;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
public final 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();
|
2013-12-13 08:09:16 +00:00
|
|
|
public boolean isInDraggingFinger();
|
2012-08-07 05:45:06 +00:00
|
|
|
public void onPhantomUpEvent(long eventTime);
|
2013-04-22 08:57:05 +00:00
|
|
|
public void cancelTrackingForAction();
|
2012-08-07 05:45:06 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 05:30:28 +00:00
|
|
|
private static final int INITIAL_CAPACITY = 10;
|
2013-04-10 09:34:01 +00:00
|
|
|
// Note: {@link #mExpandableArrayOfActivePointers} and {@link #mArraySize} are synchronized by
|
|
|
|
// {@link #mExpandableArrayOfActivePointers}
|
2012-08-07 05:30:28 +00:00
|
|
|
private final ArrayList<Element> mExpandableArrayOfActivePointers =
|
2012-08-21 07:34:55 +00:00
|
|
|
CollectionUtils.newArrayList(INITIAL_CAPACITY);
|
2012-08-07 05:30:28 +00:00
|
|
|
private int mArraySize = 0;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2013-04-10 09:34:01 +00:00
|
|
|
public int size() {
|
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
|
|
|
return mArraySize;
|
|
|
|
}
|
2012-06-12 10:40:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 09:34:01 +00:00
|
|
|
public void add(final Element pointer) {
|
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
2013-05-14 03:25:21 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "add: " + pointer + " " + this);
|
|
|
|
}
|
2013-04-10 09:34:01 +00:00
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
if (arraySize < expandableArray.size()) {
|
|
|
|
expandableArray.set(arraySize, pointer);
|
|
|
|
} else {
|
|
|
|
expandableArray.add(pointer);
|
|
|
|
}
|
|
|
|
mArraySize = arraySize + 1;
|
2012-08-07 05:30:28 +00:00
|
|
|
}
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 09:34:01 +00:00
|
|
|
public void remove(final Element pointer) {
|
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
2013-05-14 03:25:21 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "remove: " + pointer + " " + this);
|
|
|
|
}
|
2013-04-10 09:34:01 +00:00
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
2013-05-14 03:25:21 +00:00
|
|
|
int newIndex = 0;
|
2013-04-10 09:34:01 +00:00
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (element == pointer) {
|
2013-05-14 03:25:21 +00:00
|
|
|
if (newIndex != index) {
|
2013-04-10 09:34:01 +00:00
|
|
|
Log.w(TAG, "Found duplicated element in remove: " + pointer);
|
|
|
|
}
|
|
|
|
continue; // Remove this element from the expandableArray.
|
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
if (newIndex != index) {
|
2013-04-10 09:34:01 +00:00
|
|
|
// Shift this element toward the beginning of the expandableArray.
|
2013-05-14 03:25:21 +00:00
|
|
|
expandableArray.set(newIndex, element);
|
2012-08-07 05:30:28 +00:00
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
newIndex++;
|
2012-08-07 05:30:28 +00:00
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
mArraySize = newIndex;
|
2012-08-07 05:30:28 +00:00
|
|
|
}
|
2011-12-01 08:50:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 09:34:01 +00:00
|
|
|
public Element getOldestElement() {
|
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
|
|
|
return (mArraySize == 0) ? null : mExpandableArrayOfActivePointers.get(0);
|
|
|
|
}
|
2012-09-13 03:38:08 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 09:34:01 +00:00
|
|
|
public void releaseAllPointersOlderThan(final Element pointer, final long eventTime) {
|
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "releaseAllPoniterOlderThan: " + pointer + " " + this);
|
2012-08-07 05:30:28 +00:00
|
|
|
}
|
2013-04-10 09:34:01 +00:00
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
2013-05-14 03:25:21 +00:00
|
|
|
int newIndex, index;
|
|
|
|
for (newIndex = index = 0; index < arraySize; index++) {
|
2013-04-10 09:34:01 +00:00
|
|
|
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.
|
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
if (newIndex != index) {
|
2013-04-10 09:34:01 +00:00
|
|
|
// Shift this element toward the beginning of the expandableArray.
|
2013-05-14 03:25:21 +00:00
|
|
|
expandableArray.set(newIndex, element);
|
2012-08-07 05:30:28 +00:00
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
newIndex++;
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
2013-04-10 09:34:01 +00:00
|
|
|
// Shift rest of the expandableArray.
|
|
|
|
int count = 0;
|
|
|
|
for (; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (element == pointer) {
|
2013-05-14 03:25:21 +00:00
|
|
|
count++;
|
|
|
|
if (count > 1) {
|
2013-04-10 09:34:01 +00:00
|
|
|
Log.w(TAG, "Found duplicated element in releaseAllPointersOlderThan: "
|
|
|
|
+ pointer);
|
|
|
|
}
|
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
if (newIndex != index) {
|
|
|
|
// Shift this element toward the beginning of the expandableArray.
|
|
|
|
expandableArray.set(newIndex, expandableArray.get(index));
|
2013-04-10 09:34:01 +00:00
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
newIndex++;
|
2013-04-10 09:34:01 +00:00
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
mArraySize = newIndex;
|
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
|
|
|
}
|
|
|
|
|
2013-04-10 09:34:01 +00:00
|
|
|
public void releaseAllPointersExcept(final Element pointer, final long eventTime) {
|
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
|
|
|
if (DEBUG) {
|
|
|
|
if (pointer == null) {
|
|
|
|
Log.d(TAG, "releaseAllPoniters: " + this);
|
|
|
|
} else {
|
|
|
|
Log.d(TAG, "releaseAllPoniterExcept: " + pointer + " " + this);
|
2012-08-07 05:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-10 09:34:01 +00:00
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
2013-05-14 03:25:21 +00:00
|
|
|
int newIndex = 0, count = 0;
|
2013-04-10 09:34:01 +00:00
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (element == pointer) {
|
2013-05-14 03:25:21 +00:00
|
|
|
count++;
|
|
|
|
if (count > 1) {
|
2013-04-10 09:34:01 +00:00
|
|
|
Log.w(TAG, "Found duplicated element in releaseAllPointersExcept: "
|
|
|
|
+ pointer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
element.onPhantomUpEvent(eventTime);
|
|
|
|
continue; // Remove this element from the expandableArray.
|
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
if (newIndex != index) {
|
2013-04-10 09:34:01 +00:00
|
|
|
// Shift this element toward the beginning of the expandableArray.
|
2013-05-14 03:25:21 +00:00
|
|
|
expandableArray.set(newIndex, element);
|
2013-04-10 09:34:01 +00:00
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
newIndex++;
|
2011-11-16 23:53:03 +00:00
|
|
|
}
|
2013-05-14 03:25:21 +00:00
|
|
|
mArraySize = newIndex;
|
2011-11-16 23:53:03 +00:00
|
|
|
}
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 09:34:01 +00:00
|
|
|
public boolean hasModifierKeyOlderThan(final Element pointer) {
|
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
if (element.isModifier()) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-06-27 00:47:59 +00:00
|
|
|
}
|
2013-04-10 09:34:01 +00:00
|
|
|
return false;
|
2012-06-27 00:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
public boolean isAnyInDraggingFinger() {
|
2013-04-10 09:34:01 +00:00
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
2013-12-13 08:09:16 +00:00
|
|
|
if (element.isInDraggingFinger()) {
|
2013-04-10 09:34:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-16 23:53:03 +00:00
|
|
|
}
|
2013-04-10 09:34:01 +00:00
|
|
|
return false;
|
2010-12-20 07:21:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 10:00:45 +00:00
|
|
|
public void cancelAllPointerTrackers() {
|
2013-04-10 09:34:01 +00:00
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
2013-05-14 03:25:21 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "cancelAllPointerTracker: " + this);
|
|
|
|
}
|
2013-04-10 09:34:01 +00:00
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
2013-04-22 08:57:05 +00:00
|
|
|
element.cancelTrackingForAction();
|
2013-04-10 09:34:01 +00:00
|
|
|
}
|
2012-11-22 06:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 13:59:10 +00:00
|
|
|
@Override
|
2013-04-10 09:34:01 +00:00
|
|
|
public String toString() {
|
|
|
|
synchronized (mExpandableArrayOfActivePointers) {
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
|
|
|
|
final int arraySize = mArraySize;
|
|
|
|
for (int index = 0; index < arraySize; index++) {
|
|
|
|
final Element element = expandableArray.get(index);
|
|
|
|
if (sb.length() > 0) {
|
|
|
|
sb.append(" ");
|
|
|
|
}
|
|
|
|
sb.append(element.toString());
|
|
|
|
}
|
|
|
|
return "[" + sb.toString() + "]";
|
2010-12-02 13:59:10 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|