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