Merge changes Iac724187,I60dc7a01,Ia40c8e4a

* changes:
  gn2bp: Support //tools/grit:grit_sources
  gn2bp: Support write_native_libraries_java.py
  gn2bp: Support //build/android:build_config_gen
diff --git a/Cronet/Android.bp b/Cronet/Android.bp
index ec7158c..e699656 100644
--- a/Cronet/Android.bp
+++ b/Cronet/Android.bp
@@ -20,26 +20,28 @@
     name: "cronet_impl_native_java",
     jars: ["prebuilt/cronet_impl_native_java.jar"],
     visibility: ["//visibility:private"],
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
+    min_sdk_version: "29",
 }
 
 java_import {
     name: "cronet_impl_common_java",
     jars: ["prebuilt/cronet_impl_common_java.jar"],
     visibility: ["//visibility:private"],
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
+    min_sdk_version: "29",
 }
 
 java_import {
     name: "cronet_impl_platform_java",
     jars: ["prebuilt/cronet_impl_platform_java.jar"],
     visibility: ["//visibility:private"],
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
+    min_sdk_version: "29",
 }
 
 cc_prebuilt_library_shared {
     name: "libcronet.80.0.3986.0",
-    // STOPSHIP: Confirm if it is safe
     shared_libs: [
         "libandroid",
         "libc",
@@ -66,7 +68,8 @@
     strip: {
         none: true,
     },
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
+    min_sdk_version: "29",
 }
 
 genrule {
@@ -79,15 +82,11 @@
 }
 
 java_sdk_library {
-    name: "org.chromium.net.cronet",
+    name: "framework-cronet",
+    defaults: ["framework-module-defaults"],
     srcs: [
         ":cronet_api-src",
     ],
-    // Only public non-hide methods for now.
-    // TODO: Confirm target APIs with Cronet team.
-    api_packages: [
-        "org.chromium.net",
-    ],
     libs: [
         "androidx.annotation_annotation",
     ],
@@ -96,18 +95,9 @@
         "cronet_impl_native_java",
         "cronet_impl_platform_java",
     ],
-    // Add dependencies on files used by droiddoc_options.
-    droiddoc_option_files: [":cronet-api-current.txt"],
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
     unsafe_ignore_missing_latest_api: true,
     dist_group: "android",
-}
-
-// Make the current.txt available for use by the cts/tests/signature tests.
-// ========================================================================
-filegroup {
-    name: "cronet-api-current.txt",
-    srcs: [
-        "api/current.txt",
-    ],
+    // cronet is used as a shared library.
+    shared_library: true,
 }
diff --git a/Cronet/apex/Android.bp b/Cronet/apex/Android.bp
index 2c5b481..64362da 100644
--- a/Cronet/apex/Android.bp
+++ b/Cronet/apex/Android.bp
@@ -31,37 +31,18 @@
 apex_defaults {
     name: "CronetApexDefaultsEnabled",
     jni_libs: ["libcronet.80.0.3986.0"],
-    java_libs: ["org.chromium.net.cronet"],
+    java_libs: ["framework-cronet"],
 }
 
 apex_defaults {
     name: "CronetApexDefaultsDisabled",
 }
 
-apex_defaults {
-    name: "com.android.cronet-defaults",
-    compile_multilib: "both",
-    jni_libs: ["libcronet.80.0.3986.0"],
-    java_libs: ["org.chromium.net.cronet"],
-    key: "com.android.cronet.key",
-    certificate: ":com.android.cronet.certificate",
-    updatable: false,
-    generate_hashtree: false,
-
-    // Use a custom AndroidManifest.xml used for API targeting.
-    androidManifest: ":cronet-manifest.xml",
-}
-
-filegroup {
-    name: "cronet-manifest.xml",
-    srcs: [
-        "AndroidManifest.xml",
-    ],
-}
-
+// TODO: Remove cronet apex after com.android.cronet is removed from PRODUCT_PACKAGES
 apex {
     name: "com.android.cronet",
-    defaults: ["com.android.cronet-defaults"],
+    key: "com.android.cronet.key",
+    updatable: false,
     manifest: "manifest.json",
 }
 
@@ -70,8 +51,3 @@
     public_key: "com.android.cronet.avbpubkey",
     private_key: "com.android.cronet.pem",
 }
-
-android_app_certificate {
-     name: "com.android.cronet.certificate",
-     certificate: "com.android.cronet",
-}
diff --git a/Cronet/apex/testing/Android.bp b/Cronet/apex/testing/Android.bp
deleted file mode 100644
index a6859ff..0000000
--- a/Cronet/apex/testing/Android.bp
+++ /dev/null
@@ -1,29 +0,0 @@
-// Copyright 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 {
-    default_applicable_licenses: ["Android-Apache-2.0"],
-}
-
-apex_test {
-    name: "test_com.android.cronet",
-    visibility: [
-        "//system/apex/tests",
-    ],
-    defaults: ["com.android.cronet-defaults"],
-    manifest: "test_apex_manifest.json",
-    file_contexts: ":com.android.cronet-file_contexts",
-    // Test APEX, should never be installed
-    installable: false,
-}
diff --git a/Cronet/apex/testing/test_apex_manifest.json b/Cronet/apex/testing/test_apex_manifest.json
deleted file mode 100644
index 15aaead..0000000
--- a/Cronet/apex/testing/test_apex_manifest.json
+++ /dev/null
@@ -1,4 +0,0 @@
-{
-    "name": "com.android.cronet",
-    "version": 300000000
-}
\ No newline at end of file
diff --git a/Cronet/api/module-lib-current.txt b/Cronet/api/module-lib-current.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/Cronet/api/module-lib-current.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/Cronet/api/module-lib-removed.txt b/Cronet/api/module-lib-removed.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/Cronet/api/module-lib-removed.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/Cronet/tests/apitest/Android.bp b/Cronet/tests/apitest/Android.bp
index 9f1c007..e71c707 100644
--- a/Cronet/tests/apitest/Android.bp
+++ b/Cronet/tests/apitest/Android.bp
@@ -31,13 +31,13 @@
         "android.test.base",
         "android.test.mock",
         "androidx.annotation_annotation",
-        "org.chromium.net.cronet.stubs",
+        "framework-cronet",
     ],
 }
 
 android_test {
     name: "CronetApiTest",
-    platform_apis: true,
+    sdk_version: "test_current",
     test_suites: ["device-tests"],
     certificate: "platform",
     static_libs: [
diff --git a/Cronet/tests/apitest/AndroidManifest.xml b/Cronet/tests/apitest/AndroidManifest.xml
index 052ac8f..db0f0b3 100644
--- a/Cronet/tests/apitest/AndroidManifest.xml
+++ b/Cronet/tests/apitest/AndroidManifest.xml
@@ -25,7 +25,7 @@
 
     <application>
         <uses-library android:name="android.test.runner" />
-        <uses-library android:name="org.chromium.net.cronet" />
+        <uses-library android:name="framework-cronet" />
     </application>
 
     <instrumentation
diff --git a/Tethering/apex/Android.bp b/Tethering/apex/Android.bp
index 8cf46ef..c590850 100644
--- a/Tethering/apex/Android.bp
+++ b/Tethering/apex/Android.bp
@@ -54,6 +54,7 @@
     name: "com.android.tethering",
     defaults: [
         "ConnectivityApexDefaults",
+        "CronetApexDefaults",
         "r-launched-apex-module",
     ],
     compile_multilib: "both",
diff --git a/framework/src/android/net/ConnectivityManager.java b/framework/src/android/net/ConnectivityManager.java
index 547b4ba..b5c3c64 100644
--- a/framework/src/android/net/ConnectivityManager.java
+++ b/framework/src/android/net/ConnectivityManager.java
@@ -4080,7 +4080,7 @@
         }
     }
 
-    private class CallbackHandler extends Handler {
+    private static class CallbackHandler extends Handler {
         private static final String TAG = "ConnectivityManager.CallbackHandler";
         private static final boolean DBG = false;
 
@@ -4095,7 +4095,10 @@
         @Override
         public void handleMessage(Message message) {
             if (message.what == EXPIRE_LEGACY_REQUEST) {
-                expireRequest((NetworkCapabilities) message.obj, message.arg1);
+                // the sInstance can't be null because to send this message a ConnectivityManager
+                // instance must have been created prior to creating the thread on which this
+                // Handler is running.
+                sInstance.expireRequest((NetworkCapabilities) message.obj, message.arg1);
                 return;
             }