Do not show options if screen lock types is empty

Bug: 63311251
Test: Manually tested on device
Change-Id: Ia56a0791b89635efb4f8305f39b586b8054271cf
diff --git a/src/com/android/settings/password/SetupChooseLockPassword.java b/src/com/android/settings/password/SetupChooseLockPassword.java
index 0b21129..2988f52 100644
--- a/src/com/android/settings/password/SetupChooseLockPassword.java
+++ b/src/com/android/settings/password/SetupChooseLockPassword.java
@@ -18,10 +18,12 @@
 
 import android.app.Activity;
 import android.app.Fragment;
+import android.app.admin.DevicePolicyManager;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
+import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 import android.widget.LinearLayout;
@@ -43,6 +45,8 @@
  */
 public class SetupChooseLockPassword extends ChooseLockPassword {
 
+    private static final String TAG = "SetupChooseLockPassword";
+
     public static Intent modifyIntentForSetup(
             Context context,
             Intent chooseLockPasswordIntent) {
@@ -87,9 +91,19 @@
                 mCancelButton.setText(R.string.skip_label);
             }
 
-            boolean showOptionsButton = getActivity().getIntent().getBooleanExtra(
+            final Activity activity = getActivity();
+            ChooseLockGenericController chooseLockGenericController =
+                    new ChooseLockGenericController(activity, mUserId);
+            boolean anyOptionsShown = chooseLockGenericController.getVisibleScreenLockTypes(
+                    DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, false).size() > 0;
+            boolean showOptionsButton = activity.getIntent().getBooleanExtra(
                     ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, false);
-            if (showOptionsButton) {
+
+            if (!anyOptionsShown) {
+                Log.w(TAG, "Visible screen lock types is empty!");
+            }
+
+            if (showOptionsButton && anyOptionsShown) {
                 mOptionsButton = view.findViewById(R.id.screen_lock_options);
                 mOptionsButton.setVisibility(View.VISIBLE);
                 mOptionsButton.setOnClickListener(this);
diff --git a/tests/robotests/src/com/android/settings/password/SetupChooseLockPasswordTest.java b/tests/robotests/src/com/android/settings/password/SetupChooseLockPasswordTest.java
index 5f4c16a..3e9c39c 100644
--- a/tests/robotests/src/com/android/settings/password/SetupChooseLockPasswordTest.java
+++ b/tests/robotests/src/com/android/settings/password/SetupChooseLockPasswordTest.java
@@ -24,6 +24,7 @@
 import android.app.AlertDialog;
 import android.content.Intent;
 import android.os.Bundle;
+import android.view.View;
 import android.widget.Button;
 
 import com.android.settings.R;
@@ -42,11 +43,16 @@
 import org.robolectric.Robolectric;
 import org.robolectric.Shadows;
 import org.robolectric.annotation.Config;
+import org.robolectric.annotation.Implementation;
+import org.robolectric.annotation.Implements;
 import org.robolectric.shadows.ShadowActivity;
 import org.robolectric.shadows.ShadowActivity.IntentForResult;
 import org.robolectric.shadows.ShadowAlertDialog;
 import org.robolectric.shadows.ShadowDialog;
 
+import java.util.Collections;
+import java.util.List;
+
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(
         manifest = TestConfig.MANIFEST_PATH,
@@ -80,6 +86,15 @@
     }
 
     @Test
+    @Config(shadows = ShadowChooseLockGenericController.class)
+    public void createActivity_withShowOptionsButtonExtra_buttonNotVisibleIfNoVisibleLockTypes() {
+        SetupChooseLockPassword activity = createSetupChooseLockPassword();
+        Button optionsButton = activity.findViewById(R.id.screen_lock_options);
+        assertThat(optionsButton).isNotNull();
+        assertThat(optionsButton.getVisibility()).isEqualTo(View.GONE);
+    }
+
+    @Test
     public void allSecurityOptions_shouldBeShown_When_OptionsButtonIsClicked() {
         SetupChooseLockPassword activity = createSetupChooseLockPassword();
         activity.findViewById(R.id.screen_lock_options).performClick();
@@ -122,4 +137,13 @@
         intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, true);
         return Robolectric.buildActivity(SetupChooseLockPassword.class, intent).setup().get();
     }
+
+    @Implements(ChooseLockGenericController.class)
+    public static class ShadowChooseLockGenericController {
+        @Implementation
+        public List<ScreenLockType> getVisibleScreenLockTypes(int quality,
+                boolean includeDisabled) {
+            return Collections.emptyList();
+        }
+    }
 }