Init the App metadata bundles

Bug: 287487923
Test: N/A
Change-Id: I27dd83fe44b947dbfb2742fd3a208f9a25bbe0d0
diff --git a/tools/app_metadata_bundles/Android.bp b/tools/app_metadata_bundles/Android.bp
new file mode 100644
index 0000000..5f7589d
--- /dev/null
+++ b/tools/app_metadata_bundles/Android.bp
@@ -0,0 +1,31 @@
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "frameworks_base_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["frameworks_base_license"],
+}
+
+java_library_host {
+    name: "asllib",
+    srcs: [
+        "src/lib/java/**/*.java",
+    ],
+    target: {
+        windows: {
+            enabled: true,
+        },
+    },
+}
+
+java_binary_host {
+    name: "aslgen",
+    manifest: "src/aslgen/aslgen.mf",
+    srcs: [
+        "src/aslgen/java/**/*.java",
+    ],
+    static_libs: [
+        "asllib",
+    ],
+}
diff --git a/tools/app_metadata_bundles/OWNERS b/tools/app_metadata_bundles/OWNERS
new file mode 100644
index 0000000..a2a250b
--- /dev/null
+++ b/tools/app_metadata_bundles/OWNERS
@@ -0,0 +1,2 @@
+wenhaowang@google.com
+mloh@google.com
diff --git a/tools/app_metadata_bundles/README.md b/tools/app_metadata_bundles/README.md
new file mode 100644
index 0000000..6e8d287
--- /dev/null
+++ b/tools/app_metadata_bundles/README.md
@@ -0,0 +1,9 @@
+# App metadata bundles
+
+This project delivers a comprehensive toolchain solution for developers
+to efficiently manage app metadata bundles.
+
+The project consists of two subprojects:
+
+  * A pure Java library, and
+  * A pure Java command-line tool.
diff --git a/tools/app_metadata_bundles/src/aslgen/aslgen.mf b/tools/app_metadata_bundles/src/aslgen/aslgen.mf
new file mode 100644
index 0000000..fc656e2
--- /dev/null
+++ b/tools/app_metadata_bundles/src/aslgen/aslgen.mf
@@ -0,0 +1 @@
+Main-Class: com.android.aslgen.Main
\ No newline at end of file
diff --git a/tools/app_metadata_bundles/src/aslgen/java/com/android/aslgen/Main.java b/tools/app_metadata_bundles/src/aslgen/java/com/android/aslgen/Main.java
new file mode 100644
index 0000000..bf906ee
--- /dev/null
+++ b/tools/app_metadata_bundles/src/aslgen/java/com/android/aslgen/Main.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+package com.android.aslgen;
+
+import com.android.asllib.AndroidSafetyLabel;
+import com.android.asllib.AndroidSafetyLabel.Format;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class Main {
+
+    /**
+     * Takes the options to make file conversion.
+     */
+    public static void main(String[] args) throws IOException {
+
+        String inFile = null;
+        String outFile = null;
+        Format inFormat = Format.NULL;
+        Format outFormat = Format.NULL;
+
+
+        // Except for "--help", all arguments require a value currently.
+        // So just make sure we have an even number and
+        // then process them all two at a time.
+        if (args.length == 1 && "--help".equals(args[0])) {
+            showUsage();
+            return;
+        }
+        if (args.length % 2 != 0) {
+            throw new IllegalArgumentException("Argument is missing corresponding value");
+        }
+        for (int i = 0; i < args.length - 1; i += 2) {
+            final String arg = args[i].trim();
+            final String argValue = args[i + 1].trim();
+            if ("--in-path".equals(arg)) {
+                inFile = argValue;
+            } else if ("--out-path".equals(arg)) {
+                outFile = argValue;
+            } else if ("--in-format".equals(arg)) {
+                inFormat = getFormat(argValue);
+            } else if ("--out-format".equals(arg)) {
+                outFormat = getFormat(argValue);
+            } else {
+                throw new IllegalArgumentException("Unknown argument: " + arg);
+            }
+        }
+
+        if (inFile == null) {
+            throw new IllegalArgumentException("input file is required");
+        }
+
+        if (outFile == null) {
+            throw new IllegalArgumentException("output file is required");
+        }
+
+        if (inFormat == Format.NULL) {
+            throw new IllegalArgumentException("input format is required");
+        }
+
+        if (outFormat == Format.NULL) {
+            throw new IllegalArgumentException("output format is required");
+        }
+
+
+        System.out.println("in path: " + inFile);
+        System.out.println("out path: " + outFile);
+        System.out.println("in format: " + inFormat);
+        System.out.println("out format: " + outFormat);
+
+        var asl = AndroidSafetyLabel.readFromStream(new FileInputStream(inFile),
+                Format.HUMAN_READABLE);
+        asl.writeToStream(new FileOutputStream(outFile), Format.ON_DEVICE);
+    }
+
+    private static Format getFormat(String argValue) {
+        if ("hr".equals(argValue)) {
+            return Format.HUMAN_READABLE;
+        } else if ("od".equals(argValue)) {
+            return Format.ON_DEVICE;
+        } else {
+            return Format.NULL;
+        }
+    }
+
+    private static void showUsage() {
+        AndroidSafetyLabel.test();
+        System.err.println(
+                "Usage:\n"
+        );
+    }
+}
diff --git a/tools/app_metadata_bundles/src/lib/java/com/android/asllib/AndroidSafetyLabel.java b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/AndroidSafetyLabel.java
new file mode 100644
index 0000000..0d13a0f
--- /dev/null
+++ b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/AndroidSafetyLabel.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+package com.android.asllib;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+
+public class AndroidSafetyLabel {
+
+    public enum Format {
+        NULL, HUMAN_READABLE, ON_DEVICE;
+    }
+
+    /**
+     * Reads a {@link AndroidSafetyLabel} from an {@link InputStream}.
+     */
+    public static AndroidSafetyLabel readFromStream(InputStream in, Format format)
+            throws IOException {
+        System.out.println(format);
+        var br = new BufferedReader(new InputStreamReader(in));
+        String line;
+        while ((line = br.readLine()) != null) {
+            System.out.println(line);
+        }
+        return new AndroidSafetyLabel();
+    }
+
+    /**
+     * Write the content of the {@link AndroidSafetyLabel} to a {@link OutputStream}.
+     */
+    public void writeToStream(OutputStream out, Format format) throws IOException {
+        var bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
+        bw.write("Just a Test");
+        bw.close();
+    }
+
+    public static void test() {
+        System.out.println("test lib");
+    }
+}
diff --git a/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataCategory.java b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataCategory.java
new file mode 100644
index 0000000..35ec68d
--- /dev/null
+++ b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataCategory.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+package com.android.asllib;
+
+import java.util.Map;
+
+/**
+ * Data usage category representation containing one or more {@link DataType}. Valid category keys
+ * are defined in {@link DataCategoryConstants}, each category has a valid set of types {@link
+ * DataType}, which are mapped in {@link DataTypeConstants}
+ */
+public class DataCategory {
+    private final Map<String, DataType> mDataTypes;
+
+    private DataCategory(Map<String, DataType> dataTypes) {
+        this.mDataTypes = dataTypes;
+    }
+
+    /** Return the type {@link Map} of String type key to {@link DataType} */
+
+    public Map<String, DataType> getDataTypes() {
+        return mDataTypes;
+    }
+}
diff --git a/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataCategoryConstants.java b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataCategoryConstants.java
new file mode 100644
index 0000000..1925c28
--- /dev/null
+++ b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataCategoryConstants.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+package com.android.asllib;
+
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Constants for determining valid {@link String} data types for usage within {@link SafetyLabel},
+ * {@link DataCategory}, and {@link DataType}
+ */
+public class DataCategoryConstants {
+
+    public static final String CATEGORY_PERSONAL = "personal";
+    public static final String CATEGORY_FINANCIAL = "financial";
+    public static final String CATEGORY_LOCATION = "location";
+    public static final String CATEGORY_EMAIL_TEXT_MESSAGE = "email_text_message";
+    public static final String CATEGORY_PHOTO_VIDEO = "photo_video";
+    public static final String CATEGORY_AUDIO = "audio";
+    public static final String CATEGORY_STORAGE = "storage";
+    public static final String CATEGORY_HEALTH_FITNESS = "health_fitness";
+    public static final String CATEGORY_CONTACTS = "contacts";
+    public static final String CATEGORY_CALENDAR = "calendar";
+    public static final String CATEGORY_IDENTIFIERS = "identifiers";
+    public static final String CATEGORY_APP_PERFORMANCE = "app_performance";
+    public static final String CATEGORY_ACTIONS_IN_APP = "actions_in_app";
+    public static final String CATEGORY_SEARCH_AND_BROWSING = "search_and_browsing";
+
+    /** Set of valid categories */
+    public static final Set<String> VALID_CATEGORIES =
+            Collections.unmodifiableSet(
+                    new HashSet<>(
+                            Arrays.asList(
+                                    CATEGORY_PERSONAL,
+                                    CATEGORY_FINANCIAL,
+                                    CATEGORY_LOCATION,
+                                    CATEGORY_EMAIL_TEXT_MESSAGE,
+                                    CATEGORY_PHOTO_VIDEO,
+                                    CATEGORY_AUDIO,
+                                    CATEGORY_STORAGE,
+                                    CATEGORY_HEALTH_FITNESS,
+                                    CATEGORY_CONTACTS,
+                                    CATEGORY_CALENDAR,
+                                    CATEGORY_IDENTIFIERS,
+                                    CATEGORY_APP_PERFORMANCE,
+                                    CATEGORY_ACTIONS_IN_APP,
+                                    CATEGORY_SEARCH_AND_BROWSING)));
+
+    /** Returns {@link Set} of valid {@link String} category keys */
+    public static Set<String> getValidDataCategories() {
+        return VALID_CATEGORIES;
+    }
+
+    private DataCategoryConstants() {
+        /* do nothing - hide constructor */
+    }
+}
diff --git a/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataLabel.java b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataLabel.java
new file mode 100644
index 0000000..dc8f5cc2
--- /dev/null
+++ b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataLabel.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+package com.android.asllib;
+
+import java.util.Map;
+
+/**
+ * Data label representation with data shared and data collected maps containing zero or more
+ * {@link DataCategory}
+ */
+public class DataLabel {
+    private final Map<String, DataCategory> mDataAccessed;
+    private final Map<String, DataCategory> mDataCollected;
+    private final Map<String, DataCategory> mDataShared;
+
+    public DataLabel(
+            Map<String, DataCategory> dataAccessed,
+            Map<String, DataCategory> dataCollected,
+            Map<String, DataCategory> dataShared) {
+        mDataAccessed = dataAccessed;
+        mDataCollected = dataCollected;
+        mDataShared = dataShared;
+    }
+
+    /**
+     * Returns the data accessed {@link Map} of {@link
+     * com.android.asllib.DataCategoryConstants} to {@link DataCategory}
+     */
+    public Map<String, DataCategory> getDataAccessed() {
+        return mDataAccessed;
+    }
+
+    /**
+     * Returns the data collected {@link Map} of {@link
+     * com.android.asllib.DataCategoryConstants} to {@link DataCategory}
+     */
+    public Map<String, DataCategory> getDataCollected() {
+        return mDataCollected;
+    }
+
+    /**
+     * Returns the data shared {@link Map} of {@link
+     * com.android.asllib.DataCategoryConstants} to {@link DataCategory}
+     */
+    public Map<String, DataCategory> getDataShared() {
+        return mDataShared;
+    }
+}
diff --git a/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataType.java b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataType.java
new file mode 100644
index 0000000..1601d15
--- /dev/null
+++ b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/DataType.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+package com.android.asllib;
+
+import java.util.Set;
+
+/**
+ * Data usage type representation. Types are specific to a {@link DataCategory} and contains
+ * metadata related to the data usage purpose.
+ */
+public class DataType {
+
+    private final Set<Integer> mPurposeSet;
+    private final Boolean mIsCollectionOptional;
+    private final Boolean mIsSharingOptional;
+    private final Boolean mEphemeral;
+
+    private DataType(
+            Set<Integer> purposeSet,
+            Boolean isCollectionOptional,
+            Boolean isSharingOptional,
+            Boolean ephemeral) {
+        this.mPurposeSet = purposeSet;
+        this.mIsCollectionOptional = isCollectionOptional;
+        this.mIsSharingOptional = isSharingOptional;
+        this.mEphemeral = ephemeral;
+    }
+
+    /**
+     * Returns {@link Set} of valid {@link Integer} purposes for using the associated data category
+     * and type
+     */
+    public Set<Integer> getPurposeSet() {
+        return mPurposeSet;
+    }
+
+    /**
+     * For data-collected, returns {@code true} if data usage is user optional and {@code false} if
+     * data usage is required. Should return {@code null} for data-accessed and data-shared.
+     */
+    public Boolean getIsCollectionOptional() {
+        return mIsCollectionOptional;
+    }
+
+    /**
+     * For data-shared, returns {@code true} if data usage is user optional and {@code false} if
+     * data usage is required. Should return {@code null} for data-accessed and data-collected.
+     */
+    public Boolean getIsSharingOptional() {
+        return mIsSharingOptional;
+    }
+
+    /**
+     * For data-collected, returns {@code true} if data usage is user optional and {@code false} if
+     * data usage is processed ephemerally. Should return {@code null} for data-shared.
+     */
+    public Boolean getEphemeral() {
+        return mEphemeral;
+    }
+}
+
diff --git a/tools/app_metadata_bundles/src/lib/java/com/android/asllib/SafetyLabel.java b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/SafetyLabel.java
new file mode 100644
index 0000000..8d8f0bb
--- /dev/null
+++ b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/SafetyLabel.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+package com.android.asllib;
+
+
+/** Safety Label representation containing zero or more {@link DataCategory} for data shared */
+public class SafetyLabel {
+
+    private final long mVersion;
+    private final DataLabel mDataLabel;
+
+    private SafetyLabel(long version, DataLabel dataLabel) {
+        this.mVersion = version;
+        this.mDataLabel = dataLabel;
+    }
+
+    /** Returns the data label for the safety label */
+    public DataLabel getDataLabel() {
+        return mDataLabel;
+    }
+
+    public long getVersion() {
+        return mVersion;
+    }
+}
+