2015-08-28 15:19:36 -07:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3.util;
|
|
|
|
|
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.View.OnAttachStateChangeListener;
|
|
|
|
|
import android.view.ViewTreeObserver.OnDrawListener;
|
|
|
|
|
|
2024-02-14 15:07:26 -08:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
2015-08-28 15:19:36 -07:00
|
|
|
import com.android.launcher3.Launcher;
|
|
|
|
|
|
2020-01-07 13:07:55 -08:00
|
|
|
import java.util.function.Consumer;
|
2015-08-28 15:19:36 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An executor which runs all the tasks after the first onDraw is called on the target view.
|
|
|
|
|
*/
|
2021-06-17 15:15:46 -07:00
|
|
|
public class ViewOnDrawExecutor implements OnDrawListener, Runnable,
|
2015-08-28 15:19:36 -07:00
|
|
|
OnAttachStateChangeListener {
|
|
|
|
|
|
2021-06-17 15:15:46 -07:00
|
|
|
private final RunnableList mTasks;
|
2024-02-14 15:07:26 -08:00
|
|
|
private final Consumer<ViewOnDrawExecutor> mOnClearCallback;
|
2015-08-28 15:19:36 -07:00
|
|
|
private View mAttachedView;
|
|
|
|
|
private boolean mCompleted;
|
|
|
|
|
|
2016-04-02 11:23:39 -07:00
|
|
|
private boolean mFirstDrawCompleted;
|
|
|
|
|
|
2021-06-17 15:15:46 -07:00
|
|
|
private boolean mCancelled;
|
|
|
|
|
|
2024-02-14 15:07:26 -08:00
|
|
|
public ViewOnDrawExecutor(RunnableList tasks,
|
|
|
|
|
@NonNull Consumer<ViewOnDrawExecutor> onClearCallback) {
|
2021-06-17 15:15:46 -07:00
|
|
|
mTasks = tasks;
|
2024-02-14 15:07:26 -08:00
|
|
|
mOnClearCallback = onClearCallback;
|
2018-02-22 15:05:41 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-17 15:15:46 -07:00
|
|
|
public void attachTo(Launcher launcher) {
|
|
|
|
|
mAttachedView = launcher.getWorkspace();
|
2015-08-28 15:19:36 -07:00
|
|
|
mAttachedView.addOnAttachStateChangeListener(this);
|
2019-10-18 11:34:21 -07:00
|
|
|
if (mAttachedView.isAttachedToWindow()) {
|
|
|
|
|
attachObserver();
|
|
|
|
|
}
|
2015-08-28 15:19:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void attachObserver() {
|
|
|
|
|
if (!mCompleted) {
|
|
|
|
|
mAttachedView.getViewTreeObserver().addOnDrawListener(this);
|
2023-05-31 10:59:25 -07:00
|
|
|
mAttachedView.getRootView().invalidate();
|
2015-08-28 15:19:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onViewAttachedToWindow(View v) {
|
|
|
|
|
attachObserver();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2018-02-22 15:05:41 -08:00
|
|
|
public void onViewDetachedFromWindow(View v) {}
|
2015-08-28 15:19:36 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDraw() {
|
2016-04-02 11:23:39 -07:00
|
|
|
mFirstDrawCompleted = true;
|
2015-08-28 15:19:36 -07:00
|
|
|
mAttachedView.post(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2024-02-14 15:07:26 -08:00
|
|
|
// Post the pending tasks after first draw
|
|
|
|
|
if (mFirstDrawCompleted && !mCompleted) {
|
2021-06-17 15:15:46 -07:00
|
|
|
markCompleted();
|
2015-08-28 15:19:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 15:15:46 -07:00
|
|
|
/**
|
|
|
|
|
* Executes all tasks immediately
|
|
|
|
|
*/
|
2015-08-28 15:19:36 -07:00
|
|
|
public void markCompleted() {
|
2021-06-17 15:15:46 -07:00
|
|
|
if (!mCancelled) {
|
|
|
|
|
mTasks.executeAllAndDestroy();
|
|
|
|
|
}
|
2016-04-02 11:23:39 -07:00
|
|
|
mCompleted = true;
|
2015-08-28 15:19:36 -07:00
|
|
|
if (mAttachedView != null) {
|
|
|
|
|
mAttachedView.getViewTreeObserver().removeOnDrawListener(this);
|
|
|
|
|
mAttachedView.removeOnAttachStateChangeListener(this);
|
|
|
|
|
}
|
2024-02-14 15:07:26 -08:00
|
|
|
|
|
|
|
|
mOnClearCallback.accept(this);
|
2018-02-22 15:05:41 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-17 15:15:46 -07:00
|
|
|
public void cancel() {
|
|
|
|
|
mCancelled = true;
|
2018-02-22 15:05:41 -08:00
|
|
|
markCompleted();
|
|
|
|
|
}
|
2015-08-28 15:19:36 -07:00
|
|
|
}
|