Merge "Avoid checking write access if not necessary"
diff --git a/build_and_run_benchmark.sh b/build_and_run_benchmark.sh
index dbcaf5d..4f4949b 100755
--- a/build_and_run_benchmark.sh
+++ b/build_and_run_benchmark.sh
@@ -47,9 +47,7 @@
 done
 
 MODE="${1:-scoring}"
-
 INSTALL_NATIVE_TESTS=false
-
 case "$MODE" in
   scoring)
     CLASS=com.android.nn.benchmark.app.NNScoringTest
@@ -99,11 +97,6 @@
 set -e
 cd $ANDROID_BUILD_TOP
 
-LOGDIR=$(mktemp -d)/mlts-logs
-
-mkdir -p $LOGDIR
-echo Creating logs in $LOGDIR
-
 # Build and install benchmark app
 TMPFILE=$(mktemp)
 build/soong/soong_ui.bash --make-mode NeuralNetworksApiBenchmark 2>&1 | tee ${TMPFILE}
@@ -141,61 +134,73 @@
   AM_INSTRUMENT_FLAGS+=" --no-isolated-storage"
 fi
 
-if [[ "$MLTS_RELEASE_DEVICE" == "True" ]]; then
-  TEST_EXTENRAL_STORAGE="com.android.nn.benchmark.app/com.android.nn.benchmark.util.TestExternalStorageActivity"
-  while ! adb shell "am start -W $TEST_EXTENRAL_STORAGE && rm /sdcard/mlts_write_external_storage" > /dev/null 2>&1; do
-     echo "************************************************************"
-     echo "Grant External storage write permissions to MLTS to proceed!"
-     echo "************************************************************"
-     read -n 1 -r -p "Continue? (press any key)"
-     echo
-  done
-else
-  adb root
-  adb shell "pm grant com.android.nn.benchmark.app android.permission.WRITE_EXTERNAL_STORAGE"
-  # Skip setup wizard and remount (read-write)
-  if ! adb shell test -f /data/local.prop; then
-    adb shell 'echo ro.setupwizard.mode=DISABLED > /data/local.prop'
-    adb shell 'chmod 644 /data/local.prop'
-    adb shell 'settings put global device_provisioned 1*'
-    adb shell 'settings put secure user_setup_complete 1'
-    adb disable-verity
-    adb reboot
-    sleep 5
-    adb wait-for-usb-device root
-    adb wait-for-usb-device remount
-    sleep 5
+if [[ "$MODE" == "scoring" ]]; then
+  if [[ "$MLTS_RELEASE_DEVICE" == "True" ]]; then
+    TEST_EXTENRAL_STORAGE="com.android.nn.benchmark.app/com.android.nn.benchmark.util.TestExternalStorageActivity"
+    while ! adb shell "am start -W $TEST_EXTENRAL_STORAGE && rm /sdcard/mlts_write_external_storage" > /dev/null 2>&1; do
+       echo "************************************************************"
+       echo "Grant External storage write permissions to MLTS to proceed!"
+       echo "************************************************************"
+       read -n 1 -r -p "Continue? (press any key)"
+       echo
+    done
+  else
+    adb root
+    adb shell "pm grant com.android.nn.benchmark.app android.permission.WRITE_EXTERNAL_STORAGE"
+    # Skip setup wizard and remount (read-write)
+    if ! adb shell test -f /data/local.prop; then
+      adb shell 'echo ro.setupwizard.mode=DISABLED > /data/local.prop'
+      adb shell 'chmod 644 /data/local.prop'
+      adb shell 'settings put global device_provisioned 1*'
+      adb shell 'settings put secure user_setup_complete 1'
+      adb disable-verity
+      adb reboot
+      sleep 5
+      adb wait-for-usb-device root
+      adb wait-for-usb-device remount
+      sleep 5
+    fi
+    set +e
+    # Enable menu key press through adb
+    adb shell 'echo testing > /data/local/enable_menu_key'
+    # Leave screen on (affects scheduling)
+    adb shell settings put system screen_off_timeout 86400000
+    # Stop background apps, seem to take ~10% CPU otherwise
+    adb shell 'pm disable com.google.android.googlequicksearchbox'
+    adb shell 'pm list packages -f' | sed -e 's/.*=//' | sed 's/\r//g' | grep "com.breel.wallpapers" | while read pkg; do adb shell "pm disable $pkg"; done;
+    set -e
   fi
-  set +e
-  # Enable menu key press through adb
-  adb shell 'echo testing > /data/local/enable_menu_key'
-  # Leave screen on (affects scheduling)
-  adb shell settings put system screen_off_timeout 86400000
-  # Stop background apps, seem to take ~10% CPU otherwise
-  adb shell 'pm disable com.google.android.googlequicksearchbox'
-  adb shell 'pm list packages -f' | sed -e 's/.*=//' | sed 's/\r//g' | grep "com.breel.wallpapers" | while read pkg; do adb shell "pm disable $pkg"; done;
-  set -e
 fi
 
 adb shell setprop debug.nn.cpuonly 0
 adb shell setprop debug.nn.vlog "''"
 
-HOST_CSV=$LOGDIR/benchmark.csv
-RESULT_HTML=$LOGDIR/result.html
-DEVICE_CSV=/sdcard/mlts_benchmark.csv
-
 # Menukey - make sure screen is on
 adb shell "input keyevent 82"
 # Show homescreen
 adb shell wm dismiss-keyguard
-# Remove old benchmark csv data
-adb shell rm -f ${DEVICE_CSV}
+
+if [[ "$MODE" == "scoring" ]]; then
+  LOGDIR=$(mktemp -d)/mlts-logs
+  HOST_CSV=$LOGDIR/benchmark.csv
+  RESULT_HTML=$LOGDIR/result.html
+  DEVICE_CSV=/sdcard/mlts_benchmark.csv
+
+  mkdir -p $LOGDIR
+  echo Creating logs in $LOGDIR
+
+  # Remove old benchmark csv data
+  adb shell rm -f ${DEVICE_CSV}
+fi
 
 # Set the shell pid as a top-app and run tests
 time adb shell "echo $$ > /dev/stune/top-app/tasks; am instrument ${AM_INSTRUMENT_FLAGS} -w -e class $CLASS com.android.nn.benchmark.app/androidx.test.runner.AndroidJUnitRunner"
-adb pull $DEVICE_CSV $HOST_CSV
-echo Benchmark data saved in $HOST_CSV
 
-$ANDROID_BUILD_TOP/test/mlts/benchmark/results/generate_result.py $HOST_CSV $RESULT_HTML
-echo Results stored  in $RESULT_HTML
-xdg-open $RESULT_HTML
+if [[ "$MODE" == "scoring" ]]; then
+  adb pull $DEVICE_CSV $HOST_CSV
+  echo Benchmark data saved in $HOST_CSV
+
+  $ANDROID_BUILD_TOP/test/mlts/benchmark/results/generate_result.py $HOST_CSV $RESULT_HTML
+  echo Results stored  in $RESULT_HTML
+  xdg-open $RESULT_HTML
+fi