2019-09-26 17:05:31 -07:00
|
|
|
/*
|
|
|
|
|
* 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.launcher3.util;
|
|
|
|
|
|
2024-02-22 16:17:40 -08:00
|
|
|
import static com.android.launcher3.testing.shared.TestProtocol.GET_FROM_RECENTS_FAILURE;
|
|
|
|
|
import static com.android.launcher3.testing.shared.TestProtocol.testLogD;
|
|
|
|
|
|
2019-09-26 17:05:31 -07:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.BaseActivity;
|
|
|
|
|
|
|
|
|
|
import java.lang.ref.WeakReference;
|
2021-06-08 11:50:13 -07:00
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2019-09-26 17:05:31 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper class to statically track activity creation
|
2020-02-27 21:29:15 -08:00
|
|
|
* @param <T> The activity type to track
|
2019-09-26 17:05:31 -07:00
|
|
|
*/
|
2020-02-27 21:29:15 -08:00
|
|
|
public final class ActivityTracker<T extends BaseActivity> {
|
2019-09-26 17:05:31 -07:00
|
|
|
|
|
|
|
|
private WeakReference<T> mCurrentActivity = new WeakReference<>(null);
|
2022-12-07 19:45:55 +00:00
|
|
|
private CopyOnWriteArrayList<SchedulerCallback<T>> mCallbacks = new CopyOnWriteArrayList<>();
|
2019-09-26 17:05:31 -07:00
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
public <R extends T> R getCreatedActivity() {
|
|
|
|
|
return (R) mCurrentActivity.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onActivityDestroyed(T activity) {
|
|
|
|
|
if (mCurrentActivity.get() == activity) {
|
2024-02-22 16:17:40 -08:00
|
|
|
testLogD(GET_FROM_RECENTS_FAILURE,
|
|
|
|
|
String.format("ActivityTracker.onActivityDestroyed this=%s, activity=%s",
|
|
|
|
|
this, activity));
|
2019-09-26 17:05:31 -07:00
|
|
|
mCurrentActivity.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 16:17:56 -07:00
|
|
|
/**
|
2022-12-07 19:45:55 +00:00
|
|
|
* Call {@link SchedulerCallback#init(BaseActivity, boolean)} when the
|
|
|
|
|
* activity is ready. If the activity is already created, this is called immediately.
|
2021-06-08 11:50:13 -07:00
|
|
|
*
|
2022-12-07 19:45:55 +00:00
|
|
|
* 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.
|
2021-06-08 11:50:13 -07:00
|
|
|
*
|
2022-12-07 19:45:55 +00:00
|
|
|
* @param callback The callback to call init() on when the activity is ready.
|
2019-10-24 16:17:56 -07:00
|
|
|
*/
|
2022-12-07 19:45:55 +00:00
|
|
|
public void registerCallback(SchedulerCallback<T> callback) {
|
2019-09-26 17:05:31 -07:00
|
|
|
T activity = mCurrentActivity.get();
|
2022-12-07 19:45:55 +00:00
|
|
|
mCallbacks.add(callback);
|
|
|
|
|
if (activity != null) {
|
|
|
|
|
if (!callback.init(activity, activity.isStarted())) {
|
|
|
|
|
unregisterCallback(callback);
|
2021-06-08 11:50:13 -07:00
|
|
|
}
|
2019-09-26 17:05:31 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 14:22:20 -08:00
|
|
|
/**
|
|
|
|
|
* Unregisters a registered callback.
|
|
|
|
|
*/
|
2022-12-07 19:45:55 +00:00
|
|
|
public void unregisterCallback(SchedulerCallback<T> callback) {
|
|
|
|
|
mCallbacks.remove(callback);
|
2021-06-08 11:50:13 -07:00
|
|
|
}
|
|
|
|
|
|
2019-09-26 17:05:31 -07:00
|
|
|
public boolean handleCreate(T activity) {
|
2024-02-22 16:17:40 -08:00
|
|
|
testLogD(GET_FROM_RECENTS_FAILURE,
|
|
|
|
|
String.format("ActivityTracker.handleCreate this=%s, activity=%s", this, activity));
|
2019-09-26 17:05:31 -07:00
|
|
|
mCurrentActivity = new WeakReference<>(activity);
|
2021-06-08 11:50:13 -07:00
|
|
|
return handleIntent(activity, false /* alreadyOnHome */);
|
2019-09-26 17:05:31 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-08 11:50:13 -07:00
|
|
|
public boolean handleNewIntent(T activity) {
|
|
|
|
|
return handleIntent(activity, activity.isStarted());
|
2019-09-26 17:05:31 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-08 11:50:13 -07:00
|
|
|
private boolean handleIntent(T activity, boolean alreadyOnHome) {
|
|
|
|
|
boolean handled = false;
|
2022-12-07 19:45:55 +00:00
|
|
|
for (SchedulerCallback<T> cb : mCallbacks) {
|
|
|
|
|
if (!cb.init(activity, alreadyOnHome)) {
|
2021-06-08 11:50:13 -07:00
|
|
|
// Callback doesn't want any more updates
|
|
|
|
|
unregisterCallback(cb);
|
2019-09-26 17:05:31 -07:00
|
|
|
}
|
2021-06-08 11:50:13 -07:00
|
|
|
handled = true;
|
2019-09-26 17:05:31 -07:00
|
|
|
}
|
2021-06-08 11:50:13 -07:00
|
|
|
return handled;
|
2019-09-26 17:05:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface SchedulerCallback<T extends BaseActivity> {
|
|
|
|
|
|
2020-02-27 21:29:15 -08:00
|
|
|
/**
|
|
|
|
|
* 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).
|
|
|
|
|
*/
|
2022-12-07 19:45:55 +00:00
|
|
|
boolean init(T activity, boolean alreadyOnHome);
|
2019-09-26 17:05:31 -07:00
|
|
|
}
|
|
|
|
|
}
|