| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.os.Handler; |
| 20 |
|
| 21 |
import com.android.internal.util.GrowingArrayUtils; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class HandlerActionQueue { |
| 29 |
private HandlerAction[] mActions; |
| 30 |
private int mCount; |
| 31 |
|
| 32 |
public void post(Runnable action) { |
| 33 |
postDelayed(action, 0); |
| 34 |
} |
| 35 |
|
| 36 |
public void postDelayed(Runnable action, long delayMillis) { |
| 37 |
final HandlerAction handlerAction = new HandlerAction(action, delayMillis); |
| 38 |
|
| 39 |
synchronized (this) { |
| 40 |
if (mActions == null) { |
| 41 |
mActions = new HandlerAction[4]; |
| 42 |
} |
| 43 |
mActions = GrowingArrayUtils.append(mActions, mCount, handlerAction); |
| 44 |
mCount++; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public void removeCallbacks(Runnable action) { |
| 49 |
synchronized (this) { |
| 50 |
final int count = mCount; |
| 51 |
int j = 0; |
| 52 |
|
| 53 |
final HandlerAction[] actions = mActions; |
| 54 |
for (int i = 0; i < count; i++) { |
| 55 |
if (actions[i].matches(action)) { |
| 56 |
|
| 57 |
|
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
if (j != i) { |
| 62 |
|
| 63 |
|
| 64 |
actions[j] = actions[i]; |
| 65 |
} |
| 66 |
|
| 67 |
j++; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
mCount = j; |
| 72 |
|
| 73 |
|
| 74 |
for (; j < count; j++) { |
| 75 |
actions[j] = null; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public void executeActions(Handler handler) { |
| 81 |
synchronized (this) { |
| 82 |
final HandlerAction[] actions = mActions; |
| 83 |
for (int i = 0, count = mCount; i < count; i++) { |
| 84 |
final HandlerAction handlerAction = actions[i]; |
| 85 |
handler.postDelayed(handlerAction.action, handlerAction.delay); |
| 86 |
} |
| 87 |
|
| 88 |
mActions = null; |
| 89 |
mCount = 0; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public int size() { |
| 94 |
return mCount; |
| 95 |
} |
| 96 |
|
| 97 |
public Runnable getRunnable(int index) { |
| 98 |
if (index >= mCount) { |
| 99 |
throw new IndexOutOfBoundsException(); |
| 100 |
} |
| 101 |
return mActions[index].action; |
| 102 |
} |
| 103 |
|
| 104 |
public long getDelay(int index) { |
| 105 |
if (index >= mCount) { |
| 106 |
throw new IndexOutOfBoundsException(); |
| 107 |
} |
| 108 |
return mActions[index].delay; |
| 109 |
} |
| 110 |
|
| 111 |
private static class HandlerAction { |
| 112 |
final Runnable action; |
| 113 |
final long delay; |
| 114 |
|
| 115 |
public HandlerAction(Runnable action, long delay) { |
| 116 |
this.action = action; |
| 117 |
this.delay = delay; |
| 118 |
} |
| 119 |
|
| 120 |
public boolean matches(Runnable otherAction) { |
| 121 |
return otherAction == null && action == null |
| 122 |
|| action != null && action.equals(otherAction); |
| 123 |
} |
| 124 |
} |
| 125 |
} |