[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
diff --git a/src/com/android/settings/activityembedding/SplitStateObserver.java b/src/com/android/settings/activityembedding/SplitStateObserver.java
new file mode 100644
index 0000000..ba13c82
--- /dev/null
+++ b/src/com/android/settings/activityembedding/SplitStateObserver.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.activityembedding;
+
+import static androidx.lifecycle.Lifecycle.Event.ON_START;
+import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
+
+import android.app.Activity;
+
+import androidx.annotation.NonNull;
+import androidx.core.content.ContextCompat;
+import androidx.core.util.Consumer;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.OnLifecycleEvent;
+import androidx.window.embedding.SplitController;
+import androidx.window.embedding.SplitInfo;
+
+import java.util.List;
+
+/** A lifecycle-aware observer listens to active split state. */
+public class SplitStateObserver implements LifecycleObserver, Consumer<List<SplitInfo>> {
+
+    private final Activity mActivity;
+    private final boolean mListenOnce;
+    private final SplitStateListener mListener;
+    private final SplitController mSplitController;
+
+    public SplitStateObserver(@NonNull Activity activity, boolean listenOnce,
+            @NonNull SplitStateListener listener) {
+        mActivity = activity;
+        mListenOnce = listenOnce;
+        mListener = listener;
+        mSplitController = SplitController.getInstance();
+    }
+
+    /**
+     * Start lifecycle event.
+     */
+    @OnLifecycleEvent(ON_START)
+    public void onStart() {
+        mSplitController.addSplitListener(mActivity, ContextCompat.getMainExecutor(mActivity),
+                this);
+    }
+
+    /**
+     * Stop lifecycle event.
+     */
+    @OnLifecycleEvent(ON_STOP)
+    public void onStop() {
+        mSplitController.removeSplitListener(this);
+    }
+
+    @Override
+    public void accept(List<SplitInfo> splitInfos) {
+        if (mListenOnce) {
+            mSplitController.removeSplitListener(this);
+        }
+        mListener.onSplitInfoChanged(splitInfos);
+    }
+
+    /** This interface makes as class that it wants to listen to {@link SplitInfo} changes. */
+    public interface SplitStateListener {
+
+        /** Receive a set of split info change */
+        void onSplitInfoChanged(List<SplitInfo> splitInfos);
+    }
+}
diff --git a/src/com/android/settings/homepage/SettingsHomepageActivity.java b/src/com/android/settings/homepage/SettingsHomepageActivity.java
index ae8c61e..0322cfa 100644
--- a/src/com/android/settings/homepage/SettingsHomepageActivity.java
+++ b/src/com/android/settings/homepage/SettingsHomepageActivity.java
@@ -64,10 +64,6 @@
     private static final String TAG = "SettingsHomepageActivity";
 
     // Additional extra of Settings#ACTION_SETTINGS_LARGE_SCREEN_DEEP_LINK.
-    // Put true value to the intent when startActivity for a deep link intent from this Activity.
-    public static final String EXTRA_IS_FROM_SETTINGS_HOMEPAGE = "is_from_settings_homepage";
-
-    // Additional extra of Settings#ACTION_SETTINGS_LARGE_SCREEN_DEEP_LINK.
     // Set & get Uri of the Intent separately to prevent failure of Intent#ParseUri.
     public static final String EXTRA_SETTINGS_LARGE_SCREEN_DEEP_LINK_INTENT_DATA =
             "settings_large_screen_deep_link_intent_data";
@@ -268,11 +264,11 @@
         // Sender of intent may want to send intent extra data to the destination of targetIntent.
         targetIntent.replaceExtras(intent);
 
-        targetIntent.putExtra(EXTRA_IS_FROM_SETTINGS_HOMEPAGE, true);
         targetIntent.putExtra(SettingsActivity.EXTRA_IS_FROM_SLICE, false);
 
         targetIntent.setData(intent.getParcelableExtra(
                 SettingsHomepageActivity.EXTRA_SETTINGS_LARGE_SCREEN_DEEP_LINK_INTENT_DATA));
+
         // Set 2-pane pair rule for the deep link page.
         ActivityEmbeddingRulesController.registerTwoPanePairRule(this,
                 getDeepLinkComponent(),