Remove mPendingCallback from ActivityTracker
Instead, all callers should use EXTRA_SCHEDULER_CALLBACK, set via addToIntent(), to provide a callback for the next handleIntent(). The updated runCallbackWhenActivityExists() will do that for the caller if the activity doesn't already exist (otherwise it will run the callback synchronously as before). Bug: 151389129 Change-Id: Idbec264354fd6de166ff3bae98249230078d5674
This commit is contained in:
1 parent
2e201a887d
commit
73ff6ddeac
11 files changed
+88
-124
No files matched your search
@@ -15,27 +15,23 @@
|
||||
*/
|
||||
package com.android.launcher3.util;
|
||||
|
||||
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.android.launcher3.BaseActivity;
|
||||
import com.android.launcher3.testing.TestProtocol;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/**
|
||||
* Helper class to statically track activity creation
|
||||
* @param <T> The activity type to track
|
||||
*/
|
||||
public final class ActivityTracker<T extends BaseActivity> implements Runnable {
|
||||
public final class ActivityTracker<T extends BaseActivity> {
|
||||
|
||||
private WeakReference<T> mCurrentActivity = new WeakReference<>(null);
|
||||
private WeakReference<SchedulerCallback<T>> mPendingCallback = new WeakReference<>(null);
|
||||
|
||||
private static final String EXTRA_SCHEDULER_CALLBACK = "launcher.scheduler_callback";
|
||||
|
||||
@@ -51,76 +47,32 @@ public final class ActivityTracker<T extends BaseActivity> implements Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules the callback to be notified when the activity is created.
|
||||
* @return true if the activity is already created, false otherwise
|
||||
* Call {@link SchedulerCallback#init(BaseActivity, boolean)} when the activity is ready.
|
||||
* If the activity is already created, this is called immediately, otherwise we add the
|
||||
* callback as an extra on the intent, and will call init() when we get handleIntent().
|
||||
* @param callback The callback to call init() on when the activity is ready.
|
||||
* @param intent The intent that will be used to initialize the activity, if the activity
|
||||
* doesn't already exist. We add the callback as an extra on this intent.
|
||||
*/
|
||||
public boolean schedule(SchedulerCallback<? extends T> callback) {
|
||||
synchronized (this) {
|
||||
mPendingCallback = new WeakReference<>((SchedulerCallback<T>) callback);
|
||||
}
|
||||
if (!notifyInitIfPending()) {
|
||||
// If the activity doesn't already exist, then post and wait for the activity to start
|
||||
MAIN_EXECUTOR.execute(this);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
notifyInitIfPending();
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies the pending callback if the activity is now created.
|
||||
* @return true if the activity is now created.
|
||||
*/
|
||||
private boolean notifyInitIfPending() {
|
||||
public void runCallbackWhenActivityExists(SchedulerCallback<T> callback, Intent intent) {
|
||||
T activity = mCurrentActivity.get();
|
||||
if (activity != null) {
|
||||
notifyInitIfPending(activity, activity.isStarted());
|
||||
return true;
|
||||
callback.init(activity, activity.isStarted());
|
||||
} else {
|
||||
callback.addToIntent(intent);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean notifyInitIfPending(T activity, boolean alreadyOnHome) {
|
||||
SchedulerCallback<T> pendingCallback = mPendingCallback.get();
|
||||
if (pendingCallback != null) {
|
||||
if (!pendingCallback.init(activity, alreadyOnHome)) {
|
||||
clearReference(pendingCallback);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean clearReference(SchedulerCallback<? extends T> handler) {
|
||||
synchronized (this) {
|
||||
if (mPendingCallback.get() == handler) {
|
||||
mPendingCallback.clear();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasPending() {
|
||||
return mPendingCallback.get() != null;
|
||||
}
|
||||
|
||||
public boolean handleCreate(T activity) {
|
||||
mCurrentActivity = new WeakReference<>(activity);
|
||||
return handleIntent(activity, activity.getIntent(), false, false);
|
||||
return handleIntent(activity, activity.getIntent(), false);
|
||||
}
|
||||
|
||||
public boolean handleNewIntent(T activity, Intent intent) {
|
||||
return handleIntent(activity, intent, activity.isStarted(), true);
|
||||
return handleIntent(activity, intent, activity.isStarted());
|
||||
}
|
||||
|
||||
private boolean handleIntent(
|
||||
T activity, Intent intent, boolean alreadyOnHome, boolean explicitIntent) {
|
||||
boolean result = false;
|
||||
private boolean handleIntent(T activity, Intent intent, boolean alreadyOnHome) {
|
||||
if (intent != null && intent.getExtras() != null) {
|
||||
IBinder stateBinder = intent.getExtras().getBinder(EXTRA_SCHEDULER_CALLBACK);
|
||||
if (stateBinder instanceof ObjectWrapper) {
|
||||
@@ -129,19 +81,26 @@ public final class ActivityTracker<T extends BaseActivity> implements Runnable {
|
||||
if (!handler.init(activity, alreadyOnHome)) {
|
||||
intent.getExtras().remove(EXTRA_SCHEDULER_CALLBACK);
|
||||
}
|
||||
result = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (!result && !explicitIntent) {
|
||||
result = notifyInitIfPending(activity, alreadyOnHome);
|
||||
}
|
||||
return result;
|
||||
return false;
|
||||
}
|
||||
|
||||
public interface SchedulerCallback<T extends BaseActivity> {
|
||||
|
||||
/**
|
||||
* Called when the activity is ready.
|
||||
* @param alreadyOnHome Whether the activity is already started.
|
||||
* @return Whether to continue receiving callbacks (i.e. if the activity is recreated).
|
||||
*/
|
||||
boolean init(T activity, boolean alreadyOnHome);
|
||||
|
||||
/**
|
||||
* Adds this callback as an extra on the intent, so we can retrieve it in handleIntent() and
|
||||
* call {@link #init}. The intent should be used to start the activity after calling this
|
||||
* method in order for us to get handleIntent().
|
||||
*/
|
||||
default Intent addToIntent(Intent intent) {
|
||||
Bundle extras = new Bundle();
|
||||
extras.putBinder(EXTRA_SCHEDULER_CALLBACK, ObjectWrapper.wrap(this));
|
||||
|
||||
Reference in new issue
Block a user