Revert "Cancel gestures on launcher destroy"

Revert "Refactor SchedulerCallback implementation"

Revert submission 20555112-launcher-destroy-gestures

Reason for revert: causes test failures
Reverted Changes:
Ib43cdd597:Refactor SchedulerCallback implementation
Ia8e936e26:Cancel gestures on launcher destroy

Change-Id: I2199e39237b7c9fe25784b46e16960cd5f17c46c
This commit is contained in:
Schneider Victor-tulias authored and Android (Google) Code Review committed 2022-12-07 19:46:00 +00:00
1 parent 65dda6d03e
commit f0de6cc2ca
11 files changed
+121 -246

No files matched your search

@@ -20,6 +20,8 @@ import androidx.annotation.Nullable;
import com.android.launcher3.BaseActivity;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArrayList;
/**
@@ -28,15 +30,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
*/
public final class ActivityTracker<T extends BaseActivity> {
public static final int TYPE_INIT = 0;
public static final int TYPE_DESTROY = 1;
public static final int TYPE_BOTH = 2;
private WeakReference<T> mCurrentActivity = new WeakReference<>(null);
private CopyOnWriteArrayList<SchedulerCallback<T>> mActivityReadyCallbacks =
new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<SchedulerCallback<T>> mActivityDestroyedCallbacks =
new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<SchedulerCallback<T>> mCallbacks = new CopyOnWriteArrayList<>();
@Nullable
public <R extends T> R getCreatedActivity() {
@@ -47,74 +42,32 @@ public final class ActivityTracker<T extends BaseActivity> {
if (mCurrentActivity.get() == activity) {
mCurrentActivity.clear();
}
for (SchedulerCallback<T> cb : mActivityDestroyedCallbacks) {
cb.onActivityDestroyed();
unregisterCallback(cb, TYPE_DESTROY);
}
}
/** Registers an activity create callback. */
/**
* Call {@link SchedulerCallback#init(BaseActivity, boolean)} when the
* activity is ready. If the activity is already created, this is called immediately.
*
* The tracker maintains a strong ref to the callback, so it is up to the caller to return
* {@code false} in the callback OR to unregister the callback explicitly.
*
* @param callback The callback to call init() on when the activity is ready.
*/
public void registerCallback(SchedulerCallback<T> callback) {
registerCallback(callback, TYPE_INIT);
}
/**
* Call {@link SchedulerCallback#onActivityReady(BaseActivity, boolean)} when the
* activity is ready and/or {@link SchedulerCallback#onActivityDestroyed()} when the activity
* is destroyed.
*
* If type is {@link ActivityTracker#TYPE_INIT} TYPE_INIT or
* {@link ActivityTracker#TYPE_BOTH} and the activity is already created, this
* {@link SchedulerCallback#onActivityReady(BaseActivity, boolean)} is called immediately.
*
* If type is {@link ActivityTracker#TYPE_DESTROY} or
* {@link ActivityTracker#TYPE_BOTH} and the activity is already destroyed,
* {@link SchedulerCallback#onActivityDestroyed()} is called immediately.
*
* The tracker maintains a strong ref to the callbacks, so it is up to the caller to return
* {@code false} in {@link SchedulerCallback#onActivityReady(BaseActivity, boolean)} OR to
* unregister the callback explicitly.
*
* @param callback The callback to call init() or cleanUp() on when the activity is ready or
* destroyed.
* @param type whether to use this callback on activity create, destroy or both.
*/
public void registerCallback(SchedulerCallback<T> callback, int type) {
T activity = mCurrentActivity.get();
if (type == TYPE_INIT || type == TYPE_BOTH) {
mActivityReadyCallbacks.add(callback);
if (activity != null) {
if (!callback.onActivityReady(activity, activity.isStarted())) {
unregisterCallback(callback, TYPE_INIT);
}
mCallbacks.add(callback);
if (activity != null) {
if (!callback.init(activity, activity.isStarted())) {
unregisterCallback(callback);
}
}
if (type == TYPE_DESTROY || type == TYPE_BOTH) {
mActivityDestroyedCallbacks.add(callback);
if (activity == null) {
callback.onActivityDestroyed();
unregisterCallback(callback, TYPE_DESTROY);
}
}
}
/**
* Unregisters a registered activity create callback.
*/
public void unregisterCallback(SchedulerCallback<T> callback) {
unregisterCallback(callback, TYPE_INIT);
}
/**
* Unregisters a registered callback.
*/
public void unregisterCallback(SchedulerCallback<T> callback, int type) {
if (type == TYPE_INIT || type == TYPE_BOTH) {
mActivityReadyCallbacks.remove(callback);
}
if (type == TYPE_DESTROY || type == TYPE_BOTH) {
mActivityDestroyedCallbacks.remove(callback);
}
public void unregisterCallback(SchedulerCallback<T> callback) {
mCallbacks.remove(callback);
}
public boolean handleCreate(T activity) {
@@ -128,8 +81,8 @@ public final class ActivityTracker<T extends BaseActivity> {
private boolean handleIntent(T activity, boolean alreadyOnHome) {
boolean handled = false;
for (SchedulerCallback<T> cb : mActivityReadyCallbacks) {
if (!cb.onActivityReady(activity, alreadyOnHome)) {
for (SchedulerCallback<T> cb : mCallbacks) {
if (!cb.init(activity, alreadyOnHome)) {
// Callback doesn't want any more updates
unregisterCallback(cb);
}
@@ -145,11 +98,6 @@ public final class ActivityTracker<T extends BaseActivity> {
* @param alreadyOnHome Whether the activity is already started.
* @return Whether to continue receiving callbacks (i.e. if the activity is recreated).
*/
boolean onActivityReady(T activity, boolean alreadyOnHome);
/**
* Called then the activity gets destroyed.
*/
default void onActivityDestroyed() { }
boolean init(T activity, boolean alreadyOnHome);
}
}