Fix that multiple screenshots of one step are taken before the step is closed.

Bug: 321157648
Test: locally
Change-Id: Ia1430765885bfbb0b4e31a5fc34a8c82112d7185
diff --git a/common/device-side/interactive/src/main/java/com/android/interactive/Step.java b/common/device-side/interactive/src/main/java/com/android/interactive/Step.java
index 58410ec..df08c83 100644
--- a/common/device-side/interactive/src/main/java/com/android/interactive/Step.java
+++ b/common/device-side/interactive/src/main/java/com/android/interactive/Step.java
@@ -85,6 +85,9 @@
 
     private Optional<E> mValue = Optional.empty();
     private boolean mFailed = false;
+    // Whether there's a screenshot taken for this step.
+    // In case multiple close() calls creating multiple screenshot files.
+    private boolean mHasTakenScreenshot = false;
 
     private static Map<Class<? extends Step<?>>, Object> sStepCache = new HashMap<>();
 
@@ -144,7 +147,7 @@
                                         .terminalValue((b) -> step.hasFailed())
                                         .errorOnFail(
                                                 "Expected value from step. No value provided or"
-                                                    + " step failed.")
+                                                        + " step failed.")
                                         .timeout(MAX_STEP_DURATION)
                                         .await()
                                         .get();
@@ -224,7 +227,7 @@
         } catch (ClassCastException e) {
             throw new IllegalStateException(
                     "You cannot call pass() for a step which requires a return value. If no return"
-                        + " value is required, the step should use Nothing (not Void)");
+                            + " value is required, the step should use Nothing (not Void)");
         }
     }
 
@@ -336,8 +339,14 @@
         sWindowManager.updateViewLayout(mInstructionView, params);
     }
 
+    /**
+     * Closes the step, takes a screenshot of the device if the feature is enabled, and removes the
+     * instruction view if it's still there.
+     */
     protected void close() {
-        if (TestApis.instrumentation().arguments().getBoolean("TAKE_SCREENSHOT", false)) {
+        if (!mHasTakenScreenshot
+                && TestApis.instrumentation().arguments().getBoolean("TAKE_SCREENSHOT", false)) {
+            mHasTakenScreenshot = true;
             ScreenshotUtil.captureScreenshot(getClass().getCanonicalName());
         }
         if (mInstructionView != null) {