1 /* 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 17 package com.android.launcher2; 18 19 import android.os.Handler; 20 import android.os.Looper; 21 import android.os.Message; 22 import android.os.MessageQueue; 23 import android.util.Log; 24 25 import 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 */ 34 public 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) { 43 if (mQueue.size() == 0) { 44 return; 45 } 46 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 cancelRunnable(Runnable runnable) { 91 synchronized (mQueue) { 92 while (mQueue.remove(runnable)) { } 93 } 94 } 95 96 public void cancel() { 97 synchronized (mQueue) { 98 mQueue.clear(); 99 } 100 } 101 102 void scheduleNextLocked() { 103 if (mQueue.size() > 0) { 104 Runnable peek = mQueue.getFirst(); 105 if (peek instanceof IdleRunnable) { 106 mMessageQueue.addIdleHandler(mHandler); 107 } else { 108 mHandler.sendEmptyMessage(1); 109 } 110 } 111 } 112 } 113 114