blob: fbb24586204ca71526c51bae6181c0fd939f08ac [file] [log] [blame]
Josh Gargus51f9c0f2012-03-20 12:18:23 -07001/*
2 * Copyright (C) 2012 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.contacts.util;
18
19import android.view.View;
Josh Gargusee287e92012-04-06 20:11:33 -070020import android.view.ViewTreeObserver.OnDrawListener;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070021import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Brian Attwellb7e43642014-06-02 14:33:04 -070022import android.view.ViewTreeObserver.OnPreDrawListener;
Josh Gargus51f9c0f2012-03-20 12:18:23 -070023
Josh Gargus6f5557e2012-03-21 10:45:08 -070024/** Static methods that are useful for scheduling actions to occur at a later time. */
25public class SchedulingUtils {
Josh Gargus51f9c0f2012-03-20 12:18:23 -070026
Josh Gargusee287e92012-04-06 20:11:33 -070027
Josh Gargus51f9c0f2012-03-20 12:18:23 -070028 /** Runs a piece of code after the next layout run */
29 public static void doAfterLayout(final View view, final Runnable runnable) {
30 final OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {
31 @Override
32 public void onGlobalLayout() {
33 // Layout pass done, unregister for further events
34 view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
35 runnable.run();
36 }
37 };
38 view.getViewTreeObserver().addOnGlobalLayoutListener(listener);
39 }
Josh Gargusee287e92012-04-06 20:11:33 -070040
Brian Attwellb7e43642014-06-02 14:33:04 -070041 /** Runs a piece of code just before the next draw, after layout and measurement */
42 public static void doOnPreDraw(final View view, final boolean drawNextFrame,
43 final Runnable runnable) {
44 final OnPreDrawListener listener = new OnPreDrawListener() {
Josh Gargusee287e92012-04-06 20:11:33 -070045 @Override
Brian Attwellb7e43642014-06-02 14:33:04 -070046 public boolean onPreDraw() {
47 view.getViewTreeObserver().removeOnPreDrawListener(this);
Josh Gargusee287e92012-04-06 20:11:33 -070048 runnable.run();
Brian Attwellb7e43642014-06-02 14:33:04 -070049 return drawNextFrame;
Josh Gargusee287e92012-04-06 20:11:33 -070050 }
51 };
Brian Attwellb7e43642014-06-02 14:33:04 -070052 view.getViewTreeObserver().addOnPreDrawListener(listener);
Josh Gargusee287e92012-04-06 20:11:33 -070053 }
Josh Gargus51f9c0f2012-03-20 12:18:23 -070054}