Merge "Fix theme differences between left/right pane problem" into main
diff --git a/Android.bp b/Android.bp
index e27b9a1..d34aaac 100644
--- a/Android.bp
+++ b/Android.bp
@@ -74,6 +74,7 @@
         "androidx.preference_preference",
         "androidx.recyclerview_recyclerview",
         "androidx.window_window",
+        "androidx.window_window-java",
         "com.google.android.material_material",
         "setupcompat",
         "setupdesign",
diff --git a/src/com/android/settings/homepage/SettingsHomepageActivity.java b/src/com/android/settings/homepage/SettingsHomepageActivity.java
index 533fbe7..d96ec39 100644
--- a/src/com/android/settings/homepage/SettingsHomepageActivity.java
+++ b/src/com/android/settings/homepage/SettingsHomepageActivity.java
@@ -50,6 +50,7 @@
 
 import androidx.annotation.VisibleForTesting;
 import androidx.core.graphics.Insets;
+import androidx.core.util.Consumer;
 import androidx.core.view.ViewCompat;
 import androidx.core.view.WindowCompat;
 import androidx.core.view.WindowInsetsCompat;
@@ -57,7 +58,10 @@
 import androidx.fragment.app.FragmentActivity;
 import androidx.fragment.app.FragmentManager;
 import androidx.fragment.app.FragmentTransaction;
+import androidx.window.embedding.SplitController;
+import androidx.window.embedding.SplitInfo;
 import androidx.window.embedding.SplitRule;
+import androidx.window.java.embedding.SplitControllerCallbackAdapter;
 
 import com.android.settings.R;
 import com.android.settings.Settings;
@@ -77,6 +81,7 @@
 import com.google.android.setupcompat.util.WizardManagerHelper;
 
 import java.net.URISyntaxException;
+import java.util.List;
 import java.util.Set;
 
 /** Settings homepage activity */
@@ -112,6 +117,9 @@
     // A regular layout shows icons on homepage, whereas a simplified layout doesn't.
     private boolean mIsRegularLayout = true;
 
+    private SplitControllerCallbackAdapter mSplitControllerAdapter;
+    private SplitInfoCallback mCallback;
+
     /** A listener receiving homepage loaded events. */
     public interface HomepageLoadedListener {
         /** Called when the homepage is loaded. */
@@ -259,6 +267,22 @@
     protected void onStart() {
         ((SettingsApplication) getApplication()).setHomeActivity(this);
         super.onStart();
+        if (mIsEmbeddingActivityEnabled) {
+            final SplitController splitController = SplitController.getInstance(this);
+            mSplitControllerAdapter = new SplitControllerCallbackAdapter(splitController);
+            mCallback = new SplitInfoCallback(this);
+            mSplitControllerAdapter.addSplitListener(this, Runnable::run, mCallback);
+        }
+    }
+
+    @Override
+    protected void onStop() {
+        super.onStop();
+        if (mSplitControllerAdapter != null && mCallback != null) {
+            mSplitControllerAdapter.removeSplitListener(mCallback);
+            mCallback = null;
+            mSplitControllerAdapter = null;
+        }
     }
 
     @Override
@@ -281,21 +305,13 @@
     @Override
     public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
-        final boolean newTwoPaneState = ActivityEmbeddingUtils.isAlreadyEmbedded(this);
-        if (mIsTwoPane != newTwoPaneState) {
-            mIsTwoPane = newTwoPaneState;
-            updateHomepageAppBar();
-            updateHomepageBackground();
-            updateHomepagePaddings();
-        }
-        updateSplitLayout();
+        updateHomepageUI();
     }
 
     private void updateSplitLayout() {
         if (!mIsEmbeddingActivityEnabled) {
             return;
         }
-
         if (mIsTwoPane) {
             if (mIsRegularLayout == ActivityEmbeddingUtils.isRegularHomepageLayout(this)) {
                 // Layout unchanged
@@ -365,6 +381,17 @@
         }
     }
 
+    private void updateHomepageUI() {
+        final boolean newTwoPaneState = ActivityEmbeddingUtils.isAlreadyEmbedded(this);
+        if (mIsTwoPane != newTwoPaneState) {
+            mIsTwoPane = newTwoPaneState;
+            updateHomepageAppBar();
+            updateHomepageBackground();
+            updateHomepagePaddings();
+        }
+        updateSplitLayout();
+    }
+
     private void updateHomepageBackground() {
         if (!mIsEmbeddingActivityEnabled) {
             return;
@@ -733,4 +760,24 @@
             }
         }
     }
+
+    /** The callback invoked while AE splitting. */
+    private static class SplitInfoCallback implements Consumer<List<SplitInfo>> {
+        private final SettingsHomepageActivity mActivity;
+
+        private boolean mIsSplitUpdatedUI = false;
+
+        SplitInfoCallback(SettingsHomepageActivity activity) {
+            mActivity = activity;
+        }
+
+        @Override
+        public void accept(List<SplitInfo> splitInfoList) {
+            if (!splitInfoList.isEmpty() && !mIsSplitUpdatedUI && !mActivity.isFinishing()
+                    && ActivityEmbeddingUtils.isAlreadyEmbedded(mActivity)) {
+                mIsSplitUpdatedUI = true;
+                mActivity.updateHomepageUI();
+            }
+        }
+    }
 }