Phil Weaver | 89e3ffc | 2016-09-19 13:51:10 -0700 | [diff] [blame] | 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 | |
Phil Weaver | ff70683 | 2017-09-08 17:09:23 -0700 | [diff] [blame] | 19 | import android.app.Notification; |
Phil Weaver | 89e3ffc | 2016-09-19 13:51:10 -0700 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.os.Message; |
| 22 | import android.util.Pair; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * Utility class to capture messages dispatched through a handler and control when they arrive |
| 29 | * at their target. |
| 30 | */ |
| 31 | public class MessageCapturingHandler extends Handler { |
| 32 | List<Pair<Message, Long>> timedMessages = new ArrayList<>(); |
| 33 | |
| 34 | Handler.Callback mCallback; |
| 35 | |
| 36 | public MessageCapturingHandler(Handler.Callback callback) { |
| 37 | mCallback = callback; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public boolean sendMessageAtTime(Message message, long uptimeMillis) { |
| 42 | timedMessages.add(new Pair<>(Message.obtain(message), uptimeMillis)); |
| 43 | return super.sendMessageAtTime(message, uptimeMillis); |
| 44 | } |
| 45 | |
Phil Weaver | 9213d23 | 2017-03-28 16:32:43 -0700 | [diff] [blame] | 46 | public void setCallback(Handler.Callback callback) { |
| 47 | mCallback = callback; |
| 48 | } |
| 49 | |
Phil Weaver | 89e3ffc | 2016-09-19 13:51:10 -0700 | [diff] [blame] | 50 | public void sendOneMessage() { |
| 51 | Message message = timedMessages.remove(0).first; |
| 52 | removeMessages(message.what, message.obj); |
Phil Weaver | ff70683 | 2017-09-08 17:09:23 -0700 | [diff] [blame] | 53 | dispatchMessage(message); |
Phil Weaver | 89e3ffc | 2016-09-19 13:51:10 -0700 | [diff] [blame] | 54 | removeStaleMessages(); |
| 55 | } |
| 56 | |
| 57 | public void sendAllMessages() { |
| 58 | while (!timedMessages.isEmpty()) { |
| 59 | sendOneMessage(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public void sendLastMessage() { |
| 64 | Message message = timedMessages.remove(timedMessages.size() - 1).first; |
| 65 | removeMessages(message.what, message.obj); |
Phil Weaver | ff70683 | 2017-09-08 17:09:23 -0700 | [diff] [blame] | 66 | dispatchMessage(message); |
Phil Weaver | 89e3ffc | 2016-09-19 13:51:10 -0700 | [diff] [blame] | 67 | removeStaleMessages(); |
| 68 | } |
| 69 | |
| 70 | public boolean hasMessages() { |
| 71 | removeStaleMessages(); |
| 72 | return !timedMessages.isEmpty(); |
| 73 | } |
| 74 | |
| 75 | private void removeStaleMessages() { |
| 76 | for (int i = 0; i < timedMessages.size(); i++) { |
| 77 | Message message = timedMessages.get(i).first; |
| 78 | if (!hasMessages(message.what, message.obj)) { |
| 79 | timedMessages.remove(i--); |
| 80 | } |
| 81 | } |
| 82 | } |
Phil Weaver | ff70683 | 2017-09-08 17:09:23 -0700 | [diff] [blame] | 83 | |
| 84 | public void dispatchMessage(Message m) { |
| 85 | if (mCallback != null) { |
| 86 | mCallback.handleMessage(m); |
| 87 | return; |
| 88 | } |
| 89 | super.dispatchMessage(m); |
| 90 | } |
Phil Weaver | 89e3ffc | 2016-09-19 13:51:10 -0700 | [diff] [blame] | 91 | } |