Josh Gargus | 51f9c0f | 2012-03-20 12:18:23 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts.util; |
| 18 | |
| 19 | import android.view.View; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 20 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
Brian Attwell | b7e4364 | 2014-06-02 14:33:04 -0700 | [diff] [blame] | 21 | import android.view.ViewTreeObserver.OnPreDrawListener; |
Josh Gargus | 51f9c0f | 2012-03-20 12:18:23 -0700 | [diff] [blame] | 22 | |
Josh Gargus | 6f5557e | 2012-03-21 10:45:08 -0700 | [diff] [blame] | 23 | /** Static methods that are useful for scheduling actions to occur at a later time. */ |
| 24 | public class SchedulingUtils { |
Josh Gargus | 51f9c0f | 2012-03-20 12:18:23 -0700 | [diff] [blame] | 25 | |
Josh Gargus | ee287e9 | 2012-04-06 20:11:33 -0700 | [diff] [blame] | 26 | |
Josh Gargus | 51f9c0f | 2012-03-20 12:18:23 -0700 | [diff] [blame] | 27 | /** Runs a piece of code after the next layout run */ |
| 28 | public static void doAfterLayout(final View view, final Runnable runnable) { |
| 29 | final OnGlobalLayoutListener listener = new OnGlobalLayoutListener() { |
| 30 | @Override |
| 31 | public void onGlobalLayout() { |
| 32 | // Layout pass done, unregister for further events |
| 33 | view.getViewTreeObserver().removeOnGlobalLayoutListener(this); |
| 34 | runnable.run(); |
| 35 | } |
| 36 | }; |
| 37 | view.getViewTreeObserver().addOnGlobalLayoutListener(listener); |
| 38 | } |
Josh Gargus | ee287e9 | 2012-04-06 20:11:33 -0700 | [diff] [blame] | 39 | |
Brian Attwell | b7e4364 | 2014-06-02 14:33:04 -0700 | [diff] [blame] | 40 | /** Runs a piece of code just before the next draw, after layout and measurement */ |
| 41 | public static void doOnPreDraw(final View view, final boolean drawNextFrame, |
| 42 | final Runnable runnable) { |
| 43 | final OnPreDrawListener listener = new OnPreDrawListener() { |
Josh Gargus | ee287e9 | 2012-04-06 20:11:33 -0700 | [diff] [blame] | 44 | @Override |
Brian Attwell | b7e4364 | 2014-06-02 14:33:04 -0700 | [diff] [blame] | 45 | public boolean onPreDraw() { |
| 46 | view.getViewTreeObserver().removeOnPreDrawListener(this); |
Josh Gargus | ee287e9 | 2012-04-06 20:11:33 -0700 | [diff] [blame] | 47 | runnable.run(); |
Brian Attwell | b7e4364 | 2014-06-02 14:33:04 -0700 | [diff] [blame] | 48 | return drawNextFrame; |
Josh Gargus | ee287e9 | 2012-04-06 20:11:33 -0700 | [diff] [blame] | 49 | } |
| 50 | }; |
Brian Attwell | b7e4364 | 2014-06-02 14:33:04 -0700 | [diff] [blame] | 51 | view.getViewTreeObserver().addOnPreDrawListener(listener); |
Josh Gargus | ee287e9 | 2012-04-06 20:11:33 -0700 | [diff] [blame] | 52 | } |
Josh Gargus | 51f9c0f | 2012-03-20 12:18:23 -0700 | [diff] [blame] | 53 | } |