mirror of
https://github.com/LawnchairLauncher/lawnchair.git
synced 2026-03-04 09:56:49 +00:00
Fixing race condition on test information provider
Activity tracker is accessed on a non-UI thread, which can cause a non-initialized Launcher to be treated as initialized Bug: 149022794 Change-Id: I6634a6aff891592369c16469bbe95a9ea611819c
This commit is contained in:
@@ -17,16 +17,22 @@ package com.android.launcher3.testing;
|
||||
|
||||
import static android.graphics.Bitmap.Config.ARGB_8888;
|
||||
|
||||
import static com.android.launcher3.allapps.AllAppsStore.DEFER_UPDATES_TEST;
|
||||
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Insets;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Debug;
|
||||
import android.system.Os;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.WindowInsets;
|
||||
|
||||
import androidx.annotation.Keep;
|
||||
|
||||
@@ -36,14 +42,19 @@ import com.android.launcher3.Launcher;
|
||||
import com.android.launcher3.LauncherAppState;
|
||||
import com.android.launcher3.LauncherState;
|
||||
import com.android.launcher3.R;
|
||||
import com.android.launcher3.allapps.AllAppsStore;
|
||||
import com.android.launcher3.util.ResourceBasedOverride;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Class to handle requests from tests
|
||||
*/
|
||||
@TargetApi(Build.VERSION_CODES.Q)
|
||||
public class TestInformationHandler implements ResourceBasedOverride {
|
||||
|
||||
public static TestInformationHandler newInstance(Context context) {
|
||||
@@ -54,7 +65,6 @@ public class TestInformationHandler implements ResourceBasedOverride {
|
||||
protected Context mContext;
|
||||
protected DeviceProfile mDeviceProfile;
|
||||
protected LauncherAppState mLauncherAppState;
|
||||
protected Launcher mLauncher;
|
||||
private static LinkedList mLeaks;
|
||||
|
||||
public void init(Context context) {
|
||||
@@ -62,35 +72,31 @@ public class TestInformationHandler implements ResourceBasedOverride {
|
||||
mDeviceProfile = InvariantDeviceProfile.INSTANCE.
|
||||
get(context).getDeviceProfile(context);
|
||||
mLauncherAppState = LauncherAppState.getInstanceNoCreate();
|
||||
mLauncher = Launcher.ACTIVITY_TRACKER.getCreatedActivity();
|
||||
}
|
||||
|
||||
public Bundle call(String method) {
|
||||
final Bundle response = new Bundle();
|
||||
switch (method) {
|
||||
case TestProtocol.REQUEST_ALL_APPS_TO_OVERVIEW_SWIPE_HEIGHT: {
|
||||
if (mLauncher == null) return null;
|
||||
|
||||
final float progress = LauncherState.OVERVIEW.getVerticalProgress(mLauncher)
|
||||
- LauncherState.ALL_APPS.getVerticalProgress(mLauncher);
|
||||
final float distance = mLauncher.getAllAppsController().getShiftRange() * progress;
|
||||
response.putInt(TestProtocol.TEST_INFO_RESPONSE_FIELD, (int) distance);
|
||||
break;
|
||||
return getLauncherUIProperty(Bundle::putInt, l -> {
|
||||
final float progress = LauncherState.OVERVIEW.getVerticalProgress(l)
|
||||
- LauncherState.ALL_APPS.getVerticalProgress(l);
|
||||
final float distance = l.getAllAppsController().getShiftRange() * progress;
|
||||
return (int) distance;
|
||||
});
|
||||
}
|
||||
|
||||
case TestProtocol.REQUEST_HOME_TO_ALL_APPS_SWIPE_HEIGHT: {
|
||||
if (mLauncher == null) return null;
|
||||
|
||||
final float progress = LauncherState.NORMAL.getVerticalProgress(mLauncher)
|
||||
- LauncherState.ALL_APPS.getVerticalProgress(mLauncher);
|
||||
final float distance = mLauncher.getAllAppsController().getShiftRange() * progress;
|
||||
response.putInt(TestProtocol.TEST_INFO_RESPONSE_FIELD, (int) distance);
|
||||
break;
|
||||
return getLauncherUIProperty(Bundle::putInt, l -> {
|
||||
final float progress = LauncherState.NORMAL.getVerticalProgress(l)
|
||||
- LauncherState.ALL_APPS.getVerticalProgress(l);
|
||||
final float distance = l.getAllAppsController().getShiftRange() * progress;
|
||||
return (int) distance;
|
||||
});
|
||||
}
|
||||
|
||||
case TestProtocol.REQUEST_IS_LAUNCHER_INITIALIZED: {
|
||||
response.putBoolean(TestProtocol.TEST_INFO_RESPONSE_FIELD, isLauncherInitialized());
|
||||
break;
|
||||
return getUIProperty(Bundle::putBoolean, t -> isLauncherInitialized(), () -> true);
|
||||
}
|
||||
|
||||
case TestProtocol.REQUEST_ENABLE_DEBUG_TRACING:
|
||||
@@ -102,40 +108,33 @@ public class TestInformationHandler implements ResourceBasedOverride {
|
||||
break;
|
||||
|
||||
case TestProtocol.REQUEST_FREEZE_APP_LIST:
|
||||
MAIN_EXECUTOR.execute(() ->
|
||||
mLauncher.getAppsView().getAppsStore().enableDeferUpdates(
|
||||
AllAppsStore.DEFER_UPDATES_TEST));
|
||||
break;
|
||||
|
||||
return getLauncherUIProperty(Bundle::putBoolean, l -> {
|
||||
l.getAppsView().getAppsStore().enableDeferUpdates(DEFER_UPDATES_TEST);
|
||||
return true;
|
||||
});
|
||||
case TestProtocol.REQUEST_UNFREEZE_APP_LIST:
|
||||
MAIN_EXECUTOR.execute(() ->
|
||||
mLauncher.getAppsView().getAppsStore().disableDeferUpdates(
|
||||
AllAppsStore.DEFER_UPDATES_TEST));
|
||||
break;
|
||||
return getLauncherUIProperty(Bundle::putBoolean, l -> {
|
||||
l.getAppsView().getAppsStore().disableDeferUpdates(DEFER_UPDATES_TEST);
|
||||
return true;
|
||||
});
|
||||
|
||||
case TestProtocol.REQUEST_APP_LIST_FREEZE_FLAGS: {
|
||||
try {
|
||||
final int deferUpdatesFlags = MAIN_EXECUTOR.submit(() ->
|
||||
mLauncher.getAppsView().getAppsStore().getDeferUpdatesFlags()).get();
|
||||
response.putInt(TestProtocol.TEST_INFO_RESPONSE_FIELD,
|
||||
deferUpdatesFlags);
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
break;
|
||||
return getLauncherUIProperty(Bundle::putInt,
|
||||
l -> l.getAppsView().getAppsStore().getDeferUpdatesFlags());
|
||||
}
|
||||
|
||||
case TestProtocol.REQUEST_APPS_LIST_SCROLL_Y: {
|
||||
try {
|
||||
final int scroll = MAIN_EXECUTOR.submit(() ->
|
||||
mLauncher.getAppsView().getActiveRecyclerView().getCurrentScrollY())
|
||||
.get();
|
||||
response.putInt(TestProtocol.TEST_INFO_RESPONSE_FIELD,
|
||||
scroll);
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
break;
|
||||
return getLauncherUIProperty(Bundle::putInt,
|
||||
l -> l.getAppsView().getActiveRecyclerView().getCurrentScrollY());
|
||||
}
|
||||
|
||||
case TestProtocol.REQUEST_WINDOW_INSETS: {
|
||||
return getUIProperty(Bundle::putParcelable, a -> {
|
||||
WindowInsets insets = a.getWindow()
|
||||
.getDecorView().getRootWindowInsets();
|
||||
return Insets.max(
|
||||
insets.getSystemGestureInsets(), insets.getSystemWindowInsets());
|
||||
}, this::getCurrentActivity);
|
||||
}
|
||||
|
||||
case TestProtocol.REQUEST_PID: {
|
||||
@@ -176,7 +175,6 @@ public class TestInformationHandler implements ResourceBasedOverride {
|
||||
|
||||
case TestProtocol.REQUEST_VIEW_LEAK: {
|
||||
if (mLeaks == null) mLeaks = new LinkedList();
|
||||
|
||||
mLeaks.add(new View(mContext));
|
||||
break;
|
||||
}
|
||||
@@ -200,6 +198,10 @@ public class TestInformationHandler implements ResourceBasedOverride {
|
||||
|| LauncherAppState.getInstance(mContext).getModel().isModelLoaded();
|
||||
}
|
||||
|
||||
protected Activity getCurrentActivity() {
|
||||
return Launcher.ACTIVITY_TRACKER.getCreatedActivity();
|
||||
}
|
||||
|
||||
private static void runGcAndFinalizersSync() {
|
||||
Runtime.getRuntime().gc();
|
||||
Runtime.getRuntime().runFinalization();
|
||||
@@ -216,6 +218,47 @@ public class TestInformationHandler implements ResourceBasedOverride {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result by getting a Launcher property on UI thread
|
||||
*/
|
||||
public static <T> Bundle getLauncherUIProperty(
|
||||
BundleSetter<T> bundleSetter, Function<Launcher, T> provider) {
|
||||
return getUIProperty(bundleSetter, provider, Launcher.ACTIVITY_TRACKER::getCreatedActivity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result by getting a generic property on UI thread
|
||||
*/
|
||||
private static <S, T> Bundle getUIProperty(
|
||||
BundleSetter<T> bundleSetter, Function<S, T> provider, Supplier<S> targetSupplier) {
|
||||
try {
|
||||
return MAIN_EXECUTOR.submit(() -> {
|
||||
S target = targetSupplier.get();
|
||||
if (target == null) {
|
||||
return null;
|
||||
}
|
||||
T value = provider.apply(target);
|
||||
Bundle response = new Bundle();
|
||||
bundleSetter.set(response, TestProtocol.TEST_INFO_RESPONSE_FIELD, value);
|
||||
return response;
|
||||
}).get();
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic interface for setting a fiend in bundle
|
||||
* @param <T> the type of value being set
|
||||
*/
|
||||
public interface BundleSetter<T> {
|
||||
|
||||
/**
|
||||
* Sets any generic property to the bundle
|
||||
*/
|
||||
void set(Bundle b, String key, T value);
|
||||
}
|
||||
|
||||
// Create the observer in the scope of a method to minimize the chance that
|
||||
// it remains live in a DEX/machine register at the point of the fence guard.
|
||||
// This must be kept to avoid R8 inlining it.
|
||||
|
||||
Reference in New Issue
Block a user