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

* goog/androidx-platform-dev:
  Update Icing from upstream.
  Update Icing from upstream.
  Update Icing from upstream.

Descriptions:
========================================================================
Add @UsedByNative annotations to native* methods in IcingSearchEngineImpl.
========================================================================
[Icing][version 3] Bump kVersion to 3
========================================================================
Make lite index magic dependent on `IcingSearchEngineOptions::build_property_existence_metadata_hits`
========================================================================
Add a flag in IcingSearchEngineOptions to control whether to build property existence metadata hits
========================================================================
Support `hasProperty(property_path)` in the advanced query language
========================================================================
Add PropertyExistenceIndexingHandler to index property existence metadata hit
========================================================================
[JoinIndex Improvement][11/x] Add IcingSearchEngine initialization unit test for switching join index
========================================================================
[JoinIndex Improvement][10/x] Change/Add IcingSearchEngine unit tests
========================================================================
[JoinIndex Improvement][9/x] Integrate QualifiedIdJoinIndexImplV2 with IcingSearchEngine
========================================================================
[JoinIndex Improvement][8/x] Integrate QualifiedIdJoinIndexImplV2 with JoinProcessor
========================================================================
[JoinIndex Improvement][8/x] Integrate QualifiedIdJoinIndexImplV2 with QualifiedIdJoinIndexingHandler
========================================================================
[JoinIndex Improvement][7/x] Create QualifiedIdJoinIndex interface
========================================================================
[JoinIndex Improvement][6.1/x] Unit test (Optimize)
========================================================================
[JoinIndex Improvement][6.0/x] Unit test (General, Put, GetIterator)
========================================================================
[JoinIndex Improvement][5.3/x] Implement Optimize
========================================================================
Remove accents from Greek letters in normalizer
========================================================================
Make arm emulator tests build-only.
========================================================================
[JoinIndex Improvement][5.2/x] Implement GetIterator
========================================================================
[JoinIndex Improvement][5.1/x] Implement Put
========================================================================
[JoinIndex Improvement][5.0/x] Branch QualifiedIdJoinIndex to QualifiedIdJoinIndexImplV2
========================================================================
[JoinIndex Improvement][4/x] Implement PostingListJoinDataAccessor
========================================================================
[JoinIndex Improvement][3/x] Implement PostingListJoinDataSerializer and DocumentIdToJoinInfo data type
========================================================================
[JoinIndex Improvement][2/x] Create NamespaceFingerprintIdentifier
========================================================================
[JoinIndex Improvement][1/x] Implement namespace_id_old_to_new in Compaction
========================================================================
Update test to also handle ICU 74 segmentation rules.
========================================================================
[Icing][Expand QueryStats][3/x] Add new fields into QueryStats (1)
========================================================================
[Icing][Expand QueryStats][2/x] Refactor QueryStatsProto
========================================================================
[Icing][Expand QueryStats][1/x] Publish DocHitInfoIterator CallStats
========================================================================
Add additional property filter tests
========================================================================
Deprecate hit_intersect_section_ids_mask in DocHitInfoIterator
========================================================================
Change default requires_full_emulation to False for portable_cc_test (third_party/icing/testing)
========================================================================
Cleanup Set requires_full_emulation to True for selective tests
========================================================================
Fix monkey test failures
========================================================================
Complete monkey test logic to change schema during monkey test runtime
========================================================================
Refactor monkey test to prepare for schema update
========================================================================
Fix the schema bug found by monkey test with seed 2551429844
========================================================================
Move set query stats to the very top of InternalSearch()
========================================================================
Apply section restriction only on leaf nodes
========================================================================
[6/n] Fix callsites in Icing that forgot to check libtextclassifier3::Status (Advanced query parser)
========================================================================
[5/n] Fix callsites in Icing that forgot to check libtextclassifier3::Status (PersistentHashMap)
========================================================================
[4/n] Fix callsites in Icing that forgot to check libtextclassifier3::Status (PostingListIntegerIndexSerializer)
========================================================================
[3/n] Fix callsites in Icing that forgot to check libtextclassifier3::Status (PostingListHitSerializer)
========================================================================
[2/n] Fix callsites in Icing that forgot to check libtextclassifier3::Status (Posting list storage)
========================================================================
[1/n] Fix callsites in Icing that forgot to check libtextclassifier3::Status (Non-functional changes)
========================================================================
Decouple section restriction data from iterators
========================================================================
Fix the crash when a schema type gets more indexable properties than allowed
========================================================================
Add a checker to verify the property data type matches the schema.
========================================================================
Change global std::string in i18n-utils to constexpr std::string_view.
========================================================================
Adjust LiteIndex sort at indexing check conditions.
========================================================================
Fix the build error in aosp/2856025
========================================================================

Bug: 305098009
Bug: 291130542
Bug: 275121148
Bug: 303239901
Bug: 301116242
Bug: 299321977
Bug: 300135897
Bug: 297549761
Bug: 309826655
Bug: 296349369
Bug: 302192690
Bug: 302609704
Bug: 301566713
Bug: 296938196

Change-Id: I957abd9da83e30106dac188677415bd68f64185b
Merged-In: I957abd9da83e30106dac188677415bd68f64185b
diff --git a/Android.bp b/Android.bp
index 82b7b59..747e493 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,51 +57,99 @@
 
         "-Bsymbolic",
     ],
+    apex_available: ["com.android.appsearch"],
 }
 
-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"],
-}
-
+// TODO(b/193244409): Use the filegroup libicing_test_common along with
+//                    libicing_defaults to build libicing.
 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",
     ],
     shared_libs: [
-        "libandroidicu",
+        "libicu",
         "liblog",
-        // TODO(b/147509515): We only need the full version for GzipStream. If we can remove
-        // that dependency, then we can just use libprotobuf-cpp-lite
-        "libprotobuf-cpp-full",
+        "libprotobuf-cpp-lite",
         "libz",
     ],
-
     version_script: "icing/jni.lds",
+    min_sdk_version: "Tiramisu",
+}
+
+filegroup {
+    name: "libicing_test_common",
+    // TODO(b/193244409): Utilize globs once all build errors are fixed.
+    srcs: [
+        "icing/absl_ports/*.cc",
+        "icing/file/**/*.cc",
+        "icing/index/**/*.cc",
+        "icing/legacy/**/*.cc",
+        "icing/portable/*.cc",
+        "icing/query/**/*.cc",
+        "icing/schema/*.cc",
+        "icing/scoring/**/*.cc",
+        "icing/store/*.cc",
+        "icing/testing/*.cc",
+        "icing/text_classifier/lib3/utils/base/*.cc",
+        "icing/text_classifier/lib3/utils/hash/*.cc",
+        "icing/tokenization/*.cc",
+        "icing/util/*.cc",
+    ],
+    // TODO(b/193244409): Remove after all the excluded tests are passing.
+    exclude_srcs: [
+        "icing/**/*_benchmark.cc",
+        "icing/**/*-jni-layer.cc",
+        "icing/index/**/*_test.cc",
+        "icing/legacy/**/*_test.cc",
+        "icing/query/**/*_test.cc",
+        "icing/scoring/**/*_test.cc",
+        "icing/store/*_test.cc",
+        "icing/testing/*test*.cc",
+        "icing/tokenization/*_test.cc",
+        "icing/util/*_test.cc",
+    ],
+}
+
+cc_test {
+    name: "libicing_tests",
+    defaults: ["libicing_defaults"],
+    test_suites: ["device_tests"],
+    // TODO(b/193244409): Implement globs for additional tests.
+    srcs: [
+        ":libicing_test_common",
+    ],
+    static_libs: [
+        "icing-c-proto",
+        "libgmock",
+        "libgoogle-benchmark",
+        "libgtest",
+    ],
+    shared_libs: [
+        "libicu",
+        "libicu_cts_stub",
+        "liblog",
+        "libprotobuf-cpp-lite",
+        "libz",
+    ],
 }
 
 // TODO(cassiewang): Add build rules and a TEST_MAPPING for cc_tests
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 37cb5fc..baef43b 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -4,9 +4,14 @@
       "name": "IcingSearchEngineTest"
     }
   ],
+  "hwasan-postsubmit": [
+    {
+      "name": "IcingSearchEngineTest"
+    }
+  ],
   "imports": [
     {
-      "path": "frameworks/base/apex/appsearch/service/java/com/android/server/appsearch"
+      "path": "packages/modules/AppSearch"
     }
   ]
 }
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..6133230 100644
--- a/java/Android.bp
+++ b/java/Android.bp
@@ -12,6 +12,15 @@
 // 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"],
@@ -19,4 +28,10 @@
         "icing-java-proto-lite",
         "libprotobuf-java-lite",
     ],
+    libs: [
+        "androidx.annotation_annotation",
+    ],
+    sdk_version: "current",
+    min_sdk_version: "Tiramisu",
+    apex_available: ["com.android.appsearch"],
 }
diff --git a/java/tests/instrumentation/Android.bp b/java/tests/instrumentation/Android.bp
index c941acf..587566a 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",
     ],
-
-    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/AndroidXSmokeTest.java b/java/tests/instrumentation/src/androidx/appsearch/smoketest/AndroidXSmokeTest.java
new file mode 100644
index 0000000..21f66c2
--- /dev/null
+++ b/java/tests/instrumentation/src/androidx/appsearch/smoketest/AndroidXSmokeTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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.GenericDocument;
+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.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.List;
+
+@RunWith(AndroidJUnit4.class)
+public class AndroidXSmokeTest {
+    private AppSearchSession appSearch;
+
+    @Before
+    public void setUp() throws Exception {
+        appSearch =
+                LocalStorage.createSearchSessionAsync(
+                                new SearchContext.Builder(
+                                                ApplicationProvider.getApplicationContext(),
+                                                "database")
+                                        .build())
+                        .get();
+        // Remove all data before test
+        appSearch.setSchemaAsync(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.setSchemaAsync(new SetSchemaRequest.Builder().addSchemas(schema).build()).get();
+    }
+
+    @Test
+    public void smokeTestAnnotationProcessor() throws Exception {
+        appSearch
+                .setSchemaAsync(
+                        new SetSchemaRequest.Builder()
+                                .addDocumentClasses(TestDocument.class)
+                                .build())
+                .get();
+
+        TestDocument input = new TestDocument("namespace", "id1", "avocado");
+        appSearch
+                .putAsync(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.getNextPageAsync().get();
+        assertThat(page).hasSize(1);
+        SearchResult result = page.get(0);
+        assertThat(results.getNextPageAsync().get()).isEmpty();
+
+        GenericDocument genericOutput = result.getGenericDocument();
+        assertEquals("id1", genericOutput.getId());
+        assertEquals("avocado", genericOutput.getPropertyString("body"));
+        TestDocument output = genericOutput.toDocumentClass(TestDocument.class);
+        assertEquals("id1", output.getId());
+        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..ebf32e4
--- /dev/null
+++ b/java/tests/instrumentation/src/androidx/appsearch/smoketest/TestDocument.java
@@ -0,0 +1,48 @@
+/*
+ * 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.Namespace private final String mNamespace;
+
+    @Document.Id private final String mId;
+
+    @Document.StringProperty(indexingType = StringPropertyConfig.INDEXING_TYPE_PREFIXES)
+    private final String mBody;
+
+    TestDocument(String namespace, String id, String body) {
+        mNamespace = namespace;
+        mId = id;
+        mBody = body;
+    }
+
+    public String getNamespace() {
+        return mNamespace;
+    }
+
+    public String getId() {
+        return mId;
+    }
+
+    public String getBody() {
+        return mBody;
+    }
+}
diff --git a/java/tests/instrumentation/src/com/google/android/icing/IcingSearchEngineTest.java b/java/tests/instrumentation/src/com/google/android/icing/IcingSearchEngineTest.java
index 1ed2d9a..2bbd621 100644
--- a/java/tests/instrumentation/src/com/google/android/icing/IcingSearchEngineTest.java
+++ b/java/tests/instrumentation/src/com/google/android/icing/IcingSearchEngineTest.java
@@ -139,26 +139,6 @@
   }
 
   @Test
-  public void testSetAndGetSchema() throws Exception {
-    assertStatusOk(icingSearchEngine.initialize().getStatus());
-
-    SchemaTypeConfigProto emailTypeConfig = createEmailTypeConfig();
-    SchemaProto schema = SchemaProto.newBuilder().addTypes(emailTypeConfig).build();
-    SetSchemaResultProto setSchemaResultProto =
-        icingSearchEngine.setSchema(schema, /*ignoreErrorsAndDeleteDocuments=*/ false);
-    assertStatusOk(setSchemaResultProto.getStatus());
-
-    GetSchemaResultProto getSchemaResultProto = icingSearchEngine.getSchema();
-    assertStatusOk(getSchemaResultProto.getStatus());
-    assertThat(getSchemaResultProto.getSchema()).isEqualTo(schema);
-
-    GetSchemaTypeResultProto getSchemaTypeResultProto =
-        icingSearchEngine.getSchemaType(emailTypeConfig.getSchemaType());
-    assertStatusOk(getSchemaTypeResultProto.getStatus());
-    assertThat(getSchemaTypeResultProto.getSchemaTypeConfig()).isEqualTo(emailTypeConfig);
-  }
-
-  @Test
   public void testPutAndGetDocuments() throws Exception {
     assertStatusOk(icingSearchEngine.initialize().getStatus());
 
diff --git a/proto/Android.bp b/proto/Android.bp
new file mode 100644
index 0000000..5849129
--- /dev/null
+++ b/proto/Android.bp
@@ -0,0 +1,51 @@
+// 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",
+    apex_available: [
+        "//apex_available:platform",
+        "com.android.appsearch",
+    ],
+}
+
+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"],
+    min_sdk_version: "Tiramisu",
+}