Skip creating config context if it is the same as current

If the launching app is fullscreen on default display, the task
config should be the same as current global config. Then it is
unnecessary to create a context again.

Use diffPublicOnly because task config may have specified windowing
mode, which won't affect resources resolving.

It saves up to 4ms when adding splash screen starting window.
Also simply the initialization a bit.

Bug: 187104264
Test: OpenAppMicrobenchmark
Change-Id: I2bc7e8dfba3b1d8ad501f6935080a86f2c45f032
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/StartingSurfaceDrawer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/StartingSurfaceDrawer.java
index 4273f89..6d8b471 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/StartingSurfaceDrawer.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/StartingSurfaceDrawer.java
@@ -17,7 +17,6 @@
 package com.android.wm.shell.startingsurface;
 
 import static android.content.Context.CONTEXT_RESTRICTED;
-import static android.content.res.Configuration.EMPTY;
 import static android.view.Choreographer.CALLBACK_INSETS_ANIMATION;
 import static android.view.Display.DEFAULT_DISPLAY;
 
@@ -54,7 +53,6 @@
 import com.android.wm.shell.common.ShellExecutor;
 import com.android.wm.shell.common.TransactionPool;
 
-import java.util.function.Consumer;
 import java.util.function.Supplier;
 
 /**
@@ -92,8 +90,6 @@
  * => makeSplashScreenContentView -> cachePaint(=AdaptiveIconDrawable#draw)
  * => WM#addView -> .. waiting for Choreographer#doFrame -> relayout -> draw -> (draw the Paint
  * directly).
- *
- * @hide
  */
 public class StartingSurfaceDrawer {
     static final String TAG = StartingSurfaceDrawer.class.getSimpleName();
@@ -200,7 +196,7 @@
         }
 
         final Configuration taskConfig = taskInfo.getConfiguration();
-        if (taskConfig != null && !taskConfig.equals(EMPTY)) {
+        if (taskConfig.diffPublicOnly(context.getResources().getConfiguration()) != 0) {
             if (DEBUG_SPLASH_SCREEN) {
                 Slog.d(TAG, "addSplashScreen: creating context based"
                         + " on task Configuration " + taskConfig + " for splash screen");
@@ -229,20 +225,11 @@
             typedArray.recycle();
         }
 
-        int windowFlags = 0;
-        windowFlags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
-
-        final boolean[] showWallpaper = new boolean[1];
-        getWindowResFromContext(context, a ->
-                showWallpaper[0] = a.getBoolean(R.styleable.Window_windowShowWallpaper, false));
-        if (showWallpaper[0]) {
-            windowFlags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
-        }
-
         final PhoneWindow win = new PhoneWindow(context);
         win.setIsStartingWindow(true);
 
-        CharSequence label = context.getResources().getText(labelRes, null);
+        final Resources res = context.getResources();
+        final CharSequence label = res.getText(labelRes, null);
         // Only change the accessibility title if the label is localized
         if (label != null) {
             win.setTitle(label, true);
@@ -250,7 +237,12 @@
             win.setTitle(nonLocalizedLabel, false);
         }
 
-        win.setType(WindowManager.LayoutParams.TYPE_APPLICATION_STARTING);
+        int windowFlags = WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
+        final TypedArray a = context.obtainStyledAttributes(R.styleable.Window);
+        if (a.getBoolean(R.styleable.Window_windowShowWallpaper, false)) {
+            windowFlags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
+        }
+        a.recycle();
 
         // Assumes it's safe to show starting windows of launched apps while
         // the keyguard is being hidden. This is okay because starting windows never show
@@ -264,24 +256,20 @@
         // touchable or focusable by the user.  We also add in the ALT_FOCUSABLE_IM
         // flag because we do know that the next window will take input
         // focus, so we want to get the IME window up on top of us right away.
-        win.setFlags(windowFlags
-                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
-                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
-                        | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
-                windowFlags
-                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
-                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
-                        | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
+        windowFlags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
+                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
+                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
+        win.setFlags(windowFlags, windowFlags);
 
         final int iconRes = activityInfo.getIconResource();
         final int logoRes = activityInfo.getLogoResource();
         win.setDefaultIcon(iconRes);
         win.setDefaultLogo(logoRes);
 
-        win.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
-                WindowManager.LayoutParams.MATCH_PARENT);
-
         final WindowManager.LayoutParams params = win.getAttributes();
+        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
+        params.width = WindowManager.LayoutParams.MATCH_PARENT;
+        params.height = WindowManager.LayoutParams.MATCH_PARENT;
         params.token = appToken;
         params.packageName = activityInfo.packageName;
         params.windowAnimations = win.getWindowStyle().getResourceId(
@@ -292,10 +280,7 @@
         params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY;
         params.format = PixelFormat.RGBA_8888;
 
-        final Resources res = context.getResources();
-        final boolean supportsScreen = res != null && (res.getCompatibilityInfo() != null
-                && res.getCompatibilityInfo().supportsScreen());
-        if (!supportsScreen) {
+        if (!res.getCompatibilityInfo().supportsScreen()) {
             params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
         }
 
@@ -340,7 +325,7 @@
 
         try {
             final View view = win.getDecorView();
-            final WindowManager wm = mContext.getSystemService(WindowManager.class);
+            final WindowManager wm = context.getSystemService(WindowManager.class);
             postAddWindow(taskId, appToken, view, wm, params);
 
             // We use the splash screen worker thread to create SplashScreenView while adding the
@@ -504,12 +489,6 @@
         }
     }
 
-    private void getWindowResFromContext(Context ctx, Consumer<TypedArray> consumer) {
-        final TypedArray a = ctx.obtainStyledAttributes(R.styleable.Window);
-        consumer.accept(a);
-        a.recycle();
-    }
-
     /**
      * Record the view or surface for a starting window.
      */