Merge "Use window manager global to add/remove starting window." into sc-v2-dev

This commit is contained in:
Wei Sheng Shih
2021-08-11 01:38:26 +00:00
committed by Android (Google) Code Review
3 changed files with 20 additions and 33 deletions

View File

@@ -360,7 +360,7 @@ public class SplashscreenContentDrawer {
createIconDrawable(iconDrawable, false); createIconDrawable(iconDrawable, false);
} else { } else {
final float iconScale = (float) mIconSize / (float) mDefaultIconSize; final float iconScale = (float) mIconSize / (float) mDefaultIconSize;
final int densityDpi = mContext.getResources().getDisplayMetrics().densityDpi; final int densityDpi = mContext.getResources().getConfiguration().densityDpi;
final int scaledIconDpi = final int scaledIconDpi =
(int) (0.5f + iconScale * densityDpi * NO_BACKGROUND_SCALE); (int) (0.5f + iconScale * densityDpi * NO_BACKGROUND_SCALE);
Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "getIcon"); Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "getIcon");

View File

@@ -52,6 +52,7 @@ import android.view.SurfaceControl;
import android.view.SurfaceControlViewHost; import android.view.SurfaceControlViewHost;
import android.view.View; import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.WindowManagerGlobal;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.window.SplashScreenView; import android.window.SplashScreenView;
import android.window.SplashScreenView.SplashScreenViewParcelable; import android.window.SplashScreenView.SplashScreenViewParcelable;
@@ -115,6 +116,7 @@ public class StartingSurfaceDrawer {
@VisibleForTesting @VisibleForTesting
final SplashscreenContentDrawer mSplashscreenContentDrawer; final SplashscreenContentDrawer mSplashscreenContentDrawer;
private Choreographer mChoreographer; private Choreographer mChoreographer;
private final WindowManagerGlobal mWindowManagerGlobal;
/** /**
* @param splashScreenExecutor The thread used to control add and remove starting window. * @param splashScreenExecutor The thread used to control add and remove starting window.
@@ -126,6 +128,8 @@ public class StartingSurfaceDrawer {
mSplashScreenExecutor = splashScreenExecutor; mSplashScreenExecutor = splashScreenExecutor;
mSplashscreenContentDrawer = new SplashscreenContentDrawer(mContext, pool); mSplashscreenContentDrawer = new SplashscreenContentDrawer(mContext, pool);
mSplashScreenExecutor.execute(() -> mChoreographer = Choreographer.getInstance()); mSplashScreenExecutor.execute(() -> mChoreographer = Choreographer.getInstance());
mWindowManagerGlobal = WindowManagerGlobal.getInstance();
mDisplayManager.getDisplay(DEFAULT_DISPLAY);
} }
private final SparseArray<StartingWindowRecord> mStartingWindowRecords = new SparseArray<>(); private final SparseArray<StartingWindowRecord> mStartingWindowRecords = new SparseArray<>();
@@ -137,21 +141,8 @@ public class StartingSurfaceDrawer {
private final SparseArray<SurfaceControlViewHost> mAnimatedSplashScreenSurfaceHosts = private final SparseArray<SurfaceControlViewHost> mAnimatedSplashScreenSurfaceHosts =
new SparseArray<>(1); new SparseArray<>(1);
/** Obtain proper context for showing splash screen on the provided display. */ private Display getDisplay(int displayId) {
private Context getDisplayContext(Context context, int displayId) { return mDisplayManager.getDisplay(displayId);
if (displayId == DEFAULT_DISPLAY) {
// The default context fits.
return context;
}
final Display targetDisplay = mDisplayManager.getDisplay(displayId);
if (targetDisplay == null) {
// Failed to obtain the non-default display where splash screen should be shown,
// lets not show at all.
return null;
}
return context.createDisplayContext(targetDisplay);
} }
private int getSplashScreenTheme(int splashScreenThemeResId, ActivityInfo activityInfo) { private int getSplashScreenTheme(int splashScreenThemeResId, ActivityInfo activityInfo) {
@@ -186,13 +177,11 @@ public class StartingSurfaceDrawer {
+ " suggestType=" + suggestType); + " suggestType=" + suggestType);
} }
// Obtain proper context to launch on the right display. final Display display = getDisplay(displayId);
final Context displayContext = getDisplayContext(context, displayId); if (display == null) {
if (displayContext == null) {
// Can't show splash screen on requested display, so skip showing at all. // Can't show splash screen on requested display, so skip showing at all.
return; return;
} }
context = displayContext;
if (theme != context.getThemeResId()) { if (theme != context.getThemeResId()) {
try { try {
context = context.createPackageContextAsUser(activityInfo.packageName, context = context.createPackageContextAsUser(activityInfo.packageName,
@@ -330,10 +319,8 @@ public class StartingSurfaceDrawer {
}; };
mSplashscreenContentDrawer.createContentView(context, suggestType, activityInfo, taskId, mSplashscreenContentDrawer.createContentView(context, suggestType, activityInfo, taskId,
viewSupplier::setView); viewSupplier::setView);
try { try {
final WindowManager wm = context.getSystemService(WindowManager.class); if (addWindow(taskId, appToken, rootLayout, display, params, suggestType)) {
if (addWindow(taskId, appToken, rootLayout, wm, params, suggestType)) {
// We use the splash screen worker thread to create SplashScreenView while adding // We use the splash screen worker thread to create SplashScreenView while adding
// the window, as otherwise Choreographer#doFrame might be delayed on this thread. // the window, as otherwise Choreographer#doFrame might be delayed on this thread.
// And since Choreographer#doFrame won't happen immediately after adding the window, // And since Choreographer#doFrame won't happen immediately after adding the window,
@@ -508,12 +495,14 @@ public class StartingSurfaceDrawer {
viewHost.getView().post(viewHost::release); viewHost.getView().post(viewHost::release);
} }
protected boolean addWindow(int taskId, IBinder appToken, View view, WindowManager wm, protected boolean addWindow(int taskId, IBinder appToken, View view, Display display,
WindowManager.LayoutParams params, @StartingWindowType int suggestType) { WindowManager.LayoutParams params, @StartingWindowType int suggestType) {
boolean shouldSaveView = true; boolean shouldSaveView = true;
final Context context = view.getContext();
try { try {
Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "addRootView"); Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "addRootView");
wm.addView(view, params); mWindowManagerGlobal.addView(view, params, display,
null /* parentWindow */, context.getUserId());
} catch (WindowManager.BadTokenException e) { } catch (WindowManager.BadTokenException e) {
// ignore // ignore
Slog.w(TAG, appToken + " already running, starting window not displayed. " Slog.w(TAG, appToken + " already running, starting window not displayed. "
@@ -521,9 +510,9 @@ public class StartingSurfaceDrawer {
shouldSaveView = false; shouldSaveView = false;
} finally { } finally {
Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER); Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
if (view != null && view.getParent() == null) { if (view.getParent() == null) {
Slog.w(TAG, "view not successfully added to wm, removing view"); Slog.w(TAG, "view not successfully added to wm, removing view");
wm.removeViewImmediate(view); mWindowManagerGlobal.removeView(view, true /* immediate */);
shouldSaveView = false; shouldSaveView = false;
} }
} }
@@ -587,10 +576,7 @@ public class StartingSurfaceDrawer {
if (hideView) { if (hideView) {
decorView.setVisibility(View.GONE); decorView.setVisibility(View.GONE);
} }
final WindowManager wm = decorView.getContext().getSystemService(WindowManager.class); mWindowManagerGlobal.removeView(decorView, false /* immediate */);
if (wm != null) {
wm.removeView(decorView);
}
} }
/** /**

View File

@@ -42,6 +42,7 @@ import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
import android.os.UserHandle; import android.os.UserHandle;
import android.testing.TestableContext; import android.testing.TestableContext;
import android.view.Display;
import android.view.SurfaceControl; import android.view.SurfaceControl;
import android.view.View; import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
@@ -92,8 +93,8 @@ public class StartingSurfaceDrawerTests {
} }
@Override @Override
protected boolean addWindow(int taskId, IBinder appToken, protected boolean addWindow(int taskId, IBinder appToken, View view, Display display,
View view, WindowManager wm, WindowManager.LayoutParams params, int suggestType) { WindowManager.LayoutParams params, int suggestType) {
// listen for addView // listen for addView
mAddWindowForTask = taskId; mAddWindowForTask = taskId;
mViewThemeResId = view.getContext().getThemeResId(); mViewThemeResId = view.getContext().getThemeResId();