@@ -685,6 +685,7 @@ public static void moveStateWithSeparation(@NonNull NavigationScene navigationSc
685
685
break ;
686
686
case STARTED :
687
687
scene .dispatchResume ();
688
+ ((NavigationSceneManager )navigationScene .mNavigationSceneManager ).onSceneResumedWindowFocusChanged (scene );
688
689
moveStateWithSeparation (navigationScene , scene , to , bundle , causedByActivityLifeCycle , null , endAction );
689
690
break ;
690
691
default :
@@ -694,6 +695,7 @@ public static void moveStateWithSeparation(@NonNull NavigationScene navigationSc
694
695
switch (currentState ) {
695
696
case RESUMED :
696
697
scene .dispatchPause ();
698
+ ((NavigationSceneManager )navigationScene .mNavigationSceneManager ).onScenePausedWindowFocusChanged (scene );
697
699
moveStateWithSeparation (navigationScene , scene , to , bundle , causedByActivityLifeCycle , null , endAction );
698
700
break ;
699
701
case STARTED :
@@ -1970,4 +1972,97 @@ public void onConfigurationChanged(@NonNull Configuration newConfig) {
1970
1972
}
1971
1973
}
1972
1974
}
1975
+
1976
+ @ Override
1977
+ public void onWindowFocusChanged (boolean hasFocus ) {
1978
+ if (!this .mNavigationScene .mNavigationSceneOptions .getUseWindowFocusChangedDispatch ()) {
1979
+ return ;
1980
+ }
1981
+ LoggerManager .getInstance ().i (TAG , "onWindowFocusChanged " + hasFocus );
1982
+
1983
+ if (!mSceneMessageQueue .hasPendingTasks ()) {
1984
+ Scene scene = getCurrentScene ();
1985
+ if (ActivityCompatibleInfoCollector .isTargetSceneType (scene )) {
1986
+ onSceneResumedWindowFocusChangedToTarget (scene , hasFocus );
1987
+ } else {
1988
+ //try to uninstall window focus listener if there are no ActivityCompatibleBehavior Scene
1989
+ uninstallUselessWindowFocusChangeListener ();
1990
+ }
1991
+ } else {
1992
+ LoggerManager .getInstance ().i (TAG , "sync window focus by SceneMessageQueue" );
1993
+
1994
+ //sync focus after all pending tasks are finished
1995
+ NavigationRunnable syncWindowFocusRunnable = new NavigationRunnable () {
1996
+ @ Override
1997
+ public void run () {
1998
+ onWindowFocusChanged (hasFocus );
1999
+ }
2000
+ };
2001
+ mSceneMessageQueue .postAsync (syncWindowFocusRunnable );
2002
+ }
2003
+ }
2004
+
2005
+ private void uninstallUselessWindowFocusChangeListener () {
2006
+ List <Scene > sceneList = new ArrayList <>(getCurrentSceneList ());
2007
+ boolean found = false ;
2008
+ for (int i = sceneList .size () - 1 ; i >= 0 ; i --) {
2009
+ Scene targetScene = sceneList .get (i );
2010
+ if (ActivityCompatibleInfoCollector .isTargetSceneType (targetScene )) {
2011
+ found = true ;
2012
+ break ;
2013
+ }
2014
+ }
2015
+ if (!found ) {
2016
+ LoggerManager .getInstance ().i (TAG , "uninstall useless WindowFocusChangeListener" );
2017
+ mNavigationScene .uninstallWindowFocusChangeListenerIfNeeded ();
2018
+ }
2019
+ }
2020
+
2021
+ private void onSceneResumedWindowFocusChangedToTarget (Scene scene , boolean hasFocus ) {
2022
+ if (!this .mNavigationScene .mNavigationSceneOptions .getUseWindowFocusChangedDispatch ()) {
2023
+ return ;
2024
+ }
2025
+ if (!ActivityCompatibleInfoCollector .isTargetSceneType (scene )) {
2026
+ return ;
2027
+ }
2028
+ Record record = mNavigationScene .findRecordByScene (scene );
2029
+ if (record .mLastSceneWindowFocused == hasFocus ) {
2030
+ return ;
2031
+ }
2032
+ ActivityCompatibleBehavior activityCompatibleBehavior = (ActivityCompatibleBehavior ) scene ;
2033
+ activityCompatibleBehavior .onWindowFocusChanged (hasFocus );
2034
+ record .mLastSceneWindowFocused = hasFocus ;
2035
+ }
2036
+
2037
+ private void onSceneResumedWindowFocusChanged (Scene scene ) {
2038
+ if (!this .mNavigationScene .mNavigationSceneOptions .getUseWindowFocusChangedDispatch ()) {
2039
+ return ;
2040
+ }
2041
+ if (!ActivityCompatibleInfoCollector .isTargetSceneType (scene )) {
2042
+ return ;
2043
+ }
2044
+ if (scene .getState () != State .RESUMED ) {
2045
+ return ;
2046
+ }
2047
+ Activity activity = scene .requireActivity ();
2048
+ if (!activity .hasWindowFocus ()) {
2049
+ mNavigationScene .installWindowFocusChangeListenerIfNeeded ();
2050
+ } else {
2051
+ onSceneResumedWindowFocusChangedToTarget (scene , true );
2052
+ mNavigationScene .installWindowFocusChangeListenerIfNeeded ();
2053
+ }
2054
+ }
2055
+
2056
+ private void onScenePausedWindowFocusChanged (Scene scene ) {
2057
+ if (!this .mNavigationScene .mNavigationSceneOptions .getUseWindowFocusChangedDispatch ()) {
2058
+ return ;
2059
+ }
2060
+ if (!ActivityCompatibleInfoCollector .isTargetSceneType (scene )) {
2061
+ return ;
2062
+ }
2063
+ if (scene .getState () != State .STARTED ) {
2064
+ return ;
2065
+ }
2066
+ onSceneResumedWindowFocusChangedToTarget (scene , false );
2067
+ }
1973
2068
}
0 commit comments