blob: a43ab672398c24833593f6322647e3792c5549b6 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Joe Onorato9c1289c2009-08-17 11:03:03 -040018
19import android.os.Handler;
20import android.os.Looper;
21import android.os.Message;
22import android.os.MessageQueue;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010023
Adam Cohen091440a2015-03-18 14:16:05 -070024import com.android.launcher3.util.Thunk;
25
Winson Chung81b52252012-08-27 15:34:29 -070026import java.util.LinkedList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040027
28/**
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.
32 *
33 * This class is fifo.
34 */
35public class DeferredHandler {
Sunny Goyald33860f2015-04-23 16:02:20 -070036 @Thunk LinkedList<Runnable> mQueue = new LinkedList<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -040037 private MessageQueue mMessageQueue = Looper.myQueue();
38 private Impl mHandler = new Impl();
39
Adam Cohen091440a2015-03-18 14:16:05 -070040 @Thunk class Impl extends Handler implements MessageQueue.IdleHandler {
Joe Onorato9c1289c2009-08-17 11:03:03 -040041 public void handleMessage(Message msg) {
42 Runnable r;
43 synchronized (mQueue) {
Joe Onorato33ed7b22009-09-23 18:20:54 -070044 if (mQueue.size() == 0) {
45 return;
46 }
Sunny Goyald33860f2015-04-23 16:02:20 -070047 r = mQueue.removeFirst();
Joe Onorato9c1289c2009-08-17 11:03:03 -040048 }
49 r.run();
50 synchronized (mQueue) {
51 scheduleNextLocked();
52 }
53 }
54
55 public boolean queueIdle() {
56 handleMessage(null);
57 return false;
58 }
59 }
60
61 private class IdleRunnable implements Runnable {
62 Runnable mRunnable;
63
64 IdleRunnable(Runnable r) {
65 mRunnable = r;
66 }
67
68 public void run() {
69 mRunnable.run();
70 }
71 }
72
73 public DeferredHandler() {
74 }
75
76 /** Schedule runnable to run after everything that's on the queue right now. */
77 public void post(Runnable runnable) {
78 synchronized (mQueue) {
Sunny Goyald33860f2015-04-23 16:02:20 -070079 mQueue.add(runnable);
Joe Onorato9c1289c2009-08-17 11:03:03 -040080 if (mQueue.size() == 1) {
81 scheduleNextLocked();
82 }
83 }
84 }
85
86 /** Schedule runnable to run when the queue goes idle. */
87 public void postIdle(final Runnable runnable) {
Sunny Goyald33860f2015-04-23 16:02:20 -070088 post(new IdleRunnable(runnable));
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 }
90
Sunny Goyald33860f2015-04-23 16:02:20 -070091 public void cancelAll() {
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 synchronized (mQueue) {
93 mQueue.clear();
94 }
95 }
96
Adam Cohena13a2f22012-07-23 14:29:15 -070097 /** Runs all queued Runnables from the calling thread. */
98 public void flush() {
Sunny Goyald33860f2015-04-23 16:02:20 -070099 LinkedList<Runnable> queue = new LinkedList<>();
Adam Cohena13a2f22012-07-23 14:29:15 -0700100 synchronized (mQueue) {
101 queue.addAll(mQueue);
102 mQueue.clear();
103 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700104 for (Runnable r : queue) {
105 r.run();
Adam Cohena13a2f22012-07-23 14:29:15 -0700106 }
107 }
108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 void scheduleNextLocked() {
110 if (mQueue.size() > 0) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700111 Runnable peek = mQueue.getFirst();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 if (peek instanceof IdleRunnable) {
113 mMessageQueue.addIdleHandler(mHandler);
114 } else {
115 mHandler.sendEmptyMessage(1);
116 }
117 }
118 }
119}
120