1 /* 2 * Copyright (C) 2016 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 com.android.server.accessibility; 18 19 import android.os.Handler; 20 import android.os.Looper; 21 import android.os.Message; 22 import android.util.Pair; 23 24 import androidx.test.InstrumentationRegistry; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 /** 30 * Utility class to capture messages dispatched through a handler and control when they arrive 31 * at their target. 32 */ 33 public class MessageCapturingHandler extends Handler { 34 List<Pair<Message, Long>> timedMessages = new ArrayList<>(); 35 36 Handler.Callback mCallback; 37 MessageCapturingHandler(Handler.Callback callback)38 public MessageCapturingHandler(Handler.Callback callback) { 39 this(InstrumentationRegistry.getContext().getMainLooper(), callback); 40 } 41 MessageCapturingHandler(Looper looper, Callback callback)42 public MessageCapturingHandler(Looper looper, Callback callback) { 43 super(looper); 44 mCallback = callback; 45 } 46 47 /** 48 * Holding messages in queue, but never dispatching. 49 * @see #removeAllMessages() 50 */ 51 @Override sendMessageAtTime(Message message, long uptimeMillis)52 public boolean sendMessageAtTime(Message message, long uptimeMillis) { 53 timedMessages.add(new Pair<>(Message.obtain(message), uptimeMillis)); 54 return super.sendMessageAtTime(message, Long.MAX_VALUE); 55 } 56 setCallback(Handler.Callback callback)57 public void setCallback(Handler.Callback callback) { 58 mCallback = callback; 59 } 60 sendOneMessage()61 public void sendOneMessage() { 62 Message message = timedMessages.remove(0).first; 63 removeMessages(message.what, message.obj); 64 dispatchMessage(message); 65 removeStaleMessages(); 66 } 67 sendAllMessages()68 public void sendAllMessages() { 69 while (!timedMessages.isEmpty()) { 70 sendOneMessage(); 71 } 72 } 73 sendLastMessage()74 public void sendLastMessage() { 75 Message message = timedMessages.remove(timedMessages.size() - 1).first; 76 removeMessages(message.what, message.obj); 77 dispatchMessage(message); 78 removeStaleMessages(); 79 } 80 81 /** 82 * Clear messages sent from this handler in queue. 83 * <p> 84 * If main looper is used, this method should be called in tear down function 85 * to ensure messages isolation between test cases. 86 * </p> 87 */ removeAllMessages()88 public void removeAllMessages() { 89 if (hasMessages()) { 90 for (int i = 0; i < timedMessages.size(); i++) { 91 Message message = timedMessages.get(i).first; 92 removeMessages(message.what, message.obj); 93 } 94 } 95 } 96 hasMessages()97 public boolean hasMessages() { 98 removeStaleMessages(); 99 return !timedMessages.isEmpty(); 100 } 101 removeStaleMessages()102 private void removeStaleMessages() { 103 for (int i = 0; i < timedMessages.size(); i++) { 104 Message message = timedMessages.get(i).first; 105 if (!hasMessages(message.what, message.obj)) { 106 timedMessages.remove(i--); 107 } 108 } 109 } 110 dispatchMessage(Message m)111 public void dispatchMessage(Message m) { 112 if (mCallback != null) { 113 mCallback.handleMessage(m); 114 return; 115 } 116 super.dispatchMessage(m); 117 } 118 } 119