1 /**
2  * Copyright (C) 2015 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.launcher3.util;
18 
19 import android.view.View;
20 import android.view.View.OnAttachStateChangeListener;
21 import android.view.ViewTreeObserver.OnDrawListener;
22 
23 import androidx.annotation.NonNull;
24 
25 import com.android.launcher3.Launcher;
26 
27 import java.util.function.Consumer;
28 
29 /**
30  * An executor which runs all the tasks after the first onDraw is called on the target view.
31  */
32 public class ViewOnDrawExecutor implements OnDrawListener, Runnable,
33         OnAttachStateChangeListener {
34 
35     private final RunnableList mTasks;
36     private final Consumer<ViewOnDrawExecutor> mOnClearCallback;
37     private View mAttachedView;
38     private boolean mCompleted;
39 
40     private boolean mFirstDrawCompleted;
41 
42     private boolean mCancelled;
43 
ViewOnDrawExecutor(RunnableList tasks, @NonNull Consumer<ViewOnDrawExecutor> onClearCallback)44     public ViewOnDrawExecutor(RunnableList tasks,
45             @NonNull Consumer<ViewOnDrawExecutor> onClearCallback) {
46         mTasks = tasks;
47         mOnClearCallback = onClearCallback;
48     }
49 
attachTo(Launcher launcher)50     public void attachTo(Launcher launcher) {
51         mAttachedView = launcher.getWorkspace();
52         mAttachedView.addOnAttachStateChangeListener(this);
53         if (mAttachedView.isAttachedToWindow()) {
54             attachObserver();
55         }
56     }
57 
attachObserver()58     private void attachObserver() {
59         if (!mCompleted) {
60             mAttachedView.getViewTreeObserver().addOnDrawListener(this);
61             mAttachedView.getRootView().invalidate();
62         }
63     }
64 
65     @Override
onViewAttachedToWindow(View v)66     public void onViewAttachedToWindow(View v) {
67         attachObserver();
68     }
69 
70     @Override
onViewDetachedFromWindow(View v)71     public void onViewDetachedFromWindow(View v) {}
72 
73     @Override
onDraw()74     public void onDraw() {
75         mFirstDrawCompleted = true;
76         mAttachedView.post(this);
77     }
78 
79     @Override
run()80     public void run() {
81         // Post the pending tasks after first draw
82         if (mFirstDrawCompleted && !mCompleted) {
83             markCompleted();
84         }
85     }
86 
87     /**
88      * Executes all tasks immediately
89      */
markCompleted()90     public void markCompleted() {
91         if (!mCancelled) {
92             mTasks.executeAllAndDestroy();
93         }
94         mCompleted = true;
95         if (mAttachedView != null) {
96             mAttachedView.getViewTreeObserver().removeOnDrawListener(this);
97             mAttachedView.removeOnAttachStateChangeListener(this);
98         }
99 
100         mOnClearCallback.accept(this);
101     }
102 
cancel()103     public void cancel() {
104         mCancelled = true;
105         markCompleted();
106     }
107 }
108