2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher3;
19 import android.os.Handler;
20 import android.os.Looper;
21 import android.os.Message;
22 import android.os.MessageQueue;
24 import com.android.launcher3.util.Thunk;
26 import java.util.LinkedList;
29 * Queue of things to run on a looper thread. Items posted with {@link #post} will not
30 * be actually enqued on the handler until after the last one has run, to keep from
31 * starving the thread.
35 public class DeferredHandler {
36 @Thunk LinkedList<Runnable> mQueue = new LinkedList<>();
37 private MessageQueue mMessageQueue = Looper.myQueue();
38 private Impl mHandler = new Impl();
40 @Thunk class Impl extends Handler implements MessageQueue.IdleHandler {
41 public void handleMessage(Message msg) {
43 synchronized (mQueue) {
44 if (mQueue.size() == 0) {
47 r = mQueue.removeFirst();
50 synchronized (mQueue) {
55 public boolean queueIdle() {
61 private class IdleRunnable implements Runnable {
64 IdleRunnable(Runnable r) {
73 public DeferredHandler() {
76 /** Schedule runnable to run after everything that's on the queue right now. */
77 public void post(Runnable runnable) {
78 synchronized (mQueue) {
80 if (mQueue.size() == 1) {
86 /** Schedule runnable to run when the queue goes idle. */
87 public void postIdle(final Runnable runnable) {
88 post(new IdleRunnable(runnable));
91 public void cancelAll() {
92 synchronized (mQueue) {
97 /** Runs all queued Runnables from the calling thread. */
99 LinkedList<Runnable> queue = new LinkedList<>();
100 synchronized (mQueue) {
101 queue.addAll(mQueue);
104 for (Runnable r : queue) {
109 void scheduleNextLocked() {
110 if (mQueue.size() > 0) {
111 Runnable peek = mQueue.getFirst();
112 if (peek instanceof IdleRunnable) {
113 mMessageQueue.addIdleHandler(mHandler);
115 mHandler.sendEmptyMessage(1);