Camera: Update SDK metadata key/enum generation

- Flatten Key hierarchy
- Insert Keys into their respective containers instead of separate files
- Use ints instead of Enum types
- Insert enum ints into CameraMetadata
- Add @see cross-references between enum Keys and values.
- Add Javadoc to Keys and enum values.
- Map SCALER_AVAILABLE_FORMATS to int

Bug: 10345522
Change-Id: Ia9762b326b404c572de97c2c7814c4e2e0f3070d
diff --git a/camera/docs/metadata-generate b/camera/docs/metadata-generate
index 36df140..9b17b2c 100755
--- a/camera/docs/metadata-generate
+++ b/camera/docs/metadata-generate
@@ -31,7 +31,7 @@
 thisdir=$(cd "$(dirname "$0")"; pwd)
 fwkdir="$ANDROID_BUILD_TOP/frameworks/base/core/java/android/hardware/camera2/"
 ctsdir="$ANDROID_BUILD_TOP/cts/tests/tests/hardware/src/android/hardware/camera2/cts"
-
+outdir="$ANDROID_PRODUCT_OUT/obj/ETC/system-media-camera-docs_intermediates"
 out_files=()
 
 function relpath() {
@@ -50,6 +50,7 @@
 function gen_file_abs() {
     local in="$1"
     local out="$2"
+    local intermediates="$3"
 
     python $thisdir/metadata_parser_xml.py $thisdir/metadata_properties.xml $in $out
 
@@ -58,7 +59,9 @@
     if [[ $succ -eq 0 ]]
     then
         echo "OK: Generated $(relpath "$out")"
-        out_files+=$'\n'" $out"
+        if [[ "$intermediates" != "no" ]]; then
+          out_files+=$'\n'" $out"
+        fi
     else
         echo "FAIL: Errors while generating $(relpath "$out")" >& 2
     fi
@@ -96,15 +99,80 @@
     printf %s\\n "${git_directories[@]}" | sort | uniq
 }
 
+# Insert a file into the middle of another, starting at the line containing the
+# start delim and ending on the end delim, both of which are replaced
+function insert_file() {
+    local src_part="$1"
+    local dst_file="$2"
+    local start_delim="/*@O~"
+    local end_delim="~O@*/"
+
+    local start_line="$(grep -n -F "${start_delim}" "${dst_file}" | cut -d: -f1)"
+    local end_line="$(grep -n -F "${end_delim}" "${dst_file}" | cut -d: -f1)"
+
+    # Adjust cutoff points to use start/end line from inserted file
+    (( start_line-- ))
+    (( end_line++ ))
+
+    # Do some basic sanity checks
+
+    if [[ -z "$start_line" ]]; then
+       echo "No starting delimiter found in ${dst_file}" >& 2
+       echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2
+       return 1
+    fi
+
+    if [[ -z "$end_line" ]]; then
+       echo "No ending delimiter found in ${dst_file}" >& 2
+       echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2
+       return 1
+    fi
+
+    if [[ "$start_line" -ge "$end_line" ]]; then
+       echo "Starting delim later than ending delim: $start_line vs $end_line" >& 2
+       echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2
+       return 1
+    fi
+
+    local tmp_name=$(mktemp -t XXXXXXXX)
+
+    # Compose the three parts of the final file together
+
+    head -n "$start_line" "${dst_file}" > "${tmp_name}"
+    cat "${src_part}" >> "${tmp_name}"
+    tail -n "+${end_line}" "${dst_file}" >> "${tmp_name}"
+
+    # And replace the destination file with the new version
+
+    mv "${tmp_name}" "${dst_file}"
+    echo "OK: Inserted $(relpath "$src_part") into $(relpath "$dst_file")"
+    out_files+=$'\n'" $dst_file"
+}
+
 $thisdir/metadata-check-dependencies || exit 1
 $thisdir/metadata-validate $thisdir/metadata_properties.xml || exit 1
 $thisdir/metadata-parser-sanity-check || exit 1
+
+# Generate HTML properties documentation
 gen_file html.mako docs.html || exit 1
+
+# Generate C API headers and implementation
 gen_file camera_metadata_tag_info.mako ../src/camera_metadata_tag_info.c || exit 1
 gen_file camera_metadata_tags.mako ../include/system/camera_metadata_tags.h || exit 1
-gen_file_abs CaptureResultKeys.mako "$fwkdir/CaptureResultKeys.java" || exit 1
-gen_file_abs CaptureRequestKeys.mako "$fwkdir/CaptureRequestKeys.java" || exit 1
-gen_file_abs CameraPropertiesKeys.mako "$fwkdir/CameraPropertiesKeys.java" || exit 1
+
+# Generate Java API definitions
+mkdir -p "${outdir}"
+gen_file_abs CameraMetadataEnums.mako "$outdir/CameraMetadataEnums.java.part" no || exit 1
+gen_file_abs CameraPropertiesKeys.mako "$outdir/CameraPropertiesKeys.java.part" no || exit 1
+gen_file_abs CaptureRequestKeys.mako "$outdir/CaptureRequestKeys.java.part" no || exit 1
+gen_file_abs CaptureResultKeys.mako "$outdir/CaptureResultKeys.java.part" no || exit 1
+
+insert_file "$outdir/CameraMetadataEnums.java.part" "$fwkdir/CameraMetadata.java" || exit 1
+insert_file "$outdir/CameraPropertiesKeys.java.part" "$fwkdir/CameraProperties.java" || exit 1
+insert_file "$outdir/CaptureRequestKeys.java.part" "$fwkdir/CaptureRequest.java" || exit 1
+insert_file "$outdir/CaptureResultKeys.java.part" "$fwkdir/CaptureResult.java" || exit 1
+
+# Generate CTS tests
 gen_file_abs CameraPropertiesTest.mako "$ctsdir/CameraPropertiesTest.java" || exit 1
 
 echo ""