blob: ce60352c309ded6c7f25fdc9cbe39cedbb4cf50e [file] [log] [blame]
Joe Onorato9c1289c2009-08-17 11:03:03 -04001/*
2 * Copyright (C) 2008 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
17package com.android.launcher2;
18
19import android.os.Handler;
20import android.os.Looper;
21import android.os.Message;
22import android.os.MessageQueue;
23import android.util.Log;
24
25import java.util.LinkedList;
26
27/**
28 * Queue of things to run on a looper thread. Items posted with {@link #post} will not
29 * be actually enqued on the handler until after the last one has run, to keep from
30 * starving the thread.
31 *
32 * This class is fifo.
33 */
34public class DeferredHandler {
35 private LinkedList<Runnable> mQueue = new LinkedList();
36 private MessageQueue mMessageQueue = Looper.myQueue();
37 private Impl mHandler = new Impl();
38
39 private class Impl extends Handler implements MessageQueue.IdleHandler {
40 public void handleMessage(Message msg) {
41 Runnable r;
42 synchronized (mQueue) {
Joe Onorato33ed7b22009-09-23 18:20:54 -070043 if (mQueue.size() == 0) {
44 return;
45 }
Joe Onorato9c1289c2009-08-17 11:03:03 -040046 r = mQueue.removeFirst();
47 }
48 r.run();
49 synchronized (mQueue) {
50 scheduleNextLocked();
51 }
52 }
53
54 public boolean queueIdle() {
55 handleMessage(null);
56 return false;
57 }
58 }
59
60 private class IdleRunnable implements Runnable {
61 Runnable mRunnable;
62
63 IdleRunnable(Runnable r) {
64 mRunnable = r;
65 }
66
67 public void run() {
68 mRunnable.run();
69 }
70 }
71
72 public DeferredHandler() {
73 }
74
75 /** Schedule runnable to run after everything that's on the queue right now. */
76 public void post(Runnable runnable) {
77 synchronized (mQueue) {
78 mQueue.add(runnable);
79 if (mQueue.size() == 1) {
80 scheduleNextLocked();
81 }
82 }
83 }
84
85 /** Schedule runnable to run when the queue goes idle. */
86 public void postIdle(final Runnable runnable) {
87 post(new IdleRunnable(runnable));
88 }
89
90 public void cancel() {
91 synchronized (mQueue) {
92 mQueue.clear();
93 }
94 }
95
96 void scheduleNextLocked() {
97 if (mQueue.size() > 0) {
98 Runnable peek = mQueue.getFirst();
99 if (peek instanceof IdleRunnable) {
100 mMessageQueue.addIdleHandler(mHandler);
101 } else {
102 mHandler.sendEmptyMessage(1);
103 }
104 }
105 }
106}
107