[Large screen] Check if it's in 2-pane before 2-pane deep link flow

The callback of SplitStateObserver.SplitStateListener can have one
or more active splits when there is already 2-pane in Activity stack.

This change prevent unnecessary 2-pane deep link flow if the Activity
is already in 2-pane activity stack.

This solution does not work if the Activity was started in a new task.
(e.g., Intent.FLAG_ACTIVITY_NEW_TASK, launchMode singleTask).

Bug: 201379454
Bug: 201620626
Bug: 204398432
Bug: 204397936
Bug: 197609195
Bug: 197609197
Bug: 204501179
Bug: 204959335
Bug: 204845334
Test: manual
      1. Settings -> Apps > Default apps > Opening links.
      2. Click back button should back to Default apps page.
Change-Id: I04aaceed47a8f2754a4e17c53b49252f61e0a1d1
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 9c81895..226e1de 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -58,6 +58,7 @@
 import com.android.internal.util.ArrayUtils;
 import com.android.settings.Settings.WifiSettingsActivity;
 import com.android.settings.activityembedding.ActivityEmbeddingUtils;
+import com.android.settings.activityembedding.SplitStateObserver;
 import com.android.settings.applications.manageapplications.ManageApplications;
 import com.android.settings.core.OnActivityResultListener;
 import com.android.settings.core.SettingsBaseActivity;
@@ -255,11 +256,8 @@
         // Should happen before any call to getIntent()
         getMetaData();
         final Intent intent = getIntent();
-        if (shouldShowTwoPaneDeepLink(intent)) {
-            launchHomepageForTwoPaneDeepLink(intent);
-            finishAndRemoveTask();
-            return;
-        }
+
+        registerSplitStateObserverForTwoPaneDeepLink();
 
         final FeatureFactory factory = FeatureFactory.getFactory(this);
         mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
@@ -364,6 +362,29 @@
         }
     }
 
+    private void registerSplitStateObserverForTwoPaneDeepLink() {
+        if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(this)) {
+            return;
+        }
+
+        final SplitStateObserver splitStateObserver = new SplitStateObserver(this /* activity*/,
+                true /* listenOnce */,
+                splitInfos -> {
+                    if (!splitInfos.isEmpty()) {
+                        // It's already in 2-pane and no need to go 2-pane deep link flow.
+                        return;
+                    }
+
+                    if (shouldShowTwoPaneDeepLink(getIntent())) {
+                        launchHomepageForTwoPaneDeepLink(getIntent());
+                        finishAndRemoveTask();
+                        return;
+                    }
+                }
+            );
+        getLifecycle().addObserver(splitStateObserver);
+    }
+
     private boolean isSubSettings(Intent intent) {
         return this instanceof SubSettings ||
             intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
@@ -413,10 +434,6 @@
     }
 
     private boolean shouldShowTwoPaneDeepLink(Intent intent) {
-        if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(this)) {
-            return false;
-        }
-
         // Only starts trampoline for deep links. Should return false for all the cases that
         // Settings app starts SettingsActivity or SubSetting by itself.
         if (intent.getAction() == null) {
@@ -434,11 +451,6 @@
             return false;
         }
 
-        if (intent.getBooleanExtra(SettingsHomepageActivity.EXTRA_IS_FROM_SETTINGS_HOMEPAGE,
-                /* defaultValue */ false)) {
-            return false;
-        }
-
         if (TextUtils.equals(intent.getAction(), Intent.ACTION_CREATE_SHORTCUT)) {
             // Returns false to show full screen for Intent.ACTION_CREATE_SHORTCUT because
             // - Launcher startActivityForResult for Intent.ACTION_CREATE_SHORTCUT and activity