ViewOnDrawExecutor.java revision 527c7d3460345953e6b3427a74b1dacaddcb1111
1527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal/** 2527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * Copyright (C) 2015 The Android Open Source Project 3527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * 4527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License"); 5527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * you may not use this file except in compliance with the License. 6527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * You may obtain a copy of the License at 7527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * 8527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * http://www.apache.org/licenses/LICENSE-2.0 9527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * 10527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * Unless required by applicable law or agreed to in writing, software 11527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS, 12527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * See the License for the specific language governing permissions and 14527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * limitations under the License. 15527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal */ 16527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 17527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyalpackage com.android.launcher3.util; 18527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 19527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyalimport android.view.View; 20527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyalimport android.view.View.OnAttachStateChangeListener; 21527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyalimport android.view.ViewTreeObserver.OnDrawListener; 22527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 23527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyalimport com.android.launcher3.DeferredHandler; 24527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyalimport com.android.launcher3.Launcher; 25527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 26527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyalimport java.util.ArrayList; 27527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyalimport java.util.concurrent.Executor; 28527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 29527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal/** 30527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal * An executor which runs all the tasks after the first onDraw is called on the target view. 31527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal */ 32527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyalpublic class ViewOnDrawExecutor implements Executor, OnDrawListener, Runnable, 33527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal OnAttachStateChangeListener { 34527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 35527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal private final ArrayList<Runnable> mTasks = new ArrayList<>(); 36527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal private final DeferredHandler mHandler; 37527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 38527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal private Launcher mLauncher; 39527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal private View mAttachedView; 40527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal private boolean mCompleted; 41527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 42527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal public ViewOnDrawExecutor(DeferredHandler handler) { 43527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mHandler = handler; 44527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 45527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 46527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal public void attachTo(Launcher launcher) { 47527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mLauncher = launcher; 48527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mAttachedView = launcher.getWorkspace(); 49527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mAttachedView.addOnAttachStateChangeListener(this); 50527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 51527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal attachObserver(); 52527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 53527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 54527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal private void attachObserver() { 55527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal if (!mCompleted) { 56527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mAttachedView.getViewTreeObserver().addOnDrawListener(this); 57527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 58527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 59527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 60527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal @Override 61527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal public void execute(Runnable command) { 62527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mTasks.add(command); 63527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 64527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 65527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal @Override 66527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal public void onViewAttachedToWindow(View v) { 67527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal attachObserver(); 68527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 69527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 70527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal @Override 71527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal public void onViewDetachedFromWindow(View v) { } 72527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 73527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal @Override 74527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal public void onDraw() { 75527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mAttachedView.post(this); 76527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 77527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 78527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal @Override 79527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal public void run() { 80527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal for (final Runnable r : mTasks) { 81527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mHandler.post(r); 82527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 83527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal markCompleted(); 84527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 85527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal 86527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal public void markCompleted() { 87527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mTasks.clear(); 88527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal if (mAttachedView != null) { 89527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mAttachedView.getViewTreeObserver().removeOnDrawListener(this); 90527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mAttachedView.removeOnAttachStateChangeListener(this); 91527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 92527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal if (mLauncher != null) { 93527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal mLauncher.clearPendingExecutor(this); 94527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 95527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal } 96527c7d3460345953e6b3427a74b1dacaddcb1111Sunny Goyal} 97