美文网首页
冷启动白屏分析(StartingWindow)

冷启动白屏分析(StartingWindow)

作者: 馒Care | 来源:发表于2023-11-27 21:24 被阅读0次

    背景:项目中需要根据配置文件,动态修改Theme设置的windowBackground资源

    很不错的文章分享

    结论:不行,搞不定,源码基于SDK29 分析
    源码分析:
    Zygote启动的时候,通过ActivityStart启动Activity,调用startActivity,中间省略一堆代码。最终调用
    mTargetStack.startActivityLocked(mStartActivity, topFocused, newTask, mKeepCurTransition,
                    mOptions);
    

    我们查看ActivityStack#startActivityLocked

     if (r.mLaunchTaskBehind) {
                    // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
                    // tell WindowManager that r is visible even though it is at the back of the stack.
                    r.setVisibility(true);
                    ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
                } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
                    // Figure out if we are transitioning from another activity that is
                    // "has the same starting icon" as the next one.  This allows the
                    // window manager to keep the previous window it had previously
                    // created, if it still had one.
                    TaskRecord prevTask = r.getTaskRecord();
                    ActivityRecord prev = prevTask.topRunningActivityWithStartingWindowLocked();
                    if (prev != null) {
                  
                        // We don't want to reuse the previous starting preview if:
                        // (1) The current activity is in a different task.
                        if (prev.getTaskRecord() != prevTask) {
                            prev = null;
                        }
                        // (2) The current activity is already displayed.
                        else if (prev.nowVisible) {
                            prev = null;
                        }
                    }
                    r.showStartingWindow(prev, newTask, isTaskSwitch(r, focusedTopActivity));
                }
    

    分析showStartingWindow 如下,代码在ActivityRecord#showStartingWindow

     void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch,
                boolean fromRecents) {
            if (mAppWindowToken == null) {
                return;
            }
            if (mTaskOverlay) {
                // We don't show starting window for overlay activities.
                return;
            }
            if (pendingOptions != null
                    && pendingOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
                // Don't show starting window when using shared element transition.
                return;
            }
    
            final CompatibilityInfo compatInfo =
                    mAtmService.compatibilityInfoForPackageLocked(info.applicationInfo);
            final boolean shown = addStartingWindow(packageName, theme,
                    compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
                    prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning(),
                    allowTaskSnapshot(),
                    mState.ordinal() >= RESUMED.ordinal() && mState.ordinal() <= STOPPED.ordinal(),
                    fromRecents);
            if (shown) {
                mStartingWindowState = STARTING_WINDOW_SHOWN;
            }
        }
    

    我们可以看到这里创建了一个StartingWindow,也就是冷启动的时候,看到的那个白屏的玩意儿,那么这里做了啥?我们继续分析

    查看源码可以知道,AppWindowToken#addStartingWindow,做了一系列的判断,例如是否是透明主题,是否是弹窗等。为了方便跟踪结论,直接看关键点。

    WindowManagerPolicy.StartingSurface surface = null;
                try {
                    surface = startingData.createStartingSurface(AppWindowToken.this);
                } catch (Exception e) {
                    Slog.w(TAG, "Exception when adding starting window", e);
                }
    
    • 这里创建了一个surface
    @Override
        StartingSurface createStartingSurface(AppWindowToken atoken) {
            return mService.mPolicy.addSplashScreen(atoken.token, mPkg, mTheme, mCompatInfo,
                    mNonLocalizedLabel, mLabelRes, mIcon, mLogo, mWindowFlags,
                    mMergedOverrideConfiguration, atoken.getDisplayContent().getDisplayId());
        }
    
    • 很熟悉的感觉,添加了一个引导页的页面。最终跟踪到PhoneWindowManager#addSplashScreen
    
    public StartingSurface addSplashScreen(IBinder appToken, String packageName, int theme,
                CompatibilityInfo compatInfo, CharSequence nonLocalizedLabel, int labelRes, int icon,
                int logo, int windowFlags, Configuration overrideConfig, int displayId) {
            if (!SHOW_SPLASH_SCREENS) {
                return null;
            }
            if (packageName == null) {
                return null;
            }
    
            WindowManager wm = null;
            View view = null;
    
            try {
                Context context = mContext;
                if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "addSplashScreen " + packageName
                        + ": nonLocalizedLabel=" + nonLocalizedLabel + " theme="
                        + Integer.toHexString(theme));
    
                // Obtain proper context to launch on the right display.
                final Context displayContext = getDisplayContext(context, displayId);
                if (displayContext == null) {
                    // Can't show splash screen on requested display, so skip showing at all.
                    return null;
                }
                context = displayContext;
    
                if (theme != context.getThemeResId() || labelRes != 0) {
                    try {
                        context = context.createPackageContext(packageName, CONTEXT_RESTRICTED);
                        context.setTheme(theme);
                    } catch (PackageManager.NameNotFoundException e) {
                        // Ignore
                    }
                }
    
                if (overrideConfig != null && !overrideConfig.equals(EMPTY)) {
                    if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "addSplashScreen: creating context based"
                            + " on overrideConfig" + overrideConfig + " for splash screen");
                    final Context overrideContext = context.createConfigurationContext(overrideConfig);
                    overrideContext.setTheme(theme);
                    final TypedArray typedArray = overrideContext.obtainStyledAttributes(
                            com.android.internal.R.styleable.Window);
                    final int resId = typedArray.getResourceId(R.styleable.Window_windowBackground, 0);
                    if (resId != 0 && overrideContext.getDrawable(resId) != null) {
                        // We want to use the windowBackground for the override context if it is
                        // available, otherwise we use the default one to make sure a themed starting
                        // window is displayed for the app.
                        if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "addSplashScreen: apply overrideConfig"
                                + overrideConfig + " to starting window resId=" + resId);
                        context = overrideContext;
                    }
                    typedArray.recycle();
                }
    
                final PhoneWindow win = new PhoneWindow(context);
                win.setIsStartingWindow(true);
    
                CharSequence label = context.getResources().getText(labelRes, null);
                // Only change the accessibility title if the label is localized
                if (label != null) {
                    win.setTitle(label, true);
                } else {
                    win.setTitle(nonLocalizedLabel, false);
                }
    
                win.setType(
                    WindowManager.LayoutParams.TYPE_APPLICATION_STARTING);
    
                synchronized (mWindowManagerFuncs.getWindowManagerLock()) {
                    // 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
                    // secret information.
                    if (mKeyguardOccluded) {
                        windowFlags |= FLAG_SHOW_WHEN_LOCKED;
                    }
                }
    
                // Force the window flags: this is a fake window, so it is not really
                // 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);
    
                win.setDefaultIcon(icon);
                win.setDefaultLogo(logo);
    
                win.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.MATCH_PARENT);
    
                final WindowManager.LayoutParams params = win.getAttributes();
                params.token = appToken;
                params.packageName = packageName;
                params.windowAnimations = win.getWindowStyle().getResourceId(
                        com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
                params.privateFlags |=
                        WindowManager.LayoutParams.PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED;
                params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
    
                if (!compatInfo.supportsScreen()) {
                    params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
                }
    
                params.setTitle("Splash Screen " + packageName);
                addSplashscreenContent(win, context);
    
                wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
                view = win.getDecorView();
    
                if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "Adding splash screen window for "
                    + packageName + " / " + appToken + ": " + (view.getParent() != null ? view : null));
    
                wm.addView(view, params);
    
                // Only return the view if it was successfully added to the
                // window manager... which we can tell by it having a parent.
                return view.getParent() != null ? new SplashScreenSurface(view, appToken) : null;
            } catch (WindowManager.BadTokenException e) {
                // ignore
                Log.w(TAG, appToken + " already running, starting window not displayed. " +
                        e.getMessage());
            } catch (RuntimeException e) {
                // don't crash if something else bad happens, for example a
                // failure loading resources because we are loading from an app
                // on external storage that has been unmounted.
                Log.w(TAG, appToken + " failed creating starting window", e);
            } finally {
                if (view != null && view.getParent() == null) {
                    Log.w(TAG, "view not successfully added to wm, removing view");
                    wm.removeViewImmediate(view);
                }
            }
    
            return null;
        }
    

    关键的点来了

                wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
                view = win.getDecorView();
    
                if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "Adding splash screen window for "
                    + packageName + " / " + appToken + ": " + (view.getParent() != null ? view : null));
    
                wm.addView(view, params);
    
    
    • 这里通过WindowManager获取到了DecorView,我们继续跟踪代码,查看PhoneWindow#getDecorView
     @Override
        public final @NonNull View getDecorView() {
            if (mDecor == null || mForceDecorInstall) {
                installDecor();
            }
            return mDecor;
        }
    

    又是很熟悉的感觉,这里执行了installDecor*

    private void installDecor() {
            mForceDecorInstall = false;
            if (mDecor == null) {
                mDecor = generateDecor(-1);
                mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
                mDecor.setIsRootNamespace(true);
                if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
                    mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
                }
            } else {
                mDecor.setWindow(this);
            }
            if (mContentParent == null) {
                mContentParent = generateLayout(mDecor);
    
                // Set up decor part of UI to ignore fitsSystemWindows if appropriate.
                mDecor.makeOptionalFitsSystemWindows();
    
                final DecorContentParent decorContentParent = (DecorContentParent) mDecor.findViewById(
                        R.id.decor_content_parent);
    
                if (decorContentParent != null) {
                    mDecorContentParent = decorContentParent;
                    mDecorContentParent.setWindowCallback(getCallback());
                    if (mDecorContentParent.getTitle() == null) {
                        mDecorContentParent.setWindowTitle(mTitle);
                    }
    
                    final int localFeatures = getLocalFeatures();
                    for (int i = 0; i < FEATURE_MAX; i++) {
                        if ((localFeatures & (1 << i)) != 0) {
                            mDecorContentParent.initFeature(i);
                        }
                    }
    
                    mDecorContentParent.setUiOptions(mUiOptions);
    
                    if ((mResourcesSetFlags & FLAG_RESOURCE_SET_ICON) != 0 ||
                            (mIconRes != 0 && !mDecorContentParent.hasIcon())) {
                        mDecorContentParent.setIcon(mIconRes);
                    } else if ((mResourcesSetFlags & FLAG_RESOURCE_SET_ICON) == 0 &&
                            mIconRes == 0 && !mDecorContentParent.hasIcon()) {
                        mDecorContentParent.setIcon(
                                getContext().getPackageManager().getDefaultActivityIcon());
                        mResourcesSetFlags |= FLAG_RESOURCE_SET_ICON_FALLBACK;
                    }
                    if ((mResourcesSetFlags & FLAG_RESOURCE_SET_LOGO) != 0 ||
                            (mLogoRes != 0 && !mDecorContentParent.hasLogo())) {
                        mDecorContentParent.setLogo(mLogoRes);
                    }
    
                    // Invalidate if the panel menu hasn't been created before this.
                    // Panel menu invalidation is deferred avoiding application onCreateOptionsMenu
                    // being called in the middle of onCreate or similar.
                    // A pending invalidation will typically be resolved before the posted message
                    // would run normally in order to satisfy instance state restoration.
                    PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
                    if (!isDestroyed() && (st == null || st.menu == null) && !mIsStartingWindow) {
                        invalidatePanelMenu(FEATURE_ACTION_BAR);
                    }
                } else {
                    mTitleView = findViewById(R.id.title);
                    if (mTitleView != null) {
                        if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {
                            final View titleContainer = findViewById(R.id.title_container);
                            if (titleContainer != null) {
                                titleContainer.setVisibility(View.GONE);
                            } else {
                                mTitleView.setVisibility(View.GONE);
                            }
                            mContentParent.setForeground(null);
                        } else {
                            mTitleView.setText(mTitle);
                        }
                    }
                }
    
                if (mDecor.getBackground() == null && mBackgroundFallbackDrawable != null) {
                    mDecor.setBackgroundFallback(mBackgroundFallbackDrawable);
                }
    
                // Only inflate or create a new TransitionManager if the caller hasn't
                // already set a custom one.
                if (hasFeature(FEATURE_ACTIVITY_TRANSITIONS)) {
                    if (mTransitionManager == null) {
                        final int transitionRes = getWindowStyle().getResourceId(
                                R.styleable.Window_windowContentTransitionManager,
                                0);
                        if (transitionRes != 0) {
                            final TransitionInflater inflater = TransitionInflater.from(getContext());
                            mTransitionManager = inflater.inflateTransitionManager(transitionRes,
                                    mContentParent);
                        } else {
                            mTransitionManager = new TransitionManager();
                        }
                    }
    
                    mEnterTransition = getTransition(mEnterTransition, null,
                            R.styleable.Window_windowEnterTransition);
                    mReturnTransition = getTransition(mReturnTransition, USE_DEFAULT_TRANSITION,
                            R.styleable.Window_windowReturnTransition);
                    mExitTransition = getTransition(mExitTransition, null,
                            R.styleable.Window_windowExitTransition);
                    mReenterTransition = getTransition(mReenterTransition, USE_DEFAULT_TRANSITION,
                            R.styleable.Window_windowReenterTransition);
                    mSharedElementEnterTransition = getTransition(mSharedElementEnterTransition, null,
                            R.styleable.Window_windowSharedElementEnterTransition);
                    mSharedElementReturnTransition = getTransition(mSharedElementReturnTransition,
                            USE_DEFAULT_TRANSITION,
                            R.styleable.Window_windowSharedElementReturnTransition);
                    mSharedElementExitTransition = getTransition(mSharedElementExitTransition, null,
                            R.styleable.Window_windowSharedElementExitTransition);
                    mSharedElementReenterTransition = getTransition(mSharedElementReenterTransition,
                            USE_DEFAULT_TRANSITION,
                            R.styleable.Window_windowSharedElementReenterTransition);
                    if (mAllowEnterTransitionOverlap == null) {
                        mAllowEnterTransitionOverlap = getWindowStyle().getBoolean(
                                R.styleable.Window_windowAllowEnterTransitionOverlap, true);
                    }
                    if (mAllowReturnTransitionOverlap == null) {
                        mAllowReturnTransitionOverlap = getWindowStyle().getBoolean(
                                R.styleable.Window_windowAllowReturnTransitionOverlap, true);
                    }
                    if (mBackgroundFadeDurationMillis < 0) {
                        mBackgroundFadeDurationMillis = getWindowStyle().getInteger(
                                R.styleable.Window_windowTransitionBackgroundFadeDuration,
                                DEFAULT_BACKGROUND_FADE_DURATION_MS);
                    }
                    if (mSharedElementsUseOverlay == null) {
                        mSharedElementsUseOverlay = getWindowStyle().getBoolean(
                                R.styleable.Window_windowSharedElementsUseOverlay, true);
                    }
                }
            }
        }
    
    • 最终分析,看关键代码
     mContentParent = generateLayout(mDecor);
    

    这里生成了一个layout

    if (getContainer() == null) {
                if (mBackgroundDrawable == null) {
                   if (a.hasValue(R.styleable.Window_windowBackground)) {
                        mBackgroundDrawable = a.getDrawable(R.styleable.Window_windowBackground);
                    }
                }
              
            }
    
    if (getContainer() == null) {
                mDecor.setWindowBackground(mBackgroundDrawable);
    
    • 最终获取到了windowBackground,这个就是我们通过themes.xml设置的主题背景了
     <!--启动屏主题-->
        <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
     
            <item name="android:windowBackground">@drawable/ic_default_avatar_user</item>
         
        </style>
    
    • 一系列的操作后,我们最终的结论就是,App启动的时候,会帮我们创建一个Window。这个Window是过渡引导使用的。并不是真正的引导页Activity,谷歌的目的就是为了平滑过渡到引导页,不至于,启动白屏/黑屏。或者卡顿。如果想要测试一下,避免卡顿,可以尝试修改主题如下
     <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowIsTranslucent">true</item> 
      <item name="android:windowBackground">@color/transparent</item>
        </style>
    
    • 当设置windowIsTranslucent 为透明的时候,系统不会为我们创建StartingWindow。所以,当我们点击桌面图标后,会明显的卡顿一定时间后,直接进入SplashActivity。

    • 最最终结论,为什么说不能根据配置修改Theme的windowBackground呢。因为系统启动的时候,会自动创建StartingWindow,并且去读取theme的配置,这个动作我们无法感知,所以,也就没有办法去修改,当然,如果强制去修改,也不是不行。但是会出现启动的时候,先看到背景A(StartingWindow),再看到背景B(SplashActivity)

    相关文章

      网友评论

          本文标题:冷启动白屏分析(StartingWindow)

          本文链接:https://www.haomeiwen.com/subject/eghtgdtx.html