Merge "Revert "Cancel gestures on launcher destroy"" into tm-qpr-dev

This commit is contained in:
Schneider Victor-tulias authored and Android (Google) Code Review committed 2022-12-08 14:55:15 +00:00
commit ad48decd4f
11 files changed
+121 -246

No files matched your search

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2022 The Android Open Source Project * Copyright (C) 2018 The Android Open Source Project
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,23 +16,19 @@
package com.android.launcher3; package com.android.launcher3;
import android.animation.AnimatorSet; import android.animation.AnimatorSet;
import android.annotation.Nullable;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.os.Build; import android.os.Build;
import android.os.CancellationSignal; import android.os.CancellationSignal;
import android.view.RemoteAnimationTarget; import android.view.RemoteAnimationTarget;
import com.android.launcher3.uioverrides.QuickstepLauncher; import com.android.launcher3.uioverrides.QuickstepLauncher;
import com.android.quickstep.util.ActivityLifecycleListener; import com.android.quickstep.util.ActivityInitListener;
import com.android.quickstep.util.RemoteAnimationProvider; import com.android.quickstep.util.RemoteAnimationProvider;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
/**
* {@link ActivityLifecycleListener} for the in-launcher recents.
*/
@TargetApi(Build.VERSION_CODES.P) @TargetApi(Build.VERSION_CODES.P)
public class LauncherLifecycleListener extends ActivityLifecycleListener<Launcher> { public class LauncherInitListener extends ActivityInitListener<Launcher> {
private RemoteAnimationProvider mRemoteAnimationProvider; private RemoteAnimationProvider mRemoteAnimationProvider;
@@ -40,16 +36,13 @@ public class LauncherLifecycleListener extends ActivityLifecycleListener<Launche
* @param onInitListener a callback made when the activity is initialized. The callback should * @param onInitListener a callback made when the activity is initialized. The callback should
* return true to continue receiving callbacks (ie. for if the activity is * return true to continue receiving callbacks (ie. for if the activity is
* recreated). * recreated).
* @param onDestroyListener a callback made when the activity is destroyed.
*/ */
public LauncherLifecycleListener( public LauncherInitListener(BiPredicate<Launcher, Boolean> onInitListener) {
@Nullable BiPredicate<Launcher, Boolean> onInitListener, super(onInitListener, Launcher.ACTIVITY_TRACKER);
@Nullable Runnable onDestroyListener) {
super(onInitListener, onDestroyListener, Launcher.ACTIVITY_TRACKER);
} }
@Override @Override
public boolean handleActivityReady(Launcher launcher, boolean alreadyOnHome) { public boolean handleInit(Launcher launcher, boolean alreadyOnHome) {
if (mRemoteAnimationProvider != null) { if (mRemoteAnimationProvider != null) {
QuickstepTransitionManager appTransitionManager = QuickstepTransitionManager appTransitionManager =
((QuickstepLauncher) launcher).getAppTransitionManager(); ((QuickstepLauncher) launcher).getAppTransitionManager();
@@ -75,7 +68,7 @@ public class LauncherLifecycleListener extends ActivityLifecycleListener<Launche
}, cancellationSignal); }, cancellationSignal);
} }
launcher.deferOverlayCallbacksUntilNextResumeOrStop(); launcher.deferOverlayCallbacksUntilNextResumeOrStop();
return super.handleActivityReady(launcher, alreadyOnHome); return super.handleInit(launcher, alreadyOnHome);
} }
@Override @Override
@@ -50,7 +50,6 @@ import static com.android.quickstep.GestureState.STATE_RECENTS_SCROLLING_FINISHE
import static com.android.quickstep.MultiStateCallback.DEBUG_STATES; import static com.android.quickstep.MultiStateCallback.DEBUG_STATES;
import static com.android.quickstep.util.ActiveGestureErrorDetector.GestureEvent.CANCEL_RECENTS_ANIMATION; import static com.android.quickstep.util.ActiveGestureErrorDetector.GestureEvent.CANCEL_RECENTS_ANIMATION;
import static com.android.quickstep.util.ActiveGestureErrorDetector.GestureEvent.EXPECTING_TASK_APPEARED; import static com.android.quickstep.util.ActiveGestureErrorDetector.GestureEvent.EXPECTING_TASK_APPEARED;
import static com.android.quickstep.util.ActiveGestureErrorDetector.GestureEvent.LAUNCHER_DESTROYED;
import static com.android.quickstep.util.ActiveGestureErrorDetector.GestureEvent.ON_SETTLED_ON_END_TARGET; import static com.android.quickstep.util.ActiveGestureErrorDetector.GestureEvent.ON_SETTLED_ON_END_TARGET;
import static com.android.quickstep.util.VibratorWrapper.OVERVIEW_HAPTIC; import static com.android.quickstep.util.VibratorWrapper.OVERVIEW_HAPTIC;
import static com.android.quickstep.views.RecentsView.UPDATE_SYSUI_FLAGS_THRESHOLD; import static com.android.quickstep.views.RecentsView.UPDATE_SYSUI_FLAGS_THRESHOLD;
@@ -114,7 +113,7 @@ import com.android.quickstep.GestureState.GestureEndTarget;
import com.android.quickstep.RemoteTargetGluer.RemoteTargetHandle; import com.android.quickstep.RemoteTargetGluer.RemoteTargetHandle;
import com.android.quickstep.util.ActiveGestureErrorDetector; import com.android.quickstep.util.ActiveGestureErrorDetector;
import com.android.quickstep.util.ActiveGestureLog; import com.android.quickstep.util.ActiveGestureLog;
import com.android.quickstep.util.ActivityLifecycleListener; import com.android.quickstep.util.ActivityInitListener;
import com.android.quickstep.util.AnimatorControllerWithResistance; import com.android.quickstep.util.AnimatorControllerWithResistance;
import com.android.quickstep.util.InputConsumerProxy; import com.android.quickstep.util.InputConsumerProxy;
import com.android.quickstep.util.InputProxyHandlerFactory; import com.android.quickstep.util.InputProxyHandlerFactory;
@@ -160,7 +159,7 @@ public abstract class AbsSwipeUpHandler<T extends StatefulActivity<S>,
protected final BaseActivityInterface<S, T> mActivityInterface; protected final BaseActivityInterface<S, T> mActivityInterface;
protected final InputConsumerProxy mInputConsumerProxy; protected final InputConsumerProxy mInputConsumerProxy;
protected final ActivityLifecycleListener mActivityInitListener; protected final ActivityInitListener mActivityInitListener;
// Callbacks to be made once the recents animation starts // Callbacks to be made once the recents animation starts
private final ArrayList<Runnable> mRecentsAnimationStartCallbacks = new ArrayList<>(); private final ArrayList<Runnable> mRecentsAnimationStartCallbacks = new ArrayList<>();
private final OnScrollChangedListener mOnRecentsScrollListener = this::onRecentsViewScroll; private final OnScrollChangedListener mOnRecentsScrollListener = this::onRecentsViewScroll;
@@ -330,8 +329,7 @@ public abstract class AbsSwipeUpHandler<T extends StatefulActivity<S>,
InputConsumerController inputConsumer) { InputConsumerController inputConsumer) {
super(context, deviceState, gestureState); super(context, deviceState, gestureState);
mActivityInterface = gestureState.getActivityInterface(); mActivityInterface = gestureState.getActivityInterface();
mActivityInitListener = mActivityInterface.createActivityLifecycleListener( mActivityInitListener = mActivityInterface.createActivityInitListener(this::onActivityInit);
this::onActivityInit, this::onActivityDestroy);
mInputConsumerProxy = mInputConsumerProxy =
new InputConsumerProxy(context, /* rotationSupplier = */ () -> { new InputConsumerProxy(context, /* rotationSupplier = */ () -> {
if (mRecentsView == null) { if (mRecentsView == null) {
@@ -522,11 +520,6 @@ public abstract class AbsSwipeUpHandler<T extends StatefulActivity<S>,
return true; return true;
} }
private void onActivityDestroy() {
ActiveGestureLog.INSTANCE.addLog("Launcher activity destroyed", LAUNCHER_DESTROYED);
onGestureCancelled();
}
/** /**
* Return true if the window should be translated horizontally if the recents view scrolls * Return true if the window should be translated horizontally if the recents view scrolls
*/ */
@@ -60,7 +60,7 @@ import com.android.launcher3.touch.PagedOrientationHandler;
import com.android.launcher3.util.DisplayController; import com.android.launcher3.util.DisplayController;
import com.android.launcher3.util.NavigationMode; import com.android.launcher3.util.NavigationMode;
import com.android.launcher3.views.ScrimView; import com.android.launcher3.views.ScrimView;
import com.android.quickstep.util.ActivityLifecycleListener; import com.android.quickstep.util.ActivityInitListener;
import com.android.quickstep.util.AnimatorControllerWithResistance; import com.android.quickstep.util.AnimatorControllerWithResistance;
import com.android.quickstep.views.RecentsView; import com.android.quickstep.views.RecentsView;
import com.android.systemui.shared.recents.model.ThumbnailData; import com.android.systemui.shared.recents.model.ThumbnailData;
@@ -123,17 +123,8 @@ public abstract class BaseActivityInterface<STATE_TYPE extends BaseState<STATE_T
public abstract AnimationFactory prepareRecentsUI(RecentsAnimationDeviceState deviceState, public abstract AnimationFactory prepareRecentsUI(RecentsAnimationDeviceState deviceState,
boolean activityVisible, Consumer<AnimatorControllerWithResistance> callback); boolean activityVisible, Consumer<AnimatorControllerWithResistance> callback);
/** public abstract ActivityInitListener createActivityInitListener(
* Creates a activity listener for activity initialized and/or destroyed. One or both of these Predicate<Boolean> onInitListener);
* listeners must be provided.
*
* @param onInitListener a callback made when the activity is initialized. The callback should
* return true to continue receiving callbacks (ie. for if the activity is
* recreated).
* @param onDestroyListener a callback made when the activity is destroyed.
*/
public abstract ActivityLifecycleListener createActivityLifecycleListener(
@Nullable Predicate<Boolean> onInitListener, @Nullable Runnable onDestroyListener);
/** /**
* Sets a callback to be run when an activity launch happens while launcher is not yet resumed. * Sets a callback to be run when an activity launch happens while launcher is not yet resumed.
@@ -36,7 +36,7 @@ import com.android.launcher3.touch.PagedOrientationHandler;
import com.android.launcher3.util.DisplayController; import com.android.launcher3.util.DisplayController;
import com.android.quickstep.GestureState.GestureEndTarget; import com.android.quickstep.GestureState.GestureEndTarget;
import com.android.quickstep.fallback.RecentsState; import com.android.quickstep.fallback.RecentsState;
import com.android.quickstep.util.ActivityLifecycleListener; import com.android.quickstep.util.ActivityInitListener;
import com.android.quickstep.util.AnimatorControllerWithResistance; import com.android.quickstep.util.AnimatorControllerWithResistance;
import com.android.quickstep.views.RecentsView; import com.android.quickstep.views.RecentsView;
@@ -88,12 +88,10 @@ public final class FallbackActivityInterface extends
} }
@Override @Override
public ActivityLifecycleListener createActivityLifecycleListener( public ActivityInitListener createActivityInitListener(
@Nullable Predicate<Boolean> onInitListener, @Nullable Runnable onDestroyListener) { Predicate<Boolean> onInitListener) {
return new ActivityLifecycleListener<>( return new ActivityInitListener<>((activity, alreadyOnHome) ->
(activity, alreadyOnHome) -> onInitListener.test(alreadyOnHome), onInitListener.test(alreadyOnHome), RecentsActivity.ACTIVITY_TRACKER);
onDestroyListener,
RecentsActivity.ACTIVITY_TRACKER);
} }
@Nullable @Nullable
@@ -37,7 +37,7 @@ import androidx.annotation.UiThread;
import com.android.launcher3.DeviceProfile; import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Launcher; import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherAnimUtils; import com.android.launcher3.LauncherAnimUtils;
import com.android.launcher3.LauncherLifecycleListener; import com.android.launcher3.LauncherInitListener;
import com.android.launcher3.LauncherState; import com.android.launcher3.LauncherState;
import com.android.launcher3.anim.PendingAnimation; import com.android.launcher3.anim.PendingAnimation;
import com.android.launcher3.statehandlers.DepthController; import com.android.launcher3.statehandlers.DepthController;
@@ -49,7 +49,7 @@ import com.android.launcher3.uioverrides.QuickstepLauncher;
import com.android.launcher3.util.DisplayController; import com.android.launcher3.util.DisplayController;
import com.android.launcher3.util.NavigationMode; import com.android.launcher3.util.NavigationMode;
import com.android.quickstep.GestureState.GestureEndTarget; import com.android.quickstep.GestureState.GestureEndTarget;
import com.android.quickstep.util.ActivityLifecycleListener; import com.android.quickstep.util.ActivityInitListener;
import com.android.quickstep.util.AnimatorControllerWithResistance; import com.android.quickstep.util.AnimatorControllerWithResistance;
import com.android.quickstep.util.LayoutUtils; import com.android.quickstep.util.LayoutUtils;
import com.android.quickstep.views.RecentsView; import com.android.quickstep.views.RecentsView;
@@ -136,10 +136,9 @@ public final class LauncherActivityInterface extends
} }
@Override @Override
public ActivityLifecycleListener createActivityLifecycleListener( public ActivityInitListener createActivityInitListener(Predicate<Boolean> onInitListener) {
@Nullable Predicate<Boolean> onInitListener, @Nullable Runnable onDestroyListener) { return new LauncherInitListener((activity, alreadyOnHome) ->
return new LauncherLifecycleListener((activity, alreadyOnHome) -> onInitListener.test(alreadyOnHome));
onInitListener.test(alreadyOnHome), onDestroyListener);
} }
@Override @Override
@@ -37,7 +37,7 @@ public class ActiveGestureErrorDetector {
ON_SETTLED_ON_END_TARGET, START_RECENTS_ANIMATION, FINISH_RECENTS_ANIMATION, ON_SETTLED_ON_END_TARGET, START_RECENTS_ANIMATION, FINISH_RECENTS_ANIMATION,
CANCEL_RECENTS_ANIMATION, SET_ON_PAGE_TRANSITION_END_CALLBACK, CANCEL_CURRENT_ANIMATION, CANCEL_RECENTS_ANIMATION, SET_ON_PAGE_TRANSITION_END_CALLBACK, CANCEL_CURRENT_ANIMATION,
CLEANUP_SCREENSHOT, SCROLLER_ANIMATION_ABORTED, TASK_APPEARED, EXPECTING_TASK_APPEARED, CLEANUP_SCREENSHOT, SCROLLER_ANIMATION_ABORTED, TASK_APPEARED, EXPECTING_TASK_APPEARED,
FLAG_USING_OTHER_ACTIVITY_INPUT_CONSUMER, LAUNCHER_DESTROYED, FLAG_USING_OTHER_ACTIVITY_INPUT_CONSUMER,
/** /**
* These GestureEvents are specifically associated to state flags that get set in * These GestureEvents are specifically associated to state flags that get set in
@@ -162,13 +162,6 @@ public class ActiveGestureErrorDetector {
+ "before/without setting end target to new task", + "before/without setting end target to new task",
writer); writer);
break; break;
case LAUNCHER_DESTROYED:
errorDetected |= printErrorIfTrue(
true,
prefix,
/* errorMessage= */ "Launcher destroyed mid-gesture",
writer);
break;
case STATE_GESTURE_COMPLETED: case STATE_GESTURE_COMPLETED:
errorDetected |= printErrorIfTrue( errorDetected |= printErrorIfTrue(
!encounteredEvents.contains(GestureEvent.MOTION_UP), !encounteredEvents.contains(GestureEvent.MOTION_UP),
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2019 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.quickstep.util;
import com.android.launcher3.BaseActivity;
import com.android.launcher3.util.ActivityTracker;
import com.android.launcher3.util.ActivityTracker.SchedulerCallback;
import java.util.function.BiPredicate;
public class ActivityInitListener<T extends BaseActivity> implements
SchedulerCallback<T> {
private BiPredicate<T, Boolean> mOnInitListener;
private final ActivityTracker<T> mActivityTracker;
private boolean mIsRegistered = false;
/**
* @param onInitListener a callback made when the activity is initialized. The callback should
* return true to continue receiving callbacks (ie. for if the activity is
* recreated).
*/
public ActivityInitListener(BiPredicate<T, Boolean> onInitListener,
ActivityTracker<T> tracker) {
mOnInitListener = onInitListener;
mActivityTracker = tracker;
}
@Override
public final boolean init(T activity, boolean alreadyOnHome) {
if (!mIsRegistered) {
// Don't receive any more updates
return false;
}
return handleInit(activity, alreadyOnHome);
}
protected boolean handleInit(T activity, boolean alreadyOnHome) {
return mOnInitListener.test(activity, alreadyOnHome);
}
/**
* Registers the activity-created listener. If the activity is already created, then the
* callback provided in the constructor will be called synchronously.
*/
public void register() {
mIsRegistered = true;
mActivityTracker.registerCallback(this);
}
/**
* After calling this, we won't {@link #init} even when the activity is ready.
*/
public void unregister() {
mActivityTracker.unregisterCallback(this);
mIsRegistered = false;
mOnInitListener = null;
}
}
@@ -1,113 +0,0 @@
/*
* Copyright (C) 2022 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.quickstep.util;
import android.util.Log;
import androidx.annotation.Nullable;
import com.android.launcher3.BaseActivity;
import com.android.launcher3.util.ActivityTracker;
import com.android.launcher3.util.ActivityTracker.SchedulerCallback;
import java.util.function.BiPredicate;
/**
* Listener for activity initialized and/or destroyed.
*/
public class ActivityLifecycleListener<T extends BaseActivity> implements
SchedulerCallback<T> {
private static final String TAG = "ActivityLifecycleListener";
@Nullable private final BiPredicate<T, Boolean> mOnInitListener;
@Nullable private final Runnable mOnDestroyListener;
private final ActivityTracker<T> mActivityTracker;
private boolean mIsRegistered = false;
/**
* One or both of {@code onInitListener} and {@code onInitListener} must be provided, otherwise
* the created instance will effectively be a no-op.
*
* @param onInitListener a callback made when the activity is initialized. The callback should
* return true to continue receiving callbacks (ie. for if the activity is
* recreated).
* @param onDestroyListener a callback made when the activity is destroyed.
*/
public ActivityLifecycleListener(
@Nullable BiPredicate<T, Boolean> onInitListener,
@Nullable Runnable onDestroyListener,
ActivityTracker<T> tracker) {
if (onInitListener == null && onDestroyListener == null) {
throw new IllegalArgumentException("Both listeners cannot be null");
}
mOnInitListener = onInitListener;
mOnDestroyListener = onDestroyListener;
mActivityTracker = tracker;
}
@Override
public final boolean onActivityReady(T activity, boolean alreadyOnHome) {
if (!mIsRegistered) {
// Don't receive any more updates
return false;
}
return handleActivityReady(activity, alreadyOnHome);
}
protected boolean handleActivityReady(T activity, boolean alreadyOnHome) {
if (mOnInitListener == null) {
Log.e(TAG, "Cannot handle init: init listener is null", new Exception());
return false;
}
return mOnInitListener.test(activity, alreadyOnHome);
}
@Override
public void onActivityDestroyed() {
if (mOnDestroyListener == null) {
Log.e(TAG, "Cannot clean up: destroy listener is null", new Exception());
return;
}
mOnDestroyListener.run();
}
/**
* Registers the activity-created listener. If the activity is already created, then the
* callback provided in the constructor will be called synchronously.
*/
public void register() {
mIsRegistered = true;
mActivityTracker.registerCallback(this, getType());
}
/**
* After calling this, we won't call {@link #onActivityReady} even when the activity is ready.
*/
public void unregister() {
mActivityTracker.unregisterCallback(this, getType());
mIsRegistered = false;
}
private int getType() {
return mOnInitListener != null && mOnDestroyListener != null
? ActivityTracker.TYPE_BOTH
: (mOnInitListener != null
? ActivityTracker.TYPE_INIT
: ActivityTracker.TYPE_DESTROY);
}
}
@@ -74,7 +74,7 @@ public abstract class BaseItemDragListener implements View.OnDragListener, DragS
} }
@Override @Override
public boolean onActivityReady(Launcher launcher, boolean alreadyOnHome) { public boolean init(Launcher launcher, boolean alreadyOnHome) {
AbstractFloatingView.closeAllOpenViews(launcher, alreadyOnHome); AbstractFloatingView.closeAllOpenViews(launcher, alreadyOnHome);
launcher.getStateManager().goToState(NORMAL, alreadyOnHome /* animated */); launcher.getStateManager().goToState(NORMAL, alreadyOnHome /* animated */);
launcher.getDragLayer().setOnDragListener(this); launcher.getDragLayer().setOnDragListener(this);
@@ -72,8 +72,8 @@ public class PinItemDragListener extends BaseItemDragListener {
} }
@Override @Override
public boolean onActivityReady(Launcher launcher, boolean alreadyOnHome) { public boolean init(Launcher launcher, boolean alreadyOnHome) {
super.onActivityReady(launcher, alreadyOnHome); super.init(launcher, alreadyOnHome);
if (!alreadyOnHome) { if (!alreadyOnHome) {
launcher.useFadeOutAnimationForLauncherStart(mCancelSignal); launcher.useFadeOutAnimationForLauncherStart(mCancelSignal);
} }
@@ -20,6 +20,8 @@ import androidx.annotation.Nullable;
import com.android.launcher3.BaseActivity; import com.android.launcher3.BaseActivity;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
/** /**
@@ -28,15 +30,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
*/ */
public final class ActivityTracker<T extends BaseActivity> { 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 WeakReference<T> mCurrentActivity = new WeakReference<>(null);
private CopyOnWriteArrayList<SchedulerCallback<T>> mActivityReadyCallbacks = private CopyOnWriteArrayList<SchedulerCallback<T>> mCallbacks = new CopyOnWriteArrayList<>();
new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<SchedulerCallback<T>> mActivityDestroyedCallbacks =
new CopyOnWriteArrayList<>();
@Nullable @Nullable
public <R extends T> R getCreatedActivity() { public <R extends T> R getCreatedActivity() {
@@ -47,74 +42,32 @@ public final class ActivityTracker<T extends BaseActivity> {
if (mCurrentActivity.get() == activity) { if (mCurrentActivity.get() == activity) {
mCurrentActivity.clear(); 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) { 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(); T activity = mCurrentActivity.get();
if (type == TYPE_INIT || type == TYPE_BOTH) { mCallbacks.add(callback);
mActivityReadyCallbacks.add(callback); if (activity != null) {
if (activity != null) { if (!callback.init(activity, activity.isStarted())) {
if (!callback.onActivityReady(activity, activity.isStarted())) { unregisterCallback(callback);
unregisterCallback(callback, TYPE_INIT);
}
} }
} }
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. * Unregisters a registered callback.
*/ */
public void unregisterCallback(SchedulerCallback<T> callback, int type) { public void unregisterCallback(SchedulerCallback<T> callback) {
if (type == TYPE_INIT || type == TYPE_BOTH) { mCallbacks.remove(callback);
mActivityReadyCallbacks.remove(callback);
}
if (type == TYPE_DESTROY || type == TYPE_BOTH) {
mActivityDestroyedCallbacks.remove(callback);
}
} }
public boolean handleCreate(T activity) { public boolean handleCreate(T activity) {
@@ -128,8 +81,8 @@ public final class ActivityTracker<T extends BaseActivity> {
private boolean handleIntent(T activity, boolean alreadyOnHome) { private boolean handleIntent(T activity, boolean alreadyOnHome) {
boolean handled = false; boolean handled = false;
for (SchedulerCallback<T> cb : mActivityReadyCallbacks) { for (SchedulerCallback<T> cb : mCallbacks) {
if (!cb.onActivityReady(activity, alreadyOnHome)) { if (!cb.init(activity, alreadyOnHome)) {
// Callback doesn't want any more updates // Callback doesn't want any more updates
unregisterCallback(cb); unregisterCallback(cb);
} }
@@ -145,11 +98,6 @@ public final class ActivityTracker<T extends BaseActivity> {
* @param alreadyOnHome Whether the activity is already started. * @param alreadyOnHome Whether the activity is already started.
* @return Whether to continue receiving callbacks (i.e. if the activity is recreated). * @return Whether to continue receiving callbacks (i.e. if the activity is recreated).
*/ */
boolean onActivityReady(T activity, boolean alreadyOnHome); boolean init(T activity, boolean alreadyOnHome);
/**
* Called then the activity gets destroyed.
*/
default void onActivityDestroyed() { }
} }
} }