1/*
2 * Copyright (C) 2016 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 */
16package com.android.internal.view;
17
18import android.view.View;
19import android.view.ViewTreeObserver;
20
21/**
22 * An OnPreDrawListener that will remove itself after one OnPreDraw call. Typical
23 * usage is:
24 * <pre><code>
25 *     OneShotPreDrawListener.add(view, () -> { view.doSomething(); })
26 * </code></pre>
27 * <p>
28 * The listener will also remove itself from the ViewTreeObserver when the view
29 * is detached from the view hierarchy. In that case, the Runnable will never be
30 * executed.
31 */
32public class OneShotPreDrawListener implements ViewTreeObserver.OnPreDrawListener,
33        View.OnAttachStateChangeListener {
34    private final View mView;
35    private ViewTreeObserver mViewTreeObserver;
36    private final Runnable mRunnable;
37    private final boolean mReturnValue;
38
39    private OneShotPreDrawListener(View view, boolean returnValue, Runnable runnable) {
40        mView = view;
41        mViewTreeObserver = view.getViewTreeObserver();
42        mRunnable = runnable;
43        mReturnValue = returnValue;
44    }
45
46    /**
47     * Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver. The
48     * return value from the OnPreDrawListener is {@code true}.
49     *
50     * @param view The view whose ViewTreeObserver the OnPreDrawListener should listen.
51     * @param runnable The Runnable to execute in the OnPreDraw (once)
52     * @return The added OneShotPreDrawListener. It can be removed prior to
53     * the onPreDraw by calling {@link #removeListener()}.
54     */
55    public static OneShotPreDrawListener add(View view, Runnable runnable) {
56        return add(view, true, runnable);
57    }
58
59    /**
60     * Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver.
61     *
62     * @param view The view whose ViewTreeObserver the OnPreDrawListener should listen.
63     * @param returnValue The value to be returned from the OnPreDrawListener.
64     * @param runnable The Runnable to execute in the OnPreDraw (once)
65     * @return The added OneShotPreDrawListener. It can be removed prior to
66     * the onPreDraw by calling {@link #removeListener()}.
67     */
68    public static OneShotPreDrawListener add(View view, boolean returnValue, Runnable runnable) {
69        OneShotPreDrawListener listener = new OneShotPreDrawListener(view, returnValue, runnable);
70        view.getViewTreeObserver().addOnPreDrawListener(listener);
71        view.addOnAttachStateChangeListener(listener);
72        return listener;
73    }
74
75    @Override
76    public boolean onPreDraw() {
77        removeListener();
78        mRunnable.run();
79        return mReturnValue;
80    }
81
82    /**
83     * Removes the listener from the ViewTreeObserver. This is useful to call if the
84     * callback should be removed prior to {@link #onPreDraw()}.
85     */
86    public void removeListener() {
87        if (mViewTreeObserver.isAlive()) {
88            mViewTreeObserver.removeOnPreDrawListener(this);
89        } else {
90            mView.getViewTreeObserver().removeOnPreDrawListener(this);
91        }
92        mView.removeOnAttachStateChangeListener(this);
93    }
94
95    @Override
96    public void onViewAttachedToWindow(View v) {
97        mViewTreeObserver = v.getViewTreeObserver();
98    }
99
100    @Override
101    public void onViewDetachedFromWindow(View v) {
102        removeListener();
103    }
104}
105