Prototype app bundle release update & test am: 29e43ec7a6 am: 2d31dd9171 am: f28a9f6f0c am: a54d0dad23

Original change: https://android-review.googlesource.com/c/platform/tools/aadevtools/+/1778873

Change-Id: I5918afff4927926e811a18e03d0f10b9c3ce38f0
diff --git a/validation/README.md b/validation/README.md
new file mode 100644
index 0000000..96945bc
--- /dev/null
+++ b/validation/README.md
@@ -0,0 +1,39 @@
+# Validation Tools
+This is a collection of validation tools for better development & integration
+productivity for AAOS devices.
+
+## Validating new app releases
+Incoming quality control is an important practice to prevent new technical debts
+added to risk the device development. These scripts illustrate a basic check for
+a new app bundle release against a known good virtual or physical device build.
+So that, you know what changed better & if they break any basic use cases.
+
+1. Prepare a new app release in a directory, a device under test reachable
+via adb & aapt is available in the shell environment.
+```
+appDir="/path/to/appDir"
+renameCsv="/path/to/renameCsvFile"
+```
+
+- As sometime mk file change the file name when copying XML files to the device at the build time, you can supply a CSV file to guide the script. The format is as:
+```
+name,newName
+privapp-permissions-in-app-release.xml,privapp-permissions-on-device.xml
+```
+
+2. batch_install_app.sh: find & install all APKs in a given directory to
+a device via adb. Launch their launchable activities if any & capture
+screenshots. To use:
+
+```
+./batch_install_app.sh ${appDir}
+```
+
+3. batch_check_permission.sh: find & diff permissions XML files in a given
+directory against those on a device via adb. To use:
+
+```
+./batch_check_permission.sh ${appDir} ${renameCsv}
+```
+
+4. test.sh has all the commands above to make is easier to use.
diff --git a/validation/batch_check_permission.sh b/validation/batch_check_permission.sh
new file mode 100755
index 0000000..5af4e91
--- /dev/null
+++ b/validation/batch_check_permission.sh
@@ -0,0 +1,117 @@
+#!/bin/bash
+
+# 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.
+
+readme() {
+  echo '''
+  check permissions xml from a apk bundle release aginast those on a device
+  ./batch_check_permission.sh ~/Downloads/apk_bundle_dir ~/Downloads/override.csv
+'''
+}
+
+SECONDS=0
+MY_NAME=$0
+SCRIPT_NAME="${MY_NAME##*/}"
+SCRIPT_DIR="${MY_NAME%/$SCRIPT_NAME}"
+echo Running from $SCRIPT_DIR
+
+if [[ -z $OUT_DIR ]]; then
+  OUT_DIR="${HOME}/Downloads"
+fi
+
+INPUT_DIR=$1
+if [[ -z "${INPUT_DIR}" ]]; then
+  readme
+  exit
+fi
+
+RENAME_CSV=$2
+# Read rename csv to create xmlRenameDic
+declare -A xmlRenameDic
+if [[ -f ${RENAME_CSV} ]]; then
+  while IFS=',' read -r name newName others || [ -n "${name}" ]; do
+    if [[ "${name}" == "name" ]]; then
+      # skip header
+      header="${name},${newName}"
+    else
+      xmlRenameDic["${name}"]="${newName}"
+    fi
+  done < $RENAME_CSV
+fi
+
+echo "LOG=${LOG}"
+log() {
+  if [[ -n ${LOG} ]]; then
+    echo $1
+  fi
+}
+
+echo "Listing xmls in ${INPUT_DIR}"
+declare -A relXmlDic
+
+declare -A relXmlDic="$(${SCRIPT_DIR}/get_file_dir.sh ${INPUT_DIR} xml)"
+echo "Found: ${#relXmlDic[@]} xmls"
+
+echo "Listing xmls in the device"
+declare -A deviceXmlDic
+deviceXmlList=$(adb shell "find / -name *.xml" 2>/dev/null)
+for xml in ${deviceXmlList}; do
+  file=${xml##*/}
+  fPath=${xml%/*}
+  fParentPathPostfix=${fPath:(-11)}
+  if [[ "permissions" == ${fParentPathPostfix} ]]; then
+    deviceXmlDic[${file}]=${xml}
+    log "${file} ${fPath} ${fParentPathPostfix}"
+  fi
+done
+echo "Found: ${#deviceXmlDic[@]} xmls"
+
+echo "Comparing xmls from ${INPUT_DIR} to those on the device."
+i=1
+for xml in "${!relXmlDic[@]}"; do
+  # relFile="...google/etc/permissions/privapp-permissions-car.xml"
+  relFile=${relXmlDic[$xml]}
+  # fPath="...google/etc/permissions"
+  fPath=${relFile%/*}
+  # fParentPathPostfix="permissions"
+  fParentPathPostfix=${fPath:(-11)}
+  log "${xml} ${fPath} ${fParentPathPostfix}"
+
+  # Only care about permissions
+  if [[ "permissions" == ${fParentPathPostfix} ]]; then
+    echo "$i Comparing permission file: $xml"
+
+    deviceFile=${deviceXmlDic[$xml]}
+    if [[ -z ${deviceFile} ]]; then
+      # Maybe it's renamed
+      newXml=${xmlRenameDic[$xml]}
+      log "Rename $xml to $newXml"
+      deviceFile=${deviceXmlDic[$newXml]}
+      if [[ -z ${deviceFile} ]]; then
+        echo "Error: no ${xml} on the device."
+        echo
+        i=$(($i + 1))
+        continue
+      fi
+    fi
+
+    # Pull the xml from device & diff
+    adb pull "${deviceFile}" "${OUT_DIR}/${xml}"
+    diff "${relXmlDic[$xml]}" "${OUT_DIR}/${xml}"
+    i=$(($i + 1))
+    echo
+  fi
+done
+echo "Took ${SECONDS} seconds"
diff --git a/validation/batch_install_app.sh b/validation/batch_install_app.sh
new file mode 100755
index 0000000..6e0cd97
--- /dev/null
+++ b/validation/batch_install_app.sh
@@ -0,0 +1,110 @@
+#!/bin/bash
+
+# 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.
+
+readme() {
+  echo '''
+Install apps in an app bundle release directory to the device via adb, e.g.
+./batch_install_app.sh /path/to/app_bundle /path/to/report
+
+Note: aapt is needed to get the metadata from APKs.
+'''
+}
+
+SECONDS=0
+MY_NAME=$0
+SCRIPT_NAME="${MY_NAME##*/}"
+SCRIPT_DIR="${MY_NAME%/$SCRIPT_NAME}"
+echo Running from $SCRIPT_DIR
+
+if [[ -z $OUT_DIR ]]; then
+  OUT_DIR="${HOME}/Downloads"
+fi
+
+INPUT_DIR=$1
+if [[ ! -d ${INPUT_DIR} ]]; then
+  echo "Error: ${INPUT_DIR} is not a directory."
+  readme
+  exit
+fi
+
+echo "LOG=${LOG}"
+log() {
+  if [[ -n ${LOG} ]]; then
+    echo $1
+  fi
+}
+
+# check an app/package version via adb, e.g.
+# checkAppVersion package_name
+checkAppVersion() {
+  pkg=$1
+  cmd="adb shell dumpsys package ${pkg}"
+  dump=$(${cmd})
+  log "$dump"
+  echo "${dump}" | grep versionName
+}
+
+echo "Process all APKs in ${INPUT_DIR}"
+# apkDic[apk_name]=apk_path
+declare -A apkDic="$(${SCRIPT_DIR}/get_file_dir.sh ${INPUT_DIR} apk)"
+echo "Found: ${#apkDic[@]} apks"
+
+screenshotDir="/data/local/tmp/screenshots"
+echo "Removig the following screenshots from the device"
+adb shell ls -l ${screenshotDir}
+adb shell rm -r ${screenshotDir}
+adb shell mkdir -p ${screenshotDir}
+
+# apkBadgingDic[apk_name]=aapt_badging_output_string
+declare -A apkBadgingDic
+# manifestDic[apk_name]=AndroidManifest_xml_content_string
+declare -A manifestDic
+i=1
+for apk in "${!apkDic[@]}"; do
+  path="${apkDic[${apk}]}"
+  badging=$(aapt dump badging ${path})
+  apkBadgingDic[${apk}]="\"${badging}\""
+  log "${apkBadgingDic[${apk}]}"
+
+  # Get package name from the aapt badging output string
+  # ... package: name='com.google.android.gsf' versionCode...
+  pkg0=${badging#package: name=\'}
+  pkg=${pkg0%\' versionCode*}
+
+  echo "$i,${pkg},${apk},${path}"
+  checkAppVersion ${pkg}
+  ${SCRIPT_DIR}/install_apk.sh ${path}
+  checkAppVersion ${pkg}
+  echo
+
+  # Get the 1st launchable activity
+  # ... launchable-activity: name='com.google.android.maps.MapsActivity'  label...
+  if [[ "$badging" == *"launchable-activity: name="* ]]; then
+    activity0=${badging#*launchable-activity: name=\'}
+    activity=${activity0%%\'  label=*}
+    echo "Launching an activity: ${activity}"
+    adb shell am start -n "${pkg}/${activity}"
+    sleep 5
+    adb shell screencap "${screenshotDir}/${pkg}.png"
+    echo "grep screen"
+  fi
+
+  i=$(($i + 1))
+done
+
+adb shell ls -l ${screenshotDir}
+adb pull ${screenshotDir} ${OUT_DIR}
+echo "Took ${SECONDS} seconds"
diff --git a/validation/get_file_dir.sh b/validation/get_file_dir.sh
new file mode 100755
index 0000000..58ee16d
--- /dev/null
+++ b/validation/get_file_dir.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+# 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.
+
+# Get a dictionary of all the file_extension files in a directory, e.g.
+# declare -a aDic="$(.\get_file_dir.sh directory file_extension)"
+declare -A aDic
+
+dir=$1
+ext=$2
+if [[ -d ${dir} ]]; then
+  cd ${dir} && fList=$(find "${dir}" -name "*.${ext}")
+
+  echo '( \'
+  for f in ${fList}; do
+    fileName=${f##*/}
+    echo "[${fileName}]=\"$f\" \\"
+  done
+  echo ')'
+fi
diff --git a/validation/install_apk.sh b/validation/install_apk.sh
new file mode 100755
index 0000000..00a059c
--- /dev/null
+++ b/validation/install_apk.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+# 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.
+
+# install an apk via adb, e.g.
+# ./install_app.sh apk_path
+declare -A aDic
+
+apk=$1
+if [[ -f ${apk} ]]; then
+  echo "Installing ${apk}"
+  cmd="adb install -r ${apk}"
+  eval "${cmd}"
+else
+  echo "Error: Can not find ${apk}"
+fi
diff --git a/validation/test.sh b/validation/test.sh
new file mode 100755
index 0000000..5a9bf61
--- /dev/null
+++ b/validation/test.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+# 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.
+
+echo test install new app release
+
+appDir=$1
+renameCsv=$2
+
+echo ${appDir}
+echo ${renameCsv}
+cat ${renameCsv}
+adb shell getprop | grep finger
+
+./batch_install_app.sh ${appDir}
+./batch_check_permission.sh ${appDir} ${renameCsv}