Enable/disable fingerprint for SUW suggestion activity

Bug: 115251237
Test: atest RunSettingsRoboTests, manually test enable/disable situations
Change-Id: Ia53d2723fca5d7e3093e79037cb9053d11e4babc
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 68a7cbb..ad30f62 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -1590,10 +1590,6 @@
             android:permission="android.permission.MANAGE_FINGERPRINT"
             android:icon="@drawable/ic_suggestion_fingerprint"
             android:theme="@style/GlifTheme.Light">
-            <intent-filter>
-                <action android:name="android.intent.action.MAIN" />
-                <category android:name="com.android.settings.suggested.category.FINGERPRINT_ENROLL" />
-            </intent-filter>
             <meta-data android:name="com.android.settings.require_feature"
                 android:value="android.hardware.fingerprint" />
             <meta-data android:name="com.android.settings.title"
@@ -1602,6 +1598,22 @@
                 android:resource="@string/suggestion_additional_fingerprints_summary" />
         </activity>
 
+        <activity-alias android:name=".SetupFingerprintSuggestionActivity"
+            android:enabled="false"
+            android:exported="true"
+            android:targetActivity=".biometrics.fingerprint.FingerprintSuggestionActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.settings.suggested.category.FINGERPRINT_ENROLL" />
+            </intent-filter>
+            <meta-data android:name="com.android.settings.require_feature"
+                       android:value="android.hardware.fingerprint" />
+            <meta-data android:name="com.android.settings.title"
+                       android:resource="@string/suggestion_additional_fingerprints" />
+            <meta-data android:name="com.android.settings.summary"
+                       android:resource="@string/suggestion_additional_fingerprints_summary" />
+        </activity-alias>
+
         <!-- Note this must not be exported since it returns the password in the intent -->
         <activity android:name=".password.ConfirmLockPattern$InternalActivity"
             android:exported="false"
diff --git a/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollFinish.java b/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollFinish.java
index 3f02b6f..05ee7be 100644
--- a/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollFinish.java
+++ b/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollFinish.java
@@ -17,11 +17,15 @@
 package com.android.settings.biometrics.fingerprint;
 
 import android.app.settings.SettingsEnums;
+import android.content.ComponentName;
 import android.content.Intent;
+import android.content.pm.PackageManager;
 import android.hardware.fingerprint.FingerprintManager;
 import android.os.Bundle;
 import android.view.View;
 
+import androidx.annotation.VisibleForTesting;
+
 import com.android.settings.R;
 import com.android.settings.Utils;
 import com.android.settings.biometrics.BiometricEnrollBase;
@@ -34,7 +38,11 @@
  */
 public class FingerprintEnrollFinish extends BiometricEnrollBase {
 
-    private static final int REQUEST_ADD_ANOTHER = 1;
+    @VisibleForTesting
+    static final int REQUEST_ADD_ANOTHER = 1;
+    @VisibleForTesting
+    static final String FINGERPRINT_SUGGESTION_ACTIVITY =
+            "com.android.settings.SetupFingerprintSuggestionActivity";
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -85,16 +93,37 @@
 
     @Override
     protected void onNextButtonClick(View view) {
+        setFingerprintSuggestionEnabled();
         setResult(RESULT_FINISHED);
         finish();
     }
 
+    private void setFingerprintSuggestionEnabled() {
+        final FingerprintManager fpm = Utils.getFingerprintManagerOrNull(this);
+        if (fpm != null) {
+            int enrolled = fpm.getEnrolledFingerprints(mUserId).size();
+
+            // Only show "Add another fingerprint" if the user already enrolled one.
+            // "Add fingerprint" will be shown in the main flow if the user hasn't enrolled any
+            // fingerprints. If the user already added more than one fingerprint, they already know
+            // to add multiple fingerprints so we don't show the suggestion.
+            int flag = (enrolled == 1) ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
+                    : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
+
+            ComponentName componentName = new ComponentName(getApplicationContext(),
+                    FINGERPRINT_SUGGESTION_ACTIVITY);
+            getPackageManager().setComponentEnabledSetting(
+                    componentName, flag, PackageManager.DONT_KILL_APP);
+        }
+    }
+
     private void onAddAnotherButtonClick(View view) {
         startActivityForResult(getFingerprintEnrollingIntent(), REQUEST_ADD_ANOTHER);
     }
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        setFingerprintSuggestionEnabled();
         if (requestCode == REQUEST_ADD_ANOTHER && resultCode != RESULT_CANCELED) {
             setResult(resultCode, data);
             finish();
diff --git a/tests/robotests/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollFinishTest.java b/tests/robotests/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollFinishTest.java
new file mode 100644
index 0000000..74b35c9
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollFinishTest.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2019 Google Inc.
+ *
+ * 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.biometrics.fingerprint;
+
+import static com.android.settings.biometrics.fingerprint.FingerprintEnrollFinish.FINGERPRINT_SUGGESTION_ACTIVITY;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.robolectric.RuntimeEnvironment.application;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.hardware.fingerprint.FingerprintManager;
+
+import com.android.settings.R;
+import com.android.settings.testutils.shadow.ShadowFingerprintManager;
+
+import com.google.android.setupcompat.PartnerCustomizationLayout;
+import com.google.android.setupcompat.template.FooterBarMixin;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.Shadows;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowActivity;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(shadows = {ShadowFingerprintManager.class})
+public class SetupFingerprintEnrollFinishTest {
+
+    private FingerprintEnrollFinish mActivity;
+    private ComponentName mComponentName;
+    private PartnerCustomizationLayout mLayout;
+    private FingerprintManager mFingerprintManager;
+
+    @Before
+    public void setUp() {
+        mActivity = Robolectric.buildActivity(FingerprintEnrollFinish.class).setup().get();
+        mLayout = mActivity.findViewById(R.id.setup_wizard_layout);
+        Shadows.shadowOf(application.getPackageManager())
+                .setSystemFeature(PackageManager.FEATURE_FINGERPRINT, true);
+
+        mFingerprintManager = (FingerprintManager) application.getSystemService(
+                Context.FINGERPRINT_SERVICE);
+        Shadows.shadowOf(mFingerprintManager).setIsHardwareDetected(true);
+
+        mComponentName = new ComponentName(
+                application, FINGERPRINT_SUGGESTION_ACTIVITY);
+    }
+
+    @Test
+    public void clickAddAnother_shouldLaunchEnrolling() {
+        final ComponentName enrollingComponent = new ComponentName(
+                application,
+                FingerprintEnrollEnrolling.class);
+
+        mLayout.getMixin(FooterBarMixin.class).getSecondaryButtonView().performClick();
+
+        ShadowActivity.IntentForResult startedActivity =
+                Shadows.shadowOf(mActivity).getNextStartedActivityForResult();
+        assertThat(startedActivity).named("Next activity").isNotNull();
+        assertThat(startedActivity.intent.getComponent())
+                .isEqualTo(enrollingComponent);
+    }
+
+    @Test
+    public void clickAddAnother_shouldPropagateResults() {
+        final ComponentName enrollingComponent = new ComponentName(
+                application,
+                FingerprintEnrollEnrolling.class);
+
+        mLayout.getMixin(FooterBarMixin.class).getSecondaryButtonView().performClick();
+
+        ShadowActivity.IntentForResult startedActivity =
+                Shadows.shadowOf(mActivity).getNextStartedActivityForResult();
+        assertThat(startedActivity).named("Next activity").isNotNull();
+        assertThat(startedActivity.intent.getComponent())
+                .isEqualTo(enrollingComponent);
+    }
+
+    @Test
+    public void clickNext_shouldFinish() {
+        mLayout.getMixin(FooterBarMixin.class).getPrimaryButtonView().performClick();
+
+        assertThat(mActivity.isFinishing()).isTrue();
+    }
+
+    @Test
+    public void onActivityResult_fingerprintCountIsNotOne_fingerprintSuggestionActivityDisabled() {
+        Shadows.shadowOf((FingerprintManager) mFingerprintManager).setDefaultFingerprints(0);
+
+        mActivity.onActivityResult(0, 0, null);
+
+        assertThat(application.getPackageManager().getComponentEnabledSetting(
+                mComponentName)).isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
+    }
+
+    @Test
+    public void onActivityResult_fingerprintCountIsOne_fingerprintSuggestionActivityEnabled() {
+        Shadows.shadowOf((FingerprintManager) mFingerprintManager).setDefaultFingerprints(1);
+
+        mActivity.onActivityResult(0, 0, null);
+
+        assertThat(application.getPackageManager().getComponentEnabledSetting(
+                mComponentName)).isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
+    }
+
+    @Test
+    public void clickNext_fingerprintCountIsNotOne_fingerprintSuggestionActivityDisabled() {
+        Shadows.shadowOf((FingerprintManager) mFingerprintManager).setDefaultFingerprints(2);
+
+        mLayout.getMixin(FooterBarMixin.class).getPrimaryButtonView().performClick();
+
+        assertThat(application.getPackageManager().getComponentEnabledSetting(
+                mComponentName)).isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
+    }
+
+    @Test
+    public void clickNext_fingerprintCountIsOne_fngerprintSuggestionActivityEnabled() {
+        Shadows.shadowOf((FingerprintManager) mFingerprintManager).setDefaultFingerprints(1);
+
+        mLayout.getMixin(FooterBarMixin.class).getPrimaryButtonView().performClick();
+
+        assertThat(application.getPackageManager().getComponentEnabledSetting(
+                mComponentName)).isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
+    }
+}
\ No newline at end of file