Launch SecurityHub fragment on SECURITY_SETTINGS intent when available.

Test: atest SettingsUnitTests
Test: adb shell am start -a android.settings.SECURITY_SETTINGS opens
SecurityHub screen (when SecurityHub enabled)
Test: assistant opens SecurityHub screen (when SecurityHub enabled) when
given instrucion to "open security settings"
Bug: 183930061

Change-Id: Ie8fcb2f2dce4cd0a2a84c6cd21a0a1c0b2b3665e
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index c89402c..d28f86d 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -19,11 +19,14 @@
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.util.FeatureFlagUtils;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.settings.core.FeatureFlags;
 import com.android.settings.enterprise.EnterprisePrivacySettings;
 import com.android.settings.overlay.FeatureFactory;
+import com.android.settings.security.SecuritySettingsFeatureProvider;
 
 /**
  * Top-level Settings activity
@@ -118,7 +121,39 @@
      * Activity for Reduce Bright Colors.
      */
     public static class ReduceBrightColorsSettingsActivity extends SettingsActivity { /* empty */ }
-    public static class SecurityDashboardActivity extends SettingsActivity { /* empty */ }
+    /** Activity for the security dashboard. */
+    public static class SecurityDashboardActivity extends SettingsActivity {
+
+        /** Whether the given fragment is allowed. */
+        @VisibleForTesting
+        @Override
+        public boolean isValidFragment(String fragmentName) {
+            return super.isValidFragment(fragmentName)
+                    || (fragmentName != null
+                            && TextUtils.equals(fragmentName, getAlternativeFragmentName()));
+        }
+
+        @Override
+        public String getInitialFragmentName(Intent intent) {
+            final String alternativeFragmentName = getAlternativeFragmentName();
+            if (alternativeFragmentName != null) {
+                return alternativeFragmentName;
+            }
+
+            return super.getInitialFragmentName(intent);
+        }
+
+        private String getAlternativeFragmentName() {
+            String alternativeFragmentClassname = null;
+            final SecuritySettingsFeatureProvider securitySettingsFeatureProvider =
+                    FeatureFactory.getFactory(this).getSecuritySettingsFeatureProvider();
+            if (securitySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) {
+                alternativeFragmentClassname = securitySettingsFeatureProvider
+                        .getAlternativeSecuritySettingsFragmentClassname();
+            }
+            return alternativeFragmentClassname;
+        }
+    }
     public static class UsageAccessSettingsActivity extends SettingsActivity { /* empty */ }
     public static class AppUsageAccessSettingsActivity extends SettingsActivity { /* empty */ }
     public static class LocationSettingsActivity extends SettingsActivity { /* empty */ }
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index b672917..923c2bd 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -219,7 +219,7 @@
     private String getMetricsTag() {
         String tag = null;
         if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
-            tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
+            tag = getInitialFragmentName(getIntent());
         }
         if (TextUtils.isEmpty(tag)) {
             Log.w(LOG_TAG, "MetricsTag is invalid " + tag);
@@ -262,7 +262,7 @@
         }
 
         // Getting Intent properties can only be done after the super.onCreate(...)
-        final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
+        final String initialFragmentName = getInitialFragmentName(intent);
 
         // This is a "Sub Settings" when:
         // - this is a real SubSettings
@@ -363,6 +363,12 @@
         }
     }
 
+    /** Returns the initial fragment name that the activity will launch. */
+    @VisibleForTesting
+    public String getInitialFragmentName(Intent intent) {
+        return intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
+    }
+
     @Override
     protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
         theme.applyStyle(R.style.SetupWizardPartnerResource, true);
diff --git a/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java b/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java
new file mode 100644
index 0000000..3bda96e
--- /dev/null
+++ b/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.security;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Intent;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.platform.app.InstrumentationRegistry;
+
+import com.android.settings.Settings;
+import com.android.settings.SettingsActivity;
+import com.android.settings.testutils.FakeFeatureFactory;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+
+@RunWith(AndroidJUnit4.class)
+public class SecurityDashboardActivityTest {
+    private static final String ALTERNATIVE_FRAGMENT_CLASSNAME = "AlternativeFragmentClassname";
+    private static final String DEFAULT_FRAGMENT_CLASSNAME = "DefaultFragmentClassname";
+
+    private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider;
+    private Settings.SecurityDashboardActivity mActivity;
+    private Intent mDefaultIntent;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        FakeFeatureFactory mFeatureFactory = FakeFeatureFactory.setupForTest();
+        mSecuritySettingsFeatureProvider = mFeatureFactory.getSecuritySettingsFeatureProvider();
+        mDefaultIntent = new Intent();
+        mDefaultIntent.setAction(android.provider.Settings.ACTION_SECURITY_SETTINGS);
+        mDefaultIntent.setClass(InstrumentationRegistry.getInstrumentation().getTargetContext(),
+                Settings.SecurityDashboardActivity.class);
+        mDefaultIntent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT_CLASSNAME);
+        InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
+            try {
+                mActivity =
+                        (Settings.SecurityDashboardActivity) InstrumentationRegistry
+                                .getInstrumentation().newActivity(
+                                        getClass().getClassLoader(),
+                                        Settings.SecurityDashboardActivity.class.getName(),
+                                        mDefaultIntent);
+            } catch (Exception e) {
+                throw new RuntimeException(e); // nothing to do
+            }
+        });
+    }
+
+    @Test
+    public void noAvailableAlternativeFragmentAvailable_defaultFragmentSet() {
+        when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
+                .thenReturn(false);
+
+        assertThat(mActivity.getInitialFragmentName(mDefaultIntent))
+                .isEqualTo(DEFAULT_FRAGMENT_CLASSNAME);
+    }
+
+    @Test
+    public void alternativeFragmentAvailable_alternativeFragmentSet() {
+        when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
+                .thenReturn(true);
+        when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname())
+                .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME);
+
+        assertThat(mActivity.getInitialFragmentName(mDefaultIntent))
+                .isEqualTo(ALTERNATIVE_FRAGMENT_CLASSNAME);
+    }
+
+    @Test
+    public void noAvailableAlternativeFragmentAvailable_alternativeFragmentNotValid() {
+        when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
+                .thenReturn(false);
+
+        assertThat(mActivity.isValidFragment(ALTERNATIVE_FRAGMENT_CLASSNAME)).isFalse();
+    }
+
+    @Test
+    public void alternativeFragmentAvailable_alternativeFragmentIsValid() {
+        when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
+                .thenReturn(true);
+        when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname())
+                .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME);
+
+        assertThat(mActivity.isValidFragment(ALTERNATIVE_FRAGMENT_CLASSNAME)).isTrue();
+    }
+}