1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.view; 18 19 import com.android.internal.util.GrowingArrayUtils; 20 21 import android.os.Handler; 22 23 import java.util.ArrayList; 24 25 /** 26 * Class used to enqueue pending work from Views when no Handler is attached. 27 * 28 * @hide Exposed for test framework only. 29 */ 30 public class HandlerActionQueue { 31 private HandlerAction[] mActions; 32 private int mCount; 33 post(Runnable action)34 public void post(Runnable action) { 35 postDelayed(action, 0); 36 } 37 postDelayed(Runnable action, long delayMillis)38 public void postDelayed(Runnable action, long delayMillis) { 39 final HandlerAction handlerAction = new HandlerAction(action, delayMillis); 40 41 synchronized (this) { 42 if (mActions == null) { 43 mActions = new HandlerAction[4]; 44 } 45 mActions = GrowingArrayUtils.append(mActions, mCount, handlerAction); 46 mCount++; 47 } 48 } 49 removeCallbacks(Runnable action)50 public void removeCallbacks(Runnable action) { 51 synchronized (this) { 52 final int count = mCount; 53 int j = 0; 54 55 final HandlerAction[] actions = mActions; 56 for (int i = 0; i < count; i++) { 57 if (actions[i].matches(action)) { 58 // Remove this action by overwriting it within 59 // this loop or nulling it out later. 60 continue; 61 } 62 63 if (j != i) { 64 // At least one previous entry was removed, so 65 // this one needs to move to the "new" list. 66 actions[j] = actions[i]; 67 } 68 69 j++; 70 } 71 72 // The "new" list only has j entries. 73 mCount = j; 74 75 // Null out any remaining entries. 76 for (; j < count; j++) { 77 actions[j] = null; 78 } 79 } 80 } 81 executeActions(Handler handler)82 public void executeActions(Handler handler) { 83 synchronized (this) { 84 final HandlerAction[] actions = mActions; 85 for (int i = 0, count = mCount; i < count; i++) { 86 final HandlerAction handlerAction = actions[i]; 87 handler.postDelayed(handlerAction.action, handlerAction.delay); 88 } 89 90 mActions = null; 91 mCount = 0; 92 } 93 } 94 size()95 public int size() { 96 return mCount; 97 } 98 getRunnable(int index)99 public Runnable getRunnable(int index) { 100 if (index >= mCount) { 101 throw new IndexOutOfBoundsException(); 102 } 103 return mActions[index].action; 104 } 105 getDelay(int index)106 public long getDelay(int index) { 107 if (index >= mCount) { 108 throw new IndexOutOfBoundsException(); 109 } 110 return mActions[index].delay; 111 } 112 113 private static class HandlerAction { 114 final Runnable action; 115 final long delay; 116 HandlerAction(Runnable action, long delay)117 public HandlerAction(Runnable action, long delay) { 118 this.action = action; 119 this.delay = delay; 120 } 121 matches(Runnable otherAction)122 public boolean matches(Runnable otherAction) { 123 return otherAction == null && action == null 124 || action != null && action.equals(otherAction); 125 } 126 } 127 } 128