Merge remote-tracking branch 'goog/androidx-platform-dev' into sc-dev

* goog/androidx-platform-dev:
  Sync from upstream.
  Sync from upstream.
  Sync from upstream.

Bug: 185822878
Bug: 186035252
Bug: 182958600
Bug: 186033076
Bug: 185804196
Test: Presubmit
Change-Id: Id3f2a621f66f1fec8414d1c6e28694622bcc461c
diff --git a/Android.bp b/Android.bp
index 7982c4f..87ed49e 100644
--- a/Android.bp
+++ b/Android.bp
@@ -12,18 +12,21 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-java_library {
-    name: "icing-java-proto-lite",
-    proto: {
-        // If you change the proto type, also update jarjar-rules.txt to match
-        // the corresponding proto lib's package path.
-        type: "lite",
-        include_dirs: ["external/protobuf/src"],
-        canonical_path_from_root: false,
-    },
-    srcs: ["icing/proto/*.proto"],
-    jarjar_rules: "jarjar-rules.txt",
-    sdk_version: "core_current",
+package {
+    default_applicable_licenses: ["external_icing_license"],
+}
+
+// Added automatically by a large-scale-change
+// See: http://go/android-license-faq
+license {
+    name: "external_icing_license",
+    visibility: [":__subpackages__"],
+    license_kinds: [
+        "SPDX-license-identifier-Apache-2.0",
+    ],
+    license_text: [
+        "LICENSE",
+    ],
 }
 
 cc_defaults {
@@ -46,6 +49,7 @@
         "-Wno-undefined-var-template",
         "-Wno-unused-function",
         "-Wno-unused-parameter",
+        "-Wno-unused-private-field",
         "-Wno-extern-c-compat",
 
         "-funsigned-char",
@@ -53,35 +57,25 @@
     ],
 }
 
-cc_library_static {
-    name: "icing-c-proto",
-    defaults: ["libicing_defaults"],
-    proto: {
-        type: "lite",
-        // Find protos relative from where they're specified (useful for external protos)
-        canonical_path_from_root: false,
-        // Need to be able to see the .pb.h files that are generated
-        export_proto_headers: true,
-    },
-    srcs: ["icing/**/*.proto"],
-}
-
 cc_library_shared {
-    name: "libicing_jni",
+    name: "libicing",
     defaults: ["libicing_defaults"],
     srcs: [
         "icing/**/*.cc",
     ],
     exclude_srcs: [
-        // Tests
+        "icing/**/*-test-*",
+        "icing/**/*-test.*",
         "icing/**/*_test.cc",
-        // Benchmarks
         "icing/**/*_benchmark.cc",
-        // Test-only related files (i.e. utils)
         "icing/testing/**/*",
-        // Tools for manual debugging/investigating
+        "icing/tokenization/reverse_jni/**/*",
+        "icing/tokenization/simple/**/*",
         "icing/tools/**/*",
+        "icing/transform/map/**/*",
+        "icing/transform/simple/**/*",
     ],
+    header_libs: ["jni_headers"],
     static_libs: [
         "icing-c-proto",
         "libutf",
diff --git a/icing/jni.lds b/icing/jni.lds
new file mode 100644
index 0000000..401682a
--- /dev/null
+++ b/icing/jni.lds
@@ -0,0 +1,10 @@
+VERS_1.0 {
+  # Export JNI symbols.
+  global:
+    Java_*;
+    JNI_OnLoad;
+
+  # Hide everything else
+  local:
+    *;
+};
diff --git a/jarjar-rules.txt b/jarjar-rules.txt
deleted file mode 100644
index 0f4292e..0000000
--- a/jarjar-rules.txt
+++ /dev/null
@@ -1 +0,0 @@
-rule com.google.protobuf.** com.google.android.icing.protobuf.@1
diff --git a/java/Android.bp b/java/Android.bp
index 6bc8836..7daeb0a 100644
--- a/java/Android.bp
+++ b/java/Android.bp
@@ -12,11 +12,22 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "external_icing_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["external_icing_license"],
+}
+
 java_library {
     name: "libicing-java",
     srcs: ["src/**/*.java"],
     static_libs: [
+        "androidx.annotation_annotation",
         "icing-java-proto-lite",
         "libprotobuf-java-lite",
     ],
+    apex_available: ["com.android.appsearch"],
 }
diff --git a/java/tests/instrumentation/Android.bp b/java/tests/instrumentation/Android.bp
index c941acf..2b680f0 100644
--- a/java/tests/instrumentation/Android.bp
+++ b/java/tests/instrumentation/Android.bp
@@ -12,15 +12,20 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "external_icing_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["external_icing_license"],
+}
+
 android_test {
     name: "IcingSearchEngineTest",
-
-    manifest: "AndroidManifest.xml",
-
     srcs: [
-        "src/**/*.java",
+        "src/com/google/android/icing/**/*.java",
     ],
-
     static_libs: [
         "androidx.test.ext.junit",
         "androidx.test.rules",
@@ -28,15 +33,25 @@
         "libicing-java",
         "icing-java-proto-lite",
     ],
+    jni_libs: ["libicing"],
+    test_suites: ["device-tests"],
+    platform_apis: true,
+    use_embedded_native_libs: true,
+}
 
-    jni_libs: [
-        "libicing_jni",
+android_test {
+    name: "AndroidXSmokeTest",
+    srcs: ["src/androidx/**/*.java"],
+    static_libs: [
+        "androidx.appsearch_appsearch",
+        "androidx.appsearch_appsearch_local_storage",
+        "androidx.test.ext.junit",
+        "androidx.test.runner",
+        "truth-prebuilt",
     ],
-
-    test_suites: [
-        "device-tests",
-    ],
-
+    jni_libs: ["libicing"],
+    plugins: ["androidx.appsearch_appsearch-compiler-plugin"],
+    test_suites: ["device-tests"],
     platform_apis: true,
     use_embedded_native_libs: true,
 }
diff --git a/java/tests/instrumentation/src/androidx/appsearch/smoketest/AppSearchSmokeTest.java b/java/tests/instrumentation/src/androidx/appsearch/smoketest/AppSearchSmokeTest.java
new file mode 100644
index 0000000..8fae104
--- /dev/null
+++ b/java/tests/instrumentation/src/androidx/appsearch/smoketest/AppSearchSmokeTest.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2020 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 androidx.appsearch.smoketest;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertEquals;
+
+import androidx.appsearch.app.AppSearchSchema;
+import androidx.appsearch.app.AppSearchSchema.PropertyConfig;
+import androidx.appsearch.app.AppSearchSchema.StringPropertyConfig;
+import androidx.appsearch.app.AppSearchSession;
+import androidx.appsearch.app.PutDocumentsRequest;
+import androidx.appsearch.app.SearchResult;
+import androidx.appsearch.app.SearchResults;
+import androidx.appsearch.app.SearchSpec;
+import androidx.appsearch.app.SetSchemaRequest;
+import androidx.appsearch.localstorage.LocalStorage;
+import androidx.appsearch.localstorage.LocalStorage.SearchContext;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.filters.SmallTest;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.List;
+
+@SmallTest
+public class AppSearchSmokeTest {
+    private AppSearchSession appSearch;
+
+    @Before
+    public void setUp() throws Exception {
+        appSearch =
+                LocalStorage.createSearchSession(
+                                new SearchContext.Builder(
+                                                ApplicationProvider.getApplicationContext())
+                                        .build())
+                        .get();
+        // Remove all data before test
+        appSearch.setSchema(new SetSchemaRequest.Builder().setForceOverride(true).build()).get();
+    }
+
+    @Test
+    public void smokeTest() throws Exception {
+        AppSearchSchema schema =
+                new AppSearchSchema.Builder("testType")
+                        .addProperty(
+                                new StringPropertyConfig.Builder("prop")
+                                        .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
+                                        .setIndexingType(
+                                                StringPropertyConfig.INDEXING_TYPE_PREFIXES)
+                                        .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
+                                        .build())
+                        .build();
+        appSearch.setSchema(new SetSchemaRequest.Builder().addSchemas(schema).build()).get();
+    }
+
+    @Test
+    public void smokeTestAnnotationProcessor() throws Exception {
+        appSearch
+                .setSchema(
+                        new SetSchemaRequest.Builder()
+                                .addDocumentClasses(TestDocument.class)
+                                .build())
+                .get();
+
+        TestDocument input = new TestDocument("uri1", "avocado");
+        appSearch
+                .put(new PutDocumentsRequest.Builder().addDocuments(input).build())
+                .get()
+                .checkSuccess();
+        SearchResults results =
+                appSearch.search(
+                        "av",
+                        new SearchSpec.Builder()
+                                .setTermMatch(SearchSpec.TERM_MATCH_PREFIX)
+                                .build());
+        List<SearchResult> page = results.getNextPage().get();
+        assertThat(page).hasSize(1);
+        SearchResult result = page.get(0);
+        assertThat(results.getNextPage().get()).isEmpty();
+
+        assertEquals("uri1", result.getDocument().getUri());
+        assertEquals("avocado", result.getDocument().getPropertyString("body"));
+        TestDocument output = result.getDocument().toDocumentClass(TestDocument.class);
+        assertEquals("uri1", output.getUri());
+        assertEquals("avocado", output.getBody());
+    }
+}
diff --git a/java/tests/instrumentation/src/androidx/appsearch/smoketest/TestDocument.java b/java/tests/instrumentation/src/androidx/appsearch/smoketest/TestDocument.java
new file mode 100644
index 0000000..089ff55
--- /dev/null
+++ b/java/tests/instrumentation/src/androidx/appsearch/smoketest/TestDocument.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2020 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 androidx.appsearch.smoketest;
+
+import androidx.appsearch.annotation.Document;
+import androidx.appsearch.app.AppSearchSchema.StringPropertyConfig;
+
+@Document
+public class TestDocument {
+    @Document.Uri private final String uri;
+
+    @Document.Property(indexingType = StringPropertyConfig.INDEXING_TYPE_PREFIXES)
+    private final String body;
+
+    TestDocument(String uri, String body) {
+        this.uri = uri;
+        this.body = body;
+    }
+
+    public String getUri() {
+        return uri;
+    }
+
+    public String getBody() {
+        return body;
+    }
+}
diff --git a/proto/Android.bp b/proto/Android.bp
new file mode 100644
index 0000000..4fb0c18
--- /dev/null
+++ b/proto/Android.bp
@@ -0,0 +1,46 @@
+// Copyright (C) 2019 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 {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "external_icing_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["external_icing_license"],
+}
+
+java_library {
+    name: "icing-java-proto-lite",
+    proto: {
+        type: "lite",
+        include_dirs: ["external/protobuf/src"],
+        canonical_path_from_root: false,
+    },
+    srcs: ["icing/proto/*.proto"],
+    sdk_version: "core_current",
+}
+
+cc_library_static {
+    name: "icing-c-proto",
+    defaults: ["libicing_defaults"],
+    proto: {
+        type: "lite",
+        // Find protos relative from where they're specified (useful for external protos)
+        canonical_path_from_root: false,
+        // Need to be able to see the .pb.h files that are generated
+        export_proto_headers: true,
+    },
+    srcs: ["icing/**/*.proto"],
+}