Register rule for wallpaper entry

We should keep consistent split rule for wallpaper and other home menu
item. So, we create this cl to register the split rule for wallpaper.

Also, I do some minor refactoring in this cl for reusing existing
method and better code quality.

Test: Go to wallpaper, and click back key. App can close.
Fix: 204406425
Fix: 204364572

Change-Id: Ia7de9483b351d1121cc26c4af1cb8a89ad0a16bc
diff --git a/src/com/android/settings/activityembedding/ActivityEmbeddingRulesController.java b/src/com/android/settings/activityembedding/ActivityEmbeddingRulesController.java
index 974ec2c..53e8020 100644
--- a/src/com/android/settings/activityembedding/ActivityEmbeddingRulesController.java
+++ b/src/com/android/settings/activityembedding/ActivityEmbeddingRulesController.java
@@ -73,6 +73,9 @@
             boolean finishPrimaryWithSecondary,
             boolean finishSecondaryWithPrimary,
             boolean clearTop) {
+        if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(context)) {
+            return;
+        }
         final Set<SplitPairFilter> filters = new HashSet<>();
         filters.add(new SplitPairFilter(primaryComponent, secondaryComponent,
                 secondaryIntentAction));
@@ -87,18 +90,45 @@
                 LayoutDirection.LOCALE));
     }
 
+    /**
+     * Register a new SplitPairRule for Settings home. Because homepage is able to be opened by
+     * {@link Settings} or {@link SettingsHomepageActivity}, we register split rule twice for
+     * two cases.
+     */
+    public static void registerTwoPanePairRuleForSettingsHome(Context context,
+            ComponentName secondaryComponent,
+            String secondaryIntentAction,
+            boolean clearTop) {
+
+        registerTwoPanePairRule(
+                context,
+                getComponentName(context, Settings.class),
+                secondaryComponent,
+                secondaryIntentAction,
+                true /* finishPrimaryWithSecondary */,
+                true /* finishSecondaryWithPrimary */,
+                clearTop);
+
+        registerTwoPanePairRule(
+                context,
+                getComponentName(context, SettingsHomepageActivity.class),
+                secondaryComponent,
+                secondaryIntentAction,
+                true /* finishPrimaryWithSecondary */,
+                true /* finishSecondaryWithPrimary */,
+                clearTop);
+    }
+
     /** Register a SplitPairRule for SubSettings if the device supports 2-pane. */
-    public static void registerSubSettingsPairRuleIfNeeded(Context context, boolean clearTop) {
+    public static void registerSubSettingsPairRule(Context context, boolean clearTop) {
         if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(context)) {
             return;
         }
 
-        registerTwoPanePairRule(context,
-                getComponentName(context, Settings.class),
+        registerTwoPanePairRuleForSettingsHome(
+                context,
                 getComponentName(context, SubSettings.class),
                 null /* secondaryIntentAction */,
-                true /* finishPrimaryWithSecondary */,
-                true /* finishSecondaryWithPrimary */,
                 clearTop);
     }
 
@@ -140,7 +170,7 @@
 
     @NonNull
     private static ComponentName getComponentName(Context context,
-                Class<? extends Activity> activityClass) {
+            Class<? extends Activity> activityClass) {
         return new ComponentName(context.getPackageName(), activityClass.getName());
     }
 }
diff --git a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
index 951eb3c..3d1381d 100644
--- a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
+++ b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
@@ -175,14 +175,10 @@
                     if (fragment instanceof TopLevelSettings
                             && ActivityEmbeddingUtils.isEmbeddingActivityEnabled(mContext)) {
                         // Register the rule for injected apps.
-                        ActivityEmbeddingRulesController.registerTwoPanePairRule(mContext,
-                                new ComponentName(activity.getPackageName(),
-                                        com.android.settings.Settings.class.getName()),
-                                new ComponentName(tile.getPackageName(),
-                                        tile.getComponentName()),
+                        ActivityEmbeddingRulesController.registerTwoPanePairRuleForSettingsHome(
+                                mContext,
+                                new ComponentName(tile.getPackageName(), tile.getComponentName()),
                                 null /* secondaryIntentAction */,
-                                true /* finishPrimaryWithSecondary */,
-                                true /* finishSecondaryWithPrimary */,
                                 true /* clearTop */);
 
                         // Highlight preference ui.
diff --git a/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java b/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java
index 2e59725..7640d08 100644
--- a/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java
+++ b/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java
@@ -31,6 +31,7 @@
 import androidx.preference.PreferenceScreen;
 
 import com.android.settings.R;
+import com.android.settings.activityembedding.ActivityEmbeddingRulesController;
 import com.android.settings.activityembedding.ActivityEmbeddingUtils;
 import com.android.settings.core.BasePreferenceController;
 import com.android.settingslib.RestrictedLockUtilsInternal;
@@ -102,6 +103,11 @@
                     mContext)) {
                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
             }
+            ActivityEmbeddingRulesController.registerTwoPanePairRuleForSettingsHome(
+                    mContext,
+                    intent.getComponent(),
+                    null /* secondaryIntentAction */,
+                    true /* clearTop */);
             preference.getContext().startActivity(intent);
             return true;
         }
diff --git a/src/com/android/settings/homepage/TopLevelSettings.java b/src/com/android/settings/homepage/TopLevelSettings.java
index e9c7ef8..2b0f788 100644
--- a/src/com/android/settings/homepage/TopLevelSettings.java
+++ b/src/com/android/settings/homepage/TopLevelSettings.java
@@ -100,7 +100,7 @@
     @Override
     public boolean onPreferenceTreeClick(Preference preference) {
         // Register SplitPairRule for SubSettings.
-        ActivityEmbeddingRulesController.registerSubSettingsPairRuleIfNeeded(getContext(),
+        ActivityEmbeddingRulesController.registerSubSettingsPairRule(getContext(),
                 true /* clearTop */);
 
         setHighlightPreferenceKey(preference.getKey());
diff --git a/src/com/android/settings/search/SearchResultTrampoline.java b/src/com/android/settings/search/SearchResultTrampoline.java
index d20a2ea..8805d19 100644
--- a/src/com/android/settings/search/SearchResultTrampoline.java
+++ b/src/com/android/settings/search/SearchResultTrampoline.java
@@ -97,7 +97,7 @@
         } else if (isFromSettingsIntelligence(callingActivity)) {
             // Register SplitPairRule for SubSettings, set clearTop false to prevent unexpected back
             // navigation behavior.
-            ActivityEmbeddingRulesController.registerSubSettingsPairRuleIfNeeded(this,
+            ActivityEmbeddingRulesController.registerSubSettingsPairRule(this,
                     false /* clearTop */);
             // TODO: pass menu key to homepage
             intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK);